]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch3.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
82
83 package body Sem_Ch3 is
84
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
88
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
92 -- record type.
93
94 procedure Build_Derived_Type
95 (N : Node_Id;
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
114 -- the type).
115 --
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
118
119 procedure Build_Derived_Access_Type
120 (N : Node_Id;
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
126
127 procedure Build_Derived_Array_Type
128 (N : Node_Id;
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
134
135 procedure Build_Derived_Concurrent_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
142
143 procedure Build_Derived_Enumeration_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
150
151 procedure Build_Derived_Numeric_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
157
158 procedure Build_Derived_Private_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
167
168 procedure Build_Derived_Record_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
182
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
190
191 function Build_Discriminant_Constraints
192 (T : Entity_Id;
193 Def : Node_Id;
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
204
205 procedure Build_Discriminated_Subtype
206 (T : Entity_Id;
207 Def_Id : Entity_Id;
208 Elist : Elist_Id;
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
213 --
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
216 --
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 --
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
227
228 function Build_Scalar_Bound
229 (Bound : Node_Id;
230 Par_T : Entity_Id;
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
236
237 procedure Build_Underlying_Full_View
238 (N : Node_Id;
239 Typ : Entity_Id;
240 Par : Entity_Id);
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
247 -- view exchanges).
248
249 procedure Check_Access_Discriminant_Requires_Limited
250 (D : Node_Id;
251 Loc : Node_Id);
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
255
256 procedure Check_Anonymous_Access_Components
257 (Typ_Decl : Node_Id;
258 Typ : Entity_Id;
259 Prev : Entity_Id;
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
269
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
273
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
277
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
281
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
284
285 procedure Check_Or_Process_Discriminants
286 (N : Node_Id;
287 T : Entity_Id;
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
293 -- if any.
294
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
298
299 procedure Constant_Redeclaration
300 (Id : Entity_Id;
301 N : Node_Id;
302 T : out Entity_Id);
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
306
307 function Contain_Interface
308 (Iface : Entity_Id;
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
311
312 procedure Convert_Scalar_Bounds
313 (N : Node_Id;
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
316 Loc : Source_Ptr);
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
324
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
328
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
332
333 procedure Create_Constrained_Components
334 (Subt : Entity_Id;
335 Decl_Node : Node_Id;
336 Typ : Entity_Id;
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
346
347 function Constrain_Component_Type
348 (Comp : Entity_Id;
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
351 Typ : Entity_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
360
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
368
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
385
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
394 --
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
397 --
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
403 --
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree.
406 --
407 -- The last two arguments are used to create its external name if needed.
408
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
415
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
419
420 procedure Constrain_Discriminated_Type
421 (Def_Id : Entity_Id;
422 S : Node_Id;
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 -- of For_Access.
431
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
435
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
439
440 procedure Constrain_Index
441 (Index : Node_Id;
442 S : Node_Id;
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
445 Suffix : Character;
446 Suffix_Index : Nat);
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
452
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
455
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
459
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
464
465 procedure Decimal_Fixed_Point_Type_Declaration
466 (T : Entity_Id;
467 Def : Node_Id);
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
470
471 procedure Complete_Private_Subtype
472 (Priv : Entity_Id;
473 Full : Entity_Id;
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
479
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
490
491 procedure Derived_Standard_Character
492 (N : Node_Id;
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
497
498 procedure Derived_Type_Declaration
499 (T : Entity_Id;
500 N : Node_Id;
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
507
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
514
515 function Expand_To_Stored_Constraint
516 (Typ : Entity_Id;
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
521
522 function Find_Type_Of_Object
523 (Obj_Def : Node_Id;
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
527
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
530
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
536
537 function Inherit_Components
538 (N : Node_Id;
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
541 Is_Tagged : Boolean;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
548 --
549 -- N is the original derived type declaration
550 --
551 -- Is_Tagged is set if we are dealing with tagged types
552 --
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
555 --
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
559 --
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
562 --
563 -- which gets treated as
564 --
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
566 --
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
570 --
571 -- (Old_Component => New_Component),
572 --
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
578
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
582
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
588
589 function Is_Valid_Constraint_Kind
590 (T_Kind : Type_Kind;
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
594
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
597
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
601
602 procedure Ordinary_Fixed_Point_Type_Declaration
603 (T : Entity_Id;
604 Def : Node_Id);
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
607
608 procedure Prepare_Private_Subtype_Completion
609 (Id : Entity_Id;
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
615
616 procedure Process_Incomplete_Dependents
617 (N : Node_Id;
618 Full_T : Entity_Id;
619 Inc_T : Entity_Id);
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
623 -- incomplete type.
624
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
627 --
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
629 --
630 -- Full_T is N's defining identifier.
631 --
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
637 -- full type.
638 --
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
642
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
648
649 procedure Record_Type_Declaration
650 (T : Entity_Id;
651 N : Node_Id;
652 Prev : Entity_Id);
653 -- Process a record type declaration (for both untagged and tagged
654 -- records). Parameters T and N are exactly like in procedure
655 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
656 -- for this routine. If this is the completion of an incomplete type
657 -- declaration, Prev is the entity of the incomplete declaration, used for
658 -- cross-referencing. Otherwise Prev = T.
659
660 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
661 -- This routine is used to process the actual record type definition (both
662 -- for untagged and tagged records). Def is a record type definition node.
663 -- This procedure analyzes the components in this record type definition.
664 -- Prev_T is the entity for the enclosing record type. It is provided so
665 -- that its Has_Task flag can be set if any of the component have Has_Task
666 -- set. If the declaration is the completion of an incomplete type
667 -- declaration, Prev_T is the original incomplete type, whose full view is
668 -- the record type.
669
670 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
671 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
672 -- build a copy of the declaration tree of the parent, and we create
673 -- independently the list of components for the derived type. Semantic
674 -- information uses the component entities, but record representation
675 -- clauses are validated on the declaration tree. This procedure replaces
676 -- discriminants and components in the declaration with those that have
677 -- been created by Inherit_Components.
678
679 procedure Set_Fixed_Range
680 (E : Entity_Id;
681 Loc : Source_Ptr;
682 Lo : Ureal;
683 Hi : Ureal);
684 -- Build a range node with the given bounds and set it as the Scalar_Range
685 -- of the given fixed-point type entity. Loc is the source location used
686 -- for the constructed range. See body for further details.
687
688 procedure Set_Scalar_Range_For_Subtype
689 (Def_Id : Entity_Id;
690 R : Node_Id;
691 Subt : Entity_Id);
692 -- This routine is used to set the scalar range field for a subtype given
693 -- Def_Id, the entity for the subtype, and R, the range expression for the
694 -- scalar range. Subt provides the parent subtype to be used to analyze,
695 -- resolve, and check the given range.
696
697 procedure Set_Default_SSO (T : Entity_Id);
698 -- T is the entity for an array or record being declared. This procedure
699 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
700 -- to the setting of Opt.Default_SSO.
701
702 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
703 -- Create a new signed integer entity, and apply the constraint to obtain
704 -- the required first named subtype of this type.
705
706 procedure Set_Stored_Constraint_From_Discriminant_Constraint
707 (E : Entity_Id);
708 -- E is some record type. This routine computes E's Stored_Constraint
709 -- from its Discriminant_Constraint.
710
711 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
712 -- Check that an entity in a list of progenitors is an interface,
713 -- emit error otherwise.
714
715 -----------------------
716 -- Access_Definition --
717 -----------------------
718
719 function Access_Definition
720 (Related_Nod : Node_Id;
721 N : Node_Id) return Entity_Id
722 is
723 Anon_Type : Entity_Id;
724 Anon_Scope : Entity_Id;
725 Desig_Type : Entity_Id;
726 Enclosing_Prot_Type : Entity_Id := Empty;
727
728 begin
729 Check_SPARK_05_Restriction ("access type is not allowed", N);
730
731 if Is_Entry (Current_Scope)
732 and then Is_Task_Type (Etype (Scope (Current_Scope)))
733 then
734 Error_Msg_N ("task entries cannot have access parameters", N);
735 return Empty;
736 end if;
737
738 -- Ada 2005: For an object declaration the corresponding anonymous
739 -- type is declared in the current scope.
740
741 -- If the access definition is the return type of another access to
742 -- function, scope is the current one, because it is the one of the
743 -- current type declaration, except for the pathological case below.
744
745 if Nkind_In (Related_Nod, N_Object_Declaration,
746 N_Access_Function_Definition)
747 then
748 Anon_Scope := Current_Scope;
749
750 -- A pathological case: function returning access functions that
751 -- return access functions, etc. Each anonymous access type created
752 -- is in the enclosing scope of the outermost function.
753
754 declare
755 Par : Node_Id;
756
757 begin
758 Par := Related_Nod;
759 while Nkind_In (Par, N_Access_Function_Definition,
760 N_Access_Definition)
761 loop
762 Par := Parent (Par);
763 end loop;
764
765 if Nkind (Par) = N_Function_Specification then
766 Anon_Scope := Scope (Defining_Entity (Par));
767 end if;
768 end;
769
770 -- For the anonymous function result case, retrieve the scope of the
771 -- function specification's associated entity rather than using the
772 -- current scope. The current scope will be the function itself if the
773 -- formal part is currently being analyzed, but will be the parent scope
774 -- in the case of a parameterless function, and we always want to use
775 -- the function's parent scope. Finally, if the function is a child
776 -- unit, we must traverse the tree to retrieve the proper entity.
777
778 elsif Nkind (Related_Nod) = N_Function_Specification
779 and then Nkind (Parent (N)) /= N_Parameter_Specification
780 then
781 -- If the current scope is a protected type, the anonymous access
782 -- is associated with one of the protected operations, and must
783 -- be available in the scope that encloses the protected declaration.
784 -- Otherwise the type is in the scope enclosing the subprogram.
785
786 -- If the function has formals, The return type of a subprogram
787 -- declaration is analyzed in the scope of the subprogram (see
788 -- Process_Formals) and thus the protected type, if present, is
789 -- the scope of the current function scope.
790
791 if Ekind (Current_Scope) = E_Protected_Type then
792 Enclosing_Prot_Type := Current_Scope;
793
794 elsif Ekind (Current_Scope) = E_Function
795 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
796 then
797 Enclosing_Prot_Type := Scope (Current_Scope);
798 end if;
799
800 if Present (Enclosing_Prot_Type) then
801 Anon_Scope := Scope (Enclosing_Prot_Type);
802
803 else
804 Anon_Scope := Scope (Defining_Entity (Related_Nod));
805 end if;
806
807 -- For an access type definition, if the current scope is a child
808 -- unit it is the scope of the type.
809
810 elsif Is_Compilation_Unit (Current_Scope) then
811 Anon_Scope := Current_Scope;
812
813 -- For access formals, access components, and access discriminants, the
814 -- scope is that of the enclosing declaration,
815
816 else
817 Anon_Scope := Scope (Current_Scope);
818 end if;
819
820 Anon_Type :=
821 Create_Itype
822 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823
824 if All_Present (N)
825 and then Ada_Version >= Ada_2005
826 then
827 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
828 end if;
829
830 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
831 -- the corresponding semantic routine
832
833 if Present (Access_To_Subprogram_Definition (N)) then
834
835 -- Compiler runtime units are compiled in Ada 2005 mode when building
836 -- the runtime library but must also be compilable in Ada 95 mode
837 -- (when bootstrapping the compiler).
838
839 Check_Compiler_Unit ("anonymous access to subprogram", N);
840
841 Access_Subprogram_Declaration
842 (T_Name => Anon_Type,
843 T_Def => Access_To_Subprogram_Definition (N));
844
845 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
846 Set_Ekind
847 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
848 else
849 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
850 end if;
851
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
854
855 -- If the anonymous access is associated with a protected operation,
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
858
859 -- If the anonymous access itself is protected, a full type
860 -- declaratiton will be created for it, so that the equivalent
861 -- record type can be constructed. For further details, see
862 -- Replace_Anonymous_Access_To_Protected-Subprogram.
863
864 if Ekind (Current_Scope) = E_Protected_Type
865 and then not Protected_Present (Access_To_Subprogram_Definition (N))
866 then
867 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
868 end if;
869
870 return Anon_Type;
871 end if;
872
873 Find_Type (Subtype_Mark (N));
874 Desig_Type := Entity (Subtype_Mark (N));
875
876 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
877 Set_Etype (Anon_Type, Anon_Type);
878
879 -- Make sure the anonymous access type has size and alignment fields
880 -- set, as required by gigi. This is necessary in the case of the
881 -- Task_Body_Procedure.
882
883 if not Has_Private_Component (Desig_Type) then
884 Layout_Type (Anon_Type);
885 end if;
886
887 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
888 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
889 -- the null value is allowed. In Ada 95 the null value is never allowed.
890
891 if Ada_Version >= Ada_2005 then
892 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
893 else
894 Set_Can_Never_Be_Null (Anon_Type, True);
895 end if;
896
897 -- The anonymous access type is as public as the discriminated type or
898 -- subprogram that defines it. It is imported (for back-end purposes)
899 -- if the designated type is.
900
901 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
902
903 -- Ada 2005 (AI-231): Propagate the access-constant attribute
904
905 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
906
907 -- The context is either a subprogram declaration, object declaration,
908 -- or an access discriminant, in a private or a full type declaration.
909 -- In the case of a subprogram, if the designated type is incomplete,
910 -- the operation will be a primitive operation of the full type, to be
911 -- updated subsequently. If the type is imported through a limited_with
912 -- clause, the subprogram is not a primitive operation of the type
913 -- (which is declared elsewhere in some other scope).
914
915 if Ekind (Desig_Type) = E_Incomplete_Type
916 and then not From_Limited_With (Desig_Type)
917 and then Is_Overloadable (Current_Scope)
918 then
919 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
920 Set_Has_Delayed_Freeze (Current_Scope);
921 end if;
922
923 -- Ada 2005: If the designated type is an interface that may contain
924 -- tasks, create a Master entity for the declaration. This must be done
925 -- before expansion of the full declaration, because the declaration may
926 -- include an expression that is an allocator, whose expansion needs the
927 -- proper Master for the created tasks.
928
929 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
930 then
931 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
932 then
933 Build_Class_Wide_Master (Anon_Type);
934
935 -- Similarly, if the type is an anonymous access that designates
936 -- tasks, create a master entity for it in the current context.
937
938 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
939 then
940 Build_Master_Entity (Defining_Identifier (Related_Nod));
941 Build_Master_Renaming (Anon_Type);
942 end if;
943 end if;
944
945 -- For a private component of a protected type, it is imperative that
946 -- the back-end elaborate the type immediately after the protected
947 -- declaration, because this type will be used in the declarations
948 -- created for the component within each protected body, so we must
949 -- create an itype reference for it now.
950
951 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
952 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953
954 -- Similarly, if the access definition is the return result of a
955 -- function, create an itype reference for it because it will be used
956 -- within the function body. For a regular function that is not a
957 -- compilation unit, insert reference after the declaration. For a
958 -- protected operation, insert it after the enclosing protected type
959 -- declaration. In either case, do not create a reference for a type
960 -- obtained through a limited_with clause, because this would introduce
961 -- semantic dependencies.
962
963 -- Similarly, do not create a reference if the designated type is a
964 -- generic formal, because no use of it will reach the backend.
965
966 elsif Nkind (Related_Nod) = N_Function_Specification
967 and then not From_Limited_With (Desig_Type)
968 and then not Is_Generic_Type (Desig_Type)
969 then
970 if Present (Enclosing_Prot_Type) then
971 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
972
973 elsif Is_List_Member (Parent (Related_Nod))
974 and then Nkind (Parent (N)) /= N_Parameter_Specification
975 then
976 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
977 end if;
978
979 -- Finally, create an itype reference for an object declaration of an
980 -- anonymous access type. This is strictly necessary only for deferred
981 -- constants, but in any case will avoid out-of-scope problems in the
982 -- back-end.
983
984 elsif Nkind (Related_Nod) = N_Object_Declaration then
985 Build_Itype_Reference (Anon_Type, Related_Nod);
986 end if;
987
988 return Anon_Type;
989 end Access_Definition;
990
991 -----------------------------------
992 -- Access_Subprogram_Declaration --
993 -----------------------------------
994
995 procedure Access_Subprogram_Declaration
996 (T_Name : Entity_Id;
997 T_Def : Node_Id)
998 is
999 procedure Check_For_Premature_Usage (Def : Node_Id);
1000 -- Check that type T_Name is not used, directly or recursively, as a
1001 -- parameter or a return type in Def. Def is either a subtype, an
1002 -- access_definition, or an access_to_subprogram_definition.
1003
1004 -------------------------------
1005 -- Check_For_Premature_Usage --
1006 -------------------------------
1007
1008 procedure Check_For_Premature_Usage (Def : Node_Id) is
1009 Param : Node_Id;
1010
1011 begin
1012 -- Check for a subtype mark
1013
1014 if Nkind (Def) in N_Has_Etype then
1015 if Etype (Def) = T_Name then
1016 Error_Msg_N
1017 ("type& cannot be used before end of its declaration", Def);
1018 end if;
1019
1020 -- If this is not a subtype, then this is an access_definition
1021
1022 elsif Nkind (Def) = N_Access_Definition then
1023 if Present (Access_To_Subprogram_Definition (Def)) then
1024 Check_For_Premature_Usage
1025 (Access_To_Subprogram_Definition (Def));
1026 else
1027 Check_For_Premature_Usage (Subtype_Mark (Def));
1028 end if;
1029
1030 -- The only cases left are N_Access_Function_Definition and
1031 -- N_Access_Procedure_Definition.
1032
1033 else
1034 if Present (Parameter_Specifications (Def)) then
1035 Param := First (Parameter_Specifications (Def));
1036 while Present (Param) loop
1037 Check_For_Premature_Usage (Parameter_Type (Param));
1038 Param := Next (Param);
1039 end loop;
1040 end if;
1041
1042 if Nkind (Def) = N_Access_Function_Definition then
1043 Check_For_Premature_Usage (Result_Definition (Def));
1044 end if;
1045 end if;
1046 end Check_For_Premature_Usage;
1047
1048 -- Local variables
1049
1050 Formals : constant List_Id := Parameter_Specifications (T_Def);
1051 Formal : Entity_Id;
1052 D_Ityp : Node_Id;
1053 Desig_Type : constant Entity_Id :=
1054 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1055
1056 -- Start of processing for Access_Subprogram_Declaration
1057
1058 begin
1059 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1060
1061 -- Associate the Itype node with the inner full-type declaration or
1062 -- subprogram spec or entry body. This is required to handle nested
1063 -- anonymous declarations. For example:
1064
1065 -- procedure P
1066 -- (X : access procedure
1067 -- (Y : access procedure
1068 -- (Z : access T)))
1069
1070 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1071 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1072 N_Private_Type_Declaration,
1073 N_Private_Extension_Declaration,
1074 N_Procedure_Specification,
1075 N_Function_Specification,
1076 N_Entry_Body)
1077
1078 or else
1079 Nkind_In (D_Ityp, N_Object_Declaration,
1080 N_Object_Renaming_Declaration,
1081 N_Formal_Object_Declaration,
1082 N_Formal_Type_Declaration,
1083 N_Task_Type_Declaration,
1084 N_Protected_Type_Declaration))
1085 loop
1086 D_Ityp := Parent (D_Ityp);
1087 pragma Assert (D_Ityp /= Empty);
1088 end loop;
1089
1090 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1091
1092 if Nkind_In (D_Ityp, N_Procedure_Specification,
1093 N_Function_Specification)
1094 then
1095 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1096
1097 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1098 N_Object_Declaration,
1099 N_Object_Renaming_Declaration,
1100 N_Formal_Type_Declaration)
1101 then
1102 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1103 end if;
1104
1105 if Nkind (T_Def) = N_Access_Function_Definition then
1106 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1107 declare
1108 Acc : constant Node_Id := Result_Definition (T_Def);
1109
1110 begin
1111 if Present (Access_To_Subprogram_Definition (Acc))
1112 and then
1113 Protected_Present (Access_To_Subprogram_Definition (Acc))
1114 then
1115 Set_Etype
1116 (Desig_Type,
1117 Replace_Anonymous_Access_To_Protected_Subprogram
1118 (T_Def));
1119
1120 else
1121 Set_Etype
1122 (Desig_Type,
1123 Access_Definition (T_Def, Result_Definition (T_Def)));
1124 end if;
1125 end;
1126
1127 else
1128 Analyze (Result_Definition (T_Def));
1129
1130 declare
1131 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1132
1133 begin
1134 -- If a null exclusion is imposed on the result type, then
1135 -- create a null-excluding itype (an access subtype) and use
1136 -- it as the function's Etype.
1137
1138 if Is_Access_Type (Typ)
1139 and then Null_Exclusion_In_Return_Present (T_Def)
1140 then
1141 Set_Etype (Desig_Type,
1142 Create_Null_Excluding_Itype
1143 (T => Typ,
1144 Related_Nod => T_Def,
1145 Scope_Id => Current_Scope));
1146
1147 else
1148 if From_Limited_With (Typ) then
1149
1150 -- AI05-151: Incomplete types are allowed in all basic
1151 -- declarations, including access to subprograms.
1152
1153 if Ada_Version >= Ada_2012 then
1154 null;
1155
1156 else
1157 Error_Msg_NE
1158 ("illegal use of incomplete type&",
1159 Result_Definition (T_Def), Typ);
1160 end if;
1161
1162 elsif Ekind (Current_Scope) = E_Package
1163 and then In_Private_Part (Current_Scope)
1164 then
1165 if Ekind (Typ) = E_Incomplete_Type then
1166 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1167
1168 elsif Is_Class_Wide_Type (Typ)
1169 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1170 then
1171 Append_Elmt
1172 (Desig_Type, Private_Dependents (Etype (Typ)));
1173 end if;
1174 end if;
1175
1176 Set_Etype (Desig_Type, Typ);
1177 end if;
1178 end;
1179 end if;
1180
1181 if not (Is_Type (Etype (Desig_Type))) then
1182 Error_Msg_N
1183 ("expect type in function specification",
1184 Result_Definition (T_Def));
1185 end if;
1186
1187 else
1188 Set_Etype (Desig_Type, Standard_Void_Type);
1189 end if;
1190
1191 if Present (Formals) then
1192 Push_Scope (Desig_Type);
1193
1194 -- Some special tests here. These special tests can be removed
1195 -- if and when Itypes always have proper parent pointers to their
1196 -- declarations???
1197
1198 -- Special test 1) Link defining_identifier of formals. Required by
1199 -- First_Formal to provide its functionality.
1200
1201 declare
1202 F : Node_Id;
1203
1204 begin
1205 F := First (Formals);
1206
1207 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1208 -- when it is part of an unconstrained type and subtype expansion
1209 -- is disabled. To avoid back-end problems with shared profiles,
1210 -- use previous subprogram type as the designated type, and then
1211 -- remove scope added above.
1212
1213 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1214 then
1215 Set_Etype (T_Name, T_Name);
1216 Init_Size_Align (T_Name);
1217 Set_Directly_Designated_Type (T_Name,
1218 Scope (Defining_Identifier (F)));
1219 End_Scope;
1220 return;
1221 end if;
1222
1223 while Present (F) loop
1224 if No (Parent (Defining_Identifier (F))) then
1225 Set_Parent (Defining_Identifier (F), F);
1226 end if;
1227
1228 Next (F);
1229 end loop;
1230 end;
1231
1232 Process_Formals (Formals, Parent (T_Def));
1233
1234 -- Special test 2) End_Scope requires that the parent pointer be set
1235 -- to something reasonable, but Itypes don't have parent pointers. So
1236 -- we set it and then unset it ???
1237
1238 Set_Parent (Desig_Type, T_Name);
1239 End_Scope;
1240 Set_Parent (Desig_Type, Empty);
1241 end if;
1242
1243 -- Check for premature usage of the type being defined
1244
1245 Check_For_Premature_Usage (T_Def);
1246
1247 -- The return type and/or any parameter type may be incomplete. Mark the
1248 -- subprogram_type as depending on the incomplete type, so that it can
1249 -- be updated when the full type declaration is seen. This only applies
1250 -- to incomplete types declared in some enclosing scope, not to limited
1251 -- views from other packages.
1252
1253 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254
1255 if Present (Formals) then
1256 Formal := First_Formal (Desig_Type);
1257 while Present (Formal) loop
1258 if Ekind (Formal) /= E_In_Parameter
1259 and then Nkind (T_Def) = N_Access_Function_Definition
1260 and then Ada_Version < Ada_2012
1261 then
1262 Error_Msg_N ("functions can only have IN parameters", Formal);
1263 end if;
1264
1265 if Ekind (Etype (Formal)) = E_Incomplete_Type
1266 and then In_Open_Scopes (Scope (Etype (Formal)))
1267 then
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1270 end if;
1271
1272 Next_Formal (Formal);
1273 end loop;
1274 end if;
1275
1276 -- Check whether an indirect call without actuals may be possible. This
1277 -- is used when resolving calls whose result is then indexed.
1278
1279 May_Need_Actuals (Desig_Type);
1280
1281 -- If the return type is incomplete, this is legal as long as the type
1282 -- is declared in the current scope and will be completed in it (rather
1283 -- than being part of limited view).
1284
1285 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1286 and then not Has_Delayed_Freeze (Desig_Type)
1287 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1288 then
1289 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1290 Set_Has_Delayed_Freeze (Desig_Type);
1291 end if;
1292
1293 Check_Delayed_Subprogram (Desig_Type);
1294
1295 if Protected_Present (T_Def) then
1296 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1297 Set_Convention (Desig_Type, Convention_Protected);
1298 else
1299 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1300 end if;
1301
1302 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1303
1304 Set_Etype (T_Name, T_Name);
1305 Init_Size_Align (T_Name);
1306 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307
1308 Generate_Reference_To_Formals (T_Name);
1309
1310 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1311
1312 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1313
1314 Check_Restriction (No_Access_Subprograms, T_Def);
1315 end Access_Subprogram_Declaration;
1316
1317 ----------------------------
1318 -- Access_Type_Declaration --
1319 ----------------------------
1320
1321 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1322 P : constant Node_Id := Parent (Def);
1323 S : constant Node_Id := Subtype_Indication (Def);
1324
1325 Full_Desig : Entity_Id;
1326
1327 begin
1328 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1329
1330 -- Check for permissible use of incomplete type
1331
1332 if Nkind (S) /= N_Subtype_Indication then
1333 Analyze (S);
1334
1335 if Present (Entity (S))
1336 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1337 then
1338 Set_Directly_Designated_Type (T, Entity (S));
1339
1340 -- If the designated type is a limited view, we cannot tell if
1341 -- the full view contains tasks, and there is no way to handle
1342 -- that full view in a client. We create a master entity for the
1343 -- scope, which will be used when a client determines that one
1344 -- is needed.
1345
1346 if From_Limited_With (Entity (S))
1347 and then not Is_Class_Wide_Type (Entity (S))
1348 then
1349 Set_Ekind (T, E_Access_Type);
1350 Build_Master_Entity (T);
1351 Build_Master_Renaming (T);
1352 end if;
1353
1354 else
1355 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1356 end if;
1357
1358 -- If the access definition is of the form: ACCESS NOT NULL ..
1359 -- the subtype indication must be of an access type. Create
1360 -- a null-excluding subtype of it.
1361
1362 if Null_Excluding_Subtype (Def) then
1363 if not Is_Access_Type (Entity (S)) then
1364 Error_Msg_N ("null exclusion must apply to access type", Def);
1365
1366 else
1367 declare
1368 Loc : constant Source_Ptr := Sloc (S);
1369 Decl : Node_Id;
1370 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1371
1372 begin
1373 Decl :=
1374 Make_Subtype_Declaration (Loc,
1375 Defining_Identifier => Nam,
1376 Subtype_Indication =>
1377 New_Occurrence_Of (Entity (S), Loc));
1378 Set_Null_Exclusion_Present (Decl);
1379 Insert_Before (Parent (Def), Decl);
1380 Analyze (Decl);
1381 Set_Entity (S, Nam);
1382 end;
1383 end if;
1384 end if;
1385
1386 else
1387 Set_Directly_Designated_Type (T,
1388 Process_Subtype (S, P, T, 'P'));
1389 end if;
1390
1391 if All_Present (Def) or Constant_Present (Def) then
1392 Set_Ekind (T, E_General_Access_Type);
1393 else
1394 Set_Ekind (T, E_Access_Type);
1395 end if;
1396
1397 Full_Desig := Designated_Type (T);
1398
1399 if Base_Type (Full_Desig) = T then
1400 Error_Msg_N ("access type cannot designate itself", S);
1401
1402 -- In Ada 2005, the type may have a limited view through some unit in
1403 -- its own context, allowing the following circularity that cannot be
1404 -- detected earlier.
1405
1406 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1407 then
1408 Error_Msg_N
1409 ("access type cannot designate its own class-wide type", S);
1410
1411 -- Clean up indication of tagged status to prevent cascaded errors
1412
1413 Set_Is_Tagged_Type (T, False);
1414 end if;
1415
1416 Set_Etype (T, T);
1417
1418 -- If the type has appeared already in a with_type clause, it is frozen
1419 -- and the pointer size is already set. Else, initialize.
1420
1421 if not From_Limited_With (T) then
1422 Init_Size_Align (T);
1423 end if;
1424
1425 -- Note that Has_Task is always false, since the access type itself
1426 -- is not a task type. See Einfo for more description on this point.
1427 -- Exactly the same consideration applies to Has_Controlled_Component
1428 -- and to Has_Protected.
1429
1430 Set_Has_Task (T, False);
1431 Set_Has_Protected (T, False);
1432 Set_Has_Timing_Event (T, False);
1433 Set_Has_Controlled_Component (T, False);
1434
1435 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1436 -- problems where an incomplete view of this entity has been previously
1437 -- established by a limited with and an overlaid version of this field
1438 -- (Stored_Constraint) was initialized for the incomplete view.
1439
1440 -- This reset is performed in most cases except where the access type
1441 -- has been created for the purposes of allocating or deallocating a
1442 -- build-in-place object. Such access types have explicitly set pools
1443 -- and finalization masters.
1444
1445 if No (Associated_Storage_Pool (T)) then
1446 Set_Finalization_Master (T, Empty);
1447 end if;
1448
1449 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1450 -- attributes
1451
1452 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1453 Set_Is_Access_Constant (T, Constant_Present (Def));
1454 end Access_Type_Declaration;
1455
1456 ----------------------------------
1457 -- Add_Interface_Tag_Components --
1458 ----------------------------------
1459
1460 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1461 Loc : constant Source_Ptr := Sloc (N);
1462 L : List_Id;
1463 Last_Tag : Node_Id;
1464
1465 procedure Add_Tag (Iface : Entity_Id);
1466 -- Add tag for one of the progenitor interfaces
1467
1468 -------------
1469 -- Add_Tag --
1470 -------------
1471
1472 procedure Add_Tag (Iface : Entity_Id) is
1473 Decl : Node_Id;
1474 Def : Node_Id;
1475 Tag : Entity_Id;
1476 Offset : Entity_Id;
1477
1478 begin
1479 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1480
1481 -- This is a reasonable place to propagate predicates
1482
1483 if Has_Predicates (Iface) then
1484 Set_Has_Predicates (Typ);
1485 end if;
1486
1487 Def :=
1488 Make_Component_Definition (Loc,
1489 Aliased_Present => True,
1490 Subtype_Indication =>
1491 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1492
1493 Tag := Make_Temporary (Loc, 'V');
1494
1495 Decl :=
1496 Make_Component_Declaration (Loc,
1497 Defining_Identifier => Tag,
1498 Component_Definition => Def);
1499
1500 Analyze_Component_Declaration (Decl);
1501
1502 Set_Analyzed (Decl);
1503 Set_Ekind (Tag, E_Component);
1504 Set_Is_Tag (Tag);
1505 Set_Is_Aliased (Tag);
1506 Set_Related_Type (Tag, Iface);
1507 Init_Component_Location (Tag);
1508
1509 pragma Assert (Is_Frozen (Iface));
1510
1511 Set_DT_Entry_Count (Tag,
1512 DT_Entry_Count (First_Entity (Iface)));
1513
1514 if No (Last_Tag) then
1515 Prepend (Decl, L);
1516 else
1517 Insert_After (Last_Tag, Decl);
1518 end if;
1519
1520 Last_Tag := Decl;
1521
1522 -- If the ancestor has discriminants we need to give special support
1523 -- to store the offset_to_top value of the secondary dispatch tables.
1524 -- For this purpose we add a supplementary component just after the
1525 -- field that contains the tag associated with each secondary DT.
1526
1527 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1528 Def :=
1529 Make_Component_Definition (Loc,
1530 Subtype_Indication =>
1531 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1532
1533 Offset := Make_Temporary (Loc, 'V');
1534
1535 Decl :=
1536 Make_Component_Declaration (Loc,
1537 Defining_Identifier => Offset,
1538 Component_Definition => Def);
1539
1540 Analyze_Component_Declaration (Decl);
1541
1542 Set_Analyzed (Decl);
1543 Set_Ekind (Offset, E_Component);
1544 Set_Is_Aliased (Offset);
1545 Set_Related_Type (Offset, Iface);
1546 Init_Component_Location (Offset);
1547 Insert_After (Last_Tag, Decl);
1548 Last_Tag := Decl;
1549 end if;
1550 end Add_Tag;
1551
1552 -- Local variables
1553
1554 Elmt : Elmt_Id;
1555 Ext : Node_Id;
1556 Comp : Node_Id;
1557
1558 -- Start of processing for Add_Interface_Tag_Components
1559
1560 begin
1561 if not RTE_Available (RE_Interface_Tag) then
1562 Error_Msg
1563 ("(Ada 2005) interface types not supported by this run-time!",
1564 Sloc (N));
1565 return;
1566 end if;
1567
1568 if Ekind (Typ) /= E_Record_Type
1569 or else (Is_Concurrent_Record_Type (Typ)
1570 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1571 or else (not Is_Concurrent_Record_Type (Typ)
1572 and then No (Interfaces (Typ))
1573 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1574 then
1575 return;
1576 end if;
1577
1578 -- Find the current last tag
1579
1580 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1581 Ext := Record_Extension_Part (Type_Definition (N));
1582 else
1583 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1584 Ext := Type_Definition (N);
1585 end if;
1586
1587 Last_Tag := Empty;
1588
1589 if not (Present (Component_List (Ext))) then
1590 Set_Null_Present (Ext, False);
1591 L := New_List;
1592 Set_Component_List (Ext,
1593 Make_Component_List (Loc,
1594 Component_Items => L,
1595 Null_Present => False));
1596 else
1597 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1598 L := Component_Items
1599 (Component_List
1600 (Record_Extension_Part
1601 (Type_Definition (N))));
1602 else
1603 L := Component_Items
1604 (Component_List
1605 (Type_Definition (N)));
1606 end if;
1607
1608 -- Find the last tag component
1609
1610 Comp := First (L);
1611 while Present (Comp) loop
1612 if Nkind (Comp) = N_Component_Declaration
1613 and then Is_Tag (Defining_Identifier (Comp))
1614 then
1615 Last_Tag := Comp;
1616 end if;
1617
1618 Next (Comp);
1619 end loop;
1620 end if;
1621
1622 -- At this point L references the list of components and Last_Tag
1623 -- references the current last tag (if any). Now we add the tag
1624 -- corresponding with all the interfaces that are not implemented
1625 -- by the parent.
1626
1627 if Present (Interfaces (Typ)) then
1628 Elmt := First_Elmt (Interfaces (Typ));
1629 while Present (Elmt) loop
1630 Add_Tag (Node (Elmt));
1631 Next_Elmt (Elmt);
1632 end loop;
1633 end if;
1634 end Add_Interface_Tag_Components;
1635
1636 -------------------------------------
1637 -- Add_Internal_Interface_Entities --
1638 -------------------------------------
1639
1640 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1641 Elmt : Elmt_Id;
1642 Iface : Entity_Id;
1643 Iface_Elmt : Elmt_Id;
1644 Iface_Prim : Entity_Id;
1645 Ifaces_List : Elist_Id;
1646 New_Subp : Entity_Id := Empty;
1647 Prim : Entity_Id;
1648 Restore_Scope : Boolean := False;
1649
1650 begin
1651 pragma Assert (Ada_Version >= Ada_2005
1652 and then Is_Record_Type (Tagged_Type)
1653 and then Is_Tagged_Type (Tagged_Type)
1654 and then Has_Interfaces (Tagged_Type)
1655 and then not Is_Interface (Tagged_Type));
1656
1657 -- Ensure that the internal entities are added to the scope of the type
1658
1659 if Scope (Tagged_Type) /= Current_Scope then
1660 Push_Scope (Scope (Tagged_Type));
1661 Restore_Scope := True;
1662 end if;
1663
1664 Collect_Interfaces (Tagged_Type, Ifaces_List);
1665
1666 Iface_Elmt := First_Elmt (Ifaces_List);
1667 while Present (Iface_Elmt) loop
1668 Iface := Node (Iface_Elmt);
1669
1670 -- Originally we excluded here from this processing interfaces that
1671 -- are parents of Tagged_Type because their primitives are located
1672 -- in the primary dispatch table (and hence no auxiliary internal
1673 -- entities are required to handle secondary dispatch tables in such
1674 -- case). However, these auxiliary entities are also required to
1675 -- handle derivations of interfaces in formals of generics (see
1676 -- Derive_Subprograms).
1677
1678 Elmt := First_Elmt (Primitive_Operations (Iface));
1679 while Present (Elmt) loop
1680 Iface_Prim := Node (Elmt);
1681
1682 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1683 Prim :=
1684 Find_Primitive_Covering_Interface
1685 (Tagged_Type => Tagged_Type,
1686 Iface_Prim => Iface_Prim);
1687
1688 if No (Prim) and then Serious_Errors_Detected > 0 then
1689 goto Continue;
1690 end if;
1691
1692 pragma Assert (Present (Prim));
1693
1694 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1695 -- differs from the name of the interface primitive then it is
1696 -- a private primitive inherited from a parent type. In such
1697 -- case, given that Tagged_Type covers the interface, the
1698 -- inherited private primitive becomes visible. For such
1699 -- purpose we add a new entity that renames the inherited
1700 -- private primitive.
1701
1702 if Chars (Prim) /= Chars (Iface_Prim) then
1703 pragma Assert (Has_Suffix (Prim, 'P'));
1704 Derive_Subprogram
1705 (New_Subp => New_Subp,
1706 Parent_Subp => Iface_Prim,
1707 Derived_Type => Tagged_Type,
1708 Parent_Type => Iface);
1709 Set_Alias (New_Subp, Prim);
1710 Set_Is_Abstract_Subprogram
1711 (New_Subp, Is_Abstract_Subprogram (Prim));
1712 end if;
1713
1714 Derive_Subprogram
1715 (New_Subp => New_Subp,
1716 Parent_Subp => Iface_Prim,
1717 Derived_Type => Tagged_Type,
1718 Parent_Type => Iface);
1719
1720 declare
1721 Anc : Entity_Id;
1722 begin
1723 if Is_Inherited_Operation (Prim)
1724 and then Present (Alias (Prim))
1725 then
1726 Anc := Alias (Prim);
1727 else
1728 Anc := Overridden_Operation (Prim);
1729 end if;
1730
1731 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1732 -- nonconforming preconditions in both an ancestor and
1733 -- a progenitor operation.
1734
1735 if Present (Anc)
1736 and then Has_Non_Trivial_Precondition (Anc)
1737 and then Has_Non_Trivial_Precondition (Iface_Prim)
1738 then
1739 if Is_Abstract_Subprogram (Prim)
1740 or else
1741 (Ekind (Prim) = E_Procedure
1742 and then Nkind (Parent (Prim)) =
1743 N_Procedure_Specification
1744 and then Null_Present (Parent (Prim)))
1745 then
1746 null;
1747
1748 -- The inherited operation must be overridden
1749
1750 elsif not Comes_From_Source (Prim) then
1751 Error_Msg_NE
1752 ("&inherits non-conforming preconditions and must "
1753 & "be overridden (RM 6.1.1 (10-16)",
1754 Parent (Tagged_Type), Prim);
1755 end if;
1756 end if;
1757 end;
1758
1759 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1760 -- associated with interface types. These entities are
1761 -- only registered in the list of primitives of its
1762 -- corresponding tagged type because they are only used
1763 -- to fill the contents of the secondary dispatch tables.
1764 -- Therefore they are removed from the homonym chains.
1765
1766 Set_Is_Hidden (New_Subp);
1767 Set_Is_Internal (New_Subp);
1768 Set_Alias (New_Subp, Prim);
1769 Set_Is_Abstract_Subprogram
1770 (New_Subp, Is_Abstract_Subprogram (Prim));
1771 Set_Interface_Alias (New_Subp, Iface_Prim);
1772
1773 -- If the returned type is an interface then propagate it to
1774 -- the returned type. Needed by the thunk to generate the code
1775 -- which displaces "this" to reference the corresponding
1776 -- secondary dispatch table in the returned object.
1777
1778 if Is_Interface (Etype (Iface_Prim)) then
1779 Set_Etype (New_Subp, Etype (Iface_Prim));
1780 end if;
1781
1782 -- Internal entities associated with interface types are only
1783 -- registered in the list of primitives of the tagged type.
1784 -- They are only used to fill the contents of the secondary
1785 -- dispatch tables. Therefore they are not needed in the
1786 -- homonym chains.
1787
1788 Remove_Homonym (New_Subp);
1789
1790 -- Hidden entities associated with interfaces must have set
1791 -- the Has_Delay_Freeze attribute to ensure that, in case
1792 -- of locally defined tagged types (or compiling with static
1793 -- dispatch tables generation disabled) the corresponding
1794 -- entry of the secondary dispatch table is filled when such
1795 -- an entity is frozen. This is an expansion activity that must
1796 -- be suppressed for ASIS because it leads to gigi elaboration
1797 -- issues in annotate mode.
1798
1799 if not ASIS_Mode then
1800 Set_Has_Delayed_Freeze (New_Subp);
1801 end if;
1802 end if;
1803
1804 <<Continue>>
1805 Next_Elmt (Elmt);
1806 end loop;
1807
1808 Next_Elmt (Iface_Elmt);
1809 end loop;
1810
1811 if Restore_Scope then
1812 Pop_Scope;
1813 end if;
1814 end Add_Internal_Interface_Entities;
1815
1816 -----------------------------------
1817 -- Analyze_Component_Declaration --
1818 -----------------------------------
1819
1820 procedure Analyze_Component_Declaration (N : Node_Id) is
1821 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1822 Id : constant Entity_Id := Defining_Identifier (N);
1823 E : constant Node_Id := Expression (N);
1824 Typ : constant Node_Id :=
1825 Subtype_Indication (Component_Definition (N));
1826 T : Entity_Id;
1827 P : Entity_Id;
1828
1829 function Contains_POC (Constr : Node_Id) return Boolean;
1830 -- Determines whether a constraint uses the discriminant of a record
1831 -- type thus becoming a per-object constraint (POC).
1832
1833 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1834 -- Typ is the type of the current component, check whether this type is
1835 -- a limited type. Used to validate declaration against that of
1836 -- enclosing record.
1837
1838 ------------------
1839 -- Contains_POC --
1840 ------------------
1841
1842 function Contains_POC (Constr : Node_Id) return Boolean is
1843 begin
1844 -- Prevent cascaded errors
1845
1846 if Error_Posted (Constr) then
1847 return False;
1848 end if;
1849
1850 case Nkind (Constr) is
1851 when N_Attribute_Reference =>
1852 return Attribute_Name (Constr) = Name_Access
1853 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1854
1855 when N_Discriminant_Association =>
1856 return Denotes_Discriminant (Expression (Constr));
1857
1858 when N_Identifier =>
1859 return Denotes_Discriminant (Constr);
1860
1861 when N_Index_Or_Discriminant_Constraint =>
1862 declare
1863 IDC : Node_Id;
1864
1865 begin
1866 IDC := First (Constraints (Constr));
1867 while Present (IDC) loop
1868
1869 -- One per-object constraint is sufficient
1870
1871 if Contains_POC (IDC) then
1872 return True;
1873 end if;
1874
1875 Next (IDC);
1876 end loop;
1877
1878 return False;
1879 end;
1880
1881 when N_Range =>
1882 return Denotes_Discriminant (Low_Bound (Constr))
1883 or else
1884 Denotes_Discriminant (High_Bound (Constr));
1885
1886 when N_Range_Constraint =>
1887 return Denotes_Discriminant (Range_Expression (Constr));
1888
1889 when others =>
1890 return False;
1891 end case;
1892 end Contains_POC;
1893
1894 ----------------------
1895 -- Is_Known_Limited --
1896 ----------------------
1897
1898 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1899 P : constant Entity_Id := Etype (Typ);
1900 R : constant Entity_Id := Root_Type (Typ);
1901
1902 begin
1903 if Is_Limited_Record (Typ) then
1904 return True;
1905
1906 -- If the root type is limited (and not a limited interface)
1907 -- so is the current type
1908
1909 elsif Is_Limited_Record (R)
1910 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1911 then
1912 return True;
1913
1914 -- Else the type may have a limited interface progenitor, but a
1915 -- limited record parent.
1916
1917 elsif R /= P and then Is_Limited_Record (P) then
1918 return True;
1919
1920 else
1921 return False;
1922 end if;
1923 end Is_Known_Limited;
1924
1925 -- Start of processing for Analyze_Component_Declaration
1926
1927 begin
1928 Generate_Definition (Id);
1929 Enter_Name (Id);
1930
1931 if Present (Typ) then
1932 T := Find_Type_Of_Object
1933 (Subtype_Indication (Component_Definition (N)), N);
1934
1935 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1936 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1937 end if;
1938
1939 -- Ada 2005 (AI-230): Access Definition case
1940
1941 else
1942 pragma Assert (Present
1943 (Access_Definition (Component_Definition (N))));
1944
1945 T := Access_Definition
1946 (Related_Nod => N,
1947 N => Access_Definition (Component_Definition (N)));
1948 Set_Is_Local_Anonymous_Access (T);
1949
1950 -- Ada 2005 (AI-254)
1951
1952 if Present (Access_To_Subprogram_Definition
1953 (Access_Definition (Component_Definition (N))))
1954 and then Protected_Present (Access_To_Subprogram_Definition
1955 (Access_Definition
1956 (Component_Definition (N))))
1957 then
1958 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1959 end if;
1960 end if;
1961
1962 -- If the subtype is a constrained subtype of the enclosing record,
1963 -- (which must have a partial view) the back-end does not properly
1964 -- handle the recursion. Rewrite the component declaration with an
1965 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1966 -- the tree directly because side effects have already been removed from
1967 -- discriminant constraints.
1968
1969 if Ekind (T) = E_Access_Subtype
1970 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1971 and then Comes_From_Source (T)
1972 and then Nkind (Parent (T)) = N_Subtype_Declaration
1973 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1974 then
1975 Rewrite
1976 (Subtype_Indication (Component_Definition (N)),
1977 New_Copy_Tree (Subtype_Indication (Parent (T))));
1978 T := Find_Type_Of_Object
1979 (Subtype_Indication (Component_Definition (N)), N);
1980 end if;
1981
1982 -- If the component declaration includes a default expression, then we
1983 -- check that the component is not of a limited type (RM 3.7(5)),
1984 -- and do the special preanalysis of the expression (see section on
1985 -- "Handling of Default and Per-Object Expressions" in the spec of
1986 -- package Sem).
1987
1988 if Present (E) then
1989 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1990 Preanalyze_Default_Expression (E, T);
1991 Check_Initialization (T, E);
1992
1993 if Ada_Version >= Ada_2005
1994 and then Ekind (T) = E_Anonymous_Access_Type
1995 and then Etype (E) /= Any_Type
1996 then
1997 -- Check RM 3.9.2(9): "if the expected type for an expression is
1998 -- an anonymous access-to-specific tagged type, then the object
1999 -- designated by the expression shall not be dynamically tagged
2000 -- unless it is a controlling operand in a call on a dispatching
2001 -- operation"
2002
2003 if Is_Tagged_Type (Directly_Designated_Type (T))
2004 and then
2005 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2006 and then
2007 Ekind (Directly_Designated_Type (Etype (E))) =
2008 E_Class_Wide_Type
2009 then
2010 Error_Msg_N
2011 ("access to specific tagged type required (RM 3.9.2(9))", E);
2012 end if;
2013
2014 -- (Ada 2005: AI-230): Accessibility check for anonymous
2015 -- components
2016
2017 if Type_Access_Level (Etype (E)) >
2018 Deepest_Type_Access_Level (T)
2019 then
2020 Error_Msg_N
2021 ("expression has deeper access level than component " &
2022 "(RM 3.10.2 (12.2))", E);
2023 end if;
2024
2025 -- The initialization expression is a reference to an access
2026 -- discriminant. The type of the discriminant is always deeper
2027 -- than any access type.
2028
2029 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2030 and then Is_Entity_Name (E)
2031 and then Ekind (Entity (E)) = E_In_Parameter
2032 and then Present (Discriminal_Link (Entity (E)))
2033 then
2034 Error_Msg_N
2035 ("discriminant has deeper accessibility level than target",
2036 E);
2037 end if;
2038 end if;
2039 end if;
2040
2041 -- The parent type may be a private view with unknown discriminants,
2042 -- and thus unconstrained. Regular components must be constrained.
2043
2044 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2045 if Is_Class_Wide_Type (T) then
2046 Error_Msg_N
2047 ("class-wide subtype with unknown discriminants" &
2048 " in component declaration",
2049 Subtype_Indication (Component_Definition (N)));
2050 else
2051 Error_Msg_N
2052 ("unconstrained subtype in component declaration",
2053 Subtype_Indication (Component_Definition (N)));
2054 end if;
2055
2056 -- Components cannot be abstract, except for the special case of
2057 -- the _Parent field (case of extending an abstract tagged type)
2058
2059 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2060 Error_Msg_N ("type of a component cannot be abstract", N);
2061 end if;
2062
2063 Set_Etype (Id, T);
2064 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2065
2066 -- The component declaration may have a per-object constraint, set
2067 -- the appropriate flag in the defining identifier of the subtype.
2068
2069 if Present (Subtype_Indication (Component_Definition (N))) then
2070 declare
2071 Sindic : constant Node_Id :=
2072 Subtype_Indication (Component_Definition (N));
2073 begin
2074 if Nkind (Sindic) = N_Subtype_Indication
2075 and then Present (Constraint (Sindic))
2076 and then Contains_POC (Constraint (Sindic))
2077 then
2078 Set_Has_Per_Object_Constraint (Id);
2079 end if;
2080 end;
2081 end if;
2082
2083 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2084 -- out some static checks.
2085
2086 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2087 Null_Exclusion_Static_Checks (N);
2088 end if;
2089
2090 -- If this component is private (or depends on a private type), flag the
2091 -- record type to indicate that some operations are not available.
2092
2093 P := Private_Component (T);
2094
2095 if Present (P) then
2096
2097 -- Check for circular definitions
2098
2099 if P = Any_Type then
2100 Set_Etype (Id, Any_Type);
2101
2102 -- There is a gap in the visibility of operations only if the
2103 -- component type is not defined in the scope of the record type.
2104
2105 elsif Scope (P) = Scope (Current_Scope) then
2106 null;
2107
2108 elsif Is_Limited_Type (P) then
2109 Set_Is_Limited_Composite (Current_Scope);
2110
2111 else
2112 Set_Is_Private_Composite (Current_Scope);
2113 end if;
2114 end if;
2115
2116 if P /= Any_Type
2117 and then Is_Limited_Type (T)
2118 and then Chars (Id) /= Name_uParent
2119 and then Is_Tagged_Type (Current_Scope)
2120 then
2121 if Is_Derived_Type (Current_Scope)
2122 and then not Is_Known_Limited (Current_Scope)
2123 then
2124 Error_Msg_N
2125 ("extension of nonlimited type cannot have limited components",
2126 N);
2127
2128 if Is_Interface (Root_Type (Current_Scope)) then
2129 Error_Msg_N
2130 ("\limitedness is not inherited from limited interface", N);
2131 Error_Msg_N ("\add LIMITED to type indication", N);
2132 end if;
2133
2134 Explain_Limited_Type (T, N);
2135 Set_Etype (Id, Any_Type);
2136 Set_Is_Limited_Composite (Current_Scope, False);
2137
2138 elsif not Is_Derived_Type (Current_Scope)
2139 and then not Is_Limited_Record (Current_Scope)
2140 and then not Is_Concurrent_Type (Current_Scope)
2141 then
2142 Error_Msg_N
2143 ("nonlimited tagged type cannot have limited components", N);
2144 Explain_Limited_Type (T, N);
2145 Set_Etype (Id, Any_Type);
2146 Set_Is_Limited_Composite (Current_Scope, False);
2147 end if;
2148 end if;
2149
2150 -- If the component is an unconstrained task or protected type with
2151 -- discriminants, the component and the enclosing record are limited
2152 -- and the component is constrained by its default values. Compute
2153 -- its actual subtype, else it may be allocated the maximum size by
2154 -- the backend, and possibly overflow.
2155
2156 if Is_Concurrent_Type (T)
2157 and then not Is_Constrained (T)
2158 and then Has_Discriminants (T)
2159 and then not Has_Discriminants (Current_Scope)
2160 then
2161 declare
2162 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2163
2164 begin
2165 Set_Etype (Id, Act_T);
2166
2167 -- Rewrite component definition to use the constrained subtype
2168
2169 Rewrite (Component_Definition (N),
2170 Make_Component_Definition (Loc,
2171 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2172 end;
2173 end if;
2174
2175 Set_Original_Record_Component (Id, Id);
2176
2177 if Has_Aspects (N) then
2178 Analyze_Aspect_Specifications (N, Id);
2179 end if;
2180
2181 Analyze_Dimension (N);
2182 end Analyze_Component_Declaration;
2183
2184 --------------------------
2185 -- Analyze_Declarations --
2186 --------------------------
2187
2188 procedure Analyze_Declarations (L : List_Id) is
2189 Decl : Node_Id;
2190
2191 procedure Adjust_Decl;
2192 -- Adjust Decl not to include implicit label declarations, since these
2193 -- have strange Sloc values that result in elaboration check problems.
2194 -- (They have the sloc of the label as found in the source, and that
2195 -- is ahead of the current declarative part).
2196
2197 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2198 -- Create the subprogram bodies which verify the run-time semantics of
2199 -- the pragmas listed below for each elibigle type found in declarative
2200 -- list Decls. The pragmas are:
2201 --
2202 -- Default_Initial_Condition
2203 -- Invariant
2204 -- Type_Invariant
2205 --
2206 -- Context denotes the owner of the declarative list.
2207
2208 procedure Check_Entry_Contracts;
2209 -- Perform a pre-analysis of the pre- and postconditions of an entry
2210 -- declaration. This must be done before full resolution and creation
2211 -- of the parameter block, etc. to catch illegal uses within the
2212 -- contract expression. Full analysis of the expression is done when
2213 -- the contract is processed.
2214
2215 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2216 -- Check if a nested package has entities within it that rely on library
2217 -- level private types where the full view has not been completed for
2218 -- the purposes of checking if it is acceptable to freeze an expression
2219 -- function at the point of declaration.
2220
2221 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2222 -- Determine whether Body_Decl denotes the body of a late controlled
2223 -- primitive (either Initialize, Adjust or Finalize). If this is the
2224 -- case, add a proper spec if the body lacks one. The spec is inserted
2225 -- before Body_Decl and immediately analyzed.
2226
2227 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2228 -- Spec_Id is the entity of a package that may define abstract states,
2229 -- and in the case of a child unit, whose ancestors may define abstract
2230 -- states. If the states have partial visible refinement, remove the
2231 -- partial visibility of each constituent at the end of the package
2232 -- spec and body declarations.
2233
2234 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2235 -- Spec_Id is the entity of a package that may define abstract states.
2236 -- If the states have visible refinement, remove the visibility of each
2237 -- constituent at the end of the package body declaration.
2238
2239 procedure Resolve_Aspects;
2240 -- Utility to resolve the expressions of aspects at the end of a list of
2241 -- declarations, or before a declaration that freezes previous entities,
2242 -- such as in a subprogram body.
2243
2244 -----------------
2245 -- Adjust_Decl --
2246 -----------------
2247
2248 procedure Adjust_Decl is
2249 begin
2250 while Present (Prev (Decl))
2251 and then Nkind (Decl) = N_Implicit_Label_Declaration
2252 loop
2253 Prev (Decl);
2254 end loop;
2255 end Adjust_Decl;
2256
2257 ----------------------------
2258 -- Build_Assertion_Bodies --
2259 ----------------------------
2260
2261 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2262 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2263 -- Create the subprogram bodies which verify the run-time semantics
2264 -- of the pragmas listed below for type Typ. The pragmas are:
2265 --
2266 -- Default_Initial_Condition
2267 -- Invariant
2268 -- Type_Invariant
2269
2270 -------------------------------------
2271 -- Build_Assertion_Bodies_For_Type --
2272 -------------------------------------
2273
2274 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2275 begin
2276 -- Preanalyze and resolve the Default_Initial_Condition assertion
2277 -- expression at the end of the declarations to catch any errors.
2278
2279 if Has_DIC (Typ) then
2280 Build_DIC_Procedure_Body (Typ);
2281 end if;
2282
2283 if Nkind (Context) = N_Package_Specification then
2284
2285 -- Preanalyze and resolve the class-wide invariants of an
2286 -- interface at the end of whichever declarative part has the
2287 -- interface type. Note that an interface may be declared in
2288 -- any non-package declarative part, but reaching the end of
2289 -- such a declarative part will always freeze the type and
2290 -- generate the invariant procedure (see Freeze_Type).
2291
2292 if Is_Interface (Typ) then
2293
2294 -- Interfaces are treated as the partial view of a private
2295 -- type, in order to achieve uniformity with the general
2296 -- case. As a result, an interface receives only a "partial"
2297 -- invariant procedure, which is never called.
2298
2299 if Has_Own_Invariants (Typ) then
2300 Build_Invariant_Procedure_Body
2301 (Typ => Typ,
2302 Partial_Invariant => True);
2303 end if;
2304
2305 -- Preanalyze and resolve the invariants of a private type
2306 -- at the end of the visible declarations to catch potential
2307 -- errors. Inherited class-wide invariants are not included
2308 -- because they have already been resolved.
2309
2310 elsif Decls = Visible_Declarations (Context)
2311 and then Ekind_In (Typ, E_Limited_Private_Type,
2312 E_Private_Type,
2313 E_Record_Type_With_Private)
2314 and then Has_Own_Invariants (Typ)
2315 then
2316 Build_Invariant_Procedure_Body
2317 (Typ => Typ,
2318 Partial_Invariant => True);
2319
2320 -- Preanalyze and resolve the invariants of a private type's
2321 -- full view at the end of the private declarations to catch
2322 -- potential errors.
2323
2324 elsif Decls = Private_Declarations (Context)
2325 and then not Is_Private_Type (Typ)
2326 and then Has_Private_Declaration (Typ)
2327 and then Has_Invariants (Typ)
2328 then
2329 Build_Invariant_Procedure_Body (Typ);
2330 end if;
2331 end if;
2332 end Build_Assertion_Bodies_For_Type;
2333
2334 -- Local variables
2335
2336 Decl : Node_Id;
2337 Decl_Id : Entity_Id;
2338
2339 -- Start of processing for Build_Assertion_Bodies
2340
2341 begin
2342 Decl := First (Decls);
2343 while Present (Decl) loop
2344 if Is_Declaration (Decl) then
2345 Decl_Id := Defining_Entity (Decl);
2346
2347 if Is_Type (Decl_Id) then
2348 Build_Assertion_Bodies_For_Type (Decl_Id);
2349 end if;
2350 end if;
2351
2352 Next (Decl);
2353 end loop;
2354 end Build_Assertion_Bodies;
2355
2356 ---------------------------
2357 -- Check_Entry_Contracts --
2358 ---------------------------
2359
2360 procedure Check_Entry_Contracts is
2361 ASN : Node_Id;
2362 Ent : Entity_Id;
2363 Exp : Node_Id;
2364
2365 begin
2366 Ent := First_Entity (Current_Scope);
2367 while Present (Ent) loop
2368
2369 -- This only concerns entries with pre/postconditions
2370
2371 if Ekind (Ent) = E_Entry
2372 and then Present (Contract (Ent))
2373 and then Present (Pre_Post_Conditions (Contract (Ent)))
2374 then
2375 ASN := Pre_Post_Conditions (Contract (Ent));
2376 Push_Scope (Ent);
2377 Install_Formals (Ent);
2378
2379 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2380 -- is performed on a copy of the pragma expression, to prevent
2381 -- modifying the original expression.
2382
2383 while Present (ASN) loop
2384 if Nkind (ASN) = N_Pragma then
2385 Exp :=
2386 New_Copy_Tree
2387 (Expression
2388 (First (Pragma_Argument_Associations (ASN))));
2389 Set_Parent (Exp, ASN);
2390
2391 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2392 end if;
2393
2394 ASN := Next_Pragma (ASN);
2395 end loop;
2396
2397 End_Scope;
2398 end if;
2399
2400 Next_Entity (Ent);
2401 end loop;
2402 end Check_Entry_Contracts;
2403
2404 ----------------------------------
2405 -- Contains_Lib_Incomplete_Type --
2406 ----------------------------------
2407
2408 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2409 Curr : Entity_Id;
2410
2411 begin
2412 -- Avoid looking through scopes that do not meet the precondition of
2413 -- Pkg not being within a library unit spec.
2414
2415 if not Is_Compilation_Unit (Pkg)
2416 and then not Is_Generic_Instance (Pkg)
2417 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2418 then
2419 -- Loop through all entities in the current scope to identify
2420 -- an entity that depends on a private type.
2421
2422 Curr := First_Entity (Pkg);
2423 loop
2424 if Nkind (Curr) in N_Entity
2425 and then Depends_On_Private (Curr)
2426 then
2427 return True;
2428 end if;
2429
2430 exit when Last_Entity (Current_Scope) = Curr;
2431 Curr := Next_Entity (Curr);
2432 end loop;
2433 end if;
2434
2435 return False;
2436 end Contains_Lib_Incomplete_Type;
2437
2438 --------------------------------------
2439 -- Handle_Late_Controlled_Primitive --
2440 --------------------------------------
2441
2442 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2443 Body_Spec : constant Node_Id := Specification (Body_Decl);
2444 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2445 Loc : constant Source_Ptr := Sloc (Body_Id);
2446 Params : constant List_Id :=
2447 Parameter_Specifications (Body_Spec);
2448 Spec : Node_Id;
2449 Spec_Id : Entity_Id;
2450 Typ : Node_Id;
2451
2452 begin
2453 -- Consider only procedure bodies whose name matches one of the three
2454 -- controlled primitives.
2455
2456 if Nkind (Body_Spec) /= N_Procedure_Specification
2457 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2458 Name_Finalize,
2459 Name_Initialize)
2460 then
2461 return;
2462
2463 -- A controlled primitive must have exactly one formal which is not
2464 -- an anonymous access type.
2465
2466 elsif List_Length (Params) /= 1 then
2467 return;
2468 end if;
2469
2470 Typ := Parameter_Type (First (Params));
2471
2472 if Nkind (Typ) = N_Access_Definition then
2473 return;
2474 end if;
2475
2476 Find_Type (Typ);
2477
2478 -- The type of the formal must be derived from [Limited_]Controlled
2479
2480 if not Is_Controlled (Entity (Typ)) then
2481 return;
2482 end if;
2483
2484 -- Check whether a specification exists for this body. We do not
2485 -- analyze the spec of the body in full, because it will be analyzed
2486 -- again when the body is properly analyzed, and we cannot create
2487 -- duplicate entries in the formals chain. We look for an explicit
2488 -- specification because the body may be an overriding operation and
2489 -- an inherited spec may be present.
2490
2491 Spec_Id := Current_Entity (Body_Id);
2492
2493 while Present (Spec_Id) loop
2494 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2495 and then Scope (Spec_Id) = Current_Scope
2496 and then Present (First_Formal (Spec_Id))
2497 and then No (Next_Formal (First_Formal (Spec_Id)))
2498 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2499 and then Comes_From_Source (Spec_Id)
2500 then
2501 return;
2502 end if;
2503
2504 Spec_Id := Homonym (Spec_Id);
2505 end loop;
2506
2507 -- At this point the body is known to be a late controlled primitive.
2508 -- Generate a matching spec and insert it before the body. Note the
2509 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2510 -- tree in this case.
2511
2512 Spec := Copy_Separate_Tree (Body_Spec);
2513
2514 -- Ensure that the subprogram declaration does not inherit the null
2515 -- indicator from the body as we now have a proper spec/body pair.
2516
2517 Set_Null_Present (Spec, False);
2518
2519 -- Ensure that the freeze node is inserted after the declaration of
2520 -- the primitive since its expansion will freeze the primitive.
2521
2522 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2523
2524 Insert_Before_And_Analyze (Body_Decl, Decl);
2525 end Handle_Late_Controlled_Primitive;
2526
2527 ----------------------------------------
2528 -- Remove_Partial_Visible_Refinements --
2529 ----------------------------------------
2530
2531 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2532 State_Elmt : Elmt_Id;
2533 begin
2534 if Present (Abstract_States (Spec_Id)) then
2535 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2536 while Present (State_Elmt) loop
2537 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2538 Next_Elmt (State_Elmt);
2539 end loop;
2540 end if;
2541
2542 -- For a child unit, also hide the partial state refinement from
2543 -- ancestor packages.
2544
2545 if Is_Child_Unit (Spec_Id) then
2546 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2547 end if;
2548 end Remove_Partial_Visible_Refinements;
2549
2550 --------------------------------
2551 -- Remove_Visible_Refinements --
2552 --------------------------------
2553
2554 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2555 State_Elmt : Elmt_Id;
2556 begin
2557 if Present (Abstract_States (Spec_Id)) then
2558 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2559 while Present (State_Elmt) loop
2560 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2561 Next_Elmt (State_Elmt);
2562 end loop;
2563 end if;
2564 end Remove_Visible_Refinements;
2565
2566 ---------------------
2567 -- Resolve_Aspects --
2568 ---------------------
2569
2570 procedure Resolve_Aspects is
2571 E : Entity_Id;
2572
2573 begin
2574 E := First_Entity (Current_Scope);
2575 while Present (E) loop
2576 Resolve_Aspect_Expressions (E);
2577 Next_Entity (E);
2578 end loop;
2579 end Resolve_Aspects;
2580
2581 -- Local variables
2582
2583 Context : Node_Id := Empty;
2584 Freeze_From : Entity_Id := Empty;
2585 Next_Decl : Node_Id;
2586
2587 Body_Seen : Boolean := False;
2588 -- Flag set when the first body [stub] is encountered
2589
2590 -- Start of processing for Analyze_Declarations
2591
2592 begin
2593 if Restriction_Check_Required (SPARK_05) then
2594 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2595 end if;
2596
2597 Decl := First (L);
2598 while Present (Decl) loop
2599
2600 -- Package spec cannot contain a package declaration in SPARK
2601
2602 if Nkind (Decl) = N_Package_Declaration
2603 and then Nkind (Parent (L)) = N_Package_Specification
2604 then
2605 Check_SPARK_05_Restriction
2606 ("package specification cannot contain a package declaration",
2607 Decl);
2608 end if;
2609
2610 -- Complete analysis of declaration
2611
2612 Analyze (Decl);
2613 Next_Decl := Next (Decl);
2614
2615 if No (Freeze_From) then
2616 Freeze_From := First_Entity (Current_Scope);
2617 end if;
2618
2619 -- At the end of a declarative part, freeze remaining entities
2620 -- declared in it. The end of the visible declarations of package
2621 -- specification is not the end of a declarative part if private
2622 -- declarations are present. The end of a package declaration is a
2623 -- freezing point only if it a library package. A task definition or
2624 -- protected type definition is not a freeze point either. Finally,
2625 -- we do not freeze entities in generic scopes, because there is no
2626 -- code generated for them and freeze nodes will be generated for
2627 -- the instance.
2628
2629 -- The end of a package instantiation is not a freeze point, but
2630 -- for now we make it one, because the generic body is inserted
2631 -- (currently) immediately after. Generic instantiations will not
2632 -- be a freeze point once delayed freezing of bodies is implemented.
2633 -- (This is needed in any case for early instantiations ???).
2634
2635 if No (Next_Decl) then
2636 if Nkind (Parent (L)) = N_Component_List then
2637 null;
2638
2639 elsif Nkind_In (Parent (L), N_Protected_Definition,
2640 N_Task_Definition)
2641 then
2642 Check_Entry_Contracts;
2643
2644 elsif Nkind (Parent (L)) /= N_Package_Specification then
2645 if Nkind (Parent (L)) = N_Package_Body then
2646 Freeze_From := First_Entity (Current_Scope);
2647 end if;
2648
2649 -- There may have been several freezing points previously,
2650 -- for example object declarations or subprogram bodies, but
2651 -- at the end of a declarative part we check freezing from
2652 -- the beginning, even though entities may already be frozen,
2653 -- in order to perform visibility checks on delayed aspects.
2654
2655 Adjust_Decl;
2656
2657 -- If the current scope is a generic subprogram body. Skip the
2658 -- generic formal parameters that are not frozen here.
2659
2660 if Is_Subprogram (Current_Scope)
2661 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2662 N_Generic_Subprogram_Declaration
2663 and then Present (First_Entity (Current_Scope))
2664 then
2665 while Is_Generic_Formal (Freeze_From) loop
2666 Freeze_From := Next_Entity (Freeze_From);
2667 end loop;
2668
2669 Freeze_All (Freeze_From, Decl);
2670 Freeze_From := Last_Entity (Current_Scope);
2671
2672 else
2673 -- For declarations in a subprogram body there is no issue
2674 -- with name resolution in aspect specifications, but in
2675 -- ASIS mode we need to preanalyze aspect specifications
2676 -- that may otherwise only be analyzed during expansion
2677 -- (e.g. during generation of a related subprogram).
2678
2679 if ASIS_Mode then
2680 Resolve_Aspects;
2681 end if;
2682
2683 Freeze_All (First_Entity (Current_Scope), Decl);
2684 Freeze_From := Last_Entity (Current_Scope);
2685 end if;
2686
2687 -- Current scope is a package specification
2688
2689 elsif Scope (Current_Scope) /= Standard_Standard
2690 and then not Is_Child_Unit (Current_Scope)
2691 and then No (Generic_Parent (Parent (L)))
2692 then
2693 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2694 -- resolved at the end of the immediately enclosing declaration
2695 -- list (AI05-0183-1).
2696
2697 Resolve_Aspects;
2698
2699 elsif L /= Visible_Declarations (Parent (L))
2700 or else No (Private_Declarations (Parent (L)))
2701 or else Is_Empty_List (Private_Declarations (Parent (L)))
2702 then
2703 Adjust_Decl;
2704
2705 -- End of a package declaration
2706
2707 -- In compilation mode the expansion of freeze node takes care
2708 -- of resolving expressions of all aspects in the list. In ASIS
2709 -- mode this must be done explicitly.
2710
2711 if ASIS_Mode
2712 and then Scope (Current_Scope) = Standard_Standard
2713 then
2714 Resolve_Aspects;
2715 end if;
2716
2717 -- This is a freeze point because it is the end of a
2718 -- compilation unit.
2719
2720 Freeze_All (First_Entity (Current_Scope), Decl);
2721 Freeze_From := Last_Entity (Current_Scope);
2722
2723 -- At the end of the visible declarations the expressions in
2724 -- aspects of all entities declared so far must be resolved.
2725 -- The entities themselves might be frozen later, and the
2726 -- generated pragmas and attribute definition clauses analyzed
2727 -- in full at that point, but name resolution must take place
2728 -- now.
2729 -- In addition to being the proper semantics, this is mandatory
2730 -- within generic units, because global name capture requires
2731 -- those expressions to be analyzed, given that the generated
2732 -- pragmas do not appear in the original generic tree.
2733
2734 elsif Serious_Errors_Detected = 0 then
2735 Resolve_Aspects;
2736 end if;
2737
2738 -- If next node is a body then freeze all types before the body.
2739 -- An exception occurs for some expander-generated bodies. If these
2740 -- are generated at places where in general language rules would not
2741 -- allow a freeze point, then we assume that the expander has
2742 -- explicitly checked that all required types are properly frozen,
2743 -- and we do not cause general freezing here. This special circuit
2744 -- is used when the encountered body is marked as having already
2745 -- been analyzed.
2746
2747 -- In all other cases (bodies that come from source, and expander
2748 -- generated bodies that have not been analyzed yet), freeze all
2749 -- types now. Note that in the latter case, the expander must take
2750 -- care to attach the bodies at a proper place in the tree so as to
2751 -- not cause unwanted freezing at that point.
2752
2753 -- It is also necessary to check for a case where both an expression
2754 -- function is used and the current scope depends on an incomplete
2755 -- private type from a library unit, otherwise premature freezing of
2756 -- the private type will occur.
2757
2758 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2759 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2760 or else not Was_Expression_Function (Next_Decl))
2761 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2762 and then not Contains_Lib_Incomplete_Type
2763 (Current_Scope)))
2764 then
2765 -- When a controlled type is frozen, the expander generates stream
2766 -- and controlled-type support routines. If the freeze is caused
2767 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2768 -- expander will end up using the wrong version of these routines,
2769 -- as the body has not been processed yet. To remedy this, detect
2770 -- a late controlled primitive and create a proper spec for it.
2771 -- This ensures that the primitive will override its inherited
2772 -- counterpart before the freeze takes place.
2773
2774 -- If the declaration we just processed is a body, do not attempt
2775 -- to examine Next_Decl as the late primitive idiom can only apply
2776 -- to the first encountered body.
2777
2778 -- The spec of the late primitive is not generated in ASIS mode to
2779 -- ensure a consistent list of primitives that indicates the true
2780 -- semantic structure of the program (which is not relevant when
2781 -- generating executable code).
2782
2783 -- ??? A cleaner approach may be possible and/or this solution
2784 -- could be extended to general-purpose late primitives, TBD.
2785
2786 if not ASIS_Mode
2787 and then not Body_Seen
2788 and then not Is_Body (Decl)
2789 then
2790 Body_Seen := True;
2791
2792 if Nkind (Next_Decl) = N_Subprogram_Body then
2793 Handle_Late_Controlled_Primitive (Next_Decl);
2794 end if;
2795
2796 else
2797 -- In ASIS mode, if the next declaration is a body, complete
2798 -- the analysis of declarations so far.
2799
2800 Resolve_Aspects;
2801 end if;
2802
2803 Adjust_Decl;
2804
2805 -- The generated body of an expression function does not freeze,
2806 -- unless it is a completion, in which case only the expression
2807 -- itself freezes. This is handled when the body itself is
2808 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2809
2810 Freeze_All (Freeze_From, Decl);
2811 Freeze_From := Last_Entity (Current_Scope);
2812 end if;
2813
2814 Decl := Next_Decl;
2815 end loop;
2816
2817 -- Post-freezing actions
2818
2819 if Present (L) then
2820 Context := Parent (L);
2821
2822 -- Certain contract annocations have forward visibility semantics and
2823 -- must be analyzed after all declarative items have been processed.
2824 -- This timing ensures that entities referenced by such contracts are
2825 -- visible.
2826
2827 -- Analyze the contract of an immediately enclosing package spec or
2828 -- body first because other contracts may depend on its information.
2829
2830 if Nkind (Context) = N_Package_Body then
2831 Analyze_Package_Body_Contract (Defining_Entity (Context));
2832
2833 elsif Nkind (Context) = N_Package_Specification then
2834 Analyze_Package_Contract (Defining_Entity (Context));
2835 end if;
2836
2837 -- Analyze the contracts of various constructs in the declarative
2838 -- list.
2839
2840 Analyze_Contracts (L);
2841
2842 if Nkind (Context) = N_Package_Body then
2843
2844 -- Ensure that all abstract states and objects declared in the
2845 -- state space of a package body are utilized as constituents.
2846
2847 Check_Unused_Body_States (Defining_Entity (Context));
2848
2849 -- State refinements are visible up to the end of the package body
2850 -- declarations. Hide the state refinements from visibility to
2851 -- restore the original state conditions.
2852
2853 Remove_Visible_Refinements (Corresponding_Spec (Context));
2854 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2855
2856 elsif Nkind (Context) = N_Package_Specification then
2857
2858 -- Partial state refinements are visible up to the end of the
2859 -- package spec declarations. Hide the partial state refinements
2860 -- from visibility to restore the original state conditions.
2861
2862 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2863 end if;
2864
2865 -- Verify that all abstract states found in any package declared in
2866 -- the input declarative list have proper refinements. The check is
2867 -- performed only when the context denotes a block, entry, package,
2868 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2869
2870 Check_State_Refinements (Context);
2871
2872 -- Create the subprogram bodies which verify the run-time semantics
2873 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2874 -- types within the current declarative list. This ensures that all
2875 -- assertion expressions are preanalyzed and resolved at the end of
2876 -- the declarative part. Note that the resolution happens even when
2877 -- freezing does not take place.
2878
2879 Build_Assertion_Bodies (L, Context);
2880 end if;
2881 end Analyze_Declarations;
2882
2883 -----------------------------------
2884 -- Analyze_Full_Type_Declaration --
2885 -----------------------------------
2886
2887 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2888 Def : constant Node_Id := Type_Definition (N);
2889 Def_Id : constant Entity_Id := Defining_Identifier (N);
2890 T : Entity_Id;
2891 Prev : Entity_Id;
2892
2893 Is_Remote : constant Boolean :=
2894 (Is_Remote_Types (Current_Scope)
2895 or else Is_Remote_Call_Interface (Current_Scope))
2896 and then not (In_Private_Part (Current_Scope)
2897 or else In_Package_Body (Current_Scope));
2898
2899 procedure Check_Nonoverridable_Aspects;
2900 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2901 -- be overridden, and can only be confirmed on derivation.
2902
2903 procedure Check_Ops_From_Incomplete_Type;
2904 -- If there is a tagged incomplete partial view of the type, traverse
2905 -- the primitives of the incomplete view and change the type of any
2906 -- controlling formals and result to indicate the full view. The
2907 -- primitives will be added to the full type's primitive operations
2908 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2909 -- is called from Process_Incomplete_Dependents).
2910
2911 ----------------------------------
2912 -- Check_Nonoverridable_Aspects --
2913 ----------------------------------
2914
2915 procedure Check_Nonoverridable_Aspects is
2916 function Get_Aspect_Spec
2917 (Specs : List_Id;
2918 Aspect_Name : Name_Id) return Node_Id;
2919 -- Check whether a list of aspect specifications includes an entry
2920 -- for a specific aspect. The list is either that of a partial or
2921 -- a full view.
2922
2923 ---------------------
2924 -- Get_Aspect_Spec --
2925 ---------------------
2926
2927 function Get_Aspect_Spec
2928 (Specs : List_Id;
2929 Aspect_Name : Name_Id) return Node_Id
2930 is
2931 Spec : Node_Id;
2932
2933 begin
2934 Spec := First (Specs);
2935 while Present (Spec) loop
2936 if Chars (Identifier (Spec)) = Aspect_Name then
2937 return Spec;
2938 end if;
2939 Next (Spec);
2940 end loop;
2941
2942 return Empty;
2943 end Get_Aspect_Spec;
2944
2945 -- Local variables
2946
2947 Prev_Aspects : constant List_Id :=
2948 Aspect_Specifications (Parent (Def_Id));
2949 Par_Type : Entity_Id;
2950 Prev_Aspect : Node_Id;
2951
2952 -- Start of processing for Check_Nonoverridable_Aspects
2953
2954 begin
2955 -- Get parent type of derived type. Note that Prev is the entity in
2956 -- the partial declaration, but its contents are now those of full
2957 -- view, while Def_Id reflects the partial view.
2958
2959 if Is_Private_Type (Def_Id) then
2960 Par_Type := Etype (Full_View (Def_Id));
2961 else
2962 Par_Type := Etype (Def_Id);
2963 end if;
2964
2965 -- If there is an inherited Implicit_Dereference, verify that it is
2966 -- made explicit in the partial view.
2967
2968 if Has_Discriminants (Base_Type (Par_Type))
2969 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2970 and then Present (Discriminant_Specifications (Parent (Prev)))
2971 and then Present (Get_Reference_Discriminant (Par_Type))
2972 then
2973 Prev_Aspect :=
2974 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2975
2976 if No (Prev_Aspect)
2977 and then Present
2978 (Discriminant_Specifications
2979 (Original_Node (Parent (Prev))))
2980 then
2981 Error_Msg_N
2982 ("type does not inherit implicit dereference", Prev);
2983
2984 else
2985 -- If one of the views has the aspect specified, verify that it
2986 -- is consistent with that of the parent.
2987
2988 declare
2989 Par_Discr : constant Entity_Id :=
2990 Get_Reference_Discriminant (Par_Type);
2991 Cur_Discr : constant Entity_Id :=
2992 Get_Reference_Discriminant (Prev);
2993
2994 begin
2995 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2996 Error_Msg_N ("aspect incosistent with that of parent", N);
2997 end if;
2998
2999 -- Check that specification in partial view matches the
3000 -- inherited aspect. Compare names directly because aspect
3001 -- expression may not be analyzed.
3002
3003 if Present (Prev_Aspect)
3004 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3005 and then Chars (Expression (Prev_Aspect)) /=
3006 Chars (Cur_Discr)
3007 then
3008 Error_Msg_N
3009 ("aspect incosistent with that of parent", N);
3010 end if;
3011 end;
3012 end if;
3013 end if;
3014
3015 -- TBD : other nonoverridable aspects.
3016 end Check_Nonoverridable_Aspects;
3017
3018 ------------------------------------
3019 -- Check_Ops_From_Incomplete_Type --
3020 ------------------------------------
3021
3022 procedure Check_Ops_From_Incomplete_Type is
3023 Elmt : Elmt_Id;
3024 Formal : Entity_Id;
3025 Op : Entity_Id;
3026
3027 begin
3028 if Prev /= T
3029 and then Ekind (Prev) = E_Incomplete_Type
3030 and then Is_Tagged_Type (Prev)
3031 and then Is_Tagged_Type (T)
3032 then
3033 Elmt := First_Elmt (Primitive_Operations (Prev));
3034 while Present (Elmt) loop
3035 Op := Node (Elmt);
3036
3037 Formal := First_Formal (Op);
3038 while Present (Formal) loop
3039 if Etype (Formal) = Prev then
3040 Set_Etype (Formal, T);
3041 end if;
3042
3043 Next_Formal (Formal);
3044 end loop;
3045
3046 if Etype (Op) = Prev then
3047 Set_Etype (Op, T);
3048 end if;
3049
3050 Next_Elmt (Elmt);
3051 end loop;
3052 end if;
3053 end Check_Ops_From_Incomplete_Type;
3054
3055 -- Start of processing for Analyze_Full_Type_Declaration
3056
3057 begin
3058 Prev := Find_Type_Name (N);
3059
3060 -- The full view, if present, now points to the current type. If there
3061 -- is an incomplete partial view, set a link to it, to simplify the
3062 -- retrieval of primitive operations of the type.
3063
3064 -- Ada 2005 (AI-50217): If the type was previously decorated when
3065 -- imported through a LIMITED WITH clause, it appears as incomplete
3066 -- but has no full view.
3067
3068 if Ekind (Prev) = E_Incomplete_Type
3069 and then Present (Full_View (Prev))
3070 then
3071 T := Full_View (Prev);
3072 Set_Incomplete_View (N, Parent (Prev));
3073 else
3074 T := Prev;
3075 end if;
3076
3077 Set_Is_Pure (T, Is_Pure (Current_Scope));
3078
3079 -- We set the flag Is_First_Subtype here. It is needed to set the
3080 -- corresponding flag for the Implicit class-wide-type created
3081 -- during tagged types processing.
3082
3083 Set_Is_First_Subtype (T, True);
3084
3085 -- Only composite types other than array types are allowed to have
3086 -- discriminants.
3087
3088 case Nkind (Def) is
3089
3090 -- For derived types, the rule will be checked once we've figured
3091 -- out the parent type.
3092
3093 when N_Derived_Type_Definition =>
3094 null;
3095
3096 -- For record types, discriminants are allowed, unless we are in
3097 -- SPARK.
3098
3099 when N_Record_Definition =>
3100 if Present (Discriminant_Specifications (N)) then
3101 Check_SPARK_05_Restriction
3102 ("discriminant type is not allowed",
3103 Defining_Identifier
3104 (First (Discriminant_Specifications (N))));
3105 end if;
3106
3107 when others =>
3108 if Present (Discriminant_Specifications (N)) then
3109 Error_Msg_N
3110 ("elementary or array type cannot have discriminants",
3111 Defining_Identifier
3112 (First (Discriminant_Specifications (N))));
3113 end if;
3114 end case;
3115
3116 -- Elaborate the type definition according to kind, and generate
3117 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3118 -- already done (this happens during the reanalysis that follows a call
3119 -- to the high level optimizer).
3120
3121 if not Analyzed (T) then
3122 Set_Analyzed (T);
3123
3124 -- Set the SPARK mode from the current context
3125
3126 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3127 Set_SPARK_Pragma_Inherited (T);
3128
3129 case Nkind (Def) is
3130 when N_Access_To_Subprogram_Definition =>
3131 Access_Subprogram_Declaration (T, Def);
3132
3133 -- If this is a remote access to subprogram, we must create the
3134 -- equivalent fat pointer type, and related subprograms.
3135
3136 if Is_Remote then
3137 Process_Remote_AST_Declaration (N);
3138 end if;
3139
3140 -- Validate categorization rule against access type declaration
3141 -- usually a violation in Pure unit, Shared_Passive unit.
3142
3143 Validate_Access_Type_Declaration (T, N);
3144
3145 when N_Access_To_Object_Definition =>
3146 Access_Type_Declaration (T, Def);
3147
3148 -- Validate categorization rule against access type declaration
3149 -- usually a violation in Pure unit, Shared_Passive unit.
3150
3151 Validate_Access_Type_Declaration (T, N);
3152
3153 -- If we are in a Remote_Call_Interface package and define a
3154 -- RACW, then calling stubs and specific stream attributes
3155 -- must be added.
3156
3157 if Is_Remote
3158 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3159 then
3160 Add_RACW_Features (Def_Id);
3161 end if;
3162
3163 when N_Array_Type_Definition =>
3164 Array_Type_Declaration (T, Def);
3165
3166 when N_Derived_Type_Definition =>
3167 Derived_Type_Declaration (T, N, T /= Def_Id);
3168
3169 -- Inherit predicates from parent, and protect against illegal
3170 -- derivations.
3171
3172 if Is_Type (T) and then Has_Predicates (T) then
3173 Set_Has_Predicates (Def_Id);
3174 end if;
3175
3176 -- Save the scenario for examination by the ABE Processing
3177 -- phase.
3178
3179 Record_Elaboration_Scenario (N);
3180
3181 when N_Enumeration_Type_Definition =>
3182 Enumeration_Type_Declaration (T, Def);
3183
3184 when N_Floating_Point_Definition =>
3185 Floating_Point_Type_Declaration (T, Def);
3186
3187 when N_Decimal_Fixed_Point_Definition =>
3188 Decimal_Fixed_Point_Type_Declaration (T, Def);
3189
3190 when N_Ordinary_Fixed_Point_Definition =>
3191 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3192
3193 when N_Signed_Integer_Type_Definition =>
3194 Signed_Integer_Type_Declaration (T, Def);
3195
3196 when N_Modular_Type_Definition =>
3197 Modular_Type_Declaration (T, Def);
3198
3199 when N_Record_Definition =>
3200 Record_Type_Declaration (T, N, Prev);
3201
3202 -- If declaration has a parse error, nothing to elaborate.
3203
3204 when N_Error =>
3205 null;
3206
3207 when others =>
3208 raise Program_Error;
3209 end case;
3210 end if;
3211
3212 if Etype (T) = Any_Type then
3213 return;
3214 end if;
3215
3216 -- Controlled type is not allowed in SPARK
3217
3218 if Is_Visibly_Controlled (T) then
3219 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3220 end if;
3221
3222 -- Some common processing for all types
3223
3224 Set_Depends_On_Private (T, Has_Private_Component (T));
3225 Check_Ops_From_Incomplete_Type;
3226
3227 -- Both the declared entity, and its anonymous base type if one was
3228 -- created, need freeze nodes allocated.
3229
3230 declare
3231 B : constant Entity_Id := Base_Type (T);
3232
3233 begin
3234 -- In the case where the base type differs from the first subtype, we
3235 -- pre-allocate a freeze node, and set the proper link to the first
3236 -- subtype. Freeze_Entity will use this preallocated freeze node when
3237 -- it freezes the entity.
3238
3239 -- This does not apply if the base type is a generic type, whose
3240 -- declaration is independent of the current derived definition.
3241
3242 if B /= T and then not Is_Generic_Type (B) then
3243 Ensure_Freeze_Node (B);
3244 Set_First_Subtype_Link (Freeze_Node (B), T);
3245 end if;
3246
3247 -- A type that is imported through a limited_with clause cannot
3248 -- generate any code, and thus need not be frozen. However, an access
3249 -- type with an imported designated type needs a finalization list,
3250 -- which may be referenced in some other package that has non-limited
3251 -- visibility on the designated type. Thus we must create the
3252 -- finalization list at the point the access type is frozen, to
3253 -- prevent unsatisfied references at link time.
3254
3255 if not From_Limited_With (T) or else Is_Access_Type (T) then
3256 Set_Has_Delayed_Freeze (T);
3257 end if;
3258 end;
3259
3260 -- Case where T is the full declaration of some private type which has
3261 -- been swapped in Defining_Identifier (N).
3262
3263 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3264 Process_Full_View (N, T, Def_Id);
3265
3266 -- Record the reference. The form of this is a little strange, since
3267 -- the full declaration has been swapped in. So the first parameter
3268 -- here represents the entity to which a reference is made which is
3269 -- the "real" entity, i.e. the one swapped in, and the second
3270 -- parameter provides the reference location.
3271
3272 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3273 -- since we don't want a complaint about the full type being an
3274 -- unwanted reference to the private type
3275
3276 declare
3277 B : constant Boolean := Has_Pragma_Unreferenced (T);
3278 begin
3279 Set_Has_Pragma_Unreferenced (T, False);
3280 Generate_Reference (T, T, 'c');
3281 Set_Has_Pragma_Unreferenced (T, B);
3282 end;
3283
3284 Set_Completion_Referenced (Def_Id);
3285
3286 -- For completion of incomplete type, process incomplete dependents
3287 -- and always mark the full type as referenced (it is the incomplete
3288 -- type that we get for any real reference).
3289
3290 elsif Ekind (Prev) = E_Incomplete_Type then
3291 Process_Incomplete_Dependents (N, T, Prev);
3292 Generate_Reference (Prev, Def_Id, 'c');
3293 Set_Completion_Referenced (Def_Id);
3294
3295 -- If not private type or incomplete type completion, this is a real
3296 -- definition of a new entity, so record it.
3297
3298 else
3299 Generate_Definition (Def_Id);
3300 end if;
3301
3302 -- Propagate any pending access types whose finalization masters need to
3303 -- be fully initialized from the partial to the full view. Guard against
3304 -- an illegal full view that remains unanalyzed.
3305
3306 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3307 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3308 end if;
3309
3310 if Chars (Scope (Def_Id)) = Name_System
3311 and then Chars (Def_Id) = Name_Address
3312 and then In_Predefined_Unit (N)
3313 then
3314 Set_Is_Descendant_Of_Address (Def_Id);
3315 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3316 Set_Is_Descendant_Of_Address (Prev);
3317 end if;
3318
3319 Set_Optimize_Alignment_Flags (Def_Id);
3320 Check_Eliminated (Def_Id);
3321
3322 -- If the declaration is a completion and aspects are present, apply
3323 -- them to the entity for the type which is currently the partial
3324 -- view, but which is the one that will be frozen.
3325
3326 if Has_Aspects (N) then
3327
3328 -- In most cases the partial view is a private type, and both views
3329 -- appear in different declarative parts. In the unusual case where
3330 -- the partial view is incomplete, perform the analysis on the
3331 -- full view, to prevent freezing anomalies with the corresponding
3332 -- class-wide type, which otherwise might be frozen before the
3333 -- dispatch table is built.
3334
3335 if Prev /= Def_Id
3336 and then Ekind (Prev) /= E_Incomplete_Type
3337 then
3338 Analyze_Aspect_Specifications (N, Prev);
3339
3340 -- Normal case
3341
3342 else
3343 Analyze_Aspect_Specifications (N, Def_Id);
3344 end if;
3345 end if;
3346
3347 if Is_Derived_Type (Prev)
3348 and then Def_Id /= Prev
3349 then
3350 Check_Nonoverridable_Aspects;
3351 end if;
3352 end Analyze_Full_Type_Declaration;
3353
3354 ----------------------------------
3355 -- Analyze_Incomplete_Type_Decl --
3356 ----------------------------------
3357
3358 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3359 F : constant Boolean := Is_Pure (Current_Scope);
3360 T : Entity_Id;
3361
3362 begin
3363 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3364
3365 Generate_Definition (Defining_Identifier (N));
3366
3367 -- Process an incomplete declaration. The identifier must not have been
3368 -- declared already in the scope. However, an incomplete declaration may
3369 -- appear in the private part of a package, for a private type that has
3370 -- already been declared.
3371
3372 -- In this case, the discriminants (if any) must match
3373
3374 T := Find_Type_Name (N);
3375
3376 Set_Ekind (T, E_Incomplete_Type);
3377 Set_Etype (T, T);
3378 Set_Is_First_Subtype (T);
3379 Init_Size_Align (T);
3380
3381 -- Set the SPARK mode from the current context
3382
3383 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3384 Set_SPARK_Pragma_Inherited (T);
3385
3386 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3387 -- incomplete types.
3388
3389 if Tagged_Present (N) then
3390 Set_Is_Tagged_Type (T, True);
3391 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3392 Make_Class_Wide_Type (T);
3393 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3394 end if;
3395
3396 Set_Stored_Constraint (T, No_Elist);
3397
3398 if Present (Discriminant_Specifications (N)) then
3399 Push_Scope (T);
3400 Process_Discriminants (N);
3401 End_Scope;
3402 end if;
3403
3404 -- If the type has discriminants, nontrivial subtypes may be declared
3405 -- before the full view of the type. The full views of those subtypes
3406 -- will be built after the full view of the type.
3407
3408 Set_Private_Dependents (T, New_Elmt_List);
3409 Set_Is_Pure (T, F);
3410 end Analyze_Incomplete_Type_Decl;
3411
3412 -----------------------------------
3413 -- Analyze_Interface_Declaration --
3414 -----------------------------------
3415
3416 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3417 CW : constant Entity_Id := Class_Wide_Type (T);
3418
3419 begin
3420 Set_Is_Tagged_Type (T);
3421 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3422
3423 Set_Is_Limited_Record (T, Limited_Present (Def)
3424 or else Task_Present (Def)
3425 or else Protected_Present (Def)
3426 or else Synchronized_Present (Def));
3427
3428 -- Type is abstract if full declaration carries keyword, or if previous
3429 -- partial view did.
3430
3431 Set_Is_Abstract_Type (T);
3432 Set_Is_Interface (T);
3433
3434 -- Type is a limited interface if it includes the keyword limited, task,
3435 -- protected, or synchronized.
3436
3437 Set_Is_Limited_Interface
3438 (T, Limited_Present (Def)
3439 or else Protected_Present (Def)
3440 or else Synchronized_Present (Def)
3441 or else Task_Present (Def));
3442
3443 Set_Interfaces (T, New_Elmt_List);
3444 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3445
3446 -- Complete the decoration of the class-wide entity if it was already
3447 -- built (i.e. during the creation of the limited view)
3448
3449 if Present (CW) then
3450 Set_Is_Interface (CW);
3451 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3452 end if;
3453
3454 -- Check runtime support for synchronized interfaces
3455
3456 if (Is_Task_Interface (T)
3457 or else Is_Protected_Interface (T)
3458 or else Is_Synchronized_Interface (T))
3459 and then not RTE_Available (RE_Select_Specific_Data)
3460 then
3461 Error_Msg_CRT ("synchronized interfaces", T);
3462 end if;
3463 end Analyze_Interface_Declaration;
3464
3465 -----------------------------
3466 -- Analyze_Itype_Reference --
3467 -----------------------------
3468
3469 -- Nothing to do. This node is placed in the tree only for the benefit of
3470 -- back end processing, and has no effect on the semantic processing.
3471
3472 procedure Analyze_Itype_Reference (N : Node_Id) is
3473 begin
3474 pragma Assert (Is_Itype (Itype (N)));
3475 null;
3476 end Analyze_Itype_Reference;
3477
3478 --------------------------------
3479 -- Analyze_Number_Declaration --
3480 --------------------------------
3481
3482 procedure Analyze_Number_Declaration (N : Node_Id) is
3483 E : constant Node_Id := Expression (N);
3484 Id : constant Entity_Id := Defining_Identifier (N);
3485 Index : Interp_Index;
3486 It : Interp;
3487 T : Entity_Id;
3488
3489 begin
3490 Generate_Definition (Id);
3491 Enter_Name (Id);
3492
3493 -- This is an optimization of a common case of an integer literal
3494
3495 if Nkind (E) = N_Integer_Literal then
3496 Set_Is_Static_Expression (E, True);
3497 Set_Etype (E, Universal_Integer);
3498
3499 Set_Etype (Id, Universal_Integer);
3500 Set_Ekind (Id, E_Named_Integer);
3501 Set_Is_Frozen (Id, True);
3502 return;
3503 end if;
3504
3505 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3506
3507 -- Process expression, replacing error by integer zero, to avoid
3508 -- cascaded errors or aborts further along in the processing
3509
3510 -- Replace Error by integer zero, which seems least likely to cause
3511 -- cascaded errors.
3512
3513 if E = Error then
3514 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3515 Set_Error_Posted (E);
3516 end if;
3517
3518 Analyze (E);
3519
3520 -- Verify that the expression is static and numeric. If
3521 -- the expression is overloaded, we apply the preference
3522 -- rule that favors root numeric types.
3523
3524 if not Is_Overloaded (E) then
3525 T := Etype (E);
3526 if Has_Dynamic_Predicate_Aspect (T) then
3527 Error_Msg_N
3528 ("subtype has dynamic predicate, "
3529 & "not allowed in number declaration", N);
3530 end if;
3531
3532 else
3533 T := Any_Type;
3534
3535 Get_First_Interp (E, Index, It);
3536 while Present (It.Typ) loop
3537 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3538 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3539 then
3540 if T = Any_Type then
3541 T := It.Typ;
3542
3543 elsif It.Typ = Universal_Real
3544 or else
3545 It.Typ = Universal_Integer
3546 then
3547 -- Choose universal interpretation over any other
3548
3549 T := It.Typ;
3550 exit;
3551 end if;
3552 end if;
3553
3554 Get_Next_Interp (Index, It);
3555 end loop;
3556 end if;
3557
3558 if Is_Integer_Type (T) then
3559 Resolve (E, T);
3560 Set_Etype (Id, Universal_Integer);
3561 Set_Ekind (Id, E_Named_Integer);
3562
3563 elsif Is_Real_Type (T) then
3564
3565 -- Because the real value is converted to universal_real, this is a
3566 -- legal context for a universal fixed expression.
3567
3568 if T = Universal_Fixed then
3569 declare
3570 Loc : constant Source_Ptr := Sloc (N);
3571 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3572 Subtype_Mark =>
3573 New_Occurrence_Of (Universal_Real, Loc),
3574 Expression => Relocate_Node (E));
3575
3576 begin
3577 Rewrite (E, Conv);
3578 Analyze (E);
3579 end;
3580
3581 elsif T = Any_Fixed then
3582 Error_Msg_N ("illegal context for mixed mode operation", E);
3583
3584 -- Expression is of the form : universal_fixed * integer. Try to
3585 -- resolve as universal_real.
3586
3587 T := Universal_Real;
3588 Set_Etype (E, T);
3589 end if;
3590
3591 Resolve (E, T);
3592 Set_Etype (Id, Universal_Real);
3593 Set_Ekind (Id, E_Named_Real);
3594
3595 else
3596 Wrong_Type (E, Any_Numeric);
3597 Resolve (E, T);
3598
3599 Set_Etype (Id, T);
3600 Set_Ekind (Id, E_Constant);
3601 Set_Never_Set_In_Source (Id, True);
3602 Set_Is_True_Constant (Id, True);
3603 return;
3604 end if;
3605
3606 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3607 Set_Etype (E, Etype (Id));
3608 end if;
3609
3610 if not Is_OK_Static_Expression (E) then
3611 Flag_Non_Static_Expr
3612 ("non-static expression used in number declaration!", E);
3613 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3614 Set_Etype (E, Any_Type);
3615 end if;
3616
3617 Analyze_Dimension (N);
3618 end Analyze_Number_Declaration;
3619
3620 --------------------------------
3621 -- Analyze_Object_Declaration --
3622 --------------------------------
3623
3624 -- WARNING: This routine manages Ghost regions. Return statements must be
3625 -- replaced by gotos which jump to the end of the routine and restore the
3626 -- Ghost mode.
3627
3628 procedure Analyze_Object_Declaration (N : Node_Id) is
3629 Loc : constant Source_Ptr := Sloc (N);
3630 Id : constant Entity_Id := Defining_Identifier (N);
3631 Act_T : Entity_Id;
3632 T : Entity_Id;
3633
3634 E : Node_Id := Expression (N);
3635 -- E is set to Expression (N) throughout this routine. When Expression
3636 -- (N) is modified, E is changed accordingly.
3637
3638 Prev_Entity : Entity_Id := Empty;
3639
3640 procedure Check_Dynamic_Object (Typ : Entity_Id);
3641 -- A library-level object with non-static discriminant constraints may
3642 -- require dynamic allocation. The declaration is illegal if the
3643 -- profile includes the restriction No_Implicit_Heap_Allocations.
3644
3645 procedure Check_For_Null_Excluding_Components
3646 (Obj_Typ : Entity_Id;
3647 Obj_Decl : Node_Id);
3648 -- Verify that each null-excluding component of object declaration
3649 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3650 -- a compile-time warning if this is not the case.
3651
3652 function Count_Tasks (T : Entity_Id) return Uint;
3653 -- This function is called when a non-generic library level object of a
3654 -- task type is declared. Its function is to count the static number of
3655 -- tasks declared within the type (it is only called if Has_Task is set
3656 -- for T). As a side effect, if an array of tasks with non-static bounds
3657 -- or a variant record type is encountered, Check_Restriction is called
3658 -- indicating the count is unknown.
3659
3660 function Delayed_Aspect_Present return Boolean;
3661 -- If the declaration has an expression that is an aggregate, and it
3662 -- has aspects that require delayed analysis, the resolution of the
3663 -- aggregate must be deferred to the freeze point of the objet. This
3664 -- special processing was created for address clauses, but it must
3665 -- also apply to Alignment. This must be done before the aspect
3666 -- specifications are analyzed because we must handle the aggregate
3667 -- before the analysis of the object declaration is complete.
3668
3669 -- Any other relevant delayed aspects on object declarations ???
3670
3671 --------------------------
3672 -- Check_Dynamic_Object --
3673 --------------------------
3674
3675 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3676 Comp : Entity_Id;
3677 Obj_Type : Entity_Id;
3678
3679 begin
3680 Obj_Type := Typ;
3681
3682 if Is_Private_Type (Obj_Type)
3683 and then Present (Full_View (Obj_Type))
3684 then
3685 Obj_Type := Full_View (Obj_Type);
3686 end if;
3687
3688 if Known_Static_Esize (Obj_Type) then
3689 return;
3690 end if;
3691
3692 if Restriction_Active (No_Implicit_Heap_Allocations)
3693 and then Expander_Active
3694 and then Has_Discriminants (Obj_Type)
3695 then
3696 Comp := First_Component (Obj_Type);
3697 while Present (Comp) loop
3698 if Known_Static_Esize (Etype (Comp))
3699 or else Size_Known_At_Compile_Time (Etype (Comp))
3700 then
3701 null;
3702
3703 elsif not Discriminated_Size (Comp)
3704 and then Comes_From_Source (Comp)
3705 then
3706 Error_Msg_NE
3707 ("component& of non-static size will violate restriction "
3708 & "No_Implicit_Heap_Allocation?", N, Comp);
3709
3710 elsif Is_Record_Type (Etype (Comp)) then
3711 Check_Dynamic_Object (Etype (Comp));
3712 end if;
3713
3714 Next_Component (Comp);
3715 end loop;
3716 end if;
3717 end Check_Dynamic_Object;
3718
3719 -----------------------------------------
3720 -- Check_For_Null_Excluding_Components --
3721 -----------------------------------------
3722
3723 procedure Check_For_Null_Excluding_Components
3724 (Obj_Typ : Entity_Id;
3725 Obj_Decl : Node_Id)
3726 is
3727 procedure Check_Component
3728 (Comp_Typ : Entity_Id;
3729 Comp_Decl : Node_Id := Empty;
3730 Array_Comp : Boolean := False);
3731 -- Apply a compile-time null-exclusion check on a component denoted
3732 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3733 -- subcomponents (if any).
3734
3735 ---------------------
3736 -- Check_Component --
3737 ---------------------
3738
3739 procedure Check_Component
3740 (Comp_Typ : Entity_Id;
3741 Comp_Decl : Node_Id := Empty;
3742 Array_Comp : Boolean := False)
3743 is
3744 Comp : Entity_Id;
3745 T : Entity_Id;
3746
3747 begin
3748 -- Do not consider internally-generated components or those that
3749 -- are already initialized.
3750
3751 if Present (Comp_Decl)
3752 and then (not Comes_From_Source (Comp_Decl)
3753 or else Present (Expression (Comp_Decl)))
3754 then
3755 return;
3756 end if;
3757
3758 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3759 and then Present (Full_View (Comp_Typ))
3760 then
3761 T := Full_View (Comp_Typ);
3762 else
3763 T := Comp_Typ;
3764 end if;
3765
3766 -- Verify a component of a null-excluding access type
3767
3768 if Is_Access_Type (T)
3769 and then Can_Never_Be_Null (T)
3770 then
3771 if Comp_Decl = Obj_Decl then
3772 Null_Exclusion_Static_Checks
3773 (N => Obj_Decl,
3774 Comp => Empty,
3775 Array_Comp => Array_Comp);
3776
3777 else
3778 Null_Exclusion_Static_Checks
3779 (N => Obj_Decl,
3780 Comp => Comp_Decl,
3781 Array_Comp => Array_Comp);
3782 end if;
3783
3784 -- Check array components
3785
3786 elsif Is_Array_Type (T) then
3787
3788 -- There is no suitable component when the object is of an
3789 -- array type. However, a namable component may appear at some
3790 -- point during the recursive inspection, but not at the top
3791 -- level. At the top level just indicate array component case.
3792
3793 if Comp_Decl = Obj_Decl then
3794 Check_Component (Component_Type (T), Array_Comp => True);
3795 else
3796 Check_Component (Component_Type (T), Comp_Decl);
3797 end if;
3798
3799 -- Verify all components of type T
3800
3801 -- Note: No checks are performed on types with discriminants due
3802 -- to complexities involving variants. ???
3803
3804 elsif (Is_Concurrent_Type (T)
3805 or else Is_Incomplete_Or_Private_Type (T)
3806 or else Is_Record_Type (T))
3807 and then not Has_Discriminants (T)
3808 then
3809 Comp := First_Component (T);
3810 while Present (Comp) loop
3811 Check_Component (Etype (Comp), Parent (Comp));
3812
3813 Comp := Next_Component (Comp);
3814 end loop;
3815 end if;
3816 end Check_Component;
3817
3818 -- Start processing for Check_For_Null_Excluding_Components
3819
3820 begin
3821 Check_Component (Obj_Typ, Obj_Decl);
3822 end Check_For_Null_Excluding_Components;
3823
3824 -----------------
3825 -- Count_Tasks --
3826 -----------------
3827
3828 function Count_Tasks (T : Entity_Id) return Uint is
3829 C : Entity_Id;
3830 X : Node_Id;
3831 V : Uint;
3832
3833 begin
3834 if Is_Task_Type (T) then
3835 return Uint_1;
3836
3837 elsif Is_Record_Type (T) then
3838 if Has_Discriminants (T) then
3839 Check_Restriction (Max_Tasks, N);
3840 return Uint_0;
3841
3842 else
3843 V := Uint_0;
3844 C := First_Component (T);
3845 while Present (C) loop
3846 V := V + Count_Tasks (Etype (C));
3847 Next_Component (C);
3848 end loop;
3849
3850 return V;
3851 end if;
3852
3853 elsif Is_Array_Type (T) then
3854 X := First_Index (T);
3855 V := Count_Tasks (Component_Type (T));
3856 while Present (X) loop
3857 C := Etype (X);
3858
3859 if not Is_OK_Static_Subtype (C) then
3860 Check_Restriction (Max_Tasks, N);
3861 return Uint_0;
3862 else
3863 V := V * (UI_Max (Uint_0,
3864 Expr_Value (Type_High_Bound (C)) -
3865 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3866 end if;
3867
3868 Next_Index (X);
3869 end loop;
3870
3871 return V;
3872
3873 else
3874 return Uint_0;
3875 end if;
3876 end Count_Tasks;
3877
3878 ----------------------------
3879 -- Delayed_Aspect_Present --
3880 ----------------------------
3881
3882 function Delayed_Aspect_Present return Boolean is
3883 A : Node_Id;
3884 A_Id : Aspect_Id;
3885
3886 begin
3887 if Present (Aspect_Specifications (N)) then
3888 A := First (Aspect_Specifications (N));
3889 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3890 while Present (A) loop
3891 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3892 return True;
3893 end if;
3894
3895 Next (A);
3896 end loop;
3897 end if;
3898
3899 return False;
3900 end Delayed_Aspect_Present;
3901
3902 -- Local variables
3903
3904 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3905 -- Save the Ghost mode to restore on exit
3906
3907 Related_Id : Entity_Id;
3908
3909 -- Start of processing for Analyze_Object_Declaration
3910
3911 begin
3912 -- There are three kinds of implicit types generated by an
3913 -- object declaration:
3914
3915 -- 1. Those generated by the original Object Definition
3916
3917 -- 2. Those generated by the Expression
3918
3919 -- 3. Those used to constrain the Object Definition with the
3920 -- expression constraints when the definition is unconstrained.
3921
3922 -- They must be generated in this order to avoid order of elaboration
3923 -- issues. Thus the first step (after entering the name) is to analyze
3924 -- the object definition.
3925
3926 if Constant_Present (N) then
3927 Prev_Entity := Current_Entity_In_Scope (Id);
3928
3929 if Present (Prev_Entity)
3930 and then
3931 -- If the homograph is an implicit subprogram, it is overridden
3932 -- by the current declaration.
3933
3934 ((Is_Overloadable (Prev_Entity)
3935 and then Is_Inherited_Operation (Prev_Entity))
3936
3937 -- The current object is a discriminal generated for an entry
3938 -- family index. Even though the index is a constant, in this
3939 -- particular context there is no true constant redeclaration.
3940 -- Enter_Name will handle the visibility.
3941
3942 or else
3943 (Is_Discriminal (Id)
3944 and then Ekind (Discriminal_Link (Id)) =
3945 E_Entry_Index_Parameter)
3946
3947 -- The current object is the renaming for a generic declared
3948 -- within the instance.
3949
3950 or else
3951 (Ekind (Prev_Entity) = E_Package
3952 and then Nkind (Parent (Prev_Entity)) =
3953 N_Package_Renaming_Declaration
3954 and then not Comes_From_Source (Prev_Entity)
3955 and then
3956 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3957
3958 -- The entity may be a homonym of a private component of the
3959 -- enclosing protected object, for which we create a local
3960 -- renaming declaration. The declaration is legal, even if
3961 -- useless when it just captures that component.
3962
3963 or else
3964 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3965 and then Nkind (Parent (Prev_Entity)) =
3966 N_Object_Renaming_Declaration))
3967 then
3968 Prev_Entity := Empty;
3969 end if;
3970 end if;
3971
3972 if Present (Prev_Entity) then
3973
3974 -- The object declaration is Ghost when it completes a deferred Ghost
3975 -- constant.
3976
3977 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3978
3979 Constant_Redeclaration (Id, N, T);
3980
3981 Generate_Reference (Prev_Entity, Id, 'c');
3982 Set_Completion_Referenced (Id);
3983
3984 if Error_Posted (N) then
3985
3986 -- Type mismatch or illegal redeclaration; do not analyze
3987 -- expression to avoid cascaded errors.
3988
3989 T := Find_Type_Of_Object (Object_Definition (N), N);
3990 Set_Etype (Id, T);
3991 Set_Ekind (Id, E_Variable);
3992 goto Leave;
3993 end if;
3994
3995 -- In the normal case, enter identifier at the start to catch premature
3996 -- usage in the initialization expression.
3997
3998 else
3999 Generate_Definition (Id);
4000 Enter_Name (Id);
4001
4002 Mark_Coextensions (N, Object_Definition (N));
4003
4004 T := Find_Type_Of_Object (Object_Definition (N), N);
4005
4006 if Nkind (Object_Definition (N)) = N_Access_Definition
4007 and then Present
4008 (Access_To_Subprogram_Definition (Object_Definition (N)))
4009 and then Protected_Present
4010 (Access_To_Subprogram_Definition (Object_Definition (N)))
4011 then
4012 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4013 end if;
4014
4015 if Error_Posted (Id) then
4016 Set_Etype (Id, T);
4017 Set_Ekind (Id, E_Variable);
4018 goto Leave;
4019 end if;
4020 end if;
4021
4022 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4023 -- out some static checks.
4024
4025 if Ada_Version >= Ada_2005 then
4026
4027 -- In case of aggregates we must also take care of the correct
4028 -- initialization of nested aggregates bug this is done at the
4029 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4030
4031 if Can_Never_Be_Null (T) then
4032 if Present (Expression (N))
4033 and then Nkind (Expression (N)) = N_Aggregate
4034 then
4035 null;
4036
4037 else
4038 declare
4039 Save_Typ : constant Entity_Id := Etype (Id);
4040 begin
4041 Set_Etype (Id, T); -- Temp. decoration for static checks
4042 Null_Exclusion_Static_Checks (N);
4043 Set_Etype (Id, Save_Typ);
4044 end;
4045 end if;
4046
4047 -- We might be dealing with an object of a composite type containing
4048 -- null-excluding components without an aggregate, so we must verify
4049 -- that such components have default initialization.
4050
4051 else
4052 Check_For_Null_Excluding_Components (T, N);
4053 end if;
4054 end if;
4055
4056 -- Object is marked pure if it is in a pure scope
4057
4058 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4059
4060 -- If deferred constant, make sure context is appropriate. We detect
4061 -- a deferred constant as a constant declaration with no expression.
4062 -- A deferred constant can appear in a package body if its completion
4063 -- is by means of an interface pragma.
4064
4065 if Constant_Present (N) and then No (E) then
4066
4067 -- A deferred constant may appear in the declarative part of the
4068 -- following constructs:
4069
4070 -- blocks
4071 -- entry bodies
4072 -- extended return statements
4073 -- package specs
4074 -- package bodies
4075 -- subprogram bodies
4076 -- task bodies
4077
4078 -- When declared inside a package spec, a deferred constant must be
4079 -- completed by a full constant declaration or pragma Import. In all
4080 -- other cases, the only proper completion is pragma Import. Extended
4081 -- return statements are flagged as invalid contexts because they do
4082 -- not have a declarative part and so cannot accommodate the pragma.
4083
4084 if Ekind (Current_Scope) = E_Return_Statement then
4085 Error_Msg_N
4086 ("invalid context for deferred constant declaration (RM 7.4)",
4087 N);
4088 Error_Msg_N
4089 ("\declaration requires an initialization expression",
4090 N);
4091 Set_Constant_Present (N, False);
4092
4093 -- In Ada 83, deferred constant must be of private type
4094
4095 elsif not Is_Private_Type (T) then
4096 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4097 Error_Msg_N
4098 ("(Ada 83) deferred constant must be private type", N);
4099 end if;
4100 end if;
4101
4102 -- If not a deferred constant, then the object declaration freezes
4103 -- its type, unless the object is of an anonymous type and has delayed
4104 -- aspects. In that case the type is frozen when the object itself is.
4105
4106 else
4107 Check_Fully_Declared (T, N);
4108
4109 if Has_Delayed_Aspects (Id)
4110 and then Is_Array_Type (T)
4111 and then Is_Itype (T)
4112 then
4113 Set_Has_Delayed_Freeze (T);
4114 else
4115 Freeze_Before (N, T);
4116 end if;
4117 end if;
4118
4119 -- If the object was created by a constrained array definition, then
4120 -- set the link in both the anonymous base type and anonymous subtype
4121 -- that are built to represent the array type to point to the object.
4122
4123 if Nkind (Object_Definition (Declaration_Node (Id))) =
4124 N_Constrained_Array_Definition
4125 then
4126 Set_Related_Array_Object (T, Id);
4127 Set_Related_Array_Object (Base_Type (T), Id);
4128 end if;
4129
4130 -- Special checks for protected objects not at library level
4131
4132 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4133 Check_Restriction (No_Local_Protected_Objects, Id);
4134
4135 -- Protected objects with interrupt handlers must be at library level
4136
4137 -- Ada 2005: This test is not needed (and the corresponding clause
4138 -- in the RM is removed) because accessibility checks are sufficient
4139 -- to make handlers not at the library level illegal.
4140
4141 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4142 -- applies to the '95 version of the language as well.
4143
4144 if Is_Protected_Type (T)
4145 and then Has_Interrupt_Handler (T)
4146 and then Ada_Version < Ada_95
4147 then
4148 Error_Msg_N
4149 ("interrupt object can only be declared at library level", Id);
4150 end if;
4151 end if;
4152
4153 -- Check for violation of No_Local_Timing_Events
4154
4155 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4156 Check_Restriction (No_Local_Timing_Events, Id);
4157 end if;
4158
4159 -- The actual subtype of the object is the nominal subtype, unless
4160 -- the nominal one is unconstrained and obtained from the expression.
4161
4162 Act_T := T;
4163
4164 -- These checks should be performed before the initialization expression
4165 -- is considered, so that the Object_Definition node is still the same
4166 -- as in source code.
4167
4168 -- In SPARK, the nominal subtype is always given by a subtype mark
4169 -- and must not be unconstrained. (The only exception to this is the
4170 -- acceptance of declarations of constants of type String.)
4171
4172 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4173 then
4174 Check_SPARK_05_Restriction
4175 ("subtype mark required", Object_Definition (N));
4176
4177 elsif Is_Array_Type (T)
4178 and then not Is_Constrained (T)
4179 and then T /= Standard_String
4180 then
4181 Check_SPARK_05_Restriction
4182 ("subtype mark of constrained type expected",
4183 Object_Definition (N));
4184 end if;
4185
4186 if Is_Library_Level_Entity (Id) then
4187 Check_Dynamic_Object (T);
4188 end if;
4189
4190 -- There are no aliased objects in SPARK
4191
4192 if Aliased_Present (N) then
4193 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4194 end if;
4195
4196 -- Process initialization expression if present and not in error
4197
4198 if Present (E) and then E /= Error then
4199
4200 -- Generate an error in case of CPP class-wide object initialization.
4201 -- Required because otherwise the expansion of the class-wide
4202 -- assignment would try to use 'size to initialize the object
4203 -- (primitive that is not available in CPP tagged types).
4204
4205 if Is_Class_Wide_Type (Act_T)
4206 and then
4207 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4208 or else
4209 (Present (Full_View (Root_Type (Etype (Act_T))))
4210 and then
4211 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4212 then
4213 Error_Msg_N
4214 ("predefined assignment not available for 'C'P'P tagged types",
4215 E);
4216 end if;
4217
4218 Mark_Coextensions (N, E);
4219 Analyze (E);
4220
4221 -- In case of errors detected in the analysis of the expression,
4222 -- decorate it with the expected type to avoid cascaded errors
4223
4224 if No (Etype (E)) then
4225 Set_Etype (E, T);
4226 end if;
4227
4228 -- If an initialization expression is present, then we set the
4229 -- Is_True_Constant flag. It will be reset if this is a variable
4230 -- and it is indeed modified.
4231
4232 Set_Is_True_Constant (Id, True);
4233
4234 -- If we are analyzing a constant declaration, set its completion
4235 -- flag after analyzing and resolving the expression.
4236
4237 if Constant_Present (N) then
4238 Set_Has_Completion (Id);
4239 end if;
4240
4241 -- Set type and resolve (type may be overridden later on). Note:
4242 -- Ekind (Id) must still be E_Void at this point so that incorrect
4243 -- early usage within E is properly diagnosed.
4244
4245 Set_Etype (Id, T);
4246
4247 -- If the expression is an aggregate we must look ahead to detect
4248 -- the possible presence of an address clause, and defer resolution
4249 -- and expansion of the aggregate to the freeze point of the entity.
4250
4251 -- This is not always legal because the aggregate may contain other
4252 -- references that need freezing, e.g. references to other entities
4253 -- with address clauses. In any case, when compiling with -gnatI the
4254 -- presence of the address clause must be ignored.
4255
4256 if Comes_From_Source (N)
4257 and then Expander_Active
4258 and then Nkind (E) = N_Aggregate
4259 and then
4260 ((Present (Following_Address_Clause (N))
4261 and then not Ignore_Rep_Clauses)
4262 or else Delayed_Aspect_Present)
4263 then
4264 Set_Etype (E, T);
4265
4266 else
4267 Resolve (E, T);
4268 end if;
4269
4270 -- No further action needed if E is a call to an inlined function
4271 -- which returns an unconstrained type and it has been expanded into
4272 -- a procedure call. In that case N has been replaced by an object
4273 -- declaration without initializing expression and it has been
4274 -- analyzed (see Expand_Inlined_Call).
4275
4276 if Back_End_Inlining
4277 and then Expander_Active
4278 and then Nkind (E) = N_Function_Call
4279 and then Nkind (Name (E)) in N_Has_Entity
4280 and then Is_Inlined (Entity (Name (E)))
4281 and then not Is_Constrained (Etype (E))
4282 and then Analyzed (N)
4283 and then No (Expression (N))
4284 then
4285 goto Leave;
4286 end if;
4287
4288 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4289 -- node (which was marked already-analyzed), we need to set the type
4290 -- to something other than Any_Access in order to keep gigi happy.
4291
4292 if Etype (E) = Any_Access then
4293 Set_Etype (E, T);
4294 end if;
4295
4296 -- If the object is an access to variable, the initialization
4297 -- expression cannot be an access to constant.
4298
4299 if Is_Access_Type (T)
4300 and then not Is_Access_Constant (T)
4301 and then Is_Access_Type (Etype (E))
4302 and then Is_Access_Constant (Etype (E))
4303 then
4304 Error_Msg_N
4305 ("access to variable cannot be initialized with an "
4306 & "access-to-constant expression", E);
4307 end if;
4308
4309 if not Assignment_OK (N) then
4310 Check_Initialization (T, E);
4311 end if;
4312
4313 Check_Unset_Reference (E);
4314
4315 -- If this is a variable, then set current value. If this is a
4316 -- declared constant of a scalar type with a static expression,
4317 -- indicate that it is always valid.
4318
4319 if not Constant_Present (N) then
4320 if Compile_Time_Known_Value (E) then
4321 Set_Current_Value (Id, E);
4322 end if;
4323
4324 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4325 Set_Is_Known_Valid (Id);
4326 end if;
4327
4328 -- Deal with setting of null flags
4329
4330 if Is_Access_Type (T) then
4331 if Known_Non_Null (E) then
4332 Set_Is_Known_Non_Null (Id, True);
4333 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4334 Set_Is_Known_Null (Id, True);
4335 end if;
4336 end if;
4337
4338 -- Check incorrect use of dynamically tagged expressions
4339
4340 if Is_Tagged_Type (T) then
4341 Check_Dynamically_Tagged_Expression
4342 (Expr => E,
4343 Typ => T,
4344 Related_Nod => N);
4345 end if;
4346
4347 Apply_Scalar_Range_Check (E, T);
4348 Apply_Static_Length_Check (E, T);
4349
4350 if Nkind (Original_Node (N)) = N_Object_Declaration
4351 and then Comes_From_Source (Original_Node (N))
4352
4353 -- Only call test if needed
4354
4355 and then Restriction_Check_Required (SPARK_05)
4356 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4357 then
4358 Check_SPARK_05_Restriction
4359 ("initialization expression is not appropriate", E);
4360 end if;
4361
4362 -- A formal parameter of a specific tagged type whose related
4363 -- subprogram is subject to pragma Extensions_Visible with value
4364 -- "False" cannot be implicitly converted to a class-wide type by
4365 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4366 -- not consider internally generated expressions.
4367
4368 if Is_Class_Wide_Type (T)
4369 and then Comes_From_Source (E)
4370 and then Is_EVF_Expression (E)
4371 then
4372 Error_Msg_N
4373 ("formal parameter cannot be implicitly converted to "
4374 & "class-wide type when Extensions_Visible is False", E);
4375 end if;
4376 end if;
4377
4378 -- If the No_Streams restriction is set, check that the type of the
4379 -- object is not, and does not contain, any subtype derived from
4380 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4381 -- Has_Stream just for efficiency reasons. There is no point in
4382 -- spending time on a Has_Stream check if the restriction is not set.
4383
4384 if Restriction_Check_Required (No_Streams) then
4385 if Has_Stream (T) then
4386 Check_Restriction (No_Streams, N);
4387 end if;
4388 end if;
4389
4390 -- Deal with predicate check before we start to do major rewriting. It
4391 -- is OK to initialize and then check the initialized value, since the
4392 -- object goes out of scope if we get a predicate failure. Note that we
4393 -- do this in the analyzer and not the expander because the analyzer
4394 -- does some substantial rewriting in some cases.
4395
4396 -- We need a predicate check if the type has predicates that are not
4397 -- ignored, and if either there is an initializing expression, or for
4398 -- default initialization when we have at least one case of an explicit
4399 -- default initial value and then this is not an internal declaration
4400 -- whose initialization comes later (as for an aggregate expansion).
4401
4402 if not Suppress_Assignment_Checks (N)
4403 and then Present (Predicate_Function (T))
4404 and then not Predicates_Ignored (T)
4405 and then not No_Initialization (N)
4406 and then
4407 (Present (E)
4408 or else
4409 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4410 then
4411 -- If the type has a static predicate and the expression is known at
4412 -- compile time, see if the expression satisfies the predicate.
4413
4414 if Present (E) then
4415 Check_Expression_Against_Static_Predicate (E, T);
4416 end if;
4417
4418 -- If the type is a null record and there is no explicit initial
4419 -- expression, no predicate check applies.
4420
4421 if No (E) and then Is_Null_Record_Type (T) then
4422 null;
4423
4424 -- Do not generate a predicate check if the initialization expression
4425 -- is a type conversion because the conversion has been subjected to
4426 -- the same check. This is a small optimization which avoid redundant
4427 -- checks.
4428
4429 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4430 null;
4431
4432 else
4433 Insert_After (N,
4434 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4435 end if;
4436 end if;
4437
4438 -- Case of unconstrained type
4439
4440 if not Is_Definite_Subtype (T) then
4441
4442 -- In SPARK, a declaration of unconstrained type is allowed
4443 -- only for constants of type string.
4444
4445 if Is_String_Type (T) and then not Constant_Present (N) then
4446 Check_SPARK_05_Restriction
4447 ("declaration of object of unconstrained type not allowed", N);
4448 end if;
4449
4450 -- Nothing to do in deferred constant case
4451
4452 if Constant_Present (N) and then No (E) then
4453 null;
4454
4455 -- Case of no initialization present
4456
4457 elsif No (E) then
4458 if No_Initialization (N) then
4459 null;
4460
4461 elsif Is_Class_Wide_Type (T) then
4462 Error_Msg_N
4463 ("initialization required in class-wide declaration ", N);
4464
4465 else
4466 Error_Msg_N
4467 ("unconstrained subtype not allowed (need initialization)",
4468 Object_Definition (N));
4469
4470 if Is_Record_Type (T) and then Has_Discriminants (T) then
4471 Error_Msg_N
4472 ("\provide initial value or explicit discriminant values",
4473 Object_Definition (N));
4474
4475 Error_Msg_NE
4476 ("\or give default discriminant values for type&",
4477 Object_Definition (N), T);
4478
4479 elsif Is_Array_Type (T) then
4480 Error_Msg_N
4481 ("\provide initial value or explicit array bounds",
4482 Object_Definition (N));
4483 end if;
4484 end if;
4485
4486 -- Case of initialization present but in error. Set initial
4487 -- expression as absent (but do not make above complaints)
4488
4489 elsif E = Error then
4490 Set_Expression (N, Empty);
4491 E := Empty;
4492
4493 -- Case of initialization present
4494
4495 else
4496 -- Check restrictions in Ada 83
4497
4498 if not Constant_Present (N) then
4499
4500 -- Unconstrained variables not allowed in Ada 83 mode
4501
4502 if Ada_Version = Ada_83
4503 and then Comes_From_Source (Object_Definition (N))
4504 then
4505 Error_Msg_N
4506 ("(Ada 83) unconstrained variable not allowed",
4507 Object_Definition (N));
4508 end if;
4509 end if;
4510
4511 -- Now we constrain the variable from the initializing expression
4512
4513 -- If the expression is an aggregate, it has been expanded into
4514 -- individual assignments. Retrieve the actual type from the
4515 -- expanded construct.
4516
4517 if Is_Array_Type (T)
4518 and then No_Initialization (N)
4519 and then Nkind (Original_Node (E)) = N_Aggregate
4520 then
4521 Act_T := Etype (E);
4522
4523 -- In case of class-wide interface object declarations we delay
4524 -- the generation of the equivalent record type declarations until
4525 -- its expansion because there are cases in they are not required.
4526
4527 elsif Is_Interface (T) then
4528 null;
4529
4530 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4531 -- we should prevent the generation of another Itype with the
4532 -- same name as the one already generated, or we end up with
4533 -- two identical types in GNATprove.
4534
4535 elsif GNATprove_Mode then
4536 null;
4537
4538 -- If the type is an unchecked union, no subtype can be built from
4539 -- the expression. Rewrite declaration as a renaming, which the
4540 -- back-end can handle properly. This is a rather unusual case,
4541 -- because most unchecked_union declarations have default values
4542 -- for discriminants and are thus not indefinite.
4543
4544 elsif Is_Unchecked_Union (T) then
4545 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4546 Set_Ekind (Id, E_Constant);
4547 else
4548 Set_Ekind (Id, E_Variable);
4549 end if;
4550
4551 Rewrite (N,
4552 Make_Object_Renaming_Declaration (Loc,
4553 Defining_Identifier => Id,
4554 Subtype_Mark => New_Occurrence_Of (T, Loc),
4555 Name => E));
4556
4557 Set_Renamed_Object (Id, E);
4558 Freeze_Before (N, T);
4559 Set_Is_Frozen (Id);
4560 goto Leave;
4561
4562 else
4563 -- Ensure that the generated subtype has a unique external name
4564 -- when the related object is public. This guarantees that the
4565 -- subtype and its bounds will not be affected by switches or
4566 -- pragmas that may offset the internal counter due to extra
4567 -- generated code.
4568
4569 if Is_Public (Id) then
4570 Related_Id := Id;
4571 else
4572 Related_Id := Empty;
4573 end if;
4574
4575 Expand_Subtype_From_Expr
4576 (N => N,
4577 Unc_Type => T,
4578 Subtype_Indic => Object_Definition (N),
4579 Exp => E,
4580 Related_Id => Related_Id);
4581
4582 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4583 end if;
4584
4585 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4586
4587 if Aliased_Present (N) then
4588 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4589 end if;
4590
4591 Freeze_Before (N, Act_T);
4592 Freeze_Before (N, T);
4593 end if;
4594
4595 elsif Is_Array_Type (T)
4596 and then No_Initialization (N)
4597 and then (Nkind (Original_Node (E)) = N_Aggregate
4598 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4599 and then Nkind (Original_Node (Expression
4600 (Original_Node (E)))) = N_Aggregate))
4601 then
4602 if not Is_Entity_Name (Object_Definition (N)) then
4603 Act_T := Etype (E);
4604 Check_Compile_Time_Size (Act_T);
4605
4606 if Aliased_Present (N) then
4607 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4608 end if;
4609 end if;
4610
4611 -- When the given object definition and the aggregate are specified
4612 -- independently, and their lengths might differ do a length check.
4613 -- This cannot happen if the aggregate is of the form (others =>...)
4614
4615 if not Is_Constrained (T) then
4616 null;
4617
4618 elsif Nkind (E) = N_Raise_Constraint_Error then
4619
4620 -- Aggregate is statically illegal. Place back in declaration
4621
4622 Set_Expression (N, E);
4623 Set_No_Initialization (N, False);
4624
4625 elsif T = Etype (E) then
4626 null;
4627
4628 elsif Nkind (E) = N_Aggregate
4629 and then Present (Component_Associations (E))
4630 and then Present (Choice_List (First (Component_Associations (E))))
4631 and then
4632 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4633 N_Others_Choice
4634 then
4635 null;
4636
4637 else
4638 Apply_Length_Check (E, T);
4639 end if;
4640
4641 -- If the type is limited unconstrained with defaulted discriminants and
4642 -- there is no expression, then the object is constrained by the
4643 -- defaults, so it is worthwhile building the corresponding subtype.
4644
4645 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4646 and then not Is_Constrained (T)
4647 and then Has_Discriminants (T)
4648 then
4649 if No (E) then
4650 Act_T := Build_Default_Subtype (T, N);
4651 else
4652 -- Ada 2005: A limited object may be initialized by means of an
4653 -- aggregate. If the type has default discriminants it has an
4654 -- unconstrained nominal type, Its actual subtype will be obtained
4655 -- from the aggregate, and not from the default discriminants.
4656
4657 Act_T := Etype (E);
4658 end if;
4659
4660 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4661
4662 elsif Nkind (E) = N_Function_Call
4663 and then Constant_Present (N)
4664 and then Has_Unconstrained_Elements (Etype (E))
4665 then
4666 -- The back-end has problems with constants of a discriminated type
4667 -- with defaults, if the initial value is a function call. We
4668 -- generate an intermediate temporary that will receive a reference
4669 -- to the result of the call. The initialization expression then
4670 -- becomes a dereference of that temporary.
4671
4672 Remove_Side_Effects (E);
4673
4674 -- If this is a constant declaration of an unconstrained type and
4675 -- the initialization is an aggregate, we can use the subtype of the
4676 -- aggregate for the declared entity because it is immutable.
4677
4678 elsif not Is_Constrained (T)
4679 and then Has_Discriminants (T)
4680 and then Constant_Present (N)
4681 and then not Has_Unchecked_Union (T)
4682 and then Nkind (E) = N_Aggregate
4683 then
4684 Act_T := Etype (E);
4685 end if;
4686
4687 -- Check No_Wide_Characters restriction
4688
4689 Check_Wide_Character_Restriction (T, Object_Definition (N));
4690
4691 -- Indicate this is not set in source. Certainly true for constants, and
4692 -- true for variables so far (will be reset for a variable if and when
4693 -- we encounter a modification in the source).
4694
4695 Set_Never_Set_In_Source (Id);
4696
4697 -- Now establish the proper kind and type of the object
4698
4699 if Constant_Present (N) then
4700 Set_Ekind (Id, E_Constant);
4701 Set_Is_True_Constant (Id);
4702
4703 else
4704 Set_Ekind (Id, E_Variable);
4705
4706 -- A variable is set as shared passive if it appears in a shared
4707 -- passive package, and is at the outer level. This is not done for
4708 -- entities generated during expansion, because those are always
4709 -- manipulated locally.
4710
4711 if Is_Shared_Passive (Current_Scope)
4712 and then Is_Library_Level_Entity (Id)
4713 and then Comes_From_Source (Id)
4714 then
4715 Set_Is_Shared_Passive (Id);
4716 Check_Shared_Var (Id, T, N);
4717 end if;
4718
4719 -- Set Has_Initial_Value if initializing expression present. Note
4720 -- that if there is no initializing expression, we leave the state
4721 -- of this flag unchanged (usually it will be False, but notably in
4722 -- the case of exception choice variables, it will already be true).
4723
4724 if Present (E) then
4725 Set_Has_Initial_Value (Id);
4726 end if;
4727 end if;
4728
4729 -- Set the SPARK mode from the current context (may be overwritten later
4730 -- with explicit pragma).
4731
4732 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4733 Set_SPARK_Pragma_Inherited (Id);
4734
4735 -- Preserve relevant elaboration-related attributes of the context which
4736 -- are no longer available or very expensive to recompute once analysis,
4737 -- resolution, and expansion are over.
4738
4739 Mark_Elaboration_Attributes
4740 (N_Id => Id,
4741 Checks => True);
4742
4743 -- Initialize alignment and size and capture alignment setting
4744
4745 Init_Alignment (Id);
4746 Init_Esize (Id);
4747 Set_Optimize_Alignment_Flags (Id);
4748
4749 -- Deal with aliased case
4750
4751 if Aliased_Present (N) then
4752 Set_Is_Aliased (Id);
4753
4754 -- If the object is aliased and the type is unconstrained with
4755 -- defaulted discriminants and there is no expression, then the
4756 -- object is constrained by the defaults, so it is worthwhile
4757 -- building the corresponding subtype.
4758
4759 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4760 -- unconstrained, then only establish an actual subtype if the
4761 -- nominal subtype is indefinite. In definite cases the object is
4762 -- unconstrained in Ada 2005.
4763
4764 if No (E)
4765 and then Is_Record_Type (T)
4766 and then not Is_Constrained (T)
4767 and then Has_Discriminants (T)
4768 and then (Ada_Version < Ada_2005
4769 or else not Is_Definite_Subtype (T))
4770 then
4771 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4772 end if;
4773 end if;
4774
4775 -- Now we can set the type of the object
4776
4777 Set_Etype (Id, Act_T);
4778
4779 -- Non-constant object is marked to be treated as volatile if type is
4780 -- volatile and we clear the Current_Value setting that may have been
4781 -- set above. Doing so for constants isn't required and might interfere
4782 -- with possible uses of the object as a static expression in contexts
4783 -- incompatible with volatility (e.g. as a case-statement alternative).
4784
4785 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4786 Set_Treat_As_Volatile (Id);
4787 Set_Current_Value (Id, Empty);
4788 end if;
4789
4790 -- Deal with controlled types
4791
4792 if Has_Controlled_Component (Etype (Id))
4793 or else Is_Controlled (Etype (Id))
4794 then
4795 if not Is_Library_Level_Entity (Id) then
4796 Check_Restriction (No_Nested_Finalization, N);
4797 else
4798 Validate_Controlled_Object (Id);
4799 end if;
4800 end if;
4801
4802 if Has_Task (Etype (Id)) then
4803 Check_Restriction (No_Tasking, N);
4804
4805 -- Deal with counting max tasks
4806
4807 -- Nothing to do if inside a generic
4808
4809 if Inside_A_Generic then
4810 null;
4811
4812 -- If library level entity, then count tasks
4813
4814 elsif Is_Library_Level_Entity (Id) then
4815 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4816
4817 -- If not library level entity, then indicate we don't know max
4818 -- tasks and also check task hierarchy restriction and blocking
4819 -- operation (since starting a task is definitely blocking).
4820
4821 else
4822 Check_Restriction (Max_Tasks, N);
4823 Check_Restriction (No_Task_Hierarchy, N);
4824 Check_Potentially_Blocking_Operation (N);
4825 end if;
4826
4827 -- A rather specialized test. If we see two tasks being declared
4828 -- of the same type in the same object declaration, and the task
4829 -- has an entry with an address clause, we know that program error
4830 -- will be raised at run time since we can't have two tasks with
4831 -- entries at the same address.
4832
4833 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4834 declare
4835 E : Entity_Id;
4836
4837 begin
4838 E := First_Entity (Etype (Id));
4839 while Present (E) loop
4840 if Ekind (E) = E_Entry
4841 and then Present (Get_Attribute_Definition_Clause
4842 (E, Attribute_Address))
4843 then
4844 Error_Msg_Warn := SPARK_Mode /= On;
4845 Error_Msg_N
4846 ("more than one task with same entry address<<", N);
4847 Error_Msg_N ("\Program_Error [<<", N);
4848 Insert_Action (N,
4849 Make_Raise_Program_Error (Loc,
4850 Reason => PE_Duplicated_Entry_Address));
4851 exit;
4852 end if;
4853
4854 Next_Entity (E);
4855 end loop;
4856 end;
4857 end if;
4858 end if;
4859
4860 -- Some simple constant-propagation: if the expression is a constant
4861 -- string initialized with a literal, share the literal. This avoids
4862 -- a run-time copy.
4863
4864 if Present (E)
4865 and then Is_Entity_Name (E)
4866 and then Ekind (Entity (E)) = E_Constant
4867 and then Base_Type (Etype (E)) = Standard_String
4868 then
4869 declare
4870 Val : constant Node_Id := Constant_Value (Entity (E));
4871 begin
4872 if Present (Val) and then Nkind (Val) = N_String_Literal then
4873 Rewrite (E, New_Copy (Val));
4874 end if;
4875 end;
4876 end if;
4877
4878 -- Another optimization: if the nominal subtype is unconstrained and
4879 -- the expression is a function call that returns an unconstrained
4880 -- type, rewrite the declaration as a renaming of the result of the
4881 -- call. The exceptions below are cases where the copy is expected,
4882 -- either by the back end (Aliased case) or by the semantics, as for
4883 -- initializing controlled types or copying tags for class-wide types.
4884
4885 if Present (E)
4886 and then Nkind (E) = N_Explicit_Dereference
4887 and then Nkind (Original_Node (E)) = N_Function_Call
4888 and then not Is_Library_Level_Entity (Id)
4889 and then not Is_Constrained (Underlying_Type (T))
4890 and then not Is_Aliased (Id)
4891 and then not Is_Class_Wide_Type (T)
4892 and then not Is_Controlled (T)
4893 and then not Has_Controlled_Component (Base_Type (T))
4894 and then Expander_Active
4895 then
4896 Rewrite (N,
4897 Make_Object_Renaming_Declaration (Loc,
4898 Defining_Identifier => Id,
4899 Access_Definition => Empty,
4900 Subtype_Mark => New_Occurrence_Of
4901 (Base_Type (Etype (Id)), Loc),
4902 Name => E));
4903
4904 Set_Renamed_Object (Id, E);
4905
4906 -- Force generation of debugging information for the constant and for
4907 -- the renamed function call.
4908
4909 Set_Debug_Info_Needed (Id);
4910 Set_Debug_Info_Needed (Entity (Prefix (E)));
4911 end if;
4912
4913 if Present (Prev_Entity)
4914 and then Is_Frozen (Prev_Entity)
4915 and then not Error_Posted (Id)
4916 then
4917 Error_Msg_N ("full constant declaration appears too late", N);
4918 end if;
4919
4920 Check_Eliminated (Id);
4921
4922 -- Deal with setting In_Private_Part flag if in private part
4923
4924 if Ekind (Scope (Id)) = E_Package
4925 and then In_Private_Part (Scope (Id))
4926 then
4927 Set_In_Private_Part (Id);
4928 end if;
4929
4930 <<Leave>>
4931 -- Initialize the refined state of a variable here because this is a
4932 -- common destination for legal and illegal object declarations.
4933
4934 if Ekind (Id) = E_Variable then
4935 Set_Encapsulating_State (Id, Empty);
4936 end if;
4937
4938 if Has_Aspects (N) then
4939 Analyze_Aspect_Specifications (N, Id);
4940 end if;
4941
4942 Analyze_Dimension (N);
4943
4944 -- Verify whether the object declaration introduces an illegal hidden
4945 -- state within a package subject to a null abstract state.
4946
4947 if Ekind (Id) = E_Variable then
4948 Check_No_Hidden_State (Id);
4949 end if;
4950
4951 Restore_Ghost_Mode (Saved_GM);
4952 end Analyze_Object_Declaration;
4953
4954 ---------------------------
4955 -- Analyze_Others_Choice --
4956 ---------------------------
4957
4958 -- Nothing to do for the others choice node itself, the semantic analysis
4959 -- of the others choice will occur as part of the processing of the parent
4960
4961 procedure Analyze_Others_Choice (N : Node_Id) is
4962 pragma Warnings (Off, N);
4963 begin
4964 null;
4965 end Analyze_Others_Choice;
4966
4967 -------------------------------------------
4968 -- Analyze_Private_Extension_Declaration --
4969 -------------------------------------------
4970
4971 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4972 Indic : constant Node_Id := Subtype_Indication (N);
4973 T : constant Entity_Id := Defining_Identifier (N);
4974 Iface : Entity_Id;
4975 Iface_Elmt : Elmt_Id;
4976 Parent_Base : Entity_Id;
4977 Parent_Type : Entity_Id;
4978
4979 begin
4980 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4981
4982 if Is_Non_Empty_List (Interface_List (N)) then
4983 declare
4984 Intf : Node_Id;
4985 T : Entity_Id;
4986
4987 begin
4988 Intf := First (Interface_List (N));
4989 while Present (Intf) loop
4990 T := Find_Type_Of_Subtype_Indic (Intf);
4991
4992 Diagnose_Interface (Intf, T);
4993 Next (Intf);
4994 end loop;
4995 end;
4996 end if;
4997
4998 Generate_Definition (T);
4999
5000 -- For other than Ada 2012, just enter the name in the current scope
5001
5002 if Ada_Version < Ada_2012 then
5003 Enter_Name (T);
5004
5005 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5006 -- case of private type that completes an incomplete type.
5007
5008 else
5009 declare
5010 Prev : Entity_Id;
5011
5012 begin
5013 Prev := Find_Type_Name (N);
5014
5015 pragma Assert (Prev = T
5016 or else (Ekind (Prev) = E_Incomplete_Type
5017 and then Present (Full_View (Prev))
5018 and then Full_View (Prev) = T));
5019 end;
5020 end if;
5021
5022 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5023 Parent_Base := Base_Type (Parent_Type);
5024
5025 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5026 Set_Ekind (T, Ekind (Parent_Type));
5027 Set_Etype (T, Any_Type);
5028 goto Leave;
5029
5030 elsif not Is_Tagged_Type (Parent_Type) then
5031 Error_Msg_N
5032 ("parent of type extension must be a tagged type ", Indic);
5033 goto Leave;
5034
5035 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5036 Error_Msg_N ("premature derivation of incomplete type", Indic);
5037 goto Leave;
5038
5039 elsif Is_Concurrent_Type (Parent_Type) then
5040 Error_Msg_N
5041 ("parent type of a private extension cannot be a synchronized "
5042 & "tagged type (RM 3.9.1 (3/1))", N);
5043
5044 Set_Etype (T, Any_Type);
5045 Set_Ekind (T, E_Limited_Private_Type);
5046 Set_Private_Dependents (T, New_Elmt_List);
5047 Set_Error_Posted (T);
5048 goto Leave;
5049 end if;
5050
5051 -- Perhaps the parent type should be changed to the class-wide type's
5052 -- specific type in this case to prevent cascading errors ???
5053
5054 if Is_Class_Wide_Type (Parent_Type) then
5055 Error_Msg_N
5056 ("parent of type extension must not be a class-wide type", Indic);
5057 goto Leave;
5058 end if;
5059
5060 if (not Is_Package_Or_Generic_Package (Current_Scope)
5061 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5062 or else In_Private_Part (Current_Scope)
5063 then
5064 Error_Msg_N ("invalid context for private extension", N);
5065 end if;
5066
5067 -- Set common attributes
5068
5069 Set_Is_Pure (T, Is_Pure (Current_Scope));
5070 Set_Scope (T, Current_Scope);
5071 Set_Ekind (T, E_Record_Type_With_Private);
5072 Init_Size_Align (T);
5073 Set_Default_SSO (T);
5074 Set_No_Reordering (T, No_Component_Reordering);
5075
5076 Set_Etype (T, Parent_Base);
5077 Propagate_Concurrent_Flags (T, Parent_Base);
5078
5079 Set_Convention (T, Convention (Parent_Type));
5080 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5081 Set_Is_First_Subtype (T);
5082 Make_Class_Wide_Type (T);
5083
5084 -- Set the SPARK mode from the current context
5085
5086 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5087 Set_SPARK_Pragma_Inherited (T);
5088
5089 if Unknown_Discriminants_Present (N) then
5090 Set_Discriminant_Constraint (T, No_Elist);
5091 end if;
5092
5093 Build_Derived_Record_Type (N, Parent_Type, T);
5094
5095 -- A private extension inherits the Default_Initial_Condition pragma
5096 -- coming from any parent type within the derivation chain.
5097
5098 if Has_DIC (Parent_Type) then
5099 Set_Has_Inherited_DIC (T);
5100 end if;
5101
5102 -- A private extension inherits any class-wide invariants coming from a
5103 -- parent type or an interface. Note that the invariant procedure of the
5104 -- parent type should not be inherited because the private extension may
5105 -- define invariants of its own.
5106
5107 if Has_Inherited_Invariants (Parent_Type)
5108 or else Has_Inheritable_Invariants (Parent_Type)
5109 then
5110 Set_Has_Inherited_Invariants (T);
5111
5112 elsif Present (Interfaces (T)) then
5113 Iface_Elmt := First_Elmt (Interfaces (T));
5114 while Present (Iface_Elmt) loop
5115 Iface := Node (Iface_Elmt);
5116
5117 if Has_Inheritable_Invariants (Iface) then
5118 Set_Has_Inherited_Invariants (T);
5119 exit;
5120 end if;
5121
5122 Next_Elmt (Iface_Elmt);
5123 end loop;
5124 end if;
5125
5126 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5127 -- synchronized formal derived type.
5128
5129 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5130 Set_Is_Limited_Record (T);
5131
5132 -- Formal derived type case
5133
5134 if Is_Generic_Type (T) then
5135
5136 -- The parent must be a tagged limited type or a synchronized
5137 -- interface.
5138
5139 if (not Is_Tagged_Type (Parent_Type)
5140 or else not Is_Limited_Type (Parent_Type))
5141 and then
5142 (not Is_Interface (Parent_Type)
5143 or else not Is_Synchronized_Interface (Parent_Type))
5144 then
5145 Error_Msg_NE
5146 ("parent type of & must be tagged limited or synchronized",
5147 N, T);
5148 end if;
5149
5150 -- The progenitors (if any) must be limited or synchronized
5151 -- interfaces.
5152
5153 if Present (Interfaces (T)) then
5154 Iface_Elmt := First_Elmt (Interfaces (T));
5155 while Present (Iface_Elmt) loop
5156 Iface := Node (Iface_Elmt);
5157
5158 if not Is_Limited_Interface (Iface)
5159 and then not Is_Synchronized_Interface (Iface)
5160 then
5161 Error_Msg_NE
5162 ("progenitor & must be limited or synchronized",
5163 N, Iface);
5164 end if;
5165
5166 Next_Elmt (Iface_Elmt);
5167 end loop;
5168 end if;
5169
5170 -- Regular derived extension, the parent must be a limited or
5171 -- synchronized interface.
5172
5173 else
5174 if not Is_Interface (Parent_Type)
5175 or else (not Is_Limited_Interface (Parent_Type)
5176 and then not Is_Synchronized_Interface (Parent_Type))
5177 then
5178 Error_Msg_NE
5179 ("parent type of & must be limited interface", N, T);
5180 end if;
5181 end if;
5182
5183 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5184 -- extension with a synchronized parent must be explicitly declared
5185 -- synchronized, because the full view will be a synchronized type.
5186 -- This must be checked before the check for limited types below,
5187 -- to ensure that types declared limited are not allowed to extend
5188 -- synchronized interfaces.
5189
5190 elsif Is_Interface (Parent_Type)
5191 and then Is_Synchronized_Interface (Parent_Type)
5192 and then not Synchronized_Present (N)
5193 then
5194 Error_Msg_NE
5195 ("private extension of& must be explicitly synchronized",
5196 N, Parent_Type);
5197
5198 elsif Limited_Present (N) then
5199 Set_Is_Limited_Record (T);
5200
5201 if not Is_Limited_Type (Parent_Type)
5202 and then
5203 (not Is_Interface (Parent_Type)
5204 or else not Is_Limited_Interface (Parent_Type))
5205 then
5206 Error_Msg_NE ("parent type& of limited extension must be limited",
5207 N, Parent_Type);
5208 end if;
5209 end if;
5210
5211 -- Remember that its parent type has a private extension. Used to warn
5212 -- on public primitives of the parent type defined after its private
5213 -- extensions (see Check_Dispatching_Operation).
5214
5215 Set_Has_Private_Extension (Parent_Type);
5216
5217 <<Leave>>
5218 if Has_Aspects (N) then
5219 Analyze_Aspect_Specifications (N, T);
5220 end if;
5221 end Analyze_Private_Extension_Declaration;
5222
5223 ---------------------------------
5224 -- Analyze_Subtype_Declaration --
5225 ---------------------------------
5226
5227 procedure Analyze_Subtype_Declaration
5228 (N : Node_Id;
5229 Skip : Boolean := False)
5230 is
5231 Id : constant Entity_Id := Defining_Identifier (N);
5232 R_Checks : Check_Result;
5233 T : Entity_Id;
5234
5235 begin
5236 Generate_Definition (Id);
5237 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5238 Init_Size_Align (Id);
5239
5240 -- The following guard condition on Enter_Name is to handle cases where
5241 -- the defining identifier has already been entered into the scope but
5242 -- the declaration as a whole needs to be analyzed.
5243
5244 -- This case in particular happens for derived enumeration types. The
5245 -- derived enumeration type is processed as an inserted enumeration type
5246 -- declaration followed by a rewritten subtype declaration. The defining
5247 -- identifier, however, is entered into the name scope very early in the
5248 -- processing of the original type declaration and therefore needs to be
5249 -- avoided here, when the created subtype declaration is analyzed. (See
5250 -- Build_Derived_Types)
5251
5252 -- This also happens when the full view of a private type is derived
5253 -- type with constraints. In this case the entity has been introduced
5254 -- in the private declaration.
5255
5256 -- Finally this happens in some complex cases when validity checks are
5257 -- enabled, where the same subtype declaration may be analyzed twice.
5258 -- This can happen if the subtype is created by the pre-analysis of
5259 -- an attribute tht gives the range of a loop statement, and the loop
5260 -- itself appears within an if_statement that will be rewritten during
5261 -- expansion.
5262
5263 if Skip
5264 or else (Present (Etype (Id))
5265 and then (Is_Private_Type (Etype (Id))
5266 or else Is_Task_Type (Etype (Id))
5267 or else Is_Rewrite_Substitution (N)))
5268 then
5269 null;
5270
5271 elsif Current_Entity (Id) = Id then
5272 null;
5273
5274 else
5275 Enter_Name (Id);
5276 end if;
5277
5278 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5279
5280 -- Class-wide equivalent types of records with unknown discriminants
5281 -- involve the generation of an itype which serves as the private view
5282 -- of a constrained record subtype. In such cases the base type of the
5283 -- current subtype we are processing is the private itype. Use the full
5284 -- of the private itype when decorating various attributes.
5285
5286 if Is_Itype (T)
5287 and then Is_Private_Type (T)
5288 and then Present (Full_View (T))
5289 then
5290 T := Full_View (T);
5291 end if;
5292
5293 -- Inherit common attributes
5294
5295 Set_Is_Volatile (Id, Is_Volatile (T));
5296 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5297 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5298 Set_Convention (Id, Convention (T));
5299
5300 -- If ancestor has predicates then so does the subtype, and in addition
5301 -- we must delay the freeze to properly arrange predicate inheritance.
5302
5303 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5304 -- in which T = ID, so the above tests and assignments do nothing???
5305
5306 if Has_Predicates (T)
5307 or else (Present (Ancestor_Subtype (T))
5308 and then Has_Predicates (Ancestor_Subtype (T)))
5309 then
5310 Set_Has_Predicates (Id);
5311 Set_Has_Delayed_Freeze (Id);
5312
5313 -- Generated subtypes inherit the predicate function from the parent
5314 -- (no aspects to examine on the generated declaration).
5315
5316 if not Comes_From_Source (N) then
5317 Set_Ekind (Id, Ekind (T));
5318
5319 if Present (Predicate_Function (T)) then
5320 Set_Predicate_Function (Id, Predicate_Function (T));
5321
5322 elsif Present (Ancestor_Subtype (T))
5323 and then Has_Predicates (Ancestor_Subtype (T))
5324 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5325 then
5326 Set_Predicate_Function (Id,
5327 Predicate_Function (Ancestor_Subtype (T)));
5328 end if;
5329 end if;
5330 end if;
5331
5332 -- Subtype of Boolean cannot have a constraint in SPARK
5333
5334 if Is_Boolean_Type (T)
5335 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5336 then
5337 Check_SPARK_05_Restriction
5338 ("subtype of Boolean cannot have constraint", N);
5339 end if;
5340
5341 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5342 declare
5343 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5344 One_Cstr : Node_Id;
5345 Low : Node_Id;
5346 High : Node_Id;
5347
5348 begin
5349 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5350 One_Cstr := First (Constraints (Cstr));
5351 while Present (One_Cstr) loop
5352
5353 -- Index or discriminant constraint in SPARK must be a
5354 -- subtype mark.
5355
5356 if not
5357 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5358 then
5359 Check_SPARK_05_Restriction
5360 ("subtype mark required", One_Cstr);
5361
5362 -- String subtype must have a lower bound of 1 in SPARK.
5363 -- Note that we do not need to test for the non-static case
5364 -- here, since that was already taken care of in
5365 -- Process_Range_Expr_In_Decl.
5366
5367 elsif Base_Type (T) = Standard_String then
5368 Get_Index_Bounds (One_Cstr, Low, High);
5369
5370 if Is_OK_Static_Expression (Low)
5371 and then Expr_Value (Low) /= 1
5372 then
5373 Check_SPARK_05_Restriction
5374 ("String subtype must have lower bound of 1", N);
5375 end if;
5376 end if;
5377
5378 Next (One_Cstr);
5379 end loop;
5380 end if;
5381 end;
5382 end if;
5383
5384 -- In the case where there is no constraint given in the subtype
5385 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5386 -- semantic attributes must be established here.
5387
5388 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5389 Set_Etype (Id, Base_Type (T));
5390
5391 -- Subtype of unconstrained array without constraint is not allowed
5392 -- in SPARK.
5393
5394 if Is_Array_Type (T) and then not Is_Constrained (T) then
5395 Check_SPARK_05_Restriction
5396 ("subtype of unconstrained array must have constraint", N);
5397 end if;
5398
5399 case Ekind (T) is
5400 when Array_Kind =>
5401 Set_Ekind (Id, E_Array_Subtype);
5402 Copy_Array_Subtype_Attributes (Id, T);
5403
5404 when Decimal_Fixed_Point_Kind =>
5405 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5406 Set_Digits_Value (Id, Digits_Value (T));
5407 Set_Delta_Value (Id, Delta_Value (T));
5408 Set_Scale_Value (Id, Scale_Value (T));
5409 Set_Small_Value (Id, Small_Value (T));
5410 Set_Scalar_Range (Id, Scalar_Range (T));
5411 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5412 Set_Is_Constrained (Id, Is_Constrained (T));
5413 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5414 Set_RM_Size (Id, RM_Size (T));
5415
5416 when Enumeration_Kind =>
5417 Set_Ekind (Id, E_Enumeration_Subtype);
5418 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5419 Set_Scalar_Range (Id, Scalar_Range (T));
5420 Set_Is_Character_Type (Id, Is_Character_Type (T));
5421 Set_Is_Constrained (Id, Is_Constrained (T));
5422 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5423 Set_RM_Size (Id, RM_Size (T));
5424 Inherit_Predicate_Flags (Id, T);
5425
5426 when Ordinary_Fixed_Point_Kind =>
5427 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5428 Set_Scalar_Range (Id, Scalar_Range (T));
5429 Set_Small_Value (Id, Small_Value (T));
5430 Set_Delta_Value (Id, Delta_Value (T));
5431 Set_Is_Constrained (Id, Is_Constrained (T));
5432 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5433 Set_RM_Size (Id, RM_Size (T));
5434
5435 when Float_Kind =>
5436 Set_Ekind (Id, E_Floating_Point_Subtype);
5437 Set_Scalar_Range (Id, Scalar_Range (T));
5438 Set_Digits_Value (Id, Digits_Value (T));
5439 Set_Is_Constrained (Id, Is_Constrained (T));
5440
5441 -- If the floating point type has dimensions, these will be
5442 -- inherited subsequently when Analyze_Dimensions is called.
5443
5444 when Signed_Integer_Kind =>
5445 Set_Ekind (Id, E_Signed_Integer_Subtype);
5446 Set_Scalar_Range (Id, Scalar_Range (T));
5447 Set_Is_Constrained (Id, Is_Constrained (T));
5448 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5449 Set_RM_Size (Id, RM_Size (T));
5450 Inherit_Predicate_Flags (Id, T);
5451
5452 when Modular_Integer_Kind =>
5453 Set_Ekind (Id, E_Modular_Integer_Subtype);
5454 Set_Scalar_Range (Id, Scalar_Range (T));
5455 Set_Is_Constrained (Id, Is_Constrained (T));
5456 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5457 Set_RM_Size (Id, RM_Size (T));
5458 Inherit_Predicate_Flags (Id, T);
5459
5460 when Class_Wide_Kind =>
5461 Set_Ekind (Id, E_Class_Wide_Subtype);
5462 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5463 Set_Cloned_Subtype (Id, T);
5464 Set_Is_Tagged_Type (Id, True);
5465 Set_Has_Unknown_Discriminants
5466 (Id, True);
5467 Set_No_Tagged_Streams_Pragma
5468 (Id, No_Tagged_Streams_Pragma (T));
5469
5470 if Ekind (T) = E_Class_Wide_Subtype then
5471 Set_Equivalent_Type (Id, Equivalent_Type (T));
5472 end if;
5473
5474 when E_Record_Subtype
5475 | E_Record_Type
5476 =>
5477 Set_Ekind (Id, E_Record_Subtype);
5478
5479 if Ekind (T) = E_Record_Subtype
5480 and then Present (Cloned_Subtype (T))
5481 then
5482 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5483 else
5484 Set_Cloned_Subtype (Id, T);
5485 end if;
5486
5487 Set_First_Entity (Id, First_Entity (T));
5488 Set_Last_Entity (Id, Last_Entity (T));
5489 Set_Has_Discriminants (Id, Has_Discriminants (T));
5490 Set_Is_Constrained (Id, Is_Constrained (T));
5491 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5492 Set_Has_Implicit_Dereference
5493 (Id, Has_Implicit_Dereference (T));
5494 Set_Has_Unknown_Discriminants
5495 (Id, Has_Unknown_Discriminants (T));
5496
5497 if Has_Discriminants (T) then
5498 Set_Discriminant_Constraint
5499 (Id, Discriminant_Constraint (T));
5500 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5501
5502 elsif Has_Unknown_Discriminants (Id) then
5503 Set_Discriminant_Constraint (Id, No_Elist);
5504 end if;
5505
5506 if Is_Tagged_Type (T) then
5507 Set_Is_Tagged_Type (Id, True);
5508 Set_No_Tagged_Streams_Pragma
5509 (Id, No_Tagged_Streams_Pragma (T));
5510 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5511 Set_Direct_Primitive_Operations
5512 (Id, Direct_Primitive_Operations (T));
5513 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5514
5515 if Is_Interface (T) then
5516 Set_Is_Interface (Id);
5517 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5518 end if;
5519 end if;
5520
5521 when Private_Kind =>
5522 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5523 Set_Has_Discriminants (Id, Has_Discriminants (T));
5524 Set_Is_Constrained (Id, Is_Constrained (T));
5525 Set_First_Entity (Id, First_Entity (T));
5526 Set_Last_Entity (Id, Last_Entity (T));
5527 Set_Private_Dependents (Id, New_Elmt_List);
5528 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5529 Set_Has_Implicit_Dereference
5530 (Id, Has_Implicit_Dereference (T));
5531 Set_Has_Unknown_Discriminants
5532 (Id, Has_Unknown_Discriminants (T));
5533 Set_Known_To_Have_Preelab_Init
5534 (Id, Known_To_Have_Preelab_Init (T));
5535
5536 if Is_Tagged_Type (T) then
5537 Set_Is_Tagged_Type (Id);
5538 Set_No_Tagged_Streams_Pragma (Id,
5539 No_Tagged_Streams_Pragma (T));
5540 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5541 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5542 Set_Direct_Primitive_Operations (Id,
5543 Direct_Primitive_Operations (T));
5544 end if;
5545
5546 -- In general the attributes of the subtype of a private type
5547 -- are the attributes of the partial view of parent. However,
5548 -- the full view may be a discriminated type, and the subtype
5549 -- must share the discriminant constraint to generate correct
5550 -- calls to initialization procedures.
5551
5552 if Has_Discriminants (T) then
5553 Set_Discriminant_Constraint
5554 (Id, Discriminant_Constraint (T));
5555 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5556
5557 elsif Present (Full_View (T))
5558 and then Has_Discriminants (Full_View (T))
5559 then
5560 Set_Discriminant_Constraint
5561 (Id, Discriminant_Constraint (Full_View (T)));
5562 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5563
5564 -- This would seem semantically correct, but apparently
5565 -- generates spurious errors about missing components ???
5566
5567 -- Set_Has_Discriminants (Id);
5568 end if;
5569
5570 Prepare_Private_Subtype_Completion (Id, N);
5571
5572 -- If this is the subtype of a constrained private type with
5573 -- discriminants that has got a full view and we also have
5574 -- built a completion just above, show that the completion
5575 -- is a clone of the full view to the back-end.
5576
5577 if Has_Discriminants (T)
5578 and then not Has_Unknown_Discriminants (T)
5579 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5580 and then Present (Full_View (T))
5581 and then Present (Full_View (Id))
5582 then
5583 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5584 end if;
5585
5586 when Access_Kind =>
5587 Set_Ekind (Id, E_Access_Subtype);
5588 Set_Is_Constrained (Id, Is_Constrained (T));
5589 Set_Is_Access_Constant
5590 (Id, Is_Access_Constant (T));
5591 Set_Directly_Designated_Type
5592 (Id, Designated_Type (T));
5593 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5594
5595 -- A Pure library_item must not contain the declaration of a
5596 -- named access type, except within a subprogram, generic
5597 -- subprogram, task unit, or protected unit, or if it has
5598 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5599
5600 if Comes_From_Source (Id)
5601 and then In_Pure_Unit
5602 and then not In_Subprogram_Task_Protected_Unit
5603 and then not No_Pool_Assigned (Id)
5604 then
5605 Error_Msg_N
5606 ("named access types not allowed in pure unit", N);
5607 end if;
5608
5609 when Concurrent_Kind =>
5610 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5611 Set_Corresponding_Record_Type (Id,
5612 Corresponding_Record_Type (T));
5613 Set_First_Entity (Id, First_Entity (T));
5614 Set_First_Private_Entity (Id, First_Private_Entity (T));
5615 Set_Has_Discriminants (Id, Has_Discriminants (T));
5616 Set_Is_Constrained (Id, Is_Constrained (T));
5617 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5618 Set_Last_Entity (Id, Last_Entity (T));
5619
5620 if Is_Tagged_Type (T) then
5621 Set_No_Tagged_Streams_Pragma
5622 (Id, No_Tagged_Streams_Pragma (T));
5623 end if;
5624
5625 if Has_Discriminants (T) then
5626 Set_Discriminant_Constraint
5627 (Id, Discriminant_Constraint (T));
5628 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5629 end if;
5630
5631 when Incomplete_Kind =>
5632 if Ada_Version >= Ada_2005 then
5633
5634 -- In Ada 2005 an incomplete type can be explicitly tagged:
5635 -- propagate indication. Note that we also have to include
5636 -- subtypes for Ada 2012 extended use of incomplete types.
5637
5638 Set_Ekind (Id, E_Incomplete_Subtype);
5639 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5640 Set_Private_Dependents (Id, New_Elmt_List);
5641
5642 if Is_Tagged_Type (Id) then
5643 Set_No_Tagged_Streams_Pragma
5644 (Id, No_Tagged_Streams_Pragma (T));
5645 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5646 end if;
5647
5648 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5649 -- incomplete type visible through a limited with clause.
5650
5651 if From_Limited_With (T)
5652 and then Present (Non_Limited_View (T))
5653 then
5654 Set_From_Limited_With (Id);
5655 Set_Non_Limited_View (Id, Non_Limited_View (T));
5656
5657 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5658 -- to the private dependents of the original incomplete
5659 -- type for future transformation.
5660
5661 else
5662 Append_Elmt (Id, Private_Dependents (T));
5663 end if;
5664
5665 -- If the subtype name denotes an incomplete type an error
5666 -- was already reported by Process_Subtype.
5667
5668 else
5669 Set_Etype (Id, Any_Type);
5670 end if;
5671
5672 when others =>
5673 raise Program_Error;
5674 end case;
5675 end if;
5676
5677 if Etype (Id) = Any_Type then
5678 goto Leave;
5679 end if;
5680
5681 -- Some common processing on all types
5682
5683 Set_Size_Info (Id, T);
5684 Set_First_Rep_Item (Id, First_Rep_Item (T));
5685
5686 -- If the parent type is a generic actual, so is the subtype. This may
5687 -- happen in a nested instance. Why Comes_From_Source test???
5688
5689 if not Comes_From_Source (N) then
5690 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5691 end if;
5692
5693 -- If this is a subtype declaration for an actual in an instance,
5694 -- inherit static and dynamic predicates if any.
5695
5696 -- If declaration has no aspect specifications, inherit predicate
5697 -- info as well. Unclear how to handle the case of both specified
5698 -- and inherited predicates ??? Other inherited aspects, such as
5699 -- invariants, should be OK, but the combination with later pragmas
5700 -- may also require special merging.
5701
5702 if Has_Predicates (T)
5703 and then Present (Predicate_Function (T))
5704 and then
5705 ((In_Instance and then not Comes_From_Source (N))
5706 or else No (Aspect_Specifications (N)))
5707 then
5708 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5709
5710 if Has_Static_Predicate (T) then
5711 Set_Has_Static_Predicate (Id);
5712 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5713 end if;
5714 end if;
5715
5716 -- Remaining processing depends on characteristics of base type
5717
5718 T := Etype (Id);
5719
5720 Set_Is_Immediately_Visible (Id, True);
5721 Set_Depends_On_Private (Id, Has_Private_Component (T));
5722 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5723
5724 if Is_Interface (T) then
5725 Set_Is_Interface (Id);
5726 end if;
5727
5728 if Present (Generic_Parent_Type (N))
5729 and then
5730 (Nkind (Parent (Generic_Parent_Type (N))) /=
5731 N_Formal_Type_Declaration
5732 or else Nkind (Formal_Type_Definition
5733 (Parent (Generic_Parent_Type (N)))) /=
5734 N_Formal_Private_Type_Definition)
5735 then
5736 if Is_Tagged_Type (Id) then
5737
5738 -- If this is a generic actual subtype for a synchronized type,
5739 -- the primitive operations are those of the corresponding record
5740 -- for which there is a separate subtype declaration.
5741
5742 if Is_Concurrent_Type (Id) then
5743 null;
5744 elsif Is_Class_Wide_Type (Id) then
5745 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5746 else
5747 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5748 end if;
5749
5750 elsif Scope (Etype (Id)) /= Standard_Standard then
5751 Derive_Subprograms (Generic_Parent_Type (N), Id);
5752 end if;
5753 end if;
5754
5755 if Is_Private_Type (T) and then Present (Full_View (T)) then
5756 Conditional_Delay (Id, Full_View (T));
5757
5758 -- The subtypes of components or subcomponents of protected types
5759 -- do not need freeze nodes, which would otherwise appear in the
5760 -- wrong scope (before the freeze node for the protected type). The
5761 -- proper subtypes are those of the subcomponents of the corresponding
5762 -- record.
5763
5764 elsif Ekind (Scope (Id)) /= E_Protected_Type
5765 and then Present (Scope (Scope (Id))) -- error defense
5766 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5767 then
5768 Conditional_Delay (Id, T);
5769 end if;
5770
5771 -- If we have a subtype of an incomplete type whose full type is a
5772 -- derived numeric type, we need to have a freeze node for the subtype.
5773 -- Otherwise gigi will complain while computing the (static) bounds of
5774 -- the subtype.
5775
5776 if Is_Itype (T)
5777 and then Is_Elementary_Type (Id)
5778 and then Etype (Id) /= Id
5779 then
5780 declare
5781 Partial : constant Entity_Id :=
5782 Incomplete_Or_Partial_View (First_Subtype (Id));
5783 begin
5784 if Present (Partial)
5785 and then Ekind (Partial) = E_Incomplete_Type
5786 then
5787 Set_Has_Delayed_Freeze (Id);
5788 end if;
5789 end;
5790 end if;
5791
5792 -- Check that Constraint_Error is raised for a scalar subtype indication
5793 -- when the lower or upper bound of a non-null range lies outside the
5794 -- range of the type mark.
5795
5796 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5797 if Is_Scalar_Type (Etype (Id))
5798 and then Scalar_Range (Id) /=
5799 Scalar_Range
5800 (Etype (Subtype_Mark (Subtype_Indication (N))))
5801 then
5802 Apply_Range_Check
5803 (Scalar_Range (Id),
5804 Etype (Subtype_Mark (Subtype_Indication (N))));
5805
5806 -- In the array case, check compatibility for each index
5807
5808 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5809 then
5810 -- This really should be a subprogram that finds the indications
5811 -- to check???
5812
5813 declare
5814 Subt_Index : Node_Id := First_Index (Id);
5815 Target_Index : Node_Id :=
5816 First_Index (Etype
5817 (Subtype_Mark (Subtype_Indication (N))));
5818 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5819
5820 begin
5821 while Present (Subt_Index) loop
5822 if ((Nkind (Subt_Index) = N_Identifier
5823 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5824 or else Nkind (Subt_Index) = N_Subtype_Indication)
5825 and then
5826 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5827 then
5828 declare
5829 Target_Typ : constant Entity_Id :=
5830 Etype (Target_Index);
5831 begin
5832 R_Checks :=
5833 Get_Range_Checks
5834 (Scalar_Range (Etype (Subt_Index)),
5835 Target_Typ,
5836 Etype (Subt_Index),
5837 Defining_Identifier (N));
5838
5839 -- Reset Has_Dynamic_Range_Check on the subtype to
5840 -- prevent elision of the index check due to a dynamic
5841 -- check generated for a preceding index (needed since
5842 -- Insert_Range_Checks tries to avoid generating
5843 -- redundant checks on a given declaration).
5844
5845 Set_Has_Dynamic_Range_Check (N, False);
5846
5847 Insert_Range_Checks
5848 (R_Checks,
5849 N,
5850 Target_Typ,
5851 Sloc (Defining_Identifier (N)));
5852
5853 -- Record whether this index involved a dynamic check
5854
5855 Has_Dyn_Chk :=
5856 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5857 end;
5858 end if;
5859
5860 Next_Index (Subt_Index);
5861 Next_Index (Target_Index);
5862 end loop;
5863
5864 -- Finally, mark whether the subtype involves dynamic checks
5865
5866 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5867 end;
5868 end if;
5869 end if;
5870
5871 Set_Optimize_Alignment_Flags (Id);
5872 Check_Eliminated (Id);
5873
5874 <<Leave>>
5875 if Has_Aspects (N) then
5876 Analyze_Aspect_Specifications (N, Id);
5877 end if;
5878
5879 Analyze_Dimension (N);
5880
5881 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5882 -- indications on composite types where the constraints are dynamic.
5883 -- Note that object declarations and aggregates generate implicit
5884 -- subtype declarations, which this covers. One special case is that the
5885 -- implicitly generated "=" for discriminated types includes an
5886 -- offending subtype declaration, which is harmless, so we ignore it
5887 -- here.
5888
5889 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5890 declare
5891 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5892 begin
5893 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5894 and then not (Is_Internal (Id)
5895 and then Is_TSS (Scope (Id),
5896 TSS_Composite_Equality))
5897 and then not Within_Init_Proc
5898 and then not All_Composite_Constraints_Static (Cstr)
5899 then
5900 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5901 end if;
5902 end;
5903 end if;
5904 end Analyze_Subtype_Declaration;
5905
5906 --------------------------------
5907 -- Analyze_Subtype_Indication --
5908 --------------------------------
5909
5910 procedure Analyze_Subtype_Indication (N : Node_Id) is
5911 T : constant Entity_Id := Subtype_Mark (N);
5912 R : constant Node_Id := Range_Expression (Constraint (N));
5913
5914 begin
5915 Analyze (T);
5916
5917 if R /= Error then
5918 Analyze (R);
5919 Set_Etype (N, Etype (R));
5920 Resolve (R, Entity (T));
5921 else
5922 Set_Error_Posted (R);
5923 Set_Error_Posted (T);
5924 end if;
5925 end Analyze_Subtype_Indication;
5926
5927 --------------------------
5928 -- Analyze_Variant_Part --
5929 --------------------------
5930
5931 procedure Analyze_Variant_Part (N : Node_Id) is
5932 Discr_Name : Node_Id;
5933 Discr_Type : Entity_Id;
5934
5935 procedure Process_Variant (A : Node_Id);
5936 -- Analyze declarations for a single variant
5937
5938 package Analyze_Variant_Choices is
5939 new Generic_Analyze_Choices (Process_Variant);
5940 use Analyze_Variant_Choices;
5941
5942 ---------------------
5943 -- Process_Variant --
5944 ---------------------
5945
5946 procedure Process_Variant (A : Node_Id) is
5947 CL : constant Node_Id := Component_List (A);
5948 begin
5949 if not Null_Present (CL) then
5950 Analyze_Declarations (Component_Items (CL));
5951
5952 if Present (Variant_Part (CL)) then
5953 Analyze (Variant_Part (CL));
5954 end if;
5955 end if;
5956 end Process_Variant;
5957
5958 -- Start of processing for Analyze_Variant_Part
5959
5960 begin
5961 Discr_Name := Name (N);
5962 Analyze (Discr_Name);
5963
5964 -- If Discr_Name bad, get out (prevent cascaded errors)
5965
5966 if Etype (Discr_Name) = Any_Type then
5967 return;
5968 end if;
5969
5970 -- Check invalid discriminant in variant part
5971
5972 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5973 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5974 end if;
5975
5976 Discr_Type := Etype (Entity (Discr_Name));
5977
5978 if not Is_Discrete_Type (Discr_Type) then
5979 Error_Msg_N
5980 ("discriminant in a variant part must be of a discrete type",
5981 Name (N));
5982 return;
5983 end if;
5984
5985 -- Now analyze the choices, which also analyzes the declarations that
5986 -- are associated with each choice.
5987
5988 Analyze_Choices (Variants (N), Discr_Type);
5989
5990 -- Note: we used to instantiate and call Check_Choices here to check
5991 -- that the choices covered the discriminant, but it's too early to do
5992 -- that because of statically predicated subtypes, whose analysis may
5993 -- be deferred to their freeze point which may be as late as the freeze
5994 -- point of the containing record. So this call is now to be found in
5995 -- Freeze_Record_Declaration.
5996
5997 end Analyze_Variant_Part;
5998
5999 ----------------------------
6000 -- Array_Type_Declaration --
6001 ----------------------------
6002
6003 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6004 Component_Def : constant Node_Id := Component_Definition (Def);
6005 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6006 P : constant Node_Id := Parent (Def);
6007 Element_Type : Entity_Id;
6008 Implicit_Base : Entity_Id;
6009 Index : Node_Id;
6010 Nb_Index : Nat;
6011 Priv : Entity_Id;
6012 Related_Id : Entity_Id := Empty;
6013
6014 begin
6015 if Nkind (Def) = N_Constrained_Array_Definition then
6016 Index := First (Discrete_Subtype_Definitions (Def));
6017 else
6018 Index := First (Subtype_Marks (Def));
6019 end if;
6020
6021 -- Find proper names for the implicit types which may be public. In case
6022 -- of anonymous arrays we use the name of the first object of that type
6023 -- as prefix.
6024
6025 if No (T) then
6026 Related_Id := Defining_Identifier (P);
6027 else
6028 Related_Id := T;
6029 end if;
6030
6031 Nb_Index := 1;
6032 while Present (Index) loop
6033 Analyze (Index);
6034
6035 -- Test for odd case of trying to index a type by the type itself
6036
6037 if Is_Entity_Name (Index) and then Entity (Index) = T then
6038 Error_Msg_N ("type& cannot be indexed by itself", Index);
6039 Set_Entity (Index, Standard_Boolean);
6040 Set_Etype (Index, Standard_Boolean);
6041 end if;
6042
6043 -- Check SPARK restriction requiring a subtype mark
6044
6045 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6046 Check_SPARK_05_Restriction ("subtype mark required", Index);
6047 end if;
6048
6049 -- Add a subtype declaration for each index of private array type
6050 -- declaration whose etype is also private. For example:
6051
6052 -- package Pkg is
6053 -- type Index is private;
6054 -- private
6055 -- type Table is array (Index) of ...
6056 -- end;
6057
6058 -- This is currently required by the expander for the internally
6059 -- generated equality subprogram of records with variant parts in
6060 -- which the etype of some component is such private type.
6061
6062 if Ekind (Current_Scope) = E_Package
6063 and then In_Private_Part (Current_Scope)
6064 and then Has_Private_Declaration (Etype (Index))
6065 then
6066 declare
6067 Loc : constant Source_Ptr := Sloc (Def);
6068 Decl : Entity_Id;
6069 New_E : Entity_Id;
6070
6071 begin
6072 New_E := Make_Temporary (Loc, 'T');
6073 Set_Is_Internal (New_E);
6074
6075 Decl :=
6076 Make_Subtype_Declaration (Loc,
6077 Defining_Identifier => New_E,
6078 Subtype_Indication =>
6079 New_Occurrence_Of (Etype (Index), Loc));
6080
6081 Insert_Before (Parent (Def), Decl);
6082 Analyze (Decl);
6083 Set_Etype (Index, New_E);
6084
6085 -- If the index is a range or a subtype indication it carries
6086 -- no entity. Example:
6087
6088 -- package Pkg is
6089 -- type T is private;
6090 -- private
6091 -- type T is new Natural;
6092 -- Table : array (T(1) .. T(10)) of Boolean;
6093 -- end Pkg;
6094
6095 -- Otherwise the type of the reference is its entity.
6096
6097 if Is_Entity_Name (Index) then
6098 Set_Entity (Index, New_E);
6099 end if;
6100 end;
6101 end if;
6102
6103 Make_Index (Index, P, Related_Id, Nb_Index);
6104
6105 -- Check error of subtype with predicate for index type
6106
6107 Bad_Predicated_Subtype_Use
6108 ("subtype& has predicate, not allowed as index subtype",
6109 Index, Etype (Index));
6110
6111 -- Move to next index
6112
6113 Next_Index (Index);
6114 Nb_Index := Nb_Index + 1;
6115 end loop;
6116
6117 -- Process subtype indication if one is present
6118
6119 if Present (Component_Typ) then
6120 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6121
6122 Set_Etype (Component_Typ, Element_Type);
6123
6124 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6125 Check_SPARK_05_Restriction
6126 ("subtype mark required", Component_Typ);
6127 end if;
6128
6129 -- Ada 2005 (AI-230): Access Definition case
6130
6131 else pragma Assert (Present (Access_Definition (Component_Def)));
6132
6133 -- Indicate that the anonymous access type is created by the
6134 -- array type declaration.
6135
6136 Element_Type := Access_Definition
6137 (Related_Nod => P,
6138 N => Access_Definition (Component_Def));
6139 Set_Is_Local_Anonymous_Access (Element_Type);
6140
6141 -- Propagate the parent. This field is needed if we have to generate
6142 -- the master_id associated with an anonymous access to task type
6143 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6144
6145 Set_Parent (Element_Type, Parent (T));
6146
6147 -- Ada 2005 (AI-230): In case of components that are anonymous access
6148 -- types the level of accessibility depends on the enclosing type
6149 -- declaration
6150
6151 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6152
6153 -- Ada 2005 (AI-254)
6154
6155 declare
6156 CD : constant Node_Id :=
6157 Access_To_Subprogram_Definition
6158 (Access_Definition (Component_Def));
6159 begin
6160 if Present (CD) and then Protected_Present (CD) then
6161 Element_Type :=
6162 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6163 end if;
6164 end;
6165 end if;
6166
6167 -- Constrained array case
6168
6169 if No (T) then
6170 T := Create_Itype (E_Void, P, Related_Id, 'T');
6171 end if;
6172
6173 if Nkind (Def) = N_Constrained_Array_Definition then
6174
6175 -- Establish Implicit_Base as unconstrained base type
6176
6177 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6178
6179 Set_Etype (Implicit_Base, Implicit_Base);
6180 Set_Scope (Implicit_Base, Current_Scope);
6181 Set_Has_Delayed_Freeze (Implicit_Base);
6182 Set_Default_SSO (Implicit_Base);
6183
6184 -- The constrained array type is a subtype of the unconstrained one
6185
6186 Set_Ekind (T, E_Array_Subtype);
6187 Init_Size_Align (T);
6188 Set_Etype (T, Implicit_Base);
6189 Set_Scope (T, Current_Scope);
6190 Set_Is_Constrained (T);
6191 Set_First_Index (T,
6192 First (Discrete_Subtype_Definitions (Def)));
6193 Set_Has_Delayed_Freeze (T);
6194
6195 -- Complete setup of implicit base type
6196
6197 Set_Component_Size (Implicit_Base, Uint_0);
6198 Set_Component_Type (Implicit_Base, Element_Type);
6199 Set_Finalize_Storage_Only
6200 (Implicit_Base,
6201 Finalize_Storage_Only (Element_Type));
6202 Set_First_Index (Implicit_Base, First_Index (T));
6203 Set_Has_Controlled_Component
6204 (Implicit_Base,
6205 Has_Controlled_Component (Element_Type)
6206 or else Is_Controlled (Element_Type));
6207 Set_Packed_Array_Impl_Type
6208 (Implicit_Base, Empty);
6209
6210 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6211
6212 -- Unconstrained array case
6213
6214 else
6215 Set_Ekind (T, E_Array_Type);
6216 Init_Size_Align (T);
6217 Set_Etype (T, T);
6218 Set_Scope (T, Current_Scope);
6219 Set_Component_Size (T, Uint_0);
6220 Set_Is_Constrained (T, False);
6221 Set_First_Index (T, First (Subtype_Marks (Def)));
6222 Set_Has_Delayed_Freeze (T, True);
6223 Propagate_Concurrent_Flags (T, Element_Type);
6224 Set_Has_Controlled_Component (T, Has_Controlled_Component
6225 (Element_Type)
6226 or else
6227 Is_Controlled (Element_Type));
6228 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6229 (Element_Type));
6230 Set_Default_SSO (T);
6231 end if;
6232
6233 -- Common attributes for both cases
6234
6235 Set_Component_Type (Base_Type (T), Element_Type);
6236 Set_Packed_Array_Impl_Type (T, Empty);
6237
6238 if Aliased_Present (Component_Definition (Def)) then
6239 Check_SPARK_05_Restriction
6240 ("aliased is not allowed", Component_Definition (Def));
6241 Set_Has_Aliased_Components (Etype (T));
6242 end if;
6243
6244 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6245 -- array type to ensure that objects of this type are initialized.
6246
6247 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6248 Set_Can_Never_Be_Null (T);
6249
6250 if Null_Exclusion_Present (Component_Definition (Def))
6251
6252 -- No need to check itypes because in their case this check was
6253 -- done at their point of creation
6254
6255 and then not Is_Itype (Element_Type)
6256 then
6257 Error_Msg_N
6258 ("`NOT NULL` not allowed (null already excluded)",
6259 Subtype_Indication (Component_Definition (Def)));
6260 end if;
6261 end if;
6262
6263 Priv := Private_Component (Element_Type);
6264
6265 if Present (Priv) then
6266
6267 -- Check for circular definitions
6268
6269 if Priv = Any_Type then
6270 Set_Component_Type (Etype (T), Any_Type);
6271
6272 -- There is a gap in the visibility of operations on the composite
6273 -- type only if the component type is defined in a different scope.
6274
6275 elsif Scope (Priv) = Current_Scope then
6276 null;
6277
6278 elsif Is_Limited_Type (Priv) then
6279 Set_Is_Limited_Composite (Etype (T));
6280 Set_Is_Limited_Composite (T);
6281 else
6282 Set_Is_Private_Composite (Etype (T));
6283 Set_Is_Private_Composite (T);
6284 end if;
6285 end if;
6286
6287 -- A syntax error in the declaration itself may lead to an empty index
6288 -- list, in which case do a minimal patch.
6289
6290 if No (First_Index (T)) then
6291 Error_Msg_N ("missing index definition in array type declaration", T);
6292
6293 declare
6294 Indexes : constant List_Id :=
6295 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6296 begin
6297 Set_Discrete_Subtype_Definitions (Def, Indexes);
6298 Set_First_Index (T, First (Indexes));
6299 return;
6300 end;
6301 end if;
6302
6303 -- Create a concatenation operator for the new type. Internal array
6304 -- types created for packed entities do not need such, they are
6305 -- compatible with the user-defined type.
6306
6307 if Number_Dimensions (T) = 1
6308 and then not Is_Packed_Array_Impl_Type (T)
6309 then
6310 New_Concatenation_Op (T);
6311 end if;
6312
6313 -- In the case of an unconstrained array the parser has already verified
6314 -- that all the indexes are unconstrained but we still need to make sure
6315 -- that the element type is constrained.
6316
6317 if not Is_Definite_Subtype (Element_Type) then
6318 Error_Msg_N
6319 ("unconstrained element type in array declaration",
6320 Subtype_Indication (Component_Def));
6321
6322 elsif Is_Abstract_Type (Element_Type) then
6323 Error_Msg_N
6324 ("the type of a component cannot be abstract",
6325 Subtype_Indication (Component_Def));
6326 end if;
6327
6328 -- There may be an invariant declared for the component type, but
6329 -- the construction of the component invariant checking procedure
6330 -- takes place during expansion.
6331 end Array_Type_Declaration;
6332
6333 ------------------------------------------------------
6334 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6335 ------------------------------------------------------
6336
6337 function Replace_Anonymous_Access_To_Protected_Subprogram
6338 (N : Node_Id) return Entity_Id
6339 is
6340 Loc : constant Source_Ptr := Sloc (N);
6341
6342 Curr_Scope : constant Scope_Stack_Entry :=
6343 Scope_Stack.Table (Scope_Stack.Last);
6344
6345 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6346
6347 Acc : Node_Id;
6348 -- Access definition in declaration
6349
6350 Comp : Node_Id;
6351 -- Object definition or formal definition with an access definition
6352
6353 Decl : Node_Id;
6354 -- Declaration of anonymous access to subprogram type
6355
6356 Spec : Node_Id;
6357 -- Original specification in access to subprogram
6358
6359 P : Node_Id;
6360
6361 begin
6362 Set_Is_Internal (Anon);
6363
6364 case Nkind (N) is
6365 when N_Constrained_Array_Definition
6366 | N_Component_Declaration
6367 | N_Unconstrained_Array_Definition
6368 =>
6369 Comp := Component_Definition (N);
6370 Acc := Access_Definition (Comp);
6371
6372 when N_Discriminant_Specification =>
6373 Comp := Discriminant_Type (N);
6374 Acc := Comp;
6375
6376 when N_Parameter_Specification =>
6377 Comp := Parameter_Type (N);
6378 Acc := Comp;
6379
6380 when N_Access_Function_Definition =>
6381 Comp := Result_Definition (N);
6382 Acc := Comp;
6383
6384 when N_Object_Declaration =>
6385 Comp := Object_Definition (N);
6386 Acc := Comp;
6387
6388 when N_Function_Specification =>
6389 Comp := Result_Definition (N);
6390 Acc := Comp;
6391
6392 when others =>
6393 raise Program_Error;
6394 end case;
6395
6396 Spec := Access_To_Subprogram_Definition (Acc);
6397
6398 Decl :=
6399 Make_Full_Type_Declaration (Loc,
6400 Defining_Identifier => Anon,
6401 Type_Definition => Copy_Separate_Tree (Spec));
6402
6403 Mark_Rewrite_Insertion (Decl);
6404
6405 -- In ASIS mode, analyze the profile on the original node, because
6406 -- the separate copy does not provide enough links to recover the
6407 -- original tree. Analysis is limited to type annotations, within
6408 -- a temporary scope that serves as an anonymous subprogram to collect
6409 -- otherwise useless temporaries and itypes.
6410
6411 if ASIS_Mode then
6412 declare
6413 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6414
6415 begin
6416 if Nkind (Spec) = N_Access_Function_Definition then
6417 Set_Ekind (Typ, E_Function);
6418 else
6419 Set_Ekind (Typ, E_Procedure);
6420 end if;
6421
6422 Set_Parent (Typ, N);
6423 Set_Scope (Typ, Current_Scope);
6424 Push_Scope (Typ);
6425
6426 -- Nothing to do if procedure is parameterless
6427
6428 if Present (Parameter_Specifications (Spec)) then
6429 Process_Formals (Parameter_Specifications (Spec), Spec);
6430 end if;
6431
6432 if Nkind (Spec) = N_Access_Function_Definition then
6433 declare
6434 Def : constant Node_Id := Result_Definition (Spec);
6435
6436 begin
6437 -- The result might itself be an anonymous access type, so
6438 -- have to recurse.
6439
6440 if Nkind (Def) = N_Access_Definition then
6441 if Present (Access_To_Subprogram_Definition (Def)) then
6442 Set_Etype
6443 (Def,
6444 Replace_Anonymous_Access_To_Protected_Subprogram
6445 (Spec));
6446 else
6447 Find_Type (Subtype_Mark (Def));
6448 end if;
6449
6450 else
6451 Find_Type (Def);
6452 end if;
6453 end;
6454 end if;
6455
6456 End_Scope;
6457 end;
6458 end if;
6459
6460 -- Insert the new declaration in the nearest enclosing scope. If the
6461 -- parent is a body and N is its return type, the declaration belongs
6462 -- in the enclosing scope. Likewise if N is the type of a parameter.
6463
6464 P := Parent (N);
6465
6466 if Nkind (N) = N_Function_Specification
6467 and then Nkind (P) = N_Subprogram_Body
6468 then
6469 P := Parent (P);
6470 elsif Nkind (N) = N_Parameter_Specification
6471 and then Nkind (P) in N_Subprogram_Specification
6472 and then Nkind (Parent (P)) = N_Subprogram_Body
6473 then
6474 P := Parent (Parent (P));
6475 end if;
6476
6477 while Present (P) and then not Has_Declarations (P) loop
6478 P := Parent (P);
6479 end loop;
6480
6481 pragma Assert (Present (P));
6482
6483 if Nkind (P) = N_Package_Specification then
6484 Prepend (Decl, Visible_Declarations (P));
6485 else
6486 Prepend (Decl, Declarations (P));
6487 end if;
6488
6489 -- Replace the anonymous type with an occurrence of the new declaration.
6490 -- In all cases the rewritten node does not have the null-exclusion
6491 -- attribute because (if present) it was already inherited by the
6492 -- anonymous entity (Anon). Thus, in case of components we do not
6493 -- inherit this attribute.
6494
6495 if Nkind (N) = N_Parameter_Specification then
6496 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6497 Set_Etype (Defining_Identifier (N), Anon);
6498 Set_Null_Exclusion_Present (N, False);
6499
6500 elsif Nkind (N) = N_Object_Declaration then
6501 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6502 Set_Etype (Defining_Identifier (N), Anon);
6503
6504 elsif Nkind (N) = N_Access_Function_Definition then
6505 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6506
6507 elsif Nkind (N) = N_Function_Specification then
6508 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6509 Set_Etype (Defining_Unit_Name (N), Anon);
6510
6511 else
6512 Rewrite (Comp,
6513 Make_Component_Definition (Loc,
6514 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6515 end if;
6516
6517 Mark_Rewrite_Insertion (Comp);
6518
6519 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6520 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6521 and then not Is_Type (Current_Scope))
6522 then
6523
6524 -- Declaration can be analyzed in the current scope.
6525
6526 Analyze (Decl);
6527
6528 else
6529 -- Temporarily remove the current scope (record or subprogram) from
6530 -- the stack to add the new declarations to the enclosing scope.
6531 -- The anonymous entity is an Itype with the proper attributes.
6532
6533 Scope_Stack.Decrement_Last;
6534 Analyze (Decl);
6535 Set_Is_Itype (Anon);
6536 Set_Associated_Node_For_Itype (Anon, N);
6537 Scope_Stack.Append (Curr_Scope);
6538 end if;
6539
6540 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6541 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6542 return Anon;
6543 end Replace_Anonymous_Access_To_Protected_Subprogram;
6544
6545 -------------------------------
6546 -- Build_Derived_Access_Type --
6547 -------------------------------
6548
6549 procedure Build_Derived_Access_Type
6550 (N : Node_Id;
6551 Parent_Type : Entity_Id;
6552 Derived_Type : Entity_Id)
6553 is
6554 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6555
6556 Desig_Type : Entity_Id;
6557 Discr : Entity_Id;
6558 Discr_Con_Elist : Elist_Id;
6559 Discr_Con_El : Elmt_Id;
6560 Subt : Entity_Id;
6561
6562 begin
6563 -- Set the designated type so it is available in case this is an access
6564 -- to a self-referential type, e.g. a standard list type with a next
6565 -- pointer. Will be reset after subtype is built.
6566
6567 Set_Directly_Designated_Type
6568 (Derived_Type, Designated_Type (Parent_Type));
6569
6570 Subt := Process_Subtype (S, N);
6571
6572 if Nkind (S) /= N_Subtype_Indication
6573 and then Subt /= Base_Type (Subt)
6574 then
6575 Set_Ekind (Derived_Type, E_Access_Subtype);
6576 end if;
6577
6578 if Ekind (Derived_Type) = E_Access_Subtype then
6579 declare
6580 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6581 Ibase : constant Entity_Id :=
6582 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6583 Svg_Chars : constant Name_Id := Chars (Ibase);
6584 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6585
6586 begin
6587 Copy_Node (Pbase, Ibase);
6588
6589 -- Restore Itype status after Copy_Node
6590
6591 Set_Is_Itype (Ibase);
6592 Set_Associated_Node_For_Itype (Ibase, N);
6593
6594 Set_Chars (Ibase, Svg_Chars);
6595 Set_Next_Entity (Ibase, Svg_Next_E);
6596 Set_Sloc (Ibase, Sloc (Derived_Type));
6597 Set_Scope (Ibase, Scope (Derived_Type));
6598 Set_Freeze_Node (Ibase, Empty);
6599 Set_Is_Frozen (Ibase, False);
6600 Set_Comes_From_Source (Ibase, False);
6601 Set_Is_First_Subtype (Ibase, False);
6602
6603 Set_Etype (Ibase, Pbase);
6604 Set_Etype (Derived_Type, Ibase);
6605 end;
6606 end if;
6607
6608 Set_Directly_Designated_Type
6609 (Derived_Type, Designated_Type (Subt));
6610
6611 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6612 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6613 Set_Size_Info (Derived_Type, Parent_Type);
6614 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6615 Set_Depends_On_Private (Derived_Type,
6616 Has_Private_Component (Derived_Type));
6617 Conditional_Delay (Derived_Type, Subt);
6618
6619 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6620 -- that it is not redundant.
6621
6622 if Null_Exclusion_Present (Type_Definition (N)) then
6623 Set_Can_Never_Be_Null (Derived_Type);
6624
6625 elsif Can_Never_Be_Null (Parent_Type) then
6626 Set_Can_Never_Be_Null (Derived_Type);
6627 end if;
6628
6629 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6630 -- the root type for this information.
6631
6632 -- Apply range checks to discriminants for derived record case
6633 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6634
6635 Desig_Type := Designated_Type (Derived_Type);
6636
6637 if Is_Composite_Type (Desig_Type)
6638 and then (not Is_Array_Type (Desig_Type))
6639 and then Has_Discriminants (Desig_Type)
6640 and then Base_Type (Desig_Type) /= Desig_Type
6641 then
6642 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6643 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6644
6645 Discr := First_Discriminant (Base_Type (Desig_Type));
6646 while Present (Discr_Con_El) loop
6647 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6648 Next_Elmt (Discr_Con_El);
6649 Next_Discriminant (Discr);
6650 end loop;
6651 end if;
6652 end Build_Derived_Access_Type;
6653
6654 ------------------------------
6655 -- Build_Derived_Array_Type --
6656 ------------------------------
6657
6658 procedure Build_Derived_Array_Type
6659 (N : Node_Id;
6660 Parent_Type : Entity_Id;
6661 Derived_Type : Entity_Id)
6662 is
6663 Loc : constant Source_Ptr := Sloc (N);
6664 Tdef : constant Node_Id := Type_Definition (N);
6665 Indic : constant Node_Id := Subtype_Indication (Tdef);
6666 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6667 Implicit_Base : Entity_Id := Empty;
6668 New_Indic : Node_Id;
6669
6670 procedure Make_Implicit_Base;
6671 -- If the parent subtype is constrained, the derived type is a subtype
6672 -- of an implicit base type derived from the parent base.
6673
6674 ------------------------
6675 -- Make_Implicit_Base --
6676 ------------------------
6677
6678 procedure Make_Implicit_Base is
6679 begin
6680 Implicit_Base :=
6681 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6682
6683 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6684 Set_Etype (Implicit_Base, Parent_Base);
6685
6686 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6687 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6688
6689 Set_Has_Delayed_Freeze (Implicit_Base, True);
6690 end Make_Implicit_Base;
6691
6692 -- Start of processing for Build_Derived_Array_Type
6693
6694 begin
6695 if not Is_Constrained (Parent_Type) then
6696 if Nkind (Indic) /= N_Subtype_Indication then
6697 Set_Ekind (Derived_Type, E_Array_Type);
6698
6699 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6700 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6701
6702 Set_Has_Delayed_Freeze (Derived_Type, True);
6703
6704 else
6705 Make_Implicit_Base;
6706 Set_Etype (Derived_Type, Implicit_Base);
6707
6708 New_Indic :=
6709 Make_Subtype_Declaration (Loc,
6710 Defining_Identifier => Derived_Type,
6711 Subtype_Indication =>
6712 Make_Subtype_Indication (Loc,
6713 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6714 Constraint => Constraint (Indic)));
6715
6716 Rewrite (N, New_Indic);
6717 Analyze (N);
6718 end if;
6719
6720 else
6721 if Nkind (Indic) /= N_Subtype_Indication then
6722 Make_Implicit_Base;
6723
6724 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6725 Set_Etype (Derived_Type, Implicit_Base);
6726 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6727
6728 else
6729 Error_Msg_N ("illegal constraint on constrained type", Indic);
6730 end if;
6731 end if;
6732
6733 -- If parent type is not a derived type itself, and is declared in
6734 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6735 -- the new type's concatenation operator since Derive_Subprograms
6736 -- will not inherit the parent's operator. If the parent type is
6737 -- unconstrained, the operator is of the unconstrained base type.
6738
6739 if Number_Dimensions (Parent_Type) = 1
6740 and then not Is_Limited_Type (Parent_Type)
6741 and then not Is_Derived_Type (Parent_Type)
6742 and then not Is_Package_Or_Generic_Package
6743 (Scope (Base_Type (Parent_Type)))
6744 then
6745 if not Is_Constrained (Parent_Type)
6746 and then Is_Constrained (Derived_Type)
6747 then
6748 New_Concatenation_Op (Implicit_Base);
6749 else
6750 New_Concatenation_Op (Derived_Type);
6751 end if;
6752 end if;
6753 end Build_Derived_Array_Type;
6754
6755 -----------------------------------
6756 -- Build_Derived_Concurrent_Type --
6757 -----------------------------------
6758
6759 procedure Build_Derived_Concurrent_Type
6760 (N : Node_Id;
6761 Parent_Type : Entity_Id;
6762 Derived_Type : Entity_Id)
6763 is
6764 Loc : constant Source_Ptr := Sloc (N);
6765
6766 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6767 Corr_Decl : Node_Id;
6768 Corr_Decl_Needed : Boolean;
6769 -- If the derived type has fewer discriminants than its parent, the
6770 -- corresponding record is also a derived type, in order to account for
6771 -- the bound discriminants. We create a full type declaration for it in
6772 -- this case.
6773
6774 Constraint_Present : constant Boolean :=
6775 Nkind (Subtype_Indication (Type_Definition (N))) =
6776 N_Subtype_Indication;
6777
6778 D_Constraint : Node_Id;
6779 New_Constraint : Elist_Id := No_Elist;
6780 Old_Disc : Entity_Id;
6781 New_Disc : Entity_Id;
6782 New_N : Node_Id;
6783
6784 begin
6785 Set_Stored_Constraint (Derived_Type, No_Elist);
6786 Corr_Decl_Needed := False;
6787 Old_Disc := Empty;
6788
6789 if Present (Discriminant_Specifications (N))
6790 and then Constraint_Present
6791 then
6792 Old_Disc := First_Discriminant (Parent_Type);
6793 New_Disc := First (Discriminant_Specifications (N));
6794 while Present (New_Disc) and then Present (Old_Disc) loop
6795 Next_Discriminant (Old_Disc);
6796 Next (New_Disc);
6797 end loop;
6798 end if;
6799
6800 if Present (Old_Disc) and then Expander_Active then
6801
6802 -- The new type has fewer discriminants, so we need to create a new
6803 -- corresponding record, which is derived from the corresponding
6804 -- record of the parent, and has a stored constraint that captures
6805 -- the values of the discriminant constraints. The corresponding
6806 -- record is needed only if expander is active and code generation is
6807 -- enabled.
6808
6809 -- The type declaration for the derived corresponding record has the
6810 -- same discriminant part and constraints as the current declaration.
6811 -- Copy the unanalyzed tree to build declaration.
6812
6813 Corr_Decl_Needed := True;
6814 New_N := Copy_Separate_Tree (N);
6815
6816 Corr_Decl :=
6817 Make_Full_Type_Declaration (Loc,
6818 Defining_Identifier => Corr_Record,
6819 Discriminant_Specifications =>
6820 Discriminant_Specifications (New_N),
6821 Type_Definition =>
6822 Make_Derived_Type_Definition (Loc,
6823 Subtype_Indication =>
6824 Make_Subtype_Indication (Loc,
6825 Subtype_Mark =>
6826 New_Occurrence_Of
6827 (Corresponding_Record_Type (Parent_Type), Loc),
6828 Constraint =>
6829 Constraint
6830 (Subtype_Indication (Type_Definition (New_N))))));
6831 end if;
6832
6833 -- Copy Storage_Size and Relative_Deadline variables if task case
6834
6835 if Is_Task_Type (Parent_Type) then
6836 Set_Storage_Size_Variable (Derived_Type,
6837 Storage_Size_Variable (Parent_Type));
6838 Set_Relative_Deadline_Variable (Derived_Type,
6839 Relative_Deadline_Variable (Parent_Type));
6840 end if;
6841
6842 if Present (Discriminant_Specifications (N)) then
6843 Push_Scope (Derived_Type);
6844 Check_Or_Process_Discriminants (N, Derived_Type);
6845
6846 if Constraint_Present then
6847 New_Constraint :=
6848 Expand_To_Stored_Constraint
6849 (Parent_Type,
6850 Build_Discriminant_Constraints
6851 (Parent_Type,
6852 Subtype_Indication (Type_Definition (N)), True));
6853 end if;
6854
6855 End_Scope;
6856
6857 elsif Constraint_Present then
6858
6859 -- Build constrained subtype, copying the constraint, and derive
6860 -- from it to create a derived constrained type.
6861
6862 declare
6863 Loc : constant Source_Ptr := Sloc (N);
6864 Anon : constant Entity_Id :=
6865 Make_Defining_Identifier (Loc,
6866 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6867 Decl : Node_Id;
6868
6869 begin
6870 Decl :=
6871 Make_Subtype_Declaration (Loc,
6872 Defining_Identifier => Anon,
6873 Subtype_Indication =>
6874 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6875 Insert_Before (N, Decl);
6876 Analyze (Decl);
6877
6878 Rewrite (Subtype_Indication (Type_Definition (N)),
6879 New_Occurrence_Of (Anon, Loc));
6880 Set_Analyzed (Derived_Type, False);
6881 Analyze (N);
6882 return;
6883 end;
6884 end if;
6885
6886 -- By default, operations and private data are inherited from parent.
6887 -- However, in the presence of bound discriminants, a new corresponding
6888 -- record will be created, see below.
6889
6890 Set_Has_Discriminants
6891 (Derived_Type, Has_Discriminants (Parent_Type));
6892 Set_Corresponding_Record_Type
6893 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6894
6895 -- Is_Constrained is set according the parent subtype, but is set to
6896 -- False if the derived type is declared with new discriminants.
6897
6898 Set_Is_Constrained
6899 (Derived_Type,
6900 (Is_Constrained (Parent_Type) or else Constraint_Present)
6901 and then not Present (Discriminant_Specifications (N)));
6902
6903 if Constraint_Present then
6904 if not Has_Discriminants (Parent_Type) then
6905 Error_Msg_N ("untagged parent must have discriminants", N);
6906
6907 elsif Present (Discriminant_Specifications (N)) then
6908
6909 -- Verify that new discriminants are used to constrain old ones
6910
6911 D_Constraint :=
6912 First
6913 (Constraints
6914 (Constraint (Subtype_Indication (Type_Definition (N)))));
6915
6916 Old_Disc := First_Discriminant (Parent_Type);
6917
6918 while Present (D_Constraint) loop
6919 if Nkind (D_Constraint) /= N_Discriminant_Association then
6920
6921 -- Positional constraint. If it is a reference to a new
6922 -- discriminant, it constrains the corresponding old one.
6923
6924 if Nkind (D_Constraint) = N_Identifier then
6925 New_Disc := First_Discriminant (Derived_Type);
6926 while Present (New_Disc) loop
6927 exit when Chars (New_Disc) = Chars (D_Constraint);
6928 Next_Discriminant (New_Disc);
6929 end loop;
6930
6931 if Present (New_Disc) then
6932 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6933 end if;
6934 end if;
6935
6936 Next_Discriminant (Old_Disc);
6937
6938 -- if this is a named constraint, search by name for the old
6939 -- discriminants constrained by the new one.
6940
6941 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6942
6943 -- Find new discriminant with that name
6944
6945 New_Disc := First_Discriminant (Derived_Type);
6946 while Present (New_Disc) loop
6947 exit when
6948 Chars (New_Disc) = Chars (Expression (D_Constraint));
6949 Next_Discriminant (New_Disc);
6950 end loop;
6951
6952 if Present (New_Disc) then
6953
6954 -- Verify that new discriminant renames some discriminant
6955 -- of the parent type, and associate the new discriminant
6956 -- with one or more old ones that it renames.
6957
6958 declare
6959 Selector : Node_Id;
6960
6961 begin
6962 Selector := First (Selector_Names (D_Constraint));
6963 while Present (Selector) loop
6964 Old_Disc := First_Discriminant (Parent_Type);
6965 while Present (Old_Disc) loop
6966 exit when Chars (Old_Disc) = Chars (Selector);
6967 Next_Discriminant (Old_Disc);
6968 end loop;
6969
6970 if Present (Old_Disc) then
6971 Set_Corresponding_Discriminant
6972 (New_Disc, Old_Disc);
6973 end if;
6974
6975 Next (Selector);
6976 end loop;
6977 end;
6978 end if;
6979 end if;
6980
6981 Next (D_Constraint);
6982 end loop;
6983
6984 New_Disc := First_Discriminant (Derived_Type);
6985 while Present (New_Disc) loop
6986 if No (Corresponding_Discriminant (New_Disc)) then
6987 Error_Msg_NE
6988 ("new discriminant& must constrain old one", N, New_Disc);
6989
6990 elsif not
6991 Subtypes_Statically_Compatible
6992 (Etype (New_Disc),
6993 Etype (Corresponding_Discriminant (New_Disc)))
6994 then
6995 Error_Msg_NE
6996 ("& not statically compatible with parent discriminant",
6997 N, New_Disc);
6998 end if;
6999
7000 Next_Discriminant (New_Disc);
7001 end loop;
7002 end if;
7003
7004 elsif Present (Discriminant_Specifications (N)) then
7005 Error_Msg_N
7006 ("missing discriminant constraint in untagged derivation", N);
7007 end if;
7008
7009 -- The entity chain of the derived type includes the new discriminants
7010 -- but shares operations with the parent.
7011
7012 if Present (Discriminant_Specifications (N)) then
7013 Old_Disc := First_Discriminant (Parent_Type);
7014 while Present (Old_Disc) loop
7015 if No (Next_Entity (Old_Disc))
7016 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7017 then
7018 Set_Next_Entity
7019 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7020 exit;
7021 end if;
7022
7023 Next_Discriminant (Old_Disc);
7024 end loop;
7025
7026 else
7027 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7028 if Has_Discriminants (Parent_Type) then
7029 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7030 Set_Discriminant_Constraint (
7031 Derived_Type, Discriminant_Constraint (Parent_Type));
7032 end if;
7033 end if;
7034
7035 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7036
7037 Set_Has_Completion (Derived_Type);
7038
7039 if Corr_Decl_Needed then
7040 Set_Stored_Constraint (Derived_Type, New_Constraint);
7041 Insert_After (N, Corr_Decl);
7042 Analyze (Corr_Decl);
7043 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7044 end if;
7045 end Build_Derived_Concurrent_Type;
7046
7047 ------------------------------------
7048 -- Build_Derived_Enumeration_Type --
7049 ------------------------------------
7050
7051 procedure Build_Derived_Enumeration_Type
7052 (N : Node_Id;
7053 Parent_Type : Entity_Id;
7054 Derived_Type : Entity_Id)
7055 is
7056 Loc : constant Source_Ptr := Sloc (N);
7057 Def : constant Node_Id := Type_Definition (N);
7058 Indic : constant Node_Id := Subtype_Indication (Def);
7059 Implicit_Base : Entity_Id;
7060 Literal : Entity_Id;
7061 New_Lit : Entity_Id;
7062 Literals_List : List_Id;
7063 Type_Decl : Node_Id;
7064 Hi, Lo : Node_Id;
7065 Rang_Expr : Node_Id;
7066
7067 begin
7068 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7069 -- not have explicit literals lists we need to process types derived
7070 -- from them specially. This is handled by Derived_Standard_Character.
7071 -- If the parent type is a generic type, there are no literals either,
7072 -- and we construct the same skeletal representation as for the generic
7073 -- parent type.
7074
7075 if Is_Standard_Character_Type (Parent_Type) then
7076 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7077
7078 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7079 declare
7080 Lo : Node_Id;
7081 Hi : Node_Id;
7082
7083 begin
7084 if Nkind (Indic) /= N_Subtype_Indication then
7085 Lo :=
7086 Make_Attribute_Reference (Loc,
7087 Attribute_Name => Name_First,
7088 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7089 Set_Etype (Lo, Derived_Type);
7090
7091 Hi :=
7092 Make_Attribute_Reference (Loc,
7093 Attribute_Name => Name_Last,
7094 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7095 Set_Etype (Hi, Derived_Type);
7096
7097 Set_Scalar_Range (Derived_Type,
7098 Make_Range (Loc,
7099 Low_Bound => Lo,
7100 High_Bound => Hi));
7101 else
7102
7103 -- Analyze subtype indication and verify compatibility
7104 -- with parent type.
7105
7106 if Base_Type (Process_Subtype (Indic, N)) /=
7107 Base_Type (Parent_Type)
7108 then
7109 Error_Msg_N
7110 ("illegal constraint for formal discrete type", N);
7111 end if;
7112 end if;
7113 end;
7114
7115 else
7116 -- If a constraint is present, analyze the bounds to catch
7117 -- premature usage of the derived literals.
7118
7119 if Nkind (Indic) = N_Subtype_Indication
7120 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7121 then
7122 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7123 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7124 end if;
7125
7126 -- Introduce an implicit base type for the derived type even if there
7127 -- is no constraint attached to it, since this seems closer to the
7128 -- Ada semantics. Build a full type declaration tree for the derived
7129 -- type using the implicit base type as the defining identifier. The
7130 -- build a subtype declaration tree which applies the constraint (if
7131 -- any) have it replace the derived type declaration.
7132
7133 Literal := First_Literal (Parent_Type);
7134 Literals_List := New_List;
7135 while Present (Literal)
7136 and then Ekind (Literal) = E_Enumeration_Literal
7137 loop
7138 -- Literals of the derived type have the same representation as
7139 -- those of the parent type, but this representation can be
7140 -- overridden by an explicit representation clause. Indicate
7141 -- that there is no explicit representation given yet. These
7142 -- derived literals are implicit operations of the new type,
7143 -- and can be overridden by explicit ones.
7144
7145 if Nkind (Literal) = N_Defining_Character_Literal then
7146 New_Lit :=
7147 Make_Defining_Character_Literal (Loc, Chars (Literal));
7148 else
7149 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7150 end if;
7151
7152 Set_Ekind (New_Lit, E_Enumeration_Literal);
7153 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7154 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7155 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7156 Set_Alias (New_Lit, Literal);
7157 Set_Is_Known_Valid (New_Lit, True);
7158
7159 Append (New_Lit, Literals_List);
7160 Next_Literal (Literal);
7161 end loop;
7162
7163 Implicit_Base :=
7164 Make_Defining_Identifier (Sloc (Derived_Type),
7165 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7166
7167 -- Indicate the proper nature of the derived type. This must be done
7168 -- before analysis of the literals, to recognize cases when a literal
7169 -- may be hidden by a previous explicit function definition (cf.
7170 -- c83031a).
7171
7172 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7173 Set_Etype (Derived_Type, Implicit_Base);
7174
7175 Type_Decl :=
7176 Make_Full_Type_Declaration (Loc,
7177 Defining_Identifier => Implicit_Base,
7178 Discriminant_Specifications => No_List,
7179 Type_Definition =>
7180 Make_Enumeration_Type_Definition (Loc, Literals_List));
7181
7182 Mark_Rewrite_Insertion (Type_Decl);
7183 Insert_Before (N, Type_Decl);
7184 Analyze (Type_Decl);
7185
7186 -- The anonymous base now has a full declaration, but this base
7187 -- is not a first subtype.
7188
7189 Set_Is_First_Subtype (Implicit_Base, False);
7190
7191 -- After the implicit base is analyzed its Etype needs to be changed
7192 -- to reflect the fact that it is derived from the parent type which
7193 -- was ignored during analysis. We also set the size at this point.
7194
7195 Set_Etype (Implicit_Base, Parent_Type);
7196
7197 Set_Size_Info (Implicit_Base, Parent_Type);
7198 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7199 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7200
7201 -- Copy other flags from parent type
7202
7203 Set_Has_Non_Standard_Rep
7204 (Implicit_Base, Has_Non_Standard_Rep
7205 (Parent_Type));
7206 Set_Has_Pragma_Ordered
7207 (Implicit_Base, Has_Pragma_Ordered
7208 (Parent_Type));
7209 Set_Has_Delayed_Freeze (Implicit_Base);
7210
7211 -- Process the subtype indication including a validation check on the
7212 -- constraint, if any. If a constraint is given, its bounds must be
7213 -- implicitly converted to the new type.
7214
7215 if Nkind (Indic) = N_Subtype_Indication then
7216 declare
7217 R : constant Node_Id :=
7218 Range_Expression (Constraint (Indic));
7219
7220 begin
7221 if Nkind (R) = N_Range then
7222 Hi := Build_Scalar_Bound
7223 (High_Bound (R), Parent_Type, Implicit_Base);
7224 Lo := Build_Scalar_Bound
7225 (Low_Bound (R), Parent_Type, Implicit_Base);
7226
7227 else
7228 -- Constraint is a Range attribute. Replace with explicit
7229 -- mention of the bounds of the prefix, which must be a
7230 -- subtype.
7231
7232 Analyze (Prefix (R));
7233 Hi :=
7234 Convert_To (Implicit_Base,
7235 Make_Attribute_Reference (Loc,
7236 Attribute_Name => Name_Last,
7237 Prefix =>
7238 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7239
7240 Lo :=
7241 Convert_To (Implicit_Base,
7242 Make_Attribute_Reference (Loc,
7243 Attribute_Name => Name_First,
7244 Prefix =>
7245 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7246 end if;
7247 end;
7248
7249 else
7250 Hi :=
7251 Build_Scalar_Bound
7252 (Type_High_Bound (Parent_Type),
7253 Parent_Type, Implicit_Base);
7254 Lo :=
7255 Build_Scalar_Bound
7256 (Type_Low_Bound (Parent_Type),
7257 Parent_Type, Implicit_Base);
7258 end if;
7259
7260 Rang_Expr :=
7261 Make_Range (Loc,
7262 Low_Bound => Lo,
7263 High_Bound => Hi);
7264
7265 -- If we constructed a default range for the case where no range
7266 -- was given, then the expressions in the range must not freeze
7267 -- since they do not correspond to expressions in the source.
7268 -- However, if the type inherits predicates the expressions will
7269 -- be elaborated earlier and must freeze.
7270
7271 if Nkind (Indic) /= N_Subtype_Indication
7272 and then not Has_Predicates (Derived_Type)
7273 then
7274 Set_Must_Not_Freeze (Lo);
7275 Set_Must_Not_Freeze (Hi);
7276 Set_Must_Not_Freeze (Rang_Expr);
7277 end if;
7278
7279 Rewrite (N,
7280 Make_Subtype_Declaration (Loc,
7281 Defining_Identifier => Derived_Type,
7282 Subtype_Indication =>
7283 Make_Subtype_Indication (Loc,
7284 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7285 Constraint =>
7286 Make_Range_Constraint (Loc,
7287 Range_Expression => Rang_Expr))));
7288
7289 Analyze (N);
7290
7291 -- Propagate the aspects from the original type declaration to the
7292 -- declaration of the implicit base.
7293
7294 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7295
7296 -- Apply a range check. Since this range expression doesn't have an
7297 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7298 -- this right???
7299
7300 if Nkind (Indic) = N_Subtype_Indication then
7301 Apply_Range_Check
7302 (Range_Expression (Constraint (Indic)), Parent_Type,
7303 Source_Typ => Entity (Subtype_Mark (Indic)));
7304 end if;
7305 end if;
7306 end Build_Derived_Enumeration_Type;
7307
7308 --------------------------------
7309 -- Build_Derived_Numeric_Type --
7310 --------------------------------
7311
7312 procedure Build_Derived_Numeric_Type
7313 (N : Node_Id;
7314 Parent_Type : Entity_Id;
7315 Derived_Type : Entity_Id)
7316 is
7317 Loc : constant Source_Ptr := Sloc (N);
7318 Tdef : constant Node_Id := Type_Definition (N);
7319 Indic : constant Node_Id := Subtype_Indication (Tdef);
7320 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7321 No_Constraint : constant Boolean := Nkind (Indic) /=
7322 N_Subtype_Indication;
7323 Implicit_Base : Entity_Id;
7324
7325 Lo : Node_Id;
7326 Hi : Node_Id;
7327
7328 begin
7329 -- Process the subtype indication including a validation check on
7330 -- the constraint if any.
7331
7332 Discard_Node (Process_Subtype (Indic, N));
7333
7334 -- Introduce an implicit base type for the derived type even if there
7335 -- is no constraint attached to it, since this seems closer to the Ada
7336 -- semantics.
7337
7338 Implicit_Base :=
7339 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7340
7341 Set_Etype (Implicit_Base, Parent_Base);
7342 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7343 Set_Size_Info (Implicit_Base, Parent_Base);
7344 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7345 Set_Parent (Implicit_Base, Parent (Derived_Type));
7346 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7347
7348 -- Set RM Size for discrete type or decimal fixed-point type
7349 -- Ordinary fixed-point is excluded, why???
7350
7351 if Is_Discrete_Type (Parent_Base)
7352 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7353 then
7354 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7355 end if;
7356
7357 Set_Has_Delayed_Freeze (Implicit_Base);
7358
7359 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7360 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7361
7362 Set_Scalar_Range (Implicit_Base,
7363 Make_Range (Loc,
7364 Low_Bound => Lo,
7365 High_Bound => Hi));
7366
7367 if Has_Infinities (Parent_Base) then
7368 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7369 end if;
7370
7371 -- The Derived_Type, which is the entity of the declaration, is a
7372 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7373 -- absence of an explicit constraint.
7374
7375 Set_Etype (Derived_Type, Implicit_Base);
7376
7377 -- If we did not have a constraint, then the Ekind is set from the
7378 -- parent type (otherwise Process_Subtype has set the bounds)
7379
7380 if No_Constraint then
7381 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7382 end if;
7383
7384 -- If we did not have a range constraint, then set the range from the
7385 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7386
7387 if No_Constraint or else not Has_Range_Constraint (Indic) then
7388 Set_Scalar_Range (Derived_Type,
7389 Make_Range (Loc,
7390 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7391 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7392 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7393
7394 if Has_Infinities (Parent_Type) then
7395 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7396 end if;
7397
7398 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7399 end if;
7400
7401 Set_Is_Descendant_Of_Address (Derived_Type,
7402 Is_Descendant_Of_Address (Parent_Type));
7403 Set_Is_Descendant_Of_Address (Implicit_Base,
7404 Is_Descendant_Of_Address (Parent_Type));
7405
7406 -- Set remaining type-specific fields, depending on numeric type
7407
7408 if Is_Modular_Integer_Type (Parent_Type) then
7409 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7410
7411 Set_Non_Binary_Modulus
7412 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7413
7414 Set_Is_Known_Valid
7415 (Implicit_Base, Is_Known_Valid (Parent_Base));
7416
7417 elsif Is_Floating_Point_Type (Parent_Type) then
7418
7419 -- Digits of base type is always copied from the digits value of
7420 -- the parent base type, but the digits of the derived type will
7421 -- already have been set if there was a constraint present.
7422
7423 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7424 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7425
7426 if No_Constraint then
7427 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7428 end if;
7429
7430 elsif Is_Fixed_Point_Type (Parent_Type) then
7431
7432 -- Small of base type and derived type are always copied from the
7433 -- parent base type, since smalls never change. The delta of the
7434 -- base type is also copied from the parent base type. However the
7435 -- delta of the derived type will have been set already if a
7436 -- constraint was present.
7437
7438 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7439 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7440 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7441
7442 if No_Constraint then
7443 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7444 end if;
7445
7446 -- The scale and machine radix in the decimal case are always
7447 -- copied from the parent base type.
7448
7449 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7450 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7451 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7452
7453 Set_Machine_Radix_10
7454 (Derived_Type, Machine_Radix_10 (Parent_Base));
7455 Set_Machine_Radix_10
7456 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7457
7458 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7459
7460 if No_Constraint then
7461 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7462
7463 else
7464 -- the analysis of the subtype_indication sets the
7465 -- digits value of the derived type.
7466
7467 null;
7468 end if;
7469 end if;
7470 end if;
7471
7472 if Is_Integer_Type (Parent_Type) then
7473 Set_Has_Shift_Operator
7474 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7475 end if;
7476
7477 -- The type of the bounds is that of the parent type, and they
7478 -- must be converted to the derived type.
7479
7480 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7481
7482 -- The implicit_base should be frozen when the derived type is frozen,
7483 -- but note that it is used in the conversions of the bounds. For fixed
7484 -- types we delay the determination of the bounds until the proper
7485 -- freezing point. For other numeric types this is rejected by GCC, for
7486 -- reasons that are currently unclear (???), so we choose to freeze the
7487 -- implicit base now. In the case of integers and floating point types
7488 -- this is harmless because subsequent representation clauses cannot
7489 -- affect anything, but it is still baffling that we cannot use the
7490 -- same mechanism for all derived numeric types.
7491
7492 -- There is a further complication: actually some representation
7493 -- clauses can affect the implicit base type. For example, attribute
7494 -- definition clauses for stream-oriented attributes need to set the
7495 -- corresponding TSS entries on the base type, and this normally
7496 -- cannot be done after the base type is frozen, so the circuitry in
7497 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7498 -- and not use Set_TSS in this case.
7499
7500 -- There are also consequences for the case of delayed representation
7501 -- aspects for some cases. For example, a Size aspect is delayed and
7502 -- should not be evaluated to the freeze point. This early freezing
7503 -- means that the size attribute evaluation happens too early???
7504
7505 if Is_Fixed_Point_Type (Parent_Type) then
7506 Conditional_Delay (Implicit_Base, Parent_Type);
7507 else
7508 Freeze_Before (N, Implicit_Base);
7509 end if;
7510 end Build_Derived_Numeric_Type;
7511
7512 --------------------------------
7513 -- Build_Derived_Private_Type --
7514 --------------------------------
7515
7516 procedure Build_Derived_Private_Type
7517 (N : Node_Id;
7518 Parent_Type : Entity_Id;
7519 Derived_Type : Entity_Id;
7520 Is_Completion : Boolean;
7521 Derive_Subps : Boolean := True)
7522 is
7523 Loc : constant Source_Ptr := Sloc (N);
7524 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7525 Par_Scope : constant Entity_Id := Scope (Par_Base);
7526 Full_N : constant Node_Id := New_Copy_Tree (N);
7527 Full_Der : Entity_Id := New_Copy (Derived_Type);
7528 Full_P : Entity_Id;
7529
7530 procedure Build_Full_Derivation;
7531 -- Build full derivation, i.e. derive from the full view
7532
7533 procedure Copy_And_Build;
7534 -- Copy derived type declaration, replace parent with its full view,
7535 -- and build derivation
7536
7537 ---------------------------
7538 -- Build_Full_Derivation --
7539 ---------------------------
7540
7541 procedure Build_Full_Derivation is
7542 begin
7543 -- If parent scope is not open, install the declarations
7544
7545 if not In_Open_Scopes (Par_Scope) then
7546 Install_Private_Declarations (Par_Scope);
7547 Install_Visible_Declarations (Par_Scope);
7548 Copy_And_Build;
7549 Uninstall_Declarations (Par_Scope);
7550
7551 -- If parent scope is open and in another unit, and parent has a
7552 -- completion, then the derivation is taking place in the visible
7553 -- part of a child unit. In that case retrieve the full view of
7554 -- the parent momentarily.
7555
7556 elsif not In_Same_Source_Unit (N, Parent_Type) then
7557 Full_P := Full_View (Parent_Type);
7558 Exchange_Declarations (Parent_Type);
7559 Copy_And_Build;
7560 Exchange_Declarations (Full_P);
7561
7562 -- Otherwise it is a local derivation
7563
7564 else
7565 Copy_And_Build;
7566 end if;
7567 end Build_Full_Derivation;
7568
7569 --------------------
7570 -- Copy_And_Build --
7571 --------------------
7572
7573 procedure Copy_And_Build is
7574 Full_Parent : Entity_Id := Parent_Type;
7575
7576 begin
7577 -- If the parent is itself derived from another private type,
7578 -- installing the private declarations has not affected its
7579 -- privacy status, so use its own full view explicitly.
7580
7581 if Is_Private_Type (Full_Parent)
7582 and then Present (Full_View (Full_Parent))
7583 then
7584 Full_Parent := Full_View (Full_Parent);
7585 end if;
7586
7587 -- And its underlying full view if necessary
7588
7589 if Is_Private_Type (Full_Parent)
7590 and then Present (Underlying_Full_View (Full_Parent))
7591 then
7592 Full_Parent := Underlying_Full_View (Full_Parent);
7593 end if;
7594
7595 -- For record, access and most enumeration types, derivation from
7596 -- the full view requires a fully-fledged declaration. In the other
7597 -- cases, just use an itype.
7598
7599 if Ekind (Full_Parent) in Record_Kind
7600 or else Ekind (Full_Parent) in Access_Kind
7601 or else
7602 (Ekind (Full_Parent) in Enumeration_Kind
7603 and then not Is_Standard_Character_Type (Full_Parent)
7604 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7605 then
7606 -- Copy and adjust declaration to provide a completion for what
7607 -- is originally a private declaration. Indicate that full view
7608 -- is internally generated.
7609
7610 Set_Comes_From_Source (Full_N, False);
7611 Set_Comes_From_Source (Full_Der, False);
7612 Set_Parent (Full_Der, Full_N);
7613 Set_Defining_Identifier (Full_N, Full_Der);
7614
7615 -- If there are no constraints, adjust the subtype mark
7616
7617 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7618 N_Subtype_Indication
7619 then
7620 Set_Subtype_Indication
7621 (Type_Definition (Full_N),
7622 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7623 end if;
7624
7625 Insert_After (N, Full_N);
7626
7627 -- Build full view of derived type from full view of parent which
7628 -- is now installed. Subprograms have been derived on the partial
7629 -- view, the completion does not derive them anew.
7630
7631 if Ekind (Full_Parent) in Record_Kind then
7632
7633 -- If parent type is tagged, the completion inherits the proper
7634 -- primitive operations.
7635
7636 if Is_Tagged_Type (Parent_Type) then
7637 Build_Derived_Record_Type
7638 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7639 else
7640 Build_Derived_Record_Type
7641 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7642 end if;
7643
7644 else
7645 Build_Derived_Type
7646 (Full_N, Full_Parent, Full_Der,
7647 Is_Completion => False, Derive_Subps => False);
7648 end if;
7649
7650 -- The full declaration has been introduced into the tree and
7651 -- processed in the step above. It should not be analyzed again
7652 -- (when encountered later in the current list of declarations)
7653 -- to prevent spurious name conflicts. The full entity remains
7654 -- invisible.
7655
7656 Set_Analyzed (Full_N);
7657
7658 else
7659 Full_Der :=
7660 Make_Defining_Identifier (Sloc (Derived_Type),
7661 Chars => Chars (Derived_Type));
7662 Set_Is_Itype (Full_Der);
7663 Set_Associated_Node_For_Itype (Full_Der, N);
7664 Set_Parent (Full_Der, N);
7665 Build_Derived_Type
7666 (N, Full_Parent, Full_Der,
7667 Is_Completion => False, Derive_Subps => False);
7668 end if;
7669
7670 Set_Has_Private_Declaration (Full_Der);
7671 Set_Has_Private_Declaration (Derived_Type);
7672
7673 Set_Scope (Full_Der, Scope (Derived_Type));
7674 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7675 Set_Has_Size_Clause (Full_Der, False);
7676 Set_Has_Alignment_Clause (Full_Der, False);
7677 Set_Has_Delayed_Freeze (Full_Der);
7678 Set_Is_Frozen (Full_Der, False);
7679 Set_Freeze_Node (Full_Der, Empty);
7680 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7681 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7682
7683 -- The convention on the base type may be set in the private part
7684 -- and not propagated to the subtype until later, so we obtain the
7685 -- convention from the base type of the parent.
7686
7687 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7688 end Copy_And_Build;
7689
7690 -- Start of processing for Build_Derived_Private_Type
7691
7692 begin
7693 if Is_Tagged_Type (Parent_Type) then
7694 Full_P := Full_View (Parent_Type);
7695
7696 -- A type extension of a type with unknown discriminants is an
7697 -- indefinite type that the back-end cannot handle directly.
7698 -- We treat it as a private type, and build a completion that is
7699 -- derived from the full view of the parent, and hopefully has
7700 -- known discriminants.
7701
7702 -- If the full view of the parent type has an underlying record view,
7703 -- use it to generate the underlying record view of this derived type
7704 -- (required for chains of derivations with unknown discriminants).
7705
7706 -- Minor optimization: we avoid the generation of useless underlying
7707 -- record view entities if the private type declaration has unknown
7708 -- discriminants but its corresponding full view has no
7709 -- discriminants.
7710
7711 if Has_Unknown_Discriminants (Parent_Type)
7712 and then Present (Full_P)
7713 and then (Has_Discriminants (Full_P)
7714 or else Present (Underlying_Record_View (Full_P)))
7715 and then not In_Open_Scopes (Par_Scope)
7716 and then Expander_Active
7717 then
7718 declare
7719 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7720 New_Ext : constant Node_Id :=
7721 Copy_Separate_Tree
7722 (Record_Extension_Part (Type_Definition (N)));
7723 Decl : Node_Id;
7724
7725 begin
7726 Build_Derived_Record_Type
7727 (N, Parent_Type, Derived_Type, Derive_Subps);
7728
7729 -- Build anonymous completion, as a derivation from the full
7730 -- view of the parent. This is not a completion in the usual
7731 -- sense, because the current type is not private.
7732
7733 Decl :=
7734 Make_Full_Type_Declaration (Loc,
7735 Defining_Identifier => Full_Der,
7736 Type_Definition =>
7737 Make_Derived_Type_Definition (Loc,
7738 Subtype_Indication =>
7739 New_Copy_Tree
7740 (Subtype_Indication (Type_Definition (N))),
7741 Record_Extension_Part => New_Ext));
7742
7743 -- If the parent type has an underlying record view, use it
7744 -- here to build the new underlying record view.
7745
7746 if Present (Underlying_Record_View (Full_P)) then
7747 pragma Assert
7748 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7749 = N_Identifier);
7750 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7751 Underlying_Record_View (Full_P));
7752 end if;
7753
7754 Install_Private_Declarations (Par_Scope);
7755 Install_Visible_Declarations (Par_Scope);
7756 Insert_Before (N, Decl);
7757
7758 -- Mark entity as an underlying record view before analysis,
7759 -- to avoid generating the list of its primitive operations
7760 -- (which is not really required for this entity) and thus
7761 -- prevent spurious errors associated with missing overriding
7762 -- of abstract primitives (overridden only for Derived_Type).
7763
7764 Set_Ekind (Full_Der, E_Record_Type);
7765 Set_Is_Underlying_Record_View (Full_Der);
7766 Set_Default_SSO (Full_Der);
7767 Set_No_Reordering (Full_Der, No_Component_Reordering);
7768
7769 Analyze (Decl);
7770
7771 pragma Assert (Has_Discriminants (Full_Der)
7772 and then not Has_Unknown_Discriminants (Full_Der));
7773
7774 Uninstall_Declarations (Par_Scope);
7775
7776 -- Freeze the underlying record view, to prevent generation of
7777 -- useless dispatching information, which is simply shared with
7778 -- the real derived type.
7779
7780 Set_Is_Frozen (Full_Der);
7781
7782 -- If the derived type has access discriminants, create
7783 -- references to their anonymous types now, to prevent
7784 -- back-end problems when their first use is in generated
7785 -- bodies of primitives.
7786
7787 declare
7788 E : Entity_Id;
7789
7790 begin
7791 E := First_Entity (Full_Der);
7792
7793 while Present (E) loop
7794 if Ekind (E) = E_Discriminant
7795 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7796 then
7797 Build_Itype_Reference (Etype (E), Decl);
7798 end if;
7799
7800 Next_Entity (E);
7801 end loop;
7802 end;
7803
7804 -- Set up links between real entity and underlying record view
7805
7806 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7807 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7808 end;
7809
7810 -- If discriminants are known, build derived record
7811
7812 else
7813 Build_Derived_Record_Type
7814 (N, Parent_Type, Derived_Type, Derive_Subps);
7815 end if;
7816
7817 return;
7818
7819 elsif Has_Discriminants (Parent_Type) then
7820
7821 -- Build partial view of derived type from partial view of parent.
7822 -- This must be done before building the full derivation because the
7823 -- second derivation will modify the discriminants of the first and
7824 -- the discriminants are chained with the rest of the components in
7825 -- the full derivation.
7826
7827 Build_Derived_Record_Type
7828 (N, Parent_Type, Derived_Type, Derive_Subps);
7829
7830 -- Build the full derivation if this is not the anonymous derived
7831 -- base type created by Build_Derived_Record_Type in the constrained
7832 -- case (see point 5. of its head comment) since we build it for the
7833 -- derived subtype. And skip it for protected types altogether, as
7834 -- gigi does not use these types directly.
7835
7836 if Present (Full_View (Parent_Type))
7837 and then not Is_Itype (Derived_Type)
7838 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7839 then
7840 declare
7841 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7842 Discr : Entity_Id;
7843 Last_Discr : Entity_Id;
7844
7845 begin
7846 -- If this is not a completion, construct the implicit full
7847 -- view by deriving from the full view of the parent type.
7848 -- But if this is a completion, the derived private type
7849 -- being built is a full view and the full derivation can
7850 -- only be its underlying full view.
7851
7852 Build_Full_Derivation;
7853
7854 if not Is_Completion then
7855 Set_Full_View (Derived_Type, Full_Der);
7856 else
7857 Set_Underlying_Full_View (Derived_Type, Full_Der);
7858 Set_Is_Underlying_Full_View (Full_Der);
7859 end if;
7860
7861 if not Is_Base_Type (Derived_Type) then
7862 Set_Full_View (Der_Base, Base_Type (Full_Der));
7863 end if;
7864
7865 -- Copy the discriminant list from full view to the partial
7866 -- view (base type and its subtype). Gigi requires that the
7867 -- partial and full views have the same discriminants.
7868
7869 -- Note that since the partial view points to discriminants
7870 -- in the full view, their scope will be that of the full
7871 -- view. This might cause some front end problems and need
7872 -- adjustment???
7873
7874 Discr := First_Discriminant (Base_Type (Full_Der));
7875 Set_First_Entity (Der_Base, Discr);
7876
7877 loop
7878 Last_Discr := Discr;
7879 Next_Discriminant (Discr);
7880 exit when No (Discr);
7881 end loop;
7882
7883 Set_Last_Entity (Der_Base, Last_Discr);
7884 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7885 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7886 end;
7887 end if;
7888
7889 elsif Present (Full_View (Parent_Type))
7890 and then Has_Discriminants (Full_View (Parent_Type))
7891 then
7892 if Has_Unknown_Discriminants (Parent_Type)
7893 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7894 N_Subtype_Indication
7895 then
7896 Error_Msg_N
7897 ("cannot constrain type with unknown discriminants",
7898 Subtype_Indication (Type_Definition (N)));
7899 return;
7900 end if;
7901
7902 -- If this is not a completion, construct the implicit full view by
7903 -- deriving from the full view of the parent type. But if this is a
7904 -- completion, the derived private type being built is a full view
7905 -- and the full derivation can only be its underlying full view.
7906
7907 Build_Full_Derivation;
7908
7909 if not Is_Completion then
7910 Set_Full_View (Derived_Type, Full_Der);
7911 else
7912 Set_Underlying_Full_View (Derived_Type, Full_Der);
7913 Set_Is_Underlying_Full_View (Full_Der);
7914 end if;
7915
7916 -- In any case, the primitive operations are inherited from the
7917 -- parent type, not from the internal full view.
7918
7919 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7920
7921 if Derive_Subps then
7922 Derive_Subprograms (Parent_Type, Derived_Type);
7923 end if;
7924
7925 Set_Stored_Constraint (Derived_Type, No_Elist);
7926 Set_Is_Constrained
7927 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7928
7929 else
7930 -- Untagged type, No discriminants on either view
7931
7932 if Nkind (Subtype_Indication (Type_Definition (N))) =
7933 N_Subtype_Indication
7934 then
7935 Error_Msg_N
7936 ("illegal constraint on type without discriminants", N);
7937 end if;
7938
7939 if Present (Discriminant_Specifications (N))
7940 and then Present (Full_View (Parent_Type))
7941 and then not Is_Tagged_Type (Full_View (Parent_Type))
7942 then
7943 Error_Msg_N ("cannot add discriminants to untagged type", N);
7944 end if;
7945
7946 Set_Stored_Constraint (Derived_Type, No_Elist);
7947 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7948
7949 Set_Is_Controlled_Active
7950 (Derived_Type, Is_Controlled_Active (Parent_Type));
7951
7952 Set_Disable_Controlled
7953 (Derived_Type, Disable_Controlled (Parent_Type));
7954
7955 Set_Has_Controlled_Component
7956 (Derived_Type, Has_Controlled_Component (Parent_Type));
7957
7958 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7959
7960 if not Is_Controlled (Parent_Type) then
7961 Set_Finalize_Storage_Only
7962 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7963 end if;
7964
7965 -- If this is not a completion, construct the implicit full view by
7966 -- deriving from the full view of the parent type.
7967
7968 -- ??? If the parent is untagged private and its completion is
7969 -- tagged, this mechanism will not work because we cannot derive from
7970 -- the tagged full view unless we have an extension.
7971
7972 if Present (Full_View (Parent_Type))
7973 and then not Is_Tagged_Type (Full_View (Parent_Type))
7974 and then not Is_Completion
7975 then
7976 Build_Full_Derivation;
7977 Set_Full_View (Derived_Type, Full_Der);
7978 end if;
7979 end if;
7980
7981 Set_Has_Unknown_Discriminants (Derived_Type,
7982 Has_Unknown_Discriminants (Parent_Type));
7983
7984 if Is_Private_Type (Derived_Type) then
7985 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7986 end if;
7987
7988 -- If the parent base type is in scope, add the derived type to its
7989 -- list of private dependents, because its full view may become
7990 -- visible subsequently (in a nested private part, a body, or in a
7991 -- further child unit).
7992
7993 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7994 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7995
7996 -- Check for unusual case where a type completed by a private
7997 -- derivation occurs within a package nested in a child unit, and
7998 -- the parent is declared in an ancestor.
7999
8000 if Is_Child_Unit (Scope (Current_Scope))
8001 and then Is_Completion
8002 and then In_Private_Part (Current_Scope)
8003 and then Scope (Parent_Type) /= Current_Scope
8004
8005 -- Note that if the parent has a completion in the private part,
8006 -- (which is itself a derivation from some other private type)
8007 -- it is that completion that is visible, there is no full view
8008 -- available, and no special processing is needed.
8009
8010 and then Present (Full_View (Parent_Type))
8011 then
8012 -- In this case, the full view of the parent type will become
8013 -- visible in the body of the enclosing child, and only then will
8014 -- the current type be possibly non-private. Build an underlying
8015 -- full view that will be installed when the enclosing child body
8016 -- is compiled.
8017
8018 if Present (Underlying_Full_View (Derived_Type)) then
8019 Full_Der := Underlying_Full_View (Derived_Type);
8020 else
8021 Build_Full_Derivation;
8022 Set_Underlying_Full_View (Derived_Type, Full_Der);
8023 Set_Is_Underlying_Full_View (Full_Der);
8024 end if;
8025
8026 -- The full view will be used to swap entities on entry/exit to
8027 -- the body, and must appear in the entity list for the package.
8028
8029 Append_Entity (Full_Der, Scope (Derived_Type));
8030 end if;
8031 end if;
8032 end Build_Derived_Private_Type;
8033
8034 -------------------------------
8035 -- Build_Derived_Record_Type --
8036 -------------------------------
8037
8038 -- 1. INTRODUCTION
8039
8040 -- Ideally we would like to use the same model of type derivation for
8041 -- tagged and untagged record types. Unfortunately this is not quite
8042 -- possible because the semantics of representation clauses is different
8043 -- for tagged and untagged records under inheritance. Consider the
8044 -- following:
8045
8046 -- type R (...) is [tagged] record ... end record;
8047 -- type T (...) is new R (...) [with ...];
8048
8049 -- The representation clauses for T can specify a completely different
8050 -- record layout from R's. Hence the same component can be placed in two
8051 -- very different positions in objects of type T and R. If R and T are
8052 -- tagged types, representation clauses for T can only specify the layout
8053 -- of non inherited components, thus components that are common in R and T
8054 -- have the same position in objects of type R and T.
8055
8056 -- This has two implications. The first is that the entire tree for R's
8057 -- declaration needs to be copied for T in the untagged case, so that T
8058 -- can be viewed as a record type of its own with its own representation
8059 -- clauses. The second implication is the way we handle discriminants.
8060 -- Specifically, in the untagged case we need a way to communicate to Gigi
8061 -- what are the real discriminants in the record, while for the semantics
8062 -- we need to consider those introduced by the user to rename the
8063 -- discriminants in the parent type. This is handled by introducing the
8064 -- notion of stored discriminants. See below for more.
8065
8066 -- Fortunately the way regular components are inherited can be handled in
8067 -- the same way in tagged and untagged types.
8068
8069 -- To complicate things a bit more the private view of a private extension
8070 -- cannot be handled in the same way as the full view (for one thing the
8071 -- semantic rules are somewhat different). We will explain what differs
8072 -- below.
8073
8074 -- 2. DISCRIMINANTS UNDER INHERITANCE
8075
8076 -- The semantic rules governing the discriminants of derived types are
8077 -- quite subtle.
8078
8079 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8080 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8081
8082 -- If parent type has discriminants, then the discriminants that are
8083 -- declared in the derived type are [3.4 (11)]:
8084
8085 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8086 -- there is one;
8087
8088 -- o Otherwise, each discriminant of the parent type (implicitly declared
8089 -- in the same order with the same specifications). In this case, the
8090 -- discriminants are said to be "inherited", or if unknown in the parent
8091 -- are also unknown in the derived type.
8092
8093 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8094
8095 -- o The parent subtype must be constrained;
8096
8097 -- o If the parent type is not a tagged type, then each discriminant of
8098 -- the derived type must be used in the constraint defining a parent
8099 -- subtype. [Implementation note: This ensures that the new discriminant
8100 -- can share storage with an existing discriminant.]
8101
8102 -- For the derived type each discriminant of the parent type is either
8103 -- inherited, constrained to equal some new discriminant of the derived
8104 -- type, or constrained to the value of an expression.
8105
8106 -- When inherited or constrained to equal some new discriminant, the
8107 -- parent discriminant and the discriminant of the derived type are said
8108 -- to "correspond".
8109
8110 -- If a discriminant of the parent type is constrained to a specific value
8111 -- in the derived type definition, then the discriminant is said to be
8112 -- "specified" by that derived type definition.
8113
8114 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8115
8116 -- We have spoken about stored discriminants in point 1 (introduction)
8117 -- above. There are two sorts of stored discriminants: implicit and
8118 -- explicit. As long as the derived type inherits the same discriminants as
8119 -- the root record type, stored discriminants are the same as regular
8120 -- discriminants, and are said to be implicit. However, if any discriminant
8121 -- in the root type was renamed in the derived type, then the derived
8122 -- type will contain explicit stored discriminants. Explicit stored
8123 -- discriminants are discriminants in addition to the semantically visible
8124 -- discriminants defined for the derived type. Stored discriminants are
8125 -- used by Gigi to figure out what are the physical discriminants in
8126 -- objects of the derived type (see precise definition in einfo.ads).
8127 -- As an example, consider the following:
8128
8129 -- type R (D1, D2, D3 : Int) is record ... end record;
8130 -- type T1 is new R;
8131 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8132 -- type T3 is new T2;
8133 -- type T4 (Y : Int) is new T3 (Y, 99);
8134
8135 -- The following table summarizes the discriminants and stored
8136 -- discriminants in R and T1 through T4:
8137
8138 -- Type Discrim Stored Discrim Comment
8139 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8140 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8141 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8142 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8143 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8144
8145 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8146 -- find the corresponding discriminant in the parent type, while
8147 -- Original_Record_Component (abbreviated ORC below) the actual physical
8148 -- component that is renamed. Finally the field Is_Completely_Hidden
8149 -- (abbreviated ICH below) is set for all explicit stored discriminants
8150 -- (see einfo.ads for more info). For the above example this gives:
8151
8152 -- Discrim CD ORC ICH
8153 -- ^^^^^^^ ^^ ^^^ ^^^
8154 -- D1 in R empty itself no
8155 -- D2 in R empty itself no
8156 -- D3 in R empty itself no
8157
8158 -- D1 in T1 D1 in R itself no
8159 -- D2 in T1 D2 in R itself no
8160 -- D3 in T1 D3 in R itself no
8161
8162 -- X1 in T2 D3 in T1 D3 in T2 no
8163 -- X2 in T2 D1 in T1 D1 in T2 no
8164 -- D1 in T2 empty itself yes
8165 -- D2 in T2 empty itself yes
8166 -- D3 in T2 empty itself yes
8167
8168 -- X1 in T3 X1 in T2 D3 in T3 no
8169 -- X2 in T3 X2 in T2 D1 in T3 no
8170 -- D1 in T3 empty itself yes
8171 -- D2 in T3 empty itself yes
8172 -- D3 in T3 empty itself yes
8173
8174 -- Y in T4 X1 in T3 D3 in T4 no
8175 -- D1 in T4 empty itself yes
8176 -- D2 in T4 empty itself yes
8177 -- D3 in T4 empty itself yes
8178
8179 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8180
8181 -- Type derivation for tagged types is fairly straightforward. If no
8182 -- discriminants are specified by the derived type, these are inherited
8183 -- from the parent. No explicit stored discriminants are ever necessary.
8184 -- The only manipulation that is done to the tree is that of adding a
8185 -- _parent field with parent type and constrained to the same constraint
8186 -- specified for the parent in the derived type definition. For instance:
8187
8188 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8189 -- type T1 is new R with null record;
8190 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8191
8192 -- are changed into:
8193
8194 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8195 -- _parent : R (D1, D2, D3);
8196 -- end record;
8197
8198 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8199 -- _parent : T1 (X2, 88, X1);
8200 -- end record;
8201
8202 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8203 -- ORC and ICH fields are:
8204
8205 -- Discrim CD ORC ICH
8206 -- ^^^^^^^ ^^ ^^^ ^^^
8207 -- D1 in R empty itself no
8208 -- D2 in R empty itself no
8209 -- D3 in R empty itself no
8210
8211 -- D1 in T1 D1 in R D1 in R no
8212 -- D2 in T1 D2 in R D2 in R no
8213 -- D3 in T1 D3 in R D3 in R no
8214
8215 -- X1 in T2 D3 in T1 D3 in R no
8216 -- X2 in T2 D1 in T1 D1 in R no
8217
8218 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8219 --
8220 -- Regardless of whether we dealing with a tagged or untagged type
8221 -- we will transform all derived type declarations of the form
8222 --
8223 -- type T is new R (...) [with ...];
8224 -- or
8225 -- subtype S is R (...);
8226 -- type T is new S [with ...];
8227 -- into
8228 -- type BT is new R [with ...];
8229 -- subtype T is BT (...);
8230 --
8231 -- That is, the base derived type is constrained only if it has no
8232 -- discriminants. The reason for doing this is that GNAT's semantic model
8233 -- assumes that a base type with discriminants is unconstrained.
8234 --
8235 -- Note that, strictly speaking, the above transformation is not always
8236 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8237 --
8238 -- procedure B34011A is
8239 -- type REC (D : integer := 0) is record
8240 -- I : Integer;
8241 -- end record;
8242
8243 -- package P is
8244 -- type T6 is new Rec;
8245 -- function F return T6;
8246 -- end P;
8247
8248 -- use P;
8249 -- package Q6 is
8250 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8251 -- end Q6;
8252 --
8253 -- The definition of Q6.U is illegal. However transforming Q6.U into
8254
8255 -- type BaseU is new T6;
8256 -- subtype U is BaseU (Q6.F.I)
8257
8258 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8259 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8260 -- the transformation described above.
8261
8262 -- There is another instance where the above transformation is incorrect.
8263 -- Consider:
8264
8265 -- package Pack is
8266 -- type Base (D : Integer) is tagged null record;
8267 -- procedure P (X : Base);
8268
8269 -- type Der is new Base (2) with null record;
8270 -- procedure P (X : Der);
8271 -- end Pack;
8272
8273 -- Then the above transformation turns this into
8274
8275 -- type Der_Base is new Base with null record;
8276 -- -- procedure P (X : Base) is implicitly inherited here
8277 -- -- as procedure P (X : Der_Base).
8278
8279 -- subtype Der is Der_Base (2);
8280 -- procedure P (X : Der);
8281 -- -- The overriding of P (X : Der_Base) is illegal since we
8282 -- -- have a parameter conformance problem.
8283
8284 -- To get around this problem, after having semantically processed Der_Base
8285 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8286 -- Discriminant_Constraint from Der so that when parameter conformance is
8287 -- checked when P is overridden, no semantic errors are flagged.
8288
8289 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8290
8291 -- Regardless of whether we are dealing with a tagged or untagged type
8292 -- we will transform all derived type declarations of the form
8293
8294 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8295 -- type T is new R [with ...];
8296 -- into
8297 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8298
8299 -- The reason for such transformation is that it allows us to implement a
8300 -- very clean form of component inheritance as explained below.
8301
8302 -- Note that this transformation is not achieved by direct tree rewriting
8303 -- and manipulation, but rather by redoing the semantic actions that the
8304 -- above transformation will entail. This is done directly in routine
8305 -- Inherit_Components.
8306
8307 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8308
8309 -- In both tagged and untagged derived types, regular non discriminant
8310 -- components are inherited in the derived type from the parent type. In
8311 -- the absence of discriminants component, inheritance is straightforward
8312 -- as components can simply be copied from the parent.
8313
8314 -- If the parent has discriminants, inheriting components constrained with
8315 -- these discriminants requires caution. Consider the following example:
8316
8317 -- type R (D1, D2 : Positive) is [tagged] record
8318 -- S : String (D1 .. D2);
8319 -- end record;
8320
8321 -- type T1 is new R [with null record];
8322 -- type T2 (X : positive) is new R (1, X) [with null record];
8323
8324 -- As explained in 6. above, T1 is rewritten as
8325 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8326 -- which makes the treatment for T1 and T2 identical.
8327
8328 -- What we want when inheriting S, is that references to D1 and D2 in R are
8329 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8330 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8331 -- with either discriminant references in the derived type or expressions.
8332 -- This replacement is achieved as follows: before inheriting R's
8333 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8334 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8335 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8336 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8337 -- by String (1 .. X).
8338
8339 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8340
8341 -- We explain here the rules governing private type extensions relevant to
8342 -- type derivation. These rules are explained on the following example:
8343
8344 -- type D [(...)] is new A [(...)] with private; <-- partial view
8345 -- type D [(...)] is new P [(...)] with null record; <-- full view
8346
8347 -- Type A is called the ancestor subtype of the private extension.
8348 -- Type P is the parent type of the full view of the private extension. It
8349 -- must be A or a type derived from A.
8350
8351 -- The rules concerning the discriminants of private type extensions are
8352 -- [7.3(10-13)]:
8353
8354 -- o If a private extension inherits known discriminants from the ancestor
8355 -- subtype, then the full view must also inherit its discriminants from
8356 -- the ancestor subtype and the parent subtype of the full view must be
8357 -- constrained if and only if the ancestor subtype is constrained.
8358
8359 -- o If a partial view has unknown discriminants, then the full view may
8360 -- define a definite or an indefinite subtype, with or without
8361 -- discriminants.
8362
8363 -- o If a partial view has neither known nor unknown discriminants, then
8364 -- the full view must define a definite subtype.
8365
8366 -- o If the ancestor subtype of a private extension has constrained
8367 -- discriminants, then the parent subtype of the full view must impose a
8368 -- statically matching constraint on those discriminants.
8369
8370 -- This means that only the following forms of private extensions are
8371 -- allowed:
8372
8373 -- type D is new A with private; <-- partial view
8374 -- type D is new P with null record; <-- full view
8375
8376 -- If A has no discriminants than P has no discriminants, otherwise P must
8377 -- inherit A's discriminants.
8378
8379 -- type D is new A (...) with private; <-- partial view
8380 -- type D is new P (:::) with null record; <-- full view
8381
8382 -- P must inherit A's discriminants and (...) and (:::) must statically
8383 -- match.
8384
8385 -- subtype A is R (...);
8386 -- type D is new A with private; <-- partial view
8387 -- type D is new P with null record; <-- full view
8388
8389 -- P must have inherited R's discriminants and must be derived from A or
8390 -- any of its subtypes.
8391
8392 -- type D (..) is new A with private; <-- partial view
8393 -- type D (..) is new P [(:::)] with null record; <-- full view
8394
8395 -- No specific constraints on P's discriminants or constraint (:::).
8396 -- Note that A can be unconstrained, but the parent subtype P must either
8397 -- be constrained or (:::) must be present.
8398
8399 -- type D (..) is new A [(...)] with private; <-- partial view
8400 -- type D (..) is new P [(:::)] with null record; <-- full view
8401
8402 -- P's constraints on A's discriminants must statically match those
8403 -- imposed by (...).
8404
8405 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8406
8407 -- The full view of a private extension is handled exactly as described
8408 -- above. The model chose for the private view of a private extension is
8409 -- the same for what concerns discriminants (i.e. they receive the same
8410 -- treatment as in the tagged case). However, the private view of the
8411 -- private extension always inherits the components of the parent base,
8412 -- without replacing any discriminant reference. Strictly speaking this is
8413 -- incorrect. However, Gigi never uses this view to generate code so this
8414 -- is a purely semantic issue. In theory, a set of transformations similar
8415 -- to those given in 5. and 6. above could be applied to private views of
8416 -- private extensions to have the same model of component inheritance as
8417 -- for non private extensions. However, this is not done because it would
8418 -- further complicate private type processing. Semantically speaking, this
8419 -- leaves us in an uncomfortable situation. As an example consider:
8420
8421 -- package Pack is
8422 -- type R (D : integer) is tagged record
8423 -- S : String (1 .. D);
8424 -- end record;
8425 -- procedure P (X : R);
8426 -- type T is new R (1) with private;
8427 -- private
8428 -- type T is new R (1) with null record;
8429 -- end;
8430
8431 -- This is transformed into:
8432
8433 -- package Pack is
8434 -- type R (D : integer) is tagged record
8435 -- S : String (1 .. D);
8436 -- end record;
8437 -- procedure P (X : R);
8438 -- type T is new R (1) with private;
8439 -- private
8440 -- type BaseT is new R with null record;
8441 -- subtype T is BaseT (1);
8442 -- end;
8443
8444 -- (strictly speaking the above is incorrect Ada)
8445
8446 -- From the semantic standpoint the private view of private extension T
8447 -- should be flagged as constrained since one can clearly have
8448 --
8449 -- Obj : T;
8450 --
8451 -- in a unit withing Pack. However, when deriving subprograms for the
8452 -- private view of private extension T, T must be seen as unconstrained
8453 -- since T has discriminants (this is a constraint of the current
8454 -- subprogram derivation model). Thus, when processing the private view of
8455 -- a private extension such as T, we first mark T as unconstrained, we
8456 -- process it, we perform program derivation and just before returning from
8457 -- Build_Derived_Record_Type we mark T as constrained.
8458
8459 -- ??? Are there are other uncomfortable cases that we will have to
8460 -- deal with.
8461
8462 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8463
8464 -- Types that are derived from a visible record type and have a private
8465 -- extension present other peculiarities. They behave mostly like private
8466 -- types, but if they have primitive operations defined, these will not
8467 -- have the proper signatures for further inheritance, because other
8468 -- primitive operations will use the implicit base that we define for
8469 -- private derivations below. This affect subprogram inheritance (see
8470 -- Derive_Subprograms for details). We also derive the implicit base from
8471 -- the base type of the full view, so that the implicit base is a record
8472 -- type and not another private type, This avoids infinite loops.
8473
8474 procedure Build_Derived_Record_Type
8475 (N : Node_Id;
8476 Parent_Type : Entity_Id;
8477 Derived_Type : Entity_Id;
8478 Derive_Subps : Boolean := True)
8479 is
8480 Discriminant_Specs : constant Boolean :=
8481 Present (Discriminant_Specifications (N));
8482 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8483 Loc : constant Source_Ptr := Sloc (N);
8484 Private_Extension : constant Boolean :=
8485 Nkind (N) = N_Private_Extension_Declaration;
8486 Assoc_List : Elist_Id;
8487 Constraint_Present : Boolean;
8488 Constrs : Elist_Id;
8489 Discrim : Entity_Id;
8490 Indic : Node_Id;
8491 Inherit_Discrims : Boolean := False;
8492 Last_Discrim : Entity_Id;
8493 New_Base : Entity_Id;
8494 New_Decl : Node_Id;
8495 New_Discrs : Elist_Id;
8496 New_Indic : Node_Id;
8497 Parent_Base : Entity_Id;
8498 Save_Etype : Entity_Id;
8499 Save_Discr_Constr : Elist_Id;
8500 Save_Next_Entity : Entity_Id;
8501 Type_Def : Node_Id;
8502
8503 Discs : Elist_Id := New_Elmt_List;
8504 -- An empty Discs list means that there were no constraints in the
8505 -- subtype indication or that there was an error processing it.
8506
8507 begin
8508 if Ekind (Parent_Type) = E_Record_Type_With_Private
8509 and then Present (Full_View (Parent_Type))
8510 and then Has_Discriminants (Parent_Type)
8511 then
8512 Parent_Base := Base_Type (Full_View (Parent_Type));
8513 else
8514 Parent_Base := Base_Type (Parent_Type);
8515 end if;
8516
8517 -- AI05-0115 : if this is a derivation from a private type in some
8518 -- other scope that may lead to invisible components for the derived
8519 -- type, mark it accordingly.
8520
8521 if Is_Private_Type (Parent_Type) then
8522 if Scope (Parent_Type) = Scope (Derived_Type) then
8523 null;
8524
8525 elsif In_Open_Scopes (Scope (Parent_Type))
8526 and then In_Private_Part (Scope (Parent_Type))
8527 then
8528 null;
8529
8530 else
8531 Set_Has_Private_Ancestor (Derived_Type);
8532 end if;
8533
8534 else
8535 Set_Has_Private_Ancestor
8536 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8537 end if;
8538
8539 -- Before we start the previously documented transformations, here is
8540 -- little fix for size and alignment of tagged types. Normally when we
8541 -- derive type D from type P, we copy the size and alignment of P as the
8542 -- default for D, and in the absence of explicit representation clauses
8543 -- for D, the size and alignment are indeed the same as the parent.
8544
8545 -- But this is wrong for tagged types, since fields may be added, and
8546 -- the default size may need to be larger, and the default alignment may
8547 -- need to be larger.
8548
8549 -- We therefore reset the size and alignment fields in the tagged case.
8550 -- Note that the size and alignment will in any case be at least as
8551 -- large as the parent type (since the derived type has a copy of the
8552 -- parent type in the _parent field)
8553
8554 -- The type is also marked as being tagged here, which is needed when
8555 -- processing components with a self-referential anonymous access type
8556 -- in the call to Check_Anonymous_Access_Components below. Note that
8557 -- this flag is also set later on for completeness.
8558
8559 if Is_Tagged then
8560 Set_Is_Tagged_Type (Derived_Type);
8561 Init_Size_Align (Derived_Type);
8562 end if;
8563
8564 -- STEP 0a: figure out what kind of derived type declaration we have
8565
8566 if Private_Extension then
8567 Type_Def := N;
8568 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8569 Set_Default_SSO (Derived_Type);
8570 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8571
8572 else
8573 Type_Def := Type_Definition (N);
8574
8575 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8576 -- Parent_Base can be a private type or private extension. However,
8577 -- for tagged types with an extension the newly added fields are
8578 -- visible and hence the Derived_Type is always an E_Record_Type.
8579 -- (except that the parent may have its own private fields).
8580 -- For untagged types we preserve the Ekind of the Parent_Base.
8581
8582 if Present (Record_Extension_Part (Type_Def)) then
8583 Set_Ekind (Derived_Type, E_Record_Type);
8584 Set_Default_SSO (Derived_Type);
8585 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8586
8587 -- Create internal access types for components with anonymous
8588 -- access types.
8589
8590 if Ada_Version >= Ada_2005 then
8591 Check_Anonymous_Access_Components
8592 (N, Derived_Type, Derived_Type,
8593 Component_List (Record_Extension_Part (Type_Def)));
8594 end if;
8595
8596 else
8597 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8598 end if;
8599 end if;
8600
8601 -- Indic can either be an N_Identifier if the subtype indication
8602 -- contains no constraint or an N_Subtype_Indication if the subtype
8603 -- indication has a constraint.
8604
8605 Indic := Subtype_Indication (Type_Def);
8606 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8607
8608 -- Check that the type has visible discriminants. The type may be
8609 -- a private type with unknown discriminants whose full view has
8610 -- discriminants which are invisible.
8611
8612 if Constraint_Present then
8613 if not Has_Discriminants (Parent_Base)
8614 or else
8615 (Has_Unknown_Discriminants (Parent_Base)
8616 and then Is_Private_Type (Parent_Base))
8617 then
8618 Error_Msg_N
8619 ("invalid constraint: type has no discriminant",
8620 Constraint (Indic));
8621
8622 Constraint_Present := False;
8623 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8624
8625 elsif Is_Constrained (Parent_Type) then
8626 Error_Msg_N
8627 ("invalid constraint: parent type is already constrained",
8628 Constraint (Indic));
8629
8630 Constraint_Present := False;
8631 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8632 end if;
8633 end if;
8634
8635 -- STEP 0b: If needed, apply transformation given in point 5. above
8636
8637 if not Private_Extension
8638 and then Has_Discriminants (Parent_Type)
8639 and then not Discriminant_Specs
8640 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8641 then
8642 -- First, we must analyze the constraint (see comment in point 5.)
8643 -- The constraint may come from the subtype indication of the full
8644 -- declaration.
8645
8646 if Constraint_Present then
8647 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8648
8649 -- If there is no explicit constraint, there might be one that is
8650 -- inherited from a constrained parent type. In that case verify that
8651 -- it conforms to the constraint in the partial view. In perverse
8652 -- cases the parent subtypes of the partial and full view can have
8653 -- different constraints.
8654
8655 elsif Present (Stored_Constraint (Parent_Type)) then
8656 New_Discrs := Stored_Constraint (Parent_Type);
8657
8658 else
8659 New_Discrs := No_Elist;
8660 end if;
8661
8662 if Has_Discriminants (Derived_Type)
8663 and then Has_Private_Declaration (Derived_Type)
8664 and then Present (Discriminant_Constraint (Derived_Type))
8665 and then Present (New_Discrs)
8666 then
8667 -- Verify that constraints of the full view statically match
8668 -- those given in the partial view.
8669
8670 declare
8671 C1, C2 : Elmt_Id;
8672
8673 begin
8674 C1 := First_Elmt (New_Discrs);
8675 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8676 while Present (C1) and then Present (C2) loop
8677 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8678 or else
8679 (Is_OK_Static_Expression (Node (C1))
8680 and then Is_OK_Static_Expression (Node (C2))
8681 and then
8682 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8683 then
8684 null;
8685
8686 else
8687 if Constraint_Present then
8688 Error_Msg_N
8689 ("constraint not conformant to previous declaration",
8690 Node (C1));
8691 else
8692 Error_Msg_N
8693 ("constraint of full view is incompatible "
8694 & "with partial view", N);
8695 end if;
8696 end if;
8697
8698 Next_Elmt (C1);
8699 Next_Elmt (C2);
8700 end loop;
8701 end;
8702 end if;
8703
8704 -- Insert and analyze the declaration for the unconstrained base type
8705
8706 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8707
8708 New_Decl :=
8709 Make_Full_Type_Declaration (Loc,
8710 Defining_Identifier => New_Base,
8711 Type_Definition =>
8712 Make_Derived_Type_Definition (Loc,
8713 Abstract_Present => Abstract_Present (Type_Def),
8714 Limited_Present => Limited_Present (Type_Def),
8715 Subtype_Indication =>
8716 New_Occurrence_Of (Parent_Base, Loc),
8717 Record_Extension_Part =>
8718 Relocate_Node (Record_Extension_Part (Type_Def)),
8719 Interface_List => Interface_List (Type_Def)));
8720
8721 Set_Parent (New_Decl, Parent (N));
8722 Mark_Rewrite_Insertion (New_Decl);
8723 Insert_Before (N, New_Decl);
8724
8725 -- In the extension case, make sure ancestor is frozen appropriately
8726 -- (see also non-discriminated case below).
8727
8728 if Present (Record_Extension_Part (Type_Def))
8729 or else Is_Interface (Parent_Base)
8730 then
8731 Freeze_Before (New_Decl, Parent_Type);
8732 end if;
8733
8734 -- Note that this call passes False for the Derive_Subps parameter
8735 -- because subprogram derivation is deferred until after creating
8736 -- the subtype (see below).
8737
8738 Build_Derived_Type
8739 (New_Decl, Parent_Base, New_Base,
8740 Is_Completion => False, Derive_Subps => False);
8741
8742 -- ??? This needs re-examination to determine whether the
8743 -- above call can simply be replaced by a call to Analyze.
8744
8745 Set_Analyzed (New_Decl);
8746
8747 -- Insert and analyze the declaration for the constrained subtype
8748
8749 if Constraint_Present then
8750 New_Indic :=
8751 Make_Subtype_Indication (Loc,
8752 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8753 Constraint => Relocate_Node (Constraint (Indic)));
8754
8755 else
8756 declare
8757 Constr_List : constant List_Id := New_List;
8758 C : Elmt_Id;
8759 Expr : Node_Id;
8760
8761 begin
8762 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8763 while Present (C) loop
8764 Expr := Node (C);
8765
8766 -- It is safe here to call New_Copy_Tree since we called
8767 -- Force_Evaluation on each constraint previously
8768 -- in Build_Discriminant_Constraints.
8769
8770 Append (New_Copy_Tree (Expr), To => Constr_List);
8771
8772 Next_Elmt (C);
8773 end loop;
8774
8775 New_Indic :=
8776 Make_Subtype_Indication (Loc,
8777 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8778 Constraint =>
8779 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8780 end;
8781 end if;
8782
8783 Rewrite (N,
8784 Make_Subtype_Declaration (Loc,
8785 Defining_Identifier => Derived_Type,
8786 Subtype_Indication => New_Indic));
8787
8788 Analyze (N);
8789
8790 -- Derivation of subprograms must be delayed until the full subtype
8791 -- has been established, to ensure proper overriding of subprograms
8792 -- inherited by full types. If the derivations occurred as part of
8793 -- the call to Build_Derived_Type above, then the check for type
8794 -- conformance would fail because earlier primitive subprograms
8795 -- could still refer to the full type prior the change to the new
8796 -- subtype and hence would not match the new base type created here.
8797 -- Subprograms are not derived, however, when Derive_Subps is False
8798 -- (since otherwise there could be redundant derivations).
8799
8800 if Derive_Subps then
8801 Derive_Subprograms (Parent_Type, Derived_Type);
8802 end if;
8803
8804 -- For tagged types the Discriminant_Constraint of the new base itype
8805 -- is inherited from the first subtype so that no subtype conformance
8806 -- problem arise when the first subtype overrides primitive
8807 -- operations inherited by the implicit base type.
8808
8809 if Is_Tagged then
8810 Set_Discriminant_Constraint
8811 (New_Base, Discriminant_Constraint (Derived_Type));
8812 end if;
8813
8814 return;
8815 end if;
8816
8817 -- If we get here Derived_Type will have no discriminants or it will be
8818 -- a discriminated unconstrained base type.
8819
8820 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8821
8822 if Is_Tagged then
8823
8824 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8825 -- The declaration of a specific descendant of an interface type
8826 -- freezes the interface type (RM 13.14).
8827
8828 if not Private_Extension or else Is_Interface (Parent_Base) then
8829 Freeze_Before (N, Parent_Type);
8830 end if;
8831
8832 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8833 -- cannot be declared at a deeper level than its parent type is
8834 -- removed. The check on derivation within a generic body is also
8835 -- relaxed, but there's a restriction that a derived tagged type
8836 -- cannot be declared in a generic body if it's derived directly
8837 -- or indirectly from a formal type of that generic.
8838
8839 if Ada_Version >= Ada_2005 then
8840 if Present (Enclosing_Generic_Body (Derived_Type)) then
8841 declare
8842 Ancestor_Type : Entity_Id;
8843
8844 begin
8845 -- Check to see if any ancestor of the derived type is a
8846 -- formal type.
8847
8848 Ancestor_Type := Parent_Type;
8849 while not Is_Generic_Type (Ancestor_Type)
8850 and then Etype (Ancestor_Type) /= Ancestor_Type
8851 loop
8852 Ancestor_Type := Etype (Ancestor_Type);
8853 end loop;
8854
8855 -- If the derived type does have a formal type as an
8856 -- ancestor, then it's an error if the derived type is
8857 -- declared within the body of the generic unit that
8858 -- declares the formal type in its generic formal part. It's
8859 -- sufficient to check whether the ancestor type is declared
8860 -- inside the same generic body as the derived type (such as
8861 -- within a nested generic spec), in which case the
8862 -- derivation is legal. If the formal type is declared
8863 -- outside of that generic body, then it's guaranteed that
8864 -- the derived type is declared within the generic body of
8865 -- the generic unit declaring the formal type.
8866
8867 if Is_Generic_Type (Ancestor_Type)
8868 and then Enclosing_Generic_Body (Ancestor_Type) /=
8869 Enclosing_Generic_Body (Derived_Type)
8870 then
8871 Error_Msg_NE
8872 ("parent type of& must not be descendant of formal type"
8873 & " of an enclosing generic body",
8874 Indic, Derived_Type);
8875 end if;
8876 end;
8877 end if;
8878
8879 elsif Type_Access_Level (Derived_Type) /=
8880 Type_Access_Level (Parent_Type)
8881 and then not Is_Generic_Type (Derived_Type)
8882 then
8883 if Is_Controlled (Parent_Type) then
8884 Error_Msg_N
8885 ("controlled type must be declared at the library level",
8886 Indic);
8887 else
8888 Error_Msg_N
8889 ("type extension at deeper accessibility level than parent",
8890 Indic);
8891 end if;
8892
8893 else
8894 declare
8895 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8896 begin
8897 if Present (GB)
8898 and then GB /= Enclosing_Generic_Body (Parent_Base)
8899 then
8900 Error_Msg_NE
8901 ("parent type of& must not be outside generic body"
8902 & " (RM 3.9.1(4))",
8903 Indic, Derived_Type);
8904 end if;
8905 end;
8906 end if;
8907 end if;
8908
8909 -- Ada 2005 (AI-251)
8910
8911 if Ada_Version >= Ada_2005 and then Is_Tagged then
8912
8913 -- "The declaration of a specific descendant of an interface type
8914 -- freezes the interface type" (RM 13.14).
8915
8916 declare
8917 Iface : Node_Id;
8918 begin
8919 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8920 Iface := First (Interface_List (Type_Def));
8921 while Present (Iface) loop
8922 Freeze_Before (N, Etype (Iface));
8923 Next (Iface);
8924 end loop;
8925 end if;
8926 end;
8927 end if;
8928
8929 -- STEP 1b : preliminary cleanup of the full view of private types
8930
8931 -- If the type is already marked as having discriminants, then it's the
8932 -- completion of a private type or private extension and we need to
8933 -- retain the discriminants from the partial view if the current
8934 -- declaration has Discriminant_Specifications so that we can verify
8935 -- conformance. However, we must remove any existing components that
8936 -- were inherited from the parent (and attached in Copy_And_Swap)
8937 -- because the full type inherits all appropriate components anyway, and
8938 -- we do not want the partial view's components interfering.
8939
8940 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8941 Discrim := First_Discriminant (Derived_Type);
8942 loop
8943 Last_Discrim := Discrim;
8944 Next_Discriminant (Discrim);
8945 exit when No (Discrim);
8946 end loop;
8947
8948 Set_Last_Entity (Derived_Type, Last_Discrim);
8949
8950 -- In all other cases wipe out the list of inherited components (even
8951 -- inherited discriminants), it will be properly rebuilt here.
8952
8953 else
8954 Set_First_Entity (Derived_Type, Empty);
8955 Set_Last_Entity (Derived_Type, Empty);
8956 end if;
8957
8958 -- STEP 1c: Initialize some flags for the Derived_Type
8959
8960 -- The following flags must be initialized here so that
8961 -- Process_Discriminants can check that discriminants of tagged types do
8962 -- not have a default initial value and that access discriminants are
8963 -- only specified for limited records. For completeness, these flags are
8964 -- also initialized along with all the other flags below.
8965
8966 -- AI-419: Limitedness is not inherited from an interface parent, so to
8967 -- be limited in that case the type must be explicitly declared as
8968 -- limited. However, task and protected interfaces are always limited.
8969
8970 if Limited_Present (Type_Def) then
8971 Set_Is_Limited_Record (Derived_Type);
8972
8973 elsif Is_Limited_Record (Parent_Type)
8974 or else (Present (Full_View (Parent_Type))
8975 and then Is_Limited_Record (Full_View (Parent_Type)))
8976 then
8977 if not Is_Interface (Parent_Type)
8978 or else Is_Synchronized_Interface (Parent_Type)
8979 or else Is_Protected_Interface (Parent_Type)
8980 or else Is_Task_Interface (Parent_Type)
8981 then
8982 Set_Is_Limited_Record (Derived_Type);
8983 end if;
8984 end if;
8985
8986 -- STEP 2a: process discriminants of derived type if any
8987
8988 Push_Scope (Derived_Type);
8989
8990 if Discriminant_Specs then
8991 Set_Has_Unknown_Discriminants (Derived_Type, False);
8992
8993 -- The following call initializes fields Has_Discriminants and
8994 -- Discriminant_Constraint, unless we are processing the completion
8995 -- of a private type declaration.
8996
8997 Check_Or_Process_Discriminants (N, Derived_Type);
8998
8999 -- For untagged types, the constraint on the Parent_Type must be
9000 -- present and is used to rename the discriminants.
9001
9002 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9003 Error_Msg_N ("untagged parent must have discriminants", Indic);
9004
9005 elsif not Is_Tagged and then not Constraint_Present then
9006 Error_Msg_N
9007 ("discriminant constraint needed for derived untagged records",
9008 Indic);
9009
9010 -- Otherwise the parent subtype must be constrained unless we have a
9011 -- private extension.
9012
9013 elsif not Constraint_Present
9014 and then not Private_Extension
9015 and then not Is_Constrained (Parent_Type)
9016 then
9017 Error_Msg_N
9018 ("unconstrained type not allowed in this context", Indic);
9019
9020 elsif Constraint_Present then
9021 -- The following call sets the field Corresponding_Discriminant
9022 -- for the discriminants in the Derived_Type.
9023
9024 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9025
9026 -- For untagged types all new discriminants must rename
9027 -- discriminants in the parent. For private extensions new
9028 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9029
9030 Discrim := First_Discriminant (Derived_Type);
9031 while Present (Discrim) loop
9032 if not Is_Tagged
9033 and then No (Corresponding_Discriminant (Discrim))
9034 then
9035 Error_Msg_N
9036 ("new discriminants must constrain old ones", Discrim);
9037
9038 elsif Private_Extension
9039 and then Present (Corresponding_Discriminant (Discrim))
9040 then
9041 Error_Msg_N
9042 ("only static constraints allowed for parent"
9043 & " discriminants in the partial view", Indic);
9044 exit;
9045 end if;
9046
9047 -- If a new discriminant is used in the constraint, then its
9048 -- subtype must be statically compatible with the parent
9049 -- discriminant's subtype (3.7(15)).
9050
9051 -- However, if the record contains an array constrained by
9052 -- the discriminant but with some different bound, the compiler
9053 -- tries to create a smaller range for the discriminant type.
9054 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9055 -- the discriminant type is a scalar type, the check must use
9056 -- the original discriminant type in the parent declaration.
9057
9058 declare
9059 Corr_Disc : constant Entity_Id :=
9060 Corresponding_Discriminant (Discrim);
9061 Disc_Type : constant Entity_Id := Etype (Discrim);
9062 Corr_Type : Entity_Id;
9063
9064 begin
9065 if Present (Corr_Disc) then
9066 if Is_Scalar_Type (Disc_Type) then
9067 Corr_Type :=
9068 Entity (Discriminant_Type (Parent (Corr_Disc)));
9069 else
9070 Corr_Type := Etype (Corr_Disc);
9071 end if;
9072
9073 if not
9074 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9075 then
9076 Error_Msg_N
9077 ("subtype must be compatible "
9078 & "with parent discriminant",
9079 Discrim);
9080 end if;
9081 end if;
9082 end;
9083
9084 Next_Discriminant (Discrim);
9085 end loop;
9086
9087 -- Check whether the constraints of the full view statically
9088 -- match those imposed by the parent subtype [7.3(13)].
9089
9090 if Present (Stored_Constraint (Derived_Type)) then
9091 declare
9092 C1, C2 : Elmt_Id;
9093
9094 begin
9095 C1 := First_Elmt (Discs);
9096 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9097 while Present (C1) and then Present (C2) loop
9098 if not
9099 Fully_Conformant_Expressions (Node (C1), Node (C2))
9100 then
9101 Error_Msg_N
9102 ("not conformant with previous declaration",
9103 Node (C1));
9104 end if;
9105
9106 Next_Elmt (C1);
9107 Next_Elmt (C2);
9108 end loop;
9109 end;
9110 end if;
9111 end if;
9112
9113 -- STEP 2b: No new discriminants, inherit discriminants if any
9114
9115 else
9116 if Private_Extension then
9117 Set_Has_Unknown_Discriminants
9118 (Derived_Type,
9119 Has_Unknown_Discriminants (Parent_Type)
9120 or else Unknown_Discriminants_Present (N));
9121
9122 -- The partial view of the parent may have unknown discriminants,
9123 -- but if the full view has discriminants and the parent type is
9124 -- in scope they must be inherited.
9125
9126 elsif Has_Unknown_Discriminants (Parent_Type)
9127 and then
9128 (not Has_Discriminants (Parent_Type)
9129 or else not In_Open_Scopes (Scope (Parent_Type)))
9130 then
9131 Set_Has_Unknown_Discriminants (Derived_Type);
9132 end if;
9133
9134 if not Has_Unknown_Discriminants (Derived_Type)
9135 and then not Has_Unknown_Discriminants (Parent_Base)
9136 and then Has_Discriminants (Parent_Type)
9137 then
9138 Inherit_Discrims := True;
9139 Set_Has_Discriminants
9140 (Derived_Type, True);
9141 Set_Discriminant_Constraint
9142 (Derived_Type, Discriminant_Constraint (Parent_Base));
9143 end if;
9144
9145 -- The following test is true for private types (remember
9146 -- transformation 5. is not applied to those) and in an error
9147 -- situation.
9148
9149 if Constraint_Present then
9150 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9151 end if;
9152
9153 -- For now mark a new derived type as constrained only if it has no
9154 -- discriminants. At the end of Build_Derived_Record_Type we properly
9155 -- set this flag in the case of private extensions. See comments in
9156 -- point 9. just before body of Build_Derived_Record_Type.
9157
9158 Set_Is_Constrained
9159 (Derived_Type,
9160 not (Inherit_Discrims
9161 or else Has_Unknown_Discriminants (Derived_Type)));
9162 end if;
9163
9164 -- STEP 3: initialize fields of derived type
9165
9166 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9167 Set_Stored_Constraint (Derived_Type, No_Elist);
9168
9169 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9170 -- but cannot be interfaces
9171
9172 if not Private_Extension
9173 and then Ekind (Derived_Type) /= E_Private_Type
9174 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9175 then
9176 if Interface_Present (Type_Def) then
9177 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9178 end if;
9179
9180 Set_Interfaces (Derived_Type, No_Elist);
9181 end if;
9182
9183 -- Fields inherited from the Parent_Type
9184
9185 Set_Has_Specified_Layout
9186 (Derived_Type, Has_Specified_Layout (Parent_Type));
9187 Set_Is_Limited_Composite
9188 (Derived_Type, Is_Limited_Composite (Parent_Type));
9189 Set_Is_Private_Composite
9190 (Derived_Type, Is_Private_Composite (Parent_Type));
9191
9192 if Is_Tagged_Type (Parent_Type) then
9193 Set_No_Tagged_Streams_Pragma
9194 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9195 end if;
9196
9197 -- Fields inherited from the Parent_Base
9198
9199 Set_Has_Controlled_Component
9200 (Derived_Type, Has_Controlled_Component (Parent_Base));
9201 Set_Has_Non_Standard_Rep
9202 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9203 Set_Has_Primitive_Operations
9204 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9205
9206 -- Set fields for private derived types
9207
9208 if Is_Private_Type (Derived_Type) then
9209 Set_Depends_On_Private (Derived_Type, True);
9210 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9211 end if;
9212
9213 -- Inherit fields for non-private types. If this is the completion of a
9214 -- derivation from a private type, the parent itself is private and the
9215 -- attributes come from its full view, which must be present.
9216
9217 if Is_Record_Type (Derived_Type) then
9218 declare
9219 Parent_Full : Entity_Id;
9220
9221 begin
9222 if Is_Private_Type (Parent_Base)
9223 and then not Is_Record_Type (Parent_Base)
9224 then
9225 Parent_Full := Full_View (Parent_Base);
9226 else
9227 Parent_Full := Parent_Base;
9228 end if;
9229
9230 Set_Component_Alignment
9231 (Derived_Type, Component_Alignment (Parent_Full));
9232 Set_C_Pass_By_Copy
9233 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9234 Set_Has_Complex_Representation
9235 (Derived_Type, Has_Complex_Representation (Parent_Full));
9236
9237 -- For untagged types, inherit the layout by default to avoid
9238 -- costly changes of representation for type conversions.
9239
9240 if not Is_Tagged then
9241 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9242 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9243 end if;
9244 end;
9245 end if;
9246
9247 -- Set fields for tagged types
9248
9249 if Is_Tagged then
9250 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9251
9252 -- All tagged types defined in Ada.Finalization are controlled
9253
9254 if Chars (Scope (Derived_Type)) = Name_Finalization
9255 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9256 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9257 then
9258 Set_Is_Controlled_Active (Derived_Type);
9259 else
9260 Set_Is_Controlled_Active
9261 (Derived_Type, Is_Controlled_Active (Parent_Base));
9262 end if;
9263
9264 -- Minor optimization: there is no need to generate the class-wide
9265 -- entity associated with an underlying record view.
9266
9267 if not Is_Underlying_Record_View (Derived_Type) then
9268 Make_Class_Wide_Type (Derived_Type);
9269 end if;
9270
9271 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9272
9273 if Has_Discriminants (Derived_Type)
9274 and then Constraint_Present
9275 then
9276 Set_Stored_Constraint
9277 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9278 end if;
9279
9280 if Ada_Version >= Ada_2005 then
9281 declare
9282 Ifaces_List : Elist_Id;
9283
9284 begin
9285 -- Checks rules 3.9.4 (13/2 and 14/2)
9286
9287 if Comes_From_Source (Derived_Type)
9288 and then not Is_Private_Type (Derived_Type)
9289 and then Is_Interface (Parent_Type)
9290 and then not Is_Interface (Derived_Type)
9291 then
9292 if Is_Task_Interface (Parent_Type) then
9293 Error_Msg_N
9294 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9295 Derived_Type);
9296
9297 elsif Is_Protected_Interface (Parent_Type) then
9298 Error_Msg_N
9299 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9300 Derived_Type);
9301 end if;
9302 end if;
9303
9304 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9305
9306 Check_Interfaces (N, Type_Def);
9307
9308 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9309 -- not already in the parents.
9310
9311 Collect_Interfaces
9312 (T => Derived_Type,
9313 Ifaces_List => Ifaces_List,
9314 Exclude_Parents => True);
9315
9316 Set_Interfaces (Derived_Type, Ifaces_List);
9317
9318 -- If the derived type is the anonymous type created for
9319 -- a declaration whose parent has a constraint, propagate
9320 -- the interface list to the source type. This must be done
9321 -- prior to the completion of the analysis of the source type
9322 -- because the components in the extension may contain current
9323 -- instances whose legality depends on some ancestor.
9324
9325 if Is_Itype (Derived_Type) then
9326 declare
9327 Def : constant Node_Id :=
9328 Associated_Node_For_Itype (Derived_Type);
9329 begin
9330 if Present (Def)
9331 and then Nkind (Def) = N_Full_Type_Declaration
9332 then
9333 Set_Interfaces
9334 (Defining_Identifier (Def), Ifaces_List);
9335 end if;
9336 end;
9337 end if;
9338
9339 -- A type extension is automatically Ghost when one of its
9340 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9341 -- also inherited when the parent type is Ghost, but this is
9342 -- done in Build_Derived_Type as the mechanism also handles
9343 -- untagged derivations.
9344
9345 if Implements_Ghost_Interface (Derived_Type) then
9346 Set_Is_Ghost_Entity (Derived_Type);
9347 end if;
9348 end;
9349 end if;
9350 end if;
9351
9352 -- STEP 4: Inherit components from the parent base and constrain them.
9353 -- Apply the second transformation described in point 6. above.
9354
9355 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9356 or else not Has_Discriminants (Parent_Type)
9357 or else not Is_Constrained (Parent_Type)
9358 then
9359 Constrs := Discs;
9360 else
9361 Constrs := Discriminant_Constraint (Parent_Type);
9362 end if;
9363
9364 Assoc_List :=
9365 Inherit_Components
9366 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9367
9368 -- STEP 5a: Copy the parent record declaration for untagged types
9369
9370 Set_Has_Implicit_Dereference
9371 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9372
9373 if not Is_Tagged then
9374
9375 -- Discriminant_Constraint (Derived_Type) has been properly
9376 -- constructed. Save it and temporarily set it to Empty because we
9377 -- do not want the call to New_Copy_Tree below to mess this list.
9378
9379 if Has_Discriminants (Derived_Type) then
9380 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9381 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9382 else
9383 Save_Discr_Constr := No_Elist;
9384 end if;
9385
9386 -- Save the Etype field of Derived_Type. It is correctly set now,
9387 -- but the call to New_Copy tree may remap it to point to itself,
9388 -- which is not what we want. Ditto for the Next_Entity field.
9389
9390 Save_Etype := Etype (Derived_Type);
9391 Save_Next_Entity := Next_Entity (Derived_Type);
9392
9393 -- Assoc_List maps all stored discriminants in the Parent_Base to
9394 -- stored discriminants in the Derived_Type. It is fundamental that
9395 -- no types or itypes with discriminants other than the stored
9396 -- discriminants appear in the entities declared inside
9397 -- Derived_Type, since the back end cannot deal with it.
9398
9399 New_Decl :=
9400 New_Copy_Tree
9401 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9402 Copy_Dimensions_Of_Components (Derived_Type);
9403
9404 -- Restore the fields saved prior to the New_Copy_Tree call
9405 -- and compute the stored constraint.
9406
9407 Set_Etype (Derived_Type, Save_Etype);
9408 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9409
9410 if Has_Discriminants (Derived_Type) then
9411 Set_Discriminant_Constraint
9412 (Derived_Type, Save_Discr_Constr);
9413 Set_Stored_Constraint
9414 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9415 Replace_Components (Derived_Type, New_Decl);
9416 end if;
9417
9418 -- Insert the new derived type declaration
9419
9420 Rewrite (N, New_Decl);
9421
9422 -- STEP 5b: Complete the processing for record extensions in generics
9423
9424 -- There is no completion for record extensions declared in the
9425 -- parameter part of a generic, so we need to complete processing for
9426 -- these generic record extensions here. The Record_Type_Definition call
9427 -- will change the Ekind of the components from E_Void to E_Component.
9428
9429 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9430 Record_Type_Definition (Empty, Derived_Type);
9431
9432 -- STEP 5c: Process the record extension for non private tagged types
9433
9434 elsif not Private_Extension then
9435 Expand_Record_Extension (Derived_Type, Type_Def);
9436
9437 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9438 -- derived type to propagate some semantic information. This led
9439 -- to other ASIS failures and has been removed.
9440
9441 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9442 -- implemented interfaces if we are in expansion mode
9443
9444 if Expander_Active
9445 and then Has_Interfaces (Derived_Type)
9446 then
9447 Add_Interface_Tag_Components (N, Derived_Type);
9448 end if;
9449
9450 -- Analyze the record extension
9451
9452 Record_Type_Definition
9453 (Record_Extension_Part (Type_Def), Derived_Type);
9454 end if;
9455
9456 End_Scope;
9457
9458 -- Nothing else to do if there is an error in the derivation.
9459 -- An unusual case: the full view may be derived from a type in an
9460 -- instance, when the partial view was used illegally as an actual
9461 -- in that instance, leading to a circular definition.
9462
9463 if Etype (Derived_Type) = Any_Type
9464 or else Etype (Parent_Type) = Derived_Type
9465 then
9466 return;
9467 end if;
9468
9469 -- Set delayed freeze and then derive subprograms, we need to do
9470 -- this in this order so that derived subprograms inherit the
9471 -- derived freeze if necessary.
9472
9473 Set_Has_Delayed_Freeze (Derived_Type);
9474
9475 if Derive_Subps then
9476 Derive_Subprograms (Parent_Type, Derived_Type);
9477 end if;
9478
9479 -- If we have a private extension which defines a constrained derived
9480 -- type mark as constrained here after we have derived subprograms. See
9481 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9482
9483 if Private_Extension and then Inherit_Discrims then
9484 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9485 Set_Is_Constrained (Derived_Type, True);
9486 Set_Discriminant_Constraint (Derived_Type, Discs);
9487
9488 elsif Is_Constrained (Parent_Type) then
9489 Set_Is_Constrained
9490 (Derived_Type, True);
9491 Set_Discriminant_Constraint
9492 (Derived_Type, Discriminant_Constraint (Parent_Type));
9493 end if;
9494 end if;
9495
9496 -- Update the class-wide type, which shares the now-completed entity
9497 -- list with its specific type. In case of underlying record views,
9498 -- we do not generate the corresponding class wide entity.
9499
9500 if Is_Tagged
9501 and then not Is_Underlying_Record_View (Derived_Type)
9502 then
9503 Set_First_Entity
9504 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9505 Set_Last_Entity
9506 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9507 end if;
9508
9509 Check_Function_Writable_Actuals (N);
9510 end Build_Derived_Record_Type;
9511
9512 ------------------------
9513 -- Build_Derived_Type --
9514 ------------------------
9515
9516 procedure Build_Derived_Type
9517 (N : Node_Id;
9518 Parent_Type : Entity_Id;
9519 Derived_Type : Entity_Id;
9520 Is_Completion : Boolean;
9521 Derive_Subps : Boolean := True)
9522 is
9523 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9524
9525 begin
9526 -- Set common attributes
9527
9528 Set_Scope (Derived_Type, Current_Scope);
9529 Set_Etype (Derived_Type, Parent_Base);
9530 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9531 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9532
9533 Set_Size_Info (Derived_Type, Parent_Type);
9534 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9535
9536 Set_Is_Controlled_Active
9537 (Derived_Type, Is_Controlled_Active (Parent_Type));
9538
9539 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9540 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9541 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9542
9543 if Is_Tagged_Type (Derived_Type) then
9544 Set_No_Tagged_Streams_Pragma
9545 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9546 end if;
9547
9548 -- If the parent has primitive routines, set the derived type link
9549
9550 if Has_Primitive_Operations (Parent_Type) then
9551 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9552 end if;
9553
9554 -- If the parent type is a private subtype, the convention on the base
9555 -- type may be set in the private part, and not propagated to the
9556 -- subtype until later, so we obtain the convention from the base type.
9557
9558 Set_Convention (Derived_Type, Convention (Parent_Base));
9559
9560 -- Set SSO default for record or array type
9561
9562 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9563 and then Is_Base_Type (Derived_Type)
9564 then
9565 Set_Default_SSO (Derived_Type);
9566 end if;
9567
9568 -- A derived type inherits the Default_Initial_Condition pragma coming
9569 -- from any parent type within the derivation chain.
9570
9571 if Has_DIC (Parent_Type) then
9572 Set_Has_Inherited_DIC (Derived_Type);
9573 end if;
9574
9575 -- A derived type inherits any class-wide invariants coming from a
9576 -- parent type or an interface. Note that the invariant procedure of
9577 -- the parent type should not be inherited because the derived type may
9578 -- define invariants of its own.
9579
9580 if not Is_Interface (Derived_Type) then
9581 if Has_Inherited_Invariants (Parent_Type)
9582 or else Has_Inheritable_Invariants (Parent_Type)
9583 then
9584 Set_Has_Inherited_Invariants (Derived_Type);
9585
9586 elsif Is_Concurrent_Type (Derived_Type)
9587 or else Is_Tagged_Type (Derived_Type)
9588 then
9589 declare
9590 Iface : Entity_Id;
9591 Ifaces : Elist_Id;
9592 Iface_Elmt : Elmt_Id;
9593
9594 begin
9595 Collect_Interfaces
9596 (T => Derived_Type,
9597 Ifaces_List => Ifaces,
9598 Exclude_Parents => True);
9599
9600 if Present (Ifaces) then
9601 Iface_Elmt := First_Elmt (Ifaces);
9602 while Present (Iface_Elmt) loop
9603 Iface := Node (Iface_Elmt);
9604
9605 if Has_Inheritable_Invariants (Iface) then
9606 Set_Has_Inherited_Invariants (Derived_Type);
9607 exit;
9608 end if;
9609
9610 Next_Elmt (Iface_Elmt);
9611 end loop;
9612 end if;
9613 end;
9614 end if;
9615 end if;
9616
9617 -- We similarly inherit predicates. Note that for scalar derived types
9618 -- the predicate is inherited from the first subtype, and not from its
9619 -- (anonymous) base type.
9620
9621 if Has_Predicates (Parent_Type)
9622 or else Has_Predicates (First_Subtype (Parent_Type))
9623 then
9624 Set_Has_Predicates (Derived_Type);
9625 end if;
9626
9627 -- The derived type inherits representation clauses from the parent
9628 -- type, and from any interfaces.
9629
9630 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9631
9632 declare
9633 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9634 begin
9635 while Present (Iface) loop
9636 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9637 Next (Iface);
9638 end loop;
9639 end;
9640
9641 -- If the parent type has delayed rep aspects, then mark the derived
9642 -- type as possibly inheriting a delayed rep aspect.
9643
9644 if Has_Delayed_Rep_Aspects (Parent_Type) then
9645 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9646 end if;
9647
9648 -- A derived type becomes Ghost when its parent type is also Ghost
9649 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9650 -- directly inherited because the Ghost policy in effect may differ.
9651
9652 if Is_Ghost_Entity (Parent_Type) then
9653 Set_Is_Ghost_Entity (Derived_Type);
9654 end if;
9655
9656 -- Type dependent processing
9657
9658 case Ekind (Parent_Type) is
9659 when Numeric_Kind =>
9660 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9661
9662 when Array_Kind =>
9663 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9664
9665 when Class_Wide_Kind
9666 | E_Record_Subtype
9667 | E_Record_Type
9668 =>
9669 Build_Derived_Record_Type
9670 (N, Parent_Type, Derived_Type, Derive_Subps);
9671 return;
9672
9673 when Enumeration_Kind =>
9674 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9675
9676 when Access_Kind =>
9677 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9678
9679 when Incomplete_Or_Private_Kind =>
9680 Build_Derived_Private_Type
9681 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9682
9683 -- For discriminated types, the derivation includes deriving
9684 -- primitive operations. For others it is done below.
9685
9686 if Is_Tagged_Type (Parent_Type)
9687 or else Has_Discriminants (Parent_Type)
9688 or else (Present (Full_View (Parent_Type))
9689 and then Has_Discriminants (Full_View (Parent_Type)))
9690 then
9691 return;
9692 end if;
9693
9694 when Concurrent_Kind =>
9695 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9696
9697 when others =>
9698 raise Program_Error;
9699 end case;
9700
9701 -- Nothing more to do if some error occurred
9702
9703 if Etype (Derived_Type) = Any_Type then
9704 return;
9705 end if;
9706
9707 -- Set delayed freeze and then derive subprograms, we need to do this
9708 -- in this order so that derived subprograms inherit the derived freeze
9709 -- if necessary.
9710
9711 Set_Has_Delayed_Freeze (Derived_Type);
9712
9713 if Derive_Subps then
9714 Derive_Subprograms (Parent_Type, Derived_Type);
9715 end if;
9716
9717 Set_Has_Primitive_Operations
9718 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9719 end Build_Derived_Type;
9720
9721 -----------------------
9722 -- Build_Discriminal --
9723 -----------------------
9724
9725 procedure Build_Discriminal (Discrim : Entity_Id) is
9726 D_Minal : Entity_Id;
9727 CR_Disc : Entity_Id;
9728
9729 begin
9730 -- A discriminal has the same name as the discriminant
9731
9732 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9733
9734 Set_Ekind (D_Minal, E_In_Parameter);
9735 Set_Mechanism (D_Minal, Default_Mechanism);
9736 Set_Etype (D_Minal, Etype (Discrim));
9737 Set_Scope (D_Minal, Current_Scope);
9738 Set_Parent (D_Minal, Parent (Discrim));
9739
9740 Set_Discriminal (Discrim, D_Minal);
9741 Set_Discriminal_Link (D_Minal, Discrim);
9742
9743 -- For task types, build at once the discriminants of the corresponding
9744 -- record, which are needed if discriminants are used in entry defaults
9745 -- and in family bounds.
9746
9747 if Is_Concurrent_Type (Current_Scope)
9748 or else
9749 Is_Limited_Type (Current_Scope)
9750 then
9751 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9752
9753 Set_Ekind (CR_Disc, E_In_Parameter);
9754 Set_Mechanism (CR_Disc, Default_Mechanism);
9755 Set_Etype (CR_Disc, Etype (Discrim));
9756 Set_Scope (CR_Disc, Current_Scope);
9757 Set_Discriminal_Link (CR_Disc, Discrim);
9758 Set_CR_Discriminant (Discrim, CR_Disc);
9759 end if;
9760 end Build_Discriminal;
9761
9762 ------------------------------------
9763 -- Build_Discriminant_Constraints --
9764 ------------------------------------
9765
9766 function Build_Discriminant_Constraints
9767 (T : Entity_Id;
9768 Def : Node_Id;
9769 Derived_Def : Boolean := False) return Elist_Id
9770 is
9771 C : constant Node_Id := Constraint (Def);
9772 Nb_Discr : constant Nat := Number_Discriminants (T);
9773
9774 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9775 -- Saves the expression corresponding to a given discriminant in T
9776
9777 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9778 -- Return the Position number within array Discr_Expr of a discriminant
9779 -- D within the discriminant list of the discriminated type T.
9780
9781 procedure Process_Discriminant_Expression
9782 (Expr : Node_Id;
9783 D : Entity_Id);
9784 -- If this is a discriminant constraint on a partial view, do not
9785 -- generate an overflow check on the discriminant expression. The check
9786 -- will be generated when constraining the full view. Otherwise the
9787 -- backend creates duplicate symbols for the temporaries corresponding
9788 -- to the expressions to be checked, causing spurious assembler errors.
9789
9790 ------------------
9791 -- Pos_Of_Discr --
9792 ------------------
9793
9794 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9795 Disc : Entity_Id;
9796
9797 begin
9798 Disc := First_Discriminant (T);
9799 for J in Discr_Expr'Range loop
9800 if Disc = D then
9801 return J;
9802 end if;
9803
9804 Next_Discriminant (Disc);
9805 end loop;
9806
9807 -- Note: Since this function is called on discriminants that are
9808 -- known to belong to the discriminated type, falling through the
9809 -- loop with no match signals an internal compiler error.
9810
9811 raise Program_Error;
9812 end Pos_Of_Discr;
9813
9814 -------------------------------------
9815 -- Process_Discriminant_Expression --
9816 -------------------------------------
9817
9818 procedure Process_Discriminant_Expression
9819 (Expr : Node_Id;
9820 D : Entity_Id)
9821 is
9822 BDT : constant Entity_Id := Base_Type (Etype (D));
9823
9824 begin
9825 -- If this is a discriminant constraint on a partial view, do
9826 -- not generate an overflow on the discriminant expression. The
9827 -- check will be generated when constraining the full view.
9828
9829 if Is_Private_Type (T)
9830 and then Present (Full_View (T))
9831 then
9832 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9833 else
9834 Analyze_And_Resolve (Expr, BDT);
9835 end if;
9836 end Process_Discriminant_Expression;
9837
9838 -- Declarations local to Build_Discriminant_Constraints
9839
9840 Discr : Entity_Id;
9841 E : Entity_Id;
9842 Elist : constant Elist_Id := New_Elmt_List;
9843
9844 Constr : Node_Id;
9845 Expr : Node_Id;
9846 Id : Node_Id;
9847 Position : Nat;
9848 Found : Boolean;
9849
9850 Discrim_Present : Boolean := False;
9851
9852 -- Start of processing for Build_Discriminant_Constraints
9853
9854 begin
9855 -- The following loop will process positional associations only.
9856 -- For a positional association, the (single) discriminant is
9857 -- implicitly specified by position, in textual order (RM 3.7.2).
9858
9859 Discr := First_Discriminant (T);
9860 Constr := First (Constraints (C));
9861 for D in Discr_Expr'Range loop
9862 exit when Nkind (Constr) = N_Discriminant_Association;
9863
9864 if No (Constr) then
9865 Error_Msg_N ("too few discriminants given in constraint", C);
9866 return New_Elmt_List;
9867
9868 elsif Nkind (Constr) = N_Range
9869 or else (Nkind (Constr) = N_Attribute_Reference
9870 and then Attribute_Name (Constr) = Name_Range)
9871 then
9872 Error_Msg_N
9873 ("a range is not a valid discriminant constraint", Constr);
9874 Discr_Expr (D) := Error;
9875
9876 else
9877 Process_Discriminant_Expression (Constr, Discr);
9878 Discr_Expr (D) := Constr;
9879 end if;
9880
9881 Next_Discriminant (Discr);
9882 Next (Constr);
9883 end loop;
9884
9885 if No (Discr) and then Present (Constr) then
9886 Error_Msg_N ("too many discriminants given in constraint", Constr);
9887 return New_Elmt_List;
9888 end if;
9889
9890 -- Named associations can be given in any order, but if both positional
9891 -- and named associations are used in the same discriminant constraint,
9892 -- then positional associations must occur first, at their normal
9893 -- position. Hence once a named association is used, the rest of the
9894 -- discriminant constraint must use only named associations.
9895
9896 while Present (Constr) loop
9897
9898 -- Positional association forbidden after a named association
9899
9900 if Nkind (Constr) /= N_Discriminant_Association then
9901 Error_Msg_N ("positional association follows named one", Constr);
9902 return New_Elmt_List;
9903
9904 -- Otherwise it is a named association
9905
9906 else
9907 -- E records the type of the discriminants in the named
9908 -- association. All the discriminants specified in the same name
9909 -- association must have the same type.
9910
9911 E := Empty;
9912
9913 -- Search the list of discriminants in T to see if the simple name
9914 -- given in the constraint matches any of them.
9915
9916 Id := First (Selector_Names (Constr));
9917 while Present (Id) loop
9918 Found := False;
9919
9920 -- If Original_Discriminant is present, we are processing a
9921 -- generic instantiation and this is an instance node. We need
9922 -- to find the name of the corresponding discriminant in the
9923 -- actual record type T and not the name of the discriminant in
9924 -- the generic formal. Example:
9925
9926 -- generic
9927 -- type G (D : int) is private;
9928 -- package P is
9929 -- subtype W is G (D => 1);
9930 -- end package;
9931 -- type Rec (X : int) is record ... end record;
9932 -- package Q is new P (G => Rec);
9933
9934 -- At the point of the instantiation, formal type G is Rec
9935 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9936 -- which really looks like "subtype W is Rec (D => 1);" at
9937 -- the point of instantiation, we want to find the discriminant
9938 -- that corresponds to D in Rec, i.e. X.
9939
9940 if Present (Original_Discriminant (Id))
9941 and then In_Instance
9942 then
9943 Discr := Find_Corresponding_Discriminant (Id, T);
9944 Found := True;
9945
9946 else
9947 Discr := First_Discriminant (T);
9948 while Present (Discr) loop
9949 if Chars (Discr) = Chars (Id) then
9950 Found := True;
9951 exit;
9952 end if;
9953
9954 Next_Discriminant (Discr);
9955 end loop;
9956
9957 if not Found then
9958 Error_Msg_N ("& does not match any discriminant", Id);
9959 return New_Elmt_List;
9960
9961 -- If the parent type is a generic formal, preserve the
9962 -- name of the discriminant for subsequent instances.
9963 -- see comment at the beginning of this if statement.
9964
9965 elsif Is_Generic_Type (Root_Type (T)) then
9966 Set_Original_Discriminant (Id, Discr);
9967 end if;
9968 end if;
9969
9970 Position := Pos_Of_Discr (T, Discr);
9971
9972 if Present (Discr_Expr (Position)) then
9973 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9974
9975 else
9976 -- Each discriminant specified in the same named association
9977 -- must be associated with a separate copy of the
9978 -- corresponding expression.
9979
9980 if Present (Next (Id)) then
9981 Expr := New_Copy_Tree (Expression (Constr));
9982 Set_Parent (Expr, Parent (Expression (Constr)));
9983 else
9984 Expr := Expression (Constr);
9985 end if;
9986
9987 Discr_Expr (Position) := Expr;
9988 Process_Discriminant_Expression (Expr, Discr);
9989 end if;
9990
9991 -- A discriminant association with more than one discriminant
9992 -- name is only allowed if the named discriminants are all of
9993 -- the same type (RM 3.7.1(8)).
9994
9995 if E = Empty then
9996 E := Base_Type (Etype (Discr));
9997
9998 elsif Base_Type (Etype (Discr)) /= E then
9999 Error_Msg_N
10000 ("all discriminants in an association " &
10001 "must have the same type", Id);
10002 end if;
10003
10004 Next (Id);
10005 end loop;
10006 end if;
10007
10008 Next (Constr);
10009 end loop;
10010
10011 -- A discriminant constraint must provide exactly one value for each
10012 -- discriminant of the type (RM 3.7.1(8)).
10013
10014 for J in Discr_Expr'Range loop
10015 if No (Discr_Expr (J)) then
10016 Error_Msg_N ("too few discriminants given in constraint", C);
10017 return New_Elmt_List;
10018 end if;
10019 end loop;
10020
10021 -- Determine if there are discriminant expressions in the constraint
10022
10023 for J in Discr_Expr'Range loop
10024 if Denotes_Discriminant
10025 (Discr_Expr (J), Check_Concurrent => True)
10026 then
10027 Discrim_Present := True;
10028 end if;
10029 end loop;
10030
10031 -- Build an element list consisting of the expressions given in the
10032 -- discriminant constraint and apply the appropriate checks. The list
10033 -- is constructed after resolving any named discriminant associations
10034 -- and therefore the expressions appear in the textual order of the
10035 -- discriminants.
10036
10037 Discr := First_Discriminant (T);
10038 for J in Discr_Expr'Range loop
10039 if Discr_Expr (J) /= Error then
10040 Append_Elmt (Discr_Expr (J), Elist);
10041
10042 -- If any of the discriminant constraints is given by a
10043 -- discriminant and we are in a derived type declaration we
10044 -- have a discriminant renaming. Establish link between new
10045 -- and old discriminant. The new discriminant has an implicit
10046 -- dereference if the old one does.
10047
10048 if Denotes_Discriminant (Discr_Expr (J)) then
10049 if Derived_Def then
10050 declare
10051 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10052
10053 begin
10054 Set_Corresponding_Discriminant (New_Discr, Discr);
10055 Set_Has_Implicit_Dereference (New_Discr,
10056 Has_Implicit_Dereference (Discr));
10057 end;
10058 end if;
10059
10060 -- Force the evaluation of non-discriminant expressions.
10061 -- If we have found a discriminant in the constraint 3.4(26)
10062 -- and 3.8(18) demand that no range checks are performed are
10063 -- after evaluation. If the constraint is for a component
10064 -- definition that has a per-object constraint, expressions are
10065 -- evaluated but not checked either. In all other cases perform
10066 -- a range check.
10067
10068 else
10069 if Discrim_Present then
10070 null;
10071
10072 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10073 and then Has_Per_Object_Constraint
10074 (Defining_Identifier (Parent (Parent (Def))))
10075 then
10076 null;
10077
10078 elsif Is_Access_Type (Etype (Discr)) then
10079 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10080
10081 else
10082 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10083 end if;
10084
10085 Force_Evaluation (Discr_Expr (J));
10086 end if;
10087
10088 -- Check that the designated type of an access discriminant's
10089 -- expression is not a class-wide type unless the discriminant's
10090 -- designated type is also class-wide.
10091
10092 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10093 and then not Is_Class_Wide_Type
10094 (Designated_Type (Etype (Discr)))
10095 and then Etype (Discr_Expr (J)) /= Any_Type
10096 and then Is_Class_Wide_Type
10097 (Designated_Type (Etype (Discr_Expr (J))))
10098 then
10099 Wrong_Type (Discr_Expr (J), Etype (Discr));
10100
10101 elsif Is_Access_Type (Etype (Discr))
10102 and then not Is_Access_Constant (Etype (Discr))
10103 and then Is_Access_Type (Etype (Discr_Expr (J)))
10104 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10105 then
10106 Error_Msg_NE
10107 ("constraint for discriminant& must be access to variable",
10108 Def, Discr);
10109 end if;
10110 end if;
10111
10112 Next_Discriminant (Discr);
10113 end loop;
10114
10115 return Elist;
10116 end Build_Discriminant_Constraints;
10117
10118 ---------------------------------
10119 -- Build_Discriminated_Subtype --
10120 ---------------------------------
10121
10122 procedure Build_Discriminated_Subtype
10123 (T : Entity_Id;
10124 Def_Id : Entity_Id;
10125 Elist : Elist_Id;
10126 Related_Nod : Node_Id;
10127 For_Access : Boolean := False)
10128 is
10129 Has_Discrs : constant Boolean := Has_Discriminants (T);
10130 Constrained : constant Boolean :=
10131 (Has_Discrs
10132 and then not Is_Empty_Elmt_List (Elist)
10133 and then not Is_Class_Wide_Type (T))
10134 or else Is_Constrained (T);
10135
10136 begin
10137 if Ekind (T) = E_Record_Type then
10138 if For_Access then
10139 Set_Ekind (Def_Id, E_Private_Subtype);
10140 Set_Is_For_Access_Subtype (Def_Id, True);
10141 else
10142 Set_Ekind (Def_Id, E_Record_Subtype);
10143 end if;
10144
10145 -- Inherit preelaboration flag from base, for types for which it
10146 -- may have been set: records, private types, protected types.
10147
10148 Set_Known_To_Have_Preelab_Init
10149 (Def_Id, Known_To_Have_Preelab_Init (T));
10150
10151 elsif Ekind (T) = E_Task_Type then
10152 Set_Ekind (Def_Id, E_Task_Subtype);
10153
10154 elsif Ekind (T) = E_Protected_Type then
10155 Set_Ekind (Def_Id, E_Protected_Subtype);
10156 Set_Known_To_Have_Preelab_Init
10157 (Def_Id, Known_To_Have_Preelab_Init (T));
10158
10159 elsif Is_Private_Type (T) then
10160 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10161 Set_Known_To_Have_Preelab_Init
10162 (Def_Id, Known_To_Have_Preelab_Init (T));
10163
10164 -- Private subtypes may have private dependents
10165
10166 Set_Private_Dependents (Def_Id, New_Elmt_List);
10167
10168 elsif Is_Class_Wide_Type (T) then
10169 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10170
10171 else
10172 -- Incomplete type. Attach subtype to list of dependents, to be
10173 -- completed with full view of parent type, unless is it the
10174 -- designated subtype of a record component within an init_proc.
10175 -- This last case arises for a component of an access type whose
10176 -- designated type is incomplete (e.g. a Taft Amendment type).
10177 -- The designated subtype is within an inner scope, and needs no
10178 -- elaboration, because only the access type is needed in the
10179 -- initialization procedure.
10180
10181 if Ekind (T) = E_Incomplete_Type then
10182 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10183 else
10184 Set_Ekind (Def_Id, Ekind (T));
10185 end if;
10186
10187 if For_Access and then Within_Init_Proc then
10188 null;
10189 else
10190 Append_Elmt (Def_Id, Private_Dependents (T));
10191 end if;
10192 end if;
10193
10194 Set_Etype (Def_Id, T);
10195 Init_Size_Align (Def_Id);
10196 Set_Has_Discriminants (Def_Id, Has_Discrs);
10197 Set_Is_Constrained (Def_Id, Constrained);
10198
10199 Set_First_Entity (Def_Id, First_Entity (T));
10200 Set_Last_Entity (Def_Id, Last_Entity (T));
10201 Set_Has_Implicit_Dereference
10202 (Def_Id, Has_Implicit_Dereference (T));
10203 Set_Has_Pragma_Unreferenced_Objects
10204 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10205
10206 -- If the subtype is the completion of a private declaration, there may
10207 -- have been representation clauses for the partial view, and they must
10208 -- be preserved. Build_Derived_Type chains the inherited clauses with
10209 -- the ones appearing on the extension. If this comes from a subtype
10210 -- declaration, all clauses are inherited.
10211
10212 if No (First_Rep_Item (Def_Id)) then
10213 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10214 end if;
10215
10216 if Is_Tagged_Type (T) then
10217 Set_Is_Tagged_Type (Def_Id);
10218 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10219 Make_Class_Wide_Type (Def_Id);
10220 end if;
10221
10222 Set_Stored_Constraint (Def_Id, No_Elist);
10223
10224 if Has_Discrs then
10225 Set_Discriminant_Constraint (Def_Id, Elist);
10226 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10227 end if;
10228
10229 if Is_Tagged_Type (T) then
10230
10231 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10232 -- concurrent record type (which has the list of primitive
10233 -- operations).
10234
10235 if Ada_Version >= Ada_2005
10236 and then Is_Concurrent_Type (T)
10237 then
10238 Set_Corresponding_Record_Type (Def_Id,
10239 Corresponding_Record_Type (T));
10240 else
10241 Set_Direct_Primitive_Operations (Def_Id,
10242 Direct_Primitive_Operations (T));
10243 end if;
10244
10245 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10246 end if;
10247
10248 -- Subtypes introduced by component declarations do not need to be
10249 -- marked as delayed, and do not get freeze nodes, because the semantics
10250 -- verifies that the parents of the subtypes are frozen before the
10251 -- enclosing record is frozen.
10252
10253 if not Is_Type (Scope (Def_Id)) then
10254 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10255
10256 if Is_Private_Type (T)
10257 and then Present (Full_View (T))
10258 then
10259 Conditional_Delay (Def_Id, Full_View (T));
10260 else
10261 Conditional_Delay (Def_Id, T);
10262 end if;
10263 end if;
10264
10265 if Is_Record_Type (T) then
10266 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10267
10268 if Has_Discrs
10269 and then not Is_Empty_Elmt_List (Elist)
10270 and then not For_Access
10271 then
10272 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10273
10274 elsif not For_Access then
10275 Set_Cloned_Subtype (Def_Id, T);
10276 end if;
10277 end if;
10278 end Build_Discriminated_Subtype;
10279
10280 ---------------------------
10281 -- Build_Itype_Reference --
10282 ---------------------------
10283
10284 procedure Build_Itype_Reference
10285 (Ityp : Entity_Id;
10286 Nod : Node_Id)
10287 is
10288 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10289 begin
10290
10291 -- Itype references are only created for use by the back-end
10292
10293 if Inside_A_Generic then
10294 return;
10295 else
10296 Set_Itype (IR, Ityp);
10297
10298 -- If Nod is a library unit entity, then Insert_After won't work,
10299 -- because Nod is not a member of any list. Therefore, we use
10300 -- Add_Global_Declaration in this case. This can happen if we have a
10301 -- build-in-place library function.
10302
10303 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10304 or else
10305 (Nkind (Nod) = N_Defining_Program_Unit_Name
10306 and then Is_Compilation_Unit (Defining_Identifier (Nod)))
10307 then
10308 Add_Global_Declaration (IR);
10309 else
10310 Insert_After (Nod, IR);
10311 end if;
10312 end if;
10313 end Build_Itype_Reference;
10314
10315 ------------------------
10316 -- Build_Scalar_Bound --
10317 ------------------------
10318
10319 function Build_Scalar_Bound
10320 (Bound : Node_Id;
10321 Par_T : Entity_Id;
10322 Der_T : Entity_Id) return Node_Id
10323 is
10324 New_Bound : Entity_Id;
10325
10326 begin
10327 -- Note: not clear why this is needed, how can the original bound
10328 -- be unanalyzed at this point? and if it is, what business do we
10329 -- have messing around with it? and why is the base type of the
10330 -- parent type the right type for the resolution. It probably is
10331 -- not. It is OK for the new bound we are creating, but not for
10332 -- the old one??? Still if it never happens, no problem.
10333
10334 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10335
10336 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10337 New_Bound := New_Copy (Bound);
10338 Set_Etype (New_Bound, Der_T);
10339 Set_Analyzed (New_Bound);
10340
10341 elsif Is_Entity_Name (Bound) then
10342 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10343
10344 -- The following is almost certainly wrong. What business do we have
10345 -- relocating a node (Bound) that is presumably still attached to
10346 -- the tree elsewhere???
10347
10348 else
10349 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10350 end if;
10351
10352 Set_Etype (New_Bound, Der_T);
10353 return New_Bound;
10354 end Build_Scalar_Bound;
10355
10356 --------------------------------
10357 -- Build_Underlying_Full_View --
10358 --------------------------------
10359
10360 procedure Build_Underlying_Full_View
10361 (N : Node_Id;
10362 Typ : Entity_Id;
10363 Par : Entity_Id)
10364 is
10365 Loc : constant Source_Ptr := Sloc (N);
10366 Subt : constant Entity_Id :=
10367 Make_Defining_Identifier
10368 (Loc, New_External_Name (Chars (Typ), 'S'));
10369
10370 Constr : Node_Id;
10371 Indic : Node_Id;
10372 C : Node_Id;
10373 Id : Node_Id;
10374
10375 procedure Set_Discriminant_Name (Id : Node_Id);
10376 -- If the derived type has discriminants, they may rename discriminants
10377 -- of the parent. When building the full view of the parent, we need to
10378 -- recover the names of the original discriminants if the constraint is
10379 -- given by named associations.
10380
10381 ---------------------------
10382 -- Set_Discriminant_Name --
10383 ---------------------------
10384
10385 procedure Set_Discriminant_Name (Id : Node_Id) is
10386 Disc : Entity_Id;
10387
10388 begin
10389 Set_Original_Discriminant (Id, Empty);
10390
10391 if Has_Discriminants (Typ) then
10392 Disc := First_Discriminant (Typ);
10393 while Present (Disc) loop
10394 if Chars (Disc) = Chars (Id)
10395 and then Present (Corresponding_Discriminant (Disc))
10396 then
10397 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10398 end if;
10399 Next_Discriminant (Disc);
10400 end loop;
10401 end if;
10402 end Set_Discriminant_Name;
10403
10404 -- Start of processing for Build_Underlying_Full_View
10405
10406 begin
10407 if Nkind (N) = N_Full_Type_Declaration then
10408 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10409
10410 elsif Nkind (N) = N_Subtype_Declaration then
10411 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10412
10413 elsif Nkind (N) = N_Component_Declaration then
10414 Constr :=
10415 New_Copy_Tree
10416 (Constraint (Subtype_Indication (Component_Definition (N))));
10417
10418 else
10419 raise Program_Error;
10420 end if;
10421
10422 C := First (Constraints (Constr));
10423 while Present (C) loop
10424 if Nkind (C) = N_Discriminant_Association then
10425 Id := First (Selector_Names (C));
10426 while Present (Id) loop
10427 Set_Discriminant_Name (Id);
10428 Next (Id);
10429 end loop;
10430 end if;
10431
10432 Next (C);
10433 end loop;
10434
10435 Indic :=
10436 Make_Subtype_Declaration (Loc,
10437 Defining_Identifier => Subt,
10438 Subtype_Indication =>
10439 Make_Subtype_Indication (Loc,
10440 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10441 Constraint => New_Copy_Tree (Constr)));
10442
10443 -- If this is a component subtype for an outer itype, it is not
10444 -- a list member, so simply set the parent link for analysis: if
10445 -- the enclosing type does not need to be in a declarative list,
10446 -- neither do the components.
10447
10448 if Is_List_Member (N)
10449 and then Nkind (N) /= N_Component_Declaration
10450 then
10451 Insert_Before (N, Indic);
10452 else
10453 Set_Parent (Indic, Parent (N));
10454 end if;
10455
10456 Analyze (Indic);
10457 Set_Underlying_Full_View (Typ, Full_View (Subt));
10458 Set_Is_Underlying_Full_View (Full_View (Subt));
10459 end Build_Underlying_Full_View;
10460
10461 -------------------------------
10462 -- Check_Abstract_Overriding --
10463 -------------------------------
10464
10465 procedure Check_Abstract_Overriding (T : Entity_Id) is
10466 Alias_Subp : Entity_Id;
10467 Elmt : Elmt_Id;
10468 Op_List : Elist_Id;
10469 Subp : Entity_Id;
10470 Type_Def : Node_Id;
10471
10472 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10473 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10474 -- which has pragma Implemented already set. Check whether Subp's entity
10475 -- kind conforms to the implementation kind of the overridden routine.
10476
10477 procedure Check_Pragma_Implemented
10478 (Subp : Entity_Id;
10479 Iface_Subp : Entity_Id);
10480 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10481 -- Iface_Subp and both entities have pragma Implemented already set on
10482 -- them. Check whether the two implementation kinds are conforming.
10483
10484 procedure Inherit_Pragma_Implemented
10485 (Subp : Entity_Id;
10486 Iface_Subp : Entity_Id);
10487 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10488 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10489 -- Propagate the implementation kind of Iface_Subp to Subp.
10490
10491 ------------------------------
10492 -- Check_Pragma_Implemented --
10493 ------------------------------
10494
10495 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10496 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10497 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10498 Subp_Alias : constant Entity_Id := Alias (Subp);
10499 Contr_Typ : Entity_Id;
10500 Impl_Subp : Entity_Id;
10501
10502 begin
10503 -- Subp must have an alias since it is a hidden entity used to link
10504 -- an interface subprogram to its overriding counterpart.
10505
10506 pragma Assert (Present (Subp_Alias));
10507
10508 -- Handle aliases to synchronized wrappers
10509
10510 Impl_Subp := Subp_Alias;
10511
10512 if Is_Primitive_Wrapper (Impl_Subp) then
10513 Impl_Subp := Wrapped_Entity (Impl_Subp);
10514 end if;
10515
10516 -- Extract the type of the controlling formal
10517
10518 Contr_Typ := Etype (First_Formal (Subp_Alias));
10519
10520 if Is_Concurrent_Record_Type (Contr_Typ) then
10521 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10522 end if;
10523
10524 -- An interface subprogram whose implementation kind is By_Entry must
10525 -- be implemented by an entry.
10526
10527 if Impl_Kind = Name_By_Entry
10528 and then Ekind (Impl_Subp) /= E_Entry
10529 then
10530 Error_Msg_Node_2 := Iface_Alias;
10531 Error_Msg_NE
10532 ("type & must implement abstract subprogram & with an entry",
10533 Subp_Alias, Contr_Typ);
10534
10535 elsif Impl_Kind = Name_By_Protected_Procedure then
10536
10537 -- An interface subprogram whose implementation kind is By_
10538 -- Protected_Procedure cannot be implemented by a primitive
10539 -- procedure of a task type.
10540
10541 if Ekind (Contr_Typ) /= E_Protected_Type then
10542 Error_Msg_Node_2 := Contr_Typ;
10543 Error_Msg_NE
10544 ("interface subprogram & cannot be implemented by a " &
10545 "primitive procedure of task type &", Subp_Alias,
10546 Iface_Alias);
10547
10548 -- An interface subprogram whose implementation kind is By_
10549 -- Protected_Procedure must be implemented by a procedure.
10550
10551 elsif Ekind (Impl_Subp) /= E_Procedure then
10552 Error_Msg_Node_2 := Iface_Alias;
10553 Error_Msg_NE
10554 ("type & must implement abstract subprogram & with a " &
10555 "procedure", Subp_Alias, Contr_Typ);
10556
10557 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10558 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10559 then
10560 Error_Msg_Name_1 := Impl_Kind;
10561 Error_Msg_N
10562 ("overriding operation& must have synchronization%",
10563 Subp_Alias);
10564 end if;
10565
10566 -- If primitive has Optional synchronization, overriding operation
10567 -- must match if it has an explicit synchronization..
10568
10569 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10570 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10571 then
10572 Error_Msg_Name_1 := Impl_Kind;
10573 Error_Msg_N
10574 ("overriding operation& must have syncrhonization%",
10575 Subp_Alias);
10576 end if;
10577 end Check_Pragma_Implemented;
10578
10579 ------------------------------
10580 -- Check_Pragma_Implemented --
10581 ------------------------------
10582
10583 procedure Check_Pragma_Implemented
10584 (Subp : Entity_Id;
10585 Iface_Subp : Entity_Id)
10586 is
10587 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10588 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10589
10590 begin
10591 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10592 -- and overriding subprogram are different. In general this is an
10593 -- error except when the implementation kind of the overridden
10594 -- subprograms is By_Any or Optional.
10595
10596 if Iface_Kind /= Subp_Kind
10597 and then Iface_Kind /= Name_By_Any
10598 and then Iface_Kind /= Name_Optional
10599 then
10600 if Iface_Kind = Name_By_Entry then
10601 Error_Msg_N
10602 ("incompatible implementation kind, overridden subprogram " &
10603 "is marked By_Entry", Subp);
10604 else
10605 Error_Msg_N
10606 ("incompatible implementation kind, overridden subprogram " &
10607 "is marked By_Protected_Procedure", Subp);
10608 end if;
10609 end if;
10610 end Check_Pragma_Implemented;
10611
10612 --------------------------------
10613 -- Inherit_Pragma_Implemented --
10614 --------------------------------
10615
10616 procedure Inherit_Pragma_Implemented
10617 (Subp : Entity_Id;
10618 Iface_Subp : Entity_Id)
10619 is
10620 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10621 Loc : constant Source_Ptr := Sloc (Subp);
10622 Impl_Prag : Node_Id;
10623
10624 begin
10625 -- Since the implementation kind is stored as a representation item
10626 -- rather than a flag, create a pragma node.
10627
10628 Impl_Prag :=
10629 Make_Pragma (Loc,
10630 Chars => Name_Implemented,
10631 Pragma_Argument_Associations => New_List (
10632 Make_Pragma_Argument_Association (Loc,
10633 Expression => New_Occurrence_Of (Subp, Loc)),
10634
10635 Make_Pragma_Argument_Association (Loc,
10636 Expression => Make_Identifier (Loc, Iface_Kind))));
10637
10638 -- The pragma doesn't need to be analyzed because it is internally
10639 -- built. It is safe to directly register it as a rep item since we
10640 -- are only interested in the characters of the implementation kind.
10641
10642 Record_Rep_Item (Subp, Impl_Prag);
10643 end Inherit_Pragma_Implemented;
10644
10645 -- Start of processing for Check_Abstract_Overriding
10646
10647 begin
10648 Op_List := Primitive_Operations (T);
10649
10650 -- Loop to check primitive operations
10651
10652 Elmt := First_Elmt (Op_List);
10653 while Present (Elmt) loop
10654 Subp := Node (Elmt);
10655 Alias_Subp := Alias (Subp);
10656
10657 -- Inherited subprograms are identified by the fact that they do not
10658 -- come from source, and the associated source location is the
10659 -- location of the first subtype of the derived type.
10660
10661 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10662 -- subprograms that "require overriding".
10663
10664 -- Special exception, do not complain about failure to override the
10665 -- stream routines _Input and _Output, as well as the primitive
10666 -- operations used in dispatching selects since we always provide
10667 -- automatic overridings for these subprograms.
10668
10669 -- The partial view of T may have been a private extension, for
10670 -- which inherited functions dispatching on result are abstract.
10671 -- If the full view is a null extension, there is no need for
10672 -- overriding in Ada 2005, but wrappers need to be built for them
10673 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10674
10675 if Is_Null_Extension (T)
10676 and then Has_Controlling_Result (Subp)
10677 and then Ada_Version >= Ada_2005
10678 and then Present (Alias_Subp)
10679 and then not Comes_From_Source (Subp)
10680 and then not Is_Abstract_Subprogram (Alias_Subp)
10681 and then not Is_Access_Type (Etype (Subp))
10682 then
10683 null;
10684
10685 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10686 -- processing because this check is done with the aliased
10687 -- entity
10688
10689 elsif Present (Interface_Alias (Subp)) then
10690 null;
10691
10692 elsif (Is_Abstract_Subprogram (Subp)
10693 or else Requires_Overriding (Subp)
10694 or else
10695 (Has_Controlling_Result (Subp)
10696 and then Present (Alias_Subp)
10697 and then not Comes_From_Source (Subp)
10698 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10699 and then not Is_TSS (Subp, TSS_Stream_Input)
10700 and then not Is_TSS (Subp, TSS_Stream_Output)
10701 and then not Is_Abstract_Type (T)
10702 and then not Is_Predefined_Interface_Primitive (Subp)
10703
10704 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10705 -- with abstract interface types because the check will be done
10706 -- with the aliased entity (otherwise we generate a duplicated
10707 -- error message).
10708
10709 and then not Present (Interface_Alias (Subp))
10710 then
10711 if Present (Alias_Subp) then
10712
10713 -- Only perform the check for a derived subprogram when the
10714 -- type has an explicit record extension. This avoids incorrect
10715 -- flagging of abstract subprograms for the case of a type
10716 -- without an extension that is derived from a formal type
10717 -- with a tagged actual (can occur within a private part).
10718
10719 -- Ada 2005 (AI-391): In the case of an inherited function with
10720 -- a controlling result of the type, the rule does not apply if
10721 -- the type is a null extension (unless the parent function
10722 -- itself is abstract, in which case the function must still be
10723 -- be overridden). The expander will generate an overriding
10724 -- wrapper function calling the parent subprogram (see
10725 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10726
10727 Type_Def := Type_Definition (Parent (T));
10728
10729 if Nkind (Type_Def) = N_Derived_Type_Definition
10730 and then Present (Record_Extension_Part (Type_Def))
10731 and then
10732 (Ada_Version < Ada_2005
10733 or else not Is_Null_Extension (T)
10734 or else Ekind (Subp) = E_Procedure
10735 or else not Has_Controlling_Result (Subp)
10736 or else Is_Abstract_Subprogram (Alias_Subp)
10737 or else Requires_Overriding (Subp)
10738 or else Is_Access_Type (Etype (Subp)))
10739 then
10740 -- Avoid reporting error in case of abstract predefined
10741 -- primitive inherited from interface type because the
10742 -- body of internally generated predefined primitives
10743 -- of tagged types are generated later by Freeze_Type
10744
10745 if Is_Interface (Root_Type (T))
10746 and then Is_Abstract_Subprogram (Subp)
10747 and then Is_Predefined_Dispatching_Operation (Subp)
10748 and then not Comes_From_Source (Ultimate_Alias (Subp))
10749 then
10750 null;
10751
10752 -- A null extension is not obliged to override an inherited
10753 -- procedure subject to pragma Extensions_Visible with value
10754 -- False and at least one controlling OUT parameter
10755 -- (SPARK RM 6.1.7(6)).
10756
10757 elsif Is_Null_Extension (T)
10758 and then Is_EVF_Procedure (Subp)
10759 then
10760 null;
10761
10762 else
10763 Error_Msg_NE
10764 ("type must be declared abstract or & overridden",
10765 T, Subp);
10766
10767 -- Traverse the whole chain of aliased subprograms to
10768 -- complete the error notification. This is especially
10769 -- useful for traceability of the chain of entities when
10770 -- the subprogram corresponds with an interface
10771 -- subprogram (which may be defined in another package).
10772
10773 if Present (Alias_Subp) then
10774 declare
10775 E : Entity_Id;
10776
10777 begin
10778 E := Subp;
10779 while Present (Alias (E)) loop
10780
10781 -- Avoid reporting redundant errors on entities
10782 -- inherited from interfaces
10783
10784 if Sloc (E) /= Sloc (T) then
10785 Error_Msg_Sloc := Sloc (E);
10786 Error_Msg_NE
10787 ("\& has been inherited #", T, Subp);
10788 end if;
10789
10790 E := Alias (E);
10791 end loop;
10792
10793 Error_Msg_Sloc := Sloc (E);
10794
10795 -- AI05-0068: report if there is an overriding
10796 -- non-abstract subprogram that is invisible.
10797
10798 if Is_Hidden (E)
10799 and then not Is_Abstract_Subprogram (E)
10800 then
10801 Error_Msg_NE
10802 ("\& subprogram# is not visible",
10803 T, Subp);
10804
10805 -- Clarify the case where a non-null extension must
10806 -- override inherited procedure subject to pragma
10807 -- Extensions_Visible with value False and at least
10808 -- one controlling OUT param.
10809
10810 elsif Is_EVF_Procedure (E) then
10811 Error_Msg_NE
10812 ("\& # is subject to Extensions_Visible False",
10813 T, Subp);
10814
10815 else
10816 Error_Msg_NE
10817 ("\& has been inherited from subprogram #",
10818 T, Subp);
10819 end if;
10820 end;
10821 end if;
10822 end if;
10823
10824 -- Ada 2005 (AI-345): Protected or task type implementing
10825 -- abstract interfaces.
10826
10827 elsif Is_Concurrent_Record_Type (T)
10828 and then Present (Interfaces (T))
10829 then
10830 -- There is no need to check here RM 9.4(11.9/3) since we
10831 -- are processing the corresponding record type and the
10832 -- mode of the overriding subprograms was verified by
10833 -- Check_Conformance when the corresponding concurrent
10834 -- type declaration was analyzed.
10835
10836 Error_Msg_NE
10837 ("interface subprogram & must be overridden", T, Subp);
10838
10839 -- Examine primitive operations of synchronized type to find
10840 -- homonyms that have the wrong profile.
10841
10842 declare
10843 Prim : Entity_Id;
10844
10845 begin
10846 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10847 while Present (Prim) loop
10848 if Chars (Prim) = Chars (Subp) then
10849 Error_Msg_NE
10850 ("profile is not type conformant with prefixed "
10851 & "view profile of inherited operation&",
10852 Prim, Subp);
10853 end if;
10854
10855 Next_Entity (Prim);
10856 end loop;
10857 end;
10858 end if;
10859
10860 else
10861 Error_Msg_Node_2 := T;
10862 Error_Msg_N
10863 ("abstract subprogram& not allowed for type&", Subp);
10864
10865 -- Also post unconditional warning on the type (unconditional
10866 -- so that if there are more than one of these cases, we get
10867 -- them all, and not just the first one).
10868
10869 Error_Msg_Node_2 := Subp;
10870 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10871 end if;
10872
10873 -- A subprogram subject to pragma Extensions_Visible with value
10874 -- "True" cannot override a subprogram subject to the same pragma
10875 -- with value "False" (SPARK RM 6.1.7(5)).
10876
10877 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10878 and then Present (Overridden_Operation (Subp))
10879 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10880 Extensions_Visible_False
10881 then
10882 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10883 Error_Msg_N
10884 ("subprogram & with Extensions_Visible True cannot override "
10885 & "subprogram # with Extensions_Visible False", Subp);
10886 end if;
10887
10888 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10889
10890 -- Subp is an expander-generated procedure which maps an interface
10891 -- alias to a protected wrapper. The interface alias is flagged by
10892 -- pragma Implemented. Ensure that Subp is a procedure when the
10893 -- implementation kind is By_Protected_Procedure or an entry when
10894 -- By_Entry.
10895
10896 if Ada_Version >= Ada_2012
10897 and then Is_Hidden (Subp)
10898 and then Present (Interface_Alias (Subp))
10899 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10900 then
10901 Check_Pragma_Implemented (Subp);
10902 end if;
10903
10904 -- Subp is an interface primitive which overrides another interface
10905 -- primitive marked with pragma Implemented.
10906
10907 if Ada_Version >= Ada_2012
10908 and then Present (Overridden_Operation (Subp))
10909 and then Has_Rep_Pragma
10910 (Overridden_Operation (Subp), Name_Implemented)
10911 then
10912 -- If the overriding routine is also marked by Implemented, check
10913 -- that the two implementation kinds are conforming.
10914
10915 if Has_Rep_Pragma (Subp, Name_Implemented) then
10916 Check_Pragma_Implemented
10917 (Subp => Subp,
10918 Iface_Subp => Overridden_Operation (Subp));
10919
10920 -- Otherwise the overriding routine inherits the implementation
10921 -- kind from the overridden subprogram.
10922
10923 else
10924 Inherit_Pragma_Implemented
10925 (Subp => Subp,
10926 Iface_Subp => Overridden_Operation (Subp));
10927 end if;
10928 end if;
10929
10930 -- If the operation is a wrapper for a synchronized primitive, it
10931 -- may be called indirectly through a dispatching select. We assume
10932 -- that it will be referenced elsewhere indirectly, and suppress
10933 -- warnings about an unused entity.
10934
10935 if Is_Primitive_Wrapper (Subp)
10936 and then Present (Wrapped_Entity (Subp))
10937 then
10938 Set_Referenced (Wrapped_Entity (Subp));
10939 end if;
10940
10941 Next_Elmt (Elmt);
10942 end loop;
10943 end Check_Abstract_Overriding;
10944
10945 ------------------------------------------------
10946 -- Check_Access_Discriminant_Requires_Limited --
10947 ------------------------------------------------
10948
10949 procedure Check_Access_Discriminant_Requires_Limited
10950 (D : Node_Id;
10951 Loc : Node_Id)
10952 is
10953 begin
10954 -- A discriminant_specification for an access discriminant shall appear
10955 -- only in the declaration for a task or protected type, or for a type
10956 -- with the reserved word 'limited' in its definition or in one of its
10957 -- ancestors (RM 3.7(10)).
10958
10959 -- AI-0063: The proper condition is that type must be immutably limited,
10960 -- or else be a partial view.
10961
10962 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10963 if Is_Limited_View (Current_Scope)
10964 or else
10965 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10966 and then Limited_Present (Parent (Current_Scope)))
10967 then
10968 null;
10969
10970 else
10971 Error_Msg_N
10972 ("access discriminants allowed only for limited types", Loc);
10973 end if;
10974 end if;
10975 end Check_Access_Discriminant_Requires_Limited;
10976
10977 -----------------------------------
10978 -- Check_Aliased_Component_Types --
10979 -----------------------------------
10980
10981 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10982 C : Entity_Id;
10983
10984 begin
10985 -- ??? Also need to check components of record extensions, but not
10986 -- components of protected types (which are always limited).
10987
10988 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10989 -- types to be unconstrained. This is safe because it is illegal to
10990 -- create access subtypes to such types with explicit discriminant
10991 -- constraints.
10992
10993 if not Is_Limited_Type (T) then
10994 if Ekind (T) = E_Record_Type then
10995 C := First_Component (T);
10996 while Present (C) loop
10997 if Is_Aliased (C)
10998 and then Has_Discriminants (Etype (C))
10999 and then not Is_Constrained (Etype (C))
11000 and then not In_Instance_Body
11001 and then Ada_Version < Ada_2005
11002 then
11003 Error_Msg_N
11004 ("aliased component must be constrained (RM 3.6(11))",
11005 C);
11006 end if;
11007
11008 Next_Component (C);
11009 end loop;
11010
11011 elsif Ekind (T) = E_Array_Type then
11012 if Has_Aliased_Components (T)
11013 and then Has_Discriminants (Component_Type (T))
11014 and then not Is_Constrained (Component_Type (T))
11015 and then not In_Instance_Body
11016 and then Ada_Version < Ada_2005
11017 then
11018 Error_Msg_N
11019 ("aliased component type must be constrained (RM 3.6(11))",
11020 T);
11021 end if;
11022 end if;
11023 end if;
11024 end Check_Aliased_Component_Types;
11025
11026 ---------------------------------------
11027 -- Check_Anonymous_Access_Components --
11028 ---------------------------------------
11029
11030 procedure Check_Anonymous_Access_Components
11031 (Typ_Decl : Node_Id;
11032 Typ : Entity_Id;
11033 Prev : Entity_Id;
11034 Comp_List : Node_Id)
11035 is
11036 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11037 Anon_Access : Entity_Id;
11038 Acc_Def : Node_Id;
11039 Comp : Node_Id;
11040 Comp_Def : Node_Id;
11041 Decl : Node_Id;
11042 Type_Def : Node_Id;
11043
11044 procedure Build_Incomplete_Type_Declaration;
11045 -- If the record type contains components that include an access to the
11046 -- current record, then create an incomplete type declaration for the
11047 -- record, to be used as the designated type of the anonymous access.
11048 -- This is done only once, and only if there is no previous partial
11049 -- view of the type.
11050
11051 function Designates_T (Subt : Node_Id) return Boolean;
11052 -- Check whether a node designates the enclosing record type, or 'Class
11053 -- of that type
11054
11055 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11056 -- Check whether an access definition includes a reference to
11057 -- the enclosing record type. The reference can be a subtype mark
11058 -- in the access definition itself, a 'Class attribute reference, or
11059 -- recursively a reference appearing in a parameter specification
11060 -- or result definition of an access_to_subprogram definition.
11061
11062 --------------------------------------
11063 -- Build_Incomplete_Type_Declaration --
11064 --------------------------------------
11065
11066 procedure Build_Incomplete_Type_Declaration is
11067 Decl : Node_Id;
11068 Inc_T : Entity_Id;
11069 H : Entity_Id;
11070
11071 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11072 -- it's "is new ... with record" or else "is tagged record ...".
11073
11074 Is_Tagged : constant Boolean :=
11075 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11076 and then
11077 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11078 or else
11079 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11080 and then Tagged_Present (Type_Definition (Typ_Decl)));
11081
11082 begin
11083 -- If there is a previous partial view, no need to create a new one
11084 -- If the partial view, given by Prev, is incomplete, If Prev is
11085 -- a private declaration, full declaration is flagged accordingly.
11086
11087 if Prev /= Typ then
11088 if Is_Tagged then
11089 Make_Class_Wide_Type (Prev);
11090 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11091 Set_Etype (Class_Wide_Type (Typ), Typ);
11092 end if;
11093
11094 return;
11095
11096 elsif Has_Private_Declaration (Typ) then
11097
11098 -- If we refer to T'Class inside T, and T is the completion of a
11099 -- private type, then make sure the class-wide type exists.
11100
11101 if Is_Tagged then
11102 Make_Class_Wide_Type (Typ);
11103 end if;
11104
11105 return;
11106
11107 -- If there was a previous anonymous access type, the incomplete
11108 -- type declaration will have been created already.
11109
11110 elsif Present (Current_Entity (Typ))
11111 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11112 and then Full_View (Current_Entity (Typ)) = Typ
11113 then
11114 if Is_Tagged
11115 and then Comes_From_Source (Current_Entity (Typ))
11116 and then not Is_Tagged_Type (Current_Entity (Typ))
11117 then
11118 Make_Class_Wide_Type (Typ);
11119 Error_Msg_N
11120 ("incomplete view of tagged type should be declared tagged??",
11121 Parent (Current_Entity (Typ)));
11122 end if;
11123 return;
11124
11125 else
11126 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11127 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11128
11129 -- Type has already been inserted into the current scope. Remove
11130 -- it, and add incomplete declaration for type, so that subsequent
11131 -- anonymous access types can use it. The entity is unchained from
11132 -- the homonym list and from immediate visibility. After analysis,
11133 -- the entity in the incomplete declaration becomes immediately
11134 -- visible in the record declaration that follows.
11135
11136 H := Current_Entity (Typ);
11137
11138 if H = Typ then
11139 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11140 else
11141 while Present (H)
11142 and then Homonym (H) /= Typ
11143 loop
11144 H := Homonym (Typ);
11145 end loop;
11146
11147 Set_Homonym (H, Homonym (Typ));
11148 end if;
11149
11150 Insert_Before (Typ_Decl, Decl);
11151 Analyze (Decl);
11152 Set_Full_View (Inc_T, Typ);
11153
11154 if Is_Tagged then
11155
11156 -- Create a common class-wide type for both views, and set the
11157 -- Etype of the class-wide type to the full view.
11158
11159 Make_Class_Wide_Type (Inc_T);
11160 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11161 Set_Etype (Class_Wide_Type (Typ), Typ);
11162 end if;
11163 end if;
11164 end Build_Incomplete_Type_Declaration;
11165
11166 ------------------
11167 -- Designates_T --
11168 ------------------
11169
11170 function Designates_T (Subt : Node_Id) return Boolean is
11171 Type_Id : constant Name_Id := Chars (Typ);
11172
11173 function Names_T (Nam : Node_Id) return Boolean;
11174 -- The record type has not been introduced in the current scope
11175 -- yet, so we must examine the name of the type itself, either
11176 -- an identifier T, or an expanded name of the form P.T, where
11177 -- P denotes the current scope.
11178
11179 -------------
11180 -- Names_T --
11181 -------------
11182
11183 function Names_T (Nam : Node_Id) return Boolean is
11184 begin
11185 if Nkind (Nam) = N_Identifier then
11186 return Chars (Nam) = Type_Id;
11187
11188 elsif Nkind (Nam) = N_Selected_Component then
11189 if Chars (Selector_Name (Nam)) = Type_Id then
11190 if Nkind (Prefix (Nam)) = N_Identifier then
11191 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11192
11193 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11194 return Chars (Selector_Name (Prefix (Nam))) =
11195 Chars (Current_Scope);
11196 else
11197 return False;
11198 end if;
11199
11200 else
11201 return False;
11202 end if;
11203
11204 else
11205 return False;
11206 end if;
11207 end Names_T;
11208
11209 -- Start of processing for Designates_T
11210
11211 begin
11212 if Nkind (Subt) = N_Identifier then
11213 return Chars (Subt) = Type_Id;
11214
11215 -- Reference can be through an expanded name which has not been
11216 -- analyzed yet, and which designates enclosing scopes.
11217
11218 elsif Nkind (Subt) = N_Selected_Component then
11219 if Names_T (Subt) then
11220 return True;
11221
11222 -- Otherwise it must denote an entity that is already visible.
11223 -- The access definition may name a subtype of the enclosing
11224 -- type, if there is a previous incomplete declaration for it.
11225
11226 else
11227 Find_Selected_Component (Subt);
11228 return
11229 Is_Entity_Name (Subt)
11230 and then Scope (Entity (Subt)) = Current_Scope
11231 and then
11232 (Chars (Base_Type (Entity (Subt))) = Type_Id
11233 or else
11234 (Is_Class_Wide_Type (Entity (Subt))
11235 and then
11236 Chars (Etype (Base_Type (Entity (Subt)))) =
11237 Type_Id));
11238 end if;
11239
11240 -- A reference to the current type may appear as the prefix of
11241 -- a 'Class attribute.
11242
11243 elsif Nkind (Subt) = N_Attribute_Reference
11244 and then Attribute_Name (Subt) = Name_Class
11245 then
11246 return Names_T (Prefix (Subt));
11247
11248 else
11249 return False;
11250 end if;
11251 end Designates_T;
11252
11253 ----------------
11254 -- Mentions_T --
11255 ----------------
11256
11257 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11258 Param_Spec : Node_Id;
11259
11260 Acc_Subprg : constant Node_Id :=
11261 Access_To_Subprogram_Definition (Acc_Def);
11262
11263 begin
11264 if No (Acc_Subprg) then
11265 return Designates_T (Subtype_Mark (Acc_Def));
11266 end if;
11267
11268 -- Component is an access_to_subprogram: examine its formals,
11269 -- and result definition in the case of an access_to_function.
11270
11271 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11272 while Present (Param_Spec) loop
11273 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11274 and then Mentions_T (Parameter_Type (Param_Spec))
11275 then
11276 return True;
11277
11278 elsif Designates_T (Parameter_Type (Param_Spec)) then
11279 return True;
11280 end if;
11281
11282 Next (Param_Spec);
11283 end loop;
11284
11285 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11286 if Nkind (Result_Definition (Acc_Subprg)) =
11287 N_Access_Definition
11288 then
11289 return Mentions_T (Result_Definition (Acc_Subprg));
11290 else
11291 return Designates_T (Result_Definition (Acc_Subprg));
11292 end if;
11293 end if;
11294
11295 return False;
11296 end Mentions_T;
11297
11298 -- Start of processing for Check_Anonymous_Access_Components
11299
11300 begin
11301 if No (Comp_List) then
11302 return;
11303 end if;
11304
11305 Comp := First (Component_Items (Comp_List));
11306 while Present (Comp) loop
11307 if Nkind (Comp) = N_Component_Declaration
11308 and then Present
11309 (Access_Definition (Component_Definition (Comp)))
11310 and then
11311 Mentions_T (Access_Definition (Component_Definition (Comp)))
11312 then
11313 Comp_Def := Component_Definition (Comp);
11314 Acc_Def :=
11315 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11316
11317 Build_Incomplete_Type_Declaration;
11318 Anon_Access := Make_Temporary (Loc, 'S');
11319
11320 -- Create a declaration for the anonymous access type: either
11321 -- an access_to_object or an access_to_subprogram.
11322
11323 if Present (Acc_Def) then
11324 if Nkind (Acc_Def) = N_Access_Function_Definition then
11325 Type_Def :=
11326 Make_Access_Function_Definition (Loc,
11327 Parameter_Specifications =>
11328 Parameter_Specifications (Acc_Def),
11329 Result_Definition => Result_Definition (Acc_Def));
11330 else
11331 Type_Def :=
11332 Make_Access_Procedure_Definition (Loc,
11333 Parameter_Specifications =>
11334 Parameter_Specifications (Acc_Def));
11335 end if;
11336
11337 else
11338 Type_Def :=
11339 Make_Access_To_Object_Definition (Loc,
11340 Subtype_Indication =>
11341 Relocate_Node
11342 (Subtype_Mark (Access_Definition (Comp_Def))));
11343
11344 Set_Constant_Present
11345 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11346 Set_All_Present
11347 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11348 end if;
11349
11350 Set_Null_Exclusion_Present
11351 (Type_Def,
11352 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11353
11354 Decl :=
11355 Make_Full_Type_Declaration (Loc,
11356 Defining_Identifier => Anon_Access,
11357 Type_Definition => Type_Def);
11358
11359 Insert_Before (Typ_Decl, Decl);
11360 Analyze (Decl);
11361
11362 -- If an access to subprogram, create the extra formals
11363
11364 if Present (Acc_Def) then
11365 Create_Extra_Formals (Designated_Type (Anon_Access));
11366
11367 -- If an access to object, preserve entity of designated type,
11368 -- for ASIS use, before rewriting the component definition.
11369
11370 else
11371 declare
11372 Desig : Entity_Id;
11373
11374 begin
11375 Desig := Entity (Subtype_Indication (Type_Def));
11376
11377 -- If the access definition is to the current record,
11378 -- the visible entity at this point is an incomplete
11379 -- type. Retrieve the full view to simplify ASIS queries
11380
11381 if Ekind (Desig) = E_Incomplete_Type then
11382 Desig := Full_View (Desig);
11383 end if;
11384
11385 Set_Entity
11386 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11387 end;
11388 end if;
11389
11390 Rewrite (Comp_Def,
11391 Make_Component_Definition (Loc,
11392 Subtype_Indication =>
11393 New_Occurrence_Of (Anon_Access, Loc)));
11394
11395 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11396 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11397 else
11398 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11399 end if;
11400
11401 Set_Is_Local_Anonymous_Access (Anon_Access);
11402 end if;
11403
11404 Next (Comp);
11405 end loop;
11406
11407 if Present (Variant_Part (Comp_List)) then
11408 declare
11409 V : Node_Id;
11410 begin
11411 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11412 while Present (V) loop
11413 Check_Anonymous_Access_Components
11414 (Typ_Decl, Typ, Prev, Component_List (V));
11415 Next_Non_Pragma (V);
11416 end loop;
11417 end;
11418 end if;
11419 end Check_Anonymous_Access_Components;
11420
11421 ----------------------
11422 -- Check_Completion --
11423 ----------------------
11424
11425 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11426 E : Entity_Id;
11427
11428 procedure Post_Error;
11429 -- Post error message for lack of completion for entity E
11430
11431 ----------------
11432 -- Post_Error --
11433 ----------------
11434
11435 procedure Post_Error is
11436 procedure Missing_Body;
11437 -- Output missing body message
11438
11439 ------------------
11440 -- Missing_Body --
11441 ------------------
11442
11443 procedure Missing_Body is
11444 begin
11445 -- Spec is in same unit, so we can post on spec
11446
11447 if In_Same_Source_Unit (Body_Id, E) then
11448 Error_Msg_N ("missing body for &", E);
11449
11450 -- Spec is in a separate unit, so we have to post on the body
11451
11452 else
11453 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11454 end if;
11455 end Missing_Body;
11456
11457 -- Start of processing for Post_Error
11458
11459 begin
11460 if not Comes_From_Source (E) then
11461 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11462
11463 -- It may be an anonymous protected type created for a
11464 -- single variable. Post error on variable, if present.
11465
11466 declare
11467 Var : Entity_Id;
11468
11469 begin
11470 Var := First_Entity (Current_Scope);
11471 while Present (Var) loop
11472 exit when Etype (Var) = E
11473 and then Comes_From_Source (Var);
11474
11475 Next_Entity (Var);
11476 end loop;
11477
11478 if Present (Var) then
11479 E := Var;
11480 end if;
11481 end;
11482 end if;
11483 end if;
11484
11485 -- If a generated entity has no completion, then either previous
11486 -- semantic errors have disabled the expansion phase, or else we had
11487 -- missing subunits, or else we are compiling without expansion,
11488 -- or else something is very wrong.
11489
11490 if not Comes_From_Source (E) then
11491 pragma Assert
11492 (Serious_Errors_Detected > 0
11493 or else Configurable_Run_Time_Violations > 0
11494 or else Subunits_Missing
11495 or else not Expander_Active);
11496 return;
11497
11498 -- Here for source entity
11499
11500 else
11501 -- Here if no body to post the error message, so we post the error
11502 -- on the declaration that has no completion. This is not really
11503 -- the right place to post it, think about this later ???
11504
11505 if No (Body_Id) then
11506 if Is_Type (E) then
11507 Error_Msg_NE
11508 ("missing full declaration for }", Parent (E), E);
11509 else
11510 Error_Msg_NE ("missing body for &", Parent (E), E);
11511 end if;
11512
11513 -- Package body has no completion for a declaration that appears
11514 -- in the corresponding spec. Post error on the body, with a
11515 -- reference to the non-completed declaration.
11516
11517 else
11518 Error_Msg_Sloc := Sloc (E);
11519
11520 if Is_Type (E) then
11521 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11522
11523 elsif Is_Overloadable (E)
11524 and then Current_Entity_In_Scope (E) /= E
11525 then
11526 -- It may be that the completion is mistyped and appears as
11527 -- a distinct overloading of the entity.
11528
11529 declare
11530 Candidate : constant Entity_Id :=
11531 Current_Entity_In_Scope (E);
11532 Decl : constant Node_Id :=
11533 Unit_Declaration_Node (Candidate);
11534
11535 begin
11536 if Is_Overloadable (Candidate)
11537 and then Ekind (Candidate) = Ekind (E)
11538 and then Nkind (Decl) = N_Subprogram_Body
11539 and then Acts_As_Spec (Decl)
11540 then
11541 Check_Type_Conformant (Candidate, E);
11542
11543 else
11544 Missing_Body;
11545 end if;
11546 end;
11547
11548 else
11549 Missing_Body;
11550 end if;
11551 end if;
11552 end if;
11553 end Post_Error;
11554
11555 -- Local variables
11556
11557 Pack_Id : constant Entity_Id := Current_Scope;
11558
11559 -- Start of processing for Check_Completion
11560
11561 begin
11562 E := First_Entity (Pack_Id);
11563 while Present (E) loop
11564 if Is_Intrinsic_Subprogram (E) then
11565 null;
11566
11567 -- The following situation requires special handling: a child unit
11568 -- that appears in the context clause of the body of its parent:
11569
11570 -- procedure Parent.Child (...);
11571
11572 -- with Parent.Child;
11573 -- package body Parent is
11574
11575 -- Here Parent.Child appears as a local entity, but should not be
11576 -- flagged as requiring completion, because it is a compilation
11577 -- unit.
11578
11579 -- Ignore missing completion for a subprogram that does not come from
11580 -- source (including the _Call primitive operation of RAS types,
11581 -- which has to have the flag Comes_From_Source for other purposes):
11582 -- we assume that the expander will provide the missing completion.
11583 -- In case of previous errors, other expansion actions that provide
11584 -- bodies for null procedures with not be invoked, so inhibit message
11585 -- in those cases.
11586
11587 -- Note that E_Operator is not in the list that follows, because
11588 -- this kind is reserved for predefined operators, that are
11589 -- intrinsic and do not need completion.
11590
11591 elsif Ekind_In (E, E_Function,
11592 E_Procedure,
11593 E_Generic_Function,
11594 E_Generic_Procedure)
11595 then
11596 if Has_Completion (E) then
11597 null;
11598
11599 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11600 null;
11601
11602 elsif Is_Subprogram (E)
11603 and then (not Comes_From_Source (E)
11604 or else Chars (E) = Name_uCall)
11605 then
11606 null;
11607
11608 elsif
11609 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11610 then
11611 null;
11612
11613 elsif Nkind (Parent (E)) = N_Procedure_Specification
11614 and then Null_Present (Parent (E))
11615 and then Serious_Errors_Detected > 0
11616 then
11617 null;
11618
11619 else
11620 Post_Error;
11621 end if;
11622
11623 elsif Is_Entry (E) then
11624 if not Has_Completion (E) and then
11625 (Ekind (Scope (E)) = E_Protected_Object
11626 or else Ekind (Scope (E)) = E_Protected_Type)
11627 then
11628 Post_Error;
11629 end if;
11630
11631 elsif Is_Package_Or_Generic_Package (E) then
11632 if Unit_Requires_Body (E) then
11633 if not Has_Completion (E)
11634 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11635 N_Compilation_Unit
11636 then
11637 Post_Error;
11638 end if;
11639
11640 elsif not Is_Child_Unit (E) then
11641 May_Need_Implicit_Body (E);
11642 end if;
11643
11644 -- A formal incomplete type (Ada 2012) does not require a completion;
11645 -- other incomplete type declarations do.
11646
11647 elsif Ekind (E) = E_Incomplete_Type
11648 and then No (Underlying_Type (E))
11649 and then not Is_Generic_Type (E)
11650 then
11651 Post_Error;
11652
11653 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11654 and then not Has_Completion (E)
11655 then
11656 Post_Error;
11657
11658 -- A single task declared in the current scope is a constant, verify
11659 -- that the body of its anonymous type is in the same scope. If the
11660 -- task is defined elsewhere, this may be a renaming declaration for
11661 -- which no completion is needed.
11662
11663 elsif Ekind (E) = E_Constant
11664 and then Ekind (Etype (E)) = E_Task_Type
11665 and then not Has_Completion (Etype (E))
11666 and then Scope (Etype (E)) = Current_Scope
11667 then
11668 Post_Error;
11669
11670 elsif Ekind (E) = E_Protected_Object
11671 and then not Has_Completion (Etype (E))
11672 then
11673 Post_Error;
11674
11675 elsif Ekind (E) = E_Record_Type then
11676 if Is_Tagged_Type (E) then
11677 Check_Abstract_Overriding (E);
11678 Check_Conventions (E);
11679 end if;
11680
11681 Check_Aliased_Component_Types (E);
11682
11683 elsif Ekind (E) = E_Array_Type then
11684 Check_Aliased_Component_Types (E);
11685
11686 end if;
11687
11688 Next_Entity (E);
11689 end loop;
11690 end Check_Completion;
11691
11692 ------------------------------------
11693 -- Check_CPP_Type_Has_No_Defaults --
11694 ------------------------------------
11695
11696 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11697 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11698 Clist : Node_Id;
11699 Comp : Node_Id;
11700
11701 begin
11702 -- Obtain the component list
11703
11704 if Nkind (Tdef) = N_Record_Definition then
11705 Clist := Component_List (Tdef);
11706 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11707 Clist := Component_List (Record_Extension_Part (Tdef));
11708 end if;
11709
11710 -- Check all components to ensure no default expressions
11711
11712 if Present (Clist) then
11713 Comp := First (Component_Items (Clist));
11714 while Present (Comp) loop
11715 if Present (Expression (Comp)) then
11716 Error_Msg_N
11717 ("component of imported 'C'P'P type cannot have "
11718 & "default expression", Expression (Comp));
11719 end if;
11720
11721 Next (Comp);
11722 end loop;
11723 end if;
11724 end Check_CPP_Type_Has_No_Defaults;
11725
11726 ----------------------------
11727 -- Check_Delta_Expression --
11728 ----------------------------
11729
11730 procedure Check_Delta_Expression (E : Node_Id) is
11731 begin
11732 if not (Is_Real_Type (Etype (E))) then
11733 Wrong_Type (E, Any_Real);
11734
11735 elsif not Is_OK_Static_Expression (E) then
11736 Flag_Non_Static_Expr
11737 ("non-static expression used for delta value!", E);
11738
11739 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11740 Error_Msg_N ("delta expression must be positive", E);
11741
11742 else
11743 return;
11744 end if;
11745
11746 -- If any of above errors occurred, then replace the incorrect
11747 -- expression by the real 0.1, which should prevent further errors.
11748
11749 Rewrite (E,
11750 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11751 Analyze_And_Resolve (E, Standard_Float);
11752 end Check_Delta_Expression;
11753
11754 -----------------------------
11755 -- Check_Digits_Expression --
11756 -----------------------------
11757
11758 procedure Check_Digits_Expression (E : Node_Id) is
11759 begin
11760 if not (Is_Integer_Type (Etype (E))) then
11761 Wrong_Type (E, Any_Integer);
11762
11763 elsif not Is_OK_Static_Expression (E) then
11764 Flag_Non_Static_Expr
11765 ("non-static expression used for digits value!", E);
11766
11767 elsif Expr_Value (E) <= 0 then
11768 Error_Msg_N ("digits value must be greater than zero", E);
11769
11770 else
11771 return;
11772 end if;
11773
11774 -- If any of above errors occurred, then replace the incorrect
11775 -- expression by the integer 1, which should prevent further errors.
11776
11777 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11778 Analyze_And_Resolve (E, Standard_Integer);
11779
11780 end Check_Digits_Expression;
11781
11782 --------------------------
11783 -- Check_Initialization --
11784 --------------------------
11785
11786 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11787 begin
11788 -- Special processing for limited types
11789
11790 if Is_Limited_Type (T)
11791 and then not In_Instance
11792 and then not In_Inlined_Body
11793 then
11794 if not OK_For_Limited_Init (T, Exp) then
11795
11796 -- In GNAT mode, this is just a warning, to allow it to be evilly
11797 -- turned off. Otherwise it is a real error.
11798
11799 if GNAT_Mode then
11800 Error_Msg_N
11801 ("??cannot initialize entities of limited type!", Exp);
11802
11803 elsif Ada_Version < Ada_2005 then
11804
11805 -- The side effect removal machinery may generate illegal Ada
11806 -- code to avoid the usage of access types and 'reference in
11807 -- SPARK mode. Since this is legal code with respect to theorem
11808 -- proving, do not emit the error.
11809
11810 if GNATprove_Mode
11811 and then Nkind (Exp) = N_Function_Call
11812 and then Nkind (Parent (Exp)) = N_Object_Declaration
11813 and then not Comes_From_Source
11814 (Defining_Identifier (Parent (Exp)))
11815 then
11816 null;
11817
11818 else
11819 Error_Msg_N
11820 ("cannot initialize entities of limited type", Exp);
11821 Explain_Limited_Type (T, Exp);
11822 end if;
11823
11824 else
11825 -- Specialize error message according to kind of illegal
11826 -- initial expression.
11827
11828 if Nkind (Exp) = N_Type_Conversion
11829 and then Nkind (Expression (Exp)) = N_Function_Call
11830 then
11831 -- No error for internally-generated object declarations,
11832 -- which can come from build-in-place assignment statements.
11833
11834 if Nkind (Parent (Exp)) = N_Object_Declaration
11835 and then not Comes_From_Source
11836 (Defining_Identifier (Parent (Exp)))
11837 then
11838 null;
11839
11840 else
11841 Error_Msg_N
11842 ("illegal context for call to function with limited "
11843 & "result", Exp);
11844 end if;
11845
11846 else
11847 Error_Msg_N
11848 ("initialization of limited object requires aggregate or "
11849 & "function call", Exp);
11850 end if;
11851 end if;
11852 end if;
11853 end if;
11854
11855 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11856 -- set unless we can be sure that no range check is required.
11857
11858 if (GNATprove_Mode or not Expander_Active)
11859 and then Is_Scalar_Type (T)
11860 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11861 then
11862 Set_Do_Range_Check (Exp);
11863 end if;
11864 end Check_Initialization;
11865
11866 ----------------------
11867 -- Check_Interfaces --
11868 ----------------------
11869
11870 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11871 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11872
11873 Iface : Node_Id;
11874 Iface_Def : Node_Id;
11875 Iface_Typ : Entity_Id;
11876 Parent_Node : Node_Id;
11877
11878 Is_Task : Boolean := False;
11879 -- Set True if parent type or any progenitor is a task interface
11880
11881 Is_Protected : Boolean := False;
11882 -- Set True if parent type or any progenitor is a protected interface
11883
11884 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11885 -- Check that a progenitor is compatible with declaration. If an error
11886 -- message is output, it is posted on Error_Node.
11887
11888 ------------------
11889 -- Check_Ifaces --
11890 ------------------
11891
11892 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11893 Iface_Id : constant Entity_Id :=
11894 Defining_Identifier (Parent (Iface_Def));
11895 Type_Def : Node_Id;
11896
11897 begin
11898 if Nkind (N) = N_Private_Extension_Declaration then
11899 Type_Def := N;
11900 else
11901 Type_Def := Type_Definition (N);
11902 end if;
11903
11904 if Is_Task_Interface (Iface_Id) then
11905 Is_Task := True;
11906
11907 elsif Is_Protected_Interface (Iface_Id) then
11908 Is_Protected := True;
11909 end if;
11910
11911 if Is_Synchronized_Interface (Iface_Id) then
11912
11913 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11914 -- extension derived from a synchronized interface must explicitly
11915 -- be declared synchronized, because the full view will be a
11916 -- synchronized type.
11917
11918 if Nkind (N) = N_Private_Extension_Declaration then
11919 if not Synchronized_Present (N) then
11920 Error_Msg_NE
11921 ("private extension of& must be explicitly synchronized",
11922 N, Iface_Id);
11923 end if;
11924
11925 -- However, by 3.9.4(16/2), a full type that is a record extension
11926 -- is never allowed to derive from a synchronized interface (note
11927 -- that interfaces must be excluded from this check, because those
11928 -- are represented by derived type definitions in some cases).
11929
11930 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11931 and then not Interface_Present (Type_Definition (N))
11932 then
11933 Error_Msg_N ("record extension cannot derive from synchronized "
11934 & "interface", Error_Node);
11935 end if;
11936 end if;
11937
11938 -- Check that the characteristics of the progenitor are compatible
11939 -- with the explicit qualifier in the declaration.
11940 -- The check only applies to qualifiers that come from source.
11941 -- Limited_Present also appears in the declaration of corresponding
11942 -- records, and the check does not apply to them.
11943
11944 if Limited_Present (Type_Def)
11945 and then not
11946 Is_Concurrent_Record_Type (Defining_Identifier (N))
11947 then
11948 if Is_Limited_Interface (Parent_Type)
11949 and then not Is_Limited_Interface (Iface_Id)
11950 then
11951 Error_Msg_NE
11952 ("progenitor & must be limited interface",
11953 Error_Node, Iface_Id);
11954
11955 elsif
11956 (Task_Present (Iface_Def)
11957 or else Protected_Present (Iface_Def)
11958 or else Synchronized_Present (Iface_Def))
11959 and then Nkind (N) /= N_Private_Extension_Declaration
11960 and then not Error_Posted (N)
11961 then
11962 Error_Msg_NE
11963 ("progenitor & must be limited interface",
11964 Error_Node, Iface_Id);
11965 end if;
11966
11967 -- Protected interfaces can only inherit from limited, synchronized
11968 -- or protected interfaces.
11969
11970 elsif Nkind (N) = N_Full_Type_Declaration
11971 and then Protected_Present (Type_Def)
11972 then
11973 if Limited_Present (Iface_Def)
11974 or else Synchronized_Present (Iface_Def)
11975 or else Protected_Present (Iface_Def)
11976 then
11977 null;
11978
11979 elsif Task_Present (Iface_Def) then
11980 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11981 & "from task interface", Error_Node);
11982
11983 else
11984 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11985 & "from non-limited interface", Error_Node);
11986 end if;
11987
11988 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11989 -- limited and synchronized.
11990
11991 elsif Synchronized_Present (Type_Def) then
11992 if Limited_Present (Iface_Def)
11993 or else Synchronized_Present (Iface_Def)
11994 then
11995 null;
11996
11997 elsif Protected_Present (Iface_Def)
11998 and then Nkind (N) /= N_Private_Extension_Declaration
11999 then
12000 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12001 & "from protected interface", Error_Node);
12002
12003 elsif Task_Present (Iface_Def)
12004 and then Nkind (N) /= N_Private_Extension_Declaration
12005 then
12006 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12007 & "from task interface", Error_Node);
12008
12009 elsif not Is_Limited_Interface (Iface_Id) then
12010 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12011 & "from non-limited interface", Error_Node);
12012 end if;
12013
12014 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12015 -- synchronized or task interfaces.
12016
12017 elsif Nkind (N) = N_Full_Type_Declaration
12018 and then Task_Present (Type_Def)
12019 then
12020 if Limited_Present (Iface_Def)
12021 or else Synchronized_Present (Iface_Def)
12022 or else Task_Present (Iface_Def)
12023 then
12024 null;
12025
12026 elsif Protected_Present (Iface_Def) then
12027 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12028 & "protected interface", Error_Node);
12029
12030 else
12031 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12032 & "non-limited interface", Error_Node);
12033 end if;
12034 end if;
12035 end Check_Ifaces;
12036
12037 -- Start of processing for Check_Interfaces
12038
12039 begin
12040 if Is_Interface (Parent_Type) then
12041 if Is_Task_Interface (Parent_Type) then
12042 Is_Task := True;
12043
12044 elsif Is_Protected_Interface (Parent_Type) then
12045 Is_Protected := True;
12046 end if;
12047 end if;
12048
12049 if Nkind (N) = N_Private_Extension_Declaration then
12050
12051 -- Check that progenitors are compatible with declaration
12052
12053 Iface := First (Interface_List (Def));
12054 while Present (Iface) loop
12055 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12056
12057 Parent_Node := Parent (Base_Type (Iface_Typ));
12058 Iface_Def := Type_Definition (Parent_Node);
12059
12060 if not Is_Interface (Iface_Typ) then
12061 Diagnose_Interface (Iface, Iface_Typ);
12062 else
12063 Check_Ifaces (Iface_Def, Iface);
12064 end if;
12065
12066 Next (Iface);
12067 end loop;
12068
12069 if Is_Task and Is_Protected then
12070 Error_Msg_N
12071 ("type cannot derive from task and protected interface", N);
12072 end if;
12073
12074 return;
12075 end if;
12076
12077 -- Full type declaration of derived type.
12078 -- Check compatibility with parent if it is interface type
12079
12080 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12081 and then Is_Interface (Parent_Type)
12082 then
12083 Parent_Node := Parent (Parent_Type);
12084
12085 -- More detailed checks for interface varieties
12086
12087 Check_Ifaces
12088 (Iface_Def => Type_Definition (Parent_Node),
12089 Error_Node => Subtype_Indication (Type_Definition (N)));
12090 end if;
12091
12092 Iface := First (Interface_List (Def));
12093 while Present (Iface) loop
12094 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12095
12096 Parent_Node := Parent (Base_Type (Iface_Typ));
12097 Iface_Def := Type_Definition (Parent_Node);
12098
12099 if not Is_Interface (Iface_Typ) then
12100 Diagnose_Interface (Iface, Iface_Typ);
12101
12102 else
12103 -- "The declaration of a specific descendant of an interface
12104 -- type freezes the interface type" RM 13.14
12105
12106 Freeze_Before (N, Iface_Typ);
12107 Check_Ifaces (Iface_Def, Error_Node => Iface);
12108 end if;
12109
12110 Next (Iface);
12111 end loop;
12112
12113 if Is_Task and Is_Protected then
12114 Error_Msg_N
12115 ("type cannot derive from task and protected interface", N);
12116 end if;
12117 end Check_Interfaces;
12118
12119 ------------------------------------
12120 -- Check_Or_Process_Discriminants --
12121 ------------------------------------
12122
12123 -- If an incomplete or private type declaration was already given for the
12124 -- type, the discriminants may have already been processed if they were
12125 -- present on the incomplete declaration. In this case a full conformance
12126 -- check has been performed in Find_Type_Name, and we then recheck here
12127 -- some properties that can't be checked on the partial view alone.
12128 -- Otherwise we call Process_Discriminants.
12129
12130 procedure Check_Or_Process_Discriminants
12131 (N : Node_Id;
12132 T : Entity_Id;
12133 Prev : Entity_Id := Empty)
12134 is
12135 begin
12136 if Has_Discriminants (T) then
12137
12138 -- Discriminants are already set on T if they were already present
12139 -- on the partial view. Make them visible to component declarations.
12140
12141 declare
12142 D : Entity_Id;
12143 -- Discriminant on T (full view) referencing expr on partial view
12144
12145 Prev_D : Entity_Id;
12146 -- Entity of corresponding discriminant on partial view
12147
12148 New_D : Node_Id;
12149 -- Discriminant specification for full view, expression is
12150 -- the syntactic copy on full view (which has been checked for
12151 -- conformance with partial view), only used here to post error
12152 -- message.
12153
12154 begin
12155 D := First_Discriminant (T);
12156 New_D := First (Discriminant_Specifications (N));
12157 while Present (D) loop
12158 Prev_D := Current_Entity (D);
12159 Set_Current_Entity (D);
12160 Set_Is_Immediately_Visible (D);
12161 Set_Homonym (D, Prev_D);
12162
12163 -- Handle the case where there is an untagged partial view and
12164 -- the full view is tagged: must disallow discriminants with
12165 -- defaults, unless compiling for Ada 2012, which allows a
12166 -- limited tagged type to have defaulted discriminants (see
12167 -- AI05-0214). However, suppress error here if it was already
12168 -- reported on the default expression of the partial view.
12169
12170 if Is_Tagged_Type (T)
12171 and then Present (Expression (Parent (D)))
12172 and then (not Is_Limited_Type (Current_Scope)
12173 or else Ada_Version < Ada_2012)
12174 and then not Error_Posted (Expression (Parent (D)))
12175 then
12176 if Ada_Version >= Ada_2012 then
12177 Error_Msg_N
12178 ("discriminants of nonlimited tagged type cannot have "
12179 & "defaults",
12180 Expression (New_D));
12181 else
12182 Error_Msg_N
12183 ("discriminants of tagged type cannot have defaults",
12184 Expression (New_D));
12185 end if;
12186 end if;
12187
12188 -- Ada 2005 (AI-230): Access discriminant allowed in
12189 -- non-limited record types.
12190
12191 if Ada_Version < Ada_2005 then
12192
12193 -- This restriction gets applied to the full type here. It
12194 -- has already been applied earlier to the partial view.
12195
12196 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12197 end if;
12198
12199 Next_Discriminant (D);
12200 Next (New_D);
12201 end loop;
12202 end;
12203
12204 elsif Present (Discriminant_Specifications (N)) then
12205 Process_Discriminants (N, Prev);
12206 end if;
12207 end Check_Or_Process_Discriminants;
12208
12209 ----------------------
12210 -- Check_Real_Bound --
12211 ----------------------
12212
12213 procedure Check_Real_Bound (Bound : Node_Id) is
12214 begin
12215 if not Is_Real_Type (Etype (Bound)) then
12216 Error_Msg_N
12217 ("bound in real type definition must be of real type", Bound);
12218
12219 elsif not Is_OK_Static_Expression (Bound) then
12220 Flag_Non_Static_Expr
12221 ("non-static expression used for real type bound!", Bound);
12222
12223 else
12224 return;
12225 end if;
12226
12227 Rewrite
12228 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12229 Analyze (Bound);
12230 Resolve (Bound, Standard_Float);
12231 end Check_Real_Bound;
12232
12233 ------------------------------
12234 -- Complete_Private_Subtype --
12235 ------------------------------
12236
12237 procedure Complete_Private_Subtype
12238 (Priv : Entity_Id;
12239 Full : Entity_Id;
12240 Full_Base : Entity_Id;
12241 Related_Nod : Node_Id)
12242 is
12243 Save_Next_Entity : Entity_Id;
12244 Save_Homonym : Entity_Id;
12245
12246 begin
12247 -- Set semantic attributes for (implicit) private subtype completion.
12248 -- If the full type has no discriminants, then it is a copy of the
12249 -- full view of the base. Otherwise, it is a subtype of the base with
12250 -- a possible discriminant constraint. Save and restore the original
12251 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12252 -- not corrupt the entity chain.
12253
12254 -- Note that the type of the full view is the same entity as the type
12255 -- of the partial view. In this fashion, the subtype has access to the
12256 -- correct view of the parent.
12257
12258 Save_Next_Entity := Next_Entity (Full);
12259 Save_Homonym := Homonym (Priv);
12260
12261 case Ekind (Full_Base) is
12262 when Class_Wide_Kind
12263 | Private_Kind
12264 | Protected_Kind
12265 | Task_Kind
12266 | E_Record_Subtype
12267 | E_Record_Type
12268 =>
12269 Copy_Node (Priv, Full);
12270
12271 Set_Has_Discriminants
12272 (Full, Has_Discriminants (Full_Base));
12273 Set_Has_Unknown_Discriminants
12274 (Full, Has_Unknown_Discriminants (Full_Base));
12275 Set_First_Entity (Full, First_Entity (Full_Base));
12276 Set_Last_Entity (Full, Last_Entity (Full_Base));
12277
12278 -- If the underlying base type is constrained, we know that the
12279 -- full view of the subtype is constrained as well (the converse
12280 -- is not necessarily true).
12281
12282 if Is_Constrained (Full_Base) then
12283 Set_Is_Constrained (Full);
12284 end if;
12285
12286 when others =>
12287 Copy_Node (Full_Base, Full);
12288
12289 Set_Chars (Full, Chars (Priv));
12290 Conditional_Delay (Full, Priv);
12291 Set_Sloc (Full, Sloc (Priv));
12292 end case;
12293
12294 Set_Next_Entity (Full, Save_Next_Entity);
12295 Set_Homonym (Full, Save_Homonym);
12296 Set_Associated_Node_For_Itype (Full, Related_Nod);
12297
12298 -- Set common attributes for all subtypes: kind, convention, etc.
12299
12300 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12301 Set_Convention (Full, Convention (Full_Base));
12302
12303 -- The Etype of the full view is inconsistent. Gigi needs to see the
12304 -- structural full view, which is what the current scheme gives: the
12305 -- Etype of the full view is the etype of the full base. However, if the
12306 -- full base is a derived type, the full view then looks like a subtype
12307 -- of the parent, not a subtype of the full base. If instead we write:
12308
12309 -- Set_Etype (Full, Full_Base);
12310
12311 -- then we get inconsistencies in the front-end (confusion between
12312 -- views). Several outstanding bugs are related to this ???
12313
12314 Set_Is_First_Subtype (Full, False);
12315 Set_Scope (Full, Scope (Priv));
12316 Set_Size_Info (Full, Full_Base);
12317 Set_RM_Size (Full, RM_Size (Full_Base));
12318 Set_Is_Itype (Full);
12319
12320 -- A subtype of a private-type-without-discriminants, whose full-view
12321 -- has discriminants with default expressions, is not constrained.
12322
12323 if not Has_Discriminants (Priv) then
12324 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12325
12326 if Has_Discriminants (Full_Base) then
12327 Set_Discriminant_Constraint
12328 (Full, Discriminant_Constraint (Full_Base));
12329
12330 -- The partial view may have been indefinite, the full view
12331 -- might not be.
12332
12333 Set_Has_Unknown_Discriminants
12334 (Full, Has_Unknown_Discriminants (Full_Base));
12335 end if;
12336 end if;
12337
12338 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12339 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12340
12341 -- Freeze the private subtype entity if its parent is delayed, and not
12342 -- already frozen. We skip this processing if the type is an anonymous
12343 -- subtype of a record component, or is the corresponding record of a
12344 -- protected type, since these are processed when the enclosing type
12345 -- is frozen. If the parent type is declared in a nested package then
12346 -- the freezing of the private and full views also happens later.
12347
12348 if not Is_Type (Scope (Full)) then
12349 if Is_Itype (Priv)
12350 and then In_Same_Source_Unit (Full, Full_Base)
12351 and then Scope (Full_Base) /= Scope (Full)
12352 then
12353 Set_Has_Delayed_Freeze (Full);
12354 Set_Has_Delayed_Freeze (Priv);
12355
12356 else
12357 Set_Has_Delayed_Freeze (Full,
12358 Has_Delayed_Freeze (Full_Base)
12359 and then not Is_Frozen (Full_Base));
12360 end if;
12361 end if;
12362
12363 Set_Freeze_Node (Full, Empty);
12364 Set_Is_Frozen (Full, False);
12365 Set_Full_View (Priv, Full);
12366
12367 if Has_Discriminants (Full) then
12368 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12369 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12370
12371 if Has_Unknown_Discriminants (Full) then
12372 Set_Discriminant_Constraint (Full, No_Elist);
12373 end if;
12374 end if;
12375
12376 if Ekind (Full_Base) = E_Record_Type
12377 and then Has_Discriminants (Full_Base)
12378 and then Has_Discriminants (Priv) -- might not, if errors
12379 and then not Has_Unknown_Discriminants (Priv)
12380 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12381 then
12382 Create_Constrained_Components
12383 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12384
12385 -- If the full base is itself derived from private, build a congruent
12386 -- subtype of its underlying type, for use by the back end. For a
12387 -- constrained record component, the declaration cannot be placed on
12388 -- the component list, but it must nevertheless be built an analyzed, to
12389 -- supply enough information for Gigi to compute the size of component.
12390
12391 elsif Ekind (Full_Base) in Private_Kind
12392 and then Is_Derived_Type (Full_Base)
12393 and then Has_Discriminants (Full_Base)
12394 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12395 then
12396 if not Is_Itype (Priv)
12397 and then
12398 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12399 then
12400 Build_Underlying_Full_View
12401 (Parent (Priv), Full, Etype (Full_Base));
12402
12403 elsif Nkind (Related_Nod) = N_Component_Declaration then
12404 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12405 end if;
12406
12407 elsif Is_Record_Type (Full_Base) then
12408
12409 -- Show Full is simply a renaming of Full_Base
12410
12411 Set_Cloned_Subtype (Full, Full_Base);
12412 end if;
12413
12414 -- It is unsafe to share the bounds of a scalar type, because the Itype
12415 -- is elaborated on demand, and if a bound is non-static then different
12416 -- orders of elaboration in different units will lead to different
12417 -- external symbols.
12418
12419 if Is_Scalar_Type (Full_Base) then
12420 Set_Scalar_Range (Full,
12421 Make_Range (Sloc (Related_Nod),
12422 Low_Bound =>
12423 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12424 High_Bound =>
12425 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12426
12427 -- This completion inherits the bounds of the full parent, but if
12428 -- the parent is an unconstrained floating point type, so is the
12429 -- completion.
12430
12431 if Is_Floating_Point_Type (Full_Base) then
12432 Set_Includes_Infinities
12433 (Scalar_Range (Full), Has_Infinities (Full_Base));
12434 end if;
12435 end if;
12436
12437 -- ??? It seems that a lot of fields are missing that should be copied
12438 -- from Full_Base to Full. Here are some that are introduced in a
12439 -- non-disruptive way but a cleanup is necessary.
12440
12441 if Is_Tagged_Type (Full_Base) then
12442 Set_Is_Tagged_Type (Full);
12443 Set_Direct_Primitive_Operations
12444 (Full, Direct_Primitive_Operations (Full_Base));
12445 Set_No_Tagged_Streams_Pragma
12446 (Full, No_Tagged_Streams_Pragma (Full_Base));
12447
12448 -- Inherit class_wide type of full_base in case the partial view was
12449 -- not tagged. Otherwise it has already been created when the private
12450 -- subtype was analyzed.
12451
12452 if No (Class_Wide_Type (Full)) then
12453 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12454 end if;
12455
12456 -- If this is a subtype of a protected or task type, constrain its
12457 -- corresponding record, unless this is a subtype without constraints,
12458 -- i.e. a simple renaming as with an actual subtype in an instance.
12459
12460 elsif Is_Concurrent_Type (Full_Base) then
12461 if Has_Discriminants (Full)
12462 and then Present (Corresponding_Record_Type (Full_Base))
12463 and then
12464 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12465 then
12466 Set_Corresponding_Record_Type (Full,
12467 Constrain_Corresponding_Record
12468 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12469
12470 else
12471 Set_Corresponding_Record_Type (Full,
12472 Corresponding_Record_Type (Full_Base));
12473 end if;
12474 end if;
12475
12476 -- Link rep item chain, and also setting of Has_Predicates from private
12477 -- subtype to full subtype, since we will need these on the full subtype
12478 -- to create the predicate function. Note that the full subtype may
12479 -- already have rep items, inherited from the full view of the base
12480 -- type, so we must be sure not to overwrite these entries.
12481
12482 declare
12483 Append : Boolean;
12484 Item : Node_Id;
12485 Next_Item : Node_Id;
12486 Priv_Item : Node_Id;
12487
12488 begin
12489 Item := First_Rep_Item (Full);
12490 Priv_Item := First_Rep_Item (Priv);
12491
12492 -- If no existing rep items on full type, we can just link directly
12493 -- to the list of items on the private type, if any exist.. Same if
12494 -- the rep items are only those inherited from the base
12495
12496 if (No (Item)
12497 or else Nkind (Item) /= N_Aspect_Specification
12498 or else Entity (Item) = Full_Base)
12499 and then Present (First_Rep_Item (Priv))
12500 then
12501 Set_First_Rep_Item (Full, Priv_Item);
12502
12503 -- Otherwise, search to the end of items currently linked to the full
12504 -- subtype and append the private items to the end. However, if Priv
12505 -- and Full already have the same list of rep items, then the append
12506 -- is not done, as that would create a circularity.
12507 --
12508 -- The partial view may have a predicate and the rep item lists of
12509 -- both views agree when inherited from the same ancestor. In that
12510 -- case, simply propagate the list from one view to the other.
12511 -- A more complex analysis needed here ???
12512
12513 elsif Present (Priv_Item)
12514 and then Item = Next_Rep_Item (Priv_Item)
12515 then
12516 Set_First_Rep_Item (Full, Priv_Item);
12517
12518 elsif Item /= Priv_Item then
12519 Append := True;
12520 loop
12521 Next_Item := Next_Rep_Item (Item);
12522 exit when No (Next_Item);
12523 Item := Next_Item;
12524
12525 -- If the private view has aspect specifications, the full view
12526 -- inherits them. Since these aspects may already have been
12527 -- attached to the full view during derivation, do not append
12528 -- them if already present.
12529
12530 if Item = First_Rep_Item (Priv) then
12531 Append := False;
12532 exit;
12533 end if;
12534 end loop;
12535
12536 -- And link the private type items at the end of the chain
12537
12538 if Append then
12539 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12540 end if;
12541 end if;
12542 end;
12543
12544 -- Make sure Has_Predicates is set on full type if it is set on the
12545 -- private type. Note that it may already be set on the full type and
12546 -- if so, we don't want to unset it. Similarly, propagate information
12547 -- about delayed aspects, because the corresponding pragmas must be
12548 -- analyzed when one of the views is frozen. This last step is needed
12549 -- in particular when the full type is a scalar type for which an
12550 -- anonymous base type is constructed.
12551
12552 -- The predicate functions are generated either at the freeze point
12553 -- of the type or at the end of the visible part, and we must avoid
12554 -- generating them twice.
12555
12556 if Has_Predicates (Priv) then
12557 Set_Has_Predicates (Full);
12558
12559 if Present (Predicate_Function (Priv))
12560 and then No (Predicate_Function (Full))
12561 then
12562 Set_Predicate_Function (Full, Predicate_Function (Priv));
12563 end if;
12564 end if;
12565
12566 if Has_Delayed_Aspects (Priv) then
12567 Set_Has_Delayed_Aspects (Full);
12568 end if;
12569 end Complete_Private_Subtype;
12570
12571 ----------------------------
12572 -- Constant_Redeclaration --
12573 ----------------------------
12574
12575 procedure Constant_Redeclaration
12576 (Id : Entity_Id;
12577 N : Node_Id;
12578 T : out Entity_Id)
12579 is
12580 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12581 Obj_Def : constant Node_Id := Object_Definition (N);
12582 New_T : Entity_Id;
12583
12584 procedure Check_Possible_Deferred_Completion
12585 (Prev_Id : Entity_Id;
12586 Prev_Obj_Def : Node_Id;
12587 Curr_Obj_Def : Node_Id);
12588 -- Determine whether the two object definitions describe the partial
12589 -- and the full view of a constrained deferred constant. Generate
12590 -- a subtype for the full view and verify that it statically matches
12591 -- the subtype of the partial view.
12592
12593 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12594 -- If deferred constant is an access type initialized with an allocator,
12595 -- check whether there is an illegal recursion in the definition,
12596 -- through a default value of some record subcomponent. This is normally
12597 -- detected when generating init procs, but requires this additional
12598 -- mechanism when expansion is disabled.
12599
12600 ----------------------------------------
12601 -- Check_Possible_Deferred_Completion --
12602 ----------------------------------------
12603
12604 procedure Check_Possible_Deferred_Completion
12605 (Prev_Id : Entity_Id;
12606 Prev_Obj_Def : Node_Id;
12607 Curr_Obj_Def : Node_Id)
12608 is
12609 begin
12610 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12611 and then Present (Constraint (Prev_Obj_Def))
12612 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12613 and then Present (Constraint (Curr_Obj_Def))
12614 then
12615 declare
12616 Loc : constant Source_Ptr := Sloc (N);
12617 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12618 Decl : constant Node_Id :=
12619 Make_Subtype_Declaration (Loc,
12620 Defining_Identifier => Def_Id,
12621 Subtype_Indication =>
12622 Relocate_Node (Curr_Obj_Def));
12623
12624 begin
12625 Insert_Before_And_Analyze (N, Decl);
12626 Set_Etype (Id, Def_Id);
12627
12628 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12629 Error_Msg_Sloc := Sloc (Prev_Id);
12630 Error_Msg_N ("subtype does not statically match deferred "
12631 & "declaration #", N);
12632 end if;
12633 end;
12634 end if;
12635 end Check_Possible_Deferred_Completion;
12636
12637 ---------------------------------
12638 -- Check_Recursive_Declaration --
12639 ---------------------------------
12640
12641 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12642 Comp : Entity_Id;
12643
12644 begin
12645 if Is_Record_Type (Typ) then
12646 Comp := First_Component (Typ);
12647 while Present (Comp) loop
12648 if Comes_From_Source (Comp) then
12649 if Present (Expression (Parent (Comp)))
12650 and then Is_Entity_Name (Expression (Parent (Comp)))
12651 and then Entity (Expression (Parent (Comp))) = Prev
12652 then
12653 Error_Msg_Sloc := Sloc (Parent (Comp));
12654 Error_Msg_NE
12655 ("illegal circularity with declaration for & #",
12656 N, Comp);
12657 return;
12658
12659 elsif Is_Record_Type (Etype (Comp)) then
12660 Check_Recursive_Declaration (Etype (Comp));
12661 end if;
12662 end if;
12663
12664 Next_Component (Comp);
12665 end loop;
12666 end if;
12667 end Check_Recursive_Declaration;
12668
12669 -- Start of processing for Constant_Redeclaration
12670
12671 begin
12672 if Nkind (Parent (Prev)) = N_Object_Declaration then
12673 if Nkind (Object_Definition
12674 (Parent (Prev))) = N_Subtype_Indication
12675 then
12676 -- Find type of new declaration. The constraints of the two
12677 -- views must match statically, but there is no point in
12678 -- creating an itype for the full view.
12679
12680 if Nkind (Obj_Def) = N_Subtype_Indication then
12681 Find_Type (Subtype_Mark (Obj_Def));
12682 New_T := Entity (Subtype_Mark (Obj_Def));
12683
12684 else
12685 Find_Type (Obj_Def);
12686 New_T := Entity (Obj_Def);
12687 end if;
12688
12689 T := Etype (Prev);
12690
12691 else
12692 -- The full view may impose a constraint, even if the partial
12693 -- view does not, so construct the subtype.
12694
12695 New_T := Find_Type_Of_Object (Obj_Def, N);
12696 T := New_T;
12697 end if;
12698
12699 else
12700 -- Current declaration is illegal, diagnosed below in Enter_Name
12701
12702 T := Empty;
12703 New_T := Any_Type;
12704 end if;
12705
12706 -- If previous full declaration or a renaming declaration exists, or if
12707 -- a homograph is present, let Enter_Name handle it, either with an
12708 -- error or with the removal of an overridden implicit subprogram.
12709 -- The previous one is a full declaration if it has an expression
12710 -- (which in the case of an aggregate is indicated by the Init flag).
12711
12712 if Ekind (Prev) /= E_Constant
12713 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12714 or else Present (Expression (Parent (Prev)))
12715 or else Has_Init_Expression (Parent (Prev))
12716 or else Present (Full_View (Prev))
12717 then
12718 Enter_Name (Id);
12719
12720 -- Verify that types of both declarations match, or else that both types
12721 -- are anonymous access types whose designated subtypes statically match
12722 -- (as allowed in Ada 2005 by AI-385).
12723
12724 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12725 and then
12726 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12727 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12728 or else Is_Access_Constant (Etype (New_T)) /=
12729 Is_Access_Constant (Etype (Prev))
12730 or else Can_Never_Be_Null (Etype (New_T)) /=
12731 Can_Never_Be_Null (Etype (Prev))
12732 or else Null_Exclusion_Present (Parent (Prev)) /=
12733 Null_Exclusion_Present (Parent (Id))
12734 or else not Subtypes_Statically_Match
12735 (Designated_Type (Etype (Prev)),
12736 Designated_Type (Etype (New_T))))
12737 then
12738 Error_Msg_Sloc := Sloc (Prev);
12739 Error_Msg_N ("type does not match declaration#", N);
12740 Set_Full_View (Prev, Id);
12741 Set_Etype (Id, Any_Type);
12742
12743 -- A deferred constant whose type is an anonymous array is always
12744 -- illegal (unless imported). A detailed error message might be
12745 -- helpful for Ada beginners.
12746
12747 if Nkind (Object_Definition (Parent (Prev)))
12748 = N_Constrained_Array_Definition
12749 and then Nkind (Object_Definition (N))
12750 = N_Constrained_Array_Definition
12751 then
12752 Error_Msg_N ("\each anonymous array is a distinct type", N);
12753 Error_Msg_N ("a deferred constant must have a named type",
12754 Object_Definition (Parent (Prev)));
12755 end if;
12756
12757 elsif
12758 Null_Exclusion_Present (Parent (Prev))
12759 and then not Null_Exclusion_Present (N)
12760 then
12761 Error_Msg_Sloc := Sloc (Prev);
12762 Error_Msg_N ("null-exclusion does not match declaration#", N);
12763 Set_Full_View (Prev, Id);
12764 Set_Etype (Id, Any_Type);
12765
12766 -- If so, process the full constant declaration
12767
12768 else
12769 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12770 -- the deferred declaration is constrained, then the subtype defined
12771 -- by the subtype_indication in the full declaration shall match it
12772 -- statically.
12773
12774 Check_Possible_Deferred_Completion
12775 (Prev_Id => Prev,
12776 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12777 Curr_Obj_Def => Obj_Def);
12778
12779 Set_Full_View (Prev, Id);
12780 Set_Is_Public (Id, Is_Public (Prev));
12781 Set_Is_Internal (Id);
12782 Append_Entity (Id, Current_Scope);
12783
12784 -- Check ALIASED present if present before (RM 7.4(7))
12785
12786 if Is_Aliased (Prev)
12787 and then not Aliased_Present (N)
12788 then
12789 Error_Msg_Sloc := Sloc (Prev);
12790 Error_Msg_N ("ALIASED required (see declaration #)", N);
12791 end if;
12792
12793 -- Check that placement is in private part and that the incomplete
12794 -- declaration appeared in the visible part.
12795
12796 if Ekind (Current_Scope) = E_Package
12797 and then not In_Private_Part (Current_Scope)
12798 then
12799 Error_Msg_Sloc := Sloc (Prev);
12800 Error_Msg_N
12801 ("full constant for declaration # must be in private part", N);
12802
12803 elsif Ekind (Current_Scope) = E_Package
12804 and then
12805 List_Containing (Parent (Prev)) /=
12806 Visible_Declarations (Package_Specification (Current_Scope))
12807 then
12808 Error_Msg_N
12809 ("deferred constant must be declared in visible part",
12810 Parent (Prev));
12811 end if;
12812
12813 if Is_Access_Type (T)
12814 and then Nkind (Expression (N)) = N_Allocator
12815 then
12816 Check_Recursive_Declaration (Designated_Type (T));
12817 end if;
12818
12819 -- A deferred constant is a visible entity. If type has invariants,
12820 -- verify that the initial value satisfies them. This is not done in
12821 -- GNATprove mode, as GNATprove handles invariant checks itself.
12822
12823 if Has_Invariants (T)
12824 and then Present (Invariant_Procedure (T))
12825 and then not GNATprove_Mode
12826 then
12827 Insert_After (N,
12828 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12829 end if;
12830 end if;
12831 end Constant_Redeclaration;
12832
12833 ----------------------
12834 -- Constrain_Access --
12835 ----------------------
12836
12837 procedure Constrain_Access
12838 (Def_Id : in out Entity_Id;
12839 S : Node_Id;
12840 Related_Nod : Node_Id)
12841 is
12842 T : constant Entity_Id := Entity (Subtype_Mark (S));
12843 Desig_Type : constant Entity_Id := Designated_Type (T);
12844 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12845 Constraint_OK : Boolean := True;
12846
12847 begin
12848 if Is_Array_Type (Desig_Type) then
12849 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12850
12851 elsif (Is_Record_Type (Desig_Type)
12852 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12853 and then not Is_Constrained (Desig_Type)
12854 then
12855 -- ??? The following code is a temporary bypass to ignore a
12856 -- discriminant constraint on access type if it is constraining
12857 -- the current record. Avoid creating the implicit subtype of the
12858 -- record we are currently compiling since right now, we cannot
12859 -- handle these. For now, just return the access type itself.
12860
12861 if Desig_Type = Current_Scope
12862 and then No (Def_Id)
12863 then
12864 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12865 Def_Id := Entity (Subtype_Mark (S));
12866
12867 -- This call added to ensure that the constraint is analyzed
12868 -- (needed for a B test). Note that we still return early from
12869 -- this procedure to avoid recursive processing. ???
12870
12871 Constrain_Discriminated_Type
12872 (Desig_Subtype, S, Related_Nod, For_Access => True);
12873 return;
12874 end if;
12875
12876 -- Enforce rule that the constraint is illegal if there is an
12877 -- unconstrained view of the designated type. This means that the
12878 -- partial view (either a private type declaration or a derivation
12879 -- from a private type) has no discriminants. (Defect Report
12880 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12881
12882 -- Rule updated for Ada 2005: The private type is said to have
12883 -- a constrained partial view, given that objects of the type
12884 -- can be declared. Furthermore, the rule applies to all access
12885 -- types, unlike the rule concerning default discriminants (see
12886 -- RM 3.7.1(7/3))
12887
12888 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12889 and then Has_Private_Declaration (Desig_Type)
12890 and then In_Open_Scopes (Scope (Desig_Type))
12891 and then Has_Discriminants (Desig_Type)
12892 then
12893 declare
12894 Pack : constant Node_Id :=
12895 Unit_Declaration_Node (Scope (Desig_Type));
12896 Decls : List_Id;
12897 Decl : Node_Id;
12898
12899 begin
12900 if Nkind (Pack) = N_Package_Declaration then
12901 Decls := Visible_Declarations (Specification (Pack));
12902 Decl := First (Decls);
12903 while Present (Decl) loop
12904 if (Nkind (Decl) = N_Private_Type_Declaration
12905 and then Chars (Defining_Identifier (Decl)) =
12906 Chars (Desig_Type))
12907
12908 or else
12909 (Nkind (Decl) = N_Full_Type_Declaration
12910 and then
12911 Chars (Defining_Identifier (Decl)) =
12912 Chars (Desig_Type)
12913 and then Is_Derived_Type (Desig_Type)
12914 and then
12915 Has_Private_Declaration (Etype (Desig_Type)))
12916 then
12917 if No (Discriminant_Specifications (Decl)) then
12918 Error_Msg_N
12919 ("cannot constrain access type if designated "
12920 & "type has constrained partial view", S);
12921 end if;
12922
12923 exit;
12924 end if;
12925
12926 Next (Decl);
12927 end loop;
12928 end if;
12929 end;
12930 end if;
12931
12932 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12933 For_Access => True);
12934
12935 elsif Is_Concurrent_Type (Desig_Type)
12936 and then not Is_Constrained (Desig_Type)
12937 then
12938 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12939
12940 else
12941 Error_Msg_N ("invalid constraint on access type", S);
12942
12943 -- We simply ignore an invalid constraint
12944
12945 Desig_Subtype := Desig_Type;
12946 Constraint_OK := False;
12947 end if;
12948
12949 if No (Def_Id) then
12950 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12951 else
12952 Set_Ekind (Def_Id, E_Access_Subtype);
12953 end if;
12954
12955 if Constraint_OK then
12956 Set_Etype (Def_Id, Base_Type (T));
12957
12958 if Is_Private_Type (Desig_Type) then
12959 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12960 end if;
12961 else
12962 Set_Etype (Def_Id, Any_Type);
12963 end if;
12964
12965 Set_Size_Info (Def_Id, T);
12966 Set_Is_Constrained (Def_Id, Constraint_OK);
12967 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12968 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12969 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12970
12971 Conditional_Delay (Def_Id, T);
12972
12973 -- AI-363 : Subtypes of general access types whose designated types have
12974 -- default discriminants are disallowed. In instances, the rule has to
12975 -- be checked against the actual, of which T is the subtype. In a
12976 -- generic body, the rule is checked assuming that the actual type has
12977 -- defaulted discriminants.
12978
12979 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12980 if Ekind (Base_Type (T)) = E_General_Access_Type
12981 and then Has_Defaulted_Discriminants (Desig_Type)
12982 then
12983 if Ada_Version < Ada_2005 then
12984 Error_Msg_N
12985 ("access subtype of general access type would not " &
12986 "be allowed in Ada 2005?y?", S);
12987 else
12988 Error_Msg_N
12989 ("access subtype of general access type not allowed", S);
12990 end if;
12991
12992 Error_Msg_N ("\discriminants have defaults", S);
12993
12994 elsif Is_Access_Type (T)
12995 and then Is_Generic_Type (Desig_Type)
12996 and then Has_Discriminants (Desig_Type)
12997 and then In_Package_Body (Current_Scope)
12998 then
12999 if Ada_Version < Ada_2005 then
13000 Error_Msg_N
13001 ("access subtype would not be allowed in generic body "
13002 & "in Ada 2005?y?", S);
13003 else
13004 Error_Msg_N
13005 ("access subtype not allowed in generic body", S);
13006 end if;
13007
13008 Error_Msg_N
13009 ("\designated type is a discriminated formal", S);
13010 end if;
13011 end if;
13012 end Constrain_Access;
13013
13014 ---------------------
13015 -- Constrain_Array --
13016 ---------------------
13017
13018 procedure Constrain_Array
13019 (Def_Id : in out Entity_Id;
13020 SI : Node_Id;
13021 Related_Nod : Node_Id;
13022 Related_Id : Entity_Id;
13023 Suffix : Character)
13024 is
13025 C : constant Node_Id := Constraint (SI);
13026 Number_Of_Constraints : Nat := 0;
13027 Index : Node_Id;
13028 S, T : Entity_Id;
13029 Constraint_OK : Boolean := True;
13030
13031 begin
13032 T := Entity (Subtype_Mark (SI));
13033
13034 if Is_Access_Type (T) then
13035 T := Designated_Type (T);
13036 end if;
13037
13038 -- If an index constraint follows a subtype mark in a subtype indication
13039 -- then the type or subtype denoted by the subtype mark must not already
13040 -- impose an index constraint. The subtype mark must denote either an
13041 -- unconstrained array type or an access type whose designated type
13042 -- is such an array type... (RM 3.6.1)
13043
13044 if Is_Constrained (T) then
13045 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13046 Constraint_OK := False;
13047
13048 else
13049 S := First (Constraints (C));
13050 while Present (S) loop
13051 Number_Of_Constraints := Number_Of_Constraints + 1;
13052 Next (S);
13053 end loop;
13054
13055 -- In either case, the index constraint must provide a discrete
13056 -- range for each index of the array type and the type of each
13057 -- discrete range must be the same as that of the corresponding
13058 -- index. (RM 3.6.1)
13059
13060 if Number_Of_Constraints /= Number_Dimensions (T) then
13061 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13062 Constraint_OK := False;
13063
13064 else
13065 S := First (Constraints (C));
13066 Index := First_Index (T);
13067 Analyze (Index);
13068
13069 -- Apply constraints to each index type
13070
13071 for J in 1 .. Number_Of_Constraints loop
13072 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13073 Next (Index);
13074 Next (S);
13075 end loop;
13076
13077 end if;
13078 end if;
13079
13080 if No (Def_Id) then
13081 Def_Id :=
13082 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13083 Set_Parent (Def_Id, Related_Nod);
13084
13085 else
13086 Set_Ekind (Def_Id, E_Array_Subtype);
13087 end if;
13088
13089 Set_Size_Info (Def_Id, (T));
13090 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13091 Set_Etype (Def_Id, Base_Type (T));
13092
13093 if Constraint_OK then
13094 Set_First_Index (Def_Id, First (Constraints (C)));
13095 else
13096 Set_First_Index (Def_Id, First_Index (T));
13097 end if;
13098
13099 Set_Is_Constrained (Def_Id, True);
13100 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13101 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13102
13103 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13104 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13105
13106 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13107 -- We need to initialize the attribute because if Def_Id is previously
13108 -- analyzed through a limited_with clause, it will have the attributes
13109 -- of an incomplete type, one of which is an Elist that overlaps the
13110 -- Packed_Array_Impl_Type field.
13111
13112 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13113
13114 -- Build a freeze node if parent still needs one. Also make sure that
13115 -- the Depends_On_Private status is set because the subtype will need
13116 -- reprocessing at the time the base type does, and also we must set a
13117 -- conditional delay.
13118
13119 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13120 Conditional_Delay (Def_Id, T);
13121 end Constrain_Array;
13122
13123 ------------------------------
13124 -- Constrain_Component_Type --
13125 ------------------------------
13126
13127 function Constrain_Component_Type
13128 (Comp : Entity_Id;
13129 Constrained_Typ : Entity_Id;
13130 Related_Node : Node_Id;
13131 Typ : Entity_Id;
13132 Constraints : Elist_Id) return Entity_Id
13133 is
13134 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13135 Compon_Type : constant Entity_Id := Etype (Comp);
13136
13137 function Build_Constrained_Array_Type
13138 (Old_Type : Entity_Id) return Entity_Id;
13139 -- If Old_Type is an array type, one of whose indexes is constrained
13140 -- by a discriminant, build an Itype whose constraint replaces the
13141 -- discriminant with its value in the constraint.
13142
13143 function Build_Constrained_Discriminated_Type
13144 (Old_Type : Entity_Id) return Entity_Id;
13145 -- Ditto for record components
13146
13147 function Build_Constrained_Access_Type
13148 (Old_Type : Entity_Id) return Entity_Id;
13149 -- Ditto for access types. Makes use of previous two functions, to
13150 -- constrain designated type.
13151
13152 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13153 -- T is an array or discriminated type, C is a list of constraints
13154 -- that apply to T. This routine builds the constrained subtype.
13155
13156 function Is_Discriminant (Expr : Node_Id) return Boolean;
13157 -- Returns True if Expr is a discriminant
13158
13159 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13160 -- Find the value of discriminant Discrim in Constraint
13161
13162 -----------------------------------
13163 -- Build_Constrained_Access_Type --
13164 -----------------------------------
13165
13166 function Build_Constrained_Access_Type
13167 (Old_Type : Entity_Id) return Entity_Id
13168 is
13169 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13170 Itype : Entity_Id;
13171 Desig_Subtype : Entity_Id;
13172 Scop : Entity_Id;
13173
13174 begin
13175 -- if the original access type was not embedded in the enclosing
13176 -- type definition, there is no need to produce a new access
13177 -- subtype. In fact every access type with an explicit constraint
13178 -- generates an itype whose scope is the enclosing record.
13179
13180 if not Is_Type (Scope (Old_Type)) then
13181 return Old_Type;
13182
13183 elsif Is_Array_Type (Desig_Type) then
13184 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13185
13186 elsif Has_Discriminants (Desig_Type) then
13187
13188 -- This may be an access type to an enclosing record type for
13189 -- which we are constructing the constrained components. Return
13190 -- the enclosing record subtype. This is not always correct,
13191 -- but avoids infinite recursion. ???
13192
13193 Desig_Subtype := Any_Type;
13194
13195 for J in reverse 0 .. Scope_Stack.Last loop
13196 Scop := Scope_Stack.Table (J).Entity;
13197
13198 if Is_Type (Scop)
13199 and then Base_Type (Scop) = Base_Type (Desig_Type)
13200 then
13201 Desig_Subtype := Scop;
13202 end if;
13203
13204 exit when not Is_Type (Scop);
13205 end loop;
13206
13207 if Desig_Subtype = Any_Type then
13208 Desig_Subtype :=
13209 Build_Constrained_Discriminated_Type (Desig_Type);
13210 end if;
13211
13212 else
13213 return Old_Type;
13214 end if;
13215
13216 if Desig_Subtype /= Desig_Type then
13217
13218 -- The Related_Node better be here or else we won't be able
13219 -- to attach new itypes to a node in the tree.
13220
13221 pragma Assert (Present (Related_Node));
13222
13223 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13224
13225 Set_Etype (Itype, Base_Type (Old_Type));
13226 Set_Size_Info (Itype, (Old_Type));
13227 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13228 Set_Depends_On_Private (Itype, Has_Private_Component
13229 (Old_Type));
13230 Set_Is_Access_Constant (Itype, Is_Access_Constant
13231 (Old_Type));
13232
13233 -- The new itype needs freezing when it depends on a not frozen
13234 -- type and the enclosing subtype needs freezing.
13235
13236 if Has_Delayed_Freeze (Constrained_Typ)
13237 and then not Is_Frozen (Constrained_Typ)
13238 then
13239 Conditional_Delay (Itype, Base_Type (Old_Type));
13240 end if;
13241
13242 return Itype;
13243
13244 else
13245 return Old_Type;
13246 end if;
13247 end Build_Constrained_Access_Type;
13248
13249 ----------------------------------
13250 -- Build_Constrained_Array_Type --
13251 ----------------------------------
13252
13253 function Build_Constrained_Array_Type
13254 (Old_Type : Entity_Id) return Entity_Id
13255 is
13256 Lo_Expr : Node_Id;
13257 Hi_Expr : Node_Id;
13258 Old_Index : Node_Id;
13259 Range_Node : Node_Id;
13260 Constr_List : List_Id;
13261
13262 Need_To_Create_Itype : Boolean := False;
13263
13264 begin
13265 Old_Index := First_Index (Old_Type);
13266 while Present (Old_Index) loop
13267 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13268
13269 if Is_Discriminant (Lo_Expr)
13270 or else
13271 Is_Discriminant (Hi_Expr)
13272 then
13273 Need_To_Create_Itype := True;
13274 end if;
13275
13276 Next_Index (Old_Index);
13277 end loop;
13278
13279 if Need_To_Create_Itype then
13280 Constr_List := New_List;
13281
13282 Old_Index := First_Index (Old_Type);
13283 while Present (Old_Index) loop
13284 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13285
13286 if Is_Discriminant (Lo_Expr) then
13287 Lo_Expr := Get_Discr_Value (Lo_Expr);
13288 end if;
13289
13290 if Is_Discriminant (Hi_Expr) then
13291 Hi_Expr := Get_Discr_Value (Hi_Expr);
13292 end if;
13293
13294 Range_Node :=
13295 Make_Range
13296 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13297
13298 Append (Range_Node, To => Constr_List);
13299
13300 Next_Index (Old_Index);
13301 end loop;
13302
13303 return Build_Subtype (Old_Type, Constr_List);
13304
13305 else
13306 return Old_Type;
13307 end if;
13308 end Build_Constrained_Array_Type;
13309
13310 ------------------------------------------
13311 -- Build_Constrained_Discriminated_Type --
13312 ------------------------------------------
13313
13314 function Build_Constrained_Discriminated_Type
13315 (Old_Type : Entity_Id) return Entity_Id
13316 is
13317 Expr : Node_Id;
13318 Constr_List : List_Id;
13319 Old_Constraint : Elmt_Id;
13320
13321 Need_To_Create_Itype : Boolean := False;
13322
13323 begin
13324 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13325 while Present (Old_Constraint) loop
13326 Expr := Node (Old_Constraint);
13327
13328 if Is_Discriminant (Expr) then
13329 Need_To_Create_Itype := True;
13330 end if;
13331
13332 Next_Elmt (Old_Constraint);
13333 end loop;
13334
13335 if Need_To_Create_Itype then
13336 Constr_List := New_List;
13337
13338 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13339 while Present (Old_Constraint) loop
13340 Expr := Node (Old_Constraint);
13341
13342 if Is_Discriminant (Expr) then
13343 Expr := Get_Discr_Value (Expr);
13344 end if;
13345
13346 Append (New_Copy_Tree (Expr), To => Constr_List);
13347
13348 Next_Elmt (Old_Constraint);
13349 end loop;
13350
13351 return Build_Subtype (Old_Type, Constr_List);
13352
13353 else
13354 return Old_Type;
13355 end if;
13356 end Build_Constrained_Discriminated_Type;
13357
13358 -------------------
13359 -- Build_Subtype --
13360 -------------------
13361
13362 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13363 Indic : Node_Id;
13364 Subtyp_Decl : Node_Id;
13365 Def_Id : Entity_Id;
13366 Btyp : Entity_Id := Base_Type (T);
13367
13368 begin
13369 -- The Related_Node better be here or else we won't be able to
13370 -- attach new itypes to a node in the tree.
13371
13372 pragma Assert (Present (Related_Node));
13373
13374 -- If the view of the component's type is incomplete or private
13375 -- with unknown discriminants, then the constraint must be applied
13376 -- to the full type.
13377
13378 if Has_Unknown_Discriminants (Btyp)
13379 and then Present (Underlying_Type (Btyp))
13380 then
13381 Btyp := Underlying_Type (Btyp);
13382 end if;
13383
13384 Indic :=
13385 Make_Subtype_Indication (Loc,
13386 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13387 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13388
13389 Def_Id := Create_Itype (Ekind (T), Related_Node);
13390
13391 Subtyp_Decl :=
13392 Make_Subtype_Declaration (Loc,
13393 Defining_Identifier => Def_Id,
13394 Subtype_Indication => Indic);
13395
13396 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13397
13398 -- Itypes must be analyzed with checks off (see package Itypes)
13399
13400 Analyze (Subtyp_Decl, Suppress => All_Checks);
13401
13402 return Def_Id;
13403 end Build_Subtype;
13404
13405 ---------------------
13406 -- Get_Discr_Value --
13407 ---------------------
13408
13409 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13410 D : Entity_Id;
13411 E : Elmt_Id;
13412
13413 begin
13414 -- The discriminant may be declared for the type, in which case we
13415 -- find it by iterating over the list of discriminants. If the
13416 -- discriminant is inherited from a parent type, it appears as the
13417 -- corresponding discriminant of the current type. This will be the
13418 -- case when constraining an inherited component whose constraint is
13419 -- given by a discriminant of the parent.
13420
13421 D := First_Discriminant (Typ);
13422 E := First_Elmt (Constraints);
13423
13424 while Present (D) loop
13425 if D = Entity (Discrim)
13426 or else D = CR_Discriminant (Entity (Discrim))
13427 or else Corresponding_Discriminant (D) = Entity (Discrim)
13428 then
13429 return Node (E);
13430 end if;
13431
13432 Next_Discriminant (D);
13433 Next_Elmt (E);
13434 end loop;
13435
13436 -- The Corresponding_Discriminant mechanism is incomplete, because
13437 -- the correspondence between new and old discriminants is not one
13438 -- to one: one new discriminant can constrain several old ones. In
13439 -- that case, scan sequentially the stored_constraint, the list of
13440 -- discriminants of the parents, and the constraints.
13441
13442 -- Previous code checked for the present of the Stored_Constraint
13443 -- list for the derived type, but did not use it at all. Should it
13444 -- be present when the component is a discriminated task type?
13445
13446 if Is_Derived_Type (Typ)
13447 and then Scope (Entity (Discrim)) = Etype (Typ)
13448 then
13449 D := First_Discriminant (Etype (Typ));
13450 E := First_Elmt (Constraints);
13451 while Present (D) loop
13452 if D = Entity (Discrim) then
13453 return Node (E);
13454 end if;
13455
13456 Next_Discriminant (D);
13457 Next_Elmt (E);
13458 end loop;
13459 end if;
13460
13461 -- Something is wrong if we did not find the value
13462
13463 raise Program_Error;
13464 end Get_Discr_Value;
13465
13466 ---------------------
13467 -- Is_Discriminant --
13468 ---------------------
13469
13470 function Is_Discriminant (Expr : Node_Id) return Boolean is
13471 Discrim_Scope : Entity_Id;
13472
13473 begin
13474 if Denotes_Discriminant (Expr) then
13475 Discrim_Scope := Scope (Entity (Expr));
13476
13477 -- Either we have a reference to one of Typ's discriminants,
13478
13479 pragma Assert (Discrim_Scope = Typ
13480
13481 -- or to the discriminants of the parent type, in the case
13482 -- of a derivation of a tagged type with variants.
13483
13484 or else Discrim_Scope = Etype (Typ)
13485 or else Full_View (Discrim_Scope) = Etype (Typ)
13486
13487 -- or same as above for the case where the discriminants
13488 -- were declared in Typ's private view.
13489
13490 or else (Is_Private_Type (Discrim_Scope)
13491 and then Chars (Discrim_Scope) = Chars (Typ))
13492
13493 -- or else we are deriving from the full view and the
13494 -- discriminant is declared in the private entity.
13495
13496 or else (Is_Private_Type (Typ)
13497 and then Chars (Discrim_Scope) = Chars (Typ))
13498
13499 -- Or we are constrained the corresponding record of a
13500 -- synchronized type that completes a private declaration.
13501
13502 or else (Is_Concurrent_Record_Type (Typ)
13503 and then
13504 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13505
13506 -- or we have a class-wide type, in which case make sure the
13507 -- discriminant found belongs to the root type.
13508
13509 or else (Is_Class_Wide_Type (Typ)
13510 and then Etype (Typ) = Discrim_Scope));
13511
13512 return True;
13513 end if;
13514
13515 -- In all other cases we have something wrong
13516
13517 return False;
13518 end Is_Discriminant;
13519
13520 -- Start of processing for Constrain_Component_Type
13521
13522 begin
13523 if Nkind (Parent (Comp)) = N_Component_Declaration
13524 and then Comes_From_Source (Parent (Comp))
13525 and then Comes_From_Source
13526 (Subtype_Indication (Component_Definition (Parent (Comp))))
13527 and then
13528 Is_Entity_Name
13529 (Subtype_Indication (Component_Definition (Parent (Comp))))
13530 then
13531 return Compon_Type;
13532
13533 elsif Is_Array_Type (Compon_Type) then
13534 return Build_Constrained_Array_Type (Compon_Type);
13535
13536 elsif Has_Discriminants (Compon_Type) then
13537 return Build_Constrained_Discriminated_Type (Compon_Type);
13538
13539 elsif Is_Access_Type (Compon_Type) then
13540 return Build_Constrained_Access_Type (Compon_Type);
13541
13542 else
13543 return Compon_Type;
13544 end if;
13545 end Constrain_Component_Type;
13546
13547 --------------------------
13548 -- Constrain_Concurrent --
13549 --------------------------
13550
13551 -- For concurrent types, the associated record value type carries the same
13552 -- discriminants, so when we constrain a concurrent type, we must constrain
13553 -- the corresponding record type as well.
13554
13555 procedure Constrain_Concurrent
13556 (Def_Id : in out Entity_Id;
13557 SI : Node_Id;
13558 Related_Nod : Node_Id;
13559 Related_Id : Entity_Id;
13560 Suffix : Character)
13561 is
13562 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13563 -- case of a private subtype (needed when only doing semantic analysis).
13564
13565 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13566 T_Val : Entity_Id;
13567
13568 begin
13569 if Is_Access_Type (T_Ent) then
13570 T_Ent := Designated_Type (T_Ent);
13571 end if;
13572
13573 T_Val := Corresponding_Record_Type (T_Ent);
13574
13575 if Present (T_Val) then
13576
13577 if No (Def_Id) then
13578 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13579
13580 -- Elaborate itype now, as it may be used in a subsequent
13581 -- synchronized operation in another scope.
13582
13583 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13584 Build_Itype_Reference (Def_Id, Related_Nod);
13585 end if;
13586 end if;
13587
13588 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13589 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13590
13591 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13592 Set_Corresponding_Record_Type (Def_Id,
13593 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13594
13595 else
13596 -- If there is no associated record, expansion is disabled and this
13597 -- is a generic context. Create a subtype in any case, so that
13598 -- semantic analysis can proceed.
13599
13600 if No (Def_Id) then
13601 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13602 end if;
13603
13604 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13605 end if;
13606 end Constrain_Concurrent;
13607
13608 ------------------------------------
13609 -- Constrain_Corresponding_Record --
13610 ------------------------------------
13611
13612 function Constrain_Corresponding_Record
13613 (Prot_Subt : Entity_Id;
13614 Corr_Rec : Entity_Id;
13615 Related_Nod : Node_Id) return Entity_Id
13616 is
13617 T_Sub : constant Entity_Id :=
13618 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13619
13620 begin
13621 Set_Etype (T_Sub, Corr_Rec);
13622 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13623 Set_Is_Constrained (T_Sub, True);
13624 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13625 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13626
13627 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13628 Set_Discriminant_Constraint
13629 (T_Sub, Discriminant_Constraint (Prot_Subt));
13630 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13631 Create_Constrained_Components
13632 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13633 end if;
13634
13635 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13636
13637 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13638 Conditional_Delay (T_Sub, Corr_Rec);
13639
13640 else
13641 -- This is a component subtype: it will be frozen in the context of
13642 -- the enclosing record's init_proc, so that discriminant references
13643 -- are resolved to discriminals. (Note: we used to skip freezing
13644 -- altogether in that case, which caused errors downstream for
13645 -- components of a bit packed array type).
13646
13647 Set_Has_Delayed_Freeze (T_Sub);
13648 end if;
13649
13650 return T_Sub;
13651 end Constrain_Corresponding_Record;
13652
13653 -----------------------
13654 -- Constrain_Decimal --
13655 -----------------------
13656
13657 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13658 T : constant Entity_Id := Entity (Subtype_Mark (S));
13659 C : constant Node_Id := Constraint (S);
13660 Loc : constant Source_Ptr := Sloc (C);
13661 Range_Expr : Node_Id;
13662 Digits_Expr : Node_Id;
13663 Digits_Val : Uint;
13664 Bound_Val : Ureal;
13665
13666 begin
13667 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13668
13669 if Nkind (C) = N_Range_Constraint then
13670 Range_Expr := Range_Expression (C);
13671 Digits_Val := Digits_Value (T);
13672
13673 else
13674 pragma Assert (Nkind (C) = N_Digits_Constraint);
13675
13676 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13677
13678 Digits_Expr := Digits_Expression (C);
13679 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13680
13681 Check_Digits_Expression (Digits_Expr);
13682 Digits_Val := Expr_Value (Digits_Expr);
13683
13684 if Digits_Val > Digits_Value (T) then
13685 Error_Msg_N
13686 ("digits expression is incompatible with subtype", C);
13687 Digits_Val := Digits_Value (T);
13688 end if;
13689
13690 if Present (Range_Constraint (C)) then
13691 Range_Expr := Range_Expression (Range_Constraint (C));
13692 else
13693 Range_Expr := Empty;
13694 end if;
13695 end if;
13696
13697 Set_Etype (Def_Id, Base_Type (T));
13698 Set_Size_Info (Def_Id, (T));
13699 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13700 Set_Delta_Value (Def_Id, Delta_Value (T));
13701 Set_Scale_Value (Def_Id, Scale_Value (T));
13702 Set_Small_Value (Def_Id, Small_Value (T));
13703 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13704 Set_Digits_Value (Def_Id, Digits_Val);
13705
13706 -- Manufacture range from given digits value if no range present
13707
13708 if No (Range_Expr) then
13709 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13710 Range_Expr :=
13711 Make_Range (Loc,
13712 Low_Bound =>
13713 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13714 High_Bound =>
13715 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13716 end if;
13717
13718 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13719 Set_Discrete_RM_Size (Def_Id);
13720
13721 -- Unconditionally delay the freeze, since we cannot set size
13722 -- information in all cases correctly until the freeze point.
13723
13724 Set_Has_Delayed_Freeze (Def_Id);
13725 end Constrain_Decimal;
13726
13727 ----------------------------------
13728 -- Constrain_Discriminated_Type --
13729 ----------------------------------
13730
13731 procedure Constrain_Discriminated_Type
13732 (Def_Id : Entity_Id;
13733 S : Node_Id;
13734 Related_Nod : Node_Id;
13735 For_Access : Boolean := False)
13736 is
13737 E : Entity_Id := Entity (Subtype_Mark (S));
13738 T : Entity_Id;
13739
13740 procedure Fixup_Bad_Constraint;
13741 -- Called after finding a bad constraint, and after having posted an
13742 -- appropriate error message. The goal is to leave type Def_Id in as
13743 -- reasonable state as possible.
13744
13745 --------------------------
13746 -- Fixup_Bad_Constraint --
13747 --------------------------
13748
13749 procedure Fixup_Bad_Constraint is
13750 begin
13751 -- Set a reasonable Ekind for the entity, including incomplete types.
13752
13753 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13754
13755 -- Set Etype to the known type, to reduce chances of cascaded errors
13756
13757 Set_Etype (Def_Id, E);
13758 Set_Error_Posted (Def_Id);
13759 end Fixup_Bad_Constraint;
13760
13761 -- Local variables
13762
13763 C : Node_Id;
13764 Constr : Elist_Id := New_Elmt_List;
13765
13766 -- Start of processing for Constrain_Discriminated_Type
13767
13768 begin
13769 C := Constraint (S);
13770
13771 -- A discriminant constraint is only allowed in a subtype indication,
13772 -- after a subtype mark. This subtype mark must denote either a type
13773 -- with discriminants, or an access type whose designated type is a
13774 -- type with discriminants. A discriminant constraint specifies the
13775 -- values of these discriminants (RM 3.7.2(5)).
13776
13777 T := Base_Type (Entity (Subtype_Mark (S)));
13778
13779 if Is_Access_Type (T) then
13780 T := Designated_Type (T);
13781 end if;
13782
13783 -- In an instance it may be necessary to retrieve the full view of a
13784 -- type with unknown discriminants, or a full view with defaulted
13785 -- discriminants. In other contexts the constraint is illegal.
13786
13787 if In_Instance
13788 and then Is_Private_Type (T)
13789 and then Present (Full_View (T))
13790 and then
13791 (Has_Unknown_Discriminants (T)
13792 or else
13793 (not Has_Discriminants (T)
13794 and then Has_Discriminants (Full_View (T))
13795 and then Present (Discriminant_Default_Value
13796 (First_Discriminant (Full_View (T))))))
13797 then
13798 T := Full_View (T);
13799 E := Full_View (E);
13800 end if;
13801
13802 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13803 -- generating an error for access-to-incomplete subtypes.
13804
13805 if Ada_Version >= Ada_2005
13806 and then Ekind (T) = E_Incomplete_Type
13807 and then Nkind (Parent (S)) = N_Subtype_Declaration
13808 and then not Is_Itype (Def_Id)
13809 then
13810 -- A little sanity check: emit an error message if the type has
13811 -- discriminants to begin with. Type T may be a regular incomplete
13812 -- type or imported via a limited with clause.
13813
13814 if Has_Discriminants (T)
13815 or else (From_Limited_With (T)
13816 and then Present (Non_Limited_View (T))
13817 and then Nkind (Parent (Non_Limited_View (T))) =
13818 N_Full_Type_Declaration
13819 and then Present (Discriminant_Specifications
13820 (Parent (Non_Limited_View (T)))))
13821 then
13822 Error_Msg_N
13823 ("(Ada 2005) incomplete subtype may not be constrained", C);
13824 else
13825 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13826 end if;
13827
13828 Fixup_Bad_Constraint;
13829 return;
13830
13831 -- Check that the type has visible discriminants. The type may be
13832 -- a private type with unknown discriminants whose full view has
13833 -- discriminants which are invisible.
13834
13835 elsif not Has_Discriminants (T)
13836 or else
13837 (Has_Unknown_Discriminants (T)
13838 and then Is_Private_Type (T))
13839 then
13840 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13841 Fixup_Bad_Constraint;
13842 return;
13843
13844 elsif Is_Constrained (E)
13845 or else (Ekind (E) = E_Class_Wide_Subtype
13846 and then Present (Discriminant_Constraint (E)))
13847 then
13848 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13849 Fixup_Bad_Constraint;
13850 return;
13851 end if;
13852
13853 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13854 -- applies to the base type.
13855
13856 T := Base_Type (T);
13857
13858 Constr := Build_Discriminant_Constraints (T, S);
13859
13860 -- If the list returned was empty we had an error in building the
13861 -- discriminant constraint. We have also already signalled an error
13862 -- in the incomplete type case
13863
13864 if Is_Empty_Elmt_List (Constr) then
13865 Fixup_Bad_Constraint;
13866 return;
13867 end if;
13868
13869 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13870 end Constrain_Discriminated_Type;
13871
13872 ---------------------------
13873 -- Constrain_Enumeration --
13874 ---------------------------
13875
13876 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13877 T : constant Entity_Id := Entity (Subtype_Mark (S));
13878 C : constant Node_Id := Constraint (S);
13879
13880 begin
13881 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13882
13883 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13884
13885 Set_Etype (Def_Id, Base_Type (T));
13886 Set_Size_Info (Def_Id, (T));
13887 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13888 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13889
13890 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13891
13892 Set_Discrete_RM_Size (Def_Id);
13893 end Constrain_Enumeration;
13894
13895 ----------------------
13896 -- Constrain_Float --
13897 ----------------------
13898
13899 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13900 T : constant Entity_Id := Entity (Subtype_Mark (S));
13901 C : Node_Id;
13902 D : Node_Id;
13903 Rais : Node_Id;
13904
13905 begin
13906 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13907
13908 Set_Etype (Def_Id, Base_Type (T));
13909 Set_Size_Info (Def_Id, (T));
13910 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13911
13912 -- Process the constraint
13913
13914 C := Constraint (S);
13915
13916 -- Digits constraint present
13917
13918 if Nkind (C) = N_Digits_Constraint then
13919
13920 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13921 Check_Restriction (No_Obsolescent_Features, C);
13922
13923 if Warn_On_Obsolescent_Feature then
13924 Error_Msg_N
13925 ("subtype digits constraint is an " &
13926 "obsolescent feature (RM J.3(8))?j?", C);
13927 end if;
13928
13929 D := Digits_Expression (C);
13930 Analyze_And_Resolve (D, Any_Integer);
13931 Check_Digits_Expression (D);
13932 Set_Digits_Value (Def_Id, Expr_Value (D));
13933
13934 -- Check that digits value is in range. Obviously we can do this
13935 -- at compile time, but it is strictly a runtime check, and of
13936 -- course there is an ACVC test that checks this.
13937
13938 if Digits_Value (Def_Id) > Digits_Value (T) then
13939 Error_Msg_Uint_1 := Digits_Value (T);
13940 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13941 Rais :=
13942 Make_Raise_Constraint_Error (Sloc (D),
13943 Reason => CE_Range_Check_Failed);
13944 Insert_Action (Declaration_Node (Def_Id), Rais);
13945 end if;
13946
13947 C := Range_Constraint (C);
13948
13949 -- No digits constraint present
13950
13951 else
13952 Set_Digits_Value (Def_Id, Digits_Value (T));
13953 end if;
13954
13955 -- Range constraint present
13956
13957 if Nkind (C) = N_Range_Constraint then
13958 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13959
13960 -- No range constraint present
13961
13962 else
13963 pragma Assert (No (C));
13964 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13965 end if;
13966
13967 Set_Is_Constrained (Def_Id);
13968 end Constrain_Float;
13969
13970 ---------------------
13971 -- Constrain_Index --
13972 ---------------------
13973
13974 procedure Constrain_Index
13975 (Index : Node_Id;
13976 S : Node_Id;
13977 Related_Nod : Node_Id;
13978 Related_Id : Entity_Id;
13979 Suffix : Character;
13980 Suffix_Index : Nat)
13981 is
13982 Def_Id : Entity_Id;
13983 R : Node_Id := Empty;
13984 T : constant Entity_Id := Etype (Index);
13985
13986 begin
13987 Def_Id :=
13988 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13989 Set_Etype (Def_Id, Base_Type (T));
13990
13991 if Nkind (S) = N_Range
13992 or else
13993 (Nkind (S) = N_Attribute_Reference
13994 and then Attribute_Name (S) = Name_Range)
13995 then
13996 -- A Range attribute will be transformed into N_Range by Resolve
13997
13998 Analyze (S);
13999 Set_Etype (S, T);
14000 R := S;
14001
14002 Process_Range_Expr_In_Decl (R, T);
14003
14004 if not Error_Posted (S)
14005 and then
14006 (Nkind (S) /= N_Range
14007 or else not Covers (T, (Etype (Low_Bound (S))))
14008 or else not Covers (T, (Etype (High_Bound (S)))))
14009 then
14010 if Base_Type (T) /= Any_Type
14011 and then Etype (Low_Bound (S)) /= Any_Type
14012 and then Etype (High_Bound (S)) /= Any_Type
14013 then
14014 Error_Msg_N ("range expected", S);
14015 end if;
14016 end if;
14017
14018 elsif Nkind (S) = N_Subtype_Indication then
14019
14020 -- The parser has verified that this is a discrete indication
14021
14022 Resolve_Discrete_Subtype_Indication (S, T);
14023 Bad_Predicated_Subtype_Use
14024 ("subtype& has predicate, not allowed in index constraint",
14025 S, Entity (Subtype_Mark (S)));
14026
14027 R := Range_Expression (Constraint (S));
14028
14029 -- Capture values of bounds and generate temporaries for them if
14030 -- needed, since checks may cause duplication of the expressions
14031 -- which must not be reevaluated.
14032
14033 -- The forced evaluation removes side effects from expressions, which
14034 -- should occur also in GNATprove mode. Otherwise, we end up with
14035 -- unexpected insertions of actions at places where this is not
14036 -- supposed to occur, e.g. on default parameters of a call.
14037
14038 if Expander_Active or GNATprove_Mode then
14039 Force_Evaluation
14040 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14041 Force_Evaluation
14042 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14043 end if;
14044
14045 elsif Nkind (S) = N_Discriminant_Association then
14046
14047 -- Syntactically valid in subtype indication
14048
14049 Error_Msg_N ("invalid index constraint", S);
14050 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14051 return;
14052
14053 -- Subtype_Mark case, no anonymous subtypes to construct
14054
14055 else
14056 Analyze (S);
14057
14058 if Is_Entity_Name (S) then
14059 if not Is_Type (Entity (S)) then
14060 Error_Msg_N ("expect subtype mark for index constraint", S);
14061
14062 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14063 Wrong_Type (S, Base_Type (T));
14064
14065 -- Check error of subtype with predicate in index constraint
14066
14067 else
14068 Bad_Predicated_Subtype_Use
14069 ("subtype& has predicate, not allowed in index constraint",
14070 S, Entity (S));
14071 end if;
14072
14073 return;
14074
14075 else
14076 Error_Msg_N ("invalid index constraint", S);
14077 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14078 return;
14079 end if;
14080 end if;
14081
14082 -- Complete construction of the Itype
14083
14084 if Is_Modular_Integer_Type (T) then
14085 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14086
14087 elsif Is_Integer_Type (T) then
14088 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14089
14090 else
14091 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14092 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14093 Set_First_Literal (Def_Id, First_Literal (T));
14094 end if;
14095
14096 Set_Size_Info (Def_Id, (T));
14097 Set_RM_Size (Def_Id, RM_Size (T));
14098 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14099
14100 Set_Scalar_Range (Def_Id, R);
14101
14102 Set_Etype (S, Def_Id);
14103 Set_Discrete_RM_Size (Def_Id);
14104 end Constrain_Index;
14105
14106 -----------------------
14107 -- Constrain_Integer --
14108 -----------------------
14109
14110 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14111 T : constant Entity_Id := Entity (Subtype_Mark (S));
14112 C : constant Node_Id := Constraint (S);
14113
14114 begin
14115 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14116
14117 if Is_Modular_Integer_Type (T) then
14118 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14119 else
14120 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14121 end if;
14122
14123 Set_Etype (Def_Id, Base_Type (T));
14124 Set_Size_Info (Def_Id, (T));
14125 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14126 Set_Discrete_RM_Size (Def_Id);
14127 end Constrain_Integer;
14128
14129 ------------------------------
14130 -- Constrain_Ordinary_Fixed --
14131 ------------------------------
14132
14133 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14134 T : constant Entity_Id := Entity (Subtype_Mark (S));
14135 C : Node_Id;
14136 D : Node_Id;
14137 Rais : Node_Id;
14138
14139 begin
14140 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14141 Set_Etype (Def_Id, Base_Type (T));
14142 Set_Size_Info (Def_Id, (T));
14143 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14144 Set_Small_Value (Def_Id, Small_Value (T));
14145
14146 -- Process the constraint
14147
14148 C := Constraint (S);
14149
14150 -- Delta constraint present
14151
14152 if Nkind (C) = N_Delta_Constraint then
14153
14154 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14155 Check_Restriction (No_Obsolescent_Features, C);
14156
14157 if Warn_On_Obsolescent_Feature then
14158 Error_Msg_S
14159 ("subtype delta constraint is an " &
14160 "obsolescent feature (RM J.3(7))?j?");
14161 end if;
14162
14163 D := Delta_Expression (C);
14164 Analyze_And_Resolve (D, Any_Real);
14165 Check_Delta_Expression (D);
14166 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14167
14168 -- Check that delta value is in range. Obviously we can do this
14169 -- at compile time, but it is strictly a runtime check, and of
14170 -- course there is an ACVC test that checks this.
14171
14172 if Delta_Value (Def_Id) < Delta_Value (T) then
14173 Error_Msg_N ("??delta value is too small", D);
14174 Rais :=
14175 Make_Raise_Constraint_Error (Sloc (D),
14176 Reason => CE_Range_Check_Failed);
14177 Insert_Action (Declaration_Node (Def_Id), Rais);
14178 end if;
14179
14180 C := Range_Constraint (C);
14181
14182 -- No delta constraint present
14183
14184 else
14185 Set_Delta_Value (Def_Id, Delta_Value (T));
14186 end if;
14187
14188 -- Range constraint present
14189
14190 if Nkind (C) = N_Range_Constraint then
14191 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14192
14193 -- No range constraint present
14194
14195 else
14196 pragma Assert (No (C));
14197 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14198 end if;
14199
14200 Set_Discrete_RM_Size (Def_Id);
14201
14202 -- Unconditionally delay the freeze, since we cannot set size
14203 -- information in all cases correctly until the freeze point.
14204
14205 Set_Has_Delayed_Freeze (Def_Id);
14206 end Constrain_Ordinary_Fixed;
14207
14208 -----------------------
14209 -- Contain_Interface --
14210 -----------------------
14211
14212 function Contain_Interface
14213 (Iface : Entity_Id;
14214 Ifaces : Elist_Id) return Boolean
14215 is
14216 Iface_Elmt : Elmt_Id;
14217
14218 begin
14219 if Present (Ifaces) then
14220 Iface_Elmt := First_Elmt (Ifaces);
14221 while Present (Iface_Elmt) loop
14222 if Node (Iface_Elmt) = Iface then
14223 return True;
14224 end if;
14225
14226 Next_Elmt (Iface_Elmt);
14227 end loop;
14228 end if;
14229
14230 return False;
14231 end Contain_Interface;
14232
14233 ---------------------------
14234 -- Convert_Scalar_Bounds --
14235 ---------------------------
14236
14237 procedure Convert_Scalar_Bounds
14238 (N : Node_Id;
14239 Parent_Type : Entity_Id;
14240 Derived_Type : Entity_Id;
14241 Loc : Source_Ptr)
14242 is
14243 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14244
14245 Lo : Node_Id;
14246 Hi : Node_Id;
14247 Rng : Node_Id;
14248
14249 begin
14250 -- Defend against previous errors
14251
14252 if No (Scalar_Range (Derived_Type)) then
14253 Check_Error_Detected;
14254 return;
14255 end if;
14256
14257 Lo := Build_Scalar_Bound
14258 (Type_Low_Bound (Derived_Type),
14259 Parent_Type, Implicit_Base);
14260
14261 Hi := Build_Scalar_Bound
14262 (Type_High_Bound (Derived_Type),
14263 Parent_Type, Implicit_Base);
14264
14265 Rng :=
14266 Make_Range (Loc,
14267 Low_Bound => Lo,
14268 High_Bound => Hi);
14269
14270 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14271
14272 Set_Parent (Rng, N);
14273 Set_Scalar_Range (Derived_Type, Rng);
14274
14275 -- Analyze the bounds
14276
14277 Analyze_And_Resolve (Lo, Implicit_Base);
14278 Analyze_And_Resolve (Hi, Implicit_Base);
14279
14280 -- Analyze the range itself, except that we do not analyze it if
14281 -- the bounds are real literals, and we have a fixed-point type.
14282 -- The reason for this is that we delay setting the bounds in this
14283 -- case till we know the final Small and Size values (see circuit
14284 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14285
14286 if Is_Fixed_Point_Type (Parent_Type)
14287 and then Nkind (Lo) = N_Real_Literal
14288 and then Nkind (Hi) = N_Real_Literal
14289 then
14290 return;
14291
14292 -- Here we do the analysis of the range
14293
14294 -- Note: we do this manually, since if we do a normal Analyze and
14295 -- Resolve call, there are problems with the conversions used for
14296 -- the derived type range.
14297
14298 else
14299 Set_Etype (Rng, Implicit_Base);
14300 Set_Analyzed (Rng, True);
14301 end if;
14302 end Convert_Scalar_Bounds;
14303
14304 -------------------
14305 -- Copy_And_Swap --
14306 -------------------
14307
14308 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14309 begin
14310 -- Initialize new full declaration entity by copying the pertinent
14311 -- fields of the corresponding private declaration entity.
14312
14313 -- We temporarily set Ekind to a value appropriate for a type to
14314 -- avoid assert failures in Einfo from checking for setting type
14315 -- attributes on something that is not a type. Ekind (Priv) is an
14316 -- appropriate choice, since it allowed the attributes to be set
14317 -- in the first place. This Ekind value will be modified later.
14318
14319 Set_Ekind (Full, Ekind (Priv));
14320
14321 -- Also set Etype temporarily to Any_Type, again, in the absence
14322 -- of errors, it will be properly reset, and if there are errors,
14323 -- then we want a value of Any_Type to remain.
14324
14325 Set_Etype (Full, Any_Type);
14326
14327 -- Now start copying attributes
14328
14329 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14330
14331 if Has_Discriminants (Full) then
14332 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14333 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14334 end if;
14335
14336 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14337 Set_Homonym (Full, Homonym (Priv));
14338 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14339 Set_Is_Public (Full, Is_Public (Priv));
14340 Set_Is_Pure (Full, Is_Pure (Priv));
14341 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14342 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14343 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14344 Set_Has_Pragma_Unreferenced_Objects
14345 (Full, Has_Pragma_Unreferenced_Objects
14346 (Priv));
14347
14348 Conditional_Delay (Full, Priv);
14349
14350 if Is_Tagged_Type (Full) then
14351 Set_Direct_Primitive_Operations
14352 (Full, Direct_Primitive_Operations (Priv));
14353 Set_No_Tagged_Streams_Pragma
14354 (Full, No_Tagged_Streams_Pragma (Priv));
14355
14356 if Is_Base_Type (Priv) then
14357 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14358 end if;
14359 end if;
14360
14361 Set_Is_Volatile (Full, Is_Volatile (Priv));
14362 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14363 Set_Scope (Full, Scope (Priv));
14364 Set_Next_Entity (Full, Next_Entity (Priv));
14365 Set_First_Entity (Full, First_Entity (Priv));
14366 Set_Last_Entity (Full, Last_Entity (Priv));
14367
14368 -- If access types have been recorded for later handling, keep them in
14369 -- the full view so that they get handled when the full view freeze
14370 -- node is expanded.
14371
14372 if Present (Freeze_Node (Priv))
14373 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14374 then
14375 Ensure_Freeze_Node (Full);
14376 Set_Access_Types_To_Process
14377 (Freeze_Node (Full),
14378 Access_Types_To_Process (Freeze_Node (Priv)));
14379 end if;
14380
14381 -- Swap the two entities. Now Private is the full type entity and Full
14382 -- is the private one. They will be swapped back at the end of the
14383 -- private part. This swapping ensures that the entity that is visible
14384 -- in the private part is the full declaration.
14385
14386 Exchange_Entities (Priv, Full);
14387 Append_Entity (Full, Scope (Full));
14388 end Copy_And_Swap;
14389
14390 -------------------------------------
14391 -- Copy_Array_Base_Type_Attributes --
14392 -------------------------------------
14393
14394 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14395 begin
14396 Set_Component_Alignment (T1, Component_Alignment (T2));
14397 Set_Component_Type (T1, Component_Type (T2));
14398 Set_Component_Size (T1, Component_Size (T2));
14399 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14400 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14401 Propagate_Concurrent_Flags (T1, T2);
14402 Set_Is_Packed (T1, Is_Packed (T2));
14403 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14404 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14405 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14406 end Copy_Array_Base_Type_Attributes;
14407
14408 -----------------------------------
14409 -- Copy_Array_Subtype_Attributes --
14410 -----------------------------------
14411
14412 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14413 begin
14414 Set_Size_Info (T1, T2);
14415
14416 Set_First_Index (T1, First_Index (T2));
14417 Set_Is_Aliased (T1, Is_Aliased (T2));
14418 Set_Is_Volatile (T1, Is_Volatile (T2));
14419 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14420 Set_Is_Constrained (T1, Is_Constrained (T2));
14421 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14422 Inherit_Rep_Item_Chain (T1, T2);
14423 Set_Convention (T1, Convention (T2));
14424 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14425 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14426 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14427 end Copy_Array_Subtype_Attributes;
14428
14429 -----------------------------------
14430 -- Create_Constrained_Components --
14431 -----------------------------------
14432
14433 procedure Create_Constrained_Components
14434 (Subt : Entity_Id;
14435 Decl_Node : Node_Id;
14436 Typ : Entity_Id;
14437 Constraints : Elist_Id)
14438 is
14439 Loc : constant Source_Ptr := Sloc (Subt);
14440 Comp_List : constant Elist_Id := New_Elmt_List;
14441 Parent_Type : constant Entity_Id := Etype (Typ);
14442 Assoc_List : constant List_Id := New_List;
14443 Discr_Val : Elmt_Id;
14444 Errors : Boolean;
14445 New_C : Entity_Id;
14446 Old_C : Entity_Id;
14447 Is_Static : Boolean := True;
14448
14449 procedure Collect_Fixed_Components (Typ : Entity_Id);
14450 -- Collect parent type components that do not appear in a variant part
14451
14452 procedure Create_All_Components;
14453 -- Iterate over Comp_List to create the components of the subtype
14454
14455 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14456 -- Creates a new component from Old_Compon, copying all the fields from
14457 -- it, including its Etype, inserts the new component in the Subt entity
14458 -- chain and returns the new component.
14459
14460 function Is_Variant_Record (T : Entity_Id) return Boolean;
14461 -- If true, and discriminants are static, collect only components from
14462 -- variants selected by discriminant values.
14463
14464 ------------------------------
14465 -- Collect_Fixed_Components --
14466 ------------------------------
14467
14468 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14469 begin
14470 -- Build association list for discriminants, and find components of the
14471 -- variant part selected by the values of the discriminants.
14472
14473 Old_C := First_Discriminant (Typ);
14474 Discr_Val := First_Elmt (Constraints);
14475 while Present (Old_C) loop
14476 Append_To (Assoc_List,
14477 Make_Component_Association (Loc,
14478 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14479 Expression => New_Copy (Node (Discr_Val))));
14480
14481 Next_Elmt (Discr_Val);
14482 Next_Discriminant (Old_C);
14483 end loop;
14484
14485 -- The tag and the possible parent component are unconditionally in
14486 -- the subtype.
14487
14488 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14489 Old_C := First_Component (Typ);
14490 while Present (Old_C) loop
14491 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14492 Append_Elmt (Old_C, Comp_List);
14493 end if;
14494
14495 Next_Component (Old_C);
14496 end loop;
14497 end if;
14498 end Collect_Fixed_Components;
14499
14500 ---------------------------
14501 -- Create_All_Components --
14502 ---------------------------
14503
14504 procedure Create_All_Components is
14505 Comp : Elmt_Id;
14506
14507 begin
14508 Comp := First_Elmt (Comp_List);
14509 while Present (Comp) loop
14510 Old_C := Node (Comp);
14511 New_C := Create_Component (Old_C);
14512
14513 Set_Etype
14514 (New_C,
14515 Constrain_Component_Type
14516 (Old_C, Subt, Decl_Node, Typ, Constraints));
14517 Set_Is_Public (New_C, Is_Public (Subt));
14518
14519 Next_Elmt (Comp);
14520 end loop;
14521 end Create_All_Components;
14522
14523 ----------------------
14524 -- Create_Component --
14525 ----------------------
14526
14527 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14528 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14529
14530 begin
14531 if Ekind (Old_Compon) = E_Discriminant
14532 and then Is_Completely_Hidden (Old_Compon)
14533 then
14534 -- This is a shadow discriminant created for a discriminant of
14535 -- the parent type, which needs to be present in the subtype.
14536 -- Give the shadow discriminant an internal name that cannot
14537 -- conflict with that of visible components.
14538
14539 Set_Chars (New_Compon, New_Internal_Name ('C'));
14540 end if;
14541
14542 -- Set the parent so we have a proper link for freezing etc. This is
14543 -- not a real parent pointer, since of course our parent does not own
14544 -- up to us and reference us, we are an illegitimate child of the
14545 -- original parent.
14546
14547 Set_Parent (New_Compon, Parent (Old_Compon));
14548
14549 -- We do not want this node marked as Comes_From_Source, since
14550 -- otherwise it would get first class status and a separate cross-
14551 -- reference line would be generated. Illegitimate children do not
14552 -- rate such recognition.
14553
14554 Set_Comes_From_Source (New_Compon, False);
14555
14556 -- But it is a real entity, and a birth certificate must be properly
14557 -- registered by entering it into the entity list.
14558
14559 Enter_Name (New_Compon);
14560
14561 return New_Compon;
14562 end Create_Component;
14563
14564 -----------------------
14565 -- Is_Variant_Record --
14566 -----------------------
14567
14568 function Is_Variant_Record (T : Entity_Id) return Boolean is
14569 begin
14570 return Nkind (Parent (T)) = N_Full_Type_Declaration
14571 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14572 and then Present (Component_List (Type_Definition (Parent (T))))
14573 and then
14574 Present
14575 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14576 end Is_Variant_Record;
14577
14578 -- Start of processing for Create_Constrained_Components
14579
14580 begin
14581 pragma Assert (Subt /= Base_Type (Subt));
14582 pragma Assert (Typ = Base_Type (Typ));
14583
14584 Set_First_Entity (Subt, Empty);
14585 Set_Last_Entity (Subt, Empty);
14586
14587 -- Check whether constraint is fully static, in which case we can
14588 -- optimize the list of components.
14589
14590 Discr_Val := First_Elmt (Constraints);
14591 while Present (Discr_Val) loop
14592 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14593 Is_Static := False;
14594 exit;
14595 end if;
14596
14597 Next_Elmt (Discr_Val);
14598 end loop;
14599
14600 Set_Has_Static_Discriminants (Subt, Is_Static);
14601
14602 Push_Scope (Subt);
14603
14604 -- Inherit the discriminants of the parent type
14605
14606 Add_Discriminants : declare
14607 Num_Disc : Nat;
14608 Num_Gird : Nat;
14609
14610 begin
14611 Num_Disc := 0;
14612 Old_C := First_Discriminant (Typ);
14613
14614 while Present (Old_C) loop
14615 Num_Disc := Num_Disc + 1;
14616 New_C := Create_Component (Old_C);
14617 Set_Is_Public (New_C, Is_Public (Subt));
14618 Next_Discriminant (Old_C);
14619 end loop;
14620
14621 -- For an untagged derived subtype, the number of discriminants may
14622 -- be smaller than the number of inherited discriminants, because
14623 -- several of them may be renamed by a single new discriminant or
14624 -- constrained. In this case, add the hidden discriminants back into
14625 -- the subtype, because they need to be present if the optimizer of
14626 -- the GCC 4.x back-end decides to break apart assignments between
14627 -- objects using the parent view into member-wise assignments.
14628
14629 Num_Gird := 0;
14630
14631 if Is_Derived_Type (Typ)
14632 and then not Is_Tagged_Type (Typ)
14633 then
14634 Old_C := First_Stored_Discriminant (Typ);
14635
14636 while Present (Old_C) loop
14637 Num_Gird := Num_Gird + 1;
14638 Next_Stored_Discriminant (Old_C);
14639 end loop;
14640 end if;
14641
14642 if Num_Gird > Num_Disc then
14643
14644 -- Find out multiple uses of new discriminants, and add hidden
14645 -- components for the extra renamed discriminants. We recognize
14646 -- multiple uses through the Corresponding_Discriminant of a
14647 -- new discriminant: if it constrains several old discriminants,
14648 -- this field points to the last one in the parent type. The
14649 -- stored discriminants of the derived type have the same name
14650 -- as those of the parent.
14651
14652 declare
14653 Constr : Elmt_Id;
14654 New_Discr : Entity_Id;
14655 Old_Discr : Entity_Id;
14656
14657 begin
14658 Constr := First_Elmt (Stored_Constraint (Typ));
14659 Old_Discr := First_Stored_Discriminant (Typ);
14660 while Present (Constr) loop
14661 if Is_Entity_Name (Node (Constr))
14662 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14663 then
14664 New_Discr := Entity (Node (Constr));
14665
14666 if Chars (Corresponding_Discriminant (New_Discr)) /=
14667 Chars (Old_Discr)
14668 then
14669 -- The new discriminant has been used to rename a
14670 -- subsequent old discriminant. Introduce a shadow
14671 -- component for the current old discriminant.
14672
14673 New_C := Create_Component (Old_Discr);
14674 Set_Original_Record_Component (New_C, Old_Discr);
14675 end if;
14676
14677 else
14678 -- The constraint has eliminated the old discriminant.
14679 -- Introduce a shadow component.
14680
14681 New_C := Create_Component (Old_Discr);
14682 Set_Original_Record_Component (New_C, Old_Discr);
14683 end if;
14684
14685 Next_Elmt (Constr);
14686 Next_Stored_Discriminant (Old_Discr);
14687 end loop;
14688 end;
14689 end if;
14690 end Add_Discriminants;
14691
14692 if Is_Static
14693 and then Is_Variant_Record (Typ)
14694 then
14695 Collect_Fixed_Components (Typ);
14696
14697 Gather_Components (
14698 Typ,
14699 Component_List (Type_Definition (Parent (Typ))),
14700 Governed_By => Assoc_List,
14701 Into => Comp_List,
14702 Report_Errors => Errors);
14703 pragma Assert (not Errors
14704 or else Serious_Errors_Detected > 0);
14705
14706 Create_All_Components;
14707
14708 -- If the subtype declaration is created for a tagged type derivation
14709 -- with constraints, we retrieve the record definition of the parent
14710 -- type to select the components of the proper variant.
14711
14712 elsif Is_Static
14713 and then Is_Tagged_Type (Typ)
14714 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14715 and then
14716 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14717 and then Is_Variant_Record (Parent_Type)
14718 then
14719 Collect_Fixed_Components (Typ);
14720
14721 Gather_Components
14722 (Typ,
14723 Component_List (Type_Definition (Parent (Parent_Type))),
14724 Governed_By => Assoc_List,
14725 Into => Comp_List,
14726 Report_Errors => Errors);
14727
14728 -- Note: previously there was a check at this point that no errors
14729 -- were detected. As a consequence of AI05-220 there may be an error
14730 -- if an inherited discriminant that controls a variant has a non-
14731 -- static constraint.
14732
14733 -- If the tagged derivation has a type extension, collect all the
14734 -- new components therein.
14735
14736 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14737 then
14738 Old_C := First_Component (Typ);
14739 while Present (Old_C) loop
14740 if Original_Record_Component (Old_C) = Old_C
14741 and then Chars (Old_C) /= Name_uTag
14742 and then Chars (Old_C) /= Name_uParent
14743 then
14744 Append_Elmt (Old_C, Comp_List);
14745 end if;
14746
14747 Next_Component (Old_C);
14748 end loop;
14749 end if;
14750
14751 Create_All_Components;
14752
14753 else
14754 -- If discriminants are not static, or if this is a multi-level type
14755 -- extension, we have to include all components of the parent type.
14756
14757 Old_C := First_Component (Typ);
14758 while Present (Old_C) loop
14759 New_C := Create_Component (Old_C);
14760
14761 Set_Etype
14762 (New_C,
14763 Constrain_Component_Type
14764 (Old_C, Subt, Decl_Node, Typ, Constraints));
14765 Set_Is_Public (New_C, Is_Public (Subt));
14766
14767 Next_Component (Old_C);
14768 end loop;
14769 end if;
14770
14771 End_Scope;
14772 end Create_Constrained_Components;
14773
14774 ------------------------------------------
14775 -- Decimal_Fixed_Point_Type_Declaration --
14776 ------------------------------------------
14777
14778 procedure Decimal_Fixed_Point_Type_Declaration
14779 (T : Entity_Id;
14780 Def : Node_Id)
14781 is
14782 Loc : constant Source_Ptr := Sloc (Def);
14783 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14784 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14785 Implicit_Base : Entity_Id;
14786 Digs_Val : Uint;
14787 Delta_Val : Ureal;
14788 Scale_Val : Uint;
14789 Bound_Val : Ureal;
14790
14791 begin
14792 Check_SPARK_05_Restriction
14793 ("decimal fixed point type is not allowed", Def);
14794 Check_Restriction (No_Fixed_Point, Def);
14795
14796 -- Create implicit base type
14797
14798 Implicit_Base :=
14799 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14800 Set_Etype (Implicit_Base, Implicit_Base);
14801
14802 -- Analyze and process delta expression
14803
14804 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14805
14806 Check_Delta_Expression (Delta_Expr);
14807 Delta_Val := Expr_Value_R (Delta_Expr);
14808
14809 -- Check delta is power of 10, and determine scale value from it
14810
14811 declare
14812 Val : Ureal;
14813
14814 begin
14815 Scale_Val := Uint_0;
14816 Val := Delta_Val;
14817
14818 if Val < Ureal_1 then
14819 while Val < Ureal_1 loop
14820 Val := Val * Ureal_10;
14821 Scale_Val := Scale_Val + 1;
14822 end loop;
14823
14824 if Scale_Val > 18 then
14825 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14826 Scale_Val := UI_From_Int (+18);
14827 end if;
14828
14829 else
14830 while Val > Ureal_1 loop
14831 Val := Val / Ureal_10;
14832 Scale_Val := Scale_Val - 1;
14833 end loop;
14834
14835 if Scale_Val < -18 then
14836 Error_Msg_N ("scale is less than minimum value of -18", Def);
14837 Scale_Val := UI_From_Int (-18);
14838 end if;
14839 end if;
14840
14841 if Val /= Ureal_1 then
14842 Error_Msg_N ("delta expression must be a power of 10", Def);
14843 Delta_Val := Ureal_10 ** (-Scale_Val);
14844 end if;
14845 end;
14846
14847 -- Set delta, scale and small (small = delta for decimal type)
14848
14849 Set_Delta_Value (Implicit_Base, Delta_Val);
14850 Set_Scale_Value (Implicit_Base, Scale_Val);
14851 Set_Small_Value (Implicit_Base, Delta_Val);
14852
14853 -- Analyze and process digits expression
14854
14855 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14856 Check_Digits_Expression (Digs_Expr);
14857 Digs_Val := Expr_Value (Digs_Expr);
14858
14859 if Digs_Val > 18 then
14860 Digs_Val := UI_From_Int (+18);
14861 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14862 end if;
14863
14864 Set_Digits_Value (Implicit_Base, Digs_Val);
14865 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14866
14867 -- Set range of base type from digits value for now. This will be
14868 -- expanded to represent the true underlying base range by Freeze.
14869
14870 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14871
14872 -- Note: We leave size as zero for now, size will be set at freeze
14873 -- time. We have to do this for ordinary fixed-point, because the size
14874 -- depends on the specified small, and we might as well do the same for
14875 -- decimal fixed-point.
14876
14877 pragma Assert (Esize (Implicit_Base) = Uint_0);
14878
14879 -- If there are bounds given in the declaration use them as the
14880 -- bounds of the first named subtype.
14881
14882 if Present (Real_Range_Specification (Def)) then
14883 declare
14884 RRS : constant Node_Id := Real_Range_Specification (Def);
14885 Low : constant Node_Id := Low_Bound (RRS);
14886 High : constant Node_Id := High_Bound (RRS);
14887 Low_Val : Ureal;
14888 High_Val : Ureal;
14889
14890 begin
14891 Analyze_And_Resolve (Low, Any_Real);
14892 Analyze_And_Resolve (High, Any_Real);
14893 Check_Real_Bound (Low);
14894 Check_Real_Bound (High);
14895 Low_Val := Expr_Value_R (Low);
14896 High_Val := Expr_Value_R (High);
14897
14898 if Low_Val < (-Bound_Val) then
14899 Error_Msg_N
14900 ("range low bound too small for digits value", Low);
14901 Low_Val := -Bound_Val;
14902 end if;
14903
14904 if High_Val > Bound_Val then
14905 Error_Msg_N
14906 ("range high bound too large for digits value", High);
14907 High_Val := Bound_Val;
14908 end if;
14909
14910 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14911 end;
14912
14913 -- If no explicit range, use range that corresponds to given
14914 -- digits value. This will end up as the final range for the
14915 -- first subtype.
14916
14917 else
14918 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14919 end if;
14920
14921 -- Complete entity for first subtype. The inheritance of the rep item
14922 -- chain ensures that SPARK-related pragmas are not clobbered when the
14923 -- decimal fixed point type acts as a full view of a private type.
14924
14925 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14926 Set_Etype (T, Implicit_Base);
14927 Set_Size_Info (T, Implicit_Base);
14928 Inherit_Rep_Item_Chain (T, Implicit_Base);
14929 Set_Digits_Value (T, Digs_Val);
14930 Set_Delta_Value (T, Delta_Val);
14931 Set_Small_Value (T, Delta_Val);
14932 Set_Scale_Value (T, Scale_Val);
14933 Set_Is_Constrained (T);
14934 end Decimal_Fixed_Point_Type_Declaration;
14935
14936 -----------------------------------
14937 -- Derive_Progenitor_Subprograms --
14938 -----------------------------------
14939
14940 procedure Derive_Progenitor_Subprograms
14941 (Parent_Type : Entity_Id;
14942 Tagged_Type : Entity_Id)
14943 is
14944 E : Entity_Id;
14945 Elmt : Elmt_Id;
14946 Iface : Entity_Id;
14947 Iface_Elmt : Elmt_Id;
14948 Iface_Subp : Entity_Id;
14949 New_Subp : Entity_Id := Empty;
14950 Prim_Elmt : Elmt_Id;
14951 Subp : Entity_Id;
14952 Typ : Entity_Id;
14953
14954 begin
14955 pragma Assert (Ada_Version >= Ada_2005
14956 and then Is_Record_Type (Tagged_Type)
14957 and then Is_Tagged_Type (Tagged_Type)
14958 and then Has_Interfaces (Tagged_Type));
14959
14960 -- Step 1: Transfer to the full-view primitives associated with the
14961 -- partial-view that cover interface primitives. Conceptually this
14962 -- work should be done later by Process_Full_View; done here to
14963 -- simplify its implementation at later stages. It can be safely
14964 -- done here because interfaces must be visible in the partial and
14965 -- private view (RM 7.3(7.3/2)).
14966
14967 -- Small optimization: This work is only required if the parent may
14968 -- have entities whose Alias attribute reference an interface primitive.
14969 -- Such a situation may occur if the parent is an abstract type and the
14970 -- primitive has not been yet overridden or if the parent is a generic
14971 -- formal type covering interfaces.
14972
14973 -- If the tagged type is not abstract, it cannot have abstract
14974 -- primitives (the only entities in the list of primitives of
14975 -- non-abstract tagged types that can reference abstract primitives
14976 -- through its Alias attribute are the internal entities that have
14977 -- attribute Interface_Alias, and these entities are generated later
14978 -- by Add_Internal_Interface_Entities).
14979
14980 if In_Private_Part (Current_Scope)
14981 and then (Is_Abstract_Type (Parent_Type)
14982 or else
14983 Is_Generic_Type (Parent_Type))
14984 then
14985 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14986 while Present (Elmt) loop
14987 Subp := Node (Elmt);
14988
14989 -- At this stage it is not possible to have entities in the list
14990 -- of primitives that have attribute Interface_Alias.
14991
14992 pragma Assert (No (Interface_Alias (Subp)));
14993
14994 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14995
14996 if Is_Interface (Typ) then
14997 E := Find_Primitive_Covering_Interface
14998 (Tagged_Type => Tagged_Type,
14999 Iface_Prim => Subp);
15000
15001 if Present (E)
15002 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15003 then
15004 Replace_Elmt (Elmt, E);
15005 Remove_Homonym (Subp);
15006 end if;
15007 end if;
15008
15009 Next_Elmt (Elmt);
15010 end loop;
15011 end if;
15012
15013 -- Step 2: Add primitives of progenitors that are not implemented by
15014 -- parents of Tagged_Type.
15015
15016 if Present (Interfaces (Base_Type (Tagged_Type))) then
15017 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15018 while Present (Iface_Elmt) loop
15019 Iface := Node (Iface_Elmt);
15020
15021 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15022 while Present (Prim_Elmt) loop
15023 Iface_Subp := Node (Prim_Elmt);
15024
15025 -- Exclude derivation of predefined primitives except those
15026 -- that come from source, or are inherited from one that comes
15027 -- from source. Required to catch declarations of equality
15028 -- operators of interfaces. For example:
15029
15030 -- type Iface is interface;
15031 -- function "=" (Left, Right : Iface) return Boolean;
15032
15033 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15034 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
15035 then
15036 E := Find_Primitive_Covering_Interface
15037 (Tagged_Type => Tagged_Type,
15038 Iface_Prim => Iface_Subp);
15039
15040 -- If not found we derive a new primitive leaving its alias
15041 -- attribute referencing the interface primitive.
15042
15043 if No (E) then
15044 Derive_Subprogram
15045 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15046
15047 -- Ada 2012 (AI05-0197): If the covering primitive's name
15048 -- differs from the name of the interface primitive then it
15049 -- is a private primitive inherited from a parent type. In
15050 -- such case, given that Tagged_Type covers the interface,
15051 -- the inherited private primitive becomes visible. For such
15052 -- purpose we add a new entity that renames the inherited
15053 -- private primitive.
15054
15055 elsif Chars (E) /= Chars (Iface_Subp) then
15056 pragma Assert (Has_Suffix (E, 'P'));
15057 Derive_Subprogram
15058 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15059 Set_Alias (New_Subp, E);
15060 Set_Is_Abstract_Subprogram (New_Subp,
15061 Is_Abstract_Subprogram (E));
15062
15063 -- Propagate to the full view interface entities associated
15064 -- with the partial view.
15065
15066 elsif In_Private_Part (Current_Scope)
15067 and then Present (Alias (E))
15068 and then Alias (E) = Iface_Subp
15069 and then
15070 List_Containing (Parent (E)) /=
15071 Private_Declarations
15072 (Specification
15073 (Unit_Declaration_Node (Current_Scope)))
15074 then
15075 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15076 end if;
15077 end if;
15078
15079 Next_Elmt (Prim_Elmt);
15080 end loop;
15081
15082 Next_Elmt (Iface_Elmt);
15083 end loop;
15084 end if;
15085 end Derive_Progenitor_Subprograms;
15086
15087 -----------------------
15088 -- Derive_Subprogram --
15089 -----------------------
15090
15091 procedure Derive_Subprogram
15092 (New_Subp : out Entity_Id;
15093 Parent_Subp : Entity_Id;
15094 Derived_Type : Entity_Id;
15095 Parent_Type : Entity_Id;
15096 Actual_Subp : Entity_Id := Empty)
15097 is
15098 Formal : Entity_Id;
15099 -- Formal parameter of parent primitive operation
15100
15101 Formal_Of_Actual : Entity_Id;
15102 -- Formal parameter of actual operation, when the derivation is to
15103 -- create a renaming for a primitive operation of an actual in an
15104 -- instantiation.
15105
15106 New_Formal : Entity_Id;
15107 -- Formal of inherited operation
15108
15109 Visible_Subp : Entity_Id := Parent_Subp;
15110
15111 function Is_Private_Overriding return Boolean;
15112 -- If Subp is a private overriding of a visible operation, the inherited
15113 -- operation derives from the overridden op (even though its body is the
15114 -- overriding one) and the inherited operation is visible now. See
15115 -- sem_disp to see the full details of the handling of the overridden
15116 -- subprogram, which is removed from the list of primitive operations of
15117 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15118 -- and used to diagnose abstract operations that need overriding in the
15119 -- derived type.
15120
15121 procedure Replace_Type (Id, New_Id : Entity_Id);
15122 -- When the type is an anonymous access type, create a new access type
15123 -- designating the derived type.
15124
15125 procedure Set_Derived_Name;
15126 -- This procedure sets the appropriate Chars name for New_Subp. This
15127 -- is normally just a copy of the parent name. An exception arises for
15128 -- type support subprograms, where the name is changed to reflect the
15129 -- name of the derived type, e.g. if type foo is derived from type bar,
15130 -- then a procedure barDA is derived with a name fooDA.
15131
15132 ---------------------------
15133 -- Is_Private_Overriding --
15134 ---------------------------
15135
15136 function Is_Private_Overriding return Boolean is
15137 Prev : Entity_Id;
15138
15139 begin
15140 -- If the parent is not a dispatching operation there is no
15141 -- need to investigate overridings
15142
15143 if not Is_Dispatching_Operation (Parent_Subp) then
15144 return False;
15145 end if;
15146
15147 -- The visible operation that is overridden is a homonym of the
15148 -- parent subprogram. We scan the homonym chain to find the one
15149 -- whose alias is the subprogram we are deriving.
15150
15151 Prev := Current_Entity (Parent_Subp);
15152 while Present (Prev) loop
15153 if Ekind (Prev) = Ekind (Parent_Subp)
15154 and then Alias (Prev) = Parent_Subp
15155 and then Scope (Parent_Subp) = Scope (Prev)
15156 and then not Is_Hidden (Prev)
15157 then
15158 Visible_Subp := Prev;
15159 return True;
15160 end if;
15161
15162 Prev := Homonym (Prev);
15163 end loop;
15164
15165 return False;
15166 end Is_Private_Overriding;
15167
15168 ------------------
15169 -- Replace_Type --
15170 ------------------
15171
15172 procedure Replace_Type (Id, New_Id : Entity_Id) is
15173 Id_Type : constant Entity_Id := Etype (Id);
15174 Acc_Type : Entity_Id;
15175 Par : constant Node_Id := Parent (Derived_Type);
15176
15177 begin
15178 -- When the type is an anonymous access type, create a new access
15179 -- type designating the derived type. This itype must be elaborated
15180 -- at the point of the derivation, not on subsequent calls that may
15181 -- be out of the proper scope for Gigi, so we insert a reference to
15182 -- it after the derivation.
15183
15184 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15185 declare
15186 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15187
15188 begin
15189 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15190 and then Present (Full_View (Desig_Typ))
15191 and then not Is_Private_Type (Parent_Type)
15192 then
15193 Desig_Typ := Full_View (Desig_Typ);
15194 end if;
15195
15196 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15197
15198 -- Ada 2005 (AI-251): Handle also derivations of abstract
15199 -- interface primitives.
15200
15201 or else (Is_Interface (Desig_Typ)
15202 and then not Is_Class_Wide_Type (Desig_Typ))
15203 then
15204 Acc_Type := New_Copy (Id_Type);
15205 Set_Etype (Acc_Type, Acc_Type);
15206 Set_Scope (Acc_Type, New_Subp);
15207
15208 -- Set size of anonymous access type. If we have an access
15209 -- to an unconstrained array, this is a fat pointer, so it
15210 -- is sizes at twice addtress size.
15211
15212 if Is_Array_Type (Desig_Typ)
15213 and then not Is_Constrained (Desig_Typ)
15214 then
15215 Init_Size (Acc_Type, 2 * System_Address_Size);
15216
15217 -- Other cases use a thin pointer
15218
15219 else
15220 Init_Size (Acc_Type, System_Address_Size);
15221 end if;
15222
15223 -- Set remaining characterstics of anonymous access type
15224
15225 Init_Alignment (Acc_Type);
15226 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15227
15228 Set_Etype (New_Id, Acc_Type);
15229 Set_Scope (New_Id, New_Subp);
15230
15231 -- Create a reference to it
15232
15233 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15234
15235 else
15236 Set_Etype (New_Id, Id_Type);
15237 end if;
15238 end;
15239
15240 -- In Ada2012, a formal may have an incomplete type but the type
15241 -- derivation that inherits the primitive follows the full view.
15242
15243 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15244 or else
15245 (Ekind (Id_Type) = E_Record_Type_With_Private
15246 and then Present (Full_View (Id_Type))
15247 and then
15248 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15249 or else
15250 (Ada_Version >= Ada_2012
15251 and then Ekind (Id_Type) = E_Incomplete_Type
15252 and then Full_View (Id_Type) = Parent_Type)
15253 then
15254 -- Constraint checks on formals are generated during expansion,
15255 -- based on the signature of the original subprogram. The bounds
15256 -- of the derived type are not relevant, and thus we can use
15257 -- the base type for the formals. However, the return type may be
15258 -- used in a context that requires that the proper static bounds
15259 -- be used (a case statement, for example) and for those cases
15260 -- we must use the derived type (first subtype), not its base.
15261
15262 -- If the derived_type_definition has no constraints, we know that
15263 -- the derived type has the same constraints as the first subtype
15264 -- of the parent, and we can also use it rather than its base,
15265 -- which can lead to more efficient code.
15266
15267 if Etype (Id) = Parent_Type then
15268 if Is_Scalar_Type (Parent_Type)
15269 and then
15270 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15271 then
15272 Set_Etype (New_Id, Derived_Type);
15273
15274 elsif Nkind (Par) = N_Full_Type_Declaration
15275 and then
15276 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15277 and then
15278 Is_Entity_Name
15279 (Subtype_Indication (Type_Definition (Par)))
15280 then
15281 Set_Etype (New_Id, Derived_Type);
15282
15283 else
15284 Set_Etype (New_Id, Base_Type (Derived_Type));
15285 end if;
15286
15287 else
15288 Set_Etype (New_Id, Base_Type (Derived_Type));
15289 end if;
15290
15291 else
15292 Set_Etype (New_Id, Etype (Id));
15293 end if;
15294 end Replace_Type;
15295
15296 ----------------------
15297 -- Set_Derived_Name --
15298 ----------------------
15299
15300 procedure Set_Derived_Name is
15301 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15302 begin
15303 if Nm = TSS_Null then
15304 Set_Chars (New_Subp, Chars (Parent_Subp));
15305 else
15306 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15307 end if;
15308 end Set_Derived_Name;
15309
15310 -- Start of processing for Derive_Subprogram
15311
15312 begin
15313 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15314 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15315
15316 -- Check whether the inherited subprogram is a private operation that
15317 -- should be inherited but not yet made visible. Such subprograms can
15318 -- become visible at a later point (e.g., the private part of a public
15319 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15320 -- following predicate is true, then this is not such a private
15321 -- operation and the subprogram simply inherits the name of the parent
15322 -- subprogram. Note the special check for the names of controlled
15323 -- operations, which are currently exempted from being inherited with
15324 -- a hidden name because they must be findable for generation of
15325 -- implicit run-time calls.
15326
15327 if not Is_Hidden (Parent_Subp)
15328 or else Is_Internal (Parent_Subp)
15329 or else Is_Private_Overriding
15330 or else Is_Internal_Name (Chars (Parent_Subp))
15331 or else (Is_Controlled (Parent_Type)
15332 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15333 Name_Finalize,
15334 Name_Initialize))
15335 then
15336 Set_Derived_Name;
15337
15338 -- An inherited dispatching equality will be overridden by an internally
15339 -- generated one, or by an explicit one, so preserve its name and thus
15340 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15341 -- private operation it may become invisible if the full view has
15342 -- progenitors, and the dispatch table will be malformed.
15343 -- We check that the type is limited to handle the anomalous declaration
15344 -- of Limited_Controlled, which is derived from a non-limited type, and
15345 -- which is handled specially elsewhere as well.
15346
15347 elsif Chars (Parent_Subp) = Name_Op_Eq
15348 and then Is_Dispatching_Operation (Parent_Subp)
15349 and then Etype (Parent_Subp) = Standard_Boolean
15350 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15351 and then
15352 Etype (First_Formal (Parent_Subp)) =
15353 Etype (Next_Formal (First_Formal (Parent_Subp)))
15354 then
15355 Set_Derived_Name;
15356
15357 -- If parent is hidden, this can be a regular derivation if the
15358 -- parent is immediately visible in a non-instantiating context,
15359 -- or if we are in the private part of an instance. This test
15360 -- should still be refined ???
15361
15362 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15363 -- operation as a non-visible operation in cases where the parent
15364 -- subprogram might not be visible now, but was visible within the
15365 -- original generic, so it would be wrong to make the inherited
15366 -- subprogram non-visible now. (Not clear if this test is fully
15367 -- correct; are there any cases where we should declare the inherited
15368 -- operation as not visible to avoid it being overridden, e.g., when
15369 -- the parent type is a generic actual with private primitives ???)
15370
15371 -- (they should be treated the same as other private inherited
15372 -- subprograms, but it's not clear how to do this cleanly). ???
15373
15374 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15375 and then Is_Immediately_Visible (Parent_Subp)
15376 and then not In_Instance)
15377 or else In_Instance_Not_Visible
15378 then
15379 Set_Derived_Name;
15380
15381 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15382 -- overrides an interface primitive because interface primitives
15383 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15384
15385 elsif Ada_Version >= Ada_2005
15386 and then Is_Dispatching_Operation (Parent_Subp)
15387 and then Present (Covered_Interface_Op (Parent_Subp))
15388 then
15389 Set_Derived_Name;
15390
15391 -- Otherwise, the type is inheriting a private operation, so enter it
15392 -- with a special name so it can't be overridden.
15393
15394 else
15395 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15396 end if;
15397
15398 Set_Parent (New_Subp, Parent (Derived_Type));
15399
15400 if Present (Actual_Subp) then
15401 Replace_Type (Actual_Subp, New_Subp);
15402 else
15403 Replace_Type (Parent_Subp, New_Subp);
15404 end if;
15405
15406 Conditional_Delay (New_Subp, Parent_Subp);
15407
15408 -- If we are creating a renaming for a primitive operation of an
15409 -- actual of a generic derived type, we must examine the signature
15410 -- of the actual primitive, not that of the generic formal, which for
15411 -- example may be an interface. However the name and initial value
15412 -- of the inherited operation are those of the formal primitive.
15413
15414 Formal := First_Formal (Parent_Subp);
15415
15416 if Present (Actual_Subp) then
15417 Formal_Of_Actual := First_Formal (Actual_Subp);
15418 else
15419 Formal_Of_Actual := Empty;
15420 end if;
15421
15422 while Present (Formal) loop
15423 New_Formal := New_Copy (Formal);
15424
15425 -- Normally we do not go copying parents, but in the case of
15426 -- formals, we need to link up to the declaration (which is the
15427 -- parameter specification), and it is fine to link up to the
15428 -- original formal's parameter specification in this case.
15429
15430 Set_Parent (New_Formal, Parent (Formal));
15431 Append_Entity (New_Formal, New_Subp);
15432
15433 if Present (Formal_Of_Actual) then
15434 Replace_Type (Formal_Of_Actual, New_Formal);
15435 Next_Formal (Formal_Of_Actual);
15436 else
15437 Replace_Type (Formal, New_Formal);
15438 end if;
15439
15440 Next_Formal (Formal);
15441 end loop;
15442
15443 -- If this derivation corresponds to a tagged generic actual, then
15444 -- primitive operations rename those of the actual. Otherwise the
15445 -- primitive operations rename those of the parent type, If the parent
15446 -- renames an intrinsic operator, so does the new subprogram. We except
15447 -- concatenation, which is always properly typed, and does not get
15448 -- expanded as other intrinsic operations.
15449
15450 if No (Actual_Subp) then
15451 if Is_Intrinsic_Subprogram (Parent_Subp) then
15452 Set_Is_Intrinsic_Subprogram (New_Subp);
15453
15454 if Present (Alias (Parent_Subp))
15455 and then Chars (Parent_Subp) /= Name_Op_Concat
15456 then
15457 Set_Alias (New_Subp, Alias (Parent_Subp));
15458 else
15459 Set_Alias (New_Subp, Parent_Subp);
15460 end if;
15461
15462 else
15463 Set_Alias (New_Subp, Parent_Subp);
15464 end if;
15465
15466 else
15467 Set_Alias (New_Subp, Actual_Subp);
15468 end if;
15469
15470 -- Derived subprograms of a tagged type must inherit the convention
15471 -- of the parent subprogram (a requirement of AI-117). Derived
15472 -- subprograms of untagged types simply get convention Ada by default.
15473
15474 -- If the derived type is a tagged generic formal type with unknown
15475 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15476
15477 -- However, if the type is derived from a generic formal, the further
15478 -- inherited subprogram has the convention of the non-generic ancestor.
15479 -- Otherwise there would be no way to override the operation.
15480 -- (This is subject to forthcoming ARG discussions).
15481
15482 if Is_Tagged_Type (Derived_Type) then
15483 if Is_Generic_Type (Derived_Type)
15484 and then Has_Unknown_Discriminants (Derived_Type)
15485 then
15486 Set_Convention (New_Subp, Convention_Intrinsic);
15487
15488 else
15489 if Is_Generic_Type (Parent_Type)
15490 and then Has_Unknown_Discriminants (Parent_Type)
15491 then
15492 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15493 else
15494 Set_Convention (New_Subp, Convention (Parent_Subp));
15495 end if;
15496 end if;
15497 end if;
15498
15499 -- Predefined controlled operations retain their name even if the parent
15500 -- is hidden (see above), but they are not primitive operations if the
15501 -- ancestor is not visible, for example if the parent is a private
15502 -- extension completed with a controlled extension. Note that a full
15503 -- type that is controlled can break privacy: the flag Is_Controlled is
15504 -- set on both views of the type.
15505
15506 if Is_Controlled (Parent_Type)
15507 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15508 Name_Adjust,
15509 Name_Finalize)
15510 and then Is_Hidden (Parent_Subp)
15511 and then not Is_Visibly_Controlled (Parent_Type)
15512 then
15513 Set_Is_Hidden (New_Subp);
15514 end if;
15515
15516 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15517 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15518
15519 if Ekind (Parent_Subp) = E_Procedure then
15520 Set_Is_Valued_Procedure
15521 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15522 else
15523 Set_Has_Controlling_Result
15524 (New_Subp, Has_Controlling_Result (Parent_Subp));
15525 end if;
15526
15527 -- No_Return must be inherited properly. If this is overridden in the
15528 -- case of a dispatching operation, then a check is made in Sem_Disp
15529 -- that the overriding operation is also No_Return (no such check is
15530 -- required for the case of non-dispatching operation.
15531
15532 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15533
15534 -- A derived function with a controlling result is abstract. If the
15535 -- Derived_Type is a nonabstract formal generic derived type, then
15536 -- inherited operations are not abstract: the required check is done at
15537 -- instantiation time. If the derivation is for a generic actual, the
15538 -- function is not abstract unless the actual is.
15539
15540 if Is_Generic_Type (Derived_Type)
15541 and then not Is_Abstract_Type (Derived_Type)
15542 then
15543 null;
15544
15545 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15546 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15547
15548 -- A subprogram subject to pragma Extensions_Visible with value False
15549 -- requires overriding if the subprogram has at least one controlling
15550 -- OUT parameter (SPARK RM 6.1.7(6)).
15551
15552 elsif Ada_Version >= Ada_2005
15553 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15554 or else (Is_Tagged_Type (Derived_Type)
15555 and then Etype (New_Subp) = Derived_Type
15556 and then not Is_Null_Extension (Derived_Type))
15557 or else (Is_Tagged_Type (Derived_Type)
15558 and then Ekind (Etype (New_Subp)) =
15559 E_Anonymous_Access_Type
15560 and then Designated_Type (Etype (New_Subp)) =
15561 Derived_Type
15562 and then not Is_Null_Extension (Derived_Type))
15563 or else (Comes_From_Source (Alias (New_Subp))
15564 and then Is_EVF_Procedure (Alias (New_Subp))))
15565 and then No (Actual_Subp)
15566 then
15567 if not Is_Tagged_Type (Derived_Type)
15568 or else Is_Abstract_Type (Derived_Type)
15569 or else Is_Abstract_Subprogram (Alias (New_Subp))
15570 then
15571 Set_Is_Abstract_Subprogram (New_Subp);
15572 else
15573 Set_Requires_Overriding (New_Subp);
15574 end if;
15575
15576 elsif Ada_Version < Ada_2005
15577 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15578 or else (Is_Tagged_Type (Derived_Type)
15579 and then Etype (New_Subp) = Derived_Type
15580 and then No (Actual_Subp)))
15581 then
15582 Set_Is_Abstract_Subprogram (New_Subp);
15583
15584 -- AI05-0097 : an inherited operation that dispatches on result is
15585 -- abstract if the derived type is abstract, even if the parent type
15586 -- is concrete and the derived type is a null extension.
15587
15588 elsif Has_Controlling_Result (Alias (New_Subp))
15589 and then Is_Abstract_Type (Etype (New_Subp))
15590 then
15591 Set_Is_Abstract_Subprogram (New_Subp);
15592
15593 -- Finally, if the parent type is abstract we must verify that all
15594 -- inherited operations are either non-abstract or overridden, or that
15595 -- the derived type itself is abstract (this check is performed at the
15596 -- end of a package declaration, in Check_Abstract_Overriding). A
15597 -- private overriding in the parent type will not be visible in the
15598 -- derivation if we are not in an inner package or in a child unit of
15599 -- the parent type, in which case the abstractness of the inherited
15600 -- operation is carried to the new subprogram.
15601
15602 elsif Is_Abstract_Type (Parent_Type)
15603 and then not In_Open_Scopes (Scope (Parent_Type))
15604 and then Is_Private_Overriding
15605 and then Is_Abstract_Subprogram (Visible_Subp)
15606 then
15607 if No (Actual_Subp) then
15608 Set_Alias (New_Subp, Visible_Subp);
15609 Set_Is_Abstract_Subprogram (New_Subp, True);
15610
15611 else
15612 -- If this is a derivation for an instance of a formal derived
15613 -- type, abstractness comes from the primitive operation of the
15614 -- actual, not from the operation inherited from the ancestor.
15615
15616 Set_Is_Abstract_Subprogram
15617 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15618 end if;
15619 end if;
15620
15621 New_Overloaded_Entity (New_Subp, Derived_Type);
15622
15623 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15624 -- preconditions and the derived type is abstract, the derived operation
15625 -- is abstract as well if parent subprogram is not abstract or null.
15626
15627 if Is_Abstract_Type (Derived_Type)
15628 and then Has_Non_Trivial_Precondition (Parent_Subp)
15629 and then Present (Interfaces (Derived_Type))
15630 then
15631
15632 -- Add useful attributes of subprogram before the freeze point,
15633 -- in case freezing is delayed or there are previous errors.
15634
15635 Set_Is_Dispatching_Operation (New_Subp);
15636
15637 declare
15638 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15639
15640 begin
15641 if Present (Iface_Prim)
15642 and then Has_Non_Trivial_Precondition (Iface_Prim)
15643 then
15644 Set_Is_Abstract_Subprogram (New_Subp);
15645 end if;
15646 end;
15647 end if;
15648
15649 -- Check for case of a derived subprogram for the instantiation of a
15650 -- formal derived tagged type, if so mark the subprogram as dispatching
15651 -- and inherit the dispatching attributes of the actual subprogram. The
15652 -- derived subprogram is effectively renaming of the actual subprogram,
15653 -- so it needs to have the same attributes as the actual.
15654
15655 if Present (Actual_Subp)
15656 and then Is_Dispatching_Operation (Actual_Subp)
15657 then
15658 Set_Is_Dispatching_Operation (New_Subp);
15659
15660 if Present (DTC_Entity (Actual_Subp)) then
15661 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15662 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15663 end if;
15664 end if;
15665
15666 -- Indicate that a derived subprogram does not require a body and that
15667 -- it does not require processing of default expressions.
15668
15669 Set_Has_Completion (New_Subp);
15670 Set_Default_Expressions_Processed (New_Subp);
15671
15672 if Ekind (New_Subp) = E_Function then
15673 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15674 end if;
15675 end Derive_Subprogram;
15676
15677 ------------------------
15678 -- Derive_Subprograms --
15679 ------------------------
15680
15681 procedure Derive_Subprograms
15682 (Parent_Type : Entity_Id;
15683 Derived_Type : Entity_Id;
15684 Generic_Actual : Entity_Id := Empty)
15685 is
15686 Op_List : constant Elist_Id :=
15687 Collect_Primitive_Operations (Parent_Type);
15688
15689 function Check_Derived_Type return Boolean;
15690 -- Check that all the entities derived from Parent_Type are found in
15691 -- the list of primitives of Derived_Type exactly in the same order.
15692
15693 procedure Derive_Interface_Subprogram
15694 (New_Subp : out Entity_Id;
15695 Subp : Entity_Id;
15696 Actual_Subp : Entity_Id);
15697 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15698 -- (which is an interface primitive). If Generic_Actual is present then
15699 -- Actual_Subp is the actual subprogram corresponding with the generic
15700 -- subprogram Subp.
15701
15702 ------------------------
15703 -- Check_Derived_Type --
15704 ------------------------
15705
15706 function Check_Derived_Type return Boolean is
15707 E : Entity_Id;
15708 Elmt : Elmt_Id;
15709 List : Elist_Id;
15710 New_Subp : Entity_Id;
15711 Op_Elmt : Elmt_Id;
15712 Subp : Entity_Id;
15713
15714 begin
15715 -- Traverse list of entities in the current scope searching for
15716 -- an incomplete type whose full-view is derived type.
15717
15718 E := First_Entity (Scope (Derived_Type));
15719 while Present (E) and then E /= Derived_Type loop
15720 if Ekind (E) = E_Incomplete_Type
15721 and then Present (Full_View (E))
15722 and then Full_View (E) = Derived_Type
15723 then
15724 -- Disable this test if Derived_Type completes an incomplete
15725 -- type because in such case more primitives can be added
15726 -- later to the list of primitives of Derived_Type by routine
15727 -- Process_Incomplete_Dependents
15728
15729 return True;
15730 end if;
15731
15732 E := Next_Entity (E);
15733 end loop;
15734
15735 List := Collect_Primitive_Operations (Derived_Type);
15736 Elmt := First_Elmt (List);
15737
15738 Op_Elmt := First_Elmt (Op_List);
15739 while Present (Op_Elmt) loop
15740 Subp := Node (Op_Elmt);
15741 New_Subp := Node (Elmt);
15742
15743 -- At this early stage Derived_Type has no entities with attribute
15744 -- Interface_Alias. In addition, such primitives are always
15745 -- located at the end of the list of primitives of Parent_Type.
15746 -- Therefore, if found we can safely stop processing pending
15747 -- entities.
15748
15749 exit when Present (Interface_Alias (Subp));
15750
15751 -- Handle hidden entities
15752
15753 if not Is_Predefined_Dispatching_Operation (Subp)
15754 and then Is_Hidden (Subp)
15755 then
15756 if Present (New_Subp)
15757 and then Primitive_Names_Match (Subp, New_Subp)
15758 then
15759 Next_Elmt (Elmt);
15760 end if;
15761
15762 else
15763 if not Present (New_Subp)
15764 or else Ekind (Subp) /= Ekind (New_Subp)
15765 or else not Primitive_Names_Match (Subp, New_Subp)
15766 then
15767 return False;
15768 end if;
15769
15770 Next_Elmt (Elmt);
15771 end if;
15772
15773 Next_Elmt (Op_Elmt);
15774 end loop;
15775
15776 return True;
15777 end Check_Derived_Type;
15778
15779 ---------------------------------
15780 -- Derive_Interface_Subprogram --
15781 ---------------------------------
15782
15783 procedure Derive_Interface_Subprogram
15784 (New_Subp : out Entity_Id;
15785 Subp : Entity_Id;
15786 Actual_Subp : Entity_Id)
15787 is
15788 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15789 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15790
15791 begin
15792 pragma Assert (Is_Interface (Iface_Type));
15793
15794 Derive_Subprogram
15795 (New_Subp => New_Subp,
15796 Parent_Subp => Iface_Subp,
15797 Derived_Type => Derived_Type,
15798 Parent_Type => Iface_Type,
15799 Actual_Subp => Actual_Subp);
15800
15801 -- Given that this new interface entity corresponds with a primitive
15802 -- of the parent that was not overridden we must leave it associated
15803 -- with its parent primitive to ensure that it will share the same
15804 -- dispatch table slot when overridden. We must set the Alias to Subp
15805 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15806 -- (in case we inherited Subp from Iface_Type via a nonabstract
15807 -- generic formal type).
15808
15809 if No (Actual_Subp) then
15810 Set_Alias (New_Subp, Subp);
15811
15812 declare
15813 T : Entity_Id := Find_Dispatching_Type (Subp);
15814 begin
15815 while Etype (T) /= T loop
15816 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15817 Set_Is_Abstract_Subprogram (New_Subp, False);
15818 exit;
15819 end if;
15820
15821 T := Etype (T);
15822 end loop;
15823 end;
15824
15825 -- For instantiations this is not needed since the previous call to
15826 -- Derive_Subprogram leaves the entity well decorated.
15827
15828 else
15829 pragma Assert (Alias (New_Subp) = Actual_Subp);
15830 null;
15831 end if;
15832 end Derive_Interface_Subprogram;
15833
15834 -- Local variables
15835
15836 Alias_Subp : Entity_Id;
15837 Act_List : Elist_Id;
15838 Act_Elmt : Elmt_Id;
15839 Act_Subp : Entity_Id := Empty;
15840 Elmt : Elmt_Id;
15841 Need_Search : Boolean := False;
15842 New_Subp : Entity_Id := Empty;
15843 Parent_Base : Entity_Id;
15844 Subp : Entity_Id;
15845
15846 -- Start of processing for Derive_Subprograms
15847
15848 begin
15849 if Ekind (Parent_Type) = E_Record_Type_With_Private
15850 and then Has_Discriminants (Parent_Type)
15851 and then Present (Full_View (Parent_Type))
15852 then
15853 Parent_Base := Full_View (Parent_Type);
15854 else
15855 Parent_Base := Parent_Type;
15856 end if;
15857
15858 if Present (Generic_Actual) then
15859 Act_List := Collect_Primitive_Operations (Generic_Actual);
15860 Act_Elmt := First_Elmt (Act_List);
15861 else
15862 Act_List := No_Elist;
15863 Act_Elmt := No_Elmt;
15864 end if;
15865
15866 -- Derive primitives inherited from the parent. Note that if the generic
15867 -- actual is present, this is not really a type derivation, it is a
15868 -- completion within an instance.
15869
15870 -- Case 1: Derived_Type does not implement interfaces
15871
15872 if not Is_Tagged_Type (Derived_Type)
15873 or else (not Has_Interfaces (Derived_Type)
15874 and then not (Present (Generic_Actual)
15875 and then Has_Interfaces (Generic_Actual)))
15876 then
15877 Elmt := First_Elmt (Op_List);
15878 while Present (Elmt) loop
15879 Subp := Node (Elmt);
15880
15881 -- Literals are derived earlier in the process of building the
15882 -- derived type, and are skipped here.
15883
15884 if Ekind (Subp) = E_Enumeration_Literal then
15885 null;
15886
15887 -- The actual is a direct descendant and the common primitive
15888 -- operations appear in the same order.
15889
15890 -- If the generic parent type is present, the derived type is an
15891 -- instance of a formal derived type, and within the instance its
15892 -- operations are those of the actual. We derive from the formal
15893 -- type but make the inherited operations aliases of the
15894 -- corresponding operations of the actual.
15895
15896 else
15897 pragma Assert (No (Node (Act_Elmt))
15898 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15899 and then
15900 Type_Conformant
15901 (Subp, Node (Act_Elmt),
15902 Skip_Controlling_Formals => True)));
15903
15904 Derive_Subprogram
15905 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15906
15907 if Present (Act_Elmt) then
15908 Next_Elmt (Act_Elmt);
15909 end if;
15910 end if;
15911
15912 Next_Elmt (Elmt);
15913 end loop;
15914
15915 -- Case 2: Derived_Type implements interfaces
15916
15917 else
15918 -- If the parent type has no predefined primitives we remove
15919 -- predefined primitives from the list of primitives of generic
15920 -- actual to simplify the complexity of this algorithm.
15921
15922 if Present (Generic_Actual) then
15923 declare
15924 Has_Predefined_Primitives : Boolean := False;
15925
15926 begin
15927 -- Check if the parent type has predefined primitives
15928
15929 Elmt := First_Elmt (Op_List);
15930 while Present (Elmt) loop
15931 Subp := Node (Elmt);
15932
15933 if Is_Predefined_Dispatching_Operation (Subp)
15934 and then not Comes_From_Source (Ultimate_Alias (Subp))
15935 then
15936 Has_Predefined_Primitives := True;
15937 exit;
15938 end if;
15939
15940 Next_Elmt (Elmt);
15941 end loop;
15942
15943 -- Remove predefined primitives of Generic_Actual. We must use
15944 -- an auxiliary list because in case of tagged types the value
15945 -- returned by Collect_Primitive_Operations is the value stored
15946 -- in its Primitive_Operations attribute (and we don't want to
15947 -- modify its current contents).
15948
15949 if not Has_Predefined_Primitives then
15950 declare
15951 Aux_List : constant Elist_Id := New_Elmt_List;
15952
15953 begin
15954 Elmt := First_Elmt (Act_List);
15955 while Present (Elmt) loop
15956 Subp := Node (Elmt);
15957
15958 if not Is_Predefined_Dispatching_Operation (Subp)
15959 or else Comes_From_Source (Subp)
15960 then
15961 Append_Elmt (Subp, Aux_List);
15962 end if;
15963
15964 Next_Elmt (Elmt);
15965 end loop;
15966
15967 Act_List := Aux_List;
15968 end;
15969 end if;
15970
15971 Act_Elmt := First_Elmt (Act_List);
15972 Act_Subp := Node (Act_Elmt);
15973 end;
15974 end if;
15975
15976 -- Stage 1: If the generic actual is not present we derive the
15977 -- primitives inherited from the parent type. If the generic parent
15978 -- type is present, the derived type is an instance of a formal
15979 -- derived type, and within the instance its operations are those of
15980 -- the actual. We derive from the formal type but make the inherited
15981 -- operations aliases of the corresponding operations of the actual.
15982
15983 Elmt := First_Elmt (Op_List);
15984 while Present (Elmt) loop
15985 Subp := Node (Elmt);
15986 Alias_Subp := Ultimate_Alias (Subp);
15987
15988 -- Do not derive internal entities of the parent that link
15989 -- interface primitives with their covering primitive. These
15990 -- entities will be added to this type when frozen.
15991
15992 if Present (Interface_Alias (Subp)) then
15993 goto Continue;
15994 end if;
15995
15996 -- If the generic actual is present find the corresponding
15997 -- operation in the generic actual. If the parent type is a
15998 -- direct ancestor of the derived type then, even if it is an
15999 -- interface, the operations are inherited from the primary
16000 -- dispatch table and are in the proper order. If we detect here
16001 -- that primitives are not in the same order we traverse the list
16002 -- of primitive operations of the actual to find the one that
16003 -- implements the interface primitive.
16004
16005 if Need_Search
16006 or else
16007 (Present (Generic_Actual)
16008 and then Present (Act_Subp)
16009 and then not
16010 (Primitive_Names_Match (Subp, Act_Subp)
16011 and then
16012 Type_Conformant (Subp, Act_Subp,
16013 Skip_Controlling_Formals => True)))
16014 then
16015 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16016 Use_Full_View => True));
16017
16018 -- Remember that we need searching for all pending primitives
16019
16020 Need_Search := True;
16021
16022 -- Handle entities associated with interface primitives
16023
16024 if Present (Alias_Subp)
16025 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16026 and then not Is_Predefined_Dispatching_Operation (Subp)
16027 then
16028 -- Search for the primitive in the homonym chain
16029
16030 Act_Subp :=
16031 Find_Primitive_Covering_Interface
16032 (Tagged_Type => Generic_Actual,
16033 Iface_Prim => Alias_Subp);
16034
16035 -- Previous search may not locate primitives covering
16036 -- interfaces defined in generics units or instantiations.
16037 -- (it fails if the covering primitive has formals whose
16038 -- type is also defined in generics or instantiations).
16039 -- In such case we search in the list of primitives of the
16040 -- generic actual for the internal entity that links the
16041 -- interface primitive and the covering primitive.
16042
16043 if No (Act_Subp)
16044 and then Is_Generic_Type (Parent_Type)
16045 then
16046 -- This code has been designed to handle only generic
16047 -- formals that implement interfaces that are defined
16048 -- in a generic unit or instantiation. If this code is
16049 -- needed for other cases we must review it because
16050 -- (given that it relies on Original_Location to locate
16051 -- the primitive of Generic_Actual that covers the
16052 -- interface) it could leave linked through attribute
16053 -- Alias entities of unrelated instantiations).
16054
16055 pragma Assert
16056 (Is_Generic_Unit
16057 (Scope (Find_Dispatching_Type (Alias_Subp)))
16058 or else
16059 Instantiation_Depth
16060 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16061
16062 declare
16063 Iface_Prim_Loc : constant Source_Ptr :=
16064 Original_Location (Sloc (Alias_Subp));
16065
16066 Elmt : Elmt_Id;
16067 Prim : Entity_Id;
16068
16069 begin
16070 Elmt :=
16071 First_Elmt (Primitive_Operations (Generic_Actual));
16072
16073 Search : while Present (Elmt) loop
16074 Prim := Node (Elmt);
16075
16076 if Present (Interface_Alias (Prim))
16077 and then Original_Location
16078 (Sloc (Interface_Alias (Prim))) =
16079 Iface_Prim_Loc
16080 then
16081 Act_Subp := Alias (Prim);
16082 exit Search;
16083 end if;
16084
16085 Next_Elmt (Elmt);
16086 end loop Search;
16087 end;
16088 end if;
16089
16090 pragma Assert (Present (Act_Subp)
16091 or else Is_Abstract_Type (Generic_Actual)
16092 or else Serious_Errors_Detected > 0);
16093
16094 -- Handle predefined primitives plus the rest of user-defined
16095 -- primitives
16096
16097 else
16098 Act_Elmt := First_Elmt (Act_List);
16099 while Present (Act_Elmt) loop
16100 Act_Subp := Node (Act_Elmt);
16101
16102 exit when Primitive_Names_Match (Subp, Act_Subp)
16103 and then Type_Conformant
16104 (Subp, Act_Subp,
16105 Skip_Controlling_Formals => True)
16106 and then No (Interface_Alias (Act_Subp));
16107
16108 Next_Elmt (Act_Elmt);
16109 end loop;
16110
16111 if No (Act_Elmt) then
16112 Act_Subp := Empty;
16113 end if;
16114 end if;
16115 end if;
16116
16117 -- Case 1: If the parent is a limited interface then it has the
16118 -- predefined primitives of synchronized interfaces. However, the
16119 -- actual type may be a non-limited type and hence it does not
16120 -- have such primitives.
16121
16122 if Present (Generic_Actual)
16123 and then not Present (Act_Subp)
16124 and then Is_Limited_Interface (Parent_Base)
16125 and then Is_Predefined_Interface_Primitive (Subp)
16126 then
16127 null;
16128
16129 -- Case 2: Inherit entities associated with interfaces that were
16130 -- not covered by the parent type. We exclude here null interface
16131 -- primitives because they do not need special management.
16132
16133 -- We also exclude interface operations that are renamings. If the
16134 -- subprogram is an explicit renaming of an interface primitive,
16135 -- it is a regular primitive operation, and the presence of its
16136 -- alias is not relevant: it has to be derived like any other
16137 -- primitive.
16138
16139 elsif Present (Alias (Subp))
16140 and then Nkind (Unit_Declaration_Node (Subp)) /=
16141 N_Subprogram_Renaming_Declaration
16142 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16143 and then not
16144 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16145 and then Null_Present (Parent (Alias_Subp)))
16146 then
16147 -- If this is an abstract private type then we transfer the
16148 -- derivation of the interface primitive from the partial view
16149 -- to the full view. This is safe because all the interfaces
16150 -- must be visible in the partial view. Done to avoid adding
16151 -- a new interface derivation to the private part of the
16152 -- enclosing package; otherwise this new derivation would be
16153 -- decorated as hidden when the analysis of the enclosing
16154 -- package completes.
16155
16156 if Is_Abstract_Type (Derived_Type)
16157 and then In_Private_Part (Current_Scope)
16158 and then Has_Private_Declaration (Derived_Type)
16159 then
16160 declare
16161 Partial_View : Entity_Id;
16162 Elmt : Elmt_Id;
16163 Ent : Entity_Id;
16164
16165 begin
16166 Partial_View := First_Entity (Current_Scope);
16167 loop
16168 exit when No (Partial_View)
16169 or else (Has_Private_Declaration (Partial_View)
16170 and then
16171 Full_View (Partial_View) = Derived_Type);
16172
16173 Next_Entity (Partial_View);
16174 end loop;
16175
16176 -- If the partial view was not found then the source code
16177 -- has errors and the derivation is not needed.
16178
16179 if Present (Partial_View) then
16180 Elmt :=
16181 First_Elmt (Primitive_Operations (Partial_View));
16182 while Present (Elmt) loop
16183 Ent := Node (Elmt);
16184
16185 if Present (Alias (Ent))
16186 and then Ultimate_Alias (Ent) = Alias (Subp)
16187 then
16188 Append_Elmt
16189 (Ent, Primitive_Operations (Derived_Type));
16190 exit;
16191 end if;
16192
16193 Next_Elmt (Elmt);
16194 end loop;
16195
16196 -- If the interface primitive was not found in the
16197 -- partial view then this interface primitive was
16198 -- overridden. We add a derivation to activate in
16199 -- Derive_Progenitor_Subprograms the machinery to
16200 -- search for it.
16201
16202 if No (Elmt) then
16203 Derive_Interface_Subprogram
16204 (New_Subp => New_Subp,
16205 Subp => Subp,
16206 Actual_Subp => Act_Subp);
16207 end if;
16208 end if;
16209 end;
16210 else
16211 Derive_Interface_Subprogram
16212 (New_Subp => New_Subp,
16213 Subp => Subp,
16214 Actual_Subp => Act_Subp);
16215 end if;
16216
16217 -- Case 3: Common derivation
16218
16219 else
16220 Derive_Subprogram
16221 (New_Subp => New_Subp,
16222 Parent_Subp => Subp,
16223 Derived_Type => Derived_Type,
16224 Parent_Type => Parent_Base,
16225 Actual_Subp => Act_Subp);
16226 end if;
16227
16228 -- No need to update Act_Elm if we must search for the
16229 -- corresponding operation in the generic actual
16230
16231 if not Need_Search
16232 and then Present (Act_Elmt)
16233 then
16234 Next_Elmt (Act_Elmt);
16235 Act_Subp := Node (Act_Elmt);
16236 end if;
16237
16238 <<Continue>>
16239 Next_Elmt (Elmt);
16240 end loop;
16241
16242 -- Inherit additional operations from progenitors. If the derived
16243 -- type is a generic actual, there are not new primitive operations
16244 -- for the type because it has those of the actual, and therefore
16245 -- nothing needs to be done. The renamings generated above are not
16246 -- primitive operations, and their purpose is simply to make the
16247 -- proper operations visible within an instantiation.
16248
16249 if No (Generic_Actual) then
16250 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16251 end if;
16252 end if;
16253
16254 -- Final check: Direct descendants must have their primitives in the
16255 -- same order. We exclude from this test untagged types and instances
16256 -- of formal derived types. We skip this test if we have already
16257 -- reported serious errors in the sources.
16258
16259 pragma Assert (not Is_Tagged_Type (Derived_Type)
16260 or else Present (Generic_Actual)
16261 or else Serious_Errors_Detected > 0
16262 or else Check_Derived_Type);
16263 end Derive_Subprograms;
16264
16265 --------------------------------
16266 -- Derived_Standard_Character --
16267 --------------------------------
16268
16269 procedure Derived_Standard_Character
16270 (N : Node_Id;
16271 Parent_Type : Entity_Id;
16272 Derived_Type : Entity_Id)
16273 is
16274 Loc : constant Source_Ptr := Sloc (N);
16275 Def : constant Node_Id := Type_Definition (N);
16276 Indic : constant Node_Id := Subtype_Indication (Def);
16277 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16278 Implicit_Base : constant Entity_Id :=
16279 Create_Itype
16280 (E_Enumeration_Type, N, Derived_Type, 'B');
16281
16282 Lo : Node_Id;
16283 Hi : Node_Id;
16284
16285 begin
16286 Discard_Node (Process_Subtype (Indic, N));
16287
16288 Set_Etype (Implicit_Base, Parent_Base);
16289 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16290 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16291
16292 Set_Is_Character_Type (Implicit_Base, True);
16293 Set_Has_Delayed_Freeze (Implicit_Base);
16294
16295 -- The bounds of the implicit base are the bounds of the parent base.
16296 -- Note that their type is the parent base.
16297
16298 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16299 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16300
16301 Set_Scalar_Range (Implicit_Base,
16302 Make_Range (Loc,
16303 Low_Bound => Lo,
16304 High_Bound => Hi));
16305
16306 Conditional_Delay (Derived_Type, Parent_Type);
16307
16308 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16309 Set_Etype (Derived_Type, Implicit_Base);
16310 Set_Size_Info (Derived_Type, Parent_Type);
16311
16312 if Unknown_RM_Size (Derived_Type) then
16313 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16314 end if;
16315
16316 Set_Is_Character_Type (Derived_Type, True);
16317
16318 if Nkind (Indic) /= N_Subtype_Indication then
16319
16320 -- If no explicit constraint, the bounds are those
16321 -- of the parent type.
16322
16323 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16324 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16325 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16326 end if;
16327
16328 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16329
16330 -- Because the implicit base is used in the conversion of the bounds, we
16331 -- have to freeze it now. This is similar to what is done for numeric
16332 -- types, and it equally suspicious, but otherwise a non-static bound
16333 -- will have a reference to an unfrozen type, which is rejected by Gigi
16334 -- (???). This requires specific care for definition of stream
16335 -- attributes. For details, see comments at the end of
16336 -- Build_Derived_Numeric_Type.
16337
16338 Freeze_Before (N, Implicit_Base);
16339 end Derived_Standard_Character;
16340
16341 ------------------------------
16342 -- Derived_Type_Declaration --
16343 ------------------------------
16344
16345 procedure Derived_Type_Declaration
16346 (T : Entity_Id;
16347 N : Node_Id;
16348 Is_Completion : Boolean)
16349 is
16350 Parent_Type : Entity_Id;
16351
16352 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16353 -- Check whether the parent type is a generic formal, or derives
16354 -- directly or indirectly from one.
16355
16356 ------------------------
16357 -- Comes_From_Generic --
16358 ------------------------
16359
16360 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16361 begin
16362 if Is_Generic_Type (Typ) then
16363 return True;
16364
16365 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16366 return True;
16367
16368 elsif Is_Private_Type (Typ)
16369 and then Present (Full_View (Typ))
16370 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16371 then
16372 return True;
16373
16374 elsif Is_Generic_Actual_Type (Typ) then
16375 return True;
16376
16377 else
16378 return False;
16379 end if;
16380 end Comes_From_Generic;
16381
16382 -- Local variables
16383
16384 Def : constant Node_Id := Type_Definition (N);
16385 Iface_Def : Node_Id;
16386 Indic : constant Node_Id := Subtype_Indication (Def);
16387 Extension : constant Node_Id := Record_Extension_Part (Def);
16388 Parent_Node : Node_Id;
16389 Taggd : Boolean;
16390
16391 -- Start of processing for Derived_Type_Declaration
16392
16393 begin
16394 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16395
16396 if SPARK_Mode = On
16397 and then Is_Tagged_Type (Parent_Type)
16398 then
16399 declare
16400 Partial_View : constant Entity_Id :=
16401 Incomplete_Or_Partial_View (Parent_Type);
16402
16403 begin
16404 -- If the partial view was not found then the parent type is not
16405 -- a private type. Otherwise check if the partial view is a tagged
16406 -- private type.
16407
16408 if Present (Partial_View)
16409 and then Is_Private_Type (Partial_View)
16410 and then not Is_Tagged_Type (Partial_View)
16411 then
16412 Error_Msg_NE
16413 ("cannot derive from & declared as untagged private "
16414 & "(SPARK RM 3.4(1))", N, Partial_View);
16415 end if;
16416 end;
16417 end if;
16418
16419 -- Ada 2005 (AI-251): In case of interface derivation check that the
16420 -- parent is also an interface.
16421
16422 if Interface_Present (Def) then
16423 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16424
16425 if not Is_Interface (Parent_Type) then
16426 Diagnose_Interface (Indic, Parent_Type);
16427
16428 else
16429 Parent_Node := Parent (Base_Type (Parent_Type));
16430 Iface_Def := Type_Definition (Parent_Node);
16431
16432 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16433 -- other limited interfaces.
16434
16435 if Limited_Present (Def) then
16436 if Limited_Present (Iface_Def) then
16437 null;
16438
16439 elsif Protected_Present (Iface_Def) then
16440 Error_Msg_NE
16441 ("descendant of & must be declared as a protected "
16442 & "interface", N, Parent_Type);
16443
16444 elsif Synchronized_Present (Iface_Def) then
16445 Error_Msg_NE
16446 ("descendant of & must be declared as a synchronized "
16447 & "interface", N, Parent_Type);
16448
16449 elsif Task_Present (Iface_Def) then
16450 Error_Msg_NE
16451 ("descendant of & must be declared as a task interface",
16452 N, Parent_Type);
16453
16454 else
16455 Error_Msg_N
16456 ("(Ada 2005) limited interface cannot inherit from "
16457 & "non-limited interface", Indic);
16458 end if;
16459
16460 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16461 -- from non-limited or limited interfaces.
16462
16463 elsif not Protected_Present (Def)
16464 and then not Synchronized_Present (Def)
16465 and then not Task_Present (Def)
16466 then
16467 if Limited_Present (Iface_Def) then
16468 null;
16469
16470 elsif Protected_Present (Iface_Def) then
16471 Error_Msg_NE
16472 ("descendant of & must be declared as a protected "
16473 & "interface", N, Parent_Type);
16474
16475 elsif Synchronized_Present (Iface_Def) then
16476 Error_Msg_NE
16477 ("descendant of & must be declared as a synchronized "
16478 & "interface", N, Parent_Type);
16479
16480 elsif Task_Present (Iface_Def) then
16481 Error_Msg_NE
16482 ("descendant of & must be declared as a task interface",
16483 N, Parent_Type);
16484 else
16485 null;
16486 end if;
16487 end if;
16488 end if;
16489 end if;
16490
16491 if Is_Tagged_Type (Parent_Type)
16492 and then Is_Concurrent_Type (Parent_Type)
16493 and then not Is_Interface (Parent_Type)
16494 then
16495 Error_Msg_N
16496 ("parent type of a record extension cannot be a synchronized "
16497 & "tagged type (RM 3.9.1 (3/1))", N);
16498 Set_Etype (T, Any_Type);
16499 return;
16500 end if;
16501
16502 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16503 -- interfaces
16504
16505 if Is_Tagged_Type (Parent_Type)
16506 and then Is_Non_Empty_List (Interface_List (Def))
16507 then
16508 declare
16509 Intf : Node_Id;
16510 T : Entity_Id;
16511
16512 begin
16513 Intf := First (Interface_List (Def));
16514 while Present (Intf) loop
16515 T := Find_Type_Of_Subtype_Indic (Intf);
16516
16517 if not Is_Interface (T) then
16518 Diagnose_Interface (Intf, T);
16519
16520 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16521 -- a limited type from having a nonlimited progenitor.
16522
16523 elsif (Limited_Present (Def)
16524 or else (not Is_Interface (Parent_Type)
16525 and then Is_Limited_Type (Parent_Type)))
16526 and then not Is_Limited_Interface (T)
16527 then
16528 Error_Msg_NE
16529 ("progenitor interface& of limited type must be limited",
16530 N, T);
16531 end if;
16532
16533 Next (Intf);
16534 end loop;
16535 end;
16536 end if;
16537
16538 if Parent_Type = Any_Type
16539 or else Etype (Parent_Type) = Any_Type
16540 or else (Is_Class_Wide_Type (Parent_Type)
16541 and then Etype (Parent_Type) = T)
16542 then
16543 -- If Parent_Type is undefined or illegal, make new type into a
16544 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16545 -- errors. If this is a self-definition, emit error now.
16546
16547 if T = Parent_Type or else T = Etype (Parent_Type) then
16548 Error_Msg_N ("type cannot be used in its own definition", Indic);
16549 end if;
16550
16551 Set_Ekind (T, Ekind (Parent_Type));
16552 Set_Etype (T, Any_Type);
16553 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16554
16555 if Is_Tagged_Type (T)
16556 and then Is_Record_Type (T)
16557 then
16558 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16559 end if;
16560
16561 return;
16562 end if;
16563
16564 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16565 -- an interface is special because the list of interfaces in the full
16566 -- view can be given in any order. For example:
16567
16568 -- type A is interface;
16569 -- type B is interface and A;
16570 -- type D is new B with private;
16571 -- private
16572 -- type D is new A and B with null record; -- 1 --
16573
16574 -- In this case we perform the following transformation of -1-:
16575
16576 -- type D is new B and A with null record;
16577
16578 -- If the parent of the full-view covers the parent of the partial-view
16579 -- we have two possible cases:
16580
16581 -- 1) They have the same parent
16582 -- 2) The parent of the full-view implements some further interfaces
16583
16584 -- In both cases we do not need to perform the transformation. In the
16585 -- first case the source program is correct and the transformation is
16586 -- not needed; in the second case the source program does not fulfill
16587 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16588 -- later.
16589
16590 -- This transformation not only simplifies the rest of the analysis of
16591 -- this type declaration but also simplifies the correct generation of
16592 -- the object layout to the expander.
16593
16594 if In_Private_Part (Current_Scope)
16595 and then Is_Interface (Parent_Type)
16596 then
16597 declare
16598 Iface : Node_Id;
16599 Partial_View : Entity_Id;
16600 Partial_View_Parent : Entity_Id;
16601 New_Iface : Node_Id;
16602
16603 begin
16604 -- Look for the associated private type declaration
16605
16606 Partial_View := Incomplete_Or_Partial_View (T);
16607
16608 -- If the partial view was not found then the source code has
16609 -- errors and the transformation is not needed.
16610
16611 if Present (Partial_View) then
16612 Partial_View_Parent := Etype (Partial_View);
16613
16614 -- If the parent of the full-view covers the parent of the
16615 -- partial-view we have nothing else to do.
16616
16617 if Interface_Present_In_Ancestor
16618 (Parent_Type, Partial_View_Parent)
16619 then
16620 null;
16621
16622 -- Traverse the list of interfaces of the full-view to look
16623 -- for the parent of the partial-view and perform the tree
16624 -- transformation.
16625
16626 else
16627 Iface := First (Interface_List (Def));
16628 while Present (Iface) loop
16629 if Etype (Iface) = Etype (Partial_View) then
16630 Rewrite (Subtype_Indication (Def),
16631 New_Copy (Subtype_Indication
16632 (Parent (Partial_View))));
16633
16634 New_Iface :=
16635 Make_Identifier (Sloc (N), Chars (Parent_Type));
16636 Append (New_Iface, Interface_List (Def));
16637
16638 -- Analyze the transformed code
16639
16640 Derived_Type_Declaration (T, N, Is_Completion);
16641 return;
16642 end if;
16643
16644 Next (Iface);
16645 end loop;
16646 end if;
16647 end if;
16648 end;
16649 end if;
16650
16651 -- Only composite types other than array types are allowed to have
16652 -- discriminants.
16653
16654 if Present (Discriminant_Specifications (N)) then
16655 if (Is_Elementary_Type (Parent_Type)
16656 or else
16657 Is_Array_Type (Parent_Type))
16658 and then not Error_Posted (N)
16659 then
16660 Error_Msg_N
16661 ("elementary or array type cannot have discriminants",
16662 Defining_Identifier (First (Discriminant_Specifications (N))));
16663 Set_Has_Discriminants (T, False);
16664
16665 -- The type is allowed to have discriminants
16666
16667 else
16668 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16669 end if;
16670 end if;
16671
16672 -- In Ada 83, a derived type defined in a package specification cannot
16673 -- be used for further derivation until the end of its visible part.
16674 -- Note that derivation in the private part of the package is allowed.
16675
16676 if Ada_Version = Ada_83
16677 and then Is_Derived_Type (Parent_Type)
16678 and then In_Visible_Part (Scope (Parent_Type))
16679 then
16680 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16681 Error_Msg_N
16682 ("(Ada 83): premature use of type for derivation", Indic);
16683 end if;
16684 end if;
16685
16686 -- Check for early use of incomplete or private type
16687
16688 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16689 Error_Msg_N ("premature derivation of incomplete type", Indic);
16690 return;
16691
16692 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16693 and then not Comes_From_Generic (Parent_Type))
16694 or else Has_Private_Component (Parent_Type)
16695 then
16696 -- The ancestor type of a formal type can be incomplete, in which
16697 -- case only the operations of the partial view are available in the
16698 -- generic. Subsequent checks may be required when the full view is
16699 -- analyzed to verify that a derivation from a tagged type has an
16700 -- extension.
16701
16702 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16703 null;
16704
16705 elsif No (Underlying_Type (Parent_Type))
16706 or else Has_Private_Component (Parent_Type)
16707 then
16708 Error_Msg_N
16709 ("premature derivation of derived or private type", Indic);
16710
16711 -- Flag the type itself as being in error, this prevents some
16712 -- nasty problems with subsequent uses of the malformed type.
16713
16714 Set_Error_Posted (T);
16715
16716 -- Check that within the immediate scope of an untagged partial
16717 -- view it's illegal to derive from the partial view if the
16718 -- full view is tagged. (7.3(7))
16719
16720 -- We verify that the Parent_Type is a partial view by checking
16721 -- that it is not a Full_Type_Declaration (i.e. a private type or
16722 -- private extension declaration), to distinguish a partial view
16723 -- from a derivation from a private type which also appears as
16724 -- E_Private_Type. If the parent base type is not declared in an
16725 -- enclosing scope there is no need to check.
16726
16727 elsif Present (Full_View (Parent_Type))
16728 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16729 and then not Is_Tagged_Type (Parent_Type)
16730 and then Is_Tagged_Type (Full_View (Parent_Type))
16731 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16732 then
16733 Error_Msg_N
16734 ("premature derivation from type with tagged full view",
16735 Indic);
16736 end if;
16737 end if;
16738
16739 -- Check that form of derivation is appropriate
16740
16741 Taggd := Is_Tagged_Type (Parent_Type);
16742
16743 -- Set the parent type to the class-wide type's specific type in this
16744 -- case to prevent cascading errors
16745
16746 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16747 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16748 Set_Etype (T, Etype (Parent_Type));
16749 return;
16750 end if;
16751
16752 if Present (Extension) and then not Taggd then
16753 Error_Msg_N
16754 ("type derived from untagged type cannot have extension", Indic);
16755
16756 elsif No (Extension) and then Taggd then
16757
16758 -- If this declaration is within a private part (or body) of a
16759 -- generic instantiation then the derivation is allowed (the parent
16760 -- type can only appear tagged in this case if it's a generic actual
16761 -- type, since it would otherwise have been rejected in the analysis
16762 -- of the generic template).
16763
16764 if not Is_Generic_Actual_Type (Parent_Type)
16765 or else In_Visible_Part (Scope (Parent_Type))
16766 then
16767 if Is_Class_Wide_Type (Parent_Type) then
16768 Error_Msg_N
16769 ("parent type must not be a class-wide type", Indic);
16770
16771 -- Use specific type to prevent cascaded errors.
16772
16773 Parent_Type := Etype (Parent_Type);
16774
16775 else
16776 Error_Msg_N
16777 ("type derived from tagged type must have extension", Indic);
16778 end if;
16779 end if;
16780 end if;
16781
16782 -- AI-443: Synchronized formal derived types require a private
16783 -- extension. There is no point in checking the ancestor type or
16784 -- the progenitors since the construct is wrong to begin with.
16785
16786 if Ada_Version >= Ada_2005
16787 and then Is_Generic_Type (T)
16788 and then Present (Original_Node (N))
16789 then
16790 declare
16791 Decl : constant Node_Id := Original_Node (N);
16792
16793 begin
16794 if Nkind (Decl) = N_Formal_Type_Declaration
16795 and then Nkind (Formal_Type_Definition (Decl)) =
16796 N_Formal_Derived_Type_Definition
16797 and then Synchronized_Present (Formal_Type_Definition (Decl))
16798 and then No (Extension)
16799
16800 -- Avoid emitting a duplicate error message
16801
16802 and then not Error_Posted (Indic)
16803 then
16804 Error_Msg_N
16805 ("synchronized derived type must have extension", N);
16806 end if;
16807 end;
16808 end if;
16809
16810 if Null_Exclusion_Present (Def)
16811 and then not Is_Access_Type (Parent_Type)
16812 then
16813 Error_Msg_N ("null exclusion can only apply to an access type", N);
16814 end if;
16815
16816 -- Avoid deriving parent primitives of underlying record views
16817
16818 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16819 Derive_Subps => not Is_Underlying_Record_View (T));
16820
16821 -- AI-419: The parent type of an explicitly limited derived type must
16822 -- be a limited type or a limited interface.
16823
16824 if Limited_Present (Def) then
16825 Set_Is_Limited_Record (T);
16826
16827 if Is_Interface (T) then
16828 Set_Is_Limited_Interface (T);
16829 end if;
16830
16831 if not Is_Limited_Type (Parent_Type)
16832 and then
16833 (not Is_Interface (Parent_Type)
16834 or else not Is_Limited_Interface (Parent_Type))
16835 then
16836 -- AI05-0096: a derivation in the private part of an instance is
16837 -- legal if the generic formal is untagged limited, and the actual
16838 -- is non-limited.
16839
16840 if Is_Generic_Actual_Type (Parent_Type)
16841 and then In_Private_Part (Current_Scope)
16842 and then
16843 not Is_Tagged_Type
16844 (Generic_Parent_Type (Parent (Parent_Type)))
16845 then
16846 null;
16847
16848 else
16849 Error_Msg_NE
16850 ("parent type& of limited type must be limited",
16851 N, Parent_Type);
16852 end if;
16853 end if;
16854 end if;
16855
16856 -- In SPARK, there are no derived type definitions other than type
16857 -- extensions of tagged record types.
16858
16859 if No (Extension) then
16860 Check_SPARK_05_Restriction
16861 ("derived type is not allowed", Original_Node (N));
16862 end if;
16863 end Derived_Type_Declaration;
16864
16865 ------------------------
16866 -- Diagnose_Interface --
16867 ------------------------
16868
16869 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16870 begin
16871 if not Is_Interface (E) and then E /= Any_Type then
16872 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16873 end if;
16874 end Diagnose_Interface;
16875
16876 ----------------------------------
16877 -- Enumeration_Type_Declaration --
16878 ----------------------------------
16879
16880 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16881 Ev : Uint;
16882 L : Node_Id;
16883 R_Node : Node_Id;
16884 B_Node : Node_Id;
16885
16886 begin
16887 -- Create identifier node representing lower bound
16888
16889 B_Node := New_Node (N_Identifier, Sloc (Def));
16890 L := First (Literals (Def));
16891 Set_Chars (B_Node, Chars (L));
16892 Set_Entity (B_Node, L);
16893 Set_Etype (B_Node, T);
16894 Set_Is_Static_Expression (B_Node, True);
16895
16896 R_Node := New_Node (N_Range, Sloc (Def));
16897 Set_Low_Bound (R_Node, B_Node);
16898
16899 Set_Ekind (T, E_Enumeration_Type);
16900 Set_First_Literal (T, L);
16901 Set_Etype (T, T);
16902 Set_Is_Constrained (T);
16903
16904 Ev := Uint_0;
16905
16906 -- Loop through literals of enumeration type setting pos and rep values
16907 -- except that if the Ekind is already set, then it means the literal
16908 -- was already constructed (case of a derived type declaration and we
16909 -- should not disturb the Pos and Rep values.
16910
16911 while Present (L) loop
16912 if Ekind (L) /= E_Enumeration_Literal then
16913 Set_Ekind (L, E_Enumeration_Literal);
16914 Set_Enumeration_Pos (L, Ev);
16915 Set_Enumeration_Rep (L, Ev);
16916 Set_Is_Known_Valid (L, True);
16917 end if;
16918
16919 Set_Etype (L, T);
16920 New_Overloaded_Entity (L);
16921 Generate_Definition (L);
16922 Set_Convention (L, Convention_Intrinsic);
16923
16924 -- Case of character literal
16925
16926 if Nkind (L) = N_Defining_Character_Literal then
16927 Set_Is_Character_Type (T, True);
16928
16929 -- Check violation of No_Wide_Characters
16930
16931 if Restriction_Check_Required (No_Wide_Characters) then
16932 Get_Name_String (Chars (L));
16933
16934 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16935 Check_Restriction (No_Wide_Characters, L);
16936 end if;
16937 end if;
16938 end if;
16939
16940 Ev := Ev + 1;
16941 Next (L);
16942 end loop;
16943
16944 -- Now create a node representing upper bound
16945
16946 B_Node := New_Node (N_Identifier, Sloc (Def));
16947 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16948 Set_Entity (B_Node, Last (Literals (Def)));
16949 Set_Etype (B_Node, T);
16950 Set_Is_Static_Expression (B_Node, True);
16951
16952 Set_High_Bound (R_Node, B_Node);
16953
16954 -- Initialize various fields of the type. Some of this information
16955 -- may be overwritten later through rep.clauses.
16956
16957 Set_Scalar_Range (T, R_Node);
16958 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16959 Set_Enum_Esize (T);
16960 Set_Enum_Pos_To_Rep (T, Empty);
16961
16962 -- Set Discard_Names if configuration pragma set, or if there is
16963 -- a parameterless pragma in the current declarative region
16964
16965 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16966 Set_Discard_Names (T);
16967 end if;
16968
16969 -- Process end label if there is one
16970
16971 if Present (Def) then
16972 Process_End_Label (Def, 'e', T);
16973 end if;
16974 end Enumeration_Type_Declaration;
16975
16976 ---------------------------------
16977 -- Expand_To_Stored_Constraint --
16978 ---------------------------------
16979
16980 function Expand_To_Stored_Constraint
16981 (Typ : Entity_Id;
16982 Constraint : Elist_Id) return Elist_Id
16983 is
16984 Explicitly_Discriminated_Type : Entity_Id;
16985 Expansion : Elist_Id;
16986 Discriminant : Entity_Id;
16987
16988 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16989 -- Find the nearest type that actually specifies discriminants
16990
16991 ---------------------------------
16992 -- Type_With_Explicit_Discrims --
16993 ---------------------------------
16994
16995 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16996 Typ : constant E := Base_Type (Id);
16997
16998 begin
16999 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17000 if Present (Full_View (Typ)) then
17001 return Type_With_Explicit_Discrims (Full_View (Typ));
17002 end if;
17003
17004 else
17005 if Has_Discriminants (Typ) then
17006 return Typ;
17007 end if;
17008 end if;
17009
17010 if Etype (Typ) = Typ then
17011 return Empty;
17012 elsif Has_Discriminants (Typ) then
17013 return Typ;
17014 else
17015 return Type_With_Explicit_Discrims (Etype (Typ));
17016 end if;
17017
17018 end Type_With_Explicit_Discrims;
17019
17020 -- Start of processing for Expand_To_Stored_Constraint
17021
17022 begin
17023 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17024 return No_Elist;
17025 end if;
17026
17027 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17028
17029 if No (Explicitly_Discriminated_Type) then
17030 return No_Elist;
17031 end if;
17032
17033 Expansion := New_Elmt_List;
17034
17035 Discriminant :=
17036 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17037 while Present (Discriminant) loop
17038 Append_Elmt
17039 (Get_Discriminant_Value
17040 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17041 To => Expansion);
17042 Next_Stored_Discriminant (Discriminant);
17043 end loop;
17044
17045 return Expansion;
17046 end Expand_To_Stored_Constraint;
17047
17048 ---------------------------
17049 -- Find_Hidden_Interface --
17050 ---------------------------
17051
17052 function Find_Hidden_Interface
17053 (Src : Elist_Id;
17054 Dest : Elist_Id) return Entity_Id
17055 is
17056 Iface : Entity_Id;
17057 Iface_Elmt : Elmt_Id;
17058
17059 begin
17060 if Present (Src) and then Present (Dest) then
17061 Iface_Elmt := First_Elmt (Src);
17062 while Present (Iface_Elmt) loop
17063 Iface := Node (Iface_Elmt);
17064
17065 if Is_Interface (Iface)
17066 and then not Contain_Interface (Iface, Dest)
17067 then
17068 return Iface;
17069 end if;
17070
17071 Next_Elmt (Iface_Elmt);
17072 end loop;
17073 end if;
17074
17075 return Empty;
17076 end Find_Hidden_Interface;
17077
17078 --------------------
17079 -- Find_Type_Name --
17080 --------------------
17081
17082 function Find_Type_Name (N : Node_Id) return Entity_Id is
17083 Id : constant Entity_Id := Defining_Identifier (N);
17084 New_Id : Entity_Id;
17085 Prev : Entity_Id;
17086 Prev_Par : Node_Id;
17087
17088 procedure Check_Duplicate_Aspects;
17089 -- Check that aspects specified in a completion have not been specified
17090 -- already in the partial view.
17091
17092 procedure Tag_Mismatch;
17093 -- Diagnose a tagged partial view whose full view is untagged. We post
17094 -- the message on the full view, with a reference to the previous
17095 -- partial view. The partial view can be private or incomplete, and
17096 -- these are handled in a different manner, so we determine the position
17097 -- of the error message from the respective slocs of both.
17098
17099 -----------------------------
17100 -- Check_Duplicate_Aspects --
17101 -----------------------------
17102
17103 procedure Check_Duplicate_Aspects is
17104 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17105 -- Return the corresponding aspect of the partial view which matches
17106 -- the aspect id of Asp. Return Empty is no such aspect exists.
17107
17108 -----------------------------
17109 -- Get_Partial_View_Aspect --
17110 -----------------------------
17111
17112 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17113 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17114 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17115 Prev_Asp : Node_Id;
17116
17117 begin
17118 if Present (Prev_Asps) then
17119 Prev_Asp := First (Prev_Asps);
17120 while Present (Prev_Asp) loop
17121 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17122 return Prev_Asp;
17123 end if;
17124
17125 Next (Prev_Asp);
17126 end loop;
17127 end if;
17128
17129 return Empty;
17130 end Get_Partial_View_Aspect;
17131
17132 -- Local variables
17133
17134 Full_Asps : constant List_Id := Aspect_Specifications (N);
17135 Full_Asp : Node_Id;
17136 Part_Asp : Node_Id;
17137
17138 -- Start of processing for Check_Duplicate_Aspects
17139
17140 begin
17141 if Present (Full_Asps) then
17142 Full_Asp := First (Full_Asps);
17143 while Present (Full_Asp) loop
17144 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17145
17146 -- An aspect and its class-wide counterpart are two distinct
17147 -- aspects and may apply to both views of an entity.
17148
17149 if Present (Part_Asp)
17150 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17151 then
17152 Error_Msg_N
17153 ("aspect already specified in private declaration",
17154 Full_Asp);
17155
17156 Remove (Full_Asp);
17157 return;
17158 end if;
17159
17160 if Has_Discriminants (Prev)
17161 and then not Has_Unknown_Discriminants (Prev)
17162 and then Get_Aspect_Id (Full_Asp) =
17163 Aspect_Implicit_Dereference
17164 then
17165 Error_Msg_N
17166 ("cannot specify aspect if partial view has known "
17167 & "discriminants", Full_Asp);
17168 end if;
17169
17170 Next (Full_Asp);
17171 end loop;
17172 end if;
17173 end Check_Duplicate_Aspects;
17174
17175 ------------------
17176 -- Tag_Mismatch --
17177 ------------------
17178
17179 procedure Tag_Mismatch is
17180 begin
17181 if Sloc (Prev) < Sloc (Id) then
17182 if Ada_Version >= Ada_2012
17183 and then Nkind (N) = N_Private_Type_Declaration
17184 then
17185 Error_Msg_NE
17186 ("declaration of private } must be a tagged type ", Id, Prev);
17187 else
17188 Error_Msg_NE
17189 ("full declaration of } must be a tagged type ", Id, Prev);
17190 end if;
17191
17192 else
17193 if Ada_Version >= Ada_2012
17194 and then Nkind (N) = N_Private_Type_Declaration
17195 then
17196 Error_Msg_NE
17197 ("declaration of private } must be a tagged type ", Prev, Id);
17198 else
17199 Error_Msg_NE
17200 ("full declaration of } must be a tagged type ", Prev, Id);
17201 end if;
17202 end if;
17203 end Tag_Mismatch;
17204
17205 -- Start of processing for Find_Type_Name
17206
17207 begin
17208 -- Find incomplete declaration, if one was given
17209
17210 Prev := Current_Entity_In_Scope (Id);
17211
17212 -- New type declaration
17213
17214 if No (Prev) then
17215 Enter_Name (Id);
17216 return Id;
17217
17218 -- Previous declaration exists
17219
17220 else
17221 Prev_Par := Parent (Prev);
17222
17223 -- Error if not incomplete/private case except if previous
17224 -- declaration is implicit, etc. Enter_Name will emit error if
17225 -- appropriate.
17226
17227 if not Is_Incomplete_Or_Private_Type (Prev) then
17228 Enter_Name (Id);
17229 New_Id := Id;
17230
17231 -- Check invalid completion of private or incomplete type
17232
17233 elsif not Nkind_In (N, N_Full_Type_Declaration,
17234 N_Task_Type_Declaration,
17235 N_Protected_Type_Declaration)
17236 and then
17237 (Ada_Version < Ada_2012
17238 or else not Is_Incomplete_Type (Prev)
17239 or else not Nkind_In (N, N_Private_Type_Declaration,
17240 N_Private_Extension_Declaration))
17241 then
17242 -- Completion must be a full type declarations (RM 7.3(4))
17243
17244 Error_Msg_Sloc := Sloc (Prev);
17245 Error_Msg_NE ("invalid completion of }", Id, Prev);
17246
17247 -- Set scope of Id to avoid cascaded errors. Entity is never
17248 -- examined again, except when saving globals in generics.
17249
17250 Set_Scope (Id, Current_Scope);
17251 New_Id := Id;
17252
17253 -- If this is a repeated incomplete declaration, no further
17254 -- checks are possible.
17255
17256 if Nkind (N) = N_Incomplete_Type_Declaration then
17257 return Prev;
17258 end if;
17259
17260 -- Case of full declaration of incomplete type
17261
17262 elsif Ekind (Prev) = E_Incomplete_Type
17263 and then (Ada_Version < Ada_2012
17264 or else No (Full_View (Prev))
17265 or else not Is_Private_Type (Full_View (Prev)))
17266 then
17267 -- Indicate that the incomplete declaration has a matching full
17268 -- declaration. The defining occurrence of the incomplete
17269 -- declaration remains the visible one, and the procedure
17270 -- Get_Full_View dereferences it whenever the type is used.
17271
17272 if Present (Full_View (Prev)) then
17273 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17274 end if;
17275
17276 Set_Full_View (Prev, Id);
17277 Append_Entity (Id, Current_Scope);
17278 Set_Is_Public (Id, Is_Public (Prev));
17279 Set_Is_Internal (Id);
17280 New_Id := Prev;
17281
17282 -- If the incomplete view is tagged, a class_wide type has been
17283 -- created already. Use it for the private type as well, in order
17284 -- to prevent multiple incompatible class-wide types that may be
17285 -- created for self-referential anonymous access components.
17286
17287 if Is_Tagged_Type (Prev)
17288 and then Present (Class_Wide_Type (Prev))
17289 then
17290 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17291 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17292
17293 -- Type of the class-wide type is the current Id. Previously
17294 -- this was not done for private declarations because of order-
17295 -- of-elaboration issues in the back end, but gigi now handles
17296 -- this properly.
17297
17298 Set_Etype (Class_Wide_Type (Id), Id);
17299 end if;
17300
17301 -- Case of full declaration of private type
17302
17303 else
17304 -- If the private type was a completion of an incomplete type then
17305 -- update Prev to reference the private type
17306
17307 if Ada_Version >= Ada_2012
17308 and then Ekind (Prev) = E_Incomplete_Type
17309 and then Present (Full_View (Prev))
17310 and then Is_Private_Type (Full_View (Prev))
17311 then
17312 Prev := Full_View (Prev);
17313 Prev_Par := Parent (Prev);
17314 end if;
17315
17316 if Nkind (N) = N_Full_Type_Declaration
17317 and then Nkind_In
17318 (Type_Definition (N), N_Record_Definition,
17319 N_Derived_Type_Definition)
17320 and then Interface_Present (Type_Definition (N))
17321 then
17322 Error_Msg_N
17323 ("completion of private type cannot be an interface", N);
17324 end if;
17325
17326 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17327 if Etype (Prev) /= Prev then
17328
17329 -- Prev is a private subtype or a derived type, and needs
17330 -- no completion.
17331
17332 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17333 New_Id := Id;
17334
17335 elsif Ekind (Prev) = E_Private_Type
17336 and then Nkind_In (N, N_Task_Type_Declaration,
17337 N_Protected_Type_Declaration)
17338 then
17339 Error_Msg_N
17340 ("completion of nonlimited type cannot be limited", N);
17341
17342 elsif Ekind (Prev) = E_Record_Type_With_Private
17343 and then Nkind_In (N, N_Task_Type_Declaration,
17344 N_Protected_Type_Declaration)
17345 then
17346 if not Is_Limited_Record (Prev) then
17347 Error_Msg_N
17348 ("completion of nonlimited type cannot be limited", N);
17349
17350 elsif No (Interface_List (N)) then
17351 Error_Msg_N
17352 ("completion of tagged private type must be tagged",
17353 N);
17354 end if;
17355 end if;
17356
17357 -- Ada 2005 (AI-251): Private extension declaration of a task
17358 -- type or a protected type. This case arises when covering
17359 -- interface types.
17360
17361 elsif Nkind_In (N, N_Task_Type_Declaration,
17362 N_Protected_Type_Declaration)
17363 then
17364 null;
17365
17366 elsif Nkind (N) /= N_Full_Type_Declaration
17367 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17368 then
17369 Error_Msg_N
17370 ("full view of private extension must be an extension", N);
17371
17372 elsif not (Abstract_Present (Parent (Prev)))
17373 and then Abstract_Present (Type_Definition (N))
17374 then
17375 Error_Msg_N
17376 ("full view of non-abstract extension cannot be abstract", N);
17377 end if;
17378
17379 if not In_Private_Part (Current_Scope) then
17380 Error_Msg_N
17381 ("declaration of full view must appear in private part", N);
17382 end if;
17383
17384 if Ada_Version >= Ada_2012 then
17385 Check_Duplicate_Aspects;
17386 end if;
17387
17388 Copy_And_Swap (Prev, Id);
17389 Set_Has_Private_Declaration (Prev);
17390 Set_Has_Private_Declaration (Id);
17391
17392 -- AI12-0133: Indicate whether we have a partial view with
17393 -- unknown discriminants, in which case initialization of objects
17394 -- of the type do not receive an invariant check.
17395
17396 Set_Partial_View_Has_Unknown_Discr
17397 (Prev, Has_Unknown_Discriminants (Id));
17398
17399 -- Preserve aspect and iterator flags that may have been set on
17400 -- the partial view.
17401
17402 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17403 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17404
17405 -- If no error, propagate freeze_node from private to full view.
17406 -- It may have been generated for an early operational item.
17407
17408 if Present (Freeze_Node (Id))
17409 and then Serious_Errors_Detected = 0
17410 and then No (Full_View (Id))
17411 then
17412 Set_Freeze_Node (Prev, Freeze_Node (Id));
17413 Set_Freeze_Node (Id, Empty);
17414 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17415 end if;
17416
17417 Set_Full_View (Id, Prev);
17418 New_Id := Prev;
17419 end if;
17420
17421 -- Verify that full declaration conforms to partial one
17422
17423 if Is_Incomplete_Or_Private_Type (Prev)
17424 and then Present (Discriminant_Specifications (Prev_Par))
17425 then
17426 if Present (Discriminant_Specifications (N)) then
17427 if Ekind (Prev) = E_Incomplete_Type then
17428 Check_Discriminant_Conformance (N, Prev, Prev);
17429 else
17430 Check_Discriminant_Conformance (N, Prev, Id);
17431 end if;
17432
17433 else
17434 Error_Msg_N
17435 ("missing discriminants in full type declaration", N);
17436
17437 -- To avoid cascaded errors on subsequent use, share the
17438 -- discriminants of the partial view.
17439
17440 Set_Discriminant_Specifications (N,
17441 Discriminant_Specifications (Prev_Par));
17442 end if;
17443 end if;
17444
17445 -- A prior untagged partial view can have an associated class-wide
17446 -- type due to use of the class attribute, and in this case the full
17447 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17448 -- of incomplete tagged declarations, but we check for it.
17449
17450 if Is_Type (Prev)
17451 and then (Is_Tagged_Type (Prev)
17452 or else Present (Class_Wide_Type (Prev)))
17453 then
17454 -- Ada 2012 (AI05-0162): A private type may be the completion of
17455 -- an incomplete type.
17456
17457 if Ada_Version >= Ada_2012
17458 and then Is_Incomplete_Type (Prev)
17459 and then Nkind_In (N, N_Private_Type_Declaration,
17460 N_Private_Extension_Declaration)
17461 then
17462 -- No need to check private extensions since they are tagged
17463
17464 if Nkind (N) = N_Private_Type_Declaration
17465 and then not Tagged_Present (N)
17466 then
17467 Tag_Mismatch;
17468 end if;
17469
17470 -- The full declaration is either a tagged type (including
17471 -- a synchronized type that implements interfaces) or a
17472 -- type extension, otherwise this is an error.
17473
17474 elsif Nkind_In (N, N_Task_Type_Declaration,
17475 N_Protected_Type_Declaration)
17476 then
17477 if No (Interface_List (N)) and then not Error_Posted (N) then
17478 Tag_Mismatch;
17479 end if;
17480
17481 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17482
17483 -- Indicate that the previous declaration (tagged incomplete
17484 -- or private declaration) requires the same on the full one.
17485
17486 if not Tagged_Present (Type_Definition (N)) then
17487 Tag_Mismatch;
17488 Set_Is_Tagged_Type (Id);
17489 end if;
17490
17491 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17492 if No (Record_Extension_Part (Type_Definition (N))) then
17493 Error_Msg_NE
17494 ("full declaration of } must be a record extension",
17495 Prev, Id);
17496
17497 -- Set some attributes to produce a usable full view
17498
17499 Set_Is_Tagged_Type (Id);
17500 end if;
17501
17502 else
17503 Tag_Mismatch;
17504 end if;
17505 end if;
17506
17507 if Present (Prev)
17508 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17509 and then Present (Premature_Use (Parent (Prev)))
17510 then
17511 Error_Msg_Sloc := Sloc (N);
17512 Error_Msg_N
17513 ("\full declaration #", Premature_Use (Parent (Prev)));
17514 end if;
17515
17516 return New_Id;
17517 end if;
17518 end Find_Type_Name;
17519
17520 -------------------------
17521 -- Find_Type_Of_Object --
17522 -------------------------
17523
17524 function Find_Type_Of_Object
17525 (Obj_Def : Node_Id;
17526 Related_Nod : Node_Id) return Entity_Id
17527 is
17528 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17529 P : Node_Id := Parent (Obj_Def);
17530 T : Entity_Id;
17531 Nam : Name_Id;
17532
17533 begin
17534 -- If the parent is a component_definition node we climb to the
17535 -- component_declaration node
17536
17537 if Nkind (P) = N_Component_Definition then
17538 P := Parent (P);
17539 end if;
17540
17541 -- Case of an anonymous array subtype
17542
17543 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17544 N_Unconstrained_Array_Definition)
17545 then
17546 T := Empty;
17547 Array_Type_Declaration (T, Obj_Def);
17548
17549 -- Create an explicit subtype whenever possible
17550
17551 elsif Nkind (P) /= N_Component_Declaration
17552 and then Def_Kind = N_Subtype_Indication
17553 then
17554 -- Base name of subtype on object name, which will be unique in
17555 -- the current scope.
17556
17557 -- If this is a duplicate declaration, return base type, to avoid
17558 -- generating duplicate anonymous types.
17559
17560 if Error_Posted (P) then
17561 Analyze (Subtype_Mark (Obj_Def));
17562 return Entity (Subtype_Mark (Obj_Def));
17563 end if;
17564
17565 Nam :=
17566 New_External_Name
17567 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17568
17569 T := Make_Defining_Identifier (Sloc (P), Nam);
17570
17571 Insert_Action (Obj_Def,
17572 Make_Subtype_Declaration (Sloc (P),
17573 Defining_Identifier => T,
17574 Subtype_Indication => Relocate_Node (Obj_Def)));
17575
17576 -- This subtype may need freezing, and this will not be done
17577 -- automatically if the object declaration is not in declarative
17578 -- part. Since this is an object declaration, the type cannot always
17579 -- be frozen here. Deferred constants do not freeze their type
17580 -- (which often enough will be private).
17581
17582 if Nkind (P) = N_Object_Declaration
17583 and then Constant_Present (P)
17584 and then No (Expression (P))
17585 then
17586 null;
17587
17588 -- Here we freeze the base type of object type to catch premature use
17589 -- of discriminated private type without a full view.
17590
17591 else
17592 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17593 end if;
17594
17595 -- Ada 2005 AI-406: the object definition in an object declaration
17596 -- can be an access definition.
17597
17598 elsif Def_Kind = N_Access_Definition then
17599 T := Access_Definition (Related_Nod, Obj_Def);
17600
17601 Set_Is_Local_Anonymous_Access
17602 (T,
17603 V => (Ada_Version < Ada_2012)
17604 or else (Nkind (P) /= N_Object_Declaration)
17605 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17606
17607 -- Otherwise, the object definition is just a subtype_mark
17608
17609 else
17610 T := Process_Subtype (Obj_Def, Related_Nod);
17611
17612 -- If expansion is disabled an object definition that is an aggregate
17613 -- will not get expanded and may lead to scoping problems in the back
17614 -- end, if the object is referenced in an inner scope. In that case
17615 -- create an itype reference for the object definition now. This
17616 -- may be redundant in some cases, but harmless.
17617
17618 if Is_Itype (T)
17619 and then Nkind (Related_Nod) = N_Object_Declaration
17620 and then ASIS_Mode
17621 then
17622 Build_Itype_Reference (T, Related_Nod);
17623 end if;
17624 end if;
17625
17626 return T;
17627 end Find_Type_Of_Object;
17628
17629 --------------------------------
17630 -- Find_Type_Of_Subtype_Indic --
17631 --------------------------------
17632
17633 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17634 Typ : Entity_Id;
17635
17636 begin
17637 -- Case of subtype mark with a constraint
17638
17639 if Nkind (S) = N_Subtype_Indication then
17640 Find_Type (Subtype_Mark (S));
17641 Typ := Entity (Subtype_Mark (S));
17642
17643 if not
17644 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17645 then
17646 Error_Msg_N
17647 ("incorrect constraint for this kind of type", Constraint (S));
17648 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17649 end if;
17650
17651 -- Otherwise we have a subtype mark without a constraint
17652
17653 elsif Error_Posted (S) then
17654 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17655 return Any_Type;
17656
17657 else
17658 Find_Type (S);
17659 Typ := Entity (S);
17660 end if;
17661
17662 -- Check No_Wide_Characters restriction
17663
17664 Check_Wide_Character_Restriction (Typ, S);
17665
17666 return Typ;
17667 end Find_Type_Of_Subtype_Indic;
17668
17669 -------------------------------------
17670 -- Floating_Point_Type_Declaration --
17671 -------------------------------------
17672
17673 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17674 Digs : constant Node_Id := Digits_Expression (Def);
17675 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17676 Digs_Val : Uint;
17677 Base_Typ : Entity_Id;
17678 Implicit_Base : Entity_Id;
17679 Bound : Node_Id;
17680
17681 function Can_Derive_From (E : Entity_Id) return Boolean;
17682 -- Find if given digits value, and possibly a specified range, allows
17683 -- derivation from specified type
17684
17685 function Find_Base_Type return Entity_Id;
17686 -- Find a predefined base type that Def can derive from, or generate
17687 -- an error and substitute Long_Long_Float if none exists.
17688
17689 ---------------------
17690 -- Can_Derive_From --
17691 ---------------------
17692
17693 function Can_Derive_From (E : Entity_Id) return Boolean is
17694 Spec : constant Entity_Id := Real_Range_Specification (Def);
17695
17696 begin
17697 -- Check specified "digits" constraint
17698
17699 if Digs_Val > Digits_Value (E) then
17700 return False;
17701 end if;
17702
17703 -- Check for matching range, if specified
17704
17705 if Present (Spec) then
17706 if Expr_Value_R (Type_Low_Bound (E)) >
17707 Expr_Value_R (Low_Bound (Spec))
17708 then
17709 return False;
17710 end if;
17711
17712 if Expr_Value_R (Type_High_Bound (E)) <
17713 Expr_Value_R (High_Bound (Spec))
17714 then
17715 return False;
17716 end if;
17717 end if;
17718
17719 return True;
17720 end Can_Derive_From;
17721
17722 --------------------
17723 -- Find_Base_Type --
17724 --------------------
17725
17726 function Find_Base_Type return Entity_Id is
17727 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17728
17729 begin
17730 -- Iterate over the predefined types in order, returning the first
17731 -- one that Def can derive from.
17732
17733 while Present (Choice) loop
17734 if Can_Derive_From (Node (Choice)) then
17735 return Node (Choice);
17736 end if;
17737
17738 Next_Elmt (Choice);
17739 end loop;
17740
17741 -- If we can't derive from any existing type, use Long_Long_Float
17742 -- and give appropriate message explaining the problem.
17743
17744 if Digs_Val > Max_Digs_Val then
17745 -- It might be the case that there is a type with the requested
17746 -- range, just not the combination of digits and range.
17747
17748 Error_Msg_N
17749 ("no predefined type has requested range and precision",
17750 Real_Range_Specification (Def));
17751
17752 else
17753 Error_Msg_N
17754 ("range too large for any predefined type",
17755 Real_Range_Specification (Def));
17756 end if;
17757
17758 return Standard_Long_Long_Float;
17759 end Find_Base_Type;
17760
17761 -- Start of processing for Floating_Point_Type_Declaration
17762
17763 begin
17764 Check_Restriction (No_Floating_Point, Def);
17765
17766 -- Create an implicit base type
17767
17768 Implicit_Base :=
17769 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17770
17771 -- Analyze and verify digits value
17772
17773 Analyze_And_Resolve (Digs, Any_Integer);
17774 Check_Digits_Expression (Digs);
17775 Digs_Val := Expr_Value (Digs);
17776
17777 -- Process possible range spec and find correct type to derive from
17778
17779 Process_Real_Range_Specification (Def);
17780
17781 -- Check that requested number of digits is not too high.
17782
17783 if Digs_Val > Max_Digs_Val then
17784
17785 -- The check for Max_Base_Digits may be somewhat expensive, as it
17786 -- requires reading System, so only do it when necessary.
17787
17788 declare
17789 Max_Base_Digits : constant Uint :=
17790 Expr_Value
17791 (Expression
17792 (Parent (RTE (RE_Max_Base_Digits))));
17793
17794 begin
17795 if Digs_Val > Max_Base_Digits then
17796 Error_Msg_Uint_1 := Max_Base_Digits;
17797 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17798
17799 elsif No (Real_Range_Specification (Def)) then
17800 Error_Msg_Uint_1 := Max_Digs_Val;
17801 Error_Msg_N ("types with more than ^ digits need range spec "
17802 & "(RM 3.5.7(6))", Digs);
17803 end if;
17804 end;
17805 end if;
17806
17807 -- Find a suitable type to derive from or complain and use a substitute
17808
17809 Base_Typ := Find_Base_Type;
17810
17811 -- If there are bounds given in the declaration use them as the bounds
17812 -- of the type, otherwise use the bounds of the predefined base type
17813 -- that was chosen based on the Digits value.
17814
17815 if Present (Real_Range_Specification (Def)) then
17816 Set_Scalar_Range (T, Real_Range_Specification (Def));
17817 Set_Is_Constrained (T);
17818
17819 -- The bounds of this range must be converted to machine numbers
17820 -- in accordance with RM 4.9(38).
17821
17822 Bound := Type_Low_Bound (T);
17823
17824 if Nkind (Bound) = N_Real_Literal then
17825 Set_Realval
17826 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17827 Set_Is_Machine_Number (Bound);
17828 end if;
17829
17830 Bound := Type_High_Bound (T);
17831
17832 if Nkind (Bound) = N_Real_Literal then
17833 Set_Realval
17834 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17835 Set_Is_Machine_Number (Bound);
17836 end if;
17837
17838 else
17839 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17840 end if;
17841
17842 -- Complete definition of implicit base and declared first subtype. The
17843 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17844 -- are not clobbered when the floating point type acts as a full view of
17845 -- a private type.
17846
17847 Set_Etype (Implicit_Base, Base_Typ);
17848 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17849 Set_Size_Info (Implicit_Base, Base_Typ);
17850 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17851 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17852 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17853 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17854
17855 Set_Ekind (T, E_Floating_Point_Subtype);
17856 Set_Etype (T, Implicit_Base);
17857 Set_Size_Info (T, Implicit_Base);
17858 Set_RM_Size (T, RM_Size (Implicit_Base));
17859 Inherit_Rep_Item_Chain (T, Implicit_Base);
17860 Set_Digits_Value (T, Digs_Val);
17861 end Floating_Point_Type_Declaration;
17862
17863 ----------------------------
17864 -- Get_Discriminant_Value --
17865 ----------------------------
17866
17867 -- This is the situation:
17868
17869 -- There is a non-derived type
17870
17871 -- type T0 (Dx, Dy, Dz...)
17872
17873 -- There are zero or more levels of derivation, with each derivation
17874 -- either purely inheriting the discriminants, or defining its own.
17875
17876 -- type Ti is new Ti-1
17877 -- or
17878 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17879 -- or
17880 -- subtype Ti is ...
17881
17882 -- The subtype issue is avoided by the use of Original_Record_Component,
17883 -- and the fact that derived subtypes also derive the constraints.
17884
17885 -- This chain leads back from
17886
17887 -- Typ_For_Constraint
17888
17889 -- Typ_For_Constraint has discriminants, and the value for each
17890 -- discriminant is given by its corresponding Elmt of Constraints.
17891
17892 -- Discriminant is some discriminant in this hierarchy
17893
17894 -- We need to return its value
17895
17896 -- We do this by recursively searching each level, and looking for
17897 -- Discriminant. Once we get to the bottom, we start backing up
17898 -- returning the value for it which may in turn be a discriminant
17899 -- further up, so on the backup we continue the substitution.
17900
17901 function Get_Discriminant_Value
17902 (Discriminant : Entity_Id;
17903 Typ_For_Constraint : Entity_Id;
17904 Constraint : Elist_Id) return Node_Id
17905 is
17906 function Root_Corresponding_Discriminant
17907 (Discr : Entity_Id) return Entity_Id;
17908 -- Given a discriminant, traverse the chain of inherited discriminants
17909 -- and return the topmost discriminant.
17910
17911 function Search_Derivation_Levels
17912 (Ti : Entity_Id;
17913 Discrim_Values : Elist_Id;
17914 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17915 -- This is the routine that performs the recursive search of levels
17916 -- as described above.
17917
17918 -------------------------------------
17919 -- Root_Corresponding_Discriminant --
17920 -------------------------------------
17921
17922 function Root_Corresponding_Discriminant
17923 (Discr : Entity_Id) return Entity_Id
17924 is
17925 D : Entity_Id;
17926
17927 begin
17928 D := Discr;
17929 while Present (Corresponding_Discriminant (D)) loop
17930 D := Corresponding_Discriminant (D);
17931 end loop;
17932
17933 return D;
17934 end Root_Corresponding_Discriminant;
17935
17936 ------------------------------
17937 -- Search_Derivation_Levels --
17938 ------------------------------
17939
17940 function Search_Derivation_Levels
17941 (Ti : Entity_Id;
17942 Discrim_Values : Elist_Id;
17943 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17944 is
17945 Assoc : Elmt_Id;
17946 Disc : Entity_Id;
17947 Result : Node_Or_Entity_Id;
17948 Result_Entity : Node_Id;
17949
17950 begin
17951 -- If inappropriate type, return Error, this happens only in
17952 -- cascaded error situations, and we want to avoid a blow up.
17953
17954 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17955 return Error;
17956 end if;
17957
17958 -- Look deeper if possible. Use Stored_Constraints only for
17959 -- untagged types. For tagged types use the given constraint.
17960 -- This asymmetry needs explanation???
17961
17962 if not Stored_Discrim_Values
17963 and then Present (Stored_Constraint (Ti))
17964 and then not Is_Tagged_Type (Ti)
17965 then
17966 Result :=
17967 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17968 else
17969 declare
17970 Td : constant Entity_Id := Etype (Ti);
17971
17972 begin
17973 if Td = Ti then
17974 Result := Discriminant;
17975
17976 else
17977 if Present (Stored_Constraint (Ti)) then
17978 Result :=
17979 Search_Derivation_Levels
17980 (Td, Stored_Constraint (Ti), True);
17981 else
17982 Result :=
17983 Search_Derivation_Levels
17984 (Td, Discrim_Values, Stored_Discrim_Values);
17985 end if;
17986 end if;
17987 end;
17988 end if;
17989
17990 -- Extra underlying places to search, if not found above. For
17991 -- concurrent types, the relevant discriminant appears in the
17992 -- corresponding record. For a type derived from a private type
17993 -- without discriminant, the full view inherits the discriminants
17994 -- of the full view of the parent.
17995
17996 if Result = Discriminant then
17997 if Is_Concurrent_Type (Ti)
17998 and then Present (Corresponding_Record_Type (Ti))
17999 then
18000 Result :=
18001 Search_Derivation_Levels (
18002 Corresponding_Record_Type (Ti),
18003 Discrim_Values,
18004 Stored_Discrim_Values);
18005
18006 elsif Is_Private_Type (Ti)
18007 and then not Has_Discriminants (Ti)
18008 and then Present (Full_View (Ti))
18009 and then Etype (Full_View (Ti)) /= Ti
18010 then
18011 Result :=
18012 Search_Derivation_Levels (
18013 Full_View (Ti),
18014 Discrim_Values,
18015 Stored_Discrim_Values);
18016 end if;
18017 end if;
18018
18019 -- If Result is not a (reference to a) discriminant, return it,
18020 -- otherwise set Result_Entity to the discriminant.
18021
18022 if Nkind (Result) = N_Defining_Identifier then
18023 pragma Assert (Result = Discriminant);
18024 Result_Entity := Result;
18025
18026 else
18027 if not Denotes_Discriminant (Result) then
18028 return Result;
18029 end if;
18030
18031 Result_Entity := Entity (Result);
18032 end if;
18033
18034 -- See if this level of derivation actually has discriminants because
18035 -- tagged derivations can add them, hence the lower levels need not
18036 -- have any.
18037
18038 if not Has_Discriminants (Ti) then
18039 return Result;
18040 end if;
18041
18042 -- Scan Ti's discriminants for Result_Entity, and return its
18043 -- corresponding value, if any.
18044
18045 Result_Entity := Original_Record_Component (Result_Entity);
18046
18047 Assoc := First_Elmt (Discrim_Values);
18048
18049 if Stored_Discrim_Values then
18050 Disc := First_Stored_Discriminant (Ti);
18051 else
18052 Disc := First_Discriminant (Ti);
18053 end if;
18054
18055 while Present (Disc) loop
18056
18057 -- If no further associations return the discriminant, value will
18058 -- be found on the second pass.
18059
18060 if No (Assoc) then
18061 return Result;
18062 end if;
18063
18064 if Original_Record_Component (Disc) = Result_Entity then
18065 return Node (Assoc);
18066 end if;
18067
18068 Next_Elmt (Assoc);
18069
18070 if Stored_Discrim_Values then
18071 Next_Stored_Discriminant (Disc);
18072 else
18073 Next_Discriminant (Disc);
18074 end if;
18075 end loop;
18076
18077 -- Could not find it
18078
18079 return Result;
18080 end Search_Derivation_Levels;
18081
18082 -- Local Variables
18083
18084 Result : Node_Or_Entity_Id;
18085
18086 -- Start of processing for Get_Discriminant_Value
18087
18088 begin
18089 -- ??? This routine is a gigantic mess and will be deleted. For the
18090 -- time being just test for the trivial case before calling recurse.
18091
18092 -- We are now celebrating the 20th anniversary of this comment!
18093
18094 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18095 declare
18096 D : Entity_Id;
18097 E : Elmt_Id;
18098
18099 begin
18100 D := First_Discriminant (Typ_For_Constraint);
18101 E := First_Elmt (Constraint);
18102 while Present (D) loop
18103 if Chars (D) = Chars (Discriminant) then
18104 return Node (E);
18105 end if;
18106
18107 Next_Discriminant (D);
18108 Next_Elmt (E);
18109 end loop;
18110 end;
18111 end if;
18112
18113 Result := Search_Derivation_Levels
18114 (Typ_For_Constraint, Constraint, False);
18115
18116 -- ??? hack to disappear when this routine is gone
18117
18118 if Nkind (Result) = N_Defining_Identifier then
18119 declare
18120 D : Entity_Id;
18121 E : Elmt_Id;
18122
18123 begin
18124 D := First_Discriminant (Typ_For_Constraint);
18125 E := First_Elmt (Constraint);
18126 while Present (D) loop
18127 if Root_Corresponding_Discriminant (D) = Discriminant then
18128 return Node (E);
18129 end if;
18130
18131 Next_Discriminant (D);
18132 Next_Elmt (E);
18133 end loop;
18134 end;
18135 end if;
18136
18137 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18138 return Result;
18139 end Get_Discriminant_Value;
18140
18141 --------------------------
18142 -- Has_Range_Constraint --
18143 --------------------------
18144
18145 function Has_Range_Constraint (N : Node_Id) return Boolean is
18146 C : constant Node_Id := Constraint (N);
18147
18148 begin
18149 if Nkind (C) = N_Range_Constraint then
18150 return True;
18151
18152 elsif Nkind (C) = N_Digits_Constraint then
18153 return
18154 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18155 or else Present (Range_Constraint (C));
18156
18157 elsif Nkind (C) = N_Delta_Constraint then
18158 return Present (Range_Constraint (C));
18159
18160 else
18161 return False;
18162 end if;
18163 end Has_Range_Constraint;
18164
18165 ------------------------
18166 -- Inherit_Components --
18167 ------------------------
18168
18169 function Inherit_Components
18170 (N : Node_Id;
18171 Parent_Base : Entity_Id;
18172 Derived_Base : Entity_Id;
18173 Is_Tagged : Boolean;
18174 Inherit_Discr : Boolean;
18175 Discs : Elist_Id) return Elist_Id
18176 is
18177 Assoc_List : constant Elist_Id := New_Elmt_List;
18178
18179 procedure Inherit_Component
18180 (Old_C : Entity_Id;
18181 Plain_Discrim : Boolean := False;
18182 Stored_Discrim : Boolean := False);
18183 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18184 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18185 -- True, Old_C is a stored discriminant. If they are both false then
18186 -- Old_C is a regular component.
18187
18188 -----------------------
18189 -- Inherit_Component --
18190 -----------------------
18191
18192 procedure Inherit_Component
18193 (Old_C : Entity_Id;
18194 Plain_Discrim : Boolean := False;
18195 Stored_Discrim : Boolean := False)
18196 is
18197 procedure Set_Anonymous_Type (Id : Entity_Id);
18198 -- Id denotes the entity of an access discriminant or anonymous
18199 -- access component. Set the type of Id to either the same type of
18200 -- Old_C or create a new one depending on whether the parent and
18201 -- the child types are in the same scope.
18202
18203 ------------------------
18204 -- Set_Anonymous_Type --
18205 ------------------------
18206
18207 procedure Set_Anonymous_Type (Id : Entity_Id) is
18208 Old_Typ : constant Entity_Id := Etype (Old_C);
18209
18210 begin
18211 if Scope (Parent_Base) = Scope (Derived_Base) then
18212 Set_Etype (Id, Old_Typ);
18213
18214 -- The parent and the derived type are in two different scopes.
18215 -- Reuse the type of the original discriminant / component by
18216 -- copying it in order to preserve all attributes.
18217
18218 else
18219 declare
18220 Typ : constant Entity_Id := New_Copy (Old_Typ);
18221
18222 begin
18223 Set_Etype (Id, Typ);
18224
18225 -- Since we do not generate component declarations for
18226 -- inherited components, associate the itype with the
18227 -- derived type.
18228
18229 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18230 Set_Scope (Typ, Derived_Base);
18231 end;
18232 end if;
18233 end Set_Anonymous_Type;
18234
18235 -- Local variables and constants
18236
18237 New_C : constant Entity_Id := New_Copy (Old_C);
18238
18239 Corr_Discrim : Entity_Id;
18240 Discrim : Entity_Id;
18241
18242 -- Start of processing for Inherit_Component
18243
18244 begin
18245 pragma Assert (not Is_Tagged or not Stored_Discrim);
18246
18247 Set_Parent (New_C, Parent (Old_C));
18248
18249 -- Regular discriminants and components must be inserted in the scope
18250 -- of the Derived_Base. Do it here.
18251
18252 if not Stored_Discrim then
18253 Enter_Name (New_C);
18254 end if;
18255
18256 -- For tagged types the Original_Record_Component must point to
18257 -- whatever this field was pointing to in the parent type. This has
18258 -- already been achieved by the call to New_Copy above.
18259
18260 if not Is_Tagged then
18261 Set_Original_Record_Component (New_C, New_C);
18262 Set_Corresponding_Record_Component (New_C, Old_C);
18263 end if;
18264
18265 -- Set the proper type of an access discriminant
18266
18267 if Ekind (New_C) = E_Discriminant
18268 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18269 then
18270 Set_Anonymous_Type (New_C);
18271 end if;
18272
18273 -- If we have inherited a component then see if its Etype contains
18274 -- references to Parent_Base discriminants. In this case, replace
18275 -- these references with the constraints given in Discs. We do not
18276 -- do this for the partial view of private types because this is
18277 -- not needed (only the components of the full view will be used
18278 -- for code generation) and cause problem. We also avoid this
18279 -- transformation in some error situations.
18280
18281 if Ekind (New_C) = E_Component then
18282
18283 -- Set the proper type of an anonymous access component
18284
18285 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18286 Set_Anonymous_Type (New_C);
18287
18288 elsif (Is_Private_Type (Derived_Base)
18289 and then not Is_Generic_Type (Derived_Base))
18290 or else (Is_Empty_Elmt_List (Discs)
18291 and then not Expander_Active)
18292 then
18293 Set_Etype (New_C, Etype (Old_C));
18294
18295 else
18296 -- The current component introduces a circularity of the
18297 -- following kind:
18298
18299 -- limited with Pack_2;
18300 -- package Pack_1 is
18301 -- type T_1 is tagged record
18302 -- Comp : access Pack_2.T_2;
18303 -- ...
18304 -- end record;
18305 -- end Pack_1;
18306
18307 -- with Pack_1;
18308 -- package Pack_2 is
18309 -- type T_2 is new Pack_1.T_1 with ...;
18310 -- end Pack_2;
18311
18312 Set_Etype
18313 (New_C,
18314 Constrain_Component_Type
18315 (Old_C, Derived_Base, N, Parent_Base, Discs));
18316 end if;
18317 end if;
18318
18319 -- In derived tagged types it is illegal to reference a non
18320 -- discriminant component in the parent type. To catch this, mark
18321 -- these components with an Ekind of E_Void. This will be reset in
18322 -- Record_Type_Definition after processing the record extension of
18323 -- the derived type.
18324
18325 -- If the declaration is a private extension, there is no further
18326 -- record extension to process, and the components retain their
18327 -- current kind, because they are visible at this point.
18328
18329 if Is_Tagged and then Ekind (New_C) = E_Component
18330 and then Nkind (N) /= N_Private_Extension_Declaration
18331 then
18332 Set_Ekind (New_C, E_Void);
18333 end if;
18334
18335 if Plain_Discrim then
18336 Set_Corresponding_Discriminant (New_C, Old_C);
18337 Build_Discriminal (New_C);
18338
18339 -- If we are explicitly inheriting a stored discriminant it will be
18340 -- completely hidden.
18341
18342 elsif Stored_Discrim then
18343 Set_Corresponding_Discriminant (New_C, Empty);
18344 Set_Discriminal (New_C, Empty);
18345 Set_Is_Completely_Hidden (New_C);
18346
18347 -- Set the Original_Record_Component of each discriminant in the
18348 -- derived base to point to the corresponding stored that we just
18349 -- created.
18350
18351 Discrim := First_Discriminant (Derived_Base);
18352 while Present (Discrim) loop
18353 Corr_Discrim := Corresponding_Discriminant (Discrim);
18354
18355 -- Corr_Discrim could be missing in an error situation
18356
18357 if Present (Corr_Discrim)
18358 and then Original_Record_Component (Corr_Discrim) = Old_C
18359 then
18360 Set_Original_Record_Component (Discrim, New_C);
18361 Set_Corresponding_Record_Component (Discrim, Empty);
18362 end if;
18363
18364 Next_Discriminant (Discrim);
18365 end loop;
18366
18367 Append_Entity (New_C, Derived_Base);
18368 end if;
18369
18370 if not Is_Tagged then
18371 Append_Elmt (Old_C, Assoc_List);
18372 Append_Elmt (New_C, Assoc_List);
18373 end if;
18374 end Inherit_Component;
18375
18376 -- Variables local to Inherit_Component
18377
18378 Loc : constant Source_Ptr := Sloc (N);
18379
18380 Parent_Discrim : Entity_Id;
18381 Stored_Discrim : Entity_Id;
18382 D : Entity_Id;
18383 Component : Entity_Id;
18384
18385 -- Start of processing for Inherit_Components
18386
18387 begin
18388 if not Is_Tagged then
18389 Append_Elmt (Parent_Base, Assoc_List);
18390 Append_Elmt (Derived_Base, Assoc_List);
18391 end if;
18392
18393 -- Inherit parent discriminants if needed
18394
18395 if Inherit_Discr then
18396 Parent_Discrim := First_Discriminant (Parent_Base);
18397 while Present (Parent_Discrim) loop
18398 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18399 Next_Discriminant (Parent_Discrim);
18400 end loop;
18401 end if;
18402
18403 -- Create explicit stored discrims for untagged types when necessary
18404
18405 if not Has_Unknown_Discriminants (Derived_Base)
18406 and then Has_Discriminants (Parent_Base)
18407 and then not Is_Tagged
18408 and then
18409 (not Inherit_Discr
18410 or else First_Discriminant (Parent_Base) /=
18411 First_Stored_Discriminant (Parent_Base))
18412 then
18413 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18414 while Present (Stored_Discrim) loop
18415 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18416 Next_Stored_Discriminant (Stored_Discrim);
18417 end loop;
18418 end if;
18419
18420 -- See if we can apply the second transformation for derived types, as
18421 -- explained in point 6. in the comments above Build_Derived_Record_Type
18422 -- This is achieved by appending Derived_Base discriminants into Discs,
18423 -- which has the side effect of returning a non empty Discs list to the
18424 -- caller of Inherit_Components, which is what we want. This must be
18425 -- done for private derived types if there are explicit stored
18426 -- discriminants, to ensure that we can retrieve the values of the
18427 -- constraints provided in the ancestors.
18428
18429 if Inherit_Discr
18430 and then Is_Empty_Elmt_List (Discs)
18431 and then Present (First_Discriminant (Derived_Base))
18432 and then
18433 (not Is_Private_Type (Derived_Base)
18434 or else Is_Completely_Hidden
18435 (First_Stored_Discriminant (Derived_Base))
18436 or else Is_Generic_Type (Derived_Base))
18437 then
18438 D := First_Discriminant (Derived_Base);
18439 while Present (D) loop
18440 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18441 Next_Discriminant (D);
18442 end loop;
18443 end if;
18444
18445 -- Finally, inherit non-discriminant components unless they are not
18446 -- visible because defined or inherited from the full view of the
18447 -- parent. Don't inherit the _parent field of the parent type.
18448
18449 Component := First_Entity (Parent_Base);
18450 while Present (Component) loop
18451
18452 -- Ada 2005 (AI-251): Do not inherit components associated with
18453 -- secondary tags of the parent.
18454
18455 if Ekind (Component) = E_Component
18456 and then Present (Related_Type (Component))
18457 then
18458 null;
18459
18460 elsif Ekind (Component) /= E_Component
18461 or else Chars (Component) = Name_uParent
18462 then
18463 null;
18464
18465 -- If the derived type is within the parent type's declarative
18466 -- region, then the components can still be inherited even though
18467 -- they aren't visible at this point. This can occur for cases
18468 -- such as within public child units where the components must
18469 -- become visible upon entering the child unit's private part.
18470
18471 elsif not Is_Visible_Component (Component)
18472 and then not In_Open_Scopes (Scope (Parent_Base))
18473 then
18474 null;
18475
18476 elsif Ekind_In (Derived_Base, E_Private_Type,
18477 E_Limited_Private_Type)
18478 then
18479 null;
18480
18481 else
18482 Inherit_Component (Component);
18483 end if;
18484
18485 Next_Entity (Component);
18486 end loop;
18487
18488 -- For tagged derived types, inherited discriminants cannot be used in
18489 -- component declarations of the record extension part. To achieve this
18490 -- we mark the inherited discriminants as not visible.
18491
18492 if Is_Tagged and then Inherit_Discr then
18493 D := First_Discriminant (Derived_Base);
18494 while Present (D) loop
18495 Set_Is_Immediately_Visible (D, False);
18496 Next_Discriminant (D);
18497 end loop;
18498 end if;
18499
18500 return Assoc_List;
18501 end Inherit_Components;
18502
18503 -----------------------------
18504 -- Inherit_Predicate_Flags --
18505 -----------------------------
18506
18507 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18508 begin
18509 Set_Has_Predicates (Subt, Has_Predicates (Par));
18510 Set_Has_Static_Predicate_Aspect
18511 (Subt, Has_Static_Predicate_Aspect (Par));
18512 Set_Has_Dynamic_Predicate_Aspect
18513 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18514
18515 -- A named subtype does not inherit the predicate function of its
18516 -- parent but an itype declared for a loop index needs the discrete
18517 -- predicate information of its parent to execute the loop properly.
18518
18519 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18520 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18521
18522 if Has_Static_Predicate (Par) then
18523 Set_Static_Discrete_Predicate
18524 (Subt, Static_Discrete_Predicate (Par));
18525 end if;
18526 end if;
18527 end Inherit_Predicate_Flags;
18528
18529 ----------------------
18530 -- Is_EVF_Procedure --
18531 ----------------------
18532
18533 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18534 Formal : Entity_Id;
18535
18536 begin
18537 -- Examine the formals of an Extensions_Visible False procedure looking
18538 -- for a controlling OUT parameter.
18539
18540 if Ekind (Subp) = E_Procedure
18541 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18542 then
18543 Formal := First_Formal (Subp);
18544 while Present (Formal) loop
18545 if Ekind (Formal) = E_Out_Parameter
18546 and then Is_Controlling_Formal (Formal)
18547 then
18548 return True;
18549 end if;
18550
18551 Next_Formal (Formal);
18552 end loop;
18553 end if;
18554
18555 return False;
18556 end Is_EVF_Procedure;
18557
18558 -----------------------
18559 -- Is_Null_Extension --
18560 -----------------------
18561
18562 function Is_Null_Extension (T : Entity_Id) return Boolean is
18563 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18564 Comp_List : Node_Id;
18565 Comp : Node_Id;
18566
18567 begin
18568 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18569 or else not Is_Tagged_Type (T)
18570 or else Nkind (Type_Definition (Type_Decl)) /=
18571 N_Derived_Type_Definition
18572 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18573 then
18574 return False;
18575 end if;
18576
18577 Comp_List :=
18578 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18579
18580 if Present (Discriminant_Specifications (Type_Decl)) then
18581 return False;
18582
18583 elsif Present (Comp_List)
18584 and then Is_Non_Empty_List (Component_Items (Comp_List))
18585 then
18586 Comp := First (Component_Items (Comp_List));
18587
18588 -- Only user-defined components are relevant. The component list
18589 -- may also contain a parent component and internal components
18590 -- corresponding to secondary tags, but these do not determine
18591 -- whether this is a null extension.
18592
18593 while Present (Comp) loop
18594 if Comes_From_Source (Comp) then
18595 return False;
18596 end if;
18597
18598 Next (Comp);
18599 end loop;
18600
18601 return True;
18602
18603 else
18604 return True;
18605 end if;
18606 end Is_Null_Extension;
18607
18608 ------------------------------
18609 -- Is_Valid_Constraint_Kind --
18610 ------------------------------
18611
18612 function Is_Valid_Constraint_Kind
18613 (T_Kind : Type_Kind;
18614 Constraint_Kind : Node_Kind) return Boolean
18615 is
18616 begin
18617 case T_Kind is
18618 when Enumeration_Kind
18619 | Integer_Kind
18620 =>
18621 return Constraint_Kind = N_Range_Constraint;
18622
18623 when Decimal_Fixed_Point_Kind =>
18624 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18625 N_Range_Constraint);
18626
18627 when Ordinary_Fixed_Point_Kind =>
18628 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18629 N_Range_Constraint);
18630
18631 when Float_Kind =>
18632 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18633 N_Range_Constraint);
18634
18635 when Access_Kind
18636 | Array_Kind
18637 | Class_Wide_Kind
18638 | Concurrent_Kind
18639 | Private_Kind
18640 | E_Incomplete_Type
18641 | E_Record_Subtype
18642 | E_Record_Type
18643 =>
18644 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18645
18646 when others =>
18647 return True; -- Error will be detected later
18648 end case;
18649 end Is_Valid_Constraint_Kind;
18650
18651 --------------------------
18652 -- Is_Visible_Component --
18653 --------------------------
18654
18655 function Is_Visible_Component
18656 (C : Entity_Id;
18657 N : Node_Id := Empty) return Boolean
18658 is
18659 Original_Comp : Entity_Id := Empty;
18660 Original_Type : Entity_Id;
18661 Type_Scope : Entity_Id;
18662
18663 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18664 -- Check whether parent type of inherited component is declared locally,
18665 -- possibly within a nested package or instance. The current scope is
18666 -- the derived record itself.
18667
18668 -------------------
18669 -- Is_Local_Type --
18670 -------------------
18671
18672 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18673 Scop : Entity_Id;
18674
18675 begin
18676 Scop := Scope (Typ);
18677 while Present (Scop)
18678 and then Scop /= Standard_Standard
18679 loop
18680 if Scop = Scope (Current_Scope) then
18681 return True;
18682 end if;
18683
18684 Scop := Scope (Scop);
18685 end loop;
18686
18687 return False;
18688 end Is_Local_Type;
18689
18690 -- Start of processing for Is_Visible_Component
18691
18692 begin
18693 if Ekind_In (C, E_Component, E_Discriminant) then
18694 Original_Comp := Original_Record_Component (C);
18695 end if;
18696
18697 if No (Original_Comp) then
18698
18699 -- Premature usage, or previous error
18700
18701 return False;
18702
18703 else
18704 Original_Type := Scope (Original_Comp);
18705 Type_Scope := Scope (Base_Type (Scope (C)));
18706 end if;
18707
18708 -- This test only concerns tagged types
18709
18710 if not Is_Tagged_Type (Original_Type) then
18711 return True;
18712
18713 -- If it is _Parent or _Tag, there is no visibility issue
18714
18715 elsif not Comes_From_Source (Original_Comp) then
18716 return True;
18717
18718 -- Discriminants are visible unless the (private) type has unknown
18719 -- discriminants. If the discriminant reference is inserted for a
18720 -- discriminant check on a full view it is also visible.
18721
18722 elsif Ekind (Original_Comp) = E_Discriminant
18723 and then
18724 (not Has_Unknown_Discriminants (Original_Type)
18725 or else (Present (N)
18726 and then Nkind (N) = N_Selected_Component
18727 and then Nkind (Prefix (N)) = N_Type_Conversion
18728 and then not Comes_From_Source (Prefix (N))))
18729 then
18730 return True;
18731
18732 -- In the body of an instantiation, check the visibility of a component
18733 -- in case it has a homograph that is a primitive operation of a private
18734 -- type which was not visible in the generic unit.
18735
18736 -- Should Is_Prefixed_Call be propagated from template to instance???
18737
18738 elsif In_Instance_Body then
18739 if not Is_Tagged_Type (Original_Type)
18740 or else not Is_Private_Type (Original_Type)
18741 then
18742 return True;
18743
18744 else
18745 declare
18746 Subp_Elmt : Elmt_Id;
18747
18748 begin
18749 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18750 while Present (Subp_Elmt) loop
18751
18752 -- The component is hidden by a primitive operation
18753
18754 if Chars (Node (Subp_Elmt)) = Chars (C) then
18755 return False;
18756 end if;
18757
18758 Next_Elmt (Subp_Elmt);
18759 end loop;
18760
18761 return True;
18762 end;
18763 end if;
18764
18765 -- If the component has been declared in an ancestor which is currently
18766 -- a private type, then it is not visible. The same applies if the
18767 -- component's containing type is not in an open scope and the original
18768 -- component's enclosing type is a visible full view of a private type
18769 -- (which can occur in cases where an attempt is being made to reference
18770 -- a component in a sibling package that is inherited from a visible
18771 -- component of a type in an ancestor package; the component in the
18772 -- sibling package should not be visible even though the component it
18773 -- inherited from is visible). This does not apply however in the case
18774 -- where the scope of the type is a private child unit, or when the
18775 -- parent comes from a local package in which the ancestor is currently
18776 -- visible. The latter suppression of visibility is needed for cases
18777 -- that are tested in B730006.
18778
18779 elsif Is_Private_Type (Original_Type)
18780 or else
18781 (not Is_Private_Descendant (Type_Scope)
18782 and then not In_Open_Scopes (Type_Scope)
18783 and then Has_Private_Declaration (Original_Type))
18784 then
18785 -- If the type derives from an entity in a formal package, there
18786 -- are no additional visible components.
18787
18788 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18789 N_Formal_Package_Declaration
18790 then
18791 return False;
18792
18793 -- if we are not in the private part of the current package, there
18794 -- are no additional visible components.
18795
18796 elsif Ekind (Scope (Current_Scope)) = E_Package
18797 and then not In_Private_Part (Scope (Current_Scope))
18798 then
18799 return False;
18800 else
18801 return
18802 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18803 and then In_Open_Scopes (Scope (Original_Type))
18804 and then Is_Local_Type (Type_Scope);
18805 end if;
18806
18807 -- There is another weird way in which a component may be invisible when
18808 -- the private and the full view are not derived from the same ancestor.
18809 -- Here is an example :
18810
18811 -- type A1 is tagged record F1 : integer; end record;
18812 -- type A2 is new A1 with record F2 : integer; end record;
18813 -- type T is new A1 with private;
18814 -- private
18815 -- type T is new A2 with null record;
18816
18817 -- In this case, the full view of T inherits F1 and F2 but the private
18818 -- view inherits only F1
18819
18820 else
18821 declare
18822 Ancestor : Entity_Id := Scope (C);
18823
18824 begin
18825 loop
18826 if Ancestor = Original_Type then
18827 return True;
18828
18829 -- The ancestor may have a partial view of the original type,
18830 -- but if the full view is in scope, as in a child body, the
18831 -- component is visible.
18832
18833 elsif In_Private_Part (Scope (Original_Type))
18834 and then Full_View (Ancestor) = Original_Type
18835 then
18836 return True;
18837
18838 elsif Ancestor = Etype (Ancestor) then
18839
18840 -- No further ancestors to examine
18841
18842 return False;
18843 end if;
18844
18845 Ancestor := Etype (Ancestor);
18846 end loop;
18847 end;
18848 end if;
18849 end Is_Visible_Component;
18850
18851 --------------------------
18852 -- Make_Class_Wide_Type --
18853 --------------------------
18854
18855 procedure Make_Class_Wide_Type (T : Entity_Id) is
18856 CW_Type : Entity_Id;
18857 CW_Name : Name_Id;
18858 Next_E : Entity_Id;
18859
18860 begin
18861 if Present (Class_Wide_Type (T)) then
18862
18863 -- The class-wide type is a partially decorated entity created for a
18864 -- unanalyzed tagged type referenced through a limited with clause.
18865 -- When the tagged type is analyzed, its class-wide type needs to be
18866 -- redecorated. Note that we reuse the entity created by Decorate_
18867 -- Tagged_Type in order to preserve all links.
18868
18869 if Materialize_Entity (Class_Wide_Type (T)) then
18870 CW_Type := Class_Wide_Type (T);
18871 Set_Materialize_Entity (CW_Type, False);
18872
18873 -- The class wide type can have been defined by the partial view, in
18874 -- which case everything is already done.
18875
18876 else
18877 return;
18878 end if;
18879
18880 -- Default case, we need to create a new class-wide type
18881
18882 else
18883 CW_Type :=
18884 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18885 end if;
18886
18887 -- Inherit root type characteristics
18888
18889 CW_Name := Chars (CW_Type);
18890 Next_E := Next_Entity (CW_Type);
18891 Copy_Node (T, CW_Type);
18892 Set_Comes_From_Source (CW_Type, False);
18893 Set_Chars (CW_Type, CW_Name);
18894 Set_Parent (CW_Type, Parent (T));
18895 Set_Next_Entity (CW_Type, Next_E);
18896
18897 -- Ensure we have a new freeze node for the class-wide type. The partial
18898 -- view may have freeze action of its own, requiring a proper freeze
18899 -- node, and the same freeze node cannot be shared between the two
18900 -- types.
18901
18902 Set_Has_Delayed_Freeze (CW_Type);
18903 Set_Freeze_Node (CW_Type, Empty);
18904
18905 -- Customize the class-wide type: It has no prim. op., it cannot be
18906 -- abstract, its Etype points back to the specific root type, and it
18907 -- cannot have any invariants.
18908
18909 Set_Ekind (CW_Type, E_Class_Wide_Type);
18910 Set_Is_Tagged_Type (CW_Type, True);
18911 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18912 Set_Is_Abstract_Type (CW_Type, False);
18913 Set_Is_Constrained (CW_Type, False);
18914 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18915 Set_Default_SSO (CW_Type);
18916 Set_Has_Inheritable_Invariants (CW_Type, False);
18917 Set_Has_Inherited_Invariants (CW_Type, False);
18918 Set_Has_Own_Invariants (CW_Type, False);
18919
18920 if Ekind (T) = E_Class_Wide_Subtype then
18921 Set_Etype (CW_Type, Etype (Base_Type (T)));
18922 else
18923 Set_Etype (CW_Type, T);
18924 end if;
18925
18926 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18927
18928 -- If this is the class_wide type of a constrained subtype, it does
18929 -- not have discriminants.
18930
18931 Set_Has_Discriminants (CW_Type,
18932 Has_Discriminants (T) and then not Is_Constrained (T));
18933
18934 Set_Has_Unknown_Discriminants (CW_Type, True);
18935 Set_Class_Wide_Type (T, CW_Type);
18936 Set_Equivalent_Type (CW_Type, Empty);
18937
18938 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18939
18940 Set_Class_Wide_Type (CW_Type, CW_Type);
18941 end Make_Class_Wide_Type;
18942
18943 ----------------
18944 -- Make_Index --
18945 ----------------
18946
18947 procedure Make_Index
18948 (N : Node_Id;
18949 Related_Nod : Node_Id;
18950 Related_Id : Entity_Id := Empty;
18951 Suffix_Index : Nat := 1;
18952 In_Iter_Schm : Boolean := False)
18953 is
18954 R : Node_Id;
18955 T : Entity_Id;
18956 Def_Id : Entity_Id := Empty;
18957 Found : Boolean := False;
18958
18959 begin
18960 -- For a discrete range used in a constrained array definition and
18961 -- defined by a range, an implicit conversion to the predefined type
18962 -- INTEGER is assumed if each bound is either a numeric literal, a named
18963 -- number, or an attribute, and the type of both bounds (prior to the
18964 -- implicit conversion) is the type universal_integer. Otherwise, both
18965 -- bounds must be of the same discrete type, other than universal
18966 -- integer; this type must be determinable independently of the
18967 -- context, but using the fact that the type must be discrete and that
18968 -- both bounds must have the same type.
18969
18970 -- Character literals also have a universal type in the absence of
18971 -- of additional context, and are resolved to Standard_Character.
18972
18973 if Nkind (N) = N_Range then
18974
18975 -- The index is given by a range constraint. The bounds are known
18976 -- to be of a consistent type.
18977
18978 if not Is_Overloaded (N) then
18979 T := Etype (N);
18980
18981 -- For universal bounds, choose the specific predefined type
18982
18983 if T = Universal_Integer then
18984 T := Standard_Integer;
18985
18986 elsif T = Any_Character then
18987 Ambiguous_Character (Low_Bound (N));
18988
18989 T := Standard_Character;
18990 end if;
18991
18992 -- The node may be overloaded because some user-defined operators
18993 -- are available, but if a universal interpretation exists it is
18994 -- also the selected one.
18995
18996 elsif Universal_Interpretation (N) = Universal_Integer then
18997 T := Standard_Integer;
18998
18999 else
19000 T := Any_Type;
19001
19002 declare
19003 Ind : Interp_Index;
19004 It : Interp;
19005
19006 begin
19007 Get_First_Interp (N, Ind, It);
19008 while Present (It.Typ) loop
19009 if Is_Discrete_Type (It.Typ) then
19010
19011 if Found
19012 and then not Covers (It.Typ, T)
19013 and then not Covers (T, It.Typ)
19014 then
19015 Error_Msg_N ("ambiguous bounds in discrete range", N);
19016 exit;
19017 else
19018 T := It.Typ;
19019 Found := True;
19020 end if;
19021 end if;
19022
19023 Get_Next_Interp (Ind, It);
19024 end loop;
19025
19026 if T = Any_Type then
19027 Error_Msg_N ("discrete type required for range", N);
19028 Set_Etype (N, Any_Type);
19029 return;
19030
19031 elsif T = Universal_Integer then
19032 T := Standard_Integer;
19033 end if;
19034 end;
19035 end if;
19036
19037 if not Is_Discrete_Type (T) then
19038 Error_Msg_N ("discrete type required for range", N);
19039 Set_Etype (N, Any_Type);
19040 return;
19041 end if;
19042
19043 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19044 and then Attribute_Name (Low_Bound (N)) = Name_First
19045 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19046 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19047 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19048 then
19049 -- The type of the index will be the type of the prefix, as long
19050 -- as the upper bound is 'Last of the same type.
19051
19052 Def_Id := Entity (Prefix (Low_Bound (N)));
19053
19054 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19055 or else Attribute_Name (High_Bound (N)) /= Name_Last
19056 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19057 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19058 then
19059 Def_Id := Empty;
19060 end if;
19061 end if;
19062
19063 R := N;
19064 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19065
19066 elsif Nkind (N) = N_Subtype_Indication then
19067
19068 -- The index is given by a subtype with a range constraint
19069
19070 T := Base_Type (Entity (Subtype_Mark (N)));
19071
19072 if not Is_Discrete_Type (T) then
19073 Error_Msg_N ("discrete type required for range", N);
19074 Set_Etype (N, Any_Type);
19075 return;
19076 end if;
19077
19078 R := Range_Expression (Constraint (N));
19079
19080 Resolve (R, T);
19081 Process_Range_Expr_In_Decl
19082 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19083
19084 elsif Nkind (N) = N_Attribute_Reference then
19085
19086 -- Catch beginner's error (use of attribute other than 'Range)
19087
19088 if Attribute_Name (N) /= Name_Range then
19089 Error_Msg_N ("expect attribute ''Range", N);
19090 Set_Etype (N, Any_Type);
19091 return;
19092 end if;
19093
19094 -- If the node denotes the range of a type mark, that is also the
19095 -- resulting type, and we do not need to create an Itype for it.
19096
19097 if Is_Entity_Name (Prefix (N))
19098 and then Comes_From_Source (N)
19099 and then Is_Type (Entity (Prefix (N)))
19100 and then Is_Discrete_Type (Entity (Prefix (N)))
19101 then
19102 Def_Id := Entity (Prefix (N));
19103 end if;
19104
19105 Analyze_And_Resolve (N);
19106 T := Etype (N);
19107 R := N;
19108
19109 -- If none of the above, must be a subtype. We convert this to a
19110 -- range attribute reference because in the case of declared first
19111 -- named subtypes, the types in the range reference can be different
19112 -- from the type of the entity. A range attribute normalizes the
19113 -- reference and obtains the correct types for the bounds.
19114
19115 -- This transformation is in the nature of an expansion, is only
19116 -- done if expansion is active. In particular, it is not done on
19117 -- formal generic types, because we need to retain the name of the
19118 -- original index for instantiation purposes.
19119
19120 else
19121 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19122 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19123 Set_Etype (N, Any_Integer);
19124 return;
19125
19126 else
19127 -- The type mark may be that of an incomplete type. It is only
19128 -- now that we can get the full view, previous analysis does
19129 -- not look specifically for a type mark.
19130
19131 Set_Entity (N, Get_Full_View (Entity (N)));
19132 Set_Etype (N, Entity (N));
19133 Def_Id := Entity (N);
19134
19135 if not Is_Discrete_Type (Def_Id) then
19136 Error_Msg_N ("discrete type required for index", N);
19137 Set_Etype (N, Any_Type);
19138 return;
19139 end if;
19140 end if;
19141
19142 if Expander_Active then
19143 Rewrite (N,
19144 Make_Attribute_Reference (Sloc (N),
19145 Attribute_Name => Name_Range,
19146 Prefix => Relocate_Node (N)));
19147
19148 -- The original was a subtype mark that does not freeze. This
19149 -- means that the rewritten version must not freeze either.
19150
19151 Set_Must_Not_Freeze (N);
19152 Set_Must_Not_Freeze (Prefix (N));
19153 Analyze_And_Resolve (N);
19154 T := Etype (N);
19155 R := N;
19156
19157 -- If expander is inactive, type is legal, nothing else to construct
19158
19159 else
19160 return;
19161 end if;
19162 end if;
19163
19164 if not Is_Discrete_Type (T) then
19165 Error_Msg_N ("discrete type required for range", N);
19166 Set_Etype (N, Any_Type);
19167 return;
19168
19169 elsif T = Any_Type then
19170 Set_Etype (N, Any_Type);
19171 return;
19172 end if;
19173
19174 -- We will now create the appropriate Itype to describe the range, but
19175 -- first a check. If we originally had a subtype, then we just label
19176 -- the range with this subtype. Not only is there no need to construct
19177 -- a new subtype, but it is wrong to do so for two reasons:
19178
19179 -- 1. A legality concern, if we have a subtype, it must not freeze,
19180 -- and the Itype would cause freezing incorrectly
19181
19182 -- 2. An efficiency concern, if we created an Itype, it would not be
19183 -- recognized as the same type for the purposes of eliminating
19184 -- checks in some circumstances.
19185
19186 -- We signal this case by setting the subtype entity in Def_Id
19187
19188 if No (Def_Id) then
19189 Def_Id :=
19190 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19191 Set_Etype (Def_Id, Base_Type (T));
19192
19193 if Is_Signed_Integer_Type (T) then
19194 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19195
19196 elsif Is_Modular_Integer_Type (T) then
19197 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19198
19199 else
19200 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19201 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19202 Set_First_Literal (Def_Id, First_Literal (T));
19203 end if;
19204
19205 Set_Size_Info (Def_Id, (T));
19206 Set_RM_Size (Def_Id, RM_Size (T));
19207 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19208
19209 Set_Scalar_Range (Def_Id, R);
19210 Conditional_Delay (Def_Id, T);
19211
19212 if Nkind (N) = N_Subtype_Indication then
19213 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19214 end if;
19215
19216 -- In the subtype indication case, if the immediate parent of the
19217 -- new subtype is non-static, then the subtype we create is non-
19218 -- static, even if its bounds are static.
19219
19220 if Nkind (N) = N_Subtype_Indication
19221 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19222 then
19223 Set_Is_Non_Static_Subtype (Def_Id);
19224 end if;
19225 end if;
19226
19227 -- Final step is to label the index with this constructed type
19228
19229 Set_Etype (N, Def_Id);
19230 end Make_Index;
19231
19232 ------------------------------
19233 -- Modular_Type_Declaration --
19234 ------------------------------
19235
19236 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19237 Mod_Expr : constant Node_Id := Expression (Def);
19238 M_Val : Uint;
19239
19240 procedure Set_Modular_Size (Bits : Int);
19241 -- Sets RM_Size to Bits, and Esize to normal word size above this
19242
19243 ----------------------
19244 -- Set_Modular_Size --
19245 ----------------------
19246
19247 procedure Set_Modular_Size (Bits : Int) is
19248 begin
19249 Set_RM_Size (T, UI_From_Int (Bits));
19250
19251 if Bits <= 8 then
19252 Init_Esize (T, 8);
19253
19254 elsif Bits <= 16 then
19255 Init_Esize (T, 16);
19256
19257 elsif Bits <= 32 then
19258 Init_Esize (T, 32);
19259
19260 else
19261 Init_Esize (T, System_Max_Binary_Modulus_Power);
19262 end if;
19263
19264 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19265 Set_Is_Known_Valid (T);
19266 end if;
19267 end Set_Modular_Size;
19268
19269 -- Start of processing for Modular_Type_Declaration
19270
19271 begin
19272 -- If the mod expression is (exactly) 2 * literal, where literal is
19273 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19274
19275 if Warn_On_Suspicious_Modulus_Value
19276 and then Nkind (Mod_Expr) = N_Op_Multiply
19277 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19278 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19279 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19280 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19281 then
19282 Error_Msg_N
19283 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19284 end if;
19285
19286 -- Proceed with analysis of mod expression
19287
19288 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19289 Set_Etype (T, T);
19290 Set_Ekind (T, E_Modular_Integer_Type);
19291 Init_Alignment (T);
19292 Set_Is_Constrained (T);
19293
19294 if not Is_OK_Static_Expression (Mod_Expr) then
19295 Flag_Non_Static_Expr
19296 ("non-static expression used for modular type bound!", Mod_Expr);
19297 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19298 else
19299 M_Val := Expr_Value (Mod_Expr);
19300 end if;
19301
19302 if M_Val < 1 then
19303 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19304 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19305 end if;
19306
19307 if M_Val > 2 ** Standard_Long_Integer_Size then
19308 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19309 end if;
19310
19311 Set_Modulus (T, M_Val);
19312
19313 -- Create bounds for the modular type based on the modulus given in
19314 -- the type declaration and then analyze and resolve those bounds.
19315
19316 Set_Scalar_Range (T,
19317 Make_Range (Sloc (Mod_Expr),
19318 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19319 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19320
19321 -- Properly analyze the literals for the range. We do this manually
19322 -- because we can't go calling Resolve, since we are resolving these
19323 -- bounds with the type, and this type is certainly not complete yet.
19324
19325 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19326 Set_Etype (High_Bound (Scalar_Range (T)), T);
19327 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19328 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19329
19330 -- Loop through powers of two to find number of bits required
19331
19332 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19333
19334 -- Binary case
19335
19336 if M_Val = 2 ** Bits then
19337 Set_Modular_Size (Bits);
19338 return;
19339
19340 -- Nonbinary case
19341
19342 elsif M_Val < 2 ** Bits then
19343 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19344 Set_Non_Binary_Modulus (T);
19345
19346 if Bits > System_Max_Nonbinary_Modulus_Power then
19347 Error_Msg_Uint_1 :=
19348 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19349 Error_Msg_F
19350 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19351 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19352 return;
19353
19354 else
19355 -- In the nonbinary case, set size as per RM 13.3(55)
19356
19357 Set_Modular_Size (Bits);
19358 return;
19359 end if;
19360 end if;
19361
19362 end loop;
19363
19364 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19365 -- so we just signal an error and set the maximum size.
19366
19367 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19368 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19369
19370 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19371 Init_Alignment (T);
19372
19373 end Modular_Type_Declaration;
19374
19375 --------------------------
19376 -- New_Concatenation_Op --
19377 --------------------------
19378
19379 procedure New_Concatenation_Op (Typ : Entity_Id) is
19380 Loc : constant Source_Ptr := Sloc (Typ);
19381 Op : Entity_Id;
19382
19383 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19384 -- Create abbreviated declaration for the formal of a predefined
19385 -- Operator 'Op' of type 'Typ'
19386
19387 --------------------
19388 -- Make_Op_Formal --
19389 --------------------
19390
19391 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19392 Formal : Entity_Id;
19393 begin
19394 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19395 Set_Etype (Formal, Typ);
19396 Set_Mechanism (Formal, Default_Mechanism);
19397 return Formal;
19398 end Make_Op_Formal;
19399
19400 -- Start of processing for New_Concatenation_Op
19401
19402 begin
19403 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19404
19405 Set_Ekind (Op, E_Operator);
19406 Set_Scope (Op, Current_Scope);
19407 Set_Etype (Op, Typ);
19408 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19409 Set_Is_Immediately_Visible (Op);
19410 Set_Is_Intrinsic_Subprogram (Op);
19411 Set_Has_Completion (Op);
19412 Append_Entity (Op, Current_Scope);
19413
19414 Set_Name_Entity_Id (Name_Op_Concat, Op);
19415
19416 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19417 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19418 end New_Concatenation_Op;
19419
19420 -------------------------
19421 -- OK_For_Limited_Init --
19422 -------------------------
19423
19424 -- ???Check all calls of this, and compare the conditions under which it's
19425 -- called.
19426
19427 function OK_For_Limited_Init
19428 (Typ : Entity_Id;
19429 Exp : Node_Id) return Boolean
19430 is
19431 begin
19432 return Is_CPP_Constructor_Call (Exp)
19433 or else (Ada_Version >= Ada_2005
19434 and then not Debug_Flag_Dot_L
19435 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19436 end OK_For_Limited_Init;
19437
19438 -------------------------------
19439 -- OK_For_Limited_Init_In_05 --
19440 -------------------------------
19441
19442 function OK_For_Limited_Init_In_05
19443 (Typ : Entity_Id;
19444 Exp : Node_Id) return Boolean
19445 is
19446 begin
19447 -- An object of a limited interface type can be initialized with any
19448 -- expression of a nonlimited descendant type. However this does not
19449 -- apply if this is a view conversion of some other expression. This
19450 -- is checked below.
19451
19452 if Is_Class_Wide_Type (Typ)
19453 and then Is_Limited_Interface (Typ)
19454 and then not Is_Limited_Type (Etype (Exp))
19455 and then Nkind (Exp) /= N_Type_Conversion
19456 then
19457 return True;
19458 end if;
19459
19460 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19461 -- case of limited aggregates (including extension aggregates), and
19462 -- function calls. The function call may have been given in prefixed
19463 -- notation, in which case the original node is an indexed component.
19464 -- If the function is parameterless, the original node was an explicit
19465 -- dereference. The function may also be parameterless, in which case
19466 -- the source node is just an identifier.
19467
19468 -- A branch of a conditional expression may have been removed if the
19469 -- condition is statically known. This happens during expansion, and
19470 -- thus will not happen if previous errors were encountered. The check
19471 -- will have been performed on the chosen branch, which replaces the
19472 -- original conditional expression.
19473
19474 if No (Exp) then
19475 return True;
19476 end if;
19477
19478 case Nkind (Original_Node (Exp)) is
19479 when N_Aggregate
19480 | N_Extension_Aggregate
19481 | N_Function_Call
19482 | N_Op
19483 =>
19484 return True;
19485
19486 when N_Identifier =>
19487 return Present (Entity (Original_Node (Exp)))
19488 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19489
19490 when N_Qualified_Expression =>
19491 return
19492 OK_For_Limited_Init_In_05
19493 (Typ, Expression (Original_Node (Exp)));
19494
19495 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19496 -- with a function call, the expander has rewritten the call into an
19497 -- N_Type_Conversion node to force displacement of the pointer to
19498 -- reference the component containing the secondary dispatch table.
19499 -- Otherwise a type conversion is not a legal context.
19500 -- A return statement for a build-in-place function returning a
19501 -- synchronized type also introduces an unchecked conversion.
19502
19503 when N_Type_Conversion
19504 | N_Unchecked_Type_Conversion
19505 =>
19506 return not Comes_From_Source (Exp)
19507 and then
19508 OK_For_Limited_Init_In_05
19509 (Typ, Expression (Original_Node (Exp)));
19510
19511 when N_Explicit_Dereference
19512 | N_Indexed_Component
19513 | N_Selected_Component
19514 =>
19515 return Nkind (Exp) = N_Function_Call;
19516
19517 -- A use of 'Input is a function call, hence allowed. Normally the
19518 -- attribute will be changed to a call, but the attribute by itself
19519 -- can occur with -gnatc.
19520
19521 when N_Attribute_Reference =>
19522 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19523
19524 -- "return raise ..." is OK
19525
19526 when N_Raise_Expression =>
19527 return True;
19528
19529 -- For a case expression, all dependent expressions must be legal
19530
19531 when N_Case_Expression =>
19532 declare
19533 Alt : Node_Id;
19534
19535 begin
19536 Alt := First (Alternatives (Original_Node (Exp)));
19537 while Present (Alt) loop
19538 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19539 return False;
19540 end if;
19541
19542 Next (Alt);
19543 end loop;
19544
19545 return True;
19546 end;
19547
19548 -- For an if expression, all dependent expressions must be legal
19549
19550 when N_If_Expression =>
19551 declare
19552 Then_Expr : constant Node_Id :=
19553 Next (First (Expressions (Original_Node (Exp))));
19554 Else_Expr : constant Node_Id := Next (Then_Expr);
19555 begin
19556 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19557 and then
19558 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19559 end;
19560
19561 when others =>
19562 return False;
19563 end case;
19564 end OK_For_Limited_Init_In_05;
19565
19566 -------------------------------------------
19567 -- Ordinary_Fixed_Point_Type_Declaration --
19568 -------------------------------------------
19569
19570 procedure Ordinary_Fixed_Point_Type_Declaration
19571 (T : Entity_Id;
19572 Def : Node_Id)
19573 is
19574 Loc : constant Source_Ptr := Sloc (Def);
19575 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19576 RRS : constant Node_Id := Real_Range_Specification (Def);
19577 Implicit_Base : Entity_Id;
19578 Delta_Val : Ureal;
19579 Small_Val : Ureal;
19580 Low_Val : Ureal;
19581 High_Val : Ureal;
19582
19583 begin
19584 Check_Restriction (No_Fixed_Point, Def);
19585
19586 -- Create implicit base type
19587
19588 Implicit_Base :=
19589 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19590 Set_Etype (Implicit_Base, Implicit_Base);
19591
19592 -- Analyze and process delta expression
19593
19594 Analyze_And_Resolve (Delta_Expr, Any_Real);
19595
19596 Check_Delta_Expression (Delta_Expr);
19597 Delta_Val := Expr_Value_R (Delta_Expr);
19598
19599 Set_Delta_Value (Implicit_Base, Delta_Val);
19600
19601 -- Compute default small from given delta, which is the largest power
19602 -- of two that does not exceed the given delta value.
19603
19604 declare
19605 Tmp : Ureal;
19606 Scale : Int;
19607
19608 begin
19609 Tmp := Ureal_1;
19610 Scale := 0;
19611
19612 if Delta_Val < Ureal_1 then
19613 while Delta_Val < Tmp loop
19614 Tmp := Tmp / Ureal_2;
19615 Scale := Scale + 1;
19616 end loop;
19617
19618 else
19619 loop
19620 Tmp := Tmp * Ureal_2;
19621 exit when Tmp > Delta_Val;
19622 Scale := Scale - 1;
19623 end loop;
19624 end if;
19625
19626 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19627 end;
19628
19629 Set_Small_Value (Implicit_Base, Small_Val);
19630
19631 -- If no range was given, set a dummy range
19632
19633 if RRS <= Empty_Or_Error then
19634 Low_Val := -Small_Val;
19635 High_Val := Small_Val;
19636
19637 -- Otherwise analyze and process given range
19638
19639 else
19640 declare
19641 Low : constant Node_Id := Low_Bound (RRS);
19642 High : constant Node_Id := High_Bound (RRS);
19643
19644 begin
19645 Analyze_And_Resolve (Low, Any_Real);
19646 Analyze_And_Resolve (High, Any_Real);
19647 Check_Real_Bound (Low);
19648 Check_Real_Bound (High);
19649
19650 -- Obtain and set the range
19651
19652 Low_Val := Expr_Value_R (Low);
19653 High_Val := Expr_Value_R (High);
19654
19655 if Low_Val > High_Val then
19656 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19657 end if;
19658 end;
19659 end if;
19660
19661 -- The range for both the implicit base and the declared first subtype
19662 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19663 -- set a temporary range in place. Note that the bounds of the base
19664 -- type will be widened to be symmetrical and to fill the available
19665 -- bits when the type is frozen.
19666
19667 -- We could do this with all discrete types, and probably should, but
19668 -- we absolutely have to do it for fixed-point, since the end-points
19669 -- of the range and the size are determined by the small value, which
19670 -- could be reset before the freeze point.
19671
19672 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19673 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19674
19675 -- Complete definition of first subtype. The inheritance of the rep item
19676 -- chain ensures that SPARK-related pragmas are not clobbered when the
19677 -- ordinary fixed point type acts as a full view of a private type.
19678
19679 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19680 Set_Etype (T, Implicit_Base);
19681 Init_Size_Align (T);
19682 Inherit_Rep_Item_Chain (T, Implicit_Base);
19683 Set_Small_Value (T, Small_Val);
19684 Set_Delta_Value (T, Delta_Val);
19685 Set_Is_Constrained (T);
19686 end Ordinary_Fixed_Point_Type_Declaration;
19687
19688 ----------------------------------
19689 -- Preanalyze_Assert_Expression --
19690 ----------------------------------
19691
19692 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19693 begin
19694 In_Assertion_Expr := In_Assertion_Expr + 1;
19695 Preanalyze_Spec_Expression (N, T);
19696 In_Assertion_Expr := In_Assertion_Expr - 1;
19697 end Preanalyze_Assert_Expression;
19698
19699 -----------------------------------
19700 -- Preanalyze_Default_Expression --
19701 -----------------------------------
19702
19703 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19704 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19705 begin
19706 In_Default_Expr := True;
19707 Preanalyze_Spec_Expression (N, T);
19708 In_Default_Expr := Save_In_Default_Expr;
19709 end Preanalyze_Default_Expression;
19710
19711 --------------------------------
19712 -- Preanalyze_Spec_Expression --
19713 --------------------------------
19714
19715 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19716 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19717 begin
19718 In_Spec_Expression := True;
19719 Preanalyze_And_Resolve (N, T);
19720 In_Spec_Expression := Save_In_Spec_Expression;
19721 end Preanalyze_Spec_Expression;
19722
19723 ----------------------------------------
19724 -- Prepare_Private_Subtype_Completion --
19725 ----------------------------------------
19726
19727 procedure Prepare_Private_Subtype_Completion
19728 (Id : Entity_Id;
19729 Related_Nod : Node_Id)
19730 is
19731 Id_B : constant Entity_Id := Base_Type (Id);
19732 Full_B : Entity_Id := Full_View (Id_B);
19733 Full : Entity_Id;
19734
19735 begin
19736 if Present (Full_B) then
19737
19738 -- Get to the underlying full view if necessary
19739
19740 if Is_Private_Type (Full_B)
19741 and then Present (Underlying_Full_View (Full_B))
19742 then
19743 Full_B := Underlying_Full_View (Full_B);
19744 end if;
19745
19746 -- The Base_Type is already completed, we can complete the subtype
19747 -- now. We have to create a new entity with the same name, Thus we
19748 -- can't use Create_Itype.
19749
19750 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19751 Set_Is_Itype (Full);
19752 Set_Associated_Node_For_Itype (Full, Related_Nod);
19753 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19754 end if;
19755
19756 -- The parent subtype may be private, but the base might not, in some
19757 -- nested instances. In that case, the subtype does not need to be
19758 -- exchanged. It would still be nice to make private subtypes and their
19759 -- bases consistent at all times ???
19760
19761 if Is_Private_Type (Id_B) then
19762 Append_Elmt (Id, Private_Dependents (Id_B));
19763 end if;
19764 end Prepare_Private_Subtype_Completion;
19765
19766 ---------------------------
19767 -- Process_Discriminants --
19768 ---------------------------
19769
19770 procedure Process_Discriminants
19771 (N : Node_Id;
19772 Prev : Entity_Id := Empty)
19773 is
19774 Elist : constant Elist_Id := New_Elmt_List;
19775 Id : Node_Id;
19776 Discr : Node_Id;
19777 Discr_Number : Uint;
19778 Discr_Type : Entity_Id;
19779 Default_Present : Boolean := False;
19780 Default_Not_Present : Boolean := False;
19781
19782 begin
19783 -- A composite type other than an array type can have discriminants.
19784 -- On entry, the current scope is the composite type.
19785
19786 -- The discriminants are initially entered into the scope of the type
19787 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19788 -- use, as explained at the end of this procedure.
19789
19790 Discr := First (Discriminant_Specifications (N));
19791 while Present (Discr) loop
19792 Enter_Name (Defining_Identifier (Discr));
19793
19794 -- For navigation purposes we add a reference to the discriminant
19795 -- in the entity for the type. If the current declaration is a
19796 -- completion, place references on the partial view. Otherwise the
19797 -- type is the current scope.
19798
19799 if Present (Prev) then
19800
19801 -- The references go on the partial view, if present. If the
19802 -- partial view has discriminants, the references have been
19803 -- generated already.
19804
19805 if not Has_Discriminants (Prev) then
19806 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19807 end if;
19808 else
19809 Generate_Reference
19810 (Current_Scope, Defining_Identifier (Discr), 'd');
19811 end if;
19812
19813 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19814 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19815
19816 -- Ada 2005 (AI-254)
19817
19818 if Present (Access_To_Subprogram_Definition
19819 (Discriminant_Type (Discr)))
19820 and then Protected_Present (Access_To_Subprogram_Definition
19821 (Discriminant_Type (Discr)))
19822 then
19823 Discr_Type :=
19824 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19825 end if;
19826
19827 else
19828 Find_Type (Discriminant_Type (Discr));
19829 Discr_Type := Etype (Discriminant_Type (Discr));
19830
19831 if Error_Posted (Discriminant_Type (Discr)) then
19832 Discr_Type := Any_Type;
19833 end if;
19834 end if;
19835
19836 -- Handling of discriminants that are access types
19837
19838 if Is_Access_Type (Discr_Type) then
19839
19840 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19841 -- limited record types
19842
19843 if Ada_Version < Ada_2005 then
19844 Check_Access_Discriminant_Requires_Limited
19845 (Discr, Discriminant_Type (Discr));
19846 end if;
19847
19848 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19849 Error_Msg_N
19850 ("(Ada 83) access discriminant not allowed", Discr);
19851 end if;
19852
19853 -- If not access type, must be a discrete type
19854
19855 elsif not Is_Discrete_Type (Discr_Type) then
19856 Error_Msg_N
19857 ("discriminants must have a discrete or access type",
19858 Discriminant_Type (Discr));
19859 end if;
19860
19861 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19862
19863 -- If a discriminant specification includes the assignment compound
19864 -- delimiter followed by an expression, the expression is the default
19865 -- expression of the discriminant; the default expression must be of
19866 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19867 -- a default expression, we do the special preanalysis, since this
19868 -- expression does not freeze (see section "Handling of Default and
19869 -- Per-Object Expressions" in spec of package Sem).
19870
19871 if Present (Expression (Discr)) then
19872 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19873
19874 -- Legaity checks
19875
19876 if Nkind (N) = N_Formal_Type_Declaration then
19877 Error_Msg_N
19878 ("discriminant defaults not allowed for formal type",
19879 Expression (Discr));
19880
19881 -- Flag an error for a tagged type with defaulted discriminants,
19882 -- excluding limited tagged types when compiling for Ada 2012
19883 -- (see AI05-0214).
19884
19885 elsif Is_Tagged_Type (Current_Scope)
19886 and then (not Is_Limited_Type (Current_Scope)
19887 or else Ada_Version < Ada_2012)
19888 and then Comes_From_Source (N)
19889 then
19890 -- Note: see similar test in Check_Or_Process_Discriminants, to
19891 -- handle the (illegal) case of the completion of an untagged
19892 -- view with discriminants with defaults by a tagged full view.
19893 -- We skip the check if Discr does not come from source, to
19894 -- account for the case of an untagged derived type providing
19895 -- defaults for a renamed discriminant from a private untagged
19896 -- ancestor with a tagged full view (ACATS B460006).
19897
19898 if Ada_Version >= Ada_2012 then
19899 Error_Msg_N
19900 ("discriminants of nonlimited tagged type cannot have"
19901 & " defaults",
19902 Expression (Discr));
19903 else
19904 Error_Msg_N
19905 ("discriminants of tagged type cannot have defaults",
19906 Expression (Discr));
19907 end if;
19908
19909 else
19910 Default_Present := True;
19911 Append_Elmt (Expression (Discr), Elist);
19912
19913 -- Tag the defining identifiers for the discriminants with
19914 -- their corresponding default expressions from the tree.
19915
19916 Set_Discriminant_Default_Value
19917 (Defining_Identifier (Discr), Expression (Discr));
19918 end if;
19919
19920 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19921 -- gets set unless we can be sure that no range check is required.
19922
19923 if (GNATprove_Mode or not Expander_Active)
19924 and then not
19925 Is_In_Range
19926 (Expression (Discr), Discr_Type, Assume_Valid => True)
19927 then
19928 Set_Do_Range_Check (Expression (Discr));
19929 end if;
19930
19931 -- No default discriminant value given
19932
19933 else
19934 Default_Not_Present := True;
19935 end if;
19936
19937 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19938 -- Discr_Type but with the null-exclusion attribute
19939
19940 if Ada_Version >= Ada_2005 then
19941
19942 -- Ada 2005 (AI-231): Static checks
19943
19944 if Can_Never_Be_Null (Discr_Type) then
19945 Null_Exclusion_Static_Checks (Discr);
19946
19947 elsif Is_Access_Type (Discr_Type)
19948 and then Null_Exclusion_Present (Discr)
19949
19950 -- No need to check itypes because in their case this check
19951 -- was done at their point of creation
19952
19953 and then not Is_Itype (Discr_Type)
19954 then
19955 if Can_Never_Be_Null (Discr_Type) then
19956 Error_Msg_NE
19957 ("`NOT NULL` not allowed (& already excludes null)",
19958 Discr,
19959 Discr_Type);
19960 end if;
19961
19962 Set_Etype (Defining_Identifier (Discr),
19963 Create_Null_Excluding_Itype
19964 (T => Discr_Type,
19965 Related_Nod => Discr));
19966
19967 -- Check for improper null exclusion if the type is otherwise
19968 -- legal for a discriminant.
19969
19970 elsif Null_Exclusion_Present (Discr)
19971 and then Is_Discrete_Type (Discr_Type)
19972 then
19973 Error_Msg_N
19974 ("null exclusion can only apply to an access type", Discr);
19975 end if;
19976
19977 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19978 -- can't have defaults. Synchronized types, or types that are
19979 -- explicitly limited are fine, but special tests apply to derived
19980 -- types in generics: in a generic body we have to assume the
19981 -- worst, and therefore defaults are not allowed if the parent is
19982 -- a generic formal private type (see ACATS B370001).
19983
19984 if Is_Access_Type (Discr_Type) and then Default_Present then
19985 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19986 or else Is_Limited_Record (Current_Scope)
19987 or else Is_Concurrent_Type (Current_Scope)
19988 or else Is_Concurrent_Record_Type (Current_Scope)
19989 or else Ekind (Current_Scope) = E_Limited_Private_Type
19990 then
19991 if not Is_Derived_Type (Current_Scope)
19992 or else not Is_Generic_Type (Etype (Current_Scope))
19993 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19994 or else Limited_Present
19995 (Type_Definition (Parent (Current_Scope)))
19996 then
19997 null;
19998
19999 else
20000 Error_Msg_N
20001 ("access discriminants of nonlimited types cannot "
20002 & "have defaults", Expression (Discr));
20003 end if;
20004
20005 elsif Present (Expression (Discr)) then
20006 Error_Msg_N
20007 ("(Ada 2005) access discriminants of nonlimited types "
20008 & "cannot have defaults", Expression (Discr));
20009 end if;
20010 end if;
20011 end if;
20012
20013 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
20014 -- This check is relevant only when SPARK_Mode is on as it is not a
20015 -- standard Ada legality rule.
20016
20017 if SPARK_Mode = On
20018 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20019 then
20020 Error_Msg_N ("discriminant cannot be volatile", Discr);
20021 end if;
20022
20023 Next (Discr);
20024 end loop;
20025
20026 -- An element list consisting of the default expressions of the
20027 -- discriminants is constructed in the above loop and used to set
20028 -- the Discriminant_Constraint attribute for the type. If an object
20029 -- is declared of this (record or task) type without any explicit
20030 -- discriminant constraint given, this element list will form the
20031 -- actual parameters for the corresponding initialization procedure
20032 -- for the type.
20033
20034 Set_Discriminant_Constraint (Current_Scope, Elist);
20035 Set_Stored_Constraint (Current_Scope, No_Elist);
20036
20037 -- Default expressions must be provided either for all or for none
20038 -- of the discriminants of a discriminant part. (RM 3.7.1)
20039
20040 if Default_Present and then Default_Not_Present then
20041 Error_Msg_N
20042 ("incomplete specification of defaults for discriminants", N);
20043 end if;
20044
20045 -- The use of the name of a discriminant is not allowed in default
20046 -- expressions of a discriminant part if the specification of the
20047 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20048
20049 -- To detect this, the discriminant names are entered initially with an
20050 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20051 -- attempt to use a void entity (for example in an expression that is
20052 -- type-checked) produces the error message: premature usage. Now after
20053 -- completing the semantic analysis of the discriminant part, we can set
20054 -- the Ekind of all the discriminants appropriately.
20055
20056 Discr := First (Discriminant_Specifications (N));
20057 Discr_Number := Uint_1;
20058 while Present (Discr) loop
20059 Id := Defining_Identifier (Discr);
20060 Set_Ekind (Id, E_Discriminant);
20061 Init_Component_Location (Id);
20062 Init_Esize (Id);
20063 Set_Discriminant_Number (Id, Discr_Number);
20064
20065 -- Make sure this is always set, even in illegal programs
20066
20067 Set_Corresponding_Discriminant (Id, Empty);
20068
20069 -- Initialize the Original_Record_Component to the entity itself.
20070 -- Inherit_Components will propagate the right value to
20071 -- discriminants in derived record types.
20072
20073 Set_Original_Record_Component (Id, Id);
20074
20075 -- Create the discriminal for the discriminant
20076
20077 Build_Discriminal (Id);
20078
20079 Next (Discr);
20080 Discr_Number := Discr_Number + 1;
20081 end loop;
20082
20083 Set_Has_Discriminants (Current_Scope);
20084 end Process_Discriminants;
20085
20086 -----------------------
20087 -- Process_Full_View --
20088 -----------------------
20089
20090 -- WARNING: This routine manages Ghost regions. Return statements must be
20091 -- replaced by gotos which jump to the end of the routine and restore the
20092 -- Ghost mode.
20093
20094 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20095 procedure Collect_Implemented_Interfaces
20096 (Typ : Entity_Id;
20097 Ifaces : Elist_Id);
20098 -- Ada 2005: Gather all the interfaces that Typ directly or
20099 -- inherently implements. Duplicate entries are not added to
20100 -- the list Ifaces.
20101
20102 ------------------------------------
20103 -- Collect_Implemented_Interfaces --
20104 ------------------------------------
20105
20106 procedure Collect_Implemented_Interfaces
20107 (Typ : Entity_Id;
20108 Ifaces : Elist_Id)
20109 is
20110 Iface : Entity_Id;
20111 Iface_Elmt : Elmt_Id;
20112
20113 begin
20114 -- Abstract interfaces are only associated with tagged record types
20115
20116 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20117 return;
20118 end if;
20119
20120 -- Recursively climb to the ancestors
20121
20122 if Etype (Typ) /= Typ
20123
20124 -- Protect the frontend against wrong cyclic declarations like:
20125
20126 -- type B is new A with private;
20127 -- type C is new A with private;
20128 -- private
20129 -- type B is new C with null record;
20130 -- type C is new B with null record;
20131
20132 and then Etype (Typ) /= Priv_T
20133 and then Etype (Typ) /= Full_T
20134 then
20135 -- Keep separate the management of private type declarations
20136
20137 if Ekind (Typ) = E_Record_Type_With_Private then
20138
20139 -- Handle the following illegal usage:
20140 -- type Private_Type is tagged private;
20141 -- private
20142 -- type Private_Type is new Type_Implementing_Iface;
20143
20144 if Present (Full_View (Typ))
20145 and then Etype (Typ) /= Full_View (Typ)
20146 then
20147 if Is_Interface (Etype (Typ)) then
20148 Append_Unique_Elmt (Etype (Typ), Ifaces);
20149 end if;
20150
20151 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20152 end if;
20153
20154 -- Non-private types
20155
20156 else
20157 if Is_Interface (Etype (Typ)) then
20158 Append_Unique_Elmt (Etype (Typ), Ifaces);
20159 end if;
20160
20161 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20162 end if;
20163 end if;
20164
20165 -- Handle entities in the list of abstract interfaces
20166
20167 if Present (Interfaces (Typ)) then
20168 Iface_Elmt := First_Elmt (Interfaces (Typ));
20169 while Present (Iface_Elmt) loop
20170 Iface := Node (Iface_Elmt);
20171
20172 pragma Assert (Is_Interface (Iface));
20173
20174 if not Contain_Interface (Iface, Ifaces) then
20175 Append_Elmt (Iface, Ifaces);
20176 Collect_Implemented_Interfaces (Iface, Ifaces);
20177 end if;
20178
20179 Next_Elmt (Iface_Elmt);
20180 end loop;
20181 end if;
20182 end Collect_Implemented_Interfaces;
20183
20184 -- Local variables
20185
20186 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20187
20188 Full_Indic : Node_Id;
20189 Full_Parent : Entity_Id;
20190 Priv_Parent : Entity_Id;
20191
20192 -- Start of processing for Process_Full_View
20193
20194 begin
20195 Mark_And_Set_Ghost_Completion (N, Priv_T);
20196
20197 -- First some sanity checks that must be done after semantic
20198 -- decoration of the full view and thus cannot be placed with other
20199 -- similar checks in Find_Type_Name
20200
20201 if not Is_Limited_Type (Priv_T)
20202 and then (Is_Limited_Type (Full_T)
20203 or else Is_Limited_Composite (Full_T))
20204 then
20205 if In_Instance then
20206 null;
20207 else
20208 Error_Msg_N
20209 ("completion of nonlimited type cannot be limited", Full_T);
20210 Explain_Limited_Type (Full_T, Full_T);
20211 end if;
20212
20213 elsif Is_Abstract_Type (Full_T)
20214 and then not Is_Abstract_Type (Priv_T)
20215 then
20216 Error_Msg_N
20217 ("completion of nonabstract type cannot be abstract", Full_T);
20218
20219 elsif Is_Tagged_Type (Priv_T)
20220 and then Is_Limited_Type (Priv_T)
20221 and then not Is_Limited_Type (Full_T)
20222 then
20223 -- If pragma CPP_Class was applied to the private declaration
20224 -- propagate the limitedness to the full-view
20225
20226 if Is_CPP_Class (Priv_T) then
20227 Set_Is_Limited_Record (Full_T);
20228
20229 -- GNAT allow its own definition of Limited_Controlled to disobey
20230 -- this rule in order in ease the implementation. This test is safe
20231 -- because Root_Controlled is defined in a child of System that
20232 -- normal programs are not supposed to use.
20233
20234 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20235 Set_Is_Limited_Composite (Full_T);
20236 else
20237 Error_Msg_N
20238 ("completion of limited tagged type must be limited", Full_T);
20239 end if;
20240
20241 elsif Is_Generic_Type (Priv_T) then
20242 Error_Msg_N ("generic type cannot have a completion", Full_T);
20243 end if;
20244
20245 -- Check that ancestor interfaces of private and full views are
20246 -- consistent. We omit this check for synchronized types because
20247 -- they are performed on the corresponding record type when frozen.
20248
20249 if Ada_Version >= Ada_2005
20250 and then Is_Tagged_Type (Priv_T)
20251 and then Is_Tagged_Type (Full_T)
20252 and then not Is_Concurrent_Type (Full_T)
20253 then
20254 declare
20255 Iface : Entity_Id;
20256 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20257 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20258
20259 begin
20260 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20261 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20262
20263 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20264 -- an interface type if and only if the full type is descendant
20265 -- of the interface type (AARM 7.3 (7.3/2)).
20266
20267 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20268
20269 if Present (Iface) then
20270 Error_Msg_NE
20271 ("interface in partial view& not implemented by full type "
20272 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20273 end if;
20274
20275 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20276
20277 if Present (Iface) then
20278 Error_Msg_NE
20279 ("interface & not implemented by partial view "
20280 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20281 end if;
20282 end;
20283 end if;
20284
20285 if Is_Tagged_Type (Priv_T)
20286 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20287 and then Is_Derived_Type (Full_T)
20288 then
20289 Priv_Parent := Etype (Priv_T);
20290
20291 -- The full view of a private extension may have been transformed
20292 -- into an unconstrained derived type declaration and a subtype
20293 -- declaration (see build_derived_record_type for details).
20294
20295 if Nkind (N) = N_Subtype_Declaration then
20296 Full_Indic := Subtype_Indication (N);
20297 Full_Parent := Etype (Base_Type (Full_T));
20298 else
20299 Full_Indic := Subtype_Indication (Type_Definition (N));
20300 Full_Parent := Etype (Full_T);
20301 end if;
20302
20303 -- Check that the parent type of the full type is a descendant of
20304 -- the ancestor subtype given in the private extension. If either
20305 -- entity has an Etype equal to Any_Type then we had some previous
20306 -- error situation [7.3(8)].
20307
20308 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20309 goto Leave;
20310
20311 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20312 -- any order. Therefore we don't have to check that its parent must
20313 -- be a descendant of the parent of the private type declaration.
20314
20315 elsif Is_Interface (Priv_Parent)
20316 and then Is_Interface (Full_Parent)
20317 then
20318 null;
20319
20320 -- Ada 2005 (AI-251): If the parent of the private type declaration
20321 -- is an interface there is no need to check that it is an ancestor
20322 -- of the associated full type declaration. The required tests for
20323 -- this case are performed by Build_Derived_Record_Type.
20324
20325 elsif not Is_Interface (Base_Type (Priv_Parent))
20326 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20327 then
20328 Error_Msg_N
20329 ("parent of full type must descend from parent of private "
20330 & "extension", Full_Indic);
20331
20332 -- First check a formal restriction, and then proceed with checking
20333 -- Ada rules. Since the formal restriction is not a serious error, we
20334 -- don't prevent further error detection for this check, hence the
20335 -- ELSE.
20336
20337 else
20338 -- In formal mode, when completing a private extension the type
20339 -- named in the private part must be exactly the same as that
20340 -- named in the visible part.
20341
20342 if Priv_Parent /= Full_Parent then
20343 Error_Msg_Name_1 := Chars (Priv_Parent);
20344 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20345 end if;
20346
20347 -- Check the rules of 7.3(10): if the private extension inherits
20348 -- known discriminants, then the full type must also inherit those
20349 -- discriminants from the same (ancestor) type, and the parent
20350 -- subtype of the full type must be constrained if and only if
20351 -- the ancestor subtype of the private extension is constrained.
20352
20353 if No (Discriminant_Specifications (Parent (Priv_T)))
20354 and then not Has_Unknown_Discriminants (Priv_T)
20355 and then Has_Discriminants (Base_Type (Priv_Parent))
20356 then
20357 declare
20358 Priv_Indic : constant Node_Id :=
20359 Subtype_Indication (Parent (Priv_T));
20360
20361 Priv_Constr : constant Boolean :=
20362 Is_Constrained (Priv_Parent)
20363 or else
20364 Nkind (Priv_Indic) = N_Subtype_Indication
20365 or else
20366 Is_Constrained (Entity (Priv_Indic));
20367
20368 Full_Constr : constant Boolean :=
20369 Is_Constrained (Full_Parent)
20370 or else
20371 Nkind (Full_Indic) = N_Subtype_Indication
20372 or else
20373 Is_Constrained (Entity (Full_Indic));
20374
20375 Priv_Discr : Entity_Id;
20376 Full_Discr : Entity_Id;
20377
20378 begin
20379 Priv_Discr := First_Discriminant (Priv_Parent);
20380 Full_Discr := First_Discriminant (Full_Parent);
20381 while Present (Priv_Discr) and then Present (Full_Discr) loop
20382 if Original_Record_Component (Priv_Discr) =
20383 Original_Record_Component (Full_Discr)
20384 or else
20385 Corresponding_Discriminant (Priv_Discr) =
20386 Corresponding_Discriminant (Full_Discr)
20387 then
20388 null;
20389 else
20390 exit;
20391 end if;
20392
20393 Next_Discriminant (Priv_Discr);
20394 Next_Discriminant (Full_Discr);
20395 end loop;
20396
20397 if Present (Priv_Discr) or else Present (Full_Discr) then
20398 Error_Msg_N
20399 ("full view must inherit discriminants of the parent "
20400 & "type used in the private extension", Full_Indic);
20401
20402 elsif Priv_Constr and then not Full_Constr then
20403 Error_Msg_N
20404 ("parent subtype of full type must be constrained",
20405 Full_Indic);
20406
20407 elsif Full_Constr and then not Priv_Constr then
20408 Error_Msg_N
20409 ("parent subtype of full type must be unconstrained",
20410 Full_Indic);
20411 end if;
20412 end;
20413
20414 -- Check the rules of 7.3(12): if a partial view has neither
20415 -- known or unknown discriminants, then the full type
20416 -- declaration shall define a definite subtype.
20417
20418 elsif not Has_Unknown_Discriminants (Priv_T)
20419 and then not Has_Discriminants (Priv_T)
20420 and then not Is_Constrained (Full_T)
20421 then
20422 Error_Msg_N
20423 ("full view must define a constrained type if partial view "
20424 & "has no discriminants", Full_T);
20425 end if;
20426
20427 -- ??????? Do we implement the following properly ?????
20428 -- If the ancestor subtype of a private extension has constrained
20429 -- discriminants, then the parent subtype of the full view shall
20430 -- impose a statically matching constraint on those discriminants
20431 -- [7.3(13)].
20432 end if;
20433
20434 else
20435 -- For untagged types, verify that a type without discriminants is
20436 -- not completed with an unconstrained type. A separate error message
20437 -- is produced if the full type has defaulted discriminants.
20438
20439 if Is_Definite_Subtype (Priv_T)
20440 and then not Is_Definite_Subtype (Full_T)
20441 then
20442 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20443 Error_Msg_NE
20444 ("full view of& not compatible with declaration#",
20445 Full_T, Priv_T);
20446
20447 if not Is_Tagged_Type (Full_T) then
20448 Error_Msg_N
20449 ("\one is constrained, the other unconstrained", Full_T);
20450 end if;
20451 end if;
20452 end if;
20453
20454 -- AI-419: verify that the use of "limited" is consistent
20455
20456 declare
20457 Orig_Decl : constant Node_Id := Original_Node (N);
20458
20459 begin
20460 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20461 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20462 and then Nkind
20463 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20464 then
20465 if not Limited_Present (Parent (Priv_T))
20466 and then not Synchronized_Present (Parent (Priv_T))
20467 and then Limited_Present (Type_Definition (Orig_Decl))
20468 then
20469 Error_Msg_N
20470 ("full view of non-limited extension cannot be limited", N);
20471
20472 -- Conversely, if the partial view carries the limited keyword,
20473 -- the full view must as well, even if it may be redundant.
20474
20475 elsif Limited_Present (Parent (Priv_T))
20476 and then not Limited_Present (Type_Definition (Orig_Decl))
20477 then
20478 Error_Msg_N
20479 ("full view of limited extension must be explicitly limited",
20480 N);
20481 end if;
20482 end if;
20483 end;
20484
20485 -- Ada 2005 (AI-443): A synchronized private extension must be
20486 -- completed by a task or protected type.
20487
20488 if Ada_Version >= Ada_2005
20489 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20490 and then Synchronized_Present (Parent (Priv_T))
20491 and then not Is_Concurrent_Type (Full_T)
20492 then
20493 Error_Msg_N ("full view of synchronized extension must " &
20494 "be synchronized type", N);
20495 end if;
20496
20497 -- Ada 2005 AI-363: if the full view has discriminants with
20498 -- defaults, it is illegal to declare constrained access subtypes
20499 -- whose designated type is the current type. This allows objects
20500 -- of the type that are declared in the heap to be unconstrained.
20501
20502 if not Has_Unknown_Discriminants (Priv_T)
20503 and then not Has_Discriminants (Priv_T)
20504 and then Has_Discriminants (Full_T)
20505 and then
20506 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20507 then
20508 Set_Has_Constrained_Partial_View (Full_T);
20509 Set_Has_Constrained_Partial_View (Priv_T);
20510 end if;
20511
20512 -- Create a full declaration for all its subtypes recorded in
20513 -- Private_Dependents and swap them similarly to the base type. These
20514 -- are subtypes that have been define before the full declaration of
20515 -- the private type. We also swap the entry in Private_Dependents list
20516 -- so we can properly restore the private view on exit from the scope.
20517
20518 declare
20519 Priv_Elmt : Elmt_Id;
20520 Priv_Scop : Entity_Id;
20521 Priv : Entity_Id;
20522 Full : Entity_Id;
20523
20524 begin
20525 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20526 while Present (Priv_Elmt) loop
20527 Priv := Node (Priv_Elmt);
20528 Priv_Scop := Scope (Priv);
20529
20530 if Ekind_In (Priv, E_Private_Subtype,
20531 E_Limited_Private_Subtype,
20532 E_Record_Subtype_With_Private)
20533 then
20534 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20535 Set_Is_Itype (Full);
20536 Set_Parent (Full, Parent (Priv));
20537 Set_Associated_Node_For_Itype (Full, N);
20538
20539 -- Now we need to complete the private subtype, but since the
20540 -- base type has already been swapped, we must also swap the
20541 -- subtypes (and thus, reverse the arguments in the call to
20542 -- Complete_Private_Subtype). Also note that we may need to
20543 -- re-establish the scope of the private subtype.
20544
20545 Copy_And_Swap (Priv, Full);
20546
20547 if not In_Open_Scopes (Priv_Scop) then
20548 Push_Scope (Priv_Scop);
20549
20550 else
20551 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20552
20553 Priv_Scop := Empty;
20554 end if;
20555
20556 Complete_Private_Subtype (Full, Priv, Full_T, N);
20557
20558 if Present (Priv_Scop) then
20559 Pop_Scope;
20560 end if;
20561
20562 Replace_Elmt (Priv_Elmt, Full);
20563 end if;
20564
20565 Next_Elmt (Priv_Elmt);
20566 end loop;
20567 end;
20568
20569 -- If the private view was tagged, copy the new primitive operations
20570 -- from the private view to the full view.
20571
20572 if Is_Tagged_Type (Full_T) then
20573 declare
20574 Disp_Typ : Entity_Id;
20575 Full_List : Elist_Id;
20576 Prim : Entity_Id;
20577 Prim_Elmt : Elmt_Id;
20578 Priv_List : Elist_Id;
20579
20580 function Contains
20581 (E : Entity_Id;
20582 L : Elist_Id) return Boolean;
20583 -- Determine whether list L contains element E
20584
20585 --------------
20586 -- Contains --
20587 --------------
20588
20589 function Contains
20590 (E : Entity_Id;
20591 L : Elist_Id) return Boolean
20592 is
20593 List_Elmt : Elmt_Id;
20594
20595 begin
20596 List_Elmt := First_Elmt (L);
20597 while Present (List_Elmt) loop
20598 if Node (List_Elmt) = E then
20599 return True;
20600 end if;
20601
20602 Next_Elmt (List_Elmt);
20603 end loop;
20604
20605 return False;
20606 end Contains;
20607
20608 -- Start of processing
20609
20610 begin
20611 if Is_Tagged_Type (Priv_T) then
20612 Priv_List := Primitive_Operations (Priv_T);
20613 Prim_Elmt := First_Elmt (Priv_List);
20614
20615 -- In the case of a concurrent type completing a private tagged
20616 -- type, primitives may have been declared in between the two
20617 -- views. These subprograms need to be wrapped the same way
20618 -- entries and protected procedures are handled because they
20619 -- cannot be directly shared by the two views.
20620
20621 if Is_Concurrent_Type (Full_T) then
20622 declare
20623 Conc_Typ : constant Entity_Id :=
20624 Corresponding_Record_Type (Full_T);
20625 Curr_Nod : Node_Id := Parent (Conc_Typ);
20626 Wrap_Spec : Node_Id;
20627
20628 begin
20629 while Present (Prim_Elmt) loop
20630 Prim := Node (Prim_Elmt);
20631
20632 if Comes_From_Source (Prim)
20633 and then not Is_Abstract_Subprogram (Prim)
20634 then
20635 Wrap_Spec :=
20636 Make_Subprogram_Declaration (Sloc (Prim),
20637 Specification =>
20638 Build_Wrapper_Spec
20639 (Subp_Id => Prim,
20640 Obj_Typ => Conc_Typ,
20641 Formals =>
20642 Parameter_Specifications
20643 (Parent (Prim))));
20644
20645 Insert_After (Curr_Nod, Wrap_Spec);
20646 Curr_Nod := Wrap_Spec;
20647
20648 Analyze (Wrap_Spec);
20649
20650 -- Remove the wrapper from visibility to avoid
20651 -- spurious conflict with the wrapped entity.
20652
20653 Set_Is_Immediately_Visible
20654 (Defining_Entity (Specification (Wrap_Spec)),
20655 False);
20656 end if;
20657
20658 Next_Elmt (Prim_Elmt);
20659 end loop;
20660
20661 goto Leave;
20662 end;
20663
20664 -- For non-concurrent types, transfer explicit primitives, but
20665 -- omit those inherited from the parent of the private view
20666 -- since they will be re-inherited later on.
20667
20668 else
20669 Full_List := Primitive_Operations (Full_T);
20670
20671 while Present (Prim_Elmt) loop
20672 Prim := Node (Prim_Elmt);
20673
20674 if Comes_From_Source (Prim)
20675 and then not Contains (Prim, Full_List)
20676 then
20677 Append_Elmt (Prim, Full_List);
20678 end if;
20679
20680 Next_Elmt (Prim_Elmt);
20681 end loop;
20682 end if;
20683
20684 -- Untagged private view
20685
20686 else
20687 Full_List := Primitive_Operations (Full_T);
20688
20689 -- In this case the partial view is untagged, so here we locate
20690 -- all of the earlier primitives that need to be treated as
20691 -- dispatching (those that appear between the two views). Note
20692 -- that these additional operations must all be new operations
20693 -- (any earlier operations that override inherited operations
20694 -- of the full view will already have been inserted in the
20695 -- primitives list, marked by Check_Operation_From_Private_View
20696 -- as dispatching. Note that implicit "/=" operators are
20697 -- excluded from being added to the primitives list since they
20698 -- shouldn't be treated as dispatching (tagged "/=" is handled
20699 -- specially).
20700
20701 Prim := Next_Entity (Full_T);
20702 while Present (Prim) and then Prim /= Priv_T loop
20703 if Ekind_In (Prim, E_Procedure, E_Function) then
20704 Disp_Typ := Find_Dispatching_Type (Prim);
20705
20706 if Disp_Typ = Full_T
20707 and then (Chars (Prim) /= Name_Op_Ne
20708 or else Comes_From_Source (Prim))
20709 then
20710 Check_Controlling_Formals (Full_T, Prim);
20711
20712 if not Is_Dispatching_Operation (Prim) then
20713 Append_Elmt (Prim, Full_List);
20714 Set_Is_Dispatching_Operation (Prim, True);
20715 Set_DT_Position_Value (Prim, No_Uint);
20716 end if;
20717
20718 elsif Is_Dispatching_Operation (Prim)
20719 and then Disp_Typ /= Full_T
20720 then
20721
20722 -- Verify that it is not otherwise controlled by a
20723 -- formal or a return value of type T.
20724
20725 Check_Controlling_Formals (Disp_Typ, Prim);
20726 end if;
20727 end if;
20728
20729 Next_Entity (Prim);
20730 end loop;
20731 end if;
20732
20733 -- For the tagged case, the two views can share the same primitive
20734 -- operations list and the same class-wide type. Update attributes
20735 -- of the class-wide type which depend on the full declaration.
20736
20737 if Is_Tagged_Type (Priv_T) then
20738 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20739 Set_Class_Wide_Type
20740 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20741
20742 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20743 end if;
20744 end;
20745 end if;
20746
20747 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20748
20749 if Known_To_Have_Preelab_Init (Priv_T) then
20750
20751 -- Case where there is a pragma Preelaborable_Initialization. We
20752 -- always allow this in predefined units, which is cheating a bit,
20753 -- but it means we don't have to struggle to meet the requirements in
20754 -- the RM for having Preelaborable Initialization. Otherwise we
20755 -- require that the type meets the RM rules. But we can't check that
20756 -- yet, because of the rule about overriding Initialize, so we simply
20757 -- set a flag that will be checked at freeze time.
20758
20759 if not In_Predefined_Unit (Full_T) then
20760 Set_Must_Have_Preelab_Init (Full_T);
20761 end if;
20762 end if;
20763
20764 -- If pragma CPP_Class was applied to the private type declaration,
20765 -- propagate it now to the full type declaration.
20766
20767 if Is_CPP_Class (Priv_T) then
20768 Set_Is_CPP_Class (Full_T);
20769 Set_Convention (Full_T, Convention_CPP);
20770
20771 -- Check that components of imported CPP types do not have default
20772 -- expressions.
20773
20774 Check_CPP_Type_Has_No_Defaults (Full_T);
20775 end if;
20776
20777 -- If the private view has user specified stream attributes, then so has
20778 -- the full view.
20779
20780 -- Why the test, how could these flags be already set in Full_T ???
20781
20782 if Has_Specified_Stream_Read (Priv_T) then
20783 Set_Has_Specified_Stream_Read (Full_T);
20784 end if;
20785
20786 if Has_Specified_Stream_Write (Priv_T) then
20787 Set_Has_Specified_Stream_Write (Full_T);
20788 end if;
20789
20790 if Has_Specified_Stream_Input (Priv_T) then
20791 Set_Has_Specified_Stream_Input (Full_T);
20792 end if;
20793
20794 if Has_Specified_Stream_Output (Priv_T) then
20795 Set_Has_Specified_Stream_Output (Full_T);
20796 end if;
20797
20798 -- Propagate Default_Initial_Condition-related attributes from the
20799 -- partial view to the full view and its base type.
20800
20801 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20802 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20803
20804 -- Propagate invariant-related attributes from the partial view to the
20805 -- full view and its base type.
20806
20807 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20808 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20809
20810 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20811 -- in the full view without advertising the inheritance in the partial
20812 -- view. This can only occur when the partial view has no parent type
20813 -- and the full view has an interface as a parent. Any other scenarios
20814 -- are illegal because implemented interfaces must match between the
20815 -- two views.
20816
20817 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20818 declare
20819 Full_Par : constant Entity_Id := Etype (Full_T);
20820 Priv_Par : constant Entity_Id := Etype (Priv_T);
20821
20822 begin
20823 if not Is_Interface (Priv_Par)
20824 and then Is_Interface (Full_Par)
20825 and then Has_Inheritable_Invariants (Full_Par)
20826 then
20827 Error_Msg_N
20828 ("hidden inheritance of class-wide type invariants not "
20829 & "allowed", N);
20830 end if;
20831 end;
20832 end if;
20833
20834 -- Propagate predicates to full type, and predicate function if already
20835 -- defined. It is not clear that this can actually happen? the partial
20836 -- view cannot be frozen yet, and the predicate function has not been
20837 -- built. Still it is a cheap check and seems safer to make it.
20838
20839 if Has_Predicates (Priv_T) then
20840 Set_Has_Predicates (Full_T);
20841
20842 if Present (Predicate_Function (Priv_T)) then
20843 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20844 end if;
20845 end if;
20846
20847 <<Leave>>
20848 Restore_Ghost_Mode (Saved_GM);
20849 end Process_Full_View;
20850
20851 -----------------------------------
20852 -- Process_Incomplete_Dependents --
20853 -----------------------------------
20854
20855 procedure Process_Incomplete_Dependents
20856 (N : Node_Id;
20857 Full_T : Entity_Id;
20858 Inc_T : Entity_Id)
20859 is
20860 Inc_Elmt : Elmt_Id;
20861 Priv_Dep : Entity_Id;
20862 New_Subt : Entity_Id;
20863
20864 Disc_Constraint : Elist_Id;
20865
20866 begin
20867 if No (Private_Dependents (Inc_T)) then
20868 return;
20869 end if;
20870
20871 -- Itypes that may be generated by the completion of an incomplete
20872 -- subtype are not used by the back-end and not attached to the tree.
20873 -- They are created only for constraint-checking purposes.
20874
20875 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20876 while Present (Inc_Elmt) loop
20877 Priv_Dep := Node (Inc_Elmt);
20878
20879 if Ekind (Priv_Dep) = E_Subprogram_Type then
20880
20881 -- An Access_To_Subprogram type may have a return type or a
20882 -- parameter type that is incomplete. Replace with the full view.
20883
20884 if Etype (Priv_Dep) = Inc_T then
20885 Set_Etype (Priv_Dep, Full_T);
20886 end if;
20887
20888 declare
20889 Formal : Entity_Id;
20890
20891 begin
20892 Formal := First_Formal (Priv_Dep);
20893 while Present (Formal) loop
20894 if Etype (Formal) = Inc_T then
20895 Set_Etype (Formal, Full_T);
20896 end if;
20897
20898 Next_Formal (Formal);
20899 end loop;
20900 end;
20901
20902 elsif Is_Overloadable (Priv_Dep) then
20903
20904 -- If a subprogram in the incomplete dependents list is primitive
20905 -- for a tagged full type then mark it as a dispatching operation,
20906 -- check whether it overrides an inherited subprogram, and check
20907 -- restrictions on its controlling formals. Note that a protected
20908 -- operation is never dispatching: only its wrapper operation
20909 -- (which has convention Ada) is.
20910
20911 if Is_Tagged_Type (Full_T)
20912 and then Is_Primitive (Priv_Dep)
20913 and then Convention (Priv_Dep) /= Convention_Protected
20914 then
20915 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20916 Set_Is_Dispatching_Operation (Priv_Dep);
20917 Check_Controlling_Formals (Full_T, Priv_Dep);
20918 end if;
20919
20920 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20921
20922 -- Can happen during processing of a body before the completion
20923 -- of a TA type. Ignore, because spec is also on dependent list.
20924
20925 return;
20926
20927 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20928 -- corresponding subtype of the full view.
20929
20930 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
20931 and then Comes_From_Source (Priv_Dep)
20932 then
20933 Set_Subtype_Indication
20934 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20935 Set_Etype (Priv_Dep, Full_T);
20936 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20937 Set_Analyzed (Parent (Priv_Dep), False);
20938
20939 -- Reanalyze the declaration, suppressing the call to Enter_Name
20940 -- to avoid duplicate names.
20941
20942 Analyze_Subtype_Declaration
20943 (N => Parent (Priv_Dep),
20944 Skip => True);
20945
20946 -- Dependent is a subtype
20947
20948 else
20949 -- We build a new subtype indication using the full view of the
20950 -- incomplete parent. The discriminant constraints have been
20951 -- elaborated already at the point of the subtype declaration.
20952
20953 New_Subt := Create_Itype (E_Void, N);
20954
20955 if Has_Discriminants (Full_T) then
20956 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20957 else
20958 Disc_Constraint := No_Elist;
20959 end if;
20960
20961 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20962 Set_Full_View (Priv_Dep, New_Subt);
20963 end if;
20964
20965 Next_Elmt (Inc_Elmt);
20966 end loop;
20967 end Process_Incomplete_Dependents;
20968
20969 --------------------------------
20970 -- Process_Range_Expr_In_Decl --
20971 --------------------------------
20972
20973 procedure Process_Range_Expr_In_Decl
20974 (R : Node_Id;
20975 T : Entity_Id;
20976 Subtyp : Entity_Id := Empty;
20977 Check_List : List_Id := Empty_List;
20978 R_Check_Off : Boolean := False;
20979 In_Iter_Schm : Boolean := False)
20980 is
20981 Lo, Hi : Node_Id;
20982 R_Checks : Check_Result;
20983 Insert_Node : Node_Id;
20984 Def_Id : Entity_Id;
20985
20986 begin
20987 Analyze_And_Resolve (R, Base_Type (T));
20988
20989 if Nkind (R) = N_Range then
20990
20991 -- In SPARK, all ranges should be static, with the exception of the
20992 -- discrete type definition of a loop parameter specification.
20993
20994 if not In_Iter_Schm
20995 and then not Is_OK_Static_Range (R)
20996 then
20997 Check_SPARK_05_Restriction ("range should be static", R);
20998 end if;
20999
21000 Lo := Low_Bound (R);
21001 Hi := High_Bound (R);
21002
21003 -- Validity checks on the range of a quantified expression are
21004 -- delayed until the construct is transformed into a loop.
21005
21006 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21007 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21008 then
21009 null;
21010
21011 -- We need to ensure validity of the bounds here, because if we
21012 -- go ahead and do the expansion, then the expanded code will get
21013 -- analyzed with range checks suppressed and we miss the check.
21014
21015 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21016 -- the temporaries generated by routine Remove_Side_Effects by means
21017 -- of validity checks must use the same names. When a range appears
21018 -- in the parent of a generic, the range is processed with checks
21019 -- disabled as part of the generic context and with checks enabled
21020 -- for code generation purposes. This leads to link issues as the
21021 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21022 -- template sees the temporaries generated by Remove_Side_Effects.
21023
21024 else
21025 Validity_Check_Range (R, Subtyp);
21026 end if;
21027
21028 -- If there were errors in the declaration, try and patch up some
21029 -- common mistakes in the bounds. The cases handled are literals
21030 -- which are Integer where the expected type is Real and vice versa.
21031 -- These corrections allow the compilation process to proceed further
21032 -- along since some basic assumptions of the format of the bounds
21033 -- are guaranteed.
21034
21035 if Etype (R) = Any_Type then
21036 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21037 Rewrite (Lo,
21038 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21039
21040 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21041 Rewrite (Hi,
21042 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21043
21044 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21045 Rewrite (Lo,
21046 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21047
21048 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21049 Rewrite (Hi,
21050 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21051 end if;
21052
21053 Set_Etype (Lo, T);
21054 Set_Etype (Hi, T);
21055 end if;
21056
21057 -- If the bounds of the range have been mistakenly given as string
21058 -- literals (perhaps in place of character literals), then an error
21059 -- has already been reported, but we rewrite the string literal as a
21060 -- bound of the range's type to avoid blowups in later processing
21061 -- that looks at static values.
21062
21063 if Nkind (Lo) = N_String_Literal then
21064 Rewrite (Lo,
21065 Make_Attribute_Reference (Sloc (Lo),
21066 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21067 Attribute_Name => Name_First));
21068 Analyze_And_Resolve (Lo);
21069 end if;
21070
21071 if Nkind (Hi) = N_String_Literal then
21072 Rewrite (Hi,
21073 Make_Attribute_Reference (Sloc (Hi),
21074 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21075 Attribute_Name => Name_First));
21076 Analyze_And_Resolve (Hi);
21077 end if;
21078
21079 -- If bounds aren't scalar at this point then exit, avoiding
21080 -- problems with further processing of the range in this procedure.
21081
21082 if not Is_Scalar_Type (Etype (Lo)) then
21083 return;
21084 end if;
21085
21086 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21087 -- then range of the base type. Here we check whether the bounds
21088 -- are in the range of the subtype itself. Note that if the bounds
21089 -- represent the null range the Constraint_Error exception should
21090 -- not be raised.
21091
21092 -- ??? The following code should be cleaned up as follows
21093
21094 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21095 -- is done in the call to Range_Check (R, T); below
21096
21097 -- 2. The use of R_Check_Off should be investigated and possibly
21098 -- removed, this would clean up things a bit.
21099
21100 if Is_Null_Range (Lo, Hi) then
21101 null;
21102
21103 else
21104 -- Capture values of bounds and generate temporaries for them
21105 -- if needed, before applying checks, since checks may cause
21106 -- duplication of the expression without forcing evaluation.
21107
21108 -- The forced evaluation removes side effects from expressions,
21109 -- which should occur also in GNATprove mode. Otherwise, we end up
21110 -- with unexpected insertions of actions at places where this is
21111 -- not supposed to occur, e.g. on default parameters of a call.
21112
21113 if Expander_Active or GNATprove_Mode then
21114
21115 -- Call Force_Evaluation to create declarations as needed to
21116 -- deal with side effects, and also create typ_FIRST/LAST
21117 -- entities for bounds if we have a subtype name.
21118
21119 -- Note: we do this transformation even if expansion is not
21120 -- active if we are in GNATprove_Mode since the transformation
21121 -- is in general required to ensure that the resulting tree has
21122 -- proper Ada semantics.
21123
21124 Force_Evaluation
21125 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21126 Force_Evaluation
21127 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21128 end if;
21129
21130 -- We use a flag here instead of suppressing checks on the type
21131 -- because the type we check against isn't necessarily the place
21132 -- where we put the check.
21133
21134 if not R_Check_Off then
21135 R_Checks := Get_Range_Checks (R, T);
21136
21137 -- Look up tree to find an appropriate insertion point. We
21138 -- can't just use insert_actions because later processing
21139 -- depends on the insertion node. Prior to Ada 2012 the
21140 -- insertion point could only be a declaration or a loop, but
21141 -- quantified expressions can appear within any context in an
21142 -- expression, and the insertion point can be any statement,
21143 -- pragma, or declaration.
21144
21145 Insert_Node := Parent (R);
21146 while Present (Insert_Node) loop
21147 exit when
21148 Nkind (Insert_Node) in N_Declaration
21149 and then
21150 not Nkind_In
21151 (Insert_Node, N_Component_Declaration,
21152 N_Loop_Parameter_Specification,
21153 N_Function_Specification,
21154 N_Procedure_Specification);
21155
21156 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21157 or else Nkind (Insert_Node) in
21158 N_Statement_Other_Than_Procedure_Call
21159 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21160 N_Pragma);
21161
21162 Insert_Node := Parent (Insert_Node);
21163 end loop;
21164
21165 -- Why would Type_Decl not be present??? Without this test,
21166 -- short regression tests fail.
21167
21168 if Present (Insert_Node) then
21169
21170 -- Case of loop statement. Verify that the range is part
21171 -- of the subtype indication of the iteration scheme.
21172
21173 if Nkind (Insert_Node) = N_Loop_Statement then
21174 declare
21175 Indic : Node_Id;
21176
21177 begin
21178 Indic := Parent (R);
21179 while Present (Indic)
21180 and then Nkind (Indic) /= N_Subtype_Indication
21181 loop
21182 Indic := Parent (Indic);
21183 end loop;
21184
21185 if Present (Indic) then
21186 Def_Id := Etype (Subtype_Mark (Indic));
21187
21188 Insert_Range_Checks
21189 (R_Checks,
21190 Insert_Node,
21191 Def_Id,
21192 Sloc (Insert_Node),
21193 R,
21194 Do_Before => True);
21195 end if;
21196 end;
21197
21198 -- Insertion before a declaration. If the declaration
21199 -- includes discriminants, the list of applicable checks
21200 -- is given by the caller.
21201
21202 elsif Nkind (Insert_Node) in N_Declaration then
21203 Def_Id := Defining_Identifier (Insert_Node);
21204
21205 if (Ekind (Def_Id) = E_Record_Type
21206 and then Depends_On_Discriminant (R))
21207 or else
21208 (Ekind (Def_Id) = E_Protected_Type
21209 and then Has_Discriminants (Def_Id))
21210 then
21211 Append_Range_Checks
21212 (R_Checks,
21213 Check_List, Def_Id, Sloc (Insert_Node), R);
21214
21215 else
21216 Insert_Range_Checks
21217 (R_Checks,
21218 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21219
21220 end if;
21221
21222 -- Insertion before a statement. Range appears in the
21223 -- context of a quantified expression. Insertion will
21224 -- take place when expression is expanded.
21225
21226 else
21227 null;
21228 end if;
21229 end if;
21230 end if;
21231 end if;
21232
21233 -- Case of other than an explicit N_Range node
21234
21235 -- The forced evaluation removes side effects from expressions, which
21236 -- should occur also in GNATprove mode. Otherwise, we end up with
21237 -- unexpected insertions of actions at places where this is not
21238 -- supposed to occur, e.g. on default parameters of a call.
21239
21240 elsif Expander_Active or GNATprove_Mode then
21241 Get_Index_Bounds (R, Lo, Hi);
21242 Force_Evaluation (Lo);
21243 Force_Evaluation (Hi);
21244 end if;
21245 end Process_Range_Expr_In_Decl;
21246
21247 --------------------------------------
21248 -- Process_Real_Range_Specification --
21249 --------------------------------------
21250
21251 procedure Process_Real_Range_Specification (Def : Node_Id) is
21252 Spec : constant Node_Id := Real_Range_Specification (Def);
21253 Lo : Node_Id;
21254 Hi : Node_Id;
21255 Err : Boolean := False;
21256
21257 procedure Analyze_Bound (N : Node_Id);
21258 -- Analyze and check one bound
21259
21260 -------------------
21261 -- Analyze_Bound --
21262 -------------------
21263
21264 procedure Analyze_Bound (N : Node_Id) is
21265 begin
21266 Analyze_And_Resolve (N, Any_Real);
21267
21268 if not Is_OK_Static_Expression (N) then
21269 Flag_Non_Static_Expr
21270 ("bound in real type definition is not static!", N);
21271 Err := True;
21272 end if;
21273 end Analyze_Bound;
21274
21275 -- Start of processing for Process_Real_Range_Specification
21276
21277 begin
21278 if Present (Spec) then
21279 Lo := Low_Bound (Spec);
21280 Hi := High_Bound (Spec);
21281 Analyze_Bound (Lo);
21282 Analyze_Bound (Hi);
21283
21284 -- If error, clear away junk range specification
21285
21286 if Err then
21287 Set_Real_Range_Specification (Def, Empty);
21288 end if;
21289 end if;
21290 end Process_Real_Range_Specification;
21291
21292 ---------------------
21293 -- Process_Subtype --
21294 ---------------------
21295
21296 function Process_Subtype
21297 (S : Node_Id;
21298 Related_Nod : Node_Id;
21299 Related_Id : Entity_Id := Empty;
21300 Suffix : Character := ' ') return Entity_Id
21301 is
21302 P : Node_Id;
21303 Def_Id : Entity_Id;
21304 Error_Node : Node_Id;
21305 Full_View_Id : Entity_Id;
21306 Subtype_Mark_Id : Entity_Id;
21307
21308 May_Have_Null_Exclusion : Boolean;
21309
21310 procedure Check_Incomplete (T : Node_Id);
21311 -- Called to verify that an incomplete type is not used prematurely
21312
21313 ----------------------
21314 -- Check_Incomplete --
21315 ----------------------
21316
21317 procedure Check_Incomplete (T : Node_Id) is
21318 begin
21319 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21320
21321 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21322 and then
21323 not (Ada_Version >= Ada_2005
21324 and then
21325 (Nkind (Parent (T)) = N_Subtype_Declaration
21326 or else (Nkind (Parent (T)) = N_Subtype_Indication
21327 and then Nkind (Parent (Parent (T))) =
21328 N_Subtype_Declaration)))
21329 then
21330 Error_Msg_N ("invalid use of type before its full declaration", T);
21331 end if;
21332 end Check_Incomplete;
21333
21334 -- Start of processing for Process_Subtype
21335
21336 begin
21337 -- Case of no constraints present
21338
21339 if Nkind (S) /= N_Subtype_Indication then
21340 Find_Type (S);
21341 Check_Incomplete (S);
21342 P := Parent (S);
21343
21344 -- Ada 2005 (AI-231): Static check
21345
21346 if Ada_Version >= Ada_2005
21347 and then Present (P)
21348 and then Null_Exclusion_Present (P)
21349 and then Nkind (P) /= N_Access_To_Object_Definition
21350 and then not Is_Access_Type (Entity (S))
21351 then
21352 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21353 end if;
21354
21355 -- The following is ugly, can't we have a range or even a flag???
21356
21357 May_Have_Null_Exclusion :=
21358 Nkind_In (P, N_Access_Definition,
21359 N_Access_Function_Definition,
21360 N_Access_Procedure_Definition,
21361 N_Access_To_Object_Definition,
21362 N_Allocator,
21363 N_Component_Definition)
21364 or else
21365 Nkind_In (P, N_Derived_Type_Definition,
21366 N_Discriminant_Specification,
21367 N_Formal_Object_Declaration,
21368 N_Object_Declaration,
21369 N_Object_Renaming_Declaration,
21370 N_Parameter_Specification,
21371 N_Subtype_Declaration);
21372
21373 -- Create an Itype that is a duplicate of Entity (S) but with the
21374 -- null-exclusion attribute.
21375
21376 if May_Have_Null_Exclusion
21377 and then Is_Access_Type (Entity (S))
21378 and then Null_Exclusion_Present (P)
21379
21380 -- No need to check the case of an access to object definition.
21381 -- It is correct to define double not-null pointers.
21382
21383 -- Example:
21384 -- type Not_Null_Int_Ptr is not null access Integer;
21385 -- type Acc is not null access Not_Null_Int_Ptr;
21386
21387 and then Nkind (P) /= N_Access_To_Object_Definition
21388 then
21389 if Can_Never_Be_Null (Entity (S)) then
21390 case Nkind (Related_Nod) is
21391 when N_Full_Type_Declaration =>
21392 if Nkind (Type_Definition (Related_Nod))
21393 in N_Array_Type_Definition
21394 then
21395 Error_Node :=
21396 Subtype_Indication
21397 (Component_Definition
21398 (Type_Definition (Related_Nod)));
21399 else
21400 Error_Node :=
21401 Subtype_Indication (Type_Definition (Related_Nod));
21402 end if;
21403
21404 when N_Subtype_Declaration =>
21405 Error_Node := Subtype_Indication (Related_Nod);
21406
21407 when N_Object_Declaration =>
21408 Error_Node := Object_Definition (Related_Nod);
21409
21410 when N_Component_Declaration =>
21411 Error_Node :=
21412 Subtype_Indication (Component_Definition (Related_Nod));
21413
21414 when N_Allocator =>
21415 Error_Node := Expression (Related_Nod);
21416
21417 when others =>
21418 pragma Assert (False);
21419 Error_Node := Related_Nod;
21420 end case;
21421
21422 Error_Msg_NE
21423 ("`NOT NULL` not allowed (& already excludes null)",
21424 Error_Node,
21425 Entity (S));
21426 end if;
21427
21428 Set_Etype (S,
21429 Create_Null_Excluding_Itype
21430 (T => Entity (S),
21431 Related_Nod => P));
21432 Set_Entity (S, Etype (S));
21433 end if;
21434
21435 return Entity (S);
21436
21437 -- Case of constraint present, so that we have an N_Subtype_Indication
21438 -- node (this node is created only if constraints are present).
21439
21440 else
21441 Find_Type (Subtype_Mark (S));
21442
21443 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21444 and then not
21445 (Nkind (Parent (S)) = N_Subtype_Declaration
21446 and then Is_Itype (Defining_Identifier (Parent (S))))
21447 then
21448 Check_Incomplete (Subtype_Mark (S));
21449 end if;
21450
21451 P := Parent (S);
21452 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21453
21454 -- Explicit subtype declaration case
21455
21456 if Nkind (P) = N_Subtype_Declaration then
21457 Def_Id := Defining_Identifier (P);
21458
21459 -- Explicit derived type definition case
21460
21461 elsif Nkind (P) = N_Derived_Type_Definition then
21462 Def_Id := Defining_Identifier (Parent (P));
21463
21464 -- Implicit case, the Def_Id must be created as an implicit type.
21465 -- The one exception arises in the case of concurrent types, array
21466 -- and access types, where other subsidiary implicit types may be
21467 -- created and must appear before the main implicit type. In these
21468 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21469 -- has not yet been called to create Def_Id.
21470
21471 else
21472 if Is_Array_Type (Subtype_Mark_Id)
21473 or else Is_Concurrent_Type (Subtype_Mark_Id)
21474 or else Is_Access_Type (Subtype_Mark_Id)
21475 then
21476 Def_Id := Empty;
21477
21478 -- For the other cases, we create a new unattached Itype,
21479 -- and set the indication to ensure it gets attached later.
21480
21481 else
21482 Def_Id :=
21483 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21484 end if;
21485 end if;
21486
21487 -- If the kind of constraint is invalid for this kind of type,
21488 -- then give an error, and then pretend no constraint was given.
21489
21490 if not Is_Valid_Constraint_Kind
21491 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21492 then
21493 Error_Msg_N
21494 ("incorrect constraint for this kind of type", Constraint (S));
21495
21496 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21497
21498 -- Set Ekind of orphan itype, to prevent cascaded errors
21499
21500 if Present (Def_Id) then
21501 Set_Ekind (Def_Id, Ekind (Any_Type));
21502 end if;
21503
21504 -- Make recursive call, having got rid of the bogus constraint
21505
21506 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21507 end if;
21508
21509 -- Remaining processing depends on type. Select on Base_Type kind to
21510 -- ensure getting to the concrete type kind in the case of a private
21511 -- subtype (needed when only doing semantic analysis).
21512
21513 case Ekind (Base_Type (Subtype_Mark_Id)) is
21514 when Access_Kind =>
21515
21516 -- If this is a constraint on a class-wide type, discard it.
21517 -- There is currently no way to express a partial discriminant
21518 -- constraint on a type with unknown discriminants. This is
21519 -- a pathology that the ACATS wisely decides not to test.
21520
21521 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21522 if Comes_From_Source (S) then
21523 Error_Msg_N
21524 ("constraint on class-wide type ignored??",
21525 Constraint (S));
21526 end if;
21527
21528 if Nkind (P) = N_Subtype_Declaration then
21529 Set_Subtype_Indication (P,
21530 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21531 end if;
21532
21533 return Subtype_Mark_Id;
21534 end if;
21535
21536 Constrain_Access (Def_Id, S, Related_Nod);
21537
21538 if Expander_Active
21539 and then Is_Itype (Designated_Type (Def_Id))
21540 and then Nkind (Related_Nod) = N_Subtype_Declaration
21541 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21542 then
21543 Build_Itype_Reference
21544 (Designated_Type (Def_Id), Related_Nod);
21545 end if;
21546
21547 when Array_Kind =>
21548 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21549
21550 when Decimal_Fixed_Point_Kind =>
21551 Constrain_Decimal (Def_Id, S);
21552
21553 when Enumeration_Kind =>
21554 Constrain_Enumeration (Def_Id, S);
21555 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21556
21557 when Ordinary_Fixed_Point_Kind =>
21558 Constrain_Ordinary_Fixed (Def_Id, S);
21559
21560 when Float_Kind =>
21561 Constrain_Float (Def_Id, S);
21562
21563 when Integer_Kind =>
21564 Constrain_Integer (Def_Id, S);
21565 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21566
21567 when Class_Wide_Kind
21568 | E_Incomplete_Type
21569 | E_Record_Subtype
21570 | E_Record_Type
21571 =>
21572 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21573
21574 if Ekind (Def_Id) = E_Incomplete_Type then
21575 Set_Private_Dependents (Def_Id, New_Elmt_List);
21576 end if;
21577
21578 when Private_Kind =>
21579 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21580
21581 -- The base type may be private but Def_Id may be a full view
21582 -- in an instance.
21583
21584 if Is_Private_Type (Def_Id) then
21585 Set_Private_Dependents (Def_Id, New_Elmt_List);
21586 end if;
21587
21588 -- In case of an invalid constraint prevent further processing
21589 -- since the type constructed is missing expected fields.
21590
21591 if Etype (Def_Id) = Any_Type then
21592 return Def_Id;
21593 end if;
21594
21595 -- If the full view is that of a task with discriminants,
21596 -- we must constrain both the concurrent type and its
21597 -- corresponding record type. Otherwise we will just propagate
21598 -- the constraint to the full view, if available.
21599
21600 if Present (Full_View (Subtype_Mark_Id))
21601 and then Has_Discriminants (Subtype_Mark_Id)
21602 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21603 then
21604 Full_View_Id :=
21605 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21606
21607 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21608 Constrain_Concurrent (Full_View_Id, S,
21609 Related_Nod, Related_Id, Suffix);
21610 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21611 Set_Full_View (Def_Id, Full_View_Id);
21612
21613 -- Introduce an explicit reference to the private subtype,
21614 -- to prevent scope anomalies in gigi if first use appears
21615 -- in a nested context, e.g. a later function body.
21616 -- Should this be generated in other contexts than a full
21617 -- type declaration?
21618
21619 if Is_Itype (Def_Id)
21620 and then
21621 Nkind (Parent (P)) = N_Full_Type_Declaration
21622 then
21623 Build_Itype_Reference (Def_Id, Parent (P));
21624 end if;
21625
21626 else
21627 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21628 end if;
21629
21630 when Concurrent_Kind =>
21631 Constrain_Concurrent (Def_Id, S,
21632 Related_Nod, Related_Id, Suffix);
21633
21634 when others =>
21635 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21636 end case;
21637
21638 -- Size, Alignment, Representation aspects and Convention are always
21639 -- inherited from the base type.
21640
21641 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21642 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21643 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21644
21645 return Def_Id;
21646 end if;
21647 end Process_Subtype;
21648
21649 -----------------------------
21650 -- Record_Type_Declaration --
21651 -----------------------------
21652
21653 procedure Record_Type_Declaration
21654 (T : Entity_Id;
21655 N : Node_Id;
21656 Prev : Entity_Id)
21657 is
21658 Def : constant Node_Id := Type_Definition (N);
21659 Is_Tagged : Boolean;
21660 Tag_Comp : Entity_Id;
21661
21662 begin
21663 -- These flags must be initialized before calling Process_Discriminants
21664 -- because this routine makes use of them.
21665
21666 Set_Ekind (T, E_Record_Type);
21667 Set_Etype (T, T);
21668 Init_Size_Align (T);
21669 Set_Interfaces (T, No_Elist);
21670 Set_Stored_Constraint (T, No_Elist);
21671 Set_Default_SSO (T);
21672 Set_No_Reordering (T, No_Component_Reordering);
21673
21674 -- Normal case
21675
21676 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21677 if Limited_Present (Def) then
21678 Check_SPARK_05_Restriction ("limited is not allowed", N);
21679 end if;
21680
21681 if Abstract_Present (Def) then
21682 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21683 end if;
21684
21685 -- The flag Is_Tagged_Type might have already been set by
21686 -- Find_Type_Name if it detected an error for declaration T. This
21687 -- arises in the case of private tagged types where the full view
21688 -- omits the word tagged.
21689
21690 Is_Tagged :=
21691 Tagged_Present (Def)
21692 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21693
21694 Set_Is_Limited_Record (T, Limited_Present (Def));
21695
21696 if Is_Tagged then
21697 Set_Is_Tagged_Type (T, True);
21698 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21699 end if;
21700
21701 -- Type is abstract if full declaration carries keyword, or if
21702 -- previous partial view did.
21703
21704 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21705 or else Abstract_Present (Def));
21706
21707 else
21708 Check_SPARK_05_Restriction ("interface is not allowed", N);
21709
21710 Is_Tagged := True;
21711 Analyze_Interface_Declaration (T, Def);
21712
21713 if Present (Discriminant_Specifications (N)) then
21714 Error_Msg_N
21715 ("interface types cannot have discriminants",
21716 Defining_Identifier
21717 (First (Discriminant_Specifications (N))));
21718 end if;
21719 end if;
21720
21721 -- First pass: if there are self-referential access components,
21722 -- create the required anonymous access type declarations, and if
21723 -- need be an incomplete type declaration for T itself.
21724
21725 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21726
21727 if Ada_Version >= Ada_2005
21728 and then Present (Interface_List (Def))
21729 then
21730 Check_Interfaces (N, Def);
21731
21732 declare
21733 Ifaces_List : Elist_Id;
21734
21735 begin
21736 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21737 -- already in the parents.
21738
21739 Collect_Interfaces
21740 (T => T,
21741 Ifaces_List => Ifaces_List,
21742 Exclude_Parents => True);
21743
21744 Set_Interfaces (T, Ifaces_List);
21745 end;
21746 end if;
21747
21748 -- Records constitute a scope for the component declarations within.
21749 -- The scope is created prior to the processing of these declarations.
21750 -- Discriminants are processed first, so that they are visible when
21751 -- processing the other components. The Ekind of the record type itself
21752 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21753
21754 -- Enter record scope
21755
21756 Push_Scope (T);
21757
21758 -- If an incomplete or private type declaration was already given for
21759 -- the type, then this scope already exists, and the discriminants have
21760 -- been declared within. We must verify that the full declaration
21761 -- matches the incomplete one.
21762
21763 Check_Or_Process_Discriminants (N, T, Prev);
21764
21765 Set_Is_Constrained (T, not Has_Discriminants (T));
21766 Set_Has_Delayed_Freeze (T, True);
21767
21768 -- For tagged types add a manually analyzed component corresponding
21769 -- to the component _tag, the corresponding piece of tree will be
21770 -- expanded as part of the freezing actions if it is not a CPP_Class.
21771
21772 if Is_Tagged then
21773
21774 -- Do not add the tag unless we are in expansion mode
21775
21776 if Expander_Active then
21777 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21778 Enter_Name (Tag_Comp);
21779
21780 Set_Ekind (Tag_Comp, E_Component);
21781 Set_Is_Tag (Tag_Comp);
21782 Set_Is_Aliased (Tag_Comp);
21783 Set_Etype (Tag_Comp, RTE (RE_Tag));
21784 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21785 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21786 Init_Component_Location (Tag_Comp);
21787
21788 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21789 -- implemented interfaces.
21790
21791 if Has_Interfaces (T) then
21792 Add_Interface_Tag_Components (N, T);
21793 end if;
21794 end if;
21795
21796 Make_Class_Wide_Type (T);
21797 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21798 end if;
21799
21800 -- We must suppress range checks when processing record components in
21801 -- the presence of discriminants, since we don't want spurious checks to
21802 -- be generated during their analysis, but Suppress_Range_Checks flags
21803 -- must be reset the after processing the record definition.
21804
21805 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21806 -- couldn't we just use the normal range check suppression method here.
21807 -- That would seem cleaner ???
21808
21809 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21810 Set_Kill_Range_Checks (T, True);
21811 Record_Type_Definition (Def, Prev);
21812 Set_Kill_Range_Checks (T, False);
21813 else
21814 Record_Type_Definition (Def, Prev);
21815 end if;
21816
21817 -- Exit from record scope
21818
21819 End_Scope;
21820
21821 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21822 -- the implemented interfaces and associate them an aliased entity.
21823
21824 if Is_Tagged
21825 and then not Is_Empty_List (Interface_List (Def))
21826 then
21827 Derive_Progenitor_Subprograms (T, T);
21828 end if;
21829
21830 Check_Function_Writable_Actuals (N);
21831 end Record_Type_Declaration;
21832
21833 ----------------------------
21834 -- Record_Type_Definition --
21835 ----------------------------
21836
21837 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21838 Component : Entity_Id;
21839 Ctrl_Components : Boolean := False;
21840 Final_Storage_Only : Boolean;
21841 T : Entity_Id;
21842
21843 begin
21844 if Ekind (Prev_T) = E_Incomplete_Type then
21845 T := Full_View (Prev_T);
21846 else
21847 T := Prev_T;
21848 end if;
21849
21850 -- In SPARK, tagged types and type extensions may only be declared in
21851 -- the specification of library unit packages.
21852
21853 if Present (Def) and then Is_Tagged_Type (T) then
21854 declare
21855 Typ : Node_Id;
21856 Ctxt : Node_Id;
21857
21858 begin
21859 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21860 Typ := Parent (Def);
21861 else
21862 pragma Assert
21863 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21864 Typ := Parent (Parent (Def));
21865 end if;
21866
21867 Ctxt := Parent (Typ);
21868
21869 if Nkind (Ctxt) = N_Package_Body
21870 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21871 then
21872 Check_SPARK_05_Restriction
21873 ("type should be defined in package specification", Typ);
21874
21875 elsif Nkind (Ctxt) /= N_Package_Specification
21876 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21877 then
21878 Check_SPARK_05_Restriction
21879 ("type should be defined in library unit package", Typ);
21880 end if;
21881 end;
21882 end if;
21883
21884 Final_Storage_Only := not Is_Controlled (T);
21885
21886 -- Ada 2005: Check whether an explicit Limited is present in a derived
21887 -- type declaration.
21888
21889 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21890 and then Limited_Present (Parent (Def))
21891 then
21892 Set_Is_Limited_Record (T);
21893 end if;
21894
21895 -- If the component list of a record type is defined by the reserved
21896 -- word null and there is no discriminant part, then the record type has
21897 -- no components and all records of the type are null records (RM 3.7)
21898 -- This procedure is also called to process the extension part of a
21899 -- record extension, in which case the current scope may have inherited
21900 -- components.
21901
21902 if No (Def)
21903 or else No (Component_List (Def))
21904 or else Null_Present (Component_List (Def))
21905 then
21906 if not Is_Tagged_Type (T) then
21907 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21908 end if;
21909
21910 else
21911 Analyze_Declarations (Component_Items (Component_List (Def)));
21912
21913 if Present (Variant_Part (Component_List (Def))) then
21914 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21915 Analyze (Variant_Part (Component_List (Def)));
21916 end if;
21917 end if;
21918
21919 -- After completing the semantic analysis of the record definition,
21920 -- record components, both new and inherited, are accessible. Set their
21921 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21922 -- whose Ekind may be void.
21923
21924 Component := First_Entity (Current_Scope);
21925 while Present (Component) loop
21926 if Ekind (Component) = E_Void
21927 and then not Is_Itype (Component)
21928 then
21929 Set_Ekind (Component, E_Component);
21930 Init_Component_Location (Component);
21931 end if;
21932
21933 Propagate_Concurrent_Flags (T, Etype (Component));
21934
21935 if Ekind (Component) /= E_Component then
21936 null;
21937
21938 -- Do not set Has_Controlled_Component on a class-wide equivalent
21939 -- type. See Make_CW_Equivalent_Type.
21940
21941 elsif not Is_Class_Wide_Equivalent_Type (T)
21942 and then (Has_Controlled_Component (Etype (Component))
21943 or else (Chars (Component) /= Name_uParent
21944 and then Is_Controlled (Etype (Component))))
21945 then
21946 Set_Has_Controlled_Component (T, True);
21947 Final_Storage_Only :=
21948 Final_Storage_Only
21949 and then Finalize_Storage_Only (Etype (Component));
21950 Ctrl_Components := True;
21951 end if;
21952
21953 Next_Entity (Component);
21954 end loop;
21955
21956 -- A Type is Finalize_Storage_Only only if all its controlled components
21957 -- are also.
21958
21959 if Ctrl_Components then
21960 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21961 end if;
21962
21963 -- Place reference to end record on the proper entity, which may
21964 -- be a partial view.
21965
21966 if Present (Def) then
21967 Process_End_Label (Def, 'e', Prev_T);
21968 end if;
21969 end Record_Type_Definition;
21970
21971 ------------------------
21972 -- Replace_Components --
21973 ------------------------
21974
21975 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21976 function Process (N : Node_Id) return Traverse_Result;
21977
21978 -------------
21979 -- Process --
21980 -------------
21981
21982 function Process (N : Node_Id) return Traverse_Result is
21983 Comp : Entity_Id;
21984
21985 begin
21986 if Nkind (N) = N_Discriminant_Specification then
21987 Comp := First_Discriminant (Typ);
21988 while Present (Comp) loop
21989 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21990 Set_Defining_Identifier (N, Comp);
21991 exit;
21992 end if;
21993
21994 Next_Discriminant (Comp);
21995 end loop;
21996
21997 elsif Nkind (N) = N_Variant_Part then
21998 Comp := First_Discriminant (Typ);
21999 while Present (Comp) loop
22000 if Chars (Comp) = Chars (Name (N)) then
22001 Set_Entity (Name (N), Comp);
22002 exit;
22003 end if;
22004
22005 Next_Discriminant (Comp);
22006 end loop;
22007
22008 elsif Nkind (N) = N_Component_Declaration then
22009 Comp := First_Component (Typ);
22010 while Present (Comp) loop
22011 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22012 Set_Defining_Identifier (N, Comp);
22013 exit;
22014 end if;
22015
22016 Next_Component (Comp);
22017 end loop;
22018 end if;
22019
22020 return OK;
22021 end Process;
22022
22023 procedure Replace is new Traverse_Proc (Process);
22024
22025 -- Start of processing for Replace_Components
22026
22027 begin
22028 Replace (Decl);
22029 end Replace_Components;
22030
22031 -------------------------------
22032 -- Set_Completion_Referenced --
22033 -------------------------------
22034
22035 procedure Set_Completion_Referenced (E : Entity_Id) is
22036 begin
22037 -- If in main unit, mark entity that is a completion as referenced,
22038 -- warnings go on the partial view when needed.
22039
22040 if In_Extended_Main_Source_Unit (E) then
22041 Set_Referenced (E);
22042 end if;
22043 end Set_Completion_Referenced;
22044
22045 ---------------------
22046 -- Set_Default_SSO --
22047 ---------------------
22048
22049 procedure Set_Default_SSO (T : Entity_Id) is
22050 begin
22051 case Opt.Default_SSO is
22052 when ' ' =>
22053 null;
22054 when 'L' =>
22055 Set_SSO_Set_Low_By_Default (T, True);
22056 when 'H' =>
22057 Set_SSO_Set_High_By_Default (T, True);
22058 when others =>
22059 raise Program_Error;
22060 end case;
22061 end Set_Default_SSO;
22062
22063 ---------------------
22064 -- Set_Fixed_Range --
22065 ---------------------
22066
22067 -- The range for fixed-point types is complicated by the fact that we
22068 -- do not know the exact end points at the time of the declaration. This
22069 -- is true for three reasons:
22070
22071 -- A size clause may affect the fudging of the end-points.
22072 -- A small clause may affect the values of the end-points.
22073 -- We try to include the end-points if it does not affect the size.
22074
22075 -- This means that the actual end-points must be established at the
22076 -- point when the type is frozen. Meanwhile, we first narrow the range
22077 -- as permitted (so that it will fit if necessary in a small specified
22078 -- size), and then build a range subtree with these narrowed bounds.
22079 -- Set_Fixed_Range constructs the range from real literal values, and
22080 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22081
22082 -- The parent of this range is set to point to the entity so that it is
22083 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22084 -- other scalar types, which are just pointers to the range in the
22085 -- original tree, this would otherwise be an orphan).
22086
22087 -- The tree is left unanalyzed. When the type is frozen, the processing
22088 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22089 -- analyzed, and uses this as an indication that it should complete
22090 -- work on the range (it will know the final small and size values).
22091
22092 procedure Set_Fixed_Range
22093 (E : Entity_Id;
22094 Loc : Source_Ptr;
22095 Lo : Ureal;
22096 Hi : Ureal)
22097 is
22098 S : constant Node_Id :=
22099 Make_Range (Loc,
22100 Low_Bound => Make_Real_Literal (Loc, Lo),
22101 High_Bound => Make_Real_Literal (Loc, Hi));
22102 begin
22103 Set_Scalar_Range (E, S);
22104 Set_Parent (S, E);
22105
22106 -- Before the freeze point, the bounds of a fixed point are universal
22107 -- and carry the corresponding type.
22108
22109 Set_Etype (Low_Bound (S), Universal_Real);
22110 Set_Etype (High_Bound (S), Universal_Real);
22111 end Set_Fixed_Range;
22112
22113 ----------------------------------
22114 -- Set_Scalar_Range_For_Subtype --
22115 ----------------------------------
22116
22117 procedure Set_Scalar_Range_For_Subtype
22118 (Def_Id : Entity_Id;
22119 R : Node_Id;
22120 Subt : Entity_Id)
22121 is
22122 Kind : constant Entity_Kind := Ekind (Def_Id);
22123
22124 begin
22125 -- Defend against previous error
22126
22127 if Nkind (R) = N_Error then
22128 return;
22129 end if;
22130
22131 Set_Scalar_Range (Def_Id, R);
22132
22133 -- We need to link the range into the tree before resolving it so
22134 -- that types that are referenced, including importantly the subtype
22135 -- itself, are properly frozen (Freeze_Expression requires that the
22136 -- expression be properly linked into the tree). Of course if it is
22137 -- already linked in, then we do not disturb the current link.
22138
22139 if No (Parent (R)) then
22140 Set_Parent (R, Def_Id);
22141 end if;
22142
22143 -- Reset the kind of the subtype during analysis of the range, to
22144 -- catch possible premature use in the bounds themselves.
22145
22146 Set_Ekind (Def_Id, E_Void);
22147 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22148 Set_Ekind (Def_Id, Kind);
22149 end Set_Scalar_Range_For_Subtype;
22150
22151 --------------------------------------------------------
22152 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22153 --------------------------------------------------------
22154
22155 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22156 (E : Entity_Id)
22157 is
22158 begin
22159 -- Make sure set if encountered during Expand_To_Stored_Constraint
22160
22161 Set_Stored_Constraint (E, No_Elist);
22162
22163 -- Give it the right value
22164
22165 if Is_Constrained (E) and then Has_Discriminants (E) then
22166 Set_Stored_Constraint (E,
22167 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22168 end if;
22169 end Set_Stored_Constraint_From_Discriminant_Constraint;
22170
22171 -------------------------------------
22172 -- Signed_Integer_Type_Declaration --
22173 -------------------------------------
22174
22175 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22176 Implicit_Base : Entity_Id;
22177 Base_Typ : Entity_Id;
22178 Lo_Val : Uint;
22179 Hi_Val : Uint;
22180 Errs : Boolean := False;
22181 Lo : Node_Id;
22182 Hi : Node_Id;
22183
22184 function Can_Derive_From (E : Entity_Id) return Boolean;
22185 -- Determine whether given bounds allow derivation from specified type
22186
22187 procedure Check_Bound (Expr : Node_Id);
22188 -- Check bound to make sure it is integral and static. If not, post
22189 -- appropriate error message and set Errs flag
22190
22191 ---------------------
22192 -- Can_Derive_From --
22193 ---------------------
22194
22195 -- Note we check both bounds against both end values, to deal with
22196 -- strange types like ones with a range of 0 .. -12341234.
22197
22198 function Can_Derive_From (E : Entity_Id) return Boolean is
22199 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22200 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22201 begin
22202 return Lo <= Lo_Val and then Lo_Val <= Hi
22203 and then
22204 Lo <= Hi_Val and then Hi_Val <= Hi;
22205 end Can_Derive_From;
22206
22207 -----------------
22208 -- Check_Bound --
22209 -----------------
22210
22211 procedure Check_Bound (Expr : Node_Id) is
22212 begin
22213 -- If a range constraint is used as an integer type definition, each
22214 -- bound of the range must be defined by a static expression of some
22215 -- integer type, but the two bounds need not have the same integer
22216 -- type (Negative bounds are allowed.) (RM 3.5.4)
22217
22218 if not Is_Integer_Type (Etype (Expr)) then
22219 Error_Msg_N
22220 ("integer type definition bounds must be of integer type", Expr);
22221 Errs := True;
22222
22223 elsif not Is_OK_Static_Expression (Expr) then
22224 Flag_Non_Static_Expr
22225 ("non-static expression used for integer type bound!", Expr);
22226 Errs := True;
22227
22228 -- The bounds are folded into literals, and we set their type to be
22229 -- universal, to avoid typing difficulties: we cannot set the type
22230 -- of the literal to the new type, because this would be a forward
22231 -- reference for the back end, and if the original type is user-
22232 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22233
22234 else
22235 if Is_Entity_Name (Expr) then
22236 Fold_Uint (Expr, Expr_Value (Expr), True);
22237 end if;
22238
22239 Set_Etype (Expr, Universal_Integer);
22240 end if;
22241 end Check_Bound;
22242
22243 -- Start of processing for Signed_Integer_Type_Declaration
22244
22245 begin
22246 -- Create an anonymous base type
22247
22248 Implicit_Base :=
22249 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22250
22251 -- Analyze and check the bounds, they can be of any integer type
22252
22253 Lo := Low_Bound (Def);
22254 Hi := High_Bound (Def);
22255
22256 -- Arbitrarily use Integer as the type if either bound had an error
22257
22258 if Hi = Error or else Lo = Error then
22259 Base_Typ := Any_Integer;
22260 Set_Error_Posted (T, True);
22261
22262 -- Here both bounds are OK expressions
22263
22264 else
22265 Analyze_And_Resolve (Lo, Any_Integer);
22266 Analyze_And_Resolve (Hi, Any_Integer);
22267
22268 Check_Bound (Lo);
22269 Check_Bound (Hi);
22270
22271 if Errs then
22272 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22273 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22274 end if;
22275
22276 -- Find type to derive from
22277
22278 Lo_Val := Expr_Value (Lo);
22279 Hi_Val := Expr_Value (Hi);
22280
22281 if Can_Derive_From (Standard_Short_Short_Integer) then
22282 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22283
22284 elsif Can_Derive_From (Standard_Short_Integer) then
22285 Base_Typ := Base_Type (Standard_Short_Integer);
22286
22287 elsif Can_Derive_From (Standard_Integer) then
22288 Base_Typ := Base_Type (Standard_Integer);
22289
22290 elsif Can_Derive_From (Standard_Long_Integer) then
22291 Base_Typ := Base_Type (Standard_Long_Integer);
22292
22293 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22294 Check_Restriction (No_Long_Long_Integers, Def);
22295 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22296
22297 else
22298 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22299 Error_Msg_N ("integer type definition bounds out of range", Def);
22300 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22301 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22302 end if;
22303 end if;
22304
22305 -- Complete both implicit base and declared first subtype entities. The
22306 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22307 -- are not clobbered when the signed integer type acts as a full view of
22308 -- a private type.
22309
22310 Set_Etype (Implicit_Base, Base_Typ);
22311 Set_Size_Info (Implicit_Base, Base_Typ);
22312 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22313 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22314 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22315
22316 Set_Ekind (T, E_Signed_Integer_Subtype);
22317 Set_Etype (T, Implicit_Base);
22318 Set_Size_Info (T, Implicit_Base);
22319 Inherit_Rep_Item_Chain (T, Implicit_Base);
22320 Set_Scalar_Range (T, Def);
22321 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22322 Set_Is_Constrained (T);
22323 end Signed_Integer_Type_Declaration;
22324
22325 end Sem_Ch3;