]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch3.adb
[Ada] Remove obsolete Is_For_Access_Subtype machinery
[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-2019, 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.
225
226 function Build_Scalar_Bound
227 (Bound : Node_Id;
228 Par_T : Entity_Id;
229 Der_T : Entity_Id) return Node_Id;
230 -- The bounds of a derived scalar type are conversions of the bounds of
231 -- the parent type. Optimize the representation if the bounds are literals.
232 -- Needs a more complete spec--what are the parameters exactly, and what
233 -- exactly is the returned value, and how is Bound affected???
234
235 procedure Build_Underlying_Full_View
236 (N : Node_Id;
237 Typ : Entity_Id;
238 Par : Entity_Id);
239 -- If the completion of a private type is itself derived from a private
240 -- type, or if the full view of a private subtype is itself private, the
241 -- back-end has no way to compute the actual size of this type. We build
242 -- an internal subtype declaration of the proper parent type to convey
243 -- this information. This extra mechanism is needed because a full
244 -- view cannot itself have a full view (it would get clobbered during
245 -- view exchanges).
246
247 procedure Check_Access_Discriminant_Requires_Limited
248 (D : Node_Id;
249 Loc : Node_Id);
250 -- Check the restriction that the type to which an access discriminant
251 -- belongs must be a concurrent type or a descendant of a type with
252 -- the reserved word 'limited' in its declaration.
253
254 procedure Check_Anonymous_Access_Components
255 (Typ_Decl : Node_Id;
256 Typ : Entity_Id;
257 Prev : Entity_Id;
258 Comp_List : Node_Id);
259 -- Ada 2005 AI-382: an access component in a record definition can refer to
260 -- the enclosing record, in which case it denotes the type itself, and not
261 -- the current instance of the type. We create an anonymous access type for
262 -- the component, and flag it as an access to a component, so accessibility
263 -- checks are properly performed on it. The declaration of the access type
264 -- is placed ahead of that of the record to prevent order-of-elaboration
265 -- circularity issues in Gigi. We create an incomplete type for the record
266 -- declaration, which is the designated type of the anonymous access.
267
268 procedure Check_Delta_Expression (E : Node_Id);
269 -- Check that the expression represented by E is suitable for use as a
270 -- delta expression, i.e. it is of real type and is static.
271
272 procedure Check_Digits_Expression (E : Node_Id);
273 -- Check that the expression represented by E is suitable for use as a
274 -- digits expression, i.e. it is of integer type, positive and static.
275
276 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
277 -- Validate the initialization of an object declaration. T is the required
278 -- type, and Exp is the initialization expression.
279
280 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
281 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
282
283 procedure Check_Or_Process_Discriminants
284 (N : Node_Id;
285 T : Entity_Id;
286 Prev : Entity_Id := Empty);
287 -- If N is the full declaration of the completion T of an incomplete or
288 -- private type, check its discriminants (which are already known to be
289 -- conformant with those of the partial view, see Find_Type_Name),
290 -- otherwise process them. Prev is the entity of the partial declaration,
291 -- if any.
292
293 procedure Check_Real_Bound (Bound : Node_Id);
294 -- Check given bound for being of real type and static. If not, post an
295 -- appropriate message, and rewrite the bound with the real literal zero.
296
297 procedure Constant_Redeclaration
298 (Id : Entity_Id;
299 N : Node_Id;
300 T : out Entity_Id);
301 -- Various checks on legality of full declaration of deferred constant.
302 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
303 -- node. The caller has not yet set any attributes of this entity.
304
305 function Contain_Interface
306 (Iface : Entity_Id;
307 Ifaces : Elist_Id) return Boolean;
308 -- Ada 2005: Determine whether Iface is present in the list Ifaces
309
310 procedure Convert_Scalar_Bounds
311 (N : Node_Id;
312 Parent_Type : Entity_Id;
313 Derived_Type : Entity_Id;
314 Loc : Source_Ptr);
315 -- For derived scalar types, convert the bounds in the type definition to
316 -- the derived type, and complete their analysis. Given a constraint of the
317 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
318 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
319 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
320 -- subtype are conversions of those bounds to the derived_type, so that
321 -- their typing is consistent.
322
323 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
324 -- Copies attributes from array base type T2 to array base type T1. Copies
325 -- only attributes that apply to base types, but not subtypes.
326
327 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
328 -- Copies attributes from array subtype T2 to array subtype T1. Copies
329 -- attributes that apply to both subtypes and base types.
330
331 procedure Create_Constrained_Components
332 (Subt : Entity_Id;
333 Decl_Node : Node_Id;
334 Typ : Entity_Id;
335 Constraints : Elist_Id);
336 -- Build the list of entities for a constrained discriminated record
337 -- subtype. If a component depends on a discriminant, replace its subtype
338 -- using the discriminant values in the discriminant constraint. Subt
339 -- is the defining identifier for the subtype whose list of constrained
340 -- entities we will create. Decl_Node is the type declaration node where
341 -- we will attach all the itypes created. Typ is the base discriminated
342 -- type for the subtype Subt. Constraints is the list of discriminant
343 -- constraints for Typ.
344
345 function Constrain_Component_Type
346 (Comp : Entity_Id;
347 Constrained_Typ : Entity_Id;
348 Related_Node : Node_Id;
349 Typ : Entity_Id;
350 Constraints : Elist_Id) return Entity_Id;
351 -- Given a discriminated base type Typ, a list of discriminant constraints,
352 -- Constraints, for Typ and a component Comp of Typ, create and return the
353 -- type corresponding to Etype (Comp) where all discriminant references
354 -- are replaced with the corresponding constraint. If Etype (Comp) contains
355 -- no discriminant references then it is returned as-is. Constrained_Typ
356 -- is the final constrained subtype to which the constrained component
357 -- belongs. Related_Node is the node where we attach all created itypes.
358
359 procedure Constrain_Access
360 (Def_Id : in out Entity_Id;
361 S : Node_Id;
362 Related_Nod : Node_Id);
363 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
364 -- an anonymous type created for a subtype indication. In that case it is
365 -- created in the procedure and attached to Related_Nod.
366
367 procedure Constrain_Array
368 (Def_Id : in out Entity_Id;
369 SI : Node_Id;
370 Related_Nod : Node_Id;
371 Related_Id : Entity_Id;
372 Suffix : Character);
373 -- Apply a list of index constraints to an unconstrained array type. The
374 -- first parameter is the entity for the resulting subtype. A value of
375 -- Empty for Def_Id indicates that an implicit type must be created, but
376 -- creation is delayed (and must be done by this procedure) because other
377 -- subsidiary implicit types must be created first (which is why Def_Id
378 -- is an in/out parameter). The second parameter is a subtype indication
379 -- node for the constrained array to be created (e.g. something of the
380 -- form string (1 .. 10)). Related_Nod gives the place where this type
381 -- has to be inserted in the tree. The Related_Id and Suffix parameters
382 -- are used to build the associated Implicit type name.
383
384 procedure Constrain_Concurrent
385 (Def_Id : in out Entity_Id;
386 SI : Node_Id;
387 Related_Nod : Node_Id;
388 Related_Id : Entity_Id;
389 Suffix : Character);
390 -- Apply list of discriminant constraints to an unconstrained concurrent
391 -- type.
392 --
393 -- SI is the N_Subtype_Indication node containing the constraint and
394 -- the unconstrained type to constrain.
395 --
396 -- Def_Id is the entity for the resulting constrained subtype. A value
397 -- of Empty for Def_Id indicates that an implicit type must be created,
398 -- but creation is delayed (and must be done by this procedure) because
399 -- other subsidiary implicit types must be created first (which is why
400 -- Def_Id is an in/out parameter).
401 --
402 -- Related_Nod gives the place where this type has to be inserted
403 -- in the tree.
404 --
405 -- The last two arguments are used to create its external name if needed.
406
407 function Constrain_Corresponding_Record
408 (Prot_Subt : Entity_Id;
409 Corr_Rec : Entity_Id;
410 Related_Nod : Node_Id) return Entity_Id;
411 -- When constraining a protected type or task type with discriminants,
412 -- constrain the corresponding record with the same discriminant values.
413
414 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
415 -- Constrain a decimal fixed point type with a digits constraint and/or a
416 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
417
418 procedure Constrain_Discriminated_Type
419 (Def_Id : Entity_Id;
420 S : Node_Id;
421 Related_Nod : Node_Id;
422 For_Access : Boolean := False);
423 -- Process discriminant constraints of composite type. Verify that values
424 -- have been provided for all discriminants, that the original type is
425 -- unconstrained, and that the types of the supplied expressions match
426 -- the discriminant types. The first three parameters are like in routine
427 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
428 -- of For_Access.
429
430 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
431 -- Constrain an enumeration type with a range constraint. This is identical
432 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
433
434 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
435 -- Constrain a floating point type with either a digits constraint
436 -- and/or a range constraint, building a E_Floating_Point_Subtype.
437
438 procedure Constrain_Index
439 (Index : Node_Id;
440 S : Node_Id;
441 Related_Nod : Node_Id;
442 Related_Id : Entity_Id;
443 Suffix : Character;
444 Suffix_Index : Nat);
445 -- Process an index constraint S in a constrained array declaration. The
446 -- constraint can be a subtype name, or a range with or without an explicit
447 -- subtype mark. The index is the corresponding index of the unconstrained
448 -- array. The Related_Id and Suffix parameters are used to build the
449 -- associated Implicit type name.
450
451 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
452 -- Build subtype of a signed or modular integer type
453
454 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
455 -- Constrain an ordinary fixed point type with a range constraint, and
456 -- build an E_Ordinary_Fixed_Point_Subtype entity.
457
458 procedure Copy_And_Swap (Priv, Full : Entity_Id);
459 -- Copy the Priv entity into the entity of its full declaration then swap
460 -- the two entities in such a manner that the former private type is now
461 -- seen as a full type.
462
463 procedure Decimal_Fixed_Point_Type_Declaration
464 (T : Entity_Id;
465 Def : Node_Id);
466 -- Create a new decimal fixed point type, and apply the constraint to
467 -- obtain a subtype of this new type.
468
469 procedure Complete_Private_Subtype
470 (Priv : Entity_Id;
471 Full : Entity_Id;
472 Full_Base : Entity_Id;
473 Related_Nod : Node_Id);
474 -- Complete the implicit full view of a private subtype by setting the
475 -- appropriate semantic fields. If the full view of the parent is a record
476 -- type, build constrained components of subtype.
477
478 procedure Derive_Progenitor_Subprograms
479 (Parent_Type : Entity_Id;
480 Tagged_Type : Entity_Id);
481 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
482 -- operations of progenitors of Tagged_Type, and replace the subsidiary
483 -- subtypes with Tagged_Type, to build the specs of the inherited interface
484 -- primitives. The derived primitives are aliased to those of the
485 -- interface. This routine takes care also of transferring to the full view
486 -- subprograms associated with the partial view of Tagged_Type that cover
487 -- interface primitives.
488
489 procedure Derived_Standard_Character
490 (N : Node_Id;
491 Parent_Type : Entity_Id;
492 Derived_Type : Entity_Id);
493 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
494 -- derivations from types Standard.Character and Standard.Wide_Character.
495
496 procedure Derived_Type_Declaration
497 (T : Entity_Id;
498 N : Node_Id;
499 Is_Completion : Boolean);
500 -- Process a derived type declaration. Build_Derived_Type is invoked
501 -- to process the actual derived type definition. Parameters N and
502 -- Is_Completion have the same meaning as in Build_Derived_Type.
503 -- T is the N_Defining_Identifier for the entity defined in the
504 -- N_Full_Type_Declaration node N, that is T is the derived type.
505
506 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
507 -- Insert each literal in symbol table, as an overloadable identifier. Each
508 -- enumeration type is mapped into a sequence of integers, and each literal
509 -- is defined as a constant with integer value. If any of the literals are
510 -- character literals, the type is a character type, which means that
511 -- strings are legal aggregates for arrays of components of the type.
512
513 function Expand_To_Stored_Constraint
514 (Typ : Entity_Id;
515 Constraint : Elist_Id) return Elist_Id;
516 -- Given a constraint (i.e. a list of expressions) on the discriminants of
517 -- Typ, expand it into a constraint on the stored discriminants and return
518 -- the new list of expressions constraining the stored discriminants.
519
520 function Find_Type_Of_Object
521 (Obj_Def : Node_Id;
522 Related_Nod : Node_Id) return Entity_Id;
523 -- Get type entity for object referenced by Obj_Def, attaching the implicit
524 -- types generated to Related_Nod.
525
526 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
527 -- Create a new float and apply the constraint to obtain subtype of it
528
529 function Has_Range_Constraint (N : Node_Id) return Boolean;
530 -- Given an N_Subtype_Indication node N, return True if a range constraint
531 -- is present, either directly, or as part of a digits or delta constraint.
532 -- In addition, a digits constraint in the decimal case returns True, since
533 -- it establishes a default range if no explicit range is present.
534
535 function Inherit_Components
536 (N : Node_Id;
537 Parent_Base : Entity_Id;
538 Derived_Base : Entity_Id;
539 Is_Tagged : Boolean;
540 Inherit_Discr : Boolean;
541 Discs : Elist_Id) return Elist_Id;
542 -- Called from Build_Derived_Record_Type to inherit the components of
543 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
544 -- For more information on derived types and component inheritance please
545 -- consult the comment above the body of Build_Derived_Record_Type.
546 --
547 -- N is the original derived type declaration
548 --
549 -- Is_Tagged is set if we are dealing with tagged types
550 --
551 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
552 -- Parent_Base, otherwise no discriminants are inherited.
553 --
554 -- Discs gives the list of constraints that apply to Parent_Base in the
555 -- derived type declaration. If Discs is set to No_Elist, then we have
556 -- the following situation:
557 --
558 -- type Parent (D1..Dn : ..) is [tagged] record ...;
559 -- type Derived is new Parent [with ...];
560 --
561 -- which gets treated as
562 --
563 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
564 --
565 -- For untagged types the returned value is an association list. The list
566 -- starts from the association (Parent_Base => Derived_Base), and then it
567 -- contains a sequence of the associations of the form
568 --
569 -- (Old_Component => New_Component),
570 --
571 -- where Old_Component is the Entity_Id of a component in Parent_Base and
572 -- New_Component is the Entity_Id of the corresponding component in
573 -- Derived_Base. For untagged records, this association list is needed when
574 -- copying the record declaration for the derived base. In the tagged case
575 -- the value returned is irrelevant.
576
577 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
578 -- Propagate static and dynamic predicate flags from a parent to the
579 -- subtype in a subtype declaration with and without constraints.
580
581 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
582 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
583 -- Determine whether subprogram Subp is a procedure subject to pragma
584 -- Extensions_Visible with value False and has at least one controlling
585 -- parameter of mode OUT.
586
587 function Is_Valid_Constraint_Kind
588 (T_Kind : Type_Kind;
589 Constraint_Kind : Node_Kind) return Boolean;
590 -- Returns True if it is legal to apply the given kind of constraint to the
591 -- given kind of type (index constraint to an array type, for example).
592
593 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
594 -- Create new modular type. Verify that modulus is in bounds
595
596 procedure New_Concatenation_Op (Typ : Entity_Id);
597 -- Create an abbreviated declaration for an operator in order to
598 -- materialize concatenation on array types.
599
600 procedure Ordinary_Fixed_Point_Type_Declaration
601 (T : Entity_Id;
602 Def : Node_Id);
603 -- Create a new ordinary fixed point type, and apply the constraint to
604 -- obtain subtype of it.
605
606 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
607 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
608 -- In_Default_Expr can be properly adjusted.
609
610 procedure Prepare_Private_Subtype_Completion
611 (Id : Entity_Id;
612 Related_Nod : Node_Id);
613 -- Id is a subtype of some private type. Creates the full declaration
614 -- associated with Id whenever possible, i.e. when the full declaration
615 -- of the base type is already known. Records each subtype into
616 -- Private_Dependents of the base type.
617
618 procedure Process_Incomplete_Dependents
619 (N : Node_Id;
620 Full_T : Entity_Id;
621 Inc_T : Entity_Id);
622 -- Process all entities that depend on an incomplete type. There include
623 -- subtypes, subprogram types that mention the incomplete type in their
624 -- profiles, and subprogram with access parameters that designate the
625 -- incomplete type.
626
627 -- Inc_T is the defining identifier of an incomplete type declaration, its
628 -- Ekind is E_Incomplete_Type.
629 --
630 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
631 --
632 -- Full_T is N's defining identifier.
633 --
634 -- Subtypes of incomplete types with discriminants are completed when the
635 -- parent type is. This is simpler than private subtypes, because they can
636 -- only appear in the same scope, and there is no need to exchange views.
637 -- Similarly, access_to_subprogram types may have a parameter or a return
638 -- type that is an incomplete type, and that must be replaced with the
639 -- full type.
640 --
641 -- If the full type is tagged, subprogram with access parameters that
642 -- designated the incomplete may be primitive operations of the full type,
643 -- and have to be processed accordingly.
644
645 procedure Process_Real_Range_Specification (Def : Node_Id);
646 -- Given the type definition for a real type, this procedure processes and
647 -- checks the real range specification of this type definition if one is
648 -- present. If errors are found, error messages are posted, and the
649 -- Real_Range_Specification of Def is reset to Empty.
650
651 procedure Record_Type_Declaration
652 (T : Entity_Id;
653 N : Node_Id;
654 Prev : Entity_Id);
655 -- Process a record type declaration (for both untagged and tagged
656 -- records). Parameters T and N are exactly like in procedure
657 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
658 -- for this routine. If this is the completion of an incomplete type
659 -- declaration, Prev is the entity of the incomplete declaration, used for
660 -- cross-referencing. Otherwise Prev = T.
661
662 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
663 -- This routine is used to process the actual record type definition (both
664 -- for untagged and tagged records). Def is a record type definition node.
665 -- This procedure analyzes the components in this record type definition.
666 -- Prev_T is the entity for the enclosing record type. It is provided so
667 -- that its Has_Task flag can be set if any of the component have Has_Task
668 -- set. If the declaration is the completion of an incomplete type
669 -- declaration, Prev_T is the original incomplete type, whose full view is
670 -- the record type.
671
672 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
673 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
674 -- build a copy of the declaration tree of the parent, and we create
675 -- independently the list of components for the derived type. Semantic
676 -- information uses the component entities, but record representation
677 -- clauses are validated on the declaration tree. This procedure replaces
678 -- discriminants and components in the declaration with those that have
679 -- been created by Inherit_Components.
680
681 procedure Set_Fixed_Range
682 (E : Entity_Id;
683 Loc : Source_Ptr;
684 Lo : Ureal;
685 Hi : Ureal);
686 -- Build a range node with the given bounds and set it as the Scalar_Range
687 -- of the given fixed-point type entity. Loc is the source location used
688 -- for the constructed range. See body for further details.
689
690 procedure Set_Scalar_Range_For_Subtype
691 (Def_Id : Entity_Id;
692 R : Node_Id;
693 Subt : Entity_Id);
694 -- This routine is used to set the scalar range field for a subtype given
695 -- Def_Id, the entity for the subtype, and R, the range expression for the
696 -- scalar range. Subt provides the parent subtype to be used to analyze,
697 -- resolve, and check the given range.
698
699 procedure Set_Default_SSO (T : Entity_Id);
700 -- T is the entity for an array or record being declared. This procedure
701 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
702 -- to the setting of Opt.Default_SSO.
703
704 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
705 -- Create a new signed integer entity, and apply the constraint to obtain
706 -- the required first named subtype of this type.
707
708 procedure Set_Stored_Constraint_From_Discriminant_Constraint
709 (E : Entity_Id);
710 -- E is some record type. This routine computes E's Stored_Constraint
711 -- from its Discriminant_Constraint.
712
713 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
714 -- Check that an entity in a list of progenitors is an interface,
715 -- emit error otherwise.
716
717 -----------------------
718 -- Access_Definition --
719 -----------------------
720
721 function Access_Definition
722 (Related_Nod : Node_Id;
723 N : Node_Id) return Entity_Id
724 is
725 Anon_Type : Entity_Id;
726 Anon_Scope : Entity_Id;
727 Desig_Type : Entity_Id;
728 Enclosing_Prot_Type : Entity_Id := Empty;
729
730 begin
731 Check_SPARK_05_Restriction ("access type is not allowed", N);
732
733 if Is_Entry (Current_Scope)
734 and then Is_Task_Type (Etype (Scope (Current_Scope)))
735 then
736 Error_Msg_N ("task entries cannot have access parameters", N);
737 return Empty;
738 end if;
739
740 -- Ada 2005: For an object declaration the corresponding anonymous
741 -- type is declared in the current scope.
742
743 -- If the access definition is the return type of another access to
744 -- function, scope is the current one, because it is the one of the
745 -- current type declaration, except for the pathological case below.
746
747 if Nkind_In (Related_Nod, N_Object_Declaration,
748 N_Access_Function_Definition)
749 then
750 Anon_Scope := Current_Scope;
751
752 -- A pathological case: function returning access functions that
753 -- return access functions, etc. Each anonymous access type created
754 -- is in the enclosing scope of the outermost function.
755
756 declare
757 Par : Node_Id;
758
759 begin
760 Par := Related_Nod;
761 while Nkind_In (Par, N_Access_Function_Definition,
762 N_Access_Definition)
763 loop
764 Par := Parent (Par);
765 end loop;
766
767 if Nkind (Par) = N_Function_Specification then
768 Anon_Scope := Scope (Defining_Entity (Par));
769 end if;
770 end;
771
772 -- For the anonymous function result case, retrieve the scope of the
773 -- function specification's associated entity rather than using the
774 -- current scope. The current scope will be the function itself if the
775 -- formal part is currently being analyzed, but will be the parent scope
776 -- in the case of a parameterless function, and we always want to use
777 -- the function's parent scope. Finally, if the function is a child
778 -- unit, we must traverse the tree to retrieve the proper entity.
779
780 elsif Nkind (Related_Nod) = N_Function_Specification
781 and then Nkind (Parent (N)) /= N_Parameter_Specification
782 then
783 -- If the current scope is a protected type, the anonymous access
784 -- is associated with one of the protected operations, and must
785 -- be available in the scope that encloses the protected declaration.
786 -- Otherwise the type is in the scope enclosing the subprogram.
787
788 -- If the function has formals, The return type of a subprogram
789 -- declaration is analyzed in the scope of the subprogram (see
790 -- Process_Formals) and thus the protected type, if present, is
791 -- the scope of the current function scope.
792
793 if Ekind (Current_Scope) = E_Protected_Type then
794 Enclosing_Prot_Type := Current_Scope;
795
796 elsif Ekind (Current_Scope) = E_Function
797 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
798 then
799 Enclosing_Prot_Type := Scope (Current_Scope);
800 end if;
801
802 if Present (Enclosing_Prot_Type) then
803 Anon_Scope := Scope (Enclosing_Prot_Type);
804
805 else
806 Anon_Scope := Scope (Defining_Entity (Related_Nod));
807 end if;
808
809 -- For an access type definition, if the current scope is a child
810 -- unit it is the scope of the type.
811
812 elsif Is_Compilation_Unit (Current_Scope) then
813 Anon_Scope := Current_Scope;
814
815 -- For access formals, access components, and access discriminants, the
816 -- scope is that of the enclosing declaration,
817
818 else
819 Anon_Scope := Scope (Current_Scope);
820 end if;
821
822 Anon_Type :=
823 Create_Itype
824 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
825
826 if All_Present (N)
827 and then Ada_Version >= Ada_2005
828 then
829 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
830 end if;
831
832 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
833 -- the corresponding semantic routine
834
835 if Present (Access_To_Subprogram_Definition (N)) then
836
837 -- Compiler runtime units are compiled in Ada 2005 mode when building
838 -- the runtime library but must also be compilable in Ada 95 mode
839 -- (when bootstrapping the compiler).
840
841 Check_Compiler_Unit ("anonymous access to subprogram", N);
842
843 Access_Subprogram_Declaration
844 (T_Name => Anon_Type,
845 T_Def => Access_To_Subprogram_Definition (N));
846
847 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
848 Set_Ekind
849 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
850 else
851 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
852 end if;
853
854 Set_Can_Use_Internal_Rep
855 (Anon_Type, not Always_Compatible_Rep_On_Target);
856
857 -- If the anonymous access is associated with a protected operation,
858 -- create a reference to it after the enclosing protected definition
859 -- because the itype will be used in the subsequent bodies.
860
861 -- If the anonymous access itself is protected, a full type
862 -- declaratiton will be created for it, so that the equivalent
863 -- record type can be constructed. For further details, see
864 -- Replace_Anonymous_Access_To_Protected-Subprogram.
865
866 if Ekind (Current_Scope) = E_Protected_Type
867 and then not Protected_Present (Access_To_Subprogram_Definition (N))
868 then
869 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
870 end if;
871
872 return Anon_Type;
873 end if;
874
875 Find_Type (Subtype_Mark (N));
876 Desig_Type := Entity (Subtype_Mark (N));
877
878 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
879 Set_Etype (Anon_Type, Anon_Type);
880
881 -- Make sure the anonymous access type has size and alignment fields
882 -- set, as required by gigi. This is necessary in the case of the
883 -- Task_Body_Procedure.
884
885 if not Has_Private_Component (Desig_Type) then
886 Layout_Type (Anon_Type);
887 end if;
888
889 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
890 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
891 -- the null value is allowed. In Ada 95 the null value is never allowed.
892
893 if Ada_Version >= Ada_2005 then
894 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
895 else
896 Set_Can_Never_Be_Null (Anon_Type, True);
897 end if;
898
899 -- The anonymous access type is as public as the discriminated type or
900 -- subprogram that defines it. It is imported (for back-end purposes)
901 -- if the designated type is.
902
903 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
904
905 -- Ada 2005 (AI-231): Propagate the access-constant attribute
906
907 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
908
909 -- The context is either a subprogram declaration, object declaration,
910 -- or an access discriminant, in a private or a full type declaration.
911 -- In the case of a subprogram, if the designated type is incomplete,
912 -- the operation will be a primitive operation of the full type, to be
913 -- updated subsequently. If the type is imported through a limited_with
914 -- clause, the subprogram is not a primitive operation of the type
915 -- (which is declared elsewhere in some other scope).
916
917 if Ekind (Desig_Type) = E_Incomplete_Type
918 and then not From_Limited_With (Desig_Type)
919 and then Is_Overloadable (Current_Scope)
920 then
921 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
922 Set_Has_Delayed_Freeze (Current_Scope);
923 end if;
924
925 -- If the designated type is limited and class-wide, the object might
926 -- contain tasks, so we create a Master entity for the declaration. This
927 -- must be done before expansion of the full declaration, because the
928 -- declaration may include an expression that is an allocator, whose
929 -- expansion needs the proper Master for the created tasks.
930
931 if Expander_Active
932 and then Nkind (Related_Nod) = N_Object_Declaration
933 then
934 if Is_Limited_Record (Desig_Type)
935 and then Is_Class_Wide_Type (Desig_Type)
936 and then Tasking_Allowed
937 then
938 Build_Class_Wide_Master (Anon_Type);
939
940 -- Similarly, if the type is an anonymous access that designates
941 -- tasks, create a master entity for it in the current context.
942
943 elsif Has_Task (Desig_Type)
944 and then Comes_From_Source (Related_Nod)
945 then
946 Build_Master_Entity (Defining_Identifier (Related_Nod));
947 Build_Master_Renaming (Anon_Type);
948 end if;
949 end if;
950
951 -- For a private component of a protected type, it is imperative that
952 -- the back-end elaborate the type immediately after the protected
953 -- declaration, because this type will be used in the declarations
954 -- created for the component within each protected body, so we must
955 -- create an itype reference for it now.
956
957 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
958 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
959
960 -- Similarly, if the access definition is the return result of a
961 -- function, create an itype reference for it because it will be used
962 -- within the function body. For a regular function that is not a
963 -- compilation unit, insert reference after the declaration. For a
964 -- protected operation, insert it after the enclosing protected type
965 -- declaration. In either case, do not create a reference for a type
966 -- obtained through a limited_with clause, because this would introduce
967 -- semantic dependencies.
968
969 -- Similarly, do not create a reference if the designated type is a
970 -- generic formal, because no use of it will reach the backend.
971
972 elsif Nkind (Related_Nod) = N_Function_Specification
973 and then not From_Limited_With (Desig_Type)
974 and then not Is_Generic_Type (Desig_Type)
975 then
976 if Present (Enclosing_Prot_Type) then
977 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
978
979 elsif Is_List_Member (Parent (Related_Nod))
980 and then Nkind (Parent (N)) /= N_Parameter_Specification
981 then
982 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
983 end if;
984
985 -- Finally, create an itype reference for an object declaration of an
986 -- anonymous access type. This is strictly necessary only for deferred
987 -- constants, but in any case will avoid out-of-scope problems in the
988 -- back-end.
989
990 elsif Nkind (Related_Nod) = N_Object_Declaration then
991 Build_Itype_Reference (Anon_Type, Related_Nod);
992 end if;
993
994 return Anon_Type;
995 end Access_Definition;
996
997 -----------------------------------
998 -- Access_Subprogram_Declaration --
999 -----------------------------------
1000
1001 procedure Access_Subprogram_Declaration
1002 (T_Name : Entity_Id;
1003 T_Def : Node_Id)
1004 is
1005 procedure Check_For_Premature_Usage (Def : Node_Id);
1006 -- Check that type T_Name is not used, directly or recursively, as a
1007 -- parameter or a return type in Def. Def is either a subtype, an
1008 -- access_definition, or an access_to_subprogram_definition.
1009
1010 -------------------------------
1011 -- Check_For_Premature_Usage --
1012 -------------------------------
1013
1014 procedure Check_For_Premature_Usage (Def : Node_Id) is
1015 Param : Node_Id;
1016
1017 begin
1018 -- Check for a subtype mark
1019
1020 if Nkind (Def) in N_Has_Etype then
1021 if Etype (Def) = T_Name then
1022 Error_Msg_N
1023 ("type& cannot be used before end of its declaration", Def);
1024 end if;
1025
1026 -- If this is not a subtype, then this is an access_definition
1027
1028 elsif Nkind (Def) = N_Access_Definition then
1029 if Present (Access_To_Subprogram_Definition (Def)) then
1030 Check_For_Premature_Usage
1031 (Access_To_Subprogram_Definition (Def));
1032 else
1033 Check_For_Premature_Usage (Subtype_Mark (Def));
1034 end if;
1035
1036 -- The only cases left are N_Access_Function_Definition and
1037 -- N_Access_Procedure_Definition.
1038
1039 else
1040 if Present (Parameter_Specifications (Def)) then
1041 Param := First (Parameter_Specifications (Def));
1042 while Present (Param) loop
1043 Check_For_Premature_Usage (Parameter_Type (Param));
1044 Param := Next (Param);
1045 end loop;
1046 end if;
1047
1048 if Nkind (Def) = N_Access_Function_Definition then
1049 Check_For_Premature_Usage (Result_Definition (Def));
1050 end if;
1051 end if;
1052 end Check_For_Premature_Usage;
1053
1054 -- Local variables
1055
1056 Formals : constant List_Id := Parameter_Specifications (T_Def);
1057 Formal : Entity_Id;
1058 D_Ityp : Node_Id;
1059 Desig_Type : constant Entity_Id :=
1060 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1061
1062 -- Start of processing for Access_Subprogram_Declaration
1063
1064 begin
1065 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1066
1067 -- Associate the Itype node with the inner full-type declaration or
1068 -- subprogram spec or entry body. This is required to handle nested
1069 -- anonymous declarations. For example:
1070
1071 -- procedure P
1072 -- (X : access procedure
1073 -- (Y : access procedure
1074 -- (Z : access T)))
1075
1076 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1077 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1078 N_Private_Type_Declaration,
1079 N_Private_Extension_Declaration,
1080 N_Procedure_Specification,
1081 N_Function_Specification,
1082 N_Entry_Body)
1083
1084 or else
1085 Nkind_In (D_Ityp, N_Object_Declaration,
1086 N_Object_Renaming_Declaration,
1087 N_Formal_Object_Declaration,
1088 N_Formal_Type_Declaration,
1089 N_Task_Type_Declaration,
1090 N_Protected_Type_Declaration))
1091 loop
1092 D_Ityp := Parent (D_Ityp);
1093 pragma Assert (D_Ityp /= Empty);
1094 end loop;
1095
1096 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1097
1098 if Nkind_In (D_Ityp, N_Procedure_Specification,
1099 N_Function_Specification)
1100 then
1101 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1102
1103 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1104 N_Object_Declaration,
1105 N_Object_Renaming_Declaration,
1106 N_Formal_Type_Declaration)
1107 then
1108 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1109 end if;
1110
1111 if Nkind (T_Def) = N_Access_Function_Definition then
1112 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1113 declare
1114 Acc : constant Node_Id := Result_Definition (T_Def);
1115
1116 begin
1117 if Present (Access_To_Subprogram_Definition (Acc))
1118 and then
1119 Protected_Present (Access_To_Subprogram_Definition (Acc))
1120 then
1121 Set_Etype
1122 (Desig_Type,
1123 Replace_Anonymous_Access_To_Protected_Subprogram
1124 (T_Def));
1125
1126 else
1127 Set_Etype
1128 (Desig_Type,
1129 Access_Definition (T_Def, Result_Definition (T_Def)));
1130 end if;
1131 end;
1132
1133 else
1134 Analyze (Result_Definition (T_Def));
1135
1136 declare
1137 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1138
1139 begin
1140 -- If a null exclusion is imposed on the result type, then
1141 -- create a null-excluding itype (an access subtype) and use
1142 -- it as the function's Etype.
1143
1144 if Is_Access_Type (Typ)
1145 and then Null_Exclusion_In_Return_Present (T_Def)
1146 then
1147 Set_Etype (Desig_Type,
1148 Create_Null_Excluding_Itype
1149 (T => Typ,
1150 Related_Nod => T_Def,
1151 Scope_Id => Current_Scope));
1152
1153 else
1154 if From_Limited_With (Typ) then
1155
1156 -- AI05-151: Incomplete types are allowed in all basic
1157 -- declarations, including access to subprograms.
1158
1159 if Ada_Version >= Ada_2012 then
1160 null;
1161
1162 else
1163 Error_Msg_NE
1164 ("illegal use of incomplete type&",
1165 Result_Definition (T_Def), Typ);
1166 end if;
1167
1168 elsif Ekind (Current_Scope) = E_Package
1169 and then In_Private_Part (Current_Scope)
1170 then
1171 if Ekind (Typ) = E_Incomplete_Type then
1172 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1173
1174 elsif Is_Class_Wide_Type (Typ)
1175 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1176 then
1177 Append_Elmt
1178 (Desig_Type, Private_Dependents (Etype (Typ)));
1179 end if;
1180 end if;
1181
1182 Set_Etype (Desig_Type, Typ);
1183 end if;
1184 end;
1185 end if;
1186
1187 if not (Is_Type (Etype (Desig_Type))) then
1188 Error_Msg_N
1189 ("expect type in function specification",
1190 Result_Definition (T_Def));
1191 end if;
1192
1193 else
1194 Set_Etype (Desig_Type, Standard_Void_Type);
1195 end if;
1196
1197 if Present (Formals) then
1198 Push_Scope (Desig_Type);
1199
1200 -- Some special tests here. These special tests can be removed
1201 -- if and when Itypes always have proper parent pointers to their
1202 -- declarations???
1203
1204 -- Special test 1) Link defining_identifier of formals. Required by
1205 -- First_Formal to provide its functionality.
1206
1207 declare
1208 F : Node_Id;
1209
1210 begin
1211 F := First (Formals);
1212
1213 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1214 -- when it is part of an unconstrained type and subtype expansion
1215 -- is disabled. To avoid back-end problems with shared profiles,
1216 -- use previous subprogram type as the designated type, and then
1217 -- remove scope added above.
1218
1219 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1220 then
1221 Set_Etype (T_Name, T_Name);
1222 Init_Size_Align (T_Name);
1223 Set_Directly_Designated_Type (T_Name,
1224 Scope (Defining_Identifier (F)));
1225 End_Scope;
1226 return;
1227 end if;
1228
1229 while Present (F) loop
1230 if No (Parent (Defining_Identifier (F))) then
1231 Set_Parent (Defining_Identifier (F), F);
1232 end if;
1233
1234 Next (F);
1235 end loop;
1236 end;
1237
1238 Process_Formals (Formals, Parent (T_Def));
1239
1240 -- Special test 2) End_Scope requires that the parent pointer be set
1241 -- to something reasonable, but Itypes don't have parent pointers. So
1242 -- we set it and then unset it ???
1243
1244 Set_Parent (Desig_Type, T_Name);
1245 End_Scope;
1246 Set_Parent (Desig_Type, Empty);
1247 end if;
1248
1249 -- Check for premature usage of the type being defined
1250
1251 Check_For_Premature_Usage (T_Def);
1252
1253 -- The return type and/or any parameter type may be incomplete. Mark the
1254 -- subprogram_type as depending on the incomplete type, so that it can
1255 -- be updated when the full type declaration is seen. This only applies
1256 -- to incomplete types declared in some enclosing scope, not to limited
1257 -- views from other packages.
1258
1259 -- Prior to Ada 2012, access to functions can only have in_parameters.
1260
1261 if Present (Formals) then
1262 Formal := First_Formal (Desig_Type);
1263 while Present (Formal) loop
1264 if Ekind (Formal) /= E_In_Parameter
1265 and then Nkind (T_Def) = N_Access_Function_Definition
1266 and then Ada_Version < Ada_2012
1267 then
1268 Error_Msg_N ("functions can only have IN parameters", Formal);
1269 end if;
1270
1271 if Ekind (Etype (Formal)) = E_Incomplete_Type
1272 and then In_Open_Scopes (Scope (Etype (Formal)))
1273 then
1274 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1275 Set_Has_Delayed_Freeze (Desig_Type);
1276 end if;
1277
1278 Next_Formal (Formal);
1279 end loop;
1280 end if;
1281
1282 -- Check whether an indirect call without actuals may be possible. This
1283 -- is used when resolving calls whose result is then indexed.
1284
1285 May_Need_Actuals (Desig_Type);
1286
1287 -- If the return type is incomplete, this is legal as long as the type
1288 -- is declared in the current scope and will be completed in it (rather
1289 -- than being part of limited view).
1290
1291 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1292 and then not Has_Delayed_Freeze (Desig_Type)
1293 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1294 then
1295 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1296 Set_Has_Delayed_Freeze (Desig_Type);
1297 end if;
1298
1299 Check_Delayed_Subprogram (Desig_Type);
1300
1301 if Protected_Present (T_Def) then
1302 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1303 Set_Convention (Desig_Type, Convention_Protected);
1304 else
1305 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1306 end if;
1307
1308 Set_Can_Use_Internal_Rep (T_Name,
1309 not Always_Compatible_Rep_On_Target);
1310 Set_Etype (T_Name, T_Name);
1311 Init_Size_Align (T_Name);
1312 Set_Directly_Designated_Type (T_Name, Desig_Type);
1313
1314 -- If the access_to_subprogram is not declared at the library level,
1315 -- it can only point to subprograms that are at the same or deeper
1316 -- accessibility level. The corresponding subprogram type might
1317 -- require an activation record when compiling for C.
1318
1319 Set_Needs_Activation_Record (Desig_Type,
1320 not Is_Library_Level_Entity (T_Name));
1321
1322 Generate_Reference_To_Formals (T_Name);
1323
1324 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1325
1326 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1327
1328 Check_Restriction (No_Access_Subprograms, T_Def);
1329 end Access_Subprogram_Declaration;
1330
1331 ----------------------------
1332 -- Access_Type_Declaration --
1333 ----------------------------
1334
1335 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1336 P : constant Node_Id := Parent (Def);
1337 S : constant Node_Id := Subtype_Indication (Def);
1338
1339 Full_Desig : Entity_Id;
1340
1341 begin
1342 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1343
1344 -- Check for permissible use of incomplete type
1345
1346 if Nkind (S) /= N_Subtype_Indication then
1347 Analyze (S);
1348
1349 if Present (Entity (S))
1350 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1351 then
1352 Set_Directly_Designated_Type (T, Entity (S));
1353
1354 -- If the designated type is a limited view, we cannot tell if
1355 -- the full view contains tasks, and there is no way to handle
1356 -- that full view in a client. We create a master entity for the
1357 -- scope, which will be used when a client determines that one
1358 -- is needed.
1359
1360 if From_Limited_With (Entity (S))
1361 and then not Is_Class_Wide_Type (Entity (S))
1362 then
1363 Set_Ekind (T, E_Access_Type);
1364 Build_Master_Entity (T);
1365 Build_Master_Renaming (T);
1366 end if;
1367
1368 else
1369 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1370 end if;
1371
1372 -- If the access definition is of the form: ACCESS NOT NULL ..
1373 -- the subtype indication must be of an access type. Create
1374 -- a null-excluding subtype of it.
1375
1376 if Null_Excluding_Subtype (Def) then
1377 if not Is_Access_Type (Entity (S)) then
1378 Error_Msg_N ("null exclusion must apply to access type", Def);
1379
1380 else
1381 declare
1382 Loc : constant Source_Ptr := Sloc (S);
1383 Decl : Node_Id;
1384 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1385
1386 begin
1387 Decl :=
1388 Make_Subtype_Declaration (Loc,
1389 Defining_Identifier => Nam,
1390 Subtype_Indication =>
1391 New_Occurrence_Of (Entity (S), Loc));
1392 Set_Null_Exclusion_Present (Decl);
1393 Insert_Before (Parent (Def), Decl);
1394 Analyze (Decl);
1395 Set_Entity (S, Nam);
1396 end;
1397 end if;
1398 end if;
1399
1400 else
1401 Set_Directly_Designated_Type (T,
1402 Process_Subtype (S, P, T, 'P'));
1403 end if;
1404
1405 if All_Present (Def) or Constant_Present (Def) then
1406 Set_Ekind (T, E_General_Access_Type);
1407 else
1408 Set_Ekind (T, E_Access_Type);
1409 end if;
1410
1411 Full_Desig := Designated_Type (T);
1412
1413 if Base_Type (Full_Desig) = T then
1414 Error_Msg_N ("access type cannot designate itself", S);
1415
1416 -- In Ada 2005, the type may have a limited view through some unit in
1417 -- its own context, allowing the following circularity that cannot be
1418 -- detected earlier.
1419
1420 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1421 then
1422 Error_Msg_N
1423 ("access type cannot designate its own class-wide type", S);
1424
1425 -- Clean up indication of tagged status to prevent cascaded errors
1426
1427 Set_Is_Tagged_Type (T, False);
1428 end if;
1429
1430 Set_Etype (T, T);
1431
1432 -- If the type has appeared already in a with_type clause, it is frozen
1433 -- and the pointer size is already set. Else, initialize.
1434
1435 if not From_Limited_With (T) then
1436 Init_Size_Align (T);
1437 end if;
1438
1439 -- Note that Has_Task is always false, since the access type itself
1440 -- is not a task type. See Einfo for more description on this point.
1441 -- Exactly the same consideration applies to Has_Controlled_Component
1442 -- and to Has_Protected.
1443
1444 Set_Has_Task (T, False);
1445 Set_Has_Protected (T, False);
1446 Set_Has_Timing_Event (T, False);
1447 Set_Has_Controlled_Component (T, False);
1448
1449 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1450 -- problems where an incomplete view of this entity has been previously
1451 -- established by a limited with and an overlaid version of this field
1452 -- (Stored_Constraint) was initialized for the incomplete view.
1453
1454 -- This reset is performed in most cases except where the access type
1455 -- has been created for the purposes of allocating or deallocating a
1456 -- build-in-place object. Such access types have explicitly set pools
1457 -- and finalization masters.
1458
1459 if No (Associated_Storage_Pool (T)) then
1460 Set_Finalization_Master (T, Empty);
1461 end if;
1462
1463 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1464 -- attributes
1465
1466 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1467 Set_Is_Access_Constant (T, Constant_Present (Def));
1468 end Access_Type_Declaration;
1469
1470 ----------------------------------
1471 -- Add_Interface_Tag_Components --
1472 ----------------------------------
1473
1474 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1475 Loc : constant Source_Ptr := Sloc (N);
1476 L : List_Id;
1477 Last_Tag : Node_Id;
1478
1479 procedure Add_Tag (Iface : Entity_Id);
1480 -- Add tag for one of the progenitor interfaces
1481
1482 -------------
1483 -- Add_Tag --
1484 -------------
1485
1486 procedure Add_Tag (Iface : Entity_Id) is
1487 Decl : Node_Id;
1488 Def : Node_Id;
1489 Tag : Entity_Id;
1490 Offset : Entity_Id;
1491
1492 begin
1493 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1494
1495 -- This is a reasonable place to propagate predicates
1496
1497 if Has_Predicates (Iface) then
1498 Set_Has_Predicates (Typ);
1499 end if;
1500
1501 Def :=
1502 Make_Component_Definition (Loc,
1503 Aliased_Present => True,
1504 Subtype_Indication =>
1505 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1506
1507 Tag := Make_Temporary (Loc, 'V');
1508
1509 Decl :=
1510 Make_Component_Declaration (Loc,
1511 Defining_Identifier => Tag,
1512 Component_Definition => Def);
1513
1514 Analyze_Component_Declaration (Decl);
1515
1516 Set_Analyzed (Decl);
1517 Set_Ekind (Tag, E_Component);
1518 Set_Is_Tag (Tag);
1519 Set_Is_Aliased (Tag);
1520 Set_Related_Type (Tag, Iface);
1521 Init_Component_Location (Tag);
1522
1523 pragma Assert (Is_Frozen (Iface));
1524
1525 Set_DT_Entry_Count (Tag,
1526 DT_Entry_Count (First_Entity (Iface)));
1527
1528 if No (Last_Tag) then
1529 Prepend (Decl, L);
1530 else
1531 Insert_After (Last_Tag, Decl);
1532 end if;
1533
1534 Last_Tag := Decl;
1535
1536 -- If the ancestor has discriminants we need to give special support
1537 -- to store the offset_to_top value of the secondary dispatch tables.
1538 -- For this purpose we add a supplementary component just after the
1539 -- field that contains the tag associated with each secondary DT.
1540
1541 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1542 Def :=
1543 Make_Component_Definition (Loc,
1544 Subtype_Indication =>
1545 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1546
1547 Offset := Make_Temporary (Loc, 'V');
1548
1549 Decl :=
1550 Make_Component_Declaration (Loc,
1551 Defining_Identifier => Offset,
1552 Component_Definition => Def);
1553
1554 Analyze_Component_Declaration (Decl);
1555
1556 Set_Analyzed (Decl);
1557 Set_Ekind (Offset, E_Component);
1558 Set_Is_Aliased (Offset);
1559 Set_Related_Type (Offset, Iface);
1560 Init_Component_Location (Offset);
1561 Insert_After (Last_Tag, Decl);
1562 Last_Tag := Decl;
1563 end if;
1564 end Add_Tag;
1565
1566 -- Local variables
1567
1568 Elmt : Elmt_Id;
1569 Ext : Node_Id;
1570 Comp : Node_Id;
1571
1572 -- Start of processing for Add_Interface_Tag_Components
1573
1574 begin
1575 if not RTE_Available (RE_Interface_Tag) then
1576 Error_Msg
1577 ("(Ada 2005) interface types not supported by this run-time!",
1578 Sloc (N));
1579 return;
1580 end if;
1581
1582 if Ekind (Typ) /= E_Record_Type
1583 or else (Is_Concurrent_Record_Type (Typ)
1584 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1585 or else (not Is_Concurrent_Record_Type (Typ)
1586 and then No (Interfaces (Typ))
1587 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1588 then
1589 return;
1590 end if;
1591
1592 -- Find the current last tag
1593
1594 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1595 Ext := Record_Extension_Part (Type_Definition (N));
1596 else
1597 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1598 Ext := Type_Definition (N);
1599 end if;
1600
1601 Last_Tag := Empty;
1602
1603 if not (Present (Component_List (Ext))) then
1604 Set_Null_Present (Ext, False);
1605 L := New_List;
1606 Set_Component_List (Ext,
1607 Make_Component_List (Loc,
1608 Component_Items => L,
1609 Null_Present => False));
1610 else
1611 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1612 L := Component_Items
1613 (Component_List
1614 (Record_Extension_Part
1615 (Type_Definition (N))));
1616 else
1617 L := Component_Items
1618 (Component_List
1619 (Type_Definition (N)));
1620 end if;
1621
1622 -- Find the last tag component
1623
1624 Comp := First (L);
1625 while Present (Comp) loop
1626 if Nkind (Comp) = N_Component_Declaration
1627 and then Is_Tag (Defining_Identifier (Comp))
1628 then
1629 Last_Tag := Comp;
1630 end if;
1631
1632 Next (Comp);
1633 end loop;
1634 end if;
1635
1636 -- At this point L references the list of components and Last_Tag
1637 -- references the current last tag (if any). Now we add the tag
1638 -- corresponding with all the interfaces that are not implemented
1639 -- by the parent.
1640
1641 if Present (Interfaces (Typ)) then
1642 Elmt := First_Elmt (Interfaces (Typ));
1643 while Present (Elmt) loop
1644 Add_Tag (Node (Elmt));
1645 Next_Elmt (Elmt);
1646 end loop;
1647 end if;
1648 end Add_Interface_Tag_Components;
1649
1650 -------------------------------------
1651 -- Add_Internal_Interface_Entities --
1652 -------------------------------------
1653
1654 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1655 Elmt : Elmt_Id;
1656 Iface : Entity_Id;
1657 Iface_Elmt : Elmt_Id;
1658 Iface_Prim : Entity_Id;
1659 Ifaces_List : Elist_Id;
1660 New_Subp : Entity_Id := Empty;
1661 Prim : Entity_Id;
1662 Restore_Scope : Boolean := False;
1663
1664 begin
1665 pragma Assert (Ada_Version >= Ada_2005
1666 and then Is_Record_Type (Tagged_Type)
1667 and then Is_Tagged_Type (Tagged_Type)
1668 and then Has_Interfaces (Tagged_Type)
1669 and then not Is_Interface (Tagged_Type));
1670
1671 -- Ensure that the internal entities are added to the scope of the type
1672
1673 if Scope (Tagged_Type) /= Current_Scope then
1674 Push_Scope (Scope (Tagged_Type));
1675 Restore_Scope := True;
1676 end if;
1677
1678 Collect_Interfaces (Tagged_Type, Ifaces_List);
1679
1680 Iface_Elmt := First_Elmt (Ifaces_List);
1681 while Present (Iface_Elmt) loop
1682 Iface := Node (Iface_Elmt);
1683
1684 -- Originally we excluded here from this processing interfaces that
1685 -- are parents of Tagged_Type because their primitives are located
1686 -- in the primary dispatch table (and hence no auxiliary internal
1687 -- entities are required to handle secondary dispatch tables in such
1688 -- case). However, these auxiliary entities are also required to
1689 -- handle derivations of interfaces in formals of generics (see
1690 -- Derive_Subprograms).
1691
1692 Elmt := First_Elmt (Primitive_Operations (Iface));
1693 while Present (Elmt) loop
1694 Iface_Prim := Node (Elmt);
1695
1696 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1697 Prim :=
1698 Find_Primitive_Covering_Interface
1699 (Tagged_Type => Tagged_Type,
1700 Iface_Prim => Iface_Prim);
1701
1702 if No (Prim) and then Serious_Errors_Detected > 0 then
1703 goto Continue;
1704 end if;
1705
1706 pragma Assert (Present (Prim));
1707
1708 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1709 -- differs from the name of the interface primitive then it is
1710 -- a private primitive inherited from a parent type. In such
1711 -- case, given that Tagged_Type covers the interface, the
1712 -- inherited private primitive becomes visible. For such
1713 -- purpose we add a new entity that renames the inherited
1714 -- private primitive.
1715
1716 if Chars (Prim) /= Chars (Iface_Prim) then
1717 pragma Assert (Has_Suffix (Prim, 'P'));
1718 Derive_Subprogram
1719 (New_Subp => New_Subp,
1720 Parent_Subp => Iface_Prim,
1721 Derived_Type => Tagged_Type,
1722 Parent_Type => Iface);
1723 Set_Alias (New_Subp, Prim);
1724 Set_Is_Abstract_Subprogram
1725 (New_Subp, Is_Abstract_Subprogram (Prim));
1726 end if;
1727
1728 Derive_Subprogram
1729 (New_Subp => New_Subp,
1730 Parent_Subp => Iface_Prim,
1731 Derived_Type => Tagged_Type,
1732 Parent_Type => Iface);
1733
1734 declare
1735 Anc : Entity_Id;
1736 begin
1737 if Is_Inherited_Operation (Prim)
1738 and then Present (Alias (Prim))
1739 then
1740 Anc := Alias (Prim);
1741 else
1742 Anc := Overridden_Operation (Prim);
1743 end if;
1744
1745 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1746 -- nonconforming preconditions in both an ancestor and
1747 -- a progenitor operation.
1748
1749 -- If the operation is a primitive wrapper it is an explicit
1750 -- (overriding) operqtion and all is fine.
1751
1752 if Present (Anc)
1753 and then Has_Non_Trivial_Precondition (Anc)
1754 and then Has_Non_Trivial_Precondition (Iface_Prim)
1755 then
1756 if Is_Abstract_Subprogram (Prim)
1757 or else
1758 (Ekind (Prim) = E_Procedure
1759 and then Nkind (Parent (Prim)) =
1760 N_Procedure_Specification
1761 and then Null_Present (Parent (Prim)))
1762 or else Is_Primitive_Wrapper (Prim)
1763 then
1764 null;
1765
1766 -- The operation is inherited and must be overridden
1767
1768 elsif not Comes_From_Source (Prim) then
1769 Error_Msg_NE
1770 ("&inherits non-conforming preconditions and must "
1771 & "be overridden (RM 6.1.1 (10-16)",
1772 Parent (Tagged_Type), Prim);
1773 end if;
1774 end if;
1775 end;
1776
1777 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1778 -- associated with interface types. These entities are
1779 -- only registered in the list of primitives of its
1780 -- corresponding tagged type because they are only used
1781 -- to fill the contents of the secondary dispatch tables.
1782 -- Therefore they are removed from the homonym chains.
1783
1784 Set_Is_Hidden (New_Subp);
1785 Set_Is_Internal (New_Subp);
1786 Set_Alias (New_Subp, Prim);
1787 Set_Is_Abstract_Subprogram
1788 (New_Subp, Is_Abstract_Subprogram (Prim));
1789 Set_Interface_Alias (New_Subp, Iface_Prim);
1790
1791 -- If the returned type is an interface then propagate it to
1792 -- the returned type. Needed by the thunk to generate the code
1793 -- which displaces "this" to reference the corresponding
1794 -- secondary dispatch table in the returned object.
1795
1796 if Is_Interface (Etype (Iface_Prim)) then
1797 Set_Etype (New_Subp, Etype (Iface_Prim));
1798 end if;
1799
1800 -- Internal entities associated with interface types are only
1801 -- registered in the list of primitives of the tagged type.
1802 -- They are only used to fill the contents of the secondary
1803 -- dispatch tables. Therefore they are not needed in the
1804 -- homonym chains.
1805
1806 Remove_Homonym (New_Subp);
1807
1808 -- Hidden entities associated with interfaces must have set
1809 -- the Has_Delay_Freeze attribute to ensure that, in case
1810 -- of locally defined tagged types (or compiling with static
1811 -- dispatch tables generation disabled) the corresponding
1812 -- entry of the secondary dispatch table is filled when such
1813 -- an entity is frozen. This is an expansion activity that must
1814 -- be suppressed for ASIS because it leads to gigi elaboration
1815 -- issues in annotate mode.
1816
1817 if not ASIS_Mode then
1818 Set_Has_Delayed_Freeze (New_Subp);
1819 end if;
1820 end if;
1821
1822 <<Continue>>
1823 Next_Elmt (Elmt);
1824 end loop;
1825
1826 Next_Elmt (Iface_Elmt);
1827 end loop;
1828
1829 if Restore_Scope then
1830 Pop_Scope;
1831 end if;
1832 end Add_Internal_Interface_Entities;
1833
1834 -----------------------------------
1835 -- Analyze_Component_Declaration --
1836 -----------------------------------
1837
1838 procedure Analyze_Component_Declaration (N : Node_Id) is
1839 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1840 Id : constant Entity_Id := Defining_Identifier (N);
1841 E : constant Node_Id := Expression (N);
1842 Typ : constant Node_Id :=
1843 Subtype_Indication (Component_Definition (N));
1844 T : Entity_Id;
1845 P : Entity_Id;
1846
1847 function Contains_POC (Constr : Node_Id) return Boolean;
1848 -- Determines whether a constraint uses the discriminant of a record
1849 -- type thus becoming a per-object constraint (POC).
1850
1851 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1852 -- Typ is the type of the current component, check whether this type is
1853 -- a limited type. Used to validate declaration against that of
1854 -- enclosing record.
1855
1856 ------------------
1857 -- Contains_POC --
1858 ------------------
1859
1860 function Contains_POC (Constr : Node_Id) return Boolean is
1861 begin
1862 -- Prevent cascaded errors
1863
1864 if Error_Posted (Constr) then
1865 return False;
1866 end if;
1867
1868 case Nkind (Constr) is
1869 when N_Attribute_Reference =>
1870 return Attribute_Name (Constr) = Name_Access
1871 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1872
1873 when N_Discriminant_Association =>
1874 return Denotes_Discriminant (Expression (Constr));
1875
1876 when N_Identifier =>
1877 return Denotes_Discriminant (Constr);
1878
1879 when N_Index_Or_Discriminant_Constraint =>
1880 declare
1881 IDC : Node_Id;
1882
1883 begin
1884 IDC := First (Constraints (Constr));
1885 while Present (IDC) loop
1886
1887 -- One per-object constraint is sufficient
1888
1889 if Contains_POC (IDC) then
1890 return True;
1891 end if;
1892
1893 Next (IDC);
1894 end loop;
1895
1896 return False;
1897 end;
1898
1899 when N_Range =>
1900 return Denotes_Discriminant (Low_Bound (Constr))
1901 or else
1902 Denotes_Discriminant (High_Bound (Constr));
1903
1904 when N_Range_Constraint =>
1905 return Denotes_Discriminant (Range_Expression (Constr));
1906
1907 when others =>
1908 return False;
1909 end case;
1910 end Contains_POC;
1911
1912 ----------------------
1913 -- Is_Known_Limited --
1914 ----------------------
1915
1916 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1917 P : constant Entity_Id := Etype (Typ);
1918 R : constant Entity_Id := Root_Type (Typ);
1919
1920 begin
1921 if Is_Limited_Record (Typ) then
1922 return True;
1923
1924 -- If the root type is limited (and not a limited interface) so is
1925 -- the current type.
1926
1927 elsif Is_Limited_Record (R)
1928 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1929 then
1930 return True;
1931
1932 -- Else the type may have a limited interface progenitor, but a
1933 -- limited record parent that is not an interface.
1934
1935 elsif R /= P
1936 and then Is_Limited_Record (P)
1937 and then not Is_Interface (P)
1938 then
1939 return True;
1940
1941 else
1942 return False;
1943 end if;
1944 end Is_Known_Limited;
1945
1946 -- Start of processing for Analyze_Component_Declaration
1947
1948 begin
1949 Generate_Definition (Id);
1950 Enter_Name (Id);
1951
1952 if Present (Typ) then
1953 T := Find_Type_Of_Object
1954 (Subtype_Indication (Component_Definition (N)), N);
1955
1956 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1957 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1958 end if;
1959
1960 -- Ada 2005 (AI-230): Access Definition case
1961
1962 else
1963 pragma Assert (Present
1964 (Access_Definition (Component_Definition (N))));
1965
1966 T := Access_Definition
1967 (Related_Nod => N,
1968 N => Access_Definition (Component_Definition (N)));
1969 Set_Is_Local_Anonymous_Access (T);
1970
1971 -- Ada 2005 (AI-254)
1972
1973 if Present (Access_To_Subprogram_Definition
1974 (Access_Definition (Component_Definition (N))))
1975 and then Protected_Present (Access_To_Subprogram_Definition
1976 (Access_Definition
1977 (Component_Definition (N))))
1978 then
1979 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1980 end if;
1981 end if;
1982
1983 -- If the subtype is a constrained subtype of the enclosing record,
1984 -- (which must have a partial view) the back-end does not properly
1985 -- handle the recursion. Rewrite the component declaration with an
1986 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1987 -- the tree directly because side effects have already been removed from
1988 -- discriminant constraints.
1989
1990 if Ekind (T) = E_Access_Subtype
1991 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1992 and then Comes_From_Source (T)
1993 and then Nkind (Parent (T)) = N_Subtype_Declaration
1994 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1995 then
1996 Rewrite
1997 (Subtype_Indication (Component_Definition (N)),
1998 New_Copy_Tree (Subtype_Indication (Parent (T))));
1999 T := Find_Type_Of_Object
2000 (Subtype_Indication (Component_Definition (N)), N);
2001 end if;
2002
2003 -- If the component declaration includes a default expression, then we
2004 -- check that the component is not of a limited type (RM 3.7(5)),
2005 -- and do the special preanalysis of the expression (see section on
2006 -- "Handling of Default and Per-Object Expressions" in the spec of
2007 -- package Sem).
2008
2009 if Present (E) then
2010 Check_SPARK_05_Restriction ("default expression is not allowed", E);
2011 Preanalyze_Default_Expression (E, T);
2012 Check_Initialization (T, E);
2013
2014 if Ada_Version >= Ada_2005
2015 and then Ekind (T) = E_Anonymous_Access_Type
2016 and then Etype (E) /= Any_Type
2017 then
2018 -- Check RM 3.9.2(9): "if the expected type for an expression is
2019 -- an anonymous access-to-specific tagged type, then the object
2020 -- designated by the expression shall not be dynamically tagged
2021 -- unless it is a controlling operand in a call on a dispatching
2022 -- operation"
2023
2024 if Is_Tagged_Type (Directly_Designated_Type (T))
2025 and then
2026 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2027 and then
2028 Ekind (Directly_Designated_Type (Etype (E))) =
2029 E_Class_Wide_Type
2030 then
2031 Error_Msg_N
2032 ("access to specific tagged type required (RM 3.9.2(9))", E);
2033 end if;
2034
2035 -- (Ada 2005: AI-230): Accessibility check for anonymous
2036 -- components
2037
2038 if Type_Access_Level (Etype (E)) >
2039 Deepest_Type_Access_Level (T)
2040 then
2041 Error_Msg_N
2042 ("expression has deeper access level than component " &
2043 "(RM 3.10.2 (12.2))", E);
2044 end if;
2045
2046 -- The initialization expression is a reference to an access
2047 -- discriminant. The type of the discriminant is always deeper
2048 -- than any access type.
2049
2050 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2051 and then Is_Entity_Name (E)
2052 and then Ekind (Entity (E)) = E_In_Parameter
2053 and then Present (Discriminal_Link (Entity (E)))
2054 then
2055 Error_Msg_N
2056 ("discriminant has deeper accessibility level than target",
2057 E);
2058 end if;
2059 end if;
2060 end if;
2061
2062 -- The parent type may be a private view with unknown discriminants,
2063 -- and thus unconstrained. Regular components must be constrained.
2064
2065 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2066 if Is_Class_Wide_Type (T) then
2067 Error_Msg_N
2068 ("class-wide subtype with unknown discriminants" &
2069 " in component declaration",
2070 Subtype_Indication (Component_Definition (N)));
2071 else
2072 Error_Msg_N
2073 ("unconstrained subtype in component declaration",
2074 Subtype_Indication (Component_Definition (N)));
2075 end if;
2076
2077 -- Components cannot be abstract, except for the special case of
2078 -- the _Parent field (case of extending an abstract tagged type)
2079
2080 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2081 Error_Msg_N ("type of a component cannot be abstract", N);
2082 end if;
2083
2084 Set_Etype (Id, T);
2085 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2086
2087 -- The component declaration may have a per-object constraint, set
2088 -- the appropriate flag in the defining identifier of the subtype.
2089
2090 if Present (Subtype_Indication (Component_Definition (N))) then
2091 declare
2092 Sindic : constant Node_Id :=
2093 Subtype_Indication (Component_Definition (N));
2094 begin
2095 if Nkind (Sindic) = N_Subtype_Indication
2096 and then Present (Constraint (Sindic))
2097 and then Contains_POC (Constraint (Sindic))
2098 then
2099 Set_Has_Per_Object_Constraint (Id);
2100 end if;
2101 end;
2102 end if;
2103
2104 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2105 -- out some static checks.
2106
2107 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2108 Null_Exclusion_Static_Checks (N);
2109 end if;
2110
2111 -- If this component is private (or depends on a private type), flag the
2112 -- record type to indicate that some operations are not available.
2113
2114 P := Private_Component (T);
2115
2116 if Present (P) then
2117
2118 -- Check for circular definitions
2119
2120 if P = Any_Type then
2121 Set_Etype (Id, Any_Type);
2122
2123 -- There is a gap in the visibility of operations only if the
2124 -- component type is not defined in the scope of the record type.
2125
2126 elsif Scope (P) = Scope (Current_Scope) then
2127 null;
2128
2129 elsif Is_Limited_Type (P) then
2130 Set_Is_Limited_Composite (Current_Scope);
2131
2132 else
2133 Set_Is_Private_Composite (Current_Scope);
2134 end if;
2135 end if;
2136
2137 if P /= Any_Type
2138 and then Is_Limited_Type (T)
2139 and then Chars (Id) /= Name_uParent
2140 and then Is_Tagged_Type (Current_Scope)
2141 then
2142 if Is_Derived_Type (Current_Scope)
2143 and then not Is_Known_Limited (Current_Scope)
2144 then
2145 Error_Msg_N
2146 ("extension of nonlimited type cannot have limited components",
2147 N);
2148
2149 if Is_Interface (Root_Type (Current_Scope)) then
2150 Error_Msg_N
2151 ("\limitedness is not inherited from limited interface", N);
2152 Error_Msg_N ("\add LIMITED to type indication", N);
2153 end if;
2154
2155 Explain_Limited_Type (T, N);
2156 Set_Etype (Id, Any_Type);
2157 Set_Is_Limited_Composite (Current_Scope, False);
2158
2159 elsif not Is_Derived_Type (Current_Scope)
2160 and then not Is_Limited_Record (Current_Scope)
2161 and then not Is_Concurrent_Type (Current_Scope)
2162 then
2163 Error_Msg_N
2164 ("nonlimited tagged type cannot have limited components", N);
2165 Explain_Limited_Type (T, N);
2166 Set_Etype (Id, Any_Type);
2167 Set_Is_Limited_Composite (Current_Scope, False);
2168 end if;
2169 end if;
2170
2171 -- If the component is an unconstrained task or protected type with
2172 -- discriminants, the component and the enclosing record are limited
2173 -- and the component is constrained by its default values. Compute
2174 -- its actual subtype, else it may be allocated the maximum size by
2175 -- the backend, and possibly overflow.
2176
2177 if Is_Concurrent_Type (T)
2178 and then not Is_Constrained (T)
2179 and then Has_Discriminants (T)
2180 and then not Has_Discriminants (Current_Scope)
2181 then
2182 declare
2183 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2184
2185 begin
2186 Set_Etype (Id, Act_T);
2187
2188 -- Rewrite component definition to use the constrained subtype
2189
2190 Rewrite (Component_Definition (N),
2191 Make_Component_Definition (Loc,
2192 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2193 end;
2194 end if;
2195
2196 Set_Original_Record_Component (Id, Id);
2197
2198 if Has_Aspects (N) then
2199 Analyze_Aspect_Specifications (N, Id);
2200 end if;
2201
2202 Analyze_Dimension (N);
2203 end Analyze_Component_Declaration;
2204
2205 --------------------------
2206 -- Analyze_Declarations --
2207 --------------------------
2208
2209 procedure Analyze_Declarations (L : List_Id) is
2210 Decl : Node_Id;
2211
2212 procedure Adjust_Decl;
2213 -- Adjust Decl not to include implicit label declarations, since these
2214 -- have strange Sloc values that result in elaboration check problems.
2215 -- (They have the sloc of the label as found in the source, and that
2216 -- is ahead of the current declarative part).
2217
2218 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2219 -- Create the subprogram bodies which verify the run-time semantics of
2220 -- the pragmas listed below for each elibigle type found in declarative
2221 -- list Decls. The pragmas are:
2222 --
2223 -- Default_Initial_Condition
2224 -- Invariant
2225 -- Type_Invariant
2226 --
2227 -- Context denotes the owner of the declarative list.
2228
2229 procedure Check_Entry_Contracts;
2230 -- Perform a preanalysis of the pre- and postconditions of an entry
2231 -- declaration. This must be done before full resolution and creation
2232 -- of the parameter block, etc. to catch illegal uses within the
2233 -- contract expression. Full analysis of the expression is done when
2234 -- the contract is processed.
2235
2236 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2237 -- Check if a nested package has entities within it that rely on library
2238 -- level private types where the full view has not been completed for
2239 -- the purposes of checking if it is acceptable to freeze an expression
2240 -- function at the point of declaration.
2241
2242 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2243 -- Determine whether Body_Decl denotes the body of a late controlled
2244 -- primitive (either Initialize, Adjust or Finalize). If this is the
2245 -- case, add a proper spec if the body lacks one. The spec is inserted
2246 -- before Body_Decl and immediately analyzed.
2247
2248 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2249 -- Spec_Id is the entity of a package that may define abstract states,
2250 -- and in the case of a child unit, whose ancestors may define abstract
2251 -- states. If the states have partial visible refinement, remove the
2252 -- partial visibility of each constituent at the end of the package
2253 -- spec and body declarations.
2254
2255 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2256 -- Spec_Id is the entity of a package that may define abstract states.
2257 -- If the states have visible refinement, remove the visibility of each
2258 -- constituent at the end of the package body declaration.
2259
2260 procedure Resolve_Aspects;
2261 -- Utility to resolve the expressions of aspects at the end of a list of
2262 -- declarations, or before a declaration that freezes previous entities,
2263 -- such as in a subprogram body.
2264
2265 -----------------
2266 -- Adjust_Decl --
2267 -----------------
2268
2269 procedure Adjust_Decl is
2270 begin
2271 while Present (Prev (Decl))
2272 and then Nkind (Decl) = N_Implicit_Label_Declaration
2273 loop
2274 Prev (Decl);
2275 end loop;
2276 end Adjust_Decl;
2277
2278 ----------------------------
2279 -- Build_Assertion_Bodies --
2280 ----------------------------
2281
2282 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2283 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2284 -- Create the subprogram bodies which verify the run-time semantics
2285 -- of the pragmas listed below for type Typ. The pragmas are:
2286 --
2287 -- Default_Initial_Condition
2288 -- Invariant
2289 -- Type_Invariant
2290
2291 -------------------------------------
2292 -- Build_Assertion_Bodies_For_Type --
2293 -------------------------------------
2294
2295 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2296 begin
2297 -- Preanalyze and resolve the Default_Initial_Condition assertion
2298 -- expression at the end of the declarations to catch any errors.
2299
2300 if Has_DIC (Typ) then
2301 Build_DIC_Procedure_Body (Typ);
2302 end if;
2303
2304 if Nkind (Context) = N_Package_Specification then
2305
2306 -- Preanalyze and resolve the class-wide invariants of an
2307 -- interface at the end of whichever declarative part has the
2308 -- interface type. Note that an interface may be declared in
2309 -- any non-package declarative part, but reaching the end of
2310 -- such a declarative part will always freeze the type and
2311 -- generate the invariant procedure (see Freeze_Type).
2312
2313 if Is_Interface (Typ) then
2314
2315 -- Interfaces are treated as the partial view of a private
2316 -- type, in order to achieve uniformity with the general
2317 -- case. As a result, an interface receives only a "partial"
2318 -- invariant procedure, which is never called.
2319
2320 if Has_Own_Invariants (Typ) then
2321 Build_Invariant_Procedure_Body
2322 (Typ => Typ,
2323 Partial_Invariant => True);
2324 end if;
2325
2326 -- Preanalyze and resolve the invariants of a private type
2327 -- at the end of the visible declarations to catch potential
2328 -- errors. Inherited class-wide invariants are not included
2329 -- because they have already been resolved.
2330
2331 elsif Decls = Visible_Declarations (Context)
2332 and then Ekind_In (Typ, E_Limited_Private_Type,
2333 E_Private_Type,
2334 E_Record_Type_With_Private)
2335 and then Has_Own_Invariants (Typ)
2336 then
2337 Build_Invariant_Procedure_Body
2338 (Typ => Typ,
2339 Partial_Invariant => True);
2340
2341 -- Preanalyze and resolve the invariants of a private type's
2342 -- full view at the end of the private declarations to catch
2343 -- potential errors.
2344
2345 elsif Decls = Private_Declarations (Context)
2346 and then not Is_Private_Type (Typ)
2347 and then Has_Private_Declaration (Typ)
2348 and then Has_Invariants (Typ)
2349 then
2350 Build_Invariant_Procedure_Body (Typ);
2351 end if;
2352 end if;
2353 end Build_Assertion_Bodies_For_Type;
2354
2355 -- Local variables
2356
2357 Decl : Node_Id;
2358 Decl_Id : Entity_Id;
2359
2360 -- Start of processing for Build_Assertion_Bodies
2361
2362 begin
2363 Decl := First (Decls);
2364 while Present (Decl) loop
2365 if Is_Declaration (Decl) then
2366 Decl_Id := Defining_Entity (Decl);
2367
2368 if Is_Type (Decl_Id) then
2369 Build_Assertion_Bodies_For_Type (Decl_Id);
2370 end if;
2371 end if;
2372
2373 Next (Decl);
2374 end loop;
2375 end Build_Assertion_Bodies;
2376
2377 ---------------------------
2378 -- Check_Entry_Contracts --
2379 ---------------------------
2380
2381 procedure Check_Entry_Contracts is
2382 ASN : Node_Id;
2383 Ent : Entity_Id;
2384 Exp : Node_Id;
2385
2386 begin
2387 Ent := First_Entity (Current_Scope);
2388 while Present (Ent) loop
2389
2390 -- This only concerns entries with pre/postconditions
2391
2392 if Ekind (Ent) = E_Entry
2393 and then Present (Contract (Ent))
2394 and then Present (Pre_Post_Conditions (Contract (Ent)))
2395 then
2396 ASN := Pre_Post_Conditions (Contract (Ent));
2397 Push_Scope (Ent);
2398 Install_Formals (Ent);
2399
2400 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2401 -- is performed on a copy of the pragma expression, to prevent
2402 -- modifying the original expression.
2403
2404 while Present (ASN) loop
2405 if Nkind (ASN) = N_Pragma then
2406 Exp :=
2407 New_Copy_Tree
2408 (Expression
2409 (First (Pragma_Argument_Associations (ASN))));
2410 Set_Parent (Exp, ASN);
2411
2412 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2413 end if;
2414
2415 ASN := Next_Pragma (ASN);
2416 end loop;
2417
2418 End_Scope;
2419 end if;
2420
2421 Next_Entity (Ent);
2422 end loop;
2423 end Check_Entry_Contracts;
2424
2425 ----------------------------------
2426 -- Contains_Lib_Incomplete_Type --
2427 ----------------------------------
2428
2429 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2430 Curr : Entity_Id;
2431
2432 begin
2433 -- Avoid looking through scopes that do not meet the precondition of
2434 -- Pkg not being within a library unit spec.
2435
2436 if not Is_Compilation_Unit (Pkg)
2437 and then not Is_Generic_Instance (Pkg)
2438 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2439 then
2440 -- Loop through all entities in the current scope to identify
2441 -- an entity that depends on a private type.
2442
2443 Curr := First_Entity (Pkg);
2444 loop
2445 if Nkind (Curr) in N_Entity
2446 and then Depends_On_Private (Curr)
2447 then
2448 return True;
2449 end if;
2450
2451 exit when Last_Entity (Current_Scope) = Curr;
2452 Curr := Next_Entity (Curr);
2453 end loop;
2454 end if;
2455
2456 return False;
2457 end Contains_Lib_Incomplete_Type;
2458
2459 --------------------------------------
2460 -- Handle_Late_Controlled_Primitive --
2461 --------------------------------------
2462
2463 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2464 Body_Spec : constant Node_Id := Specification (Body_Decl);
2465 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2466 Loc : constant Source_Ptr := Sloc (Body_Id);
2467 Params : constant List_Id :=
2468 Parameter_Specifications (Body_Spec);
2469 Spec : Node_Id;
2470 Spec_Id : Entity_Id;
2471 Typ : Node_Id;
2472
2473 begin
2474 -- Consider only procedure bodies whose name matches one of the three
2475 -- controlled primitives.
2476
2477 if Nkind (Body_Spec) /= N_Procedure_Specification
2478 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2479 Name_Finalize,
2480 Name_Initialize)
2481 then
2482 return;
2483
2484 -- A controlled primitive must have exactly one formal which is not
2485 -- an anonymous access type.
2486
2487 elsif List_Length (Params) /= 1 then
2488 return;
2489 end if;
2490
2491 Typ := Parameter_Type (First (Params));
2492
2493 if Nkind (Typ) = N_Access_Definition then
2494 return;
2495 end if;
2496
2497 Find_Type (Typ);
2498
2499 -- The type of the formal must be derived from [Limited_]Controlled
2500
2501 if not Is_Controlled (Entity (Typ)) then
2502 return;
2503 end if;
2504
2505 -- Check whether a specification exists for this body. We do not
2506 -- analyze the spec of the body in full, because it will be analyzed
2507 -- again when the body is properly analyzed, and we cannot create
2508 -- duplicate entries in the formals chain. We look for an explicit
2509 -- specification because the body may be an overriding operation and
2510 -- an inherited spec may be present.
2511
2512 Spec_Id := Current_Entity (Body_Id);
2513
2514 while Present (Spec_Id) loop
2515 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2516 and then Scope (Spec_Id) = Current_Scope
2517 and then Present (First_Formal (Spec_Id))
2518 and then No (Next_Formal (First_Formal (Spec_Id)))
2519 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2520 and then Comes_From_Source (Spec_Id)
2521 then
2522 return;
2523 end if;
2524
2525 Spec_Id := Homonym (Spec_Id);
2526 end loop;
2527
2528 -- At this point the body is known to be a late controlled primitive.
2529 -- Generate a matching spec and insert it before the body. Note the
2530 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2531 -- tree in this case.
2532
2533 Spec := Copy_Separate_Tree (Body_Spec);
2534
2535 -- Ensure that the subprogram declaration does not inherit the null
2536 -- indicator from the body as we now have a proper spec/body pair.
2537
2538 Set_Null_Present (Spec, False);
2539
2540 -- Ensure that the freeze node is inserted after the declaration of
2541 -- the primitive since its expansion will freeze the primitive.
2542
2543 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2544
2545 Insert_Before_And_Analyze (Body_Decl, Decl);
2546 end Handle_Late_Controlled_Primitive;
2547
2548 ----------------------------------------
2549 -- Remove_Partial_Visible_Refinements --
2550 ----------------------------------------
2551
2552 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2553 State_Elmt : Elmt_Id;
2554 begin
2555 if Present (Abstract_States (Spec_Id)) then
2556 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2557 while Present (State_Elmt) loop
2558 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2559 Next_Elmt (State_Elmt);
2560 end loop;
2561 end if;
2562
2563 -- For a child unit, also hide the partial state refinement from
2564 -- ancestor packages.
2565
2566 if Is_Child_Unit (Spec_Id) then
2567 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2568 end if;
2569 end Remove_Partial_Visible_Refinements;
2570
2571 --------------------------------
2572 -- Remove_Visible_Refinements --
2573 --------------------------------
2574
2575 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2576 State_Elmt : Elmt_Id;
2577 begin
2578 if Present (Abstract_States (Spec_Id)) then
2579 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2580 while Present (State_Elmt) loop
2581 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2582 Next_Elmt (State_Elmt);
2583 end loop;
2584 end if;
2585 end Remove_Visible_Refinements;
2586
2587 ---------------------
2588 -- Resolve_Aspects --
2589 ---------------------
2590
2591 procedure Resolve_Aspects is
2592 E : Entity_Id;
2593
2594 begin
2595 E := First_Entity (Current_Scope);
2596 while Present (E) loop
2597 Resolve_Aspect_Expressions (E);
2598 Next_Entity (E);
2599 end loop;
2600 end Resolve_Aspects;
2601
2602 -- Local variables
2603
2604 Context : Node_Id := Empty;
2605 Freeze_From : Entity_Id := Empty;
2606 Next_Decl : Node_Id;
2607
2608 Body_Seen : Boolean := False;
2609 -- Flag set when the first body [stub] is encountered
2610
2611 -- Start of processing for Analyze_Declarations
2612
2613 begin
2614 if Restriction_Check_Required (SPARK_05) then
2615 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2616 end if;
2617
2618 Decl := First (L);
2619 while Present (Decl) loop
2620
2621 -- Package spec cannot contain a package declaration in SPARK
2622
2623 if Nkind (Decl) = N_Package_Declaration
2624 and then Nkind (Parent (L)) = N_Package_Specification
2625 then
2626 Check_SPARK_05_Restriction
2627 ("package specification cannot contain a package declaration",
2628 Decl);
2629 end if;
2630
2631 -- Complete analysis of declaration
2632
2633 Analyze (Decl);
2634 Next_Decl := Next (Decl);
2635
2636 if No (Freeze_From) then
2637 Freeze_From := First_Entity (Current_Scope);
2638 end if;
2639
2640 -- At the end of a declarative part, freeze remaining entities
2641 -- declared in it. The end of the visible declarations of package
2642 -- specification is not the end of a declarative part if private
2643 -- declarations are present. The end of a package declaration is a
2644 -- freezing point only if it a library package. A task definition or
2645 -- protected type definition is not a freeze point either. Finally,
2646 -- we do not freeze entities in generic scopes, because there is no
2647 -- code generated for them and freeze nodes will be generated for
2648 -- the instance.
2649
2650 -- The end of a package instantiation is not a freeze point, but
2651 -- for now we make it one, because the generic body is inserted
2652 -- (currently) immediately after. Generic instantiations will not
2653 -- be a freeze point once delayed freezing of bodies is implemented.
2654 -- (This is needed in any case for early instantiations ???).
2655
2656 if No (Next_Decl) then
2657 if Nkind (Parent (L)) = N_Component_List then
2658 null;
2659
2660 elsif Nkind_In (Parent (L), N_Protected_Definition,
2661 N_Task_Definition)
2662 then
2663 Check_Entry_Contracts;
2664
2665 elsif Nkind (Parent (L)) /= N_Package_Specification then
2666 if Nkind (Parent (L)) = N_Package_Body then
2667 Freeze_From := First_Entity (Current_Scope);
2668 end if;
2669
2670 -- There may have been several freezing points previously,
2671 -- for example object declarations or subprogram bodies, but
2672 -- at the end of a declarative part we check freezing from
2673 -- the beginning, even though entities may already be frozen,
2674 -- in order to perform visibility checks on delayed aspects.
2675
2676 Adjust_Decl;
2677
2678 -- If the current scope is a generic subprogram body. Skip the
2679 -- generic formal parameters that are not frozen here.
2680
2681 if Is_Subprogram (Current_Scope)
2682 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2683 N_Generic_Subprogram_Declaration
2684 and then Present (First_Entity (Current_Scope))
2685 then
2686 while Is_Generic_Formal (Freeze_From) loop
2687 Freeze_From := Next_Entity (Freeze_From);
2688 end loop;
2689
2690 Freeze_All (Freeze_From, Decl);
2691 Freeze_From := Last_Entity (Current_Scope);
2692
2693 else
2694 -- For declarations in a subprogram body there is no issue
2695 -- with name resolution in aspect specifications, but in
2696 -- ASIS mode we need to preanalyze aspect specifications
2697 -- that may otherwise only be analyzed during expansion
2698 -- (e.g. during generation of a related subprogram).
2699
2700 if ASIS_Mode then
2701 Resolve_Aspects;
2702 end if;
2703
2704 Freeze_All (First_Entity (Current_Scope), Decl);
2705 Freeze_From := Last_Entity (Current_Scope);
2706 end if;
2707
2708 -- Current scope is a package specification
2709
2710 elsif Scope (Current_Scope) /= Standard_Standard
2711 and then not Is_Child_Unit (Current_Scope)
2712 and then No (Generic_Parent (Parent (L)))
2713 then
2714 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2715 -- resolved at the end of the immediately enclosing declaration
2716 -- list (AI05-0183-1).
2717
2718 Resolve_Aspects;
2719
2720 elsif L /= Visible_Declarations (Parent (L))
2721 or else No (Private_Declarations (Parent (L)))
2722 or else Is_Empty_List (Private_Declarations (Parent (L)))
2723 then
2724 Adjust_Decl;
2725
2726 -- End of a package declaration
2727
2728 -- In compilation mode the expansion of freeze node takes care
2729 -- of resolving expressions of all aspects in the list. In ASIS
2730 -- mode this must be done explicitly.
2731
2732 if ASIS_Mode
2733 and then Scope (Current_Scope) = Standard_Standard
2734 then
2735 Resolve_Aspects;
2736 end if;
2737
2738 -- This is a freeze point because it is the end of a
2739 -- compilation unit.
2740
2741 Freeze_All (First_Entity (Current_Scope), Decl);
2742 Freeze_From := Last_Entity (Current_Scope);
2743
2744 -- At the end of the visible declarations the expressions in
2745 -- aspects of all entities declared so far must be resolved.
2746 -- The entities themselves might be frozen later, and the
2747 -- generated pragmas and attribute definition clauses analyzed
2748 -- in full at that point, but name resolution must take place
2749 -- now.
2750 -- In addition to being the proper semantics, this is mandatory
2751 -- within generic units, because global name capture requires
2752 -- those expressions to be analyzed, given that the generated
2753 -- pragmas do not appear in the original generic tree.
2754
2755 elsif Serious_Errors_Detected = 0 then
2756 Resolve_Aspects;
2757 end if;
2758
2759 -- If next node is a body then freeze all types before the body.
2760 -- An exception occurs for some expander-generated bodies. If these
2761 -- are generated at places where in general language rules would not
2762 -- allow a freeze point, then we assume that the expander has
2763 -- explicitly checked that all required types are properly frozen,
2764 -- and we do not cause general freezing here. This special circuit
2765 -- is used when the encountered body is marked as having already
2766 -- been analyzed.
2767
2768 -- In all other cases (bodies that come from source, and expander
2769 -- generated bodies that have not been analyzed yet), freeze all
2770 -- types now. Note that in the latter case, the expander must take
2771 -- care to attach the bodies at a proper place in the tree so as to
2772 -- not cause unwanted freezing at that point.
2773
2774 -- It is also necessary to check for a case where both an expression
2775 -- function is used and the current scope depends on an incomplete
2776 -- private type from a library unit, otherwise premature freezing of
2777 -- the private type will occur.
2778
2779 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2780 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2781 or else not Was_Expression_Function (Next_Decl))
2782 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2783 and then not Contains_Lib_Incomplete_Type
2784 (Current_Scope)))
2785 then
2786 -- When a controlled type is frozen, the expander generates stream
2787 -- and controlled-type support routines. If the freeze is caused
2788 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2789 -- expander will end up using the wrong version of these routines,
2790 -- as the body has not been processed yet. To remedy this, detect
2791 -- a late controlled primitive and create a proper spec for it.
2792 -- This ensures that the primitive will override its inherited
2793 -- counterpart before the freeze takes place.
2794
2795 -- If the declaration we just processed is a body, do not attempt
2796 -- to examine Next_Decl as the late primitive idiom can only apply
2797 -- to the first encountered body.
2798
2799 -- The spec of the late primitive is not generated in ASIS mode to
2800 -- ensure a consistent list of primitives that indicates the true
2801 -- semantic structure of the program (which is not relevant when
2802 -- generating executable code).
2803
2804 -- ??? A cleaner approach may be possible and/or this solution
2805 -- could be extended to general-purpose late primitives, TBD.
2806
2807 if not ASIS_Mode
2808 and then not Body_Seen
2809 and then not Is_Body (Decl)
2810 then
2811 Body_Seen := True;
2812
2813 if Nkind (Next_Decl) = N_Subprogram_Body then
2814 Handle_Late_Controlled_Primitive (Next_Decl);
2815 end if;
2816
2817 else
2818 -- In ASIS mode, if the next declaration is a body, complete
2819 -- the analysis of declarations so far.
2820
2821 Resolve_Aspects;
2822 end if;
2823
2824 Adjust_Decl;
2825
2826 -- The generated body of an expression function does not freeze,
2827 -- unless it is a completion, in which case only the expression
2828 -- itself freezes. This is handled when the body itself is
2829 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2830
2831 Freeze_All (Freeze_From, Decl);
2832 Freeze_From := Last_Entity (Current_Scope);
2833 end if;
2834
2835 Decl := Next_Decl;
2836 end loop;
2837
2838 -- Post-freezing actions
2839
2840 if Present (L) then
2841 Context := Parent (L);
2842
2843 -- Certain contract annocations have forward visibility semantics and
2844 -- must be analyzed after all declarative items have been processed.
2845 -- This timing ensures that entities referenced by such contracts are
2846 -- visible.
2847
2848 -- Analyze the contract of an immediately enclosing package spec or
2849 -- body first because other contracts may depend on its information.
2850
2851 if Nkind (Context) = N_Package_Body then
2852 Analyze_Package_Body_Contract (Defining_Entity (Context));
2853
2854 elsif Nkind (Context) = N_Package_Specification then
2855 Analyze_Package_Contract (Defining_Entity (Context));
2856 end if;
2857
2858 -- Analyze the contracts of various constructs in the declarative
2859 -- list.
2860
2861 Analyze_Contracts (L);
2862
2863 if Nkind (Context) = N_Package_Body then
2864
2865 -- Ensure that all abstract states and objects declared in the
2866 -- state space of a package body are utilized as constituents.
2867
2868 Check_Unused_Body_States (Defining_Entity (Context));
2869
2870 -- State refinements are visible up to the end of the package body
2871 -- declarations. Hide the state refinements from visibility to
2872 -- restore the original state conditions.
2873
2874 Remove_Visible_Refinements (Corresponding_Spec (Context));
2875 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2876
2877 elsif Nkind (Context) = N_Package_Specification then
2878
2879 -- Partial state refinements are visible up to the end of the
2880 -- package spec declarations. Hide the partial state refinements
2881 -- from visibility to restore the original state conditions.
2882
2883 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2884 end if;
2885
2886 -- Verify that all abstract states found in any package declared in
2887 -- the input declarative list have proper refinements. The check is
2888 -- performed only when the context denotes a block, entry, package,
2889 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2890
2891 Check_State_Refinements (Context);
2892
2893 -- Create the subprogram bodies which verify the run-time semantics
2894 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2895 -- types within the current declarative list. This ensures that all
2896 -- assertion expressions are preanalyzed and resolved at the end of
2897 -- the declarative part. Note that the resolution happens even when
2898 -- freezing does not take place.
2899
2900 Build_Assertion_Bodies (L, Context);
2901 end if;
2902 end Analyze_Declarations;
2903
2904 -----------------------------------
2905 -- Analyze_Full_Type_Declaration --
2906 -----------------------------------
2907
2908 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2909 Def : constant Node_Id := Type_Definition (N);
2910 Def_Id : constant Entity_Id := Defining_Identifier (N);
2911 T : Entity_Id;
2912 Prev : Entity_Id;
2913
2914 Is_Remote : constant Boolean :=
2915 (Is_Remote_Types (Current_Scope)
2916 or else Is_Remote_Call_Interface (Current_Scope))
2917 and then not (In_Private_Part (Current_Scope)
2918 or else In_Package_Body (Current_Scope));
2919
2920 procedure Check_Nonoverridable_Aspects;
2921 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2922 -- be overridden, and can only be confirmed on derivation.
2923
2924 procedure Check_Ops_From_Incomplete_Type;
2925 -- If there is a tagged incomplete partial view of the type, traverse
2926 -- the primitives of the incomplete view and change the type of any
2927 -- controlling formals and result to indicate the full view. The
2928 -- primitives will be added to the full type's primitive operations
2929 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2930 -- is called from Process_Incomplete_Dependents).
2931
2932 ----------------------------------
2933 -- Check_Nonoverridable_Aspects --
2934 ----------------------------------
2935
2936 procedure Check_Nonoverridable_Aspects is
2937 function Get_Aspect_Spec
2938 (Specs : List_Id;
2939 Aspect_Name : Name_Id) return Node_Id;
2940 -- Check whether a list of aspect specifications includes an entry
2941 -- for a specific aspect. The list is either that of a partial or
2942 -- a full view.
2943
2944 ---------------------
2945 -- Get_Aspect_Spec --
2946 ---------------------
2947
2948 function Get_Aspect_Spec
2949 (Specs : List_Id;
2950 Aspect_Name : Name_Id) return Node_Id
2951 is
2952 Spec : Node_Id;
2953
2954 begin
2955 Spec := First (Specs);
2956 while Present (Spec) loop
2957 if Chars (Identifier (Spec)) = Aspect_Name then
2958 return Spec;
2959 end if;
2960 Next (Spec);
2961 end loop;
2962
2963 return Empty;
2964 end Get_Aspect_Spec;
2965
2966 -- Local variables
2967
2968 Prev_Aspects : constant List_Id :=
2969 Aspect_Specifications (Parent (Def_Id));
2970 Par_Type : Entity_Id;
2971 Prev_Aspect : Node_Id;
2972
2973 -- Start of processing for Check_Nonoverridable_Aspects
2974
2975 begin
2976 -- Get parent type of derived type. Note that Prev is the entity in
2977 -- the partial declaration, but its contents are now those of full
2978 -- view, while Def_Id reflects the partial view.
2979
2980 if Is_Private_Type (Def_Id) then
2981 Par_Type := Etype (Full_View (Def_Id));
2982 else
2983 Par_Type := Etype (Def_Id);
2984 end if;
2985
2986 -- If there is an inherited Implicit_Dereference, verify that it is
2987 -- made explicit in the partial view.
2988
2989 if Has_Discriminants (Base_Type (Par_Type))
2990 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2991 and then Present (Discriminant_Specifications (Parent (Prev)))
2992 and then Present (Get_Reference_Discriminant (Par_Type))
2993 then
2994 Prev_Aspect :=
2995 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2996
2997 if No (Prev_Aspect)
2998 and then Present
2999 (Discriminant_Specifications
3000 (Original_Node (Parent (Prev))))
3001 then
3002 Error_Msg_N
3003 ("type does not inherit implicit dereference", Prev);
3004
3005 else
3006 -- If one of the views has the aspect specified, verify that it
3007 -- is consistent with that of the parent.
3008
3009 declare
3010 Cur_Discr : constant Entity_Id :=
3011 Get_Reference_Discriminant (Prev);
3012 Par_Discr : constant Entity_Id :=
3013 Get_Reference_Discriminant (Par_Type);
3014
3015 begin
3016 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3017 Error_Msg_N
3018 ("aspect inconsistent with that of parent", N);
3019 end if;
3020
3021 -- Check that specification in partial view matches the
3022 -- inherited aspect. Compare names directly because aspect
3023 -- expression may not be analyzed.
3024
3025 if Present (Prev_Aspect)
3026 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3027 and then Chars (Expression (Prev_Aspect)) /=
3028 Chars (Cur_Discr)
3029 then
3030 Error_Msg_N
3031 ("aspect inconsistent with that of parent", N);
3032 end if;
3033 end;
3034 end if;
3035 end if;
3036
3037 -- TBD : other nonoverridable aspects.
3038 end Check_Nonoverridable_Aspects;
3039
3040 ------------------------------------
3041 -- Check_Ops_From_Incomplete_Type --
3042 ------------------------------------
3043
3044 procedure Check_Ops_From_Incomplete_Type is
3045 Elmt : Elmt_Id;
3046 Formal : Entity_Id;
3047 Op : Entity_Id;
3048
3049 begin
3050 if Prev /= T
3051 and then Ekind (Prev) = E_Incomplete_Type
3052 and then Is_Tagged_Type (Prev)
3053 and then Is_Tagged_Type (T)
3054 then
3055 Elmt := First_Elmt (Primitive_Operations (Prev));
3056 while Present (Elmt) loop
3057 Op := Node (Elmt);
3058
3059 Formal := First_Formal (Op);
3060 while Present (Formal) loop
3061 if Etype (Formal) = Prev then
3062 Set_Etype (Formal, T);
3063 end if;
3064
3065 Next_Formal (Formal);
3066 end loop;
3067
3068 if Etype (Op) = Prev then
3069 Set_Etype (Op, T);
3070 end if;
3071
3072 Next_Elmt (Elmt);
3073 end loop;
3074 end if;
3075 end Check_Ops_From_Incomplete_Type;
3076
3077 -- Start of processing for Analyze_Full_Type_Declaration
3078
3079 begin
3080 Prev := Find_Type_Name (N);
3081
3082 -- The full view, if present, now points to the current type. If there
3083 -- is an incomplete partial view, set a link to it, to simplify the
3084 -- retrieval of primitive operations of the type.
3085
3086 -- Ada 2005 (AI-50217): If the type was previously decorated when
3087 -- imported through a LIMITED WITH clause, it appears as incomplete
3088 -- but has no full view.
3089
3090 if Ekind (Prev) = E_Incomplete_Type
3091 and then Present (Full_View (Prev))
3092 then
3093 T := Full_View (Prev);
3094 Set_Incomplete_View (N, Parent (Prev));
3095 else
3096 T := Prev;
3097 end if;
3098
3099 Set_Is_Pure (T, Is_Pure (Current_Scope));
3100
3101 -- We set the flag Is_First_Subtype here. It is needed to set the
3102 -- corresponding flag for the Implicit class-wide-type created
3103 -- during tagged types processing.
3104
3105 Set_Is_First_Subtype (T, True);
3106
3107 -- Only composite types other than array types are allowed to have
3108 -- discriminants.
3109
3110 case Nkind (Def) is
3111
3112 -- For derived types, the rule will be checked once we've figured
3113 -- out the parent type.
3114
3115 when N_Derived_Type_Definition =>
3116 null;
3117
3118 -- For record types, discriminants are allowed, unless we are in
3119 -- SPARK.
3120
3121 when N_Record_Definition =>
3122 if Present (Discriminant_Specifications (N)) then
3123 Check_SPARK_05_Restriction
3124 ("discriminant type is not allowed",
3125 Defining_Identifier
3126 (First (Discriminant_Specifications (N))));
3127 end if;
3128
3129 when others =>
3130 if Present (Discriminant_Specifications (N)) then
3131 Error_Msg_N
3132 ("elementary or array type cannot have discriminants",
3133 Defining_Identifier
3134 (First (Discriminant_Specifications (N))));
3135 end if;
3136 end case;
3137
3138 -- Elaborate the type definition according to kind, and generate
3139 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3140 -- already done (this happens during the reanalysis that follows a call
3141 -- to the high level optimizer).
3142
3143 if not Analyzed (T) then
3144 Set_Analyzed (T);
3145
3146 -- Set the SPARK mode from the current context
3147
3148 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3149 Set_SPARK_Pragma_Inherited (T);
3150
3151 case Nkind (Def) is
3152 when N_Access_To_Subprogram_Definition =>
3153 Access_Subprogram_Declaration (T, Def);
3154
3155 -- If this is a remote access to subprogram, we must create the
3156 -- equivalent fat pointer type, and related subprograms.
3157
3158 if Is_Remote then
3159 Process_Remote_AST_Declaration (N);
3160 end if;
3161
3162 -- Validate categorization rule against access type declaration
3163 -- usually a violation in Pure unit, Shared_Passive unit.
3164
3165 Validate_Access_Type_Declaration (T, N);
3166
3167 when N_Access_To_Object_Definition =>
3168 Access_Type_Declaration (T, Def);
3169
3170 -- Validate categorization rule against access type declaration
3171 -- usually a violation in Pure unit, Shared_Passive unit.
3172
3173 Validate_Access_Type_Declaration (T, N);
3174
3175 -- If we are in a Remote_Call_Interface package and define a
3176 -- RACW, then calling stubs and specific stream attributes
3177 -- must be added.
3178
3179 if Is_Remote
3180 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3181 then
3182 Add_RACW_Features (Def_Id);
3183 end if;
3184
3185 when N_Array_Type_Definition =>
3186 Array_Type_Declaration (T, Def);
3187
3188 when N_Derived_Type_Definition =>
3189 Derived_Type_Declaration (T, N, T /= Def_Id);
3190
3191 -- Inherit predicates from parent, and protect against illegal
3192 -- derivations.
3193
3194 if Is_Type (T) and then Has_Predicates (T) then
3195 Set_Has_Predicates (Def_Id);
3196 end if;
3197
3198 -- Save the scenario for examination by the ABE Processing
3199 -- phase.
3200
3201 Record_Elaboration_Scenario (N);
3202
3203 when N_Enumeration_Type_Definition =>
3204 Enumeration_Type_Declaration (T, Def);
3205
3206 when N_Floating_Point_Definition =>
3207 Floating_Point_Type_Declaration (T, Def);
3208
3209 when N_Decimal_Fixed_Point_Definition =>
3210 Decimal_Fixed_Point_Type_Declaration (T, Def);
3211
3212 when N_Ordinary_Fixed_Point_Definition =>
3213 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3214
3215 when N_Signed_Integer_Type_Definition =>
3216 Signed_Integer_Type_Declaration (T, Def);
3217
3218 when N_Modular_Type_Definition =>
3219 Modular_Type_Declaration (T, Def);
3220
3221 when N_Record_Definition =>
3222 Record_Type_Declaration (T, N, Prev);
3223
3224 -- If declaration has a parse error, nothing to elaborate.
3225
3226 when N_Error =>
3227 null;
3228
3229 when others =>
3230 raise Program_Error;
3231 end case;
3232 end if;
3233
3234 if Etype (T) = Any_Type then
3235 return;
3236 end if;
3237
3238 -- Controlled type is not allowed in SPARK
3239
3240 if Is_Visibly_Controlled (T) then
3241 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3242 end if;
3243
3244 -- Some common processing for all types
3245
3246 Set_Depends_On_Private (T, Has_Private_Component (T));
3247 Check_Ops_From_Incomplete_Type;
3248
3249 -- Both the declared entity, and its anonymous base type if one was
3250 -- created, need freeze nodes allocated.
3251
3252 declare
3253 B : constant Entity_Id := Base_Type (T);
3254
3255 begin
3256 -- In the case where the base type differs from the first subtype, we
3257 -- pre-allocate a freeze node, and set the proper link to the first
3258 -- subtype. Freeze_Entity will use this preallocated freeze node when
3259 -- it freezes the entity.
3260
3261 -- This does not apply if the base type is a generic type, whose
3262 -- declaration is independent of the current derived definition.
3263
3264 if B /= T and then not Is_Generic_Type (B) then
3265 Ensure_Freeze_Node (B);
3266 Set_First_Subtype_Link (Freeze_Node (B), T);
3267 end if;
3268
3269 -- A type that is imported through a limited_with clause cannot
3270 -- generate any code, and thus need not be frozen. However, an access
3271 -- type with an imported designated type needs a finalization list,
3272 -- which may be referenced in some other package that has non-limited
3273 -- visibility on the designated type. Thus we must create the
3274 -- finalization list at the point the access type is frozen, to
3275 -- prevent unsatisfied references at link time.
3276
3277 if not From_Limited_With (T) or else Is_Access_Type (T) then
3278 Set_Has_Delayed_Freeze (T);
3279 end if;
3280 end;
3281
3282 -- Case where T is the full declaration of some private type which has
3283 -- been swapped in Defining_Identifier (N).
3284
3285 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3286 Process_Full_View (N, T, Def_Id);
3287
3288 -- Record the reference. The form of this is a little strange, since
3289 -- the full declaration has been swapped in. So the first parameter
3290 -- here represents the entity to which a reference is made which is
3291 -- the "real" entity, i.e. the one swapped in, and the second
3292 -- parameter provides the reference location.
3293
3294 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3295 -- since we don't want a complaint about the full type being an
3296 -- unwanted reference to the private type
3297
3298 declare
3299 B : constant Boolean := Has_Pragma_Unreferenced (T);
3300 begin
3301 Set_Has_Pragma_Unreferenced (T, False);
3302 Generate_Reference (T, T, 'c');
3303 Set_Has_Pragma_Unreferenced (T, B);
3304 end;
3305
3306 Set_Completion_Referenced (Def_Id);
3307
3308 -- For completion of incomplete type, process incomplete dependents
3309 -- and always mark the full type as referenced (it is the incomplete
3310 -- type that we get for any real reference).
3311
3312 elsif Ekind (Prev) = E_Incomplete_Type then
3313 Process_Incomplete_Dependents (N, T, Prev);
3314 Generate_Reference (Prev, Def_Id, 'c');
3315 Set_Completion_Referenced (Def_Id);
3316
3317 -- If not private type or incomplete type completion, this is a real
3318 -- definition of a new entity, so record it.
3319
3320 else
3321 Generate_Definition (Def_Id);
3322 end if;
3323
3324 -- Propagate any pending access types whose finalization masters need to
3325 -- be fully initialized from the partial to the full view. Guard against
3326 -- an illegal full view that remains unanalyzed.
3327
3328 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3329 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3330 end if;
3331
3332 if Chars (Scope (Def_Id)) = Name_System
3333 and then Chars (Def_Id) = Name_Address
3334 and then In_Predefined_Unit (N)
3335 then
3336 Set_Is_Descendant_Of_Address (Def_Id);
3337 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3338 Set_Is_Descendant_Of_Address (Prev);
3339 end if;
3340
3341 Set_Optimize_Alignment_Flags (Def_Id);
3342 Check_Eliminated (Def_Id);
3343
3344 -- If the declaration is a completion and aspects are present, apply
3345 -- them to the entity for the type which is currently the partial
3346 -- view, but which is the one that will be frozen.
3347
3348 if Has_Aspects (N) then
3349
3350 -- In most cases the partial view is a private type, and both views
3351 -- appear in different declarative parts. In the unusual case where
3352 -- the partial view is incomplete, perform the analysis on the
3353 -- full view, to prevent freezing anomalies with the corresponding
3354 -- class-wide type, which otherwise might be frozen before the
3355 -- dispatch table is built.
3356
3357 if Prev /= Def_Id
3358 and then Ekind (Prev) /= E_Incomplete_Type
3359 then
3360 Analyze_Aspect_Specifications (N, Prev);
3361
3362 -- Normal case
3363
3364 else
3365 Analyze_Aspect_Specifications (N, Def_Id);
3366 end if;
3367 end if;
3368
3369 if Is_Derived_Type (Prev)
3370 and then Def_Id /= Prev
3371 then
3372 Check_Nonoverridable_Aspects;
3373 end if;
3374 end Analyze_Full_Type_Declaration;
3375
3376 ----------------------------------
3377 -- Analyze_Incomplete_Type_Decl --
3378 ----------------------------------
3379
3380 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3381 F : constant Boolean := Is_Pure (Current_Scope);
3382 T : Entity_Id;
3383
3384 begin
3385 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3386
3387 Generate_Definition (Defining_Identifier (N));
3388
3389 -- Process an incomplete declaration. The identifier must not have been
3390 -- declared already in the scope. However, an incomplete declaration may
3391 -- appear in the private part of a package, for a private type that has
3392 -- already been declared.
3393
3394 -- In this case, the discriminants (if any) must match
3395
3396 T := Find_Type_Name (N);
3397
3398 Set_Ekind (T, E_Incomplete_Type);
3399 Set_Etype (T, T);
3400 Set_Is_First_Subtype (T);
3401 Init_Size_Align (T);
3402
3403 -- Set the SPARK mode from the current context
3404
3405 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3406 Set_SPARK_Pragma_Inherited (T);
3407
3408 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3409 -- incomplete types.
3410
3411 if Tagged_Present (N) then
3412 Set_Is_Tagged_Type (T, True);
3413 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3414 Make_Class_Wide_Type (T);
3415 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3416 end if;
3417
3418 Set_Stored_Constraint (T, No_Elist);
3419
3420 if Present (Discriminant_Specifications (N)) then
3421 Push_Scope (T);
3422 Process_Discriminants (N);
3423 End_Scope;
3424 end if;
3425
3426 -- If the type has discriminants, nontrivial subtypes may be declared
3427 -- before the full view of the type. The full views of those subtypes
3428 -- will be built after the full view of the type.
3429
3430 Set_Private_Dependents (T, New_Elmt_List);
3431 Set_Is_Pure (T, F);
3432 end Analyze_Incomplete_Type_Decl;
3433
3434 -----------------------------------
3435 -- Analyze_Interface_Declaration --
3436 -----------------------------------
3437
3438 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3439 CW : constant Entity_Id := Class_Wide_Type (T);
3440
3441 begin
3442 Set_Is_Tagged_Type (T);
3443 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3444
3445 Set_Is_Limited_Record (T, Limited_Present (Def)
3446 or else Task_Present (Def)
3447 or else Protected_Present (Def)
3448 or else Synchronized_Present (Def));
3449
3450 -- Type is abstract if full declaration carries keyword, or if previous
3451 -- partial view did.
3452
3453 Set_Is_Abstract_Type (T);
3454 Set_Is_Interface (T);
3455
3456 -- Type is a limited interface if it includes the keyword limited, task,
3457 -- protected, or synchronized.
3458
3459 Set_Is_Limited_Interface
3460 (T, Limited_Present (Def)
3461 or else Protected_Present (Def)
3462 or else Synchronized_Present (Def)
3463 or else Task_Present (Def));
3464
3465 Set_Interfaces (T, New_Elmt_List);
3466 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3467
3468 -- Complete the decoration of the class-wide entity if it was already
3469 -- built (i.e. during the creation of the limited view)
3470
3471 if Present (CW) then
3472 Set_Is_Interface (CW);
3473 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3474 end if;
3475
3476 -- Check runtime support for synchronized interfaces
3477
3478 if (Is_Task_Interface (T)
3479 or else Is_Protected_Interface (T)
3480 or else Is_Synchronized_Interface (T))
3481 and then not RTE_Available (RE_Select_Specific_Data)
3482 then
3483 Error_Msg_CRT ("synchronized interfaces", T);
3484 end if;
3485 end Analyze_Interface_Declaration;
3486
3487 -----------------------------
3488 -- Analyze_Itype_Reference --
3489 -----------------------------
3490
3491 -- Nothing to do. This node is placed in the tree only for the benefit of
3492 -- back end processing, and has no effect on the semantic processing.
3493
3494 procedure Analyze_Itype_Reference (N : Node_Id) is
3495 begin
3496 pragma Assert (Is_Itype (Itype (N)));
3497 null;
3498 end Analyze_Itype_Reference;
3499
3500 --------------------------------
3501 -- Analyze_Number_Declaration --
3502 --------------------------------
3503
3504 procedure Analyze_Number_Declaration (N : Node_Id) is
3505 E : constant Node_Id := Expression (N);
3506 Id : constant Entity_Id := Defining_Identifier (N);
3507 Index : Interp_Index;
3508 It : Interp;
3509 T : Entity_Id;
3510
3511 begin
3512 Generate_Definition (Id);
3513 Enter_Name (Id);
3514
3515 -- This is an optimization of a common case of an integer literal
3516
3517 if Nkind (E) = N_Integer_Literal then
3518 Set_Is_Static_Expression (E, True);
3519 Set_Etype (E, Universal_Integer);
3520
3521 Set_Etype (Id, Universal_Integer);
3522 Set_Ekind (Id, E_Named_Integer);
3523 Set_Is_Frozen (Id, True);
3524 return;
3525 end if;
3526
3527 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3528
3529 -- Process expression, replacing error by integer zero, to avoid
3530 -- cascaded errors or aborts further along in the processing
3531
3532 -- Replace Error by integer zero, which seems least likely to cause
3533 -- cascaded errors.
3534
3535 if E = Error then
3536 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3537 Set_Error_Posted (E);
3538 end if;
3539
3540 Analyze (E);
3541
3542 -- Verify that the expression is static and numeric. If
3543 -- the expression is overloaded, we apply the preference
3544 -- rule that favors root numeric types.
3545
3546 if not Is_Overloaded (E) then
3547 T := Etype (E);
3548 if Has_Dynamic_Predicate_Aspect (T) then
3549 Error_Msg_N
3550 ("subtype has dynamic predicate, "
3551 & "not allowed in number declaration", N);
3552 end if;
3553
3554 else
3555 T := Any_Type;
3556
3557 Get_First_Interp (E, Index, It);
3558 while Present (It.Typ) loop
3559 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3560 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3561 then
3562 if T = Any_Type then
3563 T := It.Typ;
3564
3565 elsif It.Typ = Universal_Real
3566 or else
3567 It.Typ = Universal_Integer
3568 then
3569 -- Choose universal interpretation over any other
3570
3571 T := It.Typ;
3572 exit;
3573 end if;
3574 end if;
3575
3576 Get_Next_Interp (Index, It);
3577 end loop;
3578 end if;
3579
3580 if Is_Integer_Type (T) then
3581 Resolve (E, T);
3582 Set_Etype (Id, Universal_Integer);
3583 Set_Ekind (Id, E_Named_Integer);
3584
3585 elsif Is_Real_Type (T) then
3586
3587 -- Because the real value is converted to universal_real, this is a
3588 -- legal context for a universal fixed expression.
3589
3590 if T = Universal_Fixed then
3591 declare
3592 Loc : constant Source_Ptr := Sloc (N);
3593 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3594 Subtype_Mark =>
3595 New_Occurrence_Of (Universal_Real, Loc),
3596 Expression => Relocate_Node (E));
3597
3598 begin
3599 Rewrite (E, Conv);
3600 Analyze (E);
3601 end;
3602
3603 elsif T = Any_Fixed then
3604 Error_Msg_N ("illegal context for mixed mode operation", E);
3605
3606 -- Expression is of the form : universal_fixed * integer. Try to
3607 -- resolve as universal_real.
3608
3609 T := Universal_Real;
3610 Set_Etype (E, T);
3611 end if;
3612
3613 Resolve (E, T);
3614 Set_Etype (Id, Universal_Real);
3615 Set_Ekind (Id, E_Named_Real);
3616
3617 else
3618 Wrong_Type (E, Any_Numeric);
3619 Resolve (E, T);
3620
3621 Set_Etype (Id, T);
3622 Set_Ekind (Id, E_Constant);
3623 Set_Never_Set_In_Source (Id, True);
3624 Set_Is_True_Constant (Id, True);
3625 return;
3626 end if;
3627
3628 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3629 Set_Etype (E, Etype (Id));
3630 end if;
3631
3632 if not Is_OK_Static_Expression (E) then
3633 Flag_Non_Static_Expr
3634 ("non-static expression used in number declaration!", E);
3635 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3636 Set_Etype (E, Any_Type);
3637 end if;
3638
3639 Analyze_Dimension (N);
3640 end Analyze_Number_Declaration;
3641
3642 --------------------------------
3643 -- Analyze_Object_Declaration --
3644 --------------------------------
3645
3646 -- WARNING: This routine manages Ghost regions. Return statements must be
3647 -- replaced by gotos which jump to the end of the routine and restore the
3648 -- Ghost mode.
3649
3650 procedure Analyze_Object_Declaration (N : Node_Id) is
3651 Loc : constant Source_Ptr := Sloc (N);
3652 Id : constant Entity_Id := Defining_Identifier (N);
3653 Next_Decl : constant Node_Id := Next (N);
3654
3655 Act_T : Entity_Id;
3656 T : Entity_Id;
3657
3658 E : Node_Id := Expression (N);
3659 -- E is set to Expression (N) throughout this routine. When Expression
3660 -- (N) is modified, E is changed accordingly.
3661
3662 Prev_Entity : Entity_Id := Empty;
3663
3664 procedure Check_Dynamic_Object (Typ : Entity_Id);
3665 -- A library-level object with nonstatic discriminant constraints may
3666 -- require dynamic allocation. The declaration is illegal if the
3667 -- profile includes the restriction No_Implicit_Heap_Allocations.
3668
3669 procedure Check_For_Null_Excluding_Components
3670 (Obj_Typ : Entity_Id;
3671 Obj_Decl : Node_Id);
3672 -- Verify that each null-excluding component of object declaration
3673 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3674 -- a compile-time warning if this is not the case.
3675
3676 function Count_Tasks (T : Entity_Id) return Uint;
3677 -- This function is called when a non-generic library level object of a
3678 -- task type is declared. Its function is to count the static number of
3679 -- tasks declared within the type (it is only called if Has_Task is set
3680 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3681 -- or a variant record type is encountered, Check_Restriction is called
3682 -- indicating the count is unknown.
3683
3684 function Delayed_Aspect_Present return Boolean;
3685 -- If the declaration has an expression that is an aggregate, and it
3686 -- has aspects that require delayed analysis, the resolution of the
3687 -- aggregate must be deferred to the freeze point of the object. This
3688 -- special processing was created for address clauses, but it must
3689 -- also apply to Alignment. This must be done before the aspect
3690 -- specifications are analyzed because we must handle the aggregate
3691 -- before the analysis of the object declaration is complete.
3692
3693 -- Any other relevant delayed aspects on object declarations ???
3694
3695 --------------------------
3696 -- Check_Dynamic_Object --
3697 --------------------------
3698
3699 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3700 Comp : Entity_Id;
3701 Obj_Type : Entity_Id;
3702
3703 begin
3704 Obj_Type := Typ;
3705
3706 if Is_Private_Type (Obj_Type)
3707 and then Present (Full_View (Obj_Type))
3708 then
3709 Obj_Type := Full_View (Obj_Type);
3710 end if;
3711
3712 if Known_Static_Esize (Obj_Type) then
3713 return;
3714 end if;
3715
3716 if Restriction_Active (No_Implicit_Heap_Allocations)
3717 and then Expander_Active
3718 and then Has_Discriminants (Obj_Type)
3719 then
3720 Comp := First_Component (Obj_Type);
3721 while Present (Comp) loop
3722 if Known_Static_Esize (Etype (Comp))
3723 or else Size_Known_At_Compile_Time (Etype (Comp))
3724 then
3725 null;
3726
3727 elsif not Discriminated_Size (Comp)
3728 and then Comes_From_Source (Comp)
3729 then
3730 Error_Msg_NE
3731 ("component& of non-static size will violate restriction "
3732 & "No_Implicit_Heap_Allocation?", N, Comp);
3733
3734 elsif Is_Record_Type (Etype (Comp)) then
3735 Check_Dynamic_Object (Etype (Comp));
3736 end if;
3737
3738 Next_Component (Comp);
3739 end loop;
3740 end if;
3741 end Check_Dynamic_Object;
3742
3743 -----------------------------------------
3744 -- Check_For_Null_Excluding_Components --
3745 -----------------------------------------
3746
3747 procedure Check_For_Null_Excluding_Components
3748 (Obj_Typ : Entity_Id;
3749 Obj_Decl : Node_Id)
3750 is
3751 procedure Check_Component
3752 (Comp_Typ : Entity_Id;
3753 Comp_Decl : Node_Id := Empty;
3754 Array_Comp : Boolean := False);
3755 -- Apply a compile-time null-exclusion check on a component denoted
3756 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3757 -- subcomponents (if any).
3758
3759 ---------------------
3760 -- Check_Component --
3761 ---------------------
3762
3763 procedure Check_Component
3764 (Comp_Typ : Entity_Id;
3765 Comp_Decl : Node_Id := Empty;
3766 Array_Comp : Boolean := False)
3767 is
3768 Comp : Entity_Id;
3769 T : Entity_Id;
3770
3771 begin
3772 -- Do not consider internally-generated components or those that
3773 -- are already initialized.
3774
3775 if Present (Comp_Decl)
3776 and then (not Comes_From_Source (Comp_Decl)
3777 or else Present (Expression (Comp_Decl)))
3778 then
3779 return;
3780 end if;
3781
3782 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3783 and then Present (Full_View (Comp_Typ))
3784 then
3785 T := Full_View (Comp_Typ);
3786 else
3787 T := Comp_Typ;
3788 end if;
3789
3790 -- Verify a component of a null-excluding access type
3791
3792 if Is_Access_Type (T)
3793 and then Can_Never_Be_Null (T)
3794 then
3795 if Comp_Decl = Obj_Decl then
3796 Null_Exclusion_Static_Checks
3797 (N => Obj_Decl,
3798 Comp => Empty,
3799 Array_Comp => Array_Comp);
3800
3801 else
3802 Null_Exclusion_Static_Checks
3803 (N => Obj_Decl,
3804 Comp => Comp_Decl,
3805 Array_Comp => Array_Comp);
3806 end if;
3807
3808 -- Check array components
3809
3810 elsif Is_Array_Type (T) then
3811
3812 -- There is no suitable component when the object is of an
3813 -- array type. However, a namable component may appear at some
3814 -- point during the recursive inspection, but not at the top
3815 -- level. At the top level just indicate array component case.
3816
3817 if Comp_Decl = Obj_Decl then
3818 Check_Component (Component_Type (T), Array_Comp => True);
3819 else
3820 Check_Component (Component_Type (T), Comp_Decl);
3821 end if;
3822
3823 -- Verify all components of type T
3824
3825 -- Note: No checks are performed on types with discriminants due
3826 -- to complexities involving variants. ???
3827
3828 elsif (Is_Concurrent_Type (T)
3829 or else Is_Incomplete_Or_Private_Type (T)
3830 or else Is_Record_Type (T))
3831 and then not Has_Discriminants (T)
3832 then
3833 Comp := First_Component (T);
3834 while Present (Comp) loop
3835 Check_Component (Etype (Comp), Parent (Comp));
3836
3837 Comp := Next_Component (Comp);
3838 end loop;
3839 end if;
3840 end Check_Component;
3841
3842 -- Start processing for Check_For_Null_Excluding_Components
3843
3844 begin
3845 Check_Component (Obj_Typ, Obj_Decl);
3846 end Check_For_Null_Excluding_Components;
3847
3848 -----------------
3849 -- Count_Tasks --
3850 -----------------
3851
3852 function Count_Tasks (T : Entity_Id) return Uint is
3853 C : Entity_Id;
3854 X : Node_Id;
3855 V : Uint;
3856
3857 begin
3858 if Is_Task_Type (T) then
3859 return Uint_1;
3860
3861 elsif Is_Record_Type (T) then
3862 if Has_Discriminants (T) then
3863 Check_Restriction (Max_Tasks, N);
3864 return Uint_0;
3865
3866 else
3867 V := Uint_0;
3868 C := First_Component (T);
3869 while Present (C) loop
3870 V := V + Count_Tasks (Etype (C));
3871 Next_Component (C);
3872 end loop;
3873
3874 return V;
3875 end if;
3876
3877 elsif Is_Array_Type (T) then
3878 X := First_Index (T);
3879 V := Count_Tasks (Component_Type (T));
3880 while Present (X) loop
3881 C := Etype (X);
3882
3883 if not Is_OK_Static_Subtype (C) then
3884 Check_Restriction (Max_Tasks, N);
3885 return Uint_0;
3886 else
3887 V := V * (UI_Max (Uint_0,
3888 Expr_Value (Type_High_Bound (C)) -
3889 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3890 end if;
3891
3892 Next_Index (X);
3893 end loop;
3894
3895 return V;
3896
3897 else
3898 return Uint_0;
3899 end if;
3900 end Count_Tasks;
3901
3902 ----------------------------
3903 -- Delayed_Aspect_Present --
3904 ----------------------------
3905
3906 function Delayed_Aspect_Present return Boolean is
3907 A : Node_Id;
3908 A_Id : Aspect_Id;
3909
3910 begin
3911 if Present (Aspect_Specifications (N)) then
3912 A := First (Aspect_Specifications (N));
3913 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3914 while Present (A) loop
3915 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3916
3917 -- Set flag on object entity, for later processing at
3918 -- the freeze point.
3919
3920 Set_Has_Delayed_Aspects (Id);
3921 return True;
3922 end if;
3923
3924 Next (A);
3925 end loop;
3926 end if;
3927
3928 return False;
3929 end Delayed_Aspect_Present;
3930
3931 -- Local variables
3932
3933 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3934 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3935 -- Save the Ghost-related attributes to restore on exit
3936
3937 Related_Id : Entity_Id;
3938
3939 -- Start of processing for Analyze_Object_Declaration
3940
3941 begin
3942 -- There are three kinds of implicit types generated by an
3943 -- object declaration:
3944
3945 -- 1. Those generated by the original Object Definition
3946
3947 -- 2. Those generated by the Expression
3948
3949 -- 3. Those used to constrain the Object Definition with the
3950 -- expression constraints when the definition is unconstrained.
3951
3952 -- They must be generated in this order to avoid order of elaboration
3953 -- issues. Thus the first step (after entering the name) is to analyze
3954 -- the object definition.
3955
3956 if Constant_Present (N) then
3957 Prev_Entity := Current_Entity_In_Scope (Id);
3958
3959 if Present (Prev_Entity)
3960 and then
3961 -- If the homograph is an implicit subprogram, it is overridden
3962 -- by the current declaration.
3963
3964 ((Is_Overloadable (Prev_Entity)
3965 and then Is_Inherited_Operation (Prev_Entity))
3966
3967 -- The current object is a discriminal generated for an entry
3968 -- family index. Even though the index is a constant, in this
3969 -- particular context there is no true constant redeclaration.
3970 -- Enter_Name will handle the visibility.
3971
3972 or else
3973 (Is_Discriminal (Id)
3974 and then Ekind (Discriminal_Link (Id)) =
3975 E_Entry_Index_Parameter)
3976
3977 -- The current object is the renaming for a generic declared
3978 -- within the instance.
3979
3980 or else
3981 (Ekind (Prev_Entity) = E_Package
3982 and then Nkind (Parent (Prev_Entity)) =
3983 N_Package_Renaming_Declaration
3984 and then not Comes_From_Source (Prev_Entity)
3985 and then
3986 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3987
3988 -- The entity may be a homonym of a private component of the
3989 -- enclosing protected object, for which we create a local
3990 -- renaming declaration. The declaration is legal, even if
3991 -- useless when it just captures that component.
3992
3993 or else
3994 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3995 and then Nkind (Parent (Prev_Entity)) =
3996 N_Object_Renaming_Declaration))
3997 then
3998 Prev_Entity := Empty;
3999 end if;
4000 end if;
4001
4002 if Present (Prev_Entity) then
4003
4004 -- The object declaration is Ghost when it completes a deferred Ghost
4005 -- constant.
4006
4007 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4008
4009 Constant_Redeclaration (Id, N, T);
4010
4011 Generate_Reference (Prev_Entity, Id, 'c');
4012 Set_Completion_Referenced (Id);
4013
4014 if Error_Posted (N) then
4015
4016 -- Type mismatch or illegal redeclaration; do not analyze
4017 -- expression to avoid cascaded errors.
4018
4019 T := Find_Type_Of_Object (Object_Definition (N), N);
4020 Set_Etype (Id, T);
4021 Set_Ekind (Id, E_Variable);
4022 goto Leave;
4023 end if;
4024
4025 -- In the normal case, enter identifier at the start to catch premature
4026 -- usage in the initialization expression.
4027
4028 else
4029 Generate_Definition (Id);
4030 Enter_Name (Id);
4031
4032 Mark_Coextensions (N, Object_Definition (N));
4033
4034 T := Find_Type_Of_Object (Object_Definition (N), N);
4035
4036 if Nkind (Object_Definition (N)) = N_Access_Definition
4037 and then Present
4038 (Access_To_Subprogram_Definition (Object_Definition (N)))
4039 and then Protected_Present
4040 (Access_To_Subprogram_Definition (Object_Definition (N)))
4041 then
4042 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4043 end if;
4044
4045 if Error_Posted (Id) then
4046 Set_Etype (Id, T);
4047 Set_Ekind (Id, E_Variable);
4048 goto Leave;
4049 end if;
4050 end if;
4051
4052 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4053 -- out some static checks.
4054
4055 if Ada_Version >= Ada_2005 then
4056
4057 -- In case of aggregates we must also take care of the correct
4058 -- initialization of nested aggregates bug this is done at the
4059 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4060
4061 if Can_Never_Be_Null (T) then
4062 if Present (Expression (N))
4063 and then Nkind (Expression (N)) = N_Aggregate
4064 then
4065 null;
4066
4067 else
4068 declare
4069 Save_Typ : constant Entity_Id := Etype (Id);
4070 begin
4071 Set_Etype (Id, T); -- Temp. decoration for static checks
4072 Null_Exclusion_Static_Checks (N);
4073 Set_Etype (Id, Save_Typ);
4074 end;
4075 end if;
4076
4077 -- We might be dealing with an object of a composite type containing
4078 -- null-excluding components without an aggregate, so we must verify
4079 -- that such components have default initialization.
4080
4081 else
4082 Check_For_Null_Excluding_Components (T, N);
4083 end if;
4084 end if;
4085
4086 -- Object is marked pure if it is in a pure scope
4087
4088 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4089
4090 -- If deferred constant, make sure context is appropriate. We detect
4091 -- a deferred constant as a constant declaration with no expression.
4092 -- A deferred constant can appear in a package body if its completion
4093 -- is by means of an interface pragma.
4094
4095 if Constant_Present (N) and then No (E) then
4096
4097 -- A deferred constant may appear in the declarative part of the
4098 -- following constructs:
4099
4100 -- blocks
4101 -- entry bodies
4102 -- extended return statements
4103 -- package specs
4104 -- package bodies
4105 -- subprogram bodies
4106 -- task bodies
4107
4108 -- When declared inside a package spec, a deferred constant must be
4109 -- completed by a full constant declaration or pragma Import. In all
4110 -- other cases, the only proper completion is pragma Import. Extended
4111 -- return statements are flagged as invalid contexts because they do
4112 -- not have a declarative part and so cannot accommodate the pragma.
4113
4114 if Ekind (Current_Scope) = E_Return_Statement then
4115 Error_Msg_N
4116 ("invalid context for deferred constant declaration (RM 7.4)",
4117 N);
4118 Error_Msg_N
4119 ("\declaration requires an initialization expression",
4120 N);
4121 Set_Constant_Present (N, False);
4122
4123 -- In Ada 83, deferred constant must be of private type
4124
4125 elsif not Is_Private_Type (T) then
4126 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4127 Error_Msg_N
4128 ("(Ada 83) deferred constant must be private type", N);
4129 end if;
4130 end if;
4131
4132 -- If not a deferred constant, then the object declaration freezes
4133 -- its type, unless the object is of an anonymous type and has delayed
4134 -- aspects. In that case the type is frozen when the object itself is.
4135
4136 else
4137 Check_Fully_Declared (T, N);
4138
4139 if Has_Delayed_Aspects (Id)
4140 and then Is_Array_Type (T)
4141 and then Is_Itype (T)
4142 then
4143 Set_Has_Delayed_Freeze (T);
4144 else
4145 Freeze_Before (N, T);
4146 end if;
4147 end if;
4148
4149 -- If the object was created by a constrained array definition, then
4150 -- set the link in both the anonymous base type and anonymous subtype
4151 -- that are built to represent the array type to point to the object.
4152
4153 if Nkind (Object_Definition (Declaration_Node (Id))) =
4154 N_Constrained_Array_Definition
4155 then
4156 Set_Related_Array_Object (T, Id);
4157 Set_Related_Array_Object (Base_Type (T), Id);
4158 end if;
4159
4160 -- Special checks for protected objects not at library level
4161
4162 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4163 Check_Restriction (No_Local_Protected_Objects, Id);
4164
4165 -- Protected objects with interrupt handlers must be at library level
4166
4167 -- Ada 2005: This test is not needed (and the corresponding clause
4168 -- in the RM is removed) because accessibility checks are sufficient
4169 -- to make handlers not at the library level illegal.
4170
4171 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4172 -- applies to the '95 version of the language as well.
4173
4174 if Is_Protected_Type (T)
4175 and then Has_Interrupt_Handler (T)
4176 and then Ada_Version < Ada_95
4177 then
4178 Error_Msg_N
4179 ("interrupt object can only be declared at library level", Id);
4180 end if;
4181 end if;
4182
4183 -- Check for violation of No_Local_Timing_Events
4184
4185 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4186 Check_Restriction (No_Local_Timing_Events, Id);
4187 end if;
4188
4189 -- The actual subtype of the object is the nominal subtype, unless
4190 -- the nominal one is unconstrained and obtained from the expression.
4191
4192 Act_T := T;
4193
4194 -- These checks should be performed before the initialization expression
4195 -- is considered, so that the Object_Definition node is still the same
4196 -- as in source code.
4197
4198 -- In SPARK, the nominal subtype is always given by a subtype mark
4199 -- and must not be unconstrained. (The only exception to this is the
4200 -- acceptance of declarations of constants of type String.)
4201
4202 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4203 then
4204 Check_SPARK_05_Restriction
4205 ("subtype mark required", Object_Definition (N));
4206
4207 elsif Is_Array_Type (T)
4208 and then not Is_Constrained (T)
4209 and then T /= Standard_String
4210 then
4211 Check_SPARK_05_Restriction
4212 ("subtype mark of constrained type expected",
4213 Object_Definition (N));
4214 end if;
4215
4216 if Is_Library_Level_Entity (Id) then
4217 Check_Dynamic_Object (T);
4218 end if;
4219
4220 -- There are no aliased objects in SPARK
4221
4222 if Aliased_Present (N) then
4223 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4224 end if;
4225
4226 -- Process initialization expression if present and not in error
4227
4228 if Present (E) and then E /= Error then
4229
4230 -- Generate an error in case of CPP class-wide object initialization.
4231 -- Required because otherwise the expansion of the class-wide
4232 -- assignment would try to use 'size to initialize the object
4233 -- (primitive that is not available in CPP tagged types).
4234
4235 if Is_Class_Wide_Type (Act_T)
4236 and then
4237 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4238 or else
4239 (Present (Full_View (Root_Type (Etype (Act_T))))
4240 and then
4241 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4242 then
4243 Error_Msg_N
4244 ("predefined assignment not available for 'C'P'P tagged types",
4245 E);
4246 end if;
4247
4248 Mark_Coextensions (N, E);
4249 Analyze (E);
4250
4251 -- In case of errors detected in the analysis of the expression,
4252 -- decorate it with the expected type to avoid cascaded errors
4253
4254 if No (Etype (E)) then
4255 Set_Etype (E, T);
4256 end if;
4257
4258 -- If an initialization expression is present, then we set the
4259 -- Is_True_Constant flag. It will be reset if this is a variable
4260 -- and it is indeed modified.
4261
4262 Set_Is_True_Constant (Id, True);
4263
4264 -- If we are analyzing a constant declaration, set its completion
4265 -- flag after analyzing and resolving the expression.
4266
4267 if Constant_Present (N) then
4268 Set_Has_Completion (Id);
4269 end if;
4270
4271 -- Set type and resolve (type may be overridden later on). Note:
4272 -- Ekind (Id) must still be E_Void at this point so that incorrect
4273 -- early usage within E is properly diagnosed.
4274
4275 Set_Etype (Id, T);
4276
4277 -- If the expression is an aggregate we must look ahead to detect
4278 -- the possible presence of an address clause, and defer resolution
4279 -- and expansion of the aggregate to the freeze point of the entity.
4280
4281 -- This is not always legal because the aggregate may contain other
4282 -- references that need freezing, e.g. references to other entities
4283 -- with address clauses. In any case, when compiling with -gnatI the
4284 -- presence of the address clause must be ignored.
4285
4286 if Comes_From_Source (N)
4287 and then Expander_Active
4288 and then Nkind (E) = N_Aggregate
4289 and then
4290 ((Present (Following_Address_Clause (N))
4291 and then not Ignore_Rep_Clauses)
4292 or else Delayed_Aspect_Present)
4293 then
4294 Set_Etype (E, T);
4295
4296 -- If the aggregate is limited it will be built in place, and its
4297 -- expansion is deferred until the object declaration is expanded.
4298
4299 if Is_Limited_Type (T) then
4300 Set_Expansion_Delayed (E);
4301 end if;
4302
4303 else
4304 -- If the expression is a formal that is a "subprogram pointer"
4305 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4306 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4307 -- the corresponding check, as is done for assignments.
4308
4309 if Is_Entity_Name (E)
4310 and then Present (Entity (E))
4311 and then Is_Formal (Entity (E))
4312 and then
4313 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4314 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4315 then
4316 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4317 end if;
4318
4319 Resolve (E, T);
4320 end if;
4321
4322 -- No further action needed if E is a call to an inlined function
4323 -- which returns an unconstrained type and it has been expanded into
4324 -- a procedure call. In that case N has been replaced by an object
4325 -- declaration without initializing expression and it has been
4326 -- analyzed (see Expand_Inlined_Call).
4327
4328 if Back_End_Inlining
4329 and then Expander_Active
4330 and then Nkind (E) = N_Function_Call
4331 and then Nkind (Name (E)) in N_Has_Entity
4332 and then Is_Inlined (Entity (Name (E)))
4333 and then not Is_Constrained (Etype (E))
4334 and then Analyzed (N)
4335 and then No (Expression (N))
4336 then
4337 goto Leave;
4338 end if;
4339
4340 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4341 -- node (which was marked already-analyzed), we need to set the type
4342 -- to something other than Any_Access in order to keep gigi happy.
4343
4344 if Etype (E) = Any_Access then
4345 Set_Etype (E, T);
4346 end if;
4347
4348 -- If the object is an access to variable, the initialization
4349 -- expression cannot be an access to constant.
4350
4351 if Is_Access_Type (T)
4352 and then not Is_Access_Constant (T)
4353 and then Is_Access_Type (Etype (E))
4354 and then Is_Access_Constant (Etype (E))
4355 then
4356 Error_Msg_N
4357 ("access to variable cannot be initialized with an "
4358 & "access-to-constant expression", E);
4359 end if;
4360
4361 if not Assignment_OK (N) then
4362 Check_Initialization (T, E);
4363 end if;
4364
4365 Check_Unset_Reference (E);
4366
4367 -- If this is a variable, then set current value. If this is a
4368 -- declared constant of a scalar type with a static expression,
4369 -- indicate that it is always valid.
4370
4371 if not Constant_Present (N) then
4372 if Compile_Time_Known_Value (E) then
4373 Set_Current_Value (Id, E);
4374 end if;
4375
4376 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4377 Set_Is_Known_Valid (Id);
4378
4379 -- If it is a constant initialized with a valid nonstatic entity,
4380 -- the constant is known valid as well, and can inherit the subtype
4381 -- of the entity if it is a subtype of the given type. This info
4382 -- is preserved on the actual subtype of the constant.
4383
4384 elsif Is_Scalar_Type (T)
4385 and then Is_Entity_Name (E)
4386 and then Is_Known_Valid (Entity (E))
4387 and then In_Subrange_Of (Etype (Entity (E)), T)
4388 then
4389 Set_Is_Known_Valid (Id);
4390 Set_Ekind (Id, E_Constant);
4391 Set_Actual_Subtype (Id, Etype (Entity (E)));
4392 end if;
4393
4394 -- Deal with setting of null flags
4395
4396 if Is_Access_Type (T) then
4397 if Known_Non_Null (E) then
4398 Set_Is_Known_Non_Null (Id, True);
4399 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4400 Set_Is_Known_Null (Id, True);
4401 end if;
4402 end if;
4403
4404 -- Check incorrect use of dynamically tagged expressions
4405
4406 if Is_Tagged_Type (T) then
4407 Check_Dynamically_Tagged_Expression
4408 (Expr => E,
4409 Typ => T,
4410 Related_Nod => N);
4411 end if;
4412
4413 Apply_Scalar_Range_Check (E, T);
4414 Apply_Static_Length_Check (E, T);
4415
4416 if Nkind (Original_Node (N)) = N_Object_Declaration
4417 and then Comes_From_Source (Original_Node (N))
4418
4419 -- Only call test if needed
4420
4421 and then Restriction_Check_Required (SPARK_05)
4422 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4423 then
4424 Check_SPARK_05_Restriction
4425 ("initialization expression is not appropriate", E);
4426 end if;
4427
4428 -- A formal parameter of a specific tagged type whose related
4429 -- subprogram is subject to pragma Extensions_Visible with value
4430 -- "False" cannot be implicitly converted to a class-wide type by
4431 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4432 -- not consider internally generated expressions.
4433
4434 if Is_Class_Wide_Type (T)
4435 and then Comes_From_Source (E)
4436 and then Is_EVF_Expression (E)
4437 then
4438 Error_Msg_N
4439 ("formal parameter cannot be implicitly converted to "
4440 & "class-wide type when Extensions_Visible is False", E);
4441 end if;
4442 end if;
4443
4444 -- If the No_Streams restriction is set, check that the type of the
4445 -- object is not, and does not contain, any subtype derived from
4446 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4447 -- Has_Stream just for efficiency reasons. There is no point in
4448 -- spending time on a Has_Stream check if the restriction is not set.
4449
4450 if Restriction_Check_Required (No_Streams) then
4451 if Has_Stream (T) then
4452 Check_Restriction (No_Streams, N);
4453 end if;
4454 end if;
4455
4456 -- Deal with predicate check before we start to do major rewriting. It
4457 -- is OK to initialize and then check the initialized value, since the
4458 -- object goes out of scope if we get a predicate failure. Note that we
4459 -- do this in the analyzer and not the expander because the analyzer
4460 -- does some substantial rewriting in some cases.
4461
4462 -- We need a predicate check if the type has predicates that are not
4463 -- ignored, and if either there is an initializing expression, or for
4464 -- default initialization when we have at least one case of an explicit
4465 -- default initial value and then this is not an internal declaration
4466 -- whose initialization comes later (as for an aggregate expansion).
4467 -- If expression is an aggregate it may be expanded into assignments
4468 -- and the declaration itself is marked with No_Initialization, but
4469 -- the predicate still applies.
4470
4471 if not Suppress_Assignment_Checks (N)
4472 and then Present (Predicate_Function (T))
4473 and then not Predicates_Ignored (T)
4474 and then
4475 (not No_Initialization (N)
4476 or else (Present (E) and then Nkind (E) = N_Aggregate))
4477 and then
4478 (Present (E)
4479 or else
4480 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4481 then
4482 -- If the type has a static predicate and the expression is known at
4483 -- compile time, see if the expression satisfies the predicate.
4484
4485 if Present (E) then
4486 Check_Expression_Against_Static_Predicate (E, T);
4487 end if;
4488
4489 -- If the type is a null record and there is no explicit initial
4490 -- expression, no predicate check applies.
4491
4492 if No (E) and then Is_Null_Record_Type (T) then
4493 null;
4494
4495 -- Do not generate a predicate check if the initialization expression
4496 -- is a type conversion because the conversion has been subjected to
4497 -- the same check. This is a small optimization which avoid redundant
4498 -- checks.
4499
4500 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4501 null;
4502
4503 else
4504 -- The check must be inserted after the expanded aggregate
4505 -- expansion code, if any.
4506
4507 declare
4508 Check : constant Node_Id :=
4509 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4510
4511 begin
4512 if No (Next_Decl) then
4513 Append_To (List_Containing (N), Check);
4514 else
4515 Insert_Before (Next_Decl, Check);
4516 end if;
4517 end;
4518 end if;
4519 end if;
4520
4521 -- Case of unconstrained type
4522
4523 if not Is_Definite_Subtype (T) then
4524
4525 -- In SPARK, a declaration of unconstrained type is allowed
4526 -- only for constants of type string.
4527
4528 if Is_String_Type (T) and then not Constant_Present (N) then
4529 Check_SPARK_05_Restriction
4530 ("declaration of object of unconstrained type not allowed", N);
4531 end if;
4532
4533 -- Nothing to do in deferred constant case
4534
4535 if Constant_Present (N) and then No (E) then
4536 null;
4537
4538 -- Case of no initialization present
4539
4540 elsif No (E) then
4541 if No_Initialization (N) then
4542 null;
4543
4544 elsif Is_Class_Wide_Type (T) then
4545 Error_Msg_N
4546 ("initialization required in class-wide declaration ", N);
4547
4548 else
4549 Error_Msg_N
4550 ("unconstrained subtype not allowed (need initialization)",
4551 Object_Definition (N));
4552
4553 if Is_Record_Type (T) and then Has_Discriminants (T) then
4554 Error_Msg_N
4555 ("\provide initial value or explicit discriminant values",
4556 Object_Definition (N));
4557
4558 Error_Msg_NE
4559 ("\or give default discriminant values for type&",
4560 Object_Definition (N), T);
4561
4562 elsif Is_Array_Type (T) then
4563 Error_Msg_N
4564 ("\provide initial value or explicit array bounds",
4565 Object_Definition (N));
4566 end if;
4567 end if;
4568
4569 -- Case of initialization present but in error. Set initial
4570 -- expression as absent (but do not make above complaints)
4571
4572 elsif E = Error then
4573 Set_Expression (N, Empty);
4574 E := Empty;
4575
4576 -- Case of initialization present
4577
4578 else
4579 -- Check restrictions in Ada 83
4580
4581 if not Constant_Present (N) then
4582
4583 -- Unconstrained variables not allowed in Ada 83 mode
4584
4585 if Ada_Version = Ada_83
4586 and then Comes_From_Source (Object_Definition (N))
4587 then
4588 Error_Msg_N
4589 ("(Ada 83) unconstrained variable not allowed",
4590 Object_Definition (N));
4591 end if;
4592 end if;
4593
4594 -- Now we constrain the variable from the initializing expression
4595
4596 -- If the expression is an aggregate, it has been expanded into
4597 -- individual assignments. Retrieve the actual type from the
4598 -- expanded construct.
4599
4600 if Is_Array_Type (T)
4601 and then No_Initialization (N)
4602 and then Nkind (Original_Node (E)) = N_Aggregate
4603 then
4604 Act_T := Etype (E);
4605
4606 -- In case of class-wide interface object declarations we delay
4607 -- the generation of the equivalent record type declarations until
4608 -- its expansion because there are cases in they are not required.
4609
4610 elsif Is_Interface (T) then
4611 null;
4612
4613 -- If the type is an unchecked union, no subtype can be built from
4614 -- the expression. Rewrite declaration as a renaming, which the
4615 -- back-end can handle properly. This is a rather unusual case,
4616 -- because most unchecked_union declarations have default values
4617 -- for discriminants and are thus not indefinite.
4618
4619 elsif Is_Unchecked_Union (T) then
4620 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4621 Set_Ekind (Id, E_Constant);
4622 else
4623 Set_Ekind (Id, E_Variable);
4624 end if;
4625
4626 Rewrite (N,
4627 Make_Object_Renaming_Declaration (Loc,
4628 Defining_Identifier => Id,
4629 Subtype_Mark => New_Occurrence_Of (T, Loc),
4630 Name => E));
4631
4632 Set_Renamed_Object (Id, E);
4633 Freeze_Before (N, T);
4634 Set_Is_Frozen (Id);
4635 goto Leave;
4636
4637 else
4638 -- Ensure that the generated subtype has a unique external name
4639 -- when the related object is public. This guarantees that the
4640 -- subtype and its bounds will not be affected by switches or
4641 -- pragmas that may offset the internal counter due to extra
4642 -- generated code.
4643
4644 if Is_Public (Id) then
4645 Related_Id := Id;
4646 else
4647 Related_Id := Empty;
4648 end if;
4649
4650 Expand_Subtype_From_Expr
4651 (N => N,
4652 Unc_Type => T,
4653 Subtype_Indic => Object_Definition (N),
4654 Exp => E,
4655 Related_Id => Related_Id);
4656
4657 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4658 end if;
4659
4660 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4661
4662 if Aliased_Present (N) then
4663 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4664 end if;
4665
4666 Freeze_Before (N, Act_T);
4667 Freeze_Before (N, T);
4668 end if;
4669
4670 elsif Is_Array_Type (T)
4671 and then No_Initialization (N)
4672 and then (Nkind (Original_Node (E)) = N_Aggregate
4673 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4674 and then Nkind (Original_Node (Expression
4675 (Original_Node (E)))) = N_Aggregate))
4676 then
4677 if not Is_Entity_Name (Object_Definition (N)) then
4678 Act_T := Etype (E);
4679 Check_Compile_Time_Size (Act_T);
4680
4681 if Aliased_Present (N) then
4682 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4683 end if;
4684 end if;
4685
4686 -- When the given object definition and the aggregate are specified
4687 -- independently, and their lengths might differ do a length check.
4688 -- This cannot happen if the aggregate is of the form (others =>...)
4689
4690 if not Is_Constrained (T) then
4691 null;
4692
4693 elsif Nkind (E) = N_Raise_Constraint_Error then
4694
4695 -- Aggregate is statically illegal. Place back in declaration
4696
4697 Set_Expression (N, E);
4698 Set_No_Initialization (N, False);
4699
4700 elsif T = Etype (E) then
4701 null;
4702
4703 elsif Nkind (E) = N_Aggregate
4704 and then Present (Component_Associations (E))
4705 and then Present (Choice_List (First (Component_Associations (E))))
4706 and then
4707 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4708 N_Others_Choice
4709 then
4710 null;
4711
4712 else
4713 Apply_Length_Check (E, T);
4714 end if;
4715
4716 -- If the type is limited unconstrained with defaulted discriminants and
4717 -- there is no expression, then the object is constrained by the
4718 -- defaults, so it is worthwhile building the corresponding subtype.
4719
4720 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4721 and then not Is_Constrained (T)
4722 and then Has_Discriminants (T)
4723 then
4724 if No (E) then
4725 Act_T := Build_Default_Subtype (T, N);
4726 else
4727 -- Ada 2005: A limited object may be initialized by means of an
4728 -- aggregate. If the type has default discriminants it has an
4729 -- unconstrained nominal type, Its actual subtype will be obtained
4730 -- from the aggregate, and not from the default discriminants.
4731
4732 Act_T := Etype (E);
4733 end if;
4734
4735 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4736
4737 elsif Nkind (E) = N_Function_Call
4738 and then Constant_Present (N)
4739 and then Has_Unconstrained_Elements (Etype (E))
4740 then
4741 -- The back-end has problems with constants of a discriminated type
4742 -- with defaults, if the initial value is a function call. We
4743 -- generate an intermediate temporary that will receive a reference
4744 -- to the result of the call. The initialization expression then
4745 -- becomes a dereference of that temporary.
4746
4747 Remove_Side_Effects (E);
4748
4749 -- If this is a constant declaration of an unconstrained type and
4750 -- the initialization is an aggregate, we can use the subtype of the
4751 -- aggregate for the declared entity because it is immutable.
4752
4753 elsif not Is_Constrained (T)
4754 and then Has_Discriminants (T)
4755 and then Constant_Present (N)
4756 and then not Has_Unchecked_Union (T)
4757 and then Nkind (E) = N_Aggregate
4758 then
4759 Act_T := Etype (E);
4760 end if;
4761
4762 -- Check No_Wide_Characters restriction
4763
4764 Check_Wide_Character_Restriction (T, Object_Definition (N));
4765
4766 -- Indicate this is not set in source. Certainly true for constants, and
4767 -- true for variables so far (will be reset for a variable if and when
4768 -- we encounter a modification in the source).
4769
4770 Set_Never_Set_In_Source (Id);
4771
4772 -- Now establish the proper kind and type of the object
4773
4774 if Constant_Present (N) then
4775 Set_Ekind (Id, E_Constant);
4776 Set_Is_True_Constant (Id);
4777
4778 else
4779 Set_Ekind (Id, E_Variable);
4780
4781 -- A variable is set as shared passive if it appears in a shared
4782 -- passive package, and is at the outer level. This is not done for
4783 -- entities generated during expansion, because those are always
4784 -- manipulated locally.
4785
4786 if Is_Shared_Passive (Current_Scope)
4787 and then Is_Library_Level_Entity (Id)
4788 and then Comes_From_Source (Id)
4789 then
4790 Set_Is_Shared_Passive (Id);
4791 Check_Shared_Var (Id, T, N);
4792 end if;
4793
4794 -- Set Has_Initial_Value if initializing expression present. Note
4795 -- that if there is no initializing expression, we leave the state
4796 -- of this flag unchanged (usually it will be False, but notably in
4797 -- the case of exception choice variables, it will already be true).
4798
4799 if Present (E) then
4800 Set_Has_Initial_Value (Id);
4801 end if;
4802 end if;
4803
4804 -- Set the SPARK mode from the current context (may be overwritten later
4805 -- with explicit pragma).
4806
4807 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4808 Set_SPARK_Pragma_Inherited (Id);
4809
4810 -- Preserve relevant elaboration-related attributes of the context which
4811 -- are no longer available or very expensive to recompute once analysis,
4812 -- resolution, and expansion are over.
4813
4814 Mark_Elaboration_Attributes
4815 (N_Id => Id,
4816 Checks => True,
4817 Warnings => True);
4818
4819 -- Initialize alignment and size and capture alignment setting
4820
4821 Init_Alignment (Id);
4822 Init_Esize (Id);
4823 Set_Optimize_Alignment_Flags (Id);
4824
4825 -- Deal with aliased case
4826
4827 if Aliased_Present (N) then
4828 Set_Is_Aliased (Id);
4829
4830 -- If the object is aliased and the type is unconstrained with
4831 -- defaulted discriminants and there is no expression, then the
4832 -- object is constrained by the defaults, so it is worthwhile
4833 -- building the corresponding subtype.
4834
4835 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4836 -- unconstrained, then only establish an actual subtype if the
4837 -- nominal subtype is indefinite. In definite cases the object is
4838 -- unconstrained in Ada 2005.
4839
4840 if No (E)
4841 and then Is_Record_Type (T)
4842 and then not Is_Constrained (T)
4843 and then Has_Discriminants (T)
4844 and then (Ada_Version < Ada_2005
4845 or else not Is_Definite_Subtype (T))
4846 then
4847 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4848 end if;
4849 end if;
4850
4851 -- Now we can set the type of the object
4852
4853 Set_Etype (Id, Act_T);
4854
4855 -- Non-constant object is marked to be treated as volatile if type is
4856 -- volatile and we clear the Current_Value setting that may have been
4857 -- set above. Doing so for constants isn't required and might interfere
4858 -- with possible uses of the object as a static expression in contexts
4859 -- incompatible with volatility (e.g. as a case-statement alternative).
4860
4861 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4862 Set_Treat_As_Volatile (Id);
4863 Set_Current_Value (Id, Empty);
4864 end if;
4865
4866 -- Deal with controlled types
4867
4868 if Has_Controlled_Component (Etype (Id))
4869 or else Is_Controlled (Etype (Id))
4870 then
4871 if not Is_Library_Level_Entity (Id) then
4872 Check_Restriction (No_Nested_Finalization, N);
4873 else
4874 Validate_Controlled_Object (Id);
4875 end if;
4876 end if;
4877
4878 if Has_Task (Etype (Id)) then
4879 Check_Restriction (No_Tasking, N);
4880
4881 -- Deal with counting max tasks
4882
4883 -- Nothing to do if inside a generic
4884
4885 if Inside_A_Generic then
4886 null;
4887
4888 -- If library level entity, then count tasks
4889
4890 elsif Is_Library_Level_Entity (Id) then
4891 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4892
4893 -- If not library level entity, then indicate we don't know max
4894 -- tasks and also check task hierarchy restriction and blocking
4895 -- operation (since starting a task is definitely blocking).
4896
4897 else
4898 Check_Restriction (Max_Tasks, N);
4899 Check_Restriction (No_Task_Hierarchy, N);
4900 Check_Potentially_Blocking_Operation (N);
4901 end if;
4902
4903 -- A rather specialized test. If we see two tasks being declared
4904 -- of the same type in the same object declaration, and the task
4905 -- has an entry with an address clause, we know that program error
4906 -- will be raised at run time since we can't have two tasks with
4907 -- entries at the same address.
4908
4909 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4910 declare
4911 E : Entity_Id;
4912
4913 begin
4914 E := First_Entity (Etype (Id));
4915 while Present (E) loop
4916 if Ekind (E) = E_Entry
4917 and then Present (Get_Attribute_Definition_Clause
4918 (E, Attribute_Address))
4919 then
4920 Error_Msg_Warn := SPARK_Mode /= On;
4921 Error_Msg_N
4922 ("more than one task with same entry address<<", N);
4923 Error_Msg_N ("\Program_Error [<<", N);
4924 Insert_Action (N,
4925 Make_Raise_Program_Error (Loc,
4926 Reason => PE_Duplicated_Entry_Address));
4927 exit;
4928 end if;
4929
4930 Next_Entity (E);
4931 end loop;
4932 end;
4933 end if;
4934 end if;
4935
4936 -- Some simple constant-propagation: if the expression is a constant
4937 -- string initialized with a literal, share the literal. This avoids
4938 -- a run-time copy.
4939
4940 if Present (E)
4941 and then Is_Entity_Name (E)
4942 and then Ekind (Entity (E)) = E_Constant
4943 and then Base_Type (Etype (E)) = Standard_String
4944 then
4945 declare
4946 Val : constant Node_Id := Constant_Value (Entity (E));
4947 begin
4948 if Present (Val) and then Nkind (Val) = N_String_Literal then
4949 Rewrite (E, New_Copy (Val));
4950 end if;
4951 end;
4952 end if;
4953
4954 -- Another optimization: if the nominal subtype is unconstrained and
4955 -- the expression is a function call that returns an unconstrained
4956 -- type, rewrite the declaration as a renaming of the result of the
4957 -- call. The exceptions below are cases where the copy is expected,
4958 -- either by the back end (Aliased case) or by the semantics, as for
4959 -- initializing controlled types or copying tags for class-wide types.
4960
4961 if Present (E)
4962 and then Nkind (E) = N_Explicit_Dereference
4963 and then Nkind (Original_Node (E)) = N_Function_Call
4964 and then not Is_Library_Level_Entity (Id)
4965 and then not Is_Constrained (Underlying_Type (T))
4966 and then not Is_Aliased (Id)
4967 and then not Is_Class_Wide_Type (T)
4968 and then not Is_Controlled (T)
4969 and then not Has_Controlled_Component (Base_Type (T))
4970 and then Expander_Active
4971 then
4972 Rewrite (N,
4973 Make_Object_Renaming_Declaration (Loc,
4974 Defining_Identifier => Id,
4975 Access_Definition => Empty,
4976 Subtype_Mark => New_Occurrence_Of
4977 (Base_Type (Etype (Id)), Loc),
4978 Name => E));
4979
4980 Set_Renamed_Object (Id, E);
4981
4982 -- Force generation of debugging information for the constant and for
4983 -- the renamed function call.
4984
4985 Set_Debug_Info_Needed (Id);
4986 Set_Debug_Info_Needed (Entity (Prefix (E)));
4987 end if;
4988
4989 if Present (Prev_Entity)
4990 and then Is_Frozen (Prev_Entity)
4991 and then not Error_Posted (Id)
4992 then
4993 Error_Msg_N ("full constant declaration appears too late", N);
4994 end if;
4995
4996 Check_Eliminated (Id);
4997
4998 -- Deal with setting In_Private_Part flag if in private part
4999
5000 if Ekind (Scope (Id)) = E_Package
5001 and then In_Private_Part (Scope (Id))
5002 then
5003 Set_In_Private_Part (Id);
5004 end if;
5005
5006 <<Leave>>
5007 -- Initialize the refined state of a variable here because this is a
5008 -- common destination for legal and illegal object declarations.
5009
5010 if Ekind (Id) = E_Variable then
5011 Set_Encapsulating_State (Id, Empty);
5012 end if;
5013
5014 if Has_Aspects (N) then
5015 Analyze_Aspect_Specifications (N, Id);
5016 end if;
5017
5018 Analyze_Dimension (N);
5019
5020 -- Verify whether the object declaration introduces an illegal hidden
5021 -- state within a package subject to a null abstract state.
5022
5023 if Ekind (Id) = E_Variable then
5024 Check_No_Hidden_State (Id);
5025 end if;
5026
5027 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5028 end Analyze_Object_Declaration;
5029
5030 ---------------------------
5031 -- Analyze_Others_Choice --
5032 ---------------------------
5033
5034 -- Nothing to do for the others choice node itself, the semantic analysis
5035 -- of the others choice will occur as part of the processing of the parent
5036
5037 procedure Analyze_Others_Choice (N : Node_Id) is
5038 pragma Warnings (Off, N);
5039 begin
5040 null;
5041 end Analyze_Others_Choice;
5042
5043 -------------------------------------------
5044 -- Analyze_Private_Extension_Declaration --
5045 -------------------------------------------
5046
5047 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5048 Indic : constant Node_Id := Subtype_Indication (N);
5049 T : constant Entity_Id := Defining_Identifier (N);
5050 Iface : Entity_Id;
5051 Iface_Elmt : Elmt_Id;
5052 Parent_Base : Entity_Id;
5053 Parent_Type : Entity_Id;
5054
5055 begin
5056 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5057
5058 if Is_Non_Empty_List (Interface_List (N)) then
5059 declare
5060 Intf : Node_Id;
5061 T : Entity_Id;
5062
5063 begin
5064 Intf := First (Interface_List (N));
5065 while Present (Intf) loop
5066 T := Find_Type_Of_Subtype_Indic (Intf);
5067
5068 Diagnose_Interface (Intf, T);
5069 Next (Intf);
5070 end loop;
5071 end;
5072 end if;
5073
5074 Generate_Definition (T);
5075
5076 -- For other than Ada 2012, just enter the name in the current scope
5077
5078 if Ada_Version < Ada_2012 then
5079 Enter_Name (T);
5080
5081 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5082 -- case of private type that completes an incomplete type.
5083
5084 else
5085 declare
5086 Prev : Entity_Id;
5087
5088 begin
5089 Prev := Find_Type_Name (N);
5090
5091 pragma Assert (Prev = T
5092 or else (Ekind (Prev) = E_Incomplete_Type
5093 and then Present (Full_View (Prev))
5094 and then Full_View (Prev) = T));
5095 end;
5096 end if;
5097
5098 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5099 Parent_Base := Base_Type (Parent_Type);
5100
5101 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5102 Set_Ekind (T, Ekind (Parent_Type));
5103 Set_Etype (T, Any_Type);
5104 goto Leave;
5105
5106 elsif not Is_Tagged_Type (Parent_Type) then
5107 Error_Msg_N
5108 ("parent of type extension must be a tagged type ", Indic);
5109 goto Leave;
5110
5111 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5112 Error_Msg_N ("premature derivation of incomplete type", Indic);
5113 goto Leave;
5114
5115 elsif Is_Concurrent_Type (Parent_Type) then
5116 Error_Msg_N
5117 ("parent type of a private extension cannot be a synchronized "
5118 & "tagged type (RM 3.9.1 (3/1))", N);
5119
5120 Set_Etype (T, Any_Type);
5121 Set_Ekind (T, E_Limited_Private_Type);
5122 Set_Private_Dependents (T, New_Elmt_List);
5123 Set_Error_Posted (T);
5124 goto Leave;
5125 end if;
5126
5127 -- Perhaps the parent type should be changed to the class-wide type's
5128 -- specific type in this case to prevent cascading errors ???
5129
5130 if Is_Class_Wide_Type (Parent_Type) then
5131 Error_Msg_N
5132 ("parent of type extension must not be a class-wide type", Indic);
5133 goto Leave;
5134 end if;
5135
5136 if (not Is_Package_Or_Generic_Package (Current_Scope)
5137 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5138 or else In_Private_Part (Current_Scope)
5139 then
5140 Error_Msg_N ("invalid context for private extension", N);
5141 end if;
5142
5143 -- Set common attributes
5144
5145 Set_Is_Pure (T, Is_Pure (Current_Scope));
5146 Set_Scope (T, Current_Scope);
5147 Set_Ekind (T, E_Record_Type_With_Private);
5148 Init_Size_Align (T);
5149 Set_Default_SSO (T);
5150 Set_No_Reordering (T, No_Component_Reordering);
5151
5152 Set_Etype (T, Parent_Base);
5153 Propagate_Concurrent_Flags (T, Parent_Base);
5154
5155 Set_Convention (T, Convention (Parent_Type));
5156 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5157 Set_Is_First_Subtype (T);
5158 Make_Class_Wide_Type (T);
5159
5160 -- Set the SPARK mode from the current context
5161
5162 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5163 Set_SPARK_Pragma_Inherited (T);
5164
5165 if Unknown_Discriminants_Present (N) then
5166 Set_Discriminant_Constraint (T, No_Elist);
5167 end if;
5168
5169 Build_Derived_Record_Type (N, Parent_Type, T);
5170
5171 -- A private extension inherits the Default_Initial_Condition pragma
5172 -- coming from any parent type within the derivation chain.
5173
5174 if Has_DIC (Parent_Type) then
5175 Set_Has_Inherited_DIC (T);
5176 end if;
5177
5178 -- A private extension inherits any class-wide invariants coming from a
5179 -- parent type or an interface. Note that the invariant procedure of the
5180 -- parent type should not be inherited because the private extension may
5181 -- define invariants of its own.
5182
5183 if Has_Inherited_Invariants (Parent_Type)
5184 or else Has_Inheritable_Invariants (Parent_Type)
5185 then
5186 Set_Has_Inherited_Invariants (T);
5187
5188 elsif Present (Interfaces (T)) then
5189 Iface_Elmt := First_Elmt (Interfaces (T));
5190 while Present (Iface_Elmt) loop
5191 Iface := Node (Iface_Elmt);
5192
5193 if Has_Inheritable_Invariants (Iface) then
5194 Set_Has_Inherited_Invariants (T);
5195 exit;
5196 end if;
5197
5198 Next_Elmt (Iface_Elmt);
5199 end loop;
5200 end if;
5201
5202 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5203 -- synchronized formal derived type.
5204
5205 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5206 Set_Is_Limited_Record (T);
5207
5208 -- Formal derived type case
5209
5210 if Is_Generic_Type (T) then
5211
5212 -- The parent must be a tagged limited type or a synchronized
5213 -- interface.
5214
5215 if (not Is_Tagged_Type (Parent_Type)
5216 or else not Is_Limited_Type (Parent_Type))
5217 and then
5218 (not Is_Interface (Parent_Type)
5219 or else not Is_Synchronized_Interface (Parent_Type))
5220 then
5221 Error_Msg_NE
5222 ("parent type of & must be tagged limited or synchronized",
5223 N, T);
5224 end if;
5225
5226 -- The progenitors (if any) must be limited or synchronized
5227 -- interfaces.
5228
5229 if Present (Interfaces (T)) then
5230 Iface_Elmt := First_Elmt (Interfaces (T));
5231 while Present (Iface_Elmt) loop
5232 Iface := Node (Iface_Elmt);
5233
5234 if not Is_Limited_Interface (Iface)
5235 and then not Is_Synchronized_Interface (Iface)
5236 then
5237 Error_Msg_NE
5238 ("progenitor & must be limited or synchronized",
5239 N, Iface);
5240 end if;
5241
5242 Next_Elmt (Iface_Elmt);
5243 end loop;
5244 end if;
5245
5246 -- Regular derived extension, the parent must be a limited or
5247 -- synchronized interface.
5248
5249 else
5250 if not Is_Interface (Parent_Type)
5251 or else (not Is_Limited_Interface (Parent_Type)
5252 and then not Is_Synchronized_Interface (Parent_Type))
5253 then
5254 Error_Msg_NE
5255 ("parent type of & must be limited interface", N, T);
5256 end if;
5257 end if;
5258
5259 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5260 -- extension with a synchronized parent must be explicitly declared
5261 -- synchronized, because the full view will be a synchronized type.
5262 -- This must be checked before the check for limited types below,
5263 -- to ensure that types declared limited are not allowed to extend
5264 -- synchronized interfaces.
5265
5266 elsif Is_Interface (Parent_Type)
5267 and then Is_Synchronized_Interface (Parent_Type)
5268 and then not Synchronized_Present (N)
5269 then
5270 Error_Msg_NE
5271 ("private extension of& must be explicitly synchronized",
5272 N, Parent_Type);
5273
5274 elsif Limited_Present (N) then
5275 Set_Is_Limited_Record (T);
5276
5277 if not Is_Limited_Type (Parent_Type)
5278 and then
5279 (not Is_Interface (Parent_Type)
5280 or else not Is_Limited_Interface (Parent_Type))
5281 then
5282 Error_Msg_NE ("parent type& of limited extension must be limited",
5283 N, Parent_Type);
5284 end if;
5285 end if;
5286
5287 -- Remember that its parent type has a private extension. Used to warn
5288 -- on public primitives of the parent type defined after its private
5289 -- extensions (see Check_Dispatching_Operation).
5290
5291 Set_Has_Private_Extension (Parent_Type);
5292
5293 <<Leave>>
5294 if Has_Aspects (N) then
5295 Analyze_Aspect_Specifications (N, T);
5296 end if;
5297 end Analyze_Private_Extension_Declaration;
5298
5299 ---------------------------------
5300 -- Analyze_Subtype_Declaration --
5301 ---------------------------------
5302
5303 procedure Analyze_Subtype_Declaration
5304 (N : Node_Id;
5305 Skip : Boolean := False)
5306 is
5307 Id : constant Entity_Id := Defining_Identifier (N);
5308 R_Checks : Check_Result;
5309 T : Entity_Id;
5310
5311 begin
5312 Generate_Definition (Id);
5313 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5314 Init_Size_Align (Id);
5315
5316 -- The following guard condition on Enter_Name is to handle cases where
5317 -- the defining identifier has already been entered into the scope but
5318 -- the declaration as a whole needs to be analyzed.
5319
5320 -- This case in particular happens for derived enumeration types. The
5321 -- derived enumeration type is processed as an inserted enumeration type
5322 -- declaration followed by a rewritten subtype declaration. The defining
5323 -- identifier, however, is entered into the name scope very early in the
5324 -- processing of the original type declaration and therefore needs to be
5325 -- avoided here, when the created subtype declaration is analyzed. (See
5326 -- Build_Derived_Types)
5327
5328 -- This also happens when the full view of a private type is derived
5329 -- type with constraints. In this case the entity has been introduced
5330 -- in the private declaration.
5331
5332 -- Finally this happens in some complex cases when validity checks are
5333 -- enabled, where the same subtype declaration may be analyzed twice.
5334 -- This can happen if the subtype is created by the preanalysis of
5335 -- an attribute tht gives the range of a loop statement, and the loop
5336 -- itself appears within an if_statement that will be rewritten during
5337 -- expansion.
5338
5339 if Skip
5340 or else (Present (Etype (Id))
5341 and then (Is_Private_Type (Etype (Id))
5342 or else Is_Task_Type (Etype (Id))
5343 or else Is_Rewrite_Substitution (N)))
5344 then
5345 null;
5346
5347 elsif Current_Entity (Id) = Id then
5348 null;
5349
5350 else
5351 Enter_Name (Id);
5352 end if;
5353
5354 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5355
5356 -- Class-wide equivalent types of records with unknown discriminants
5357 -- involve the generation of an itype which serves as the private view
5358 -- of a constrained record subtype. In such cases the base type of the
5359 -- current subtype we are processing is the private itype. Use the full
5360 -- of the private itype when decorating various attributes.
5361
5362 if Is_Itype (T)
5363 and then Is_Private_Type (T)
5364 and then Present (Full_View (T))
5365 then
5366 T := Full_View (T);
5367 end if;
5368
5369 -- Inherit common attributes
5370
5371 Set_Is_Volatile (Id, Is_Volatile (T));
5372 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5373 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5374 Set_Convention (Id, Convention (T));
5375
5376 -- If ancestor has predicates then so does the subtype, and in addition
5377 -- we must delay the freeze to properly arrange predicate inheritance.
5378
5379 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5380 -- in which T = ID, so the above tests and assignments do nothing???
5381
5382 if Has_Predicates (T)
5383 or else (Present (Ancestor_Subtype (T))
5384 and then Has_Predicates (Ancestor_Subtype (T)))
5385 then
5386 Set_Has_Predicates (Id);
5387 Set_Has_Delayed_Freeze (Id);
5388
5389 -- Generated subtypes inherit the predicate function from the parent
5390 -- (no aspects to examine on the generated declaration).
5391
5392 if not Comes_From_Source (N) then
5393 Set_Ekind (Id, Ekind (T));
5394
5395 if Present (Predicate_Function (Id)) then
5396 null;
5397
5398 elsif Present (Predicate_Function (T)) then
5399 Set_Predicate_Function (Id, Predicate_Function (T));
5400
5401 elsif Present (Ancestor_Subtype (T))
5402 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5403 then
5404 Set_Predicate_Function (Id,
5405 Predicate_Function (Ancestor_Subtype (T)));
5406 end if;
5407 end if;
5408 end if;
5409
5410 -- Subtype of Boolean cannot have a constraint in SPARK
5411
5412 if Is_Boolean_Type (T)
5413 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5414 then
5415 Check_SPARK_05_Restriction
5416 ("subtype of Boolean cannot have constraint", N);
5417 end if;
5418
5419 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5420 declare
5421 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5422 One_Cstr : Node_Id;
5423 Low : Node_Id;
5424 High : Node_Id;
5425
5426 begin
5427 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5428 One_Cstr := First (Constraints (Cstr));
5429 while Present (One_Cstr) loop
5430
5431 -- Index or discriminant constraint in SPARK must be a
5432 -- subtype mark.
5433
5434 if not
5435 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5436 then
5437 Check_SPARK_05_Restriction
5438 ("subtype mark required", One_Cstr);
5439
5440 -- String subtype must have a lower bound of 1 in SPARK.
5441 -- Note that we do not need to test for the nonstatic case
5442 -- here, since that was already taken care of in
5443 -- Process_Range_Expr_In_Decl.
5444
5445 elsif Base_Type (T) = Standard_String then
5446 Get_Index_Bounds (One_Cstr, Low, High);
5447
5448 if Is_OK_Static_Expression (Low)
5449 and then Expr_Value (Low) /= 1
5450 then
5451 Check_SPARK_05_Restriction
5452 ("String subtype must have lower bound of 1", N);
5453 end if;
5454 end if;
5455
5456 Next (One_Cstr);
5457 end loop;
5458 end if;
5459 end;
5460 end if;
5461
5462 -- In the case where there is no constraint given in the subtype
5463 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5464 -- semantic attributes must be established here.
5465
5466 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5467 Set_Etype (Id, Base_Type (T));
5468
5469 -- Subtype of unconstrained array without constraint is not allowed
5470 -- in SPARK.
5471
5472 if Is_Array_Type (T) and then not Is_Constrained (T) then
5473 Check_SPARK_05_Restriction
5474 ("subtype of unconstrained array must have constraint", N);
5475 end if;
5476
5477 case Ekind (T) is
5478 when Array_Kind =>
5479 Set_Ekind (Id, E_Array_Subtype);
5480 Copy_Array_Subtype_Attributes (Id, T);
5481
5482 when Decimal_Fixed_Point_Kind =>
5483 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5484 Set_Digits_Value (Id, Digits_Value (T));
5485 Set_Delta_Value (Id, Delta_Value (T));
5486 Set_Scale_Value (Id, Scale_Value (T));
5487 Set_Small_Value (Id, Small_Value (T));
5488 Set_Scalar_Range (Id, Scalar_Range (T));
5489 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5490 Set_Is_Constrained (Id, Is_Constrained (T));
5491 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5492 Set_RM_Size (Id, RM_Size (T));
5493
5494 when Enumeration_Kind =>
5495 Set_Ekind (Id, E_Enumeration_Subtype);
5496 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5497 Set_Scalar_Range (Id, Scalar_Range (T));
5498 Set_Is_Character_Type (Id, Is_Character_Type (T));
5499 Set_Is_Constrained (Id, Is_Constrained (T));
5500 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5501 Set_RM_Size (Id, RM_Size (T));
5502
5503 when Ordinary_Fixed_Point_Kind =>
5504 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5505 Set_Scalar_Range (Id, Scalar_Range (T));
5506 Set_Small_Value (Id, Small_Value (T));
5507 Set_Delta_Value (Id, Delta_Value (T));
5508 Set_Is_Constrained (Id, Is_Constrained (T));
5509 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5510 Set_RM_Size (Id, RM_Size (T));
5511
5512 when Float_Kind =>
5513 Set_Ekind (Id, E_Floating_Point_Subtype);
5514 Set_Scalar_Range (Id, Scalar_Range (T));
5515 Set_Digits_Value (Id, Digits_Value (T));
5516 Set_Is_Constrained (Id, Is_Constrained (T));
5517
5518 -- If the floating point type has dimensions, these will be
5519 -- inherited subsequently when Analyze_Dimensions is called.
5520
5521 when Signed_Integer_Kind =>
5522 Set_Ekind (Id, E_Signed_Integer_Subtype);
5523 Set_Scalar_Range (Id, Scalar_Range (T));
5524 Set_Is_Constrained (Id, Is_Constrained (T));
5525 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5526 Set_RM_Size (Id, RM_Size (T));
5527
5528 when Modular_Integer_Kind =>
5529 Set_Ekind (Id, E_Modular_Integer_Subtype);
5530 Set_Scalar_Range (Id, Scalar_Range (T));
5531 Set_Is_Constrained (Id, Is_Constrained (T));
5532 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5533 Set_RM_Size (Id, RM_Size (T));
5534
5535 when Class_Wide_Kind =>
5536 Set_Ekind (Id, E_Class_Wide_Subtype);
5537 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5538 Set_Cloned_Subtype (Id, T);
5539 Set_Is_Tagged_Type (Id, True);
5540 Set_Has_Unknown_Discriminants
5541 (Id, True);
5542 Set_No_Tagged_Streams_Pragma
5543 (Id, No_Tagged_Streams_Pragma (T));
5544
5545 if Ekind (T) = E_Class_Wide_Subtype then
5546 Set_Equivalent_Type (Id, Equivalent_Type (T));
5547 end if;
5548
5549 when E_Record_Subtype
5550 | E_Record_Type
5551 =>
5552 Set_Ekind (Id, E_Record_Subtype);
5553
5554 if Ekind (T) = E_Record_Subtype
5555 and then Present (Cloned_Subtype (T))
5556 then
5557 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5558 else
5559 Set_Cloned_Subtype (Id, T);
5560 end if;
5561
5562 Set_First_Entity (Id, First_Entity (T));
5563 Set_Last_Entity (Id, Last_Entity (T));
5564 Set_Has_Discriminants (Id, Has_Discriminants (T));
5565 Set_Is_Constrained (Id, Is_Constrained (T));
5566 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5567 Set_Has_Implicit_Dereference
5568 (Id, Has_Implicit_Dereference (T));
5569 Set_Has_Unknown_Discriminants
5570 (Id, Has_Unknown_Discriminants (T));
5571
5572 if Has_Discriminants (T) then
5573 Set_Discriminant_Constraint
5574 (Id, Discriminant_Constraint (T));
5575 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5576
5577 elsif Has_Unknown_Discriminants (Id) then
5578 Set_Discriminant_Constraint (Id, No_Elist);
5579 end if;
5580
5581 if Is_Tagged_Type (T) then
5582 Set_Is_Tagged_Type (Id, True);
5583 Set_No_Tagged_Streams_Pragma
5584 (Id, No_Tagged_Streams_Pragma (T));
5585 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5586 Set_Direct_Primitive_Operations
5587 (Id, Direct_Primitive_Operations (T));
5588 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5589
5590 if Is_Interface (T) then
5591 Set_Is_Interface (Id);
5592 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5593 end if;
5594 end if;
5595
5596 when Private_Kind =>
5597 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5598 Set_Has_Discriminants (Id, Has_Discriminants (T));
5599 Set_Is_Constrained (Id, Is_Constrained (T));
5600 Set_First_Entity (Id, First_Entity (T));
5601 Set_Last_Entity (Id, Last_Entity (T));
5602 Set_Private_Dependents (Id, New_Elmt_List);
5603 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5604 Set_Has_Implicit_Dereference
5605 (Id, Has_Implicit_Dereference (T));
5606 Set_Has_Unknown_Discriminants
5607 (Id, Has_Unknown_Discriminants (T));
5608 Set_Known_To_Have_Preelab_Init
5609 (Id, Known_To_Have_Preelab_Init (T));
5610
5611 if Is_Tagged_Type (T) then
5612 Set_Is_Tagged_Type (Id);
5613 Set_No_Tagged_Streams_Pragma (Id,
5614 No_Tagged_Streams_Pragma (T));
5615 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5616 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5617 Set_Direct_Primitive_Operations (Id,
5618 Direct_Primitive_Operations (T));
5619 end if;
5620
5621 -- In general the attributes of the subtype of a private type
5622 -- are the attributes of the partial view of parent. However,
5623 -- the full view may be a discriminated type, and the subtype
5624 -- must share the discriminant constraint to generate correct
5625 -- calls to initialization procedures.
5626
5627 if Has_Discriminants (T) then
5628 Set_Discriminant_Constraint
5629 (Id, Discriminant_Constraint (T));
5630 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5631
5632 elsif Present (Full_View (T))
5633 and then Has_Discriminants (Full_View (T))
5634 then
5635 Set_Discriminant_Constraint
5636 (Id, Discriminant_Constraint (Full_View (T)));
5637 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5638
5639 -- This would seem semantically correct, but apparently
5640 -- generates spurious errors about missing components ???
5641
5642 -- Set_Has_Discriminants (Id);
5643 end if;
5644
5645 Prepare_Private_Subtype_Completion (Id, N);
5646
5647 -- If this is the subtype of a constrained private type with
5648 -- discriminants that has got a full view and we also have
5649 -- built a completion just above, show that the completion
5650 -- is a clone of the full view to the back-end.
5651
5652 if Has_Discriminants (T)
5653 and then not Has_Unknown_Discriminants (T)
5654 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5655 and then Present (Full_View (T))
5656 and then Present (Full_View (Id))
5657 then
5658 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5659 end if;
5660
5661 when Access_Kind =>
5662 Set_Ekind (Id, E_Access_Subtype);
5663 Set_Is_Constrained (Id, Is_Constrained (T));
5664 Set_Is_Access_Constant
5665 (Id, Is_Access_Constant (T));
5666 Set_Directly_Designated_Type
5667 (Id, Designated_Type (T));
5668 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5669
5670 -- A Pure library_item must not contain the declaration of a
5671 -- named access type, except within a subprogram, generic
5672 -- subprogram, task unit, or protected unit, or if it has
5673 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5674
5675 if Comes_From_Source (Id)
5676 and then In_Pure_Unit
5677 and then not In_Subprogram_Task_Protected_Unit
5678 and then not No_Pool_Assigned (Id)
5679 then
5680 Error_Msg_N
5681 ("named access types not allowed in pure unit", N);
5682 end if;
5683
5684 when Concurrent_Kind =>
5685 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5686 Set_Corresponding_Record_Type (Id,
5687 Corresponding_Record_Type (T));
5688 Set_First_Entity (Id, First_Entity (T));
5689 Set_First_Private_Entity (Id, First_Private_Entity (T));
5690 Set_Has_Discriminants (Id, Has_Discriminants (T));
5691 Set_Is_Constrained (Id, Is_Constrained (T));
5692 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5693 Set_Last_Entity (Id, Last_Entity (T));
5694
5695 if Is_Tagged_Type (T) then
5696 Set_No_Tagged_Streams_Pragma
5697 (Id, No_Tagged_Streams_Pragma (T));
5698 end if;
5699
5700 if Has_Discriminants (T) then
5701 Set_Discriminant_Constraint
5702 (Id, Discriminant_Constraint (T));
5703 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5704 end if;
5705
5706 when Incomplete_Kind =>
5707 if Ada_Version >= Ada_2005 then
5708
5709 -- In Ada 2005 an incomplete type can be explicitly tagged:
5710 -- propagate indication. Note that we also have to include
5711 -- subtypes for Ada 2012 extended use of incomplete types.
5712
5713 Set_Ekind (Id, E_Incomplete_Subtype);
5714 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5715 Set_Private_Dependents (Id, New_Elmt_List);
5716
5717 if Is_Tagged_Type (Id) then
5718 Set_No_Tagged_Streams_Pragma
5719 (Id, No_Tagged_Streams_Pragma (T));
5720 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5721 end if;
5722
5723 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5724 -- incomplete type visible through a limited with clause.
5725
5726 if From_Limited_With (T)
5727 and then Present (Non_Limited_View (T))
5728 then
5729 Set_From_Limited_With (Id);
5730 Set_Non_Limited_View (Id, Non_Limited_View (T));
5731
5732 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5733 -- to the private dependents of the original incomplete
5734 -- type for future transformation.
5735
5736 else
5737 Append_Elmt (Id, Private_Dependents (T));
5738 end if;
5739
5740 -- If the subtype name denotes an incomplete type an error
5741 -- was already reported by Process_Subtype.
5742
5743 else
5744 Set_Etype (Id, Any_Type);
5745 end if;
5746
5747 when others =>
5748 raise Program_Error;
5749 end case;
5750
5751 -- If there is no constraint in the subtype indication, the
5752 -- declared entity inherits predicates from the parent.
5753
5754 Inherit_Predicate_Flags (Id, T);
5755 end if;
5756
5757 if Etype (Id) = Any_Type then
5758 goto Leave;
5759 end if;
5760
5761 -- Some common processing on all types
5762
5763 Set_Size_Info (Id, T);
5764 Set_First_Rep_Item (Id, First_Rep_Item (T));
5765
5766 -- If the parent type is a generic actual, so is the subtype. This may
5767 -- happen in a nested instance. Why Comes_From_Source test???
5768
5769 if not Comes_From_Source (N) then
5770 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5771 end if;
5772
5773 -- If this is a subtype declaration for an actual in an instance,
5774 -- inherit static and dynamic predicates if any.
5775
5776 -- If declaration has no aspect specifications, inherit predicate
5777 -- info as well. Unclear how to handle the case of both specified
5778 -- and inherited predicates ??? Other inherited aspects, such as
5779 -- invariants, should be OK, but the combination with later pragmas
5780 -- may also require special merging.
5781
5782 if Has_Predicates (T)
5783 and then Present (Predicate_Function (T))
5784 and then
5785 ((In_Instance and then not Comes_From_Source (N))
5786 or else No (Aspect_Specifications (N)))
5787 then
5788 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5789
5790 if Has_Static_Predicate (T) then
5791 Set_Has_Static_Predicate (Id);
5792 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5793 end if;
5794 end if;
5795
5796 -- Remaining processing depends on characteristics of base type
5797
5798 T := Etype (Id);
5799
5800 Set_Is_Immediately_Visible (Id, True);
5801 Set_Depends_On_Private (Id, Has_Private_Component (T));
5802 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5803
5804 if Is_Interface (T) then
5805 Set_Is_Interface (Id);
5806 end if;
5807
5808 if Present (Generic_Parent_Type (N))
5809 and then
5810 (Nkind (Parent (Generic_Parent_Type (N))) /=
5811 N_Formal_Type_Declaration
5812 or else Nkind (Formal_Type_Definition
5813 (Parent (Generic_Parent_Type (N)))) /=
5814 N_Formal_Private_Type_Definition)
5815 then
5816 if Is_Tagged_Type (Id) then
5817
5818 -- If this is a generic actual subtype for a synchronized type,
5819 -- the primitive operations are those of the corresponding record
5820 -- for which there is a separate subtype declaration.
5821
5822 if Is_Concurrent_Type (Id) then
5823 null;
5824 elsif Is_Class_Wide_Type (Id) then
5825 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5826 else
5827 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5828 end if;
5829
5830 elsif Scope (Etype (Id)) /= Standard_Standard then
5831 Derive_Subprograms (Generic_Parent_Type (N), Id);
5832 end if;
5833 end if;
5834
5835 if Is_Private_Type (T) and then Present (Full_View (T)) then
5836 Conditional_Delay (Id, Full_View (T));
5837
5838 -- The subtypes of components or subcomponents of protected types
5839 -- do not need freeze nodes, which would otherwise appear in the
5840 -- wrong scope (before the freeze node for the protected type). The
5841 -- proper subtypes are those of the subcomponents of the corresponding
5842 -- record.
5843
5844 elsif Ekind (Scope (Id)) /= E_Protected_Type
5845 and then Present (Scope (Scope (Id))) -- error defense
5846 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5847 then
5848 Conditional_Delay (Id, T);
5849 end if;
5850
5851 -- If we have a subtype of an incomplete type whose full type is a
5852 -- derived numeric type, we need to have a freeze node for the subtype.
5853 -- Otherwise gigi will complain while computing the (static) bounds of
5854 -- the subtype.
5855
5856 if Is_Itype (T)
5857 and then Is_Elementary_Type (Id)
5858 and then Etype (Id) /= Id
5859 then
5860 declare
5861 Partial : constant Entity_Id :=
5862 Incomplete_Or_Partial_View (First_Subtype (Id));
5863 begin
5864 if Present (Partial)
5865 and then Ekind (Partial) = E_Incomplete_Type
5866 then
5867 Set_Has_Delayed_Freeze (Id);
5868 end if;
5869 end;
5870 end if;
5871
5872 -- Check that Constraint_Error is raised for a scalar subtype indication
5873 -- when the lower or upper bound of a non-null range lies outside the
5874 -- range of the type mark.
5875
5876 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5877 if Is_Scalar_Type (Etype (Id))
5878 and then Scalar_Range (Id) /=
5879 Scalar_Range
5880 (Etype (Subtype_Mark (Subtype_Indication (N))))
5881 then
5882 Apply_Range_Check
5883 (Scalar_Range (Id),
5884 Etype (Subtype_Mark (Subtype_Indication (N))));
5885
5886 -- In the array case, check compatibility for each index
5887
5888 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5889 then
5890 -- This really should be a subprogram that finds the indications
5891 -- to check???
5892
5893 declare
5894 Subt_Index : Node_Id := First_Index (Id);
5895 Target_Index : Node_Id :=
5896 First_Index (Etype
5897 (Subtype_Mark (Subtype_Indication (N))));
5898 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5899
5900 begin
5901 while Present (Subt_Index) loop
5902 if ((Nkind (Subt_Index) = N_Identifier
5903 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5904 or else Nkind (Subt_Index) = N_Subtype_Indication)
5905 and then
5906 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5907 then
5908 declare
5909 Target_Typ : constant Entity_Id :=
5910 Etype (Target_Index);
5911 begin
5912 R_Checks :=
5913 Get_Range_Checks
5914 (Scalar_Range (Etype (Subt_Index)),
5915 Target_Typ,
5916 Etype (Subt_Index),
5917 Defining_Identifier (N));
5918
5919 -- Reset Has_Dynamic_Range_Check on the subtype to
5920 -- prevent elision of the index check due to a dynamic
5921 -- check generated for a preceding index (needed since
5922 -- Insert_Range_Checks tries to avoid generating
5923 -- redundant checks on a given declaration).
5924
5925 Set_Has_Dynamic_Range_Check (N, False);
5926
5927 Insert_Range_Checks
5928 (R_Checks,
5929 N,
5930 Target_Typ,
5931 Sloc (Defining_Identifier (N)));
5932
5933 -- Record whether this index involved a dynamic check
5934
5935 Has_Dyn_Chk :=
5936 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5937 end;
5938 end if;
5939
5940 Next_Index (Subt_Index);
5941 Next_Index (Target_Index);
5942 end loop;
5943
5944 -- Finally, mark whether the subtype involves dynamic checks
5945
5946 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5947 end;
5948 end if;
5949 end if;
5950
5951 Set_Optimize_Alignment_Flags (Id);
5952 Check_Eliminated (Id);
5953
5954 <<Leave>>
5955 if Has_Aspects (N) then
5956 Analyze_Aspect_Specifications (N, Id);
5957 end if;
5958
5959 Analyze_Dimension (N);
5960
5961 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5962 -- indications on composite types where the constraints are dynamic.
5963 -- Note that object declarations and aggregates generate implicit
5964 -- subtype declarations, which this covers. One special case is that the
5965 -- implicitly generated "=" for discriminated types includes an
5966 -- offending subtype declaration, which is harmless, so we ignore it
5967 -- here.
5968
5969 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5970 declare
5971 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5972 begin
5973 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5974 and then not (Is_Internal (Id)
5975 and then Is_TSS (Scope (Id),
5976 TSS_Composite_Equality))
5977 and then not Within_Init_Proc
5978 and then not All_Composite_Constraints_Static (Cstr)
5979 then
5980 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5981 end if;
5982 end;
5983 end if;
5984 end Analyze_Subtype_Declaration;
5985
5986 --------------------------------
5987 -- Analyze_Subtype_Indication --
5988 --------------------------------
5989
5990 procedure Analyze_Subtype_Indication (N : Node_Id) is
5991 T : constant Entity_Id := Subtype_Mark (N);
5992 R : constant Node_Id := Range_Expression (Constraint (N));
5993
5994 begin
5995 Analyze (T);
5996
5997 if R /= Error then
5998 Analyze (R);
5999 Set_Etype (N, Etype (R));
6000 Resolve (R, Entity (T));
6001 else
6002 Set_Error_Posted (R);
6003 Set_Error_Posted (T);
6004 end if;
6005 end Analyze_Subtype_Indication;
6006
6007 --------------------------
6008 -- Analyze_Variant_Part --
6009 --------------------------
6010
6011 procedure Analyze_Variant_Part (N : Node_Id) is
6012 Discr_Name : Node_Id;
6013 Discr_Type : Entity_Id;
6014
6015 procedure Process_Variant (A : Node_Id);
6016 -- Analyze declarations for a single variant
6017
6018 package Analyze_Variant_Choices is
6019 new Generic_Analyze_Choices (Process_Variant);
6020 use Analyze_Variant_Choices;
6021
6022 ---------------------
6023 -- Process_Variant --
6024 ---------------------
6025
6026 procedure Process_Variant (A : Node_Id) is
6027 CL : constant Node_Id := Component_List (A);
6028 begin
6029 if not Null_Present (CL) then
6030 Analyze_Declarations (Component_Items (CL));
6031
6032 if Present (Variant_Part (CL)) then
6033 Analyze (Variant_Part (CL));
6034 end if;
6035 end if;
6036 end Process_Variant;
6037
6038 -- Start of processing for Analyze_Variant_Part
6039
6040 begin
6041 Discr_Name := Name (N);
6042 Analyze (Discr_Name);
6043
6044 -- If Discr_Name bad, get out (prevent cascaded errors)
6045
6046 if Etype (Discr_Name) = Any_Type then
6047 return;
6048 end if;
6049
6050 -- Check invalid discriminant in variant part
6051
6052 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6053 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6054 end if;
6055
6056 Discr_Type := Etype (Entity (Discr_Name));
6057
6058 if not Is_Discrete_Type (Discr_Type) then
6059 Error_Msg_N
6060 ("discriminant in a variant part must be of a discrete type",
6061 Name (N));
6062 return;
6063 end if;
6064
6065 -- Now analyze the choices, which also analyzes the declarations that
6066 -- are associated with each choice.
6067
6068 Analyze_Choices (Variants (N), Discr_Type);
6069
6070 -- Note: we used to instantiate and call Check_Choices here to check
6071 -- that the choices covered the discriminant, but it's too early to do
6072 -- that because of statically predicated subtypes, whose analysis may
6073 -- be deferred to their freeze point which may be as late as the freeze
6074 -- point of the containing record. So this call is now to be found in
6075 -- Freeze_Record_Declaration.
6076
6077 end Analyze_Variant_Part;
6078
6079 ----------------------------
6080 -- Array_Type_Declaration --
6081 ----------------------------
6082
6083 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6084 Component_Def : constant Node_Id := Component_Definition (Def);
6085 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6086 P : constant Node_Id := Parent (Def);
6087 Element_Type : Entity_Id;
6088 Implicit_Base : Entity_Id;
6089 Index : Node_Id;
6090 Nb_Index : Nat;
6091 Priv : Entity_Id;
6092 Related_Id : Entity_Id := Empty;
6093
6094 begin
6095 if Nkind (Def) = N_Constrained_Array_Definition then
6096 Index := First (Discrete_Subtype_Definitions (Def));
6097 else
6098 Index := First (Subtype_Marks (Def));
6099 end if;
6100
6101 -- Find proper names for the implicit types which may be public. In case
6102 -- of anonymous arrays we use the name of the first object of that type
6103 -- as prefix.
6104
6105 if No (T) then
6106 Related_Id := Defining_Identifier (P);
6107 else
6108 Related_Id := T;
6109 end if;
6110
6111 Nb_Index := 1;
6112 while Present (Index) loop
6113 Analyze (Index);
6114
6115 -- Test for odd case of trying to index a type by the type itself
6116
6117 if Is_Entity_Name (Index) and then Entity (Index) = T then
6118 Error_Msg_N ("type& cannot be indexed by itself", Index);
6119 Set_Entity (Index, Standard_Boolean);
6120 Set_Etype (Index, Standard_Boolean);
6121 end if;
6122
6123 -- Check SPARK restriction requiring a subtype mark
6124
6125 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6126 Check_SPARK_05_Restriction ("subtype mark required", Index);
6127 end if;
6128
6129 -- Add a subtype declaration for each index of private array type
6130 -- declaration whose etype is also private. For example:
6131
6132 -- package Pkg is
6133 -- type Index is private;
6134 -- private
6135 -- type Table is array (Index) of ...
6136 -- end;
6137
6138 -- This is currently required by the expander for the internally
6139 -- generated equality subprogram of records with variant parts in
6140 -- which the etype of some component is such private type.
6141
6142 if Ekind (Current_Scope) = E_Package
6143 and then In_Private_Part (Current_Scope)
6144 and then Has_Private_Declaration (Etype (Index))
6145 then
6146 declare
6147 Loc : constant Source_Ptr := Sloc (Def);
6148 Decl : Entity_Id;
6149 New_E : Entity_Id;
6150
6151 begin
6152 New_E := Make_Temporary (Loc, 'T');
6153 Set_Is_Internal (New_E);
6154
6155 Decl :=
6156 Make_Subtype_Declaration (Loc,
6157 Defining_Identifier => New_E,
6158 Subtype_Indication =>
6159 New_Occurrence_Of (Etype (Index), Loc));
6160
6161 Insert_Before (Parent (Def), Decl);
6162 Analyze (Decl);
6163 Set_Etype (Index, New_E);
6164
6165 -- If the index is a range or a subtype indication it carries
6166 -- no entity. Example:
6167
6168 -- package Pkg is
6169 -- type T is private;
6170 -- private
6171 -- type T is new Natural;
6172 -- Table : array (T(1) .. T(10)) of Boolean;
6173 -- end Pkg;
6174
6175 -- Otherwise the type of the reference is its entity.
6176
6177 if Is_Entity_Name (Index) then
6178 Set_Entity (Index, New_E);
6179 end if;
6180 end;
6181 end if;
6182
6183 Make_Index (Index, P, Related_Id, Nb_Index);
6184
6185 -- Check error of subtype with predicate for index type
6186
6187 Bad_Predicated_Subtype_Use
6188 ("subtype& has predicate, not allowed as index subtype",
6189 Index, Etype (Index));
6190
6191 -- Move to next index
6192
6193 Next_Index (Index);
6194 Nb_Index := Nb_Index + 1;
6195 end loop;
6196
6197 -- Process subtype indication if one is present
6198
6199 if Present (Component_Typ) then
6200 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6201
6202 Set_Etype (Component_Typ, Element_Type);
6203
6204 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6205 Check_SPARK_05_Restriction
6206 ("subtype mark required", Component_Typ);
6207 end if;
6208
6209 -- Ada 2005 (AI-230): Access Definition case
6210
6211 else pragma Assert (Present (Access_Definition (Component_Def)));
6212
6213 -- Indicate that the anonymous access type is created by the
6214 -- array type declaration.
6215
6216 Element_Type := Access_Definition
6217 (Related_Nod => P,
6218 N => Access_Definition (Component_Def));
6219 Set_Is_Local_Anonymous_Access (Element_Type);
6220
6221 -- Propagate the parent. This field is needed if we have to generate
6222 -- the master_id associated with an anonymous access to task type
6223 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6224
6225 Set_Parent (Element_Type, Parent (T));
6226
6227 -- Ada 2005 (AI-230): In case of components that are anonymous access
6228 -- types the level of accessibility depends on the enclosing type
6229 -- declaration
6230
6231 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6232
6233 -- Ada 2005 (AI-254)
6234
6235 declare
6236 CD : constant Node_Id :=
6237 Access_To_Subprogram_Definition
6238 (Access_Definition (Component_Def));
6239 begin
6240 if Present (CD) and then Protected_Present (CD) then
6241 Element_Type :=
6242 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6243 end if;
6244 end;
6245 end if;
6246
6247 -- Constrained array case
6248
6249 if No (T) then
6250 T := Create_Itype (E_Void, P, Related_Id, 'T');
6251 end if;
6252
6253 if Nkind (Def) = N_Constrained_Array_Definition then
6254
6255 -- Establish Implicit_Base as unconstrained base type
6256
6257 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6258
6259 Set_Etype (Implicit_Base, Implicit_Base);
6260 Set_Scope (Implicit_Base, Current_Scope);
6261 Set_Has_Delayed_Freeze (Implicit_Base);
6262 Set_Default_SSO (Implicit_Base);
6263
6264 -- The constrained array type is a subtype of the unconstrained one
6265
6266 Set_Ekind (T, E_Array_Subtype);
6267 Init_Size_Align (T);
6268 Set_Etype (T, Implicit_Base);
6269 Set_Scope (T, Current_Scope);
6270 Set_Is_Constrained (T);
6271 Set_First_Index (T,
6272 First (Discrete_Subtype_Definitions (Def)));
6273 Set_Has_Delayed_Freeze (T);
6274
6275 -- Complete setup of implicit base type
6276
6277 Set_Component_Size (Implicit_Base, Uint_0);
6278 Set_Component_Type (Implicit_Base, Element_Type);
6279 Set_Finalize_Storage_Only
6280 (Implicit_Base,
6281 Finalize_Storage_Only (Element_Type));
6282 Set_First_Index (Implicit_Base, First_Index (T));
6283 Set_Has_Controlled_Component
6284 (Implicit_Base,
6285 Has_Controlled_Component (Element_Type)
6286 or else Is_Controlled (Element_Type));
6287 Set_Packed_Array_Impl_Type
6288 (Implicit_Base, Empty);
6289
6290 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6291
6292 -- Unconstrained array case
6293
6294 else
6295 Set_Ekind (T, E_Array_Type);
6296 Init_Size_Align (T);
6297 Set_Etype (T, T);
6298 Set_Scope (T, Current_Scope);
6299 Set_Component_Size (T, Uint_0);
6300 Set_Is_Constrained (T, False);
6301 Set_First_Index (T, First (Subtype_Marks (Def)));
6302 Set_Has_Delayed_Freeze (T, True);
6303 Propagate_Concurrent_Flags (T, Element_Type);
6304 Set_Has_Controlled_Component (T, Has_Controlled_Component
6305 (Element_Type)
6306 or else
6307 Is_Controlled (Element_Type));
6308 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6309 (Element_Type));
6310 Set_Default_SSO (T);
6311 end if;
6312
6313 -- Common attributes for both cases
6314
6315 Set_Component_Type (Base_Type (T), Element_Type);
6316 Set_Packed_Array_Impl_Type (T, Empty);
6317
6318 if Aliased_Present (Component_Definition (Def)) then
6319 Check_SPARK_05_Restriction
6320 ("aliased is not allowed", Component_Definition (Def));
6321 Set_Has_Aliased_Components (Etype (T));
6322 end if;
6323
6324 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6325 -- array type to ensure that objects of this type are initialized.
6326
6327 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6328 Set_Can_Never_Be_Null (T);
6329
6330 if Null_Exclusion_Present (Component_Definition (Def))
6331
6332 -- No need to check itypes because in their case this check was
6333 -- done at their point of creation
6334
6335 and then not Is_Itype (Element_Type)
6336 then
6337 Error_Msg_N
6338 ("`NOT NULL` not allowed (null already excluded)",
6339 Subtype_Indication (Component_Definition (Def)));
6340 end if;
6341 end if;
6342
6343 Priv := Private_Component (Element_Type);
6344
6345 if Present (Priv) then
6346
6347 -- Check for circular definitions
6348
6349 if Priv = Any_Type then
6350 Set_Component_Type (Etype (T), Any_Type);
6351
6352 -- There is a gap in the visibility of operations on the composite
6353 -- type only if the component type is defined in a different scope.
6354
6355 elsif Scope (Priv) = Current_Scope then
6356 null;
6357
6358 elsif Is_Limited_Type (Priv) then
6359 Set_Is_Limited_Composite (Etype (T));
6360 Set_Is_Limited_Composite (T);
6361 else
6362 Set_Is_Private_Composite (Etype (T));
6363 Set_Is_Private_Composite (T);
6364 end if;
6365 end if;
6366
6367 -- A syntax error in the declaration itself may lead to an empty index
6368 -- list, in which case do a minimal patch.
6369
6370 if No (First_Index (T)) then
6371 Error_Msg_N ("missing index definition in array type declaration", T);
6372
6373 declare
6374 Indexes : constant List_Id :=
6375 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6376 begin
6377 Set_Discrete_Subtype_Definitions (Def, Indexes);
6378 Set_First_Index (T, First (Indexes));
6379 return;
6380 end;
6381 end if;
6382
6383 -- Create a concatenation operator for the new type. Internal array
6384 -- types created for packed entities do not need such, they are
6385 -- compatible with the user-defined type.
6386
6387 if Number_Dimensions (T) = 1
6388 and then not Is_Packed_Array_Impl_Type (T)
6389 then
6390 New_Concatenation_Op (T);
6391 end if;
6392
6393 -- In the case of an unconstrained array the parser has already verified
6394 -- that all the indexes are unconstrained but we still need to make sure
6395 -- that the element type is constrained.
6396
6397 if not Is_Definite_Subtype (Element_Type) then
6398 Error_Msg_N
6399 ("unconstrained element type in array declaration",
6400 Subtype_Indication (Component_Def));
6401
6402 elsif Is_Abstract_Type (Element_Type) then
6403 Error_Msg_N
6404 ("the type of a component cannot be abstract",
6405 Subtype_Indication (Component_Def));
6406 end if;
6407
6408 -- There may be an invariant declared for the component type, but
6409 -- the construction of the component invariant checking procedure
6410 -- takes place during expansion.
6411 end Array_Type_Declaration;
6412
6413 ------------------------------------------------------
6414 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6415 ------------------------------------------------------
6416
6417 function Replace_Anonymous_Access_To_Protected_Subprogram
6418 (N : Node_Id) return Entity_Id
6419 is
6420 Loc : constant Source_Ptr := Sloc (N);
6421
6422 Curr_Scope : constant Scope_Stack_Entry :=
6423 Scope_Stack.Table (Scope_Stack.Last);
6424
6425 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6426
6427 Acc : Node_Id;
6428 -- Access definition in declaration
6429
6430 Comp : Node_Id;
6431 -- Object definition or formal definition with an access definition
6432
6433 Decl : Node_Id;
6434 -- Declaration of anonymous access to subprogram type
6435
6436 Spec : Node_Id;
6437 -- Original specification in access to subprogram
6438
6439 P : Node_Id;
6440
6441 begin
6442 Set_Is_Internal (Anon);
6443
6444 case Nkind (N) is
6445 when N_Constrained_Array_Definition
6446 | N_Component_Declaration
6447 | N_Unconstrained_Array_Definition
6448 =>
6449 Comp := Component_Definition (N);
6450 Acc := Access_Definition (Comp);
6451
6452 when N_Discriminant_Specification =>
6453 Comp := Discriminant_Type (N);
6454 Acc := Comp;
6455
6456 when N_Parameter_Specification =>
6457 Comp := Parameter_Type (N);
6458 Acc := Comp;
6459
6460 when N_Access_Function_Definition =>
6461 Comp := Result_Definition (N);
6462 Acc := Comp;
6463
6464 when N_Object_Declaration =>
6465 Comp := Object_Definition (N);
6466 Acc := Comp;
6467
6468 when N_Function_Specification =>
6469 Comp := Result_Definition (N);
6470 Acc := Comp;
6471
6472 when others =>
6473 raise Program_Error;
6474 end case;
6475
6476 Spec := Access_To_Subprogram_Definition (Acc);
6477
6478 Decl :=
6479 Make_Full_Type_Declaration (Loc,
6480 Defining_Identifier => Anon,
6481 Type_Definition => Copy_Separate_Tree (Spec));
6482
6483 Mark_Rewrite_Insertion (Decl);
6484
6485 -- In ASIS mode, analyze the profile on the original node, because
6486 -- the separate copy does not provide enough links to recover the
6487 -- original tree. Analysis is limited to type annotations, within
6488 -- a temporary scope that serves as an anonymous subprogram to collect
6489 -- otherwise useless temporaries and itypes.
6490
6491 if ASIS_Mode then
6492 declare
6493 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6494
6495 begin
6496 if Nkind (Spec) = N_Access_Function_Definition then
6497 Set_Ekind (Typ, E_Function);
6498 else
6499 Set_Ekind (Typ, E_Procedure);
6500 end if;
6501
6502 Set_Parent (Typ, N);
6503 Set_Scope (Typ, Current_Scope);
6504 Push_Scope (Typ);
6505
6506 -- Nothing to do if procedure is parameterless
6507
6508 if Present (Parameter_Specifications (Spec)) then
6509 Process_Formals (Parameter_Specifications (Spec), Spec);
6510 end if;
6511
6512 if Nkind (Spec) = N_Access_Function_Definition then
6513 declare
6514 Def : constant Node_Id := Result_Definition (Spec);
6515
6516 begin
6517 -- The result might itself be an anonymous access type, so
6518 -- have to recurse.
6519
6520 if Nkind (Def) = N_Access_Definition then
6521 if Present (Access_To_Subprogram_Definition (Def)) then
6522 Set_Etype
6523 (Def,
6524 Replace_Anonymous_Access_To_Protected_Subprogram
6525 (Spec));
6526 else
6527 Find_Type (Subtype_Mark (Def));
6528 end if;
6529
6530 else
6531 Find_Type (Def);
6532 end if;
6533 end;
6534 end if;
6535
6536 End_Scope;
6537 end;
6538 end if;
6539
6540 -- Insert the new declaration in the nearest enclosing scope. If the
6541 -- parent is a body and N is its return type, the declaration belongs
6542 -- in the enclosing scope. Likewise if N is the type of a parameter.
6543
6544 P := Parent (N);
6545
6546 if Nkind (N) = N_Function_Specification
6547 and then Nkind (P) = N_Subprogram_Body
6548 then
6549 P := Parent (P);
6550 elsif Nkind (N) = N_Parameter_Specification
6551 and then Nkind (P) in N_Subprogram_Specification
6552 and then Nkind (Parent (P)) = N_Subprogram_Body
6553 then
6554 P := Parent (Parent (P));
6555 end if;
6556
6557 while Present (P) and then not Has_Declarations (P) loop
6558 P := Parent (P);
6559 end loop;
6560
6561 pragma Assert (Present (P));
6562
6563 if Nkind (P) = N_Package_Specification then
6564 Prepend (Decl, Visible_Declarations (P));
6565 else
6566 Prepend (Decl, Declarations (P));
6567 end if;
6568
6569 -- Replace the anonymous type with an occurrence of the new declaration.
6570 -- In all cases the rewritten node does not have the null-exclusion
6571 -- attribute because (if present) it was already inherited by the
6572 -- anonymous entity (Anon). Thus, in case of components we do not
6573 -- inherit this attribute.
6574
6575 if Nkind (N) = N_Parameter_Specification then
6576 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6577 Set_Etype (Defining_Identifier (N), Anon);
6578 Set_Null_Exclusion_Present (N, False);
6579
6580 elsif Nkind (N) = N_Object_Declaration then
6581 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6582 Set_Etype (Defining_Identifier (N), Anon);
6583
6584 elsif Nkind (N) = N_Access_Function_Definition then
6585 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6586
6587 elsif Nkind (N) = N_Function_Specification then
6588 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6589 Set_Etype (Defining_Unit_Name (N), Anon);
6590
6591 else
6592 Rewrite (Comp,
6593 Make_Component_Definition (Loc,
6594 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6595 end if;
6596
6597 Mark_Rewrite_Insertion (Comp);
6598
6599 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6600 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6601 and then not Is_Type (Current_Scope))
6602 then
6603
6604 -- Declaration can be analyzed in the current scope.
6605
6606 Analyze (Decl);
6607
6608 else
6609 -- Temporarily remove the current scope (record or subprogram) from
6610 -- the stack to add the new declarations to the enclosing scope.
6611 -- The anonymous entity is an Itype with the proper attributes.
6612
6613 Scope_Stack.Decrement_Last;
6614 Analyze (Decl);
6615 Set_Is_Itype (Anon);
6616 Set_Associated_Node_For_Itype (Anon, N);
6617 Scope_Stack.Append (Curr_Scope);
6618 end if;
6619
6620 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6621 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6622 return Anon;
6623 end Replace_Anonymous_Access_To_Protected_Subprogram;
6624
6625 -------------------------------
6626 -- Build_Derived_Access_Type --
6627 -------------------------------
6628
6629 procedure Build_Derived_Access_Type
6630 (N : Node_Id;
6631 Parent_Type : Entity_Id;
6632 Derived_Type : Entity_Id)
6633 is
6634 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6635
6636 Desig_Type : Entity_Id;
6637 Discr : Entity_Id;
6638 Discr_Con_Elist : Elist_Id;
6639 Discr_Con_El : Elmt_Id;
6640 Subt : Entity_Id;
6641
6642 begin
6643 -- Set the designated type so it is available in case this is an access
6644 -- to a self-referential type, e.g. a standard list type with a next
6645 -- pointer. Will be reset after subtype is built.
6646
6647 Set_Directly_Designated_Type
6648 (Derived_Type, Designated_Type (Parent_Type));
6649
6650 Subt := Process_Subtype (S, N);
6651
6652 if Nkind (S) /= N_Subtype_Indication
6653 and then Subt /= Base_Type (Subt)
6654 then
6655 Set_Ekind (Derived_Type, E_Access_Subtype);
6656 end if;
6657
6658 if Ekind (Derived_Type) = E_Access_Subtype then
6659 declare
6660 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6661 Ibase : constant Entity_Id :=
6662 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6663 Svg_Chars : constant Name_Id := Chars (Ibase);
6664 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6665 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6666
6667 begin
6668 Copy_Node (Pbase, Ibase);
6669
6670 -- Restore Itype status after Copy_Node
6671
6672 Set_Is_Itype (Ibase);
6673 Set_Associated_Node_For_Itype (Ibase, N);
6674
6675 Set_Chars (Ibase, Svg_Chars);
6676 Set_Prev_Entity (Ibase, Svg_Prev_E);
6677 Set_Next_Entity (Ibase, Svg_Next_E);
6678 Set_Sloc (Ibase, Sloc (Derived_Type));
6679 Set_Scope (Ibase, Scope (Derived_Type));
6680 Set_Freeze_Node (Ibase, Empty);
6681 Set_Is_Frozen (Ibase, False);
6682 Set_Comes_From_Source (Ibase, False);
6683 Set_Is_First_Subtype (Ibase, False);
6684
6685 Set_Etype (Ibase, Pbase);
6686 Set_Etype (Derived_Type, Ibase);
6687 end;
6688 end if;
6689
6690 Set_Directly_Designated_Type
6691 (Derived_Type, Designated_Type (Subt));
6692
6693 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6694 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6695 Set_Size_Info (Derived_Type, Parent_Type);
6696 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6697 Set_Depends_On_Private (Derived_Type,
6698 Has_Private_Component (Derived_Type));
6699 Conditional_Delay (Derived_Type, Subt);
6700
6701 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6702 -- that it is not redundant.
6703
6704 if Null_Exclusion_Present (Type_Definition (N)) then
6705 Set_Can_Never_Be_Null (Derived_Type);
6706
6707 elsif Can_Never_Be_Null (Parent_Type) then
6708 Set_Can_Never_Be_Null (Derived_Type);
6709 end if;
6710
6711 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6712 -- the root type for this information.
6713
6714 -- Apply range checks to discriminants for derived record case
6715 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6716
6717 Desig_Type := Designated_Type (Derived_Type);
6718
6719 if Is_Composite_Type (Desig_Type)
6720 and then (not Is_Array_Type (Desig_Type))
6721 and then Has_Discriminants (Desig_Type)
6722 and then Base_Type (Desig_Type) /= Desig_Type
6723 then
6724 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6725 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6726
6727 Discr := First_Discriminant (Base_Type (Desig_Type));
6728 while Present (Discr_Con_El) loop
6729 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6730 Next_Elmt (Discr_Con_El);
6731 Next_Discriminant (Discr);
6732 end loop;
6733 end if;
6734 end Build_Derived_Access_Type;
6735
6736 ------------------------------
6737 -- Build_Derived_Array_Type --
6738 ------------------------------
6739
6740 procedure Build_Derived_Array_Type
6741 (N : Node_Id;
6742 Parent_Type : Entity_Id;
6743 Derived_Type : Entity_Id)
6744 is
6745 Loc : constant Source_Ptr := Sloc (N);
6746 Tdef : constant Node_Id := Type_Definition (N);
6747 Indic : constant Node_Id := Subtype_Indication (Tdef);
6748 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6749 Implicit_Base : Entity_Id := Empty;
6750 New_Indic : Node_Id;
6751
6752 procedure Make_Implicit_Base;
6753 -- If the parent subtype is constrained, the derived type is a subtype
6754 -- of an implicit base type derived from the parent base.
6755
6756 ------------------------
6757 -- Make_Implicit_Base --
6758 ------------------------
6759
6760 procedure Make_Implicit_Base is
6761 begin
6762 Implicit_Base :=
6763 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6764
6765 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6766 Set_Etype (Implicit_Base, Parent_Base);
6767
6768 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6769 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6770
6771 Set_Has_Delayed_Freeze (Implicit_Base, True);
6772 end Make_Implicit_Base;
6773
6774 -- Start of processing for Build_Derived_Array_Type
6775
6776 begin
6777 if not Is_Constrained (Parent_Type) then
6778 if Nkind (Indic) /= N_Subtype_Indication then
6779 Set_Ekind (Derived_Type, E_Array_Type);
6780
6781 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6782 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6783
6784 Set_Has_Delayed_Freeze (Derived_Type, True);
6785
6786 else
6787 Make_Implicit_Base;
6788 Set_Etype (Derived_Type, Implicit_Base);
6789
6790 New_Indic :=
6791 Make_Subtype_Declaration (Loc,
6792 Defining_Identifier => Derived_Type,
6793 Subtype_Indication =>
6794 Make_Subtype_Indication (Loc,
6795 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6796 Constraint => Constraint (Indic)));
6797
6798 Rewrite (N, New_Indic);
6799 Analyze (N);
6800 end if;
6801
6802 else
6803 if Nkind (Indic) /= N_Subtype_Indication then
6804 Make_Implicit_Base;
6805
6806 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6807 Set_Etype (Derived_Type, Implicit_Base);
6808 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6809
6810 else
6811 Error_Msg_N ("illegal constraint on constrained type", Indic);
6812 end if;
6813 end if;
6814
6815 -- If parent type is not a derived type itself, and is declared in
6816 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6817 -- the new type's concatenation operator since Derive_Subprograms
6818 -- will not inherit the parent's operator. If the parent type is
6819 -- unconstrained, the operator is of the unconstrained base type.
6820
6821 if Number_Dimensions (Parent_Type) = 1
6822 and then not Is_Limited_Type (Parent_Type)
6823 and then not Is_Derived_Type (Parent_Type)
6824 and then not Is_Package_Or_Generic_Package
6825 (Scope (Base_Type (Parent_Type)))
6826 then
6827 if not Is_Constrained (Parent_Type)
6828 and then Is_Constrained (Derived_Type)
6829 then
6830 New_Concatenation_Op (Implicit_Base);
6831 else
6832 New_Concatenation_Op (Derived_Type);
6833 end if;
6834 end if;
6835 end Build_Derived_Array_Type;
6836
6837 -----------------------------------
6838 -- Build_Derived_Concurrent_Type --
6839 -----------------------------------
6840
6841 procedure Build_Derived_Concurrent_Type
6842 (N : Node_Id;
6843 Parent_Type : Entity_Id;
6844 Derived_Type : Entity_Id)
6845 is
6846 Loc : constant Source_Ptr := Sloc (N);
6847
6848 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6849 Corr_Decl : Node_Id;
6850 Corr_Decl_Needed : Boolean;
6851 -- If the derived type has fewer discriminants than its parent, the
6852 -- corresponding record is also a derived type, in order to account for
6853 -- the bound discriminants. We create a full type declaration for it in
6854 -- this case.
6855
6856 Constraint_Present : constant Boolean :=
6857 Nkind (Subtype_Indication (Type_Definition (N))) =
6858 N_Subtype_Indication;
6859
6860 D_Constraint : Node_Id;
6861 New_Constraint : Elist_Id := No_Elist;
6862 Old_Disc : Entity_Id;
6863 New_Disc : Entity_Id;
6864 New_N : Node_Id;
6865
6866 begin
6867 Set_Stored_Constraint (Derived_Type, No_Elist);
6868 Corr_Decl_Needed := False;
6869 Old_Disc := Empty;
6870
6871 if Present (Discriminant_Specifications (N))
6872 and then Constraint_Present
6873 then
6874 Old_Disc := First_Discriminant (Parent_Type);
6875 New_Disc := First (Discriminant_Specifications (N));
6876 while Present (New_Disc) and then Present (Old_Disc) loop
6877 Next_Discriminant (Old_Disc);
6878 Next (New_Disc);
6879 end loop;
6880 end if;
6881
6882 if Present (Old_Disc) and then Expander_Active then
6883
6884 -- The new type has fewer discriminants, so we need to create a new
6885 -- corresponding record, which is derived from the corresponding
6886 -- record of the parent, and has a stored constraint that captures
6887 -- the values of the discriminant constraints. The corresponding
6888 -- record is needed only if expander is active and code generation is
6889 -- enabled.
6890
6891 -- The type declaration for the derived corresponding record has the
6892 -- same discriminant part and constraints as the current declaration.
6893 -- Copy the unanalyzed tree to build declaration.
6894
6895 Corr_Decl_Needed := True;
6896 New_N := Copy_Separate_Tree (N);
6897
6898 Corr_Decl :=
6899 Make_Full_Type_Declaration (Loc,
6900 Defining_Identifier => Corr_Record,
6901 Discriminant_Specifications =>
6902 Discriminant_Specifications (New_N),
6903 Type_Definition =>
6904 Make_Derived_Type_Definition (Loc,
6905 Subtype_Indication =>
6906 Make_Subtype_Indication (Loc,
6907 Subtype_Mark =>
6908 New_Occurrence_Of
6909 (Corresponding_Record_Type (Parent_Type), Loc),
6910 Constraint =>
6911 Constraint
6912 (Subtype_Indication (Type_Definition (New_N))))));
6913 end if;
6914
6915 -- Copy Storage_Size and Relative_Deadline variables if task case
6916
6917 if Is_Task_Type (Parent_Type) then
6918 Set_Storage_Size_Variable (Derived_Type,
6919 Storage_Size_Variable (Parent_Type));
6920 Set_Relative_Deadline_Variable (Derived_Type,
6921 Relative_Deadline_Variable (Parent_Type));
6922 end if;
6923
6924 if Present (Discriminant_Specifications (N)) then
6925 Push_Scope (Derived_Type);
6926 Check_Or_Process_Discriminants (N, Derived_Type);
6927
6928 if Constraint_Present then
6929 New_Constraint :=
6930 Expand_To_Stored_Constraint
6931 (Parent_Type,
6932 Build_Discriminant_Constraints
6933 (Parent_Type,
6934 Subtype_Indication (Type_Definition (N)), True));
6935 end if;
6936
6937 End_Scope;
6938
6939 elsif Constraint_Present then
6940
6941 -- Build constrained subtype, copying the constraint, and derive
6942 -- from it to create a derived constrained type.
6943
6944 declare
6945 Loc : constant Source_Ptr := Sloc (N);
6946 Anon : constant Entity_Id :=
6947 Make_Defining_Identifier (Loc,
6948 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6949 Decl : Node_Id;
6950
6951 begin
6952 Decl :=
6953 Make_Subtype_Declaration (Loc,
6954 Defining_Identifier => Anon,
6955 Subtype_Indication =>
6956 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6957 Insert_Before (N, Decl);
6958 Analyze (Decl);
6959
6960 Rewrite (Subtype_Indication (Type_Definition (N)),
6961 New_Occurrence_Of (Anon, Loc));
6962 Set_Analyzed (Derived_Type, False);
6963 Analyze (N);
6964 return;
6965 end;
6966 end if;
6967
6968 -- By default, operations and private data are inherited from parent.
6969 -- However, in the presence of bound discriminants, a new corresponding
6970 -- record will be created, see below.
6971
6972 Set_Has_Discriminants
6973 (Derived_Type, Has_Discriminants (Parent_Type));
6974 Set_Corresponding_Record_Type
6975 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6976
6977 -- Is_Constrained is set according the parent subtype, but is set to
6978 -- False if the derived type is declared with new discriminants.
6979
6980 Set_Is_Constrained
6981 (Derived_Type,
6982 (Is_Constrained (Parent_Type) or else Constraint_Present)
6983 and then not Present (Discriminant_Specifications (N)));
6984
6985 if Constraint_Present then
6986 if not Has_Discriminants (Parent_Type) then
6987 Error_Msg_N ("untagged parent must have discriminants", N);
6988
6989 elsif Present (Discriminant_Specifications (N)) then
6990
6991 -- Verify that new discriminants are used to constrain old ones
6992
6993 D_Constraint :=
6994 First
6995 (Constraints
6996 (Constraint (Subtype_Indication (Type_Definition (N)))));
6997
6998 Old_Disc := First_Discriminant (Parent_Type);
6999
7000 while Present (D_Constraint) loop
7001 if Nkind (D_Constraint) /= N_Discriminant_Association then
7002
7003 -- Positional constraint. If it is a reference to a new
7004 -- discriminant, it constrains the corresponding old one.
7005
7006 if Nkind (D_Constraint) = N_Identifier then
7007 New_Disc := First_Discriminant (Derived_Type);
7008 while Present (New_Disc) loop
7009 exit when Chars (New_Disc) = Chars (D_Constraint);
7010 Next_Discriminant (New_Disc);
7011 end loop;
7012
7013 if Present (New_Disc) then
7014 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7015 end if;
7016 end if;
7017
7018 Next_Discriminant (Old_Disc);
7019
7020 -- if this is a named constraint, search by name for the old
7021 -- discriminants constrained by the new one.
7022
7023 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7024
7025 -- Find new discriminant with that name
7026
7027 New_Disc := First_Discriminant (Derived_Type);
7028 while Present (New_Disc) loop
7029 exit when
7030 Chars (New_Disc) = Chars (Expression (D_Constraint));
7031 Next_Discriminant (New_Disc);
7032 end loop;
7033
7034 if Present (New_Disc) then
7035
7036 -- Verify that new discriminant renames some discriminant
7037 -- of the parent type, and associate the new discriminant
7038 -- with one or more old ones that it renames.
7039
7040 declare
7041 Selector : Node_Id;
7042
7043 begin
7044 Selector := First (Selector_Names (D_Constraint));
7045 while Present (Selector) loop
7046 Old_Disc := First_Discriminant (Parent_Type);
7047 while Present (Old_Disc) loop
7048 exit when Chars (Old_Disc) = Chars (Selector);
7049 Next_Discriminant (Old_Disc);
7050 end loop;
7051
7052 if Present (Old_Disc) then
7053 Set_Corresponding_Discriminant
7054 (New_Disc, Old_Disc);
7055 end if;
7056
7057 Next (Selector);
7058 end loop;
7059 end;
7060 end if;
7061 end if;
7062
7063 Next (D_Constraint);
7064 end loop;
7065
7066 New_Disc := First_Discriminant (Derived_Type);
7067 while Present (New_Disc) loop
7068 if No (Corresponding_Discriminant (New_Disc)) then
7069 Error_Msg_NE
7070 ("new discriminant& must constrain old one", N, New_Disc);
7071
7072 elsif not
7073 Subtypes_Statically_Compatible
7074 (Etype (New_Disc),
7075 Etype (Corresponding_Discriminant (New_Disc)))
7076 then
7077 Error_Msg_NE
7078 ("& not statically compatible with parent discriminant",
7079 N, New_Disc);
7080 end if;
7081
7082 Next_Discriminant (New_Disc);
7083 end loop;
7084 end if;
7085
7086 elsif Present (Discriminant_Specifications (N)) then
7087 Error_Msg_N
7088 ("missing discriminant constraint in untagged derivation", N);
7089 end if;
7090
7091 -- The entity chain of the derived type includes the new discriminants
7092 -- but shares operations with the parent.
7093
7094 if Present (Discriminant_Specifications (N)) then
7095 Old_Disc := First_Discriminant (Parent_Type);
7096 while Present (Old_Disc) loop
7097 if No (Next_Entity (Old_Disc))
7098 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7099 then
7100 Link_Entities
7101 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7102 exit;
7103 end if;
7104
7105 Next_Discriminant (Old_Disc);
7106 end loop;
7107
7108 else
7109 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7110 if Has_Discriminants (Parent_Type) then
7111 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7112 Set_Discriminant_Constraint (
7113 Derived_Type, Discriminant_Constraint (Parent_Type));
7114 end if;
7115 end if;
7116
7117 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7118
7119 Set_Has_Completion (Derived_Type);
7120
7121 if Corr_Decl_Needed then
7122 Set_Stored_Constraint (Derived_Type, New_Constraint);
7123 Insert_After (N, Corr_Decl);
7124 Analyze (Corr_Decl);
7125 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7126 end if;
7127 end Build_Derived_Concurrent_Type;
7128
7129 ------------------------------------
7130 -- Build_Derived_Enumeration_Type --
7131 ------------------------------------
7132
7133 procedure Build_Derived_Enumeration_Type
7134 (N : Node_Id;
7135 Parent_Type : Entity_Id;
7136 Derived_Type : Entity_Id)
7137 is
7138 Loc : constant Source_Ptr := Sloc (N);
7139 Def : constant Node_Id := Type_Definition (N);
7140 Indic : constant Node_Id := Subtype_Indication (Def);
7141 Implicit_Base : Entity_Id;
7142 Literal : Entity_Id;
7143 New_Lit : Entity_Id;
7144 Literals_List : List_Id;
7145 Type_Decl : Node_Id;
7146 Hi, Lo : Node_Id;
7147 Rang_Expr : Node_Id;
7148
7149 begin
7150 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7151 -- not have explicit literals lists we need to process types derived
7152 -- from them specially. This is handled by Derived_Standard_Character.
7153 -- If the parent type is a generic type, there are no literals either,
7154 -- and we construct the same skeletal representation as for the generic
7155 -- parent type.
7156
7157 if Is_Standard_Character_Type (Parent_Type) then
7158 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7159
7160 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7161 declare
7162 Lo : Node_Id;
7163 Hi : Node_Id;
7164
7165 begin
7166 if Nkind (Indic) /= N_Subtype_Indication then
7167 Lo :=
7168 Make_Attribute_Reference (Loc,
7169 Attribute_Name => Name_First,
7170 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7171 Set_Etype (Lo, Derived_Type);
7172
7173 Hi :=
7174 Make_Attribute_Reference (Loc,
7175 Attribute_Name => Name_Last,
7176 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7177 Set_Etype (Hi, Derived_Type);
7178
7179 Set_Scalar_Range (Derived_Type,
7180 Make_Range (Loc,
7181 Low_Bound => Lo,
7182 High_Bound => Hi));
7183 else
7184
7185 -- Analyze subtype indication and verify compatibility
7186 -- with parent type.
7187
7188 if Base_Type (Process_Subtype (Indic, N)) /=
7189 Base_Type (Parent_Type)
7190 then
7191 Error_Msg_N
7192 ("illegal constraint for formal discrete type", N);
7193 end if;
7194 end if;
7195 end;
7196
7197 else
7198 -- If a constraint is present, analyze the bounds to catch
7199 -- premature usage of the derived literals.
7200
7201 if Nkind (Indic) = N_Subtype_Indication
7202 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7203 then
7204 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7205 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7206 end if;
7207
7208 -- Introduce an implicit base type for the derived type even if there
7209 -- is no constraint attached to it, since this seems closer to the
7210 -- Ada semantics. Build a full type declaration tree for the derived
7211 -- type using the implicit base type as the defining identifier. The
7212 -- build a subtype declaration tree which applies the constraint (if
7213 -- any) have it replace the derived type declaration.
7214
7215 Literal := First_Literal (Parent_Type);
7216 Literals_List := New_List;
7217 while Present (Literal)
7218 and then Ekind (Literal) = E_Enumeration_Literal
7219 loop
7220 -- Literals of the derived type have the same representation as
7221 -- those of the parent type, but this representation can be
7222 -- overridden by an explicit representation clause. Indicate
7223 -- that there is no explicit representation given yet. These
7224 -- derived literals are implicit operations of the new type,
7225 -- and can be overridden by explicit ones.
7226
7227 if Nkind (Literal) = N_Defining_Character_Literal then
7228 New_Lit :=
7229 Make_Defining_Character_Literal (Loc, Chars (Literal));
7230 else
7231 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7232 end if;
7233
7234 Set_Ekind (New_Lit, E_Enumeration_Literal);
7235 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7236 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7237 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7238 Set_Alias (New_Lit, Literal);
7239 Set_Is_Known_Valid (New_Lit, True);
7240
7241 Append (New_Lit, Literals_List);
7242 Next_Literal (Literal);
7243 end loop;
7244
7245 Implicit_Base :=
7246 Make_Defining_Identifier (Sloc (Derived_Type),
7247 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7248
7249 -- Indicate the proper nature of the derived type. This must be done
7250 -- before analysis of the literals, to recognize cases when a literal
7251 -- may be hidden by a previous explicit function definition (cf.
7252 -- c83031a).
7253
7254 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7255 Set_Etype (Derived_Type, Implicit_Base);
7256
7257 Type_Decl :=
7258 Make_Full_Type_Declaration (Loc,
7259 Defining_Identifier => Implicit_Base,
7260 Discriminant_Specifications => No_List,
7261 Type_Definition =>
7262 Make_Enumeration_Type_Definition (Loc, Literals_List));
7263
7264 Mark_Rewrite_Insertion (Type_Decl);
7265 Insert_Before (N, Type_Decl);
7266 Analyze (Type_Decl);
7267
7268 -- The anonymous base now has a full declaration, but this base
7269 -- is not a first subtype.
7270
7271 Set_Is_First_Subtype (Implicit_Base, False);
7272
7273 -- After the implicit base is analyzed its Etype needs to be changed
7274 -- to reflect the fact that it is derived from the parent type which
7275 -- was ignored during analysis. We also set the size at this point.
7276
7277 Set_Etype (Implicit_Base, Parent_Type);
7278
7279 Set_Size_Info (Implicit_Base, Parent_Type);
7280 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7281 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7282
7283 -- Copy other flags from parent type
7284
7285 Set_Has_Non_Standard_Rep
7286 (Implicit_Base, Has_Non_Standard_Rep
7287 (Parent_Type));
7288 Set_Has_Pragma_Ordered
7289 (Implicit_Base, Has_Pragma_Ordered
7290 (Parent_Type));
7291 Set_Has_Delayed_Freeze (Implicit_Base);
7292
7293 -- Process the subtype indication including a validation check on the
7294 -- constraint, if any. If a constraint is given, its bounds must be
7295 -- implicitly converted to the new type.
7296
7297 if Nkind (Indic) = N_Subtype_Indication then
7298 declare
7299 R : constant Node_Id :=
7300 Range_Expression (Constraint (Indic));
7301
7302 begin
7303 if Nkind (R) = N_Range then
7304 Hi := Build_Scalar_Bound
7305 (High_Bound (R), Parent_Type, Implicit_Base);
7306 Lo := Build_Scalar_Bound
7307 (Low_Bound (R), Parent_Type, Implicit_Base);
7308
7309 else
7310 -- Constraint is a Range attribute. Replace with explicit
7311 -- mention of the bounds of the prefix, which must be a
7312 -- subtype.
7313
7314 Analyze (Prefix (R));
7315 Hi :=
7316 Convert_To (Implicit_Base,
7317 Make_Attribute_Reference (Loc,
7318 Attribute_Name => Name_Last,
7319 Prefix =>
7320 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7321
7322 Lo :=
7323 Convert_To (Implicit_Base,
7324 Make_Attribute_Reference (Loc,
7325 Attribute_Name => Name_First,
7326 Prefix =>
7327 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7328 end if;
7329 end;
7330
7331 else
7332 Hi :=
7333 Build_Scalar_Bound
7334 (Type_High_Bound (Parent_Type),
7335 Parent_Type, Implicit_Base);
7336 Lo :=
7337 Build_Scalar_Bound
7338 (Type_Low_Bound (Parent_Type),
7339 Parent_Type, Implicit_Base);
7340 end if;
7341
7342 Rang_Expr :=
7343 Make_Range (Loc,
7344 Low_Bound => Lo,
7345 High_Bound => Hi);
7346
7347 -- If we constructed a default range for the case where no range
7348 -- was given, then the expressions in the range must not freeze
7349 -- since they do not correspond to expressions in the source.
7350 -- However, if the type inherits predicates the expressions will
7351 -- be elaborated earlier and must freeze.
7352
7353 if Nkind (Indic) /= N_Subtype_Indication
7354 and then not Has_Predicates (Derived_Type)
7355 then
7356 Set_Must_Not_Freeze (Lo);
7357 Set_Must_Not_Freeze (Hi);
7358 Set_Must_Not_Freeze (Rang_Expr);
7359 end if;
7360
7361 Rewrite (N,
7362 Make_Subtype_Declaration (Loc,
7363 Defining_Identifier => Derived_Type,
7364 Subtype_Indication =>
7365 Make_Subtype_Indication (Loc,
7366 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7367 Constraint =>
7368 Make_Range_Constraint (Loc,
7369 Range_Expression => Rang_Expr))));
7370
7371 Analyze (N);
7372
7373 -- Propagate the aspects from the original type declaration to the
7374 -- declaration of the implicit base.
7375
7376 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7377
7378 -- Apply a range check. Since this range expression doesn't have an
7379 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7380 -- this right???
7381
7382 if Nkind (Indic) = N_Subtype_Indication then
7383 Apply_Range_Check
7384 (Range_Expression (Constraint (Indic)), Parent_Type,
7385 Source_Typ => Entity (Subtype_Mark (Indic)));
7386 end if;
7387 end if;
7388 end Build_Derived_Enumeration_Type;
7389
7390 --------------------------------
7391 -- Build_Derived_Numeric_Type --
7392 --------------------------------
7393
7394 procedure Build_Derived_Numeric_Type
7395 (N : Node_Id;
7396 Parent_Type : Entity_Id;
7397 Derived_Type : Entity_Id)
7398 is
7399 Loc : constant Source_Ptr := Sloc (N);
7400 Tdef : constant Node_Id := Type_Definition (N);
7401 Indic : constant Node_Id := Subtype_Indication (Tdef);
7402 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7403 No_Constraint : constant Boolean := Nkind (Indic) /=
7404 N_Subtype_Indication;
7405 Implicit_Base : Entity_Id;
7406
7407 Lo : Node_Id;
7408 Hi : Node_Id;
7409
7410 begin
7411 -- Process the subtype indication including a validation check on
7412 -- the constraint if any.
7413
7414 Discard_Node (Process_Subtype (Indic, N));
7415
7416 -- Introduce an implicit base type for the derived type even if there
7417 -- is no constraint attached to it, since this seems closer to the Ada
7418 -- semantics.
7419
7420 Implicit_Base :=
7421 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7422
7423 Set_Etype (Implicit_Base, Parent_Base);
7424 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7425 Set_Size_Info (Implicit_Base, Parent_Base);
7426 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7427 Set_Parent (Implicit_Base, Parent (Derived_Type));
7428 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7429
7430 -- Set RM Size for discrete type or decimal fixed-point type
7431 -- Ordinary fixed-point is excluded, why???
7432
7433 if Is_Discrete_Type (Parent_Base)
7434 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7435 then
7436 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7437 end if;
7438
7439 Set_Has_Delayed_Freeze (Implicit_Base);
7440
7441 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7442 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7443
7444 Set_Scalar_Range (Implicit_Base,
7445 Make_Range (Loc,
7446 Low_Bound => Lo,
7447 High_Bound => Hi));
7448
7449 if Has_Infinities (Parent_Base) then
7450 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7451 end if;
7452
7453 -- The Derived_Type, which is the entity of the declaration, is a
7454 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7455 -- absence of an explicit constraint.
7456
7457 Set_Etype (Derived_Type, Implicit_Base);
7458
7459 -- If we did not have a constraint, then the Ekind is set from the
7460 -- parent type (otherwise Process_Subtype has set the bounds)
7461
7462 if No_Constraint then
7463 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7464 end if;
7465
7466 -- If we did not have a range constraint, then set the range from the
7467 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7468
7469 if No_Constraint or else not Has_Range_Constraint (Indic) then
7470 Set_Scalar_Range (Derived_Type,
7471 Make_Range (Loc,
7472 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7473 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7474 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7475
7476 if Has_Infinities (Parent_Type) then
7477 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7478 end if;
7479
7480 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7481 end if;
7482
7483 Set_Is_Descendant_Of_Address (Derived_Type,
7484 Is_Descendant_Of_Address (Parent_Type));
7485 Set_Is_Descendant_Of_Address (Implicit_Base,
7486 Is_Descendant_Of_Address (Parent_Type));
7487
7488 -- Set remaining type-specific fields, depending on numeric type
7489
7490 if Is_Modular_Integer_Type (Parent_Type) then
7491 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7492
7493 Set_Non_Binary_Modulus
7494 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7495
7496 Set_Is_Known_Valid
7497 (Implicit_Base, Is_Known_Valid (Parent_Base));
7498
7499 elsif Is_Floating_Point_Type (Parent_Type) then
7500
7501 -- Digits of base type is always copied from the digits value of
7502 -- the parent base type, but the digits of the derived type will
7503 -- already have been set if there was a constraint present.
7504
7505 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7506 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7507
7508 if No_Constraint then
7509 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7510 end if;
7511
7512 elsif Is_Fixed_Point_Type (Parent_Type) then
7513
7514 -- Small of base type and derived type are always copied from the
7515 -- parent base type, since smalls never change. The delta of the
7516 -- base type is also copied from the parent base type. However the
7517 -- delta of the derived type will have been set already if a
7518 -- constraint was present.
7519
7520 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7521 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7522 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7523
7524 if No_Constraint then
7525 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7526 end if;
7527
7528 -- The scale and machine radix in the decimal case are always
7529 -- copied from the parent base type.
7530
7531 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7532 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7533 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7534
7535 Set_Machine_Radix_10
7536 (Derived_Type, Machine_Radix_10 (Parent_Base));
7537 Set_Machine_Radix_10
7538 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7539
7540 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7541
7542 if No_Constraint then
7543 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7544
7545 else
7546 -- the analysis of the subtype_indication sets the
7547 -- digits value of the derived type.
7548
7549 null;
7550 end if;
7551 end if;
7552 end if;
7553
7554 if Is_Integer_Type (Parent_Type) then
7555 Set_Has_Shift_Operator
7556 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7557 end if;
7558
7559 -- The type of the bounds is that of the parent type, and they
7560 -- must be converted to the derived type.
7561
7562 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7563
7564 -- The implicit_base should be frozen when the derived type is frozen,
7565 -- but note that it is used in the conversions of the bounds. For fixed
7566 -- types we delay the determination of the bounds until the proper
7567 -- freezing point. For other numeric types this is rejected by GCC, for
7568 -- reasons that are currently unclear (???), so we choose to freeze the
7569 -- implicit base now. In the case of integers and floating point types
7570 -- this is harmless because subsequent representation clauses cannot
7571 -- affect anything, but it is still baffling that we cannot use the
7572 -- same mechanism for all derived numeric types.
7573
7574 -- There is a further complication: actually some representation
7575 -- clauses can affect the implicit base type. For example, attribute
7576 -- definition clauses for stream-oriented attributes need to set the
7577 -- corresponding TSS entries on the base type, and this normally
7578 -- cannot be done after the base type is frozen, so the circuitry in
7579 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7580 -- and not use Set_TSS in this case.
7581
7582 -- There are also consequences for the case of delayed representation
7583 -- aspects for some cases. For example, a Size aspect is delayed and
7584 -- should not be evaluated to the freeze point. This early freezing
7585 -- means that the size attribute evaluation happens too early???
7586
7587 if Is_Fixed_Point_Type (Parent_Type) then
7588 Conditional_Delay (Implicit_Base, Parent_Type);
7589 else
7590 Freeze_Before (N, Implicit_Base);
7591 end if;
7592 end Build_Derived_Numeric_Type;
7593
7594 --------------------------------
7595 -- Build_Derived_Private_Type --
7596 --------------------------------
7597
7598 procedure Build_Derived_Private_Type
7599 (N : Node_Id;
7600 Parent_Type : Entity_Id;
7601 Derived_Type : Entity_Id;
7602 Is_Completion : Boolean;
7603 Derive_Subps : Boolean := True)
7604 is
7605 Loc : constant Source_Ptr := Sloc (N);
7606 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7607 Par_Scope : constant Entity_Id := Scope (Par_Base);
7608 Full_N : constant Node_Id := New_Copy_Tree (N);
7609 Full_Der : Entity_Id := New_Copy (Derived_Type);
7610 Full_P : Entity_Id;
7611
7612 procedure Build_Full_Derivation;
7613 -- Build full derivation, i.e. derive from the full view
7614
7615 procedure Copy_And_Build;
7616 -- Copy derived type declaration, replace parent with its full view,
7617 -- and build derivation
7618
7619 ---------------------------
7620 -- Build_Full_Derivation --
7621 ---------------------------
7622
7623 procedure Build_Full_Derivation is
7624 begin
7625 -- If parent scope is not open, install the declarations
7626
7627 if not In_Open_Scopes (Par_Scope) then
7628 Install_Private_Declarations (Par_Scope);
7629 Install_Visible_Declarations (Par_Scope);
7630 Copy_And_Build;
7631 Uninstall_Declarations (Par_Scope);
7632
7633 -- If parent scope is open and in another unit, and parent has a
7634 -- completion, then the derivation is taking place in the visible
7635 -- part of a child unit. In that case retrieve the full view of
7636 -- the parent momentarily.
7637
7638 elsif not In_Same_Source_Unit (N, Parent_Type) then
7639 Full_P := Full_View (Parent_Type);
7640 Exchange_Declarations (Parent_Type);
7641 Copy_And_Build;
7642 Exchange_Declarations (Full_P);
7643
7644 -- Otherwise it is a local derivation
7645
7646 else
7647 Copy_And_Build;
7648 end if;
7649 end Build_Full_Derivation;
7650
7651 --------------------
7652 -- Copy_And_Build --
7653 --------------------
7654
7655 procedure Copy_And_Build is
7656 Full_Parent : Entity_Id := Parent_Type;
7657
7658 begin
7659 -- If the parent is itself derived from another private type,
7660 -- installing the private declarations has not affected its
7661 -- privacy status, so use its own full view explicitly.
7662
7663 if Is_Private_Type (Full_Parent)
7664 and then Present (Full_View (Full_Parent))
7665 then
7666 Full_Parent := Full_View (Full_Parent);
7667 end if;
7668
7669 -- And its underlying full view if necessary
7670
7671 if Is_Private_Type (Full_Parent)
7672 and then Present (Underlying_Full_View (Full_Parent))
7673 then
7674 Full_Parent := Underlying_Full_View (Full_Parent);
7675 end if;
7676
7677 -- For record, access and most enumeration types, derivation from
7678 -- the full view requires a fully-fledged declaration. In the other
7679 -- cases, just use an itype.
7680
7681 if Ekind (Full_Parent) in Record_Kind
7682 or else Ekind (Full_Parent) in Access_Kind
7683 or else
7684 (Ekind (Full_Parent) in Enumeration_Kind
7685 and then not Is_Standard_Character_Type (Full_Parent)
7686 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7687 then
7688 -- Copy and adjust declaration to provide a completion for what
7689 -- is originally a private declaration. Indicate that full view
7690 -- is internally generated.
7691
7692 Set_Comes_From_Source (Full_N, False);
7693 Set_Comes_From_Source (Full_Der, False);
7694 Set_Parent (Full_Der, Full_N);
7695 Set_Defining_Identifier (Full_N, Full_Der);
7696
7697 -- If there are no constraints, adjust the subtype mark
7698
7699 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7700 N_Subtype_Indication
7701 then
7702 Set_Subtype_Indication
7703 (Type_Definition (Full_N),
7704 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7705 end if;
7706
7707 Insert_After (N, Full_N);
7708
7709 -- Build full view of derived type from full view of parent which
7710 -- is now installed. Subprograms have been derived on the partial
7711 -- view, the completion does not derive them anew.
7712
7713 if Ekind (Full_Parent) in Record_Kind then
7714
7715 -- If parent type is tagged, the completion inherits the proper
7716 -- primitive operations.
7717
7718 if Is_Tagged_Type (Parent_Type) then
7719 Build_Derived_Record_Type
7720 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7721 else
7722 Build_Derived_Record_Type
7723 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7724 end if;
7725
7726 else
7727 Build_Derived_Type
7728 (Full_N, Full_Parent, Full_Der,
7729 Is_Completion => False, Derive_Subps => False);
7730 end if;
7731
7732 -- The full declaration has been introduced into the tree and
7733 -- processed in the step above. It should not be analyzed again
7734 -- (when encountered later in the current list of declarations)
7735 -- to prevent spurious name conflicts. The full entity remains
7736 -- invisible.
7737
7738 Set_Analyzed (Full_N);
7739
7740 else
7741 Full_Der :=
7742 Make_Defining_Identifier (Sloc (Derived_Type),
7743 Chars => Chars (Derived_Type));
7744 Set_Is_Itype (Full_Der);
7745 Set_Associated_Node_For_Itype (Full_Der, N);
7746 Set_Parent (Full_Der, N);
7747 Build_Derived_Type
7748 (N, Full_Parent, Full_Der,
7749 Is_Completion => False, Derive_Subps => False);
7750 end if;
7751
7752 Set_Has_Private_Declaration (Full_Der);
7753 Set_Has_Private_Declaration (Derived_Type);
7754
7755 Set_Scope (Full_Der, Scope (Derived_Type));
7756 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7757 Set_Has_Size_Clause (Full_Der, False);
7758 Set_Has_Alignment_Clause (Full_Der, False);
7759 Set_Has_Delayed_Freeze (Full_Der);
7760 Set_Is_Frozen (Full_Der, False);
7761 Set_Freeze_Node (Full_Der, Empty);
7762 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7763 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7764
7765 -- The convention on the base type may be set in the private part
7766 -- and not propagated to the subtype until later, so we obtain the
7767 -- convention from the base type of the parent.
7768
7769 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7770 end Copy_And_Build;
7771
7772 -- Start of processing for Build_Derived_Private_Type
7773
7774 begin
7775 if Is_Tagged_Type (Parent_Type) then
7776 Full_P := Full_View (Parent_Type);
7777
7778 -- A type extension of a type with unknown discriminants is an
7779 -- indefinite type that the back-end cannot handle directly.
7780 -- We treat it as a private type, and build a completion that is
7781 -- derived from the full view of the parent, and hopefully has
7782 -- known discriminants.
7783
7784 -- If the full view of the parent type has an underlying record view,
7785 -- use it to generate the underlying record view of this derived type
7786 -- (required for chains of derivations with unknown discriminants).
7787
7788 -- Minor optimization: we avoid the generation of useless underlying
7789 -- record view entities if the private type declaration has unknown
7790 -- discriminants but its corresponding full view has no
7791 -- discriminants.
7792
7793 if Has_Unknown_Discriminants (Parent_Type)
7794 and then Present (Full_P)
7795 and then (Has_Discriminants (Full_P)
7796 or else Present (Underlying_Record_View (Full_P)))
7797 and then not In_Open_Scopes (Par_Scope)
7798 and then Expander_Active
7799 then
7800 declare
7801 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7802 New_Ext : constant Node_Id :=
7803 Copy_Separate_Tree
7804 (Record_Extension_Part (Type_Definition (N)));
7805 Decl : Node_Id;
7806
7807 begin
7808 Build_Derived_Record_Type
7809 (N, Parent_Type, Derived_Type, Derive_Subps);
7810
7811 -- Build anonymous completion, as a derivation from the full
7812 -- view of the parent. This is not a completion in the usual
7813 -- sense, because the current type is not private.
7814
7815 Decl :=
7816 Make_Full_Type_Declaration (Loc,
7817 Defining_Identifier => Full_Der,
7818 Type_Definition =>
7819 Make_Derived_Type_Definition (Loc,
7820 Subtype_Indication =>
7821 New_Copy_Tree
7822 (Subtype_Indication (Type_Definition (N))),
7823 Record_Extension_Part => New_Ext));
7824
7825 -- If the parent type has an underlying record view, use it
7826 -- here to build the new underlying record view.
7827
7828 if Present (Underlying_Record_View (Full_P)) then
7829 pragma Assert
7830 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7831 = N_Identifier);
7832 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7833 Underlying_Record_View (Full_P));
7834 end if;
7835
7836 Install_Private_Declarations (Par_Scope);
7837 Install_Visible_Declarations (Par_Scope);
7838 Insert_Before (N, Decl);
7839
7840 -- Mark entity as an underlying record view before analysis,
7841 -- to avoid generating the list of its primitive operations
7842 -- (which is not really required for this entity) and thus
7843 -- prevent spurious errors associated with missing overriding
7844 -- of abstract primitives (overridden only for Derived_Type).
7845
7846 Set_Ekind (Full_Der, E_Record_Type);
7847 Set_Is_Underlying_Record_View (Full_Der);
7848 Set_Default_SSO (Full_Der);
7849 Set_No_Reordering (Full_Der, No_Component_Reordering);
7850
7851 Analyze (Decl);
7852
7853 pragma Assert (Has_Discriminants (Full_Der)
7854 and then not Has_Unknown_Discriminants (Full_Der));
7855
7856 Uninstall_Declarations (Par_Scope);
7857
7858 -- Freeze the underlying record view, to prevent generation of
7859 -- useless dispatching information, which is simply shared with
7860 -- the real derived type.
7861
7862 Set_Is_Frozen (Full_Der);
7863
7864 -- If the derived type has access discriminants, create
7865 -- references to their anonymous types now, to prevent
7866 -- back-end problems when their first use is in generated
7867 -- bodies of primitives.
7868
7869 declare
7870 E : Entity_Id;
7871
7872 begin
7873 E := First_Entity (Full_Der);
7874
7875 while Present (E) loop
7876 if Ekind (E) = E_Discriminant
7877 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7878 then
7879 Build_Itype_Reference (Etype (E), Decl);
7880 end if;
7881
7882 Next_Entity (E);
7883 end loop;
7884 end;
7885
7886 -- Set up links between real entity and underlying record view
7887
7888 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7889 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7890 end;
7891
7892 -- If discriminants are known, build derived record
7893
7894 else
7895 Build_Derived_Record_Type
7896 (N, Parent_Type, Derived_Type, Derive_Subps);
7897 end if;
7898
7899 return;
7900
7901 elsif Has_Discriminants (Parent_Type) then
7902
7903 -- Build partial view of derived type from partial view of parent.
7904 -- This must be done before building the full derivation because the
7905 -- second derivation will modify the discriminants of the first and
7906 -- the discriminants are chained with the rest of the components in
7907 -- the full derivation.
7908
7909 Build_Derived_Record_Type
7910 (N, Parent_Type, Derived_Type, Derive_Subps);
7911
7912 -- Build the full derivation if this is not the anonymous derived
7913 -- base type created by Build_Derived_Record_Type in the constrained
7914 -- case (see point 5. of its head comment) since we build it for the
7915 -- derived subtype. And skip it for synchronized types altogether, as
7916 -- gigi does not use these types directly.
7917
7918 if Present (Full_View (Parent_Type))
7919 and then not Is_Itype (Derived_Type)
7920 and then not Is_Concurrent_Type (Full_View (Parent_Type))
7921 then
7922 declare
7923 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7924 Discr : Entity_Id;
7925 Last_Discr : Entity_Id;
7926
7927 begin
7928 -- If this is not a completion, construct the implicit full
7929 -- view by deriving from the full view of the parent type.
7930 -- But if this is a completion, the derived private type
7931 -- being built is a full view and the full derivation can
7932 -- only be its underlying full view.
7933
7934 Build_Full_Derivation;
7935
7936 if not Is_Completion then
7937 Set_Full_View (Derived_Type, Full_Der);
7938 else
7939 Set_Underlying_Full_View (Derived_Type, Full_Der);
7940 Set_Is_Underlying_Full_View (Full_Der);
7941 end if;
7942
7943 if not Is_Base_Type (Derived_Type) then
7944 Set_Full_View (Der_Base, Base_Type (Full_Der));
7945 end if;
7946
7947 -- Copy the discriminant list from full view to the partial
7948 -- view (base type and its subtype). Gigi requires that the
7949 -- partial and full views have the same discriminants.
7950
7951 -- Note that since the partial view points to discriminants
7952 -- in the full view, their scope will be that of the full
7953 -- view. This might cause some front end problems and need
7954 -- adjustment???
7955
7956 Discr := First_Discriminant (Base_Type (Full_Der));
7957 Set_First_Entity (Der_Base, Discr);
7958
7959 loop
7960 Last_Discr := Discr;
7961 Next_Discriminant (Discr);
7962 exit when No (Discr);
7963 end loop;
7964
7965 Set_Last_Entity (Der_Base, Last_Discr);
7966 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7967 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7968 end;
7969 end if;
7970
7971 elsif Present (Full_View (Parent_Type))
7972 and then Has_Discriminants (Full_View (Parent_Type))
7973 then
7974 if Has_Unknown_Discriminants (Parent_Type)
7975 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7976 N_Subtype_Indication
7977 then
7978 Error_Msg_N
7979 ("cannot constrain type with unknown discriminants",
7980 Subtype_Indication (Type_Definition (N)));
7981 return;
7982 end if;
7983
7984 -- If this is not a completion, construct the implicit full view by
7985 -- deriving from the full view of the parent type. But if this is a
7986 -- completion, the derived private type being built is a full view
7987 -- and the full derivation can only be its underlying full view.
7988
7989 Build_Full_Derivation;
7990
7991 if not Is_Completion then
7992 Set_Full_View (Derived_Type, Full_Der);
7993 else
7994 Set_Underlying_Full_View (Derived_Type, Full_Der);
7995 Set_Is_Underlying_Full_View (Full_Der);
7996 end if;
7997
7998 -- In any case, the primitive operations are inherited from the
7999 -- parent type, not from the internal full view.
8000
8001 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8002
8003 if Derive_Subps then
8004 Derive_Subprograms (Parent_Type, Derived_Type);
8005 end if;
8006
8007 Set_Stored_Constraint (Derived_Type, No_Elist);
8008 Set_Is_Constrained
8009 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
8010
8011 else
8012 -- Untagged type, No discriminants on either view
8013
8014 if Nkind (Subtype_Indication (Type_Definition (N))) =
8015 N_Subtype_Indication
8016 then
8017 Error_Msg_N
8018 ("illegal constraint on type without discriminants", N);
8019 end if;
8020
8021 if Present (Discriminant_Specifications (N))
8022 and then Present (Full_View (Parent_Type))
8023 and then not Is_Tagged_Type (Full_View (Parent_Type))
8024 then
8025 Error_Msg_N ("cannot add discriminants to untagged type", N);
8026 end if;
8027
8028 Set_Stored_Constraint (Derived_Type, No_Elist);
8029 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8030
8031 Set_Is_Controlled_Active
8032 (Derived_Type, Is_Controlled_Active (Parent_Type));
8033
8034 Set_Disable_Controlled
8035 (Derived_Type, Disable_Controlled (Parent_Type));
8036
8037 Set_Has_Controlled_Component
8038 (Derived_Type, Has_Controlled_Component (Parent_Type));
8039
8040 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8041
8042 if not Is_Controlled (Parent_Type) then
8043 Set_Finalize_Storage_Only
8044 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8045 end if;
8046
8047 -- If this is not a completion, construct the implicit full view by
8048 -- deriving from the full view of the parent type.
8049
8050 -- ??? If the parent is untagged private and its completion is
8051 -- tagged, this mechanism will not work because we cannot derive from
8052 -- the tagged full view unless we have an extension.
8053
8054 if Present (Full_View (Parent_Type))
8055 and then not Is_Tagged_Type (Full_View (Parent_Type))
8056 and then not Is_Completion
8057 then
8058 Build_Full_Derivation;
8059 Set_Full_View (Derived_Type, Full_Der);
8060 end if;
8061 end if;
8062
8063 Set_Has_Unknown_Discriminants (Derived_Type,
8064 Has_Unknown_Discriminants (Parent_Type));
8065
8066 if Is_Private_Type (Derived_Type) then
8067 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8068 end if;
8069
8070 -- If the parent base type is in scope, add the derived type to its
8071 -- list of private dependents, because its full view may become
8072 -- visible subsequently (in a nested private part, a body, or in a
8073 -- further child unit).
8074
8075 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8076 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8077
8078 -- Check for unusual case where a type completed by a private
8079 -- derivation occurs within a package nested in a child unit, and
8080 -- the parent is declared in an ancestor.
8081
8082 if Is_Child_Unit (Scope (Current_Scope))
8083 and then Is_Completion
8084 and then In_Private_Part (Current_Scope)
8085 and then Scope (Parent_Type) /= Current_Scope
8086
8087 -- Note that if the parent has a completion in the private part,
8088 -- (which is itself a derivation from some other private type)
8089 -- it is that completion that is visible, there is no full view
8090 -- available, and no special processing is needed.
8091
8092 and then Present (Full_View (Parent_Type))
8093 then
8094 -- In this case, the full view of the parent type will become
8095 -- visible in the body of the enclosing child, and only then will
8096 -- the current type be possibly non-private. Build an underlying
8097 -- full view that will be installed when the enclosing child body
8098 -- is compiled.
8099
8100 if Present (Underlying_Full_View (Derived_Type)) then
8101 Full_Der := Underlying_Full_View (Derived_Type);
8102 else
8103 Build_Full_Derivation;
8104 Set_Underlying_Full_View (Derived_Type, Full_Der);
8105 Set_Is_Underlying_Full_View (Full_Der);
8106 end if;
8107
8108 -- The full view will be used to swap entities on entry/exit to
8109 -- the body, and must appear in the entity list for the package.
8110
8111 Append_Entity (Full_Der, Scope (Derived_Type));
8112 end if;
8113 end if;
8114 end Build_Derived_Private_Type;
8115
8116 -------------------------------
8117 -- Build_Derived_Record_Type --
8118 -------------------------------
8119
8120 -- 1. INTRODUCTION
8121
8122 -- Ideally we would like to use the same model of type derivation for
8123 -- tagged and untagged record types. Unfortunately this is not quite
8124 -- possible because the semantics of representation clauses is different
8125 -- for tagged and untagged records under inheritance. Consider the
8126 -- following:
8127
8128 -- type R (...) is [tagged] record ... end record;
8129 -- type T (...) is new R (...) [with ...];
8130
8131 -- The representation clauses for T can specify a completely different
8132 -- record layout from R's. Hence the same component can be placed in two
8133 -- very different positions in objects of type T and R. If R and T are
8134 -- tagged types, representation clauses for T can only specify the layout
8135 -- of non inherited components, thus components that are common in R and T
8136 -- have the same position in objects of type R and T.
8137
8138 -- This has two implications. The first is that the entire tree for R's
8139 -- declaration needs to be copied for T in the untagged case, so that T
8140 -- can be viewed as a record type of its own with its own representation
8141 -- clauses. The second implication is the way we handle discriminants.
8142 -- Specifically, in the untagged case we need a way to communicate to Gigi
8143 -- what are the real discriminants in the record, while for the semantics
8144 -- we need to consider those introduced by the user to rename the
8145 -- discriminants in the parent type. This is handled by introducing the
8146 -- notion of stored discriminants. See below for more.
8147
8148 -- Fortunately the way regular components are inherited can be handled in
8149 -- the same way in tagged and untagged types.
8150
8151 -- To complicate things a bit more the private view of a private extension
8152 -- cannot be handled in the same way as the full view (for one thing the
8153 -- semantic rules are somewhat different). We will explain what differs
8154 -- below.
8155
8156 -- 2. DISCRIMINANTS UNDER INHERITANCE
8157
8158 -- The semantic rules governing the discriminants of derived types are
8159 -- quite subtle.
8160
8161 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8162 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8163
8164 -- If parent type has discriminants, then the discriminants that are
8165 -- declared in the derived type are [3.4 (11)]:
8166
8167 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8168 -- there is one;
8169
8170 -- o Otherwise, each discriminant of the parent type (implicitly declared
8171 -- in the same order with the same specifications). In this case, the
8172 -- discriminants are said to be "inherited", or if unknown in the parent
8173 -- are also unknown in the derived type.
8174
8175 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8176
8177 -- o The parent subtype must be constrained;
8178
8179 -- o If the parent type is not a tagged type, then each discriminant of
8180 -- the derived type must be used in the constraint defining a parent
8181 -- subtype. [Implementation note: This ensures that the new discriminant
8182 -- can share storage with an existing discriminant.]
8183
8184 -- For the derived type each discriminant of the parent type is either
8185 -- inherited, constrained to equal some new discriminant of the derived
8186 -- type, or constrained to the value of an expression.
8187
8188 -- When inherited or constrained to equal some new discriminant, the
8189 -- parent discriminant and the discriminant of the derived type are said
8190 -- to "correspond".
8191
8192 -- If a discriminant of the parent type is constrained to a specific value
8193 -- in the derived type definition, then the discriminant is said to be
8194 -- "specified" by that derived type definition.
8195
8196 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8197
8198 -- We have spoken about stored discriminants in point 1 (introduction)
8199 -- above. There are two sorts of stored discriminants: implicit and
8200 -- explicit. As long as the derived type inherits the same discriminants as
8201 -- the root record type, stored discriminants are the same as regular
8202 -- discriminants, and are said to be implicit. However, if any discriminant
8203 -- in the root type was renamed in the derived type, then the derived
8204 -- type will contain explicit stored discriminants. Explicit stored
8205 -- discriminants are discriminants in addition to the semantically visible
8206 -- discriminants defined for the derived type. Stored discriminants are
8207 -- used by Gigi to figure out what are the physical discriminants in
8208 -- objects of the derived type (see precise definition in einfo.ads).
8209 -- As an example, consider the following:
8210
8211 -- type R (D1, D2, D3 : Int) is record ... end record;
8212 -- type T1 is new R;
8213 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8214 -- type T3 is new T2;
8215 -- type T4 (Y : Int) is new T3 (Y, 99);
8216
8217 -- The following table summarizes the discriminants and stored
8218 -- discriminants in R and T1 through T4:
8219
8220 -- Type Discrim Stored Discrim Comment
8221 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8222 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8223 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8224 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8225 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8226
8227 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8228 -- find the corresponding discriminant in the parent type, while
8229 -- Original_Record_Component (abbreviated ORC below) the actual physical
8230 -- component that is renamed. Finally the field Is_Completely_Hidden
8231 -- (abbreviated ICH below) is set for all explicit stored discriminants
8232 -- (see einfo.ads for more info). For the above example this gives:
8233
8234 -- Discrim CD ORC ICH
8235 -- ^^^^^^^ ^^ ^^^ ^^^
8236 -- D1 in R empty itself no
8237 -- D2 in R empty itself no
8238 -- D3 in R empty itself no
8239
8240 -- D1 in T1 D1 in R itself no
8241 -- D2 in T1 D2 in R itself no
8242 -- D3 in T1 D3 in R itself no
8243
8244 -- X1 in T2 D3 in T1 D3 in T2 no
8245 -- X2 in T2 D1 in T1 D1 in T2 no
8246 -- D1 in T2 empty itself yes
8247 -- D2 in T2 empty itself yes
8248 -- D3 in T2 empty itself yes
8249
8250 -- X1 in T3 X1 in T2 D3 in T3 no
8251 -- X2 in T3 X2 in T2 D1 in T3 no
8252 -- D1 in T3 empty itself yes
8253 -- D2 in T3 empty itself yes
8254 -- D3 in T3 empty itself yes
8255
8256 -- Y in T4 X1 in T3 D3 in T4 no
8257 -- D1 in T4 empty itself yes
8258 -- D2 in T4 empty itself yes
8259 -- D3 in T4 empty itself yes
8260
8261 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8262
8263 -- Type derivation for tagged types is fairly straightforward. If no
8264 -- discriminants are specified by the derived type, these are inherited
8265 -- from the parent. No explicit stored discriminants are ever necessary.
8266 -- The only manipulation that is done to the tree is that of adding a
8267 -- _parent field with parent type and constrained to the same constraint
8268 -- specified for the parent in the derived type definition. For instance:
8269
8270 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8271 -- type T1 is new R with null record;
8272 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8273
8274 -- are changed into:
8275
8276 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8277 -- _parent : R (D1, D2, D3);
8278 -- end record;
8279
8280 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8281 -- _parent : T1 (X2, 88, X1);
8282 -- end record;
8283
8284 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8285 -- ORC and ICH fields are:
8286
8287 -- Discrim CD ORC ICH
8288 -- ^^^^^^^ ^^ ^^^ ^^^
8289 -- D1 in R empty itself no
8290 -- D2 in R empty itself no
8291 -- D3 in R empty itself no
8292
8293 -- D1 in T1 D1 in R D1 in R no
8294 -- D2 in T1 D2 in R D2 in R no
8295 -- D3 in T1 D3 in R D3 in R no
8296
8297 -- X1 in T2 D3 in T1 D3 in R no
8298 -- X2 in T2 D1 in T1 D1 in R no
8299
8300 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8301 --
8302 -- Regardless of whether we dealing with a tagged or untagged type
8303 -- we will transform all derived type declarations of the form
8304 --
8305 -- type T is new R (...) [with ...];
8306 -- or
8307 -- subtype S is R (...);
8308 -- type T is new S [with ...];
8309 -- into
8310 -- type BT is new R [with ...];
8311 -- subtype T is BT (...);
8312 --
8313 -- That is, the base derived type is constrained only if it has no
8314 -- discriminants. The reason for doing this is that GNAT's semantic model
8315 -- assumes that a base type with discriminants is unconstrained.
8316 --
8317 -- Note that, strictly speaking, the above transformation is not always
8318 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8319 --
8320 -- procedure B34011A is
8321 -- type REC (D : integer := 0) is record
8322 -- I : Integer;
8323 -- end record;
8324
8325 -- package P is
8326 -- type T6 is new Rec;
8327 -- function F return T6;
8328 -- end P;
8329
8330 -- use P;
8331 -- package Q6 is
8332 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8333 -- end Q6;
8334 --
8335 -- The definition of Q6.U is illegal. However transforming Q6.U into
8336
8337 -- type BaseU is new T6;
8338 -- subtype U is BaseU (Q6.F.I)
8339
8340 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8341 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8342 -- the transformation described above.
8343
8344 -- There is another instance where the above transformation is incorrect.
8345 -- Consider:
8346
8347 -- package Pack is
8348 -- type Base (D : Integer) is tagged null record;
8349 -- procedure P (X : Base);
8350
8351 -- type Der is new Base (2) with null record;
8352 -- procedure P (X : Der);
8353 -- end Pack;
8354
8355 -- Then the above transformation turns this into
8356
8357 -- type Der_Base is new Base with null record;
8358 -- -- procedure P (X : Base) is implicitly inherited here
8359 -- -- as procedure P (X : Der_Base).
8360
8361 -- subtype Der is Der_Base (2);
8362 -- procedure P (X : Der);
8363 -- -- The overriding of P (X : Der_Base) is illegal since we
8364 -- -- have a parameter conformance problem.
8365
8366 -- To get around this problem, after having semantically processed Der_Base
8367 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8368 -- Discriminant_Constraint from Der so that when parameter conformance is
8369 -- checked when P is overridden, no semantic errors are flagged.
8370
8371 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8372
8373 -- Regardless of whether we are dealing with a tagged or untagged type
8374 -- we will transform all derived type declarations of the form
8375
8376 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8377 -- type T is new R [with ...];
8378 -- into
8379 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8380
8381 -- The reason for such transformation is that it allows us to implement a
8382 -- very clean form of component inheritance as explained below.
8383
8384 -- Note that this transformation is not achieved by direct tree rewriting
8385 -- and manipulation, but rather by redoing the semantic actions that the
8386 -- above transformation will entail. This is done directly in routine
8387 -- Inherit_Components.
8388
8389 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8390
8391 -- In both tagged and untagged derived types, regular non discriminant
8392 -- components are inherited in the derived type from the parent type. In
8393 -- the absence of discriminants component, inheritance is straightforward
8394 -- as components can simply be copied from the parent.
8395
8396 -- If the parent has discriminants, inheriting components constrained with
8397 -- these discriminants requires caution. Consider the following example:
8398
8399 -- type R (D1, D2 : Positive) is [tagged] record
8400 -- S : String (D1 .. D2);
8401 -- end record;
8402
8403 -- type T1 is new R [with null record];
8404 -- type T2 (X : positive) is new R (1, X) [with null record];
8405
8406 -- As explained in 6. above, T1 is rewritten as
8407 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8408 -- which makes the treatment for T1 and T2 identical.
8409
8410 -- What we want when inheriting S, is that references to D1 and D2 in R are
8411 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8412 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8413 -- with either discriminant references in the derived type or expressions.
8414 -- This replacement is achieved as follows: before inheriting R's
8415 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8416 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8417 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8418 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8419 -- by String (1 .. X).
8420
8421 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8422
8423 -- We explain here the rules governing private type extensions relevant to
8424 -- type derivation. These rules are explained on the following example:
8425
8426 -- type D [(...)] is new A [(...)] with private; <-- partial view
8427 -- type D [(...)] is new P [(...)] with null record; <-- full view
8428
8429 -- Type A is called the ancestor subtype of the private extension.
8430 -- Type P is the parent type of the full view of the private extension. It
8431 -- must be A or a type derived from A.
8432
8433 -- The rules concerning the discriminants of private type extensions are
8434 -- [7.3(10-13)]:
8435
8436 -- o If a private extension inherits known discriminants from the ancestor
8437 -- subtype, then the full view must also inherit its discriminants from
8438 -- the ancestor subtype and the parent subtype of the full view must be
8439 -- constrained if and only if the ancestor subtype is constrained.
8440
8441 -- o If a partial view has unknown discriminants, then the full view may
8442 -- define a definite or an indefinite subtype, with or without
8443 -- discriminants.
8444
8445 -- o If a partial view has neither known nor unknown discriminants, then
8446 -- the full view must define a definite subtype.
8447
8448 -- o If the ancestor subtype of a private extension has constrained
8449 -- discriminants, then the parent subtype of the full view must impose a
8450 -- statically matching constraint on those discriminants.
8451
8452 -- This means that only the following forms of private extensions are
8453 -- allowed:
8454
8455 -- type D is new A with private; <-- partial view
8456 -- type D is new P with null record; <-- full view
8457
8458 -- If A has no discriminants than P has no discriminants, otherwise P must
8459 -- inherit A's discriminants.
8460
8461 -- type D is new A (...) with private; <-- partial view
8462 -- type D is new P (:::) with null record; <-- full view
8463
8464 -- P must inherit A's discriminants and (...) and (:::) must statically
8465 -- match.
8466
8467 -- subtype A is R (...);
8468 -- type D is new A with private; <-- partial view
8469 -- type D is new P with null record; <-- full view
8470
8471 -- P must have inherited R's discriminants and must be derived from A or
8472 -- any of its subtypes.
8473
8474 -- type D (..) is new A with private; <-- partial view
8475 -- type D (..) is new P [(:::)] with null record; <-- full view
8476
8477 -- No specific constraints on P's discriminants or constraint (:::).
8478 -- Note that A can be unconstrained, but the parent subtype P must either
8479 -- be constrained or (:::) must be present.
8480
8481 -- type D (..) is new A [(...)] with private; <-- partial view
8482 -- type D (..) is new P [(:::)] with null record; <-- full view
8483
8484 -- P's constraints on A's discriminants must statically match those
8485 -- imposed by (...).
8486
8487 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8488
8489 -- The full view of a private extension is handled exactly as described
8490 -- above. The model chose for the private view of a private extension is
8491 -- the same for what concerns discriminants (i.e. they receive the same
8492 -- treatment as in the tagged case). However, the private view of the
8493 -- private extension always inherits the components of the parent base,
8494 -- without replacing any discriminant reference. Strictly speaking this is
8495 -- incorrect. However, Gigi never uses this view to generate code so this
8496 -- is a purely semantic issue. In theory, a set of transformations similar
8497 -- to those given in 5. and 6. above could be applied to private views of
8498 -- private extensions to have the same model of component inheritance as
8499 -- for non private extensions. However, this is not done because it would
8500 -- further complicate private type processing. Semantically speaking, this
8501 -- leaves us in an uncomfortable situation. As an example consider:
8502
8503 -- package Pack is
8504 -- type R (D : integer) is tagged record
8505 -- S : String (1 .. D);
8506 -- end record;
8507 -- procedure P (X : R);
8508 -- type T is new R (1) with private;
8509 -- private
8510 -- type T is new R (1) with null record;
8511 -- end;
8512
8513 -- This is transformed into:
8514
8515 -- package Pack is
8516 -- type R (D : integer) is tagged record
8517 -- S : String (1 .. D);
8518 -- end record;
8519 -- procedure P (X : R);
8520 -- type T is new R (1) with private;
8521 -- private
8522 -- type BaseT is new R with null record;
8523 -- subtype T is BaseT (1);
8524 -- end;
8525
8526 -- (strictly speaking the above is incorrect Ada)
8527
8528 -- From the semantic standpoint the private view of private extension T
8529 -- should be flagged as constrained since one can clearly have
8530 --
8531 -- Obj : T;
8532 --
8533 -- in a unit withing Pack. However, when deriving subprograms for the
8534 -- private view of private extension T, T must be seen as unconstrained
8535 -- since T has discriminants (this is a constraint of the current
8536 -- subprogram derivation model). Thus, when processing the private view of
8537 -- a private extension such as T, we first mark T as unconstrained, we
8538 -- process it, we perform program derivation and just before returning from
8539 -- Build_Derived_Record_Type we mark T as constrained.
8540
8541 -- ??? Are there are other uncomfortable cases that we will have to
8542 -- deal with.
8543
8544 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8545
8546 -- Types that are derived from a visible record type and have a private
8547 -- extension present other peculiarities. They behave mostly like private
8548 -- types, but if they have primitive operations defined, these will not
8549 -- have the proper signatures for further inheritance, because other
8550 -- primitive operations will use the implicit base that we define for
8551 -- private derivations below. This affect subprogram inheritance (see
8552 -- Derive_Subprograms for details). We also derive the implicit base from
8553 -- the base type of the full view, so that the implicit base is a record
8554 -- type and not another private type, This avoids infinite loops.
8555
8556 procedure Build_Derived_Record_Type
8557 (N : Node_Id;
8558 Parent_Type : Entity_Id;
8559 Derived_Type : Entity_Id;
8560 Derive_Subps : Boolean := True)
8561 is
8562 Discriminant_Specs : constant Boolean :=
8563 Present (Discriminant_Specifications (N));
8564 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8565 Loc : constant Source_Ptr := Sloc (N);
8566 Private_Extension : constant Boolean :=
8567 Nkind (N) = N_Private_Extension_Declaration;
8568 Assoc_List : Elist_Id;
8569 Constraint_Present : Boolean;
8570 Constrs : Elist_Id;
8571 Discrim : Entity_Id;
8572 Indic : Node_Id;
8573 Inherit_Discrims : Boolean := False;
8574 Last_Discrim : Entity_Id;
8575 New_Base : Entity_Id;
8576 New_Decl : Node_Id;
8577 New_Discrs : Elist_Id;
8578 New_Indic : Node_Id;
8579 Parent_Base : Entity_Id;
8580 Save_Etype : Entity_Id;
8581 Save_Discr_Constr : Elist_Id;
8582 Save_Next_Entity : Entity_Id;
8583 Type_Def : Node_Id;
8584
8585 Discs : Elist_Id := New_Elmt_List;
8586 -- An empty Discs list means that there were no constraints in the
8587 -- subtype indication or that there was an error processing it.
8588
8589 begin
8590 if Ekind (Parent_Type) = E_Record_Type_With_Private
8591 and then Present (Full_View (Parent_Type))
8592 and then Has_Discriminants (Parent_Type)
8593 then
8594 Parent_Base := Base_Type (Full_View (Parent_Type));
8595 else
8596 Parent_Base := Base_Type (Parent_Type);
8597 end if;
8598
8599 -- If the parent type is declared as a subtype of another private
8600 -- type with inherited discriminants, its generated base type is
8601 -- itself a record subtype. To further inherit the constraint we
8602 -- need to use its own base to have an unconstrained type on which
8603 -- to apply the inherited constraint.
8604
8605 if Ekind (Parent_Base) = E_Record_Subtype then
8606 Parent_Base := Base_Type (Parent_Base);
8607 end if;
8608
8609 -- AI05-0115: if this is a derivation from a private type in some
8610 -- other scope that may lead to invisible components for the derived
8611 -- type, mark it accordingly.
8612
8613 if Is_Private_Type (Parent_Type) then
8614 if Scope (Parent_Base) = Scope (Derived_Type) then
8615 null;
8616
8617 elsif In_Open_Scopes (Scope (Parent_Base))
8618 and then In_Private_Part (Scope (Parent_Base))
8619 then
8620 null;
8621
8622 else
8623 Set_Has_Private_Ancestor (Derived_Type);
8624 end if;
8625
8626 else
8627 Set_Has_Private_Ancestor
8628 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8629 end if;
8630
8631 -- Before we start the previously documented transformations, here is
8632 -- little fix for size and alignment of tagged types. Normally when we
8633 -- derive type D from type P, we copy the size and alignment of P as the
8634 -- default for D, and in the absence of explicit representation clauses
8635 -- for D, the size and alignment are indeed the same as the parent.
8636
8637 -- But this is wrong for tagged types, since fields may be added, and
8638 -- the default size may need to be larger, and the default alignment may
8639 -- need to be larger.
8640
8641 -- We therefore reset the size and alignment fields in the tagged case.
8642 -- Note that the size and alignment will in any case be at least as
8643 -- large as the parent type (since the derived type has a copy of the
8644 -- parent type in the _parent field)
8645
8646 -- The type is also marked as being tagged here, which is needed when
8647 -- processing components with a self-referential anonymous access type
8648 -- in the call to Check_Anonymous_Access_Components below. Note that
8649 -- this flag is also set later on for completeness.
8650
8651 if Is_Tagged then
8652 Set_Is_Tagged_Type (Derived_Type);
8653 Init_Size_Align (Derived_Type);
8654 end if;
8655
8656 -- STEP 0a: figure out what kind of derived type declaration we have
8657
8658 if Private_Extension then
8659 Type_Def := N;
8660 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8661 Set_Default_SSO (Derived_Type);
8662 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8663
8664 else
8665 Type_Def := Type_Definition (N);
8666
8667 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8668 -- Parent_Base can be a private type or private extension. However,
8669 -- for tagged types with an extension the newly added fields are
8670 -- visible and hence the Derived_Type is always an E_Record_Type.
8671 -- (except that the parent may have its own private fields).
8672 -- For untagged types we preserve the Ekind of the Parent_Base.
8673
8674 if Present (Record_Extension_Part (Type_Def)) then
8675 Set_Ekind (Derived_Type, E_Record_Type);
8676 Set_Default_SSO (Derived_Type);
8677 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8678
8679 -- Create internal access types for components with anonymous
8680 -- access types.
8681
8682 if Ada_Version >= Ada_2005 then
8683 Check_Anonymous_Access_Components
8684 (N, Derived_Type, Derived_Type,
8685 Component_List (Record_Extension_Part (Type_Def)));
8686 end if;
8687
8688 else
8689 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8690 end if;
8691 end if;
8692
8693 -- Indic can either be an N_Identifier if the subtype indication
8694 -- contains no constraint or an N_Subtype_Indication if the subtype
8695 -- indication has a constraint.
8696
8697 Indic := Subtype_Indication (Type_Def);
8698 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8699
8700 -- Check that the type has visible discriminants. The type may be
8701 -- a private type with unknown discriminants whose full view has
8702 -- discriminants which are invisible.
8703
8704 if Constraint_Present then
8705 if not Has_Discriminants (Parent_Base)
8706 or else
8707 (Has_Unknown_Discriminants (Parent_Base)
8708 and then Is_Private_Type (Parent_Base))
8709 then
8710 Error_Msg_N
8711 ("invalid constraint: type has no discriminant",
8712 Constraint (Indic));
8713
8714 Constraint_Present := False;
8715 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8716
8717 elsif Is_Constrained (Parent_Type) then
8718 Error_Msg_N
8719 ("invalid constraint: parent type is already constrained",
8720 Constraint (Indic));
8721
8722 Constraint_Present := False;
8723 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8724 end if;
8725 end if;
8726
8727 -- STEP 0b: If needed, apply transformation given in point 5. above
8728
8729 if not Private_Extension
8730 and then Has_Discriminants (Parent_Type)
8731 and then not Discriminant_Specs
8732 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8733 then
8734 -- First, we must analyze the constraint (see comment in point 5.)
8735 -- The constraint may come from the subtype indication of the full
8736 -- declaration.
8737
8738 if Constraint_Present then
8739 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8740
8741 -- If there is no explicit constraint, there might be one that is
8742 -- inherited from a constrained parent type. In that case verify that
8743 -- it conforms to the constraint in the partial view. In perverse
8744 -- cases the parent subtypes of the partial and full view can have
8745 -- different constraints.
8746
8747 elsif Present (Stored_Constraint (Parent_Type)) then
8748 New_Discrs := Stored_Constraint (Parent_Type);
8749
8750 else
8751 New_Discrs := No_Elist;
8752 end if;
8753
8754 if Has_Discriminants (Derived_Type)
8755 and then Has_Private_Declaration (Derived_Type)
8756 and then Present (Discriminant_Constraint (Derived_Type))
8757 and then Present (New_Discrs)
8758 then
8759 -- Verify that constraints of the full view statically match
8760 -- those given in the partial view.
8761
8762 declare
8763 C1, C2 : Elmt_Id;
8764
8765 begin
8766 C1 := First_Elmt (New_Discrs);
8767 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8768 while Present (C1) and then Present (C2) loop
8769 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8770 or else
8771 (Is_OK_Static_Expression (Node (C1))
8772 and then Is_OK_Static_Expression (Node (C2))
8773 and then
8774 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8775 then
8776 null;
8777
8778 else
8779 if Constraint_Present then
8780 Error_Msg_N
8781 ("constraint not conformant to previous declaration",
8782 Node (C1));
8783 else
8784 Error_Msg_N
8785 ("constraint of full view is incompatible "
8786 & "with partial view", N);
8787 end if;
8788 end if;
8789
8790 Next_Elmt (C1);
8791 Next_Elmt (C2);
8792 end loop;
8793 end;
8794 end if;
8795
8796 -- Insert and analyze the declaration for the unconstrained base type
8797
8798 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8799
8800 New_Decl :=
8801 Make_Full_Type_Declaration (Loc,
8802 Defining_Identifier => New_Base,
8803 Type_Definition =>
8804 Make_Derived_Type_Definition (Loc,
8805 Abstract_Present => Abstract_Present (Type_Def),
8806 Limited_Present => Limited_Present (Type_Def),
8807 Subtype_Indication =>
8808 New_Occurrence_Of (Parent_Base, Loc),
8809 Record_Extension_Part =>
8810 Relocate_Node (Record_Extension_Part (Type_Def)),
8811 Interface_List => Interface_List (Type_Def)));
8812
8813 Set_Parent (New_Decl, Parent (N));
8814 Mark_Rewrite_Insertion (New_Decl);
8815 Insert_Before (N, New_Decl);
8816
8817 -- In the extension case, make sure ancestor is frozen appropriately
8818 -- (see also non-discriminated case below).
8819
8820 if Present (Record_Extension_Part (Type_Def))
8821 or else Is_Interface (Parent_Base)
8822 then
8823 Freeze_Before (New_Decl, Parent_Type);
8824 end if;
8825
8826 -- Note that this call passes False for the Derive_Subps parameter
8827 -- because subprogram derivation is deferred until after creating
8828 -- the subtype (see below).
8829
8830 Build_Derived_Type
8831 (New_Decl, Parent_Base, New_Base,
8832 Is_Completion => False, Derive_Subps => False);
8833
8834 -- ??? This needs re-examination to determine whether the
8835 -- above call can simply be replaced by a call to Analyze.
8836
8837 Set_Analyzed (New_Decl);
8838
8839 -- Insert and analyze the declaration for the constrained subtype
8840
8841 if Constraint_Present then
8842 New_Indic :=
8843 Make_Subtype_Indication (Loc,
8844 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8845 Constraint => Relocate_Node (Constraint (Indic)));
8846
8847 else
8848 declare
8849 Constr_List : constant List_Id := New_List;
8850 C : Elmt_Id;
8851 Expr : Node_Id;
8852
8853 begin
8854 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8855 while Present (C) loop
8856 Expr := Node (C);
8857
8858 -- It is safe here to call New_Copy_Tree since we called
8859 -- Force_Evaluation on each constraint previously
8860 -- in Build_Discriminant_Constraints.
8861
8862 Append (New_Copy_Tree (Expr), To => Constr_List);
8863
8864 Next_Elmt (C);
8865 end loop;
8866
8867 New_Indic :=
8868 Make_Subtype_Indication (Loc,
8869 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8870 Constraint =>
8871 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8872 end;
8873 end if;
8874
8875 Rewrite (N,
8876 Make_Subtype_Declaration (Loc,
8877 Defining_Identifier => Derived_Type,
8878 Subtype_Indication => New_Indic));
8879
8880 Analyze (N);
8881
8882 -- Derivation of subprograms must be delayed until the full subtype
8883 -- has been established, to ensure proper overriding of subprograms
8884 -- inherited by full types. If the derivations occurred as part of
8885 -- the call to Build_Derived_Type above, then the check for type
8886 -- conformance would fail because earlier primitive subprograms
8887 -- could still refer to the full type prior the change to the new
8888 -- subtype and hence would not match the new base type created here.
8889 -- Subprograms are not derived, however, when Derive_Subps is False
8890 -- (since otherwise there could be redundant derivations).
8891
8892 if Derive_Subps then
8893 Derive_Subprograms (Parent_Type, Derived_Type);
8894 end if;
8895
8896 -- For tagged types the Discriminant_Constraint of the new base itype
8897 -- is inherited from the first subtype so that no subtype conformance
8898 -- problem arise when the first subtype overrides primitive
8899 -- operations inherited by the implicit base type.
8900
8901 if Is_Tagged then
8902 Set_Discriminant_Constraint
8903 (New_Base, Discriminant_Constraint (Derived_Type));
8904 end if;
8905
8906 return;
8907 end if;
8908
8909 -- If we get here Derived_Type will have no discriminants or it will be
8910 -- a discriminated unconstrained base type.
8911
8912 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8913
8914 if Is_Tagged then
8915
8916 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8917 -- The declaration of a specific descendant of an interface type
8918 -- freezes the interface type (RM 13.14).
8919
8920 if not Private_Extension or else Is_Interface (Parent_Base) then
8921 Freeze_Before (N, Parent_Type);
8922 end if;
8923
8924 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8925 -- cannot be declared at a deeper level than its parent type is
8926 -- removed. The check on derivation within a generic body is also
8927 -- relaxed, but there's a restriction that a derived tagged type
8928 -- cannot be declared in a generic body if it's derived directly
8929 -- or indirectly from a formal type of that generic.
8930
8931 if Ada_Version >= Ada_2005 then
8932 if Present (Enclosing_Generic_Body (Derived_Type)) then
8933 declare
8934 Ancestor_Type : Entity_Id;
8935
8936 begin
8937 -- Check to see if any ancestor of the derived type is a
8938 -- formal type.
8939
8940 Ancestor_Type := Parent_Type;
8941 while not Is_Generic_Type (Ancestor_Type)
8942 and then Etype (Ancestor_Type) /= Ancestor_Type
8943 loop
8944 Ancestor_Type := Etype (Ancestor_Type);
8945 end loop;
8946
8947 -- If the derived type does have a formal type as an
8948 -- ancestor, then it's an error if the derived type is
8949 -- declared within the body of the generic unit that
8950 -- declares the formal type in its generic formal part. It's
8951 -- sufficient to check whether the ancestor type is declared
8952 -- inside the same generic body as the derived type (such as
8953 -- within a nested generic spec), in which case the
8954 -- derivation is legal. If the formal type is declared
8955 -- outside of that generic body, then it's guaranteed that
8956 -- the derived type is declared within the generic body of
8957 -- the generic unit declaring the formal type.
8958
8959 if Is_Generic_Type (Ancestor_Type)
8960 and then Enclosing_Generic_Body (Ancestor_Type) /=
8961 Enclosing_Generic_Body (Derived_Type)
8962 then
8963 Error_Msg_NE
8964 ("parent type of& must not be descendant of formal type"
8965 & " of an enclosing generic body",
8966 Indic, Derived_Type);
8967 end if;
8968 end;
8969 end if;
8970
8971 elsif Type_Access_Level (Derived_Type) /=
8972 Type_Access_Level (Parent_Type)
8973 and then not Is_Generic_Type (Derived_Type)
8974 then
8975 if Is_Controlled (Parent_Type) then
8976 Error_Msg_N
8977 ("controlled type must be declared at the library level",
8978 Indic);
8979 else
8980 Error_Msg_N
8981 ("type extension at deeper accessibility level than parent",
8982 Indic);
8983 end if;
8984
8985 else
8986 declare
8987 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8988 begin
8989 if Present (GB)
8990 and then GB /= Enclosing_Generic_Body (Parent_Base)
8991 then
8992 Error_Msg_NE
8993 ("parent type of& must not be outside generic body"
8994 & " (RM 3.9.1(4))",
8995 Indic, Derived_Type);
8996 end if;
8997 end;
8998 end if;
8999 end if;
9000
9001 -- Ada 2005 (AI-251)
9002
9003 if Ada_Version >= Ada_2005 and then Is_Tagged then
9004
9005 -- "The declaration of a specific descendant of an interface type
9006 -- freezes the interface type" (RM 13.14).
9007
9008 declare
9009 Iface : Node_Id;
9010 begin
9011 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9012 Iface := First (Interface_List (Type_Def));
9013 while Present (Iface) loop
9014 Freeze_Before (N, Etype (Iface));
9015 Next (Iface);
9016 end loop;
9017 end if;
9018 end;
9019 end if;
9020
9021 -- STEP 1b : preliminary cleanup of the full view of private types
9022
9023 -- If the type is already marked as having discriminants, then it's the
9024 -- completion of a private type or private extension and we need to
9025 -- retain the discriminants from the partial view if the current
9026 -- declaration has Discriminant_Specifications so that we can verify
9027 -- conformance. However, we must remove any existing components that
9028 -- were inherited from the parent (and attached in Copy_And_Swap)
9029 -- because the full type inherits all appropriate components anyway, and
9030 -- we do not want the partial view's components interfering.
9031
9032 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9033 Discrim := First_Discriminant (Derived_Type);
9034 loop
9035 Last_Discrim := Discrim;
9036 Next_Discriminant (Discrim);
9037 exit when No (Discrim);
9038 end loop;
9039
9040 Set_Last_Entity (Derived_Type, Last_Discrim);
9041
9042 -- In all other cases wipe out the list of inherited components (even
9043 -- inherited discriminants), it will be properly rebuilt here.
9044
9045 else
9046 Set_First_Entity (Derived_Type, Empty);
9047 Set_Last_Entity (Derived_Type, Empty);
9048 end if;
9049
9050 -- STEP 1c: Initialize some flags for the Derived_Type
9051
9052 -- The following flags must be initialized here so that
9053 -- Process_Discriminants can check that discriminants of tagged types do
9054 -- not have a default initial value and that access discriminants are
9055 -- only specified for limited records. For completeness, these flags are
9056 -- also initialized along with all the other flags below.
9057
9058 -- AI-419: Limitedness is not inherited from an interface parent, so to
9059 -- be limited in that case the type must be explicitly declared as
9060 -- limited. However, task and protected interfaces are always limited.
9061
9062 if Limited_Present (Type_Def) then
9063 Set_Is_Limited_Record (Derived_Type);
9064
9065 elsif Is_Limited_Record (Parent_Type)
9066 or else (Present (Full_View (Parent_Type))
9067 and then Is_Limited_Record (Full_View (Parent_Type)))
9068 then
9069 if not Is_Interface (Parent_Type)
9070 or else Is_Synchronized_Interface (Parent_Type)
9071 or else Is_Protected_Interface (Parent_Type)
9072 or else Is_Task_Interface (Parent_Type)
9073 then
9074 Set_Is_Limited_Record (Derived_Type);
9075 end if;
9076 end if;
9077
9078 -- STEP 2a: process discriminants of derived type if any
9079
9080 Push_Scope (Derived_Type);
9081
9082 if Discriminant_Specs then
9083 Set_Has_Unknown_Discriminants (Derived_Type, False);
9084
9085 -- The following call initializes fields Has_Discriminants and
9086 -- Discriminant_Constraint, unless we are processing the completion
9087 -- of a private type declaration.
9088
9089 Check_Or_Process_Discriminants (N, Derived_Type);
9090
9091 -- For untagged types, the constraint on the Parent_Type must be
9092 -- present and is used to rename the discriminants.
9093
9094 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9095 Error_Msg_N ("untagged parent must have discriminants", Indic);
9096
9097 elsif not Is_Tagged and then not Constraint_Present then
9098 Error_Msg_N
9099 ("discriminant constraint needed for derived untagged records",
9100 Indic);
9101
9102 -- Otherwise the parent subtype must be constrained unless we have a
9103 -- private extension.
9104
9105 elsif not Constraint_Present
9106 and then not Private_Extension
9107 and then not Is_Constrained (Parent_Type)
9108 then
9109 Error_Msg_N
9110 ("unconstrained type not allowed in this context", Indic);
9111
9112 elsif Constraint_Present then
9113 -- The following call sets the field Corresponding_Discriminant
9114 -- for the discriminants in the Derived_Type.
9115
9116 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9117
9118 -- For untagged types all new discriminants must rename
9119 -- discriminants in the parent. For private extensions new
9120 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9121
9122 Discrim := First_Discriminant (Derived_Type);
9123 while Present (Discrim) loop
9124 if not Is_Tagged
9125 and then No (Corresponding_Discriminant (Discrim))
9126 then
9127 Error_Msg_N
9128 ("new discriminants must constrain old ones", Discrim);
9129
9130 elsif Private_Extension
9131 and then Present (Corresponding_Discriminant (Discrim))
9132 then
9133 Error_Msg_N
9134 ("only static constraints allowed for parent"
9135 & " discriminants in the partial view", Indic);
9136 exit;
9137 end if;
9138
9139 -- If a new discriminant is used in the constraint, then its
9140 -- subtype must be statically compatible with the parent
9141 -- discriminant's subtype (3.7(15)).
9142
9143 -- However, if the record contains an array constrained by
9144 -- the discriminant but with some different bound, the compiler
9145 -- tries to create a smaller range for the discriminant type.
9146 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9147 -- the discriminant type is a scalar type, the check must use
9148 -- the original discriminant type in the parent declaration.
9149
9150 declare
9151 Corr_Disc : constant Entity_Id :=
9152 Corresponding_Discriminant (Discrim);
9153 Disc_Type : constant Entity_Id := Etype (Discrim);
9154 Corr_Type : Entity_Id;
9155
9156 begin
9157 if Present (Corr_Disc) then
9158 if Is_Scalar_Type (Disc_Type) then
9159 Corr_Type :=
9160 Entity (Discriminant_Type (Parent (Corr_Disc)));
9161 else
9162 Corr_Type := Etype (Corr_Disc);
9163 end if;
9164
9165 if not
9166 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9167 then
9168 Error_Msg_N
9169 ("subtype must be compatible "
9170 & "with parent discriminant",
9171 Discrim);
9172 end if;
9173 end if;
9174 end;
9175
9176 Next_Discriminant (Discrim);
9177 end loop;
9178
9179 -- Check whether the constraints of the full view statically
9180 -- match those imposed by the parent subtype [7.3(13)].
9181
9182 if Present (Stored_Constraint (Derived_Type)) then
9183 declare
9184 C1, C2 : Elmt_Id;
9185
9186 begin
9187 C1 := First_Elmt (Discs);
9188 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9189 while Present (C1) and then Present (C2) loop
9190 if not
9191 Fully_Conformant_Expressions (Node (C1), Node (C2))
9192 then
9193 Error_Msg_N
9194 ("not conformant with previous declaration",
9195 Node (C1));
9196 end if;
9197
9198 Next_Elmt (C1);
9199 Next_Elmt (C2);
9200 end loop;
9201 end;
9202 end if;
9203 end if;
9204
9205 -- STEP 2b: No new discriminants, inherit discriminants if any
9206
9207 else
9208 if Private_Extension then
9209 Set_Has_Unknown_Discriminants
9210 (Derived_Type,
9211 Has_Unknown_Discriminants (Parent_Type)
9212 or else Unknown_Discriminants_Present (N));
9213
9214 -- The partial view of the parent may have unknown discriminants,
9215 -- but if the full view has discriminants and the parent type is
9216 -- in scope they must be inherited.
9217
9218 elsif Has_Unknown_Discriminants (Parent_Type)
9219 and then
9220 (not Has_Discriminants (Parent_Type)
9221 or else not In_Open_Scopes (Scope (Parent_Base)))
9222 then
9223 Set_Has_Unknown_Discriminants (Derived_Type);
9224 end if;
9225
9226 if not Has_Unknown_Discriminants (Derived_Type)
9227 and then not Has_Unknown_Discriminants (Parent_Base)
9228 and then Has_Discriminants (Parent_Type)
9229 then
9230 Inherit_Discrims := True;
9231 Set_Has_Discriminants
9232 (Derived_Type, True);
9233 Set_Discriminant_Constraint
9234 (Derived_Type, Discriminant_Constraint (Parent_Base));
9235 end if;
9236
9237 -- The following test is true for private types (remember
9238 -- transformation 5. is not applied to those) and in an error
9239 -- situation.
9240
9241 if Constraint_Present then
9242 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9243 end if;
9244
9245 -- For now mark a new derived type as constrained only if it has no
9246 -- discriminants. At the end of Build_Derived_Record_Type we properly
9247 -- set this flag in the case of private extensions. See comments in
9248 -- point 9. just before body of Build_Derived_Record_Type.
9249
9250 Set_Is_Constrained
9251 (Derived_Type,
9252 not (Inherit_Discrims
9253 or else Has_Unknown_Discriminants (Derived_Type)));
9254 end if;
9255
9256 -- STEP 3: initialize fields of derived type
9257
9258 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9259 Set_Stored_Constraint (Derived_Type, No_Elist);
9260
9261 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9262 -- but cannot be interfaces
9263
9264 if not Private_Extension
9265 and then Ekind (Derived_Type) /= E_Private_Type
9266 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9267 then
9268 if Interface_Present (Type_Def) then
9269 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9270 end if;
9271
9272 Set_Interfaces (Derived_Type, No_Elist);
9273 end if;
9274
9275 -- Fields inherited from the Parent_Type
9276
9277 Set_Has_Specified_Layout
9278 (Derived_Type, Has_Specified_Layout (Parent_Type));
9279 Set_Is_Limited_Composite
9280 (Derived_Type, Is_Limited_Composite (Parent_Type));
9281 Set_Is_Private_Composite
9282 (Derived_Type, Is_Private_Composite (Parent_Type));
9283
9284 if Is_Tagged_Type (Parent_Type) then
9285 Set_No_Tagged_Streams_Pragma
9286 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9287 end if;
9288
9289 -- Fields inherited from the Parent_Base
9290
9291 Set_Has_Controlled_Component
9292 (Derived_Type, Has_Controlled_Component (Parent_Base));
9293 Set_Has_Non_Standard_Rep
9294 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9295 Set_Has_Primitive_Operations
9296 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9297
9298 -- Set fields for private derived types
9299
9300 if Is_Private_Type (Derived_Type) then
9301 Set_Depends_On_Private (Derived_Type, True);
9302 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9303 end if;
9304
9305 -- Inherit fields for non-private types. If this is the completion of a
9306 -- derivation from a private type, the parent itself is private and the
9307 -- attributes come from its full view, which must be present.
9308
9309 if Is_Record_Type (Derived_Type) then
9310 declare
9311 Parent_Full : Entity_Id;
9312
9313 begin
9314 if Is_Private_Type (Parent_Base)
9315 and then not Is_Record_Type (Parent_Base)
9316 then
9317 Parent_Full := Full_View (Parent_Base);
9318 else
9319 Parent_Full := Parent_Base;
9320 end if;
9321
9322 Set_Component_Alignment
9323 (Derived_Type, Component_Alignment (Parent_Full));
9324 Set_C_Pass_By_Copy
9325 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9326 Set_Has_Complex_Representation
9327 (Derived_Type, Has_Complex_Representation (Parent_Full));
9328
9329 -- For untagged types, inherit the layout by default to avoid
9330 -- costly changes of representation for type conversions.
9331
9332 if not Is_Tagged then
9333 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9334 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9335 end if;
9336 end;
9337 end if;
9338
9339 -- Set fields for tagged types
9340
9341 if Is_Tagged then
9342 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9343
9344 -- All tagged types defined in Ada.Finalization are controlled
9345
9346 if Chars (Scope (Derived_Type)) = Name_Finalization
9347 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9348 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9349 then
9350 Set_Is_Controlled_Active (Derived_Type);
9351 else
9352 Set_Is_Controlled_Active
9353 (Derived_Type, Is_Controlled_Active (Parent_Base));
9354 end if;
9355
9356 -- Minor optimization: there is no need to generate the class-wide
9357 -- entity associated with an underlying record view.
9358
9359 if not Is_Underlying_Record_View (Derived_Type) then
9360 Make_Class_Wide_Type (Derived_Type);
9361 end if;
9362
9363 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9364
9365 if Has_Discriminants (Derived_Type)
9366 and then Constraint_Present
9367 then
9368 Set_Stored_Constraint
9369 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9370 end if;
9371
9372 if Ada_Version >= Ada_2005 then
9373 declare
9374 Ifaces_List : Elist_Id;
9375
9376 begin
9377 -- Checks rules 3.9.4 (13/2 and 14/2)
9378
9379 if Comes_From_Source (Derived_Type)
9380 and then not Is_Private_Type (Derived_Type)
9381 and then Is_Interface (Parent_Type)
9382 and then not Is_Interface (Derived_Type)
9383 then
9384 if Is_Task_Interface (Parent_Type) then
9385 Error_Msg_N
9386 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9387 Derived_Type);
9388
9389 elsif Is_Protected_Interface (Parent_Type) then
9390 Error_Msg_N
9391 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9392 Derived_Type);
9393 end if;
9394 end if;
9395
9396 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9397
9398 Check_Interfaces (N, Type_Def);
9399
9400 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9401 -- not already in the parents.
9402
9403 Collect_Interfaces
9404 (T => Derived_Type,
9405 Ifaces_List => Ifaces_List,
9406 Exclude_Parents => True);
9407
9408 Set_Interfaces (Derived_Type, Ifaces_List);
9409
9410 -- If the derived type is the anonymous type created for
9411 -- a declaration whose parent has a constraint, propagate
9412 -- the interface list to the source type. This must be done
9413 -- prior to the completion of the analysis of the source type
9414 -- because the components in the extension may contain current
9415 -- instances whose legality depends on some ancestor.
9416
9417 if Is_Itype (Derived_Type) then
9418 declare
9419 Def : constant Node_Id :=
9420 Associated_Node_For_Itype (Derived_Type);
9421 begin
9422 if Present (Def)
9423 and then Nkind (Def) = N_Full_Type_Declaration
9424 then
9425 Set_Interfaces
9426 (Defining_Identifier (Def), Ifaces_List);
9427 end if;
9428 end;
9429 end if;
9430
9431 -- A type extension is automatically Ghost when one of its
9432 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9433 -- also inherited when the parent type is Ghost, but this is
9434 -- done in Build_Derived_Type as the mechanism also handles
9435 -- untagged derivations.
9436
9437 if Implements_Ghost_Interface (Derived_Type) then
9438 Set_Is_Ghost_Entity (Derived_Type);
9439 end if;
9440 end;
9441 end if;
9442 end if;
9443
9444 -- STEP 4: Inherit components from the parent base and constrain them.
9445 -- Apply the second transformation described in point 6. above.
9446
9447 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9448 or else not Has_Discriminants (Parent_Type)
9449 or else not Is_Constrained (Parent_Type)
9450 then
9451 Constrs := Discs;
9452 else
9453 Constrs := Discriminant_Constraint (Parent_Type);
9454 end if;
9455
9456 Assoc_List :=
9457 Inherit_Components
9458 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9459
9460 -- STEP 5a: Copy the parent record declaration for untagged types
9461
9462 Set_Has_Implicit_Dereference
9463 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9464
9465 if not Is_Tagged then
9466
9467 -- Discriminant_Constraint (Derived_Type) has been properly
9468 -- constructed. Save it and temporarily set it to Empty because we
9469 -- do not want the call to New_Copy_Tree below to mess this list.
9470
9471 if Has_Discriminants (Derived_Type) then
9472 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9473 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9474 else
9475 Save_Discr_Constr := No_Elist;
9476 end if;
9477
9478 -- Save the Etype field of Derived_Type. It is correctly set now,
9479 -- but the call to New_Copy tree may remap it to point to itself,
9480 -- which is not what we want. Ditto for the Next_Entity field.
9481
9482 Save_Etype := Etype (Derived_Type);
9483 Save_Next_Entity := Next_Entity (Derived_Type);
9484
9485 -- Assoc_List maps all stored discriminants in the Parent_Base to
9486 -- stored discriminants in the Derived_Type. It is fundamental that
9487 -- no types or itypes with discriminants other than the stored
9488 -- discriminants appear in the entities declared inside
9489 -- Derived_Type, since the back end cannot deal with it.
9490
9491 New_Decl :=
9492 New_Copy_Tree
9493 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9494 Copy_Dimensions_Of_Components (Derived_Type);
9495
9496 -- Restore the fields saved prior to the New_Copy_Tree call
9497 -- and compute the stored constraint.
9498
9499 Set_Etype (Derived_Type, Save_Etype);
9500 Link_Entities (Derived_Type, Save_Next_Entity);
9501
9502 if Has_Discriminants (Derived_Type) then
9503 Set_Discriminant_Constraint
9504 (Derived_Type, Save_Discr_Constr);
9505 Set_Stored_Constraint
9506 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9507
9508 Replace_Components (Derived_Type, New_Decl);
9509 end if;
9510
9511 -- Insert the new derived type declaration
9512
9513 Rewrite (N, New_Decl);
9514
9515 -- STEP 5b: Complete the processing for record extensions in generics
9516
9517 -- There is no completion for record extensions declared in the
9518 -- parameter part of a generic, so we need to complete processing for
9519 -- these generic record extensions here. The Record_Type_Definition call
9520 -- will change the Ekind of the components from E_Void to E_Component.
9521
9522 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9523 Record_Type_Definition (Empty, Derived_Type);
9524
9525 -- STEP 5c: Process the record extension for non private tagged types
9526
9527 elsif not Private_Extension then
9528 Expand_Record_Extension (Derived_Type, Type_Def);
9529
9530 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9531 -- derived type to propagate some semantic information. This led
9532 -- to other ASIS failures and has been removed.
9533
9534 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9535 -- implemented interfaces if we are in expansion mode
9536
9537 if Expander_Active
9538 and then Has_Interfaces (Derived_Type)
9539 then
9540 Add_Interface_Tag_Components (N, Derived_Type);
9541 end if;
9542
9543 -- Analyze the record extension
9544
9545 Record_Type_Definition
9546 (Record_Extension_Part (Type_Def), Derived_Type);
9547 end if;
9548
9549 End_Scope;
9550
9551 -- Nothing else to do if there is an error in the derivation.
9552 -- An unusual case: the full view may be derived from a type in an
9553 -- instance, when the partial view was used illegally as an actual
9554 -- in that instance, leading to a circular definition.
9555
9556 if Etype (Derived_Type) = Any_Type
9557 or else Etype (Parent_Type) = Derived_Type
9558 then
9559 return;
9560 end if;
9561
9562 -- Set delayed freeze and then derive subprograms, we need to do
9563 -- this in this order so that derived subprograms inherit the
9564 -- derived freeze if necessary.
9565
9566 Set_Has_Delayed_Freeze (Derived_Type);
9567
9568 if Derive_Subps then
9569 Derive_Subprograms (Parent_Type, Derived_Type);
9570 end if;
9571
9572 -- If we have a private extension which defines a constrained derived
9573 -- type mark as constrained here after we have derived subprograms. See
9574 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9575
9576 if Private_Extension and then Inherit_Discrims then
9577 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9578 Set_Is_Constrained (Derived_Type, True);
9579 Set_Discriminant_Constraint (Derived_Type, Discs);
9580
9581 elsif Is_Constrained (Parent_Type) then
9582 Set_Is_Constrained
9583 (Derived_Type, True);
9584 Set_Discriminant_Constraint
9585 (Derived_Type, Discriminant_Constraint (Parent_Type));
9586 end if;
9587 end if;
9588
9589 -- Update the class-wide type, which shares the now-completed entity
9590 -- list with its specific type. In case of underlying record views,
9591 -- we do not generate the corresponding class wide entity.
9592
9593 if Is_Tagged
9594 and then not Is_Underlying_Record_View (Derived_Type)
9595 then
9596 Set_First_Entity
9597 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9598 Set_Last_Entity
9599 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9600 end if;
9601
9602 Check_Function_Writable_Actuals (N);
9603 end Build_Derived_Record_Type;
9604
9605 ------------------------
9606 -- Build_Derived_Type --
9607 ------------------------
9608
9609 procedure Build_Derived_Type
9610 (N : Node_Id;
9611 Parent_Type : Entity_Id;
9612 Derived_Type : Entity_Id;
9613 Is_Completion : Boolean;
9614 Derive_Subps : Boolean := True)
9615 is
9616 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9617
9618 begin
9619 -- Set common attributes
9620
9621 Set_Scope (Derived_Type, Current_Scope);
9622 Set_Etype (Derived_Type, Parent_Base);
9623 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9624 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9625
9626 Set_Size_Info (Derived_Type, Parent_Type);
9627 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9628
9629 Set_Is_Controlled_Active
9630 (Derived_Type, Is_Controlled_Active (Parent_Type));
9631
9632 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9633 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9634 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9635
9636 if Is_Tagged_Type (Derived_Type) then
9637 Set_No_Tagged_Streams_Pragma
9638 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9639 end if;
9640
9641 -- If the parent has primitive routines, set the derived type link
9642
9643 if Has_Primitive_Operations (Parent_Type) then
9644 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9645 end if;
9646
9647 -- If the parent type is a private subtype, the convention on the base
9648 -- type may be set in the private part, and not propagated to the
9649 -- subtype until later, so we obtain the convention from the base type.
9650
9651 Set_Convention (Derived_Type, Convention (Parent_Base));
9652
9653 -- Set SSO default for record or array type
9654
9655 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9656 and then Is_Base_Type (Derived_Type)
9657 then
9658 Set_Default_SSO (Derived_Type);
9659 end if;
9660
9661 -- A derived type inherits the Default_Initial_Condition pragma coming
9662 -- from any parent type within the derivation chain.
9663
9664 if Has_DIC (Parent_Type) then
9665 Set_Has_Inherited_DIC (Derived_Type);
9666 end if;
9667
9668 -- A derived type inherits any class-wide invariants coming from a
9669 -- parent type or an interface. Note that the invariant procedure of
9670 -- the parent type should not be inherited because the derived type may
9671 -- define invariants of its own.
9672
9673 if not Is_Interface (Derived_Type) then
9674 if Has_Inherited_Invariants (Parent_Type)
9675 or else Has_Inheritable_Invariants (Parent_Type)
9676 then
9677 Set_Has_Inherited_Invariants (Derived_Type);
9678
9679 elsif Is_Concurrent_Type (Derived_Type)
9680 or else Is_Tagged_Type (Derived_Type)
9681 then
9682 declare
9683 Iface : Entity_Id;
9684 Ifaces : Elist_Id;
9685 Iface_Elmt : Elmt_Id;
9686
9687 begin
9688 Collect_Interfaces
9689 (T => Derived_Type,
9690 Ifaces_List => Ifaces,
9691 Exclude_Parents => True);
9692
9693 if Present (Ifaces) then
9694 Iface_Elmt := First_Elmt (Ifaces);
9695 while Present (Iface_Elmt) loop
9696 Iface := Node (Iface_Elmt);
9697
9698 if Has_Inheritable_Invariants (Iface) then
9699 Set_Has_Inherited_Invariants (Derived_Type);
9700 exit;
9701 end if;
9702
9703 Next_Elmt (Iface_Elmt);
9704 end loop;
9705 end if;
9706 end;
9707 end if;
9708 end if;
9709
9710 -- We similarly inherit predicates. Note that for scalar derived types
9711 -- the predicate is inherited from the first subtype, and not from its
9712 -- (anonymous) base type.
9713
9714 if Has_Predicates (Parent_Type)
9715 or else Has_Predicates (First_Subtype (Parent_Type))
9716 then
9717 Set_Has_Predicates (Derived_Type);
9718 end if;
9719
9720 -- The derived type inherits representation clauses from the parent
9721 -- type, and from any interfaces.
9722
9723 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9724
9725 declare
9726 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9727 begin
9728 while Present (Iface) loop
9729 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9730 Next (Iface);
9731 end loop;
9732 end;
9733
9734 -- If the parent type has delayed rep aspects, then mark the derived
9735 -- type as possibly inheriting a delayed rep aspect.
9736
9737 if Has_Delayed_Rep_Aspects (Parent_Type) then
9738 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9739 end if;
9740
9741 -- A derived type becomes Ghost when its parent type is also Ghost
9742 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9743 -- directly inherited because the Ghost policy in effect may differ.
9744
9745 if Is_Ghost_Entity (Parent_Type) then
9746 Set_Is_Ghost_Entity (Derived_Type);
9747 end if;
9748
9749 -- Type dependent processing
9750
9751 case Ekind (Parent_Type) is
9752 when Numeric_Kind =>
9753 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9754
9755 when Array_Kind =>
9756 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9757
9758 when Class_Wide_Kind
9759 | E_Record_Subtype
9760 | E_Record_Type
9761 =>
9762 Build_Derived_Record_Type
9763 (N, Parent_Type, Derived_Type, Derive_Subps);
9764 return;
9765
9766 when Enumeration_Kind =>
9767 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9768
9769 when Access_Kind =>
9770 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9771
9772 when Incomplete_Or_Private_Kind =>
9773 Build_Derived_Private_Type
9774 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9775
9776 -- For discriminated types, the derivation includes deriving
9777 -- primitive operations. For others it is done below.
9778
9779 if Is_Tagged_Type (Parent_Type)
9780 or else Has_Discriminants (Parent_Type)
9781 or else (Present (Full_View (Parent_Type))
9782 and then Has_Discriminants (Full_View (Parent_Type)))
9783 then
9784 return;
9785 end if;
9786
9787 when Concurrent_Kind =>
9788 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9789
9790 when others =>
9791 raise Program_Error;
9792 end case;
9793
9794 -- Nothing more to do if some error occurred
9795
9796 if Etype (Derived_Type) = Any_Type then
9797 return;
9798 end if;
9799
9800 -- Set delayed freeze and then derive subprograms, we need to do this
9801 -- in this order so that derived subprograms inherit the derived freeze
9802 -- if necessary.
9803
9804 Set_Has_Delayed_Freeze (Derived_Type);
9805
9806 if Derive_Subps then
9807 Derive_Subprograms (Parent_Type, Derived_Type);
9808 end if;
9809
9810 Set_Has_Primitive_Operations
9811 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9812 end Build_Derived_Type;
9813
9814 -----------------------
9815 -- Build_Discriminal --
9816 -----------------------
9817
9818 procedure Build_Discriminal (Discrim : Entity_Id) is
9819 D_Minal : Entity_Id;
9820 CR_Disc : Entity_Id;
9821
9822 begin
9823 -- A discriminal has the same name as the discriminant
9824
9825 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9826
9827 Set_Ekind (D_Minal, E_In_Parameter);
9828 Set_Mechanism (D_Minal, Default_Mechanism);
9829 Set_Etype (D_Minal, Etype (Discrim));
9830 Set_Scope (D_Minal, Current_Scope);
9831 Set_Parent (D_Minal, Parent (Discrim));
9832
9833 Set_Discriminal (Discrim, D_Minal);
9834 Set_Discriminal_Link (D_Minal, Discrim);
9835
9836 -- For task types, build at once the discriminants of the corresponding
9837 -- record, which are needed if discriminants are used in entry defaults
9838 -- and in family bounds.
9839
9840 if Is_Concurrent_Type (Current_Scope)
9841 or else
9842 Is_Limited_Type (Current_Scope)
9843 then
9844 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9845
9846 Set_Ekind (CR_Disc, E_In_Parameter);
9847 Set_Mechanism (CR_Disc, Default_Mechanism);
9848 Set_Etype (CR_Disc, Etype (Discrim));
9849 Set_Scope (CR_Disc, Current_Scope);
9850 Set_Discriminal_Link (CR_Disc, Discrim);
9851 Set_CR_Discriminant (Discrim, CR_Disc);
9852 end if;
9853 end Build_Discriminal;
9854
9855 ------------------------------------
9856 -- Build_Discriminant_Constraints --
9857 ------------------------------------
9858
9859 function Build_Discriminant_Constraints
9860 (T : Entity_Id;
9861 Def : Node_Id;
9862 Derived_Def : Boolean := False) return Elist_Id
9863 is
9864 C : constant Node_Id := Constraint (Def);
9865 Nb_Discr : constant Nat := Number_Discriminants (T);
9866
9867 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9868 -- Saves the expression corresponding to a given discriminant in T
9869
9870 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9871 -- Return the Position number within array Discr_Expr of a discriminant
9872 -- D within the discriminant list of the discriminated type T.
9873
9874 procedure Process_Discriminant_Expression
9875 (Expr : Node_Id;
9876 D : Entity_Id);
9877 -- If this is a discriminant constraint on a partial view, do not
9878 -- generate an overflow check on the discriminant expression. The check
9879 -- will be generated when constraining the full view. Otherwise the
9880 -- backend creates duplicate symbols for the temporaries corresponding
9881 -- to the expressions to be checked, causing spurious assembler errors.
9882
9883 ------------------
9884 -- Pos_Of_Discr --
9885 ------------------
9886
9887 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9888 Disc : Entity_Id;
9889
9890 begin
9891 Disc := First_Discriminant (T);
9892 for J in Discr_Expr'Range loop
9893 if Disc = D then
9894 return J;
9895 end if;
9896
9897 Next_Discriminant (Disc);
9898 end loop;
9899
9900 -- Note: Since this function is called on discriminants that are
9901 -- known to belong to the discriminated type, falling through the
9902 -- loop with no match signals an internal compiler error.
9903
9904 raise Program_Error;
9905 end Pos_Of_Discr;
9906
9907 -------------------------------------
9908 -- Process_Discriminant_Expression --
9909 -------------------------------------
9910
9911 procedure Process_Discriminant_Expression
9912 (Expr : Node_Id;
9913 D : Entity_Id)
9914 is
9915 BDT : constant Entity_Id := Base_Type (Etype (D));
9916
9917 begin
9918 -- If this is a discriminant constraint on a partial view, do
9919 -- not generate an overflow on the discriminant expression. The
9920 -- check will be generated when constraining the full view.
9921
9922 if Is_Private_Type (T)
9923 and then Present (Full_View (T))
9924 then
9925 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9926 else
9927 Analyze_And_Resolve (Expr, BDT);
9928 end if;
9929 end Process_Discriminant_Expression;
9930
9931 -- Declarations local to Build_Discriminant_Constraints
9932
9933 Discr : Entity_Id;
9934 E : Entity_Id;
9935 Elist : constant Elist_Id := New_Elmt_List;
9936
9937 Constr : Node_Id;
9938 Expr : Node_Id;
9939 Id : Node_Id;
9940 Position : Nat;
9941 Found : Boolean;
9942
9943 Discrim_Present : Boolean := False;
9944
9945 -- Start of processing for Build_Discriminant_Constraints
9946
9947 begin
9948 -- The following loop will process positional associations only.
9949 -- For a positional association, the (single) discriminant is
9950 -- implicitly specified by position, in textual order (RM 3.7.2).
9951
9952 Discr := First_Discriminant (T);
9953 Constr := First (Constraints (C));
9954 for D in Discr_Expr'Range loop
9955 exit when Nkind (Constr) = N_Discriminant_Association;
9956
9957 if No (Constr) then
9958 Error_Msg_N ("too few discriminants given in constraint", C);
9959 return New_Elmt_List;
9960
9961 elsif Nkind (Constr) = N_Range
9962 or else (Nkind (Constr) = N_Attribute_Reference
9963 and then Attribute_Name (Constr) = Name_Range)
9964 then
9965 Error_Msg_N
9966 ("a range is not a valid discriminant constraint", Constr);
9967 Discr_Expr (D) := Error;
9968
9969 elsif Nkind (Constr) = N_Subtype_Indication then
9970 Error_Msg_N
9971 ("a subtype indication is not a valid discriminant constraint",
9972 Constr);
9973 Discr_Expr (D) := Error;
9974
9975 else
9976 Process_Discriminant_Expression (Constr, Discr);
9977 Discr_Expr (D) := Constr;
9978 end if;
9979
9980 Next_Discriminant (Discr);
9981 Next (Constr);
9982 end loop;
9983
9984 if No (Discr) and then Present (Constr) then
9985 Error_Msg_N ("too many discriminants given in constraint", Constr);
9986 return New_Elmt_List;
9987 end if;
9988
9989 -- Named associations can be given in any order, but if both positional
9990 -- and named associations are used in the same discriminant constraint,
9991 -- then positional associations must occur first, at their normal
9992 -- position. Hence once a named association is used, the rest of the
9993 -- discriminant constraint must use only named associations.
9994
9995 while Present (Constr) loop
9996
9997 -- Positional association forbidden after a named association
9998
9999 if Nkind (Constr) /= N_Discriminant_Association then
10000 Error_Msg_N ("positional association follows named one", Constr);
10001 return New_Elmt_List;
10002
10003 -- Otherwise it is a named association
10004
10005 else
10006 -- E records the type of the discriminants in the named
10007 -- association. All the discriminants specified in the same name
10008 -- association must have the same type.
10009
10010 E := Empty;
10011
10012 -- Search the list of discriminants in T to see if the simple name
10013 -- given in the constraint matches any of them.
10014
10015 Id := First (Selector_Names (Constr));
10016 while Present (Id) loop
10017 Found := False;
10018
10019 -- If Original_Discriminant is present, we are processing a
10020 -- generic instantiation and this is an instance node. We need
10021 -- to find the name of the corresponding discriminant in the
10022 -- actual record type T and not the name of the discriminant in
10023 -- the generic formal. Example:
10024
10025 -- generic
10026 -- type G (D : int) is private;
10027 -- package P is
10028 -- subtype W is G (D => 1);
10029 -- end package;
10030 -- type Rec (X : int) is record ... end record;
10031 -- package Q is new P (G => Rec);
10032
10033 -- At the point of the instantiation, formal type G is Rec
10034 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10035 -- which really looks like "subtype W is Rec (D => 1);" at
10036 -- the point of instantiation, we want to find the discriminant
10037 -- that corresponds to D in Rec, i.e. X.
10038
10039 if Present (Original_Discriminant (Id))
10040 and then In_Instance
10041 then
10042 Discr := Find_Corresponding_Discriminant (Id, T);
10043 Found := True;
10044
10045 else
10046 Discr := First_Discriminant (T);
10047 while Present (Discr) loop
10048 if Chars (Discr) = Chars (Id) then
10049 Found := True;
10050 exit;
10051 end if;
10052
10053 Next_Discriminant (Discr);
10054 end loop;
10055
10056 if not Found then
10057 Error_Msg_N ("& does not match any discriminant", Id);
10058 return New_Elmt_List;
10059
10060 -- If the parent type is a generic formal, preserve the
10061 -- name of the discriminant for subsequent instances.
10062 -- see comment at the beginning of this if statement.
10063
10064 elsif Is_Generic_Type (Root_Type (T)) then
10065 Set_Original_Discriminant (Id, Discr);
10066 end if;
10067 end if;
10068
10069 Position := Pos_Of_Discr (T, Discr);
10070
10071 if Present (Discr_Expr (Position)) then
10072 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10073
10074 else
10075 -- Each discriminant specified in the same named association
10076 -- must be associated with a separate copy of the
10077 -- corresponding expression.
10078
10079 if Present (Next (Id)) then
10080 Expr := New_Copy_Tree (Expression (Constr));
10081 Set_Parent (Expr, Parent (Expression (Constr)));
10082 else
10083 Expr := Expression (Constr);
10084 end if;
10085
10086 Discr_Expr (Position) := Expr;
10087 Process_Discriminant_Expression (Expr, Discr);
10088 end if;
10089
10090 -- A discriminant association with more than one discriminant
10091 -- name is only allowed if the named discriminants are all of
10092 -- the same type (RM 3.7.1(8)).
10093
10094 if E = Empty then
10095 E := Base_Type (Etype (Discr));
10096
10097 elsif Base_Type (Etype (Discr)) /= E then
10098 Error_Msg_N
10099 ("all discriminants in an association " &
10100 "must have the same type", Id);
10101 end if;
10102
10103 Next (Id);
10104 end loop;
10105 end if;
10106
10107 Next (Constr);
10108 end loop;
10109
10110 -- A discriminant constraint must provide exactly one value for each
10111 -- discriminant of the type (RM 3.7.1(8)).
10112
10113 for J in Discr_Expr'Range loop
10114 if No (Discr_Expr (J)) then
10115 Error_Msg_N ("too few discriminants given in constraint", C);
10116 return New_Elmt_List;
10117 end if;
10118 end loop;
10119
10120 -- Determine if there are discriminant expressions in the constraint
10121
10122 for J in Discr_Expr'Range loop
10123 if Denotes_Discriminant
10124 (Discr_Expr (J), Check_Concurrent => True)
10125 then
10126 Discrim_Present := True;
10127 end if;
10128 end loop;
10129
10130 -- Build an element list consisting of the expressions given in the
10131 -- discriminant constraint and apply the appropriate checks. The list
10132 -- is constructed after resolving any named discriminant associations
10133 -- and therefore the expressions appear in the textual order of the
10134 -- discriminants.
10135
10136 Discr := First_Discriminant (T);
10137 for J in Discr_Expr'Range loop
10138 if Discr_Expr (J) /= Error then
10139 Append_Elmt (Discr_Expr (J), Elist);
10140
10141 -- If any of the discriminant constraints is given by a
10142 -- discriminant and we are in a derived type declaration we
10143 -- have a discriminant renaming. Establish link between new
10144 -- and old discriminant. The new discriminant has an implicit
10145 -- dereference if the old one does.
10146
10147 if Denotes_Discriminant (Discr_Expr (J)) then
10148 if Derived_Def then
10149 declare
10150 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10151
10152 begin
10153 Set_Corresponding_Discriminant (New_Discr, Discr);
10154 Set_Has_Implicit_Dereference (New_Discr,
10155 Has_Implicit_Dereference (Discr));
10156 end;
10157 end if;
10158
10159 -- Force the evaluation of non-discriminant expressions.
10160 -- If we have found a discriminant in the constraint 3.4(26)
10161 -- and 3.8(18) demand that no range checks are performed are
10162 -- after evaluation. If the constraint is for a component
10163 -- definition that has a per-object constraint, expressions are
10164 -- evaluated but not checked either. In all other cases perform
10165 -- a range check.
10166
10167 else
10168 if Discrim_Present then
10169 null;
10170
10171 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10172 and then Has_Per_Object_Constraint
10173 (Defining_Identifier (Parent (Parent (Def))))
10174 then
10175 null;
10176
10177 elsif Is_Access_Type (Etype (Discr)) then
10178 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10179
10180 else
10181 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10182 end if;
10183
10184 Force_Evaluation (Discr_Expr (J));
10185 end if;
10186
10187 -- Check that the designated type of an access discriminant's
10188 -- expression is not a class-wide type unless the discriminant's
10189 -- designated type is also class-wide.
10190
10191 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10192 and then not Is_Class_Wide_Type
10193 (Designated_Type (Etype (Discr)))
10194 and then Etype (Discr_Expr (J)) /= Any_Type
10195 and then Is_Class_Wide_Type
10196 (Designated_Type (Etype (Discr_Expr (J))))
10197 then
10198 Wrong_Type (Discr_Expr (J), Etype (Discr));
10199
10200 elsif Is_Access_Type (Etype (Discr))
10201 and then not Is_Access_Constant (Etype (Discr))
10202 and then Is_Access_Type (Etype (Discr_Expr (J)))
10203 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10204 then
10205 Error_Msg_NE
10206 ("constraint for discriminant& must be access to variable",
10207 Def, Discr);
10208 end if;
10209 end if;
10210
10211 Next_Discriminant (Discr);
10212 end loop;
10213
10214 return Elist;
10215 end Build_Discriminant_Constraints;
10216
10217 ---------------------------------
10218 -- Build_Discriminated_Subtype --
10219 ---------------------------------
10220
10221 procedure Build_Discriminated_Subtype
10222 (T : Entity_Id;
10223 Def_Id : Entity_Id;
10224 Elist : Elist_Id;
10225 Related_Nod : Node_Id;
10226 For_Access : Boolean := False)
10227 is
10228 Has_Discrs : constant Boolean := Has_Discriminants (T);
10229 Constrained : constant Boolean :=
10230 (Has_Discrs
10231 and then not Is_Empty_Elmt_List (Elist)
10232 and then not Is_Class_Wide_Type (T))
10233 or else Is_Constrained (T);
10234
10235 begin
10236 if Ekind (T) = E_Record_Type then
10237 Set_Ekind (Def_Id, E_Record_Subtype);
10238
10239 -- Inherit preelaboration flag from base, for types for which it
10240 -- may have been set: records, private types, protected types.
10241
10242 Set_Known_To_Have_Preelab_Init
10243 (Def_Id, Known_To_Have_Preelab_Init (T));
10244
10245 elsif Ekind (T) = E_Task_Type then
10246 Set_Ekind (Def_Id, E_Task_Subtype);
10247
10248 elsif Ekind (T) = E_Protected_Type then
10249 Set_Ekind (Def_Id, E_Protected_Subtype);
10250 Set_Known_To_Have_Preelab_Init
10251 (Def_Id, Known_To_Have_Preelab_Init (T));
10252
10253 elsif Is_Private_Type (T) then
10254 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10255 Set_Known_To_Have_Preelab_Init
10256 (Def_Id, Known_To_Have_Preelab_Init (T));
10257
10258 -- Private subtypes may have private dependents
10259
10260 Set_Private_Dependents (Def_Id, New_Elmt_List);
10261
10262 elsif Is_Class_Wide_Type (T) then
10263 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10264
10265 else
10266 -- Incomplete type. Attach subtype to list of dependents, to be
10267 -- completed with full view of parent type, unless is it the
10268 -- designated subtype of a record component within an init_proc.
10269 -- This last case arises for a component of an access type whose
10270 -- designated type is incomplete (e.g. a Taft Amendment type).
10271 -- The designated subtype is within an inner scope, and needs no
10272 -- elaboration, because only the access type is needed in the
10273 -- initialization procedure.
10274
10275 if Ekind (T) = E_Incomplete_Type then
10276 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10277 else
10278 Set_Ekind (Def_Id, Ekind (T));
10279 end if;
10280
10281 if For_Access and then Within_Init_Proc then
10282 null;
10283 else
10284 Append_Elmt (Def_Id, Private_Dependents (T));
10285 end if;
10286 end if;
10287
10288 Set_Etype (Def_Id, T);
10289 Init_Size_Align (Def_Id);
10290 Set_Has_Discriminants (Def_Id, Has_Discrs);
10291 Set_Is_Constrained (Def_Id, Constrained);
10292
10293 Set_First_Entity (Def_Id, First_Entity (T));
10294 Set_Last_Entity (Def_Id, Last_Entity (T));
10295 Set_Has_Implicit_Dereference
10296 (Def_Id, Has_Implicit_Dereference (T));
10297 Set_Has_Pragma_Unreferenced_Objects
10298 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10299
10300 -- If the subtype is the completion of a private declaration, there may
10301 -- have been representation clauses for the partial view, and they must
10302 -- be preserved. Build_Derived_Type chains the inherited clauses with
10303 -- the ones appearing on the extension. If this comes from a subtype
10304 -- declaration, all clauses are inherited.
10305
10306 if No (First_Rep_Item (Def_Id)) then
10307 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10308 end if;
10309
10310 if Is_Tagged_Type (T) then
10311 Set_Is_Tagged_Type (Def_Id);
10312 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10313 Make_Class_Wide_Type (Def_Id);
10314 end if;
10315
10316 Set_Stored_Constraint (Def_Id, No_Elist);
10317
10318 if Has_Discrs then
10319 Set_Discriminant_Constraint (Def_Id, Elist);
10320 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10321 end if;
10322
10323 if Is_Tagged_Type (T) then
10324
10325 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10326 -- concurrent record type (which has the list of primitive
10327 -- operations).
10328
10329 if Ada_Version >= Ada_2005
10330 and then Is_Concurrent_Type (T)
10331 then
10332 Set_Corresponding_Record_Type (Def_Id,
10333 Corresponding_Record_Type (T));
10334 else
10335 Set_Direct_Primitive_Operations (Def_Id,
10336 Direct_Primitive_Operations (T));
10337 end if;
10338
10339 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10340 end if;
10341
10342 -- Subtypes introduced by component declarations do not need to be
10343 -- marked as delayed, and do not get freeze nodes, because the semantics
10344 -- verifies that the parents of the subtypes are frozen before the
10345 -- enclosing record is frozen.
10346
10347 if not Is_Type (Scope (Def_Id)) then
10348 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10349
10350 if Is_Private_Type (T)
10351 and then Present (Full_View (T))
10352 then
10353 Conditional_Delay (Def_Id, Full_View (T));
10354 else
10355 Conditional_Delay (Def_Id, T);
10356 end if;
10357 end if;
10358
10359 if Is_Record_Type (T) then
10360 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10361
10362 if Has_Discrs
10363 and then not Is_Empty_Elmt_List (Elist)
10364 and then not For_Access
10365 then
10366 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10367
10368 else
10369 Set_Cloned_Subtype (Def_Id, T);
10370 end if;
10371 end if;
10372 end Build_Discriminated_Subtype;
10373
10374 ---------------------------
10375 -- Build_Itype_Reference --
10376 ---------------------------
10377
10378 procedure Build_Itype_Reference
10379 (Ityp : Entity_Id;
10380 Nod : Node_Id)
10381 is
10382 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10383 begin
10384
10385 -- Itype references are only created for use by the back-end
10386
10387 if Inside_A_Generic then
10388 return;
10389 else
10390 Set_Itype (IR, Ityp);
10391
10392 -- If Nod is a library unit entity, then Insert_After won't work,
10393 -- because Nod is not a member of any list. Therefore, we use
10394 -- Add_Global_Declaration in this case. This can happen if we have a
10395 -- build-in-place library function, child unit or not.
10396
10397 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10398 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10399 N_Subprogram_Declaration)
10400 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10401 then
10402 Add_Global_Declaration (IR);
10403 else
10404 Insert_After (Nod, IR);
10405 end if;
10406 end if;
10407 end Build_Itype_Reference;
10408
10409 ------------------------
10410 -- Build_Scalar_Bound --
10411 ------------------------
10412
10413 function Build_Scalar_Bound
10414 (Bound : Node_Id;
10415 Par_T : Entity_Id;
10416 Der_T : Entity_Id) return Node_Id
10417 is
10418 New_Bound : Entity_Id;
10419
10420 begin
10421 -- Note: not clear why this is needed, how can the original bound
10422 -- be unanalyzed at this point? and if it is, what business do we
10423 -- have messing around with it? and why is the base type of the
10424 -- parent type the right type for the resolution. It probably is
10425 -- not. It is OK for the new bound we are creating, but not for
10426 -- the old one??? Still if it never happens, no problem.
10427
10428 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10429
10430 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10431 New_Bound := New_Copy (Bound);
10432 Set_Etype (New_Bound, Der_T);
10433 Set_Analyzed (New_Bound);
10434
10435 elsif Is_Entity_Name (Bound) then
10436 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10437
10438 -- The following is almost certainly wrong. What business do we have
10439 -- relocating a node (Bound) that is presumably still attached to
10440 -- the tree elsewhere???
10441
10442 else
10443 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10444 end if;
10445
10446 Set_Etype (New_Bound, Der_T);
10447 return New_Bound;
10448 end Build_Scalar_Bound;
10449
10450 --------------------------------
10451 -- Build_Underlying_Full_View --
10452 --------------------------------
10453
10454 procedure Build_Underlying_Full_View
10455 (N : Node_Id;
10456 Typ : Entity_Id;
10457 Par : Entity_Id)
10458 is
10459 Loc : constant Source_Ptr := Sloc (N);
10460 Subt : constant Entity_Id :=
10461 Make_Defining_Identifier
10462 (Loc, New_External_Name (Chars (Typ), 'S'));
10463
10464 Constr : Node_Id;
10465 Indic : Node_Id;
10466 C : Node_Id;
10467 Id : Node_Id;
10468
10469 procedure Set_Discriminant_Name (Id : Node_Id);
10470 -- If the derived type has discriminants, they may rename discriminants
10471 -- of the parent. When building the full view of the parent, we need to
10472 -- recover the names of the original discriminants if the constraint is
10473 -- given by named associations.
10474
10475 ---------------------------
10476 -- Set_Discriminant_Name --
10477 ---------------------------
10478
10479 procedure Set_Discriminant_Name (Id : Node_Id) is
10480 Disc : Entity_Id;
10481
10482 begin
10483 Set_Original_Discriminant (Id, Empty);
10484
10485 if Has_Discriminants (Typ) then
10486 Disc := First_Discriminant (Typ);
10487 while Present (Disc) loop
10488 if Chars (Disc) = Chars (Id)
10489 and then Present (Corresponding_Discriminant (Disc))
10490 then
10491 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10492 end if;
10493 Next_Discriminant (Disc);
10494 end loop;
10495 end if;
10496 end Set_Discriminant_Name;
10497
10498 -- Start of processing for Build_Underlying_Full_View
10499
10500 begin
10501 if Nkind (N) = N_Full_Type_Declaration then
10502 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10503
10504 elsif Nkind (N) = N_Subtype_Declaration then
10505 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10506
10507 elsif Nkind (N) = N_Component_Declaration then
10508 Constr :=
10509 New_Copy_Tree
10510 (Constraint (Subtype_Indication (Component_Definition (N))));
10511
10512 else
10513 raise Program_Error;
10514 end if;
10515
10516 C := First (Constraints (Constr));
10517 while Present (C) loop
10518 if Nkind (C) = N_Discriminant_Association then
10519 Id := First (Selector_Names (C));
10520 while Present (Id) loop
10521 Set_Discriminant_Name (Id);
10522 Next (Id);
10523 end loop;
10524 end if;
10525
10526 Next (C);
10527 end loop;
10528
10529 Indic :=
10530 Make_Subtype_Declaration (Loc,
10531 Defining_Identifier => Subt,
10532 Subtype_Indication =>
10533 Make_Subtype_Indication (Loc,
10534 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10535 Constraint => New_Copy_Tree (Constr)));
10536
10537 -- If this is a component subtype for an outer itype, it is not
10538 -- a list member, so simply set the parent link for analysis: if
10539 -- the enclosing type does not need to be in a declarative list,
10540 -- neither do the components.
10541
10542 if Is_List_Member (N)
10543 and then Nkind (N) /= N_Component_Declaration
10544 then
10545 Insert_Before (N, Indic);
10546 else
10547 Set_Parent (Indic, Parent (N));
10548 end if;
10549
10550 Analyze (Indic);
10551 Set_Underlying_Full_View (Typ, Full_View (Subt));
10552 Set_Is_Underlying_Full_View (Full_View (Subt));
10553 end Build_Underlying_Full_View;
10554
10555 -------------------------------
10556 -- Check_Abstract_Overriding --
10557 -------------------------------
10558
10559 procedure Check_Abstract_Overriding (T : Entity_Id) is
10560 Alias_Subp : Entity_Id;
10561 Elmt : Elmt_Id;
10562 Op_List : Elist_Id;
10563 Subp : Entity_Id;
10564 Type_Def : Node_Id;
10565
10566 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10567 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10568 -- which has pragma Implemented already set. Check whether Subp's entity
10569 -- kind conforms to the implementation kind of the overridden routine.
10570
10571 procedure Check_Pragma_Implemented
10572 (Subp : Entity_Id;
10573 Iface_Subp : Entity_Id);
10574 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10575 -- Iface_Subp and both entities have pragma Implemented already set on
10576 -- them. Check whether the two implementation kinds are conforming.
10577
10578 procedure Inherit_Pragma_Implemented
10579 (Subp : Entity_Id;
10580 Iface_Subp : Entity_Id);
10581 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10582 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10583 -- Propagate the implementation kind of Iface_Subp to Subp.
10584
10585 ------------------------------
10586 -- Check_Pragma_Implemented --
10587 ------------------------------
10588
10589 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10590 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10591 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10592 Subp_Alias : constant Entity_Id := Alias (Subp);
10593 Contr_Typ : Entity_Id;
10594 Impl_Subp : Entity_Id;
10595
10596 begin
10597 -- Subp must have an alias since it is a hidden entity used to link
10598 -- an interface subprogram to its overriding counterpart.
10599
10600 pragma Assert (Present (Subp_Alias));
10601
10602 -- Handle aliases to synchronized wrappers
10603
10604 Impl_Subp := Subp_Alias;
10605
10606 if Is_Primitive_Wrapper (Impl_Subp) then
10607 Impl_Subp := Wrapped_Entity (Impl_Subp);
10608 end if;
10609
10610 -- Extract the type of the controlling formal
10611
10612 Contr_Typ := Etype (First_Formal (Subp_Alias));
10613
10614 if Is_Concurrent_Record_Type (Contr_Typ) then
10615 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10616 end if;
10617
10618 -- An interface subprogram whose implementation kind is By_Entry must
10619 -- be implemented by an entry.
10620
10621 if Impl_Kind = Name_By_Entry
10622 and then Ekind (Impl_Subp) /= E_Entry
10623 then
10624 Error_Msg_Node_2 := Iface_Alias;
10625 Error_Msg_NE
10626 ("type & must implement abstract subprogram & with an entry",
10627 Subp_Alias, Contr_Typ);
10628
10629 elsif Impl_Kind = Name_By_Protected_Procedure then
10630
10631 -- An interface subprogram whose implementation kind is By_
10632 -- Protected_Procedure cannot be implemented by a primitive
10633 -- procedure of a task type.
10634
10635 if Ekind (Contr_Typ) /= E_Protected_Type then
10636 Error_Msg_Node_2 := Contr_Typ;
10637 Error_Msg_NE
10638 ("interface subprogram & cannot be implemented by a "
10639 & "primitive procedure of task type &",
10640 Subp_Alias, Iface_Alias);
10641
10642 -- An interface subprogram whose implementation kind is By_
10643 -- Protected_Procedure must be implemented by a procedure.
10644
10645 elsif Ekind (Impl_Subp) /= E_Procedure then
10646 Error_Msg_Node_2 := Iface_Alias;
10647 Error_Msg_NE
10648 ("type & must implement abstract subprogram & with a "
10649 & "procedure", Subp_Alias, Contr_Typ);
10650
10651 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10652 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10653 then
10654 Error_Msg_Name_1 := Impl_Kind;
10655 Error_Msg_N
10656 ("overriding operation& must have synchronization%",
10657 Subp_Alias);
10658 end if;
10659
10660 -- If primitive has Optional synchronization, overriding operation
10661 -- must match if it has an explicit synchronization.
10662
10663 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10664 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10665 then
10666 Error_Msg_Name_1 := Impl_Kind;
10667 Error_Msg_N
10668 ("overriding operation& must have synchronization%", Subp_Alias);
10669 end if;
10670 end Check_Pragma_Implemented;
10671
10672 ------------------------------
10673 -- Check_Pragma_Implemented --
10674 ------------------------------
10675
10676 procedure Check_Pragma_Implemented
10677 (Subp : Entity_Id;
10678 Iface_Subp : Entity_Id)
10679 is
10680 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10681 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10682
10683 begin
10684 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10685 -- and overriding subprogram are different. In general this is an
10686 -- error except when the implementation kind of the overridden
10687 -- subprograms is By_Any or Optional.
10688
10689 if Iface_Kind /= Subp_Kind
10690 and then Iface_Kind /= Name_By_Any
10691 and then Iface_Kind /= Name_Optional
10692 then
10693 if Iface_Kind = Name_By_Entry then
10694 Error_Msg_N
10695 ("incompatible implementation kind, overridden subprogram " &
10696 "is marked By_Entry", Subp);
10697 else
10698 Error_Msg_N
10699 ("incompatible implementation kind, overridden subprogram " &
10700 "is marked By_Protected_Procedure", Subp);
10701 end if;
10702 end if;
10703 end Check_Pragma_Implemented;
10704
10705 --------------------------------
10706 -- Inherit_Pragma_Implemented --
10707 --------------------------------
10708
10709 procedure Inherit_Pragma_Implemented
10710 (Subp : Entity_Id;
10711 Iface_Subp : Entity_Id)
10712 is
10713 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10714 Loc : constant Source_Ptr := Sloc (Subp);
10715 Impl_Prag : Node_Id;
10716
10717 begin
10718 -- Since the implementation kind is stored as a representation item
10719 -- rather than a flag, create a pragma node.
10720
10721 Impl_Prag :=
10722 Make_Pragma (Loc,
10723 Chars => Name_Implemented,
10724 Pragma_Argument_Associations => New_List (
10725 Make_Pragma_Argument_Association (Loc,
10726 Expression => New_Occurrence_Of (Subp, Loc)),
10727
10728 Make_Pragma_Argument_Association (Loc,
10729 Expression => Make_Identifier (Loc, Iface_Kind))));
10730
10731 -- The pragma doesn't need to be analyzed because it is internally
10732 -- built. It is safe to directly register it as a rep item since we
10733 -- are only interested in the characters of the implementation kind.
10734
10735 Record_Rep_Item (Subp, Impl_Prag);
10736 end Inherit_Pragma_Implemented;
10737
10738 -- Start of processing for Check_Abstract_Overriding
10739
10740 begin
10741 Op_List := Primitive_Operations (T);
10742
10743 -- Loop to check primitive operations
10744
10745 Elmt := First_Elmt (Op_List);
10746 while Present (Elmt) loop
10747 Subp := Node (Elmt);
10748 Alias_Subp := Alias (Subp);
10749
10750 -- Inherited subprograms are identified by the fact that they do not
10751 -- come from source, and the associated source location is the
10752 -- location of the first subtype of the derived type.
10753
10754 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10755 -- subprograms that "require overriding".
10756
10757 -- Special exception, do not complain about failure to override the
10758 -- stream routines _Input and _Output, as well as the primitive
10759 -- operations used in dispatching selects since we always provide
10760 -- automatic overridings for these subprograms.
10761
10762 -- The partial view of T may have been a private extension, for
10763 -- which inherited functions dispatching on result are abstract.
10764 -- If the full view is a null extension, there is no need for
10765 -- overriding in Ada 2005, but wrappers need to be built for them
10766 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10767
10768 if Is_Null_Extension (T)
10769 and then Has_Controlling_Result (Subp)
10770 and then Ada_Version >= Ada_2005
10771 and then Present (Alias_Subp)
10772 and then not Comes_From_Source (Subp)
10773 and then not Is_Abstract_Subprogram (Alias_Subp)
10774 and then not Is_Access_Type (Etype (Subp))
10775 then
10776 null;
10777
10778 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10779 -- processing because this check is done with the aliased
10780 -- entity
10781
10782 elsif Present (Interface_Alias (Subp)) then
10783 null;
10784
10785 elsif (Is_Abstract_Subprogram (Subp)
10786 or else Requires_Overriding (Subp)
10787 or else
10788 (Has_Controlling_Result (Subp)
10789 and then Present (Alias_Subp)
10790 and then not Comes_From_Source (Subp)
10791 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10792 and then not Is_TSS (Subp, TSS_Stream_Input)
10793 and then not Is_TSS (Subp, TSS_Stream_Output)
10794 and then not Is_Abstract_Type (T)
10795 and then not Is_Predefined_Interface_Primitive (Subp)
10796
10797 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10798 -- with abstract interface types because the check will be done
10799 -- with the aliased entity (otherwise we generate a duplicated
10800 -- error message).
10801
10802 and then not Present (Interface_Alias (Subp))
10803 then
10804 if Present (Alias_Subp) then
10805
10806 -- Only perform the check for a derived subprogram when the
10807 -- type has an explicit record extension. This avoids incorrect
10808 -- flagging of abstract subprograms for the case of a type
10809 -- without an extension that is derived from a formal type
10810 -- with a tagged actual (can occur within a private part).
10811
10812 -- Ada 2005 (AI-391): In the case of an inherited function with
10813 -- a controlling result of the type, the rule does not apply if
10814 -- the type is a null extension (unless the parent function
10815 -- itself is abstract, in which case the function must still be
10816 -- be overridden). The expander will generate an overriding
10817 -- wrapper function calling the parent subprogram (see
10818 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10819
10820 Type_Def := Type_Definition (Parent (T));
10821
10822 if Nkind (Type_Def) = N_Derived_Type_Definition
10823 and then Present (Record_Extension_Part (Type_Def))
10824 and then
10825 (Ada_Version < Ada_2005
10826 or else not Is_Null_Extension (T)
10827 or else Ekind (Subp) = E_Procedure
10828 or else not Has_Controlling_Result (Subp)
10829 or else Is_Abstract_Subprogram (Alias_Subp)
10830 or else Requires_Overriding (Subp)
10831 or else Is_Access_Type (Etype (Subp)))
10832 then
10833 -- Avoid reporting error in case of abstract predefined
10834 -- primitive inherited from interface type because the
10835 -- body of internally generated predefined primitives
10836 -- of tagged types are generated later by Freeze_Type
10837
10838 if Is_Interface (Root_Type (T))
10839 and then Is_Abstract_Subprogram (Subp)
10840 and then Is_Predefined_Dispatching_Operation (Subp)
10841 and then not Comes_From_Source (Ultimate_Alias (Subp))
10842 then
10843 null;
10844
10845 -- A null extension is not obliged to override an inherited
10846 -- procedure subject to pragma Extensions_Visible with value
10847 -- False and at least one controlling OUT parameter
10848 -- (SPARK RM 6.1.7(6)).
10849
10850 elsif Is_Null_Extension (T)
10851 and then Is_EVF_Procedure (Subp)
10852 then
10853 null;
10854
10855 else
10856 Error_Msg_NE
10857 ("type must be declared abstract or & overridden",
10858 T, Subp);
10859
10860 -- Traverse the whole chain of aliased subprograms to
10861 -- complete the error notification. This is especially
10862 -- useful for traceability of the chain of entities when
10863 -- the subprogram corresponds with an interface
10864 -- subprogram (which may be defined in another package).
10865
10866 if Present (Alias_Subp) then
10867 declare
10868 E : Entity_Id;
10869
10870 begin
10871 E := Subp;
10872 while Present (Alias (E)) loop
10873
10874 -- Avoid reporting redundant errors on entities
10875 -- inherited from interfaces
10876
10877 if Sloc (E) /= Sloc (T) then
10878 Error_Msg_Sloc := Sloc (E);
10879 Error_Msg_NE
10880 ("\& has been inherited #", T, Subp);
10881 end if;
10882
10883 E := Alias (E);
10884 end loop;
10885
10886 Error_Msg_Sloc := Sloc (E);
10887
10888 -- AI05-0068: report if there is an overriding
10889 -- non-abstract subprogram that is invisible.
10890
10891 if Is_Hidden (E)
10892 and then not Is_Abstract_Subprogram (E)
10893 then
10894 Error_Msg_NE
10895 ("\& subprogram# is not visible",
10896 T, Subp);
10897
10898 -- Clarify the case where a non-null extension must
10899 -- override inherited procedure subject to pragma
10900 -- Extensions_Visible with value False and at least
10901 -- one controlling OUT param.
10902
10903 elsif Is_EVF_Procedure (E) then
10904 Error_Msg_NE
10905 ("\& # is subject to Extensions_Visible False",
10906 T, Subp);
10907
10908 else
10909 Error_Msg_NE
10910 ("\& has been inherited from subprogram #",
10911 T, Subp);
10912 end if;
10913 end;
10914 end if;
10915 end if;
10916
10917 -- Ada 2005 (AI-345): Protected or task type implementing
10918 -- abstract interfaces.
10919
10920 elsif Is_Concurrent_Record_Type (T)
10921 and then Present (Interfaces (T))
10922 then
10923 -- There is no need to check here RM 9.4(11.9/3) since we
10924 -- are processing the corresponding record type and the
10925 -- mode of the overriding subprograms was verified by
10926 -- Check_Conformance when the corresponding concurrent
10927 -- type declaration was analyzed.
10928
10929 Error_Msg_NE
10930 ("interface subprogram & must be overridden", T, Subp);
10931
10932 -- Examine primitive operations of synchronized type to find
10933 -- homonyms that have the wrong profile.
10934
10935 declare
10936 Prim : Entity_Id;
10937
10938 begin
10939 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10940 while Present (Prim) loop
10941 if Chars (Prim) = Chars (Subp) then
10942 Error_Msg_NE
10943 ("profile is not type conformant with prefixed "
10944 & "view profile of inherited operation&",
10945 Prim, Subp);
10946 end if;
10947
10948 Next_Entity (Prim);
10949 end loop;
10950 end;
10951 end if;
10952
10953 else
10954 Error_Msg_Node_2 := T;
10955 Error_Msg_N
10956 ("abstract subprogram& not allowed for type&", Subp);
10957
10958 -- Also post unconditional warning on the type (unconditional
10959 -- so that if there are more than one of these cases, we get
10960 -- them all, and not just the first one).
10961
10962 Error_Msg_Node_2 := Subp;
10963 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10964 end if;
10965
10966 -- A subprogram subject to pragma Extensions_Visible with value
10967 -- "True" cannot override a subprogram subject to the same pragma
10968 -- with value "False" (SPARK RM 6.1.7(5)).
10969
10970 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10971 and then Present (Overridden_Operation (Subp))
10972 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10973 Extensions_Visible_False
10974 then
10975 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10976 Error_Msg_N
10977 ("subprogram & with Extensions_Visible True cannot override "
10978 & "subprogram # with Extensions_Visible False", Subp);
10979 end if;
10980
10981 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10982
10983 -- Subp is an expander-generated procedure which maps an interface
10984 -- alias to a protected wrapper. The interface alias is flagged by
10985 -- pragma Implemented. Ensure that Subp is a procedure when the
10986 -- implementation kind is By_Protected_Procedure or an entry when
10987 -- By_Entry.
10988
10989 if Ada_Version >= Ada_2012
10990 and then Is_Hidden (Subp)
10991 and then Present (Interface_Alias (Subp))
10992 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10993 then
10994 Check_Pragma_Implemented (Subp);
10995 end if;
10996
10997 -- Subp is an interface primitive which overrides another interface
10998 -- primitive marked with pragma Implemented.
10999
11000 if Ada_Version >= Ada_2012
11001 and then Present (Overridden_Operation (Subp))
11002 and then Has_Rep_Pragma
11003 (Overridden_Operation (Subp), Name_Implemented)
11004 then
11005 -- If the overriding routine is also marked by Implemented, check
11006 -- that the two implementation kinds are conforming.
11007
11008 if Has_Rep_Pragma (Subp, Name_Implemented) then
11009 Check_Pragma_Implemented
11010 (Subp => Subp,
11011 Iface_Subp => Overridden_Operation (Subp));
11012
11013 -- Otherwise the overriding routine inherits the implementation
11014 -- kind from the overridden subprogram.
11015
11016 else
11017 Inherit_Pragma_Implemented
11018 (Subp => Subp,
11019 Iface_Subp => Overridden_Operation (Subp));
11020 end if;
11021 end if;
11022
11023 -- If the operation is a wrapper for a synchronized primitive, it
11024 -- may be called indirectly through a dispatching select. We assume
11025 -- that it will be referenced elsewhere indirectly, and suppress
11026 -- warnings about an unused entity.
11027
11028 if Is_Primitive_Wrapper (Subp)
11029 and then Present (Wrapped_Entity (Subp))
11030 then
11031 Set_Referenced (Wrapped_Entity (Subp));
11032 end if;
11033
11034 Next_Elmt (Elmt);
11035 end loop;
11036 end Check_Abstract_Overriding;
11037
11038 ------------------------------------------------
11039 -- Check_Access_Discriminant_Requires_Limited --
11040 ------------------------------------------------
11041
11042 procedure Check_Access_Discriminant_Requires_Limited
11043 (D : Node_Id;
11044 Loc : Node_Id)
11045 is
11046 begin
11047 -- A discriminant_specification for an access discriminant shall appear
11048 -- only in the declaration for a task or protected type, or for a type
11049 -- with the reserved word 'limited' in its definition or in one of its
11050 -- ancestors (RM 3.7(10)).
11051
11052 -- AI-0063: The proper condition is that type must be immutably limited,
11053 -- or else be a partial view.
11054
11055 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11056 if Is_Limited_View (Current_Scope)
11057 or else
11058 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11059 and then Limited_Present (Parent (Current_Scope)))
11060 then
11061 null;
11062
11063 else
11064 Error_Msg_N
11065 ("access discriminants allowed only for limited types", Loc);
11066 end if;
11067 end if;
11068 end Check_Access_Discriminant_Requires_Limited;
11069
11070 -----------------------------------
11071 -- Check_Aliased_Component_Types --
11072 -----------------------------------
11073
11074 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11075 C : Entity_Id;
11076
11077 begin
11078 -- ??? Also need to check components of record extensions, but not
11079 -- components of protected types (which are always limited).
11080
11081 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11082 -- types to be unconstrained. This is safe because it is illegal to
11083 -- create access subtypes to such types with explicit discriminant
11084 -- constraints.
11085
11086 if not Is_Limited_Type (T) then
11087 if Ekind (T) = E_Record_Type then
11088 C := First_Component (T);
11089 while Present (C) loop
11090 if Is_Aliased (C)
11091 and then Has_Discriminants (Etype (C))
11092 and then not Is_Constrained (Etype (C))
11093 and then not In_Instance_Body
11094 and then Ada_Version < Ada_2005
11095 then
11096 Error_Msg_N
11097 ("aliased component must be constrained (RM 3.6(11))",
11098 C);
11099 end if;
11100
11101 Next_Component (C);
11102 end loop;
11103
11104 elsif Ekind (T) = E_Array_Type then
11105 if Has_Aliased_Components (T)
11106 and then Has_Discriminants (Component_Type (T))
11107 and then not Is_Constrained (Component_Type (T))
11108 and then not In_Instance_Body
11109 and then Ada_Version < Ada_2005
11110 then
11111 Error_Msg_N
11112 ("aliased component type must be constrained (RM 3.6(11))",
11113 T);
11114 end if;
11115 end if;
11116 end if;
11117 end Check_Aliased_Component_Types;
11118
11119 ---------------------------------------
11120 -- Check_Anonymous_Access_Components --
11121 ---------------------------------------
11122
11123 procedure Check_Anonymous_Access_Components
11124 (Typ_Decl : Node_Id;
11125 Typ : Entity_Id;
11126 Prev : Entity_Id;
11127 Comp_List : Node_Id)
11128 is
11129 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11130 Anon_Access : Entity_Id;
11131 Acc_Def : Node_Id;
11132 Comp : Node_Id;
11133 Comp_Def : Node_Id;
11134 Decl : Node_Id;
11135 Type_Def : Node_Id;
11136
11137 procedure Build_Incomplete_Type_Declaration;
11138 -- If the record type contains components that include an access to the
11139 -- current record, then create an incomplete type declaration for the
11140 -- record, to be used as the designated type of the anonymous access.
11141 -- This is done only once, and only if there is no previous partial
11142 -- view of the type.
11143
11144 function Designates_T (Subt : Node_Id) return Boolean;
11145 -- Check whether a node designates the enclosing record type, or 'Class
11146 -- of that type
11147
11148 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11149 -- Check whether an access definition includes a reference to
11150 -- the enclosing record type. The reference can be a subtype mark
11151 -- in the access definition itself, a 'Class attribute reference, or
11152 -- recursively a reference appearing in a parameter specification
11153 -- or result definition of an access_to_subprogram definition.
11154
11155 --------------------------------------
11156 -- Build_Incomplete_Type_Declaration --
11157 --------------------------------------
11158
11159 procedure Build_Incomplete_Type_Declaration is
11160 Decl : Node_Id;
11161 Inc_T : Entity_Id;
11162 H : Entity_Id;
11163
11164 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11165 -- it's "is new ... with record" or else "is tagged record ...".
11166
11167 Is_Tagged : constant Boolean :=
11168 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11169 and then
11170 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11171 or else
11172 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11173 and then Tagged_Present (Type_Definition (Typ_Decl)));
11174
11175 begin
11176 -- If there is a previous partial view, no need to create a new one
11177 -- If the partial view, given by Prev, is incomplete, If Prev is
11178 -- a private declaration, full declaration is flagged accordingly.
11179
11180 if Prev /= Typ then
11181 if Is_Tagged then
11182 Make_Class_Wide_Type (Prev);
11183 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11184 Set_Etype (Class_Wide_Type (Typ), Typ);
11185 end if;
11186
11187 return;
11188
11189 elsif Has_Private_Declaration (Typ) then
11190
11191 -- If we refer to T'Class inside T, and T is the completion of a
11192 -- private type, then make sure the class-wide type exists.
11193
11194 if Is_Tagged then
11195 Make_Class_Wide_Type (Typ);
11196 end if;
11197
11198 return;
11199
11200 -- If there was a previous anonymous access type, the incomplete
11201 -- type declaration will have been created already.
11202
11203 elsif Present (Current_Entity (Typ))
11204 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11205 and then Full_View (Current_Entity (Typ)) = Typ
11206 then
11207 if Is_Tagged
11208 and then Comes_From_Source (Current_Entity (Typ))
11209 and then not Is_Tagged_Type (Current_Entity (Typ))
11210 then
11211 Make_Class_Wide_Type (Typ);
11212 Error_Msg_N
11213 ("incomplete view of tagged type should be declared tagged??",
11214 Parent (Current_Entity (Typ)));
11215 end if;
11216 return;
11217
11218 else
11219 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11220 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11221
11222 -- Type has already been inserted into the current scope. Remove
11223 -- it, and add incomplete declaration for type, so that subsequent
11224 -- anonymous access types can use it. The entity is unchained from
11225 -- the homonym list and from immediate visibility. After analysis,
11226 -- the entity in the incomplete declaration becomes immediately
11227 -- visible in the record declaration that follows.
11228
11229 H := Current_Entity (Typ);
11230
11231 if H = Typ then
11232 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11233 else
11234 while Present (H)
11235 and then Homonym (H) /= Typ
11236 loop
11237 H := Homonym (Typ);
11238 end loop;
11239
11240 Set_Homonym (H, Homonym (Typ));
11241 end if;
11242
11243 Insert_Before (Typ_Decl, Decl);
11244 Analyze (Decl);
11245 Set_Full_View (Inc_T, Typ);
11246
11247 if Is_Tagged then
11248
11249 -- Create a common class-wide type for both views, and set the
11250 -- Etype of the class-wide type to the full view.
11251
11252 Make_Class_Wide_Type (Inc_T);
11253 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11254 Set_Etype (Class_Wide_Type (Typ), Typ);
11255 end if;
11256 end if;
11257 end Build_Incomplete_Type_Declaration;
11258
11259 ------------------
11260 -- Designates_T --
11261 ------------------
11262
11263 function Designates_T (Subt : Node_Id) return Boolean is
11264 Type_Id : constant Name_Id := Chars (Typ);
11265
11266 function Names_T (Nam : Node_Id) return Boolean;
11267 -- The record type has not been introduced in the current scope
11268 -- yet, so we must examine the name of the type itself, either
11269 -- an identifier T, or an expanded name of the form P.T, where
11270 -- P denotes the current scope.
11271
11272 -------------
11273 -- Names_T --
11274 -------------
11275
11276 function Names_T (Nam : Node_Id) return Boolean is
11277 begin
11278 if Nkind (Nam) = N_Identifier then
11279 return Chars (Nam) = Type_Id;
11280
11281 elsif Nkind (Nam) = N_Selected_Component then
11282 if Chars (Selector_Name (Nam)) = Type_Id then
11283 if Nkind (Prefix (Nam)) = N_Identifier then
11284 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11285
11286 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11287 return Chars (Selector_Name (Prefix (Nam))) =
11288 Chars (Current_Scope);
11289 else
11290 return False;
11291 end if;
11292
11293 else
11294 return False;
11295 end if;
11296
11297 else
11298 return False;
11299 end if;
11300 end Names_T;
11301
11302 -- Start of processing for Designates_T
11303
11304 begin
11305 if Nkind (Subt) = N_Identifier then
11306 return Chars (Subt) = Type_Id;
11307
11308 -- Reference can be through an expanded name which has not been
11309 -- analyzed yet, and which designates enclosing scopes.
11310
11311 elsif Nkind (Subt) = N_Selected_Component then
11312 if Names_T (Subt) then
11313 return True;
11314
11315 -- Otherwise it must denote an entity that is already visible.
11316 -- The access definition may name a subtype of the enclosing
11317 -- type, if there is a previous incomplete declaration for it.
11318
11319 else
11320 Find_Selected_Component (Subt);
11321 return
11322 Is_Entity_Name (Subt)
11323 and then Scope (Entity (Subt)) = Current_Scope
11324 and then
11325 (Chars (Base_Type (Entity (Subt))) = Type_Id
11326 or else
11327 (Is_Class_Wide_Type (Entity (Subt))
11328 and then
11329 Chars (Etype (Base_Type (Entity (Subt)))) =
11330 Type_Id));
11331 end if;
11332
11333 -- A reference to the current type may appear as the prefix of
11334 -- a 'Class attribute.
11335
11336 elsif Nkind (Subt) = N_Attribute_Reference
11337 and then Attribute_Name (Subt) = Name_Class
11338 then
11339 return Names_T (Prefix (Subt));
11340
11341 else
11342 return False;
11343 end if;
11344 end Designates_T;
11345
11346 ----------------
11347 -- Mentions_T --
11348 ----------------
11349
11350 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11351 Param_Spec : Node_Id;
11352
11353 Acc_Subprg : constant Node_Id :=
11354 Access_To_Subprogram_Definition (Acc_Def);
11355
11356 begin
11357 if No (Acc_Subprg) then
11358 return Designates_T (Subtype_Mark (Acc_Def));
11359 end if;
11360
11361 -- Component is an access_to_subprogram: examine its formals,
11362 -- and result definition in the case of an access_to_function.
11363
11364 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11365 while Present (Param_Spec) loop
11366 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11367 and then Mentions_T (Parameter_Type (Param_Spec))
11368 then
11369 return True;
11370
11371 elsif Designates_T (Parameter_Type (Param_Spec)) then
11372 return True;
11373 end if;
11374
11375 Next (Param_Spec);
11376 end loop;
11377
11378 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11379 if Nkind (Result_Definition (Acc_Subprg)) =
11380 N_Access_Definition
11381 then
11382 return Mentions_T (Result_Definition (Acc_Subprg));
11383 else
11384 return Designates_T (Result_Definition (Acc_Subprg));
11385 end if;
11386 end if;
11387
11388 return False;
11389 end Mentions_T;
11390
11391 -- Start of processing for Check_Anonymous_Access_Components
11392
11393 begin
11394 if No (Comp_List) then
11395 return;
11396 end if;
11397
11398 Comp := First (Component_Items (Comp_List));
11399 while Present (Comp) loop
11400 if Nkind (Comp) = N_Component_Declaration
11401 and then Present
11402 (Access_Definition (Component_Definition (Comp)))
11403 and then
11404 Mentions_T (Access_Definition (Component_Definition (Comp)))
11405 then
11406 Comp_Def := Component_Definition (Comp);
11407 Acc_Def :=
11408 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11409
11410 Build_Incomplete_Type_Declaration;
11411 Anon_Access := Make_Temporary (Loc, 'S');
11412
11413 -- Create a declaration for the anonymous access type: either
11414 -- an access_to_object or an access_to_subprogram.
11415
11416 if Present (Acc_Def) then
11417 if Nkind (Acc_Def) = N_Access_Function_Definition then
11418 Type_Def :=
11419 Make_Access_Function_Definition (Loc,
11420 Parameter_Specifications =>
11421 Parameter_Specifications (Acc_Def),
11422 Result_Definition => Result_Definition (Acc_Def));
11423 else
11424 Type_Def :=
11425 Make_Access_Procedure_Definition (Loc,
11426 Parameter_Specifications =>
11427 Parameter_Specifications (Acc_Def));
11428 end if;
11429
11430 else
11431 Type_Def :=
11432 Make_Access_To_Object_Definition (Loc,
11433 Subtype_Indication =>
11434 Relocate_Node
11435 (Subtype_Mark (Access_Definition (Comp_Def))));
11436
11437 Set_Constant_Present
11438 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11439 Set_All_Present
11440 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11441 end if;
11442
11443 Set_Null_Exclusion_Present
11444 (Type_Def,
11445 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11446
11447 Decl :=
11448 Make_Full_Type_Declaration (Loc,
11449 Defining_Identifier => Anon_Access,
11450 Type_Definition => Type_Def);
11451
11452 Insert_Before (Typ_Decl, Decl);
11453 Analyze (Decl);
11454
11455 -- If an access to subprogram, create the extra formals
11456
11457 if Present (Acc_Def) then
11458 Create_Extra_Formals (Designated_Type (Anon_Access));
11459
11460 -- If an access to object, preserve entity of designated type,
11461 -- for ASIS use, before rewriting the component definition.
11462
11463 else
11464 declare
11465 Desig : Entity_Id;
11466
11467 begin
11468 Desig := Entity (Subtype_Indication (Type_Def));
11469
11470 -- If the access definition is to the current record,
11471 -- the visible entity at this point is an incomplete
11472 -- type. Retrieve the full view to simplify ASIS queries
11473
11474 if Ekind (Desig) = E_Incomplete_Type then
11475 Desig := Full_View (Desig);
11476 end if;
11477
11478 Set_Entity
11479 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11480 end;
11481 end if;
11482
11483 Rewrite (Comp_Def,
11484 Make_Component_Definition (Loc,
11485 Subtype_Indication =>
11486 New_Occurrence_Of (Anon_Access, Loc)));
11487
11488 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11489 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11490 else
11491 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11492 end if;
11493
11494 Set_Is_Local_Anonymous_Access (Anon_Access);
11495 end if;
11496
11497 Next (Comp);
11498 end loop;
11499
11500 if Present (Variant_Part (Comp_List)) then
11501 declare
11502 V : Node_Id;
11503 begin
11504 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11505 while Present (V) loop
11506 Check_Anonymous_Access_Components
11507 (Typ_Decl, Typ, Prev, Component_List (V));
11508 Next_Non_Pragma (V);
11509 end loop;
11510 end;
11511 end if;
11512 end Check_Anonymous_Access_Components;
11513
11514 ----------------------
11515 -- Check_Completion --
11516 ----------------------
11517
11518 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11519 E : Entity_Id;
11520
11521 procedure Post_Error;
11522 -- Post error message for lack of completion for entity E
11523
11524 ----------------
11525 -- Post_Error --
11526 ----------------
11527
11528 procedure Post_Error is
11529 procedure Missing_Body;
11530 -- Output missing body message
11531
11532 ------------------
11533 -- Missing_Body --
11534 ------------------
11535
11536 procedure Missing_Body is
11537 begin
11538 -- Spec is in same unit, so we can post on spec
11539
11540 if In_Same_Source_Unit (Body_Id, E) then
11541 Error_Msg_N ("missing body for &", E);
11542
11543 -- Spec is in a separate unit, so we have to post on the body
11544
11545 else
11546 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11547 end if;
11548 end Missing_Body;
11549
11550 -- Start of processing for Post_Error
11551
11552 begin
11553 if not Comes_From_Source (E) then
11554 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11555
11556 -- It may be an anonymous protected type created for a
11557 -- single variable. Post error on variable, if present.
11558
11559 declare
11560 Var : Entity_Id;
11561
11562 begin
11563 Var := First_Entity (Current_Scope);
11564 while Present (Var) loop
11565 exit when Etype (Var) = E
11566 and then Comes_From_Source (Var);
11567
11568 Next_Entity (Var);
11569 end loop;
11570
11571 if Present (Var) then
11572 E := Var;
11573 end if;
11574 end;
11575 end if;
11576 end if;
11577
11578 -- If a generated entity has no completion, then either previous
11579 -- semantic errors have disabled the expansion phase, or else we had
11580 -- missing subunits, or else we are compiling without expansion,
11581 -- or else something is very wrong.
11582
11583 if not Comes_From_Source (E) then
11584 pragma Assert
11585 (Serious_Errors_Detected > 0
11586 or else Configurable_Run_Time_Violations > 0
11587 or else Subunits_Missing
11588 or else not Expander_Active);
11589 return;
11590
11591 -- Here for source entity
11592
11593 else
11594 -- Here if no body to post the error message, so we post the error
11595 -- on the declaration that has no completion. This is not really
11596 -- the right place to post it, think about this later ???
11597
11598 if No (Body_Id) then
11599 if Is_Type (E) then
11600 Error_Msg_NE
11601 ("missing full declaration for }", Parent (E), E);
11602 else
11603 Error_Msg_NE ("missing body for &", Parent (E), E);
11604 end if;
11605
11606 -- Package body has no completion for a declaration that appears
11607 -- in the corresponding spec. Post error on the body, with a
11608 -- reference to the non-completed declaration.
11609
11610 else
11611 Error_Msg_Sloc := Sloc (E);
11612
11613 if Is_Type (E) then
11614 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11615
11616 elsif Is_Overloadable (E)
11617 and then Current_Entity_In_Scope (E) /= E
11618 then
11619 -- It may be that the completion is mistyped and appears as
11620 -- a distinct overloading of the entity.
11621
11622 declare
11623 Candidate : constant Entity_Id :=
11624 Current_Entity_In_Scope (E);
11625 Decl : constant Node_Id :=
11626 Unit_Declaration_Node (Candidate);
11627
11628 begin
11629 if Is_Overloadable (Candidate)
11630 and then Ekind (Candidate) = Ekind (E)
11631 and then Nkind (Decl) = N_Subprogram_Body
11632 and then Acts_As_Spec (Decl)
11633 then
11634 Check_Type_Conformant (Candidate, E);
11635
11636 else
11637 Missing_Body;
11638 end if;
11639 end;
11640
11641 else
11642 Missing_Body;
11643 end if;
11644 end if;
11645 end if;
11646 end Post_Error;
11647
11648 -- Local variables
11649
11650 Pack_Id : constant Entity_Id := Current_Scope;
11651
11652 -- Start of processing for Check_Completion
11653
11654 begin
11655 E := First_Entity (Pack_Id);
11656 while Present (E) loop
11657 if Is_Intrinsic_Subprogram (E) then
11658 null;
11659
11660 -- The following situation requires special handling: a child unit
11661 -- that appears in the context clause of the body of its parent:
11662
11663 -- procedure Parent.Child (...);
11664
11665 -- with Parent.Child;
11666 -- package body Parent is
11667
11668 -- Here Parent.Child appears as a local entity, but should not be
11669 -- flagged as requiring completion, because it is a compilation
11670 -- unit.
11671
11672 -- Ignore missing completion for a subprogram that does not come from
11673 -- source (including the _Call primitive operation of RAS types,
11674 -- which has to have the flag Comes_From_Source for other purposes):
11675 -- we assume that the expander will provide the missing completion.
11676 -- In case of previous errors, other expansion actions that provide
11677 -- bodies for null procedures with not be invoked, so inhibit message
11678 -- in those cases.
11679
11680 -- Note that E_Operator is not in the list that follows, because
11681 -- this kind is reserved for predefined operators, that are
11682 -- intrinsic and do not need completion.
11683
11684 elsif Ekind_In (E, E_Function,
11685 E_Procedure,
11686 E_Generic_Function,
11687 E_Generic_Procedure)
11688 then
11689 if Has_Completion (E) then
11690 null;
11691
11692 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11693 null;
11694
11695 elsif Is_Subprogram (E)
11696 and then (not Comes_From_Source (E)
11697 or else Chars (E) = Name_uCall)
11698 then
11699 null;
11700
11701 elsif
11702 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11703 then
11704 null;
11705
11706 elsif Nkind (Parent (E)) = N_Procedure_Specification
11707 and then Null_Present (Parent (E))
11708 and then Serious_Errors_Detected > 0
11709 then
11710 null;
11711
11712 else
11713 Post_Error;
11714 end if;
11715
11716 elsif Is_Entry (E) then
11717 if not Has_Completion (E) and then
11718 (Ekind (Scope (E)) = E_Protected_Object
11719 or else Ekind (Scope (E)) = E_Protected_Type)
11720 then
11721 Post_Error;
11722 end if;
11723
11724 elsif Is_Package_Or_Generic_Package (E) then
11725 if Unit_Requires_Body (E) then
11726 if not Has_Completion (E)
11727 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11728 N_Compilation_Unit
11729 then
11730 Post_Error;
11731 end if;
11732
11733 elsif not Is_Child_Unit (E) then
11734 May_Need_Implicit_Body (E);
11735 end if;
11736
11737 -- A formal incomplete type (Ada 2012) does not require a completion;
11738 -- other incomplete type declarations do.
11739
11740 elsif Ekind (E) = E_Incomplete_Type
11741 and then No (Underlying_Type (E))
11742 and then not Is_Generic_Type (E)
11743 then
11744 Post_Error;
11745
11746 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11747 and then not Has_Completion (E)
11748 then
11749 Post_Error;
11750
11751 -- A single task declared in the current scope is a constant, verify
11752 -- that the body of its anonymous type is in the same scope. If the
11753 -- task is defined elsewhere, this may be a renaming declaration for
11754 -- which no completion is needed.
11755
11756 elsif Ekind (E) = E_Constant
11757 and then Ekind (Etype (E)) = E_Task_Type
11758 and then not Has_Completion (Etype (E))
11759 and then Scope (Etype (E)) = Current_Scope
11760 then
11761 Post_Error;
11762
11763 elsif Ekind (E) = E_Protected_Object
11764 and then not Has_Completion (Etype (E))
11765 then
11766 Post_Error;
11767
11768 elsif Ekind (E) = E_Record_Type then
11769 if Is_Tagged_Type (E) then
11770 Check_Abstract_Overriding (E);
11771 Check_Conventions (E);
11772 end if;
11773
11774 Check_Aliased_Component_Types (E);
11775
11776 elsif Ekind (E) = E_Array_Type then
11777 Check_Aliased_Component_Types (E);
11778
11779 end if;
11780
11781 Next_Entity (E);
11782 end loop;
11783 end Check_Completion;
11784
11785 ------------------------------------
11786 -- Check_CPP_Type_Has_No_Defaults --
11787 ------------------------------------
11788
11789 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11790 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11791 Clist : Node_Id;
11792 Comp : Node_Id;
11793
11794 begin
11795 -- Obtain the component list
11796
11797 if Nkind (Tdef) = N_Record_Definition then
11798 Clist := Component_List (Tdef);
11799 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11800 Clist := Component_List (Record_Extension_Part (Tdef));
11801 end if;
11802
11803 -- Check all components to ensure no default expressions
11804
11805 if Present (Clist) then
11806 Comp := First (Component_Items (Clist));
11807 while Present (Comp) loop
11808 if Present (Expression (Comp)) then
11809 Error_Msg_N
11810 ("component of imported 'C'P'P type cannot have "
11811 & "default expression", Expression (Comp));
11812 end if;
11813
11814 Next (Comp);
11815 end loop;
11816 end if;
11817 end Check_CPP_Type_Has_No_Defaults;
11818
11819 ----------------------------
11820 -- Check_Delta_Expression --
11821 ----------------------------
11822
11823 procedure Check_Delta_Expression (E : Node_Id) is
11824 begin
11825 if not (Is_Real_Type (Etype (E))) then
11826 Wrong_Type (E, Any_Real);
11827
11828 elsif not Is_OK_Static_Expression (E) then
11829 Flag_Non_Static_Expr
11830 ("non-static expression used for delta value!", E);
11831
11832 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11833 Error_Msg_N ("delta expression must be positive", E);
11834
11835 else
11836 return;
11837 end if;
11838
11839 -- If any of above errors occurred, then replace the incorrect
11840 -- expression by the real 0.1, which should prevent further errors.
11841
11842 Rewrite (E,
11843 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11844 Analyze_And_Resolve (E, Standard_Float);
11845 end Check_Delta_Expression;
11846
11847 -----------------------------
11848 -- Check_Digits_Expression --
11849 -----------------------------
11850
11851 procedure Check_Digits_Expression (E : Node_Id) is
11852 begin
11853 if not (Is_Integer_Type (Etype (E))) then
11854 Wrong_Type (E, Any_Integer);
11855
11856 elsif not Is_OK_Static_Expression (E) then
11857 Flag_Non_Static_Expr
11858 ("non-static expression used for digits value!", E);
11859
11860 elsif Expr_Value (E) <= 0 then
11861 Error_Msg_N ("digits value must be greater than zero", E);
11862
11863 else
11864 return;
11865 end if;
11866
11867 -- If any of above errors occurred, then replace the incorrect
11868 -- expression by the integer 1, which should prevent further errors.
11869
11870 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11871 Analyze_And_Resolve (E, Standard_Integer);
11872
11873 end Check_Digits_Expression;
11874
11875 --------------------------
11876 -- Check_Initialization --
11877 --------------------------
11878
11879 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11880 begin
11881 -- Special processing for limited types
11882
11883 if Is_Limited_Type (T)
11884 and then not In_Instance
11885 and then not In_Inlined_Body
11886 then
11887 if not OK_For_Limited_Init (T, Exp) then
11888
11889 -- In GNAT mode, this is just a warning, to allow it to be evilly
11890 -- turned off. Otherwise it is a real error.
11891
11892 if GNAT_Mode then
11893 Error_Msg_N
11894 ("??cannot initialize entities of limited type!", Exp);
11895
11896 elsif Ada_Version < Ada_2005 then
11897
11898 -- The side effect removal machinery may generate illegal Ada
11899 -- code to avoid the usage of access types and 'reference in
11900 -- SPARK mode. Since this is legal code with respect to theorem
11901 -- proving, do not emit the error.
11902
11903 if GNATprove_Mode
11904 and then Nkind (Exp) = N_Function_Call
11905 and then Nkind (Parent (Exp)) = N_Object_Declaration
11906 and then not Comes_From_Source
11907 (Defining_Identifier (Parent (Exp)))
11908 then
11909 null;
11910
11911 else
11912 Error_Msg_N
11913 ("cannot initialize entities of limited type", Exp);
11914 Explain_Limited_Type (T, Exp);
11915 end if;
11916
11917 else
11918 -- Specialize error message according to kind of illegal
11919 -- initial expression.
11920
11921 if Nkind (Exp) = N_Type_Conversion
11922 and then Nkind (Expression (Exp)) = N_Function_Call
11923 then
11924 -- No error for internally-generated object declarations,
11925 -- which can come from build-in-place assignment statements.
11926
11927 if Nkind (Parent (Exp)) = N_Object_Declaration
11928 and then not Comes_From_Source
11929 (Defining_Identifier (Parent (Exp)))
11930 then
11931 null;
11932
11933 else
11934 Error_Msg_N
11935 ("illegal context for call to function with limited "
11936 & "result", Exp);
11937 end if;
11938
11939 else
11940 Error_Msg_N
11941 ("initialization of limited object requires aggregate or "
11942 & "function call", Exp);
11943 end if;
11944 end if;
11945 end if;
11946 end if;
11947
11948 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11949 -- set unless we can be sure that no range check is required.
11950
11951 if (GNATprove_Mode or not Expander_Active)
11952 and then Is_Scalar_Type (T)
11953 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11954 then
11955 Set_Do_Range_Check (Exp);
11956 end if;
11957 end Check_Initialization;
11958
11959 ----------------------
11960 -- Check_Interfaces --
11961 ----------------------
11962
11963 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11964 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11965
11966 Iface : Node_Id;
11967 Iface_Def : Node_Id;
11968 Iface_Typ : Entity_Id;
11969 Parent_Node : Node_Id;
11970
11971 Is_Task : Boolean := False;
11972 -- Set True if parent type or any progenitor is a task interface
11973
11974 Is_Protected : Boolean := False;
11975 -- Set True if parent type or any progenitor is a protected interface
11976
11977 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11978 -- Check that a progenitor is compatible with declaration. If an error
11979 -- message is output, it is posted on Error_Node.
11980
11981 ------------------
11982 -- Check_Ifaces --
11983 ------------------
11984
11985 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11986 Iface_Id : constant Entity_Id :=
11987 Defining_Identifier (Parent (Iface_Def));
11988 Type_Def : Node_Id;
11989
11990 begin
11991 if Nkind (N) = N_Private_Extension_Declaration then
11992 Type_Def := N;
11993 else
11994 Type_Def := Type_Definition (N);
11995 end if;
11996
11997 if Is_Task_Interface (Iface_Id) then
11998 Is_Task := True;
11999
12000 elsif Is_Protected_Interface (Iface_Id) then
12001 Is_Protected := True;
12002 end if;
12003
12004 if Is_Synchronized_Interface (Iface_Id) then
12005
12006 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12007 -- extension derived from a synchronized interface must explicitly
12008 -- be declared synchronized, because the full view will be a
12009 -- synchronized type.
12010
12011 if Nkind (N) = N_Private_Extension_Declaration then
12012 if not Synchronized_Present (N) then
12013 Error_Msg_NE
12014 ("private extension of& must be explicitly synchronized",
12015 N, Iface_Id);
12016 end if;
12017
12018 -- However, by 3.9.4(16/2), a full type that is a record extension
12019 -- is never allowed to derive from a synchronized interface (note
12020 -- that interfaces must be excluded from this check, because those
12021 -- are represented by derived type definitions in some cases).
12022
12023 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12024 and then not Interface_Present (Type_Definition (N))
12025 then
12026 Error_Msg_N ("record extension cannot derive from synchronized "
12027 & "interface", Error_Node);
12028 end if;
12029 end if;
12030
12031 -- Check that the characteristics of the progenitor are compatible
12032 -- with the explicit qualifier in the declaration.
12033 -- The check only applies to qualifiers that come from source.
12034 -- Limited_Present also appears in the declaration of corresponding
12035 -- records, and the check does not apply to them.
12036
12037 if Limited_Present (Type_Def)
12038 and then not
12039 Is_Concurrent_Record_Type (Defining_Identifier (N))
12040 then
12041 if Is_Limited_Interface (Parent_Type)
12042 and then not Is_Limited_Interface (Iface_Id)
12043 then
12044 Error_Msg_NE
12045 ("progenitor & must be limited interface",
12046 Error_Node, Iface_Id);
12047
12048 elsif
12049 (Task_Present (Iface_Def)
12050 or else Protected_Present (Iface_Def)
12051 or else Synchronized_Present (Iface_Def))
12052 and then Nkind (N) /= N_Private_Extension_Declaration
12053 and then not Error_Posted (N)
12054 then
12055 Error_Msg_NE
12056 ("progenitor & must be limited interface",
12057 Error_Node, Iface_Id);
12058 end if;
12059
12060 -- Protected interfaces can only inherit from limited, synchronized
12061 -- or protected interfaces.
12062
12063 elsif Nkind (N) = N_Full_Type_Declaration
12064 and then Protected_Present (Type_Def)
12065 then
12066 if Limited_Present (Iface_Def)
12067 or else Synchronized_Present (Iface_Def)
12068 or else Protected_Present (Iface_Def)
12069 then
12070 null;
12071
12072 elsif Task_Present (Iface_Def) then
12073 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12074 & "from task interface", Error_Node);
12075
12076 else
12077 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12078 & "from non-limited interface", Error_Node);
12079 end if;
12080
12081 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12082 -- limited and synchronized.
12083
12084 elsif Synchronized_Present (Type_Def) then
12085 if Limited_Present (Iface_Def)
12086 or else Synchronized_Present (Iface_Def)
12087 then
12088 null;
12089
12090 elsif Protected_Present (Iface_Def)
12091 and then Nkind (N) /= N_Private_Extension_Declaration
12092 then
12093 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12094 & "from protected interface", Error_Node);
12095
12096 elsif Task_Present (Iface_Def)
12097 and then Nkind (N) /= N_Private_Extension_Declaration
12098 then
12099 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12100 & "from task interface", Error_Node);
12101
12102 elsif not Is_Limited_Interface (Iface_Id) then
12103 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12104 & "from non-limited interface", Error_Node);
12105 end if;
12106
12107 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12108 -- synchronized or task interfaces.
12109
12110 elsif Nkind (N) = N_Full_Type_Declaration
12111 and then Task_Present (Type_Def)
12112 then
12113 if Limited_Present (Iface_Def)
12114 or else Synchronized_Present (Iface_Def)
12115 or else Task_Present (Iface_Def)
12116 then
12117 null;
12118
12119 elsif Protected_Present (Iface_Def) then
12120 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12121 & "protected interface", Error_Node);
12122
12123 else
12124 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12125 & "non-limited interface", Error_Node);
12126 end if;
12127 end if;
12128 end Check_Ifaces;
12129
12130 -- Start of processing for Check_Interfaces
12131
12132 begin
12133 if Is_Interface (Parent_Type) then
12134 if Is_Task_Interface (Parent_Type) then
12135 Is_Task := True;
12136
12137 elsif Is_Protected_Interface (Parent_Type) then
12138 Is_Protected := True;
12139 end if;
12140 end if;
12141
12142 if Nkind (N) = N_Private_Extension_Declaration then
12143
12144 -- Check that progenitors are compatible with declaration
12145
12146 Iface := First (Interface_List (Def));
12147 while Present (Iface) loop
12148 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12149
12150 Parent_Node := Parent (Base_Type (Iface_Typ));
12151 Iface_Def := Type_Definition (Parent_Node);
12152
12153 if not Is_Interface (Iface_Typ) then
12154 Diagnose_Interface (Iface, Iface_Typ);
12155 else
12156 Check_Ifaces (Iface_Def, Iface);
12157 end if;
12158
12159 Next (Iface);
12160 end loop;
12161
12162 if Is_Task and Is_Protected then
12163 Error_Msg_N
12164 ("type cannot derive from task and protected interface", N);
12165 end if;
12166
12167 return;
12168 end if;
12169
12170 -- Full type declaration of derived type.
12171 -- Check compatibility with parent if it is interface type
12172
12173 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12174 and then Is_Interface (Parent_Type)
12175 then
12176 Parent_Node := Parent (Parent_Type);
12177
12178 -- More detailed checks for interface varieties
12179
12180 Check_Ifaces
12181 (Iface_Def => Type_Definition (Parent_Node),
12182 Error_Node => Subtype_Indication (Type_Definition (N)));
12183 end if;
12184
12185 Iface := First (Interface_List (Def));
12186 while Present (Iface) loop
12187 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12188
12189 Parent_Node := Parent (Base_Type (Iface_Typ));
12190 Iface_Def := Type_Definition (Parent_Node);
12191
12192 if not Is_Interface (Iface_Typ) then
12193 Diagnose_Interface (Iface, Iface_Typ);
12194
12195 else
12196 -- "The declaration of a specific descendant of an interface
12197 -- type freezes the interface type" RM 13.14
12198
12199 Freeze_Before (N, Iface_Typ);
12200 Check_Ifaces (Iface_Def, Error_Node => Iface);
12201 end if;
12202
12203 Next (Iface);
12204 end loop;
12205
12206 if Is_Task and Is_Protected then
12207 Error_Msg_N
12208 ("type cannot derive from task and protected interface", N);
12209 end if;
12210 end Check_Interfaces;
12211
12212 ------------------------------------
12213 -- Check_Or_Process_Discriminants --
12214 ------------------------------------
12215
12216 -- If an incomplete or private type declaration was already given for the
12217 -- type, the discriminants may have already been processed if they were
12218 -- present on the incomplete declaration. In this case a full conformance
12219 -- check has been performed in Find_Type_Name, and we then recheck here
12220 -- some properties that can't be checked on the partial view alone.
12221 -- Otherwise we call Process_Discriminants.
12222
12223 procedure Check_Or_Process_Discriminants
12224 (N : Node_Id;
12225 T : Entity_Id;
12226 Prev : Entity_Id := Empty)
12227 is
12228 begin
12229 if Has_Discriminants (T) then
12230
12231 -- Discriminants are already set on T if they were already present
12232 -- on the partial view. Make them visible to component declarations.
12233
12234 declare
12235 D : Entity_Id;
12236 -- Discriminant on T (full view) referencing expr on partial view
12237
12238 Prev_D : Entity_Id;
12239 -- Entity of corresponding discriminant on partial view
12240
12241 New_D : Node_Id;
12242 -- Discriminant specification for full view, expression is
12243 -- the syntactic copy on full view (which has been checked for
12244 -- conformance with partial view), only used here to post error
12245 -- message.
12246
12247 begin
12248 D := First_Discriminant (T);
12249 New_D := First (Discriminant_Specifications (N));
12250 while Present (D) loop
12251 Prev_D := Current_Entity (D);
12252 Set_Current_Entity (D);
12253 Set_Is_Immediately_Visible (D);
12254 Set_Homonym (D, Prev_D);
12255
12256 -- Handle the case where there is an untagged partial view and
12257 -- the full view is tagged: must disallow discriminants with
12258 -- defaults, unless compiling for Ada 2012, which allows a
12259 -- limited tagged type to have defaulted discriminants (see
12260 -- AI05-0214). However, suppress error here if it was already
12261 -- reported on the default expression of the partial view.
12262
12263 if Is_Tagged_Type (T)
12264 and then Present (Expression (Parent (D)))
12265 and then (not Is_Limited_Type (Current_Scope)
12266 or else Ada_Version < Ada_2012)
12267 and then not Error_Posted (Expression (Parent (D)))
12268 then
12269 if Ada_Version >= Ada_2012 then
12270 Error_Msg_N
12271 ("discriminants of nonlimited tagged type cannot have "
12272 & "defaults",
12273 Expression (New_D));
12274 else
12275 Error_Msg_N
12276 ("discriminants of tagged type cannot have defaults",
12277 Expression (New_D));
12278 end if;
12279 end if;
12280
12281 -- Ada 2005 (AI-230): Access discriminant allowed in
12282 -- non-limited record types.
12283
12284 if Ada_Version < Ada_2005 then
12285
12286 -- This restriction gets applied to the full type here. It
12287 -- has already been applied earlier to the partial view.
12288
12289 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12290 end if;
12291
12292 Next_Discriminant (D);
12293 Next (New_D);
12294 end loop;
12295 end;
12296
12297 elsif Present (Discriminant_Specifications (N)) then
12298 Process_Discriminants (N, Prev);
12299 end if;
12300 end Check_Or_Process_Discriminants;
12301
12302 ----------------------
12303 -- Check_Real_Bound --
12304 ----------------------
12305
12306 procedure Check_Real_Bound (Bound : Node_Id) is
12307 begin
12308 if not Is_Real_Type (Etype (Bound)) then
12309 Error_Msg_N
12310 ("bound in real type definition must be of real type", Bound);
12311
12312 elsif not Is_OK_Static_Expression (Bound) then
12313 Flag_Non_Static_Expr
12314 ("non-static expression used for real type bound!", Bound);
12315
12316 else
12317 return;
12318 end if;
12319
12320 Rewrite
12321 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12322 Analyze (Bound);
12323 Resolve (Bound, Standard_Float);
12324 end Check_Real_Bound;
12325
12326 ------------------------------
12327 -- Complete_Private_Subtype --
12328 ------------------------------
12329
12330 procedure Complete_Private_Subtype
12331 (Priv : Entity_Id;
12332 Full : Entity_Id;
12333 Full_Base : Entity_Id;
12334 Related_Nod : Node_Id)
12335 is
12336 Save_Next_Entity : Entity_Id;
12337 Save_Homonym : Entity_Id;
12338
12339 begin
12340 -- Set semantic attributes for (implicit) private subtype completion.
12341 -- If the full type has no discriminants, then it is a copy of the
12342 -- full view of the base. Otherwise, it is a subtype of the base with
12343 -- a possible discriminant constraint. Save and restore the original
12344 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12345 -- not corrupt the entity chain.
12346
12347 Save_Next_Entity := Next_Entity (Full);
12348 Save_Homonym := Homonym (Priv);
12349
12350 if Ekind (Full_Base) in Private_Kind
12351 or else Ekind (Full_Base) in Protected_Kind
12352 or else Ekind (Full_Base) in Record_Kind
12353 or else Ekind (Full_Base) in Task_Kind
12354 then
12355 Copy_Node (Priv, Full);
12356
12357 -- Note that the Etype of the full view is the same as the Etype of
12358 -- the partial view. In this fashion, the subtype has access to the
12359 -- correct view of the parent.
12360
12361 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12362 Set_Has_Unknown_Discriminants
12363 (Full, Has_Unknown_Discriminants (Full_Base));
12364 Set_First_Entity (Full, First_Entity (Full_Base));
12365 Set_Last_Entity (Full, Last_Entity (Full_Base));
12366
12367 -- If the underlying base type is constrained, we know that the
12368 -- full view of the subtype is constrained as well (the converse
12369 -- is not necessarily true).
12370
12371 if Is_Constrained (Full_Base) then
12372 Set_Is_Constrained (Full);
12373 end if;
12374
12375 else
12376 Copy_Node (Full_Base, Full);
12377
12378 -- The following subtlety with the Etype of the full view needs to be
12379 -- taken into account here. One could think that it must naturally be
12380 -- set to the base type of the full base:
12381
12382 -- Set_Etype (Full, Base_Type (Full_Base));
12383
12384 -- so that the full view becomes a subtype of the full base when the
12385 -- latter is a base type, which must for example happen when the full
12386 -- base is declared as derived type. That's also correct if the full
12387 -- base is declared as an array type, or a floating-point type, or a
12388 -- fixed-point type, or a signed integer type, as these declarations
12389 -- create an implicit base type and a first subtype so the Etype of
12390 -- the full views must be the implicit base type. But that's wrong
12391 -- if the full base is declared as an access type, or an enumeration
12392 -- type, or a modular integer type, as these declarations directly
12393 -- create a base type, i.e. with Etype pointing to itself. Moreover
12394 -- the full base being declared in the private part, i.e. when the
12395 -- views are swapped, the end result is that the Etype of the full
12396 -- base is set to its private view in this case and that we need to
12397 -- propagate this setting to the full view in order for the subtype
12398 -- to be compatible with the base type.
12399
12400 if Is_Base_Type (Full_Base)
12401 and then (Is_Derived_Type (Full_Base)
12402 or else Ekind (Full_Base) in Array_Kind
12403 or else Ekind (Full_Base) in Fixed_Point_Kind
12404 or else Ekind (Full_Base) in Float_Kind
12405 or else Ekind (Full_Base) in Signed_Integer_Kind)
12406 then
12407 Set_Etype (Full, Full_Base);
12408 end if;
12409
12410 Set_Chars (Full, Chars (Priv));
12411 Set_Sloc (Full, Sloc (Priv));
12412 Conditional_Delay (Full, Priv);
12413 end if;
12414
12415 Link_Entities (Full, Save_Next_Entity);
12416 Set_Homonym (Full, Save_Homonym);
12417 Set_Associated_Node_For_Itype (Full, Related_Nod);
12418
12419 -- Set common attributes for all subtypes: kind, convention, etc.
12420
12421 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12422 Set_Convention (Full, Convention (Full_Base));
12423 Set_Is_First_Subtype (Full, False);
12424 Set_Scope (Full, Scope (Priv));
12425 Set_Size_Info (Full, Full_Base);
12426 Set_RM_Size (Full, RM_Size (Full_Base));
12427 Set_Is_Itype (Full);
12428
12429 -- A subtype of a private-type-without-discriminants, whose full-view
12430 -- has discriminants with default expressions, is not constrained.
12431
12432 if not Has_Discriminants (Priv) then
12433 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12434
12435 if Has_Discriminants (Full_Base) then
12436 Set_Discriminant_Constraint
12437 (Full, Discriminant_Constraint (Full_Base));
12438
12439 -- The partial view may have been indefinite, the full view
12440 -- might not be.
12441
12442 Set_Has_Unknown_Discriminants
12443 (Full, Has_Unknown_Discriminants (Full_Base));
12444 end if;
12445 end if;
12446
12447 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12448 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12449
12450 -- Freeze the private subtype entity if its parent is delayed, and not
12451 -- already frozen. We skip this processing if the type is an anonymous
12452 -- subtype of a record component, or is the corresponding record of a
12453 -- protected type, since these are processed when the enclosing type
12454 -- is frozen. If the parent type is declared in a nested package then
12455 -- the freezing of the private and full views also happens later.
12456
12457 if not Is_Type (Scope (Full)) then
12458 if Is_Itype (Priv)
12459 and then In_Same_Source_Unit (Full, Full_Base)
12460 and then Scope (Full_Base) /= Scope (Full)
12461 then
12462 Set_Has_Delayed_Freeze (Full);
12463 Set_Has_Delayed_Freeze (Priv);
12464
12465 else
12466 Set_Has_Delayed_Freeze (Full,
12467 Has_Delayed_Freeze (Full_Base)
12468 and then not Is_Frozen (Full_Base));
12469 end if;
12470 end if;
12471
12472 Set_Freeze_Node (Full, Empty);
12473 Set_Is_Frozen (Full, False);
12474 Set_Full_View (Priv, Full);
12475
12476 if Has_Discriminants (Full) then
12477 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12478 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12479
12480 if Has_Unknown_Discriminants (Full) then
12481 Set_Discriminant_Constraint (Full, No_Elist);
12482 end if;
12483 end if;
12484
12485 if Ekind (Full_Base) = E_Record_Type
12486 and then Has_Discriminants (Full_Base)
12487 and then Has_Discriminants (Priv) -- might not, if errors
12488 and then not Has_Unknown_Discriminants (Priv)
12489 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12490 then
12491 Create_Constrained_Components
12492 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12493
12494 -- If the full base is itself derived from private, build a congruent
12495 -- subtype of its underlying type, for use by the back end. For a
12496 -- constrained record component, the declaration cannot be placed on
12497 -- the component list, but it must nevertheless be built an analyzed, to
12498 -- supply enough information for Gigi to compute the size of component.
12499
12500 elsif Ekind (Full_Base) in Private_Kind
12501 and then Is_Derived_Type (Full_Base)
12502 and then Has_Discriminants (Full_Base)
12503 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12504 then
12505 if not Is_Itype (Priv)
12506 and then
12507 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12508 then
12509 Build_Underlying_Full_View
12510 (Parent (Priv), Full, Etype (Full_Base));
12511
12512 elsif Nkind (Related_Nod) = N_Component_Declaration then
12513 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12514 end if;
12515
12516 elsif Is_Record_Type (Full_Base) then
12517
12518 -- Show Full is simply a renaming of Full_Base
12519
12520 Set_Cloned_Subtype (Full, Full_Base);
12521 end if;
12522
12523 -- It is unsafe to share the bounds of a scalar type, because the Itype
12524 -- is elaborated on demand, and if a bound is nonstatic, then different
12525 -- orders of elaboration in different units will lead to different
12526 -- external symbols.
12527
12528 if Is_Scalar_Type (Full_Base) then
12529 Set_Scalar_Range (Full,
12530 Make_Range (Sloc (Related_Nod),
12531 Low_Bound =>
12532 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12533 High_Bound =>
12534 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12535
12536 -- This completion inherits the bounds of the full parent, but if
12537 -- the parent is an unconstrained floating point type, so is the
12538 -- completion.
12539
12540 if Is_Floating_Point_Type (Full_Base) then
12541 Set_Includes_Infinities
12542 (Scalar_Range (Full), Has_Infinities (Full_Base));
12543 end if;
12544 end if;
12545
12546 -- ??? It seems that a lot of fields are missing that should be copied
12547 -- from Full_Base to Full. Here are some that are introduced in a
12548 -- non-disruptive way but a cleanup is necessary.
12549
12550 if Is_Tagged_Type (Full_Base) then
12551 Set_Is_Tagged_Type (Full);
12552 Set_Direct_Primitive_Operations
12553 (Full, Direct_Primitive_Operations (Full_Base));
12554 Set_No_Tagged_Streams_Pragma
12555 (Full, No_Tagged_Streams_Pragma (Full_Base));
12556
12557 -- Inherit class_wide type of full_base in case the partial view was
12558 -- not tagged. Otherwise it has already been created when the private
12559 -- subtype was analyzed.
12560
12561 if No (Class_Wide_Type (Full)) then
12562 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12563 end if;
12564
12565 -- If this is a subtype of a protected or task type, constrain its
12566 -- corresponding record, unless this is a subtype without constraints,
12567 -- i.e. a simple renaming as with an actual subtype in an instance.
12568
12569 elsif Is_Concurrent_Type (Full_Base) then
12570 if Has_Discriminants (Full)
12571 and then Present (Corresponding_Record_Type (Full_Base))
12572 and then
12573 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12574 then
12575 Set_Corresponding_Record_Type (Full,
12576 Constrain_Corresponding_Record
12577 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12578
12579 else
12580 Set_Corresponding_Record_Type (Full,
12581 Corresponding_Record_Type (Full_Base));
12582 end if;
12583 end if;
12584
12585 -- Link rep item chain, and also setting of Has_Predicates from private
12586 -- subtype to full subtype, since we will need these on the full subtype
12587 -- to create the predicate function. Note that the full subtype may
12588 -- already have rep items, inherited from the full view of the base
12589 -- type, so we must be sure not to overwrite these entries.
12590
12591 declare
12592 Append : Boolean;
12593 Item : Node_Id;
12594 Next_Item : Node_Id;
12595 Priv_Item : Node_Id;
12596
12597 begin
12598 Item := First_Rep_Item (Full);
12599 Priv_Item := First_Rep_Item (Priv);
12600
12601 -- If no existing rep items on full type, we can just link directly
12602 -- to the list of items on the private type, if any exist.. Same if
12603 -- the rep items are only those inherited from the base
12604
12605 if (No (Item)
12606 or else Nkind (Item) /= N_Aspect_Specification
12607 or else Entity (Item) = Full_Base)
12608 and then Present (First_Rep_Item (Priv))
12609 then
12610 Set_First_Rep_Item (Full, Priv_Item);
12611
12612 -- Otherwise, search to the end of items currently linked to the full
12613 -- subtype and append the private items to the end. However, if Priv
12614 -- and Full already have the same list of rep items, then the append
12615 -- is not done, as that would create a circularity.
12616 --
12617 -- The partial view may have a predicate and the rep item lists of
12618 -- both views agree when inherited from the same ancestor. In that
12619 -- case, simply propagate the list from one view to the other.
12620 -- A more complex analysis needed here ???
12621
12622 elsif Present (Priv_Item)
12623 and then Item = Next_Rep_Item (Priv_Item)
12624 then
12625 Set_First_Rep_Item (Full, Priv_Item);
12626
12627 elsif Item /= Priv_Item then
12628 Append := True;
12629 loop
12630 Next_Item := Next_Rep_Item (Item);
12631 exit when No (Next_Item);
12632 Item := Next_Item;
12633
12634 -- If the private view has aspect specifications, the full view
12635 -- inherits them. Since these aspects may already have been
12636 -- attached to the full view during derivation, do not append
12637 -- them if already present.
12638
12639 if Item = First_Rep_Item (Priv) then
12640 Append := False;
12641 exit;
12642 end if;
12643 end loop;
12644
12645 -- And link the private type items at the end of the chain
12646
12647 if Append then
12648 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12649 end if;
12650 end if;
12651 end;
12652
12653 -- Make sure Has_Predicates is set on full type if it is set on the
12654 -- private type. Note that it may already be set on the full type and
12655 -- if so, we don't want to unset it. Similarly, propagate information
12656 -- about delayed aspects, because the corresponding pragmas must be
12657 -- analyzed when one of the views is frozen. This last step is needed
12658 -- in particular when the full type is a scalar type for which an
12659 -- anonymous base type is constructed.
12660
12661 -- The predicate functions are generated either at the freeze point
12662 -- of the type or at the end of the visible part, and we must avoid
12663 -- generating them twice.
12664
12665 if Has_Predicates (Priv) then
12666 Set_Has_Predicates (Full);
12667
12668 if Present (Predicate_Function (Priv))
12669 and then No (Predicate_Function (Full))
12670 then
12671 Set_Predicate_Function (Full, Predicate_Function (Priv));
12672 end if;
12673 end if;
12674
12675 if Has_Delayed_Aspects (Priv) then
12676 Set_Has_Delayed_Aspects (Full);
12677 end if;
12678 end Complete_Private_Subtype;
12679
12680 ----------------------------
12681 -- Constant_Redeclaration --
12682 ----------------------------
12683
12684 procedure Constant_Redeclaration
12685 (Id : Entity_Id;
12686 N : Node_Id;
12687 T : out Entity_Id)
12688 is
12689 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12690 Obj_Def : constant Node_Id := Object_Definition (N);
12691 New_T : Entity_Id;
12692
12693 procedure Check_Possible_Deferred_Completion
12694 (Prev_Id : Entity_Id;
12695 Prev_Obj_Def : Node_Id;
12696 Curr_Obj_Def : Node_Id);
12697 -- Determine whether the two object definitions describe the partial
12698 -- and the full view of a constrained deferred constant. Generate
12699 -- a subtype for the full view and verify that it statically matches
12700 -- the subtype of the partial view.
12701
12702 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12703 -- If deferred constant is an access type initialized with an allocator,
12704 -- check whether there is an illegal recursion in the definition,
12705 -- through a default value of some record subcomponent. This is normally
12706 -- detected when generating init procs, but requires this additional
12707 -- mechanism when expansion is disabled.
12708
12709 ----------------------------------------
12710 -- Check_Possible_Deferred_Completion --
12711 ----------------------------------------
12712
12713 procedure Check_Possible_Deferred_Completion
12714 (Prev_Id : Entity_Id;
12715 Prev_Obj_Def : Node_Id;
12716 Curr_Obj_Def : Node_Id)
12717 is
12718 begin
12719 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12720 and then Present (Constraint (Prev_Obj_Def))
12721 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12722 and then Present (Constraint (Curr_Obj_Def))
12723 then
12724 declare
12725 Loc : constant Source_Ptr := Sloc (N);
12726 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12727 Decl : constant Node_Id :=
12728 Make_Subtype_Declaration (Loc,
12729 Defining_Identifier => Def_Id,
12730 Subtype_Indication =>
12731 Relocate_Node (Curr_Obj_Def));
12732
12733 begin
12734 Insert_Before_And_Analyze (N, Decl);
12735 Set_Etype (Id, Def_Id);
12736
12737 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12738 Error_Msg_Sloc := Sloc (Prev_Id);
12739 Error_Msg_N ("subtype does not statically match deferred "
12740 & "declaration #", N);
12741 end if;
12742 end;
12743 end if;
12744 end Check_Possible_Deferred_Completion;
12745
12746 ---------------------------------
12747 -- Check_Recursive_Declaration --
12748 ---------------------------------
12749
12750 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12751 Comp : Entity_Id;
12752
12753 begin
12754 if Is_Record_Type (Typ) then
12755 Comp := First_Component (Typ);
12756 while Present (Comp) loop
12757 if Comes_From_Source (Comp) then
12758 if Present (Expression (Parent (Comp)))
12759 and then Is_Entity_Name (Expression (Parent (Comp)))
12760 and then Entity (Expression (Parent (Comp))) = Prev
12761 then
12762 Error_Msg_Sloc := Sloc (Parent (Comp));
12763 Error_Msg_NE
12764 ("illegal circularity with declaration for & #",
12765 N, Comp);
12766 return;
12767
12768 elsif Is_Record_Type (Etype (Comp)) then
12769 Check_Recursive_Declaration (Etype (Comp));
12770 end if;
12771 end if;
12772
12773 Next_Component (Comp);
12774 end loop;
12775 end if;
12776 end Check_Recursive_Declaration;
12777
12778 -- Start of processing for Constant_Redeclaration
12779
12780 begin
12781 if Nkind (Parent (Prev)) = N_Object_Declaration then
12782 if Nkind (Object_Definition
12783 (Parent (Prev))) = N_Subtype_Indication
12784 then
12785 -- Find type of new declaration. The constraints of the two
12786 -- views must match statically, but there is no point in
12787 -- creating an itype for the full view.
12788
12789 if Nkind (Obj_Def) = N_Subtype_Indication then
12790 Find_Type (Subtype_Mark (Obj_Def));
12791 New_T := Entity (Subtype_Mark (Obj_Def));
12792
12793 else
12794 Find_Type (Obj_Def);
12795 New_T := Entity (Obj_Def);
12796 end if;
12797
12798 T := Etype (Prev);
12799
12800 else
12801 -- The full view may impose a constraint, even if the partial
12802 -- view does not, so construct the subtype.
12803
12804 New_T := Find_Type_Of_Object (Obj_Def, N);
12805 T := New_T;
12806 end if;
12807
12808 else
12809 -- Current declaration is illegal, diagnosed below in Enter_Name
12810
12811 T := Empty;
12812 New_T := Any_Type;
12813 end if;
12814
12815 -- If previous full declaration or a renaming declaration exists, or if
12816 -- a homograph is present, let Enter_Name handle it, either with an
12817 -- error or with the removal of an overridden implicit subprogram.
12818 -- The previous one is a full declaration if it has an expression
12819 -- (which in the case of an aggregate is indicated by the Init flag).
12820
12821 if Ekind (Prev) /= E_Constant
12822 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12823 or else Present (Expression (Parent (Prev)))
12824 or else Has_Init_Expression (Parent (Prev))
12825 or else Present (Full_View (Prev))
12826 then
12827 Enter_Name (Id);
12828
12829 -- Verify that types of both declarations match, or else that both types
12830 -- are anonymous access types whose designated subtypes statically match
12831 -- (as allowed in Ada 2005 by AI-385).
12832
12833 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12834 and then
12835 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12836 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12837 or else Is_Access_Constant (Etype (New_T)) /=
12838 Is_Access_Constant (Etype (Prev))
12839 or else Can_Never_Be_Null (Etype (New_T)) /=
12840 Can_Never_Be_Null (Etype (Prev))
12841 or else Null_Exclusion_Present (Parent (Prev)) /=
12842 Null_Exclusion_Present (Parent (Id))
12843 or else not Subtypes_Statically_Match
12844 (Designated_Type (Etype (Prev)),
12845 Designated_Type (Etype (New_T))))
12846 then
12847 Error_Msg_Sloc := Sloc (Prev);
12848 Error_Msg_N ("type does not match declaration#", N);
12849 Set_Full_View (Prev, Id);
12850 Set_Etype (Id, Any_Type);
12851
12852 -- A deferred constant whose type is an anonymous array is always
12853 -- illegal (unless imported). A detailed error message might be
12854 -- helpful for Ada beginners.
12855
12856 if Nkind (Object_Definition (Parent (Prev)))
12857 = N_Constrained_Array_Definition
12858 and then Nkind (Object_Definition (N))
12859 = N_Constrained_Array_Definition
12860 then
12861 Error_Msg_N ("\each anonymous array is a distinct type", N);
12862 Error_Msg_N ("a deferred constant must have a named type",
12863 Object_Definition (Parent (Prev)));
12864 end if;
12865
12866 elsif
12867 Null_Exclusion_Present (Parent (Prev))
12868 and then not Null_Exclusion_Present (N)
12869 then
12870 Error_Msg_Sloc := Sloc (Prev);
12871 Error_Msg_N ("null-exclusion does not match declaration#", N);
12872 Set_Full_View (Prev, Id);
12873 Set_Etype (Id, Any_Type);
12874
12875 -- If so, process the full constant declaration
12876
12877 else
12878 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12879 -- the deferred declaration is constrained, then the subtype defined
12880 -- by the subtype_indication in the full declaration shall match it
12881 -- statically.
12882
12883 Check_Possible_Deferred_Completion
12884 (Prev_Id => Prev,
12885 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12886 Curr_Obj_Def => Obj_Def);
12887
12888 Set_Full_View (Prev, Id);
12889 Set_Is_Public (Id, Is_Public (Prev));
12890 Set_Is_Internal (Id);
12891 Append_Entity (Id, Current_Scope);
12892
12893 -- Check ALIASED present if present before (RM 7.4(7))
12894
12895 if Is_Aliased (Prev)
12896 and then not Aliased_Present (N)
12897 then
12898 Error_Msg_Sloc := Sloc (Prev);
12899 Error_Msg_N ("ALIASED required (see declaration #)", N);
12900 end if;
12901
12902 -- Check that placement is in private part and that the incomplete
12903 -- declaration appeared in the visible part.
12904
12905 if Ekind (Current_Scope) = E_Package
12906 and then not In_Private_Part (Current_Scope)
12907 then
12908 Error_Msg_Sloc := Sloc (Prev);
12909 Error_Msg_N
12910 ("full constant for declaration # must be in private part", N);
12911
12912 elsif Ekind (Current_Scope) = E_Package
12913 and then
12914 List_Containing (Parent (Prev)) /=
12915 Visible_Declarations (Package_Specification (Current_Scope))
12916 then
12917 Error_Msg_N
12918 ("deferred constant must be declared in visible part",
12919 Parent (Prev));
12920 end if;
12921
12922 if Is_Access_Type (T)
12923 and then Nkind (Expression (N)) = N_Allocator
12924 then
12925 Check_Recursive_Declaration (Designated_Type (T));
12926 end if;
12927
12928 -- A deferred constant is a visible entity. If type has invariants,
12929 -- verify that the initial value satisfies them. This is not done in
12930 -- GNATprove mode, as GNATprove handles invariant checks itself.
12931
12932 if Has_Invariants (T)
12933 and then Present (Invariant_Procedure (T))
12934 and then not GNATprove_Mode
12935 then
12936 Insert_After (N,
12937 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12938 end if;
12939 end if;
12940 end Constant_Redeclaration;
12941
12942 ----------------------
12943 -- Constrain_Access --
12944 ----------------------
12945
12946 procedure Constrain_Access
12947 (Def_Id : in out Entity_Id;
12948 S : Node_Id;
12949 Related_Nod : Node_Id)
12950 is
12951 T : constant Entity_Id := Entity (Subtype_Mark (S));
12952 Desig_Type : constant Entity_Id := Designated_Type (T);
12953 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12954 Constraint_OK : Boolean := True;
12955
12956 begin
12957 if Is_Array_Type (Desig_Type) then
12958 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12959
12960 elsif (Is_Record_Type (Desig_Type)
12961 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12962 and then not Is_Constrained (Desig_Type)
12963 then
12964 -- ??? The following code is a temporary bypass to ignore a
12965 -- discriminant constraint on access type if it is constraining
12966 -- the current record. Avoid creating the implicit subtype of the
12967 -- record we are currently compiling since right now, we cannot
12968 -- handle these. For now, just return the access type itself.
12969
12970 if Desig_Type = Current_Scope
12971 and then No (Def_Id)
12972 then
12973 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12974 Def_Id := Entity (Subtype_Mark (S));
12975
12976 -- This call added to ensure that the constraint is analyzed
12977 -- (needed for a B test). Note that we still return early from
12978 -- this procedure to avoid recursive processing. ???
12979
12980 Constrain_Discriminated_Type
12981 (Desig_Subtype, S, Related_Nod, For_Access => True);
12982 return;
12983 end if;
12984
12985 -- Enforce rule that the constraint is illegal if there is an
12986 -- unconstrained view of the designated type. This means that the
12987 -- partial view (either a private type declaration or a derivation
12988 -- from a private type) has no discriminants. (Defect Report
12989 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12990
12991 -- Rule updated for Ada 2005: The private type is said to have
12992 -- a constrained partial view, given that objects of the type
12993 -- can be declared. Furthermore, the rule applies to all access
12994 -- types, unlike the rule concerning default discriminants (see
12995 -- RM 3.7.1(7/3))
12996
12997 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12998 and then Has_Private_Declaration (Desig_Type)
12999 and then In_Open_Scopes (Scope (Desig_Type))
13000 and then Has_Discriminants (Desig_Type)
13001 then
13002 declare
13003 Pack : constant Node_Id :=
13004 Unit_Declaration_Node (Scope (Desig_Type));
13005 Decls : List_Id;
13006 Decl : Node_Id;
13007
13008 begin
13009 if Nkind (Pack) = N_Package_Declaration then
13010 Decls := Visible_Declarations (Specification (Pack));
13011 Decl := First (Decls);
13012 while Present (Decl) loop
13013 if (Nkind (Decl) = N_Private_Type_Declaration
13014 and then Chars (Defining_Identifier (Decl)) =
13015 Chars (Desig_Type))
13016
13017 or else
13018 (Nkind (Decl) = N_Full_Type_Declaration
13019 and then
13020 Chars (Defining_Identifier (Decl)) =
13021 Chars (Desig_Type)
13022 and then Is_Derived_Type (Desig_Type)
13023 and then
13024 Has_Private_Declaration (Etype (Desig_Type)))
13025 then
13026 if No (Discriminant_Specifications (Decl)) then
13027 Error_Msg_N
13028 ("cannot constrain access type if designated "
13029 & "type has constrained partial view", S);
13030 end if;
13031
13032 exit;
13033 end if;
13034
13035 Next (Decl);
13036 end loop;
13037 end if;
13038 end;
13039 end if;
13040
13041 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13042 For_Access => True);
13043
13044 elsif Is_Concurrent_Type (Desig_Type)
13045 and then not Is_Constrained (Desig_Type)
13046 then
13047 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13048
13049 else
13050 Error_Msg_N ("invalid constraint on access type", S);
13051
13052 -- We simply ignore an invalid constraint
13053
13054 Desig_Subtype := Desig_Type;
13055 Constraint_OK := False;
13056 end if;
13057
13058 if No (Def_Id) then
13059 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13060 else
13061 Set_Ekind (Def_Id, E_Access_Subtype);
13062 end if;
13063
13064 if Constraint_OK then
13065 Set_Etype (Def_Id, Base_Type (T));
13066
13067 if Is_Private_Type (Desig_Type) then
13068 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13069 end if;
13070 else
13071 Set_Etype (Def_Id, Any_Type);
13072 end if;
13073
13074 Set_Size_Info (Def_Id, T);
13075 Set_Is_Constrained (Def_Id, Constraint_OK);
13076 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13077 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13078 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13079
13080 Conditional_Delay (Def_Id, T);
13081
13082 -- AI-363 : Subtypes of general access types whose designated types have
13083 -- default discriminants are disallowed. In instances, the rule has to
13084 -- be checked against the actual, of which T is the subtype. In a
13085 -- generic body, the rule is checked assuming that the actual type has
13086 -- defaulted discriminants.
13087
13088 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13089 if Ekind (Base_Type (T)) = E_General_Access_Type
13090 and then Has_Defaulted_Discriminants (Desig_Type)
13091 then
13092 if Ada_Version < Ada_2005 then
13093 Error_Msg_N
13094 ("access subtype of general access type would not " &
13095 "be allowed in Ada 2005?y?", S);
13096 else
13097 Error_Msg_N
13098 ("access subtype of general access type not allowed", S);
13099 end if;
13100
13101 Error_Msg_N ("\discriminants have defaults", S);
13102
13103 elsif Is_Access_Type (T)
13104 and then Is_Generic_Type (Desig_Type)
13105 and then Has_Discriminants (Desig_Type)
13106 and then In_Package_Body (Current_Scope)
13107 then
13108 if Ada_Version < Ada_2005 then
13109 Error_Msg_N
13110 ("access subtype would not be allowed in generic body "
13111 & "in Ada 2005?y?", S);
13112 else
13113 Error_Msg_N
13114 ("access subtype not allowed in generic body", S);
13115 end if;
13116
13117 Error_Msg_N
13118 ("\designated type is a discriminated formal", S);
13119 end if;
13120 end if;
13121 end Constrain_Access;
13122
13123 ---------------------
13124 -- Constrain_Array --
13125 ---------------------
13126
13127 procedure Constrain_Array
13128 (Def_Id : in out Entity_Id;
13129 SI : Node_Id;
13130 Related_Nod : Node_Id;
13131 Related_Id : Entity_Id;
13132 Suffix : Character)
13133 is
13134 C : constant Node_Id := Constraint (SI);
13135 Number_Of_Constraints : Nat := 0;
13136 Index : Node_Id;
13137 S, T : Entity_Id;
13138 Constraint_OK : Boolean := True;
13139
13140 begin
13141 T := Entity (Subtype_Mark (SI));
13142
13143 if Is_Access_Type (T) then
13144 T := Designated_Type (T);
13145 end if;
13146
13147 -- If an index constraint follows a subtype mark in a subtype indication
13148 -- then the type or subtype denoted by the subtype mark must not already
13149 -- impose an index constraint. The subtype mark must denote either an
13150 -- unconstrained array type or an access type whose designated type
13151 -- is such an array type... (RM 3.6.1)
13152
13153 if Is_Constrained (T) then
13154 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13155 Constraint_OK := False;
13156
13157 else
13158 S := First (Constraints (C));
13159 while Present (S) loop
13160 Number_Of_Constraints := Number_Of_Constraints + 1;
13161 Next (S);
13162 end loop;
13163
13164 -- In either case, the index constraint must provide a discrete
13165 -- range for each index of the array type and the type of each
13166 -- discrete range must be the same as that of the corresponding
13167 -- index. (RM 3.6.1)
13168
13169 if Number_Of_Constraints /= Number_Dimensions (T) then
13170 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13171 Constraint_OK := False;
13172
13173 else
13174 S := First (Constraints (C));
13175 Index := First_Index (T);
13176 Analyze (Index);
13177
13178 -- Apply constraints to each index type
13179
13180 for J in 1 .. Number_Of_Constraints loop
13181 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13182 Next (Index);
13183 Next (S);
13184 end loop;
13185
13186 end if;
13187 end if;
13188
13189 if No (Def_Id) then
13190 Def_Id :=
13191 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13192 Set_Parent (Def_Id, Related_Nod);
13193
13194 else
13195 Set_Ekind (Def_Id, E_Array_Subtype);
13196 end if;
13197
13198 Set_Size_Info (Def_Id, (T));
13199 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13200 Set_Etype (Def_Id, Base_Type (T));
13201
13202 if Constraint_OK then
13203 Set_First_Index (Def_Id, First (Constraints (C)));
13204 else
13205 Set_First_Index (Def_Id, First_Index (T));
13206 end if;
13207
13208 Set_Is_Constrained (Def_Id, True);
13209 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13210 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13211
13212 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13213 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13214
13215 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13216 -- We need to initialize the attribute because if Def_Id is previously
13217 -- analyzed through a limited_with clause, it will have the attributes
13218 -- of an incomplete type, one of which is an Elist that overlaps the
13219 -- Packed_Array_Impl_Type field.
13220
13221 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13222
13223 -- Build a freeze node if parent still needs one. Also make sure that
13224 -- the Depends_On_Private status is set because the subtype will need
13225 -- reprocessing at the time the base type does, and also we must set a
13226 -- conditional delay.
13227
13228 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13229 Conditional_Delay (Def_Id, T);
13230 end Constrain_Array;
13231
13232 ------------------------------
13233 -- Constrain_Component_Type --
13234 ------------------------------
13235
13236 function Constrain_Component_Type
13237 (Comp : Entity_Id;
13238 Constrained_Typ : Entity_Id;
13239 Related_Node : Node_Id;
13240 Typ : Entity_Id;
13241 Constraints : Elist_Id) return Entity_Id
13242 is
13243 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13244 Compon_Type : constant Entity_Id := Etype (Comp);
13245
13246 function Build_Constrained_Array_Type
13247 (Old_Type : Entity_Id) return Entity_Id;
13248 -- If Old_Type is an array type, one of whose indexes is constrained
13249 -- by a discriminant, build an Itype whose constraint replaces the
13250 -- discriminant with its value in the constraint.
13251
13252 function Build_Constrained_Discriminated_Type
13253 (Old_Type : Entity_Id) return Entity_Id;
13254 -- Ditto for record components
13255
13256 function Build_Constrained_Access_Type
13257 (Old_Type : Entity_Id) return Entity_Id;
13258 -- Ditto for access types. Makes use of previous two functions, to
13259 -- constrain designated type.
13260
13261 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13262 -- T is an array or discriminated type, C is a list of constraints
13263 -- that apply to T. This routine builds the constrained subtype.
13264
13265 function Is_Discriminant (Expr : Node_Id) return Boolean;
13266 -- Returns True if Expr is a discriminant
13267
13268 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13269 -- Find the value of discriminant Discrim in Constraint
13270
13271 -----------------------------------
13272 -- Build_Constrained_Access_Type --
13273 -----------------------------------
13274
13275 function Build_Constrained_Access_Type
13276 (Old_Type : Entity_Id) return Entity_Id
13277 is
13278 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13279 Itype : Entity_Id;
13280 Desig_Subtype : Entity_Id;
13281 Scop : Entity_Id;
13282
13283 begin
13284 -- if the original access type was not embedded in the enclosing
13285 -- type definition, there is no need to produce a new access
13286 -- subtype. In fact every access type with an explicit constraint
13287 -- generates an itype whose scope is the enclosing record.
13288
13289 if not Is_Type (Scope (Old_Type)) then
13290 return Old_Type;
13291
13292 elsif Is_Array_Type (Desig_Type) then
13293 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13294
13295 elsif Has_Discriminants (Desig_Type) then
13296
13297 -- This may be an access type to an enclosing record type for
13298 -- which we are constructing the constrained components. Return
13299 -- the enclosing record subtype. This is not always correct,
13300 -- but avoids infinite recursion. ???
13301
13302 Desig_Subtype := Any_Type;
13303
13304 for J in reverse 0 .. Scope_Stack.Last loop
13305 Scop := Scope_Stack.Table (J).Entity;
13306
13307 if Is_Type (Scop)
13308 and then Base_Type (Scop) = Base_Type (Desig_Type)
13309 then
13310 Desig_Subtype := Scop;
13311 end if;
13312
13313 exit when not Is_Type (Scop);
13314 end loop;
13315
13316 if Desig_Subtype = Any_Type then
13317 Desig_Subtype :=
13318 Build_Constrained_Discriminated_Type (Desig_Type);
13319 end if;
13320
13321 else
13322 return Old_Type;
13323 end if;
13324
13325 if Desig_Subtype /= Desig_Type then
13326
13327 -- The Related_Node better be here or else we won't be able
13328 -- to attach new itypes to a node in the tree.
13329
13330 pragma Assert (Present (Related_Node));
13331
13332 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13333
13334 Set_Etype (Itype, Base_Type (Old_Type));
13335 Set_Size_Info (Itype, (Old_Type));
13336 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13337 Set_Depends_On_Private (Itype, Has_Private_Component
13338 (Old_Type));
13339 Set_Is_Access_Constant (Itype, Is_Access_Constant
13340 (Old_Type));
13341
13342 -- The new itype needs freezing when it depends on a not frozen
13343 -- type and the enclosing subtype needs freezing.
13344
13345 if Has_Delayed_Freeze (Constrained_Typ)
13346 and then not Is_Frozen (Constrained_Typ)
13347 then
13348 Conditional_Delay (Itype, Base_Type (Old_Type));
13349 end if;
13350
13351 return Itype;
13352
13353 else
13354 return Old_Type;
13355 end if;
13356 end Build_Constrained_Access_Type;
13357
13358 ----------------------------------
13359 -- Build_Constrained_Array_Type --
13360 ----------------------------------
13361
13362 function Build_Constrained_Array_Type
13363 (Old_Type : Entity_Id) return Entity_Id
13364 is
13365 Lo_Expr : Node_Id;
13366 Hi_Expr : Node_Id;
13367 Old_Index : Node_Id;
13368 Range_Node : Node_Id;
13369 Constr_List : List_Id;
13370
13371 Need_To_Create_Itype : Boolean := False;
13372
13373 begin
13374 Old_Index := First_Index (Old_Type);
13375 while Present (Old_Index) loop
13376 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13377
13378 if Is_Discriminant (Lo_Expr)
13379 or else
13380 Is_Discriminant (Hi_Expr)
13381 then
13382 Need_To_Create_Itype := True;
13383 end if;
13384
13385 Next_Index (Old_Index);
13386 end loop;
13387
13388 if Need_To_Create_Itype then
13389 Constr_List := New_List;
13390
13391 Old_Index := First_Index (Old_Type);
13392 while Present (Old_Index) loop
13393 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13394
13395 if Is_Discriminant (Lo_Expr) then
13396 Lo_Expr := Get_Discr_Value (Lo_Expr);
13397 end if;
13398
13399 if Is_Discriminant (Hi_Expr) then
13400 Hi_Expr := Get_Discr_Value (Hi_Expr);
13401 end if;
13402
13403 Range_Node :=
13404 Make_Range
13405 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13406
13407 Append (Range_Node, To => Constr_List);
13408
13409 Next_Index (Old_Index);
13410 end loop;
13411
13412 return Build_Subtype (Old_Type, Constr_List);
13413
13414 else
13415 return Old_Type;
13416 end if;
13417 end Build_Constrained_Array_Type;
13418
13419 ------------------------------------------
13420 -- Build_Constrained_Discriminated_Type --
13421 ------------------------------------------
13422
13423 function Build_Constrained_Discriminated_Type
13424 (Old_Type : Entity_Id) return Entity_Id
13425 is
13426 Expr : Node_Id;
13427 Constr_List : List_Id;
13428 Old_Constraint : Elmt_Id;
13429
13430 Need_To_Create_Itype : Boolean := False;
13431
13432 begin
13433 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13434 while Present (Old_Constraint) loop
13435 Expr := Node (Old_Constraint);
13436
13437 if Is_Discriminant (Expr) then
13438 Need_To_Create_Itype := True;
13439 end if;
13440
13441 Next_Elmt (Old_Constraint);
13442 end loop;
13443
13444 if Need_To_Create_Itype then
13445 Constr_List := New_List;
13446
13447 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13448 while Present (Old_Constraint) loop
13449 Expr := Node (Old_Constraint);
13450
13451 if Is_Discriminant (Expr) then
13452 Expr := Get_Discr_Value (Expr);
13453 end if;
13454
13455 Append (New_Copy_Tree (Expr), To => Constr_List);
13456
13457 Next_Elmt (Old_Constraint);
13458 end loop;
13459
13460 return Build_Subtype (Old_Type, Constr_List);
13461
13462 else
13463 return Old_Type;
13464 end if;
13465 end Build_Constrained_Discriminated_Type;
13466
13467 -------------------
13468 -- Build_Subtype --
13469 -------------------
13470
13471 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13472 Indic : Node_Id;
13473 Subtyp_Decl : Node_Id;
13474 Def_Id : Entity_Id;
13475 Btyp : Entity_Id := Base_Type (T);
13476
13477 begin
13478 -- The Related_Node better be here or else we won't be able to
13479 -- attach new itypes to a node in the tree.
13480
13481 pragma Assert (Present (Related_Node));
13482
13483 -- If the view of the component's type is incomplete or private
13484 -- with unknown discriminants, then the constraint must be applied
13485 -- to the full type.
13486
13487 if Has_Unknown_Discriminants (Btyp)
13488 and then Present (Underlying_Type (Btyp))
13489 then
13490 Btyp := Underlying_Type (Btyp);
13491 end if;
13492
13493 Indic :=
13494 Make_Subtype_Indication (Loc,
13495 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13496 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13497
13498 Def_Id := Create_Itype (Ekind (T), Related_Node);
13499
13500 Subtyp_Decl :=
13501 Make_Subtype_Declaration (Loc,
13502 Defining_Identifier => Def_Id,
13503 Subtype_Indication => Indic);
13504
13505 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13506
13507 -- Itypes must be analyzed with checks off (see package Itypes)
13508
13509 Analyze (Subtyp_Decl, Suppress => All_Checks);
13510
13511 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13512 Inherit_Predicate_Flags (Def_Id, T);
13513
13514 -- Indicate where the predicate function may be found
13515
13516 if Is_Itype (T) then
13517 if Present (Predicate_Function (Def_Id)) then
13518 null;
13519
13520 elsif Present (Predicate_Function (T)) then
13521 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13522
13523 else
13524 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13525 end if;
13526
13527 elsif No (Predicate_Function (Def_Id)) then
13528 Set_Predicated_Parent (Def_Id, T);
13529 end if;
13530 end if;
13531
13532 return Def_Id;
13533 end Build_Subtype;
13534
13535 ---------------------
13536 -- Get_Discr_Value --
13537 ---------------------
13538
13539 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13540 D : Entity_Id;
13541 E : Elmt_Id;
13542
13543 begin
13544 -- The discriminant may be declared for the type, in which case we
13545 -- find it by iterating over the list of discriminants. If the
13546 -- discriminant is inherited from a parent type, it appears as the
13547 -- corresponding discriminant of the current type. This will be the
13548 -- case when constraining an inherited component whose constraint is
13549 -- given by a discriminant of the parent.
13550
13551 D := First_Discriminant (Typ);
13552 E := First_Elmt (Constraints);
13553
13554 while Present (D) loop
13555 if D = Entity (Discrim)
13556 or else D = CR_Discriminant (Entity (Discrim))
13557 or else Corresponding_Discriminant (D) = Entity (Discrim)
13558 then
13559 return Node (E);
13560 end if;
13561
13562 Next_Discriminant (D);
13563 Next_Elmt (E);
13564 end loop;
13565
13566 -- The Corresponding_Discriminant mechanism is incomplete, because
13567 -- the correspondence between new and old discriminants is not one
13568 -- to one: one new discriminant can constrain several old ones. In
13569 -- that case, scan sequentially the stored_constraint, the list of
13570 -- discriminants of the parents, and the constraints.
13571
13572 -- Previous code checked for the present of the Stored_Constraint
13573 -- list for the derived type, but did not use it at all. Should it
13574 -- be present when the component is a discriminated task type?
13575
13576 if Is_Derived_Type (Typ)
13577 and then Scope (Entity (Discrim)) = Etype (Typ)
13578 then
13579 D := First_Discriminant (Etype (Typ));
13580 E := First_Elmt (Constraints);
13581 while Present (D) loop
13582 if D = Entity (Discrim) then
13583 return Node (E);
13584 end if;
13585
13586 Next_Discriminant (D);
13587 Next_Elmt (E);
13588 end loop;
13589 end if;
13590
13591 -- Something is wrong if we did not find the value
13592
13593 raise Program_Error;
13594 end Get_Discr_Value;
13595
13596 ---------------------
13597 -- Is_Discriminant --
13598 ---------------------
13599
13600 function Is_Discriminant (Expr : Node_Id) return Boolean is
13601 Discrim_Scope : Entity_Id;
13602
13603 begin
13604 if Denotes_Discriminant (Expr) then
13605 Discrim_Scope := Scope (Entity (Expr));
13606
13607 -- Either we have a reference to one of Typ's discriminants,
13608
13609 pragma Assert (Discrim_Scope = Typ
13610
13611 -- or to the discriminants of the parent type, in the case
13612 -- of a derivation of a tagged type with variants.
13613
13614 or else Discrim_Scope = Etype (Typ)
13615 or else Full_View (Discrim_Scope) = Etype (Typ)
13616
13617 -- or same as above for the case where the discriminants
13618 -- were declared in Typ's private view.
13619
13620 or else (Is_Private_Type (Discrim_Scope)
13621 and then Chars (Discrim_Scope) = Chars (Typ))
13622
13623 -- or else we are deriving from the full view and the
13624 -- discriminant is declared in the private entity.
13625
13626 or else (Is_Private_Type (Typ)
13627 and then Chars (Discrim_Scope) = Chars (Typ))
13628
13629 -- Or we are constrained the corresponding record of a
13630 -- synchronized type that completes a private declaration.
13631
13632 or else (Is_Concurrent_Record_Type (Typ)
13633 and then
13634 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13635
13636 -- or we have a class-wide type, in which case make sure the
13637 -- discriminant found belongs to the root type.
13638
13639 or else (Is_Class_Wide_Type (Typ)
13640 and then Etype (Typ) = Discrim_Scope));
13641
13642 return True;
13643 end if;
13644
13645 -- In all other cases we have something wrong
13646
13647 return False;
13648 end Is_Discriminant;
13649
13650 -- Start of processing for Constrain_Component_Type
13651
13652 begin
13653 if Nkind (Parent (Comp)) = N_Component_Declaration
13654 and then Comes_From_Source (Parent (Comp))
13655 and then Comes_From_Source
13656 (Subtype_Indication (Component_Definition (Parent (Comp))))
13657 and then
13658 Is_Entity_Name
13659 (Subtype_Indication (Component_Definition (Parent (Comp))))
13660 then
13661 return Compon_Type;
13662
13663 elsif Is_Array_Type (Compon_Type) then
13664 return Build_Constrained_Array_Type (Compon_Type);
13665
13666 elsif Has_Discriminants (Compon_Type) then
13667 return Build_Constrained_Discriminated_Type (Compon_Type);
13668
13669 elsif Is_Access_Type (Compon_Type) then
13670 return Build_Constrained_Access_Type (Compon_Type);
13671
13672 else
13673 return Compon_Type;
13674 end if;
13675 end Constrain_Component_Type;
13676
13677 --------------------------
13678 -- Constrain_Concurrent --
13679 --------------------------
13680
13681 -- For concurrent types, the associated record value type carries the same
13682 -- discriminants, so when we constrain a concurrent type, we must constrain
13683 -- the corresponding record type as well.
13684
13685 procedure Constrain_Concurrent
13686 (Def_Id : in out Entity_Id;
13687 SI : Node_Id;
13688 Related_Nod : Node_Id;
13689 Related_Id : Entity_Id;
13690 Suffix : Character)
13691 is
13692 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13693 -- case of a private subtype (needed when only doing semantic analysis).
13694
13695 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13696 T_Val : Entity_Id;
13697
13698 begin
13699 if Is_Access_Type (T_Ent) then
13700 T_Ent := Designated_Type (T_Ent);
13701 end if;
13702
13703 T_Val := Corresponding_Record_Type (T_Ent);
13704
13705 if Present (T_Val) then
13706
13707 if No (Def_Id) then
13708 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13709
13710 -- Elaborate itype now, as it may be used in a subsequent
13711 -- synchronized operation in another scope.
13712
13713 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13714 Build_Itype_Reference (Def_Id, Related_Nod);
13715 end if;
13716 end if;
13717
13718 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13719 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13720
13721 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13722 Set_Corresponding_Record_Type (Def_Id,
13723 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13724
13725 else
13726 -- If there is no associated record, expansion is disabled and this
13727 -- is a generic context. Create a subtype in any case, so that
13728 -- semantic analysis can proceed.
13729
13730 if No (Def_Id) then
13731 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13732 end if;
13733
13734 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13735 end if;
13736 end Constrain_Concurrent;
13737
13738 ------------------------------------
13739 -- Constrain_Corresponding_Record --
13740 ------------------------------------
13741
13742 function Constrain_Corresponding_Record
13743 (Prot_Subt : Entity_Id;
13744 Corr_Rec : Entity_Id;
13745 Related_Nod : Node_Id) return Entity_Id
13746 is
13747 T_Sub : constant Entity_Id :=
13748 Create_Itype
13749 (Ekind => E_Record_Subtype,
13750 Related_Nod => Related_Nod,
13751 Related_Id => Corr_Rec,
13752 Suffix => 'C',
13753 Suffix_Index => -1);
13754
13755 begin
13756 Set_Etype (T_Sub, Corr_Rec);
13757 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13758 Set_Is_Constrained (T_Sub, True);
13759 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13760 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13761
13762 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13763 Set_Discriminant_Constraint
13764 (T_Sub, Discriminant_Constraint (Prot_Subt));
13765 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13766 Create_Constrained_Components
13767 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13768 end if;
13769
13770 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13771
13772 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13773 Conditional_Delay (T_Sub, Corr_Rec);
13774
13775 else
13776 -- This is a component subtype: it will be frozen in the context of
13777 -- the enclosing record's init_proc, so that discriminant references
13778 -- are resolved to discriminals. (Note: we used to skip freezing
13779 -- altogether in that case, which caused errors downstream for
13780 -- components of a bit packed array type).
13781
13782 Set_Has_Delayed_Freeze (T_Sub);
13783 end if;
13784
13785 return T_Sub;
13786 end Constrain_Corresponding_Record;
13787
13788 -----------------------
13789 -- Constrain_Decimal --
13790 -----------------------
13791
13792 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13793 T : constant Entity_Id := Entity (Subtype_Mark (S));
13794 C : constant Node_Id := Constraint (S);
13795 Loc : constant Source_Ptr := Sloc (C);
13796 Range_Expr : Node_Id;
13797 Digits_Expr : Node_Id;
13798 Digits_Val : Uint;
13799 Bound_Val : Ureal;
13800
13801 begin
13802 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13803
13804 if Nkind (C) = N_Range_Constraint then
13805 Range_Expr := Range_Expression (C);
13806 Digits_Val := Digits_Value (T);
13807
13808 else
13809 pragma Assert (Nkind (C) = N_Digits_Constraint);
13810
13811 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13812
13813 Digits_Expr := Digits_Expression (C);
13814 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13815
13816 Check_Digits_Expression (Digits_Expr);
13817 Digits_Val := Expr_Value (Digits_Expr);
13818
13819 if Digits_Val > Digits_Value (T) then
13820 Error_Msg_N
13821 ("digits expression is incompatible with subtype", C);
13822 Digits_Val := Digits_Value (T);
13823 end if;
13824
13825 if Present (Range_Constraint (C)) then
13826 Range_Expr := Range_Expression (Range_Constraint (C));
13827 else
13828 Range_Expr := Empty;
13829 end if;
13830 end if;
13831
13832 Set_Etype (Def_Id, Base_Type (T));
13833 Set_Size_Info (Def_Id, (T));
13834 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13835 Set_Delta_Value (Def_Id, Delta_Value (T));
13836 Set_Scale_Value (Def_Id, Scale_Value (T));
13837 Set_Small_Value (Def_Id, Small_Value (T));
13838 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13839 Set_Digits_Value (Def_Id, Digits_Val);
13840
13841 -- Manufacture range from given digits value if no range present
13842
13843 if No (Range_Expr) then
13844 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13845 Range_Expr :=
13846 Make_Range (Loc,
13847 Low_Bound =>
13848 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13849 High_Bound =>
13850 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13851 end if;
13852
13853 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13854 Set_Discrete_RM_Size (Def_Id);
13855
13856 -- Unconditionally delay the freeze, since we cannot set size
13857 -- information in all cases correctly until the freeze point.
13858
13859 Set_Has_Delayed_Freeze (Def_Id);
13860 end Constrain_Decimal;
13861
13862 ----------------------------------
13863 -- Constrain_Discriminated_Type --
13864 ----------------------------------
13865
13866 procedure Constrain_Discriminated_Type
13867 (Def_Id : Entity_Id;
13868 S : Node_Id;
13869 Related_Nod : Node_Id;
13870 For_Access : Boolean := False)
13871 is
13872 E : Entity_Id := Entity (Subtype_Mark (S));
13873 T : Entity_Id;
13874
13875 procedure Fixup_Bad_Constraint;
13876 -- Called after finding a bad constraint, and after having posted an
13877 -- appropriate error message. The goal is to leave type Def_Id in as
13878 -- reasonable state as possible.
13879
13880 --------------------------
13881 -- Fixup_Bad_Constraint --
13882 --------------------------
13883
13884 procedure Fixup_Bad_Constraint is
13885 begin
13886 -- Set a reasonable Ekind for the entity, including incomplete types.
13887
13888 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13889
13890 -- Set Etype to the known type, to reduce chances of cascaded errors
13891
13892 Set_Etype (Def_Id, E);
13893 Set_Error_Posted (Def_Id);
13894 end Fixup_Bad_Constraint;
13895
13896 -- Local variables
13897
13898 C : Node_Id;
13899 Constr : Elist_Id := New_Elmt_List;
13900
13901 -- Start of processing for Constrain_Discriminated_Type
13902
13903 begin
13904 C := Constraint (S);
13905
13906 -- A discriminant constraint is only allowed in a subtype indication,
13907 -- after a subtype mark. This subtype mark must denote either a type
13908 -- with discriminants, or an access type whose designated type is a
13909 -- type with discriminants. A discriminant constraint specifies the
13910 -- values of these discriminants (RM 3.7.2(5)).
13911
13912 T := Base_Type (Entity (Subtype_Mark (S)));
13913
13914 if Is_Access_Type (T) then
13915 T := Designated_Type (T);
13916 end if;
13917
13918 -- In an instance it may be necessary to retrieve the full view of a
13919 -- type with unknown discriminants, or a full view with defaulted
13920 -- discriminants. In other contexts the constraint is illegal.
13921
13922 if In_Instance
13923 and then Is_Private_Type (T)
13924 and then Present (Full_View (T))
13925 and then
13926 (Has_Unknown_Discriminants (T)
13927 or else
13928 (not Has_Discriminants (T)
13929 and then Has_Discriminants (Full_View (T))
13930 and then Present (Discriminant_Default_Value
13931 (First_Discriminant (Full_View (T))))))
13932 then
13933 T := Full_View (T);
13934 E := Full_View (E);
13935 end if;
13936
13937 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13938 -- generating an error for access-to-incomplete subtypes.
13939
13940 if Ada_Version >= Ada_2005
13941 and then Ekind (T) = E_Incomplete_Type
13942 and then Nkind (Parent (S)) = N_Subtype_Declaration
13943 and then not Is_Itype (Def_Id)
13944 then
13945 -- A little sanity check: emit an error message if the type has
13946 -- discriminants to begin with. Type T may be a regular incomplete
13947 -- type or imported via a limited with clause.
13948
13949 if Has_Discriminants (T)
13950 or else (From_Limited_With (T)
13951 and then Present (Non_Limited_View (T))
13952 and then Nkind (Parent (Non_Limited_View (T))) =
13953 N_Full_Type_Declaration
13954 and then Present (Discriminant_Specifications
13955 (Parent (Non_Limited_View (T)))))
13956 then
13957 Error_Msg_N
13958 ("(Ada 2005) incomplete subtype may not be constrained", C);
13959 else
13960 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13961 end if;
13962
13963 Fixup_Bad_Constraint;
13964 return;
13965
13966 -- Check that the type has visible discriminants. The type may be
13967 -- a private type with unknown discriminants whose full view has
13968 -- discriminants which are invisible.
13969
13970 elsif not Has_Discriminants (T)
13971 or else
13972 (Has_Unknown_Discriminants (T)
13973 and then Is_Private_Type (T))
13974 then
13975 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13976 Fixup_Bad_Constraint;
13977 return;
13978
13979 elsif Is_Constrained (E)
13980 or else (Ekind (E) = E_Class_Wide_Subtype
13981 and then Present (Discriminant_Constraint (E)))
13982 then
13983 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13984 Fixup_Bad_Constraint;
13985 return;
13986 end if;
13987
13988 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13989 -- applies to the base type.
13990
13991 T := Base_Type (T);
13992
13993 Constr := Build_Discriminant_Constraints (T, S);
13994
13995 -- If the list returned was empty we had an error in building the
13996 -- discriminant constraint. We have also already signalled an error
13997 -- in the incomplete type case
13998
13999 if Is_Empty_Elmt_List (Constr) then
14000 Fixup_Bad_Constraint;
14001 return;
14002 end if;
14003
14004 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14005 end Constrain_Discriminated_Type;
14006
14007 ---------------------------
14008 -- Constrain_Enumeration --
14009 ---------------------------
14010
14011 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
14012 T : constant Entity_Id := Entity (Subtype_Mark (S));
14013 C : constant Node_Id := Constraint (S);
14014
14015 begin
14016 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14017
14018 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14019
14020 Set_Etype (Def_Id, Base_Type (T));
14021 Set_Size_Info (Def_Id, (T));
14022 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14023 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14024
14025 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14026
14027 Set_Discrete_RM_Size (Def_Id);
14028 end Constrain_Enumeration;
14029
14030 ----------------------
14031 -- Constrain_Float --
14032 ----------------------
14033
14034 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14035 T : constant Entity_Id := Entity (Subtype_Mark (S));
14036 C : Node_Id;
14037 D : Node_Id;
14038 Rais : Node_Id;
14039
14040 begin
14041 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14042
14043 Set_Etype (Def_Id, Base_Type (T));
14044 Set_Size_Info (Def_Id, (T));
14045 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14046
14047 -- Process the constraint
14048
14049 C := Constraint (S);
14050
14051 -- Digits constraint present
14052
14053 if Nkind (C) = N_Digits_Constraint then
14054
14055 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
14056 Check_Restriction (No_Obsolescent_Features, C);
14057
14058 if Warn_On_Obsolescent_Feature then
14059 Error_Msg_N
14060 ("subtype digits constraint is an " &
14061 "obsolescent feature (RM J.3(8))?j?", C);
14062 end if;
14063
14064 D := Digits_Expression (C);
14065 Analyze_And_Resolve (D, Any_Integer);
14066 Check_Digits_Expression (D);
14067 Set_Digits_Value (Def_Id, Expr_Value (D));
14068
14069 -- Check that digits value is in range. Obviously we can do this
14070 -- at compile time, but it is strictly a runtime check, and of
14071 -- course there is an ACVC test that checks this.
14072
14073 if Digits_Value (Def_Id) > Digits_Value (T) then
14074 Error_Msg_Uint_1 := Digits_Value (T);
14075 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14076 Rais :=
14077 Make_Raise_Constraint_Error (Sloc (D),
14078 Reason => CE_Range_Check_Failed);
14079 Insert_Action (Declaration_Node (Def_Id), Rais);
14080 end if;
14081
14082 C := Range_Constraint (C);
14083
14084 -- No digits constraint present
14085
14086 else
14087 Set_Digits_Value (Def_Id, Digits_Value (T));
14088 end if;
14089
14090 -- Range constraint present
14091
14092 if Nkind (C) = N_Range_Constraint then
14093 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14094
14095 -- No range constraint present
14096
14097 else
14098 pragma Assert (No (C));
14099 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14100 end if;
14101
14102 Set_Is_Constrained (Def_Id);
14103 end Constrain_Float;
14104
14105 ---------------------
14106 -- Constrain_Index --
14107 ---------------------
14108
14109 procedure Constrain_Index
14110 (Index : Node_Id;
14111 S : Node_Id;
14112 Related_Nod : Node_Id;
14113 Related_Id : Entity_Id;
14114 Suffix : Character;
14115 Suffix_Index : Nat)
14116 is
14117 Def_Id : Entity_Id;
14118 R : Node_Id := Empty;
14119 T : constant Entity_Id := Etype (Index);
14120
14121 begin
14122 Def_Id :=
14123 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14124 Set_Etype (Def_Id, Base_Type (T));
14125
14126 if Nkind (S) = N_Range
14127 or else
14128 (Nkind (S) = N_Attribute_Reference
14129 and then Attribute_Name (S) = Name_Range)
14130 then
14131 -- A Range attribute will be transformed into N_Range by Resolve
14132
14133 Analyze (S);
14134 Set_Etype (S, T);
14135 R := S;
14136
14137 Process_Range_Expr_In_Decl (R, T);
14138
14139 if not Error_Posted (S)
14140 and then
14141 (Nkind (S) /= N_Range
14142 or else not Covers (T, (Etype (Low_Bound (S))))
14143 or else not Covers (T, (Etype (High_Bound (S)))))
14144 then
14145 if Base_Type (T) /= Any_Type
14146 and then Etype (Low_Bound (S)) /= Any_Type
14147 and then Etype (High_Bound (S)) /= Any_Type
14148 then
14149 Error_Msg_N ("range expected", S);
14150 end if;
14151 end if;
14152
14153 elsif Nkind (S) = N_Subtype_Indication then
14154
14155 -- The parser has verified that this is a discrete indication
14156
14157 Resolve_Discrete_Subtype_Indication (S, T);
14158 Bad_Predicated_Subtype_Use
14159 ("subtype& has predicate, not allowed in index constraint",
14160 S, Entity (Subtype_Mark (S)));
14161
14162 R := Range_Expression (Constraint (S));
14163
14164 -- Capture values of bounds and generate temporaries for them if
14165 -- needed, since checks may cause duplication of the expressions
14166 -- which must not be reevaluated.
14167
14168 -- The forced evaluation removes side effects from expressions, which
14169 -- should occur also in GNATprove mode. Otherwise, we end up with
14170 -- unexpected insertions of actions at places where this is not
14171 -- supposed to occur, e.g. on default parameters of a call.
14172
14173 if Expander_Active or GNATprove_Mode then
14174 Force_Evaluation
14175 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14176 Force_Evaluation
14177 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14178 end if;
14179
14180 elsif Nkind (S) = N_Discriminant_Association then
14181
14182 -- Syntactically valid in subtype indication
14183
14184 Error_Msg_N ("invalid index constraint", S);
14185 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14186 return;
14187
14188 -- Subtype_Mark case, no anonymous subtypes to construct
14189
14190 else
14191 Analyze (S);
14192
14193 if Is_Entity_Name (S) then
14194 if not Is_Type (Entity (S)) then
14195 Error_Msg_N ("expect subtype mark for index constraint", S);
14196
14197 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14198 Wrong_Type (S, Base_Type (T));
14199
14200 -- Check error of subtype with predicate in index constraint
14201
14202 else
14203 Bad_Predicated_Subtype_Use
14204 ("subtype& has predicate, not allowed in index constraint",
14205 S, Entity (S));
14206 end if;
14207
14208 return;
14209
14210 else
14211 Error_Msg_N ("invalid index constraint", S);
14212 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14213 return;
14214 end if;
14215 end if;
14216
14217 -- Complete construction of the Itype
14218
14219 if Is_Modular_Integer_Type (T) then
14220 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14221
14222 elsif Is_Integer_Type (T) then
14223 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14224
14225 else
14226 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14227 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14228 Set_First_Literal (Def_Id, First_Literal (T));
14229 end if;
14230
14231 Set_Size_Info (Def_Id, (T));
14232 Set_RM_Size (Def_Id, RM_Size (T));
14233 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14234
14235 Set_Scalar_Range (Def_Id, R);
14236
14237 Set_Etype (S, Def_Id);
14238 Set_Discrete_RM_Size (Def_Id);
14239 end Constrain_Index;
14240
14241 -----------------------
14242 -- Constrain_Integer --
14243 -----------------------
14244
14245 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14246 T : constant Entity_Id := Entity (Subtype_Mark (S));
14247 C : constant Node_Id := Constraint (S);
14248
14249 begin
14250 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14251
14252 if Is_Modular_Integer_Type (T) then
14253 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14254 else
14255 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14256 end if;
14257
14258 Set_Etype (Def_Id, Base_Type (T));
14259 Set_Size_Info (Def_Id, (T));
14260 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14261 Set_Discrete_RM_Size (Def_Id);
14262 end Constrain_Integer;
14263
14264 ------------------------------
14265 -- Constrain_Ordinary_Fixed --
14266 ------------------------------
14267
14268 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14269 T : constant Entity_Id := Entity (Subtype_Mark (S));
14270 C : Node_Id;
14271 D : Node_Id;
14272 Rais : Node_Id;
14273
14274 begin
14275 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14276 Set_Etype (Def_Id, Base_Type (T));
14277 Set_Size_Info (Def_Id, (T));
14278 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14279 Set_Small_Value (Def_Id, Small_Value (T));
14280
14281 -- Process the constraint
14282
14283 C := Constraint (S);
14284
14285 -- Delta constraint present
14286
14287 if Nkind (C) = N_Delta_Constraint then
14288
14289 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14290 Check_Restriction (No_Obsolescent_Features, C);
14291
14292 if Warn_On_Obsolescent_Feature then
14293 Error_Msg_S
14294 ("subtype delta constraint is an " &
14295 "obsolescent feature (RM J.3(7))?j?");
14296 end if;
14297
14298 D := Delta_Expression (C);
14299 Analyze_And_Resolve (D, Any_Real);
14300 Check_Delta_Expression (D);
14301 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14302
14303 -- Check that delta value is in range. Obviously we can do this
14304 -- at compile time, but it is strictly a runtime check, and of
14305 -- course there is an ACVC test that checks this.
14306
14307 if Delta_Value (Def_Id) < Delta_Value (T) then
14308 Error_Msg_N ("??delta value is too small", D);
14309 Rais :=
14310 Make_Raise_Constraint_Error (Sloc (D),
14311 Reason => CE_Range_Check_Failed);
14312 Insert_Action (Declaration_Node (Def_Id), Rais);
14313 end if;
14314
14315 C := Range_Constraint (C);
14316
14317 -- No delta constraint present
14318
14319 else
14320 Set_Delta_Value (Def_Id, Delta_Value (T));
14321 end if;
14322
14323 -- Range constraint present
14324
14325 if Nkind (C) = N_Range_Constraint then
14326 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14327
14328 -- No range constraint present
14329
14330 else
14331 pragma Assert (No (C));
14332 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14333 end if;
14334
14335 Set_Discrete_RM_Size (Def_Id);
14336
14337 -- Unconditionally delay the freeze, since we cannot set size
14338 -- information in all cases correctly until the freeze point.
14339
14340 Set_Has_Delayed_Freeze (Def_Id);
14341 end Constrain_Ordinary_Fixed;
14342
14343 -----------------------
14344 -- Contain_Interface --
14345 -----------------------
14346
14347 function Contain_Interface
14348 (Iface : Entity_Id;
14349 Ifaces : Elist_Id) return Boolean
14350 is
14351 Iface_Elmt : Elmt_Id;
14352
14353 begin
14354 if Present (Ifaces) then
14355 Iface_Elmt := First_Elmt (Ifaces);
14356 while Present (Iface_Elmt) loop
14357 if Node (Iface_Elmt) = Iface then
14358 return True;
14359 end if;
14360
14361 Next_Elmt (Iface_Elmt);
14362 end loop;
14363 end if;
14364
14365 return False;
14366 end Contain_Interface;
14367
14368 ---------------------------
14369 -- Convert_Scalar_Bounds --
14370 ---------------------------
14371
14372 procedure Convert_Scalar_Bounds
14373 (N : Node_Id;
14374 Parent_Type : Entity_Id;
14375 Derived_Type : Entity_Id;
14376 Loc : Source_Ptr)
14377 is
14378 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14379
14380 Lo : Node_Id;
14381 Hi : Node_Id;
14382 Rng : Node_Id;
14383
14384 begin
14385 -- Defend against previous errors
14386
14387 if No (Scalar_Range (Derived_Type)) then
14388 Check_Error_Detected;
14389 return;
14390 end if;
14391
14392 Lo := Build_Scalar_Bound
14393 (Type_Low_Bound (Derived_Type),
14394 Parent_Type, Implicit_Base);
14395
14396 Hi := Build_Scalar_Bound
14397 (Type_High_Bound (Derived_Type),
14398 Parent_Type, Implicit_Base);
14399
14400 Rng :=
14401 Make_Range (Loc,
14402 Low_Bound => Lo,
14403 High_Bound => Hi);
14404
14405 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14406
14407 Set_Parent (Rng, N);
14408 Set_Scalar_Range (Derived_Type, Rng);
14409
14410 -- Analyze the bounds
14411
14412 Analyze_And_Resolve (Lo, Implicit_Base);
14413 Analyze_And_Resolve (Hi, Implicit_Base);
14414
14415 -- Analyze the range itself, except that we do not analyze it if
14416 -- the bounds are real literals, and we have a fixed-point type.
14417 -- The reason for this is that we delay setting the bounds in this
14418 -- case till we know the final Small and Size values (see circuit
14419 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14420
14421 if Is_Fixed_Point_Type (Parent_Type)
14422 and then Nkind (Lo) = N_Real_Literal
14423 and then Nkind (Hi) = N_Real_Literal
14424 then
14425 return;
14426
14427 -- Here we do the analysis of the range
14428
14429 -- Note: we do this manually, since if we do a normal Analyze and
14430 -- Resolve call, there are problems with the conversions used for
14431 -- the derived type range.
14432
14433 else
14434 Set_Etype (Rng, Implicit_Base);
14435 Set_Analyzed (Rng, True);
14436 end if;
14437 end Convert_Scalar_Bounds;
14438
14439 -------------------
14440 -- Copy_And_Swap --
14441 -------------------
14442
14443 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14444 begin
14445 -- Initialize new full declaration entity by copying the pertinent
14446 -- fields of the corresponding private declaration entity.
14447
14448 -- We temporarily set Ekind to a value appropriate for a type to
14449 -- avoid assert failures in Einfo from checking for setting type
14450 -- attributes on something that is not a type. Ekind (Priv) is an
14451 -- appropriate choice, since it allowed the attributes to be set
14452 -- in the first place. This Ekind value will be modified later.
14453
14454 Set_Ekind (Full, Ekind (Priv));
14455
14456 -- Also set Etype temporarily to Any_Type, again, in the absence
14457 -- of errors, it will be properly reset, and if there are errors,
14458 -- then we want a value of Any_Type to remain.
14459
14460 Set_Etype (Full, Any_Type);
14461
14462 -- Now start copying attributes
14463
14464 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14465
14466 if Has_Discriminants (Full) then
14467 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14468 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14469 end if;
14470
14471 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14472 Set_Homonym (Full, Homonym (Priv));
14473 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14474 Set_Is_Public (Full, Is_Public (Priv));
14475 Set_Is_Pure (Full, Is_Pure (Priv));
14476 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14477 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14478 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14479 Set_Has_Pragma_Unreferenced_Objects
14480 (Full, Has_Pragma_Unreferenced_Objects
14481 (Priv));
14482
14483 Conditional_Delay (Full, Priv);
14484
14485 if Is_Tagged_Type (Full) then
14486 Set_Direct_Primitive_Operations
14487 (Full, Direct_Primitive_Operations (Priv));
14488 Set_No_Tagged_Streams_Pragma
14489 (Full, No_Tagged_Streams_Pragma (Priv));
14490
14491 if Is_Base_Type (Priv) then
14492 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14493 end if;
14494 end if;
14495
14496 Set_Is_Volatile (Full, Is_Volatile (Priv));
14497 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14498 Set_Scope (Full, Scope (Priv));
14499 Set_Prev_Entity (Full, Prev_Entity (Priv));
14500 Set_Next_Entity (Full, Next_Entity (Priv));
14501 Set_First_Entity (Full, First_Entity (Priv));
14502 Set_Last_Entity (Full, Last_Entity (Priv));
14503
14504 -- If access types have been recorded for later handling, keep them in
14505 -- the full view so that they get handled when the full view freeze
14506 -- node is expanded.
14507
14508 if Present (Freeze_Node (Priv))
14509 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14510 then
14511 Ensure_Freeze_Node (Full);
14512 Set_Access_Types_To_Process
14513 (Freeze_Node (Full),
14514 Access_Types_To_Process (Freeze_Node (Priv)));
14515 end if;
14516
14517 -- Swap the two entities. Now Private is the full type entity and Full
14518 -- is the private one. They will be swapped back at the end of the
14519 -- private part. This swapping ensures that the entity that is visible
14520 -- in the private part is the full declaration.
14521
14522 Exchange_Entities (Priv, Full);
14523 Append_Entity (Full, Scope (Full));
14524 end Copy_And_Swap;
14525
14526 -------------------------------------
14527 -- Copy_Array_Base_Type_Attributes --
14528 -------------------------------------
14529
14530 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14531 begin
14532 Set_Component_Alignment (T1, Component_Alignment (T2));
14533 Set_Component_Type (T1, Component_Type (T2));
14534 Set_Component_Size (T1, Component_Size (T2));
14535 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14536 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14537 Propagate_Concurrent_Flags (T1, T2);
14538 Set_Is_Packed (T1, Is_Packed (T2));
14539 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14540 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14541 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14542 end Copy_Array_Base_Type_Attributes;
14543
14544 -----------------------------------
14545 -- Copy_Array_Subtype_Attributes --
14546 -----------------------------------
14547
14548 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14549 begin
14550 Set_Size_Info (T1, T2);
14551
14552 Set_First_Index (T1, First_Index (T2));
14553 Set_Is_Aliased (T1, Is_Aliased (T2));
14554 Set_Is_Volatile (T1, Is_Volatile (T2));
14555 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14556 Set_Is_Constrained (T1, Is_Constrained (T2));
14557 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14558 Inherit_Rep_Item_Chain (T1, T2);
14559 Set_Convention (T1, Convention (T2));
14560 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14561 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14562 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14563 end Copy_Array_Subtype_Attributes;
14564
14565 -----------------------------------
14566 -- Create_Constrained_Components --
14567 -----------------------------------
14568
14569 procedure Create_Constrained_Components
14570 (Subt : Entity_Id;
14571 Decl_Node : Node_Id;
14572 Typ : Entity_Id;
14573 Constraints : Elist_Id)
14574 is
14575 Loc : constant Source_Ptr := Sloc (Subt);
14576 Comp_List : constant Elist_Id := New_Elmt_List;
14577 Parent_Type : constant Entity_Id := Etype (Typ);
14578 Assoc_List : constant List_Id := New_List;
14579 Discr_Val : Elmt_Id;
14580 Errors : Boolean;
14581 New_C : Entity_Id;
14582 Old_C : Entity_Id;
14583 Is_Static : Boolean := True;
14584
14585 procedure Collect_Fixed_Components (Typ : Entity_Id);
14586 -- Collect parent type components that do not appear in a variant part
14587
14588 procedure Create_All_Components;
14589 -- Iterate over Comp_List to create the components of the subtype
14590
14591 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14592 -- Creates a new component from Old_Compon, copying all the fields from
14593 -- it, including its Etype, inserts the new component in the Subt entity
14594 -- chain and returns the new component.
14595
14596 function Is_Variant_Record (T : Entity_Id) return Boolean;
14597 -- If true, and discriminants are static, collect only components from
14598 -- variants selected by discriminant values.
14599
14600 ------------------------------
14601 -- Collect_Fixed_Components --
14602 ------------------------------
14603
14604 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14605 begin
14606 -- Build association list for discriminants, and find components of the
14607 -- variant part selected by the values of the discriminants.
14608
14609 Old_C := First_Discriminant (Typ);
14610 Discr_Val := First_Elmt (Constraints);
14611 while Present (Old_C) loop
14612 Append_To (Assoc_List,
14613 Make_Component_Association (Loc,
14614 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14615 Expression => New_Copy (Node (Discr_Val))));
14616
14617 Next_Elmt (Discr_Val);
14618 Next_Discriminant (Old_C);
14619 end loop;
14620
14621 -- The tag and the possible parent component are unconditionally in
14622 -- the subtype.
14623
14624 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14625 Old_C := First_Component (Typ);
14626 while Present (Old_C) loop
14627 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14628 Append_Elmt (Old_C, Comp_List);
14629 end if;
14630
14631 Next_Component (Old_C);
14632 end loop;
14633 end if;
14634 end Collect_Fixed_Components;
14635
14636 ---------------------------
14637 -- Create_All_Components --
14638 ---------------------------
14639
14640 procedure Create_All_Components is
14641 Comp : Elmt_Id;
14642
14643 begin
14644 Comp := First_Elmt (Comp_List);
14645 while Present (Comp) loop
14646 Old_C := Node (Comp);
14647 New_C := Create_Component (Old_C);
14648
14649 Set_Etype
14650 (New_C,
14651 Constrain_Component_Type
14652 (Old_C, Subt, Decl_Node, Typ, Constraints));
14653 Set_Is_Public (New_C, Is_Public (Subt));
14654
14655 Next_Elmt (Comp);
14656 end loop;
14657 end Create_All_Components;
14658
14659 ----------------------
14660 -- Create_Component --
14661 ----------------------
14662
14663 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14664 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14665
14666 begin
14667 if Ekind (Old_Compon) = E_Discriminant
14668 and then Is_Completely_Hidden (Old_Compon)
14669 then
14670 -- This is a shadow discriminant created for a discriminant of
14671 -- the parent type, which needs to be present in the subtype.
14672 -- Give the shadow discriminant an internal name that cannot
14673 -- conflict with that of visible components.
14674
14675 Set_Chars (New_Compon, New_Internal_Name ('C'));
14676 end if;
14677
14678 -- Set the parent so we have a proper link for freezing etc. This is
14679 -- not a real parent pointer, since of course our parent does not own
14680 -- up to us and reference us, we are an illegitimate child of the
14681 -- original parent.
14682
14683 Set_Parent (New_Compon, Parent (Old_Compon));
14684
14685 -- We do not want this node marked as Comes_From_Source, since
14686 -- otherwise it would get first class status and a separate cross-
14687 -- reference line would be generated. Illegitimate children do not
14688 -- rate such recognition.
14689
14690 Set_Comes_From_Source (New_Compon, False);
14691
14692 -- But it is a real entity, and a birth certificate must be properly
14693 -- registered by entering it into the entity list, and setting its
14694 -- scope to the given subtype. This turns out to be useful for the
14695 -- LLVM code generator, but that scope is not used otherwise.
14696
14697 Enter_Name (New_Compon);
14698 Set_Scope (New_Compon, Subt);
14699
14700 return New_Compon;
14701 end Create_Component;
14702
14703 -----------------------
14704 -- Is_Variant_Record --
14705 -----------------------
14706
14707 function Is_Variant_Record (T : Entity_Id) return Boolean is
14708 begin
14709 return Nkind (Parent (T)) = N_Full_Type_Declaration
14710 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14711 and then Present (Component_List (Type_Definition (Parent (T))))
14712 and then
14713 Present
14714 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14715 end Is_Variant_Record;
14716
14717 -- Start of processing for Create_Constrained_Components
14718
14719 begin
14720 pragma Assert (Subt /= Base_Type (Subt));
14721 pragma Assert (Typ = Base_Type (Typ));
14722
14723 Set_First_Entity (Subt, Empty);
14724 Set_Last_Entity (Subt, Empty);
14725
14726 -- Check whether constraint is fully static, in which case we can
14727 -- optimize the list of components.
14728
14729 Discr_Val := First_Elmt (Constraints);
14730 while Present (Discr_Val) loop
14731 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14732 Is_Static := False;
14733 exit;
14734 end if;
14735
14736 Next_Elmt (Discr_Val);
14737 end loop;
14738
14739 Set_Has_Static_Discriminants (Subt, Is_Static);
14740
14741 Push_Scope (Subt);
14742
14743 -- Inherit the discriminants of the parent type
14744
14745 Add_Discriminants : declare
14746 Num_Disc : Nat;
14747 Num_Gird : Nat;
14748
14749 begin
14750 Num_Disc := 0;
14751 Old_C := First_Discriminant (Typ);
14752
14753 while Present (Old_C) loop
14754 Num_Disc := Num_Disc + 1;
14755 New_C := Create_Component (Old_C);
14756 Set_Is_Public (New_C, Is_Public (Subt));
14757 Next_Discriminant (Old_C);
14758 end loop;
14759
14760 -- For an untagged derived subtype, the number of discriminants may
14761 -- be smaller than the number of inherited discriminants, because
14762 -- several of them may be renamed by a single new discriminant or
14763 -- constrained. In this case, add the hidden discriminants back into
14764 -- the subtype, because they need to be present if the optimizer of
14765 -- the GCC 4.x back-end decides to break apart assignments between
14766 -- objects using the parent view into member-wise assignments.
14767
14768 Num_Gird := 0;
14769
14770 if Is_Derived_Type (Typ)
14771 and then not Is_Tagged_Type (Typ)
14772 then
14773 Old_C := First_Stored_Discriminant (Typ);
14774
14775 while Present (Old_C) loop
14776 Num_Gird := Num_Gird + 1;
14777 Next_Stored_Discriminant (Old_C);
14778 end loop;
14779 end if;
14780
14781 if Num_Gird > Num_Disc then
14782
14783 -- Find out multiple uses of new discriminants, and add hidden
14784 -- components for the extra renamed discriminants. We recognize
14785 -- multiple uses through the Corresponding_Discriminant of a
14786 -- new discriminant: if it constrains several old discriminants,
14787 -- this field points to the last one in the parent type. The
14788 -- stored discriminants of the derived type have the same name
14789 -- as those of the parent.
14790
14791 declare
14792 Constr : Elmt_Id;
14793 New_Discr : Entity_Id;
14794 Old_Discr : Entity_Id;
14795
14796 begin
14797 Constr := First_Elmt (Stored_Constraint (Typ));
14798 Old_Discr := First_Stored_Discriminant (Typ);
14799 while Present (Constr) loop
14800 if Is_Entity_Name (Node (Constr))
14801 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14802 then
14803 New_Discr := Entity (Node (Constr));
14804
14805 if Chars (Corresponding_Discriminant (New_Discr)) /=
14806 Chars (Old_Discr)
14807 then
14808 -- The new discriminant has been used to rename a
14809 -- subsequent old discriminant. Introduce a shadow
14810 -- component for the current old discriminant.
14811
14812 New_C := Create_Component (Old_Discr);
14813 Set_Original_Record_Component (New_C, Old_Discr);
14814 end if;
14815
14816 else
14817 -- The constraint has eliminated the old discriminant.
14818 -- Introduce a shadow component.
14819
14820 New_C := Create_Component (Old_Discr);
14821 Set_Original_Record_Component (New_C, Old_Discr);
14822 end if;
14823
14824 Next_Elmt (Constr);
14825 Next_Stored_Discriminant (Old_Discr);
14826 end loop;
14827 end;
14828 end if;
14829 end Add_Discriminants;
14830
14831 if Is_Static
14832 and then Is_Variant_Record (Typ)
14833 then
14834 Collect_Fixed_Components (Typ);
14835
14836 Gather_Components (
14837 Typ,
14838 Component_List (Type_Definition (Parent (Typ))),
14839 Governed_By => Assoc_List,
14840 Into => Comp_List,
14841 Report_Errors => Errors);
14842 pragma Assert (not Errors
14843 or else Serious_Errors_Detected > 0);
14844
14845 Create_All_Components;
14846
14847 -- If the subtype declaration is created for a tagged type derivation
14848 -- with constraints, we retrieve the record definition of the parent
14849 -- type to select the components of the proper variant.
14850
14851 elsif Is_Static
14852 and then Is_Tagged_Type (Typ)
14853 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14854 and then
14855 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14856 and then Is_Variant_Record (Parent_Type)
14857 then
14858 Collect_Fixed_Components (Typ);
14859
14860 Gather_Components
14861 (Typ,
14862 Component_List (Type_Definition (Parent (Parent_Type))),
14863 Governed_By => Assoc_List,
14864 Into => Comp_List,
14865 Report_Errors => Errors);
14866
14867 -- Note: previously there was a check at this point that no errors
14868 -- were detected. As a consequence of AI05-220 there may be an error
14869 -- if an inherited discriminant that controls a variant has a non-
14870 -- static constraint.
14871
14872 -- If the tagged derivation has a type extension, collect all the
14873 -- new components therein.
14874
14875 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14876 then
14877 Old_C := First_Component (Typ);
14878 while Present (Old_C) loop
14879 if Original_Record_Component (Old_C) = Old_C
14880 and then Chars (Old_C) /= Name_uTag
14881 and then Chars (Old_C) /= Name_uParent
14882 then
14883 Append_Elmt (Old_C, Comp_List);
14884 end if;
14885
14886 Next_Component (Old_C);
14887 end loop;
14888 end if;
14889
14890 Create_All_Components;
14891
14892 else
14893 -- If discriminants are not static, or if this is a multi-level type
14894 -- extension, we have to include all components of the parent type.
14895
14896 Old_C := First_Component (Typ);
14897 while Present (Old_C) loop
14898 New_C := Create_Component (Old_C);
14899
14900 Set_Etype
14901 (New_C,
14902 Constrain_Component_Type
14903 (Old_C, Subt, Decl_Node, Typ, Constraints));
14904 Set_Is_Public (New_C, Is_Public (Subt));
14905
14906 Next_Component (Old_C);
14907 end loop;
14908 end if;
14909
14910 End_Scope;
14911 end Create_Constrained_Components;
14912
14913 ------------------------------------------
14914 -- Decimal_Fixed_Point_Type_Declaration --
14915 ------------------------------------------
14916
14917 procedure Decimal_Fixed_Point_Type_Declaration
14918 (T : Entity_Id;
14919 Def : Node_Id)
14920 is
14921 Loc : constant Source_Ptr := Sloc (Def);
14922 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14923 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14924 Implicit_Base : Entity_Id;
14925 Digs_Val : Uint;
14926 Delta_Val : Ureal;
14927 Scale_Val : Uint;
14928 Bound_Val : Ureal;
14929
14930 begin
14931 Check_SPARK_05_Restriction
14932 ("decimal fixed point type is not allowed", Def);
14933 Check_Restriction (No_Fixed_Point, Def);
14934
14935 -- Create implicit base type
14936
14937 Implicit_Base :=
14938 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14939 Set_Etype (Implicit_Base, Implicit_Base);
14940
14941 -- Analyze and process delta expression
14942
14943 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14944
14945 Check_Delta_Expression (Delta_Expr);
14946 Delta_Val := Expr_Value_R (Delta_Expr);
14947
14948 -- Check delta is power of 10, and determine scale value from it
14949
14950 declare
14951 Val : Ureal;
14952
14953 begin
14954 Scale_Val := Uint_0;
14955 Val := Delta_Val;
14956
14957 if Val < Ureal_1 then
14958 while Val < Ureal_1 loop
14959 Val := Val * Ureal_10;
14960 Scale_Val := Scale_Val + 1;
14961 end loop;
14962
14963 if Scale_Val > 18 then
14964 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14965 Scale_Val := UI_From_Int (+18);
14966 end if;
14967
14968 else
14969 while Val > Ureal_1 loop
14970 Val := Val / Ureal_10;
14971 Scale_Val := Scale_Val - 1;
14972 end loop;
14973
14974 if Scale_Val < -18 then
14975 Error_Msg_N ("scale is less than minimum value of -18", Def);
14976 Scale_Val := UI_From_Int (-18);
14977 end if;
14978 end if;
14979
14980 if Val /= Ureal_1 then
14981 Error_Msg_N ("delta expression must be a power of 10", Def);
14982 Delta_Val := Ureal_10 ** (-Scale_Val);
14983 end if;
14984 end;
14985
14986 -- Set delta, scale and small (small = delta for decimal type)
14987
14988 Set_Delta_Value (Implicit_Base, Delta_Val);
14989 Set_Scale_Value (Implicit_Base, Scale_Val);
14990 Set_Small_Value (Implicit_Base, Delta_Val);
14991
14992 -- Analyze and process digits expression
14993
14994 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14995 Check_Digits_Expression (Digs_Expr);
14996 Digs_Val := Expr_Value (Digs_Expr);
14997
14998 if Digs_Val > 18 then
14999 Digs_Val := UI_From_Int (+18);
15000 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
15001 end if;
15002
15003 Set_Digits_Value (Implicit_Base, Digs_Val);
15004 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15005
15006 -- Set range of base type from digits value for now. This will be
15007 -- expanded to represent the true underlying base range by Freeze.
15008
15009 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15010
15011 -- Note: We leave size as zero for now, size will be set at freeze
15012 -- time. We have to do this for ordinary fixed-point, because the size
15013 -- depends on the specified small, and we might as well do the same for
15014 -- decimal fixed-point.
15015
15016 pragma Assert (Esize (Implicit_Base) = Uint_0);
15017
15018 -- If there are bounds given in the declaration use them as the
15019 -- bounds of the first named subtype.
15020
15021 if Present (Real_Range_Specification (Def)) then
15022 declare
15023 RRS : constant Node_Id := Real_Range_Specification (Def);
15024 Low : constant Node_Id := Low_Bound (RRS);
15025 High : constant Node_Id := High_Bound (RRS);
15026 Low_Val : Ureal;
15027 High_Val : Ureal;
15028
15029 begin
15030 Analyze_And_Resolve (Low, Any_Real);
15031 Analyze_And_Resolve (High, Any_Real);
15032 Check_Real_Bound (Low);
15033 Check_Real_Bound (High);
15034 Low_Val := Expr_Value_R (Low);
15035 High_Val := Expr_Value_R (High);
15036
15037 if Low_Val < (-Bound_Val) then
15038 Error_Msg_N
15039 ("range low bound too small for digits value", Low);
15040 Low_Val := -Bound_Val;
15041 end if;
15042
15043 if High_Val > Bound_Val then
15044 Error_Msg_N
15045 ("range high bound too large for digits value", High);
15046 High_Val := Bound_Val;
15047 end if;
15048
15049 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15050 end;
15051
15052 -- If no explicit range, use range that corresponds to given
15053 -- digits value. This will end up as the final range for the
15054 -- first subtype.
15055
15056 else
15057 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15058 end if;
15059
15060 -- Complete entity for first subtype. The inheritance of the rep item
15061 -- chain ensures that SPARK-related pragmas are not clobbered when the
15062 -- decimal fixed point type acts as a full view of a private type.
15063
15064 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15065 Set_Etype (T, Implicit_Base);
15066 Set_Size_Info (T, Implicit_Base);
15067 Inherit_Rep_Item_Chain (T, Implicit_Base);
15068 Set_Digits_Value (T, Digs_Val);
15069 Set_Delta_Value (T, Delta_Val);
15070 Set_Small_Value (T, Delta_Val);
15071 Set_Scale_Value (T, Scale_Val);
15072 Set_Is_Constrained (T);
15073 end Decimal_Fixed_Point_Type_Declaration;
15074
15075 -----------------------------------
15076 -- Derive_Progenitor_Subprograms --
15077 -----------------------------------
15078
15079 procedure Derive_Progenitor_Subprograms
15080 (Parent_Type : Entity_Id;
15081 Tagged_Type : Entity_Id)
15082 is
15083 E : Entity_Id;
15084 Elmt : Elmt_Id;
15085 Iface : Entity_Id;
15086 Iface_Alias : Entity_Id;
15087 Iface_Elmt : Elmt_Id;
15088 Iface_Subp : Entity_Id;
15089 New_Subp : Entity_Id := Empty;
15090 Prim_Elmt : Elmt_Id;
15091 Subp : Entity_Id;
15092 Typ : Entity_Id;
15093
15094 begin
15095 pragma Assert (Ada_Version >= Ada_2005
15096 and then Is_Record_Type (Tagged_Type)
15097 and then Is_Tagged_Type (Tagged_Type)
15098 and then Has_Interfaces (Tagged_Type));
15099
15100 -- Step 1: Transfer to the full-view primitives associated with the
15101 -- partial-view that cover interface primitives. Conceptually this
15102 -- work should be done later by Process_Full_View; done here to
15103 -- simplify its implementation at later stages. It can be safely
15104 -- done here because interfaces must be visible in the partial and
15105 -- private view (RM 7.3(7.3/2)).
15106
15107 -- Small optimization: This work is only required if the parent may
15108 -- have entities whose Alias attribute reference an interface primitive.
15109 -- Such a situation may occur if the parent is an abstract type and the
15110 -- primitive has not been yet overridden or if the parent is a generic
15111 -- formal type covering interfaces.
15112
15113 -- If the tagged type is not abstract, it cannot have abstract
15114 -- primitives (the only entities in the list of primitives of
15115 -- non-abstract tagged types that can reference abstract primitives
15116 -- through its Alias attribute are the internal entities that have
15117 -- attribute Interface_Alias, and these entities are generated later
15118 -- by Add_Internal_Interface_Entities).
15119
15120 if In_Private_Part (Current_Scope)
15121 and then (Is_Abstract_Type (Parent_Type)
15122 or else
15123 Is_Generic_Type (Parent_Type))
15124 then
15125 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15126 while Present (Elmt) loop
15127 Subp := Node (Elmt);
15128
15129 -- At this stage it is not possible to have entities in the list
15130 -- of primitives that have attribute Interface_Alias.
15131
15132 pragma Assert (No (Interface_Alias (Subp)));
15133
15134 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15135
15136 if Is_Interface (Typ) then
15137 E := Find_Primitive_Covering_Interface
15138 (Tagged_Type => Tagged_Type,
15139 Iface_Prim => Subp);
15140
15141 if Present (E)
15142 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15143 then
15144 Replace_Elmt (Elmt, E);
15145 Remove_Homonym (Subp);
15146 end if;
15147 end if;
15148
15149 Next_Elmt (Elmt);
15150 end loop;
15151 end if;
15152
15153 -- Step 2: Add primitives of progenitors that are not implemented by
15154 -- parents of Tagged_Type.
15155
15156 if Present (Interfaces (Base_Type (Tagged_Type))) then
15157 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15158 while Present (Iface_Elmt) loop
15159 Iface := Node (Iface_Elmt);
15160
15161 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15162 while Present (Prim_Elmt) loop
15163 Iface_Subp := Node (Prim_Elmt);
15164 Iface_Alias := Ultimate_Alias (Iface_Subp);
15165
15166 -- Exclude derivation of predefined primitives except those
15167 -- that come from source, or are inherited from one that comes
15168 -- from source. Required to catch declarations of equality
15169 -- operators of interfaces. For example:
15170
15171 -- type Iface is interface;
15172 -- function "=" (Left, Right : Iface) return Boolean;
15173
15174 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15175 or else Comes_From_Source (Iface_Alias)
15176 then
15177 E :=
15178 Find_Primitive_Covering_Interface
15179 (Tagged_Type => Tagged_Type,
15180 Iface_Prim => Iface_Subp);
15181
15182 -- If not found we derive a new primitive leaving its alias
15183 -- attribute referencing the interface primitive.
15184
15185 if No (E) then
15186 Derive_Subprogram
15187 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15188
15189 -- Ada 2012 (AI05-0197): If the covering primitive's name
15190 -- differs from the name of the interface primitive then it
15191 -- is a private primitive inherited from a parent type. In
15192 -- such case, given that Tagged_Type covers the interface,
15193 -- the inherited private primitive becomes visible. For such
15194 -- purpose we add a new entity that renames the inherited
15195 -- private primitive.
15196
15197 elsif Chars (E) /= Chars (Iface_Subp) then
15198 pragma Assert (Has_Suffix (E, 'P'));
15199 Derive_Subprogram
15200 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15201 Set_Alias (New_Subp, E);
15202 Set_Is_Abstract_Subprogram (New_Subp,
15203 Is_Abstract_Subprogram (E));
15204
15205 -- Propagate to the full view interface entities associated
15206 -- with the partial view.
15207
15208 elsif In_Private_Part (Current_Scope)
15209 and then Present (Alias (E))
15210 and then Alias (E) = Iface_Subp
15211 and then
15212 List_Containing (Parent (E)) /=
15213 Private_Declarations
15214 (Specification
15215 (Unit_Declaration_Node (Current_Scope)))
15216 then
15217 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15218 end if;
15219 end if;
15220
15221 Next_Elmt (Prim_Elmt);
15222 end loop;
15223
15224 Next_Elmt (Iface_Elmt);
15225 end loop;
15226 end if;
15227 end Derive_Progenitor_Subprograms;
15228
15229 -----------------------
15230 -- Derive_Subprogram --
15231 -----------------------
15232
15233 procedure Derive_Subprogram
15234 (New_Subp : out Entity_Id;
15235 Parent_Subp : Entity_Id;
15236 Derived_Type : Entity_Id;
15237 Parent_Type : Entity_Id;
15238 Actual_Subp : Entity_Id := Empty)
15239 is
15240 Formal : Entity_Id;
15241 -- Formal parameter of parent primitive operation
15242
15243 Formal_Of_Actual : Entity_Id;
15244 -- Formal parameter of actual operation, when the derivation is to
15245 -- create a renaming for a primitive operation of an actual in an
15246 -- instantiation.
15247
15248 New_Formal : Entity_Id;
15249 -- Formal of inherited operation
15250
15251 Visible_Subp : Entity_Id := Parent_Subp;
15252
15253 function Is_Private_Overriding return Boolean;
15254 -- If Subp is a private overriding of a visible operation, the inherited
15255 -- operation derives from the overridden op (even though its body is the
15256 -- overriding one) and the inherited operation is visible now. See
15257 -- sem_disp to see the full details of the handling of the overridden
15258 -- subprogram, which is removed from the list of primitive operations of
15259 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15260 -- and used to diagnose abstract operations that need overriding in the
15261 -- derived type.
15262
15263 procedure Replace_Type (Id, New_Id : Entity_Id);
15264 -- When the type is an anonymous access type, create a new access type
15265 -- designating the derived type.
15266
15267 procedure Set_Derived_Name;
15268 -- This procedure sets the appropriate Chars name for New_Subp. This
15269 -- is normally just a copy of the parent name. An exception arises for
15270 -- type support subprograms, where the name is changed to reflect the
15271 -- name of the derived type, e.g. if type foo is derived from type bar,
15272 -- then a procedure barDA is derived with a name fooDA.
15273
15274 ---------------------------
15275 -- Is_Private_Overriding --
15276 ---------------------------
15277
15278 function Is_Private_Overriding return Boolean is
15279 Prev : Entity_Id;
15280
15281 begin
15282 -- If the parent is not a dispatching operation there is no
15283 -- need to investigate overridings
15284
15285 if not Is_Dispatching_Operation (Parent_Subp) then
15286 return False;
15287 end if;
15288
15289 -- The visible operation that is overridden is a homonym of the
15290 -- parent subprogram. We scan the homonym chain to find the one
15291 -- whose alias is the subprogram we are deriving.
15292
15293 Prev := Current_Entity (Parent_Subp);
15294 while Present (Prev) loop
15295 if Ekind (Prev) = Ekind (Parent_Subp)
15296 and then Alias (Prev) = Parent_Subp
15297 and then Scope (Parent_Subp) = Scope (Prev)
15298 and then not Is_Hidden (Prev)
15299 then
15300 Visible_Subp := Prev;
15301 return True;
15302 end if;
15303
15304 Prev := Homonym (Prev);
15305 end loop;
15306
15307 return False;
15308 end Is_Private_Overriding;
15309
15310 ------------------
15311 -- Replace_Type --
15312 ------------------
15313
15314 procedure Replace_Type (Id, New_Id : Entity_Id) is
15315 Id_Type : constant Entity_Id := Etype (Id);
15316 Acc_Type : Entity_Id;
15317 Par : constant Node_Id := Parent (Derived_Type);
15318
15319 begin
15320 -- When the type is an anonymous access type, create a new access
15321 -- type designating the derived type. This itype must be elaborated
15322 -- at the point of the derivation, not on subsequent calls that may
15323 -- be out of the proper scope for Gigi, so we insert a reference to
15324 -- it after the derivation.
15325
15326 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15327 declare
15328 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15329
15330 begin
15331 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15332 and then Present (Full_View (Desig_Typ))
15333 and then not Is_Private_Type (Parent_Type)
15334 then
15335 Desig_Typ := Full_View (Desig_Typ);
15336 end if;
15337
15338 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15339
15340 -- Ada 2005 (AI-251): Handle also derivations of abstract
15341 -- interface primitives.
15342
15343 or else (Is_Interface (Desig_Typ)
15344 and then not Is_Class_Wide_Type (Desig_Typ))
15345 then
15346 Acc_Type := New_Copy (Id_Type);
15347 Set_Etype (Acc_Type, Acc_Type);
15348 Set_Scope (Acc_Type, New_Subp);
15349
15350 -- Set size of anonymous access type. If we have an access
15351 -- to an unconstrained array, this is a fat pointer, so it
15352 -- is sizes at twice addtress size.
15353
15354 if Is_Array_Type (Desig_Typ)
15355 and then not Is_Constrained (Desig_Typ)
15356 then
15357 Init_Size (Acc_Type, 2 * System_Address_Size);
15358
15359 -- Other cases use a thin pointer
15360
15361 else
15362 Init_Size (Acc_Type, System_Address_Size);
15363 end if;
15364
15365 -- Set remaining characterstics of anonymous access type
15366
15367 Init_Alignment (Acc_Type);
15368 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15369
15370 Set_Etype (New_Id, Acc_Type);
15371 Set_Scope (New_Id, New_Subp);
15372
15373 -- Create a reference to it
15374
15375 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15376
15377 else
15378 Set_Etype (New_Id, Id_Type);
15379 end if;
15380 end;
15381
15382 -- In Ada2012, a formal may have an incomplete type but the type
15383 -- derivation that inherits the primitive follows the full view.
15384
15385 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15386 or else
15387 (Ekind (Id_Type) = E_Record_Type_With_Private
15388 and then Present (Full_View (Id_Type))
15389 and then
15390 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15391 or else
15392 (Ada_Version >= Ada_2012
15393 and then Ekind (Id_Type) = E_Incomplete_Type
15394 and then Full_View (Id_Type) = Parent_Type)
15395 then
15396 -- Constraint checks on formals are generated during expansion,
15397 -- based on the signature of the original subprogram. The bounds
15398 -- of the derived type are not relevant, and thus we can use
15399 -- the base type for the formals. However, the return type may be
15400 -- used in a context that requires that the proper static bounds
15401 -- be used (a case statement, for example) and for those cases
15402 -- we must use the derived type (first subtype), not its base.
15403
15404 -- If the derived_type_definition has no constraints, we know that
15405 -- the derived type has the same constraints as the first subtype
15406 -- of the parent, and we can also use it rather than its base,
15407 -- which can lead to more efficient code.
15408
15409 if Etype (Id) = Parent_Type then
15410 if Is_Scalar_Type (Parent_Type)
15411 and then
15412 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15413 then
15414 Set_Etype (New_Id, Derived_Type);
15415
15416 elsif Nkind (Par) = N_Full_Type_Declaration
15417 and then
15418 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15419 and then
15420 Is_Entity_Name
15421 (Subtype_Indication (Type_Definition (Par)))
15422 then
15423 Set_Etype (New_Id, Derived_Type);
15424
15425 else
15426 Set_Etype (New_Id, Base_Type (Derived_Type));
15427 end if;
15428
15429 else
15430 Set_Etype (New_Id, Base_Type (Derived_Type));
15431 end if;
15432
15433 else
15434 Set_Etype (New_Id, Etype (Id));
15435 end if;
15436 end Replace_Type;
15437
15438 ----------------------
15439 -- Set_Derived_Name --
15440 ----------------------
15441
15442 procedure Set_Derived_Name is
15443 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15444 begin
15445 if Nm = TSS_Null then
15446 Set_Chars (New_Subp, Chars (Parent_Subp));
15447 else
15448 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15449 end if;
15450 end Set_Derived_Name;
15451
15452 -- Start of processing for Derive_Subprogram
15453
15454 begin
15455 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15456 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15457
15458 -- Check whether the inherited subprogram is a private operation that
15459 -- should be inherited but not yet made visible. Such subprograms can
15460 -- become visible at a later point (e.g., the private part of a public
15461 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15462 -- following predicate is true, then this is not such a private
15463 -- operation and the subprogram simply inherits the name of the parent
15464 -- subprogram. Note the special check for the names of controlled
15465 -- operations, which are currently exempted from being inherited with
15466 -- a hidden name because they must be findable for generation of
15467 -- implicit run-time calls.
15468
15469 if not Is_Hidden (Parent_Subp)
15470 or else Is_Internal (Parent_Subp)
15471 or else Is_Private_Overriding
15472 or else Is_Internal_Name (Chars (Parent_Subp))
15473 or else (Is_Controlled (Parent_Type)
15474 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15475 Name_Finalize,
15476 Name_Initialize))
15477 then
15478 Set_Derived_Name;
15479
15480 -- An inherited dispatching equality will be overridden by an internally
15481 -- generated one, or by an explicit one, so preserve its name and thus
15482 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15483 -- private operation it may become invisible if the full view has
15484 -- progenitors, and the dispatch table will be malformed.
15485 -- We check that the type is limited to handle the anomalous declaration
15486 -- of Limited_Controlled, which is derived from a non-limited type, and
15487 -- which is handled specially elsewhere as well.
15488
15489 elsif Chars (Parent_Subp) = Name_Op_Eq
15490 and then Is_Dispatching_Operation (Parent_Subp)
15491 and then Etype (Parent_Subp) = Standard_Boolean
15492 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15493 and then
15494 Etype (First_Formal (Parent_Subp)) =
15495 Etype (Next_Formal (First_Formal (Parent_Subp)))
15496 then
15497 Set_Derived_Name;
15498
15499 -- If parent is hidden, this can be a regular derivation if the
15500 -- parent is immediately visible in a non-instantiating context,
15501 -- or if we are in the private part of an instance. This test
15502 -- should still be refined ???
15503
15504 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15505 -- operation as a non-visible operation in cases where the parent
15506 -- subprogram might not be visible now, but was visible within the
15507 -- original generic, so it would be wrong to make the inherited
15508 -- subprogram non-visible now. (Not clear if this test is fully
15509 -- correct; are there any cases where we should declare the inherited
15510 -- operation as not visible to avoid it being overridden, e.g., when
15511 -- the parent type is a generic actual with private primitives ???)
15512
15513 -- (they should be treated the same as other private inherited
15514 -- subprograms, but it's not clear how to do this cleanly). ???
15515
15516 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15517 and then Is_Immediately_Visible (Parent_Subp)
15518 and then not In_Instance)
15519 or else In_Instance_Not_Visible
15520 then
15521 Set_Derived_Name;
15522
15523 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15524 -- overrides an interface primitive because interface primitives
15525 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15526
15527 elsif Ada_Version >= Ada_2005
15528 and then Is_Dispatching_Operation (Parent_Subp)
15529 and then Present (Covered_Interface_Op (Parent_Subp))
15530 then
15531 Set_Derived_Name;
15532
15533 -- Otherwise, the type is inheriting a private operation, so enter it
15534 -- with a special name so it can't be overridden.
15535
15536 else
15537 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15538 end if;
15539
15540 Set_Parent (New_Subp, Parent (Derived_Type));
15541
15542 if Present (Actual_Subp) then
15543 Replace_Type (Actual_Subp, New_Subp);
15544 else
15545 Replace_Type (Parent_Subp, New_Subp);
15546 end if;
15547
15548 Conditional_Delay (New_Subp, Parent_Subp);
15549
15550 -- If we are creating a renaming for a primitive operation of an
15551 -- actual of a generic derived type, we must examine the signature
15552 -- of the actual primitive, not that of the generic formal, which for
15553 -- example may be an interface. However the name and initial value
15554 -- of the inherited operation are those of the formal primitive.
15555
15556 Formal := First_Formal (Parent_Subp);
15557
15558 if Present (Actual_Subp) then
15559 Formal_Of_Actual := First_Formal (Actual_Subp);
15560 else
15561 Formal_Of_Actual := Empty;
15562 end if;
15563
15564 while Present (Formal) loop
15565 New_Formal := New_Copy (Formal);
15566
15567 -- Normally we do not go copying parents, but in the case of
15568 -- formals, we need to link up to the declaration (which is the
15569 -- parameter specification), and it is fine to link up to the
15570 -- original formal's parameter specification in this case.
15571
15572 Set_Parent (New_Formal, Parent (Formal));
15573 Append_Entity (New_Formal, New_Subp);
15574
15575 if Present (Formal_Of_Actual) then
15576 Replace_Type (Formal_Of_Actual, New_Formal);
15577 Next_Formal (Formal_Of_Actual);
15578 else
15579 Replace_Type (Formal, New_Formal);
15580 end if;
15581
15582 Next_Formal (Formal);
15583 end loop;
15584
15585 -- If this derivation corresponds to a tagged generic actual, then
15586 -- primitive operations rename those of the actual. Otherwise the
15587 -- primitive operations rename those of the parent type, If the parent
15588 -- renames an intrinsic operator, so does the new subprogram. We except
15589 -- concatenation, which is always properly typed, and does not get
15590 -- expanded as other intrinsic operations.
15591
15592 if No (Actual_Subp) then
15593 if Is_Intrinsic_Subprogram (Parent_Subp) then
15594 Set_Is_Intrinsic_Subprogram (New_Subp);
15595
15596 if Present (Alias (Parent_Subp))
15597 and then Chars (Parent_Subp) /= Name_Op_Concat
15598 then
15599 Set_Alias (New_Subp, Alias (Parent_Subp));
15600 else
15601 Set_Alias (New_Subp, Parent_Subp);
15602 end if;
15603
15604 else
15605 Set_Alias (New_Subp, Parent_Subp);
15606 end if;
15607
15608 else
15609 Set_Alias (New_Subp, Actual_Subp);
15610 end if;
15611
15612 -- Derived subprograms of a tagged type must inherit the convention
15613 -- of the parent subprogram (a requirement of AI-117). Derived
15614 -- subprograms of untagged types simply get convention Ada by default.
15615
15616 -- If the derived type is a tagged generic formal type with unknown
15617 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15618
15619 -- However, if the type is derived from a generic formal, the further
15620 -- inherited subprogram has the convention of the non-generic ancestor.
15621 -- Otherwise there would be no way to override the operation.
15622 -- (This is subject to forthcoming ARG discussions).
15623
15624 if Is_Tagged_Type (Derived_Type) then
15625 if Is_Generic_Type (Derived_Type)
15626 and then Has_Unknown_Discriminants (Derived_Type)
15627 then
15628 Set_Convention (New_Subp, Convention_Intrinsic);
15629
15630 else
15631 if Is_Generic_Type (Parent_Type)
15632 and then Has_Unknown_Discriminants (Parent_Type)
15633 then
15634 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15635 else
15636 Set_Convention (New_Subp, Convention (Parent_Subp));
15637 end if;
15638 end if;
15639 end if;
15640
15641 -- Predefined controlled operations retain their name even if the parent
15642 -- is hidden (see above), but they are not primitive operations if the
15643 -- ancestor is not visible, for example if the parent is a private
15644 -- extension completed with a controlled extension. Note that a full
15645 -- type that is controlled can break privacy: the flag Is_Controlled is
15646 -- set on both views of the type.
15647
15648 if Is_Controlled (Parent_Type)
15649 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15650 Name_Adjust,
15651 Name_Finalize)
15652 and then Is_Hidden (Parent_Subp)
15653 and then not Is_Visibly_Controlled (Parent_Type)
15654 then
15655 Set_Is_Hidden (New_Subp);
15656 end if;
15657
15658 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15659 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15660
15661 if Ekind (Parent_Subp) = E_Procedure then
15662 Set_Is_Valued_Procedure
15663 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15664 else
15665 Set_Has_Controlling_Result
15666 (New_Subp, Has_Controlling_Result (Parent_Subp));
15667 end if;
15668
15669 -- No_Return must be inherited properly. If this is overridden in the
15670 -- case of a dispatching operation, then a check is made in Sem_Disp
15671 -- that the overriding operation is also No_Return (no such check is
15672 -- required for the case of non-dispatching operation.
15673
15674 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15675
15676 -- A derived function with a controlling result is abstract. If the
15677 -- Derived_Type is a nonabstract formal generic derived type, then
15678 -- inherited operations are not abstract: the required check is done at
15679 -- instantiation time. If the derivation is for a generic actual, the
15680 -- function is not abstract unless the actual is.
15681
15682 if Is_Generic_Type (Derived_Type)
15683 and then not Is_Abstract_Type (Derived_Type)
15684 then
15685 null;
15686
15687 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15688 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15689
15690 -- A subprogram subject to pragma Extensions_Visible with value False
15691 -- requires overriding if the subprogram has at least one controlling
15692 -- OUT parameter (SPARK RM 6.1.7(6)).
15693
15694 elsif Ada_Version >= Ada_2005
15695 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15696 or else (Is_Tagged_Type (Derived_Type)
15697 and then Etype (New_Subp) = Derived_Type
15698 and then not Is_Null_Extension (Derived_Type))
15699 or else (Is_Tagged_Type (Derived_Type)
15700 and then Ekind (Etype (New_Subp)) =
15701 E_Anonymous_Access_Type
15702 and then Designated_Type (Etype (New_Subp)) =
15703 Derived_Type
15704 and then not Is_Null_Extension (Derived_Type))
15705 or else (Comes_From_Source (Alias (New_Subp))
15706 and then Is_EVF_Procedure (Alias (New_Subp))))
15707 and then No (Actual_Subp)
15708 then
15709 if not Is_Tagged_Type (Derived_Type)
15710 or else Is_Abstract_Type (Derived_Type)
15711 or else Is_Abstract_Subprogram (Alias (New_Subp))
15712 then
15713 Set_Is_Abstract_Subprogram (New_Subp);
15714 else
15715 Set_Requires_Overriding (New_Subp);
15716 end if;
15717
15718 elsif Ada_Version < Ada_2005
15719 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15720 or else (Is_Tagged_Type (Derived_Type)
15721 and then Etype (New_Subp) = Derived_Type
15722 and then No (Actual_Subp)))
15723 then
15724 Set_Is_Abstract_Subprogram (New_Subp);
15725
15726 -- AI05-0097 : an inherited operation that dispatches on result is
15727 -- abstract if the derived type is abstract, even if the parent type
15728 -- is concrete and the derived type is a null extension.
15729
15730 elsif Has_Controlling_Result (Alias (New_Subp))
15731 and then Is_Abstract_Type (Etype (New_Subp))
15732 then
15733 Set_Is_Abstract_Subprogram (New_Subp);
15734
15735 -- Finally, if the parent type is abstract we must verify that all
15736 -- inherited operations are either non-abstract or overridden, or that
15737 -- the derived type itself is abstract (this check is performed at the
15738 -- end of a package declaration, in Check_Abstract_Overriding). A
15739 -- private overriding in the parent type will not be visible in the
15740 -- derivation if we are not in an inner package or in a child unit of
15741 -- the parent type, in which case the abstractness of the inherited
15742 -- operation is carried to the new subprogram.
15743
15744 elsif Is_Abstract_Type (Parent_Type)
15745 and then not In_Open_Scopes (Scope (Parent_Type))
15746 and then Is_Private_Overriding
15747 and then Is_Abstract_Subprogram (Visible_Subp)
15748 then
15749 if No (Actual_Subp) then
15750 Set_Alias (New_Subp, Visible_Subp);
15751 Set_Is_Abstract_Subprogram (New_Subp, True);
15752
15753 else
15754 -- If this is a derivation for an instance of a formal derived
15755 -- type, abstractness comes from the primitive operation of the
15756 -- actual, not from the operation inherited from the ancestor.
15757
15758 Set_Is_Abstract_Subprogram
15759 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15760 end if;
15761 end if;
15762
15763 New_Overloaded_Entity (New_Subp, Derived_Type);
15764
15765 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15766 -- preconditions and the derived type is abstract, the derived operation
15767 -- is abstract as well if parent subprogram is not abstract or null.
15768
15769 if Is_Abstract_Type (Derived_Type)
15770 and then Has_Non_Trivial_Precondition (Parent_Subp)
15771 and then Present (Interfaces (Derived_Type))
15772 then
15773
15774 -- Add useful attributes of subprogram before the freeze point,
15775 -- in case freezing is delayed or there are previous errors.
15776
15777 Set_Is_Dispatching_Operation (New_Subp);
15778
15779 declare
15780 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15781
15782 begin
15783 if Present (Iface_Prim)
15784 and then Has_Non_Trivial_Precondition (Iface_Prim)
15785 then
15786 Set_Is_Abstract_Subprogram (New_Subp);
15787 end if;
15788 end;
15789 end if;
15790
15791 -- Check for case of a derived subprogram for the instantiation of a
15792 -- formal derived tagged type, if so mark the subprogram as dispatching
15793 -- and inherit the dispatching attributes of the actual subprogram. The
15794 -- derived subprogram is effectively renaming of the actual subprogram,
15795 -- so it needs to have the same attributes as the actual.
15796
15797 if Present (Actual_Subp)
15798 and then Is_Dispatching_Operation (Actual_Subp)
15799 then
15800 Set_Is_Dispatching_Operation (New_Subp);
15801
15802 if Present (DTC_Entity (Actual_Subp)) then
15803 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15804 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15805 end if;
15806 end if;
15807
15808 -- Indicate that a derived subprogram does not require a body and that
15809 -- it does not require processing of default expressions.
15810
15811 Set_Has_Completion (New_Subp);
15812 Set_Default_Expressions_Processed (New_Subp);
15813
15814 if Ekind (New_Subp) = E_Function then
15815 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15816 end if;
15817 end Derive_Subprogram;
15818
15819 ------------------------
15820 -- Derive_Subprograms --
15821 ------------------------
15822
15823 procedure Derive_Subprograms
15824 (Parent_Type : Entity_Id;
15825 Derived_Type : Entity_Id;
15826 Generic_Actual : Entity_Id := Empty)
15827 is
15828 Op_List : constant Elist_Id :=
15829 Collect_Primitive_Operations (Parent_Type);
15830
15831 function Check_Derived_Type return Boolean;
15832 -- Check that all the entities derived from Parent_Type are found in
15833 -- the list of primitives of Derived_Type exactly in the same order.
15834
15835 procedure Derive_Interface_Subprogram
15836 (New_Subp : out Entity_Id;
15837 Subp : Entity_Id;
15838 Actual_Subp : Entity_Id);
15839 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15840 -- (which is an interface primitive). If Generic_Actual is present then
15841 -- Actual_Subp is the actual subprogram corresponding with the generic
15842 -- subprogram Subp.
15843
15844 ------------------------
15845 -- Check_Derived_Type --
15846 ------------------------
15847
15848 function Check_Derived_Type return Boolean is
15849 E : Entity_Id;
15850 Elmt : Elmt_Id;
15851 List : Elist_Id;
15852 New_Subp : Entity_Id;
15853 Op_Elmt : Elmt_Id;
15854 Subp : Entity_Id;
15855
15856 begin
15857 -- Traverse list of entities in the current scope searching for
15858 -- an incomplete type whose full-view is derived type.
15859
15860 E := First_Entity (Scope (Derived_Type));
15861 while Present (E) and then E /= Derived_Type loop
15862 if Ekind (E) = E_Incomplete_Type
15863 and then Present (Full_View (E))
15864 and then Full_View (E) = Derived_Type
15865 then
15866 -- Disable this test if Derived_Type completes an incomplete
15867 -- type because in such case more primitives can be added
15868 -- later to the list of primitives of Derived_Type by routine
15869 -- Process_Incomplete_Dependents
15870
15871 return True;
15872 end if;
15873
15874 E := Next_Entity (E);
15875 end loop;
15876
15877 List := Collect_Primitive_Operations (Derived_Type);
15878 Elmt := First_Elmt (List);
15879
15880 Op_Elmt := First_Elmt (Op_List);
15881 while Present (Op_Elmt) loop
15882 Subp := Node (Op_Elmt);
15883 New_Subp := Node (Elmt);
15884
15885 -- At this early stage Derived_Type has no entities with attribute
15886 -- Interface_Alias. In addition, such primitives are always
15887 -- located at the end of the list of primitives of Parent_Type.
15888 -- Therefore, if found we can safely stop processing pending
15889 -- entities.
15890
15891 exit when Present (Interface_Alias (Subp));
15892
15893 -- Handle hidden entities
15894
15895 if not Is_Predefined_Dispatching_Operation (Subp)
15896 and then Is_Hidden (Subp)
15897 then
15898 if Present (New_Subp)
15899 and then Primitive_Names_Match (Subp, New_Subp)
15900 then
15901 Next_Elmt (Elmt);
15902 end if;
15903
15904 else
15905 if not Present (New_Subp)
15906 or else Ekind (Subp) /= Ekind (New_Subp)
15907 or else not Primitive_Names_Match (Subp, New_Subp)
15908 then
15909 return False;
15910 end if;
15911
15912 Next_Elmt (Elmt);
15913 end if;
15914
15915 Next_Elmt (Op_Elmt);
15916 end loop;
15917
15918 return True;
15919 end Check_Derived_Type;
15920
15921 ---------------------------------
15922 -- Derive_Interface_Subprogram --
15923 ---------------------------------
15924
15925 procedure Derive_Interface_Subprogram
15926 (New_Subp : out Entity_Id;
15927 Subp : Entity_Id;
15928 Actual_Subp : Entity_Id)
15929 is
15930 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15931 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15932
15933 begin
15934 pragma Assert (Is_Interface (Iface_Type));
15935
15936 Derive_Subprogram
15937 (New_Subp => New_Subp,
15938 Parent_Subp => Iface_Subp,
15939 Derived_Type => Derived_Type,
15940 Parent_Type => Iface_Type,
15941 Actual_Subp => Actual_Subp);
15942
15943 -- Given that this new interface entity corresponds with a primitive
15944 -- of the parent that was not overridden we must leave it associated
15945 -- with its parent primitive to ensure that it will share the same
15946 -- dispatch table slot when overridden. We must set the Alias to Subp
15947 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15948 -- (in case we inherited Subp from Iface_Type via a nonabstract
15949 -- generic formal type).
15950
15951 if No (Actual_Subp) then
15952 Set_Alias (New_Subp, Subp);
15953
15954 declare
15955 T : Entity_Id := Find_Dispatching_Type (Subp);
15956 begin
15957 while Etype (T) /= T loop
15958 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15959 Set_Is_Abstract_Subprogram (New_Subp, False);
15960 exit;
15961 end if;
15962
15963 T := Etype (T);
15964 end loop;
15965 end;
15966
15967 -- For instantiations this is not needed since the previous call to
15968 -- Derive_Subprogram leaves the entity well decorated.
15969
15970 else
15971 pragma Assert (Alias (New_Subp) = Actual_Subp);
15972 null;
15973 end if;
15974 end Derive_Interface_Subprogram;
15975
15976 -- Local variables
15977
15978 Alias_Subp : Entity_Id;
15979 Act_List : Elist_Id;
15980 Act_Elmt : Elmt_Id;
15981 Act_Subp : Entity_Id := Empty;
15982 Elmt : Elmt_Id;
15983 Need_Search : Boolean := False;
15984 New_Subp : Entity_Id := Empty;
15985 Parent_Base : Entity_Id;
15986 Subp : Entity_Id;
15987
15988 -- Start of processing for Derive_Subprograms
15989
15990 begin
15991 if Ekind (Parent_Type) = E_Record_Type_With_Private
15992 and then Has_Discriminants (Parent_Type)
15993 and then Present (Full_View (Parent_Type))
15994 then
15995 Parent_Base := Full_View (Parent_Type);
15996 else
15997 Parent_Base := Parent_Type;
15998 end if;
15999
16000 if Present (Generic_Actual) then
16001 Act_List := Collect_Primitive_Operations (Generic_Actual);
16002 Act_Elmt := First_Elmt (Act_List);
16003 else
16004 Act_List := No_Elist;
16005 Act_Elmt := No_Elmt;
16006 end if;
16007
16008 -- Derive primitives inherited from the parent. Note that if the generic
16009 -- actual is present, this is not really a type derivation, it is a
16010 -- completion within an instance.
16011
16012 -- Case 1: Derived_Type does not implement interfaces
16013
16014 if not Is_Tagged_Type (Derived_Type)
16015 or else (not Has_Interfaces (Derived_Type)
16016 and then not (Present (Generic_Actual)
16017 and then Has_Interfaces (Generic_Actual)))
16018 then
16019 Elmt := First_Elmt (Op_List);
16020 while Present (Elmt) loop
16021 Subp := Node (Elmt);
16022
16023 -- Literals are derived earlier in the process of building the
16024 -- derived type, and are skipped here.
16025
16026 if Ekind (Subp) = E_Enumeration_Literal then
16027 null;
16028
16029 -- The actual is a direct descendant and the common primitive
16030 -- operations appear in the same order.
16031
16032 -- If the generic parent type is present, the derived type is an
16033 -- instance of a formal derived type, and within the instance its
16034 -- operations are those of the actual. We derive from the formal
16035 -- type but make the inherited operations aliases of the
16036 -- corresponding operations of the actual.
16037
16038 else
16039 pragma Assert (No (Node (Act_Elmt))
16040 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16041 and then
16042 Type_Conformant
16043 (Subp, Node (Act_Elmt),
16044 Skip_Controlling_Formals => True)));
16045
16046 Derive_Subprogram
16047 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16048
16049 if Present (Act_Elmt) then
16050 Next_Elmt (Act_Elmt);
16051 end if;
16052 end if;
16053
16054 Next_Elmt (Elmt);
16055 end loop;
16056
16057 -- Case 2: Derived_Type implements interfaces
16058
16059 else
16060 -- If the parent type has no predefined primitives we remove
16061 -- predefined primitives from the list of primitives of generic
16062 -- actual to simplify the complexity of this algorithm.
16063
16064 if Present (Generic_Actual) then
16065 declare
16066 Has_Predefined_Primitives : Boolean := False;
16067
16068 begin
16069 -- Check if the parent type has predefined primitives
16070
16071 Elmt := First_Elmt (Op_List);
16072 while Present (Elmt) loop
16073 Subp := Node (Elmt);
16074
16075 if Is_Predefined_Dispatching_Operation (Subp)
16076 and then not Comes_From_Source (Ultimate_Alias (Subp))
16077 then
16078 Has_Predefined_Primitives := True;
16079 exit;
16080 end if;
16081
16082 Next_Elmt (Elmt);
16083 end loop;
16084
16085 -- Remove predefined primitives of Generic_Actual. We must use
16086 -- an auxiliary list because in case of tagged types the value
16087 -- returned by Collect_Primitive_Operations is the value stored
16088 -- in its Primitive_Operations attribute (and we don't want to
16089 -- modify its current contents).
16090
16091 if not Has_Predefined_Primitives then
16092 declare
16093 Aux_List : constant Elist_Id := New_Elmt_List;
16094
16095 begin
16096 Elmt := First_Elmt (Act_List);
16097 while Present (Elmt) loop
16098 Subp := Node (Elmt);
16099
16100 if not Is_Predefined_Dispatching_Operation (Subp)
16101 or else Comes_From_Source (Subp)
16102 then
16103 Append_Elmt (Subp, Aux_List);
16104 end if;
16105
16106 Next_Elmt (Elmt);
16107 end loop;
16108
16109 Act_List := Aux_List;
16110 end;
16111 end if;
16112
16113 Act_Elmt := First_Elmt (Act_List);
16114 Act_Subp := Node (Act_Elmt);
16115 end;
16116 end if;
16117
16118 -- Stage 1: If the generic actual is not present we derive the
16119 -- primitives inherited from the parent type. If the generic parent
16120 -- type is present, the derived type is an instance of a formal
16121 -- derived type, and within the instance its operations are those of
16122 -- the actual. We derive from the formal type but make the inherited
16123 -- operations aliases of the corresponding operations of the actual.
16124
16125 Elmt := First_Elmt (Op_List);
16126 while Present (Elmt) loop
16127 Subp := Node (Elmt);
16128 Alias_Subp := Ultimate_Alias (Subp);
16129
16130 -- Do not derive internal entities of the parent that link
16131 -- interface primitives with their covering primitive. These
16132 -- entities will be added to this type when frozen.
16133
16134 if Present (Interface_Alias (Subp)) then
16135 goto Continue;
16136 end if;
16137
16138 -- If the generic actual is present find the corresponding
16139 -- operation in the generic actual. If the parent type is a
16140 -- direct ancestor of the derived type then, even if it is an
16141 -- interface, the operations are inherited from the primary
16142 -- dispatch table and are in the proper order. If we detect here
16143 -- that primitives are not in the same order we traverse the list
16144 -- of primitive operations of the actual to find the one that
16145 -- implements the interface primitive.
16146
16147 if Need_Search
16148 or else
16149 (Present (Generic_Actual)
16150 and then Present (Act_Subp)
16151 and then not
16152 (Primitive_Names_Match (Subp, Act_Subp)
16153 and then
16154 Type_Conformant (Subp, Act_Subp,
16155 Skip_Controlling_Formals => True)))
16156 then
16157 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16158 Use_Full_View => True));
16159
16160 -- Remember that we need searching for all pending primitives
16161
16162 Need_Search := True;
16163
16164 -- Handle entities associated with interface primitives
16165
16166 if Present (Alias_Subp)
16167 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16168 and then not Is_Predefined_Dispatching_Operation (Subp)
16169 then
16170 -- Search for the primitive in the homonym chain
16171
16172 Act_Subp :=
16173 Find_Primitive_Covering_Interface
16174 (Tagged_Type => Generic_Actual,
16175 Iface_Prim => Alias_Subp);
16176
16177 -- Previous search may not locate primitives covering
16178 -- interfaces defined in generics units or instantiations.
16179 -- (it fails if the covering primitive has formals whose
16180 -- type is also defined in generics or instantiations).
16181 -- In such case we search in the list of primitives of the
16182 -- generic actual for the internal entity that links the
16183 -- interface primitive and the covering primitive.
16184
16185 if No (Act_Subp)
16186 and then Is_Generic_Type (Parent_Type)
16187 then
16188 -- This code has been designed to handle only generic
16189 -- formals that implement interfaces that are defined
16190 -- in a generic unit or instantiation. If this code is
16191 -- needed for other cases we must review it because
16192 -- (given that it relies on Original_Location to locate
16193 -- the primitive of Generic_Actual that covers the
16194 -- interface) it could leave linked through attribute
16195 -- Alias entities of unrelated instantiations).
16196
16197 pragma Assert
16198 (Is_Generic_Unit
16199 (Scope (Find_Dispatching_Type (Alias_Subp)))
16200 or else
16201 Instantiation_Depth
16202 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16203
16204 declare
16205 Iface_Prim_Loc : constant Source_Ptr :=
16206 Original_Location (Sloc (Alias_Subp));
16207
16208 Elmt : Elmt_Id;
16209 Prim : Entity_Id;
16210
16211 begin
16212 Elmt :=
16213 First_Elmt (Primitive_Operations (Generic_Actual));
16214
16215 Search : while Present (Elmt) loop
16216 Prim := Node (Elmt);
16217
16218 if Present (Interface_Alias (Prim))
16219 and then Original_Location
16220 (Sloc (Interface_Alias (Prim))) =
16221 Iface_Prim_Loc
16222 then
16223 Act_Subp := Alias (Prim);
16224 exit Search;
16225 end if;
16226
16227 Next_Elmt (Elmt);
16228 end loop Search;
16229 end;
16230 end if;
16231
16232 pragma Assert (Present (Act_Subp)
16233 or else Is_Abstract_Type (Generic_Actual)
16234 or else Serious_Errors_Detected > 0);
16235
16236 -- Handle predefined primitives plus the rest of user-defined
16237 -- primitives
16238
16239 else
16240 Act_Elmt := First_Elmt (Act_List);
16241 while Present (Act_Elmt) loop
16242 Act_Subp := Node (Act_Elmt);
16243
16244 exit when Primitive_Names_Match (Subp, Act_Subp)
16245 and then Type_Conformant
16246 (Subp, Act_Subp,
16247 Skip_Controlling_Formals => True)
16248 and then No (Interface_Alias (Act_Subp));
16249
16250 Next_Elmt (Act_Elmt);
16251 end loop;
16252
16253 if No (Act_Elmt) then
16254 Act_Subp := Empty;
16255 end if;
16256 end if;
16257 end if;
16258
16259 -- Case 1: If the parent is a limited interface then it has the
16260 -- predefined primitives of synchronized interfaces. However, the
16261 -- actual type may be a non-limited type and hence it does not
16262 -- have such primitives.
16263
16264 if Present (Generic_Actual)
16265 and then not Present (Act_Subp)
16266 and then Is_Limited_Interface (Parent_Base)
16267 and then Is_Predefined_Interface_Primitive (Subp)
16268 then
16269 null;
16270
16271 -- Case 2: Inherit entities associated with interfaces that were
16272 -- not covered by the parent type. We exclude here null interface
16273 -- primitives because they do not need special management.
16274
16275 -- We also exclude interface operations that are renamings. If the
16276 -- subprogram is an explicit renaming of an interface primitive,
16277 -- it is a regular primitive operation, and the presence of its
16278 -- alias is not relevant: it has to be derived like any other
16279 -- primitive.
16280
16281 elsif Present (Alias (Subp))
16282 and then Nkind (Unit_Declaration_Node (Subp)) /=
16283 N_Subprogram_Renaming_Declaration
16284 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16285 and then not
16286 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16287 and then Null_Present (Parent (Alias_Subp)))
16288 then
16289 -- If this is an abstract private type then we transfer the
16290 -- derivation of the interface primitive from the partial view
16291 -- to the full view. This is safe because all the interfaces
16292 -- must be visible in the partial view. Done to avoid adding
16293 -- a new interface derivation to the private part of the
16294 -- enclosing package; otherwise this new derivation would be
16295 -- decorated as hidden when the analysis of the enclosing
16296 -- package completes.
16297
16298 if Is_Abstract_Type (Derived_Type)
16299 and then In_Private_Part (Current_Scope)
16300 and then Has_Private_Declaration (Derived_Type)
16301 then
16302 declare
16303 Partial_View : Entity_Id;
16304 Elmt : Elmt_Id;
16305 Ent : Entity_Id;
16306
16307 begin
16308 Partial_View := First_Entity (Current_Scope);
16309 loop
16310 exit when No (Partial_View)
16311 or else (Has_Private_Declaration (Partial_View)
16312 and then
16313 Full_View (Partial_View) = Derived_Type);
16314
16315 Next_Entity (Partial_View);
16316 end loop;
16317
16318 -- If the partial view was not found then the source code
16319 -- has errors and the derivation is not needed.
16320
16321 if Present (Partial_View) then
16322 Elmt :=
16323 First_Elmt (Primitive_Operations (Partial_View));
16324 while Present (Elmt) loop
16325 Ent := Node (Elmt);
16326
16327 if Present (Alias (Ent))
16328 and then Ultimate_Alias (Ent) = Alias (Subp)
16329 then
16330 Append_Elmt
16331 (Ent, Primitive_Operations (Derived_Type));
16332 exit;
16333 end if;
16334
16335 Next_Elmt (Elmt);
16336 end loop;
16337
16338 -- If the interface primitive was not found in the
16339 -- partial view then this interface primitive was
16340 -- overridden. We add a derivation to activate in
16341 -- Derive_Progenitor_Subprograms the machinery to
16342 -- search for it.
16343
16344 if No (Elmt) then
16345 Derive_Interface_Subprogram
16346 (New_Subp => New_Subp,
16347 Subp => Subp,
16348 Actual_Subp => Act_Subp);
16349 end if;
16350 end if;
16351 end;
16352 else
16353 Derive_Interface_Subprogram
16354 (New_Subp => New_Subp,
16355 Subp => Subp,
16356 Actual_Subp => Act_Subp);
16357 end if;
16358
16359 -- Case 3: Common derivation
16360
16361 else
16362 Derive_Subprogram
16363 (New_Subp => New_Subp,
16364 Parent_Subp => Subp,
16365 Derived_Type => Derived_Type,
16366 Parent_Type => Parent_Base,
16367 Actual_Subp => Act_Subp);
16368 end if;
16369
16370 -- No need to update Act_Elm if we must search for the
16371 -- corresponding operation in the generic actual
16372
16373 if not Need_Search
16374 and then Present (Act_Elmt)
16375 then
16376 Next_Elmt (Act_Elmt);
16377 Act_Subp := Node (Act_Elmt);
16378 end if;
16379
16380 <<Continue>>
16381 Next_Elmt (Elmt);
16382 end loop;
16383
16384 -- Inherit additional operations from progenitors. If the derived
16385 -- type is a generic actual, there are not new primitive operations
16386 -- for the type because it has those of the actual, and therefore
16387 -- nothing needs to be done. The renamings generated above are not
16388 -- primitive operations, and their purpose is simply to make the
16389 -- proper operations visible within an instantiation.
16390
16391 if No (Generic_Actual) then
16392 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16393 end if;
16394 end if;
16395
16396 -- Final check: Direct descendants must have their primitives in the
16397 -- same order. We exclude from this test untagged types and instances
16398 -- of formal derived types. We skip this test if we have already
16399 -- reported serious errors in the sources.
16400
16401 pragma Assert (not Is_Tagged_Type (Derived_Type)
16402 or else Present (Generic_Actual)
16403 or else Serious_Errors_Detected > 0
16404 or else Check_Derived_Type);
16405 end Derive_Subprograms;
16406
16407 --------------------------------
16408 -- Derived_Standard_Character --
16409 --------------------------------
16410
16411 procedure Derived_Standard_Character
16412 (N : Node_Id;
16413 Parent_Type : Entity_Id;
16414 Derived_Type : Entity_Id)
16415 is
16416 Loc : constant Source_Ptr := Sloc (N);
16417 Def : constant Node_Id := Type_Definition (N);
16418 Indic : constant Node_Id := Subtype_Indication (Def);
16419 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16420 Implicit_Base : constant Entity_Id :=
16421 Create_Itype
16422 (E_Enumeration_Type, N, Derived_Type, 'B');
16423
16424 Lo : Node_Id;
16425 Hi : Node_Id;
16426
16427 begin
16428 Discard_Node (Process_Subtype (Indic, N));
16429
16430 Set_Etype (Implicit_Base, Parent_Base);
16431 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16432 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16433
16434 Set_Is_Character_Type (Implicit_Base, True);
16435 Set_Has_Delayed_Freeze (Implicit_Base);
16436
16437 -- The bounds of the implicit base are the bounds of the parent base.
16438 -- Note that their type is the parent base.
16439
16440 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16441 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16442
16443 Set_Scalar_Range (Implicit_Base,
16444 Make_Range (Loc,
16445 Low_Bound => Lo,
16446 High_Bound => Hi));
16447
16448 Conditional_Delay (Derived_Type, Parent_Type);
16449
16450 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16451 Set_Etype (Derived_Type, Implicit_Base);
16452 Set_Size_Info (Derived_Type, Parent_Type);
16453
16454 if Unknown_RM_Size (Derived_Type) then
16455 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16456 end if;
16457
16458 Set_Is_Character_Type (Derived_Type, True);
16459
16460 if Nkind (Indic) /= N_Subtype_Indication then
16461
16462 -- If no explicit constraint, the bounds are those
16463 -- of the parent type.
16464
16465 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16466 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16467 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16468 end if;
16469
16470 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16471
16472 -- Because the implicit base is used in the conversion of the bounds, we
16473 -- have to freeze it now. This is similar to what is done for numeric
16474 -- types, and it equally suspicious, but otherwise a nonstatic bound
16475 -- will have a reference to an unfrozen type, which is rejected by Gigi
16476 -- (???). This requires specific care for definition of stream
16477 -- attributes. For details, see comments at the end of
16478 -- Build_Derived_Numeric_Type.
16479
16480 Freeze_Before (N, Implicit_Base);
16481 end Derived_Standard_Character;
16482
16483 ------------------------------
16484 -- Derived_Type_Declaration --
16485 ------------------------------
16486
16487 procedure Derived_Type_Declaration
16488 (T : Entity_Id;
16489 N : Node_Id;
16490 Is_Completion : Boolean)
16491 is
16492 Parent_Type : Entity_Id;
16493
16494 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16495 -- Check whether the parent type is a generic formal, or derives
16496 -- directly or indirectly from one.
16497
16498 ------------------------
16499 -- Comes_From_Generic --
16500 ------------------------
16501
16502 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16503 begin
16504 if Is_Generic_Type (Typ) then
16505 return True;
16506
16507 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16508 return True;
16509
16510 elsif Is_Private_Type (Typ)
16511 and then Present (Full_View (Typ))
16512 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16513 then
16514 return True;
16515
16516 elsif Is_Generic_Actual_Type (Typ) then
16517 return True;
16518
16519 else
16520 return False;
16521 end if;
16522 end Comes_From_Generic;
16523
16524 -- Local variables
16525
16526 Def : constant Node_Id := Type_Definition (N);
16527 Iface_Def : Node_Id;
16528 Indic : constant Node_Id := Subtype_Indication (Def);
16529 Extension : constant Node_Id := Record_Extension_Part (Def);
16530 Parent_Node : Node_Id;
16531 Taggd : Boolean;
16532
16533 -- Start of processing for Derived_Type_Declaration
16534
16535 begin
16536 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16537
16538 if SPARK_Mode = On
16539 and then Is_Tagged_Type (Parent_Type)
16540 then
16541 declare
16542 Partial_View : constant Entity_Id :=
16543 Incomplete_Or_Partial_View (Parent_Type);
16544
16545 begin
16546 -- If the partial view was not found then the parent type is not
16547 -- a private type. Otherwise check if the partial view is a tagged
16548 -- private type.
16549
16550 if Present (Partial_View)
16551 and then Is_Private_Type (Partial_View)
16552 and then not Is_Tagged_Type (Partial_View)
16553 then
16554 Error_Msg_NE
16555 ("cannot derive from & declared as untagged private "
16556 & "(SPARK RM 3.4(1))", N, Partial_View);
16557 end if;
16558 end;
16559 end if;
16560
16561 -- Ada 2005 (AI-251): In case of interface derivation check that the
16562 -- parent is also an interface.
16563
16564 if Interface_Present (Def) then
16565 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16566
16567 if not Is_Interface (Parent_Type) then
16568 Diagnose_Interface (Indic, Parent_Type);
16569
16570 else
16571 Parent_Node := Parent (Base_Type (Parent_Type));
16572 Iface_Def := Type_Definition (Parent_Node);
16573
16574 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16575 -- other limited interfaces.
16576
16577 if Limited_Present (Def) then
16578 if Limited_Present (Iface_Def) then
16579 null;
16580
16581 elsif Protected_Present (Iface_Def) then
16582 Error_Msg_NE
16583 ("descendant of & must be declared as a protected "
16584 & "interface", N, Parent_Type);
16585
16586 elsif Synchronized_Present (Iface_Def) then
16587 Error_Msg_NE
16588 ("descendant of & must be declared as a synchronized "
16589 & "interface", N, Parent_Type);
16590
16591 elsif Task_Present (Iface_Def) then
16592 Error_Msg_NE
16593 ("descendant of & must be declared as a task interface",
16594 N, Parent_Type);
16595
16596 else
16597 Error_Msg_N
16598 ("(Ada 2005) limited interface cannot inherit from "
16599 & "non-limited interface", Indic);
16600 end if;
16601
16602 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16603 -- from non-limited or limited interfaces.
16604
16605 elsif not Protected_Present (Def)
16606 and then not Synchronized_Present (Def)
16607 and then not Task_Present (Def)
16608 then
16609 if Limited_Present (Iface_Def) then
16610 null;
16611
16612 elsif Protected_Present (Iface_Def) then
16613 Error_Msg_NE
16614 ("descendant of & must be declared as a protected "
16615 & "interface", N, Parent_Type);
16616
16617 elsif Synchronized_Present (Iface_Def) then
16618 Error_Msg_NE
16619 ("descendant of & must be declared as a synchronized "
16620 & "interface", N, Parent_Type);
16621
16622 elsif Task_Present (Iface_Def) then
16623 Error_Msg_NE
16624 ("descendant of & must be declared as a task interface",
16625 N, Parent_Type);
16626 else
16627 null;
16628 end if;
16629 end if;
16630 end if;
16631 end if;
16632
16633 if Is_Tagged_Type (Parent_Type)
16634 and then Is_Concurrent_Type (Parent_Type)
16635 and then not Is_Interface (Parent_Type)
16636 then
16637 Error_Msg_N
16638 ("parent type of a record extension cannot be a synchronized "
16639 & "tagged type (RM 3.9.1 (3/1))", N);
16640 Set_Etype (T, Any_Type);
16641 return;
16642 end if;
16643
16644 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16645 -- interfaces
16646
16647 if Is_Tagged_Type (Parent_Type)
16648 and then Is_Non_Empty_List (Interface_List (Def))
16649 then
16650 declare
16651 Intf : Node_Id;
16652 T : Entity_Id;
16653
16654 begin
16655 Intf := First (Interface_List (Def));
16656 while Present (Intf) loop
16657 T := Find_Type_Of_Subtype_Indic (Intf);
16658
16659 if not Is_Interface (T) then
16660 Diagnose_Interface (Intf, T);
16661
16662 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16663 -- a limited type from having a nonlimited progenitor.
16664
16665 elsif (Limited_Present (Def)
16666 or else (not Is_Interface (Parent_Type)
16667 and then Is_Limited_Type (Parent_Type)))
16668 and then not Is_Limited_Interface (T)
16669 then
16670 Error_Msg_NE
16671 ("progenitor interface& of limited type must be limited",
16672 N, T);
16673 end if;
16674
16675 Next (Intf);
16676 end loop;
16677 end;
16678 end if;
16679
16680 if Parent_Type = Any_Type
16681 or else Etype (Parent_Type) = Any_Type
16682 or else (Is_Class_Wide_Type (Parent_Type)
16683 and then Etype (Parent_Type) = T)
16684 then
16685 -- If Parent_Type is undefined or illegal, make new type into a
16686 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16687 -- errors. If this is a self-definition, emit error now.
16688
16689 if T = Parent_Type or else T = Etype (Parent_Type) then
16690 Error_Msg_N ("type cannot be used in its own definition", Indic);
16691 end if;
16692
16693 Set_Ekind (T, Ekind (Parent_Type));
16694 Set_Etype (T, Any_Type);
16695 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16696
16697 if Is_Tagged_Type (T)
16698 and then Is_Record_Type (T)
16699 then
16700 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16701 end if;
16702
16703 return;
16704 end if;
16705
16706 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16707 -- an interface is special because the list of interfaces in the full
16708 -- view can be given in any order. For example:
16709
16710 -- type A is interface;
16711 -- type B is interface and A;
16712 -- type D is new B with private;
16713 -- private
16714 -- type D is new A and B with null record; -- 1 --
16715
16716 -- In this case we perform the following transformation of -1-:
16717
16718 -- type D is new B and A with null record;
16719
16720 -- If the parent of the full-view covers the parent of the partial-view
16721 -- we have two possible cases:
16722
16723 -- 1) They have the same parent
16724 -- 2) The parent of the full-view implements some further interfaces
16725
16726 -- In both cases we do not need to perform the transformation. In the
16727 -- first case the source program is correct and the transformation is
16728 -- not needed; in the second case the source program does not fulfill
16729 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16730 -- later.
16731
16732 -- This transformation not only simplifies the rest of the analysis of
16733 -- this type declaration but also simplifies the correct generation of
16734 -- the object layout to the expander.
16735
16736 if In_Private_Part (Current_Scope)
16737 and then Is_Interface (Parent_Type)
16738 then
16739 declare
16740 Iface : Node_Id;
16741 Partial_View : Entity_Id;
16742 Partial_View_Parent : Entity_Id;
16743 New_Iface : Node_Id;
16744
16745 begin
16746 -- Look for the associated private type declaration
16747
16748 Partial_View := Incomplete_Or_Partial_View (T);
16749
16750 -- If the partial view was not found then the source code has
16751 -- errors and the transformation is not needed.
16752
16753 if Present (Partial_View) then
16754 Partial_View_Parent := Etype (Partial_View);
16755
16756 -- If the parent of the full-view covers the parent of the
16757 -- partial-view we have nothing else to do.
16758
16759 if Interface_Present_In_Ancestor
16760 (Parent_Type, Partial_View_Parent)
16761 then
16762 null;
16763
16764 -- Traverse the list of interfaces of the full-view to look
16765 -- for the parent of the partial-view and perform the tree
16766 -- transformation.
16767
16768 else
16769 Iface := First (Interface_List (Def));
16770 while Present (Iface) loop
16771 if Etype (Iface) = Etype (Partial_View) then
16772 Rewrite (Subtype_Indication (Def),
16773 New_Copy (Subtype_Indication
16774 (Parent (Partial_View))));
16775
16776 New_Iface :=
16777 Make_Identifier (Sloc (N), Chars (Parent_Type));
16778 Append (New_Iface, Interface_List (Def));
16779
16780 -- Analyze the transformed code
16781
16782 Derived_Type_Declaration (T, N, Is_Completion);
16783 return;
16784 end if;
16785
16786 Next (Iface);
16787 end loop;
16788 end if;
16789 end if;
16790 end;
16791 end if;
16792
16793 -- Only composite types other than array types are allowed to have
16794 -- discriminants.
16795
16796 if Present (Discriminant_Specifications (N)) then
16797 if (Is_Elementary_Type (Parent_Type)
16798 or else
16799 Is_Array_Type (Parent_Type))
16800 and then not Error_Posted (N)
16801 then
16802 Error_Msg_N
16803 ("elementary or array type cannot have discriminants",
16804 Defining_Identifier (First (Discriminant_Specifications (N))));
16805
16806 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16807 -- only if we are not already processing a malformed syntax tree.
16808
16809 if Is_Type (T) then
16810 Set_Has_Discriminants (T, False);
16811 end if;
16812
16813 -- The type is allowed to have discriminants
16814
16815 else
16816 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16817 end if;
16818 end if;
16819
16820 -- In Ada 83, a derived type defined in a package specification cannot
16821 -- be used for further derivation until the end of its visible part.
16822 -- Note that derivation in the private part of the package is allowed.
16823
16824 if Ada_Version = Ada_83
16825 and then Is_Derived_Type (Parent_Type)
16826 and then In_Visible_Part (Scope (Parent_Type))
16827 then
16828 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16829 Error_Msg_N
16830 ("(Ada 83): premature use of type for derivation", Indic);
16831 end if;
16832 end if;
16833
16834 -- Check for early use of incomplete or private type
16835
16836 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16837 Error_Msg_N ("premature derivation of incomplete type", Indic);
16838 return;
16839
16840 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16841 and then not Comes_From_Generic (Parent_Type))
16842 or else Has_Private_Component (Parent_Type)
16843 then
16844 -- The ancestor type of a formal type can be incomplete, in which
16845 -- case only the operations of the partial view are available in the
16846 -- generic. Subsequent checks may be required when the full view is
16847 -- analyzed to verify that a derivation from a tagged type has an
16848 -- extension.
16849
16850 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16851 null;
16852
16853 elsif No (Underlying_Type (Parent_Type))
16854 or else Has_Private_Component (Parent_Type)
16855 then
16856 Error_Msg_N
16857 ("premature derivation of derived or private type", Indic);
16858
16859 -- Flag the type itself as being in error, this prevents some
16860 -- nasty problems with subsequent uses of the malformed type.
16861
16862 Set_Error_Posted (T);
16863
16864 -- Check that within the immediate scope of an untagged partial
16865 -- view it's illegal to derive from the partial view if the
16866 -- full view is tagged. (7.3(7))
16867
16868 -- We verify that the Parent_Type is a partial view by checking
16869 -- that it is not a Full_Type_Declaration (i.e. a private type or
16870 -- private extension declaration), to distinguish a partial view
16871 -- from a derivation from a private type which also appears as
16872 -- E_Private_Type. If the parent base type is not declared in an
16873 -- enclosing scope there is no need to check.
16874
16875 elsif Present (Full_View (Parent_Type))
16876 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16877 and then not Is_Tagged_Type (Parent_Type)
16878 and then Is_Tagged_Type (Full_View (Parent_Type))
16879 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16880 then
16881 Error_Msg_N
16882 ("premature derivation from type with tagged full view",
16883 Indic);
16884 end if;
16885 end if;
16886
16887 -- Check that form of derivation is appropriate
16888
16889 Taggd := Is_Tagged_Type (Parent_Type);
16890
16891 -- Set the parent type to the class-wide type's specific type in this
16892 -- case to prevent cascading errors
16893
16894 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16895 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16896 Set_Etype (T, Etype (Parent_Type));
16897 return;
16898 end if;
16899
16900 if Present (Extension) and then not Taggd then
16901 Error_Msg_N
16902 ("type derived from untagged type cannot have extension", Indic);
16903
16904 elsif No (Extension) and then Taggd then
16905
16906 -- If this declaration is within a private part (or body) of a
16907 -- generic instantiation then the derivation is allowed (the parent
16908 -- type can only appear tagged in this case if it's a generic actual
16909 -- type, since it would otherwise have been rejected in the analysis
16910 -- of the generic template).
16911
16912 if not Is_Generic_Actual_Type (Parent_Type)
16913 or else In_Visible_Part (Scope (Parent_Type))
16914 then
16915 if Is_Class_Wide_Type (Parent_Type) then
16916 Error_Msg_N
16917 ("parent type must not be a class-wide type", Indic);
16918
16919 -- Use specific type to prevent cascaded errors.
16920
16921 Parent_Type := Etype (Parent_Type);
16922
16923 else
16924 Error_Msg_N
16925 ("type derived from tagged type must have extension", Indic);
16926 end if;
16927 end if;
16928 end if;
16929
16930 -- AI-443: Synchronized formal derived types require a private
16931 -- extension. There is no point in checking the ancestor type or
16932 -- the progenitors since the construct is wrong to begin with.
16933
16934 if Ada_Version >= Ada_2005
16935 and then Is_Generic_Type (T)
16936 and then Present (Original_Node (N))
16937 then
16938 declare
16939 Decl : constant Node_Id := Original_Node (N);
16940
16941 begin
16942 if Nkind (Decl) = N_Formal_Type_Declaration
16943 and then Nkind (Formal_Type_Definition (Decl)) =
16944 N_Formal_Derived_Type_Definition
16945 and then Synchronized_Present (Formal_Type_Definition (Decl))
16946 and then No (Extension)
16947
16948 -- Avoid emitting a duplicate error message
16949
16950 and then not Error_Posted (Indic)
16951 then
16952 Error_Msg_N
16953 ("synchronized derived type must have extension", N);
16954 end if;
16955 end;
16956 end if;
16957
16958 if Null_Exclusion_Present (Def)
16959 and then not Is_Access_Type (Parent_Type)
16960 then
16961 Error_Msg_N ("null exclusion can only apply to an access type", N);
16962 end if;
16963
16964 -- Avoid deriving parent primitives of underlying record views
16965
16966 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16967 Derive_Subps => not Is_Underlying_Record_View (T));
16968
16969 -- AI-419: The parent type of an explicitly limited derived type must
16970 -- be a limited type or a limited interface.
16971
16972 if Limited_Present (Def) then
16973 Set_Is_Limited_Record (T);
16974
16975 if Is_Interface (T) then
16976 Set_Is_Limited_Interface (T);
16977 end if;
16978
16979 if not Is_Limited_Type (Parent_Type)
16980 and then
16981 (not Is_Interface (Parent_Type)
16982 or else not Is_Limited_Interface (Parent_Type))
16983 then
16984 -- AI05-0096: a derivation in the private part of an instance is
16985 -- legal if the generic formal is untagged limited, and the actual
16986 -- is non-limited.
16987
16988 if Is_Generic_Actual_Type (Parent_Type)
16989 and then In_Private_Part (Current_Scope)
16990 and then
16991 not Is_Tagged_Type
16992 (Generic_Parent_Type (Parent (Parent_Type)))
16993 then
16994 null;
16995
16996 else
16997 Error_Msg_NE
16998 ("parent type& of limited type must be limited",
16999 N, Parent_Type);
17000 end if;
17001 end if;
17002 end if;
17003
17004 -- In SPARK, there are no derived type definitions other than type
17005 -- extensions of tagged record types.
17006
17007 if No (Extension) then
17008 Check_SPARK_05_Restriction
17009 ("derived type is not allowed", Original_Node (N));
17010 end if;
17011 end Derived_Type_Declaration;
17012
17013 ------------------------
17014 -- Diagnose_Interface --
17015 ------------------------
17016
17017 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17018 begin
17019 if not Is_Interface (E) and then E /= Any_Type then
17020 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17021 end if;
17022 end Diagnose_Interface;
17023
17024 ----------------------------------
17025 -- Enumeration_Type_Declaration --
17026 ----------------------------------
17027
17028 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17029 Ev : Uint;
17030 L : Node_Id;
17031 R_Node : Node_Id;
17032 B_Node : Node_Id;
17033
17034 begin
17035 -- Create identifier node representing lower bound
17036
17037 B_Node := New_Node (N_Identifier, Sloc (Def));
17038 L := First (Literals (Def));
17039 Set_Chars (B_Node, Chars (L));
17040 Set_Entity (B_Node, L);
17041 Set_Etype (B_Node, T);
17042 Set_Is_Static_Expression (B_Node, True);
17043
17044 R_Node := New_Node (N_Range, Sloc (Def));
17045 Set_Low_Bound (R_Node, B_Node);
17046
17047 Set_Ekind (T, E_Enumeration_Type);
17048 Set_First_Literal (T, L);
17049 Set_Etype (T, T);
17050 Set_Is_Constrained (T);
17051
17052 Ev := Uint_0;
17053
17054 -- Loop through literals of enumeration type setting pos and rep values
17055 -- except that if the Ekind is already set, then it means the literal
17056 -- was already constructed (case of a derived type declaration and we
17057 -- should not disturb the Pos and Rep values.
17058
17059 while Present (L) loop
17060 if Ekind (L) /= E_Enumeration_Literal then
17061 Set_Ekind (L, E_Enumeration_Literal);
17062 Set_Enumeration_Pos (L, Ev);
17063 Set_Enumeration_Rep (L, Ev);
17064 Set_Is_Known_Valid (L, True);
17065 end if;
17066
17067 Set_Etype (L, T);
17068 New_Overloaded_Entity (L);
17069 Generate_Definition (L);
17070 Set_Convention (L, Convention_Intrinsic);
17071
17072 -- Case of character literal
17073
17074 if Nkind (L) = N_Defining_Character_Literal then
17075 Set_Is_Character_Type (T, True);
17076
17077 -- Check violation of No_Wide_Characters
17078
17079 if Restriction_Check_Required (No_Wide_Characters) then
17080 Get_Name_String (Chars (L));
17081
17082 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17083 Check_Restriction (No_Wide_Characters, L);
17084 end if;
17085 end if;
17086 end if;
17087
17088 Ev := Ev + 1;
17089 Next (L);
17090 end loop;
17091
17092 -- Now create a node representing upper bound
17093
17094 B_Node := New_Node (N_Identifier, Sloc (Def));
17095 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17096 Set_Entity (B_Node, Last (Literals (Def)));
17097 Set_Etype (B_Node, T);
17098 Set_Is_Static_Expression (B_Node, True);
17099
17100 Set_High_Bound (R_Node, B_Node);
17101
17102 -- Initialize various fields of the type. Some of this information
17103 -- may be overwritten later through rep.clauses.
17104
17105 Set_Scalar_Range (T, R_Node);
17106 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17107 Set_Enum_Esize (T);
17108 Set_Enum_Pos_To_Rep (T, Empty);
17109
17110 -- Set Discard_Names if configuration pragma set, or if there is
17111 -- a parameterless pragma in the current declarative region
17112
17113 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17114 Set_Discard_Names (T);
17115 end if;
17116
17117 -- Process end label if there is one
17118
17119 if Present (Def) then
17120 Process_End_Label (Def, 'e', T);
17121 end if;
17122 end Enumeration_Type_Declaration;
17123
17124 ---------------------------------
17125 -- Expand_To_Stored_Constraint --
17126 ---------------------------------
17127
17128 function Expand_To_Stored_Constraint
17129 (Typ : Entity_Id;
17130 Constraint : Elist_Id) return Elist_Id
17131 is
17132 Explicitly_Discriminated_Type : Entity_Id;
17133 Expansion : Elist_Id;
17134 Discriminant : Entity_Id;
17135
17136 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17137 -- Find the nearest type that actually specifies discriminants
17138
17139 ---------------------------------
17140 -- Type_With_Explicit_Discrims --
17141 ---------------------------------
17142
17143 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17144 Typ : constant E := Base_Type (Id);
17145
17146 begin
17147 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17148 if Present (Full_View (Typ)) then
17149 return Type_With_Explicit_Discrims (Full_View (Typ));
17150 end if;
17151
17152 else
17153 if Has_Discriminants (Typ) then
17154 return Typ;
17155 end if;
17156 end if;
17157
17158 if Etype (Typ) = Typ then
17159 return Empty;
17160 elsif Has_Discriminants (Typ) then
17161 return Typ;
17162 else
17163 return Type_With_Explicit_Discrims (Etype (Typ));
17164 end if;
17165
17166 end Type_With_Explicit_Discrims;
17167
17168 -- Start of processing for Expand_To_Stored_Constraint
17169
17170 begin
17171 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17172 return No_Elist;
17173 end if;
17174
17175 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17176
17177 if No (Explicitly_Discriminated_Type) then
17178 return No_Elist;
17179 end if;
17180
17181 Expansion := New_Elmt_List;
17182
17183 Discriminant :=
17184 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17185 while Present (Discriminant) loop
17186 Append_Elmt
17187 (Get_Discriminant_Value
17188 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17189 To => Expansion);
17190 Next_Stored_Discriminant (Discriminant);
17191 end loop;
17192
17193 return Expansion;
17194 end Expand_To_Stored_Constraint;
17195
17196 ---------------------------
17197 -- Find_Hidden_Interface --
17198 ---------------------------
17199
17200 function Find_Hidden_Interface
17201 (Src : Elist_Id;
17202 Dest : Elist_Id) return Entity_Id
17203 is
17204 Iface : Entity_Id;
17205 Iface_Elmt : Elmt_Id;
17206
17207 begin
17208 if Present (Src) and then Present (Dest) then
17209 Iface_Elmt := First_Elmt (Src);
17210 while Present (Iface_Elmt) loop
17211 Iface := Node (Iface_Elmt);
17212
17213 if Is_Interface (Iface)
17214 and then not Contain_Interface (Iface, Dest)
17215 then
17216 return Iface;
17217 end if;
17218
17219 Next_Elmt (Iface_Elmt);
17220 end loop;
17221 end if;
17222
17223 return Empty;
17224 end Find_Hidden_Interface;
17225
17226 --------------------
17227 -- Find_Type_Name --
17228 --------------------
17229
17230 function Find_Type_Name (N : Node_Id) return Entity_Id is
17231 Id : constant Entity_Id := Defining_Identifier (N);
17232 New_Id : Entity_Id;
17233 Prev : Entity_Id;
17234 Prev_Par : Node_Id;
17235
17236 procedure Check_Duplicate_Aspects;
17237 -- Check that aspects specified in a completion have not been specified
17238 -- already in the partial view.
17239
17240 procedure Tag_Mismatch;
17241 -- Diagnose a tagged partial view whose full view is untagged. We post
17242 -- the message on the full view, with a reference to the previous
17243 -- partial view. The partial view can be private or incomplete, and
17244 -- these are handled in a different manner, so we determine the position
17245 -- of the error message from the respective slocs of both.
17246
17247 -----------------------------
17248 -- Check_Duplicate_Aspects --
17249 -----------------------------
17250
17251 procedure Check_Duplicate_Aspects is
17252 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17253 -- Return the corresponding aspect of the partial view which matches
17254 -- the aspect id of Asp. Return Empty is no such aspect exists.
17255
17256 -----------------------------
17257 -- Get_Partial_View_Aspect --
17258 -----------------------------
17259
17260 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17261 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17262 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17263 Prev_Asp : Node_Id;
17264
17265 begin
17266 if Present (Prev_Asps) then
17267 Prev_Asp := First (Prev_Asps);
17268 while Present (Prev_Asp) loop
17269 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17270 return Prev_Asp;
17271 end if;
17272
17273 Next (Prev_Asp);
17274 end loop;
17275 end if;
17276
17277 return Empty;
17278 end Get_Partial_View_Aspect;
17279
17280 -- Local variables
17281
17282 Full_Asps : constant List_Id := Aspect_Specifications (N);
17283 Full_Asp : Node_Id;
17284 Part_Asp : Node_Id;
17285
17286 -- Start of processing for Check_Duplicate_Aspects
17287
17288 begin
17289 if Present (Full_Asps) then
17290 Full_Asp := First (Full_Asps);
17291 while Present (Full_Asp) loop
17292 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17293
17294 -- An aspect and its class-wide counterpart are two distinct
17295 -- aspects and may apply to both views of an entity.
17296
17297 if Present (Part_Asp)
17298 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17299 then
17300 Error_Msg_N
17301 ("aspect already specified in private declaration",
17302 Full_Asp);
17303
17304 Remove (Full_Asp);
17305 return;
17306 end if;
17307
17308 if Has_Discriminants (Prev)
17309 and then not Has_Unknown_Discriminants (Prev)
17310 and then Get_Aspect_Id (Full_Asp) =
17311 Aspect_Implicit_Dereference
17312 then
17313 Error_Msg_N
17314 ("cannot specify aspect if partial view has known "
17315 & "discriminants", Full_Asp);
17316 end if;
17317
17318 Next (Full_Asp);
17319 end loop;
17320 end if;
17321 end Check_Duplicate_Aspects;
17322
17323 ------------------
17324 -- Tag_Mismatch --
17325 ------------------
17326
17327 procedure Tag_Mismatch is
17328 begin
17329 if Sloc (Prev) < Sloc (Id) then
17330 if Ada_Version >= Ada_2012
17331 and then Nkind (N) = N_Private_Type_Declaration
17332 then
17333 Error_Msg_NE
17334 ("declaration of private } must be a tagged type ", Id, Prev);
17335 else
17336 Error_Msg_NE
17337 ("full declaration of } must be a tagged type ", Id, Prev);
17338 end if;
17339
17340 else
17341 if Ada_Version >= Ada_2012
17342 and then Nkind (N) = N_Private_Type_Declaration
17343 then
17344 Error_Msg_NE
17345 ("declaration of private } must be a tagged type ", Prev, Id);
17346 else
17347 Error_Msg_NE
17348 ("full declaration of } must be a tagged type ", Prev, Id);
17349 end if;
17350 end if;
17351 end Tag_Mismatch;
17352
17353 -- Start of processing for Find_Type_Name
17354
17355 begin
17356 -- Find incomplete declaration, if one was given
17357
17358 Prev := Current_Entity_In_Scope (Id);
17359
17360 -- New type declaration
17361
17362 if No (Prev) then
17363 Enter_Name (Id);
17364 return Id;
17365
17366 -- Previous declaration exists
17367
17368 else
17369 Prev_Par := Parent (Prev);
17370
17371 -- Error if not incomplete/private case except if previous
17372 -- declaration is implicit, etc. Enter_Name will emit error if
17373 -- appropriate.
17374
17375 if not Is_Incomplete_Or_Private_Type (Prev) then
17376 Enter_Name (Id);
17377 New_Id := Id;
17378
17379 -- Check invalid completion of private or incomplete type
17380
17381 elsif not Nkind_In (N, N_Full_Type_Declaration,
17382 N_Task_Type_Declaration,
17383 N_Protected_Type_Declaration)
17384 and then
17385 (Ada_Version < Ada_2012
17386 or else not Is_Incomplete_Type (Prev)
17387 or else not Nkind_In (N, N_Private_Type_Declaration,
17388 N_Private_Extension_Declaration))
17389 then
17390 -- Completion must be a full type declarations (RM 7.3(4))
17391
17392 Error_Msg_Sloc := Sloc (Prev);
17393 Error_Msg_NE ("invalid completion of }", Id, Prev);
17394
17395 -- Set scope of Id to avoid cascaded errors. Entity is never
17396 -- examined again, except when saving globals in generics.
17397
17398 Set_Scope (Id, Current_Scope);
17399 New_Id := Id;
17400
17401 -- If this is a repeated incomplete declaration, no further
17402 -- checks are possible.
17403
17404 if Nkind (N) = N_Incomplete_Type_Declaration then
17405 return Prev;
17406 end if;
17407
17408 -- Case of full declaration of incomplete type
17409
17410 elsif Ekind (Prev) = E_Incomplete_Type
17411 and then (Ada_Version < Ada_2012
17412 or else No (Full_View (Prev))
17413 or else not Is_Private_Type (Full_View (Prev)))
17414 then
17415 -- Indicate that the incomplete declaration has a matching full
17416 -- declaration. The defining occurrence of the incomplete
17417 -- declaration remains the visible one, and the procedure
17418 -- Get_Full_View dereferences it whenever the type is used.
17419
17420 if Present (Full_View (Prev)) then
17421 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17422 end if;
17423
17424 Set_Full_View (Prev, Id);
17425 Append_Entity (Id, Current_Scope);
17426 Set_Is_Public (Id, Is_Public (Prev));
17427 Set_Is_Internal (Id);
17428 New_Id := Prev;
17429
17430 -- If the incomplete view is tagged, a class_wide type has been
17431 -- created already. Use it for the private type as well, in order
17432 -- to prevent multiple incompatible class-wide types that may be
17433 -- created for self-referential anonymous access components.
17434
17435 if Is_Tagged_Type (Prev)
17436 and then Present (Class_Wide_Type (Prev))
17437 then
17438 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17439 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17440
17441 -- Type of the class-wide type is the current Id. Previously
17442 -- this was not done for private declarations because of order-
17443 -- of-elaboration issues in the back end, but gigi now handles
17444 -- this properly.
17445
17446 Set_Etype (Class_Wide_Type (Id), Id);
17447 end if;
17448
17449 -- Case of full declaration of private type
17450
17451 else
17452 -- If the private type was a completion of an incomplete type then
17453 -- update Prev to reference the private type
17454
17455 if Ada_Version >= Ada_2012
17456 and then Ekind (Prev) = E_Incomplete_Type
17457 and then Present (Full_View (Prev))
17458 and then Is_Private_Type (Full_View (Prev))
17459 then
17460 Prev := Full_View (Prev);
17461 Prev_Par := Parent (Prev);
17462 end if;
17463
17464 if Nkind (N) = N_Full_Type_Declaration
17465 and then Nkind_In
17466 (Type_Definition (N), N_Record_Definition,
17467 N_Derived_Type_Definition)
17468 and then Interface_Present (Type_Definition (N))
17469 then
17470 Error_Msg_N
17471 ("completion of private type cannot be an interface", N);
17472 end if;
17473
17474 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17475 if Etype (Prev) /= Prev then
17476
17477 -- Prev is a private subtype or a derived type, and needs
17478 -- no completion.
17479
17480 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17481 New_Id := Id;
17482
17483 elsif Ekind (Prev) = E_Private_Type
17484 and then Nkind_In (N, N_Task_Type_Declaration,
17485 N_Protected_Type_Declaration)
17486 then
17487 Error_Msg_N
17488 ("completion of nonlimited type cannot be limited", N);
17489
17490 elsif Ekind (Prev) = E_Record_Type_With_Private
17491 and then Nkind_In (N, N_Task_Type_Declaration,
17492 N_Protected_Type_Declaration)
17493 then
17494 if not Is_Limited_Record (Prev) then
17495 Error_Msg_N
17496 ("completion of nonlimited type cannot be limited", N);
17497
17498 elsif No (Interface_List (N)) then
17499 Error_Msg_N
17500 ("completion of tagged private type must be tagged",
17501 N);
17502 end if;
17503 end if;
17504
17505 -- Ada 2005 (AI-251): Private extension declaration of a task
17506 -- type or a protected type. This case arises when covering
17507 -- interface types.
17508
17509 elsif Nkind_In (N, N_Task_Type_Declaration,
17510 N_Protected_Type_Declaration)
17511 then
17512 null;
17513
17514 elsif Nkind (N) /= N_Full_Type_Declaration
17515 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17516 then
17517 Error_Msg_N
17518 ("full view of private extension must be an extension", N);
17519
17520 elsif not (Abstract_Present (Parent (Prev)))
17521 and then Abstract_Present (Type_Definition (N))
17522 then
17523 Error_Msg_N
17524 ("full view of non-abstract extension cannot be abstract", N);
17525 end if;
17526
17527 if not In_Private_Part (Current_Scope) then
17528 Error_Msg_N
17529 ("declaration of full view must appear in private part", N);
17530 end if;
17531
17532 if Ada_Version >= Ada_2012 then
17533 Check_Duplicate_Aspects;
17534 end if;
17535
17536 Copy_And_Swap (Prev, Id);
17537 Set_Has_Private_Declaration (Prev);
17538 Set_Has_Private_Declaration (Id);
17539
17540 -- AI12-0133: Indicate whether we have a partial view with
17541 -- unknown discriminants, in which case initialization of objects
17542 -- of the type do not receive an invariant check.
17543
17544 Set_Partial_View_Has_Unknown_Discr
17545 (Prev, Has_Unknown_Discriminants (Id));
17546
17547 -- Preserve aspect and iterator flags that may have been set on
17548 -- the partial view.
17549
17550 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17551 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17552
17553 -- If no error, propagate freeze_node from private to full view.
17554 -- It may have been generated for an early operational item.
17555
17556 if Present (Freeze_Node (Id))
17557 and then Serious_Errors_Detected = 0
17558 and then No (Full_View (Id))
17559 then
17560 Set_Freeze_Node (Prev, Freeze_Node (Id));
17561 Set_Freeze_Node (Id, Empty);
17562 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17563 end if;
17564
17565 Set_Full_View (Id, Prev);
17566 New_Id := Prev;
17567 end if;
17568
17569 -- Verify that full declaration conforms to partial one
17570
17571 if Is_Incomplete_Or_Private_Type (Prev)
17572 and then Present (Discriminant_Specifications (Prev_Par))
17573 then
17574 if Present (Discriminant_Specifications (N)) then
17575 if Ekind (Prev) = E_Incomplete_Type then
17576 Check_Discriminant_Conformance (N, Prev, Prev);
17577 else
17578 Check_Discriminant_Conformance (N, Prev, Id);
17579 end if;
17580
17581 else
17582 Error_Msg_N
17583 ("missing discriminants in full type declaration", N);
17584
17585 -- To avoid cascaded errors on subsequent use, share the
17586 -- discriminants of the partial view.
17587
17588 Set_Discriminant_Specifications (N,
17589 Discriminant_Specifications (Prev_Par));
17590 end if;
17591 end if;
17592
17593 -- A prior untagged partial view can have an associated class-wide
17594 -- type due to use of the class attribute, and in this case the full
17595 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17596 -- of incomplete tagged declarations, but we check for it.
17597
17598 if Is_Type (Prev)
17599 and then (Is_Tagged_Type (Prev)
17600 or else Present (Class_Wide_Type (Prev)))
17601 then
17602 -- Ada 2012 (AI05-0162): A private type may be the completion of
17603 -- an incomplete type.
17604
17605 if Ada_Version >= Ada_2012
17606 and then Is_Incomplete_Type (Prev)
17607 and then Nkind_In (N, N_Private_Type_Declaration,
17608 N_Private_Extension_Declaration)
17609 then
17610 -- No need to check private extensions since they are tagged
17611
17612 if Nkind (N) = N_Private_Type_Declaration
17613 and then not Tagged_Present (N)
17614 then
17615 Tag_Mismatch;
17616 end if;
17617
17618 -- The full declaration is either a tagged type (including
17619 -- a synchronized type that implements interfaces) or a
17620 -- type extension, otherwise this is an error.
17621
17622 elsif Nkind_In (N, N_Task_Type_Declaration,
17623 N_Protected_Type_Declaration)
17624 then
17625 if No (Interface_List (N)) and then not Error_Posted (N) then
17626 Tag_Mismatch;
17627 end if;
17628
17629 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17630
17631 -- Indicate that the previous declaration (tagged incomplete
17632 -- or private declaration) requires the same on the full one.
17633
17634 if not Tagged_Present (Type_Definition (N)) then
17635 Tag_Mismatch;
17636 Set_Is_Tagged_Type (Id);
17637 end if;
17638
17639 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17640 if No (Record_Extension_Part (Type_Definition (N))) then
17641 Error_Msg_NE
17642 ("full declaration of } must be a record extension",
17643 Prev, Id);
17644
17645 -- Set some attributes to produce a usable full view
17646
17647 Set_Is_Tagged_Type (Id);
17648 end if;
17649
17650 else
17651 Tag_Mismatch;
17652 end if;
17653 end if;
17654
17655 if Present (Prev)
17656 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17657 and then Present (Premature_Use (Parent (Prev)))
17658 then
17659 Error_Msg_Sloc := Sloc (N);
17660 Error_Msg_N
17661 ("\full declaration #", Premature_Use (Parent (Prev)));
17662 end if;
17663
17664 return New_Id;
17665 end if;
17666 end Find_Type_Name;
17667
17668 -------------------------
17669 -- Find_Type_Of_Object --
17670 -------------------------
17671
17672 function Find_Type_Of_Object
17673 (Obj_Def : Node_Id;
17674 Related_Nod : Node_Id) return Entity_Id
17675 is
17676 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17677 P : Node_Id := Parent (Obj_Def);
17678 T : Entity_Id;
17679 Nam : Name_Id;
17680
17681 begin
17682 -- If the parent is a component_definition node we climb to the
17683 -- component_declaration node
17684
17685 if Nkind (P) = N_Component_Definition then
17686 P := Parent (P);
17687 end if;
17688
17689 -- Case of an anonymous array subtype
17690
17691 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17692 N_Unconstrained_Array_Definition)
17693 then
17694 T := Empty;
17695 Array_Type_Declaration (T, Obj_Def);
17696
17697 -- Create an explicit subtype whenever possible
17698
17699 elsif Nkind (P) /= N_Component_Declaration
17700 and then Def_Kind = N_Subtype_Indication
17701 then
17702 -- Base name of subtype on object name, which will be unique in
17703 -- the current scope.
17704
17705 -- If this is a duplicate declaration, return base type, to avoid
17706 -- generating duplicate anonymous types.
17707
17708 if Error_Posted (P) then
17709 Analyze (Subtype_Mark (Obj_Def));
17710 return Entity (Subtype_Mark (Obj_Def));
17711 end if;
17712
17713 Nam :=
17714 New_External_Name
17715 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17716
17717 T := Make_Defining_Identifier (Sloc (P), Nam);
17718
17719 Insert_Action (Obj_Def,
17720 Make_Subtype_Declaration (Sloc (P),
17721 Defining_Identifier => T,
17722 Subtype_Indication => Relocate_Node (Obj_Def)));
17723
17724 -- This subtype may need freezing, and this will not be done
17725 -- automatically if the object declaration is not in declarative
17726 -- part. Since this is an object declaration, the type cannot always
17727 -- be frozen here. Deferred constants do not freeze their type
17728 -- (which often enough will be private).
17729
17730 if Nkind (P) = N_Object_Declaration
17731 and then Constant_Present (P)
17732 and then No (Expression (P))
17733 then
17734 null;
17735
17736 -- Here we freeze the base type of object type to catch premature use
17737 -- of discriminated private type without a full view.
17738
17739 else
17740 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17741 end if;
17742
17743 -- Ada 2005 AI-406: the object definition in an object declaration
17744 -- can be an access definition.
17745
17746 elsif Def_Kind = N_Access_Definition then
17747 T := Access_Definition (Related_Nod, Obj_Def);
17748
17749 Set_Is_Local_Anonymous_Access
17750 (T,
17751 V => (Ada_Version < Ada_2012)
17752 or else (Nkind (P) /= N_Object_Declaration)
17753 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17754
17755 -- Otherwise, the object definition is just a subtype_mark
17756
17757 else
17758 T := Process_Subtype (Obj_Def, Related_Nod);
17759
17760 -- If expansion is disabled an object definition that is an aggregate
17761 -- will not get expanded and may lead to scoping problems in the back
17762 -- end, if the object is referenced in an inner scope. In that case
17763 -- create an itype reference for the object definition now. This
17764 -- may be redundant in some cases, but harmless.
17765
17766 if Is_Itype (T)
17767 and then Nkind (Related_Nod) = N_Object_Declaration
17768 and then ASIS_Mode
17769 then
17770 Build_Itype_Reference (T, Related_Nod);
17771 end if;
17772 end if;
17773
17774 return T;
17775 end Find_Type_Of_Object;
17776
17777 --------------------------------
17778 -- Find_Type_Of_Subtype_Indic --
17779 --------------------------------
17780
17781 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17782 Typ : Entity_Id;
17783
17784 begin
17785 -- Case of subtype mark with a constraint
17786
17787 if Nkind (S) = N_Subtype_Indication then
17788 Find_Type (Subtype_Mark (S));
17789 Typ := Entity (Subtype_Mark (S));
17790
17791 if not
17792 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17793 then
17794 Error_Msg_N
17795 ("incorrect constraint for this kind of type", Constraint (S));
17796 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17797 end if;
17798
17799 -- Otherwise we have a subtype mark without a constraint
17800
17801 elsif Error_Posted (S) then
17802 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17803 return Any_Type;
17804
17805 else
17806 Find_Type (S);
17807 Typ := Entity (S);
17808 end if;
17809
17810 -- Check No_Wide_Characters restriction
17811
17812 Check_Wide_Character_Restriction (Typ, S);
17813
17814 return Typ;
17815 end Find_Type_Of_Subtype_Indic;
17816
17817 -------------------------------------
17818 -- Floating_Point_Type_Declaration --
17819 -------------------------------------
17820
17821 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17822 Digs : constant Node_Id := Digits_Expression (Def);
17823 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17824 Digs_Val : Uint;
17825 Base_Typ : Entity_Id;
17826 Implicit_Base : Entity_Id;
17827
17828 function Can_Derive_From (E : Entity_Id) return Boolean;
17829 -- Find if given digits value, and possibly a specified range, allows
17830 -- derivation from specified type
17831
17832 procedure Convert_Bound (B : Node_Id);
17833 -- If specified, the bounds must be static but may be of different
17834 -- types. They must be converted into machine numbers of the base type,
17835 -- in accordance with RM 4.9(38).
17836
17837 function Find_Base_Type return Entity_Id;
17838 -- Find a predefined base type that Def can derive from, or generate
17839 -- an error and substitute Long_Long_Float if none exists.
17840
17841 ---------------------
17842 -- Can_Derive_From --
17843 ---------------------
17844
17845 function Can_Derive_From (E : Entity_Id) return Boolean is
17846 Spec : constant Entity_Id := Real_Range_Specification (Def);
17847
17848 begin
17849 -- Check specified "digits" constraint
17850
17851 if Digs_Val > Digits_Value (E) then
17852 return False;
17853 end if;
17854
17855 -- Check for matching range, if specified
17856
17857 if Present (Spec) then
17858 if Expr_Value_R (Type_Low_Bound (E)) >
17859 Expr_Value_R (Low_Bound (Spec))
17860 then
17861 return False;
17862 end if;
17863
17864 if Expr_Value_R (Type_High_Bound (E)) <
17865 Expr_Value_R (High_Bound (Spec))
17866 then
17867 return False;
17868 end if;
17869 end if;
17870
17871 return True;
17872 end Can_Derive_From;
17873
17874 -------------------
17875 -- Convert_Bound --
17876 --------------------
17877
17878 procedure Convert_Bound (B : Node_Id) is
17879 begin
17880 -- If the bound is not a literal it can only be static if it is
17881 -- a static constant, possibly of a specified type.
17882
17883 if Is_Entity_Name (B)
17884 and then Ekind (Entity (B)) = E_Constant
17885 then
17886 Rewrite (B, Constant_Value (Entity (B)));
17887 end if;
17888
17889 if Nkind (B) = N_Real_Literal then
17890 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17891 Set_Is_Machine_Number (B);
17892 Set_Etype (B, Base_Typ);
17893 end if;
17894 end Convert_Bound;
17895
17896 --------------------
17897 -- Find_Base_Type --
17898 --------------------
17899
17900 function Find_Base_Type return Entity_Id is
17901 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17902
17903 begin
17904 -- Iterate over the predefined types in order, returning the first
17905 -- one that Def can derive from.
17906
17907 while Present (Choice) loop
17908 if Can_Derive_From (Node (Choice)) then
17909 return Node (Choice);
17910 end if;
17911
17912 Next_Elmt (Choice);
17913 end loop;
17914
17915 -- If we can't derive from any existing type, use Long_Long_Float
17916 -- and give appropriate message explaining the problem.
17917
17918 if Digs_Val > Max_Digs_Val then
17919 -- It might be the case that there is a type with the requested
17920 -- range, just not the combination of digits and range.
17921
17922 Error_Msg_N
17923 ("no predefined type has requested range and precision",
17924 Real_Range_Specification (Def));
17925
17926 else
17927 Error_Msg_N
17928 ("range too large for any predefined type",
17929 Real_Range_Specification (Def));
17930 end if;
17931
17932 return Standard_Long_Long_Float;
17933 end Find_Base_Type;
17934
17935 -- Start of processing for Floating_Point_Type_Declaration
17936
17937 begin
17938 Check_Restriction (No_Floating_Point, Def);
17939
17940 -- Create an implicit base type
17941
17942 Implicit_Base :=
17943 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17944
17945 -- Analyze and verify digits value
17946
17947 Analyze_And_Resolve (Digs, Any_Integer);
17948 Check_Digits_Expression (Digs);
17949 Digs_Val := Expr_Value (Digs);
17950
17951 -- Process possible range spec and find correct type to derive from
17952
17953 Process_Real_Range_Specification (Def);
17954
17955 -- Check that requested number of digits is not too high.
17956
17957 if Digs_Val > Max_Digs_Val then
17958
17959 -- The check for Max_Base_Digits may be somewhat expensive, as it
17960 -- requires reading System, so only do it when necessary.
17961
17962 declare
17963 Max_Base_Digits : constant Uint :=
17964 Expr_Value
17965 (Expression
17966 (Parent (RTE (RE_Max_Base_Digits))));
17967
17968 begin
17969 if Digs_Val > Max_Base_Digits then
17970 Error_Msg_Uint_1 := Max_Base_Digits;
17971 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17972
17973 elsif No (Real_Range_Specification (Def)) then
17974 Error_Msg_Uint_1 := Max_Digs_Val;
17975 Error_Msg_N ("types with more than ^ digits need range spec "
17976 & "(RM 3.5.7(6))", Digs);
17977 end if;
17978 end;
17979 end if;
17980
17981 -- Find a suitable type to derive from or complain and use a substitute
17982
17983 Base_Typ := Find_Base_Type;
17984
17985 -- If there are bounds given in the declaration use them as the bounds
17986 -- of the type, otherwise use the bounds of the predefined base type
17987 -- that was chosen based on the Digits value.
17988
17989 if Present (Real_Range_Specification (Def)) then
17990 Set_Scalar_Range (T, Real_Range_Specification (Def));
17991 Set_Is_Constrained (T);
17992
17993 Convert_Bound (Type_Low_Bound (T));
17994 Convert_Bound (Type_High_Bound (T));
17995
17996 else
17997 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17998 end if;
17999
18000 -- Complete definition of implicit base and declared first subtype. The
18001 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18002 -- are not clobbered when the floating point type acts as a full view of
18003 -- a private type.
18004
18005 Set_Etype (Implicit_Base, Base_Typ);
18006 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18007 Set_Size_Info (Implicit_Base, Base_Typ);
18008 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18009 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18010 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18011 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18012
18013 Set_Ekind (T, E_Floating_Point_Subtype);
18014 Set_Etype (T, Implicit_Base);
18015 Set_Size_Info (T, Implicit_Base);
18016 Set_RM_Size (T, RM_Size (Implicit_Base));
18017 Inherit_Rep_Item_Chain (T, Implicit_Base);
18018 Set_Digits_Value (T, Digs_Val);
18019 end Floating_Point_Type_Declaration;
18020
18021 ----------------------------
18022 -- Get_Discriminant_Value --
18023 ----------------------------
18024
18025 -- This is the situation:
18026
18027 -- There is a non-derived type
18028
18029 -- type T0 (Dx, Dy, Dz...)
18030
18031 -- There are zero or more levels of derivation, with each derivation
18032 -- either purely inheriting the discriminants, or defining its own.
18033
18034 -- type Ti is new Ti-1
18035 -- or
18036 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18037 -- or
18038 -- subtype Ti is ...
18039
18040 -- The subtype issue is avoided by the use of Original_Record_Component,
18041 -- and the fact that derived subtypes also derive the constraints.
18042
18043 -- This chain leads back from
18044
18045 -- Typ_For_Constraint
18046
18047 -- Typ_For_Constraint has discriminants, and the value for each
18048 -- discriminant is given by its corresponding Elmt of Constraints.
18049
18050 -- Discriminant is some discriminant in this hierarchy
18051
18052 -- We need to return its value
18053
18054 -- We do this by recursively searching each level, and looking for
18055 -- Discriminant. Once we get to the bottom, we start backing up
18056 -- returning the value for it which may in turn be a discriminant
18057 -- further up, so on the backup we continue the substitution.
18058
18059 function Get_Discriminant_Value
18060 (Discriminant : Entity_Id;
18061 Typ_For_Constraint : Entity_Id;
18062 Constraint : Elist_Id) return Node_Id
18063 is
18064 function Root_Corresponding_Discriminant
18065 (Discr : Entity_Id) return Entity_Id;
18066 -- Given a discriminant, traverse the chain of inherited discriminants
18067 -- and return the topmost discriminant.
18068
18069 function Search_Derivation_Levels
18070 (Ti : Entity_Id;
18071 Discrim_Values : Elist_Id;
18072 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18073 -- This is the routine that performs the recursive search of levels
18074 -- as described above.
18075
18076 -------------------------------------
18077 -- Root_Corresponding_Discriminant --
18078 -------------------------------------
18079
18080 function Root_Corresponding_Discriminant
18081 (Discr : Entity_Id) return Entity_Id
18082 is
18083 D : Entity_Id;
18084
18085 begin
18086 D := Discr;
18087 while Present (Corresponding_Discriminant (D)) loop
18088 D := Corresponding_Discriminant (D);
18089 end loop;
18090
18091 return D;
18092 end Root_Corresponding_Discriminant;
18093
18094 ------------------------------
18095 -- Search_Derivation_Levels --
18096 ------------------------------
18097
18098 function Search_Derivation_Levels
18099 (Ti : Entity_Id;
18100 Discrim_Values : Elist_Id;
18101 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18102 is
18103 Assoc : Elmt_Id;
18104 Disc : Entity_Id;
18105 Result : Node_Or_Entity_Id;
18106 Result_Entity : Node_Id;
18107
18108 begin
18109 -- If inappropriate type, return Error, this happens only in
18110 -- cascaded error situations, and we want to avoid a blow up.
18111
18112 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18113 return Error;
18114 end if;
18115
18116 -- Look deeper if possible. Use Stored_Constraints only for
18117 -- untagged types. For tagged types use the given constraint.
18118 -- This asymmetry needs explanation???
18119
18120 if not Stored_Discrim_Values
18121 and then Present (Stored_Constraint (Ti))
18122 and then not Is_Tagged_Type (Ti)
18123 then
18124 Result :=
18125 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18126
18127 else
18128 declare
18129 Td : Entity_Id := Etype (Ti);
18130
18131 begin
18132 -- If the parent type is private, the full view may include
18133 -- renamed discriminants, and it is those stored values that
18134 -- may be needed (the partial view never has more information
18135 -- than the full view).
18136
18137 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18138 Td := Full_View (Td);
18139 end if;
18140
18141 if Td = Ti then
18142 Result := Discriminant;
18143
18144 else
18145 if Present (Stored_Constraint (Ti)) then
18146 Result :=
18147 Search_Derivation_Levels
18148 (Td, Stored_Constraint (Ti), True);
18149 else
18150 Result :=
18151 Search_Derivation_Levels
18152 (Td, Discrim_Values, Stored_Discrim_Values);
18153 end if;
18154 end if;
18155 end;
18156 end if;
18157
18158 -- Extra underlying places to search, if not found above. For
18159 -- concurrent types, the relevant discriminant appears in the
18160 -- corresponding record. For a type derived from a private type
18161 -- without discriminant, the full view inherits the discriminants
18162 -- of the full view of the parent.
18163
18164 if Result = Discriminant then
18165 if Is_Concurrent_Type (Ti)
18166 and then Present (Corresponding_Record_Type (Ti))
18167 then
18168 Result :=
18169 Search_Derivation_Levels (
18170 Corresponding_Record_Type (Ti),
18171 Discrim_Values,
18172 Stored_Discrim_Values);
18173
18174 elsif Is_Private_Type (Ti)
18175 and then not Has_Discriminants (Ti)
18176 and then Present (Full_View (Ti))
18177 and then Etype (Full_View (Ti)) /= Ti
18178 then
18179 Result :=
18180 Search_Derivation_Levels (
18181 Full_View (Ti),
18182 Discrim_Values,
18183 Stored_Discrim_Values);
18184 end if;
18185 end if;
18186
18187 -- If Result is not a (reference to a) discriminant, return it,
18188 -- otherwise set Result_Entity to the discriminant.
18189
18190 if Nkind (Result) = N_Defining_Identifier then
18191 pragma Assert (Result = Discriminant);
18192 Result_Entity := Result;
18193
18194 else
18195 if not Denotes_Discriminant (Result) then
18196 return Result;
18197 end if;
18198
18199 Result_Entity := Entity (Result);
18200 end if;
18201
18202 -- See if this level of derivation actually has discriminants because
18203 -- tagged derivations can add them, hence the lower levels need not
18204 -- have any.
18205
18206 if not Has_Discriminants (Ti) then
18207 return Result;
18208 end if;
18209
18210 -- Scan Ti's discriminants for Result_Entity, and return its
18211 -- corresponding value, if any.
18212
18213 Result_Entity := Original_Record_Component (Result_Entity);
18214
18215 Assoc := First_Elmt (Discrim_Values);
18216
18217 if Stored_Discrim_Values then
18218 Disc := First_Stored_Discriminant (Ti);
18219 else
18220 Disc := First_Discriminant (Ti);
18221 end if;
18222
18223 while Present (Disc) loop
18224
18225 -- If no further associations return the discriminant, value will
18226 -- be found on the second pass.
18227
18228 if No (Assoc) then
18229 return Result;
18230 end if;
18231
18232 if Original_Record_Component (Disc) = Result_Entity then
18233 return Node (Assoc);
18234 end if;
18235
18236 Next_Elmt (Assoc);
18237
18238 if Stored_Discrim_Values then
18239 Next_Stored_Discriminant (Disc);
18240 else
18241 Next_Discriminant (Disc);
18242 end if;
18243 end loop;
18244
18245 -- Could not find it
18246
18247 return Result;
18248 end Search_Derivation_Levels;
18249
18250 -- Local Variables
18251
18252 Result : Node_Or_Entity_Id;
18253
18254 -- Start of processing for Get_Discriminant_Value
18255
18256 begin
18257 -- ??? This routine is a gigantic mess and will be deleted. For the
18258 -- time being just test for the trivial case before calling recurse.
18259
18260 -- We are now celebrating the 20th anniversary of this comment!
18261
18262 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18263 declare
18264 D : Entity_Id;
18265 E : Elmt_Id;
18266
18267 begin
18268 D := First_Discriminant (Typ_For_Constraint);
18269 E := First_Elmt (Constraint);
18270 while Present (D) loop
18271 if Chars (D) = Chars (Discriminant) then
18272 return Node (E);
18273 end if;
18274
18275 Next_Discriminant (D);
18276 Next_Elmt (E);
18277 end loop;
18278 end;
18279 end if;
18280
18281 Result := Search_Derivation_Levels
18282 (Typ_For_Constraint, Constraint, False);
18283
18284 -- ??? hack to disappear when this routine is gone
18285
18286 if Nkind (Result) = N_Defining_Identifier then
18287 declare
18288 D : Entity_Id;
18289 E : Elmt_Id;
18290
18291 begin
18292 D := First_Discriminant (Typ_For_Constraint);
18293 E := First_Elmt (Constraint);
18294 while Present (D) loop
18295 if Root_Corresponding_Discriminant (D) = Discriminant then
18296 return Node (E);
18297 end if;
18298
18299 Next_Discriminant (D);
18300 Next_Elmt (E);
18301 end loop;
18302 end;
18303 end if;
18304
18305 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18306 return Result;
18307 end Get_Discriminant_Value;
18308
18309 --------------------------
18310 -- Has_Range_Constraint --
18311 --------------------------
18312
18313 function Has_Range_Constraint (N : Node_Id) return Boolean is
18314 C : constant Node_Id := Constraint (N);
18315
18316 begin
18317 if Nkind (C) = N_Range_Constraint then
18318 return True;
18319
18320 elsif Nkind (C) = N_Digits_Constraint then
18321 return
18322 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18323 or else Present (Range_Constraint (C));
18324
18325 elsif Nkind (C) = N_Delta_Constraint then
18326 return Present (Range_Constraint (C));
18327
18328 else
18329 return False;
18330 end if;
18331 end Has_Range_Constraint;
18332
18333 ------------------------
18334 -- Inherit_Components --
18335 ------------------------
18336
18337 function Inherit_Components
18338 (N : Node_Id;
18339 Parent_Base : Entity_Id;
18340 Derived_Base : Entity_Id;
18341 Is_Tagged : Boolean;
18342 Inherit_Discr : Boolean;
18343 Discs : Elist_Id) return Elist_Id
18344 is
18345 Assoc_List : constant Elist_Id := New_Elmt_List;
18346
18347 procedure Inherit_Component
18348 (Old_C : Entity_Id;
18349 Plain_Discrim : Boolean := False;
18350 Stored_Discrim : Boolean := False);
18351 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18352 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18353 -- True, Old_C is a stored discriminant. If they are both false then
18354 -- Old_C is a regular component.
18355
18356 -----------------------
18357 -- Inherit_Component --
18358 -----------------------
18359
18360 procedure Inherit_Component
18361 (Old_C : Entity_Id;
18362 Plain_Discrim : Boolean := False;
18363 Stored_Discrim : Boolean := False)
18364 is
18365 procedure Set_Anonymous_Type (Id : Entity_Id);
18366 -- Id denotes the entity of an access discriminant or anonymous
18367 -- access component. Set the type of Id to either the same type of
18368 -- Old_C or create a new one depending on whether the parent and
18369 -- the child types are in the same scope.
18370
18371 ------------------------
18372 -- Set_Anonymous_Type --
18373 ------------------------
18374
18375 procedure Set_Anonymous_Type (Id : Entity_Id) is
18376 Old_Typ : constant Entity_Id := Etype (Old_C);
18377
18378 begin
18379 if Scope (Parent_Base) = Scope (Derived_Base) then
18380 Set_Etype (Id, Old_Typ);
18381
18382 -- The parent and the derived type are in two different scopes.
18383 -- Reuse the type of the original discriminant / component by
18384 -- copying it in order to preserve all attributes.
18385
18386 else
18387 declare
18388 Typ : constant Entity_Id := New_Copy (Old_Typ);
18389
18390 begin
18391 Set_Etype (Id, Typ);
18392
18393 -- Since we do not generate component declarations for
18394 -- inherited components, associate the itype with the
18395 -- derived type.
18396
18397 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18398 Set_Scope (Typ, Derived_Base);
18399 end;
18400 end if;
18401 end Set_Anonymous_Type;
18402
18403 -- Local variables and constants
18404
18405 New_C : constant Entity_Id := New_Copy (Old_C);
18406
18407 Corr_Discrim : Entity_Id;
18408 Discrim : Entity_Id;
18409
18410 -- Start of processing for Inherit_Component
18411
18412 begin
18413 pragma Assert (not Is_Tagged or not Stored_Discrim);
18414
18415 Set_Parent (New_C, Parent (Old_C));
18416
18417 -- Regular discriminants and components must be inserted in the scope
18418 -- of the Derived_Base. Do it here.
18419
18420 if not Stored_Discrim then
18421 Enter_Name (New_C);
18422 end if;
18423
18424 -- For tagged types the Original_Record_Component must point to
18425 -- whatever this field was pointing to in the parent type. This has
18426 -- already been achieved by the call to New_Copy above.
18427
18428 if not Is_Tagged then
18429 Set_Original_Record_Component (New_C, New_C);
18430 Set_Corresponding_Record_Component (New_C, Old_C);
18431 end if;
18432
18433 -- Set the proper type of an access discriminant
18434
18435 if Ekind (New_C) = E_Discriminant
18436 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18437 then
18438 Set_Anonymous_Type (New_C);
18439 end if;
18440
18441 -- If we have inherited a component then see if its Etype contains
18442 -- references to Parent_Base discriminants. In this case, replace
18443 -- these references with the constraints given in Discs. We do not
18444 -- do this for the partial view of private types because this is
18445 -- not needed (only the components of the full view will be used
18446 -- for code generation) and cause problem. We also avoid this
18447 -- transformation in some error situations.
18448
18449 if Ekind (New_C) = E_Component then
18450
18451 -- Set the proper type of an anonymous access component
18452
18453 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18454 Set_Anonymous_Type (New_C);
18455
18456 elsif (Is_Private_Type (Derived_Base)
18457 and then not Is_Generic_Type (Derived_Base))
18458 or else (Is_Empty_Elmt_List (Discs)
18459 and then not Expander_Active)
18460 then
18461 Set_Etype (New_C, Etype (Old_C));
18462
18463 else
18464 -- The current component introduces a circularity of the
18465 -- following kind:
18466
18467 -- limited with Pack_2;
18468 -- package Pack_1 is
18469 -- type T_1 is tagged record
18470 -- Comp : access Pack_2.T_2;
18471 -- ...
18472 -- end record;
18473 -- end Pack_1;
18474
18475 -- with Pack_1;
18476 -- package Pack_2 is
18477 -- type T_2 is new Pack_1.T_1 with ...;
18478 -- end Pack_2;
18479
18480 Set_Etype
18481 (New_C,
18482 Constrain_Component_Type
18483 (Old_C, Derived_Base, N, Parent_Base, Discs));
18484 end if;
18485 end if;
18486
18487 -- In derived tagged types it is illegal to reference a non
18488 -- discriminant component in the parent type. To catch this, mark
18489 -- these components with an Ekind of E_Void. This will be reset in
18490 -- Record_Type_Definition after processing the record extension of
18491 -- the derived type.
18492
18493 -- If the declaration is a private extension, there is no further
18494 -- record extension to process, and the components retain their
18495 -- current kind, because they are visible at this point.
18496
18497 if Is_Tagged and then Ekind (New_C) = E_Component
18498 and then Nkind (N) /= N_Private_Extension_Declaration
18499 then
18500 Set_Ekind (New_C, E_Void);
18501 end if;
18502
18503 if Plain_Discrim then
18504 Set_Corresponding_Discriminant (New_C, Old_C);
18505 Build_Discriminal (New_C);
18506
18507 -- If we are explicitly inheriting a stored discriminant it will be
18508 -- completely hidden.
18509
18510 elsif Stored_Discrim then
18511 Set_Corresponding_Discriminant (New_C, Empty);
18512 Set_Discriminal (New_C, Empty);
18513 Set_Is_Completely_Hidden (New_C);
18514
18515 -- Set the Original_Record_Component of each discriminant in the
18516 -- derived base to point to the corresponding stored that we just
18517 -- created.
18518
18519 Discrim := First_Discriminant (Derived_Base);
18520 while Present (Discrim) loop
18521 Corr_Discrim := Corresponding_Discriminant (Discrim);
18522
18523 -- Corr_Discrim could be missing in an error situation
18524
18525 if Present (Corr_Discrim)
18526 and then Original_Record_Component (Corr_Discrim) = Old_C
18527 then
18528 Set_Original_Record_Component (Discrim, New_C);
18529 Set_Corresponding_Record_Component (Discrim, Empty);
18530 end if;
18531
18532 Next_Discriminant (Discrim);
18533 end loop;
18534
18535 Append_Entity (New_C, Derived_Base);
18536 end if;
18537
18538 if not Is_Tagged then
18539 Append_Elmt (Old_C, Assoc_List);
18540 Append_Elmt (New_C, Assoc_List);
18541 end if;
18542 end Inherit_Component;
18543
18544 -- Variables local to Inherit_Component
18545
18546 Loc : constant Source_Ptr := Sloc (N);
18547
18548 Parent_Discrim : Entity_Id;
18549 Stored_Discrim : Entity_Id;
18550 D : Entity_Id;
18551 Component : Entity_Id;
18552
18553 -- Start of processing for Inherit_Components
18554
18555 begin
18556 if not Is_Tagged then
18557 Append_Elmt (Parent_Base, Assoc_List);
18558 Append_Elmt (Derived_Base, Assoc_List);
18559 end if;
18560
18561 -- Inherit parent discriminants if needed
18562
18563 if Inherit_Discr then
18564 Parent_Discrim := First_Discriminant (Parent_Base);
18565 while Present (Parent_Discrim) loop
18566 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18567 Next_Discriminant (Parent_Discrim);
18568 end loop;
18569 end if;
18570
18571 -- Create explicit stored discrims for untagged types when necessary
18572
18573 if not Has_Unknown_Discriminants (Derived_Base)
18574 and then Has_Discriminants (Parent_Base)
18575 and then not Is_Tagged
18576 and then
18577 (not Inherit_Discr
18578 or else First_Discriminant (Parent_Base) /=
18579 First_Stored_Discriminant (Parent_Base))
18580 then
18581 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18582 while Present (Stored_Discrim) loop
18583 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18584 Next_Stored_Discriminant (Stored_Discrim);
18585 end loop;
18586 end if;
18587
18588 -- See if we can apply the second transformation for derived types, as
18589 -- explained in point 6. in the comments above Build_Derived_Record_Type
18590 -- This is achieved by appending Derived_Base discriminants into Discs,
18591 -- which has the side effect of returning a non empty Discs list to the
18592 -- caller of Inherit_Components, which is what we want. This must be
18593 -- done for private derived types if there are explicit stored
18594 -- discriminants, to ensure that we can retrieve the values of the
18595 -- constraints provided in the ancestors.
18596
18597 if Inherit_Discr
18598 and then Is_Empty_Elmt_List (Discs)
18599 and then Present (First_Discriminant (Derived_Base))
18600 and then
18601 (not Is_Private_Type (Derived_Base)
18602 or else Is_Completely_Hidden
18603 (First_Stored_Discriminant (Derived_Base))
18604 or else Is_Generic_Type (Derived_Base))
18605 then
18606 D := First_Discriminant (Derived_Base);
18607 while Present (D) loop
18608 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18609 Next_Discriminant (D);
18610 end loop;
18611 end if;
18612
18613 -- Finally, inherit non-discriminant components unless they are not
18614 -- visible because defined or inherited from the full view of the
18615 -- parent. Don't inherit the _parent field of the parent type.
18616
18617 Component := First_Entity (Parent_Base);
18618 while Present (Component) loop
18619
18620 -- Ada 2005 (AI-251): Do not inherit components associated with
18621 -- secondary tags of the parent.
18622
18623 if Ekind (Component) = E_Component
18624 and then Present (Related_Type (Component))
18625 then
18626 null;
18627
18628 elsif Ekind (Component) /= E_Component
18629 or else Chars (Component) = Name_uParent
18630 then
18631 null;
18632
18633 -- If the derived type is within the parent type's declarative
18634 -- region, then the components can still be inherited even though
18635 -- they aren't visible at this point. This can occur for cases
18636 -- such as within public child units where the components must
18637 -- become visible upon entering the child unit's private part.
18638
18639 elsif not Is_Visible_Component (Component)
18640 and then not In_Open_Scopes (Scope (Parent_Base))
18641 then
18642 null;
18643
18644 elsif Ekind_In (Derived_Base, E_Private_Type,
18645 E_Limited_Private_Type)
18646 then
18647 null;
18648
18649 else
18650 Inherit_Component (Component);
18651 end if;
18652
18653 Next_Entity (Component);
18654 end loop;
18655
18656 -- For tagged derived types, inherited discriminants cannot be used in
18657 -- component declarations of the record extension part. To achieve this
18658 -- we mark the inherited discriminants as not visible.
18659
18660 if Is_Tagged and then Inherit_Discr then
18661 D := First_Discriminant (Derived_Base);
18662 while Present (D) loop
18663 Set_Is_Immediately_Visible (D, False);
18664 Next_Discriminant (D);
18665 end loop;
18666 end if;
18667
18668 return Assoc_List;
18669 end Inherit_Components;
18670
18671 -----------------------------
18672 -- Inherit_Predicate_Flags --
18673 -----------------------------
18674
18675 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18676 begin
18677 if Present (Predicate_Function (Subt)) then
18678 return;
18679 end if;
18680
18681 Set_Has_Predicates (Subt, Has_Predicates (Par));
18682 Set_Has_Static_Predicate_Aspect
18683 (Subt, Has_Static_Predicate_Aspect (Par));
18684 Set_Has_Dynamic_Predicate_Aspect
18685 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18686
18687 -- A named subtype does not inherit the predicate function of its
18688 -- parent but an itype declared for a loop index needs the discrete
18689 -- predicate information of its parent to execute the loop properly.
18690 -- A non-discrete type may has a static predicate (for example True)
18691 -- but has no static_discrete_predicate.
18692
18693 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18694 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18695
18696 if Has_Static_Predicate (Par) and then Is_Discrete_Type (Par) then
18697 Set_Static_Discrete_Predicate
18698 (Subt, Static_Discrete_Predicate (Par));
18699 end if;
18700 end if;
18701 end Inherit_Predicate_Flags;
18702
18703 ----------------------
18704 -- Is_EVF_Procedure --
18705 ----------------------
18706
18707 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18708 Formal : Entity_Id;
18709
18710 begin
18711 -- Examine the formals of an Extensions_Visible False procedure looking
18712 -- for a controlling OUT parameter.
18713
18714 if Ekind (Subp) = E_Procedure
18715 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18716 then
18717 Formal := First_Formal (Subp);
18718 while Present (Formal) loop
18719 if Ekind (Formal) = E_Out_Parameter
18720 and then Is_Controlling_Formal (Formal)
18721 then
18722 return True;
18723 end if;
18724
18725 Next_Formal (Formal);
18726 end loop;
18727 end if;
18728
18729 return False;
18730 end Is_EVF_Procedure;
18731
18732 -----------------------
18733 -- Is_Null_Extension --
18734 -----------------------
18735
18736 function Is_Null_Extension (T : Entity_Id) return Boolean is
18737 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18738 Comp_List : Node_Id;
18739 Comp : Node_Id;
18740
18741 begin
18742 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18743 or else not Is_Tagged_Type (T)
18744 or else Nkind (Type_Definition (Type_Decl)) /=
18745 N_Derived_Type_Definition
18746 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18747 then
18748 return False;
18749 end if;
18750
18751 Comp_List :=
18752 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18753
18754 if Present (Discriminant_Specifications (Type_Decl)) then
18755 return False;
18756
18757 elsif Present (Comp_List)
18758 and then Is_Non_Empty_List (Component_Items (Comp_List))
18759 then
18760 Comp := First (Component_Items (Comp_List));
18761
18762 -- Only user-defined components are relevant. The component list
18763 -- may also contain a parent component and internal components
18764 -- corresponding to secondary tags, but these do not determine
18765 -- whether this is a null extension.
18766
18767 while Present (Comp) loop
18768 if Comes_From_Source (Comp) then
18769 return False;
18770 end if;
18771
18772 Next (Comp);
18773 end loop;
18774
18775 return True;
18776
18777 else
18778 return True;
18779 end if;
18780 end Is_Null_Extension;
18781
18782 ------------------------------
18783 -- Is_Valid_Constraint_Kind --
18784 ------------------------------
18785
18786 function Is_Valid_Constraint_Kind
18787 (T_Kind : Type_Kind;
18788 Constraint_Kind : Node_Kind) return Boolean
18789 is
18790 begin
18791 case T_Kind is
18792 when Enumeration_Kind
18793 | Integer_Kind
18794 =>
18795 return Constraint_Kind = N_Range_Constraint;
18796
18797 when Decimal_Fixed_Point_Kind =>
18798 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18799 N_Range_Constraint);
18800
18801 when Ordinary_Fixed_Point_Kind =>
18802 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18803 N_Range_Constraint);
18804
18805 when Float_Kind =>
18806 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18807 N_Range_Constraint);
18808
18809 when Access_Kind
18810 | Array_Kind
18811 | Class_Wide_Kind
18812 | Concurrent_Kind
18813 | Private_Kind
18814 | E_Incomplete_Type
18815 | E_Record_Subtype
18816 | E_Record_Type
18817 =>
18818 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18819
18820 when others =>
18821 return True; -- Error will be detected later
18822 end case;
18823 end Is_Valid_Constraint_Kind;
18824
18825 --------------------------
18826 -- Is_Visible_Component --
18827 --------------------------
18828
18829 function Is_Visible_Component
18830 (C : Entity_Id;
18831 N : Node_Id := Empty) return Boolean
18832 is
18833 Original_Comp : Entity_Id := Empty;
18834 Original_Type : Entity_Id;
18835 Type_Scope : Entity_Id;
18836
18837 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18838 -- Check whether parent type of inherited component is declared locally,
18839 -- possibly within a nested package or instance. The current scope is
18840 -- the derived record itself.
18841
18842 -------------------
18843 -- Is_Local_Type --
18844 -------------------
18845
18846 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18847 Scop : Entity_Id;
18848
18849 begin
18850 Scop := Scope (Typ);
18851 while Present (Scop)
18852 and then Scop /= Standard_Standard
18853 loop
18854 if Scop = Scope (Current_Scope) then
18855 return True;
18856 end if;
18857
18858 Scop := Scope (Scop);
18859 end loop;
18860
18861 return False;
18862 end Is_Local_Type;
18863
18864 -- Start of processing for Is_Visible_Component
18865
18866 begin
18867 if Ekind_In (C, E_Component, E_Discriminant) then
18868 Original_Comp := Original_Record_Component (C);
18869 end if;
18870
18871 if No (Original_Comp) then
18872
18873 -- Premature usage, or previous error
18874
18875 return False;
18876
18877 else
18878 Original_Type := Scope (Original_Comp);
18879 Type_Scope := Scope (Base_Type (Scope (C)));
18880 end if;
18881
18882 -- This test only concerns tagged types
18883
18884 if not Is_Tagged_Type (Original_Type) then
18885
18886 -- Check if this is a renamed discriminant (hidden either by the
18887 -- derived type or by some ancestor), unless we are analyzing code
18888 -- generated by the expander since it may reference such components
18889 -- (for example see the expansion of Deep_Adjust).
18890
18891 if Ekind (C) = E_Discriminant and then Present (N) then
18892 return
18893 not Comes_From_Source (N)
18894 or else not Is_Completely_Hidden (C);
18895 else
18896 return True;
18897 end if;
18898
18899 -- If it is _Parent or _Tag, there is no visibility issue
18900
18901 elsif not Comes_From_Source (Original_Comp) then
18902 return True;
18903
18904 -- Discriminants are visible unless the (private) type has unknown
18905 -- discriminants. If the discriminant reference is inserted for a
18906 -- discriminant check on a full view it is also visible.
18907
18908 elsif Ekind (Original_Comp) = E_Discriminant
18909 and then
18910 (not Has_Unknown_Discriminants (Original_Type)
18911 or else (Present (N)
18912 and then Nkind (N) = N_Selected_Component
18913 and then Nkind (Prefix (N)) = N_Type_Conversion
18914 and then not Comes_From_Source (Prefix (N))))
18915 then
18916 return True;
18917
18918 -- In the body of an instantiation, check the visibility of a component
18919 -- in case it has a homograph that is a primitive operation of a private
18920 -- type which was not visible in the generic unit.
18921
18922 -- Should Is_Prefixed_Call be propagated from template to instance???
18923
18924 elsif In_Instance_Body then
18925 if not Is_Tagged_Type (Original_Type)
18926 or else not Is_Private_Type (Original_Type)
18927 then
18928 return True;
18929
18930 else
18931 declare
18932 Subp_Elmt : Elmt_Id;
18933
18934 begin
18935 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18936 while Present (Subp_Elmt) loop
18937
18938 -- The component is hidden by a primitive operation
18939
18940 if Chars (Node (Subp_Elmt)) = Chars (C) then
18941 return False;
18942 end if;
18943
18944 Next_Elmt (Subp_Elmt);
18945 end loop;
18946
18947 return True;
18948 end;
18949 end if;
18950
18951 -- If the component has been declared in an ancestor which is currently
18952 -- a private type, then it is not visible. The same applies if the
18953 -- component's containing type is not in an open scope and the original
18954 -- component's enclosing type is a visible full view of a private type
18955 -- (which can occur in cases where an attempt is being made to reference
18956 -- a component in a sibling package that is inherited from a visible
18957 -- component of a type in an ancestor package; the component in the
18958 -- sibling package should not be visible even though the component it
18959 -- inherited from is visible). This does not apply however in the case
18960 -- where the scope of the type is a private child unit, or when the
18961 -- parent comes from a local package in which the ancestor is currently
18962 -- visible. The latter suppression of visibility is needed for cases
18963 -- that are tested in B730006.
18964
18965 elsif Is_Private_Type (Original_Type)
18966 or else
18967 (not Is_Private_Descendant (Type_Scope)
18968 and then not In_Open_Scopes (Type_Scope)
18969 and then Has_Private_Declaration (Original_Type))
18970 then
18971 -- If the type derives from an entity in a formal package, there
18972 -- are no additional visible components.
18973
18974 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18975 N_Formal_Package_Declaration
18976 then
18977 return False;
18978
18979 -- if we are not in the private part of the current package, there
18980 -- are no additional visible components.
18981
18982 elsif Ekind (Scope (Current_Scope)) = E_Package
18983 and then not In_Private_Part (Scope (Current_Scope))
18984 then
18985 return False;
18986 else
18987 return
18988 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18989 and then In_Open_Scopes (Scope (Original_Type))
18990 and then Is_Local_Type (Type_Scope);
18991 end if;
18992
18993 -- There is another weird way in which a component may be invisible when
18994 -- the private and the full view are not derived from the same ancestor.
18995 -- Here is an example :
18996
18997 -- type A1 is tagged record F1 : integer; end record;
18998 -- type A2 is new A1 with record F2 : integer; end record;
18999 -- type T is new A1 with private;
19000 -- private
19001 -- type T is new A2 with null record;
19002
19003 -- In this case, the full view of T inherits F1 and F2 but the private
19004 -- view inherits only F1
19005
19006 else
19007 declare
19008 Ancestor : Entity_Id := Scope (C);
19009
19010 begin
19011 loop
19012 if Ancestor = Original_Type then
19013 return True;
19014
19015 -- The ancestor may have a partial view of the original type,
19016 -- but if the full view is in scope, as in a child body, the
19017 -- component is visible.
19018
19019 elsif In_Private_Part (Scope (Original_Type))
19020 and then Full_View (Ancestor) = Original_Type
19021 then
19022 return True;
19023
19024 elsif Ancestor = Etype (Ancestor) then
19025
19026 -- No further ancestors to examine
19027
19028 return False;
19029 end if;
19030
19031 Ancestor := Etype (Ancestor);
19032 end loop;
19033 end;
19034 end if;
19035 end Is_Visible_Component;
19036
19037 --------------------------
19038 -- Make_Class_Wide_Type --
19039 --------------------------
19040
19041 procedure Make_Class_Wide_Type (T : Entity_Id) is
19042 CW_Type : Entity_Id;
19043 CW_Name : Name_Id;
19044 Next_E : Entity_Id;
19045 Prev_E : Entity_Id;
19046
19047 begin
19048 if Present (Class_Wide_Type (T)) then
19049
19050 -- The class-wide type is a partially decorated entity created for a
19051 -- unanalyzed tagged type referenced through a limited with clause.
19052 -- When the tagged type is analyzed, its class-wide type needs to be
19053 -- redecorated. Note that we reuse the entity created by Decorate_
19054 -- Tagged_Type in order to preserve all links.
19055
19056 if Materialize_Entity (Class_Wide_Type (T)) then
19057 CW_Type := Class_Wide_Type (T);
19058 Set_Materialize_Entity (CW_Type, False);
19059
19060 -- The class wide type can have been defined by the partial view, in
19061 -- which case everything is already done.
19062
19063 else
19064 return;
19065 end if;
19066
19067 -- Default case, we need to create a new class-wide type
19068
19069 else
19070 CW_Type :=
19071 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19072 end if;
19073
19074 -- Inherit root type characteristics
19075
19076 CW_Name := Chars (CW_Type);
19077 Next_E := Next_Entity (CW_Type);
19078 Prev_E := Prev_Entity (CW_Type);
19079 Copy_Node (T, CW_Type);
19080 Set_Comes_From_Source (CW_Type, False);
19081 Set_Chars (CW_Type, CW_Name);
19082 Set_Parent (CW_Type, Parent (T));
19083 Set_Prev_Entity (CW_Type, Prev_E);
19084 Set_Next_Entity (CW_Type, Next_E);
19085
19086 -- Ensure we have a new freeze node for the class-wide type. The partial
19087 -- view may have freeze action of its own, requiring a proper freeze
19088 -- node, and the same freeze node cannot be shared between the two
19089 -- types.
19090
19091 Set_Has_Delayed_Freeze (CW_Type);
19092 Set_Freeze_Node (CW_Type, Empty);
19093
19094 -- Customize the class-wide type: It has no prim. op., it cannot be
19095 -- abstract, its Etype points back to the specific root type, and it
19096 -- cannot have any invariants.
19097
19098 Set_Ekind (CW_Type, E_Class_Wide_Type);
19099 Set_Is_Tagged_Type (CW_Type, True);
19100 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19101 Set_Is_Abstract_Type (CW_Type, False);
19102 Set_Is_Constrained (CW_Type, False);
19103 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19104 Set_Default_SSO (CW_Type);
19105 Set_Has_Inheritable_Invariants (CW_Type, False);
19106 Set_Has_Inherited_Invariants (CW_Type, False);
19107 Set_Has_Own_Invariants (CW_Type, False);
19108
19109 if Ekind (T) = E_Class_Wide_Subtype then
19110 Set_Etype (CW_Type, Etype (Base_Type (T)));
19111 else
19112 Set_Etype (CW_Type, T);
19113 end if;
19114
19115 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19116
19117 -- If this is the class_wide type of a constrained subtype, it does
19118 -- not have discriminants.
19119
19120 Set_Has_Discriminants (CW_Type,
19121 Has_Discriminants (T) and then not Is_Constrained (T));
19122
19123 Set_Has_Unknown_Discriminants (CW_Type, True);
19124 Set_Class_Wide_Type (T, CW_Type);
19125 Set_Equivalent_Type (CW_Type, Empty);
19126
19127 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19128
19129 Set_Class_Wide_Type (CW_Type, CW_Type);
19130 end Make_Class_Wide_Type;
19131
19132 ----------------
19133 -- Make_Index --
19134 ----------------
19135
19136 procedure Make_Index
19137 (N : Node_Id;
19138 Related_Nod : Node_Id;
19139 Related_Id : Entity_Id := Empty;
19140 Suffix_Index : Nat := 1;
19141 In_Iter_Schm : Boolean := False)
19142 is
19143 R : Node_Id;
19144 T : Entity_Id;
19145 Def_Id : Entity_Id := Empty;
19146 Found : Boolean := False;
19147
19148 begin
19149 -- For a discrete range used in a constrained array definition and
19150 -- defined by a range, an implicit conversion to the predefined type
19151 -- INTEGER is assumed if each bound is either a numeric literal, a named
19152 -- number, or an attribute, and the type of both bounds (prior to the
19153 -- implicit conversion) is the type universal_integer. Otherwise, both
19154 -- bounds must be of the same discrete type, other than universal
19155 -- integer; this type must be determinable independently of the
19156 -- context, but using the fact that the type must be discrete and that
19157 -- both bounds must have the same type.
19158
19159 -- Character literals also have a universal type in the absence of
19160 -- of additional context, and are resolved to Standard_Character.
19161
19162 if Nkind (N) = N_Range then
19163
19164 -- The index is given by a range constraint. The bounds are known
19165 -- to be of a consistent type.
19166
19167 if not Is_Overloaded (N) then
19168 T := Etype (N);
19169
19170 -- For universal bounds, choose the specific predefined type
19171
19172 if T = Universal_Integer then
19173 T := Standard_Integer;
19174
19175 elsif T = Any_Character then
19176 Ambiguous_Character (Low_Bound (N));
19177
19178 T := Standard_Character;
19179 end if;
19180
19181 -- The node may be overloaded because some user-defined operators
19182 -- are available, but if a universal interpretation exists it is
19183 -- also the selected one.
19184
19185 elsif Universal_Interpretation (N) = Universal_Integer then
19186 T := Standard_Integer;
19187
19188 else
19189 T := Any_Type;
19190
19191 declare
19192 Ind : Interp_Index;
19193 It : Interp;
19194
19195 begin
19196 Get_First_Interp (N, Ind, It);
19197 while Present (It.Typ) loop
19198 if Is_Discrete_Type (It.Typ) then
19199
19200 if Found
19201 and then not Covers (It.Typ, T)
19202 and then not Covers (T, It.Typ)
19203 then
19204 Error_Msg_N ("ambiguous bounds in discrete range", N);
19205 exit;
19206 else
19207 T := It.Typ;
19208 Found := True;
19209 end if;
19210 end if;
19211
19212 Get_Next_Interp (Ind, It);
19213 end loop;
19214
19215 if T = Any_Type then
19216 Error_Msg_N ("discrete type required for range", N);
19217 Set_Etype (N, Any_Type);
19218 return;
19219
19220 elsif T = Universal_Integer then
19221 T := Standard_Integer;
19222 end if;
19223 end;
19224 end if;
19225
19226 if not Is_Discrete_Type (T) then
19227 Error_Msg_N ("discrete type required for range", N);
19228 Set_Etype (N, Any_Type);
19229 return;
19230 end if;
19231
19232 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19233 and then Attribute_Name (Low_Bound (N)) = Name_First
19234 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19235 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19236 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19237 then
19238 -- The type of the index will be the type of the prefix, as long
19239 -- as the upper bound is 'Last of the same type.
19240
19241 Def_Id := Entity (Prefix (Low_Bound (N)));
19242
19243 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19244 or else Attribute_Name (High_Bound (N)) /= Name_Last
19245 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19246 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19247 then
19248 Def_Id := Empty;
19249 end if;
19250 end if;
19251
19252 R := N;
19253 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19254
19255 elsif Nkind (N) = N_Subtype_Indication then
19256
19257 -- The index is given by a subtype with a range constraint
19258
19259 T := Base_Type (Entity (Subtype_Mark (N)));
19260
19261 if not Is_Discrete_Type (T) then
19262 Error_Msg_N ("discrete type required for range", N);
19263 Set_Etype (N, Any_Type);
19264 return;
19265 end if;
19266
19267 R := Range_Expression (Constraint (N));
19268
19269 Resolve (R, T);
19270 Process_Range_Expr_In_Decl
19271 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19272
19273 elsif Nkind (N) = N_Attribute_Reference then
19274
19275 -- Catch beginner's error (use of attribute other than 'Range)
19276
19277 if Attribute_Name (N) /= Name_Range then
19278 Error_Msg_N ("expect attribute ''Range", N);
19279 Set_Etype (N, Any_Type);
19280 return;
19281 end if;
19282
19283 -- If the node denotes the range of a type mark, that is also the
19284 -- resulting type, and we do not need to create an Itype for it.
19285
19286 if Is_Entity_Name (Prefix (N))
19287 and then Comes_From_Source (N)
19288 and then Is_Type (Entity (Prefix (N)))
19289 and then Is_Discrete_Type (Entity (Prefix (N)))
19290 then
19291 Def_Id := Entity (Prefix (N));
19292 end if;
19293
19294 Analyze_And_Resolve (N);
19295 T := Etype (N);
19296 R := N;
19297
19298 -- If none of the above, must be a subtype. We convert this to a
19299 -- range attribute reference because in the case of declared first
19300 -- named subtypes, the types in the range reference can be different
19301 -- from the type of the entity. A range attribute normalizes the
19302 -- reference and obtains the correct types for the bounds.
19303
19304 -- This transformation is in the nature of an expansion, is only
19305 -- done if expansion is active. In particular, it is not done on
19306 -- formal generic types, because we need to retain the name of the
19307 -- original index for instantiation purposes.
19308
19309 else
19310 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19311 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19312 Set_Etype (N, Any_Integer);
19313 return;
19314
19315 else
19316 -- The type mark may be that of an incomplete type. It is only
19317 -- now that we can get the full view, previous analysis does
19318 -- not look specifically for a type mark.
19319
19320 Set_Entity (N, Get_Full_View (Entity (N)));
19321 Set_Etype (N, Entity (N));
19322 Def_Id := Entity (N);
19323
19324 if not Is_Discrete_Type (Def_Id) then
19325 Error_Msg_N ("discrete type required for index", N);
19326 Set_Etype (N, Any_Type);
19327 return;
19328 end if;
19329 end if;
19330
19331 if Expander_Active then
19332 Rewrite (N,
19333 Make_Attribute_Reference (Sloc (N),
19334 Attribute_Name => Name_Range,
19335 Prefix => Relocate_Node (N)));
19336
19337 -- The original was a subtype mark that does not freeze. This
19338 -- means that the rewritten version must not freeze either.
19339
19340 Set_Must_Not_Freeze (N);
19341 Set_Must_Not_Freeze (Prefix (N));
19342 Analyze_And_Resolve (N);
19343 T := Etype (N);
19344 R := N;
19345
19346 -- If expander is inactive, type is legal, nothing else to construct
19347
19348 else
19349 return;
19350 end if;
19351 end if;
19352
19353 if not Is_Discrete_Type (T) then
19354 Error_Msg_N ("discrete type required for range", N);
19355 Set_Etype (N, Any_Type);
19356 return;
19357
19358 elsif T = Any_Type then
19359 Set_Etype (N, Any_Type);
19360 return;
19361 end if;
19362
19363 -- We will now create the appropriate Itype to describe the range, but
19364 -- first a check. If we originally had a subtype, then we just label
19365 -- the range with this subtype. Not only is there no need to construct
19366 -- a new subtype, but it is wrong to do so for two reasons:
19367
19368 -- 1. A legality concern, if we have a subtype, it must not freeze,
19369 -- and the Itype would cause freezing incorrectly
19370
19371 -- 2. An efficiency concern, if we created an Itype, it would not be
19372 -- recognized as the same type for the purposes of eliminating
19373 -- checks in some circumstances.
19374
19375 -- We signal this case by setting the subtype entity in Def_Id
19376
19377 if No (Def_Id) then
19378 Def_Id :=
19379 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19380 Set_Etype (Def_Id, Base_Type (T));
19381
19382 if Is_Signed_Integer_Type (T) then
19383 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19384
19385 elsif Is_Modular_Integer_Type (T) then
19386 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19387
19388 else
19389 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19390 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19391 Set_First_Literal (Def_Id, First_Literal (T));
19392 end if;
19393
19394 Set_Size_Info (Def_Id, (T));
19395 Set_RM_Size (Def_Id, RM_Size (T));
19396 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19397
19398 Set_Scalar_Range (Def_Id, R);
19399 Conditional_Delay (Def_Id, T);
19400
19401 if Nkind (N) = N_Subtype_Indication then
19402 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19403 end if;
19404
19405 -- In the subtype indication case, if the immediate parent of the
19406 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19407 -- even if its bounds are static.
19408
19409 if Nkind (N) = N_Subtype_Indication
19410 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19411 then
19412 Set_Is_Non_Static_Subtype (Def_Id);
19413 end if;
19414 end if;
19415
19416 -- Final step is to label the index with this constructed type
19417
19418 Set_Etype (N, Def_Id);
19419 end Make_Index;
19420
19421 ------------------------------
19422 -- Modular_Type_Declaration --
19423 ------------------------------
19424
19425 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19426 Mod_Expr : constant Node_Id := Expression (Def);
19427 M_Val : Uint;
19428
19429 procedure Set_Modular_Size (Bits : Int);
19430 -- Sets RM_Size to Bits, and Esize to normal word size above this
19431
19432 ----------------------
19433 -- Set_Modular_Size --
19434 ----------------------
19435
19436 procedure Set_Modular_Size (Bits : Int) is
19437 begin
19438 Set_RM_Size (T, UI_From_Int (Bits));
19439
19440 if Bits <= 8 then
19441 Init_Esize (T, 8);
19442
19443 elsif Bits <= 16 then
19444 Init_Esize (T, 16);
19445
19446 elsif Bits <= 32 then
19447 Init_Esize (T, 32);
19448
19449 else
19450 Init_Esize (T, System_Max_Binary_Modulus_Power);
19451 end if;
19452
19453 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19454 Set_Is_Known_Valid (T);
19455 end if;
19456 end Set_Modular_Size;
19457
19458 -- Start of processing for Modular_Type_Declaration
19459
19460 begin
19461 -- If the mod expression is (exactly) 2 * literal, where literal is
19462 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19463
19464 if Warn_On_Suspicious_Modulus_Value
19465 and then Nkind (Mod_Expr) = N_Op_Multiply
19466 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19467 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19468 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19469 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19470 then
19471 Error_Msg_N
19472 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19473 end if;
19474
19475 -- Proceed with analysis of mod expression
19476
19477 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19478 Set_Etype (T, T);
19479 Set_Ekind (T, E_Modular_Integer_Type);
19480 Init_Alignment (T);
19481 Set_Is_Constrained (T);
19482
19483 if not Is_OK_Static_Expression (Mod_Expr) then
19484 Flag_Non_Static_Expr
19485 ("non-static expression used for modular type bound!", Mod_Expr);
19486 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19487 else
19488 M_Val := Expr_Value (Mod_Expr);
19489 end if;
19490
19491 if M_Val < 1 then
19492 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19493 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19494 end if;
19495
19496 if M_Val > 2 ** Standard_Long_Integer_Size then
19497 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19498 end if;
19499
19500 Set_Modulus (T, M_Val);
19501
19502 -- Create bounds for the modular type based on the modulus given in
19503 -- the type declaration and then analyze and resolve those bounds.
19504
19505 Set_Scalar_Range (T,
19506 Make_Range (Sloc (Mod_Expr),
19507 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19508 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19509
19510 -- Properly analyze the literals for the range. We do this manually
19511 -- because we can't go calling Resolve, since we are resolving these
19512 -- bounds with the type, and this type is certainly not complete yet.
19513
19514 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19515 Set_Etype (High_Bound (Scalar_Range (T)), T);
19516 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19517 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19518
19519 -- Loop through powers of two to find number of bits required
19520
19521 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19522
19523 -- Binary case
19524
19525 if M_Val = 2 ** Bits then
19526 Set_Modular_Size (Bits);
19527 return;
19528
19529 -- Nonbinary case
19530
19531 elsif M_Val < 2 ** Bits then
19532 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19533 Set_Non_Binary_Modulus (T);
19534
19535 if Bits > System_Max_Nonbinary_Modulus_Power then
19536 Error_Msg_Uint_1 :=
19537 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19538 Error_Msg_F
19539 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19540 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19541 return;
19542
19543 else
19544 -- In the nonbinary case, set size as per RM 13.3(55)
19545
19546 Set_Modular_Size (Bits);
19547 return;
19548 end if;
19549 end if;
19550
19551 end loop;
19552
19553 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19554 -- so we just signal an error and set the maximum size.
19555
19556 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19557 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19558
19559 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19560 Init_Alignment (T);
19561
19562 end Modular_Type_Declaration;
19563
19564 --------------------------
19565 -- New_Concatenation_Op --
19566 --------------------------
19567
19568 procedure New_Concatenation_Op (Typ : Entity_Id) is
19569 Loc : constant Source_Ptr := Sloc (Typ);
19570 Op : Entity_Id;
19571
19572 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19573 -- Create abbreviated declaration for the formal of a predefined
19574 -- Operator 'Op' of type 'Typ'
19575
19576 --------------------
19577 -- Make_Op_Formal --
19578 --------------------
19579
19580 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19581 Formal : Entity_Id;
19582 begin
19583 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19584 Set_Etype (Formal, Typ);
19585 Set_Mechanism (Formal, Default_Mechanism);
19586 return Formal;
19587 end Make_Op_Formal;
19588
19589 -- Start of processing for New_Concatenation_Op
19590
19591 begin
19592 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19593
19594 Set_Ekind (Op, E_Operator);
19595 Set_Scope (Op, Current_Scope);
19596 Set_Etype (Op, Typ);
19597 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19598 Set_Is_Immediately_Visible (Op);
19599 Set_Is_Intrinsic_Subprogram (Op);
19600 Set_Has_Completion (Op);
19601 Append_Entity (Op, Current_Scope);
19602
19603 Set_Name_Entity_Id (Name_Op_Concat, Op);
19604
19605 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19606 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19607 end New_Concatenation_Op;
19608
19609 -------------------------
19610 -- OK_For_Limited_Init --
19611 -------------------------
19612
19613 -- ???Check all calls of this, and compare the conditions under which it's
19614 -- called.
19615
19616 function OK_For_Limited_Init
19617 (Typ : Entity_Id;
19618 Exp : Node_Id) return Boolean
19619 is
19620 begin
19621 return Is_CPP_Constructor_Call (Exp)
19622 or else (Ada_Version >= Ada_2005
19623 and then not Debug_Flag_Dot_L
19624 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19625 end OK_For_Limited_Init;
19626
19627 -------------------------------
19628 -- OK_For_Limited_Init_In_05 --
19629 -------------------------------
19630
19631 function OK_For_Limited_Init_In_05
19632 (Typ : Entity_Id;
19633 Exp : Node_Id) return Boolean
19634 is
19635 begin
19636 -- An object of a limited interface type can be initialized with any
19637 -- expression of a nonlimited descendant type. However this does not
19638 -- apply if this is a view conversion of some other expression. This
19639 -- is checked below.
19640
19641 if Is_Class_Wide_Type (Typ)
19642 and then Is_Limited_Interface (Typ)
19643 and then not Is_Limited_Type (Etype (Exp))
19644 and then Nkind (Exp) /= N_Type_Conversion
19645 then
19646 return True;
19647 end if;
19648
19649 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19650 -- case of limited aggregates (including extension aggregates), and
19651 -- function calls. The function call may have been given in prefixed
19652 -- notation, in which case the original node is an indexed component.
19653 -- If the function is parameterless, the original node was an explicit
19654 -- dereference. The function may also be parameterless, in which case
19655 -- the source node is just an identifier.
19656
19657 -- A branch of a conditional expression may have been removed if the
19658 -- condition is statically known. This happens during expansion, and
19659 -- thus will not happen if previous errors were encountered. The check
19660 -- will have been performed on the chosen branch, which replaces the
19661 -- original conditional expression.
19662
19663 if No (Exp) then
19664 return True;
19665 end if;
19666
19667 case Nkind (Original_Node (Exp)) is
19668 when N_Aggregate
19669 | N_Extension_Aggregate
19670 | N_Function_Call
19671 | N_Op
19672 =>
19673 return True;
19674
19675 when N_Identifier =>
19676 return Present (Entity (Original_Node (Exp)))
19677 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19678
19679 when N_Qualified_Expression =>
19680 return
19681 OK_For_Limited_Init_In_05
19682 (Typ, Expression (Original_Node (Exp)));
19683
19684 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19685 -- with a function call, the expander has rewritten the call into an
19686 -- N_Type_Conversion node to force displacement of the pointer to
19687 -- reference the component containing the secondary dispatch table.
19688 -- Otherwise a type conversion is not a legal context.
19689 -- A return statement for a build-in-place function returning a
19690 -- synchronized type also introduces an unchecked conversion.
19691
19692 when N_Type_Conversion
19693 | N_Unchecked_Type_Conversion
19694 =>
19695 return not Comes_From_Source (Exp)
19696 and then
19697 OK_For_Limited_Init_In_05
19698 (Typ, Expression (Original_Node (Exp)));
19699
19700 when N_Explicit_Dereference
19701 | N_Indexed_Component
19702 | N_Selected_Component
19703 =>
19704 return Nkind (Exp) = N_Function_Call;
19705
19706 -- A use of 'Input is a function call, hence allowed. Normally the
19707 -- attribute will be changed to a call, but the attribute by itself
19708 -- can occur with -gnatc.
19709
19710 when N_Attribute_Reference =>
19711 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19712
19713 -- "return raise ..." is OK
19714
19715 when N_Raise_Expression =>
19716 return True;
19717
19718 -- For a case expression, all dependent expressions must be legal
19719
19720 when N_Case_Expression =>
19721 declare
19722 Alt : Node_Id;
19723
19724 begin
19725 Alt := First (Alternatives (Original_Node (Exp)));
19726 while Present (Alt) loop
19727 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19728 return False;
19729 end if;
19730
19731 Next (Alt);
19732 end loop;
19733
19734 return True;
19735 end;
19736
19737 -- For an if expression, all dependent expressions must be legal
19738
19739 when N_If_Expression =>
19740 declare
19741 Then_Expr : constant Node_Id :=
19742 Next (First (Expressions (Original_Node (Exp))));
19743 Else_Expr : constant Node_Id := Next (Then_Expr);
19744 begin
19745 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19746 and then
19747 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19748 end;
19749
19750 when others =>
19751 return False;
19752 end case;
19753 end OK_For_Limited_Init_In_05;
19754
19755 -------------------------------------------
19756 -- Ordinary_Fixed_Point_Type_Declaration --
19757 -------------------------------------------
19758
19759 procedure Ordinary_Fixed_Point_Type_Declaration
19760 (T : Entity_Id;
19761 Def : Node_Id)
19762 is
19763 Loc : constant Source_Ptr := Sloc (Def);
19764 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19765 RRS : constant Node_Id := Real_Range_Specification (Def);
19766 Implicit_Base : Entity_Id;
19767 Delta_Val : Ureal;
19768 Small_Val : Ureal;
19769 Low_Val : Ureal;
19770 High_Val : Ureal;
19771
19772 begin
19773 Check_Restriction (No_Fixed_Point, Def);
19774
19775 -- Create implicit base type
19776
19777 Implicit_Base :=
19778 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19779 Set_Etype (Implicit_Base, Implicit_Base);
19780
19781 -- Analyze and process delta expression
19782
19783 Analyze_And_Resolve (Delta_Expr, Any_Real);
19784
19785 Check_Delta_Expression (Delta_Expr);
19786 Delta_Val := Expr_Value_R (Delta_Expr);
19787
19788 Set_Delta_Value (Implicit_Base, Delta_Val);
19789
19790 -- Compute default small from given delta, which is the largest power
19791 -- of two that does not exceed the given delta value.
19792
19793 declare
19794 Tmp : Ureal;
19795 Scale : Int;
19796
19797 begin
19798 Tmp := Ureal_1;
19799 Scale := 0;
19800
19801 if Delta_Val < Ureal_1 then
19802 while Delta_Val < Tmp loop
19803 Tmp := Tmp / Ureal_2;
19804 Scale := Scale + 1;
19805 end loop;
19806
19807 else
19808 loop
19809 Tmp := Tmp * Ureal_2;
19810 exit when Tmp > Delta_Val;
19811 Scale := Scale - 1;
19812 end loop;
19813 end if;
19814
19815 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19816 end;
19817
19818 Set_Small_Value (Implicit_Base, Small_Val);
19819
19820 -- If no range was given, set a dummy range
19821
19822 if RRS <= Empty_Or_Error then
19823 Low_Val := -Small_Val;
19824 High_Val := Small_Val;
19825
19826 -- Otherwise analyze and process given range
19827
19828 else
19829 declare
19830 Low : constant Node_Id := Low_Bound (RRS);
19831 High : constant Node_Id := High_Bound (RRS);
19832
19833 begin
19834 Analyze_And_Resolve (Low, Any_Real);
19835 Analyze_And_Resolve (High, Any_Real);
19836 Check_Real_Bound (Low);
19837 Check_Real_Bound (High);
19838
19839 -- Obtain and set the range
19840
19841 Low_Val := Expr_Value_R (Low);
19842 High_Val := Expr_Value_R (High);
19843
19844 if Low_Val > High_Val then
19845 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19846 end if;
19847 end;
19848 end if;
19849
19850 -- The range for both the implicit base and the declared first subtype
19851 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19852 -- set a temporary range in place. Note that the bounds of the base
19853 -- type will be widened to be symmetrical and to fill the available
19854 -- bits when the type is frozen.
19855
19856 -- We could do this with all discrete types, and probably should, but
19857 -- we absolutely have to do it for fixed-point, since the end-points
19858 -- of the range and the size are determined by the small value, which
19859 -- could be reset before the freeze point.
19860
19861 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19862 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19863
19864 -- Complete definition of first subtype. The inheritance of the rep item
19865 -- chain ensures that SPARK-related pragmas are not clobbered when the
19866 -- ordinary fixed point type acts as a full view of a private type.
19867
19868 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19869 Set_Etype (T, Implicit_Base);
19870 Init_Size_Align (T);
19871 Inherit_Rep_Item_Chain (T, Implicit_Base);
19872 Set_Small_Value (T, Small_Val);
19873 Set_Delta_Value (T, Delta_Val);
19874 Set_Is_Constrained (T);
19875 end Ordinary_Fixed_Point_Type_Declaration;
19876
19877 ----------------------------------
19878 -- Preanalyze_Assert_Expression --
19879 ----------------------------------
19880
19881 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19882 begin
19883 In_Assertion_Expr := In_Assertion_Expr + 1;
19884 Preanalyze_Spec_Expression (N, T);
19885 In_Assertion_Expr := In_Assertion_Expr - 1;
19886 end Preanalyze_Assert_Expression;
19887
19888 -----------------------------------
19889 -- Preanalyze_Default_Expression --
19890 -----------------------------------
19891
19892 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19893 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19894 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19895
19896 begin
19897 In_Default_Expr := True;
19898 In_Spec_Expression := True;
19899
19900 Preanalyze_With_Freezing_And_Resolve (N, T);
19901
19902 In_Default_Expr := Save_In_Default_Expr;
19903 In_Spec_Expression := Save_In_Spec_Expression;
19904 end Preanalyze_Default_Expression;
19905
19906 --------------------------------
19907 -- Preanalyze_Spec_Expression --
19908 --------------------------------
19909
19910 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19911 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19912 begin
19913 In_Spec_Expression := True;
19914 Preanalyze_And_Resolve (N, T);
19915 In_Spec_Expression := Save_In_Spec_Expression;
19916 end Preanalyze_Spec_Expression;
19917
19918 ----------------------------------------
19919 -- Prepare_Private_Subtype_Completion --
19920 ----------------------------------------
19921
19922 procedure Prepare_Private_Subtype_Completion
19923 (Id : Entity_Id;
19924 Related_Nod : Node_Id)
19925 is
19926 Id_B : constant Entity_Id := Base_Type (Id);
19927 Full_B : Entity_Id := Full_View (Id_B);
19928 Full : Entity_Id;
19929
19930 begin
19931 if Present (Full_B) then
19932
19933 -- Get to the underlying full view if necessary
19934
19935 if Is_Private_Type (Full_B)
19936 and then Present (Underlying_Full_View (Full_B))
19937 then
19938 Full_B := Underlying_Full_View (Full_B);
19939 end if;
19940
19941 -- The Base_Type is already completed, we can complete the subtype
19942 -- now. We have to create a new entity with the same name, Thus we
19943 -- can't use Create_Itype.
19944
19945 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19946 Set_Is_Itype (Full);
19947 Set_Associated_Node_For_Itype (Full, Related_Nod);
19948 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19949 end if;
19950
19951 -- The parent subtype may be private, but the base might not, in some
19952 -- nested instances. In that case, the subtype does not need to be
19953 -- exchanged. It would still be nice to make private subtypes and their
19954 -- bases consistent at all times ???
19955
19956 if Is_Private_Type (Id_B) then
19957 Append_Elmt (Id, Private_Dependents (Id_B));
19958 end if;
19959 end Prepare_Private_Subtype_Completion;
19960
19961 ---------------------------
19962 -- Process_Discriminants --
19963 ---------------------------
19964
19965 procedure Process_Discriminants
19966 (N : Node_Id;
19967 Prev : Entity_Id := Empty)
19968 is
19969 Elist : constant Elist_Id := New_Elmt_List;
19970 Id : Node_Id;
19971 Discr : Node_Id;
19972 Discr_Number : Uint;
19973 Discr_Type : Entity_Id;
19974 Default_Present : Boolean := False;
19975 Default_Not_Present : Boolean := False;
19976
19977 begin
19978 -- A composite type other than an array type can have discriminants.
19979 -- On entry, the current scope is the composite type.
19980
19981 -- The discriminants are initially entered into the scope of the type
19982 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19983 -- use, as explained at the end of this procedure.
19984
19985 Discr := First (Discriminant_Specifications (N));
19986 while Present (Discr) loop
19987 Enter_Name (Defining_Identifier (Discr));
19988
19989 -- For navigation purposes we add a reference to the discriminant
19990 -- in the entity for the type. If the current declaration is a
19991 -- completion, place references on the partial view. Otherwise the
19992 -- type is the current scope.
19993
19994 if Present (Prev) then
19995
19996 -- The references go on the partial view, if present. If the
19997 -- partial view has discriminants, the references have been
19998 -- generated already.
19999
20000 if not Has_Discriminants (Prev) then
20001 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20002 end if;
20003 else
20004 Generate_Reference
20005 (Current_Scope, Defining_Identifier (Discr), 'd');
20006 end if;
20007
20008 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20009 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
20010
20011 -- Ada 2005 (AI-254)
20012
20013 if Present (Access_To_Subprogram_Definition
20014 (Discriminant_Type (Discr)))
20015 and then Protected_Present (Access_To_Subprogram_Definition
20016 (Discriminant_Type (Discr)))
20017 then
20018 Discr_Type :=
20019 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20020 end if;
20021
20022 else
20023 Find_Type (Discriminant_Type (Discr));
20024 Discr_Type := Etype (Discriminant_Type (Discr));
20025
20026 if Error_Posted (Discriminant_Type (Discr)) then
20027 Discr_Type := Any_Type;
20028 end if;
20029 end if;
20030
20031 -- Handling of discriminants that are access types
20032
20033 if Is_Access_Type (Discr_Type) then
20034
20035 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20036 -- limited record types
20037
20038 if Ada_Version < Ada_2005 then
20039 Check_Access_Discriminant_Requires_Limited
20040 (Discr, Discriminant_Type (Discr));
20041 end if;
20042
20043 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20044 Error_Msg_N
20045 ("(Ada 83) access discriminant not allowed", Discr);
20046 end if;
20047
20048 -- If not access type, must be a discrete type
20049
20050 elsif not Is_Discrete_Type (Discr_Type) then
20051 Error_Msg_N
20052 ("discriminants must have a discrete or access type",
20053 Discriminant_Type (Discr));
20054 end if;
20055
20056 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20057
20058 -- If a discriminant specification includes the assignment compound
20059 -- delimiter followed by an expression, the expression is the default
20060 -- expression of the discriminant; the default expression must be of
20061 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20062 -- a default expression, we do the special preanalysis, since this
20063 -- expression does not freeze (see section "Handling of Default and
20064 -- Per-Object Expressions" in spec of package Sem).
20065
20066 if Present (Expression (Discr)) then
20067 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
20068
20069 -- Legaity checks
20070
20071 if Nkind (N) = N_Formal_Type_Declaration then
20072 Error_Msg_N
20073 ("discriminant defaults not allowed for formal type",
20074 Expression (Discr));
20075
20076 -- Flag an error for a tagged type with defaulted discriminants,
20077 -- excluding limited tagged types when compiling for Ada 2012
20078 -- (see AI05-0214).
20079
20080 elsif Is_Tagged_Type (Current_Scope)
20081 and then (not Is_Limited_Type (Current_Scope)
20082 or else Ada_Version < Ada_2012)
20083 and then Comes_From_Source (N)
20084 then
20085 -- Note: see similar test in Check_Or_Process_Discriminants, to
20086 -- handle the (illegal) case of the completion of an untagged
20087 -- view with discriminants with defaults by a tagged full view.
20088 -- We skip the check if Discr does not come from source, to
20089 -- account for the case of an untagged derived type providing
20090 -- defaults for a renamed discriminant from a private untagged
20091 -- ancestor with a tagged full view (ACATS B460006).
20092
20093 if Ada_Version >= Ada_2012 then
20094 Error_Msg_N
20095 ("discriminants of nonlimited tagged type cannot have"
20096 & " defaults",
20097 Expression (Discr));
20098 else
20099 Error_Msg_N
20100 ("discriminants of tagged type cannot have defaults",
20101 Expression (Discr));
20102 end if;
20103
20104 else
20105 Default_Present := True;
20106 Append_Elmt (Expression (Discr), Elist);
20107
20108 -- Tag the defining identifiers for the discriminants with
20109 -- their corresponding default expressions from the tree.
20110
20111 Set_Discriminant_Default_Value
20112 (Defining_Identifier (Discr), Expression (Discr));
20113 end if;
20114
20115 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20116 -- gets set unless we can be sure that no range check is required.
20117
20118 if (GNATprove_Mode or not Expander_Active)
20119 and then not
20120 Is_In_Range
20121 (Expression (Discr), Discr_Type, Assume_Valid => True)
20122 then
20123 Set_Do_Range_Check (Expression (Discr));
20124 end if;
20125
20126 -- No default discriminant value given
20127
20128 else
20129 Default_Not_Present := True;
20130 end if;
20131
20132 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20133 -- Discr_Type but with the null-exclusion attribute
20134
20135 if Ada_Version >= Ada_2005 then
20136
20137 -- Ada 2005 (AI-231): Static checks
20138
20139 if Can_Never_Be_Null (Discr_Type) then
20140 Null_Exclusion_Static_Checks (Discr);
20141
20142 elsif Is_Access_Type (Discr_Type)
20143 and then Null_Exclusion_Present (Discr)
20144
20145 -- No need to check itypes because in their case this check
20146 -- was done at their point of creation
20147
20148 and then not Is_Itype (Discr_Type)
20149 then
20150 if Can_Never_Be_Null (Discr_Type) then
20151 Error_Msg_NE
20152 ("`NOT NULL` not allowed (& already excludes null)",
20153 Discr,
20154 Discr_Type);
20155 end if;
20156
20157 Set_Etype (Defining_Identifier (Discr),
20158 Create_Null_Excluding_Itype
20159 (T => Discr_Type,
20160 Related_Nod => Discr));
20161
20162 -- Check for improper null exclusion if the type is otherwise
20163 -- legal for a discriminant.
20164
20165 elsif Null_Exclusion_Present (Discr)
20166 and then Is_Discrete_Type (Discr_Type)
20167 then
20168 Error_Msg_N
20169 ("null exclusion can only apply to an access type", Discr);
20170 end if;
20171
20172 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20173 -- can't have defaults. Synchronized types, or types that are
20174 -- explicitly limited are fine, but special tests apply to derived
20175 -- types in generics: in a generic body we have to assume the
20176 -- worst, and therefore defaults are not allowed if the parent is
20177 -- a generic formal private type (see ACATS B370001).
20178
20179 if Is_Access_Type (Discr_Type) and then Default_Present then
20180 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20181 or else Is_Limited_Record (Current_Scope)
20182 or else Is_Concurrent_Type (Current_Scope)
20183 or else Is_Concurrent_Record_Type (Current_Scope)
20184 or else Ekind (Current_Scope) = E_Limited_Private_Type
20185 then
20186 if not Is_Derived_Type (Current_Scope)
20187 or else not Is_Generic_Type (Etype (Current_Scope))
20188 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20189 or else Limited_Present
20190 (Type_Definition (Parent (Current_Scope)))
20191 then
20192 null;
20193
20194 else
20195 Error_Msg_N
20196 ("access discriminants of nonlimited types cannot "
20197 & "have defaults", Expression (Discr));
20198 end if;
20199
20200 elsif Present (Expression (Discr)) then
20201 Error_Msg_N
20202 ("(Ada 2005) access discriminants of nonlimited types "
20203 & "cannot have defaults", Expression (Discr));
20204 end if;
20205 end if;
20206 end if;
20207
20208 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20209 -- This check is relevant only when SPARK_Mode is on as it is not a
20210 -- standard Ada legality rule.
20211
20212 if SPARK_Mode = On
20213 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20214 then
20215 Error_Msg_N ("discriminant cannot be volatile", Discr);
20216 end if;
20217
20218 Next (Discr);
20219 end loop;
20220
20221 -- An element list consisting of the default expressions of the
20222 -- discriminants is constructed in the above loop and used to set
20223 -- the Discriminant_Constraint attribute for the type. If an object
20224 -- is declared of this (record or task) type without any explicit
20225 -- discriminant constraint given, this element list will form the
20226 -- actual parameters for the corresponding initialization procedure
20227 -- for the type.
20228
20229 Set_Discriminant_Constraint (Current_Scope, Elist);
20230 Set_Stored_Constraint (Current_Scope, No_Elist);
20231
20232 -- Default expressions must be provided either for all or for none
20233 -- of the discriminants of a discriminant part. (RM 3.7.1)
20234
20235 if Default_Present and then Default_Not_Present then
20236 Error_Msg_N
20237 ("incomplete specification of defaults for discriminants", N);
20238 end if;
20239
20240 -- The use of the name of a discriminant is not allowed in default
20241 -- expressions of a discriminant part if the specification of the
20242 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20243
20244 -- To detect this, the discriminant names are entered initially with an
20245 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20246 -- attempt to use a void entity (for example in an expression that is
20247 -- type-checked) produces the error message: premature usage. Now after
20248 -- completing the semantic analysis of the discriminant part, we can set
20249 -- the Ekind of all the discriminants appropriately.
20250
20251 Discr := First (Discriminant_Specifications (N));
20252 Discr_Number := Uint_1;
20253 while Present (Discr) loop
20254 Id := Defining_Identifier (Discr);
20255 Set_Ekind (Id, E_Discriminant);
20256 Init_Component_Location (Id);
20257 Init_Esize (Id);
20258 Set_Discriminant_Number (Id, Discr_Number);
20259
20260 -- Make sure this is always set, even in illegal programs
20261
20262 Set_Corresponding_Discriminant (Id, Empty);
20263
20264 -- Initialize the Original_Record_Component to the entity itself.
20265 -- Inherit_Components will propagate the right value to
20266 -- discriminants in derived record types.
20267
20268 Set_Original_Record_Component (Id, Id);
20269
20270 -- Create the discriminal for the discriminant
20271
20272 Build_Discriminal (Id);
20273
20274 Next (Discr);
20275 Discr_Number := Discr_Number + 1;
20276 end loop;
20277
20278 Set_Has_Discriminants (Current_Scope);
20279 end Process_Discriminants;
20280
20281 -----------------------
20282 -- Process_Full_View --
20283 -----------------------
20284
20285 -- WARNING: This routine manages Ghost regions. Return statements must be
20286 -- replaced by gotos which jump to the end of the routine and restore the
20287 -- Ghost mode.
20288
20289 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20290 procedure Collect_Implemented_Interfaces
20291 (Typ : Entity_Id;
20292 Ifaces : Elist_Id);
20293 -- Ada 2005: Gather all the interfaces that Typ directly or
20294 -- inherently implements. Duplicate entries are not added to
20295 -- the list Ifaces.
20296
20297 ------------------------------------
20298 -- Collect_Implemented_Interfaces --
20299 ------------------------------------
20300
20301 procedure Collect_Implemented_Interfaces
20302 (Typ : Entity_Id;
20303 Ifaces : Elist_Id)
20304 is
20305 Iface : Entity_Id;
20306 Iface_Elmt : Elmt_Id;
20307
20308 begin
20309 -- Abstract interfaces are only associated with tagged record types
20310
20311 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20312 return;
20313 end if;
20314
20315 -- Recursively climb to the ancestors
20316
20317 if Etype (Typ) /= Typ
20318
20319 -- Protect the frontend against wrong cyclic declarations like:
20320
20321 -- type B is new A with private;
20322 -- type C is new A with private;
20323 -- private
20324 -- type B is new C with null record;
20325 -- type C is new B with null record;
20326
20327 and then Etype (Typ) /= Priv_T
20328 and then Etype (Typ) /= Full_T
20329 then
20330 -- Keep separate the management of private type declarations
20331
20332 if Ekind (Typ) = E_Record_Type_With_Private then
20333
20334 -- Handle the following illegal usage:
20335 -- type Private_Type is tagged private;
20336 -- private
20337 -- type Private_Type is new Type_Implementing_Iface;
20338
20339 if Present (Full_View (Typ))
20340 and then Etype (Typ) /= Full_View (Typ)
20341 then
20342 if Is_Interface (Etype (Typ)) then
20343 Append_Unique_Elmt (Etype (Typ), Ifaces);
20344 end if;
20345
20346 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20347 end if;
20348
20349 -- Non-private types
20350
20351 else
20352 if Is_Interface (Etype (Typ)) then
20353 Append_Unique_Elmt (Etype (Typ), Ifaces);
20354 end if;
20355
20356 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20357 end if;
20358 end if;
20359
20360 -- Handle entities in the list of abstract interfaces
20361
20362 if Present (Interfaces (Typ)) then
20363 Iface_Elmt := First_Elmt (Interfaces (Typ));
20364 while Present (Iface_Elmt) loop
20365 Iface := Node (Iface_Elmt);
20366
20367 pragma Assert (Is_Interface (Iface));
20368
20369 if not Contain_Interface (Iface, Ifaces) then
20370 Append_Elmt (Iface, Ifaces);
20371 Collect_Implemented_Interfaces (Iface, Ifaces);
20372 end if;
20373
20374 Next_Elmt (Iface_Elmt);
20375 end loop;
20376 end if;
20377 end Collect_Implemented_Interfaces;
20378
20379 -- Local variables
20380
20381 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20382 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20383 -- Save the Ghost-related attributes to restore on exit
20384
20385 Full_Indic : Node_Id;
20386 Full_Parent : Entity_Id;
20387 Priv_Parent : Entity_Id;
20388
20389 -- Start of processing for Process_Full_View
20390
20391 begin
20392 Mark_And_Set_Ghost_Completion (N, Priv_T);
20393
20394 -- First some sanity checks that must be done after semantic
20395 -- decoration of the full view and thus cannot be placed with other
20396 -- similar checks in Find_Type_Name
20397
20398 if not Is_Limited_Type (Priv_T)
20399 and then (Is_Limited_Type (Full_T)
20400 or else Is_Limited_Composite (Full_T))
20401 then
20402 if In_Instance then
20403 null;
20404 else
20405 Error_Msg_N
20406 ("completion of nonlimited type cannot be limited", Full_T);
20407 Explain_Limited_Type (Full_T, Full_T);
20408 end if;
20409
20410 elsif Is_Abstract_Type (Full_T)
20411 and then not Is_Abstract_Type (Priv_T)
20412 then
20413 Error_Msg_N
20414 ("completion of nonabstract type cannot be abstract", Full_T);
20415
20416 elsif Is_Tagged_Type (Priv_T)
20417 and then Is_Limited_Type (Priv_T)
20418 and then not Is_Limited_Type (Full_T)
20419 then
20420 -- If pragma CPP_Class was applied to the private declaration
20421 -- propagate the limitedness to the full-view
20422
20423 if Is_CPP_Class (Priv_T) then
20424 Set_Is_Limited_Record (Full_T);
20425
20426 -- GNAT allow its own definition of Limited_Controlled to disobey
20427 -- this rule in order in ease the implementation. This test is safe
20428 -- because Root_Controlled is defined in a child of System that
20429 -- normal programs are not supposed to use.
20430
20431 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20432 Set_Is_Limited_Composite (Full_T);
20433 else
20434 Error_Msg_N
20435 ("completion of limited tagged type must be limited", Full_T);
20436 end if;
20437
20438 elsif Is_Generic_Type (Priv_T) then
20439 Error_Msg_N ("generic type cannot have a completion", Full_T);
20440 end if;
20441
20442 -- Check that ancestor interfaces of private and full views are
20443 -- consistent. We omit this check for synchronized types because
20444 -- they are performed on the corresponding record type when frozen.
20445
20446 if Ada_Version >= Ada_2005
20447 and then Is_Tagged_Type (Priv_T)
20448 and then Is_Tagged_Type (Full_T)
20449 and then not Is_Concurrent_Type (Full_T)
20450 then
20451 declare
20452 Iface : Entity_Id;
20453 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20454 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20455
20456 begin
20457 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20458 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20459
20460 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20461 -- an interface type if and only if the full type is descendant
20462 -- of the interface type (AARM 7.3 (7.3/2)).
20463
20464 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20465
20466 if Present (Iface) then
20467 Error_Msg_NE
20468 ("interface in partial view& not implemented by full type "
20469 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20470 end if;
20471
20472 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20473
20474 if Present (Iface) then
20475 Error_Msg_NE
20476 ("interface & not implemented by partial view "
20477 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20478 end if;
20479 end;
20480 end if;
20481
20482 if Is_Tagged_Type (Priv_T)
20483 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20484 and then Is_Derived_Type (Full_T)
20485 then
20486 Priv_Parent := Etype (Priv_T);
20487
20488 -- The full view of a private extension may have been transformed
20489 -- into an unconstrained derived type declaration and a subtype
20490 -- declaration (see build_derived_record_type for details).
20491
20492 if Nkind (N) = N_Subtype_Declaration then
20493 Full_Indic := Subtype_Indication (N);
20494 Full_Parent := Etype (Base_Type (Full_T));
20495 else
20496 Full_Indic := Subtype_Indication (Type_Definition (N));
20497 Full_Parent := Etype (Full_T);
20498 end if;
20499
20500 -- Check that the parent type of the full type is a descendant of
20501 -- the ancestor subtype given in the private extension. If either
20502 -- entity has an Etype equal to Any_Type then we had some previous
20503 -- error situation [7.3(8)].
20504
20505 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20506 goto Leave;
20507
20508 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20509 -- any order. Therefore we don't have to check that its parent must
20510 -- be a descendant of the parent of the private type declaration.
20511
20512 elsif Is_Interface (Priv_Parent)
20513 and then Is_Interface (Full_Parent)
20514 then
20515 null;
20516
20517 -- Ada 2005 (AI-251): If the parent of the private type declaration
20518 -- is an interface there is no need to check that it is an ancestor
20519 -- of the associated full type declaration. The required tests for
20520 -- this case are performed by Build_Derived_Record_Type.
20521
20522 elsif not Is_Interface (Base_Type (Priv_Parent))
20523 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20524 then
20525 Error_Msg_N
20526 ("parent of full type must descend from parent of private "
20527 & "extension", Full_Indic);
20528
20529 -- First check a formal restriction, and then proceed with checking
20530 -- Ada rules. Since the formal restriction is not a serious error, we
20531 -- don't prevent further error detection for this check, hence the
20532 -- ELSE.
20533
20534 else
20535 -- In formal mode, when completing a private extension the type
20536 -- named in the private part must be exactly the same as that
20537 -- named in the visible part.
20538
20539 if Priv_Parent /= Full_Parent then
20540 Error_Msg_Name_1 := Chars (Priv_Parent);
20541 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20542 end if;
20543
20544 -- Check the rules of 7.3(10): if the private extension inherits
20545 -- known discriminants, then the full type must also inherit those
20546 -- discriminants from the same (ancestor) type, and the parent
20547 -- subtype of the full type must be constrained if and only if
20548 -- the ancestor subtype of the private extension is constrained.
20549
20550 if No (Discriminant_Specifications (Parent (Priv_T)))
20551 and then not Has_Unknown_Discriminants (Priv_T)
20552 and then Has_Discriminants (Base_Type (Priv_Parent))
20553 then
20554 declare
20555 Priv_Indic : constant Node_Id :=
20556 Subtype_Indication (Parent (Priv_T));
20557
20558 Priv_Constr : constant Boolean :=
20559 Is_Constrained (Priv_Parent)
20560 or else
20561 Nkind (Priv_Indic) = N_Subtype_Indication
20562 or else
20563 Is_Constrained (Entity (Priv_Indic));
20564
20565 Full_Constr : constant Boolean :=
20566 Is_Constrained (Full_Parent)
20567 or else
20568 Nkind (Full_Indic) = N_Subtype_Indication
20569 or else
20570 Is_Constrained (Entity (Full_Indic));
20571
20572 Priv_Discr : Entity_Id;
20573 Full_Discr : Entity_Id;
20574
20575 begin
20576 Priv_Discr := First_Discriminant (Priv_Parent);
20577 Full_Discr := First_Discriminant (Full_Parent);
20578 while Present (Priv_Discr) and then Present (Full_Discr) loop
20579 if Original_Record_Component (Priv_Discr) =
20580 Original_Record_Component (Full_Discr)
20581 or else
20582 Corresponding_Discriminant (Priv_Discr) =
20583 Corresponding_Discriminant (Full_Discr)
20584 then
20585 null;
20586 else
20587 exit;
20588 end if;
20589
20590 Next_Discriminant (Priv_Discr);
20591 Next_Discriminant (Full_Discr);
20592 end loop;
20593
20594 if Present (Priv_Discr) or else Present (Full_Discr) then
20595 Error_Msg_N
20596 ("full view must inherit discriminants of the parent "
20597 & "type used in the private extension", Full_Indic);
20598
20599 elsif Priv_Constr and then not Full_Constr then
20600 Error_Msg_N
20601 ("parent subtype of full type must be constrained",
20602 Full_Indic);
20603
20604 elsif Full_Constr and then not Priv_Constr then
20605 Error_Msg_N
20606 ("parent subtype of full type must be unconstrained",
20607 Full_Indic);
20608 end if;
20609 end;
20610
20611 -- Check the rules of 7.3(12): if a partial view has neither
20612 -- known or unknown discriminants, then the full type
20613 -- declaration shall define a definite subtype.
20614
20615 elsif not Has_Unknown_Discriminants (Priv_T)
20616 and then not Has_Discriminants (Priv_T)
20617 and then not Is_Constrained (Full_T)
20618 then
20619 Error_Msg_N
20620 ("full view must define a constrained type if partial view "
20621 & "has no discriminants", Full_T);
20622 end if;
20623
20624 -- ??????? Do we implement the following properly ?????
20625 -- If the ancestor subtype of a private extension has constrained
20626 -- discriminants, then the parent subtype of the full view shall
20627 -- impose a statically matching constraint on those discriminants
20628 -- [7.3(13)].
20629 end if;
20630
20631 else
20632 -- For untagged types, verify that a type without discriminants is
20633 -- not completed with an unconstrained type. A separate error message
20634 -- is produced if the full type has defaulted discriminants.
20635
20636 if Is_Definite_Subtype (Priv_T)
20637 and then not Is_Definite_Subtype (Full_T)
20638 then
20639 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20640 Error_Msg_NE
20641 ("full view of& not compatible with declaration#",
20642 Full_T, Priv_T);
20643
20644 if not Is_Tagged_Type (Full_T) then
20645 Error_Msg_N
20646 ("\one is constrained, the other unconstrained", Full_T);
20647 end if;
20648 end if;
20649 end if;
20650
20651 -- AI-419: verify that the use of "limited" is consistent
20652
20653 declare
20654 Orig_Decl : constant Node_Id := Original_Node (N);
20655
20656 begin
20657 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20658 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20659 and then Nkind
20660 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20661 then
20662 if not Limited_Present (Parent (Priv_T))
20663 and then not Synchronized_Present (Parent (Priv_T))
20664 and then Limited_Present (Type_Definition (Orig_Decl))
20665 then
20666 Error_Msg_N
20667 ("full view of non-limited extension cannot be limited", N);
20668
20669 -- Conversely, if the partial view carries the limited keyword,
20670 -- the full view must as well, even if it may be redundant.
20671
20672 elsif Limited_Present (Parent (Priv_T))
20673 and then not Limited_Present (Type_Definition (Orig_Decl))
20674 then
20675 Error_Msg_N
20676 ("full view of limited extension must be explicitly limited",
20677 N);
20678 end if;
20679 end if;
20680 end;
20681
20682 -- Ada 2005 (AI-443): A synchronized private extension must be
20683 -- completed by a task or protected type.
20684
20685 if Ada_Version >= Ada_2005
20686 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20687 and then Synchronized_Present (Parent (Priv_T))
20688 and then not Is_Concurrent_Type (Full_T)
20689 then
20690 Error_Msg_N ("full view of synchronized extension must " &
20691 "be synchronized type", N);
20692 end if;
20693
20694 -- Ada 2005 AI-363: if the full view has discriminants with
20695 -- defaults, it is illegal to declare constrained access subtypes
20696 -- whose designated type is the current type. This allows objects
20697 -- of the type that are declared in the heap to be unconstrained.
20698
20699 if not Has_Unknown_Discriminants (Priv_T)
20700 and then not Has_Discriminants (Priv_T)
20701 and then Has_Discriminants (Full_T)
20702 and then
20703 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20704 then
20705 Set_Has_Constrained_Partial_View (Full_T);
20706 Set_Has_Constrained_Partial_View (Priv_T);
20707 end if;
20708
20709 -- Create a full declaration for all its subtypes recorded in
20710 -- Private_Dependents and swap them similarly to the base type. These
20711 -- are subtypes that have been define before the full declaration of
20712 -- the private type. We also swap the entry in Private_Dependents list
20713 -- so we can properly restore the private view on exit from the scope.
20714
20715 declare
20716 Priv_Elmt : Elmt_Id;
20717 Priv_Scop : Entity_Id;
20718 Priv : Entity_Id;
20719 Full : Entity_Id;
20720
20721 begin
20722 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20723 while Present (Priv_Elmt) loop
20724 Priv := Node (Priv_Elmt);
20725 Priv_Scop := Scope (Priv);
20726
20727 if Ekind_In (Priv, E_Private_Subtype,
20728 E_Limited_Private_Subtype,
20729 E_Record_Subtype_With_Private)
20730 then
20731 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20732 Set_Is_Itype (Full);
20733 Set_Parent (Full, Parent (Priv));
20734 Set_Associated_Node_For_Itype (Full, N);
20735
20736 -- Now we need to complete the private subtype, but since the
20737 -- base type has already been swapped, we must also swap the
20738 -- subtypes (and thus, reverse the arguments in the call to
20739 -- Complete_Private_Subtype). Also note that we may need to
20740 -- re-establish the scope of the private subtype.
20741
20742 Copy_And_Swap (Priv, Full);
20743
20744 if not In_Open_Scopes (Priv_Scop) then
20745 Push_Scope (Priv_Scop);
20746
20747 else
20748 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20749
20750 Priv_Scop := Empty;
20751 end if;
20752
20753 Complete_Private_Subtype (Full, Priv, Full_T, N);
20754
20755 if Present (Priv_Scop) then
20756 Pop_Scope;
20757 end if;
20758
20759 Replace_Elmt (Priv_Elmt, Full);
20760 end if;
20761
20762 Next_Elmt (Priv_Elmt);
20763 end loop;
20764 end;
20765
20766 -- If the private view was tagged, copy the new primitive operations
20767 -- from the private view to the full view.
20768
20769 if Is_Tagged_Type (Full_T) then
20770 declare
20771 Disp_Typ : Entity_Id;
20772 Full_List : Elist_Id;
20773 Prim : Entity_Id;
20774 Prim_Elmt : Elmt_Id;
20775 Priv_List : Elist_Id;
20776
20777 function Contains
20778 (E : Entity_Id;
20779 L : Elist_Id) return Boolean;
20780 -- Determine whether list L contains element E
20781
20782 --------------
20783 -- Contains --
20784 --------------
20785
20786 function Contains
20787 (E : Entity_Id;
20788 L : Elist_Id) return Boolean
20789 is
20790 List_Elmt : Elmt_Id;
20791
20792 begin
20793 List_Elmt := First_Elmt (L);
20794 while Present (List_Elmt) loop
20795 if Node (List_Elmt) = E then
20796 return True;
20797 end if;
20798
20799 Next_Elmt (List_Elmt);
20800 end loop;
20801
20802 return False;
20803 end Contains;
20804
20805 -- Start of processing
20806
20807 begin
20808 if Is_Tagged_Type (Priv_T) then
20809 Priv_List := Primitive_Operations (Priv_T);
20810 Prim_Elmt := First_Elmt (Priv_List);
20811
20812 -- In the case of a concurrent type completing a private tagged
20813 -- type, primitives may have been declared in between the two
20814 -- views. These subprograms need to be wrapped the same way
20815 -- entries and protected procedures are handled because they
20816 -- cannot be directly shared by the two views.
20817
20818 if Is_Concurrent_Type (Full_T) then
20819 declare
20820 Conc_Typ : constant Entity_Id :=
20821 Corresponding_Record_Type (Full_T);
20822 Curr_Nod : Node_Id := Parent (Conc_Typ);
20823 Wrap_Spec : Node_Id;
20824
20825 begin
20826 while Present (Prim_Elmt) loop
20827 Prim := Node (Prim_Elmt);
20828
20829 if Comes_From_Source (Prim)
20830 and then not Is_Abstract_Subprogram (Prim)
20831 then
20832 Wrap_Spec :=
20833 Make_Subprogram_Declaration (Sloc (Prim),
20834 Specification =>
20835 Build_Wrapper_Spec
20836 (Subp_Id => Prim,
20837 Obj_Typ => Conc_Typ,
20838 Formals =>
20839 Parameter_Specifications
20840 (Parent (Prim))));
20841
20842 Insert_After (Curr_Nod, Wrap_Spec);
20843 Curr_Nod := Wrap_Spec;
20844
20845 Analyze (Wrap_Spec);
20846
20847 -- Remove the wrapper from visibility to avoid
20848 -- spurious conflict with the wrapped entity.
20849
20850 Set_Is_Immediately_Visible
20851 (Defining_Entity (Specification (Wrap_Spec)),
20852 False);
20853 end if;
20854
20855 Next_Elmt (Prim_Elmt);
20856 end loop;
20857
20858 goto Leave;
20859 end;
20860
20861 -- For non-concurrent types, transfer explicit primitives, but
20862 -- omit those inherited from the parent of the private view
20863 -- since they will be re-inherited later on.
20864
20865 else
20866 Full_List := Primitive_Operations (Full_T);
20867 while Present (Prim_Elmt) loop
20868 Prim := Node (Prim_Elmt);
20869
20870 if Comes_From_Source (Prim)
20871 and then not Contains (Prim, Full_List)
20872 then
20873 Append_Elmt (Prim, Full_List);
20874 end if;
20875
20876 Next_Elmt (Prim_Elmt);
20877 end loop;
20878 end if;
20879
20880 -- Untagged private view
20881
20882 else
20883 Full_List := Primitive_Operations (Full_T);
20884
20885 -- In this case the partial view is untagged, so here we locate
20886 -- all of the earlier primitives that need to be treated as
20887 -- dispatching (those that appear between the two views). Note
20888 -- that these additional operations must all be new operations
20889 -- (any earlier operations that override inherited operations
20890 -- of the full view will already have been inserted in the
20891 -- primitives list, marked by Check_Operation_From_Private_View
20892 -- as dispatching. Note that implicit "/=" operators are
20893 -- excluded from being added to the primitives list since they
20894 -- shouldn't be treated as dispatching (tagged "/=" is handled
20895 -- specially).
20896
20897 Prim := Next_Entity (Full_T);
20898 while Present (Prim) and then Prim /= Priv_T loop
20899 if Ekind_In (Prim, E_Procedure, E_Function) then
20900 Disp_Typ := Find_Dispatching_Type (Prim);
20901
20902 if Disp_Typ = Full_T
20903 and then (Chars (Prim) /= Name_Op_Ne
20904 or else Comes_From_Source (Prim))
20905 then
20906 Check_Controlling_Formals (Full_T, Prim);
20907
20908 if Is_Suitable_Primitive (Prim)
20909 and then not Is_Dispatching_Operation (Prim)
20910 then
20911 Append_Elmt (Prim, Full_List);
20912 Set_Is_Dispatching_Operation (Prim);
20913 Set_DT_Position_Value (Prim, No_Uint);
20914 end if;
20915
20916 elsif Is_Dispatching_Operation (Prim)
20917 and then Disp_Typ /= Full_T
20918 then
20919 -- Verify that it is not otherwise controlled by a
20920 -- formal or a return value of type T.
20921
20922 Check_Controlling_Formals (Disp_Typ, Prim);
20923 end if;
20924 end if;
20925
20926 Next_Entity (Prim);
20927 end loop;
20928 end if;
20929
20930 -- For the tagged case, the two views can share the same primitive
20931 -- operations list and the same class-wide type. Update attributes
20932 -- of the class-wide type which depend on the full declaration.
20933
20934 if Is_Tagged_Type (Priv_T) then
20935 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20936 Set_Class_Wide_Type
20937 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20938
20939 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20940 end if;
20941 end;
20942 end if;
20943
20944 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20945
20946 if Known_To_Have_Preelab_Init (Priv_T) then
20947
20948 -- Case where there is a pragma Preelaborable_Initialization. We
20949 -- always allow this in predefined units, which is cheating a bit,
20950 -- but it means we don't have to struggle to meet the requirements in
20951 -- the RM for having Preelaborable Initialization. Otherwise we
20952 -- require that the type meets the RM rules. But we can't check that
20953 -- yet, because of the rule about overriding Initialize, so we simply
20954 -- set a flag that will be checked at freeze time.
20955
20956 if not In_Predefined_Unit (Full_T) then
20957 Set_Must_Have_Preelab_Init (Full_T);
20958 end if;
20959 end if;
20960
20961 -- If pragma CPP_Class was applied to the private type declaration,
20962 -- propagate it now to the full type declaration.
20963
20964 if Is_CPP_Class (Priv_T) then
20965 Set_Is_CPP_Class (Full_T);
20966 Set_Convention (Full_T, Convention_CPP);
20967
20968 -- Check that components of imported CPP types do not have default
20969 -- expressions.
20970
20971 Check_CPP_Type_Has_No_Defaults (Full_T);
20972 end if;
20973
20974 -- If the private view has user specified stream attributes, then so has
20975 -- the full view.
20976
20977 -- Why the test, how could these flags be already set in Full_T ???
20978
20979 if Has_Specified_Stream_Read (Priv_T) then
20980 Set_Has_Specified_Stream_Read (Full_T);
20981 end if;
20982
20983 if Has_Specified_Stream_Write (Priv_T) then
20984 Set_Has_Specified_Stream_Write (Full_T);
20985 end if;
20986
20987 if Has_Specified_Stream_Input (Priv_T) then
20988 Set_Has_Specified_Stream_Input (Full_T);
20989 end if;
20990
20991 if Has_Specified_Stream_Output (Priv_T) then
20992 Set_Has_Specified_Stream_Output (Full_T);
20993 end if;
20994
20995 -- Propagate Default_Initial_Condition-related attributes from the
20996 -- partial view to the full view and its base type.
20997
20998 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20999 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
21000
21001 -- Propagate invariant-related attributes from the partial view to the
21002 -- full view and its base type.
21003
21004 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21005 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
21006
21007 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21008 -- in the full view without advertising the inheritance in the partial
21009 -- view. This can only occur when the partial view has no parent type
21010 -- and the full view has an interface as a parent. Any other scenarios
21011 -- are illegal because implemented interfaces must match between the
21012 -- two views.
21013
21014 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21015 declare
21016 Full_Par : constant Entity_Id := Etype (Full_T);
21017 Priv_Par : constant Entity_Id := Etype (Priv_T);
21018
21019 begin
21020 if not Is_Interface (Priv_Par)
21021 and then Is_Interface (Full_Par)
21022 and then Has_Inheritable_Invariants (Full_Par)
21023 then
21024 Error_Msg_N
21025 ("hidden inheritance of class-wide type invariants not "
21026 & "allowed", N);
21027 end if;
21028 end;
21029 end if;
21030
21031 -- Propagate predicates to full type, and predicate function if already
21032 -- defined. It is not clear that this can actually happen? the partial
21033 -- view cannot be frozen yet, and the predicate function has not been
21034 -- built. Still it is a cheap check and seems safer to make it.
21035
21036 if Has_Predicates (Priv_T) then
21037 Set_Has_Predicates (Full_T);
21038
21039 if Present (Predicate_Function (Priv_T)) then
21040 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
21041 end if;
21042 end if;
21043
21044 <<Leave>>
21045 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21046 end Process_Full_View;
21047
21048 -----------------------------------
21049 -- Process_Incomplete_Dependents --
21050 -----------------------------------
21051
21052 procedure Process_Incomplete_Dependents
21053 (N : Node_Id;
21054 Full_T : Entity_Id;
21055 Inc_T : Entity_Id)
21056 is
21057 Inc_Elmt : Elmt_Id;
21058 Priv_Dep : Entity_Id;
21059 New_Subt : Entity_Id;
21060
21061 Disc_Constraint : Elist_Id;
21062
21063 begin
21064 if No (Private_Dependents (Inc_T)) then
21065 return;
21066 end if;
21067
21068 -- Itypes that may be generated by the completion of an incomplete
21069 -- subtype are not used by the back-end and not attached to the tree.
21070 -- They are created only for constraint-checking purposes.
21071
21072 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21073 while Present (Inc_Elmt) loop
21074 Priv_Dep := Node (Inc_Elmt);
21075
21076 if Ekind (Priv_Dep) = E_Subprogram_Type then
21077
21078 -- An Access_To_Subprogram type may have a return type or a
21079 -- parameter type that is incomplete. Replace with the full view.
21080
21081 if Etype (Priv_Dep) = Inc_T then
21082 Set_Etype (Priv_Dep, Full_T);
21083 end if;
21084
21085 declare
21086 Formal : Entity_Id;
21087
21088 begin
21089 Formal := First_Formal (Priv_Dep);
21090 while Present (Formal) loop
21091 if Etype (Formal) = Inc_T then
21092 Set_Etype (Formal, Full_T);
21093 end if;
21094
21095 Next_Formal (Formal);
21096 end loop;
21097 end;
21098
21099 elsif Is_Overloadable (Priv_Dep) then
21100
21101 -- If a subprogram in the incomplete dependents list is primitive
21102 -- for a tagged full type then mark it as a dispatching operation,
21103 -- check whether it overrides an inherited subprogram, and check
21104 -- restrictions on its controlling formals. Note that a protected
21105 -- operation is never dispatching: only its wrapper operation
21106 -- (which has convention Ada) is.
21107
21108 if Is_Tagged_Type (Full_T)
21109 and then Is_Primitive (Priv_Dep)
21110 and then Convention (Priv_Dep) /= Convention_Protected
21111 then
21112 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21113 Set_Is_Dispatching_Operation (Priv_Dep);
21114 Check_Controlling_Formals (Full_T, Priv_Dep);
21115 end if;
21116
21117 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21118
21119 -- Can happen during processing of a body before the completion
21120 -- of a TA type. Ignore, because spec is also on dependent list.
21121
21122 return;
21123
21124 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21125 -- corresponding subtype of the full view.
21126
21127 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21128 and then Comes_From_Source (Priv_Dep)
21129 then
21130 Set_Subtype_Indication
21131 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21132 Set_Etype (Priv_Dep, Full_T);
21133 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21134 Set_Analyzed (Parent (Priv_Dep), False);
21135
21136 -- Reanalyze the declaration, suppressing the call to Enter_Name
21137 -- to avoid duplicate names.
21138
21139 Analyze_Subtype_Declaration
21140 (N => Parent (Priv_Dep),
21141 Skip => True);
21142
21143 -- Dependent is a subtype
21144
21145 else
21146 -- We build a new subtype indication using the full view of the
21147 -- incomplete parent. The discriminant constraints have been
21148 -- elaborated already at the point of the subtype declaration.
21149
21150 New_Subt := Create_Itype (E_Void, N);
21151
21152 if Has_Discriminants (Full_T) then
21153 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21154 else
21155 Disc_Constraint := No_Elist;
21156 end if;
21157
21158 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21159 Set_Full_View (Priv_Dep, New_Subt);
21160 end if;
21161
21162 Next_Elmt (Inc_Elmt);
21163 end loop;
21164 end Process_Incomplete_Dependents;
21165
21166 --------------------------------
21167 -- Process_Range_Expr_In_Decl --
21168 --------------------------------
21169
21170 procedure Process_Range_Expr_In_Decl
21171 (R : Node_Id;
21172 T : Entity_Id;
21173 Subtyp : Entity_Id := Empty;
21174 Check_List : List_Id := Empty_List;
21175 R_Check_Off : Boolean := False;
21176 In_Iter_Schm : Boolean := False)
21177 is
21178 Lo, Hi : Node_Id;
21179 R_Checks : Check_Result;
21180 Insert_Node : Node_Id;
21181 Def_Id : Entity_Id;
21182
21183 begin
21184 Analyze_And_Resolve (R, Base_Type (T));
21185
21186 if Nkind (R) = N_Range then
21187
21188 -- In SPARK, all ranges should be static, with the exception of the
21189 -- discrete type definition of a loop parameter specification.
21190
21191 if not In_Iter_Schm
21192 and then not Is_OK_Static_Range (R)
21193 then
21194 Check_SPARK_05_Restriction ("range should be static", R);
21195 end if;
21196
21197 Lo := Low_Bound (R);
21198 Hi := High_Bound (R);
21199
21200 -- Validity checks on the range of a quantified expression are
21201 -- delayed until the construct is transformed into a loop.
21202
21203 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21204 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21205 then
21206 null;
21207
21208 -- We need to ensure validity of the bounds here, because if we
21209 -- go ahead and do the expansion, then the expanded code will get
21210 -- analyzed with range checks suppressed and we miss the check.
21211
21212 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21213 -- the temporaries generated by routine Remove_Side_Effects by means
21214 -- of validity checks must use the same names. When a range appears
21215 -- in the parent of a generic, the range is processed with checks
21216 -- disabled as part of the generic context and with checks enabled
21217 -- for code generation purposes. This leads to link issues as the
21218 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21219 -- template sees the temporaries generated by Remove_Side_Effects.
21220
21221 else
21222 Validity_Check_Range (R, Subtyp);
21223 end if;
21224
21225 -- If there were errors in the declaration, try and patch up some
21226 -- common mistakes in the bounds. The cases handled are literals
21227 -- which are Integer where the expected type is Real and vice versa.
21228 -- These corrections allow the compilation process to proceed further
21229 -- along since some basic assumptions of the format of the bounds
21230 -- are guaranteed.
21231
21232 if Etype (R) = Any_Type then
21233 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21234 Rewrite (Lo,
21235 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21236
21237 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21238 Rewrite (Hi,
21239 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21240
21241 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21242 Rewrite (Lo,
21243 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21244
21245 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21246 Rewrite (Hi,
21247 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21248 end if;
21249
21250 Set_Etype (Lo, T);
21251 Set_Etype (Hi, T);
21252 end if;
21253
21254 -- If the bounds of the range have been mistakenly given as string
21255 -- literals (perhaps in place of character literals), then an error
21256 -- has already been reported, but we rewrite the string literal as a
21257 -- bound of the range's type to avoid blowups in later processing
21258 -- that looks at static values.
21259
21260 if Nkind (Lo) = N_String_Literal then
21261 Rewrite (Lo,
21262 Make_Attribute_Reference (Sloc (Lo),
21263 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21264 Attribute_Name => Name_First));
21265 Analyze_And_Resolve (Lo);
21266 end if;
21267
21268 if Nkind (Hi) = N_String_Literal then
21269 Rewrite (Hi,
21270 Make_Attribute_Reference (Sloc (Hi),
21271 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21272 Attribute_Name => Name_First));
21273 Analyze_And_Resolve (Hi);
21274 end if;
21275
21276 -- If bounds aren't scalar at this point then exit, avoiding
21277 -- problems with further processing of the range in this procedure.
21278
21279 if not Is_Scalar_Type (Etype (Lo)) then
21280 return;
21281 end if;
21282
21283 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21284 -- then range of the base type. Here we check whether the bounds
21285 -- are in the range of the subtype itself. Note that if the bounds
21286 -- represent the null range the Constraint_Error exception should
21287 -- not be raised.
21288
21289 -- ??? The following code should be cleaned up as follows
21290
21291 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21292 -- is done in the call to Range_Check (R, T); below
21293
21294 -- 2. The use of R_Check_Off should be investigated and possibly
21295 -- removed, this would clean up things a bit.
21296
21297 if Is_Null_Range (Lo, Hi) then
21298 null;
21299
21300 else
21301 -- Capture values of bounds and generate temporaries for them
21302 -- if needed, before applying checks, since checks may cause
21303 -- duplication of the expression without forcing evaluation.
21304
21305 -- The forced evaluation removes side effects from expressions,
21306 -- which should occur also in GNATprove mode. Otherwise, we end up
21307 -- with unexpected insertions of actions at places where this is
21308 -- not supposed to occur, e.g. on default parameters of a call.
21309
21310 if Expander_Active or GNATprove_Mode then
21311
21312 -- Call Force_Evaluation to create declarations as needed to
21313 -- deal with side effects, and also create typ_FIRST/LAST
21314 -- entities for bounds if we have a subtype name.
21315
21316 -- Note: we do this transformation even if expansion is not
21317 -- active if we are in GNATprove_Mode since the transformation
21318 -- is in general required to ensure that the resulting tree has
21319 -- proper Ada semantics.
21320
21321 Force_Evaluation
21322 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21323 Force_Evaluation
21324 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21325 end if;
21326
21327 -- We use a flag here instead of suppressing checks on the type
21328 -- because the type we check against isn't necessarily the place
21329 -- where we put the check.
21330
21331 if not R_Check_Off then
21332 R_Checks := Get_Range_Checks (R, T);
21333
21334 -- Look up tree to find an appropriate insertion point. We
21335 -- can't just use insert_actions because later processing
21336 -- depends on the insertion node. Prior to Ada 2012 the
21337 -- insertion point could only be a declaration or a loop, but
21338 -- quantified expressions can appear within any context in an
21339 -- expression, and the insertion point can be any statement,
21340 -- pragma, or declaration.
21341
21342 Insert_Node := Parent (R);
21343 while Present (Insert_Node) loop
21344 exit when
21345 Nkind (Insert_Node) in N_Declaration
21346 and then
21347 not Nkind_In
21348 (Insert_Node, N_Component_Declaration,
21349 N_Loop_Parameter_Specification,
21350 N_Function_Specification,
21351 N_Procedure_Specification);
21352
21353 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21354 or else Nkind (Insert_Node) in
21355 N_Statement_Other_Than_Procedure_Call
21356 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21357 N_Pragma);
21358
21359 Insert_Node := Parent (Insert_Node);
21360 end loop;
21361
21362 -- Why would Type_Decl not be present??? Without this test,
21363 -- short regression tests fail.
21364
21365 if Present (Insert_Node) then
21366
21367 -- Case of loop statement. Verify that the range is part
21368 -- of the subtype indication of the iteration scheme.
21369
21370 if Nkind (Insert_Node) = N_Loop_Statement then
21371 declare
21372 Indic : Node_Id;
21373
21374 begin
21375 Indic := Parent (R);
21376 while Present (Indic)
21377 and then Nkind (Indic) /= N_Subtype_Indication
21378 loop
21379 Indic := Parent (Indic);
21380 end loop;
21381
21382 if Present (Indic) then
21383 Def_Id := Etype (Subtype_Mark (Indic));
21384
21385 Insert_Range_Checks
21386 (R_Checks,
21387 Insert_Node,
21388 Def_Id,
21389 Sloc (Insert_Node),
21390 R,
21391 Do_Before => True);
21392 end if;
21393 end;
21394
21395 -- Insertion before a declaration. If the declaration
21396 -- includes discriminants, the list of applicable checks
21397 -- is given by the caller.
21398
21399 elsif Nkind (Insert_Node) in N_Declaration then
21400 Def_Id := Defining_Identifier (Insert_Node);
21401
21402 if (Ekind (Def_Id) = E_Record_Type
21403 and then Depends_On_Discriminant (R))
21404 or else
21405 (Ekind (Def_Id) = E_Protected_Type
21406 and then Has_Discriminants (Def_Id))
21407 then
21408 Append_Range_Checks
21409 (R_Checks,
21410 Check_List, Def_Id, Sloc (Insert_Node), R);
21411
21412 else
21413 Insert_Range_Checks
21414 (R_Checks,
21415 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21416
21417 end if;
21418
21419 -- Insertion before a statement. Range appears in the
21420 -- context of a quantified expression. Insertion will
21421 -- take place when expression is expanded.
21422
21423 else
21424 null;
21425 end if;
21426 end if;
21427 end if;
21428 end if;
21429
21430 -- Case of other than an explicit N_Range node
21431
21432 -- The forced evaluation removes side effects from expressions, which
21433 -- should occur also in GNATprove mode. Otherwise, we end up with
21434 -- unexpected insertions of actions at places where this is not
21435 -- supposed to occur, e.g. on default parameters of a call.
21436
21437 elsif Expander_Active or GNATprove_Mode then
21438 Get_Index_Bounds (R, Lo, Hi);
21439 Force_Evaluation (Lo);
21440 Force_Evaluation (Hi);
21441 end if;
21442 end Process_Range_Expr_In_Decl;
21443
21444 --------------------------------------
21445 -- Process_Real_Range_Specification --
21446 --------------------------------------
21447
21448 procedure Process_Real_Range_Specification (Def : Node_Id) is
21449 Spec : constant Node_Id := Real_Range_Specification (Def);
21450 Lo : Node_Id;
21451 Hi : Node_Id;
21452 Err : Boolean := False;
21453
21454 procedure Analyze_Bound (N : Node_Id);
21455 -- Analyze and check one bound
21456
21457 -------------------
21458 -- Analyze_Bound --
21459 -------------------
21460
21461 procedure Analyze_Bound (N : Node_Id) is
21462 begin
21463 Analyze_And_Resolve (N, Any_Real);
21464
21465 if not Is_OK_Static_Expression (N) then
21466 Flag_Non_Static_Expr
21467 ("bound in real type definition is not static!", N);
21468 Err := True;
21469 end if;
21470 end Analyze_Bound;
21471
21472 -- Start of processing for Process_Real_Range_Specification
21473
21474 begin
21475 if Present (Spec) then
21476 Lo := Low_Bound (Spec);
21477 Hi := High_Bound (Spec);
21478 Analyze_Bound (Lo);
21479 Analyze_Bound (Hi);
21480
21481 -- If error, clear away junk range specification
21482
21483 if Err then
21484 Set_Real_Range_Specification (Def, Empty);
21485 end if;
21486 end if;
21487 end Process_Real_Range_Specification;
21488
21489 ---------------------
21490 -- Process_Subtype --
21491 ---------------------
21492
21493 function Process_Subtype
21494 (S : Node_Id;
21495 Related_Nod : Node_Id;
21496 Related_Id : Entity_Id := Empty;
21497 Suffix : Character := ' ') return Entity_Id
21498 is
21499 P : Node_Id;
21500 Def_Id : Entity_Id;
21501 Error_Node : Node_Id;
21502 Full_View_Id : Entity_Id;
21503 Subtype_Mark_Id : Entity_Id;
21504
21505 May_Have_Null_Exclusion : Boolean;
21506
21507 procedure Check_Incomplete (T : Node_Id);
21508 -- Called to verify that an incomplete type is not used prematurely
21509
21510 ----------------------
21511 -- Check_Incomplete --
21512 ----------------------
21513
21514 procedure Check_Incomplete (T : Node_Id) is
21515 begin
21516 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21517
21518 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21519 and then
21520 not (Ada_Version >= Ada_2005
21521 and then
21522 (Nkind (Parent (T)) = N_Subtype_Declaration
21523 or else (Nkind (Parent (T)) = N_Subtype_Indication
21524 and then Nkind (Parent (Parent (T))) =
21525 N_Subtype_Declaration)))
21526 then
21527 Error_Msg_N ("invalid use of type before its full declaration", T);
21528 end if;
21529 end Check_Incomplete;
21530
21531 -- Start of processing for Process_Subtype
21532
21533 begin
21534 -- Case of no constraints present
21535
21536 if Nkind (S) /= N_Subtype_Indication then
21537 Find_Type (S);
21538
21539 -- No way to proceed if the subtype indication is malformed. This
21540 -- will happen for example when the subtype indication in an object
21541 -- declaration is missing altogether and the expression is analyzed
21542 -- as if it were that indication.
21543
21544 if not Is_Entity_Name (S) then
21545 return Any_Type;
21546 end if;
21547
21548 Check_Incomplete (S);
21549 P := Parent (S);
21550
21551 -- Ada 2005 (AI-231): Static check
21552
21553 if Ada_Version >= Ada_2005
21554 and then Present (P)
21555 and then Null_Exclusion_Present (P)
21556 and then Nkind (P) /= N_Access_To_Object_Definition
21557 and then not Is_Access_Type (Entity (S))
21558 then
21559 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21560 end if;
21561
21562 -- The following is ugly, can't we have a range or even a flag???
21563
21564 May_Have_Null_Exclusion :=
21565 Nkind_In (P, N_Access_Definition,
21566 N_Access_Function_Definition,
21567 N_Access_Procedure_Definition,
21568 N_Access_To_Object_Definition,
21569 N_Allocator,
21570 N_Component_Definition)
21571 or else
21572 Nkind_In (P, N_Derived_Type_Definition,
21573 N_Discriminant_Specification,
21574 N_Formal_Object_Declaration,
21575 N_Object_Declaration,
21576 N_Object_Renaming_Declaration,
21577 N_Parameter_Specification,
21578 N_Subtype_Declaration);
21579
21580 -- Create an Itype that is a duplicate of Entity (S) but with the
21581 -- null-exclusion attribute.
21582
21583 if May_Have_Null_Exclusion
21584 and then Is_Access_Type (Entity (S))
21585 and then Null_Exclusion_Present (P)
21586
21587 -- No need to check the case of an access to object definition.
21588 -- It is correct to define double not-null pointers.
21589
21590 -- Example:
21591 -- type Not_Null_Int_Ptr is not null access Integer;
21592 -- type Acc is not null access Not_Null_Int_Ptr;
21593
21594 and then Nkind (P) /= N_Access_To_Object_Definition
21595 then
21596 if Can_Never_Be_Null (Entity (S)) then
21597 case Nkind (Related_Nod) is
21598 when N_Full_Type_Declaration =>
21599 if Nkind (Type_Definition (Related_Nod))
21600 in N_Array_Type_Definition
21601 then
21602 Error_Node :=
21603 Subtype_Indication
21604 (Component_Definition
21605 (Type_Definition (Related_Nod)));
21606 else
21607 Error_Node :=
21608 Subtype_Indication (Type_Definition (Related_Nod));
21609 end if;
21610
21611 when N_Subtype_Declaration =>
21612 Error_Node := Subtype_Indication (Related_Nod);
21613
21614 when N_Object_Declaration =>
21615 Error_Node := Object_Definition (Related_Nod);
21616
21617 when N_Component_Declaration =>
21618 Error_Node :=
21619 Subtype_Indication (Component_Definition (Related_Nod));
21620
21621 when N_Allocator =>
21622 Error_Node := Expression (Related_Nod);
21623
21624 when others =>
21625 pragma Assert (False);
21626 Error_Node := Related_Nod;
21627 end case;
21628
21629 Error_Msg_NE
21630 ("`NOT NULL` not allowed (& already excludes null)",
21631 Error_Node,
21632 Entity (S));
21633 end if;
21634
21635 Set_Etype (S,
21636 Create_Null_Excluding_Itype
21637 (T => Entity (S),
21638 Related_Nod => P));
21639 Set_Entity (S, Etype (S));
21640 end if;
21641
21642 return Entity (S);
21643
21644 -- Case of constraint present, so that we have an N_Subtype_Indication
21645 -- node (this node is created only if constraints are present).
21646
21647 else
21648 Find_Type (Subtype_Mark (S));
21649
21650 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21651 and then not
21652 (Nkind (Parent (S)) = N_Subtype_Declaration
21653 and then Is_Itype (Defining_Identifier (Parent (S))))
21654 then
21655 Check_Incomplete (Subtype_Mark (S));
21656 end if;
21657
21658 P := Parent (S);
21659 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21660
21661 -- Explicit subtype declaration case
21662
21663 if Nkind (P) = N_Subtype_Declaration then
21664 Def_Id := Defining_Identifier (P);
21665
21666 -- Explicit derived type definition case
21667
21668 elsif Nkind (P) = N_Derived_Type_Definition then
21669 Def_Id := Defining_Identifier (Parent (P));
21670
21671 -- Implicit case, the Def_Id must be created as an implicit type.
21672 -- The one exception arises in the case of concurrent types, array
21673 -- and access types, where other subsidiary implicit types may be
21674 -- created and must appear before the main implicit type. In these
21675 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21676 -- has not yet been called to create Def_Id.
21677
21678 else
21679 if Is_Array_Type (Subtype_Mark_Id)
21680 or else Is_Concurrent_Type (Subtype_Mark_Id)
21681 or else Is_Access_Type (Subtype_Mark_Id)
21682 then
21683 Def_Id := Empty;
21684
21685 -- For the other cases, we create a new unattached Itype,
21686 -- and set the indication to ensure it gets attached later.
21687
21688 else
21689 Def_Id :=
21690 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21691 end if;
21692 end if;
21693
21694 -- If the kind of constraint is invalid for this kind of type,
21695 -- then give an error, and then pretend no constraint was given.
21696
21697 if not Is_Valid_Constraint_Kind
21698 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21699 then
21700 Error_Msg_N
21701 ("incorrect constraint for this kind of type", Constraint (S));
21702
21703 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21704
21705 -- Set Ekind of orphan itype, to prevent cascaded errors
21706
21707 if Present (Def_Id) then
21708 Set_Ekind (Def_Id, Ekind (Any_Type));
21709 end if;
21710
21711 -- Make recursive call, having got rid of the bogus constraint
21712
21713 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21714 end if;
21715
21716 -- Remaining processing depends on type. Select on Base_Type kind to
21717 -- ensure getting to the concrete type kind in the case of a private
21718 -- subtype (needed when only doing semantic analysis).
21719
21720 case Ekind (Base_Type (Subtype_Mark_Id)) is
21721 when Access_Kind =>
21722
21723 -- If this is a constraint on a class-wide type, discard it.
21724 -- There is currently no way to express a partial discriminant
21725 -- constraint on a type with unknown discriminants. This is
21726 -- a pathology that the ACATS wisely decides not to test.
21727
21728 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21729 if Comes_From_Source (S) then
21730 Error_Msg_N
21731 ("constraint on class-wide type ignored??",
21732 Constraint (S));
21733 end if;
21734
21735 if Nkind (P) = N_Subtype_Declaration then
21736 Set_Subtype_Indication (P,
21737 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21738 end if;
21739
21740 return Subtype_Mark_Id;
21741 end if;
21742
21743 Constrain_Access (Def_Id, S, Related_Nod);
21744
21745 if Expander_Active
21746 and then Is_Itype (Designated_Type (Def_Id))
21747 and then Nkind (Related_Nod) = N_Subtype_Declaration
21748 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21749 then
21750 Build_Itype_Reference
21751 (Designated_Type (Def_Id), Related_Nod);
21752 end if;
21753
21754 when Array_Kind =>
21755 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21756
21757 when Decimal_Fixed_Point_Kind =>
21758 Constrain_Decimal (Def_Id, S);
21759
21760 when Enumeration_Kind =>
21761 Constrain_Enumeration (Def_Id, S);
21762
21763 when Ordinary_Fixed_Point_Kind =>
21764 Constrain_Ordinary_Fixed (Def_Id, S);
21765
21766 when Float_Kind =>
21767 Constrain_Float (Def_Id, S);
21768
21769 when Integer_Kind =>
21770 Constrain_Integer (Def_Id, S);
21771
21772 when Class_Wide_Kind
21773 | E_Incomplete_Type
21774 | E_Record_Subtype
21775 | E_Record_Type
21776 =>
21777 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21778
21779 if Ekind (Def_Id) = E_Incomplete_Type then
21780 Set_Private_Dependents (Def_Id, New_Elmt_List);
21781 end if;
21782
21783 when Private_Kind =>
21784
21785 -- A private type with unknown discriminants may be completed
21786 -- by an unconstrained array type.
21787
21788 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21789 and then Present (Full_View (Subtype_Mark_Id))
21790 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21791 then
21792 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21793
21794 -- ... but more commonly is completed by a discriminated record
21795 -- type.
21796
21797 else
21798 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21799 end if;
21800
21801 -- The base type may be private but Def_Id may be a full view
21802 -- in an instance.
21803
21804 if Is_Private_Type (Def_Id) then
21805 Set_Private_Dependents (Def_Id, New_Elmt_List);
21806 end if;
21807
21808 -- In case of an invalid constraint prevent further processing
21809 -- since the type constructed is missing expected fields.
21810
21811 if Etype (Def_Id) = Any_Type then
21812 return Def_Id;
21813 end if;
21814
21815 -- If the full view is that of a task with discriminants,
21816 -- we must constrain both the concurrent type and its
21817 -- corresponding record type. Otherwise we will just propagate
21818 -- the constraint to the full view, if available.
21819
21820 if Present (Full_View (Subtype_Mark_Id))
21821 and then Has_Discriminants (Subtype_Mark_Id)
21822 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21823 then
21824 Full_View_Id :=
21825 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21826
21827 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21828 Constrain_Concurrent (Full_View_Id, S,
21829 Related_Nod, Related_Id, Suffix);
21830 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21831 Set_Full_View (Def_Id, Full_View_Id);
21832
21833 -- Introduce an explicit reference to the private subtype,
21834 -- to prevent scope anomalies in gigi if first use appears
21835 -- in a nested context, e.g. a later function body.
21836 -- Should this be generated in other contexts than a full
21837 -- type declaration?
21838
21839 if Is_Itype (Def_Id)
21840 and then
21841 Nkind (Parent (P)) = N_Full_Type_Declaration
21842 then
21843 Build_Itype_Reference (Def_Id, Parent (P));
21844 end if;
21845
21846 else
21847 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21848 end if;
21849
21850 when Concurrent_Kind =>
21851 Constrain_Concurrent (Def_Id, S,
21852 Related_Nod, Related_Id, Suffix);
21853
21854 when others =>
21855 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21856 end case;
21857
21858 -- Size, Alignment, Representation aspects and Convention are always
21859 -- inherited from the base type.
21860
21861 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21862 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21863 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21864
21865 -- The anonymous subtype created for the subtype indication
21866 -- inherits the predicates of the parent.
21867
21868 if Has_Predicates (Subtype_Mark_Id) then
21869 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21870
21871 -- Indicate where the predicate function may be found
21872
21873 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21874 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21875 end if;
21876 end if;
21877
21878 return Def_Id;
21879 end if;
21880 end Process_Subtype;
21881
21882 -----------------------------
21883 -- Record_Type_Declaration --
21884 -----------------------------
21885
21886 procedure Record_Type_Declaration
21887 (T : Entity_Id;
21888 N : Node_Id;
21889 Prev : Entity_Id)
21890 is
21891 Def : constant Node_Id := Type_Definition (N);
21892 Is_Tagged : Boolean;
21893 Tag_Comp : Entity_Id;
21894
21895 begin
21896 -- These flags must be initialized before calling Process_Discriminants
21897 -- because this routine makes use of them.
21898
21899 Set_Ekind (T, E_Record_Type);
21900 Set_Etype (T, T);
21901 Init_Size_Align (T);
21902 Set_Interfaces (T, No_Elist);
21903 Set_Stored_Constraint (T, No_Elist);
21904 Set_Default_SSO (T);
21905 Set_No_Reordering (T, No_Component_Reordering);
21906
21907 -- Normal case
21908
21909 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21910 if Limited_Present (Def) then
21911 Check_SPARK_05_Restriction ("limited is not allowed", N);
21912 end if;
21913
21914 if Abstract_Present (Def) then
21915 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21916 end if;
21917
21918 -- The flag Is_Tagged_Type might have already been set by
21919 -- Find_Type_Name if it detected an error for declaration T. This
21920 -- arises in the case of private tagged types where the full view
21921 -- omits the word tagged.
21922
21923 Is_Tagged :=
21924 Tagged_Present (Def)
21925 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21926
21927 Set_Is_Limited_Record (T, Limited_Present (Def));
21928
21929 if Is_Tagged then
21930 Set_Is_Tagged_Type (T, True);
21931 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21932 end if;
21933
21934 -- Type is abstract if full declaration carries keyword, or if
21935 -- previous partial view did.
21936
21937 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21938 or else Abstract_Present (Def));
21939
21940 else
21941 Check_SPARK_05_Restriction ("interface is not allowed", N);
21942
21943 Is_Tagged := True;
21944 Analyze_Interface_Declaration (T, Def);
21945
21946 if Present (Discriminant_Specifications (N)) then
21947 Error_Msg_N
21948 ("interface types cannot have discriminants",
21949 Defining_Identifier
21950 (First (Discriminant_Specifications (N))));
21951 end if;
21952 end if;
21953
21954 -- First pass: if there are self-referential access components,
21955 -- create the required anonymous access type declarations, and if
21956 -- need be an incomplete type declaration for T itself.
21957
21958 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21959
21960 if Ada_Version >= Ada_2005
21961 and then Present (Interface_List (Def))
21962 then
21963 Check_Interfaces (N, Def);
21964
21965 declare
21966 Ifaces_List : Elist_Id;
21967
21968 begin
21969 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21970 -- already in the parents.
21971
21972 Collect_Interfaces
21973 (T => T,
21974 Ifaces_List => Ifaces_List,
21975 Exclude_Parents => True);
21976
21977 Set_Interfaces (T, Ifaces_List);
21978 end;
21979 end if;
21980
21981 -- Records constitute a scope for the component declarations within.
21982 -- The scope is created prior to the processing of these declarations.
21983 -- Discriminants are processed first, so that they are visible when
21984 -- processing the other components. The Ekind of the record type itself
21985 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21986
21987 -- Enter record scope
21988
21989 Push_Scope (T);
21990
21991 -- If an incomplete or private type declaration was already given for
21992 -- the type, then this scope already exists, and the discriminants have
21993 -- been declared within. We must verify that the full declaration
21994 -- matches the incomplete one.
21995
21996 Check_Or_Process_Discriminants (N, T, Prev);
21997
21998 Set_Is_Constrained (T, not Has_Discriminants (T));
21999 Set_Has_Delayed_Freeze (T, True);
22000
22001 -- For tagged types add a manually analyzed component corresponding
22002 -- to the component _tag, the corresponding piece of tree will be
22003 -- expanded as part of the freezing actions if it is not a CPP_Class.
22004
22005 if Is_Tagged then
22006
22007 -- Do not add the tag unless we are in expansion mode
22008
22009 if Expander_Active then
22010 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22011 Enter_Name (Tag_Comp);
22012
22013 Set_Ekind (Tag_Comp, E_Component);
22014 Set_Is_Tag (Tag_Comp);
22015 Set_Is_Aliased (Tag_Comp);
22016 Set_Etype (Tag_Comp, RTE (RE_Tag));
22017 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22018 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22019 Init_Component_Location (Tag_Comp);
22020
22021 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22022 -- implemented interfaces.
22023
22024 if Has_Interfaces (T) then
22025 Add_Interface_Tag_Components (N, T);
22026 end if;
22027 end if;
22028
22029 Make_Class_Wide_Type (T);
22030 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22031 end if;
22032
22033 -- We must suppress range checks when processing record components in
22034 -- the presence of discriminants, since we don't want spurious checks to
22035 -- be generated during their analysis, but Suppress_Range_Checks flags
22036 -- must be reset the after processing the record definition.
22037
22038 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22039 -- couldn't we just use the normal range check suppression method here.
22040 -- That would seem cleaner ???
22041
22042 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22043 Set_Kill_Range_Checks (T, True);
22044 Record_Type_Definition (Def, Prev);
22045 Set_Kill_Range_Checks (T, False);
22046 else
22047 Record_Type_Definition (Def, Prev);
22048 end if;
22049
22050 -- Exit from record scope
22051
22052 End_Scope;
22053
22054 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22055 -- the implemented interfaces and associate them an aliased entity.
22056
22057 if Is_Tagged
22058 and then not Is_Empty_List (Interface_List (Def))
22059 then
22060 Derive_Progenitor_Subprograms (T, T);
22061 end if;
22062
22063 Check_Function_Writable_Actuals (N);
22064 end Record_Type_Declaration;
22065
22066 ----------------------------
22067 -- Record_Type_Definition --
22068 ----------------------------
22069
22070 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22071 Component : Entity_Id;
22072 Ctrl_Components : Boolean := False;
22073 Final_Storage_Only : Boolean;
22074 T : Entity_Id;
22075
22076 begin
22077 if Ekind (Prev_T) = E_Incomplete_Type then
22078 T := Full_View (Prev_T);
22079 else
22080 T := Prev_T;
22081 end if;
22082
22083 -- In SPARK, tagged types and type extensions may only be declared in
22084 -- the specification of library unit packages.
22085
22086 if Present (Def) and then Is_Tagged_Type (T) then
22087 declare
22088 Typ : Node_Id;
22089 Ctxt : Node_Id;
22090
22091 begin
22092 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22093 Typ := Parent (Def);
22094 else
22095 pragma Assert
22096 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22097 Typ := Parent (Parent (Def));
22098 end if;
22099
22100 Ctxt := Parent (Typ);
22101
22102 if Nkind (Ctxt) = N_Package_Body
22103 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22104 then
22105 Check_SPARK_05_Restriction
22106 ("type should be defined in package specification", Typ);
22107
22108 elsif Nkind (Ctxt) /= N_Package_Specification
22109 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22110 then
22111 Check_SPARK_05_Restriction
22112 ("type should be defined in library unit package", Typ);
22113 end if;
22114 end;
22115 end if;
22116
22117 Final_Storage_Only := not Is_Controlled (T);
22118
22119 -- Ada 2005: Check whether an explicit Limited is present in a derived
22120 -- type declaration.
22121
22122 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22123 and then Limited_Present (Parent (Def))
22124 then
22125 Set_Is_Limited_Record (T);
22126 end if;
22127
22128 -- If the component list of a record type is defined by the reserved
22129 -- word null and there is no discriminant part, then the record type has
22130 -- no components and all records of the type are null records (RM 3.7)
22131 -- This procedure is also called to process the extension part of a
22132 -- record extension, in which case the current scope may have inherited
22133 -- components.
22134
22135 if No (Def)
22136 or else No (Component_List (Def))
22137 or else Null_Present (Component_List (Def))
22138 then
22139 if not Is_Tagged_Type (T) then
22140 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22141 end if;
22142
22143 else
22144 Analyze_Declarations (Component_Items (Component_List (Def)));
22145
22146 if Present (Variant_Part (Component_List (Def))) then
22147 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22148 Analyze (Variant_Part (Component_List (Def)));
22149 end if;
22150 end if;
22151
22152 -- After completing the semantic analysis of the record definition,
22153 -- record components, both new and inherited, are accessible. Set their
22154 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22155 -- whose Ekind may be void.
22156
22157 Component := First_Entity (Current_Scope);
22158 while Present (Component) loop
22159 if Ekind (Component) = E_Void
22160 and then not Is_Itype (Component)
22161 then
22162 Set_Ekind (Component, E_Component);
22163 Init_Component_Location (Component);
22164 end if;
22165
22166 Propagate_Concurrent_Flags (T, Etype (Component));
22167
22168 if Ekind (Component) /= E_Component then
22169 null;
22170
22171 -- Do not set Has_Controlled_Component on a class-wide equivalent
22172 -- type. See Make_CW_Equivalent_Type.
22173
22174 elsif not Is_Class_Wide_Equivalent_Type (T)
22175 and then (Has_Controlled_Component (Etype (Component))
22176 or else (Chars (Component) /= Name_uParent
22177 and then Is_Controlled (Etype (Component))))
22178 then
22179 Set_Has_Controlled_Component (T, True);
22180 Final_Storage_Only :=
22181 Final_Storage_Only
22182 and then Finalize_Storage_Only (Etype (Component));
22183 Ctrl_Components := True;
22184 end if;
22185
22186 Next_Entity (Component);
22187 end loop;
22188
22189 -- A Type is Finalize_Storage_Only only if all its controlled components
22190 -- are also.
22191
22192 if Ctrl_Components then
22193 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22194 end if;
22195
22196 -- Place reference to end record on the proper entity, which may
22197 -- be a partial view.
22198
22199 if Present (Def) then
22200 Process_End_Label (Def, 'e', Prev_T);
22201 end if;
22202 end Record_Type_Definition;
22203
22204 ------------------------
22205 -- Replace_Components --
22206 ------------------------
22207
22208 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
22209 function Process (N : Node_Id) return Traverse_Result;
22210
22211 -------------
22212 -- Process --
22213 -------------
22214
22215 function Process (N : Node_Id) return Traverse_Result is
22216 Comp : Entity_Id;
22217
22218 begin
22219 if Nkind (N) = N_Discriminant_Specification then
22220 Comp := First_Discriminant (Typ);
22221 while Present (Comp) loop
22222 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22223 Set_Defining_Identifier (N, Comp);
22224 exit;
22225 end if;
22226
22227 Next_Discriminant (Comp);
22228 end loop;
22229
22230 elsif Nkind (N) = N_Variant_Part then
22231 Comp := First_Discriminant (Typ);
22232 while Present (Comp) loop
22233 if Chars (Comp) = Chars (Name (N)) then
22234 Set_Entity (Name (N), Comp);
22235 exit;
22236 end if;
22237
22238 Next_Discriminant (Comp);
22239 end loop;
22240
22241 elsif Nkind (N) = N_Component_Declaration then
22242 Comp := First_Component (Typ);
22243 while Present (Comp) loop
22244 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22245 Set_Defining_Identifier (N, Comp);
22246 exit;
22247 end if;
22248
22249 Next_Component (Comp);
22250 end loop;
22251 end if;
22252
22253 return OK;
22254 end Process;
22255
22256 procedure Replace is new Traverse_Proc (Process);
22257
22258 -- Start of processing for Replace_Components
22259
22260 begin
22261 Replace (Decl);
22262 end Replace_Components;
22263
22264 -------------------------------
22265 -- Set_Completion_Referenced --
22266 -------------------------------
22267
22268 procedure Set_Completion_Referenced (E : Entity_Id) is
22269 begin
22270 -- If in main unit, mark entity that is a completion as referenced,
22271 -- warnings go on the partial view when needed.
22272
22273 if In_Extended_Main_Source_Unit (E) then
22274 Set_Referenced (E);
22275 end if;
22276 end Set_Completion_Referenced;
22277
22278 ---------------------
22279 -- Set_Default_SSO --
22280 ---------------------
22281
22282 procedure Set_Default_SSO (T : Entity_Id) is
22283 begin
22284 case Opt.Default_SSO is
22285 when ' ' =>
22286 null;
22287 when 'L' =>
22288 Set_SSO_Set_Low_By_Default (T, True);
22289 when 'H' =>
22290 Set_SSO_Set_High_By_Default (T, True);
22291 when others =>
22292 raise Program_Error;
22293 end case;
22294 end Set_Default_SSO;
22295
22296 ---------------------
22297 -- Set_Fixed_Range --
22298 ---------------------
22299
22300 -- The range for fixed-point types is complicated by the fact that we
22301 -- do not know the exact end points at the time of the declaration. This
22302 -- is true for three reasons:
22303
22304 -- A size clause may affect the fudging of the end-points.
22305 -- A small clause may affect the values of the end-points.
22306 -- We try to include the end-points if it does not affect the size.
22307
22308 -- This means that the actual end-points must be established at the
22309 -- point when the type is frozen. Meanwhile, we first narrow the range
22310 -- as permitted (so that it will fit if necessary in a small specified
22311 -- size), and then build a range subtree with these narrowed bounds.
22312 -- Set_Fixed_Range constructs the range from real literal values, and
22313 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22314
22315 -- The parent of this range is set to point to the entity so that it is
22316 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22317 -- other scalar types, which are just pointers to the range in the
22318 -- original tree, this would otherwise be an orphan).
22319
22320 -- The tree is left unanalyzed. When the type is frozen, the processing
22321 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22322 -- analyzed, and uses this as an indication that it should complete
22323 -- work on the range (it will know the final small and size values).
22324
22325 procedure Set_Fixed_Range
22326 (E : Entity_Id;
22327 Loc : Source_Ptr;
22328 Lo : Ureal;
22329 Hi : Ureal)
22330 is
22331 S : constant Node_Id :=
22332 Make_Range (Loc,
22333 Low_Bound => Make_Real_Literal (Loc, Lo),
22334 High_Bound => Make_Real_Literal (Loc, Hi));
22335 begin
22336 Set_Scalar_Range (E, S);
22337 Set_Parent (S, E);
22338
22339 -- Before the freeze point, the bounds of a fixed point are universal
22340 -- and carry the corresponding type.
22341
22342 Set_Etype (Low_Bound (S), Universal_Real);
22343 Set_Etype (High_Bound (S), Universal_Real);
22344 end Set_Fixed_Range;
22345
22346 ----------------------------------
22347 -- Set_Scalar_Range_For_Subtype --
22348 ----------------------------------
22349
22350 procedure Set_Scalar_Range_For_Subtype
22351 (Def_Id : Entity_Id;
22352 R : Node_Id;
22353 Subt : Entity_Id)
22354 is
22355 Kind : constant Entity_Kind := Ekind (Def_Id);
22356
22357 begin
22358 -- Defend against previous error
22359
22360 if Nkind (R) = N_Error then
22361 return;
22362 end if;
22363
22364 Set_Scalar_Range (Def_Id, R);
22365
22366 -- We need to link the range into the tree before resolving it so
22367 -- that types that are referenced, including importantly the subtype
22368 -- itself, are properly frozen (Freeze_Expression requires that the
22369 -- expression be properly linked into the tree). Of course if it is
22370 -- already linked in, then we do not disturb the current link.
22371
22372 if No (Parent (R)) then
22373 Set_Parent (R, Def_Id);
22374 end if;
22375
22376 -- Reset the kind of the subtype during analysis of the range, to
22377 -- catch possible premature use in the bounds themselves.
22378
22379 Set_Ekind (Def_Id, E_Void);
22380 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22381 Set_Ekind (Def_Id, Kind);
22382 end Set_Scalar_Range_For_Subtype;
22383
22384 --------------------------------------------------------
22385 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22386 --------------------------------------------------------
22387
22388 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22389 (E : Entity_Id)
22390 is
22391 begin
22392 -- Make sure set if encountered during Expand_To_Stored_Constraint
22393
22394 Set_Stored_Constraint (E, No_Elist);
22395
22396 -- Give it the right value
22397
22398 if Is_Constrained (E) and then Has_Discriminants (E) then
22399 Set_Stored_Constraint (E,
22400 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22401 end if;
22402 end Set_Stored_Constraint_From_Discriminant_Constraint;
22403
22404 -------------------------------------
22405 -- Signed_Integer_Type_Declaration --
22406 -------------------------------------
22407
22408 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22409 Implicit_Base : Entity_Id;
22410 Base_Typ : Entity_Id;
22411 Lo_Val : Uint;
22412 Hi_Val : Uint;
22413 Errs : Boolean := False;
22414 Lo : Node_Id;
22415 Hi : Node_Id;
22416
22417 function Can_Derive_From (E : Entity_Id) return Boolean;
22418 -- Determine whether given bounds allow derivation from specified type
22419
22420 procedure Check_Bound (Expr : Node_Id);
22421 -- Check bound to make sure it is integral and static. If not, post
22422 -- appropriate error message and set Errs flag
22423
22424 ---------------------
22425 -- Can_Derive_From --
22426 ---------------------
22427
22428 -- Note we check both bounds against both end values, to deal with
22429 -- strange types like ones with a range of 0 .. -12341234.
22430
22431 function Can_Derive_From (E : Entity_Id) return Boolean is
22432 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22433 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22434 begin
22435 return Lo <= Lo_Val and then Lo_Val <= Hi
22436 and then
22437 Lo <= Hi_Val and then Hi_Val <= Hi;
22438 end Can_Derive_From;
22439
22440 -----------------
22441 -- Check_Bound --
22442 -----------------
22443
22444 procedure Check_Bound (Expr : Node_Id) is
22445 begin
22446 -- If a range constraint is used as an integer type definition, each
22447 -- bound of the range must be defined by a static expression of some
22448 -- integer type, but the two bounds need not have the same integer
22449 -- type (Negative bounds are allowed.) (RM 3.5.4)
22450
22451 if not Is_Integer_Type (Etype (Expr)) then
22452 Error_Msg_N
22453 ("integer type definition bounds must be of integer type", Expr);
22454 Errs := True;
22455
22456 elsif not Is_OK_Static_Expression (Expr) then
22457 Flag_Non_Static_Expr
22458 ("non-static expression used for integer type bound!", Expr);
22459 Errs := True;
22460
22461 -- The bounds are folded into literals, and we set their type to be
22462 -- universal, to avoid typing difficulties: we cannot set the type
22463 -- of the literal to the new type, because this would be a forward
22464 -- reference for the back end, and if the original type is user-
22465 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22466
22467 else
22468 if Is_Entity_Name (Expr) then
22469 Fold_Uint (Expr, Expr_Value (Expr), True);
22470 end if;
22471
22472 Set_Etype (Expr, Universal_Integer);
22473 end if;
22474 end Check_Bound;
22475
22476 -- Start of processing for Signed_Integer_Type_Declaration
22477
22478 begin
22479 -- Create an anonymous base type
22480
22481 Implicit_Base :=
22482 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22483
22484 -- Analyze and check the bounds, they can be of any integer type
22485
22486 Lo := Low_Bound (Def);
22487 Hi := High_Bound (Def);
22488
22489 -- Arbitrarily use Integer as the type if either bound had an error
22490
22491 if Hi = Error or else Lo = Error then
22492 Base_Typ := Any_Integer;
22493 Set_Error_Posted (T, True);
22494
22495 -- Here both bounds are OK expressions
22496
22497 else
22498 Analyze_And_Resolve (Lo, Any_Integer);
22499 Analyze_And_Resolve (Hi, Any_Integer);
22500
22501 Check_Bound (Lo);
22502 Check_Bound (Hi);
22503
22504 if Errs then
22505 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22506 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22507 end if;
22508
22509 -- Find type to derive from
22510
22511 Lo_Val := Expr_Value (Lo);
22512 Hi_Val := Expr_Value (Hi);
22513
22514 if Can_Derive_From (Standard_Short_Short_Integer) then
22515 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22516
22517 elsif Can_Derive_From (Standard_Short_Integer) then
22518 Base_Typ := Base_Type (Standard_Short_Integer);
22519
22520 elsif Can_Derive_From (Standard_Integer) then
22521 Base_Typ := Base_Type (Standard_Integer);
22522
22523 elsif Can_Derive_From (Standard_Long_Integer) then
22524 Base_Typ := Base_Type (Standard_Long_Integer);
22525
22526 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22527 Check_Restriction (No_Long_Long_Integers, Def);
22528 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22529
22530 else
22531 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22532 Error_Msg_N ("integer type definition bounds out of range", Def);
22533 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22534 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22535 end if;
22536 end if;
22537
22538 -- Complete both implicit base and declared first subtype entities. The
22539 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22540 -- are not clobbered when the signed integer type acts as a full view of
22541 -- a private type.
22542
22543 Set_Etype (Implicit_Base, Base_Typ);
22544 Set_Size_Info (Implicit_Base, Base_Typ);
22545 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22546 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22547 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22548
22549 Set_Ekind (T, E_Signed_Integer_Subtype);
22550 Set_Etype (T, Implicit_Base);
22551 Set_Size_Info (T, Implicit_Base);
22552 Inherit_Rep_Item_Chain (T, Implicit_Base);
22553 Set_Scalar_Range (T, Def);
22554 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22555 Set_Is_Constrained (T);
22556 end Signed_Integer_Type_Declaration;
22557
22558 end Sem_Ch3;