]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch3.adb
29e8964a7257ba40037c95f83577ba52c48af8b0
[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-2020, 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 Check_Access_Discriminant_Requires_Limited
236 (D : Node_Id;
237 Loc : Node_Id);
238 -- Check the restriction that the type to which an access discriminant
239 -- belongs must be a concurrent type or a descendant of a type with
240 -- the reserved word 'limited' in its declaration.
241
242 procedure Check_Anonymous_Access_Components
243 (Typ_Decl : Node_Id;
244 Typ : Entity_Id;
245 Prev : Entity_Id;
246 Comp_List : Node_Id);
247 -- Ada 2005 AI-382: an access component in a record definition can refer to
248 -- the enclosing record, in which case it denotes the type itself, and not
249 -- the current instance of the type. We create an anonymous access type for
250 -- the component, and flag it as an access to a component, so accessibility
251 -- checks are properly performed on it. The declaration of the access type
252 -- is placed ahead of that of the record to prevent order-of-elaboration
253 -- circularity issues in Gigi. We create an incomplete type for the record
254 -- declaration, which is the designated type of the anonymous access.
255
256 procedure Check_Delta_Expression (E : Node_Id);
257 -- Check that the expression represented by E is suitable for use as a
258 -- delta expression, i.e. it is of real type and is static.
259
260 procedure Check_Digits_Expression (E : Node_Id);
261 -- Check that the expression represented by E is suitable for use as a
262 -- digits expression, i.e. it is of integer type, positive and static.
263
264 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
265 -- Validate the initialization of an object declaration. T is the required
266 -- type, and Exp is the initialization expression.
267
268 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
269 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
270
271 procedure Check_Or_Process_Discriminants
272 (N : Node_Id;
273 T : Entity_Id;
274 Prev : Entity_Id := Empty);
275 -- If N is the full declaration of the completion T of an incomplete or
276 -- private type, check its discriminants (which are already known to be
277 -- conformant with those of the partial view, see Find_Type_Name),
278 -- otherwise process them. Prev is the entity of the partial declaration,
279 -- if any.
280
281 procedure Check_Real_Bound (Bound : Node_Id);
282 -- Check given bound for being of real type and static. If not, post an
283 -- appropriate message, and rewrite the bound with the real literal zero.
284
285 procedure Constant_Redeclaration
286 (Id : Entity_Id;
287 N : Node_Id;
288 T : out Entity_Id);
289 -- Various checks on legality of full declaration of deferred constant.
290 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
291 -- node. The caller has not yet set any attributes of this entity.
292
293 function Contain_Interface
294 (Iface : Entity_Id;
295 Ifaces : Elist_Id) return Boolean;
296 -- Ada 2005: Determine whether Iface is present in the list Ifaces
297
298 procedure Convert_Scalar_Bounds
299 (N : Node_Id;
300 Parent_Type : Entity_Id;
301 Derived_Type : Entity_Id;
302 Loc : Source_Ptr);
303 -- For derived scalar types, convert the bounds in the type definition to
304 -- the derived type, and complete their analysis. Given a constraint of the
305 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
306 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
307 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
308 -- subtype are conversions of those bounds to the derived_type, so that
309 -- their typing is consistent.
310
311 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
312 -- Copies attributes from array base type T2 to array base type T1. Copies
313 -- only attributes that apply to base types, but not subtypes.
314
315 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
316 -- Copies attributes from array subtype T2 to array subtype T1. Copies
317 -- attributes that apply to both subtypes and base types.
318
319 procedure Create_Constrained_Components
320 (Subt : Entity_Id;
321 Decl_Node : Node_Id;
322 Typ : Entity_Id;
323 Constraints : Elist_Id);
324 -- Build the list of entities for a constrained discriminated record
325 -- subtype. If a component depends on a discriminant, replace its subtype
326 -- using the discriminant values in the discriminant constraint. Subt
327 -- is the defining identifier for the subtype whose list of constrained
328 -- entities we will create. Decl_Node is the type declaration node where
329 -- we will attach all the itypes created. Typ is the base discriminated
330 -- type for the subtype Subt. Constraints is the list of discriminant
331 -- constraints for Typ.
332
333 function Constrain_Component_Type
334 (Comp : Entity_Id;
335 Constrained_Typ : Entity_Id;
336 Related_Node : Node_Id;
337 Typ : Entity_Id;
338 Constraints : Elist_Id) return Entity_Id;
339 -- Given a discriminated base type Typ, a list of discriminant constraints,
340 -- Constraints, for Typ and a component Comp of Typ, create and return the
341 -- type corresponding to Etype (Comp) where all discriminant references
342 -- are replaced with the corresponding constraint. If Etype (Comp) contains
343 -- no discriminant references then it is returned as-is. Constrained_Typ
344 -- is the final constrained subtype to which the constrained component
345 -- belongs. Related_Node is the node where we attach all created itypes.
346
347 procedure Constrain_Access
348 (Def_Id : in out Entity_Id;
349 S : Node_Id;
350 Related_Nod : Node_Id);
351 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
352 -- an anonymous type created for a subtype indication. In that case it is
353 -- created in the procedure and attached to Related_Nod.
354
355 procedure Constrain_Array
356 (Def_Id : in out Entity_Id;
357 SI : Node_Id;
358 Related_Nod : Node_Id;
359 Related_Id : Entity_Id;
360 Suffix : Character);
361 -- Apply a list of index constraints to an unconstrained array type. The
362 -- first parameter is the entity for the resulting subtype. A value of
363 -- Empty for Def_Id indicates that an implicit type must be created, but
364 -- creation is delayed (and must be done by this procedure) because other
365 -- subsidiary implicit types must be created first (which is why Def_Id
366 -- is an in/out parameter). The second parameter is a subtype indication
367 -- node for the constrained array to be created (e.g. something of the
368 -- form string (1 .. 10)). Related_Nod gives the place where this type
369 -- has to be inserted in the tree. The Related_Id and Suffix parameters
370 -- are used to build the associated Implicit type name.
371
372 procedure Constrain_Concurrent
373 (Def_Id : in out Entity_Id;
374 SI : Node_Id;
375 Related_Nod : Node_Id;
376 Related_Id : Entity_Id;
377 Suffix : Character);
378 -- Apply list of discriminant constraints to an unconstrained concurrent
379 -- type.
380 --
381 -- SI is the N_Subtype_Indication node containing the constraint and
382 -- the unconstrained type to constrain.
383 --
384 -- Def_Id is the entity for the resulting constrained subtype. A value
385 -- of Empty for Def_Id indicates that an implicit type must be created,
386 -- but creation is delayed (and must be done by this procedure) because
387 -- other subsidiary implicit types must be created first (which is why
388 -- Def_Id is an in/out parameter).
389 --
390 -- Related_Nod gives the place where this type has to be inserted
391 -- in the tree.
392 --
393 -- The last two arguments are used to create its external name if needed.
394
395 function Constrain_Corresponding_Record
396 (Prot_Subt : Entity_Id;
397 Corr_Rec : Entity_Id;
398 Related_Nod : Node_Id) return Entity_Id;
399 -- When constraining a protected type or task type with discriminants,
400 -- constrain the corresponding record with the same discriminant values.
401
402 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
403 -- Constrain a decimal fixed point type with a digits constraint and/or a
404 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
405
406 procedure Constrain_Discriminated_Type
407 (Def_Id : Entity_Id;
408 S : Node_Id;
409 Related_Nod : Node_Id;
410 For_Access : Boolean := False);
411 -- Process discriminant constraints of composite type. Verify that values
412 -- have been provided for all discriminants, that the original type is
413 -- unconstrained, and that the types of the supplied expressions match
414 -- the discriminant types. The first three parameters are like in routine
415 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
416 -- of For_Access.
417
418 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
419 -- Constrain an enumeration type with a range constraint. This is identical
420 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
421
422 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
423 -- Constrain a floating point type with either a digits constraint
424 -- and/or a range constraint, building a E_Floating_Point_Subtype.
425
426 procedure Constrain_Index
427 (Index : Node_Id;
428 S : Node_Id;
429 Related_Nod : Node_Id;
430 Related_Id : Entity_Id;
431 Suffix : Character;
432 Suffix_Index : Nat);
433 -- Process an index constraint S in a constrained array declaration. The
434 -- constraint can be a subtype name, or a range with or without an explicit
435 -- subtype mark. The index is the corresponding index of the unconstrained
436 -- array. The Related_Id and Suffix parameters are used to build the
437 -- associated Implicit type name.
438
439 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
440 -- Build subtype of a signed or modular integer type
441
442 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
443 -- Constrain an ordinary fixed point type with a range constraint, and
444 -- build an E_Ordinary_Fixed_Point_Subtype entity.
445
446 procedure Copy_And_Swap (Priv, Full : Entity_Id);
447 -- Copy the Priv entity into the entity of its full declaration then swap
448 -- the two entities in such a manner that the former private type is now
449 -- seen as a full type.
450
451 procedure Decimal_Fixed_Point_Type_Declaration
452 (T : Entity_Id;
453 Def : Node_Id);
454 -- Create a new decimal fixed point type, and apply the constraint to
455 -- obtain a subtype of this new type.
456
457 procedure Complete_Private_Subtype
458 (Priv : Entity_Id;
459 Full : Entity_Id;
460 Full_Base : Entity_Id;
461 Related_Nod : Node_Id);
462 -- Complete the implicit full view of a private subtype by setting the
463 -- appropriate semantic fields. If the full view of the parent is a record
464 -- type, build constrained components of subtype.
465
466 procedure Derive_Progenitor_Subprograms
467 (Parent_Type : Entity_Id;
468 Tagged_Type : Entity_Id);
469 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
470 -- operations of progenitors of Tagged_Type, and replace the subsidiary
471 -- subtypes with Tagged_Type, to build the specs of the inherited interface
472 -- primitives. The derived primitives are aliased to those of the
473 -- interface. This routine takes care also of transferring to the full view
474 -- subprograms associated with the partial view of Tagged_Type that cover
475 -- interface primitives.
476
477 procedure Derived_Standard_Character
478 (N : Node_Id;
479 Parent_Type : Entity_Id;
480 Derived_Type : Entity_Id);
481 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
482 -- derivations from types Standard.Character and Standard.Wide_Character.
483
484 procedure Derived_Type_Declaration
485 (T : Entity_Id;
486 N : Node_Id;
487 Is_Completion : Boolean);
488 -- Process a derived type declaration. Build_Derived_Type is invoked
489 -- to process the actual derived type definition. Parameters N and
490 -- Is_Completion have the same meaning as in Build_Derived_Type.
491 -- T is the N_Defining_Identifier for the entity defined in the
492 -- N_Full_Type_Declaration node N, that is T is the derived type.
493
494 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
495 -- Insert each literal in symbol table, as an overloadable identifier. Each
496 -- enumeration type is mapped into a sequence of integers, and each literal
497 -- is defined as a constant with integer value. If any of the literals are
498 -- character literals, the type is a character type, which means that
499 -- strings are legal aggregates for arrays of components of the type.
500
501 function Expand_To_Stored_Constraint
502 (Typ : Entity_Id;
503 Constraint : Elist_Id) return Elist_Id;
504 -- Given a constraint (i.e. a list of expressions) on the discriminants of
505 -- Typ, expand it into a constraint on the stored discriminants and return
506 -- the new list of expressions constraining the stored discriminants.
507
508 function Find_Type_Of_Object
509 (Obj_Def : Node_Id;
510 Related_Nod : Node_Id) return Entity_Id;
511 -- Get type entity for object referenced by Obj_Def, attaching the implicit
512 -- types generated to Related_Nod.
513
514 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
515 -- Create a new float and apply the constraint to obtain subtype of it
516
517 function Has_Range_Constraint (N : Node_Id) return Boolean;
518 -- Given an N_Subtype_Indication node N, return True if a range constraint
519 -- is present, either directly, or as part of a digits or delta constraint.
520 -- In addition, a digits constraint in the decimal case returns True, since
521 -- it establishes a default range if no explicit range is present.
522
523 function Inherit_Components
524 (N : Node_Id;
525 Parent_Base : Entity_Id;
526 Derived_Base : Entity_Id;
527 Is_Tagged : Boolean;
528 Inherit_Discr : Boolean;
529 Discs : Elist_Id) return Elist_Id;
530 -- Called from Build_Derived_Record_Type to inherit the components of
531 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
532 -- For more information on derived types and component inheritance please
533 -- consult the comment above the body of Build_Derived_Record_Type.
534 --
535 -- N is the original derived type declaration
536 --
537 -- Is_Tagged is set if we are dealing with tagged types
538 --
539 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
540 -- Parent_Base, otherwise no discriminants are inherited.
541 --
542 -- Discs gives the list of constraints that apply to Parent_Base in the
543 -- derived type declaration. If Discs is set to No_Elist, then we have
544 -- the following situation:
545 --
546 -- type Parent (D1..Dn : ..) is [tagged] record ...;
547 -- type Derived is new Parent [with ...];
548 --
549 -- which gets treated as
550 --
551 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
552 --
553 -- For untagged types the returned value is an association list. The list
554 -- starts from the association (Parent_Base => Derived_Base), and then it
555 -- contains a sequence of the associations of the form
556 --
557 -- (Old_Component => New_Component),
558 --
559 -- where Old_Component is the Entity_Id of a component in Parent_Base and
560 -- New_Component is the Entity_Id of the corresponding component in
561 -- Derived_Base. For untagged records, this association list is needed when
562 -- copying the record declaration for the derived base. In the tagged case
563 -- the value returned is irrelevant.
564
565 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
566 -- Propagate static and dynamic predicate flags from a parent to the
567 -- subtype in a subtype declaration with and without constraints.
568
569 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
570 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
571 -- Determine whether subprogram Subp is a procedure subject to pragma
572 -- Extensions_Visible with value False and has at least one controlling
573 -- parameter of mode OUT.
574
575 function Is_Valid_Constraint_Kind
576 (T_Kind : Type_Kind;
577 Constraint_Kind : Node_Kind) return Boolean;
578 -- Returns True if it is legal to apply the given kind of constraint to the
579 -- given kind of type (index constraint to an array type, for example).
580
581 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
582 -- Create new modular type. Verify that modulus is in bounds
583
584 procedure New_Concatenation_Op (Typ : Entity_Id);
585 -- Create an abbreviated declaration for an operator in order to
586 -- materialize concatenation on array types.
587
588 procedure Ordinary_Fixed_Point_Type_Declaration
589 (T : Entity_Id;
590 Def : Node_Id);
591 -- Create a new ordinary fixed point type, and apply the constraint to
592 -- obtain subtype of it.
593
594 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
595 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
596 -- In_Default_Expr can be properly adjusted.
597
598 procedure Prepare_Private_Subtype_Completion
599 (Id : Entity_Id;
600 Related_Nod : Node_Id);
601 -- Id is a subtype of some private type. Creates the full declaration
602 -- associated with Id whenever possible, i.e. when the full declaration
603 -- of the base type is already known. Records each subtype into
604 -- Private_Dependents of the base type.
605
606 procedure Process_Incomplete_Dependents
607 (N : Node_Id;
608 Full_T : Entity_Id;
609 Inc_T : Entity_Id);
610 -- Process all entities that depend on an incomplete type. There include
611 -- subtypes, subprogram types that mention the incomplete type in their
612 -- profiles, and subprogram with access parameters that designate the
613 -- incomplete type.
614
615 -- Inc_T is the defining identifier of an incomplete type declaration, its
616 -- Ekind is E_Incomplete_Type.
617 --
618 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
619 --
620 -- Full_T is N's defining identifier.
621 --
622 -- Subtypes of incomplete types with discriminants are completed when the
623 -- parent type is. This is simpler than private subtypes, because they can
624 -- only appear in the same scope, and there is no need to exchange views.
625 -- Similarly, access_to_subprogram types may have a parameter or a return
626 -- type that is an incomplete type, and that must be replaced with the
627 -- full type.
628 --
629 -- If the full type is tagged, subprogram with access parameters that
630 -- designated the incomplete may be primitive operations of the full type,
631 -- and have to be processed accordingly.
632
633 procedure Process_Real_Range_Specification (Def : Node_Id);
634 -- Given the type definition for a real type, this procedure processes and
635 -- checks the real range specification of this type definition if one is
636 -- present. If errors are found, error messages are posted, and the
637 -- Real_Range_Specification of Def is reset to Empty.
638
639 procedure Record_Type_Declaration
640 (T : Entity_Id;
641 N : Node_Id;
642 Prev : Entity_Id);
643 -- Process a record type declaration (for both untagged and tagged
644 -- records). Parameters T and N are exactly like in procedure
645 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
646 -- for this routine. If this is the completion of an incomplete type
647 -- declaration, Prev is the entity of the incomplete declaration, used for
648 -- cross-referencing. Otherwise Prev = T.
649
650 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
651 -- This routine is used to process the actual record type definition (both
652 -- for untagged and tagged records). Def is a record type definition node.
653 -- This procedure analyzes the components in this record type definition.
654 -- Prev_T is the entity for the enclosing record type. It is provided so
655 -- that its Has_Task flag can be set if any of the component have Has_Task
656 -- set. If the declaration is the completion of an incomplete type
657 -- declaration, Prev_T is the original incomplete type, whose full view is
658 -- the record type.
659
660 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
661 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
662 -- first create the list of components for the derived type from that of
663 -- the parent by means of Inherit_Components and then build a copy of the
664 -- declaration tree of the parent with the help of the mapping returned by
665 -- Inherit_Components, which will for example be used to validate record
666 -- representation clauses given for the derived type. If the parent type
667 -- is private and has discriminants, the ancestor discriminants used in the
668 -- inheritance are that of the private declaration, whereas the ancestor
669 -- discriminants present in the declaration tree of the parent are that of
670 -- the full declaration; as a consequence, the remapping done during the
671 -- copy will leave the references to the ancestor discriminants unchanged
672 -- in the declaration tree and they need to be fixed up. If the derived
673 -- type has a known discriminant part, then the remapping done during the
674 -- copy will only create references to the girder discriminants and they
675 -- need to be replaced with references to the non-girder discriminants.
676
677 procedure Set_Fixed_Range
678 (E : Entity_Id;
679 Loc : Source_Ptr;
680 Lo : Ureal;
681 Hi : Ureal);
682 -- Build a range node with the given bounds and set it as the Scalar_Range
683 -- of the given fixed-point type entity. Loc is the source location used
684 -- for the constructed range. See body for further details.
685
686 procedure Set_Scalar_Range_For_Subtype
687 (Def_Id : Entity_Id;
688 R : Node_Id;
689 Subt : Entity_Id);
690 -- This routine is used to set the scalar range field for a subtype given
691 -- Def_Id, the entity for the subtype, and R, the range expression for the
692 -- scalar range. Subt provides the parent subtype to be used to analyze,
693 -- resolve, and check the given range.
694
695 procedure Set_Default_SSO (T : Entity_Id);
696 -- T is the entity for an array or record being declared. This procedure
697 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
698 -- to the setting of Opt.Default_SSO.
699
700 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
701 -- Create a new signed integer entity, and apply the constraint to obtain
702 -- the required first named subtype of this type.
703
704 procedure Set_Stored_Constraint_From_Discriminant_Constraint
705 (E : Entity_Id);
706 -- E is some record type. This routine computes E's Stored_Constraint
707 -- from its Discriminant_Constraint.
708
709 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
710 -- Check that an entity in a list of progenitors is an interface,
711 -- emit error otherwise.
712
713 -----------------------
714 -- Access_Definition --
715 -----------------------
716
717 function Access_Definition
718 (Related_Nod : Node_Id;
719 N : Node_Id) return Entity_Id
720 is
721 Anon_Type : Entity_Id;
722 Anon_Scope : Entity_Id;
723 Desig_Type : Entity_Id;
724 Enclosing_Prot_Type : Entity_Id := Empty;
725
726 begin
727 Check_SPARK_05_Restriction ("access type is not allowed", N);
728
729 if Is_Entry (Current_Scope)
730 and then Is_Task_Type (Etype (Scope (Current_Scope)))
731 then
732 Error_Msg_N ("task entries cannot have access parameters", N);
733 return Empty;
734 end if;
735
736 -- Ada 2005: For an object declaration the corresponding anonymous
737 -- type is declared in the current scope.
738
739 -- If the access definition is the return type of another access to
740 -- function, scope is the current one, because it is the one of the
741 -- current type declaration, except for the pathological case below.
742
743 if Nkind_In (Related_Nod, N_Object_Declaration,
744 N_Access_Function_Definition)
745 then
746 Anon_Scope := Current_Scope;
747
748 -- A pathological case: function returning access functions that
749 -- return access functions, etc. Each anonymous access type created
750 -- is in the enclosing scope of the outermost function.
751
752 declare
753 Par : Node_Id;
754
755 begin
756 Par := Related_Nod;
757 while Nkind_In (Par, N_Access_Function_Definition,
758 N_Access_Definition)
759 loop
760 Par := Parent (Par);
761 end loop;
762
763 if Nkind (Par) = N_Function_Specification then
764 Anon_Scope := Scope (Defining_Entity (Par));
765 end if;
766 end;
767
768 -- For the anonymous function result case, retrieve the scope of the
769 -- function specification's associated entity rather than using the
770 -- current scope. The current scope will be the function itself if the
771 -- formal part is currently being analyzed, but will be the parent scope
772 -- in the case of a parameterless function, and we always want to use
773 -- the function's parent scope. Finally, if the function is a child
774 -- unit, we must traverse the tree to retrieve the proper entity.
775
776 elsif Nkind (Related_Nod) = N_Function_Specification
777 and then Nkind (Parent (N)) /= N_Parameter_Specification
778 then
779 -- If the current scope is a protected type, the anonymous access
780 -- is associated with one of the protected operations, and must
781 -- be available in the scope that encloses the protected declaration.
782 -- Otherwise the type is in the scope enclosing the subprogram.
783
784 -- If the function has formals, The return type of a subprogram
785 -- declaration is analyzed in the scope of the subprogram (see
786 -- Process_Formals) and thus the protected type, if present, is
787 -- the scope of the current function scope.
788
789 if Ekind (Current_Scope) = E_Protected_Type then
790 Enclosing_Prot_Type := Current_Scope;
791
792 elsif Ekind (Current_Scope) = E_Function
793 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
794 then
795 Enclosing_Prot_Type := Scope (Current_Scope);
796 end if;
797
798 if Present (Enclosing_Prot_Type) then
799 Anon_Scope := Scope (Enclosing_Prot_Type);
800
801 else
802 Anon_Scope := Scope (Defining_Entity (Related_Nod));
803 end if;
804
805 -- For an access type definition, if the current scope is a child
806 -- unit it is the scope of the type.
807
808 elsif Is_Compilation_Unit (Current_Scope) then
809 Anon_Scope := Current_Scope;
810
811 -- For access formals, access components, and access discriminants, the
812 -- scope is that of the enclosing declaration,
813
814 else
815 Anon_Scope := Scope (Current_Scope);
816 end if;
817
818 Anon_Type :=
819 Create_Itype
820 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
821
822 if All_Present (N)
823 and then Ada_Version >= Ada_2005
824 then
825 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
826 end if;
827
828 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
829 -- the corresponding semantic routine
830
831 if Present (Access_To_Subprogram_Definition (N)) then
832
833 -- Compiler runtime units are compiled in Ada 2005 mode when building
834 -- the runtime library but must also be compilable in Ada 95 mode
835 -- (when bootstrapping the compiler).
836
837 Check_Compiler_Unit ("anonymous access to subprogram", N);
838
839 Access_Subprogram_Declaration
840 (T_Name => Anon_Type,
841 T_Def => Access_To_Subprogram_Definition (N));
842
843 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
844 Set_Ekind
845 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
846 else
847 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
848 end if;
849
850 Set_Can_Use_Internal_Rep
851 (Anon_Type, not Always_Compatible_Rep_On_Target);
852
853 -- If the anonymous access is associated with a protected operation,
854 -- create a reference to it after the enclosing protected definition
855 -- because the itype will be used in the subsequent bodies.
856
857 -- If the anonymous access itself is protected, a full type
858 -- declaratiton will be created for it, so that the equivalent
859 -- record type can be constructed. For further details, see
860 -- Replace_Anonymous_Access_To_Protected-Subprogram.
861
862 if Ekind (Current_Scope) = E_Protected_Type
863 and then not Protected_Present (Access_To_Subprogram_Definition (N))
864 then
865 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
866 end if;
867
868 return Anon_Type;
869 end if;
870
871 Find_Type (Subtype_Mark (N));
872 Desig_Type := Entity (Subtype_Mark (N));
873
874 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
875 Set_Etype (Anon_Type, Anon_Type);
876
877 -- Make sure the anonymous access type has size and alignment fields
878 -- set, as required by gigi. This is necessary in the case of the
879 -- Task_Body_Procedure.
880
881 if not Has_Private_Component (Desig_Type) then
882 Layout_Type (Anon_Type);
883 end if;
884
885 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
886 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
887 -- the null value is allowed. In Ada 95 the null value is never allowed.
888
889 if Ada_Version >= Ada_2005 then
890 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
891 else
892 Set_Can_Never_Be_Null (Anon_Type, True);
893 end if;
894
895 -- The anonymous access type is as public as the discriminated type or
896 -- subprogram that defines it. It is imported (for back-end purposes)
897 -- if the designated type is.
898
899 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
900
901 -- Ada 2005 (AI-231): Propagate the access-constant attribute
902
903 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
904
905 -- The context is either a subprogram declaration, object declaration,
906 -- or an access discriminant, in a private or a full type declaration.
907 -- In the case of a subprogram, if the designated type is incomplete,
908 -- the operation will be a primitive operation of the full type, to be
909 -- updated subsequently. If the type is imported through a limited_with
910 -- clause, the subprogram is not a primitive operation of the type
911 -- (which is declared elsewhere in some other scope).
912
913 if Ekind (Desig_Type) = E_Incomplete_Type
914 and then not From_Limited_With (Desig_Type)
915 and then Is_Overloadable (Current_Scope)
916 then
917 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
918 Set_Has_Delayed_Freeze (Current_Scope);
919 end if;
920
921 -- If the designated type is limited and class-wide, the object might
922 -- contain tasks, so we create a Master entity for the declaration. This
923 -- must be done before expansion of the full declaration, because the
924 -- declaration may include an expression that is an allocator, whose
925 -- expansion needs the proper Master for the created tasks.
926
927 if Expander_Active
928 and then Nkind (Related_Nod) = N_Object_Declaration
929 then
930 if Is_Limited_Record (Desig_Type)
931 and then Is_Class_Wide_Type (Desig_Type)
932 and then Tasking_Allowed
933 then
934 Build_Class_Wide_Master (Anon_Type);
935
936 -- Similarly, if the type is an anonymous access that designates
937 -- tasks, create a master entity for it in the current context.
938
939 elsif Has_Task (Desig_Type)
940 and then Comes_From_Source (Related_Nod)
941 then
942 Build_Master_Entity (Defining_Identifier (Related_Nod));
943 Build_Master_Renaming (Anon_Type);
944 end if;
945 end if;
946
947 -- For a private component of a protected type, it is imperative that
948 -- the back-end elaborate the type immediately after the protected
949 -- declaration, because this type will be used in the declarations
950 -- created for the component within each protected body, so we must
951 -- create an itype reference for it now.
952
953 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
954 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
955
956 -- Similarly, if the access definition is the return result of a
957 -- function, create an itype reference for it because it will be used
958 -- within the function body. For a regular function that is not a
959 -- compilation unit, insert reference after the declaration. For a
960 -- protected operation, insert it after the enclosing protected type
961 -- declaration. In either case, do not create a reference for a type
962 -- obtained through a limited_with clause, because this would introduce
963 -- semantic dependencies.
964
965 -- Similarly, do not create a reference if the designated type is a
966 -- generic formal, because no use of it will reach the backend.
967
968 elsif Nkind (Related_Nod) = N_Function_Specification
969 and then not From_Limited_With (Desig_Type)
970 and then not Is_Generic_Type (Desig_Type)
971 then
972 if Present (Enclosing_Prot_Type) then
973 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
974
975 elsif Is_List_Member (Parent (Related_Nod))
976 and then Nkind (Parent (N)) /= N_Parameter_Specification
977 then
978 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
979 end if;
980
981 -- Finally, create an itype reference for an object declaration of an
982 -- anonymous access type. This is strictly necessary only for deferred
983 -- constants, but in any case will avoid out-of-scope problems in the
984 -- back-end.
985
986 elsif Nkind (Related_Nod) = N_Object_Declaration then
987 Build_Itype_Reference (Anon_Type, Related_Nod);
988 end if;
989
990 return Anon_Type;
991 end Access_Definition;
992
993 -----------------------------------
994 -- Access_Subprogram_Declaration --
995 -----------------------------------
996
997 procedure Access_Subprogram_Declaration
998 (T_Name : Entity_Id;
999 T_Def : Node_Id)
1000 is
1001 procedure Check_For_Premature_Usage (Def : Node_Id);
1002 -- Check that type T_Name is not used, directly or recursively, as a
1003 -- parameter or a return type in Def. Def is either a subtype, an
1004 -- access_definition, or an access_to_subprogram_definition.
1005
1006 -------------------------------
1007 -- Check_For_Premature_Usage --
1008 -------------------------------
1009
1010 procedure Check_For_Premature_Usage (Def : Node_Id) is
1011 Param : Node_Id;
1012
1013 begin
1014 -- Check for a subtype mark
1015
1016 if Nkind (Def) in N_Has_Etype then
1017 if Etype (Def) = T_Name then
1018 Error_Msg_N
1019 ("type& cannot be used before end of its declaration", Def);
1020 end if;
1021
1022 -- If this is not a subtype, then this is an access_definition
1023
1024 elsif Nkind (Def) = N_Access_Definition then
1025 if Present (Access_To_Subprogram_Definition (Def)) then
1026 Check_For_Premature_Usage
1027 (Access_To_Subprogram_Definition (Def));
1028 else
1029 Check_For_Premature_Usage (Subtype_Mark (Def));
1030 end if;
1031
1032 -- The only cases left are N_Access_Function_Definition and
1033 -- N_Access_Procedure_Definition.
1034
1035 else
1036 if Present (Parameter_Specifications (Def)) then
1037 Param := First (Parameter_Specifications (Def));
1038 while Present (Param) loop
1039 Check_For_Premature_Usage (Parameter_Type (Param));
1040 Param := Next (Param);
1041 end loop;
1042 end if;
1043
1044 if Nkind (Def) = N_Access_Function_Definition then
1045 Check_For_Premature_Usage (Result_Definition (Def));
1046 end if;
1047 end if;
1048 end Check_For_Premature_Usage;
1049
1050 -- Local variables
1051
1052 Formals : constant List_Id := Parameter_Specifications (T_Def);
1053 Formal : Entity_Id;
1054 D_Ityp : Node_Id;
1055 Desig_Type : constant Entity_Id :=
1056 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1057
1058 -- Start of processing for Access_Subprogram_Declaration
1059
1060 begin
1061 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1062
1063 -- Associate the Itype node with the inner full-type declaration or
1064 -- subprogram spec or entry body. This is required to handle nested
1065 -- anonymous declarations. For example:
1066
1067 -- procedure P
1068 -- (X : access procedure
1069 -- (Y : access procedure
1070 -- (Z : access T)))
1071
1072 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1073 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1074 N_Private_Type_Declaration,
1075 N_Private_Extension_Declaration,
1076 N_Procedure_Specification,
1077 N_Function_Specification,
1078 N_Entry_Body)
1079
1080 or else
1081 Nkind_In (D_Ityp, N_Object_Declaration,
1082 N_Object_Renaming_Declaration,
1083 N_Formal_Object_Declaration,
1084 N_Formal_Type_Declaration,
1085 N_Task_Type_Declaration,
1086 N_Protected_Type_Declaration))
1087 loop
1088 D_Ityp := Parent (D_Ityp);
1089 pragma Assert (D_Ityp /= Empty);
1090 end loop;
1091
1092 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1093
1094 if Nkind_In (D_Ityp, N_Procedure_Specification,
1095 N_Function_Specification)
1096 then
1097 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1098
1099 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1100 N_Object_Declaration,
1101 N_Object_Renaming_Declaration,
1102 N_Formal_Type_Declaration)
1103 then
1104 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1105 end if;
1106
1107 if Nkind (T_Def) = N_Access_Function_Definition then
1108 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1109 declare
1110 Acc : constant Node_Id := Result_Definition (T_Def);
1111
1112 begin
1113 if Present (Access_To_Subprogram_Definition (Acc))
1114 and then
1115 Protected_Present (Access_To_Subprogram_Definition (Acc))
1116 then
1117 Set_Etype
1118 (Desig_Type,
1119 Replace_Anonymous_Access_To_Protected_Subprogram
1120 (T_Def));
1121
1122 else
1123 Set_Etype
1124 (Desig_Type,
1125 Access_Definition (T_Def, Result_Definition (T_Def)));
1126 end if;
1127 end;
1128
1129 else
1130 Analyze (Result_Definition (T_Def));
1131
1132 declare
1133 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1134
1135 begin
1136 -- If a null exclusion is imposed on the result type, then
1137 -- create a null-excluding itype (an access subtype) and use
1138 -- it as the function's Etype.
1139
1140 if Is_Access_Type (Typ)
1141 and then Null_Exclusion_In_Return_Present (T_Def)
1142 then
1143 Set_Etype (Desig_Type,
1144 Create_Null_Excluding_Itype
1145 (T => Typ,
1146 Related_Nod => T_Def,
1147 Scope_Id => Current_Scope));
1148
1149 else
1150 if From_Limited_With (Typ) then
1151
1152 -- AI05-151: Incomplete types are allowed in all basic
1153 -- declarations, including access to subprograms.
1154
1155 if Ada_Version >= Ada_2012 then
1156 null;
1157
1158 else
1159 Error_Msg_NE
1160 ("illegal use of incomplete type&",
1161 Result_Definition (T_Def), Typ);
1162 end if;
1163
1164 elsif Ekind (Current_Scope) = E_Package
1165 and then In_Private_Part (Current_Scope)
1166 then
1167 if Ekind (Typ) = E_Incomplete_Type then
1168 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1169
1170 elsif Is_Class_Wide_Type (Typ)
1171 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1172 then
1173 Append_Elmt
1174 (Desig_Type, Private_Dependents (Etype (Typ)));
1175 end if;
1176 end if;
1177
1178 Set_Etype (Desig_Type, Typ);
1179 end if;
1180 end;
1181 end if;
1182
1183 if not (Is_Type (Etype (Desig_Type))) then
1184 Error_Msg_N
1185 ("expect type in function specification",
1186 Result_Definition (T_Def));
1187 end if;
1188
1189 else
1190 Set_Etype (Desig_Type, Standard_Void_Type);
1191 end if;
1192
1193 if Present (Formals) then
1194 Push_Scope (Desig_Type);
1195
1196 -- Some special tests here. These special tests can be removed
1197 -- if and when Itypes always have proper parent pointers to their
1198 -- declarations???
1199
1200 -- Special test 1) Link defining_identifier of formals. Required by
1201 -- First_Formal to provide its functionality.
1202
1203 declare
1204 F : Node_Id;
1205
1206 begin
1207 F := First (Formals);
1208
1209 while Present (F) loop
1210 if No (Parent (Defining_Identifier (F))) then
1211 Set_Parent (Defining_Identifier (F), F);
1212 end if;
1213
1214 Next (F);
1215 end loop;
1216 end;
1217
1218 Process_Formals (Formals, Parent (T_Def));
1219
1220 -- Special test 2) End_Scope requires that the parent pointer be set
1221 -- to something reasonable, but Itypes don't have parent pointers. So
1222 -- we set it and then unset it ???
1223
1224 Set_Parent (Desig_Type, T_Name);
1225 End_Scope;
1226 Set_Parent (Desig_Type, Empty);
1227 end if;
1228
1229 -- Check for premature usage of the type being defined
1230
1231 Check_For_Premature_Usage (T_Def);
1232
1233 -- The return type and/or any parameter type may be incomplete. Mark the
1234 -- subprogram_type as depending on the incomplete type, so that it can
1235 -- be updated when the full type declaration is seen. This only applies
1236 -- to incomplete types declared in some enclosing scope, not to limited
1237 -- views from other packages.
1238
1239 -- Prior to Ada 2012, access to functions can only have in_parameters.
1240
1241 if Present (Formals) then
1242 Formal := First_Formal (Desig_Type);
1243 while Present (Formal) loop
1244 if Ekind (Formal) /= E_In_Parameter
1245 and then Nkind (T_Def) = N_Access_Function_Definition
1246 and then Ada_Version < Ada_2012
1247 then
1248 Error_Msg_N ("functions can only have IN parameters", Formal);
1249 end if;
1250
1251 if Ekind (Etype (Formal)) = E_Incomplete_Type
1252 and then In_Open_Scopes (Scope (Etype (Formal)))
1253 then
1254 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1255 Set_Has_Delayed_Freeze (Desig_Type);
1256 end if;
1257
1258 Next_Formal (Formal);
1259 end loop;
1260 end if;
1261
1262 -- Check whether an indirect call without actuals may be possible. This
1263 -- is used when resolving calls whose result is then indexed.
1264
1265 May_Need_Actuals (Desig_Type);
1266
1267 -- If the return type is incomplete, this is legal as long as the type
1268 -- is declared in the current scope and will be completed in it (rather
1269 -- than being part of limited view).
1270
1271 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1272 and then not Has_Delayed_Freeze (Desig_Type)
1273 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1274 then
1275 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1276 Set_Has_Delayed_Freeze (Desig_Type);
1277 end if;
1278
1279 Check_Delayed_Subprogram (Desig_Type);
1280
1281 if Protected_Present (T_Def) then
1282 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1283 Set_Convention (Desig_Type, Convention_Protected);
1284 else
1285 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1286 end if;
1287
1288 Set_Can_Use_Internal_Rep (T_Name,
1289 not Always_Compatible_Rep_On_Target);
1290 Set_Etype (T_Name, T_Name);
1291 Init_Size_Align (T_Name);
1292 Set_Directly_Designated_Type (T_Name, Desig_Type);
1293
1294 -- If the access_to_subprogram is not declared at the library level,
1295 -- it can only point to subprograms that are at the same or deeper
1296 -- accessibility level. The corresponding subprogram type might
1297 -- require an activation record when compiling for C.
1298
1299 Set_Needs_Activation_Record (Desig_Type,
1300 not Is_Library_Level_Entity (T_Name));
1301
1302 Generate_Reference_To_Formals (T_Name);
1303
1304 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1305
1306 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1307
1308 Check_Restriction (No_Access_Subprograms, T_Def);
1309 end Access_Subprogram_Declaration;
1310
1311 ----------------------------
1312 -- Access_Type_Declaration --
1313 ----------------------------
1314
1315 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1316 P : constant Node_Id := Parent (Def);
1317 S : constant Node_Id := Subtype_Indication (Def);
1318
1319 Full_Desig : Entity_Id;
1320
1321 begin
1322 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1323
1324 -- Check for permissible use of incomplete type
1325
1326 if Nkind (S) /= N_Subtype_Indication then
1327 Analyze (S);
1328
1329 if Present (Entity (S))
1330 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1331 then
1332 Set_Directly_Designated_Type (T, Entity (S));
1333
1334 -- If the designated type is a limited view, we cannot tell if
1335 -- the full view contains tasks, and there is no way to handle
1336 -- that full view in a client. We create a master entity for the
1337 -- scope, which will be used when a client determines that one
1338 -- is needed.
1339
1340 if From_Limited_With (Entity (S))
1341 and then not Is_Class_Wide_Type (Entity (S))
1342 then
1343 Set_Ekind (T, E_Access_Type);
1344 Build_Master_Entity (T);
1345 Build_Master_Renaming (T);
1346 end if;
1347
1348 else
1349 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1350 end if;
1351
1352 -- If the access definition is of the form: ACCESS NOT NULL ..
1353 -- the subtype indication must be of an access type. Create
1354 -- a null-excluding subtype of it.
1355
1356 if Null_Excluding_Subtype (Def) then
1357 if not Is_Access_Type (Entity (S)) then
1358 Error_Msg_N ("null exclusion must apply to access type", Def);
1359
1360 else
1361 declare
1362 Loc : constant Source_Ptr := Sloc (S);
1363 Decl : Node_Id;
1364 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1365
1366 begin
1367 Decl :=
1368 Make_Subtype_Declaration (Loc,
1369 Defining_Identifier => Nam,
1370 Subtype_Indication =>
1371 New_Occurrence_Of (Entity (S), Loc));
1372 Set_Null_Exclusion_Present (Decl);
1373 Insert_Before (Parent (Def), Decl);
1374 Analyze (Decl);
1375 Set_Entity (S, Nam);
1376 end;
1377 end if;
1378 end if;
1379
1380 else
1381 Set_Directly_Designated_Type (T,
1382 Process_Subtype (S, P, T, 'P'));
1383 end if;
1384
1385 if All_Present (Def) or Constant_Present (Def) then
1386 Set_Ekind (T, E_General_Access_Type);
1387 else
1388 Set_Ekind (T, E_Access_Type);
1389 end if;
1390
1391 Full_Desig := Designated_Type (T);
1392
1393 if Base_Type (Full_Desig) = T then
1394 Error_Msg_N ("access type cannot designate itself", S);
1395
1396 -- In Ada 2005, the type may have a limited view through some unit in
1397 -- its own context, allowing the following circularity that cannot be
1398 -- detected earlier.
1399
1400 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1401 then
1402 Error_Msg_N
1403 ("access type cannot designate its own class-wide type", S);
1404
1405 -- Clean up indication of tagged status to prevent cascaded errors
1406
1407 Set_Is_Tagged_Type (T, False);
1408 end if;
1409
1410 Set_Etype (T, T);
1411
1412 -- If the type has appeared already in a with_type clause, it is frozen
1413 -- and the pointer size is already set. Else, initialize.
1414
1415 if not From_Limited_With (T) then
1416 Init_Size_Align (T);
1417 end if;
1418
1419 -- Note that Has_Task is always false, since the access type itself
1420 -- is not a task type. See Einfo for more description on this point.
1421 -- Exactly the same consideration applies to Has_Controlled_Component
1422 -- and to Has_Protected.
1423
1424 Set_Has_Task (T, False);
1425 Set_Has_Protected (T, False);
1426 Set_Has_Timing_Event (T, False);
1427 Set_Has_Controlled_Component (T, False);
1428
1429 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1430 -- problems where an incomplete view of this entity has been previously
1431 -- established by a limited with and an overlaid version of this field
1432 -- (Stored_Constraint) was initialized for the incomplete view.
1433
1434 -- This reset is performed in most cases except where the access type
1435 -- has been created for the purposes of allocating or deallocating a
1436 -- build-in-place object. Such access types have explicitly set pools
1437 -- and finalization masters.
1438
1439 if No (Associated_Storage_Pool (T)) then
1440 Set_Finalization_Master (T, Empty);
1441 end if;
1442
1443 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1444 -- attributes
1445
1446 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1447 Set_Is_Access_Constant (T, Constant_Present (Def));
1448 end Access_Type_Declaration;
1449
1450 ----------------------------------
1451 -- Add_Interface_Tag_Components --
1452 ----------------------------------
1453
1454 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1455 Loc : constant Source_Ptr := Sloc (N);
1456 L : List_Id;
1457 Last_Tag : Node_Id;
1458
1459 procedure Add_Tag (Iface : Entity_Id);
1460 -- Add tag for one of the progenitor interfaces
1461
1462 -------------
1463 -- Add_Tag --
1464 -------------
1465
1466 procedure Add_Tag (Iface : Entity_Id) is
1467 Decl : Node_Id;
1468 Def : Node_Id;
1469 Tag : Entity_Id;
1470 Offset : Entity_Id;
1471
1472 begin
1473 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1474
1475 -- This is a reasonable place to propagate predicates
1476
1477 if Has_Predicates (Iface) then
1478 Set_Has_Predicates (Typ);
1479 end if;
1480
1481 Def :=
1482 Make_Component_Definition (Loc,
1483 Aliased_Present => True,
1484 Subtype_Indication =>
1485 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1486
1487 Tag := Make_Temporary (Loc, 'V');
1488
1489 Decl :=
1490 Make_Component_Declaration (Loc,
1491 Defining_Identifier => Tag,
1492 Component_Definition => Def);
1493
1494 Analyze_Component_Declaration (Decl);
1495
1496 Set_Analyzed (Decl);
1497 Set_Ekind (Tag, E_Component);
1498 Set_Is_Tag (Tag);
1499 Set_Is_Aliased (Tag);
1500 Set_Is_Independent (Tag);
1501 Set_Related_Type (Tag, Iface);
1502 Init_Component_Location (Tag);
1503
1504 pragma Assert (Is_Frozen (Iface));
1505
1506 Set_DT_Entry_Count (Tag,
1507 DT_Entry_Count (First_Entity (Iface)));
1508
1509 if No (Last_Tag) then
1510 Prepend (Decl, L);
1511 else
1512 Insert_After (Last_Tag, Decl);
1513 end if;
1514
1515 Last_Tag := Decl;
1516
1517 -- If the ancestor has discriminants we need to give special support
1518 -- to store the offset_to_top value of the secondary dispatch tables.
1519 -- For this purpose we add a supplementary component just after the
1520 -- field that contains the tag associated with each secondary DT.
1521
1522 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1523 Def :=
1524 Make_Component_Definition (Loc,
1525 Subtype_Indication =>
1526 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1527
1528 Offset := Make_Temporary (Loc, 'V');
1529
1530 Decl :=
1531 Make_Component_Declaration (Loc,
1532 Defining_Identifier => Offset,
1533 Component_Definition => Def);
1534
1535 Analyze_Component_Declaration (Decl);
1536
1537 Set_Analyzed (Decl);
1538 Set_Ekind (Offset, E_Component);
1539 Set_Is_Aliased (Offset);
1540 Set_Is_Independent (Offset);
1541 Set_Related_Type (Offset, Iface);
1542 Init_Component_Location (Offset);
1543 Insert_After (Last_Tag, Decl);
1544 Last_Tag := Decl;
1545 end if;
1546 end Add_Tag;
1547
1548 -- Local variables
1549
1550 Elmt : Elmt_Id;
1551 Ext : Node_Id;
1552 Comp : Node_Id;
1553
1554 -- Start of processing for Add_Interface_Tag_Components
1555
1556 begin
1557 if not RTE_Available (RE_Interface_Tag) then
1558 Error_Msg
1559 ("(Ada 2005) interface types not supported by this run-time!",
1560 Sloc (N));
1561 return;
1562 end if;
1563
1564 if Ekind (Typ) /= E_Record_Type
1565 or else (Is_Concurrent_Record_Type (Typ)
1566 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1567 or else (not Is_Concurrent_Record_Type (Typ)
1568 and then No (Interfaces (Typ))
1569 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1570 then
1571 return;
1572 end if;
1573
1574 -- Find the current last tag
1575
1576 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1577 Ext := Record_Extension_Part (Type_Definition (N));
1578 else
1579 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1580 Ext := Type_Definition (N);
1581 end if;
1582
1583 Last_Tag := Empty;
1584
1585 if not (Present (Component_List (Ext))) then
1586 Set_Null_Present (Ext, False);
1587 L := New_List;
1588 Set_Component_List (Ext,
1589 Make_Component_List (Loc,
1590 Component_Items => L,
1591 Null_Present => False));
1592 else
1593 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1594 L := Component_Items
1595 (Component_List
1596 (Record_Extension_Part
1597 (Type_Definition (N))));
1598 else
1599 L := Component_Items
1600 (Component_List
1601 (Type_Definition (N)));
1602 end if;
1603
1604 -- Find the last tag component
1605
1606 Comp := First (L);
1607 while Present (Comp) loop
1608 if Nkind (Comp) = N_Component_Declaration
1609 and then Is_Tag (Defining_Identifier (Comp))
1610 then
1611 Last_Tag := Comp;
1612 end if;
1613
1614 Next (Comp);
1615 end loop;
1616 end if;
1617
1618 -- At this point L references the list of components and Last_Tag
1619 -- references the current last tag (if any). Now we add the tag
1620 -- corresponding with all the interfaces that are not implemented
1621 -- by the parent.
1622
1623 if Present (Interfaces (Typ)) then
1624 Elmt := First_Elmt (Interfaces (Typ));
1625 while Present (Elmt) loop
1626 Add_Tag (Node (Elmt));
1627 Next_Elmt (Elmt);
1628 end loop;
1629 end if;
1630 end Add_Interface_Tag_Components;
1631
1632 -------------------------------------
1633 -- Add_Internal_Interface_Entities --
1634 -------------------------------------
1635
1636 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1637 Elmt : Elmt_Id;
1638 Iface : Entity_Id;
1639 Iface_Elmt : Elmt_Id;
1640 Iface_Prim : Entity_Id;
1641 Ifaces_List : Elist_Id;
1642 New_Subp : Entity_Id := Empty;
1643 Prim : Entity_Id;
1644 Restore_Scope : Boolean := False;
1645
1646 begin
1647 pragma Assert (Ada_Version >= Ada_2005
1648 and then Is_Record_Type (Tagged_Type)
1649 and then Is_Tagged_Type (Tagged_Type)
1650 and then Has_Interfaces (Tagged_Type)
1651 and then not Is_Interface (Tagged_Type));
1652
1653 -- Ensure that the internal entities are added to the scope of the type
1654
1655 if Scope (Tagged_Type) /= Current_Scope then
1656 Push_Scope (Scope (Tagged_Type));
1657 Restore_Scope := True;
1658 end if;
1659
1660 Collect_Interfaces (Tagged_Type, Ifaces_List);
1661
1662 Iface_Elmt := First_Elmt (Ifaces_List);
1663 while Present (Iface_Elmt) loop
1664 Iface := Node (Iface_Elmt);
1665
1666 -- Originally we excluded here from this processing interfaces that
1667 -- are parents of Tagged_Type because their primitives are located
1668 -- in the primary dispatch table (and hence no auxiliary internal
1669 -- entities are required to handle secondary dispatch tables in such
1670 -- case). However, these auxiliary entities are also required to
1671 -- handle derivations of interfaces in formals of generics (see
1672 -- Derive_Subprograms).
1673
1674 Elmt := First_Elmt (Primitive_Operations (Iface));
1675 while Present (Elmt) loop
1676 Iface_Prim := Node (Elmt);
1677
1678 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1679 Prim :=
1680 Find_Primitive_Covering_Interface
1681 (Tagged_Type => Tagged_Type,
1682 Iface_Prim => Iface_Prim);
1683
1684 if No (Prim) and then Serious_Errors_Detected > 0 then
1685 goto Continue;
1686 end if;
1687
1688 pragma Assert (Present (Prim));
1689
1690 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1691 -- differs from the name of the interface primitive then it is
1692 -- a private primitive inherited from a parent type. In such
1693 -- case, given that Tagged_Type covers the interface, the
1694 -- inherited private primitive becomes visible. For such
1695 -- purpose we add a new entity that renames the inherited
1696 -- private primitive.
1697
1698 if Chars (Prim) /= Chars (Iface_Prim) then
1699 pragma Assert (Has_Suffix (Prim, 'P'));
1700 Derive_Subprogram
1701 (New_Subp => New_Subp,
1702 Parent_Subp => Iface_Prim,
1703 Derived_Type => Tagged_Type,
1704 Parent_Type => Iface);
1705 Set_Alias (New_Subp, Prim);
1706 Set_Is_Abstract_Subprogram
1707 (New_Subp, Is_Abstract_Subprogram (Prim));
1708 end if;
1709
1710 Derive_Subprogram
1711 (New_Subp => New_Subp,
1712 Parent_Subp => Iface_Prim,
1713 Derived_Type => Tagged_Type,
1714 Parent_Type => Iface);
1715
1716 declare
1717 Anc : Entity_Id;
1718 begin
1719 if Is_Inherited_Operation (Prim)
1720 and then Present (Alias (Prim))
1721 then
1722 Anc := Alias (Prim);
1723 else
1724 Anc := Overridden_Operation (Prim);
1725 end if;
1726
1727 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1728 -- nonconforming preconditions in both an ancestor and
1729 -- a progenitor operation.
1730
1731 -- If the operation is a primitive wrapper it is an explicit
1732 -- (overriding) operqtion and all is fine.
1733
1734 if Present (Anc)
1735 and then Has_Non_Trivial_Precondition (Anc)
1736 and then Has_Non_Trivial_Precondition (Iface_Prim)
1737 then
1738 if Is_Abstract_Subprogram (Prim)
1739 or else
1740 (Ekind (Prim) = E_Procedure
1741 and then Nkind (Parent (Prim)) =
1742 N_Procedure_Specification
1743 and then Null_Present (Parent (Prim)))
1744 or else Is_Primitive_Wrapper (Prim)
1745 then
1746 null;
1747
1748 -- The operation is inherited and must be overridden
1749
1750 elsif not Comes_From_Source (Prim) then
1751 Error_Msg_NE
1752 ("&inherits non-conforming preconditions and must "
1753 & "be overridden (RM 6.1.1 (10-16)",
1754 Parent (Tagged_Type), Prim);
1755 end if;
1756 end if;
1757 end;
1758
1759 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1760 -- associated with interface types. These entities are
1761 -- only registered in the list of primitives of its
1762 -- corresponding tagged type because they are only used
1763 -- to fill the contents of the secondary dispatch tables.
1764 -- Therefore they are removed from the homonym chains.
1765
1766 Set_Is_Hidden (New_Subp);
1767 Set_Is_Internal (New_Subp);
1768 Set_Alias (New_Subp, Prim);
1769 Set_Is_Abstract_Subprogram
1770 (New_Subp, Is_Abstract_Subprogram (Prim));
1771 Set_Interface_Alias (New_Subp, Iface_Prim);
1772
1773 -- If the returned type is an interface then propagate it to
1774 -- the returned type. Needed by the thunk to generate the code
1775 -- which displaces "this" to reference the corresponding
1776 -- secondary dispatch table in the returned object.
1777
1778 if Is_Interface (Etype (Iface_Prim)) then
1779 Set_Etype (New_Subp, Etype (Iface_Prim));
1780 end if;
1781
1782 -- Internal entities associated with interface types are only
1783 -- registered in the list of primitives of the tagged type.
1784 -- They are only used to fill the contents of the secondary
1785 -- dispatch tables. Therefore they are not needed in the
1786 -- homonym chains.
1787
1788 Remove_Homonym (New_Subp);
1789
1790 -- Hidden entities associated with interfaces must have set
1791 -- the Has_Delay_Freeze attribute to ensure that, in case
1792 -- of locally defined tagged types (or compiling with static
1793 -- dispatch tables generation disabled) the corresponding
1794 -- entry of the secondary dispatch table is filled when such
1795 -- an entity is frozen.
1796
1797 Set_Has_Delayed_Freeze (New_Subp);
1798 end if;
1799
1800 <<Continue>>
1801 Next_Elmt (Elmt);
1802 end loop;
1803
1804 Next_Elmt (Iface_Elmt);
1805 end loop;
1806
1807 if Restore_Scope then
1808 Pop_Scope;
1809 end if;
1810 end Add_Internal_Interface_Entities;
1811
1812 -----------------------------------
1813 -- Analyze_Component_Declaration --
1814 -----------------------------------
1815
1816 procedure Analyze_Component_Declaration (N : Node_Id) is
1817 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1818 Id : constant Entity_Id := Defining_Identifier (N);
1819 E : constant Node_Id := Expression (N);
1820 Typ : constant Node_Id :=
1821 Subtype_Indication (Component_Definition (N));
1822 T : Entity_Id;
1823 P : Entity_Id;
1824
1825 function Contains_POC (Constr : Node_Id) return Boolean;
1826 -- Determines whether a constraint uses the discriminant of a record
1827 -- type thus becoming a per-object constraint (POC).
1828
1829 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1830 -- Typ is the type of the current component, check whether this type is
1831 -- a limited type. Used to validate declaration against that of
1832 -- enclosing record.
1833
1834 ------------------
1835 -- Contains_POC --
1836 ------------------
1837
1838 function Contains_POC (Constr : Node_Id) return Boolean is
1839 begin
1840 -- Prevent cascaded errors
1841
1842 if Error_Posted (Constr) then
1843 return False;
1844 end if;
1845
1846 case Nkind (Constr) is
1847 when N_Attribute_Reference =>
1848 return Attribute_Name (Constr) = Name_Access
1849 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1850
1851 when N_Discriminant_Association =>
1852 return Denotes_Discriminant (Expression (Constr));
1853
1854 when N_Identifier =>
1855 return Denotes_Discriminant (Constr);
1856
1857 when N_Index_Or_Discriminant_Constraint =>
1858 declare
1859 IDC : Node_Id;
1860
1861 begin
1862 IDC := First (Constraints (Constr));
1863 while Present (IDC) loop
1864
1865 -- One per-object constraint is sufficient
1866
1867 if Contains_POC (IDC) then
1868 return True;
1869 end if;
1870
1871 Next (IDC);
1872 end loop;
1873
1874 return False;
1875 end;
1876
1877 when N_Range =>
1878 return Denotes_Discriminant (Low_Bound (Constr))
1879 or else
1880 Denotes_Discriminant (High_Bound (Constr));
1881
1882 when N_Range_Constraint =>
1883 return Denotes_Discriminant (Range_Expression (Constr));
1884
1885 when others =>
1886 return False;
1887 end case;
1888 end Contains_POC;
1889
1890 ----------------------
1891 -- Is_Known_Limited --
1892 ----------------------
1893
1894 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1895 P : constant Entity_Id := Etype (Typ);
1896 R : constant Entity_Id := Root_Type (Typ);
1897
1898 begin
1899 if Is_Limited_Record (Typ) then
1900 return True;
1901
1902 -- If the root type is limited (and not a limited interface) so is
1903 -- the current type.
1904
1905 elsif Is_Limited_Record (R)
1906 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1907 then
1908 return True;
1909
1910 -- Else the type may have a limited interface progenitor, but a
1911 -- limited record parent that is not an interface.
1912
1913 elsif R /= P
1914 and then Is_Limited_Record (P)
1915 and then not Is_Interface (P)
1916 then
1917 return True;
1918
1919 else
1920 return False;
1921 end if;
1922 end Is_Known_Limited;
1923
1924 -- Start of processing for Analyze_Component_Declaration
1925
1926 begin
1927 Generate_Definition (Id);
1928 Enter_Name (Id);
1929
1930 if Present (Typ) then
1931 T := Find_Type_Of_Object
1932 (Subtype_Indication (Component_Definition (N)), N);
1933
1934 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1935 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1936 end if;
1937
1938 -- Ada 2005 (AI-230): Access Definition case
1939
1940 else
1941 pragma Assert (Present
1942 (Access_Definition (Component_Definition (N))));
1943
1944 T := Access_Definition
1945 (Related_Nod => N,
1946 N => Access_Definition (Component_Definition (N)));
1947 Set_Is_Local_Anonymous_Access (T);
1948
1949 -- Ada 2005 (AI-254)
1950
1951 if Present (Access_To_Subprogram_Definition
1952 (Access_Definition (Component_Definition (N))))
1953 and then Protected_Present (Access_To_Subprogram_Definition
1954 (Access_Definition
1955 (Component_Definition (N))))
1956 then
1957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1958 end if;
1959 end if;
1960
1961 -- If the subtype is a constrained subtype of the enclosing record,
1962 -- (which must have a partial view) the back-end does not properly
1963 -- handle the recursion. Rewrite the component declaration with an
1964 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1965 -- the tree directly because side effects have already been removed from
1966 -- discriminant constraints.
1967
1968 if Ekind (T) = E_Access_Subtype
1969 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1970 and then Comes_From_Source (T)
1971 and then Nkind (Parent (T)) = N_Subtype_Declaration
1972 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1973 then
1974 Rewrite
1975 (Subtype_Indication (Component_Definition (N)),
1976 New_Copy_Tree (Subtype_Indication (Parent (T))));
1977 T := Find_Type_Of_Object
1978 (Subtype_Indication (Component_Definition (N)), N);
1979 end if;
1980
1981 -- If the component declaration includes a default expression, then we
1982 -- check that the component is not of a limited type (RM 3.7(5)),
1983 -- and do the special preanalysis of the expression (see section on
1984 -- "Handling of Default and Per-Object Expressions" in the spec of
1985 -- package Sem).
1986
1987 if Present (E) then
1988 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1989 Preanalyze_Default_Expression (E, T);
1990 Check_Initialization (T, E);
1991
1992 if Ada_Version >= Ada_2005
1993 and then Ekind (T) = E_Anonymous_Access_Type
1994 and then Etype (E) /= Any_Type
1995 then
1996 -- Check RM 3.9.2(9): "if the expected type for an expression is
1997 -- an anonymous access-to-specific tagged type, then the object
1998 -- designated by the expression shall not be dynamically tagged
1999 -- unless it is a controlling operand in a call on a dispatching
2000 -- operation"
2001
2002 if Is_Tagged_Type (Directly_Designated_Type (T))
2003 and then
2004 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2005 and then
2006 Ekind (Directly_Designated_Type (Etype (E))) =
2007 E_Class_Wide_Type
2008 then
2009 Error_Msg_N
2010 ("access to specific tagged type required (RM 3.9.2(9))", E);
2011 end if;
2012
2013 -- (Ada 2005: AI-230): Accessibility check for anonymous
2014 -- components
2015
2016 if Type_Access_Level (Etype (E)) >
2017 Deepest_Type_Access_Level (T)
2018 then
2019 Error_Msg_N
2020 ("expression has deeper access level than component " &
2021 "(RM 3.10.2 (12.2))", E);
2022 end if;
2023
2024 -- The initialization expression is a reference to an access
2025 -- discriminant. The type of the discriminant is always deeper
2026 -- than any access type.
2027
2028 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2029 and then Is_Entity_Name (E)
2030 and then Ekind (Entity (E)) = E_In_Parameter
2031 and then Present (Discriminal_Link (Entity (E)))
2032 then
2033 Error_Msg_N
2034 ("discriminant has deeper accessibility level than target",
2035 E);
2036 end if;
2037 end if;
2038 end if;
2039
2040 -- Avoid reporting spurious errors if the component is initialized with
2041 -- a raise expression (which is legal in any expression context)
2042
2043 if Present (E)
2044 and then
2045 (Nkind (E) = N_Raise_Expression
2046 or else (Nkind (E) = N_Qualified_Expression
2047 and then Nkind (Expression (E)) = N_Raise_Expression))
2048 then
2049 null;
2050
2051 -- The parent type may be a private view with unknown discriminants,
2052 -- and thus unconstrained. Regular components must be constrained.
2053
2054 elsif not Is_Definite_Subtype (T)
2055 and then Chars (Id) /= Name_uParent
2056 then
2057 if Is_Class_Wide_Type (T) then
2058 Error_Msg_N
2059 ("class-wide subtype with unknown discriminants" &
2060 " in component declaration",
2061 Subtype_Indication (Component_Definition (N)));
2062 else
2063 Error_Msg_N
2064 ("unconstrained subtype in component declaration",
2065 Subtype_Indication (Component_Definition (N)));
2066 end if;
2067
2068 -- Components cannot be abstract, except for the special case of
2069 -- the _Parent field (case of extending an abstract tagged type)
2070
2071 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2072 Error_Msg_N ("type of a component cannot be abstract", N);
2073 end if;
2074
2075 Set_Etype (Id, T);
2076
2077 if Aliased_Present (Component_Definition (N)) then
2078 Set_Is_Aliased (Id);
2079
2080 -- AI12-001: All aliased objects are considered to be specified as
2081 -- independently addressable (RM C.6(8.1/4)).
2082
2083 Set_Is_Independent (Id);
2084 end if;
2085
2086 -- The component declaration may have a per-object constraint, set
2087 -- the appropriate flag in the defining identifier of the subtype.
2088
2089 if Present (Subtype_Indication (Component_Definition (N))) then
2090 declare
2091 Sindic : constant Node_Id :=
2092 Subtype_Indication (Component_Definition (N));
2093 begin
2094 if Nkind (Sindic) = N_Subtype_Indication
2095 and then Present (Constraint (Sindic))
2096 and then Contains_POC (Constraint (Sindic))
2097 then
2098 Set_Has_Per_Object_Constraint (Id);
2099 end if;
2100 end;
2101 end if;
2102
2103 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2104 -- out some static checks.
2105
2106 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2107 Null_Exclusion_Static_Checks (N);
2108 end if;
2109
2110 -- If this component is private (or depends on a private type), flag the
2111 -- record type to indicate that some operations are not available.
2112
2113 P := Private_Component (T);
2114
2115 if Present (P) then
2116
2117 -- Check for circular definitions
2118
2119 if P = Any_Type then
2120 Set_Etype (Id, Any_Type);
2121
2122 -- There is a gap in the visibility of operations only if the
2123 -- component type is not defined in the scope of the record type.
2124
2125 elsif Scope (P) = Scope (Current_Scope) then
2126 null;
2127
2128 elsif Is_Limited_Type (P) then
2129 Set_Is_Limited_Composite (Current_Scope);
2130
2131 else
2132 Set_Is_Private_Composite (Current_Scope);
2133 end if;
2134 end if;
2135
2136 if P /= Any_Type
2137 and then Is_Limited_Type (T)
2138 and then Chars (Id) /= Name_uParent
2139 and then Is_Tagged_Type (Current_Scope)
2140 then
2141 if Is_Derived_Type (Current_Scope)
2142 and then not Is_Known_Limited (Current_Scope)
2143 then
2144 Error_Msg_N
2145 ("extension of nonlimited type cannot have limited components",
2146 N);
2147
2148 if Is_Interface (Root_Type (Current_Scope)) then
2149 Error_Msg_N
2150 ("\limitedness is not inherited from limited interface", N);
2151 Error_Msg_N ("\add LIMITED to type indication", N);
2152 end if;
2153
2154 Explain_Limited_Type (T, N);
2155 Set_Etype (Id, Any_Type);
2156 Set_Is_Limited_Composite (Current_Scope, False);
2157
2158 elsif not Is_Derived_Type (Current_Scope)
2159 and then not Is_Limited_Record (Current_Scope)
2160 and then not Is_Concurrent_Type (Current_Scope)
2161 then
2162 Error_Msg_N
2163 ("nonlimited tagged type cannot have limited components", N);
2164 Explain_Limited_Type (T, N);
2165 Set_Etype (Id, Any_Type);
2166 Set_Is_Limited_Composite (Current_Scope, False);
2167 end if;
2168 end if;
2169
2170 -- If the component is an unconstrained task or protected type with
2171 -- discriminants, the component and the enclosing record are limited
2172 -- and the component is constrained by its default values. Compute
2173 -- its actual subtype, else it may be allocated the maximum size by
2174 -- the backend, and possibly overflow.
2175
2176 if Is_Concurrent_Type (T)
2177 and then not Is_Constrained (T)
2178 and then Has_Discriminants (T)
2179 and then not Has_Discriminants (Current_Scope)
2180 then
2181 declare
2182 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2183
2184 begin
2185 Set_Etype (Id, Act_T);
2186
2187 -- Rewrite component definition to use the constrained subtype
2188
2189 Rewrite (Component_Definition (N),
2190 Make_Component_Definition (Loc,
2191 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2192 end;
2193 end if;
2194
2195 Set_Original_Record_Component (Id, Id);
2196
2197 if Has_Aspects (N) then
2198 Analyze_Aspect_Specifications (N, Id);
2199 end if;
2200
2201 Analyze_Dimension (N);
2202 end Analyze_Component_Declaration;
2203
2204 --------------------------
2205 -- Analyze_Declarations --
2206 --------------------------
2207
2208 procedure Analyze_Declarations (L : List_Id) is
2209 Decl : Node_Id;
2210
2211 procedure Adjust_Decl;
2212 -- Adjust Decl not to include implicit label declarations, since these
2213 -- have strange Sloc values that result in elaboration check problems.
2214 -- (They have the sloc of the label as found in the source, and that
2215 -- is ahead of the current declarative part).
2216
2217 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2218 -- Create the subprogram bodies which verify the run-time semantics of
2219 -- the pragmas listed below for each elibigle type found in declarative
2220 -- list Decls. The pragmas are:
2221 --
2222 -- Default_Initial_Condition
2223 -- Invariant
2224 -- Type_Invariant
2225 --
2226 -- Context denotes the owner of the declarative list.
2227
2228 procedure Check_Entry_Contracts;
2229 -- Perform a preanalysis of the pre- and postconditions of an entry
2230 -- declaration. This must be done before full resolution and creation
2231 -- of the parameter block, etc. to catch illegal uses within the
2232 -- contract expression. Full analysis of the expression is done when
2233 -- the contract is processed.
2234
2235 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2236 -- Check if a nested package has entities within it that rely on library
2237 -- level private types where the full view has not been completed for
2238 -- the purposes of checking if it is acceptable to freeze an expression
2239 -- function at the point of declaration.
2240
2241 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2242 -- Determine whether Body_Decl denotes the body of a late controlled
2243 -- primitive (either Initialize, Adjust or Finalize). If this is the
2244 -- case, add a proper spec if the body lacks one. The spec is inserted
2245 -- before Body_Decl and immediately analyzed.
2246
2247 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2248 -- Spec_Id is the entity of a package that may define abstract states,
2249 -- and in the case of a child unit, whose ancestors may define abstract
2250 -- states. If the states have partial visible refinement, remove the
2251 -- partial visibility of each constituent at the end of the package
2252 -- spec and body declarations.
2253
2254 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2255 -- Spec_Id is the entity of a package that may define abstract states.
2256 -- If the states have visible refinement, remove the visibility of each
2257 -- constituent at the end of the package body declaration.
2258
2259 procedure Resolve_Aspects;
2260 -- Utility to resolve the expressions of aspects at the end of a list of
2261 -- declarations, or before a declaration that freezes previous entities,
2262 -- such as in a subprogram body.
2263
2264 -----------------
2265 -- Adjust_Decl --
2266 -----------------
2267
2268 procedure Adjust_Decl is
2269 begin
2270 while Present (Prev (Decl))
2271 and then Nkind (Decl) = N_Implicit_Label_Declaration
2272 loop
2273 Prev (Decl);
2274 end loop;
2275 end Adjust_Decl;
2276
2277 ----------------------------
2278 -- Build_Assertion_Bodies --
2279 ----------------------------
2280
2281 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2282 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2283 -- Create the subprogram bodies which verify the run-time semantics
2284 -- of the pragmas listed below for type Typ. The pragmas are:
2285 --
2286 -- Default_Initial_Condition
2287 -- Invariant
2288 -- Type_Invariant
2289
2290 -------------------------------------
2291 -- Build_Assertion_Bodies_For_Type --
2292 -------------------------------------
2293
2294 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2295 begin
2296 -- Preanalyze and resolve the Default_Initial_Condition assertion
2297 -- expression at the end of the declarations to catch any errors.
2298
2299 if Has_DIC (Typ) then
2300 Build_DIC_Procedure_Body (Typ);
2301 end if;
2302
2303 if Nkind (Context) = N_Package_Specification then
2304
2305 -- Preanalyze and resolve the class-wide invariants of an
2306 -- interface at the end of whichever declarative part has the
2307 -- interface type. Note that an interface may be declared in
2308 -- any non-package declarative part, but reaching the end of
2309 -- such a declarative part will always freeze the type and
2310 -- generate the invariant procedure (see Freeze_Type).
2311
2312 if Is_Interface (Typ) then
2313
2314 -- Interfaces are treated as the partial view of a private
2315 -- type, in order to achieve uniformity with the general
2316 -- case. As a result, an interface receives only a "partial"
2317 -- invariant procedure, which is never called.
2318
2319 if Has_Own_Invariants (Typ) then
2320 Build_Invariant_Procedure_Body
2321 (Typ => Typ,
2322 Partial_Invariant => True);
2323 end if;
2324
2325 -- Preanalyze and resolve the invariants of a private type
2326 -- at the end of the visible declarations to catch potential
2327 -- errors. Inherited class-wide invariants are not included
2328 -- because they have already been resolved.
2329
2330 elsif Decls = Visible_Declarations (Context)
2331 and then Ekind_In (Typ, E_Limited_Private_Type,
2332 E_Private_Type,
2333 E_Record_Type_With_Private)
2334 and then Has_Own_Invariants (Typ)
2335 then
2336 Build_Invariant_Procedure_Body
2337 (Typ => Typ,
2338 Partial_Invariant => True);
2339
2340 -- Preanalyze and resolve the invariants of a private type's
2341 -- full view at the end of the private declarations to catch
2342 -- potential errors.
2343
2344 elsif Decls = Private_Declarations (Context)
2345 and then not Is_Private_Type (Typ)
2346 and then Has_Private_Declaration (Typ)
2347 and then Has_Invariants (Typ)
2348 then
2349 Build_Invariant_Procedure_Body (Typ);
2350 end if;
2351 end if;
2352 end Build_Assertion_Bodies_For_Type;
2353
2354 -- Local variables
2355
2356 Decl : Node_Id;
2357 Decl_Id : Entity_Id;
2358
2359 -- Start of processing for Build_Assertion_Bodies
2360
2361 begin
2362 Decl := First (Decls);
2363 while Present (Decl) loop
2364 if Is_Declaration (Decl) then
2365 Decl_Id := Defining_Entity (Decl);
2366
2367 if Is_Type (Decl_Id) then
2368 Build_Assertion_Bodies_For_Type (Decl_Id);
2369 end if;
2370 end if;
2371
2372 Next (Decl);
2373 end loop;
2374 end Build_Assertion_Bodies;
2375
2376 ---------------------------
2377 -- Check_Entry_Contracts --
2378 ---------------------------
2379
2380 procedure Check_Entry_Contracts is
2381 ASN : Node_Id;
2382 Ent : Entity_Id;
2383 Exp : Node_Id;
2384
2385 begin
2386 Ent := First_Entity (Current_Scope);
2387 while Present (Ent) loop
2388
2389 -- This only concerns entries with pre/postconditions
2390
2391 if Ekind (Ent) = E_Entry
2392 and then Present (Contract (Ent))
2393 and then Present (Pre_Post_Conditions (Contract (Ent)))
2394 then
2395 ASN := Pre_Post_Conditions (Contract (Ent));
2396 Push_Scope (Ent);
2397 Install_Formals (Ent);
2398
2399 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2400 -- is performed on a copy of the pragma expression, to prevent
2401 -- modifying the original expression.
2402
2403 while Present (ASN) loop
2404 if Nkind (ASN) = N_Pragma then
2405 Exp :=
2406 New_Copy_Tree
2407 (Expression
2408 (First (Pragma_Argument_Associations (ASN))));
2409 Set_Parent (Exp, ASN);
2410
2411 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2412 end if;
2413
2414 ASN := Next_Pragma (ASN);
2415 end loop;
2416
2417 End_Scope;
2418 end if;
2419
2420 Next_Entity (Ent);
2421 end loop;
2422 end Check_Entry_Contracts;
2423
2424 ----------------------------------
2425 -- Contains_Lib_Incomplete_Type --
2426 ----------------------------------
2427
2428 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2429 Curr : Entity_Id;
2430
2431 begin
2432 -- Avoid looking through scopes that do not meet the precondition of
2433 -- Pkg not being within a library unit spec.
2434
2435 if not Is_Compilation_Unit (Pkg)
2436 and then not Is_Generic_Instance (Pkg)
2437 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2438 then
2439 -- Loop through all entities in the current scope to identify
2440 -- an entity that depends on a private type.
2441
2442 Curr := First_Entity (Pkg);
2443 loop
2444 if Nkind (Curr) in N_Entity
2445 and then Depends_On_Private (Curr)
2446 then
2447 return True;
2448 end if;
2449
2450 exit when Last_Entity (Current_Scope) = Curr;
2451 Next_Entity (Curr);
2452 end loop;
2453 end if;
2454
2455 return False;
2456 end Contains_Lib_Incomplete_Type;
2457
2458 --------------------------------------
2459 -- Handle_Late_Controlled_Primitive --
2460 --------------------------------------
2461
2462 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2463 Body_Spec : constant Node_Id := Specification (Body_Decl);
2464 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2465 Loc : constant Source_Ptr := Sloc (Body_Id);
2466 Params : constant List_Id :=
2467 Parameter_Specifications (Body_Spec);
2468 Spec : Node_Id;
2469 Spec_Id : Entity_Id;
2470 Typ : Node_Id;
2471
2472 begin
2473 -- Consider only procedure bodies whose name matches one of the three
2474 -- controlled primitives.
2475
2476 if Nkind (Body_Spec) /= N_Procedure_Specification
2477 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2478 Name_Finalize,
2479 Name_Initialize)
2480 then
2481 return;
2482
2483 -- A controlled primitive must have exactly one formal which is not
2484 -- an anonymous access type.
2485
2486 elsif List_Length (Params) /= 1 then
2487 return;
2488 end if;
2489
2490 Typ := Parameter_Type (First (Params));
2491
2492 if Nkind (Typ) = N_Access_Definition then
2493 return;
2494 end if;
2495
2496 Find_Type (Typ);
2497
2498 -- The type of the formal must be derived from [Limited_]Controlled
2499
2500 if not Is_Controlled (Entity (Typ)) then
2501 return;
2502 end if;
2503
2504 -- Check whether a specification exists for this body. We do not
2505 -- analyze the spec of the body in full, because it will be analyzed
2506 -- again when the body is properly analyzed, and we cannot create
2507 -- duplicate entries in the formals chain. We look for an explicit
2508 -- specification because the body may be an overriding operation and
2509 -- an inherited spec may be present.
2510
2511 Spec_Id := Current_Entity (Body_Id);
2512
2513 while Present (Spec_Id) loop
2514 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2515 and then Scope (Spec_Id) = Current_Scope
2516 and then Present (First_Formal (Spec_Id))
2517 and then No (Next_Formal (First_Formal (Spec_Id)))
2518 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2519 and then Comes_From_Source (Spec_Id)
2520 then
2521 return;
2522 end if;
2523
2524 Spec_Id := Homonym (Spec_Id);
2525 end loop;
2526
2527 -- At this point the body is known to be a late controlled primitive.
2528 -- Generate a matching spec and insert it before the body. Note the
2529 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2530 -- tree in this case.
2531
2532 Spec := Copy_Separate_Tree (Body_Spec);
2533
2534 -- Ensure that the subprogram declaration does not inherit the null
2535 -- indicator from the body as we now have a proper spec/body pair.
2536
2537 Set_Null_Present (Spec, False);
2538
2539 -- Ensure that the freeze node is inserted after the declaration of
2540 -- the primitive since its expansion will freeze the primitive.
2541
2542 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2543
2544 Insert_Before_And_Analyze (Body_Decl, Decl);
2545 end Handle_Late_Controlled_Primitive;
2546
2547 ----------------------------------------
2548 -- Remove_Partial_Visible_Refinements --
2549 ----------------------------------------
2550
2551 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2552 State_Elmt : Elmt_Id;
2553 begin
2554 if Present (Abstract_States (Spec_Id)) then
2555 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2556 while Present (State_Elmt) loop
2557 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2558 Next_Elmt (State_Elmt);
2559 end loop;
2560 end if;
2561
2562 -- For a child unit, also hide the partial state refinement from
2563 -- ancestor packages.
2564
2565 if Is_Child_Unit (Spec_Id) then
2566 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2567 end if;
2568 end Remove_Partial_Visible_Refinements;
2569
2570 --------------------------------
2571 -- Remove_Visible_Refinements --
2572 --------------------------------
2573
2574 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2575 State_Elmt : Elmt_Id;
2576 begin
2577 if Present (Abstract_States (Spec_Id)) then
2578 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2579 while Present (State_Elmt) loop
2580 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2581 Next_Elmt (State_Elmt);
2582 end loop;
2583 end if;
2584 end Remove_Visible_Refinements;
2585
2586 ---------------------
2587 -- Resolve_Aspects --
2588 ---------------------
2589
2590 procedure Resolve_Aspects is
2591 E : Entity_Id;
2592
2593 begin
2594 E := First_Entity (Current_Scope);
2595 while Present (E) loop
2596 Resolve_Aspect_Expressions (E);
2597 Next_Entity (E);
2598 end loop;
2599 end Resolve_Aspects;
2600
2601 -- Local variables
2602
2603 Context : Node_Id := Empty;
2604 Freeze_From : Entity_Id := Empty;
2605 Next_Decl : Node_Id;
2606
2607 Body_Seen : Boolean := False;
2608 -- Flag set when the first body [stub] is encountered
2609
2610 -- Start of processing for Analyze_Declarations
2611
2612 begin
2613 if Restriction_Check_Required (SPARK_05) then
2614 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2615 end if;
2616
2617 Decl := First (L);
2618 while Present (Decl) loop
2619
2620 -- Package spec cannot contain a package declaration in SPARK
2621
2622 if Nkind (Decl) = N_Package_Declaration
2623 and then Nkind (Parent (L)) = N_Package_Specification
2624 then
2625 Check_SPARK_05_Restriction
2626 ("package specification cannot contain a package declaration",
2627 Decl);
2628 end if;
2629
2630 -- Complete analysis of declaration
2631
2632 Analyze (Decl);
2633 Next_Decl := Next (Decl);
2634
2635 if No (Freeze_From) then
2636 Freeze_From := First_Entity (Current_Scope);
2637 end if;
2638
2639 -- At the end of a declarative part, freeze remaining entities
2640 -- declared in it. The end of the visible declarations of package
2641 -- specification is not the end of a declarative part if private
2642 -- declarations are present. The end of a package declaration is a
2643 -- freezing point only if it a library package. A task definition or
2644 -- protected type definition is not a freeze point either. Finally,
2645 -- we do not freeze entities in generic scopes, because there is no
2646 -- code generated for them and freeze nodes will be generated for
2647 -- the instance.
2648
2649 -- The end of a package instantiation is not a freeze point, but
2650 -- for now we make it one, because the generic body is inserted
2651 -- (currently) immediately after. Generic instantiations will not
2652 -- be a freeze point once delayed freezing of bodies is implemented.
2653 -- (This is needed in any case for early instantiations ???).
2654
2655 if No (Next_Decl) then
2656 if Nkind (Parent (L)) = N_Component_List then
2657 null;
2658
2659 elsif Nkind_In (Parent (L), N_Protected_Definition,
2660 N_Task_Definition)
2661 then
2662 Check_Entry_Contracts;
2663
2664 elsif Nkind (Parent (L)) /= N_Package_Specification then
2665 if Nkind (Parent (L)) = N_Package_Body then
2666 Freeze_From := First_Entity (Current_Scope);
2667 end if;
2668
2669 -- There may have been several freezing points previously,
2670 -- for example object declarations or subprogram bodies, but
2671 -- at the end of a declarative part we check freezing from
2672 -- the beginning, even though entities may already be frozen,
2673 -- in order to perform visibility checks on delayed aspects.
2674
2675 Adjust_Decl;
2676
2677 -- If the current scope is a generic subprogram body. Skip the
2678 -- generic formal parameters that are not frozen here.
2679
2680 if Is_Subprogram (Current_Scope)
2681 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2682 N_Generic_Subprogram_Declaration
2683 and then Present (First_Entity (Current_Scope))
2684 then
2685 while Is_Generic_Formal (Freeze_From) loop
2686 Next_Entity (Freeze_From);
2687 end loop;
2688
2689 Freeze_All (Freeze_From, Decl);
2690 Freeze_From := Last_Entity (Current_Scope);
2691
2692 else
2693 -- For declarations in a subprogram body there is no issue
2694 -- with name resolution in aspect specifications.
2695
2696 Freeze_All (First_Entity (Current_Scope), Decl);
2697 Freeze_From := Last_Entity (Current_Scope);
2698 end if;
2699
2700 -- Current scope is a package specification
2701
2702 elsif Scope (Current_Scope) /= Standard_Standard
2703 and then not Is_Child_Unit (Current_Scope)
2704 and then No (Generic_Parent (Parent (L)))
2705 then
2706 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2707 -- resolved at the end of the immediately enclosing declaration
2708 -- list (AI05-0183-1).
2709
2710 Resolve_Aspects;
2711
2712 elsif L /= Visible_Declarations (Parent (L))
2713 or else No (Private_Declarations (Parent (L)))
2714 or else Is_Empty_List (Private_Declarations (Parent (L)))
2715 then
2716 Adjust_Decl;
2717
2718 -- End of a package declaration
2719
2720 -- This is a freeze point because it is the end of a
2721 -- compilation unit.
2722
2723 Freeze_All (First_Entity (Current_Scope), Decl);
2724 Freeze_From := Last_Entity (Current_Scope);
2725
2726 -- At the end of the visible declarations the expressions in
2727 -- aspects of all entities declared so far must be resolved.
2728 -- The entities themselves might be frozen later, and the
2729 -- generated pragmas and attribute definition clauses analyzed
2730 -- in full at that point, but name resolution must take place
2731 -- now.
2732 -- In addition to being the proper semantics, this is mandatory
2733 -- within generic units, because global name capture requires
2734 -- those expressions to be analyzed, given that the generated
2735 -- pragmas do not appear in the original generic tree.
2736
2737 elsif Serious_Errors_Detected = 0 then
2738 Resolve_Aspects;
2739 end if;
2740
2741 -- If next node is a body then freeze all types before the body.
2742 -- An exception occurs for some expander-generated bodies. If these
2743 -- are generated at places where in general language rules would not
2744 -- allow a freeze point, then we assume that the expander has
2745 -- explicitly checked that all required types are properly frozen,
2746 -- and we do not cause general freezing here. This special circuit
2747 -- is used when the encountered body is marked as having already
2748 -- been analyzed.
2749
2750 -- In all other cases (bodies that come from source, and expander
2751 -- generated bodies that have not been analyzed yet), freeze all
2752 -- types now. Note that in the latter case, the expander must take
2753 -- care to attach the bodies at a proper place in the tree so as to
2754 -- not cause unwanted freezing at that point.
2755
2756 -- It is also necessary to check for a case where both an expression
2757 -- function is used and the current scope depends on an incomplete
2758 -- private type from a library unit, otherwise premature freezing of
2759 -- the private type will occur.
2760
2761 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2762 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2763 or else not Was_Expression_Function (Next_Decl))
2764 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2765 and then not Contains_Lib_Incomplete_Type
2766 (Current_Scope)))
2767 then
2768 -- When a controlled type is frozen, the expander generates stream
2769 -- and controlled-type support routines. If the freeze is caused
2770 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2771 -- expander will end up using the wrong version of these routines,
2772 -- as the body has not been processed yet. To remedy this, detect
2773 -- a late controlled primitive and create a proper spec for it.
2774 -- This ensures that the primitive will override its inherited
2775 -- counterpart before the freeze takes place.
2776
2777 -- If the declaration we just processed is a body, do not attempt
2778 -- to examine Next_Decl as the late primitive idiom can only apply
2779 -- to the first encountered body.
2780
2781 -- ??? A cleaner approach may be possible and/or this solution
2782 -- could be extended to general-purpose late primitives, TBD.
2783
2784 if not Body_Seen and then not Is_Body (Decl) then
2785 Body_Seen := True;
2786
2787 if Nkind (Next_Decl) = N_Subprogram_Body then
2788 Handle_Late_Controlled_Primitive (Next_Decl);
2789 end if;
2790
2791 else
2792 -- In ASIS mode, if the next declaration is a body, complete
2793 -- the analysis of declarations so far.
2794 -- Is this still needed???
2795
2796 Resolve_Aspects;
2797 end if;
2798
2799 Adjust_Decl;
2800
2801 -- The generated body of an expression function does not freeze,
2802 -- unless it is a completion, in which case only the expression
2803 -- itself freezes. This is handled when the body itself is
2804 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2805
2806 Freeze_All (Freeze_From, Decl);
2807 Freeze_From := Last_Entity (Current_Scope);
2808 end if;
2809
2810 Decl := Next_Decl;
2811 end loop;
2812
2813 -- Post-freezing actions
2814
2815 if Present (L) then
2816 Context := Parent (L);
2817
2818 -- Certain contract annocations have forward visibility semantics and
2819 -- must be analyzed after all declarative items have been processed.
2820 -- This timing ensures that entities referenced by such contracts are
2821 -- visible.
2822
2823 -- Analyze the contract of an immediately enclosing package spec or
2824 -- body first because other contracts may depend on its information.
2825
2826 if Nkind (Context) = N_Package_Body then
2827 Analyze_Package_Body_Contract (Defining_Entity (Context));
2828
2829 elsif Nkind (Context) = N_Package_Specification then
2830 Analyze_Package_Contract (Defining_Entity (Context));
2831 end if;
2832
2833 -- Analyze the contracts of various constructs in the declarative
2834 -- list.
2835
2836 Analyze_Contracts (L);
2837
2838 if Nkind (Context) = N_Package_Body then
2839
2840 -- Ensure that all abstract states and objects declared in the
2841 -- state space of a package body are utilized as constituents.
2842
2843 Check_Unused_Body_States (Defining_Entity (Context));
2844
2845 -- State refinements are visible up to the end of the package body
2846 -- declarations. Hide the state refinements from visibility to
2847 -- restore the original state conditions.
2848
2849 Remove_Visible_Refinements (Corresponding_Spec (Context));
2850 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2851
2852 elsif Nkind (Context) = N_Package_Specification then
2853
2854 -- Partial state refinements are visible up to the end of the
2855 -- package spec declarations. Hide the partial state refinements
2856 -- from visibility to restore the original state conditions.
2857
2858 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2859 end if;
2860
2861 -- Verify that all abstract states found in any package declared in
2862 -- the input declarative list have proper refinements. The check is
2863 -- performed only when the context denotes a block, entry, package,
2864 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2865
2866 Check_State_Refinements (Context);
2867
2868 -- Create the subprogram bodies which verify the run-time semantics
2869 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2870 -- types within the current declarative list. This ensures that all
2871 -- assertion expressions are preanalyzed and resolved at the end of
2872 -- the declarative part. Note that the resolution happens even when
2873 -- freezing does not take place.
2874
2875 Build_Assertion_Bodies (L, Context);
2876 end if;
2877 end Analyze_Declarations;
2878
2879 -----------------------------------
2880 -- Analyze_Full_Type_Declaration --
2881 -----------------------------------
2882
2883 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2884 Def : constant Node_Id := Type_Definition (N);
2885 Def_Id : constant Entity_Id := Defining_Identifier (N);
2886 T : Entity_Id;
2887 Prev : Entity_Id;
2888
2889 Is_Remote : constant Boolean :=
2890 (Is_Remote_Types (Current_Scope)
2891 or else Is_Remote_Call_Interface (Current_Scope))
2892 and then not (In_Private_Part (Current_Scope)
2893 or else In_Package_Body (Current_Scope));
2894
2895 procedure Check_Nonoverridable_Aspects;
2896 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2897 -- be overridden, and can only be confirmed on derivation.
2898
2899 procedure Check_Ops_From_Incomplete_Type;
2900 -- If there is a tagged incomplete partial view of the type, traverse
2901 -- the primitives of the incomplete view and change the type of any
2902 -- controlling formals and result to indicate the full view. The
2903 -- primitives will be added to the full type's primitive operations
2904 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2905 -- is called from Process_Incomplete_Dependents).
2906
2907 ----------------------------------
2908 -- Check_Nonoverridable_Aspects --
2909 ----------------------------------
2910
2911 procedure Check_Nonoverridable_Aspects is
2912 function Get_Aspect_Spec
2913 (Specs : List_Id;
2914 Aspect_Name : Name_Id) return Node_Id;
2915 -- Check whether a list of aspect specifications includes an entry
2916 -- for a specific aspect. The list is either that of a partial or
2917 -- a full view.
2918
2919 ---------------------
2920 -- Get_Aspect_Spec --
2921 ---------------------
2922
2923 function Get_Aspect_Spec
2924 (Specs : List_Id;
2925 Aspect_Name : Name_Id) return Node_Id
2926 is
2927 Spec : Node_Id;
2928
2929 begin
2930 Spec := First (Specs);
2931 while Present (Spec) loop
2932 if Chars (Identifier (Spec)) = Aspect_Name then
2933 return Spec;
2934 end if;
2935 Next (Spec);
2936 end loop;
2937
2938 return Empty;
2939 end Get_Aspect_Spec;
2940
2941 -- Local variables
2942
2943 Prev_Aspects : constant List_Id :=
2944 Aspect_Specifications (Parent (Def_Id));
2945 Par_Type : Entity_Id;
2946 Prev_Aspect : Node_Id;
2947
2948 -- Start of processing for Check_Nonoverridable_Aspects
2949
2950 begin
2951 -- Get parent type of derived type. Note that Prev is the entity in
2952 -- the partial declaration, but its contents are now those of full
2953 -- view, while Def_Id reflects the partial view.
2954
2955 if Is_Private_Type (Def_Id) then
2956 Par_Type := Etype (Full_View (Def_Id));
2957 else
2958 Par_Type := Etype (Def_Id);
2959 end if;
2960
2961 -- If there is an inherited Implicit_Dereference, verify that it is
2962 -- made explicit in the partial view.
2963
2964 if Has_Discriminants (Base_Type (Par_Type))
2965 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2966 and then Present (Discriminant_Specifications (Parent (Prev)))
2967 and then Present (Get_Reference_Discriminant (Par_Type))
2968 then
2969 Prev_Aspect :=
2970 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2971
2972 if No (Prev_Aspect)
2973 and then Present
2974 (Discriminant_Specifications
2975 (Original_Node (Parent (Prev))))
2976 then
2977 Error_Msg_N
2978 ("type does not inherit implicit dereference", Prev);
2979
2980 else
2981 -- If one of the views has the aspect specified, verify that it
2982 -- is consistent with that of the parent.
2983
2984 declare
2985 Cur_Discr : constant Entity_Id :=
2986 Get_Reference_Discriminant (Prev);
2987 Par_Discr : constant Entity_Id :=
2988 Get_Reference_Discriminant (Par_Type);
2989
2990 begin
2991 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2992 Error_Msg_N
2993 ("aspect inconsistent with that of parent", N);
2994 end if;
2995
2996 -- Check that specification in partial view matches the
2997 -- inherited aspect. Compare names directly because aspect
2998 -- expression may not be analyzed.
2999
3000 if Present (Prev_Aspect)
3001 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3002 and then Chars (Expression (Prev_Aspect)) /=
3003 Chars (Cur_Discr)
3004 then
3005 Error_Msg_N
3006 ("aspect inconsistent with that of parent", N);
3007 end if;
3008 end;
3009 end if;
3010 end if;
3011
3012 -- TBD : other nonoverridable aspects.
3013 end Check_Nonoverridable_Aspects;
3014
3015 ------------------------------------
3016 -- Check_Ops_From_Incomplete_Type --
3017 ------------------------------------
3018
3019 procedure Check_Ops_From_Incomplete_Type is
3020 Elmt : Elmt_Id;
3021 Formal : Entity_Id;
3022 Op : Entity_Id;
3023
3024 begin
3025 if Prev /= T
3026 and then Ekind (Prev) = E_Incomplete_Type
3027 and then Is_Tagged_Type (Prev)
3028 and then Is_Tagged_Type (T)
3029 then
3030 Elmt := First_Elmt (Primitive_Operations (Prev));
3031 while Present (Elmt) loop
3032 Op := Node (Elmt);
3033
3034 Formal := First_Formal (Op);
3035 while Present (Formal) loop
3036 if Etype (Formal) = Prev then
3037 Set_Etype (Formal, T);
3038 end if;
3039
3040 Next_Formal (Formal);
3041 end loop;
3042
3043 if Etype (Op) = Prev then
3044 Set_Etype (Op, T);
3045 end if;
3046
3047 Next_Elmt (Elmt);
3048 end loop;
3049 end if;
3050 end Check_Ops_From_Incomplete_Type;
3051
3052 -- Start of processing for Analyze_Full_Type_Declaration
3053
3054 begin
3055 Prev := Find_Type_Name (N);
3056
3057 -- The full view, if present, now points to the current type. If there
3058 -- is an incomplete partial view, set a link to it, to simplify the
3059 -- retrieval of primitive operations of the type.
3060
3061 -- Ada 2005 (AI-50217): If the type was previously decorated when
3062 -- imported through a LIMITED WITH clause, it appears as incomplete
3063 -- but has no full view.
3064
3065 if Ekind (Prev) = E_Incomplete_Type
3066 and then Present (Full_View (Prev))
3067 then
3068 T := Full_View (Prev);
3069 Set_Incomplete_View (N, Parent (Prev));
3070 else
3071 T := Prev;
3072 end if;
3073
3074 Set_Is_Pure (T, Is_Pure (Current_Scope));
3075
3076 -- We set the flag Is_First_Subtype here. It is needed to set the
3077 -- corresponding flag for the Implicit class-wide-type created
3078 -- during tagged types processing.
3079
3080 Set_Is_First_Subtype (T, True);
3081
3082 -- Only composite types other than array types are allowed to have
3083 -- discriminants.
3084
3085 case Nkind (Def) is
3086
3087 -- For derived types, the rule will be checked once we've figured
3088 -- out the parent type.
3089
3090 when N_Derived_Type_Definition =>
3091 null;
3092
3093 -- For record types, discriminants are allowed, unless we are in
3094 -- SPARK.
3095
3096 when N_Record_Definition =>
3097 if Present (Discriminant_Specifications (N)) then
3098 Check_SPARK_05_Restriction
3099 ("discriminant type is not allowed",
3100 Defining_Identifier
3101 (First (Discriminant_Specifications (N))));
3102 end if;
3103
3104 when others =>
3105 if Present (Discriminant_Specifications (N)) then
3106 Error_Msg_N
3107 ("elementary or array type cannot have discriminants",
3108 Defining_Identifier
3109 (First (Discriminant_Specifications (N))));
3110 end if;
3111 end case;
3112
3113 -- Elaborate the type definition according to kind, and generate
3114 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3115 -- already done (this happens during the reanalysis that follows a call
3116 -- to the high level optimizer).
3117
3118 if not Analyzed (T) then
3119 Set_Analyzed (T);
3120
3121 -- Set the SPARK mode from the current context
3122
3123 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3124 Set_SPARK_Pragma_Inherited (T);
3125
3126 case Nkind (Def) is
3127 when N_Access_To_Subprogram_Definition =>
3128 Access_Subprogram_Declaration (T, Def);
3129
3130 -- If this is a remote access to subprogram, we must create the
3131 -- equivalent fat pointer type, and related subprograms.
3132
3133 if Is_Remote then
3134 Process_Remote_AST_Declaration (N);
3135 end if;
3136
3137 -- Validate categorization rule against access type declaration
3138 -- usually a violation in Pure unit, Shared_Passive unit.
3139
3140 Validate_Access_Type_Declaration (T, N);
3141
3142 when N_Access_To_Object_Definition =>
3143 Access_Type_Declaration (T, Def);
3144
3145 -- Validate categorization rule against access type declaration
3146 -- usually a violation in Pure unit, Shared_Passive unit.
3147
3148 Validate_Access_Type_Declaration (T, N);
3149
3150 -- If we are in a Remote_Call_Interface package and define a
3151 -- RACW, then calling stubs and specific stream attributes
3152 -- must be added.
3153
3154 if Is_Remote
3155 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3156 then
3157 Add_RACW_Features (Def_Id);
3158 end if;
3159
3160 when N_Array_Type_Definition =>
3161 Array_Type_Declaration (T, Def);
3162
3163 when N_Derived_Type_Definition =>
3164 Derived_Type_Declaration (T, N, T /= Def_Id);
3165
3166 -- Inherit predicates from parent, and protect against illegal
3167 -- derivations.
3168
3169 if Is_Type (T) and then Has_Predicates (T) then
3170 Set_Has_Predicates (Def_Id);
3171 end if;
3172
3173 -- Save the scenario for examination by the ABE Processing
3174 -- phase.
3175
3176 Record_Elaboration_Scenario (N);
3177
3178 when N_Enumeration_Type_Definition =>
3179 Enumeration_Type_Declaration (T, Def);
3180
3181 when N_Floating_Point_Definition =>
3182 Floating_Point_Type_Declaration (T, Def);
3183
3184 when N_Decimal_Fixed_Point_Definition =>
3185 Decimal_Fixed_Point_Type_Declaration (T, Def);
3186
3187 when N_Ordinary_Fixed_Point_Definition =>
3188 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3189
3190 when N_Signed_Integer_Type_Definition =>
3191 Signed_Integer_Type_Declaration (T, Def);
3192
3193 when N_Modular_Type_Definition =>
3194 Modular_Type_Declaration (T, Def);
3195
3196 when N_Record_Definition =>
3197 Record_Type_Declaration (T, N, Prev);
3198
3199 -- If declaration has a parse error, nothing to elaborate.
3200
3201 when N_Error =>
3202 null;
3203
3204 when others =>
3205 raise Program_Error;
3206 end case;
3207 end if;
3208
3209 if Etype (T) = Any_Type then
3210 return;
3211 end if;
3212
3213 -- Controlled type is not allowed in SPARK
3214
3215 if Is_Visibly_Controlled (T) then
3216 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3217 end if;
3218
3219 -- Some common processing for all types
3220
3221 Set_Depends_On_Private (T, Has_Private_Component (T));
3222 Check_Ops_From_Incomplete_Type;
3223
3224 -- Both the declared entity, and its anonymous base type if one was
3225 -- created, need freeze nodes allocated.
3226
3227 declare
3228 B : constant Entity_Id := Base_Type (T);
3229
3230 begin
3231 -- In the case where the base type differs from the first subtype, we
3232 -- pre-allocate a freeze node, and set the proper link to the first
3233 -- subtype. Freeze_Entity will use this preallocated freeze node when
3234 -- it freezes the entity.
3235
3236 -- This does not apply if the base type is a generic type, whose
3237 -- declaration is independent of the current derived definition.
3238
3239 if B /= T and then not Is_Generic_Type (B) then
3240 Ensure_Freeze_Node (B);
3241 Set_First_Subtype_Link (Freeze_Node (B), T);
3242 end if;
3243
3244 -- A type that is imported through a limited_with clause cannot
3245 -- generate any code, and thus need not be frozen. However, an access
3246 -- type with an imported designated type needs a finalization list,
3247 -- which may be referenced in some other package that has non-limited
3248 -- visibility on the designated type. Thus we must create the
3249 -- finalization list at the point the access type is frozen, to
3250 -- prevent unsatisfied references at link time.
3251
3252 if not From_Limited_With (T) or else Is_Access_Type (T) then
3253 Set_Has_Delayed_Freeze (T);
3254 end if;
3255 end;
3256
3257 -- Case where T is the full declaration of some private type which has
3258 -- been swapped in Defining_Identifier (N).
3259
3260 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3261 Process_Full_View (N, T, Def_Id);
3262
3263 -- Record the reference. The form of this is a little strange, since
3264 -- the full declaration has been swapped in. So the first parameter
3265 -- here represents the entity to which a reference is made which is
3266 -- the "real" entity, i.e. the one swapped in, and the second
3267 -- parameter provides the reference location.
3268
3269 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3270 -- since we don't want a complaint about the full type being an
3271 -- unwanted reference to the private type
3272
3273 declare
3274 B : constant Boolean := Has_Pragma_Unreferenced (T);
3275 begin
3276 Set_Has_Pragma_Unreferenced (T, False);
3277 Generate_Reference (T, T, 'c');
3278 Set_Has_Pragma_Unreferenced (T, B);
3279 end;
3280
3281 Set_Completion_Referenced (Def_Id);
3282
3283 -- For completion of incomplete type, process incomplete dependents
3284 -- and always mark the full type as referenced (it is the incomplete
3285 -- type that we get for any real reference).
3286
3287 elsif Ekind (Prev) = E_Incomplete_Type then
3288 Process_Incomplete_Dependents (N, T, Prev);
3289 Generate_Reference (Prev, Def_Id, 'c');
3290 Set_Completion_Referenced (Def_Id);
3291
3292 -- If not private type or incomplete type completion, this is a real
3293 -- definition of a new entity, so record it.
3294
3295 else
3296 Generate_Definition (Def_Id);
3297 end if;
3298
3299 -- Propagate any pending access types whose finalization masters need to
3300 -- be fully initialized from the partial to the full view. Guard against
3301 -- an illegal full view that remains unanalyzed.
3302
3303 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3304 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3305 end if;
3306
3307 if Chars (Scope (Def_Id)) = Name_System
3308 and then Chars (Def_Id) = Name_Address
3309 and then In_Predefined_Unit (N)
3310 then
3311 Set_Is_Descendant_Of_Address (Def_Id);
3312 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3313 Set_Is_Descendant_Of_Address (Prev);
3314 end if;
3315
3316 Set_Optimize_Alignment_Flags (Def_Id);
3317 Check_Eliminated (Def_Id);
3318
3319 -- If the declaration is a completion and aspects are present, apply
3320 -- them to the entity for the type which is currently the partial
3321 -- view, but which is the one that will be frozen.
3322
3323 if Has_Aspects (N) then
3324
3325 -- In most cases the partial view is a private type, and both views
3326 -- appear in different declarative parts. In the unusual case where
3327 -- the partial view is incomplete, perform the analysis on the
3328 -- full view, to prevent freezing anomalies with the corresponding
3329 -- class-wide type, which otherwise might be frozen before the
3330 -- dispatch table is built.
3331
3332 if Prev /= Def_Id
3333 and then Ekind (Prev) /= E_Incomplete_Type
3334 then
3335 Analyze_Aspect_Specifications (N, Prev);
3336
3337 -- Normal case
3338
3339 else
3340 Analyze_Aspect_Specifications (N, Def_Id);
3341 end if;
3342 end if;
3343
3344 if Is_Derived_Type (Prev)
3345 and then Def_Id /= Prev
3346 then
3347 Check_Nonoverridable_Aspects;
3348 end if;
3349 end Analyze_Full_Type_Declaration;
3350
3351 ----------------------------------
3352 -- Analyze_Incomplete_Type_Decl --
3353 ----------------------------------
3354
3355 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3356 F : constant Boolean := Is_Pure (Current_Scope);
3357 T : Entity_Id;
3358
3359 begin
3360 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3361
3362 Generate_Definition (Defining_Identifier (N));
3363
3364 -- Process an incomplete declaration. The identifier must not have been
3365 -- declared already in the scope. However, an incomplete declaration may
3366 -- appear in the private part of a package, for a private type that has
3367 -- already been declared.
3368
3369 -- In this case, the discriminants (if any) must match
3370
3371 T := Find_Type_Name (N);
3372
3373 Set_Ekind (T, E_Incomplete_Type);
3374 Set_Etype (T, T);
3375 Set_Is_First_Subtype (T);
3376 Init_Size_Align (T);
3377
3378 -- Set the SPARK mode from the current context
3379
3380 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3381 Set_SPARK_Pragma_Inherited (T);
3382
3383 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3384 -- incomplete types.
3385
3386 if Tagged_Present (N) then
3387 Set_Is_Tagged_Type (T, True);
3388 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3389 Make_Class_Wide_Type (T);
3390 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3391 end if;
3392
3393 Set_Stored_Constraint (T, No_Elist);
3394
3395 if Present (Discriminant_Specifications (N)) then
3396 Push_Scope (T);
3397 Process_Discriminants (N);
3398 End_Scope;
3399 end if;
3400
3401 -- If the type has discriminants, nontrivial subtypes may be declared
3402 -- before the full view of the type. The full views of those subtypes
3403 -- will be built after the full view of the type.
3404
3405 Set_Private_Dependents (T, New_Elmt_List);
3406 Set_Is_Pure (T, F);
3407 end Analyze_Incomplete_Type_Decl;
3408
3409 -----------------------------------
3410 -- Analyze_Interface_Declaration --
3411 -----------------------------------
3412
3413 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3414 CW : constant Entity_Id := Class_Wide_Type (T);
3415
3416 begin
3417 Set_Is_Tagged_Type (T);
3418 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3419
3420 Set_Is_Limited_Record (T, Limited_Present (Def)
3421 or else Task_Present (Def)
3422 or else Protected_Present (Def)
3423 or else Synchronized_Present (Def));
3424
3425 -- Type is abstract if full declaration carries keyword, or if previous
3426 -- partial view did.
3427
3428 Set_Is_Abstract_Type (T);
3429 Set_Is_Interface (T);
3430
3431 -- Type is a limited interface if it includes the keyword limited, task,
3432 -- protected, or synchronized.
3433
3434 Set_Is_Limited_Interface
3435 (T, Limited_Present (Def)
3436 or else Protected_Present (Def)
3437 or else Synchronized_Present (Def)
3438 or else Task_Present (Def));
3439
3440 Set_Interfaces (T, New_Elmt_List);
3441 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3442
3443 -- Complete the decoration of the class-wide entity if it was already
3444 -- built (i.e. during the creation of the limited view)
3445
3446 if Present (CW) then
3447 Set_Is_Interface (CW);
3448 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3449 end if;
3450
3451 -- Check runtime support for synchronized interfaces
3452
3453 if (Is_Task_Interface (T)
3454 or else Is_Protected_Interface (T)
3455 or else Is_Synchronized_Interface (T))
3456 and then not RTE_Available (RE_Select_Specific_Data)
3457 then
3458 Error_Msg_CRT ("synchronized interfaces", T);
3459 end if;
3460 end Analyze_Interface_Declaration;
3461
3462 -----------------------------
3463 -- Analyze_Itype_Reference --
3464 -----------------------------
3465
3466 -- Nothing to do. This node is placed in the tree only for the benefit of
3467 -- back end processing, and has no effect on the semantic processing.
3468
3469 procedure Analyze_Itype_Reference (N : Node_Id) is
3470 begin
3471 pragma Assert (Is_Itype (Itype (N)));
3472 null;
3473 end Analyze_Itype_Reference;
3474
3475 --------------------------------
3476 -- Analyze_Number_Declaration --
3477 --------------------------------
3478
3479 procedure Analyze_Number_Declaration (N : Node_Id) is
3480 E : constant Node_Id := Expression (N);
3481 Id : constant Entity_Id := Defining_Identifier (N);
3482 Index : Interp_Index;
3483 It : Interp;
3484 T : Entity_Id;
3485
3486 begin
3487 Generate_Definition (Id);
3488 Enter_Name (Id);
3489
3490 -- This is an optimization of a common case of an integer literal
3491
3492 if Nkind (E) = N_Integer_Literal then
3493 Set_Is_Static_Expression (E, True);
3494 Set_Etype (E, Universal_Integer);
3495
3496 Set_Etype (Id, Universal_Integer);
3497 Set_Ekind (Id, E_Named_Integer);
3498 Set_Is_Frozen (Id, True);
3499
3500 Set_Debug_Info_Needed (Id);
3501 return;
3502 end if;
3503
3504 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3505
3506 -- Process expression, replacing error by integer zero, to avoid
3507 -- cascaded errors or aborts further along in the processing
3508
3509 -- Replace Error by integer zero, which seems least likely to cause
3510 -- cascaded errors.
3511
3512 if E = Error then
3513 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3514 Set_Error_Posted (E);
3515 end if;
3516
3517 Analyze (E);
3518
3519 -- Verify that the expression is static and numeric. If
3520 -- the expression is overloaded, we apply the preference
3521 -- rule that favors root numeric types.
3522
3523 if not Is_Overloaded (E) then
3524 T := Etype (E);
3525 if Has_Dynamic_Predicate_Aspect (T) then
3526 Error_Msg_N
3527 ("subtype has dynamic predicate, "
3528 & "not allowed in number declaration", N);
3529 end if;
3530
3531 else
3532 T := Any_Type;
3533
3534 Get_First_Interp (E, Index, It);
3535 while Present (It.Typ) loop
3536 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3537 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3538 then
3539 if T = Any_Type then
3540 T := It.Typ;
3541
3542 elsif It.Typ = Universal_Real
3543 or else
3544 It.Typ = Universal_Integer
3545 then
3546 -- Choose universal interpretation over any other
3547
3548 T := It.Typ;
3549 exit;
3550 end if;
3551 end if;
3552
3553 Get_Next_Interp (Index, It);
3554 end loop;
3555 end if;
3556
3557 if Is_Integer_Type (T) then
3558 Resolve (E, T);
3559 Set_Etype (Id, Universal_Integer);
3560 Set_Ekind (Id, E_Named_Integer);
3561
3562 elsif Is_Real_Type (T) then
3563
3564 -- Because the real value is converted to universal_real, this is a
3565 -- legal context for a universal fixed expression.
3566
3567 if T = Universal_Fixed then
3568 declare
3569 Loc : constant Source_Ptr := Sloc (N);
3570 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3571 Subtype_Mark =>
3572 New_Occurrence_Of (Universal_Real, Loc),
3573 Expression => Relocate_Node (E));
3574
3575 begin
3576 Rewrite (E, Conv);
3577 Analyze (E);
3578 end;
3579
3580 elsif T = Any_Fixed then
3581 Error_Msg_N ("illegal context for mixed mode operation", E);
3582
3583 -- Expression is of the form : universal_fixed * integer. Try to
3584 -- resolve as universal_real.
3585
3586 T := Universal_Real;
3587 Set_Etype (E, T);
3588 end if;
3589
3590 Resolve (E, T);
3591 Set_Etype (Id, Universal_Real);
3592 Set_Ekind (Id, E_Named_Real);
3593
3594 else
3595 Wrong_Type (E, Any_Numeric);
3596 Resolve (E, T);
3597
3598 Set_Etype (Id, T);
3599 Set_Ekind (Id, E_Constant);
3600 Set_Never_Set_In_Source (Id, True);
3601 Set_Is_True_Constant (Id, True);
3602 return;
3603 end if;
3604
3605 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3606 Set_Etype (E, Etype (Id));
3607 end if;
3608
3609 if not Is_OK_Static_Expression (E) then
3610 Flag_Non_Static_Expr
3611 ("non-static expression used in number declaration!", E);
3612 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3613 Set_Etype (E, Any_Type);
3614 end if;
3615
3616 Analyze_Dimension (N);
3617 end Analyze_Number_Declaration;
3618
3619 --------------------------------
3620 -- Analyze_Object_Declaration --
3621 --------------------------------
3622
3623 -- WARNING: This routine manages Ghost regions. Return statements must be
3624 -- replaced by gotos which jump to the end of the routine and restore the
3625 -- Ghost mode.
3626
3627 procedure Analyze_Object_Declaration (N : Node_Id) is
3628 Loc : constant Source_Ptr := Sloc (N);
3629 Id : constant Entity_Id := Defining_Identifier (N);
3630 Next_Decl : constant Node_Id := Next (N);
3631
3632 Act_T : Entity_Id;
3633 T : Entity_Id;
3634
3635 E : Node_Id := Expression (N);
3636 -- E is set to Expression (N) throughout this routine. When Expression
3637 -- (N) is modified, E is changed accordingly.
3638
3639 Prev_Entity : Entity_Id := Empty;
3640
3641 procedure Check_Dynamic_Object (Typ : Entity_Id);
3642 -- A library-level object with nonstatic discriminant constraints may
3643 -- require dynamic allocation. The declaration is illegal if the
3644 -- profile includes the restriction No_Implicit_Heap_Allocations.
3645
3646 procedure Check_For_Null_Excluding_Components
3647 (Obj_Typ : Entity_Id;
3648 Obj_Decl : Node_Id);
3649 -- Verify that each null-excluding component of object declaration
3650 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3651 -- a compile-time warning if this is not the case.
3652
3653 function Count_Tasks (T : Entity_Id) return Uint;
3654 -- This function is called when a non-generic library level object of a
3655 -- task type is declared. Its function is to count the static number of
3656 -- tasks declared within the type (it is only called if Has_Task is set
3657 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3658 -- or a variant record type is encountered, Check_Restriction is called
3659 -- indicating the count is unknown.
3660
3661 function Delayed_Aspect_Present return Boolean;
3662 -- If the declaration has an expression that is an aggregate, and it
3663 -- has aspects that require delayed analysis, the resolution of the
3664 -- aggregate must be deferred to the freeze point of the object. This
3665 -- special processing was created for address clauses, but it must
3666 -- also apply to Alignment. This must be done before the aspect
3667 -- specifications are analyzed because we must handle the aggregate
3668 -- before the analysis of the object declaration is complete.
3669
3670 -- Any other relevant delayed aspects on object declarations ???
3671
3672 --------------------------
3673 -- Check_Dynamic_Object --
3674 --------------------------
3675
3676 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3677 Comp : Entity_Id;
3678 Obj_Type : Entity_Id;
3679
3680 begin
3681 Obj_Type := Typ;
3682
3683 if Is_Private_Type (Obj_Type)
3684 and then Present (Full_View (Obj_Type))
3685 then
3686 Obj_Type := Full_View (Obj_Type);
3687 end if;
3688
3689 if Known_Static_Esize (Obj_Type) then
3690 return;
3691 end if;
3692
3693 if Restriction_Active (No_Implicit_Heap_Allocations)
3694 and then Expander_Active
3695 and then Has_Discriminants (Obj_Type)
3696 then
3697 Comp := First_Component (Obj_Type);
3698 while Present (Comp) loop
3699 if Known_Static_Esize (Etype (Comp))
3700 or else Size_Known_At_Compile_Time (Etype (Comp))
3701 then
3702 null;
3703
3704 elsif not Discriminated_Size (Comp)
3705 and then Comes_From_Source (Comp)
3706 then
3707 Error_Msg_NE
3708 ("component& of non-static size will violate restriction "
3709 & "No_Implicit_Heap_Allocation?", N, Comp);
3710
3711 elsif Is_Record_Type (Etype (Comp)) then
3712 Check_Dynamic_Object (Etype (Comp));
3713 end if;
3714
3715 Next_Component (Comp);
3716 end loop;
3717 end if;
3718 end Check_Dynamic_Object;
3719
3720 -----------------------------------------
3721 -- Check_For_Null_Excluding_Components --
3722 -----------------------------------------
3723
3724 procedure Check_For_Null_Excluding_Components
3725 (Obj_Typ : Entity_Id;
3726 Obj_Decl : Node_Id)
3727 is
3728 procedure Check_Component
3729 (Comp_Typ : Entity_Id;
3730 Comp_Decl : Node_Id := Empty;
3731 Array_Comp : Boolean := False);
3732 -- Apply a compile-time null-exclusion check on a component denoted
3733 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3734 -- subcomponents (if any).
3735
3736 ---------------------
3737 -- Check_Component --
3738 ---------------------
3739
3740 procedure Check_Component
3741 (Comp_Typ : Entity_Id;
3742 Comp_Decl : Node_Id := Empty;
3743 Array_Comp : Boolean := False)
3744 is
3745 Comp : Entity_Id;
3746 T : Entity_Id;
3747
3748 begin
3749 -- Do not consider internally-generated components or those that
3750 -- are already initialized.
3751
3752 if Present (Comp_Decl)
3753 and then (not Comes_From_Source (Comp_Decl)
3754 or else Present (Expression (Comp_Decl)))
3755 then
3756 return;
3757 end if;
3758
3759 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3760 and then Present (Full_View (Comp_Typ))
3761 then
3762 T := Full_View (Comp_Typ);
3763 else
3764 T := Comp_Typ;
3765 end if;
3766
3767 -- Verify a component of a null-excluding access type
3768
3769 if Is_Access_Type (T)
3770 and then Can_Never_Be_Null (T)
3771 then
3772 if Comp_Decl = Obj_Decl then
3773 Null_Exclusion_Static_Checks
3774 (N => Obj_Decl,
3775 Comp => Empty,
3776 Array_Comp => Array_Comp);
3777
3778 else
3779 Null_Exclusion_Static_Checks
3780 (N => Obj_Decl,
3781 Comp => Comp_Decl,
3782 Array_Comp => Array_Comp);
3783 end if;
3784
3785 -- Check array components
3786
3787 elsif Is_Array_Type (T) then
3788
3789 -- There is no suitable component when the object is of an
3790 -- array type. However, a namable component may appear at some
3791 -- point during the recursive inspection, but not at the top
3792 -- level. At the top level just indicate array component case.
3793
3794 if Comp_Decl = Obj_Decl then
3795 Check_Component (Component_Type (T), Array_Comp => True);
3796 else
3797 Check_Component (Component_Type (T), Comp_Decl);
3798 end if;
3799
3800 -- Verify all components of type T
3801
3802 -- Note: No checks are performed on types with discriminants due
3803 -- to complexities involving variants. ???
3804
3805 elsif (Is_Concurrent_Type (T)
3806 or else Is_Incomplete_Or_Private_Type (T)
3807 or else Is_Record_Type (T))
3808 and then not Has_Discriminants (T)
3809 then
3810 Comp := First_Component (T);
3811 while Present (Comp) loop
3812 Check_Component (Etype (Comp), Parent (Comp));
3813
3814 Next_Component (Comp);
3815 end loop;
3816 end if;
3817 end Check_Component;
3818
3819 -- Start processing for Check_For_Null_Excluding_Components
3820
3821 begin
3822 Check_Component (Obj_Typ, Obj_Decl);
3823 end Check_For_Null_Excluding_Components;
3824
3825 -----------------
3826 -- Count_Tasks --
3827 -----------------
3828
3829 function Count_Tasks (T : Entity_Id) return Uint is
3830 C : Entity_Id;
3831 X : Node_Id;
3832 V : Uint;
3833
3834 begin
3835 if Is_Task_Type (T) then
3836 return Uint_1;
3837
3838 elsif Is_Record_Type (T) then
3839 if Has_Discriminants (T) then
3840 Check_Restriction (Max_Tasks, N);
3841 return Uint_0;
3842
3843 else
3844 V := Uint_0;
3845 C := First_Component (T);
3846 while Present (C) loop
3847 V := V + Count_Tasks (Etype (C));
3848 Next_Component (C);
3849 end loop;
3850
3851 return V;
3852 end if;
3853
3854 elsif Is_Array_Type (T) then
3855 X := First_Index (T);
3856 V := Count_Tasks (Component_Type (T));
3857 while Present (X) loop
3858 C := Etype (X);
3859
3860 if not Is_OK_Static_Subtype (C) then
3861 Check_Restriction (Max_Tasks, N);
3862 return Uint_0;
3863 else
3864 V := V * (UI_Max (Uint_0,
3865 Expr_Value (Type_High_Bound (C)) -
3866 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3867 end if;
3868
3869 Next_Index (X);
3870 end loop;
3871
3872 return V;
3873
3874 else
3875 return Uint_0;
3876 end if;
3877 end Count_Tasks;
3878
3879 ----------------------------
3880 -- Delayed_Aspect_Present --
3881 ----------------------------
3882
3883 function Delayed_Aspect_Present return Boolean is
3884 A : Node_Id;
3885 A_Id : Aspect_Id;
3886
3887 begin
3888 if Present (Aspect_Specifications (N)) then
3889 A := First (Aspect_Specifications (N));
3890 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3891 while Present (A) loop
3892 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3893
3894 -- Set flag on object entity, for later processing at
3895 -- the freeze point.
3896
3897 Set_Has_Delayed_Aspects (Id);
3898 return True;
3899 end if;
3900
3901 Next (A);
3902 end loop;
3903 end if;
3904
3905 return False;
3906 end Delayed_Aspect_Present;
3907
3908 -- Local variables
3909
3910 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3911 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3912 -- Save the Ghost-related attributes to restore on exit
3913
3914 Related_Id : Entity_Id;
3915 Full_View_Present : Boolean := False;
3916
3917 -- Start of processing for Analyze_Object_Declaration
3918
3919 begin
3920 -- There are three kinds of implicit types generated by an
3921 -- object declaration:
3922
3923 -- 1. Those generated by the original Object Definition
3924
3925 -- 2. Those generated by the Expression
3926
3927 -- 3. Those used to constrain the Object Definition with the
3928 -- expression constraints when the definition is unconstrained.
3929
3930 -- They must be generated in this order to avoid order of elaboration
3931 -- issues. Thus the first step (after entering the name) is to analyze
3932 -- the object definition.
3933
3934 if Constant_Present (N) then
3935 Prev_Entity := Current_Entity_In_Scope (Id);
3936
3937 if Present (Prev_Entity)
3938 and then
3939 -- If the homograph is an implicit subprogram, it is overridden
3940 -- by the current declaration.
3941
3942 ((Is_Overloadable (Prev_Entity)
3943 and then Is_Inherited_Operation (Prev_Entity))
3944
3945 -- The current object is a discriminal generated for an entry
3946 -- family index. Even though the index is a constant, in this
3947 -- particular context there is no true constant redeclaration.
3948 -- Enter_Name will handle the visibility.
3949
3950 or else
3951 (Is_Discriminal (Id)
3952 and then Ekind (Discriminal_Link (Id)) =
3953 E_Entry_Index_Parameter)
3954
3955 -- The current object is the renaming for a generic declared
3956 -- within the instance.
3957
3958 or else
3959 (Ekind (Prev_Entity) = E_Package
3960 and then Nkind (Parent (Prev_Entity)) =
3961 N_Package_Renaming_Declaration
3962 and then not Comes_From_Source (Prev_Entity)
3963 and then
3964 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3965
3966 -- The entity may be a homonym of a private component of the
3967 -- enclosing protected object, for which we create a local
3968 -- renaming declaration. The declaration is legal, even if
3969 -- useless when it just captures that component.
3970
3971 or else
3972 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3973 and then Nkind (Parent (Prev_Entity)) =
3974 N_Object_Renaming_Declaration))
3975 then
3976 Prev_Entity := Empty;
3977 end if;
3978 end if;
3979
3980 if Present (Prev_Entity) then
3981
3982 -- The object declaration is Ghost when it completes a deferred Ghost
3983 -- constant.
3984
3985 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3986
3987 Constant_Redeclaration (Id, N, T);
3988
3989 Generate_Reference (Prev_Entity, Id, 'c');
3990 Set_Completion_Referenced (Id);
3991
3992 if Error_Posted (N) then
3993
3994 -- Type mismatch or illegal redeclaration; do not analyze
3995 -- expression to avoid cascaded errors.
3996
3997 T := Find_Type_Of_Object (Object_Definition (N), N);
3998 Set_Etype (Id, T);
3999 Set_Ekind (Id, E_Variable);
4000 goto Leave;
4001 end if;
4002
4003 -- In the normal case, enter identifier at the start to catch premature
4004 -- usage in the initialization expression.
4005
4006 else
4007 Generate_Definition (Id);
4008 Enter_Name (Id);
4009
4010 Mark_Coextensions (N, Object_Definition (N));
4011
4012 T := Find_Type_Of_Object (Object_Definition (N), N);
4013
4014 if Nkind (Object_Definition (N)) = N_Access_Definition
4015 and then Present
4016 (Access_To_Subprogram_Definition (Object_Definition (N)))
4017 and then Protected_Present
4018 (Access_To_Subprogram_Definition (Object_Definition (N)))
4019 then
4020 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4021 end if;
4022
4023 if Error_Posted (Id) then
4024 Set_Etype (Id, T);
4025 Set_Ekind (Id, E_Variable);
4026 goto Leave;
4027 end if;
4028 end if;
4029
4030 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4031 -- out some static checks.
4032
4033 if Ada_Version >= Ada_2005 then
4034
4035 -- In case of aggregates we must also take care of the correct
4036 -- initialization of nested aggregates bug this is done at the
4037 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4038
4039 if Can_Never_Be_Null (T) then
4040 if Present (Expression (N))
4041 and then Nkind (Expression (N)) = N_Aggregate
4042 then
4043 null;
4044
4045 else
4046 declare
4047 Save_Typ : constant Entity_Id := Etype (Id);
4048 begin
4049 Set_Etype (Id, T); -- Temp. decoration for static checks
4050 Null_Exclusion_Static_Checks (N);
4051 Set_Etype (Id, Save_Typ);
4052 end;
4053 end if;
4054
4055 -- We might be dealing with an object of a composite type containing
4056 -- null-excluding components without an aggregate, so we must verify
4057 -- that such components have default initialization.
4058
4059 else
4060 Check_For_Null_Excluding_Components (T, N);
4061 end if;
4062 end if;
4063
4064 -- Object is marked pure if it is in a pure scope
4065
4066 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4067
4068 -- If deferred constant, make sure context is appropriate. We detect
4069 -- a deferred constant as a constant declaration with no expression.
4070 -- A deferred constant can appear in a package body if its completion
4071 -- is by means of an interface pragma.
4072
4073 if Constant_Present (N) and then No (E) then
4074
4075 -- A deferred constant may appear in the declarative part of the
4076 -- following constructs:
4077
4078 -- blocks
4079 -- entry bodies
4080 -- extended return statements
4081 -- package specs
4082 -- package bodies
4083 -- subprogram bodies
4084 -- task bodies
4085
4086 -- When declared inside a package spec, a deferred constant must be
4087 -- completed by a full constant declaration or pragma Import. In all
4088 -- other cases, the only proper completion is pragma Import. Extended
4089 -- return statements are flagged as invalid contexts because they do
4090 -- not have a declarative part and so cannot accommodate the pragma.
4091
4092 if Ekind (Current_Scope) = E_Return_Statement then
4093 Error_Msg_N
4094 ("invalid context for deferred constant declaration (RM 7.4)",
4095 N);
4096 Error_Msg_N
4097 ("\declaration requires an initialization expression",
4098 N);
4099 Set_Constant_Present (N, False);
4100
4101 -- In Ada 83, deferred constant must be of private type
4102
4103 elsif not Is_Private_Type (T) then
4104 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4105 Error_Msg_N
4106 ("(Ada 83) deferred constant must be private type", N);
4107 end if;
4108 end if;
4109
4110 -- If not a deferred constant, then the object declaration freezes
4111 -- its type, unless the object is of an anonymous type and has delayed
4112 -- aspects. In that case the type is frozen when the object itself is.
4113
4114 else
4115 Check_Fully_Declared (T, N);
4116
4117 if Has_Delayed_Aspects (Id)
4118 and then Is_Array_Type (T)
4119 and then Is_Itype (T)
4120 then
4121 Set_Has_Delayed_Freeze (T);
4122 else
4123 Freeze_Before (N, T);
4124 end if;
4125 end if;
4126
4127 -- If the object was created by a constrained array definition, then
4128 -- set the link in both the anonymous base type and anonymous subtype
4129 -- that are built to represent the array type to point to the object.
4130
4131 if Nkind (Object_Definition (Declaration_Node (Id))) =
4132 N_Constrained_Array_Definition
4133 then
4134 Set_Related_Array_Object (T, Id);
4135 Set_Related_Array_Object (Base_Type (T), Id);
4136 end if;
4137
4138 -- Special checks for protected objects not at library level
4139
4140 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4141 Check_Restriction (No_Local_Protected_Objects, Id);
4142
4143 -- Protected objects with interrupt handlers must be at library level
4144
4145 -- Ada 2005: This test is not needed (and the corresponding clause
4146 -- in the RM is removed) because accessibility checks are sufficient
4147 -- to make handlers not at the library level illegal.
4148
4149 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4150 -- applies to the '95 version of the language as well.
4151
4152 if Is_Protected_Type (T)
4153 and then Has_Interrupt_Handler (T)
4154 and then Ada_Version < Ada_95
4155 then
4156 Error_Msg_N
4157 ("interrupt object can only be declared at library level", Id);
4158 end if;
4159 end if;
4160
4161 -- Check for violation of No_Local_Timing_Events
4162
4163 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4164 Check_Restriction (No_Local_Timing_Events, Id);
4165 end if;
4166
4167 -- The actual subtype of the object is the nominal subtype, unless
4168 -- the nominal one is unconstrained and obtained from the expression.
4169
4170 Act_T := T;
4171
4172 -- These checks should be performed before the initialization expression
4173 -- is considered, so that the Object_Definition node is still the same
4174 -- as in source code.
4175
4176 -- In SPARK, the nominal subtype is always given by a subtype mark
4177 -- and must not be unconstrained. (The only exception to this is the
4178 -- acceptance of declarations of constants of type String.)
4179
4180 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4181 then
4182 Check_SPARK_05_Restriction
4183 ("subtype mark required", Object_Definition (N));
4184
4185 elsif Is_Array_Type (T)
4186 and then not Is_Constrained (T)
4187 and then T /= Standard_String
4188 then
4189 Check_SPARK_05_Restriction
4190 ("subtype mark of constrained type expected",
4191 Object_Definition (N));
4192 end if;
4193
4194 if Is_Library_Level_Entity (Id) then
4195 Check_Dynamic_Object (T);
4196 end if;
4197
4198 -- There are no aliased objects in SPARK
4199
4200 if Aliased_Present (N) then
4201 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4202 end if;
4203
4204 -- Process initialization expression if present and not in error
4205
4206 if Present (E) and then E /= Error then
4207
4208 -- Generate an error in case of CPP class-wide object initialization.
4209 -- Required because otherwise the expansion of the class-wide
4210 -- assignment would try to use 'size to initialize the object
4211 -- (primitive that is not available in CPP tagged types).
4212
4213 if Is_Class_Wide_Type (Act_T)
4214 and then
4215 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4216 or else
4217 (Present (Full_View (Root_Type (Etype (Act_T))))
4218 and then
4219 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4220 then
4221 Error_Msg_N
4222 ("predefined assignment not available for 'C'P'P tagged types",
4223 E);
4224 end if;
4225
4226 Mark_Coextensions (N, E);
4227 Analyze (E);
4228
4229 -- In case of errors detected in the analysis of the expression,
4230 -- decorate it with the expected type to avoid cascaded errors
4231
4232 if No (Etype (E)) then
4233 Set_Etype (E, T);
4234 end if;
4235
4236 -- If an initialization expression is present, then we set the
4237 -- Is_True_Constant flag. It will be reset if this is a variable
4238 -- and it is indeed modified.
4239
4240 Set_Is_True_Constant (Id, True);
4241
4242 -- If we are analyzing a constant declaration, set its completion
4243 -- flag after analyzing and resolving the expression.
4244
4245 if Constant_Present (N) then
4246 Set_Has_Completion (Id);
4247 end if;
4248
4249 -- Set type and resolve (type may be overridden later on). Note:
4250 -- Ekind (Id) must still be E_Void at this point so that incorrect
4251 -- early usage within E is properly diagnosed.
4252
4253 Set_Etype (Id, T);
4254
4255 -- If the expression is an aggregate we must look ahead to detect
4256 -- the possible presence of an address clause, and defer resolution
4257 -- and expansion of the aggregate to the freeze point of the entity.
4258
4259 -- This is not always legal because the aggregate may contain other
4260 -- references that need freezing, e.g. references to other entities
4261 -- with address clauses. In any case, when compiling with -gnatI the
4262 -- presence of the address clause must be ignored.
4263
4264 if Comes_From_Source (N)
4265 and then Expander_Active
4266 and then Nkind (E) = N_Aggregate
4267 and then
4268 ((Present (Following_Address_Clause (N))
4269 and then not Ignore_Rep_Clauses)
4270 or else Delayed_Aspect_Present)
4271 then
4272 Set_Etype (E, T);
4273
4274 -- If the aggregate is limited it will be built in place, and its
4275 -- expansion is deferred until the object declaration is expanded.
4276
4277 -- This is also required when generating C code to ensure that an
4278 -- object with an alignment or address clause can be initialized
4279 -- by means of component by component assignments.
4280
4281 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4282 Set_Expansion_Delayed (E);
4283 end if;
4284
4285 else
4286 -- If the expression is a formal that is a "subprogram pointer"
4287 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4288 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4289 -- the corresponding check, as is done for assignments.
4290
4291 if Is_Entity_Name (E)
4292 and then Present (Entity (E))
4293 and then Is_Formal (Entity (E))
4294 and then
4295 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4296 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4297 then
4298 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4299 end if;
4300
4301 Resolve (E, T);
4302 end if;
4303
4304 -- No further action needed if E is a call to an inlined function
4305 -- which returns an unconstrained type and it has been expanded into
4306 -- a procedure call. In that case N has been replaced by an object
4307 -- declaration without initializing expression and it has been
4308 -- analyzed (see Expand_Inlined_Call).
4309
4310 if Back_End_Inlining
4311 and then Expander_Active
4312 and then Nkind (E) = N_Function_Call
4313 and then Nkind (Name (E)) in N_Has_Entity
4314 and then Is_Inlined (Entity (Name (E)))
4315 and then not Is_Constrained (Etype (E))
4316 and then Analyzed (N)
4317 and then No (Expression (N))
4318 then
4319 goto Leave;
4320 end if;
4321
4322 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4323 -- node (which was marked already-analyzed), we need to set the type
4324 -- to something other than Any_Access in order to keep gigi happy.
4325
4326 if Etype (E) = Any_Access then
4327 Set_Etype (E, T);
4328 end if;
4329
4330 -- If the object is an access to variable, the initialization
4331 -- expression cannot be an access to constant.
4332
4333 if Is_Access_Type (T)
4334 and then not Is_Access_Constant (T)
4335 and then Is_Access_Type (Etype (E))
4336 and then Is_Access_Constant (Etype (E))
4337 then
4338 Error_Msg_N
4339 ("access to variable cannot be initialized with an "
4340 & "access-to-constant expression", E);
4341 end if;
4342
4343 if not Assignment_OK (N) then
4344 Check_Initialization (T, E);
4345 end if;
4346
4347 Check_Unset_Reference (E);
4348
4349 -- If this is a variable, then set current value. If this is a
4350 -- declared constant of a scalar type with a static expression,
4351 -- indicate that it is always valid.
4352
4353 if not Constant_Present (N) then
4354 if Compile_Time_Known_Value (E) then
4355 Set_Current_Value (Id, E);
4356 end if;
4357
4358 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4359 Set_Is_Known_Valid (Id);
4360
4361 -- If it is a constant initialized with a valid nonstatic entity,
4362 -- the constant is known valid as well, and can inherit the subtype
4363 -- of the entity if it is a subtype of the given type. This info
4364 -- is preserved on the actual subtype of the constant.
4365
4366 elsif Is_Scalar_Type (T)
4367 and then Is_Entity_Name (E)
4368 and then Is_Known_Valid (Entity (E))
4369 and then In_Subrange_Of (Etype (Entity (E)), T)
4370 then
4371 Set_Is_Known_Valid (Id);
4372 Set_Ekind (Id, E_Constant);
4373 Set_Actual_Subtype (Id, Etype (Entity (E)));
4374 end if;
4375
4376 -- Deal with setting of null flags
4377
4378 if Is_Access_Type (T) then
4379 if Known_Non_Null (E) then
4380 Set_Is_Known_Non_Null (Id, True);
4381 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4382 Set_Is_Known_Null (Id, True);
4383 end if;
4384 end if;
4385
4386 -- Check incorrect use of dynamically tagged expressions
4387
4388 if Is_Tagged_Type (T) then
4389 Check_Dynamically_Tagged_Expression
4390 (Expr => E,
4391 Typ => T,
4392 Related_Nod => N);
4393 end if;
4394
4395 Apply_Scalar_Range_Check (E, T);
4396 Apply_Static_Length_Check (E, T);
4397
4398 if Nkind (Original_Node (N)) = N_Object_Declaration
4399 and then Comes_From_Source (Original_Node (N))
4400
4401 -- Only call test if needed
4402
4403 and then Restriction_Check_Required (SPARK_05)
4404 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4405 then
4406 Check_SPARK_05_Restriction
4407 ("initialization expression is not appropriate", E);
4408 end if;
4409
4410 -- A formal parameter of a specific tagged type whose related
4411 -- subprogram is subject to pragma Extensions_Visible with value
4412 -- "False" cannot be implicitly converted to a class-wide type by
4413 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4414 -- not consider internally generated expressions.
4415
4416 if Is_Class_Wide_Type (T)
4417 and then Comes_From_Source (E)
4418 and then Is_EVF_Expression (E)
4419 then
4420 Error_Msg_N
4421 ("formal parameter cannot be implicitly converted to "
4422 & "class-wide type when Extensions_Visible is False", E);
4423 end if;
4424 end if;
4425
4426 -- If the No_Streams restriction is set, check that the type of the
4427 -- object is not, and does not contain, any subtype derived from
4428 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4429 -- Has_Stream just for efficiency reasons. There is no point in
4430 -- spending time on a Has_Stream check if the restriction is not set.
4431
4432 if Restriction_Check_Required (No_Streams) then
4433 if Has_Stream (T) then
4434 Check_Restriction (No_Streams, N);
4435 end if;
4436 end if;
4437
4438 -- Deal with predicate check before we start to do major rewriting. It
4439 -- is OK to initialize and then check the initialized value, since the
4440 -- object goes out of scope if we get a predicate failure. Note that we
4441 -- do this in the analyzer and not the expander because the analyzer
4442 -- does some substantial rewriting in some cases.
4443
4444 -- We need a predicate check if the type has predicates that are not
4445 -- ignored, and if either there is an initializing expression, or for
4446 -- default initialization when we have at least one case of an explicit
4447 -- default initial value and then this is not an internal declaration
4448 -- whose initialization comes later (as for an aggregate expansion).
4449 -- If expression is an aggregate it may be expanded into assignments
4450 -- and the declaration itself is marked with No_Initialization, but
4451 -- the predicate still applies.
4452
4453 if not Suppress_Assignment_Checks (N)
4454 and then Present (Predicate_Function (T))
4455 and then not Predicates_Ignored (T)
4456 and then
4457 (not No_Initialization (N)
4458 or else (Present (E) and then Nkind (E) = N_Aggregate))
4459 and then
4460 (Present (E)
4461 or else
4462 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4463 then
4464 -- If the type has a static predicate and the expression is known at
4465 -- compile time, see if the expression satisfies the predicate.
4466
4467 if Present (E) then
4468 Check_Expression_Against_Static_Predicate (E, T);
4469 end if;
4470
4471 -- If the type is a null record and there is no explicit initial
4472 -- expression, no predicate check applies.
4473
4474 if No (E) and then Is_Null_Record_Type (T) then
4475 null;
4476
4477 -- Do not generate a predicate check if the initialization expression
4478 -- is a type conversion because the conversion has been subjected to
4479 -- the same check. This is a small optimization which avoid redundant
4480 -- checks.
4481
4482 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4483 null;
4484
4485 else
4486 -- The check must be inserted after the expanded aggregate
4487 -- expansion code, if any.
4488
4489 declare
4490 Check : constant Node_Id :=
4491 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4492
4493 begin
4494 if No (Next_Decl) then
4495 Append_To (List_Containing (N), Check);
4496 else
4497 Insert_Before (Next_Decl, Check);
4498 end if;
4499 end;
4500 end if;
4501 end if;
4502
4503 -- Case of unconstrained type
4504
4505 if not Is_Definite_Subtype (T) then
4506
4507 -- In SPARK, a declaration of unconstrained type is allowed
4508 -- only for constants of type string.
4509
4510 if Is_String_Type (T) and then not Constant_Present (N) then
4511 Check_SPARK_05_Restriction
4512 ("declaration of object of unconstrained type not allowed", N);
4513 end if;
4514
4515 -- Nothing to do in deferred constant case
4516
4517 if Constant_Present (N) and then No (E) then
4518 null;
4519
4520 -- Case of no initialization present
4521
4522 elsif No (E) then
4523 if No_Initialization (N) then
4524 null;
4525
4526 elsif Is_Class_Wide_Type (T) then
4527 Error_Msg_N
4528 ("initialization required in class-wide declaration ", N);
4529
4530 else
4531 Error_Msg_N
4532 ("unconstrained subtype not allowed (need initialization)",
4533 Object_Definition (N));
4534
4535 if Is_Record_Type (T) and then Has_Discriminants (T) then
4536 Error_Msg_N
4537 ("\provide initial value or explicit discriminant values",
4538 Object_Definition (N));
4539
4540 Error_Msg_NE
4541 ("\or give default discriminant values for type&",
4542 Object_Definition (N), T);
4543
4544 elsif Is_Array_Type (T) then
4545 Error_Msg_N
4546 ("\provide initial value or explicit array bounds",
4547 Object_Definition (N));
4548 end if;
4549 end if;
4550
4551 -- Case of initialization present but in error. Set initial
4552 -- expression as absent (but do not make above complaints)
4553
4554 elsif E = Error then
4555 Set_Expression (N, Empty);
4556 E := Empty;
4557
4558 -- Case of initialization present
4559
4560 else
4561 -- Check restrictions in Ada 83
4562
4563 if not Constant_Present (N) then
4564
4565 -- Unconstrained variables not allowed in Ada 83 mode
4566
4567 if Ada_Version = Ada_83
4568 and then Comes_From_Source (Object_Definition (N))
4569 then
4570 Error_Msg_N
4571 ("(Ada 83) unconstrained variable not allowed",
4572 Object_Definition (N));
4573 end if;
4574 end if;
4575
4576 -- Now we constrain the variable from the initializing expression
4577
4578 -- If the expression is an aggregate, it has been expanded into
4579 -- individual assignments. Retrieve the actual type from the
4580 -- expanded construct.
4581
4582 if Is_Array_Type (T)
4583 and then No_Initialization (N)
4584 and then Nkind (Original_Node (E)) = N_Aggregate
4585 then
4586 Act_T := Etype (E);
4587
4588 -- In case of class-wide interface object declarations we delay
4589 -- the generation of the equivalent record type declarations until
4590 -- its expansion because there are cases in they are not required.
4591
4592 elsif Is_Interface (T) then
4593 null;
4594
4595 -- If the type is an unchecked union, no subtype can be built from
4596 -- the expression. Rewrite declaration as a renaming, which the
4597 -- back-end can handle properly. This is a rather unusual case,
4598 -- because most unchecked_union declarations have default values
4599 -- for discriminants and are thus not indefinite.
4600
4601 elsif Is_Unchecked_Union (T) then
4602 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4603 Set_Ekind (Id, E_Constant);
4604 else
4605 Set_Ekind (Id, E_Variable);
4606 end if;
4607
4608 Rewrite (N,
4609 Make_Object_Renaming_Declaration (Loc,
4610 Defining_Identifier => Id,
4611 Subtype_Mark => New_Occurrence_Of (T, Loc),
4612 Name => E));
4613
4614 Set_Renamed_Object (Id, E);
4615 Freeze_Before (N, T);
4616 Set_Is_Frozen (Id);
4617 goto Leave;
4618
4619 else
4620 -- Ensure that the generated subtype has a unique external name
4621 -- when the related object is public. This guarantees that the
4622 -- subtype and its bounds will not be affected by switches or
4623 -- pragmas that may offset the internal counter due to extra
4624 -- generated code.
4625
4626 if Is_Public (Id) then
4627 Related_Id := Id;
4628 else
4629 Related_Id := Empty;
4630 end if;
4631
4632 Expand_Subtype_From_Expr
4633 (N => N,
4634 Unc_Type => T,
4635 Subtype_Indic => Object_Definition (N),
4636 Exp => E,
4637 Related_Id => Related_Id);
4638
4639 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4640 end if;
4641
4642 -- Propagate attributes to full view when needed.
4643
4644 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4645
4646 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4647 then
4648 Full_View_Present := True;
4649 end if;
4650
4651 if Full_View_Present then
4652 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4653 end if;
4654
4655 if Aliased_Present (N) then
4656 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4657
4658 if Full_View_Present then
4659 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4660 end if;
4661 end if;
4662
4663 Freeze_Before (N, Act_T);
4664 Freeze_Before (N, T);
4665 end if;
4666
4667 elsif Is_Array_Type (T)
4668 and then No_Initialization (N)
4669 and then (Nkind (Original_Node (E)) = N_Aggregate
4670 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4671 and then Nkind (Original_Node (Expression
4672 (Original_Node (E)))) = N_Aggregate))
4673 then
4674 if not Is_Entity_Name (Object_Definition (N)) then
4675 Act_T := Etype (E);
4676 Check_Compile_Time_Size (Act_T);
4677
4678 if Aliased_Present (N) then
4679 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4680 end if;
4681 end if;
4682
4683 -- When the given object definition and the aggregate are specified
4684 -- independently, and their lengths might differ do a length check.
4685 -- This cannot happen if the aggregate is of the form (others =>...)
4686
4687 if not Is_Constrained (T) then
4688 null;
4689
4690 elsif Nkind (E) = N_Raise_Constraint_Error then
4691
4692 -- Aggregate is statically illegal. Place back in declaration
4693
4694 Set_Expression (N, E);
4695 Set_No_Initialization (N, False);
4696
4697 elsif T = Etype (E) then
4698 null;
4699
4700 elsif Nkind (E) = N_Aggregate
4701 and then Present (Component_Associations (E))
4702 and then Present (Choice_List (First (Component_Associations (E))))
4703 and then
4704 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4705 N_Others_Choice
4706 then
4707 null;
4708
4709 else
4710 Apply_Length_Check (E, T);
4711 end if;
4712
4713 -- If the type is limited unconstrained with defaulted discriminants and
4714 -- there is no expression, then the object is constrained by the
4715 -- defaults, so it is worthwhile building the corresponding subtype.
4716
4717 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4718 and then not Is_Constrained (T)
4719 and then Has_Discriminants (T)
4720 then
4721 if No (E) then
4722 Act_T := Build_Default_Subtype (T, N);
4723 else
4724 -- Ada 2005: A limited object may be initialized by means of an
4725 -- aggregate. If the type has default discriminants it has an
4726 -- unconstrained nominal type, Its actual subtype will be obtained
4727 -- from the aggregate, and not from the default discriminants.
4728
4729 Act_T := Etype (E);
4730 end if;
4731
4732 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4733
4734 elsif Nkind (E) = N_Function_Call
4735 and then Constant_Present (N)
4736 and then Has_Unconstrained_Elements (Etype (E))
4737 then
4738 -- The back-end has problems with constants of a discriminated type
4739 -- with defaults, if the initial value is a function call. We
4740 -- generate an intermediate temporary that will receive a reference
4741 -- to the result of the call. The initialization expression then
4742 -- becomes a dereference of that temporary.
4743
4744 Remove_Side_Effects (E);
4745
4746 -- If this is a constant declaration of an unconstrained type and
4747 -- the initialization is an aggregate, we can use the subtype of the
4748 -- aggregate for the declared entity because it is immutable.
4749
4750 elsif not Is_Constrained (T)
4751 and then Has_Discriminants (T)
4752 and then Constant_Present (N)
4753 and then not Has_Unchecked_Union (T)
4754 and then Nkind (E) = N_Aggregate
4755 then
4756 Act_T := Etype (E);
4757 end if;
4758
4759 -- Check No_Wide_Characters restriction
4760
4761 Check_Wide_Character_Restriction (T, Object_Definition (N));
4762
4763 -- Indicate this is not set in source. Certainly true for constants, and
4764 -- true for variables so far (will be reset for a variable if and when
4765 -- we encounter a modification in the source).
4766
4767 Set_Never_Set_In_Source (Id);
4768
4769 -- Now establish the proper kind and type of the object
4770
4771 if Constant_Present (N) then
4772 Set_Ekind (Id, E_Constant);
4773 Set_Is_True_Constant (Id);
4774
4775 else
4776 Set_Ekind (Id, E_Variable);
4777
4778 -- A variable is set as shared passive if it appears in a shared
4779 -- passive package, and is at the outer level. This is not done for
4780 -- entities generated during expansion, because those are always
4781 -- manipulated locally.
4782
4783 if Is_Shared_Passive (Current_Scope)
4784 and then Is_Library_Level_Entity (Id)
4785 and then Comes_From_Source (Id)
4786 then
4787 Set_Is_Shared_Passive (Id);
4788 Check_Shared_Var (Id, T, N);
4789 end if;
4790
4791 -- Set Has_Initial_Value if initializing expression present. Note
4792 -- that if there is no initializing expression, we leave the state
4793 -- of this flag unchanged (usually it will be False, but notably in
4794 -- the case of exception choice variables, it will already be true).
4795
4796 if Present (E) then
4797 Set_Has_Initial_Value (Id);
4798 end if;
4799 end if;
4800
4801 -- Set the SPARK mode from the current context (may be overwritten later
4802 -- with explicit pragma).
4803
4804 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4805 Set_SPARK_Pragma_Inherited (Id);
4806
4807 -- Preserve relevant elaboration-related attributes of the context which
4808 -- are no longer available or very expensive to recompute once analysis,
4809 -- resolution, and expansion are over.
4810
4811 Mark_Elaboration_Attributes
4812 (N_Id => Id,
4813 Checks => True,
4814 Warnings => True);
4815
4816 -- Initialize alignment and size and capture alignment setting
4817
4818 Init_Alignment (Id);
4819 Init_Esize (Id);
4820 Set_Optimize_Alignment_Flags (Id);
4821
4822 -- Deal with aliased case
4823
4824 if Aliased_Present (N) then
4825 Set_Is_Aliased (Id);
4826
4827 -- AI12-001: All aliased objects are considered to be specified as
4828 -- independently addressable (RM C.6(8.1/4)).
4829
4830 Set_Is_Independent (Id);
4831
4832 -- If the object is aliased and the type is unconstrained with
4833 -- defaulted discriminants and there is no expression, then the
4834 -- object is constrained by the defaults, so it is worthwhile
4835 -- building the corresponding subtype.
4836
4837 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4838 -- unconstrained, then only establish an actual subtype if the
4839 -- nominal subtype is indefinite. In definite cases the object is
4840 -- unconstrained in Ada 2005.
4841
4842 if No (E)
4843 and then Is_Record_Type (T)
4844 and then not Is_Constrained (T)
4845 and then Has_Discriminants (T)
4846 and then (Ada_Version < Ada_2005
4847 or else not Is_Definite_Subtype (T))
4848 then
4849 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4850 end if;
4851 end if;
4852
4853 -- Now we can set the type of the object
4854
4855 Set_Etype (Id, Act_T);
4856
4857 -- Non-constant object is marked to be treated as volatile if type is
4858 -- volatile and we clear the Current_Value setting that may have been
4859 -- set above. Doing so for constants isn't required and might interfere
4860 -- with possible uses of the object as a static expression in contexts
4861 -- incompatible with volatility (e.g. as a case-statement alternative).
4862
4863 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4864 Set_Treat_As_Volatile (Id);
4865 Set_Current_Value (Id, Empty);
4866 end if;
4867
4868 -- Deal with controlled types
4869
4870 if Has_Controlled_Component (Etype (Id))
4871 or else Is_Controlled (Etype (Id))
4872 then
4873 if not Is_Library_Level_Entity (Id) then
4874 Check_Restriction (No_Nested_Finalization, N);
4875 else
4876 Validate_Controlled_Object (Id);
4877 end if;
4878 end if;
4879
4880 if Has_Task (Etype (Id)) then
4881 Check_Restriction (No_Tasking, N);
4882
4883 -- Deal with counting max tasks
4884
4885 -- Nothing to do if inside a generic
4886
4887 if Inside_A_Generic then
4888 null;
4889
4890 -- If library level entity, then count tasks
4891
4892 elsif Is_Library_Level_Entity (Id) then
4893 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4894
4895 -- If not library level entity, then indicate we don't know max
4896 -- tasks and also check task hierarchy restriction and blocking
4897 -- operation (since starting a task is definitely blocking).
4898
4899 else
4900 Check_Restriction (Max_Tasks, N);
4901 Check_Restriction (No_Task_Hierarchy, N);
4902 Check_Potentially_Blocking_Operation (N);
4903 end if;
4904
4905 -- A rather specialized test. If we see two tasks being declared
4906 -- of the same type in the same object declaration, and the task
4907 -- has an entry with an address clause, we know that program error
4908 -- will be raised at run time since we can't have two tasks with
4909 -- entries at the same address.
4910
4911 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4912 declare
4913 E : Entity_Id;
4914
4915 begin
4916 E := First_Entity (Etype (Id));
4917 while Present (E) loop
4918 if Ekind (E) = E_Entry
4919 and then Present (Get_Attribute_Definition_Clause
4920 (E, Attribute_Address))
4921 then
4922 Error_Msg_Warn := SPARK_Mode /= On;
4923 Error_Msg_N
4924 ("more than one task with same entry address<<", N);
4925 Error_Msg_N ("\Program_Error [<<", N);
4926 Insert_Action (N,
4927 Make_Raise_Program_Error (Loc,
4928 Reason => PE_Duplicated_Entry_Address));
4929 exit;
4930 end if;
4931
4932 Next_Entity (E);
4933 end loop;
4934 end;
4935 end if;
4936 end if;
4937
4938 -- Some simple constant-propagation: if the expression is a constant
4939 -- string initialized with a literal, share the literal. This avoids
4940 -- a run-time copy.
4941
4942 if Present (E)
4943 and then Is_Entity_Name (E)
4944 and then Ekind (Entity (E)) = E_Constant
4945 and then Base_Type (Etype (E)) = Standard_String
4946 then
4947 declare
4948 Val : constant Node_Id := Constant_Value (Entity (E));
4949 begin
4950 if Present (Val) and then Nkind (Val) = N_String_Literal then
4951 Rewrite (E, New_Copy (Val));
4952 end if;
4953 end;
4954 end if;
4955
4956 -- Another optimization: if the nominal subtype is unconstrained and
4957 -- the expression is a function call that returns an unconstrained
4958 -- type, rewrite the declaration as a renaming of the result of the
4959 -- call. The exceptions below are cases where the copy is expected,
4960 -- either by the back end (Aliased case) or by the semantics, as for
4961 -- initializing controlled types or copying tags for class-wide types.
4962
4963 if Present (E)
4964 and then Nkind (E) = N_Explicit_Dereference
4965 and then Nkind (Original_Node (E)) = N_Function_Call
4966 and then not Is_Library_Level_Entity (Id)
4967 and then not Is_Constrained (Underlying_Type (T))
4968 and then not Is_Aliased (Id)
4969 and then not Is_Class_Wide_Type (T)
4970 and then not Is_Controlled (T)
4971 and then not Has_Controlled_Component (Base_Type (T))
4972 and then Expander_Active
4973 then
4974 Rewrite (N,
4975 Make_Object_Renaming_Declaration (Loc,
4976 Defining_Identifier => Id,
4977 Access_Definition => Empty,
4978 Subtype_Mark => New_Occurrence_Of
4979 (Base_Type (Etype (Id)), Loc),
4980 Name => E));
4981
4982 Set_Renamed_Object (Id, E);
4983
4984 -- Force generation of debugging information for the constant and for
4985 -- the renamed function call.
4986
4987 Set_Debug_Info_Needed (Id);
4988 Set_Debug_Info_Needed (Entity (Prefix (E)));
4989 end if;
4990
4991 if Present (Prev_Entity)
4992 and then Is_Frozen (Prev_Entity)
4993 and then not Error_Posted (Id)
4994 then
4995 Error_Msg_N ("full constant declaration appears too late", N);
4996 end if;
4997
4998 Check_Eliminated (Id);
4999
5000 -- Deal with setting In_Private_Part flag if in private part
5001
5002 if Ekind (Scope (Id)) = E_Package
5003 and then In_Private_Part (Scope (Id))
5004 then
5005 Set_In_Private_Part (Id);
5006 end if;
5007
5008 <<Leave>>
5009 -- Initialize the refined state of a variable here because this is a
5010 -- common destination for legal and illegal object declarations.
5011
5012 if Ekind (Id) = E_Variable then
5013 Set_Encapsulating_State (Id, Empty);
5014 end if;
5015
5016 if Has_Aspects (N) then
5017 Analyze_Aspect_Specifications (N, Id);
5018 end if;
5019
5020 Analyze_Dimension (N);
5021
5022 -- Verify whether the object declaration introduces an illegal hidden
5023 -- state within a package subject to a null abstract state.
5024
5025 if Ekind (Id) = E_Variable then
5026 Check_No_Hidden_State (Id);
5027 end if;
5028
5029 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5030 end Analyze_Object_Declaration;
5031
5032 ---------------------------
5033 -- Analyze_Others_Choice --
5034 ---------------------------
5035
5036 -- Nothing to do for the others choice node itself, the semantic analysis
5037 -- of the others choice will occur as part of the processing of the parent
5038
5039 procedure Analyze_Others_Choice (N : Node_Id) is
5040 pragma Warnings (Off, N);
5041 begin
5042 null;
5043 end Analyze_Others_Choice;
5044
5045 -------------------------------------------
5046 -- Analyze_Private_Extension_Declaration --
5047 -------------------------------------------
5048
5049 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5050 Indic : constant Node_Id := Subtype_Indication (N);
5051 T : constant Entity_Id := Defining_Identifier (N);
5052 Iface : Entity_Id;
5053 Iface_Elmt : Elmt_Id;
5054 Parent_Base : Entity_Id;
5055 Parent_Type : Entity_Id;
5056
5057 begin
5058 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5059
5060 if Is_Non_Empty_List (Interface_List (N)) then
5061 declare
5062 Intf : Node_Id;
5063 T : Entity_Id;
5064
5065 begin
5066 Intf := First (Interface_List (N));
5067 while Present (Intf) loop
5068 T := Find_Type_Of_Subtype_Indic (Intf);
5069
5070 Diagnose_Interface (Intf, T);
5071 Next (Intf);
5072 end loop;
5073 end;
5074 end if;
5075
5076 Generate_Definition (T);
5077
5078 -- For other than Ada 2012, just enter the name in the current scope
5079
5080 if Ada_Version < Ada_2012 then
5081 Enter_Name (T);
5082
5083 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5084 -- case of private type that completes an incomplete type.
5085
5086 else
5087 declare
5088 Prev : Entity_Id;
5089
5090 begin
5091 Prev := Find_Type_Name (N);
5092
5093 pragma Assert (Prev = T
5094 or else (Ekind (Prev) = E_Incomplete_Type
5095 and then Present (Full_View (Prev))
5096 and then Full_View (Prev) = T));
5097 end;
5098 end if;
5099
5100 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5101 Parent_Base := Base_Type (Parent_Type);
5102
5103 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5104 Set_Ekind (T, Ekind (Parent_Type));
5105 Set_Etype (T, Any_Type);
5106 goto Leave;
5107
5108 elsif not Is_Tagged_Type (Parent_Type) then
5109 Error_Msg_N
5110 ("parent of type extension must be a tagged type ", Indic);
5111 goto Leave;
5112
5113 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5114 Error_Msg_N ("premature derivation of incomplete type", Indic);
5115 goto Leave;
5116
5117 elsif Is_Concurrent_Type (Parent_Type) then
5118 Error_Msg_N
5119 ("parent type of a private extension cannot be a synchronized "
5120 & "tagged type (RM 3.9.1 (3/1))", N);
5121
5122 Set_Etype (T, Any_Type);
5123 Set_Ekind (T, E_Limited_Private_Type);
5124 Set_Private_Dependents (T, New_Elmt_List);
5125 Set_Error_Posted (T);
5126 goto Leave;
5127 end if;
5128
5129 -- Perhaps the parent type should be changed to the class-wide type's
5130 -- specific type in this case to prevent cascading errors ???
5131
5132 if Is_Class_Wide_Type (Parent_Type) then
5133 Error_Msg_N
5134 ("parent of type extension must not be a class-wide type", Indic);
5135 goto Leave;
5136 end if;
5137
5138 if (not Is_Package_Or_Generic_Package (Current_Scope)
5139 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5140 or else In_Private_Part (Current_Scope)
5141 then
5142 Error_Msg_N ("invalid context for private extension", N);
5143 end if;
5144
5145 -- Set common attributes
5146
5147 Set_Is_Pure (T, Is_Pure (Current_Scope));
5148 Set_Scope (T, Current_Scope);
5149 Set_Ekind (T, E_Record_Type_With_Private);
5150 Init_Size_Align (T);
5151 Set_Default_SSO (T);
5152 Set_No_Reordering (T, No_Component_Reordering);
5153
5154 Set_Etype (T, Parent_Base);
5155 Propagate_Concurrent_Flags (T, Parent_Base);
5156
5157 Set_Convention (T, Convention (Parent_Type));
5158 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5159 Set_Is_First_Subtype (T);
5160 Make_Class_Wide_Type (T);
5161
5162 -- Set the SPARK mode from the current context
5163
5164 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5165 Set_SPARK_Pragma_Inherited (T);
5166
5167 if Unknown_Discriminants_Present (N) then
5168 Set_Discriminant_Constraint (T, No_Elist);
5169 end if;
5170
5171 Build_Derived_Record_Type (N, Parent_Type, T);
5172
5173 -- A private extension inherits the Default_Initial_Condition pragma
5174 -- coming from any parent type within the derivation chain.
5175
5176 if Has_DIC (Parent_Type) then
5177 Set_Has_Inherited_DIC (T);
5178 end if;
5179
5180 -- A private extension inherits any class-wide invariants coming from a
5181 -- parent type or an interface. Note that the invariant procedure of the
5182 -- parent type should not be inherited because the private extension may
5183 -- define invariants of its own.
5184
5185 if Has_Inherited_Invariants (Parent_Type)
5186 or else Has_Inheritable_Invariants (Parent_Type)
5187 then
5188 Set_Has_Inherited_Invariants (T);
5189
5190 elsif Present (Interfaces (T)) then
5191 Iface_Elmt := First_Elmt (Interfaces (T));
5192 while Present (Iface_Elmt) loop
5193 Iface := Node (Iface_Elmt);
5194
5195 if Has_Inheritable_Invariants (Iface) then
5196 Set_Has_Inherited_Invariants (T);
5197 exit;
5198 end if;
5199
5200 Next_Elmt (Iface_Elmt);
5201 end loop;
5202 end if;
5203
5204 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5205 -- synchronized formal derived type.
5206
5207 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5208 Set_Is_Limited_Record (T);
5209
5210 -- Formal derived type case
5211
5212 if Is_Generic_Type (T) then
5213
5214 -- The parent must be a tagged limited type or a synchronized
5215 -- interface.
5216
5217 if (not Is_Tagged_Type (Parent_Type)
5218 or else not Is_Limited_Type (Parent_Type))
5219 and then
5220 (not Is_Interface (Parent_Type)
5221 or else not Is_Synchronized_Interface (Parent_Type))
5222 then
5223 Error_Msg_NE
5224 ("parent type of & must be tagged limited or synchronized",
5225 N, T);
5226 end if;
5227
5228 -- The progenitors (if any) must be limited or synchronized
5229 -- interfaces.
5230
5231 if Present (Interfaces (T)) then
5232 Iface_Elmt := First_Elmt (Interfaces (T));
5233 while Present (Iface_Elmt) loop
5234 Iface := Node (Iface_Elmt);
5235
5236 if not Is_Limited_Interface (Iface)
5237 and then not Is_Synchronized_Interface (Iface)
5238 then
5239 Error_Msg_NE
5240 ("progenitor & must be limited or synchronized",
5241 N, Iface);
5242 end if;
5243
5244 Next_Elmt (Iface_Elmt);
5245 end loop;
5246 end if;
5247
5248 -- Regular derived extension, the parent must be a limited or
5249 -- synchronized interface.
5250
5251 else
5252 if not Is_Interface (Parent_Type)
5253 or else (not Is_Limited_Interface (Parent_Type)
5254 and then not Is_Synchronized_Interface (Parent_Type))
5255 then
5256 Error_Msg_NE
5257 ("parent type of & must be limited interface", N, T);
5258 end if;
5259 end if;
5260
5261 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5262 -- extension with a synchronized parent must be explicitly declared
5263 -- synchronized, because the full view will be a synchronized type.
5264 -- This must be checked before the check for limited types below,
5265 -- to ensure that types declared limited are not allowed to extend
5266 -- synchronized interfaces.
5267
5268 elsif Is_Interface (Parent_Type)
5269 and then Is_Synchronized_Interface (Parent_Type)
5270 and then not Synchronized_Present (N)
5271 then
5272 Error_Msg_NE
5273 ("private extension of& must be explicitly synchronized",
5274 N, Parent_Type);
5275
5276 elsif Limited_Present (N) then
5277 Set_Is_Limited_Record (T);
5278
5279 if not Is_Limited_Type (Parent_Type)
5280 and then
5281 (not Is_Interface (Parent_Type)
5282 or else not Is_Limited_Interface (Parent_Type))
5283 then
5284 Error_Msg_NE ("parent type& of limited extension must be limited",
5285 N, Parent_Type);
5286 end if;
5287 end if;
5288
5289 -- Remember that its parent type has a private extension. Used to warn
5290 -- on public primitives of the parent type defined after its private
5291 -- extensions (see Check_Dispatching_Operation).
5292
5293 Set_Has_Private_Extension (Parent_Type);
5294
5295 <<Leave>>
5296 if Has_Aspects (N) then
5297 Analyze_Aspect_Specifications (N, T);
5298 end if;
5299 end Analyze_Private_Extension_Declaration;
5300
5301 ---------------------------------
5302 -- Analyze_Subtype_Declaration --
5303 ---------------------------------
5304
5305 procedure Analyze_Subtype_Declaration
5306 (N : Node_Id;
5307 Skip : Boolean := False)
5308 is
5309 Id : constant Entity_Id := Defining_Identifier (N);
5310 R_Checks : Check_Result;
5311 T : Entity_Id;
5312
5313 begin
5314 Generate_Definition (Id);
5315 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5316 Init_Size_Align (Id);
5317
5318 -- The following guard condition on Enter_Name is to handle cases where
5319 -- the defining identifier has already been entered into the scope but
5320 -- the declaration as a whole needs to be analyzed.
5321
5322 -- This case in particular happens for derived enumeration types. The
5323 -- derived enumeration type is processed as an inserted enumeration type
5324 -- declaration followed by a rewritten subtype declaration. The defining
5325 -- identifier, however, is entered into the name scope very early in the
5326 -- processing of the original type declaration and therefore needs to be
5327 -- avoided here, when the created subtype declaration is analyzed. (See
5328 -- Build_Derived_Types)
5329
5330 -- This also happens when the full view of a private type is derived
5331 -- type with constraints. In this case the entity has been introduced
5332 -- in the private declaration.
5333
5334 -- Finally this happens in some complex cases when validity checks are
5335 -- enabled, where the same subtype declaration may be analyzed twice.
5336 -- This can happen if the subtype is created by the preanalysis of
5337 -- an attribute tht gives the range of a loop statement, and the loop
5338 -- itself appears within an if_statement that will be rewritten during
5339 -- expansion.
5340
5341 if Skip
5342 or else (Present (Etype (Id))
5343 and then (Is_Private_Type (Etype (Id))
5344 or else Is_Task_Type (Etype (Id))
5345 or else Is_Rewrite_Substitution (N)))
5346 then
5347 null;
5348
5349 elsif Current_Entity (Id) = Id then
5350 null;
5351
5352 else
5353 Enter_Name (Id);
5354 end if;
5355
5356 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5357
5358 -- Class-wide equivalent types of records with unknown discriminants
5359 -- involve the generation of an itype which serves as the private view
5360 -- of a constrained record subtype. In such cases the base type of the
5361 -- current subtype we are processing is the private itype. Use the full
5362 -- of the private itype when decorating various attributes.
5363
5364 if Is_Itype (T)
5365 and then Is_Private_Type (T)
5366 and then Present (Full_View (T))
5367 then
5368 T := Full_View (T);
5369 end if;
5370
5371 -- Inherit common attributes
5372
5373 Set_Is_Volatile (Id, Is_Volatile (T));
5374 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5375 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5376 Set_Convention (Id, Convention (T));
5377
5378 -- If ancestor has predicates then so does the subtype, and in addition
5379 -- we must delay the freeze to properly arrange predicate inheritance.
5380
5381 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5382 -- in which T = ID, so the above tests and assignments do nothing???
5383
5384 if Has_Predicates (T)
5385 or else (Present (Ancestor_Subtype (T))
5386 and then Has_Predicates (Ancestor_Subtype (T)))
5387 then
5388 Set_Has_Predicates (Id);
5389 Set_Has_Delayed_Freeze (Id);
5390
5391 -- Generated subtypes inherit the predicate function from the parent
5392 -- (no aspects to examine on the generated declaration).
5393
5394 if not Comes_From_Source (N) then
5395 Set_Ekind (Id, Ekind (T));
5396
5397 if Present (Predicate_Function (Id)) then
5398 null;
5399
5400 elsif Present (Predicate_Function (T)) then
5401 Set_Predicate_Function (Id, Predicate_Function (T));
5402
5403 elsif Present (Ancestor_Subtype (T))
5404 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5405 then
5406 Set_Predicate_Function (Id,
5407 Predicate_Function (Ancestor_Subtype (T)));
5408 end if;
5409 end if;
5410 end if;
5411
5412 -- Subtype of Boolean cannot have a constraint in SPARK
5413
5414 if Is_Boolean_Type (T)
5415 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5416 then
5417 Check_SPARK_05_Restriction
5418 ("subtype of Boolean cannot have constraint", N);
5419 end if;
5420
5421 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5422 declare
5423 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5424 One_Cstr : Node_Id;
5425 Low : Node_Id;
5426 High : Node_Id;
5427
5428 begin
5429 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5430 One_Cstr := First (Constraints (Cstr));
5431 while Present (One_Cstr) loop
5432
5433 -- Index or discriminant constraint in SPARK must be a
5434 -- subtype mark.
5435
5436 if not
5437 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5438 then
5439 Check_SPARK_05_Restriction
5440 ("subtype mark required", One_Cstr);
5441
5442 -- String subtype must have a lower bound of 1 in SPARK.
5443 -- Note that we do not need to test for the nonstatic case
5444 -- here, since that was already taken care of in
5445 -- Process_Range_Expr_In_Decl.
5446
5447 elsif Base_Type (T) = Standard_String then
5448 Get_Index_Bounds (One_Cstr, Low, High);
5449
5450 if Is_OK_Static_Expression (Low)
5451 and then Expr_Value (Low) /= 1
5452 then
5453 Check_SPARK_05_Restriction
5454 ("String subtype must have lower bound of 1", N);
5455 end if;
5456 end if;
5457
5458 Next (One_Cstr);
5459 end loop;
5460 end if;
5461 end;
5462 end if;
5463
5464 -- In the case where there is no constraint given in the subtype
5465 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5466 -- semantic attributes must be established here.
5467
5468 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5469 Set_Etype (Id, Base_Type (T));
5470
5471 -- Subtype of unconstrained array without constraint is not allowed
5472 -- in SPARK.
5473
5474 if Is_Array_Type (T) and then not Is_Constrained (T) then
5475 Check_SPARK_05_Restriction
5476 ("subtype of unconstrained array must have constraint", N);
5477 end if;
5478
5479 case Ekind (T) is
5480 when Array_Kind =>
5481 Set_Ekind (Id, E_Array_Subtype);
5482 Copy_Array_Subtype_Attributes (Id, T);
5483
5484 when Decimal_Fixed_Point_Kind =>
5485 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5486 Set_Digits_Value (Id, Digits_Value (T));
5487 Set_Delta_Value (Id, Delta_Value (T));
5488 Set_Scale_Value (Id, Scale_Value (T));
5489 Set_Small_Value (Id, Small_Value (T));
5490 Set_Scalar_Range (Id, Scalar_Range (T));
5491 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5492 Set_Is_Constrained (Id, Is_Constrained (T));
5493 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5494 Set_RM_Size (Id, RM_Size (T));
5495
5496 when Enumeration_Kind =>
5497 Set_Ekind (Id, E_Enumeration_Subtype);
5498 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5499 Set_Scalar_Range (Id, Scalar_Range (T));
5500 Set_Is_Character_Type (Id, Is_Character_Type (T));
5501 Set_Is_Constrained (Id, Is_Constrained (T));
5502 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5503 Set_RM_Size (Id, RM_Size (T));
5504
5505 when Ordinary_Fixed_Point_Kind =>
5506 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5507 Set_Scalar_Range (Id, Scalar_Range (T));
5508 Set_Small_Value (Id, Small_Value (T));
5509 Set_Delta_Value (Id, Delta_Value (T));
5510 Set_Is_Constrained (Id, Is_Constrained (T));
5511 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5512 Set_RM_Size (Id, RM_Size (T));
5513
5514 when Float_Kind =>
5515 Set_Ekind (Id, E_Floating_Point_Subtype);
5516 Set_Scalar_Range (Id, Scalar_Range (T));
5517 Set_Digits_Value (Id, Digits_Value (T));
5518 Set_Is_Constrained (Id, Is_Constrained (T));
5519
5520 -- If the floating point type has dimensions, these will be
5521 -- inherited subsequently when Analyze_Dimensions is called.
5522
5523 when Signed_Integer_Kind =>
5524 Set_Ekind (Id, E_Signed_Integer_Subtype);
5525 Set_Scalar_Range (Id, Scalar_Range (T));
5526 Set_Is_Constrained (Id, Is_Constrained (T));
5527 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5528 Set_RM_Size (Id, RM_Size (T));
5529
5530 when Modular_Integer_Kind =>
5531 Set_Ekind (Id, E_Modular_Integer_Subtype);
5532 Set_Scalar_Range (Id, Scalar_Range (T));
5533 Set_Is_Constrained (Id, Is_Constrained (T));
5534 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5535 Set_RM_Size (Id, RM_Size (T));
5536
5537 when Class_Wide_Kind =>
5538 Set_Ekind (Id, E_Class_Wide_Subtype);
5539 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5540 Set_Cloned_Subtype (Id, T);
5541 Set_Is_Tagged_Type (Id, True);
5542 Set_Has_Unknown_Discriminants
5543 (Id, True);
5544 Set_No_Tagged_Streams_Pragma
5545 (Id, No_Tagged_Streams_Pragma (T));
5546
5547 if Ekind (T) = E_Class_Wide_Subtype then
5548 Set_Equivalent_Type (Id, Equivalent_Type (T));
5549 end if;
5550
5551 when E_Record_Subtype
5552 | E_Record_Type
5553 =>
5554 Set_Ekind (Id, E_Record_Subtype);
5555
5556 -- Subtype declarations introduced for formal type parameters
5557 -- in generic instantiations should inherit the Size value of
5558 -- the type they rename.
5559
5560 if Present (Generic_Parent_Type (N)) then
5561 Set_RM_Size (Id, RM_Size (T));
5562 end if;
5563
5564 if Ekind (T) = E_Record_Subtype
5565 and then Present (Cloned_Subtype (T))
5566 then
5567 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5568 else
5569 Set_Cloned_Subtype (Id, T);
5570 end if;
5571
5572 Set_First_Entity (Id, First_Entity (T));
5573 Set_Last_Entity (Id, Last_Entity (T));
5574 Set_Has_Discriminants (Id, Has_Discriminants (T));
5575 Set_Is_Constrained (Id, Is_Constrained (T));
5576 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5577 Set_Has_Implicit_Dereference
5578 (Id, Has_Implicit_Dereference (T));
5579 Set_Has_Unknown_Discriminants
5580 (Id, Has_Unknown_Discriminants (T));
5581
5582 if Has_Discriminants (T) then
5583 Set_Discriminant_Constraint
5584 (Id, Discriminant_Constraint (T));
5585 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5586
5587 elsif Has_Unknown_Discriminants (Id) then
5588 Set_Discriminant_Constraint (Id, No_Elist);
5589 end if;
5590
5591 if Is_Tagged_Type (T) then
5592 Set_Is_Tagged_Type (Id, True);
5593 Set_No_Tagged_Streams_Pragma
5594 (Id, No_Tagged_Streams_Pragma (T));
5595 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5596 Set_Direct_Primitive_Operations
5597 (Id, Direct_Primitive_Operations (T));
5598 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5599
5600 if Is_Interface (T) then
5601 Set_Is_Interface (Id);
5602 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5603 end if;
5604 end if;
5605
5606 when Private_Kind =>
5607 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5608 Set_Has_Discriminants (Id, Has_Discriminants (T));
5609 Set_Is_Constrained (Id, Is_Constrained (T));
5610 Set_First_Entity (Id, First_Entity (T));
5611 Set_Last_Entity (Id, Last_Entity (T));
5612 Set_Private_Dependents (Id, New_Elmt_List);
5613 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5614 Set_Has_Implicit_Dereference
5615 (Id, Has_Implicit_Dereference (T));
5616 Set_Has_Unknown_Discriminants
5617 (Id, Has_Unknown_Discriminants (T));
5618 Set_Known_To_Have_Preelab_Init
5619 (Id, Known_To_Have_Preelab_Init (T));
5620
5621 if Is_Tagged_Type (T) then
5622 Set_Is_Tagged_Type (Id);
5623 Set_No_Tagged_Streams_Pragma (Id,
5624 No_Tagged_Streams_Pragma (T));
5625 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5626 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5627 Set_Direct_Primitive_Operations (Id,
5628 Direct_Primitive_Operations (T));
5629 end if;
5630
5631 -- In general the attributes of the subtype of a private type
5632 -- are the attributes of the partial view of parent. However,
5633 -- the full view may be a discriminated type, and the subtype
5634 -- must share the discriminant constraint to generate correct
5635 -- calls to initialization procedures.
5636
5637 if Has_Discriminants (T) then
5638 Set_Discriminant_Constraint
5639 (Id, Discriminant_Constraint (T));
5640 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5641
5642 elsif Present (Full_View (T))
5643 and then Has_Discriminants (Full_View (T))
5644 then
5645 Set_Discriminant_Constraint
5646 (Id, Discriminant_Constraint (Full_View (T)));
5647 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5648
5649 -- This would seem semantically correct, but apparently
5650 -- generates spurious errors about missing components ???
5651
5652 -- Set_Has_Discriminants (Id);
5653 end if;
5654
5655 Prepare_Private_Subtype_Completion (Id, N);
5656
5657 -- If this is the subtype of a constrained private type with
5658 -- discriminants that has got a full view and we also have
5659 -- built a completion just above, show that the completion
5660 -- is a clone of the full view to the back-end.
5661
5662 if Has_Discriminants (T)
5663 and then not Has_Unknown_Discriminants (T)
5664 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5665 and then Present (Full_View (T))
5666 and then Present (Full_View (Id))
5667 then
5668 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5669 end if;
5670
5671 when Access_Kind =>
5672 Set_Ekind (Id, E_Access_Subtype);
5673 Set_Is_Constrained (Id, Is_Constrained (T));
5674 Set_Is_Access_Constant
5675 (Id, Is_Access_Constant (T));
5676 Set_Directly_Designated_Type
5677 (Id, Designated_Type (T));
5678 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5679
5680 -- A Pure library_item must not contain the declaration of a
5681 -- named access type, except within a subprogram, generic
5682 -- subprogram, task unit, or protected unit, or if it has
5683 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5684
5685 if Comes_From_Source (Id)
5686 and then In_Pure_Unit
5687 and then not In_Subprogram_Task_Protected_Unit
5688 and then not No_Pool_Assigned (Id)
5689 then
5690 Error_Msg_N
5691 ("named access types not allowed in pure unit", N);
5692 end if;
5693
5694 when Concurrent_Kind =>
5695 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5696 Set_Corresponding_Record_Type (Id,
5697 Corresponding_Record_Type (T));
5698 Set_First_Entity (Id, First_Entity (T));
5699 Set_First_Private_Entity (Id, First_Private_Entity (T));
5700 Set_Has_Discriminants (Id, Has_Discriminants (T));
5701 Set_Is_Constrained (Id, Is_Constrained (T));
5702 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5703 Set_Last_Entity (Id, Last_Entity (T));
5704
5705 if Is_Tagged_Type (T) then
5706 Set_No_Tagged_Streams_Pragma
5707 (Id, No_Tagged_Streams_Pragma (T));
5708 end if;
5709
5710 if Has_Discriminants (T) then
5711 Set_Discriminant_Constraint
5712 (Id, Discriminant_Constraint (T));
5713 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5714 end if;
5715
5716 when Incomplete_Kind =>
5717 if Ada_Version >= Ada_2005 then
5718
5719 -- In Ada 2005 an incomplete type can be explicitly tagged:
5720 -- propagate indication. Note that we also have to include
5721 -- subtypes for Ada 2012 extended use of incomplete types.
5722
5723 Set_Ekind (Id, E_Incomplete_Subtype);
5724 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5725 Set_Private_Dependents (Id, New_Elmt_List);
5726
5727 if Is_Tagged_Type (Id) then
5728 Set_No_Tagged_Streams_Pragma
5729 (Id, No_Tagged_Streams_Pragma (T));
5730 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5731 end if;
5732
5733 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5734 -- incomplete type visible through a limited with clause.
5735
5736 if From_Limited_With (T)
5737 and then Present (Non_Limited_View (T))
5738 then
5739 Set_From_Limited_With (Id);
5740 Set_Non_Limited_View (Id, Non_Limited_View (T));
5741
5742 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5743 -- to the private dependents of the original incomplete
5744 -- type for future transformation.
5745
5746 else
5747 Append_Elmt (Id, Private_Dependents (T));
5748 end if;
5749
5750 -- If the subtype name denotes an incomplete type an error
5751 -- was already reported by Process_Subtype.
5752
5753 else
5754 Set_Etype (Id, Any_Type);
5755 end if;
5756
5757 when others =>
5758 raise Program_Error;
5759 end case;
5760
5761 -- If there is no constraint in the subtype indication, the
5762 -- declared entity inherits predicates from the parent.
5763
5764 Inherit_Predicate_Flags (Id, T);
5765 end if;
5766
5767 if Etype (Id) = Any_Type then
5768 goto Leave;
5769 end if;
5770
5771 -- Some common processing on all types
5772
5773 Set_Size_Info (Id, T);
5774 Set_First_Rep_Item (Id, First_Rep_Item (T));
5775
5776 -- If the parent type is a generic actual, so is the subtype. This may
5777 -- happen in a nested instance. Why Comes_From_Source test???
5778
5779 if not Comes_From_Source (N) then
5780 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5781 end if;
5782
5783 -- If this is a subtype declaration for an actual in an instance,
5784 -- inherit static and dynamic predicates if any.
5785
5786 -- If declaration has no aspect specifications, inherit predicate
5787 -- info as well. Unclear how to handle the case of both specified
5788 -- and inherited predicates ??? Other inherited aspects, such as
5789 -- invariants, should be OK, but the combination with later pragmas
5790 -- may also require special merging.
5791
5792 if Has_Predicates (T)
5793 and then Present (Predicate_Function (T))
5794 and then
5795 ((In_Instance and then not Comes_From_Source (N))
5796 or else No (Aspect_Specifications (N)))
5797 then
5798 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5799
5800 if Has_Static_Predicate (T) then
5801 Set_Has_Static_Predicate (Id);
5802 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5803 end if;
5804 end if;
5805
5806 -- Remaining processing depends on characteristics of base type
5807
5808 T := Etype (Id);
5809
5810 Set_Is_Immediately_Visible (Id, True);
5811 Set_Depends_On_Private (Id, Has_Private_Component (T));
5812 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5813
5814 if Is_Interface (T) then
5815 Set_Is_Interface (Id);
5816 end if;
5817
5818 if Present (Generic_Parent_Type (N))
5819 and then
5820 (Nkind (Parent (Generic_Parent_Type (N))) /=
5821 N_Formal_Type_Declaration
5822 or else Nkind (Formal_Type_Definition
5823 (Parent (Generic_Parent_Type (N)))) /=
5824 N_Formal_Private_Type_Definition)
5825 then
5826 if Is_Tagged_Type (Id) then
5827
5828 -- If this is a generic actual subtype for a synchronized type,
5829 -- the primitive operations are those of the corresponding record
5830 -- for which there is a separate subtype declaration.
5831
5832 if Is_Concurrent_Type (Id) then
5833 null;
5834 elsif Is_Class_Wide_Type (Id) then
5835 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5836 else
5837 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5838 end if;
5839
5840 elsif Scope (Etype (Id)) /= Standard_Standard then
5841 Derive_Subprograms (Generic_Parent_Type (N), Id);
5842 end if;
5843 end if;
5844
5845 if Is_Private_Type (T) and then Present (Full_View (T)) then
5846 Conditional_Delay (Id, Full_View (T));
5847
5848 -- The subtypes of components or subcomponents of protected types
5849 -- do not need freeze nodes, which would otherwise appear in the
5850 -- wrong scope (before the freeze node for the protected type). The
5851 -- proper subtypes are those of the subcomponents of the corresponding
5852 -- record.
5853
5854 elsif Ekind (Scope (Id)) /= E_Protected_Type
5855 and then Present (Scope (Scope (Id))) -- error defense
5856 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5857 then
5858 Conditional_Delay (Id, T);
5859 end if;
5860
5861 -- If we have a subtype of an incomplete type whose full type is a
5862 -- derived numeric type, we need to have a freeze node for the subtype.
5863 -- Otherwise gigi will complain while computing the (static) bounds of
5864 -- the subtype.
5865
5866 if Is_Itype (T)
5867 and then Is_Elementary_Type (Id)
5868 and then Etype (Id) /= Id
5869 then
5870 declare
5871 Partial : constant Entity_Id :=
5872 Incomplete_Or_Partial_View (First_Subtype (Id));
5873 begin
5874 if Present (Partial)
5875 and then Ekind (Partial) = E_Incomplete_Type
5876 then
5877 Set_Has_Delayed_Freeze (Id);
5878 end if;
5879 end;
5880 end if;
5881
5882 -- Check that Constraint_Error is raised for a scalar subtype indication
5883 -- when the lower or upper bound of a non-null range lies outside the
5884 -- range of the type mark.
5885
5886 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5887 if Is_Scalar_Type (Etype (Id))
5888 and then Scalar_Range (Id) /=
5889 Scalar_Range
5890 (Etype (Subtype_Mark (Subtype_Indication (N))))
5891 then
5892 Apply_Range_Check
5893 (Scalar_Range (Id),
5894 Etype (Subtype_Mark (Subtype_Indication (N))));
5895
5896 -- In the array case, check compatibility for each index
5897
5898 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5899 then
5900 -- This really should be a subprogram that finds the indications
5901 -- to check???
5902
5903 declare
5904 Subt_Index : Node_Id := First_Index (Id);
5905 Target_Index : Node_Id :=
5906 First_Index (Etype
5907 (Subtype_Mark (Subtype_Indication (N))));
5908 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5909
5910 begin
5911 while Present (Subt_Index) loop
5912 if ((Nkind (Subt_Index) = N_Identifier
5913 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5914 or else Nkind (Subt_Index) = N_Subtype_Indication)
5915 and then
5916 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5917 then
5918 declare
5919 Target_Typ : constant Entity_Id :=
5920 Etype (Target_Index);
5921 begin
5922 R_Checks :=
5923 Get_Range_Checks
5924 (Scalar_Range (Etype (Subt_Index)),
5925 Target_Typ,
5926 Etype (Subt_Index),
5927 Defining_Identifier (N));
5928
5929 -- Reset Has_Dynamic_Range_Check on the subtype to
5930 -- prevent elision of the index check due to a dynamic
5931 -- check generated for a preceding index (needed since
5932 -- Insert_Range_Checks tries to avoid generating
5933 -- redundant checks on a given declaration).
5934
5935 Set_Has_Dynamic_Range_Check (N, False);
5936
5937 Insert_Range_Checks
5938 (R_Checks,
5939 N,
5940 Target_Typ,
5941 Sloc (Defining_Identifier (N)));
5942
5943 -- Record whether this index involved a dynamic check
5944
5945 Has_Dyn_Chk :=
5946 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5947 end;
5948 end if;
5949
5950 Next_Index (Subt_Index);
5951 Next_Index (Target_Index);
5952 end loop;
5953
5954 -- Finally, mark whether the subtype involves dynamic checks
5955
5956 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5957 end;
5958 end if;
5959 end if;
5960
5961 Set_Optimize_Alignment_Flags (Id);
5962 Check_Eliminated (Id);
5963
5964 <<Leave>>
5965 if Has_Aspects (N) then
5966 Analyze_Aspect_Specifications (N, Id);
5967 end if;
5968
5969 Analyze_Dimension (N);
5970
5971 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5972 -- indications on composite types where the constraints are dynamic.
5973 -- Note that object declarations and aggregates generate implicit
5974 -- subtype declarations, which this covers. One special case is that the
5975 -- implicitly generated "=" for discriminated types includes an
5976 -- offending subtype declaration, which is harmless, so we ignore it
5977 -- here.
5978
5979 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5980 declare
5981 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5982 begin
5983 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5984 and then not (Is_Internal (Id)
5985 and then Is_TSS (Scope (Id),
5986 TSS_Composite_Equality))
5987 and then not Within_Init_Proc
5988 and then not All_Composite_Constraints_Static (Cstr)
5989 then
5990 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5991 end if;
5992 end;
5993 end if;
5994 end Analyze_Subtype_Declaration;
5995
5996 --------------------------------
5997 -- Analyze_Subtype_Indication --
5998 --------------------------------
5999
6000 procedure Analyze_Subtype_Indication (N : Node_Id) is
6001 T : constant Entity_Id := Subtype_Mark (N);
6002 R : constant Node_Id := Range_Expression (Constraint (N));
6003
6004 begin
6005 Analyze (T);
6006
6007 if R /= Error then
6008 Analyze (R);
6009 Set_Etype (N, Etype (R));
6010 Resolve (R, Entity (T));
6011 else
6012 Set_Error_Posted (R);
6013 Set_Error_Posted (T);
6014 end if;
6015 end Analyze_Subtype_Indication;
6016
6017 --------------------------
6018 -- Analyze_Variant_Part --
6019 --------------------------
6020
6021 procedure Analyze_Variant_Part (N : Node_Id) is
6022 Discr_Name : Node_Id;
6023 Discr_Type : Entity_Id;
6024
6025 procedure Process_Variant (A : Node_Id);
6026 -- Analyze declarations for a single variant
6027
6028 package Analyze_Variant_Choices is
6029 new Generic_Analyze_Choices (Process_Variant);
6030 use Analyze_Variant_Choices;
6031
6032 ---------------------
6033 -- Process_Variant --
6034 ---------------------
6035
6036 procedure Process_Variant (A : Node_Id) is
6037 CL : constant Node_Id := Component_List (A);
6038 begin
6039 if not Null_Present (CL) then
6040 Analyze_Declarations (Component_Items (CL));
6041
6042 if Present (Variant_Part (CL)) then
6043 Analyze (Variant_Part (CL));
6044 end if;
6045 end if;
6046 end Process_Variant;
6047
6048 -- Start of processing for Analyze_Variant_Part
6049
6050 begin
6051 Discr_Name := Name (N);
6052 Analyze (Discr_Name);
6053
6054 -- If Discr_Name bad, get out (prevent cascaded errors)
6055
6056 if Etype (Discr_Name) = Any_Type then
6057 return;
6058 end if;
6059
6060 -- Check invalid discriminant in variant part
6061
6062 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6063 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6064 end if;
6065
6066 Discr_Type := Etype (Entity (Discr_Name));
6067
6068 if not Is_Discrete_Type (Discr_Type) then
6069 Error_Msg_N
6070 ("discriminant in a variant part must be of a discrete type",
6071 Name (N));
6072 return;
6073 end if;
6074
6075 -- Now analyze the choices, which also analyzes the declarations that
6076 -- are associated with each choice.
6077
6078 Analyze_Choices (Variants (N), Discr_Type);
6079
6080 -- Note: we used to instantiate and call Check_Choices here to check
6081 -- that the choices covered the discriminant, but it's too early to do
6082 -- that because of statically predicated subtypes, whose analysis may
6083 -- be deferred to their freeze point which may be as late as the freeze
6084 -- point of the containing record. So this call is now to be found in
6085 -- Freeze_Record_Declaration.
6086
6087 end Analyze_Variant_Part;
6088
6089 ----------------------------
6090 -- Array_Type_Declaration --
6091 ----------------------------
6092
6093 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6094 Component_Def : constant Node_Id := Component_Definition (Def);
6095 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6096 P : constant Node_Id := Parent (Def);
6097 Element_Type : Entity_Id;
6098 Implicit_Base : Entity_Id;
6099 Index : Node_Id;
6100 Nb_Index : Nat;
6101 Priv : Entity_Id;
6102 Related_Id : Entity_Id := Empty;
6103
6104 begin
6105 if Nkind (Def) = N_Constrained_Array_Definition then
6106 Index := First (Discrete_Subtype_Definitions (Def));
6107 else
6108 Index := First (Subtype_Marks (Def));
6109 end if;
6110
6111 -- Find proper names for the implicit types which may be public. In case
6112 -- of anonymous arrays we use the name of the first object of that type
6113 -- as prefix.
6114
6115 if No (T) then
6116 Related_Id := Defining_Identifier (P);
6117 else
6118 Related_Id := T;
6119 end if;
6120
6121 Nb_Index := 1;
6122 while Present (Index) loop
6123 Analyze (Index);
6124
6125 -- Test for odd case of trying to index a type by the type itself
6126
6127 if Is_Entity_Name (Index) and then Entity (Index) = T then
6128 Error_Msg_N ("type& cannot be indexed by itself", Index);
6129 Set_Entity (Index, Standard_Boolean);
6130 Set_Etype (Index, Standard_Boolean);
6131 end if;
6132
6133 -- Check SPARK restriction requiring a subtype mark
6134
6135 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6136 Check_SPARK_05_Restriction ("subtype mark required", Index);
6137 end if;
6138
6139 -- Add a subtype declaration for each index of private array type
6140 -- declaration whose etype is also private. For example:
6141
6142 -- package Pkg is
6143 -- type Index is private;
6144 -- private
6145 -- type Table is array (Index) of ...
6146 -- end;
6147
6148 -- This is currently required by the expander for the internally
6149 -- generated equality subprogram of records with variant parts in
6150 -- which the etype of some component is such private type.
6151
6152 if Ekind (Current_Scope) = E_Package
6153 and then In_Private_Part (Current_Scope)
6154 and then Has_Private_Declaration (Etype (Index))
6155 then
6156 declare
6157 Loc : constant Source_Ptr := Sloc (Def);
6158 Decl : Entity_Id;
6159 New_E : Entity_Id;
6160
6161 begin
6162 New_E := Make_Temporary (Loc, 'T');
6163 Set_Is_Internal (New_E);
6164
6165 Decl :=
6166 Make_Subtype_Declaration (Loc,
6167 Defining_Identifier => New_E,
6168 Subtype_Indication =>
6169 New_Occurrence_Of (Etype (Index), Loc));
6170
6171 Insert_Before (Parent (Def), Decl);
6172 Analyze (Decl);
6173 Set_Etype (Index, New_E);
6174
6175 -- If the index is a range or a subtype indication it carries
6176 -- no entity. Example:
6177
6178 -- package Pkg is
6179 -- type T is private;
6180 -- private
6181 -- type T is new Natural;
6182 -- Table : array (T(1) .. T(10)) of Boolean;
6183 -- end Pkg;
6184
6185 -- Otherwise the type of the reference is its entity.
6186
6187 if Is_Entity_Name (Index) then
6188 Set_Entity (Index, New_E);
6189 end if;
6190 end;
6191 end if;
6192
6193 Make_Index (Index, P, Related_Id, Nb_Index);
6194
6195 -- Check error of subtype with predicate for index type
6196
6197 Bad_Predicated_Subtype_Use
6198 ("subtype& has predicate, not allowed as index subtype",
6199 Index, Etype (Index));
6200
6201 -- Move to next index
6202
6203 Next_Index (Index);
6204 Nb_Index := Nb_Index + 1;
6205 end loop;
6206
6207 -- Process subtype indication if one is present
6208
6209 if Present (Component_Typ) then
6210 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6211
6212 Set_Etype (Component_Typ, Element_Type);
6213
6214 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6215 Check_SPARK_05_Restriction
6216 ("subtype mark required", Component_Typ);
6217 end if;
6218
6219 -- Ada 2005 (AI-230): Access Definition case
6220
6221 else pragma Assert (Present (Access_Definition (Component_Def)));
6222
6223 -- Indicate that the anonymous access type is created by the
6224 -- array type declaration.
6225
6226 Element_Type := Access_Definition
6227 (Related_Nod => P,
6228 N => Access_Definition (Component_Def));
6229 Set_Is_Local_Anonymous_Access (Element_Type);
6230
6231 -- Propagate the parent. This field is needed if we have to generate
6232 -- the master_id associated with an anonymous access to task type
6233 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6234
6235 Set_Parent (Element_Type, Parent (T));
6236
6237 -- Ada 2005 (AI-230): In case of components that are anonymous access
6238 -- types the level of accessibility depends on the enclosing type
6239 -- declaration
6240
6241 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6242
6243 -- Ada 2005 (AI-254)
6244
6245 declare
6246 CD : constant Node_Id :=
6247 Access_To_Subprogram_Definition
6248 (Access_Definition (Component_Def));
6249 begin
6250 if Present (CD) and then Protected_Present (CD) then
6251 Element_Type :=
6252 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6253 end if;
6254 end;
6255 end if;
6256
6257 -- Constrained array case
6258
6259 if No (T) then
6260 T := Create_Itype (E_Void, P, Related_Id, 'T');
6261 end if;
6262
6263 if Nkind (Def) = N_Constrained_Array_Definition then
6264
6265 -- Establish Implicit_Base as unconstrained base type
6266
6267 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6268
6269 Set_Etype (Implicit_Base, Implicit_Base);
6270 Set_Scope (Implicit_Base, Current_Scope);
6271 Set_Has_Delayed_Freeze (Implicit_Base);
6272 Set_Default_SSO (Implicit_Base);
6273
6274 -- The constrained array type is a subtype of the unconstrained one
6275
6276 Set_Ekind (T, E_Array_Subtype);
6277 Init_Size_Align (T);
6278 Set_Etype (T, Implicit_Base);
6279 Set_Scope (T, Current_Scope);
6280 Set_Is_Constrained (T);
6281 Set_First_Index (T,
6282 First (Discrete_Subtype_Definitions (Def)));
6283 Set_Has_Delayed_Freeze (T);
6284
6285 -- Complete setup of implicit base type
6286
6287 Set_Component_Size (Implicit_Base, Uint_0);
6288 Set_Component_Type (Implicit_Base, Element_Type);
6289 Set_Finalize_Storage_Only
6290 (Implicit_Base,
6291 Finalize_Storage_Only (Element_Type));
6292 Set_First_Index (Implicit_Base, First_Index (T));
6293 Set_Has_Controlled_Component
6294 (Implicit_Base,
6295 Has_Controlled_Component (Element_Type)
6296 or else Is_Controlled (Element_Type));
6297 Set_Packed_Array_Impl_Type
6298 (Implicit_Base, Empty);
6299
6300 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6301
6302 -- Unconstrained array case
6303
6304 else
6305 Set_Ekind (T, E_Array_Type);
6306 Init_Size_Align (T);
6307 Set_Etype (T, T);
6308 Set_Scope (T, Current_Scope);
6309 Set_Component_Size (T, Uint_0);
6310 Set_Is_Constrained (T, False);
6311 Set_First_Index (T, First (Subtype_Marks (Def)));
6312 Set_Has_Delayed_Freeze (T, True);
6313 Propagate_Concurrent_Flags (T, Element_Type);
6314 Set_Has_Controlled_Component (T, Has_Controlled_Component
6315 (Element_Type)
6316 or else
6317 Is_Controlled (Element_Type));
6318 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6319 (Element_Type));
6320 Set_Default_SSO (T);
6321 end if;
6322
6323 -- Common attributes for both cases
6324
6325 Set_Component_Type (Base_Type (T), Element_Type);
6326 Set_Packed_Array_Impl_Type (T, Empty);
6327
6328 if Aliased_Present (Component_Definition (Def)) then
6329 Check_SPARK_05_Restriction
6330 ("aliased is not allowed", Component_Definition (Def));
6331 Set_Has_Aliased_Components (Etype (T));
6332
6333 -- AI12-001: All aliased objects are considered to be specified as
6334 -- independently addressable (RM C.6(8.1/4)).
6335
6336 Set_Has_Independent_Components (Etype (T));
6337 end if;
6338
6339 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6340 -- array type to ensure that objects of this type are initialized.
6341
6342 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6343 Set_Can_Never_Be_Null (T);
6344
6345 if Null_Exclusion_Present (Component_Definition (Def))
6346
6347 -- No need to check itypes because in their case this check was
6348 -- done at their point of creation
6349
6350 and then not Is_Itype (Element_Type)
6351 then
6352 Error_Msg_N
6353 ("`NOT NULL` not allowed (null already excluded)",
6354 Subtype_Indication (Component_Definition (Def)));
6355 end if;
6356 end if;
6357
6358 Priv := Private_Component (Element_Type);
6359
6360 if Present (Priv) then
6361
6362 -- Check for circular definitions
6363
6364 if Priv = Any_Type then
6365 Set_Component_Type (Etype (T), Any_Type);
6366
6367 -- There is a gap in the visibility of operations on the composite
6368 -- type only if the component type is defined in a different scope.
6369
6370 elsif Scope (Priv) = Current_Scope then
6371 null;
6372
6373 elsif Is_Limited_Type (Priv) then
6374 Set_Is_Limited_Composite (Etype (T));
6375 Set_Is_Limited_Composite (T);
6376 else
6377 Set_Is_Private_Composite (Etype (T));
6378 Set_Is_Private_Composite (T);
6379 end if;
6380 end if;
6381
6382 -- A syntax error in the declaration itself may lead to an empty index
6383 -- list, in which case do a minimal patch.
6384
6385 if No (First_Index (T)) then
6386 Error_Msg_N ("missing index definition in array type declaration", T);
6387
6388 declare
6389 Indexes : constant List_Id :=
6390 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6391 begin
6392 Set_Discrete_Subtype_Definitions (Def, Indexes);
6393 Set_First_Index (T, First (Indexes));
6394 return;
6395 end;
6396 end if;
6397
6398 -- Create a concatenation operator for the new type. Internal array
6399 -- types created for packed entities do not need such, they are
6400 -- compatible with the user-defined type.
6401
6402 if Number_Dimensions (T) = 1
6403 and then not Is_Packed_Array_Impl_Type (T)
6404 then
6405 New_Concatenation_Op (T);
6406 end if;
6407
6408 -- In the case of an unconstrained array the parser has already verified
6409 -- that all the indexes are unconstrained but we still need to make sure
6410 -- that the element type is constrained.
6411
6412 if not Is_Definite_Subtype (Element_Type) then
6413 Error_Msg_N
6414 ("unconstrained element type in array declaration",
6415 Subtype_Indication (Component_Def));
6416
6417 elsif Is_Abstract_Type (Element_Type) then
6418 Error_Msg_N
6419 ("the type of a component cannot be abstract",
6420 Subtype_Indication (Component_Def));
6421 end if;
6422
6423 -- There may be an invariant declared for the component type, but
6424 -- the construction of the component invariant checking procedure
6425 -- takes place during expansion.
6426 end Array_Type_Declaration;
6427
6428 ------------------------------------------------------
6429 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6430 ------------------------------------------------------
6431
6432 function Replace_Anonymous_Access_To_Protected_Subprogram
6433 (N : Node_Id) return Entity_Id
6434 is
6435 Loc : constant Source_Ptr := Sloc (N);
6436
6437 Curr_Scope : constant Scope_Stack_Entry :=
6438 Scope_Stack.Table (Scope_Stack.Last);
6439
6440 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6441
6442 Acc : Node_Id;
6443 -- Access definition in declaration
6444
6445 Comp : Node_Id;
6446 -- Object definition or formal definition with an access definition
6447
6448 Decl : Node_Id;
6449 -- Declaration of anonymous access to subprogram type
6450
6451 Spec : Node_Id;
6452 -- Original specification in access to subprogram
6453
6454 P : Node_Id;
6455
6456 begin
6457 Set_Is_Internal (Anon);
6458
6459 case Nkind (N) is
6460 when N_Constrained_Array_Definition
6461 | N_Component_Declaration
6462 | N_Unconstrained_Array_Definition
6463 =>
6464 Comp := Component_Definition (N);
6465 Acc := Access_Definition (Comp);
6466
6467 when N_Discriminant_Specification =>
6468 Comp := Discriminant_Type (N);
6469 Acc := Comp;
6470
6471 when N_Parameter_Specification =>
6472 Comp := Parameter_Type (N);
6473 Acc := Comp;
6474
6475 when N_Access_Function_Definition =>
6476 Comp := Result_Definition (N);
6477 Acc := Comp;
6478
6479 when N_Object_Declaration =>
6480 Comp := Object_Definition (N);
6481 Acc := Comp;
6482
6483 when N_Function_Specification =>
6484 Comp := Result_Definition (N);
6485 Acc := Comp;
6486
6487 when others =>
6488 raise Program_Error;
6489 end case;
6490
6491 Spec := Access_To_Subprogram_Definition (Acc);
6492
6493 Decl :=
6494 Make_Full_Type_Declaration (Loc,
6495 Defining_Identifier => Anon,
6496 Type_Definition => Copy_Separate_Tree (Spec));
6497
6498 Mark_Rewrite_Insertion (Decl);
6499
6500 -- Insert the new declaration in the nearest enclosing scope. If the
6501 -- parent is a body and N is its return type, the declaration belongs
6502 -- in the enclosing scope. Likewise if N is the type of a parameter.
6503
6504 P := Parent (N);
6505
6506 if Nkind (N) = N_Function_Specification
6507 and then Nkind (P) = N_Subprogram_Body
6508 then
6509 P := Parent (P);
6510 elsif Nkind (N) = N_Parameter_Specification
6511 and then Nkind (P) in N_Subprogram_Specification
6512 and then Nkind (Parent (P)) = N_Subprogram_Body
6513 then
6514 P := Parent (Parent (P));
6515 end if;
6516
6517 while Present (P) and then not Has_Declarations (P) loop
6518 P := Parent (P);
6519 end loop;
6520
6521 pragma Assert (Present (P));
6522
6523 if Nkind (P) = N_Package_Specification then
6524 Prepend (Decl, Visible_Declarations (P));
6525 else
6526 Prepend (Decl, Declarations (P));
6527 end if;
6528
6529 -- Replace the anonymous type with an occurrence of the new declaration.
6530 -- In all cases the rewritten node does not have the null-exclusion
6531 -- attribute because (if present) it was already inherited by the
6532 -- anonymous entity (Anon). Thus, in case of components we do not
6533 -- inherit this attribute.
6534
6535 if Nkind (N) = N_Parameter_Specification then
6536 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6537 Set_Etype (Defining_Identifier (N), Anon);
6538 Set_Null_Exclusion_Present (N, False);
6539
6540 elsif Nkind (N) = N_Object_Declaration then
6541 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6542 Set_Etype (Defining_Identifier (N), Anon);
6543
6544 elsif Nkind (N) = N_Access_Function_Definition then
6545 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6546
6547 elsif Nkind (N) = N_Function_Specification then
6548 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6549 Set_Etype (Defining_Unit_Name (N), Anon);
6550
6551 else
6552 Rewrite (Comp,
6553 Make_Component_Definition (Loc,
6554 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6555 end if;
6556
6557 Mark_Rewrite_Insertion (Comp);
6558
6559 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6560 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6561 and then not Is_Type (Current_Scope))
6562 then
6563
6564 -- Declaration can be analyzed in the current scope.
6565
6566 Analyze (Decl);
6567
6568 else
6569 -- Temporarily remove the current scope (record or subprogram) from
6570 -- the stack to add the new declarations to the enclosing scope.
6571 -- The anonymous entity is an Itype with the proper attributes.
6572
6573 Scope_Stack.Decrement_Last;
6574 Analyze (Decl);
6575 Set_Is_Itype (Anon);
6576 Set_Associated_Node_For_Itype (Anon, N);
6577 Scope_Stack.Append (Curr_Scope);
6578 end if;
6579
6580 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6581 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6582 return Anon;
6583 end Replace_Anonymous_Access_To_Protected_Subprogram;
6584
6585 -------------------------------
6586 -- Build_Derived_Access_Type --
6587 -------------------------------
6588
6589 procedure Build_Derived_Access_Type
6590 (N : Node_Id;
6591 Parent_Type : Entity_Id;
6592 Derived_Type : Entity_Id)
6593 is
6594 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6595
6596 Desig_Type : Entity_Id;
6597 Discr : Entity_Id;
6598 Discr_Con_Elist : Elist_Id;
6599 Discr_Con_El : Elmt_Id;
6600 Subt : Entity_Id;
6601
6602 begin
6603 -- Set the designated type so it is available in case this is an access
6604 -- to a self-referential type, e.g. a standard list type with a next
6605 -- pointer. Will be reset after subtype is built.
6606
6607 Set_Directly_Designated_Type
6608 (Derived_Type, Designated_Type (Parent_Type));
6609
6610 Subt := Process_Subtype (S, N);
6611
6612 if Nkind (S) /= N_Subtype_Indication
6613 and then Subt /= Base_Type (Subt)
6614 then
6615 Set_Ekind (Derived_Type, E_Access_Subtype);
6616 end if;
6617
6618 if Ekind (Derived_Type) = E_Access_Subtype then
6619 declare
6620 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6621 Ibase : constant Entity_Id :=
6622 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6623 Svg_Chars : constant Name_Id := Chars (Ibase);
6624 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6625 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6626
6627 begin
6628 Copy_Node (Pbase, Ibase);
6629
6630 -- Restore Itype status after Copy_Node
6631
6632 Set_Is_Itype (Ibase);
6633 Set_Associated_Node_For_Itype (Ibase, N);
6634
6635 Set_Chars (Ibase, Svg_Chars);
6636 Set_Prev_Entity (Ibase, Svg_Prev_E);
6637 Set_Next_Entity (Ibase, Svg_Next_E);
6638 Set_Sloc (Ibase, Sloc (Derived_Type));
6639 Set_Scope (Ibase, Scope (Derived_Type));
6640 Set_Freeze_Node (Ibase, Empty);
6641 Set_Is_Frozen (Ibase, False);
6642 Set_Comes_From_Source (Ibase, False);
6643 Set_Is_First_Subtype (Ibase, False);
6644
6645 Set_Etype (Ibase, Pbase);
6646 Set_Etype (Derived_Type, Ibase);
6647 end;
6648 end if;
6649
6650 Set_Directly_Designated_Type
6651 (Derived_Type, Designated_Type (Subt));
6652
6653 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6654 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6655 Set_Size_Info (Derived_Type, Parent_Type);
6656 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6657 Set_Depends_On_Private (Derived_Type,
6658 Has_Private_Component (Derived_Type));
6659 Conditional_Delay (Derived_Type, Subt);
6660
6661 if Is_Access_Subprogram_Type (Derived_Type) then
6662 Set_Can_Use_Internal_Rep
6663 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6664 end if;
6665
6666 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6667 -- that it is not redundant.
6668
6669 if Null_Exclusion_Present (Type_Definition (N)) then
6670 Set_Can_Never_Be_Null (Derived_Type);
6671
6672 elsif Can_Never_Be_Null (Parent_Type) then
6673 Set_Can_Never_Be_Null (Derived_Type);
6674 end if;
6675
6676 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6677 -- the root type for this information.
6678
6679 -- Apply range checks to discriminants for derived record case
6680 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6681
6682 Desig_Type := Designated_Type (Derived_Type);
6683
6684 if Is_Composite_Type (Desig_Type)
6685 and then (not Is_Array_Type (Desig_Type))
6686 and then Has_Discriminants (Desig_Type)
6687 and then Base_Type (Desig_Type) /= Desig_Type
6688 then
6689 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6690 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6691
6692 Discr := First_Discriminant (Base_Type (Desig_Type));
6693 while Present (Discr_Con_El) loop
6694 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6695 Next_Elmt (Discr_Con_El);
6696 Next_Discriminant (Discr);
6697 end loop;
6698 end if;
6699 end Build_Derived_Access_Type;
6700
6701 ------------------------------
6702 -- Build_Derived_Array_Type --
6703 ------------------------------
6704
6705 procedure Build_Derived_Array_Type
6706 (N : Node_Id;
6707 Parent_Type : Entity_Id;
6708 Derived_Type : Entity_Id)
6709 is
6710 Loc : constant Source_Ptr := Sloc (N);
6711 Tdef : constant Node_Id := Type_Definition (N);
6712 Indic : constant Node_Id := Subtype_Indication (Tdef);
6713 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6714 Implicit_Base : Entity_Id := Empty;
6715 New_Indic : Node_Id;
6716
6717 procedure Make_Implicit_Base;
6718 -- If the parent subtype is constrained, the derived type is a subtype
6719 -- of an implicit base type derived from the parent base.
6720
6721 ------------------------
6722 -- Make_Implicit_Base --
6723 ------------------------
6724
6725 procedure Make_Implicit_Base is
6726 begin
6727 Implicit_Base :=
6728 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6729
6730 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6731 Set_Etype (Implicit_Base, Parent_Base);
6732
6733 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6734 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6735
6736 Set_Has_Delayed_Freeze (Implicit_Base, True);
6737 end Make_Implicit_Base;
6738
6739 -- Start of processing for Build_Derived_Array_Type
6740
6741 begin
6742 if not Is_Constrained (Parent_Type) then
6743 if Nkind (Indic) /= N_Subtype_Indication then
6744 Set_Ekind (Derived_Type, E_Array_Type);
6745
6746 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6747 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6748
6749 Set_Has_Delayed_Freeze (Derived_Type, True);
6750
6751 else
6752 Make_Implicit_Base;
6753 Set_Etype (Derived_Type, Implicit_Base);
6754
6755 New_Indic :=
6756 Make_Subtype_Declaration (Loc,
6757 Defining_Identifier => Derived_Type,
6758 Subtype_Indication =>
6759 Make_Subtype_Indication (Loc,
6760 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6761 Constraint => Constraint (Indic)));
6762
6763 Rewrite (N, New_Indic);
6764 Analyze (N);
6765 end if;
6766
6767 else
6768 if Nkind (Indic) /= N_Subtype_Indication then
6769 Make_Implicit_Base;
6770
6771 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6772 Set_Etype (Derived_Type, Implicit_Base);
6773 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6774
6775 else
6776 Error_Msg_N ("illegal constraint on constrained type", Indic);
6777 end if;
6778 end if;
6779
6780 -- If parent type is not a derived type itself, and is declared in
6781 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6782 -- the new type's concatenation operator since Derive_Subprograms
6783 -- will not inherit the parent's operator. If the parent type is
6784 -- unconstrained, the operator is of the unconstrained base type.
6785
6786 if Number_Dimensions (Parent_Type) = 1
6787 and then not Is_Limited_Type (Parent_Type)
6788 and then not Is_Derived_Type (Parent_Type)
6789 and then not Is_Package_Or_Generic_Package
6790 (Scope (Base_Type (Parent_Type)))
6791 then
6792 if not Is_Constrained (Parent_Type)
6793 and then Is_Constrained (Derived_Type)
6794 then
6795 New_Concatenation_Op (Implicit_Base);
6796 else
6797 New_Concatenation_Op (Derived_Type);
6798 end if;
6799 end if;
6800 end Build_Derived_Array_Type;
6801
6802 -----------------------------------
6803 -- Build_Derived_Concurrent_Type --
6804 -----------------------------------
6805
6806 procedure Build_Derived_Concurrent_Type
6807 (N : Node_Id;
6808 Parent_Type : Entity_Id;
6809 Derived_Type : Entity_Id)
6810 is
6811 Loc : constant Source_Ptr := Sloc (N);
6812 Def : constant Node_Id := Type_Definition (N);
6813 Indic : constant Node_Id := Subtype_Indication (Def);
6814
6815 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6816 Corr_Decl : Node_Id;
6817 Corr_Decl_Needed : Boolean;
6818 -- If the derived type has fewer discriminants than its parent, the
6819 -- corresponding record is also a derived type, in order to account for
6820 -- the bound discriminants. We create a full type declaration for it in
6821 -- this case.
6822
6823 Constraint_Present : constant Boolean :=
6824 Nkind (Indic) = N_Subtype_Indication;
6825
6826 D_Constraint : Node_Id;
6827 New_Constraint : Elist_Id := No_Elist;
6828 Old_Disc : Entity_Id;
6829 New_Disc : Entity_Id;
6830 New_N : Node_Id;
6831
6832 begin
6833 Set_Stored_Constraint (Derived_Type, No_Elist);
6834 Corr_Decl_Needed := False;
6835 Old_Disc := Empty;
6836
6837 if Present (Discriminant_Specifications (N))
6838 and then Constraint_Present
6839 then
6840 Old_Disc := First_Discriminant (Parent_Type);
6841 New_Disc := First (Discriminant_Specifications (N));
6842 while Present (New_Disc) and then Present (Old_Disc) loop
6843 Next_Discriminant (Old_Disc);
6844 Next (New_Disc);
6845 end loop;
6846 end if;
6847
6848 if Present (Old_Disc) and then Expander_Active then
6849
6850 -- The new type has fewer discriminants, so we need to create a new
6851 -- corresponding record, which is derived from the corresponding
6852 -- record of the parent, and has a stored constraint that captures
6853 -- the values of the discriminant constraints. The corresponding
6854 -- record is needed only if expander is active and code generation is
6855 -- enabled.
6856
6857 -- The type declaration for the derived corresponding record has the
6858 -- same discriminant part and constraints as the current declaration.
6859 -- Copy the unanalyzed tree to build declaration.
6860
6861 Corr_Decl_Needed := True;
6862 New_N := Copy_Separate_Tree (N);
6863
6864 Corr_Decl :=
6865 Make_Full_Type_Declaration (Loc,
6866 Defining_Identifier => Corr_Record,
6867 Discriminant_Specifications =>
6868 Discriminant_Specifications (New_N),
6869 Type_Definition =>
6870 Make_Derived_Type_Definition (Loc,
6871 Subtype_Indication =>
6872 Make_Subtype_Indication (Loc,
6873 Subtype_Mark =>
6874 New_Occurrence_Of
6875 (Corresponding_Record_Type (Parent_Type), Loc),
6876 Constraint =>
6877 Constraint
6878 (Subtype_Indication (Type_Definition (New_N))))));
6879 end if;
6880
6881 -- Copy Storage_Size and Relative_Deadline variables if task case
6882
6883 if Is_Task_Type (Parent_Type) then
6884 Set_Storage_Size_Variable (Derived_Type,
6885 Storage_Size_Variable (Parent_Type));
6886 Set_Relative_Deadline_Variable (Derived_Type,
6887 Relative_Deadline_Variable (Parent_Type));
6888 end if;
6889
6890 if Present (Discriminant_Specifications (N)) then
6891 Push_Scope (Derived_Type);
6892 Check_Or_Process_Discriminants (N, Derived_Type);
6893
6894 if Constraint_Present then
6895 New_Constraint :=
6896 Expand_To_Stored_Constraint
6897 (Parent_Type,
6898 Build_Discriminant_Constraints
6899 (Parent_Type, Indic, True));
6900 end if;
6901
6902 End_Scope;
6903
6904 elsif Constraint_Present then
6905
6906 -- Build an unconstrained derived type and rewrite the derived type
6907 -- as a subtype of this new base type.
6908
6909 declare
6910 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6911 New_Base : Entity_Id;
6912 New_Decl : Node_Id;
6913 New_Indic : Node_Id;
6914
6915 begin
6916 New_Base :=
6917 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6918
6919 New_Decl :=
6920 Make_Full_Type_Declaration (Loc,
6921 Defining_Identifier => New_Base,
6922 Type_Definition =>
6923 Make_Derived_Type_Definition (Loc,
6924 Abstract_Present => Abstract_Present (Def),
6925 Limited_Present => Limited_Present (Def),
6926 Subtype_Indication =>
6927 New_Occurrence_Of (Parent_Base, Loc)));
6928
6929 Mark_Rewrite_Insertion (New_Decl);
6930 Insert_Before (N, New_Decl);
6931 Analyze (New_Decl);
6932
6933 New_Indic :=
6934 Make_Subtype_Indication (Loc,
6935 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6936 Constraint => Relocate_Node (Constraint (Indic)));
6937
6938 Rewrite (N,
6939 Make_Subtype_Declaration (Loc,
6940 Defining_Identifier => Derived_Type,
6941 Subtype_Indication => New_Indic));
6942
6943 Analyze (N);
6944 return;
6945 end;
6946 end if;
6947
6948 -- By default, operations and private data are inherited from parent.
6949 -- However, in the presence of bound discriminants, a new corresponding
6950 -- record will be created, see below.
6951
6952 Set_Has_Discriminants
6953 (Derived_Type, Has_Discriminants (Parent_Type));
6954 Set_Corresponding_Record_Type
6955 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6956
6957 -- Is_Constrained is set according the parent subtype, but is set to
6958 -- False if the derived type is declared with new discriminants.
6959
6960 Set_Is_Constrained
6961 (Derived_Type,
6962 (Is_Constrained (Parent_Type) or else Constraint_Present)
6963 and then not Present (Discriminant_Specifications (N)));
6964
6965 if Constraint_Present then
6966 if not Has_Discriminants (Parent_Type) then
6967 Error_Msg_N ("untagged parent must have discriminants", N);
6968
6969 elsif Present (Discriminant_Specifications (N)) then
6970
6971 -- Verify that new discriminants are used to constrain old ones
6972
6973 D_Constraint := First (Constraints (Constraint (Indic)));
6974
6975 Old_Disc := First_Discriminant (Parent_Type);
6976
6977 while Present (D_Constraint) loop
6978 if Nkind (D_Constraint) /= N_Discriminant_Association then
6979
6980 -- Positional constraint. If it is a reference to a new
6981 -- discriminant, it constrains the corresponding old one.
6982
6983 if Nkind (D_Constraint) = N_Identifier then
6984 New_Disc := First_Discriminant (Derived_Type);
6985 while Present (New_Disc) loop
6986 exit when Chars (New_Disc) = Chars (D_Constraint);
6987 Next_Discriminant (New_Disc);
6988 end loop;
6989
6990 if Present (New_Disc) then
6991 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6992 end if;
6993 end if;
6994
6995 Next_Discriminant (Old_Disc);
6996
6997 -- if this is a named constraint, search by name for the old
6998 -- discriminants constrained by the new one.
6999
7000 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7001
7002 -- Find new discriminant with that name
7003
7004 New_Disc := First_Discriminant (Derived_Type);
7005 while Present (New_Disc) loop
7006 exit when
7007 Chars (New_Disc) = Chars (Expression (D_Constraint));
7008 Next_Discriminant (New_Disc);
7009 end loop;
7010
7011 if Present (New_Disc) then
7012
7013 -- Verify that new discriminant renames some discriminant
7014 -- of the parent type, and associate the new discriminant
7015 -- with one or more old ones that it renames.
7016
7017 declare
7018 Selector : Node_Id;
7019
7020 begin
7021 Selector := First (Selector_Names (D_Constraint));
7022 while Present (Selector) loop
7023 Old_Disc := First_Discriminant (Parent_Type);
7024 while Present (Old_Disc) loop
7025 exit when Chars (Old_Disc) = Chars (Selector);
7026 Next_Discriminant (Old_Disc);
7027 end loop;
7028
7029 if Present (Old_Disc) then
7030 Set_Corresponding_Discriminant
7031 (New_Disc, Old_Disc);
7032 end if;
7033
7034 Next (Selector);
7035 end loop;
7036 end;
7037 end if;
7038 end if;
7039
7040 Next (D_Constraint);
7041 end loop;
7042
7043 New_Disc := First_Discriminant (Derived_Type);
7044 while Present (New_Disc) loop
7045 if No (Corresponding_Discriminant (New_Disc)) then
7046 Error_Msg_NE
7047 ("new discriminant& must constrain old one", N, New_Disc);
7048
7049 elsif not
7050 Subtypes_Statically_Compatible
7051 (Etype (New_Disc),
7052 Etype (Corresponding_Discriminant (New_Disc)))
7053 then
7054 Error_Msg_NE
7055 ("& not statically compatible with parent discriminant",
7056 N, New_Disc);
7057 end if;
7058
7059 Next_Discriminant (New_Disc);
7060 end loop;
7061 end if;
7062
7063 elsif Present (Discriminant_Specifications (N)) then
7064 Error_Msg_N
7065 ("missing discriminant constraint in untagged derivation", N);
7066 end if;
7067
7068 -- The entity chain of the derived type includes the new discriminants
7069 -- but shares operations with the parent.
7070
7071 if Present (Discriminant_Specifications (N)) then
7072 Old_Disc := First_Discriminant (Parent_Type);
7073 while Present (Old_Disc) loop
7074 if No (Next_Entity (Old_Disc))
7075 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7076 then
7077 Link_Entities
7078 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7079 exit;
7080 end if;
7081
7082 Next_Discriminant (Old_Disc);
7083 end loop;
7084
7085 else
7086 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7087 if Has_Discriminants (Parent_Type) then
7088 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7089 Set_Discriminant_Constraint (
7090 Derived_Type, Discriminant_Constraint (Parent_Type));
7091 end if;
7092 end if;
7093
7094 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7095
7096 Set_Has_Completion (Derived_Type);
7097
7098 if Corr_Decl_Needed then
7099 Set_Stored_Constraint (Derived_Type, New_Constraint);
7100 Insert_After (N, Corr_Decl);
7101 Analyze (Corr_Decl);
7102 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7103 end if;
7104 end Build_Derived_Concurrent_Type;
7105
7106 ------------------------------------
7107 -- Build_Derived_Enumeration_Type --
7108 ------------------------------------
7109
7110 procedure Build_Derived_Enumeration_Type
7111 (N : Node_Id;
7112 Parent_Type : Entity_Id;
7113 Derived_Type : Entity_Id)
7114 is
7115 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7116 -- When the type declaration includes a constraint, we generate
7117 -- a subtype declaration of an anonymous base type, with the constraint
7118 -- given in the original type declaration. Conceptually, the bounds
7119 -- are converted to the new base type, and this conversion freezes
7120 -- (prematurely) that base type, when the bounds are simply literals.
7121 -- As a result, a representation clause for the derived type is then
7122 -- rejected or ignored. This procedure recognizes the simple case of
7123 -- literal bounds, which allows us to indicate that the conversions
7124 -- are not freeze points, and the subsequent representation clause
7125 -- can be accepted.
7126 -- A similar approach might be used to resolve the long-standing
7127 -- problem of premature freezing of derived numeric types ???
7128
7129 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7130 begin
7131 return Nkind (B) = N_Type_Conversion
7132 and then Is_Entity_Name (Expression (B))
7133 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7134 end Bound_Belongs_To_Type;
7135
7136 Loc : constant Source_Ptr := Sloc (N);
7137 Def : constant Node_Id := Type_Definition (N);
7138 Indic : constant Node_Id := Subtype_Indication (Def);
7139 Implicit_Base : Entity_Id;
7140 Literal : Entity_Id;
7141 New_Lit : Entity_Id;
7142 Literals_List : List_Id;
7143 Type_Decl : Node_Id;
7144 Hi, Lo : Node_Id;
7145 Rang_Expr : Node_Id;
7146
7147 begin
7148 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7149 -- not have explicit literals lists we need to process types derived
7150 -- from them specially. This is handled by Derived_Standard_Character.
7151 -- If the parent type is a generic type, there are no literals either,
7152 -- and we construct the same skeletal representation as for the generic
7153 -- parent type.
7154
7155 if Is_Standard_Character_Type (Parent_Type) then
7156 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7157
7158 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7159 declare
7160 Lo : Node_Id;
7161 Hi : Node_Id;
7162
7163 begin
7164 if Nkind (Indic) /= N_Subtype_Indication then
7165 Lo :=
7166 Make_Attribute_Reference (Loc,
7167 Attribute_Name => Name_First,
7168 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7169 Set_Etype (Lo, Derived_Type);
7170
7171 Hi :=
7172 Make_Attribute_Reference (Loc,
7173 Attribute_Name => Name_Last,
7174 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7175 Set_Etype (Hi, Derived_Type);
7176
7177 Set_Scalar_Range (Derived_Type,
7178 Make_Range (Loc,
7179 Low_Bound => Lo,
7180 High_Bound => Hi));
7181 else
7182
7183 -- Analyze subtype indication and verify compatibility
7184 -- with parent type.
7185
7186 if Base_Type (Process_Subtype (Indic, N)) /=
7187 Base_Type (Parent_Type)
7188 then
7189 Error_Msg_N
7190 ("illegal constraint for formal discrete type", N);
7191 end if;
7192 end if;
7193 end;
7194
7195 else
7196 -- If a constraint is present, analyze the bounds to catch
7197 -- premature usage of the derived literals.
7198
7199 if Nkind (Indic) = N_Subtype_Indication
7200 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7201 then
7202 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7203 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7204 end if;
7205
7206 -- Introduce an implicit base type for the derived type even if there
7207 -- is no constraint attached to it, since this seems closer to the
7208 -- Ada semantics. Build a full type declaration tree for the derived
7209 -- type using the implicit base type as the defining identifier. The
7210 -- build a subtype declaration tree which applies the constraint (if
7211 -- any) have it replace the derived type declaration.
7212
7213 Literal := First_Literal (Parent_Type);
7214 Literals_List := New_List;
7215 while Present (Literal)
7216 and then Ekind (Literal) = E_Enumeration_Literal
7217 loop
7218 -- Literals of the derived type have the same representation as
7219 -- those of the parent type, but this representation can be
7220 -- overridden by an explicit representation clause. Indicate
7221 -- that there is no explicit representation given yet. These
7222 -- derived literals are implicit operations of the new type,
7223 -- and can be overridden by explicit ones.
7224
7225 if Nkind (Literal) = N_Defining_Character_Literal then
7226 New_Lit :=
7227 Make_Defining_Character_Literal (Loc, Chars (Literal));
7228 else
7229 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7230 end if;
7231
7232 Set_Ekind (New_Lit, E_Enumeration_Literal);
7233 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7234 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7235 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7236 Set_Alias (New_Lit, Literal);
7237 Set_Is_Known_Valid (New_Lit, True);
7238
7239 Append (New_Lit, Literals_List);
7240 Next_Literal (Literal);
7241 end loop;
7242
7243 Implicit_Base :=
7244 Make_Defining_Identifier (Sloc (Derived_Type),
7245 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7246
7247 -- Indicate the proper nature of the derived type. This must be done
7248 -- before analysis of the literals, to recognize cases when a literal
7249 -- may be hidden by a previous explicit function definition (cf.
7250 -- c83031a).
7251
7252 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7253 Set_Etype (Derived_Type, Implicit_Base);
7254
7255 Type_Decl :=
7256 Make_Full_Type_Declaration (Loc,
7257 Defining_Identifier => Implicit_Base,
7258 Discriminant_Specifications => No_List,
7259 Type_Definition =>
7260 Make_Enumeration_Type_Definition (Loc, Literals_List));
7261
7262 Mark_Rewrite_Insertion (Type_Decl);
7263 Insert_Before (N, Type_Decl);
7264 Analyze (Type_Decl);
7265
7266 -- The anonymous base now has a full declaration, but this base
7267 -- is not a first subtype.
7268
7269 Set_Is_First_Subtype (Implicit_Base, False);
7270
7271 -- After the implicit base is analyzed its Etype needs to be changed
7272 -- to reflect the fact that it is derived from the parent type which
7273 -- was ignored during analysis. We also set the size at this point.
7274
7275 Set_Etype (Implicit_Base, Parent_Type);
7276
7277 Set_Size_Info (Implicit_Base, Parent_Type);
7278 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7279 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7280
7281 -- Copy other flags from parent type
7282
7283 Set_Has_Non_Standard_Rep
7284 (Implicit_Base, Has_Non_Standard_Rep
7285 (Parent_Type));
7286 Set_Has_Pragma_Ordered
7287 (Implicit_Base, Has_Pragma_Ordered
7288 (Parent_Type));
7289 Set_Has_Delayed_Freeze (Implicit_Base);
7290
7291 -- Process the subtype indication including a validation check on the
7292 -- constraint, if any. If a constraint is given, its bounds must be
7293 -- implicitly converted to the new type.
7294
7295 if Nkind (Indic) = N_Subtype_Indication then
7296 declare
7297 R : constant Node_Id :=
7298 Range_Expression (Constraint (Indic));
7299
7300 begin
7301 if Nkind (R) = N_Range then
7302 Hi := Build_Scalar_Bound
7303 (High_Bound (R), Parent_Type, Implicit_Base);
7304 Lo := Build_Scalar_Bound
7305 (Low_Bound (R), Parent_Type, Implicit_Base);
7306
7307 else
7308 -- Constraint is a Range attribute. Replace with explicit
7309 -- mention of the bounds of the prefix, which must be a
7310 -- subtype.
7311
7312 Analyze (Prefix (R));
7313 Hi :=
7314 Convert_To (Implicit_Base,
7315 Make_Attribute_Reference (Loc,
7316 Attribute_Name => Name_Last,
7317 Prefix =>
7318 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7319
7320 Lo :=
7321 Convert_To (Implicit_Base,
7322 Make_Attribute_Reference (Loc,
7323 Attribute_Name => Name_First,
7324 Prefix =>
7325 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7326 end if;
7327 end;
7328
7329 else
7330 Hi :=
7331 Build_Scalar_Bound
7332 (Type_High_Bound (Parent_Type),
7333 Parent_Type, Implicit_Base);
7334 Lo :=
7335 Build_Scalar_Bound
7336 (Type_Low_Bound (Parent_Type),
7337 Parent_Type, Implicit_Base);
7338 end if;
7339
7340 Rang_Expr :=
7341 Make_Range (Loc,
7342 Low_Bound => Lo,
7343 High_Bound => Hi);
7344
7345 -- If we constructed a default range for the case where no range
7346 -- was given, then the expressions in the range must not freeze
7347 -- since they do not correspond to expressions in the source.
7348 -- However, if the type inherits predicates the expressions will
7349 -- be elaborated earlier and must freeze.
7350
7351 if (Nkind (Indic) /= N_Subtype_Indication
7352 or else
7353 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
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, concurrent, access and most enumeration types, the
7678 -- derivation from full view requires a fully-fledged declaration.
7679 -- In the other cases, just use an itype.
7680
7681 if Is_Record_Type (Full_Parent)
7682 or else Is_Concurrent_Type (Full_Parent)
7683 or else Is_Access_Type (Full_Parent)
7684 or else
7685 (Is_Enumeration_Type (Full_Parent)
7686 and then not Is_Standard_Character_Type (Full_Parent)
7687 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7688 then
7689 -- Copy and adjust declaration to provide a completion for what
7690 -- is originally a private declaration. Indicate that full view
7691 -- is internally generated.
7692
7693 Set_Comes_From_Source (Full_N, False);
7694 Set_Comes_From_Source (Full_Der, False);
7695 Set_Parent (Full_Der, Full_N);
7696 Set_Defining_Identifier (Full_N, Full_Der);
7697
7698 -- If there are no constraints, adjust the subtype mark
7699
7700 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7701 N_Subtype_Indication
7702 then
7703 Set_Subtype_Indication
7704 (Type_Definition (Full_N),
7705 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7706 end if;
7707
7708 Insert_After (N, Full_N);
7709
7710 -- Build full view of derived type from full view of parent which
7711 -- is now installed. Subprograms have been derived on the partial
7712 -- view, the completion does not derive them anew.
7713
7714 if Is_Record_Type (Full_Parent) then
7715
7716 -- If parent type is tagged, the completion inherits the proper
7717 -- primitive operations.
7718
7719 if Is_Tagged_Type (Parent_Type) then
7720 Build_Derived_Record_Type
7721 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7722 else
7723 Build_Derived_Record_Type
7724 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7725 end if;
7726
7727 else
7728 Build_Derived_Type
7729 (Full_N, Full_Parent, Full_Der,
7730 Is_Completion => False, Derive_Subps => False);
7731 end if;
7732
7733 -- The full declaration has been introduced into the tree and
7734 -- processed in the step above. It should not be analyzed again
7735 -- (when encountered later in the current list of declarations)
7736 -- to prevent spurious name conflicts. The full entity remains
7737 -- invisible.
7738
7739 Set_Analyzed (Full_N);
7740
7741 else
7742 Full_Der :=
7743 Make_Defining_Identifier (Sloc (Derived_Type),
7744 Chars => Chars (Derived_Type));
7745 Set_Is_Itype (Full_Der);
7746 Set_Associated_Node_For_Itype (Full_Der, N);
7747 Set_Parent (Full_Der, N);
7748 Build_Derived_Type
7749 (N, Full_Parent, Full_Der,
7750 Is_Completion => False, Derive_Subps => False);
7751 end if;
7752
7753 Set_Has_Private_Declaration (Full_Der);
7754 Set_Has_Private_Declaration (Derived_Type);
7755
7756 Set_Scope (Full_Der, Scope (Derived_Type));
7757 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7758 Set_Has_Size_Clause (Full_Der, False);
7759 Set_Has_Alignment_Clause (Full_Der, False);
7760 Set_Has_Delayed_Freeze (Full_Der);
7761 Set_Is_Frozen (Full_Der, False);
7762 Set_Freeze_Node (Full_Der, Empty);
7763 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7764 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7765
7766 -- The convention on the base type may be set in the private part
7767 -- and not propagated to the subtype until later, so we obtain the
7768 -- convention from the base type of the parent.
7769
7770 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7771 end Copy_And_Build;
7772
7773 -- Start of processing for Build_Derived_Private_Type
7774
7775 begin
7776 if Is_Tagged_Type (Parent_Type) then
7777 Full_P := Full_View (Parent_Type);
7778
7779 -- A type extension of a type with unknown discriminants is an
7780 -- indefinite type that the back-end cannot handle directly.
7781 -- We treat it as a private type, and build a completion that is
7782 -- derived from the full view of the parent, and hopefully has
7783 -- known discriminants.
7784
7785 -- If the full view of the parent type has an underlying record view,
7786 -- use it to generate the underlying record view of this derived type
7787 -- (required for chains of derivations with unknown discriminants).
7788
7789 -- Minor optimization: we avoid the generation of useless underlying
7790 -- record view entities if the private type declaration has unknown
7791 -- discriminants but its corresponding full view has no
7792 -- discriminants.
7793
7794 if Has_Unknown_Discriminants (Parent_Type)
7795 and then Present (Full_P)
7796 and then (Has_Discriminants (Full_P)
7797 or else Present (Underlying_Record_View (Full_P)))
7798 and then not In_Open_Scopes (Par_Scope)
7799 and then Expander_Active
7800 then
7801 declare
7802 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7803 New_Ext : constant Node_Id :=
7804 Copy_Separate_Tree
7805 (Record_Extension_Part (Type_Definition (N)));
7806 Decl : Node_Id;
7807
7808 begin
7809 Build_Derived_Record_Type
7810 (N, Parent_Type, Derived_Type, Derive_Subps);
7811
7812 -- Build anonymous completion, as a derivation from the full
7813 -- view of the parent. This is not a completion in the usual
7814 -- sense, because the current type is not private.
7815
7816 Decl :=
7817 Make_Full_Type_Declaration (Loc,
7818 Defining_Identifier => Full_Der,
7819 Type_Definition =>
7820 Make_Derived_Type_Definition (Loc,
7821 Subtype_Indication =>
7822 New_Copy_Tree
7823 (Subtype_Indication (Type_Definition (N))),
7824 Record_Extension_Part => New_Ext));
7825
7826 -- If the parent type has an underlying record view, use it
7827 -- here to build the new underlying record view.
7828
7829 if Present (Underlying_Record_View (Full_P)) then
7830 pragma Assert
7831 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7832 = N_Identifier);
7833 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7834 Underlying_Record_View (Full_P));
7835 end if;
7836
7837 Install_Private_Declarations (Par_Scope);
7838 Install_Visible_Declarations (Par_Scope);
7839 Insert_Before (N, Decl);
7840
7841 -- Mark entity as an underlying record view before analysis,
7842 -- to avoid generating the list of its primitive operations
7843 -- (which is not really required for this entity) and thus
7844 -- prevent spurious errors associated with missing overriding
7845 -- of abstract primitives (overridden only for Derived_Type).
7846
7847 Set_Ekind (Full_Der, E_Record_Type);
7848 Set_Is_Underlying_Record_View (Full_Der);
7849 Set_Default_SSO (Full_Der);
7850 Set_No_Reordering (Full_Der, No_Component_Reordering);
7851
7852 Analyze (Decl);
7853
7854 pragma Assert (Has_Discriminants (Full_Der)
7855 and then not Has_Unknown_Discriminants (Full_Der));
7856
7857 Uninstall_Declarations (Par_Scope);
7858
7859 -- Freeze the underlying record view, to prevent generation of
7860 -- useless dispatching information, which is simply shared with
7861 -- the real derived type.
7862
7863 Set_Is_Frozen (Full_Der);
7864
7865 -- If the derived type has access discriminants, create
7866 -- references to their anonymous types now, to prevent
7867 -- back-end problems when their first use is in generated
7868 -- bodies of primitives.
7869
7870 declare
7871 E : Entity_Id;
7872
7873 begin
7874 E := First_Entity (Full_Der);
7875
7876 while Present (E) loop
7877 if Ekind (E) = E_Discriminant
7878 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7879 then
7880 Build_Itype_Reference (Etype (E), Decl);
7881 end if;
7882
7883 Next_Entity (E);
7884 end loop;
7885 end;
7886
7887 -- Set up links between real entity and underlying record view
7888
7889 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7890 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7891 end;
7892
7893 -- If discriminants are known, build derived record
7894
7895 else
7896 Build_Derived_Record_Type
7897 (N, Parent_Type, Derived_Type, Derive_Subps);
7898 end if;
7899
7900 return;
7901
7902 elsif Has_Discriminants (Parent_Type) then
7903
7904 -- Build partial view of derived type from partial view of parent.
7905 -- This must be done before building the full derivation because the
7906 -- second derivation will modify the discriminants of the first and
7907 -- the discriminants are chained with the rest of the components in
7908 -- the full derivation.
7909
7910 Build_Derived_Record_Type
7911 (N, Parent_Type, Derived_Type, Derive_Subps);
7912
7913 -- Build the full derivation if this is not the anonymous derived
7914 -- base type created by Build_Derived_Record_Type in the constrained
7915 -- case (see point 5. of its head comment) since we build it for the
7916 -- derived subtype.
7917
7918 if Present (Full_View (Parent_Type))
7919 and then not Is_Itype (Derived_Type)
7920 then
7921 declare
7922 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7923 Discr : Entity_Id;
7924 Last_Discr : Entity_Id;
7925
7926 begin
7927 -- If this is not a completion, construct the implicit full
7928 -- view by deriving from the full view of the parent type.
7929 -- But if this is a completion, the derived private type
7930 -- being built is a full view and the full derivation can
7931 -- only be its underlying full view.
7932
7933 Build_Full_Derivation;
7934
7935 if not Is_Completion then
7936 Set_Full_View (Derived_Type, Full_Der);
7937 else
7938 Set_Underlying_Full_View (Derived_Type, Full_Der);
7939 Set_Is_Underlying_Full_View (Full_Der);
7940 end if;
7941
7942 if not Is_Base_Type (Derived_Type) then
7943 Set_Full_View (Der_Base, Base_Type (Full_Der));
7944 end if;
7945
7946 -- Copy the discriminant list from full view to the partial
7947 -- view (base type and its subtype). Gigi requires that the
7948 -- partial and full views have the same discriminants.
7949
7950 -- Note that since the partial view points to discriminants
7951 -- in the full view, their scope will be that of the full
7952 -- view. This might cause some front end problems and need
7953 -- adjustment???
7954
7955 Discr := First_Discriminant (Base_Type (Full_Der));
7956 Set_First_Entity (Der_Base, Discr);
7957
7958 loop
7959 Last_Discr := Discr;
7960 Next_Discriminant (Discr);
7961 exit when No (Discr);
7962 end loop;
7963
7964 Set_Last_Entity (Der_Base, Last_Discr);
7965 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7966 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7967 end;
7968 end if;
7969
7970 elsif Present (Full_View (Parent_Type))
7971 and then Has_Discriminants (Full_View (Parent_Type))
7972 then
7973 if Has_Unknown_Discriminants (Parent_Type)
7974 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7975 N_Subtype_Indication
7976 then
7977 Error_Msg_N
7978 ("cannot constrain type with unknown discriminants",
7979 Subtype_Indication (Type_Definition (N)));
7980 return;
7981 end if;
7982
7983 -- If this is not a completion, construct the implicit full view by
7984 -- deriving from the full view of the parent type. But if this is a
7985 -- completion, the derived private type being built is a full view
7986 -- and the full derivation can only be its underlying full view.
7987
7988 Build_Full_Derivation;
7989
7990 if not Is_Completion then
7991 Set_Full_View (Derived_Type, Full_Der);
7992 else
7993 Set_Underlying_Full_View (Derived_Type, Full_Der);
7994 Set_Is_Underlying_Full_View (Full_Der);
7995 end if;
7996
7997 -- In any case, the primitive operations are inherited from the
7998 -- parent type, not from the internal full view.
7999
8000 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8001
8002 if Derive_Subps then
8003 Derive_Subprograms (Parent_Type, Derived_Type);
8004 end if;
8005
8006 Set_Stored_Constraint (Derived_Type, No_Elist);
8007 Set_Is_Constrained
8008 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
8009
8010 else
8011 -- Untagged type, No discriminants on either view
8012
8013 if Nkind (Subtype_Indication (Type_Definition (N))) =
8014 N_Subtype_Indication
8015 then
8016 Error_Msg_N
8017 ("illegal constraint on type without discriminants", N);
8018 end if;
8019
8020 if Present (Discriminant_Specifications (N))
8021 and then Present (Full_View (Parent_Type))
8022 and then not Is_Tagged_Type (Full_View (Parent_Type))
8023 then
8024 Error_Msg_N ("cannot add discriminants to untagged type", N);
8025 end if;
8026
8027 Set_Stored_Constraint (Derived_Type, No_Elist);
8028 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8029
8030 Set_Is_Controlled_Active
8031 (Derived_Type, Is_Controlled_Active (Parent_Type));
8032
8033 Set_Disable_Controlled
8034 (Derived_Type, Disable_Controlled (Parent_Type));
8035
8036 Set_Has_Controlled_Component
8037 (Derived_Type, Has_Controlled_Component (Parent_Type));
8038
8039 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8040
8041 if not Is_Controlled (Parent_Type) then
8042 Set_Finalize_Storage_Only
8043 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8044 end if;
8045
8046 -- If this is not a completion, construct the implicit full view by
8047 -- deriving from the full view of the parent type.
8048
8049 -- ??? If the parent is untagged private and its completion is
8050 -- tagged, this mechanism will not work because we cannot derive from
8051 -- the tagged full view unless we have an extension.
8052
8053 if Present (Full_View (Parent_Type))
8054 and then not Is_Tagged_Type (Full_View (Parent_Type))
8055 and then not Is_Completion
8056 then
8057 Build_Full_Derivation;
8058 Set_Full_View (Derived_Type, Full_Der);
8059 end if;
8060 end if;
8061
8062 Set_Has_Unknown_Discriminants (Derived_Type,
8063 Has_Unknown_Discriminants (Parent_Type));
8064
8065 if Is_Private_Type (Derived_Type) then
8066 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8067 end if;
8068
8069 -- If the parent base type is in scope, add the derived type to its
8070 -- list of private dependents, because its full view may become
8071 -- visible subsequently (in a nested private part, a body, or in a
8072 -- further child unit).
8073
8074 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8075 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8076
8077 -- Check for unusual case where a type completed by a private
8078 -- derivation occurs within a package nested in a child unit, and
8079 -- the parent is declared in an ancestor.
8080
8081 if Is_Child_Unit (Scope (Current_Scope))
8082 and then Is_Completion
8083 and then In_Private_Part (Current_Scope)
8084 and then Scope (Parent_Type) /= Current_Scope
8085
8086 -- Note that if the parent has a completion in the private part,
8087 -- (which is itself a derivation from some other private type)
8088 -- it is that completion that is visible, there is no full view
8089 -- available, and no special processing is needed.
8090
8091 and then Present (Full_View (Parent_Type))
8092 then
8093 -- In this case, the full view of the parent type will become
8094 -- visible in the body of the enclosing child, and only then will
8095 -- the current type be possibly non-private. Build an underlying
8096 -- full view that will be installed when the enclosing child body
8097 -- is compiled.
8098
8099 if Present (Underlying_Full_View (Derived_Type)) then
8100 Full_Der := Underlying_Full_View (Derived_Type);
8101 else
8102 Build_Full_Derivation;
8103 Set_Underlying_Full_View (Derived_Type, Full_Der);
8104 Set_Is_Underlying_Full_View (Full_Der);
8105 end if;
8106
8107 -- The full view will be used to swap entities on entry/exit to
8108 -- the body, and must appear in the entity list for the package.
8109
8110 Append_Entity (Full_Der, Scope (Derived_Type));
8111 end if;
8112 end if;
8113 end Build_Derived_Private_Type;
8114
8115 -------------------------------
8116 -- Build_Derived_Record_Type --
8117 -------------------------------
8118
8119 -- 1. INTRODUCTION
8120
8121 -- Ideally we would like to use the same model of type derivation for
8122 -- tagged and untagged record types. Unfortunately this is not quite
8123 -- possible because the semantics of representation clauses is different
8124 -- for tagged and untagged records under inheritance. Consider the
8125 -- following:
8126
8127 -- type R (...) is [tagged] record ... end record;
8128 -- type T (...) is new R (...) [with ...];
8129
8130 -- The representation clauses for T can specify a completely different
8131 -- record layout from R's. Hence the same component can be placed in two
8132 -- very different positions in objects of type T and R. If R and T are
8133 -- tagged types, representation clauses for T can only specify the layout
8134 -- of non inherited components, thus components that are common in R and T
8135 -- have the same position in objects of type R and T.
8136
8137 -- This has two implications. The first is that the entire tree for R's
8138 -- declaration needs to be copied for T in the untagged case, so that T
8139 -- can be viewed as a record type of its own with its own representation
8140 -- clauses. The second implication is the way we handle discriminants.
8141 -- Specifically, in the untagged case we need a way to communicate to Gigi
8142 -- what are the real discriminants in the record, while for the semantics
8143 -- we need to consider those introduced by the user to rename the
8144 -- discriminants in the parent type. This is handled by introducing the
8145 -- notion of stored discriminants. See below for more.
8146
8147 -- Fortunately the way regular components are inherited can be handled in
8148 -- the same way in tagged and untagged types.
8149
8150 -- To complicate things a bit more the private view of a private extension
8151 -- cannot be handled in the same way as the full view (for one thing the
8152 -- semantic rules are somewhat different). We will explain what differs
8153 -- below.
8154
8155 -- 2. DISCRIMINANTS UNDER INHERITANCE
8156
8157 -- The semantic rules governing the discriminants of derived types are
8158 -- quite subtle.
8159
8160 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8161 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8162
8163 -- If parent type has discriminants, then the discriminants that are
8164 -- declared in the derived type are [3.4 (11)]:
8165
8166 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8167 -- there is one;
8168
8169 -- o Otherwise, each discriminant of the parent type (implicitly declared
8170 -- in the same order with the same specifications). In this case, the
8171 -- discriminants are said to be "inherited", or if unknown in the parent
8172 -- are also unknown in the derived type.
8173
8174 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8175
8176 -- o The parent subtype must be constrained;
8177
8178 -- o If the parent type is not a tagged type, then each discriminant of
8179 -- the derived type must be used in the constraint defining a parent
8180 -- subtype. [Implementation note: This ensures that the new discriminant
8181 -- can share storage with an existing discriminant.]
8182
8183 -- For the derived type each discriminant of the parent type is either
8184 -- inherited, constrained to equal some new discriminant of the derived
8185 -- type, or constrained to the value of an expression.
8186
8187 -- When inherited or constrained to equal some new discriminant, the
8188 -- parent discriminant and the discriminant of the derived type are said
8189 -- to "correspond".
8190
8191 -- If a discriminant of the parent type is constrained to a specific value
8192 -- in the derived type definition, then the discriminant is said to be
8193 -- "specified" by that derived type definition.
8194
8195 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8196
8197 -- We have spoken about stored discriminants in point 1 (introduction)
8198 -- above. There are two sorts of stored discriminants: implicit and
8199 -- explicit. As long as the derived type inherits the same discriminants as
8200 -- the root record type, stored discriminants are the same as regular
8201 -- discriminants, and are said to be implicit. However, if any discriminant
8202 -- in the root type was renamed in the derived type, then the derived
8203 -- type will contain explicit stored discriminants. Explicit stored
8204 -- discriminants are discriminants in addition to the semantically visible
8205 -- discriminants defined for the derived type. Stored discriminants are
8206 -- used by Gigi to figure out what are the physical discriminants in
8207 -- objects of the derived type (see precise definition in einfo.ads).
8208 -- As an example, consider the following:
8209
8210 -- type R (D1, D2, D3 : Int) is record ... end record;
8211 -- type T1 is new R;
8212 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8213 -- type T3 is new T2;
8214 -- type T4 (Y : Int) is new T3 (Y, 99);
8215
8216 -- The following table summarizes the discriminants and stored
8217 -- discriminants in R and T1 through T4:
8218
8219 -- Type Discrim Stored Discrim Comment
8220 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8221 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8222 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8223 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8224 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8225
8226 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8227 -- find the corresponding discriminant in the parent type, while
8228 -- Original_Record_Component (abbreviated ORC below) the actual physical
8229 -- component that is renamed. Finally the field Is_Completely_Hidden
8230 -- (abbreviated ICH below) is set for all explicit stored discriminants
8231 -- (see einfo.ads for more info). For the above example this gives:
8232
8233 -- Discrim CD ORC ICH
8234 -- ^^^^^^^ ^^ ^^^ ^^^
8235 -- D1 in R empty itself no
8236 -- D2 in R empty itself no
8237 -- D3 in R empty itself no
8238
8239 -- D1 in T1 D1 in R itself no
8240 -- D2 in T1 D2 in R itself no
8241 -- D3 in T1 D3 in R itself no
8242
8243 -- X1 in T2 D3 in T1 D3 in T2 no
8244 -- X2 in T2 D1 in T1 D1 in T2 no
8245 -- D1 in T2 empty itself yes
8246 -- D2 in T2 empty itself yes
8247 -- D3 in T2 empty itself yes
8248
8249 -- X1 in T3 X1 in T2 D3 in T3 no
8250 -- X2 in T3 X2 in T2 D1 in T3 no
8251 -- D1 in T3 empty itself yes
8252 -- D2 in T3 empty itself yes
8253 -- D3 in T3 empty itself yes
8254
8255 -- Y in T4 X1 in T3 D3 in T4 no
8256 -- D1 in T4 empty itself yes
8257 -- D2 in T4 empty itself yes
8258 -- D3 in T4 empty itself yes
8259
8260 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8261
8262 -- Type derivation for tagged types is fairly straightforward. If no
8263 -- discriminants are specified by the derived type, these are inherited
8264 -- from the parent. No explicit stored discriminants are ever necessary.
8265 -- The only manipulation that is done to the tree is that of adding a
8266 -- _parent field with parent type and constrained to the same constraint
8267 -- specified for the parent in the derived type definition. For instance:
8268
8269 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8270 -- type T1 is new R with null record;
8271 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8272
8273 -- are changed into:
8274
8275 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8276 -- _parent : R (D1, D2, D3);
8277 -- end record;
8278
8279 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8280 -- _parent : T1 (X2, 88, X1);
8281 -- end record;
8282
8283 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8284 -- ORC and ICH fields are:
8285
8286 -- Discrim CD ORC ICH
8287 -- ^^^^^^^ ^^ ^^^ ^^^
8288 -- D1 in R empty itself no
8289 -- D2 in R empty itself no
8290 -- D3 in R empty itself no
8291
8292 -- D1 in T1 D1 in R D1 in R no
8293 -- D2 in T1 D2 in R D2 in R no
8294 -- D3 in T1 D3 in R D3 in R no
8295
8296 -- X1 in T2 D3 in T1 D3 in R no
8297 -- X2 in T2 D1 in T1 D1 in R no
8298
8299 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8300 --
8301 -- Regardless of whether we dealing with a tagged or untagged type
8302 -- we will transform all derived type declarations of the form
8303 --
8304 -- type T is new R (...) [with ...];
8305 -- or
8306 -- subtype S is R (...);
8307 -- type T is new S [with ...];
8308 -- into
8309 -- type BT is new R [with ...];
8310 -- subtype T is BT (...);
8311 --
8312 -- That is, the base derived type is constrained only if it has no
8313 -- discriminants. The reason for doing this is that GNAT's semantic model
8314 -- assumes that a base type with discriminants is unconstrained.
8315 --
8316 -- Note that, strictly speaking, the above transformation is not always
8317 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8318 --
8319 -- procedure B34011A is
8320 -- type REC (D : integer := 0) is record
8321 -- I : Integer;
8322 -- end record;
8323
8324 -- package P is
8325 -- type T6 is new Rec;
8326 -- function F return T6;
8327 -- end P;
8328
8329 -- use P;
8330 -- package Q6 is
8331 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8332 -- end Q6;
8333 --
8334 -- The definition of Q6.U is illegal. However transforming Q6.U into
8335
8336 -- type BaseU is new T6;
8337 -- subtype U is BaseU (Q6.F.I)
8338
8339 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8340 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8341 -- the transformation described above.
8342
8343 -- There is another instance where the above transformation is incorrect.
8344 -- Consider:
8345
8346 -- package Pack is
8347 -- type Base (D : Integer) is tagged null record;
8348 -- procedure P (X : Base);
8349
8350 -- type Der is new Base (2) with null record;
8351 -- procedure P (X : Der);
8352 -- end Pack;
8353
8354 -- Then the above transformation turns this into
8355
8356 -- type Der_Base is new Base with null record;
8357 -- -- procedure P (X : Base) is implicitly inherited here
8358 -- -- as procedure P (X : Der_Base).
8359
8360 -- subtype Der is Der_Base (2);
8361 -- procedure P (X : Der);
8362 -- -- The overriding of P (X : Der_Base) is illegal since we
8363 -- -- have a parameter conformance problem.
8364
8365 -- To get around this problem, after having semantically processed Der_Base
8366 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8367 -- Discriminant_Constraint from Der so that when parameter conformance is
8368 -- checked when P is overridden, no semantic errors are flagged.
8369
8370 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8371
8372 -- Regardless of whether we are dealing with a tagged or untagged type
8373 -- we will transform all derived type declarations of the form
8374
8375 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8376 -- type T is new R [with ...];
8377 -- into
8378 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8379
8380 -- The reason for such transformation is that it allows us to implement a
8381 -- very clean form of component inheritance as explained below.
8382
8383 -- Note that this transformation is not achieved by direct tree rewriting
8384 -- and manipulation, but rather by redoing the semantic actions that the
8385 -- above transformation will entail. This is done directly in routine
8386 -- Inherit_Components.
8387
8388 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8389
8390 -- In both tagged and untagged derived types, regular non discriminant
8391 -- components are inherited in the derived type from the parent type. In
8392 -- the absence of discriminants component, inheritance is straightforward
8393 -- as components can simply be copied from the parent.
8394
8395 -- If the parent has discriminants, inheriting components constrained with
8396 -- these discriminants requires caution. Consider the following example:
8397
8398 -- type R (D1, D2 : Positive) is [tagged] record
8399 -- S : String (D1 .. D2);
8400 -- end record;
8401
8402 -- type T1 is new R [with null record];
8403 -- type T2 (X : positive) is new R (1, X) [with null record];
8404
8405 -- As explained in 6. above, T1 is rewritten as
8406 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8407 -- which makes the treatment for T1 and T2 identical.
8408
8409 -- What we want when inheriting S, is that references to D1 and D2 in R are
8410 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8411 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8412 -- with either discriminant references in the derived type or expressions.
8413 -- This replacement is achieved as follows: before inheriting R's
8414 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8415 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8416 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8417 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8418 -- by String (1 .. X).
8419
8420 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8421
8422 -- We explain here the rules governing private type extensions relevant to
8423 -- type derivation. These rules are explained on the following example:
8424
8425 -- type D [(...)] is new A [(...)] with private; <-- partial view
8426 -- type D [(...)] is new P [(...)] with null record; <-- full view
8427
8428 -- Type A is called the ancestor subtype of the private extension.
8429 -- Type P is the parent type of the full view of the private extension. It
8430 -- must be A or a type derived from A.
8431
8432 -- The rules concerning the discriminants of private type extensions are
8433 -- [7.3(10-13)]:
8434
8435 -- o If a private extension inherits known discriminants from the ancestor
8436 -- subtype, then the full view must also inherit its discriminants from
8437 -- the ancestor subtype and the parent subtype of the full view must be
8438 -- constrained if and only if the ancestor subtype is constrained.
8439
8440 -- o If a partial view has unknown discriminants, then the full view may
8441 -- define a definite or an indefinite subtype, with or without
8442 -- discriminants.
8443
8444 -- o If a partial view has neither known nor unknown discriminants, then
8445 -- the full view must define a definite subtype.
8446
8447 -- o If the ancestor subtype of a private extension has constrained
8448 -- discriminants, then the parent subtype of the full view must impose a
8449 -- statically matching constraint on those discriminants.
8450
8451 -- This means that only the following forms of private extensions are
8452 -- allowed:
8453
8454 -- type D is new A with private; <-- partial view
8455 -- type D is new P with null record; <-- full view
8456
8457 -- If A has no discriminants than P has no discriminants, otherwise P must
8458 -- inherit A's discriminants.
8459
8460 -- type D is new A (...) with private; <-- partial view
8461 -- type D is new P (:::) with null record; <-- full view
8462
8463 -- P must inherit A's discriminants and (...) and (:::) must statically
8464 -- match.
8465
8466 -- subtype A is R (...);
8467 -- type D is new A with private; <-- partial view
8468 -- type D is new P with null record; <-- full view
8469
8470 -- P must have inherited R's discriminants and must be derived from A or
8471 -- any of its subtypes.
8472
8473 -- type D (..) is new A with private; <-- partial view
8474 -- type D (..) is new P [(:::)] with null record; <-- full view
8475
8476 -- No specific constraints on P's discriminants or constraint (:::).
8477 -- Note that A can be unconstrained, but the parent subtype P must either
8478 -- be constrained or (:::) must be present.
8479
8480 -- type D (..) is new A [(...)] with private; <-- partial view
8481 -- type D (..) is new P [(:::)] with null record; <-- full view
8482
8483 -- P's constraints on A's discriminants must statically match those
8484 -- imposed by (...).
8485
8486 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8487
8488 -- The full view of a private extension is handled exactly as described
8489 -- above. The model chose for the private view of a private extension is
8490 -- the same for what concerns discriminants (i.e. they receive the same
8491 -- treatment as in the tagged case). However, the private view of the
8492 -- private extension always inherits the components of the parent base,
8493 -- without replacing any discriminant reference. Strictly speaking this is
8494 -- incorrect. However, Gigi never uses this view to generate code so this
8495 -- is a purely semantic issue. In theory, a set of transformations similar
8496 -- to those given in 5. and 6. above could be applied to private views of
8497 -- private extensions to have the same model of component inheritance as
8498 -- for non private extensions. However, this is not done because it would
8499 -- further complicate private type processing. Semantically speaking, this
8500 -- leaves us in an uncomfortable situation. As an example consider:
8501
8502 -- package Pack is
8503 -- type R (D : integer) is tagged record
8504 -- S : String (1 .. D);
8505 -- end record;
8506 -- procedure P (X : R);
8507 -- type T is new R (1) with private;
8508 -- private
8509 -- type T is new R (1) with null record;
8510 -- end;
8511
8512 -- This is transformed into:
8513
8514 -- package Pack is
8515 -- type R (D : integer) is tagged record
8516 -- S : String (1 .. D);
8517 -- end record;
8518 -- procedure P (X : R);
8519 -- type T is new R (1) with private;
8520 -- private
8521 -- type BaseT is new R with null record;
8522 -- subtype T is BaseT (1);
8523 -- end;
8524
8525 -- (strictly speaking the above is incorrect Ada)
8526
8527 -- From the semantic standpoint the private view of private extension T
8528 -- should be flagged as constrained since one can clearly have
8529 --
8530 -- Obj : T;
8531 --
8532 -- in a unit withing Pack. However, when deriving subprograms for the
8533 -- private view of private extension T, T must be seen as unconstrained
8534 -- since T has discriminants (this is a constraint of the current
8535 -- subprogram derivation model). Thus, when processing the private view of
8536 -- a private extension such as T, we first mark T as unconstrained, we
8537 -- process it, we perform program derivation and just before returning from
8538 -- Build_Derived_Record_Type we mark T as constrained.
8539
8540 -- ??? Are there are other uncomfortable cases that we will have to
8541 -- deal with.
8542
8543 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8544
8545 -- Types that are derived from a visible record type and have a private
8546 -- extension present other peculiarities. They behave mostly like private
8547 -- types, but if they have primitive operations defined, these will not
8548 -- have the proper signatures for further inheritance, because other
8549 -- primitive operations will use the implicit base that we define for
8550 -- private derivations below. This affect subprogram inheritance (see
8551 -- Derive_Subprograms for details). We also derive the implicit base from
8552 -- the base type of the full view, so that the implicit base is a record
8553 -- type and not another private type, This avoids infinite loops.
8554
8555 procedure Build_Derived_Record_Type
8556 (N : Node_Id;
8557 Parent_Type : Entity_Id;
8558 Derived_Type : Entity_Id;
8559 Derive_Subps : Boolean := True)
8560 is
8561 Discriminant_Specs : constant Boolean :=
8562 Present (Discriminant_Specifications (N));
8563 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8564 Loc : constant Source_Ptr := Sloc (N);
8565 Private_Extension : constant Boolean :=
8566 Nkind (N) = N_Private_Extension_Declaration;
8567 Assoc_List : Elist_Id;
8568 Constraint_Present : Boolean;
8569 Constrs : Elist_Id;
8570 Discrim : Entity_Id;
8571 Indic : Node_Id;
8572 Inherit_Discrims : Boolean := False;
8573 Last_Discrim : Entity_Id;
8574 New_Base : Entity_Id;
8575 New_Decl : Node_Id;
8576 New_Discrs : Elist_Id;
8577 New_Indic : Node_Id;
8578 Parent_Base : Entity_Id;
8579 Save_Etype : Entity_Id;
8580 Save_Discr_Constr : Elist_Id;
8581 Save_Next_Entity : Entity_Id;
8582 Type_Def : Node_Id;
8583
8584 Discs : Elist_Id := New_Elmt_List;
8585 -- An empty Discs list means that there were no constraints in the
8586 -- subtype indication or that there was an error processing it.
8587
8588 procedure Check_Generic_Ancestors;
8589 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8590 -- cannot be declared at a deeper level than its parent type is
8591 -- removed. The check on derivation within a generic body is also
8592 -- relaxed, but there's a restriction that a derived tagged type
8593 -- cannot be declared in a generic body if it's derived directly
8594 -- or indirectly from a formal type of that generic. This applies
8595 -- to progenitors as well.
8596
8597 -----------------------------
8598 -- Check_Generic_Ancestors --
8599 -----------------------------
8600
8601 procedure Check_Generic_Ancestors is
8602 Ancestor_Type : Entity_Id;
8603 Intf_List : List_Id;
8604 Intf_Name : Node_Id;
8605
8606 procedure Check_Ancestor;
8607 -- For parent and progenitors.
8608
8609 --------------------
8610 -- Check_Ancestor --
8611 --------------------
8612
8613 procedure Check_Ancestor is
8614 begin
8615 -- If the derived type does have a formal type as an ancestor
8616 -- then it's an error if the derived type is declared within
8617 -- the body of the generic unit that declares the formal type
8618 -- in its generic formal part. It's sufficient to check whether
8619 -- the ancestor type is declared inside the same generic body
8620 -- as the derived type (such as within a nested generic spec),
8621 -- in which case the derivation is legal. If the formal type is
8622 -- declared outside of that generic body, then it's certain
8623 -- that the derived type is declared within the generic body
8624 -- of the generic unit declaring the formal type.
8625
8626 if Is_Generic_Type (Ancestor_Type)
8627 and then Enclosing_Generic_Body (Ancestor_Type) /=
8628 Enclosing_Generic_Body (Derived_Type)
8629 then
8630 Error_Msg_NE
8631 ("ancestor type& is formal type of enclosing"
8632 & " generic unit (RM 3.9.1 (4/2))",
8633 Indic, Ancestor_Type);
8634 end if;
8635 end Check_Ancestor;
8636
8637 begin
8638 if Nkind (N) = N_Private_Extension_Declaration then
8639 Intf_List := Interface_List (N);
8640 else
8641 Intf_List := Interface_List (Type_Definition (N));
8642 end if;
8643
8644 if Present (Enclosing_Generic_Body (Derived_Type)) then
8645 Ancestor_Type := Parent_Type;
8646
8647 while not Is_Generic_Type (Ancestor_Type)
8648 and then Etype (Ancestor_Type) /= Ancestor_Type
8649 loop
8650 Ancestor_Type := Etype (Ancestor_Type);
8651 end loop;
8652
8653 Check_Ancestor;
8654
8655 if Present (Intf_List) then
8656 Intf_Name := First (Intf_List);
8657 while Present (Intf_Name) loop
8658 Ancestor_Type := Entity (Intf_Name);
8659 Check_Ancestor;
8660 Next (Intf_Name);
8661 end loop;
8662 end if;
8663 end if;
8664 end Check_Generic_Ancestors;
8665
8666 -- Start of processing for Build_Derived_Record_Type
8667
8668 begin
8669 if Ekind (Parent_Type) = E_Record_Type_With_Private
8670 and then Present (Full_View (Parent_Type))
8671 and then Has_Discriminants (Parent_Type)
8672 then
8673 Parent_Base := Base_Type (Full_View (Parent_Type));
8674 else
8675 Parent_Base := Base_Type (Parent_Type);
8676 end if;
8677
8678 -- If the parent type is declared as a subtype of another private
8679 -- type with inherited discriminants, its generated base type is
8680 -- itself a record subtype. To further inherit the constraint we
8681 -- need to use its own base to have an unconstrained type on which
8682 -- to apply the inherited constraint.
8683
8684 if Ekind (Parent_Base) = E_Record_Subtype then
8685 Parent_Base := Base_Type (Parent_Base);
8686 end if;
8687
8688 -- AI05-0115: if this is a derivation from a private type in some
8689 -- other scope that may lead to invisible components for the derived
8690 -- type, mark it accordingly.
8691
8692 if Is_Private_Type (Parent_Type) then
8693 if Scope (Parent_Base) = Scope (Derived_Type) then
8694 null;
8695
8696 elsif In_Open_Scopes (Scope (Parent_Base))
8697 and then In_Private_Part (Scope (Parent_Base))
8698 then
8699 null;
8700
8701 else
8702 Set_Has_Private_Ancestor (Derived_Type);
8703 end if;
8704
8705 else
8706 Set_Has_Private_Ancestor
8707 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8708 end if;
8709
8710 -- Before we start the previously documented transformations, here is
8711 -- little fix for size and alignment of tagged types. Normally when we
8712 -- derive type D from type P, we copy the size and alignment of P as the
8713 -- default for D, and in the absence of explicit representation clauses
8714 -- for D, the size and alignment are indeed the same as the parent.
8715
8716 -- But this is wrong for tagged types, since fields may be added, and
8717 -- the default size may need to be larger, and the default alignment may
8718 -- need to be larger.
8719
8720 -- We therefore reset the size and alignment fields in the tagged case.
8721 -- Note that the size and alignment will in any case be at least as
8722 -- large as the parent type (since the derived type has a copy of the
8723 -- parent type in the _parent field)
8724
8725 -- The type is also marked as being tagged here, which is needed when
8726 -- processing components with a self-referential anonymous access type
8727 -- in the call to Check_Anonymous_Access_Components below. Note that
8728 -- this flag is also set later on for completeness.
8729
8730 if Is_Tagged then
8731 Set_Is_Tagged_Type (Derived_Type);
8732 Init_Size_Align (Derived_Type);
8733 end if;
8734
8735 -- STEP 0a: figure out what kind of derived type declaration we have
8736
8737 if Private_Extension then
8738 Type_Def := N;
8739 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8740 Set_Default_SSO (Derived_Type);
8741 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8742
8743 else
8744 Type_Def := Type_Definition (N);
8745
8746 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8747 -- Parent_Base can be a private type or private extension. However,
8748 -- for tagged types with an extension the newly added fields are
8749 -- visible and hence the Derived_Type is always an E_Record_Type.
8750 -- (except that the parent may have its own private fields).
8751 -- For untagged types we preserve the Ekind of the Parent_Base.
8752
8753 if Present (Record_Extension_Part (Type_Def)) then
8754 Set_Ekind (Derived_Type, E_Record_Type);
8755 Set_Default_SSO (Derived_Type);
8756 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8757
8758 -- Create internal access types for components with anonymous
8759 -- access types.
8760
8761 if Ada_Version >= Ada_2005 then
8762 Check_Anonymous_Access_Components
8763 (N, Derived_Type, Derived_Type,
8764 Component_List (Record_Extension_Part (Type_Def)));
8765 end if;
8766
8767 else
8768 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8769 end if;
8770 end if;
8771
8772 -- Indic can either be an N_Identifier if the subtype indication
8773 -- contains no constraint or an N_Subtype_Indication if the subtype
8774 -- indication has a constraint. In either case it can include an
8775 -- interface list.
8776
8777 Indic := Subtype_Indication (Type_Def);
8778 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8779
8780 -- Check that the type has visible discriminants. The type may be
8781 -- a private type with unknown discriminants whose full view has
8782 -- discriminants which are invisible.
8783
8784 if Constraint_Present then
8785 if not Has_Discriminants (Parent_Base)
8786 or else
8787 (Has_Unknown_Discriminants (Parent_Base)
8788 and then Is_Private_Type (Parent_Base))
8789 then
8790 Error_Msg_N
8791 ("invalid constraint: type has no discriminant",
8792 Constraint (Indic));
8793
8794 Constraint_Present := False;
8795 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8796
8797 elsif Is_Constrained (Parent_Type) then
8798 Error_Msg_N
8799 ("invalid constraint: parent type is already constrained",
8800 Constraint (Indic));
8801
8802 Constraint_Present := False;
8803 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8804 end if;
8805 end if;
8806
8807 -- STEP 0b: If needed, apply transformation given in point 5. above
8808
8809 if not Private_Extension
8810 and then Has_Discriminants (Parent_Type)
8811 and then not Discriminant_Specs
8812 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8813 then
8814 -- First, we must analyze the constraint (see comment in point 5.)
8815 -- The constraint may come from the subtype indication of the full
8816 -- declaration.
8817
8818 if Constraint_Present then
8819 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8820
8821 -- If there is no explicit constraint, there might be one that is
8822 -- inherited from a constrained parent type. In that case verify that
8823 -- it conforms to the constraint in the partial view. In perverse
8824 -- cases the parent subtypes of the partial and full view can have
8825 -- different constraints.
8826
8827 elsif Present (Stored_Constraint (Parent_Type)) then
8828 New_Discrs := Stored_Constraint (Parent_Type);
8829
8830 else
8831 New_Discrs := No_Elist;
8832 end if;
8833
8834 if Has_Discriminants (Derived_Type)
8835 and then Has_Private_Declaration (Derived_Type)
8836 and then Present (Discriminant_Constraint (Derived_Type))
8837 and then Present (New_Discrs)
8838 then
8839 -- Verify that constraints of the full view statically match
8840 -- those given in the partial view.
8841
8842 declare
8843 C1, C2 : Elmt_Id;
8844
8845 begin
8846 C1 := First_Elmt (New_Discrs);
8847 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8848 while Present (C1) and then Present (C2) loop
8849 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8850 or else
8851 (Is_OK_Static_Expression (Node (C1))
8852 and then Is_OK_Static_Expression (Node (C2))
8853 and then
8854 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8855 then
8856 null;
8857
8858 else
8859 if Constraint_Present then
8860 Error_Msg_N
8861 ("constraint not conformant to previous declaration",
8862 Node (C1));
8863 else
8864 Error_Msg_N
8865 ("constraint of full view is incompatible "
8866 & "with partial view", N);
8867 end if;
8868 end if;
8869
8870 Next_Elmt (C1);
8871 Next_Elmt (C2);
8872 end loop;
8873 end;
8874 end if;
8875
8876 -- Insert and analyze the declaration for the unconstrained base type
8877
8878 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8879
8880 New_Decl :=
8881 Make_Full_Type_Declaration (Loc,
8882 Defining_Identifier => New_Base,
8883 Type_Definition =>
8884 Make_Derived_Type_Definition (Loc,
8885 Abstract_Present => Abstract_Present (Type_Def),
8886 Limited_Present => Limited_Present (Type_Def),
8887 Subtype_Indication =>
8888 New_Occurrence_Of (Parent_Base, Loc),
8889 Record_Extension_Part =>
8890 Relocate_Node (Record_Extension_Part (Type_Def)),
8891 Interface_List => Interface_List (Type_Def)));
8892
8893 Set_Parent (New_Decl, Parent (N));
8894 Mark_Rewrite_Insertion (New_Decl);
8895 Insert_Before (N, New_Decl);
8896
8897 -- In the extension case, make sure ancestor is frozen appropriately
8898 -- (see also non-discriminated case below).
8899
8900 if Present (Record_Extension_Part (Type_Def))
8901 or else Is_Interface (Parent_Base)
8902 then
8903 Freeze_Before (New_Decl, Parent_Type);
8904 end if;
8905
8906 -- Note that this call passes False for the Derive_Subps parameter
8907 -- because subprogram derivation is deferred until after creating
8908 -- the subtype (see below).
8909
8910 Build_Derived_Type
8911 (New_Decl, Parent_Base, New_Base,
8912 Is_Completion => False, Derive_Subps => False);
8913
8914 -- ??? This needs re-examination to determine whether the
8915 -- above call can simply be replaced by a call to Analyze.
8916
8917 Set_Analyzed (New_Decl);
8918
8919 -- Insert and analyze the declaration for the constrained subtype
8920
8921 if Constraint_Present then
8922 New_Indic :=
8923 Make_Subtype_Indication (Loc,
8924 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8925 Constraint => Relocate_Node (Constraint (Indic)));
8926
8927 else
8928 declare
8929 Constr_List : constant List_Id := New_List;
8930 C : Elmt_Id;
8931 Expr : Node_Id;
8932
8933 begin
8934 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8935 while Present (C) loop
8936 Expr := Node (C);
8937
8938 -- It is safe here to call New_Copy_Tree since we called
8939 -- Force_Evaluation on each constraint previously
8940 -- in Build_Discriminant_Constraints.
8941
8942 Append (New_Copy_Tree (Expr), To => Constr_List);
8943
8944 Next_Elmt (C);
8945 end loop;
8946
8947 New_Indic :=
8948 Make_Subtype_Indication (Loc,
8949 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8950 Constraint =>
8951 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8952 end;
8953 end if;
8954
8955 Rewrite (N,
8956 Make_Subtype_Declaration (Loc,
8957 Defining_Identifier => Derived_Type,
8958 Subtype_Indication => New_Indic));
8959
8960 Analyze (N);
8961
8962 -- Derivation of subprograms must be delayed until the full subtype
8963 -- has been established, to ensure proper overriding of subprograms
8964 -- inherited by full types. If the derivations occurred as part of
8965 -- the call to Build_Derived_Type above, then the check for type
8966 -- conformance would fail because earlier primitive subprograms
8967 -- could still refer to the full type prior the change to the new
8968 -- subtype and hence would not match the new base type created here.
8969 -- Subprograms are not derived, however, when Derive_Subps is False
8970 -- (since otherwise there could be redundant derivations).
8971
8972 if Derive_Subps then
8973 Derive_Subprograms (Parent_Type, Derived_Type);
8974 end if;
8975
8976 -- For tagged types the Discriminant_Constraint of the new base itype
8977 -- is inherited from the first subtype so that no subtype conformance
8978 -- problem arise when the first subtype overrides primitive
8979 -- operations inherited by the implicit base type.
8980
8981 if Is_Tagged then
8982 Set_Discriminant_Constraint
8983 (New_Base, Discriminant_Constraint (Derived_Type));
8984 end if;
8985
8986 return;
8987 end if;
8988
8989 -- If we get here Derived_Type will have no discriminants or it will be
8990 -- a discriminated unconstrained base type.
8991
8992 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8993
8994 if Is_Tagged then
8995
8996 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8997 -- The declaration of a specific descendant of an interface type
8998 -- freezes the interface type (RM 13.14).
8999
9000 if not Private_Extension or else Is_Interface (Parent_Base) then
9001 Freeze_Before (N, Parent_Type);
9002 end if;
9003
9004 if Ada_Version >= Ada_2005 then
9005 Check_Generic_Ancestors;
9006
9007 elsif Type_Access_Level (Derived_Type) /=
9008 Type_Access_Level (Parent_Type)
9009 and then not Is_Generic_Type (Derived_Type)
9010 then
9011 if Is_Controlled (Parent_Type) then
9012 Error_Msg_N
9013 ("controlled type must be declared at the library level",
9014 Indic);
9015 else
9016 Error_Msg_N
9017 ("type extension at deeper accessibility level than parent",
9018 Indic);
9019 end if;
9020
9021 else
9022 declare
9023 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9024 begin
9025 if Present (GB)
9026 and then GB /= Enclosing_Generic_Body (Parent_Base)
9027 then
9028 Error_Msg_NE
9029 ("parent type of& must not be outside generic body"
9030 & " (RM 3.9.1(4))",
9031 Indic, Derived_Type);
9032 end if;
9033 end;
9034 end if;
9035 end if;
9036
9037 -- Ada 2005 (AI-251)
9038
9039 if Ada_Version >= Ada_2005 and then Is_Tagged then
9040
9041 -- "The declaration of a specific descendant of an interface type
9042 -- freezes the interface type" (RM 13.14).
9043
9044 declare
9045 Iface : Node_Id;
9046 begin
9047 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9048 Iface := First (Interface_List (Type_Def));
9049 while Present (Iface) loop
9050 Freeze_Before (N, Etype (Iface));
9051 Next (Iface);
9052 end loop;
9053 end if;
9054 end;
9055 end if;
9056
9057 -- STEP 1b : preliminary cleanup of the full view of private types
9058
9059 -- If the type is already marked as having discriminants, then it's the
9060 -- completion of a private type or private extension and we need to
9061 -- retain the discriminants from the partial view if the current
9062 -- declaration has Discriminant_Specifications so that we can verify
9063 -- conformance. However, we must remove any existing components that
9064 -- were inherited from the parent (and attached in Copy_And_Swap)
9065 -- because the full type inherits all appropriate components anyway, and
9066 -- we do not want the partial view's components interfering.
9067
9068 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9069 Discrim := First_Discriminant (Derived_Type);
9070 loop
9071 Last_Discrim := Discrim;
9072 Next_Discriminant (Discrim);
9073 exit when No (Discrim);
9074 end loop;
9075
9076 Set_Last_Entity (Derived_Type, Last_Discrim);
9077
9078 -- In all other cases wipe out the list of inherited components (even
9079 -- inherited discriminants), it will be properly rebuilt here.
9080
9081 else
9082 Set_First_Entity (Derived_Type, Empty);
9083 Set_Last_Entity (Derived_Type, Empty);
9084 end if;
9085
9086 -- STEP 1c: Initialize some flags for the Derived_Type
9087
9088 -- The following flags must be initialized here so that
9089 -- Process_Discriminants can check that discriminants of tagged types do
9090 -- not have a default initial value and that access discriminants are
9091 -- only specified for limited records. For completeness, these flags are
9092 -- also initialized along with all the other flags below.
9093
9094 -- AI-419: Limitedness is not inherited from an interface parent, so to
9095 -- be limited in that case the type must be explicitly declared as
9096 -- limited. However, task and protected interfaces are always limited.
9097
9098 if Limited_Present (Type_Def) then
9099 Set_Is_Limited_Record (Derived_Type);
9100
9101 elsif Is_Limited_Record (Parent_Type)
9102 or else (Present (Full_View (Parent_Type))
9103 and then Is_Limited_Record (Full_View (Parent_Type)))
9104 then
9105 if not Is_Interface (Parent_Type)
9106 or else Is_Synchronized_Interface (Parent_Type)
9107 or else Is_Protected_Interface (Parent_Type)
9108 or else Is_Task_Interface (Parent_Type)
9109 then
9110 Set_Is_Limited_Record (Derived_Type);
9111 end if;
9112 end if;
9113
9114 -- STEP 2a: process discriminants of derived type if any
9115
9116 Push_Scope (Derived_Type);
9117
9118 if Discriminant_Specs then
9119 Set_Has_Unknown_Discriminants (Derived_Type, False);
9120
9121 -- The following call initializes fields Has_Discriminants and
9122 -- Discriminant_Constraint, unless we are processing the completion
9123 -- of a private type declaration.
9124
9125 Check_Or_Process_Discriminants (N, Derived_Type);
9126
9127 -- For untagged types, the constraint on the Parent_Type must be
9128 -- present and is used to rename the discriminants.
9129
9130 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9131 Error_Msg_N ("untagged parent must have discriminants", Indic);
9132
9133 elsif not Is_Tagged and then not Constraint_Present then
9134 Error_Msg_N
9135 ("discriminant constraint needed for derived untagged records",
9136 Indic);
9137
9138 -- Otherwise the parent subtype must be constrained unless we have a
9139 -- private extension.
9140
9141 elsif not Constraint_Present
9142 and then not Private_Extension
9143 and then not Is_Constrained (Parent_Type)
9144 then
9145 Error_Msg_N
9146 ("unconstrained type not allowed in this context", Indic);
9147
9148 elsif Constraint_Present then
9149 -- The following call sets the field Corresponding_Discriminant
9150 -- for the discriminants in the Derived_Type.
9151
9152 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9153
9154 -- For untagged types all new discriminants must rename
9155 -- discriminants in the parent. For private extensions new
9156 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9157
9158 Discrim := First_Discriminant (Derived_Type);
9159 while Present (Discrim) loop
9160 if not Is_Tagged
9161 and then No (Corresponding_Discriminant (Discrim))
9162 then
9163 Error_Msg_N
9164 ("new discriminants must constrain old ones", Discrim);
9165
9166 elsif Private_Extension
9167 and then Present (Corresponding_Discriminant (Discrim))
9168 then
9169 Error_Msg_N
9170 ("only static constraints allowed for parent"
9171 & " discriminants in the partial view", Indic);
9172 exit;
9173 end if;
9174
9175 -- If a new discriminant is used in the constraint, then its
9176 -- subtype must be statically compatible with the parent
9177 -- discriminant's subtype (3.7(15)).
9178
9179 -- However, if the record contains an array constrained by
9180 -- the discriminant but with some different bound, the compiler
9181 -- tries to create a smaller range for the discriminant type.
9182 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9183 -- the discriminant type is a scalar type, the check must use
9184 -- the original discriminant type in the parent declaration.
9185
9186 declare
9187 Corr_Disc : constant Entity_Id :=
9188 Corresponding_Discriminant (Discrim);
9189 Disc_Type : constant Entity_Id := Etype (Discrim);
9190 Corr_Type : Entity_Id;
9191
9192 begin
9193 if Present (Corr_Disc) then
9194 if Is_Scalar_Type (Disc_Type) then
9195 Corr_Type :=
9196 Entity (Discriminant_Type (Parent (Corr_Disc)));
9197 else
9198 Corr_Type := Etype (Corr_Disc);
9199 end if;
9200
9201 if not
9202 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9203 then
9204 Error_Msg_N
9205 ("subtype must be compatible "
9206 & "with parent discriminant",
9207 Discrim);
9208 end if;
9209 end if;
9210 end;
9211
9212 Next_Discriminant (Discrim);
9213 end loop;
9214
9215 -- Check whether the constraints of the full view statically
9216 -- match those imposed by the parent subtype [7.3(13)].
9217
9218 if Present (Stored_Constraint (Derived_Type)) then
9219 declare
9220 C1, C2 : Elmt_Id;
9221
9222 begin
9223 C1 := First_Elmt (Discs);
9224 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9225 while Present (C1) and then Present (C2) loop
9226 if not
9227 Fully_Conformant_Expressions (Node (C1), Node (C2))
9228 then
9229 Error_Msg_N
9230 ("not conformant with previous declaration",
9231 Node (C1));
9232 end if;
9233
9234 Next_Elmt (C1);
9235 Next_Elmt (C2);
9236 end loop;
9237 end;
9238 end if;
9239 end if;
9240
9241 -- STEP 2b: No new discriminants, inherit discriminants if any
9242
9243 else
9244 if Private_Extension then
9245 Set_Has_Unknown_Discriminants
9246 (Derived_Type,
9247 Has_Unknown_Discriminants (Parent_Type)
9248 or else Unknown_Discriminants_Present (N));
9249
9250 -- The partial view of the parent may have unknown discriminants,
9251 -- but if the full view has discriminants and the parent type is
9252 -- in scope they must be inherited.
9253
9254 elsif Has_Unknown_Discriminants (Parent_Type)
9255 and then
9256 (not Has_Discriminants (Parent_Type)
9257 or else not In_Open_Scopes (Scope (Parent_Base)))
9258 then
9259 Set_Has_Unknown_Discriminants (Derived_Type);
9260 end if;
9261
9262 if not Has_Unknown_Discriminants (Derived_Type)
9263 and then not Has_Unknown_Discriminants (Parent_Base)
9264 and then Has_Discriminants (Parent_Type)
9265 then
9266 Inherit_Discrims := True;
9267 Set_Has_Discriminants
9268 (Derived_Type, True);
9269 Set_Discriminant_Constraint
9270 (Derived_Type, Discriminant_Constraint (Parent_Base));
9271 end if;
9272
9273 -- The following test is true for private types (remember
9274 -- transformation 5. is not applied to those) and in an error
9275 -- situation.
9276
9277 if Constraint_Present then
9278 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9279 end if;
9280
9281 -- For now mark a new derived type as constrained only if it has no
9282 -- discriminants. At the end of Build_Derived_Record_Type we properly
9283 -- set this flag in the case of private extensions. See comments in
9284 -- point 9. just before body of Build_Derived_Record_Type.
9285
9286 Set_Is_Constrained
9287 (Derived_Type,
9288 not (Inherit_Discrims
9289 or else Has_Unknown_Discriminants (Derived_Type)));
9290 end if;
9291
9292 -- STEP 3: initialize fields of derived type
9293
9294 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9295 Set_Stored_Constraint (Derived_Type, No_Elist);
9296
9297 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9298 -- but cannot be interfaces
9299
9300 if not Private_Extension
9301 and then Ekind (Derived_Type) /= E_Private_Type
9302 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9303 then
9304 if Interface_Present (Type_Def) then
9305 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9306 end if;
9307
9308 Set_Interfaces (Derived_Type, No_Elist);
9309 end if;
9310
9311 -- Fields inherited from the Parent_Type
9312
9313 Set_Has_Specified_Layout
9314 (Derived_Type, Has_Specified_Layout (Parent_Type));
9315 Set_Is_Limited_Composite
9316 (Derived_Type, Is_Limited_Composite (Parent_Type));
9317 Set_Is_Private_Composite
9318 (Derived_Type, Is_Private_Composite (Parent_Type));
9319
9320 if Is_Tagged_Type (Parent_Type) then
9321 Set_No_Tagged_Streams_Pragma
9322 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9323 end if;
9324
9325 -- Fields inherited from the Parent_Base
9326
9327 Set_Has_Controlled_Component
9328 (Derived_Type, Has_Controlled_Component (Parent_Base));
9329 Set_Has_Non_Standard_Rep
9330 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9331 Set_Has_Primitive_Operations
9332 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9333
9334 -- Set fields for private derived types
9335
9336 if Is_Private_Type (Derived_Type) then
9337 Set_Depends_On_Private (Derived_Type, True);
9338 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9339 end if;
9340
9341 -- Inherit fields for non-private types. If this is the completion of a
9342 -- derivation from a private type, the parent itself is private and the
9343 -- attributes come from its full view, which must be present.
9344
9345 if Is_Record_Type (Derived_Type) then
9346 declare
9347 Parent_Full : Entity_Id;
9348
9349 begin
9350 if Is_Private_Type (Parent_Base)
9351 and then not Is_Record_Type (Parent_Base)
9352 then
9353 Parent_Full := Full_View (Parent_Base);
9354 else
9355 Parent_Full := Parent_Base;
9356 end if;
9357
9358 Set_Component_Alignment
9359 (Derived_Type, Component_Alignment (Parent_Full));
9360 Set_C_Pass_By_Copy
9361 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9362 Set_Has_Complex_Representation
9363 (Derived_Type, Has_Complex_Representation (Parent_Full));
9364
9365 -- For untagged types, inherit the layout by default to avoid
9366 -- costly changes of representation for type conversions.
9367
9368 if not Is_Tagged then
9369 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9370 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9371 end if;
9372 end;
9373 end if;
9374
9375 -- Set fields for tagged types
9376
9377 if Is_Tagged then
9378 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9379
9380 -- All tagged types defined in Ada.Finalization are controlled
9381
9382 if Chars (Scope (Derived_Type)) = Name_Finalization
9383 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9384 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9385 then
9386 Set_Is_Controlled_Active (Derived_Type);
9387 else
9388 Set_Is_Controlled_Active
9389 (Derived_Type, Is_Controlled_Active (Parent_Base));
9390 end if;
9391
9392 -- Minor optimization: there is no need to generate the class-wide
9393 -- entity associated with an underlying record view.
9394
9395 if not Is_Underlying_Record_View (Derived_Type) then
9396 Make_Class_Wide_Type (Derived_Type);
9397 end if;
9398
9399 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9400
9401 if Has_Discriminants (Derived_Type)
9402 and then Constraint_Present
9403 then
9404 Set_Stored_Constraint
9405 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9406 end if;
9407
9408 if Ada_Version >= Ada_2005 then
9409 declare
9410 Ifaces_List : Elist_Id;
9411
9412 begin
9413 -- Checks rules 3.9.4 (13/2 and 14/2)
9414
9415 if Comes_From_Source (Derived_Type)
9416 and then not Is_Private_Type (Derived_Type)
9417 and then Is_Interface (Parent_Type)
9418 and then not Is_Interface (Derived_Type)
9419 then
9420 if Is_Task_Interface (Parent_Type) then
9421 Error_Msg_N
9422 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9423 Derived_Type);
9424
9425 elsif Is_Protected_Interface (Parent_Type) then
9426 Error_Msg_N
9427 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9428 Derived_Type);
9429 end if;
9430 end if;
9431
9432 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9433
9434 Check_Interfaces (N, Type_Def);
9435
9436 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9437 -- not already in the parents.
9438
9439 Collect_Interfaces
9440 (T => Derived_Type,
9441 Ifaces_List => Ifaces_List,
9442 Exclude_Parents => True);
9443
9444 Set_Interfaces (Derived_Type, Ifaces_List);
9445
9446 -- If the derived type is the anonymous type created for
9447 -- a declaration whose parent has a constraint, propagate
9448 -- the interface list to the source type. This must be done
9449 -- prior to the completion of the analysis of the source type
9450 -- because the components in the extension may contain current
9451 -- instances whose legality depends on some ancestor.
9452
9453 if Is_Itype (Derived_Type) then
9454 declare
9455 Def : constant Node_Id :=
9456 Associated_Node_For_Itype (Derived_Type);
9457 begin
9458 if Present (Def)
9459 and then Nkind (Def) = N_Full_Type_Declaration
9460 then
9461 Set_Interfaces
9462 (Defining_Identifier (Def), Ifaces_List);
9463 end if;
9464 end;
9465 end if;
9466
9467 -- A type extension is automatically Ghost when one of its
9468 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9469 -- also inherited when the parent type is Ghost, but this is
9470 -- done in Build_Derived_Type as the mechanism also handles
9471 -- untagged derivations.
9472
9473 if Implements_Ghost_Interface (Derived_Type) then
9474 Set_Is_Ghost_Entity (Derived_Type);
9475 end if;
9476 end;
9477 end if;
9478 end if;
9479
9480 -- STEP 4: Inherit components from the parent base and constrain them.
9481 -- Apply the second transformation described in point 6. above.
9482
9483 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9484 or else not Has_Discriminants (Parent_Type)
9485 or else not Is_Constrained (Parent_Type)
9486 then
9487 Constrs := Discs;
9488 else
9489 Constrs := Discriminant_Constraint (Parent_Type);
9490 end if;
9491
9492 Assoc_List :=
9493 Inherit_Components
9494 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9495
9496 -- STEP 5a: Copy the parent record declaration for untagged types
9497
9498 Set_Has_Implicit_Dereference
9499 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9500
9501 if not Is_Tagged then
9502
9503 -- Discriminant_Constraint (Derived_Type) has been properly
9504 -- constructed. Save it and temporarily set it to Empty because we
9505 -- do not want the call to New_Copy_Tree below to mess this list.
9506
9507 if Has_Discriminants (Derived_Type) then
9508 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9509 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9510 else
9511 Save_Discr_Constr := No_Elist;
9512 end if;
9513
9514 -- Save the Etype field of Derived_Type. It is correctly set now,
9515 -- but the call to New_Copy tree may remap it to point to itself,
9516 -- which is not what we want. Ditto for the Next_Entity field.
9517
9518 Save_Etype := Etype (Derived_Type);
9519 Save_Next_Entity := Next_Entity (Derived_Type);
9520
9521 -- Assoc_List maps all stored discriminants in the Parent_Base to
9522 -- stored discriminants in the Derived_Type. It is fundamental that
9523 -- no types or itypes with discriminants other than the stored
9524 -- discriminants appear in the entities declared inside
9525 -- Derived_Type, since the back end cannot deal with it.
9526
9527 New_Decl :=
9528 New_Copy_Tree
9529 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9530 Copy_Dimensions_Of_Components (Derived_Type);
9531
9532 -- Restore the fields saved prior to the New_Copy_Tree call
9533 -- and compute the stored constraint.
9534
9535 Set_Etype (Derived_Type, Save_Etype);
9536 Link_Entities (Derived_Type, Save_Next_Entity);
9537
9538 if Has_Discriminants (Derived_Type) then
9539 Set_Discriminant_Constraint
9540 (Derived_Type, Save_Discr_Constr);
9541 Set_Stored_Constraint
9542 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9543
9544 Replace_Discriminants (Derived_Type, New_Decl);
9545 end if;
9546
9547 -- Insert the new derived type declaration
9548
9549 Rewrite (N, New_Decl);
9550
9551 -- STEP 5b: Complete the processing for record extensions in generics
9552
9553 -- There is no completion for record extensions declared in the
9554 -- parameter part of a generic, so we need to complete processing for
9555 -- these generic record extensions here. The Record_Type_Definition call
9556 -- will change the Ekind of the components from E_Void to E_Component.
9557
9558 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9559 Record_Type_Definition (Empty, Derived_Type);
9560
9561 -- STEP 5c: Process the record extension for non private tagged types
9562
9563 elsif not Private_Extension then
9564 Expand_Record_Extension (Derived_Type, Type_Def);
9565
9566 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9567 -- implemented interfaces if we are in expansion mode
9568
9569 if Expander_Active
9570 and then Has_Interfaces (Derived_Type)
9571 then
9572 Add_Interface_Tag_Components (N, Derived_Type);
9573 end if;
9574
9575 -- Analyze the record extension
9576
9577 Record_Type_Definition
9578 (Record_Extension_Part (Type_Def), Derived_Type);
9579 end if;
9580
9581 End_Scope;
9582
9583 -- Nothing else to do if there is an error in the derivation.
9584 -- An unusual case: the full view may be derived from a type in an
9585 -- instance, when the partial view was used illegally as an actual
9586 -- in that instance, leading to a circular definition.
9587
9588 if Etype (Derived_Type) = Any_Type
9589 or else Etype (Parent_Type) = Derived_Type
9590 then
9591 return;
9592 end if;
9593
9594 -- Set delayed freeze and then derive subprograms, we need to do
9595 -- this in this order so that derived subprograms inherit the
9596 -- derived freeze if necessary.
9597
9598 Set_Has_Delayed_Freeze (Derived_Type);
9599
9600 if Derive_Subps then
9601 Derive_Subprograms (Parent_Type, Derived_Type);
9602 end if;
9603
9604 -- If we have a private extension which defines a constrained derived
9605 -- type mark as constrained here after we have derived subprograms. See
9606 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9607
9608 if Private_Extension and then Inherit_Discrims then
9609 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9610 Set_Is_Constrained (Derived_Type, True);
9611 Set_Discriminant_Constraint (Derived_Type, Discs);
9612
9613 elsif Is_Constrained (Parent_Type) then
9614 Set_Is_Constrained
9615 (Derived_Type, True);
9616 Set_Discriminant_Constraint
9617 (Derived_Type, Discriminant_Constraint (Parent_Type));
9618 end if;
9619 end if;
9620
9621 -- Update the class-wide type, which shares the now-completed entity
9622 -- list with its specific type. In case of underlying record views,
9623 -- we do not generate the corresponding class wide entity.
9624
9625 if Is_Tagged
9626 and then not Is_Underlying_Record_View (Derived_Type)
9627 then
9628 Set_First_Entity
9629 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9630 Set_Last_Entity
9631 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9632 end if;
9633
9634 Check_Function_Writable_Actuals (N);
9635 end Build_Derived_Record_Type;
9636
9637 ------------------------
9638 -- Build_Derived_Type --
9639 ------------------------
9640
9641 procedure Build_Derived_Type
9642 (N : Node_Id;
9643 Parent_Type : Entity_Id;
9644 Derived_Type : Entity_Id;
9645 Is_Completion : Boolean;
9646 Derive_Subps : Boolean := True)
9647 is
9648 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9649
9650 begin
9651 -- Set common attributes
9652
9653 Set_Scope (Derived_Type, Current_Scope);
9654 Set_Etype (Derived_Type, Parent_Base);
9655 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9656 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9657
9658 Set_Size_Info (Derived_Type, Parent_Type);
9659 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9660
9661 Set_Is_Controlled_Active
9662 (Derived_Type, Is_Controlled_Active (Parent_Type));
9663
9664 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9665 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9666 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9667
9668 if Is_Tagged_Type (Derived_Type) then
9669 Set_No_Tagged_Streams_Pragma
9670 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9671 end if;
9672
9673 -- If the parent has primitive routines and may have not-seen-yet aspect
9674 -- specifications (e.g., a Pack pragma), then set the derived type link
9675 -- in order to later diagnose "early derivation" issues. If in different
9676 -- compilation units, then "early derivation" cannot be an issue (and we
9677 -- don't like interunit references that go in the opposite direction of
9678 -- semantic dependencies).
9679
9680 if Has_Primitive_Operations (Parent_Type)
9681 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9682 Enclosing_Comp_Unit_Node (Derived_Type)
9683 then
9684 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9685 end if;
9686
9687 -- If the parent type is a private subtype, the convention on the base
9688 -- type may be set in the private part, and not propagated to the
9689 -- subtype until later, so we obtain the convention from the base type.
9690
9691 Set_Convention (Derived_Type, Convention (Parent_Base));
9692
9693 -- Set SSO default for record or array type
9694
9695 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9696 and then Is_Base_Type (Derived_Type)
9697 then
9698 Set_Default_SSO (Derived_Type);
9699 end if;
9700
9701 -- A derived type inherits the Default_Initial_Condition pragma coming
9702 -- from any parent type within the derivation chain.
9703
9704 if Has_DIC (Parent_Type) then
9705 Set_Has_Inherited_DIC (Derived_Type);
9706 end if;
9707
9708 -- A derived type inherits any class-wide invariants coming from a
9709 -- parent type or an interface. Note that the invariant procedure of
9710 -- the parent type should not be inherited because the derived type may
9711 -- define invariants of its own.
9712
9713 if not Is_Interface (Derived_Type) then
9714 if Has_Inherited_Invariants (Parent_Type)
9715 or else Has_Inheritable_Invariants (Parent_Type)
9716 then
9717 Set_Has_Inherited_Invariants (Derived_Type);
9718
9719 elsif Is_Concurrent_Type (Derived_Type)
9720 or else Is_Tagged_Type (Derived_Type)
9721 then
9722 declare
9723 Iface : Entity_Id;
9724 Ifaces : Elist_Id;
9725 Iface_Elmt : Elmt_Id;
9726
9727 begin
9728 Collect_Interfaces
9729 (T => Derived_Type,
9730 Ifaces_List => Ifaces,
9731 Exclude_Parents => True);
9732
9733 if Present (Ifaces) then
9734 Iface_Elmt := First_Elmt (Ifaces);
9735 while Present (Iface_Elmt) loop
9736 Iface := Node (Iface_Elmt);
9737
9738 if Has_Inheritable_Invariants (Iface) then
9739 Set_Has_Inherited_Invariants (Derived_Type);
9740 exit;
9741 end if;
9742
9743 Next_Elmt (Iface_Elmt);
9744 end loop;
9745 end if;
9746 end;
9747 end if;
9748 end if;
9749
9750 -- We similarly inherit predicates. Note that for scalar derived types
9751 -- the predicate is inherited from the first subtype, and not from its
9752 -- (anonymous) base type.
9753
9754 if Has_Predicates (Parent_Type)
9755 or else Has_Predicates (First_Subtype (Parent_Type))
9756 then
9757 Set_Has_Predicates (Derived_Type);
9758 end if;
9759
9760 -- The derived type inherits representation clauses from the parent
9761 -- type, and from any interfaces.
9762
9763 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9764
9765 declare
9766 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9767 begin
9768 while Present (Iface) loop
9769 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9770 Next (Iface);
9771 end loop;
9772 end;
9773
9774 -- If the parent type has delayed rep aspects, then mark the derived
9775 -- type as possibly inheriting a delayed rep aspect.
9776
9777 if Has_Delayed_Rep_Aspects (Parent_Type) then
9778 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9779 end if;
9780
9781 -- A derived type becomes Ghost when its parent type is also Ghost
9782 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9783 -- directly inherited because the Ghost policy in effect may differ.
9784
9785 if Is_Ghost_Entity (Parent_Type) then
9786 Set_Is_Ghost_Entity (Derived_Type);
9787 end if;
9788
9789 -- Type dependent processing
9790
9791 case Ekind (Parent_Type) is
9792 when Numeric_Kind =>
9793 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9794
9795 when Array_Kind =>
9796 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9797
9798 when Class_Wide_Kind
9799 | E_Record_Subtype
9800 | E_Record_Type
9801 =>
9802 Build_Derived_Record_Type
9803 (N, Parent_Type, Derived_Type, Derive_Subps);
9804 return;
9805
9806 when Enumeration_Kind =>
9807 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9808
9809 when Access_Kind =>
9810 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9811
9812 when Incomplete_Or_Private_Kind =>
9813 Build_Derived_Private_Type
9814 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9815
9816 -- For discriminated types, the derivation includes deriving
9817 -- primitive operations. For others it is done below.
9818
9819 if Is_Tagged_Type (Parent_Type)
9820 or else Has_Discriminants (Parent_Type)
9821 or else (Present (Full_View (Parent_Type))
9822 and then Has_Discriminants (Full_View (Parent_Type)))
9823 then
9824 return;
9825 end if;
9826
9827 when Concurrent_Kind =>
9828 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9829
9830 when others =>
9831 raise Program_Error;
9832 end case;
9833
9834 -- Nothing more to do if some error occurred
9835
9836 if Etype (Derived_Type) = Any_Type then
9837 return;
9838 end if;
9839
9840 -- Set delayed freeze and then derive subprograms, we need to do this
9841 -- in this order so that derived subprograms inherit the derived freeze
9842 -- if necessary.
9843
9844 Set_Has_Delayed_Freeze (Derived_Type);
9845
9846 if Derive_Subps then
9847 Derive_Subprograms (Parent_Type, Derived_Type);
9848 end if;
9849
9850 Set_Has_Primitive_Operations
9851 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9852 end Build_Derived_Type;
9853
9854 -----------------------
9855 -- Build_Discriminal --
9856 -----------------------
9857
9858 procedure Build_Discriminal (Discrim : Entity_Id) is
9859 D_Minal : Entity_Id;
9860 CR_Disc : Entity_Id;
9861
9862 begin
9863 -- A discriminal has the same name as the discriminant
9864
9865 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9866
9867 Set_Ekind (D_Minal, E_In_Parameter);
9868 Set_Mechanism (D_Minal, Default_Mechanism);
9869 Set_Etype (D_Minal, Etype (Discrim));
9870 Set_Scope (D_Minal, Current_Scope);
9871 Set_Parent (D_Minal, Parent (Discrim));
9872
9873 Set_Discriminal (Discrim, D_Minal);
9874 Set_Discriminal_Link (D_Minal, Discrim);
9875
9876 -- For task types, build at once the discriminants of the corresponding
9877 -- record, which are needed if discriminants are used in entry defaults
9878 -- and in family bounds.
9879
9880 if Is_Concurrent_Type (Current_Scope)
9881 or else
9882 Is_Limited_Type (Current_Scope)
9883 then
9884 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9885
9886 Set_Ekind (CR_Disc, E_In_Parameter);
9887 Set_Mechanism (CR_Disc, Default_Mechanism);
9888 Set_Etype (CR_Disc, Etype (Discrim));
9889 Set_Scope (CR_Disc, Current_Scope);
9890 Set_Discriminal_Link (CR_Disc, Discrim);
9891 Set_CR_Discriminant (Discrim, CR_Disc);
9892 end if;
9893 end Build_Discriminal;
9894
9895 ------------------------------------
9896 -- Build_Discriminant_Constraints --
9897 ------------------------------------
9898
9899 function Build_Discriminant_Constraints
9900 (T : Entity_Id;
9901 Def : Node_Id;
9902 Derived_Def : Boolean := False) return Elist_Id
9903 is
9904 C : constant Node_Id := Constraint (Def);
9905 Nb_Discr : constant Nat := Number_Discriminants (T);
9906
9907 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9908 -- Saves the expression corresponding to a given discriminant in T
9909
9910 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9911 -- Return the Position number within array Discr_Expr of a discriminant
9912 -- D within the discriminant list of the discriminated type T.
9913
9914 procedure Process_Discriminant_Expression
9915 (Expr : Node_Id;
9916 D : Entity_Id);
9917 -- If this is a discriminant constraint on a partial view, do not
9918 -- generate an overflow check on the discriminant expression. The check
9919 -- will be generated when constraining the full view. Otherwise the
9920 -- backend creates duplicate symbols for the temporaries corresponding
9921 -- to the expressions to be checked, causing spurious assembler errors.
9922
9923 ------------------
9924 -- Pos_Of_Discr --
9925 ------------------
9926
9927 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9928 Disc : Entity_Id;
9929
9930 begin
9931 Disc := First_Discriminant (T);
9932 for J in Discr_Expr'Range loop
9933 if Disc = D then
9934 return J;
9935 end if;
9936
9937 Next_Discriminant (Disc);
9938 end loop;
9939
9940 -- Note: Since this function is called on discriminants that are
9941 -- known to belong to the discriminated type, falling through the
9942 -- loop with no match signals an internal compiler error.
9943
9944 raise Program_Error;
9945 end Pos_Of_Discr;
9946
9947 -------------------------------------
9948 -- Process_Discriminant_Expression --
9949 -------------------------------------
9950
9951 procedure Process_Discriminant_Expression
9952 (Expr : Node_Id;
9953 D : Entity_Id)
9954 is
9955 BDT : constant Entity_Id := Base_Type (Etype (D));
9956
9957 begin
9958 -- If this is a discriminant constraint on a partial view, do
9959 -- not generate an overflow on the discriminant expression. The
9960 -- check will be generated when constraining the full view.
9961
9962 if Is_Private_Type (T)
9963 and then Present (Full_View (T))
9964 then
9965 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9966 else
9967 Analyze_And_Resolve (Expr, BDT);
9968 end if;
9969 end Process_Discriminant_Expression;
9970
9971 -- Declarations local to Build_Discriminant_Constraints
9972
9973 Discr : Entity_Id;
9974 E : Entity_Id;
9975 Elist : constant Elist_Id := New_Elmt_List;
9976
9977 Constr : Node_Id;
9978 Expr : Node_Id;
9979 Id : Node_Id;
9980 Position : Nat;
9981 Found : Boolean;
9982
9983 Discrim_Present : Boolean := False;
9984
9985 -- Start of processing for Build_Discriminant_Constraints
9986
9987 begin
9988 -- The following loop will process positional associations only.
9989 -- For a positional association, the (single) discriminant is
9990 -- implicitly specified by position, in textual order (RM 3.7.2).
9991
9992 Discr := First_Discriminant (T);
9993 Constr := First (Constraints (C));
9994 for D in Discr_Expr'Range loop
9995 exit when Nkind (Constr) = N_Discriminant_Association;
9996
9997 if No (Constr) then
9998 Error_Msg_N ("too few discriminants given in constraint", C);
9999 return New_Elmt_List;
10000
10001 elsif Nkind (Constr) = N_Range
10002 or else (Nkind (Constr) = N_Attribute_Reference
10003 and then Attribute_Name (Constr) = Name_Range)
10004 then
10005 Error_Msg_N
10006 ("a range is not a valid discriminant constraint", Constr);
10007 Discr_Expr (D) := Error;
10008
10009 elsif Nkind (Constr) = N_Subtype_Indication then
10010 Error_Msg_N
10011 ("a subtype indication is not a valid discriminant constraint",
10012 Constr);
10013 Discr_Expr (D) := Error;
10014
10015 else
10016 Process_Discriminant_Expression (Constr, Discr);
10017 Discr_Expr (D) := Constr;
10018 end if;
10019
10020 Next_Discriminant (Discr);
10021 Next (Constr);
10022 end loop;
10023
10024 if No (Discr) and then Present (Constr) then
10025 Error_Msg_N ("too many discriminants given in constraint", Constr);
10026 return New_Elmt_List;
10027 end if;
10028
10029 -- Named associations can be given in any order, but if both positional
10030 -- and named associations are used in the same discriminant constraint,
10031 -- then positional associations must occur first, at their normal
10032 -- position. Hence once a named association is used, the rest of the
10033 -- discriminant constraint must use only named associations.
10034
10035 while Present (Constr) loop
10036
10037 -- Positional association forbidden after a named association
10038
10039 if Nkind (Constr) /= N_Discriminant_Association then
10040 Error_Msg_N ("positional association follows named one", Constr);
10041 return New_Elmt_List;
10042
10043 -- Otherwise it is a named association
10044
10045 else
10046 -- E records the type of the discriminants in the named
10047 -- association. All the discriminants specified in the same name
10048 -- association must have the same type.
10049
10050 E := Empty;
10051
10052 -- Search the list of discriminants in T to see if the simple name
10053 -- given in the constraint matches any of them.
10054
10055 Id := First (Selector_Names (Constr));
10056 while Present (Id) loop
10057 Found := False;
10058
10059 -- If Original_Discriminant is present, we are processing a
10060 -- generic instantiation and this is an instance node. We need
10061 -- to find the name of the corresponding discriminant in the
10062 -- actual record type T and not the name of the discriminant in
10063 -- the generic formal. Example:
10064
10065 -- generic
10066 -- type G (D : int) is private;
10067 -- package P is
10068 -- subtype W is G (D => 1);
10069 -- end package;
10070 -- type Rec (X : int) is record ... end record;
10071 -- package Q is new P (G => Rec);
10072
10073 -- At the point of the instantiation, formal type G is Rec
10074 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10075 -- which really looks like "subtype W is Rec (D => 1);" at
10076 -- the point of instantiation, we want to find the discriminant
10077 -- that corresponds to D in Rec, i.e. X.
10078
10079 if Present (Original_Discriminant (Id))
10080 and then In_Instance
10081 then
10082 Discr := Find_Corresponding_Discriminant (Id, T);
10083 Found := True;
10084
10085 else
10086 Discr := First_Discriminant (T);
10087 while Present (Discr) loop
10088 if Chars (Discr) = Chars (Id) then
10089 Found := True;
10090 exit;
10091 end if;
10092
10093 Next_Discriminant (Discr);
10094 end loop;
10095
10096 if not Found then
10097 Error_Msg_N ("& does not match any discriminant", Id);
10098 return New_Elmt_List;
10099
10100 -- If the parent type is a generic formal, preserve the
10101 -- name of the discriminant for subsequent instances.
10102 -- see comment at the beginning of this if statement.
10103
10104 elsif Is_Generic_Type (Root_Type (T)) then
10105 Set_Original_Discriminant (Id, Discr);
10106 end if;
10107 end if;
10108
10109 Position := Pos_Of_Discr (T, Discr);
10110
10111 if Present (Discr_Expr (Position)) then
10112 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10113
10114 else
10115 -- Each discriminant specified in the same named association
10116 -- must be associated with a separate copy of the
10117 -- corresponding expression.
10118
10119 if Present (Next (Id)) then
10120 Expr := New_Copy_Tree (Expression (Constr));
10121 Set_Parent (Expr, Parent (Expression (Constr)));
10122 else
10123 Expr := Expression (Constr);
10124 end if;
10125
10126 Discr_Expr (Position) := Expr;
10127 Process_Discriminant_Expression (Expr, Discr);
10128 end if;
10129
10130 -- A discriminant association with more than one discriminant
10131 -- name is only allowed if the named discriminants are all of
10132 -- the same type (RM 3.7.1(8)).
10133
10134 if E = Empty then
10135 E := Base_Type (Etype (Discr));
10136
10137 elsif Base_Type (Etype (Discr)) /= E then
10138 Error_Msg_N
10139 ("all discriminants in an association " &
10140 "must have the same type", Id);
10141 end if;
10142
10143 Next (Id);
10144 end loop;
10145 end if;
10146
10147 Next (Constr);
10148 end loop;
10149
10150 -- A discriminant constraint must provide exactly one value for each
10151 -- discriminant of the type (RM 3.7.1(8)).
10152
10153 for J in Discr_Expr'Range loop
10154 if No (Discr_Expr (J)) then
10155 Error_Msg_N ("too few discriminants given in constraint", C);
10156 return New_Elmt_List;
10157 end if;
10158 end loop;
10159
10160 -- Determine if there are discriminant expressions in the constraint
10161
10162 for J in Discr_Expr'Range loop
10163 if Denotes_Discriminant
10164 (Discr_Expr (J), Check_Concurrent => True)
10165 then
10166 Discrim_Present := True;
10167 end if;
10168 end loop;
10169
10170 -- Build an element list consisting of the expressions given in the
10171 -- discriminant constraint and apply the appropriate checks. The list
10172 -- is constructed after resolving any named discriminant associations
10173 -- and therefore the expressions appear in the textual order of the
10174 -- discriminants.
10175
10176 Discr := First_Discriminant (T);
10177 for J in Discr_Expr'Range loop
10178 if Discr_Expr (J) /= Error then
10179 Append_Elmt (Discr_Expr (J), Elist);
10180
10181 -- If any of the discriminant constraints is given by a
10182 -- discriminant and we are in a derived type declaration we
10183 -- have a discriminant renaming. Establish link between new
10184 -- and old discriminant. The new discriminant has an implicit
10185 -- dereference if the old one does.
10186
10187 if Denotes_Discriminant (Discr_Expr (J)) then
10188 if Derived_Def then
10189 declare
10190 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10191
10192 begin
10193 Set_Corresponding_Discriminant (New_Discr, Discr);
10194 Set_Has_Implicit_Dereference (New_Discr,
10195 Has_Implicit_Dereference (Discr));
10196 end;
10197 end if;
10198
10199 -- Force the evaluation of non-discriminant expressions.
10200 -- If we have found a discriminant in the constraint 3.4(26)
10201 -- and 3.8(18) demand that no range checks are performed are
10202 -- after evaluation. If the constraint is for a component
10203 -- definition that has a per-object constraint, expressions are
10204 -- evaluated but not checked either. In all other cases perform
10205 -- a range check.
10206
10207 else
10208 if Discrim_Present then
10209 null;
10210
10211 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10212 and then Has_Per_Object_Constraint
10213 (Defining_Identifier (Parent (Parent (Def))))
10214 then
10215 null;
10216
10217 elsif Is_Access_Type (Etype (Discr)) then
10218 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10219
10220 else
10221 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10222 end if;
10223
10224 Force_Evaluation (Discr_Expr (J));
10225 end if;
10226
10227 -- Check that the designated type of an access discriminant's
10228 -- expression is not a class-wide type unless the discriminant's
10229 -- designated type is also class-wide.
10230
10231 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10232 and then not Is_Class_Wide_Type
10233 (Designated_Type (Etype (Discr)))
10234 and then Etype (Discr_Expr (J)) /= Any_Type
10235 and then Is_Class_Wide_Type
10236 (Designated_Type (Etype (Discr_Expr (J))))
10237 then
10238 Wrong_Type (Discr_Expr (J), Etype (Discr));
10239
10240 elsif Is_Access_Type (Etype (Discr))
10241 and then not Is_Access_Constant (Etype (Discr))
10242 and then Is_Access_Type (Etype (Discr_Expr (J)))
10243 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10244 then
10245 Error_Msg_NE
10246 ("constraint for discriminant& must be access to variable",
10247 Def, Discr);
10248 end if;
10249 end if;
10250
10251 Next_Discriminant (Discr);
10252 end loop;
10253
10254 return Elist;
10255 end Build_Discriminant_Constraints;
10256
10257 ---------------------------------
10258 -- Build_Discriminated_Subtype --
10259 ---------------------------------
10260
10261 procedure Build_Discriminated_Subtype
10262 (T : Entity_Id;
10263 Def_Id : Entity_Id;
10264 Elist : Elist_Id;
10265 Related_Nod : Node_Id;
10266 For_Access : Boolean := False)
10267 is
10268 Has_Discrs : constant Boolean := Has_Discriminants (T);
10269 Constrained : constant Boolean :=
10270 (Has_Discrs
10271 and then not Is_Empty_Elmt_List (Elist)
10272 and then not Is_Class_Wide_Type (T))
10273 or else Is_Constrained (T);
10274
10275 begin
10276 if Ekind (T) = E_Record_Type then
10277 Set_Ekind (Def_Id, E_Record_Subtype);
10278
10279 -- Inherit preelaboration flag from base, for types for which it
10280 -- may have been set: records, private types, protected types.
10281
10282 Set_Known_To_Have_Preelab_Init
10283 (Def_Id, Known_To_Have_Preelab_Init (T));
10284
10285 elsif Ekind (T) = E_Task_Type then
10286 Set_Ekind (Def_Id, E_Task_Subtype);
10287
10288 elsif Ekind (T) = E_Protected_Type then
10289 Set_Ekind (Def_Id, E_Protected_Subtype);
10290 Set_Known_To_Have_Preelab_Init
10291 (Def_Id, Known_To_Have_Preelab_Init (T));
10292
10293 elsif Is_Private_Type (T) then
10294 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10295 Set_Known_To_Have_Preelab_Init
10296 (Def_Id, Known_To_Have_Preelab_Init (T));
10297
10298 -- Private subtypes may have private dependents
10299
10300 Set_Private_Dependents (Def_Id, New_Elmt_List);
10301
10302 elsif Is_Class_Wide_Type (T) then
10303 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10304
10305 else
10306 -- Incomplete type. Attach subtype to list of dependents, to be
10307 -- completed with full view of parent type, unless is it the
10308 -- designated subtype of a record component within an init_proc.
10309 -- This last case arises for a component of an access type whose
10310 -- designated type is incomplete (e.g. a Taft Amendment type).
10311 -- The designated subtype is within an inner scope, and needs no
10312 -- elaboration, because only the access type is needed in the
10313 -- initialization procedure.
10314
10315 if Ekind (T) = E_Incomplete_Type then
10316 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10317 else
10318 Set_Ekind (Def_Id, Ekind (T));
10319 end if;
10320
10321 if For_Access and then Within_Init_Proc then
10322 null;
10323 else
10324 Append_Elmt (Def_Id, Private_Dependents (T));
10325 end if;
10326 end if;
10327
10328 Set_Etype (Def_Id, T);
10329 Init_Size_Align (Def_Id);
10330 Set_Has_Discriminants (Def_Id, Has_Discrs);
10331 Set_Is_Constrained (Def_Id, Constrained);
10332
10333 Set_First_Entity (Def_Id, First_Entity (T));
10334 Set_Last_Entity (Def_Id, Last_Entity (T));
10335 Set_Has_Implicit_Dereference
10336 (Def_Id, Has_Implicit_Dereference (T));
10337 Set_Has_Pragma_Unreferenced_Objects
10338 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10339
10340 -- If the subtype is the completion of a private declaration, there may
10341 -- have been representation clauses for the partial view, and they must
10342 -- be preserved. Build_Derived_Type chains the inherited clauses with
10343 -- the ones appearing on the extension. If this comes from a subtype
10344 -- declaration, all clauses are inherited.
10345
10346 if No (First_Rep_Item (Def_Id)) then
10347 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10348 end if;
10349
10350 if Is_Tagged_Type (T) then
10351 Set_Is_Tagged_Type (Def_Id);
10352 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10353 Make_Class_Wide_Type (Def_Id);
10354 end if;
10355
10356 Set_Stored_Constraint (Def_Id, No_Elist);
10357
10358 if Has_Discrs then
10359 Set_Discriminant_Constraint (Def_Id, Elist);
10360 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10361 end if;
10362
10363 if Is_Tagged_Type (T) then
10364
10365 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10366 -- concurrent record type (which has the list of primitive
10367 -- operations).
10368
10369 if Ada_Version >= Ada_2005
10370 and then Is_Concurrent_Type (T)
10371 then
10372 Set_Corresponding_Record_Type (Def_Id,
10373 Corresponding_Record_Type (T));
10374 else
10375 Set_Direct_Primitive_Operations (Def_Id,
10376 Direct_Primitive_Operations (T));
10377 end if;
10378
10379 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10380 end if;
10381
10382 -- Subtypes introduced by component declarations do not need to be
10383 -- marked as delayed, and do not get freeze nodes, because the semantics
10384 -- verifies that the parents of the subtypes are frozen before the
10385 -- enclosing record is frozen.
10386
10387 if not Is_Type (Scope (Def_Id)) then
10388 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10389
10390 if Is_Private_Type (T)
10391 and then Present (Full_View (T))
10392 then
10393 Conditional_Delay (Def_Id, Full_View (T));
10394 else
10395 Conditional_Delay (Def_Id, T);
10396 end if;
10397 end if;
10398
10399 if Is_Record_Type (T) then
10400 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10401
10402 if Has_Discrs
10403 and then not Is_Empty_Elmt_List (Elist)
10404 and then not For_Access
10405 then
10406 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10407
10408 else
10409 Set_Cloned_Subtype (Def_Id, T);
10410 end if;
10411 end if;
10412 end Build_Discriminated_Subtype;
10413
10414 ---------------------------
10415 -- Build_Itype_Reference --
10416 ---------------------------
10417
10418 procedure Build_Itype_Reference
10419 (Ityp : Entity_Id;
10420 Nod : Node_Id)
10421 is
10422 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10423 begin
10424
10425 -- Itype references are only created for use by the back-end
10426
10427 if Inside_A_Generic then
10428 return;
10429 else
10430 Set_Itype (IR, Ityp);
10431
10432 -- If Nod is a library unit entity, then Insert_After won't work,
10433 -- because Nod is not a member of any list. Therefore, we use
10434 -- Add_Global_Declaration in this case. This can happen if we have a
10435 -- build-in-place library function, child unit or not.
10436
10437 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10438 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10439 N_Subprogram_Declaration)
10440 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10441 then
10442 Add_Global_Declaration (IR);
10443 else
10444 Insert_After (Nod, IR);
10445 end if;
10446 end if;
10447 end Build_Itype_Reference;
10448
10449 ------------------------
10450 -- Build_Scalar_Bound --
10451 ------------------------
10452
10453 function Build_Scalar_Bound
10454 (Bound : Node_Id;
10455 Par_T : Entity_Id;
10456 Der_T : Entity_Id) return Node_Id
10457 is
10458 New_Bound : Entity_Id;
10459
10460 begin
10461 -- Note: not clear why this is needed, how can the original bound
10462 -- be unanalyzed at this point? and if it is, what business do we
10463 -- have messing around with it? and why is the base type of the
10464 -- parent type the right type for the resolution. It probably is
10465 -- not. It is OK for the new bound we are creating, but not for
10466 -- the old one??? Still if it never happens, no problem.
10467
10468 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10469
10470 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10471 New_Bound := New_Copy (Bound);
10472 Set_Etype (New_Bound, Der_T);
10473 Set_Analyzed (New_Bound);
10474
10475 elsif Is_Entity_Name (Bound) then
10476 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10477
10478 -- The following is almost certainly wrong. What business do we have
10479 -- relocating a node (Bound) that is presumably still attached to
10480 -- the tree elsewhere???
10481
10482 else
10483 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10484 end if;
10485
10486 Set_Etype (New_Bound, Der_T);
10487 return New_Bound;
10488 end Build_Scalar_Bound;
10489
10490 -------------------------------
10491 -- Check_Abstract_Overriding --
10492 -------------------------------
10493
10494 procedure Check_Abstract_Overriding (T : Entity_Id) is
10495 Alias_Subp : Entity_Id;
10496 Elmt : Elmt_Id;
10497 Op_List : Elist_Id;
10498 Subp : Entity_Id;
10499 Type_Def : Node_Id;
10500
10501 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10502 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10503 -- which has pragma Implemented already set. Check whether Subp's entity
10504 -- kind conforms to the implementation kind of the overridden routine.
10505
10506 procedure Check_Pragma_Implemented
10507 (Subp : Entity_Id;
10508 Iface_Subp : Entity_Id);
10509 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10510 -- Iface_Subp and both entities have pragma Implemented already set on
10511 -- them. Check whether the two implementation kinds are conforming.
10512
10513 procedure Inherit_Pragma_Implemented
10514 (Subp : Entity_Id;
10515 Iface_Subp : Entity_Id);
10516 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10517 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10518 -- Propagate the implementation kind of Iface_Subp to Subp.
10519
10520 ------------------------------
10521 -- Check_Pragma_Implemented --
10522 ------------------------------
10523
10524 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10525 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10526 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10527 Subp_Alias : constant Entity_Id := Alias (Subp);
10528 Contr_Typ : Entity_Id;
10529 Impl_Subp : Entity_Id;
10530
10531 begin
10532 -- Subp must have an alias since it is a hidden entity used to link
10533 -- an interface subprogram to its overriding counterpart.
10534
10535 pragma Assert (Present (Subp_Alias));
10536
10537 -- Handle aliases to synchronized wrappers
10538
10539 Impl_Subp := Subp_Alias;
10540
10541 if Is_Primitive_Wrapper (Impl_Subp) then
10542 Impl_Subp := Wrapped_Entity (Impl_Subp);
10543 end if;
10544
10545 -- Extract the type of the controlling formal
10546
10547 Contr_Typ := Etype (First_Formal (Subp_Alias));
10548
10549 if Is_Concurrent_Record_Type (Contr_Typ) then
10550 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10551 end if;
10552
10553 -- An interface subprogram whose implementation kind is By_Entry must
10554 -- be implemented by an entry.
10555
10556 if Impl_Kind = Name_By_Entry
10557 and then Ekind (Impl_Subp) /= E_Entry
10558 then
10559 Error_Msg_Node_2 := Iface_Alias;
10560 Error_Msg_NE
10561 ("type & must implement abstract subprogram & with an entry",
10562 Subp_Alias, Contr_Typ);
10563
10564 elsif Impl_Kind = Name_By_Protected_Procedure then
10565
10566 -- An interface subprogram whose implementation kind is By_
10567 -- Protected_Procedure cannot be implemented by a primitive
10568 -- procedure of a task type.
10569
10570 if Ekind (Contr_Typ) /= E_Protected_Type then
10571 Error_Msg_Node_2 := Contr_Typ;
10572 Error_Msg_NE
10573 ("interface subprogram & cannot be implemented by a "
10574 & "primitive procedure of task type &",
10575 Subp_Alias, Iface_Alias);
10576
10577 -- An interface subprogram whose implementation kind is By_
10578 -- Protected_Procedure must be implemented by a procedure.
10579
10580 elsif Ekind (Impl_Subp) /= E_Procedure then
10581 Error_Msg_Node_2 := Iface_Alias;
10582 Error_Msg_NE
10583 ("type & must implement abstract subprogram & with a "
10584 & "procedure", Subp_Alias, Contr_Typ);
10585
10586 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10587 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10588 then
10589 Error_Msg_Name_1 := Impl_Kind;
10590 Error_Msg_N
10591 ("overriding operation& must have synchronization%",
10592 Subp_Alias);
10593 end if;
10594
10595 -- If primitive has Optional synchronization, overriding operation
10596 -- must match if it has an explicit synchronization.
10597
10598 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10599 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10600 then
10601 Error_Msg_Name_1 := Impl_Kind;
10602 Error_Msg_N
10603 ("overriding operation& must have synchronization%", Subp_Alias);
10604 end if;
10605 end Check_Pragma_Implemented;
10606
10607 ------------------------------
10608 -- Check_Pragma_Implemented --
10609 ------------------------------
10610
10611 procedure Check_Pragma_Implemented
10612 (Subp : Entity_Id;
10613 Iface_Subp : Entity_Id)
10614 is
10615 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10616 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10617
10618 begin
10619 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10620 -- and overriding subprogram are different. In general this is an
10621 -- error except when the implementation kind of the overridden
10622 -- subprograms is By_Any or Optional.
10623
10624 if Iface_Kind /= Subp_Kind
10625 and then Iface_Kind /= Name_By_Any
10626 and then Iface_Kind /= Name_Optional
10627 then
10628 if Iface_Kind = Name_By_Entry then
10629 Error_Msg_N
10630 ("incompatible implementation kind, overridden subprogram " &
10631 "is marked By_Entry", Subp);
10632 else
10633 Error_Msg_N
10634 ("incompatible implementation kind, overridden subprogram " &
10635 "is marked By_Protected_Procedure", Subp);
10636 end if;
10637 end if;
10638 end Check_Pragma_Implemented;
10639
10640 --------------------------------
10641 -- Inherit_Pragma_Implemented --
10642 --------------------------------
10643
10644 procedure Inherit_Pragma_Implemented
10645 (Subp : Entity_Id;
10646 Iface_Subp : Entity_Id)
10647 is
10648 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10649 Loc : constant Source_Ptr := Sloc (Subp);
10650 Impl_Prag : Node_Id;
10651
10652 begin
10653 -- Since the implementation kind is stored as a representation item
10654 -- rather than a flag, create a pragma node.
10655
10656 Impl_Prag :=
10657 Make_Pragma (Loc,
10658 Chars => Name_Implemented,
10659 Pragma_Argument_Associations => New_List (
10660 Make_Pragma_Argument_Association (Loc,
10661 Expression => New_Occurrence_Of (Subp, Loc)),
10662
10663 Make_Pragma_Argument_Association (Loc,
10664 Expression => Make_Identifier (Loc, Iface_Kind))));
10665
10666 -- The pragma doesn't need to be analyzed because it is internally
10667 -- built. It is safe to directly register it as a rep item since we
10668 -- are only interested in the characters of the implementation kind.
10669
10670 Record_Rep_Item (Subp, Impl_Prag);
10671 end Inherit_Pragma_Implemented;
10672
10673 -- Start of processing for Check_Abstract_Overriding
10674
10675 begin
10676 Op_List := Primitive_Operations (T);
10677
10678 -- Loop to check primitive operations
10679
10680 Elmt := First_Elmt (Op_List);
10681 while Present (Elmt) loop
10682 Subp := Node (Elmt);
10683 Alias_Subp := Alias (Subp);
10684
10685 -- Inherited subprograms are identified by the fact that they do not
10686 -- come from source, and the associated source location is the
10687 -- location of the first subtype of the derived type.
10688
10689 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10690 -- subprograms that "require overriding".
10691
10692 -- Special exception, do not complain about failure to override the
10693 -- stream routines _Input and _Output, as well as the primitive
10694 -- operations used in dispatching selects since we always provide
10695 -- automatic overridings for these subprograms.
10696
10697 -- The partial view of T may have been a private extension, for
10698 -- which inherited functions dispatching on result are abstract.
10699 -- If the full view is a null extension, there is no need for
10700 -- overriding in Ada 2005, but wrappers need to be built for them
10701 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10702
10703 if Is_Null_Extension (T)
10704 and then Has_Controlling_Result (Subp)
10705 and then Ada_Version >= Ada_2005
10706 and then Present (Alias_Subp)
10707 and then not Comes_From_Source (Subp)
10708 and then not Is_Abstract_Subprogram (Alias_Subp)
10709 and then not Is_Access_Type (Etype (Subp))
10710 then
10711 null;
10712
10713 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10714 -- processing because this check is done with the aliased
10715 -- entity
10716
10717 elsif Present (Interface_Alias (Subp)) then
10718 null;
10719
10720 elsif (Is_Abstract_Subprogram (Subp)
10721 or else Requires_Overriding (Subp)
10722 or else
10723 (Has_Controlling_Result (Subp)
10724 and then Present (Alias_Subp)
10725 and then not Comes_From_Source (Subp)
10726 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10727 and then not Is_TSS (Subp, TSS_Stream_Input)
10728 and then not Is_TSS (Subp, TSS_Stream_Output)
10729 and then not Is_Abstract_Type (T)
10730 and then not Is_Predefined_Interface_Primitive (Subp)
10731
10732 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10733 -- with abstract interface types because the check will be done
10734 -- with the aliased entity (otherwise we generate a duplicated
10735 -- error message).
10736
10737 and then not Present (Interface_Alias (Subp))
10738 then
10739 if Present (Alias_Subp) then
10740
10741 -- Only perform the check for a derived subprogram when the
10742 -- type has an explicit record extension. This avoids incorrect
10743 -- flagging of abstract subprograms for the case of a type
10744 -- without an extension that is derived from a formal type
10745 -- with a tagged actual (can occur within a private part).
10746
10747 -- Ada 2005 (AI-391): In the case of an inherited function with
10748 -- a controlling result of the type, the rule does not apply if
10749 -- the type is a null extension (unless the parent function
10750 -- itself is abstract, in which case the function must still be
10751 -- be overridden). The expander will generate an overriding
10752 -- wrapper function calling the parent subprogram (see
10753 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10754
10755 Type_Def := Type_Definition (Parent (T));
10756
10757 if Nkind (Type_Def) = N_Derived_Type_Definition
10758 and then Present (Record_Extension_Part (Type_Def))
10759 and then
10760 (Ada_Version < Ada_2005
10761 or else not Is_Null_Extension (T)
10762 or else Ekind (Subp) = E_Procedure
10763 or else not Has_Controlling_Result (Subp)
10764 or else Is_Abstract_Subprogram (Alias_Subp)
10765 or else Requires_Overriding (Subp)
10766 or else Is_Access_Type (Etype (Subp)))
10767 then
10768 -- Avoid reporting error in case of abstract predefined
10769 -- primitive inherited from interface type because the
10770 -- body of internally generated predefined primitives
10771 -- of tagged types are generated later by Freeze_Type
10772
10773 if Is_Interface (Root_Type (T))
10774 and then Is_Abstract_Subprogram (Subp)
10775 and then Is_Predefined_Dispatching_Operation (Subp)
10776 and then not Comes_From_Source (Ultimate_Alias (Subp))
10777 then
10778 null;
10779
10780 -- A null extension is not obliged to override an inherited
10781 -- procedure subject to pragma Extensions_Visible with value
10782 -- False and at least one controlling OUT parameter
10783 -- (SPARK RM 6.1.7(6)).
10784
10785 elsif Is_Null_Extension (T)
10786 and then Is_EVF_Procedure (Subp)
10787 then
10788 null;
10789
10790 else
10791 Error_Msg_NE
10792 ("type must be declared abstract or & overridden",
10793 T, Subp);
10794
10795 -- Traverse the whole chain of aliased subprograms to
10796 -- complete the error notification. This is especially
10797 -- useful for traceability of the chain of entities when
10798 -- the subprogram corresponds with an interface
10799 -- subprogram (which may be defined in another package).
10800
10801 if Present (Alias_Subp) then
10802 declare
10803 E : Entity_Id;
10804
10805 begin
10806 E := Subp;
10807 while Present (Alias (E)) loop
10808
10809 -- Avoid reporting redundant errors on entities
10810 -- inherited from interfaces
10811
10812 if Sloc (E) /= Sloc (T) then
10813 Error_Msg_Sloc := Sloc (E);
10814 Error_Msg_NE
10815 ("\& has been inherited #", T, Subp);
10816 end if;
10817
10818 E := Alias (E);
10819 end loop;
10820
10821 Error_Msg_Sloc := Sloc (E);
10822
10823 -- AI05-0068: report if there is an overriding
10824 -- non-abstract subprogram that is invisible.
10825
10826 if Is_Hidden (E)
10827 and then not Is_Abstract_Subprogram (E)
10828 then
10829 Error_Msg_NE
10830 ("\& subprogram# is not visible",
10831 T, Subp);
10832
10833 -- Clarify the case where a non-null extension must
10834 -- override inherited procedure subject to pragma
10835 -- Extensions_Visible with value False and at least
10836 -- one controlling OUT param.
10837
10838 elsif Is_EVF_Procedure (E) then
10839 Error_Msg_NE
10840 ("\& # is subject to Extensions_Visible False",
10841 T, Subp);
10842
10843 else
10844 Error_Msg_NE
10845 ("\& has been inherited from subprogram #",
10846 T, Subp);
10847 end if;
10848 end;
10849 end if;
10850 end if;
10851
10852 -- Ada 2005 (AI-345): Protected or task type implementing
10853 -- abstract interfaces.
10854
10855 elsif Is_Concurrent_Record_Type (T)
10856 and then Present (Interfaces (T))
10857 then
10858 -- There is no need to check here RM 9.4(11.9/3) since we
10859 -- are processing the corresponding record type and the
10860 -- mode of the overriding subprograms was verified by
10861 -- Check_Conformance when the corresponding concurrent
10862 -- type declaration was analyzed.
10863
10864 Error_Msg_NE
10865 ("interface subprogram & must be overridden", T, Subp);
10866
10867 -- Examine primitive operations of synchronized type to find
10868 -- homonyms that have the wrong profile.
10869
10870 declare
10871 Prim : Entity_Id;
10872
10873 begin
10874 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10875 while Present (Prim) loop
10876 if Chars (Prim) = Chars (Subp) then
10877 Error_Msg_NE
10878 ("profile is not type conformant with prefixed "
10879 & "view profile of inherited operation&",
10880 Prim, Subp);
10881 end if;
10882
10883 Next_Entity (Prim);
10884 end loop;
10885 end;
10886 end if;
10887
10888 else
10889 Error_Msg_Node_2 := T;
10890 Error_Msg_N
10891 ("abstract subprogram& not allowed for type&", Subp);
10892
10893 -- Also post unconditional warning on the type (unconditional
10894 -- so that if there are more than one of these cases, we get
10895 -- them all, and not just the first one).
10896
10897 Error_Msg_Node_2 := Subp;
10898 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10899 end if;
10900
10901 -- A subprogram subject to pragma Extensions_Visible with value
10902 -- "True" cannot override a subprogram subject to the same pragma
10903 -- with value "False" (SPARK RM 6.1.7(5)).
10904
10905 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10906 and then Present (Overridden_Operation (Subp))
10907 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10908 Extensions_Visible_False
10909 then
10910 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10911 Error_Msg_N
10912 ("subprogram & with Extensions_Visible True cannot override "
10913 & "subprogram # with Extensions_Visible False", Subp);
10914 end if;
10915
10916 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10917
10918 -- Subp is an expander-generated procedure which maps an interface
10919 -- alias to a protected wrapper. The interface alias is flagged by
10920 -- pragma Implemented. Ensure that Subp is a procedure when the
10921 -- implementation kind is By_Protected_Procedure or an entry when
10922 -- By_Entry.
10923
10924 if Ada_Version >= Ada_2012
10925 and then Is_Hidden (Subp)
10926 and then Present (Interface_Alias (Subp))
10927 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10928 then
10929 Check_Pragma_Implemented (Subp);
10930 end if;
10931
10932 -- Subp is an interface primitive which overrides another interface
10933 -- primitive marked with pragma Implemented.
10934
10935 if Ada_Version >= Ada_2012
10936 and then Present (Overridden_Operation (Subp))
10937 and then Has_Rep_Pragma
10938 (Overridden_Operation (Subp), Name_Implemented)
10939 then
10940 -- If the overriding routine is also marked by Implemented, check
10941 -- that the two implementation kinds are conforming.
10942
10943 if Has_Rep_Pragma (Subp, Name_Implemented) then
10944 Check_Pragma_Implemented
10945 (Subp => Subp,
10946 Iface_Subp => Overridden_Operation (Subp));
10947
10948 -- Otherwise the overriding routine inherits the implementation
10949 -- kind from the overridden subprogram.
10950
10951 else
10952 Inherit_Pragma_Implemented
10953 (Subp => Subp,
10954 Iface_Subp => Overridden_Operation (Subp));
10955 end if;
10956 end if;
10957
10958 -- If the operation is a wrapper for a synchronized primitive, it
10959 -- may be called indirectly through a dispatching select. We assume
10960 -- that it will be referenced elsewhere indirectly, and suppress
10961 -- warnings about an unused entity.
10962
10963 if Is_Primitive_Wrapper (Subp)
10964 and then Present (Wrapped_Entity (Subp))
10965 then
10966 Set_Referenced (Wrapped_Entity (Subp));
10967 end if;
10968
10969 Next_Elmt (Elmt);
10970 end loop;
10971 end Check_Abstract_Overriding;
10972
10973 ------------------------------------------------
10974 -- Check_Access_Discriminant_Requires_Limited --
10975 ------------------------------------------------
10976
10977 procedure Check_Access_Discriminant_Requires_Limited
10978 (D : Node_Id;
10979 Loc : Node_Id)
10980 is
10981 begin
10982 -- A discriminant_specification for an access discriminant shall appear
10983 -- only in the declaration for a task or protected type, or for a type
10984 -- with the reserved word 'limited' in its definition or in one of its
10985 -- ancestors (RM 3.7(10)).
10986
10987 -- AI-0063: The proper condition is that type must be immutably limited,
10988 -- or else be a partial view.
10989
10990 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10991 if Is_Limited_View (Current_Scope)
10992 or else
10993 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10994 and then Limited_Present (Parent (Current_Scope)))
10995 then
10996 null;
10997
10998 else
10999 Error_Msg_N
11000 ("access discriminants allowed only for limited types", Loc);
11001 end if;
11002 end if;
11003 end Check_Access_Discriminant_Requires_Limited;
11004
11005 -----------------------------------
11006 -- Check_Aliased_Component_Types --
11007 -----------------------------------
11008
11009 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11010 C : Entity_Id;
11011
11012 begin
11013 -- ??? Also need to check components of record extensions, but not
11014 -- components of protected types (which are always limited).
11015
11016 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11017 -- types to be unconstrained. This is safe because it is illegal to
11018 -- create access subtypes to such types with explicit discriminant
11019 -- constraints.
11020
11021 if not Is_Limited_Type (T) then
11022 if Ekind (T) = E_Record_Type then
11023 C := First_Component (T);
11024 while Present (C) loop
11025 if Is_Aliased (C)
11026 and then Has_Discriminants (Etype (C))
11027 and then not Is_Constrained (Etype (C))
11028 and then not In_Instance_Body
11029 and then Ada_Version < Ada_2005
11030 then
11031 Error_Msg_N
11032 ("aliased component must be constrained (RM 3.6(11))",
11033 C);
11034 end if;
11035
11036 Next_Component (C);
11037 end loop;
11038
11039 elsif Ekind (T) = E_Array_Type then
11040 if Has_Aliased_Components (T)
11041 and then Has_Discriminants (Component_Type (T))
11042 and then not Is_Constrained (Component_Type (T))
11043 and then not In_Instance_Body
11044 and then Ada_Version < Ada_2005
11045 then
11046 Error_Msg_N
11047 ("aliased component type must be constrained (RM 3.6(11))",
11048 T);
11049 end if;
11050 end if;
11051 end if;
11052 end Check_Aliased_Component_Types;
11053
11054 ---------------------------------------
11055 -- Check_Anonymous_Access_Components --
11056 ---------------------------------------
11057
11058 procedure Check_Anonymous_Access_Components
11059 (Typ_Decl : Node_Id;
11060 Typ : Entity_Id;
11061 Prev : Entity_Id;
11062 Comp_List : Node_Id)
11063 is
11064 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11065 Anon_Access : Entity_Id;
11066 Acc_Def : Node_Id;
11067 Comp : Node_Id;
11068 Comp_Def : Node_Id;
11069 Decl : Node_Id;
11070 Type_Def : Node_Id;
11071
11072 procedure Build_Incomplete_Type_Declaration;
11073 -- If the record type contains components that include an access to the
11074 -- current record, then create an incomplete type declaration for the
11075 -- record, to be used as the designated type of the anonymous access.
11076 -- This is done only once, and only if there is no previous partial
11077 -- view of the type.
11078
11079 function Designates_T (Subt : Node_Id) return Boolean;
11080 -- Check whether a node designates the enclosing record type, or 'Class
11081 -- of that type
11082
11083 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11084 -- Check whether an access definition includes a reference to
11085 -- the enclosing record type. The reference can be a subtype mark
11086 -- in the access definition itself, a 'Class attribute reference, or
11087 -- recursively a reference appearing in a parameter specification
11088 -- or result definition of an access_to_subprogram definition.
11089
11090 --------------------------------------
11091 -- Build_Incomplete_Type_Declaration --
11092 --------------------------------------
11093
11094 procedure Build_Incomplete_Type_Declaration is
11095 Decl : Node_Id;
11096 Inc_T : Entity_Id;
11097 H : Entity_Id;
11098
11099 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11100 -- it's "is new ... with record" or else "is tagged record ...".
11101
11102 Is_Tagged : constant Boolean :=
11103 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11104 and then
11105 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11106 or else
11107 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11108 and then Tagged_Present (Type_Definition (Typ_Decl)));
11109
11110 begin
11111 -- If there is a previous partial view, no need to create a new one
11112 -- If the partial view, given by Prev, is incomplete, If Prev is
11113 -- a private declaration, full declaration is flagged accordingly.
11114
11115 if Prev /= Typ then
11116 if Is_Tagged then
11117 Make_Class_Wide_Type (Prev);
11118 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11119 Set_Etype (Class_Wide_Type (Typ), Typ);
11120 end if;
11121
11122 return;
11123
11124 elsif Has_Private_Declaration (Typ) then
11125
11126 -- If we refer to T'Class inside T, and T is the completion of a
11127 -- private type, then make sure the class-wide type exists.
11128
11129 if Is_Tagged then
11130 Make_Class_Wide_Type (Typ);
11131 end if;
11132
11133 return;
11134
11135 -- If there was a previous anonymous access type, the incomplete
11136 -- type declaration will have been created already.
11137
11138 elsif Present (Current_Entity (Typ))
11139 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11140 and then Full_View (Current_Entity (Typ)) = Typ
11141 then
11142 if Is_Tagged
11143 and then Comes_From_Source (Current_Entity (Typ))
11144 and then not Is_Tagged_Type (Current_Entity (Typ))
11145 then
11146 Make_Class_Wide_Type (Typ);
11147 Error_Msg_N
11148 ("incomplete view of tagged type should be declared tagged??",
11149 Parent (Current_Entity (Typ)));
11150 end if;
11151 return;
11152
11153 else
11154 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11155 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11156
11157 -- Type has already been inserted into the current scope. Remove
11158 -- it, and add incomplete declaration for type, so that subsequent
11159 -- anonymous access types can use it. The entity is unchained from
11160 -- the homonym list and from immediate visibility. After analysis,
11161 -- the entity in the incomplete declaration becomes immediately
11162 -- visible in the record declaration that follows.
11163
11164 H := Current_Entity (Typ);
11165
11166 if H = Typ then
11167 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11168 else
11169 while Present (H)
11170 and then Homonym (H) /= Typ
11171 loop
11172 H := Homonym (Typ);
11173 end loop;
11174
11175 Set_Homonym (H, Homonym (Typ));
11176 end if;
11177
11178 Insert_Before (Typ_Decl, Decl);
11179 Analyze (Decl);
11180 Set_Full_View (Inc_T, Typ);
11181
11182 if Is_Tagged then
11183
11184 -- Create a common class-wide type for both views, and set the
11185 -- Etype of the class-wide type to the full view.
11186
11187 Make_Class_Wide_Type (Inc_T);
11188 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11189 Set_Etype (Class_Wide_Type (Typ), Typ);
11190 end if;
11191 end if;
11192 end Build_Incomplete_Type_Declaration;
11193
11194 ------------------
11195 -- Designates_T --
11196 ------------------
11197
11198 function Designates_T (Subt : Node_Id) return Boolean is
11199 Type_Id : constant Name_Id := Chars (Typ);
11200
11201 function Names_T (Nam : Node_Id) return Boolean;
11202 -- The record type has not been introduced in the current scope
11203 -- yet, so we must examine the name of the type itself, either
11204 -- an identifier T, or an expanded name of the form P.T, where
11205 -- P denotes the current scope.
11206
11207 -------------
11208 -- Names_T --
11209 -------------
11210
11211 function Names_T (Nam : Node_Id) return Boolean is
11212 begin
11213 if Nkind (Nam) = N_Identifier then
11214 return Chars (Nam) = Type_Id;
11215
11216 elsif Nkind (Nam) = N_Selected_Component then
11217 if Chars (Selector_Name (Nam)) = Type_Id then
11218 if Nkind (Prefix (Nam)) = N_Identifier then
11219 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11220
11221 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11222 return Chars (Selector_Name (Prefix (Nam))) =
11223 Chars (Current_Scope);
11224 else
11225 return False;
11226 end if;
11227
11228 else
11229 return False;
11230 end if;
11231
11232 else
11233 return False;
11234 end if;
11235 end Names_T;
11236
11237 -- Start of processing for Designates_T
11238
11239 begin
11240 if Nkind (Subt) = N_Identifier then
11241 return Chars (Subt) = Type_Id;
11242
11243 -- Reference can be through an expanded name which has not been
11244 -- analyzed yet, and which designates enclosing scopes.
11245
11246 elsif Nkind (Subt) = N_Selected_Component then
11247 if Names_T (Subt) then
11248 return True;
11249
11250 -- Otherwise it must denote an entity that is already visible.
11251 -- The access definition may name a subtype of the enclosing
11252 -- type, if there is a previous incomplete declaration for it.
11253
11254 else
11255 Find_Selected_Component (Subt);
11256 return
11257 Is_Entity_Name (Subt)
11258 and then Scope (Entity (Subt)) = Current_Scope
11259 and then
11260 (Chars (Base_Type (Entity (Subt))) = Type_Id
11261 or else
11262 (Is_Class_Wide_Type (Entity (Subt))
11263 and then
11264 Chars (Etype (Base_Type (Entity (Subt)))) =
11265 Type_Id));
11266 end if;
11267
11268 -- A reference to the current type may appear as the prefix of
11269 -- a 'Class attribute.
11270
11271 elsif Nkind (Subt) = N_Attribute_Reference
11272 and then Attribute_Name (Subt) = Name_Class
11273 then
11274 return Names_T (Prefix (Subt));
11275
11276 else
11277 return False;
11278 end if;
11279 end Designates_T;
11280
11281 ----------------
11282 -- Mentions_T --
11283 ----------------
11284
11285 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11286 Param_Spec : Node_Id;
11287
11288 Acc_Subprg : constant Node_Id :=
11289 Access_To_Subprogram_Definition (Acc_Def);
11290
11291 begin
11292 if No (Acc_Subprg) then
11293 return Designates_T (Subtype_Mark (Acc_Def));
11294 end if;
11295
11296 -- Component is an access_to_subprogram: examine its formals,
11297 -- and result definition in the case of an access_to_function.
11298
11299 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11300 while Present (Param_Spec) loop
11301 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11302 and then Mentions_T (Parameter_Type (Param_Spec))
11303 then
11304 return True;
11305
11306 elsif Designates_T (Parameter_Type (Param_Spec)) then
11307 return True;
11308 end if;
11309
11310 Next (Param_Spec);
11311 end loop;
11312
11313 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11314 if Nkind (Result_Definition (Acc_Subprg)) =
11315 N_Access_Definition
11316 then
11317 return Mentions_T (Result_Definition (Acc_Subprg));
11318 else
11319 return Designates_T (Result_Definition (Acc_Subprg));
11320 end if;
11321 end if;
11322
11323 return False;
11324 end Mentions_T;
11325
11326 -- Start of processing for Check_Anonymous_Access_Components
11327
11328 begin
11329 if No (Comp_List) then
11330 return;
11331 end if;
11332
11333 Comp := First (Component_Items (Comp_List));
11334 while Present (Comp) loop
11335 if Nkind (Comp) = N_Component_Declaration
11336 and then Present
11337 (Access_Definition (Component_Definition (Comp)))
11338 and then
11339 Mentions_T (Access_Definition (Component_Definition (Comp)))
11340 then
11341 Comp_Def := Component_Definition (Comp);
11342 Acc_Def :=
11343 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11344
11345 Build_Incomplete_Type_Declaration;
11346 Anon_Access := Make_Temporary (Loc, 'S');
11347
11348 -- Create a declaration for the anonymous access type: either
11349 -- an access_to_object or an access_to_subprogram.
11350
11351 if Present (Acc_Def) then
11352 if Nkind (Acc_Def) = N_Access_Function_Definition then
11353 Type_Def :=
11354 Make_Access_Function_Definition (Loc,
11355 Parameter_Specifications =>
11356 Parameter_Specifications (Acc_Def),
11357 Result_Definition => Result_Definition (Acc_Def));
11358 else
11359 Type_Def :=
11360 Make_Access_Procedure_Definition (Loc,
11361 Parameter_Specifications =>
11362 Parameter_Specifications (Acc_Def));
11363 end if;
11364
11365 else
11366 Type_Def :=
11367 Make_Access_To_Object_Definition (Loc,
11368 Subtype_Indication =>
11369 Relocate_Node
11370 (Subtype_Mark (Access_Definition (Comp_Def))));
11371
11372 Set_Constant_Present
11373 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11374 Set_All_Present
11375 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11376 end if;
11377
11378 Set_Null_Exclusion_Present
11379 (Type_Def,
11380 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11381
11382 Decl :=
11383 Make_Full_Type_Declaration (Loc,
11384 Defining_Identifier => Anon_Access,
11385 Type_Definition => Type_Def);
11386
11387 Insert_Before (Typ_Decl, Decl);
11388 Analyze (Decl);
11389
11390 -- If an access to subprogram, create the extra formals
11391
11392 if Present (Acc_Def) then
11393 Create_Extra_Formals (Designated_Type (Anon_Access));
11394
11395 -- If an access to object, preserve entity of designated type,
11396 -- for ASIS use, before rewriting the component definition.
11397 -- Is this still needed???
11398
11399 else
11400 declare
11401 Desig : Entity_Id;
11402
11403 begin
11404 Desig := Entity (Subtype_Indication (Type_Def));
11405
11406 -- If the access definition is to the current record,
11407 -- the visible entity at this point is an incomplete
11408 -- type. Retrieve the full view to simplify ASIS queries
11409
11410 if Ekind (Desig) = E_Incomplete_Type then
11411 Desig := Full_View (Desig);
11412 end if;
11413
11414 Set_Entity
11415 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11416 end;
11417 end if;
11418
11419 Rewrite (Comp_Def,
11420 Make_Component_Definition (Loc,
11421 Subtype_Indication =>
11422 New_Occurrence_Of (Anon_Access, Loc)));
11423
11424 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11425 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11426 else
11427 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11428 end if;
11429
11430 Set_Is_Local_Anonymous_Access (Anon_Access);
11431 end if;
11432
11433 Next (Comp);
11434 end loop;
11435
11436 if Present (Variant_Part (Comp_List)) then
11437 declare
11438 V : Node_Id;
11439 begin
11440 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11441 while Present (V) loop
11442 Check_Anonymous_Access_Components
11443 (Typ_Decl, Typ, Prev, Component_List (V));
11444 Next_Non_Pragma (V);
11445 end loop;
11446 end;
11447 end if;
11448 end Check_Anonymous_Access_Components;
11449
11450 ----------------------
11451 -- Check_Completion --
11452 ----------------------
11453
11454 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11455 E : Entity_Id;
11456
11457 procedure Post_Error;
11458 -- Post error message for lack of completion for entity E
11459
11460 ----------------
11461 -- Post_Error --
11462 ----------------
11463
11464 procedure Post_Error is
11465 procedure Missing_Body;
11466 -- Output missing body message
11467
11468 ------------------
11469 -- Missing_Body --
11470 ------------------
11471
11472 procedure Missing_Body is
11473 begin
11474 -- Spec is in same unit, so we can post on spec
11475
11476 if In_Same_Source_Unit (Body_Id, E) then
11477 Error_Msg_N ("missing body for &", E);
11478
11479 -- Spec is in a separate unit, so we have to post on the body
11480
11481 else
11482 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11483 end if;
11484 end Missing_Body;
11485
11486 -- Start of processing for Post_Error
11487
11488 begin
11489 if not Comes_From_Source (E) then
11490 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11491
11492 -- It may be an anonymous protected type created for a
11493 -- single variable. Post error on variable, if present.
11494
11495 declare
11496 Var : Entity_Id;
11497
11498 begin
11499 Var := First_Entity (Current_Scope);
11500 while Present (Var) loop
11501 exit when Etype (Var) = E
11502 and then Comes_From_Source (Var);
11503
11504 Next_Entity (Var);
11505 end loop;
11506
11507 if Present (Var) then
11508 E := Var;
11509 end if;
11510 end;
11511 end if;
11512 end if;
11513
11514 -- If a generated entity has no completion, then either previous
11515 -- semantic errors have disabled the expansion phase, or else we had
11516 -- missing subunits, or else we are compiling without expansion,
11517 -- or else something is very wrong.
11518
11519 if not Comes_From_Source (E) then
11520 pragma Assert
11521 (Serious_Errors_Detected > 0
11522 or else Configurable_Run_Time_Violations > 0
11523 or else Subunits_Missing
11524 or else not Expander_Active);
11525 return;
11526
11527 -- Here for source entity
11528
11529 else
11530 -- Here if no body to post the error message, so we post the error
11531 -- on the declaration that has no completion. This is not really
11532 -- the right place to post it, think about this later ???
11533
11534 if No (Body_Id) then
11535 if Is_Type (E) then
11536 Error_Msg_NE
11537 ("missing full declaration for }", Parent (E), E);
11538 else
11539 Error_Msg_NE ("missing body for &", Parent (E), E);
11540 end if;
11541
11542 -- Package body has no completion for a declaration that appears
11543 -- in the corresponding spec. Post error on the body, with a
11544 -- reference to the non-completed declaration.
11545
11546 else
11547 Error_Msg_Sloc := Sloc (E);
11548
11549 if Is_Type (E) then
11550 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11551
11552 elsif Is_Overloadable (E)
11553 and then Current_Entity_In_Scope (E) /= E
11554 then
11555 -- It may be that the completion is mistyped and appears as
11556 -- a distinct overloading of the entity.
11557
11558 declare
11559 Candidate : constant Entity_Id :=
11560 Current_Entity_In_Scope (E);
11561 Decl : constant Node_Id :=
11562 Unit_Declaration_Node (Candidate);
11563
11564 begin
11565 if Is_Overloadable (Candidate)
11566 and then Ekind (Candidate) = Ekind (E)
11567 and then Nkind (Decl) = N_Subprogram_Body
11568 and then Acts_As_Spec (Decl)
11569 then
11570 Check_Type_Conformant (Candidate, E);
11571
11572 else
11573 Missing_Body;
11574 end if;
11575 end;
11576
11577 else
11578 Missing_Body;
11579 end if;
11580 end if;
11581 end if;
11582 end Post_Error;
11583
11584 -- Local variables
11585
11586 Pack_Id : constant Entity_Id := Current_Scope;
11587
11588 -- Start of processing for Check_Completion
11589
11590 begin
11591 E := First_Entity (Pack_Id);
11592 while Present (E) loop
11593 if Is_Intrinsic_Subprogram (E) then
11594 null;
11595
11596 -- The following situation requires special handling: a child unit
11597 -- that appears in the context clause of the body of its parent:
11598
11599 -- procedure Parent.Child (...);
11600
11601 -- with Parent.Child;
11602 -- package body Parent is
11603
11604 -- Here Parent.Child appears as a local entity, but should not be
11605 -- flagged as requiring completion, because it is a compilation
11606 -- unit.
11607
11608 -- Ignore missing completion for a subprogram that does not come from
11609 -- source (including the _Call primitive operation of RAS types,
11610 -- which has to have the flag Comes_From_Source for other purposes):
11611 -- we assume that the expander will provide the missing completion.
11612 -- In case of previous errors, other expansion actions that provide
11613 -- bodies for null procedures with not be invoked, so inhibit message
11614 -- in those cases.
11615
11616 -- Note that E_Operator is not in the list that follows, because
11617 -- this kind is reserved for predefined operators, that are
11618 -- intrinsic and do not need completion.
11619
11620 elsif Ekind_In (E, E_Function,
11621 E_Procedure,
11622 E_Generic_Function,
11623 E_Generic_Procedure)
11624 then
11625 if Has_Completion (E) then
11626 null;
11627
11628 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11629 null;
11630
11631 elsif Is_Subprogram (E)
11632 and then (not Comes_From_Source (E)
11633 or else Chars (E) = Name_uCall)
11634 then
11635 null;
11636
11637 elsif
11638 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11639 then
11640 null;
11641
11642 elsif Nkind (Parent (E)) = N_Procedure_Specification
11643 and then Null_Present (Parent (E))
11644 and then Serious_Errors_Detected > 0
11645 then
11646 null;
11647
11648 else
11649 Post_Error;
11650 end if;
11651
11652 elsif Is_Entry (E) then
11653 if not Has_Completion (E) and then
11654 (Ekind (Scope (E)) = E_Protected_Object
11655 or else Ekind (Scope (E)) = E_Protected_Type)
11656 then
11657 Post_Error;
11658 end if;
11659
11660 elsif Is_Package_Or_Generic_Package (E) then
11661 if Unit_Requires_Body (E) then
11662 if not Has_Completion (E)
11663 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11664 N_Compilation_Unit
11665 then
11666 Post_Error;
11667 end if;
11668
11669 elsif not Is_Child_Unit (E) then
11670 May_Need_Implicit_Body (E);
11671 end if;
11672
11673 -- A formal incomplete type (Ada 2012) does not require a completion;
11674 -- other incomplete type declarations do.
11675
11676 elsif Ekind (E) = E_Incomplete_Type
11677 and then No (Underlying_Type (E))
11678 and then not Is_Generic_Type (E)
11679 then
11680 Post_Error;
11681
11682 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11683 and then not Has_Completion (E)
11684 then
11685 Post_Error;
11686
11687 -- A single task declared in the current scope is a constant, verify
11688 -- that the body of its anonymous type is in the same scope. If the
11689 -- task is defined elsewhere, this may be a renaming declaration for
11690 -- which no completion is needed.
11691
11692 elsif Ekind (E) = E_Constant
11693 and then Ekind (Etype (E)) = E_Task_Type
11694 and then not Has_Completion (Etype (E))
11695 and then Scope (Etype (E)) = Current_Scope
11696 then
11697 Post_Error;
11698
11699 elsif Ekind (E) = E_Protected_Object
11700 and then not Has_Completion (Etype (E))
11701 then
11702 Post_Error;
11703
11704 elsif Ekind (E) = E_Record_Type then
11705 if Is_Tagged_Type (E) then
11706 Check_Abstract_Overriding (E);
11707 Check_Conventions (E);
11708 end if;
11709
11710 Check_Aliased_Component_Types (E);
11711
11712 elsif Ekind (E) = E_Array_Type then
11713 Check_Aliased_Component_Types (E);
11714
11715 end if;
11716
11717 Next_Entity (E);
11718 end loop;
11719 end Check_Completion;
11720
11721 ------------------------------------
11722 -- Check_CPP_Type_Has_No_Defaults --
11723 ------------------------------------
11724
11725 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11726 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11727 Clist : Node_Id;
11728 Comp : Node_Id;
11729
11730 begin
11731 -- Obtain the component list
11732
11733 if Nkind (Tdef) = N_Record_Definition then
11734 Clist := Component_List (Tdef);
11735 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11736 Clist := Component_List (Record_Extension_Part (Tdef));
11737 end if;
11738
11739 -- Check all components to ensure no default expressions
11740
11741 if Present (Clist) then
11742 Comp := First (Component_Items (Clist));
11743 while Present (Comp) loop
11744 if Present (Expression (Comp)) then
11745 Error_Msg_N
11746 ("component of imported 'C'P'P type cannot have "
11747 & "default expression", Expression (Comp));
11748 end if;
11749
11750 Next (Comp);
11751 end loop;
11752 end if;
11753 end Check_CPP_Type_Has_No_Defaults;
11754
11755 ----------------------------
11756 -- Check_Delta_Expression --
11757 ----------------------------
11758
11759 procedure Check_Delta_Expression (E : Node_Id) is
11760 begin
11761 if not (Is_Real_Type (Etype (E))) then
11762 Wrong_Type (E, Any_Real);
11763
11764 elsif not Is_OK_Static_Expression (E) then
11765 Flag_Non_Static_Expr
11766 ("non-static expression used for delta value!", E);
11767
11768 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11769 Error_Msg_N ("delta expression must be positive", E);
11770
11771 else
11772 return;
11773 end if;
11774
11775 -- If any of above errors occurred, then replace the incorrect
11776 -- expression by the real 0.1, which should prevent further errors.
11777
11778 Rewrite (E,
11779 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11780 Analyze_And_Resolve (E, Standard_Float);
11781 end Check_Delta_Expression;
11782
11783 -----------------------------
11784 -- Check_Digits_Expression --
11785 -----------------------------
11786
11787 procedure Check_Digits_Expression (E : Node_Id) is
11788 begin
11789 if not (Is_Integer_Type (Etype (E))) then
11790 Wrong_Type (E, Any_Integer);
11791
11792 elsif not Is_OK_Static_Expression (E) then
11793 Flag_Non_Static_Expr
11794 ("non-static expression used for digits value!", E);
11795
11796 elsif Expr_Value (E) <= 0 then
11797 Error_Msg_N ("digits value must be greater than zero", E);
11798
11799 else
11800 return;
11801 end if;
11802
11803 -- If any of above errors occurred, then replace the incorrect
11804 -- expression by the integer 1, which should prevent further errors.
11805
11806 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11807 Analyze_And_Resolve (E, Standard_Integer);
11808
11809 end Check_Digits_Expression;
11810
11811 --------------------------
11812 -- Check_Initialization --
11813 --------------------------
11814
11815 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11816 begin
11817 -- Special processing for limited types
11818
11819 if Is_Limited_Type (T)
11820 and then not In_Instance
11821 and then not In_Inlined_Body
11822 then
11823 if not OK_For_Limited_Init (T, Exp) then
11824
11825 -- In GNAT mode, this is just a warning, to allow it to be evilly
11826 -- turned off. Otherwise it is a real error.
11827
11828 if GNAT_Mode then
11829 Error_Msg_N
11830 ("??cannot initialize entities of limited type!", Exp);
11831
11832 elsif Ada_Version < Ada_2005 then
11833
11834 -- The side effect removal machinery may generate illegal Ada
11835 -- code to avoid the usage of access types and 'reference in
11836 -- SPARK mode. Since this is legal code with respect to theorem
11837 -- proving, do not emit the error.
11838
11839 if GNATprove_Mode
11840 and then Nkind (Exp) = N_Function_Call
11841 and then Nkind (Parent (Exp)) = N_Object_Declaration
11842 and then not Comes_From_Source
11843 (Defining_Identifier (Parent (Exp)))
11844 then
11845 null;
11846
11847 else
11848 Error_Msg_N
11849 ("cannot initialize entities of limited type", Exp);
11850 Explain_Limited_Type (T, Exp);
11851 end if;
11852
11853 else
11854 -- Specialize error message according to kind of illegal
11855 -- initial expression. We check the Original_Node to cover
11856 -- cases where the initialization expression of an object
11857 -- declaration generated by the compiler has been rewritten
11858 -- (such as for dispatching calls).
11859
11860 if Nkind (Original_Node (Exp)) = N_Type_Conversion
11861 and then
11862 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
11863 then
11864 -- No error for internally-generated object declarations,
11865 -- which can come from build-in-place assignment statements.
11866
11867 if Nkind (Parent (Exp)) = N_Object_Declaration
11868 and then not Comes_From_Source
11869 (Defining_Identifier (Parent (Exp)))
11870 then
11871 null;
11872
11873 else
11874 Error_Msg_N
11875 ("illegal context for call to function with limited "
11876 & "result", Exp);
11877 end if;
11878
11879 else
11880 Error_Msg_N
11881 ("initialization of limited object requires aggregate or "
11882 & "function call", Exp);
11883 end if;
11884 end if;
11885 end if;
11886 end if;
11887
11888 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11889 -- set unless we can be sure that no range check is required.
11890
11891 if (GNATprove_Mode or not Expander_Active)
11892 and then Is_Scalar_Type (T)
11893 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11894 then
11895 Set_Do_Range_Check (Exp);
11896 end if;
11897 end Check_Initialization;
11898
11899 ----------------------
11900 -- Check_Interfaces --
11901 ----------------------
11902
11903 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11904 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11905
11906 Iface : Node_Id;
11907 Iface_Def : Node_Id;
11908 Iface_Typ : Entity_Id;
11909 Parent_Node : Node_Id;
11910
11911 Is_Task : Boolean := False;
11912 -- Set True if parent type or any progenitor is a task interface
11913
11914 Is_Protected : Boolean := False;
11915 -- Set True if parent type or any progenitor is a protected interface
11916
11917 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11918 -- Check that a progenitor is compatible with declaration. If an error
11919 -- message is output, it is posted on Error_Node.
11920
11921 ------------------
11922 -- Check_Ifaces --
11923 ------------------
11924
11925 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11926 Iface_Id : constant Entity_Id :=
11927 Defining_Identifier (Parent (Iface_Def));
11928 Type_Def : Node_Id;
11929
11930 begin
11931 if Nkind (N) = N_Private_Extension_Declaration then
11932 Type_Def := N;
11933 else
11934 Type_Def := Type_Definition (N);
11935 end if;
11936
11937 if Is_Task_Interface (Iface_Id) then
11938 Is_Task := True;
11939
11940 elsif Is_Protected_Interface (Iface_Id) then
11941 Is_Protected := True;
11942 end if;
11943
11944 if Is_Synchronized_Interface (Iface_Id) then
11945
11946 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11947 -- extension derived from a synchronized interface must explicitly
11948 -- be declared synchronized, because the full view will be a
11949 -- synchronized type.
11950
11951 if Nkind (N) = N_Private_Extension_Declaration then
11952 if not Synchronized_Present (N) then
11953 Error_Msg_NE
11954 ("private extension of& must be explicitly synchronized",
11955 N, Iface_Id);
11956 end if;
11957
11958 -- However, by 3.9.4(16/2), a full type that is a record extension
11959 -- is never allowed to derive from a synchronized interface (note
11960 -- that interfaces must be excluded from this check, because those
11961 -- are represented by derived type definitions in some cases).
11962
11963 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11964 and then not Interface_Present (Type_Definition (N))
11965 then
11966 Error_Msg_N ("record extension cannot derive from synchronized "
11967 & "interface", Error_Node);
11968 end if;
11969 end if;
11970
11971 -- Check that the characteristics of the progenitor are compatible
11972 -- with the explicit qualifier in the declaration.
11973 -- The check only applies to qualifiers that come from source.
11974 -- Limited_Present also appears in the declaration of corresponding
11975 -- records, and the check does not apply to them.
11976
11977 if Limited_Present (Type_Def)
11978 and then not
11979 Is_Concurrent_Record_Type (Defining_Identifier (N))
11980 then
11981 if Is_Limited_Interface (Parent_Type)
11982 and then not Is_Limited_Interface (Iface_Id)
11983 then
11984 Error_Msg_NE
11985 ("progenitor & must be limited interface",
11986 Error_Node, Iface_Id);
11987
11988 elsif
11989 (Task_Present (Iface_Def)
11990 or else Protected_Present (Iface_Def)
11991 or else Synchronized_Present (Iface_Def))
11992 and then Nkind (N) /= N_Private_Extension_Declaration
11993 and then not Error_Posted (N)
11994 then
11995 Error_Msg_NE
11996 ("progenitor & must be limited interface",
11997 Error_Node, Iface_Id);
11998 end if;
11999
12000 -- Protected interfaces can only inherit from limited, synchronized
12001 -- or protected interfaces.
12002
12003 elsif Nkind (N) = N_Full_Type_Declaration
12004 and then Protected_Present (Type_Def)
12005 then
12006 if Limited_Present (Iface_Def)
12007 or else Synchronized_Present (Iface_Def)
12008 or else Protected_Present (Iface_Def)
12009 then
12010 null;
12011
12012 elsif Task_Present (Iface_Def) then
12013 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12014 & "from task interface", Error_Node);
12015
12016 else
12017 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12018 & "from non-limited interface", Error_Node);
12019 end if;
12020
12021 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12022 -- limited and synchronized.
12023
12024 elsif Synchronized_Present (Type_Def) then
12025 if Limited_Present (Iface_Def)
12026 or else Synchronized_Present (Iface_Def)
12027 then
12028 null;
12029
12030 elsif Protected_Present (Iface_Def)
12031 and then Nkind (N) /= N_Private_Extension_Declaration
12032 then
12033 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12034 & "from protected interface", Error_Node);
12035
12036 elsif Task_Present (Iface_Def)
12037 and then Nkind (N) /= N_Private_Extension_Declaration
12038 then
12039 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12040 & "from task interface", Error_Node);
12041
12042 elsif not Is_Limited_Interface (Iface_Id) then
12043 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12044 & "from non-limited interface", Error_Node);
12045 end if;
12046
12047 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12048 -- synchronized or task interfaces.
12049
12050 elsif Nkind (N) = N_Full_Type_Declaration
12051 and then Task_Present (Type_Def)
12052 then
12053 if Limited_Present (Iface_Def)
12054 or else Synchronized_Present (Iface_Def)
12055 or else Task_Present (Iface_Def)
12056 then
12057 null;
12058
12059 elsif Protected_Present (Iface_Def) then
12060 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12061 & "protected interface", Error_Node);
12062
12063 else
12064 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12065 & "non-limited interface", Error_Node);
12066 end if;
12067 end if;
12068 end Check_Ifaces;
12069
12070 -- Start of processing for Check_Interfaces
12071
12072 begin
12073 if Is_Interface (Parent_Type) then
12074 if Is_Task_Interface (Parent_Type) then
12075 Is_Task := True;
12076
12077 elsif Is_Protected_Interface (Parent_Type) then
12078 Is_Protected := True;
12079 end if;
12080 end if;
12081
12082 if Nkind (N) = N_Private_Extension_Declaration then
12083
12084 -- Check that progenitors are compatible with declaration
12085
12086 Iface := First (Interface_List (Def));
12087 while Present (Iface) loop
12088 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12089
12090 Parent_Node := Parent (Base_Type (Iface_Typ));
12091 Iface_Def := Type_Definition (Parent_Node);
12092
12093 if not Is_Interface (Iface_Typ) then
12094 Diagnose_Interface (Iface, Iface_Typ);
12095 else
12096 Check_Ifaces (Iface_Def, Iface);
12097 end if;
12098
12099 Next (Iface);
12100 end loop;
12101
12102 if Is_Task and Is_Protected then
12103 Error_Msg_N
12104 ("type cannot derive from task and protected interface", N);
12105 end if;
12106
12107 return;
12108 end if;
12109
12110 -- Full type declaration of derived type.
12111 -- Check compatibility with parent if it is interface type
12112
12113 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12114 and then Is_Interface (Parent_Type)
12115 then
12116 Parent_Node := Parent (Parent_Type);
12117
12118 -- More detailed checks for interface varieties
12119
12120 Check_Ifaces
12121 (Iface_Def => Type_Definition (Parent_Node),
12122 Error_Node => Subtype_Indication (Type_Definition (N)));
12123 end if;
12124
12125 Iface := First (Interface_List (Def));
12126 while Present (Iface) loop
12127 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12128
12129 Parent_Node := Parent (Base_Type (Iface_Typ));
12130 Iface_Def := Type_Definition (Parent_Node);
12131
12132 if not Is_Interface (Iface_Typ) then
12133 Diagnose_Interface (Iface, Iface_Typ);
12134
12135 else
12136 -- "The declaration of a specific descendant of an interface
12137 -- type freezes the interface type" RM 13.14
12138
12139 Freeze_Before (N, Iface_Typ);
12140 Check_Ifaces (Iface_Def, Error_Node => Iface);
12141 end if;
12142
12143 Next (Iface);
12144 end loop;
12145
12146 if Is_Task and Is_Protected then
12147 Error_Msg_N
12148 ("type cannot derive from task and protected interface", N);
12149 end if;
12150 end Check_Interfaces;
12151
12152 ------------------------------------
12153 -- Check_Or_Process_Discriminants --
12154 ------------------------------------
12155
12156 -- If an incomplete or private type declaration was already given for the
12157 -- type, the discriminants may have already been processed if they were
12158 -- present on the incomplete declaration. In this case a full conformance
12159 -- check has been performed in Find_Type_Name, and we then recheck here
12160 -- some properties that can't be checked on the partial view alone.
12161 -- Otherwise we call Process_Discriminants.
12162
12163 procedure Check_Or_Process_Discriminants
12164 (N : Node_Id;
12165 T : Entity_Id;
12166 Prev : Entity_Id := Empty)
12167 is
12168 begin
12169 if Has_Discriminants (T) then
12170
12171 -- Discriminants are already set on T if they were already present
12172 -- on the partial view. Make them visible to component declarations.
12173
12174 declare
12175 D : Entity_Id;
12176 -- Discriminant on T (full view) referencing expr on partial view
12177
12178 Prev_D : Entity_Id;
12179 -- Entity of corresponding discriminant on partial view
12180
12181 New_D : Node_Id;
12182 -- Discriminant specification for full view, expression is
12183 -- the syntactic copy on full view (which has been checked for
12184 -- conformance with partial view), only used here to post error
12185 -- message.
12186
12187 begin
12188 D := First_Discriminant (T);
12189 New_D := First (Discriminant_Specifications (N));
12190 while Present (D) loop
12191 Prev_D := Current_Entity (D);
12192 Set_Current_Entity (D);
12193 Set_Is_Immediately_Visible (D);
12194 Set_Homonym (D, Prev_D);
12195
12196 -- Handle the case where there is an untagged partial view and
12197 -- the full view is tagged: must disallow discriminants with
12198 -- defaults, unless compiling for Ada 2012, which allows a
12199 -- limited tagged type to have defaulted discriminants (see
12200 -- AI05-0214). However, suppress error here if it was already
12201 -- reported on the default expression of the partial view.
12202
12203 if Is_Tagged_Type (T)
12204 and then Present (Expression (Parent (D)))
12205 and then (not Is_Limited_Type (Current_Scope)
12206 or else Ada_Version < Ada_2012)
12207 and then not Error_Posted (Expression (Parent (D)))
12208 then
12209 if Ada_Version >= Ada_2012 then
12210 Error_Msg_N
12211 ("discriminants of nonlimited tagged type cannot have "
12212 & "defaults",
12213 Expression (New_D));
12214 else
12215 Error_Msg_N
12216 ("discriminants of tagged type cannot have defaults",
12217 Expression (New_D));
12218 end if;
12219 end if;
12220
12221 -- Ada 2005 (AI-230): Access discriminant allowed in
12222 -- non-limited record types.
12223
12224 if Ada_Version < Ada_2005 then
12225
12226 -- This restriction gets applied to the full type here. It
12227 -- has already been applied earlier to the partial view.
12228
12229 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12230 end if;
12231
12232 Next_Discriminant (D);
12233 Next (New_D);
12234 end loop;
12235 end;
12236
12237 elsif Present (Discriminant_Specifications (N)) then
12238 Process_Discriminants (N, Prev);
12239 end if;
12240 end Check_Or_Process_Discriminants;
12241
12242 ----------------------
12243 -- Check_Real_Bound --
12244 ----------------------
12245
12246 procedure Check_Real_Bound (Bound : Node_Id) is
12247 begin
12248 if not Is_Real_Type (Etype (Bound)) then
12249 Error_Msg_N
12250 ("bound in real type definition must be of real type", Bound);
12251
12252 elsif not Is_OK_Static_Expression (Bound) then
12253 Flag_Non_Static_Expr
12254 ("non-static expression used for real type bound!", Bound);
12255
12256 else
12257 return;
12258 end if;
12259
12260 Rewrite
12261 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12262 Analyze (Bound);
12263 Resolve (Bound, Standard_Float);
12264 end Check_Real_Bound;
12265
12266 ------------------------------
12267 -- Complete_Private_Subtype --
12268 ------------------------------
12269
12270 procedure Complete_Private_Subtype
12271 (Priv : Entity_Id;
12272 Full : Entity_Id;
12273 Full_Base : Entity_Id;
12274 Related_Nod : Node_Id)
12275 is
12276 Save_Next_Entity : Entity_Id;
12277 Save_Homonym : Entity_Id;
12278
12279 begin
12280 -- Set semantic attributes for (implicit) private subtype completion.
12281 -- If the full type has no discriminants, then it is a copy of the
12282 -- full view of the base. Otherwise, it is a subtype of the base with
12283 -- a possible discriminant constraint. Save and restore the original
12284 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12285 -- not corrupt the entity chain.
12286
12287 Save_Next_Entity := Next_Entity (Full);
12288 Save_Homonym := Homonym (Priv);
12289
12290 if Is_Private_Type (Full_Base)
12291 or else Is_Record_Type (Full_Base)
12292 or else Is_Concurrent_Type (Full_Base)
12293 then
12294 Copy_Node (Priv, Full);
12295
12296 -- Note that the Etype of the full view is the same as the Etype of
12297 -- the partial view. In this fashion, the subtype has access to the
12298 -- correct view of the parent.
12299
12300 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12301 Set_Has_Unknown_Discriminants
12302 (Full, Has_Unknown_Discriminants (Full_Base));
12303 Set_First_Entity (Full, First_Entity (Full_Base));
12304 Set_Last_Entity (Full, Last_Entity (Full_Base));
12305
12306 -- If the underlying base type is constrained, we know that the
12307 -- full view of the subtype is constrained as well (the converse
12308 -- is not necessarily true).
12309
12310 if Is_Constrained (Full_Base) then
12311 Set_Is_Constrained (Full);
12312 end if;
12313
12314 else
12315 Copy_Node (Full_Base, Full);
12316
12317 -- The following subtlety with the Etype of the full view needs to be
12318 -- taken into account here. One could think that it must naturally be
12319 -- set to the base type of the full base:
12320
12321 -- Set_Etype (Full, Base_Type (Full_Base));
12322
12323 -- so that the full view becomes a subtype of the full base when the
12324 -- latter is a base type, which must for example happen when the full
12325 -- base is declared as derived type. That's also correct if the full
12326 -- base is declared as an array type, or a floating-point type, or a
12327 -- fixed-point type, or a signed integer type, as these declarations
12328 -- create an implicit base type and a first subtype so the Etype of
12329 -- the full views must be the implicit base type. But that's wrong
12330 -- if the full base is declared as an access type, or an enumeration
12331 -- type, or a modular integer type, as these declarations directly
12332 -- create a base type, i.e. with Etype pointing to itself. Moreover
12333 -- the full base being declared in the private part, i.e. when the
12334 -- views are swapped, the end result is that the Etype of the full
12335 -- base is set to its private view in this case and that we need to
12336 -- propagate this setting to the full view in order for the subtype
12337 -- to be compatible with the base type.
12338
12339 if Is_Base_Type (Full_Base)
12340 and then (Is_Derived_Type (Full_Base)
12341 or else Ekind (Full_Base) in Array_Kind
12342 or else Ekind (Full_Base) in Fixed_Point_Kind
12343 or else Ekind (Full_Base) in Float_Kind
12344 or else Ekind (Full_Base) in Signed_Integer_Kind)
12345 then
12346 Set_Etype (Full, Full_Base);
12347 end if;
12348
12349 Set_Chars (Full, Chars (Priv));
12350 Set_Sloc (Full, Sloc (Priv));
12351 Conditional_Delay (Full, Priv);
12352 end if;
12353
12354 Link_Entities (Full, Save_Next_Entity);
12355 Set_Homonym (Full, Save_Homonym);
12356 Set_Associated_Node_For_Itype (Full, Related_Nod);
12357
12358 -- Set common attributes for all subtypes: kind, convention, etc.
12359
12360 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12361 Set_Convention (Full, Convention (Full_Base));
12362 Set_Is_First_Subtype (Full, False);
12363 Set_Scope (Full, Scope (Priv));
12364 Set_Size_Info (Full, Full_Base);
12365 Set_RM_Size (Full, RM_Size (Full_Base));
12366 Set_Is_Itype (Full);
12367
12368 -- A subtype of a private-type-without-discriminants, whose full-view
12369 -- has discriminants with default expressions, is not constrained.
12370
12371 if not Has_Discriminants (Priv) then
12372 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12373
12374 if Has_Discriminants (Full_Base) then
12375 Set_Discriminant_Constraint
12376 (Full, Discriminant_Constraint (Full_Base));
12377
12378 -- The partial view may have been indefinite, the full view
12379 -- might not be.
12380
12381 Set_Has_Unknown_Discriminants
12382 (Full, Has_Unknown_Discriminants (Full_Base));
12383 end if;
12384 end if;
12385
12386 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12387 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12388
12389 -- Freeze the private subtype entity if its parent is delayed, and not
12390 -- already frozen. We skip this processing if the type is an anonymous
12391 -- subtype of a record component, or is the corresponding record of a
12392 -- protected type, since these are processed when the enclosing type
12393 -- is frozen. If the parent type is declared in a nested package then
12394 -- the freezing of the private and full views also happens later.
12395
12396 if not Is_Type (Scope (Full)) then
12397 if Is_Itype (Priv)
12398 and then In_Same_Source_Unit (Full, Full_Base)
12399 and then Scope (Full_Base) /= Scope (Full)
12400 then
12401 Set_Has_Delayed_Freeze (Full);
12402 Set_Has_Delayed_Freeze (Priv);
12403
12404 else
12405 Set_Has_Delayed_Freeze (Full,
12406 Has_Delayed_Freeze (Full_Base)
12407 and then not Is_Frozen (Full_Base));
12408 end if;
12409 end if;
12410
12411 Set_Freeze_Node (Full, Empty);
12412 Set_Is_Frozen (Full, False);
12413
12414 if Has_Discriminants (Full) then
12415 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12416 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12417
12418 if Has_Unknown_Discriminants (Full) then
12419 Set_Discriminant_Constraint (Full, No_Elist);
12420 end if;
12421 end if;
12422
12423 if Ekind (Full_Base) = E_Record_Type
12424 and then Has_Discriminants (Full_Base)
12425 and then Has_Discriminants (Priv) -- might not, if errors
12426 and then not Has_Unknown_Discriminants (Priv)
12427 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12428 then
12429 Create_Constrained_Components
12430 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12431
12432 -- If the full base is itself derived from private, build a congruent
12433 -- subtype of its underlying full view, for use by the back end.
12434
12435 elsif Is_Private_Type (Full_Base)
12436 and then Present (Underlying_Full_View (Full_Base))
12437 then
12438 declare
12439 Underlying_Full_Base : constant Entity_Id
12440 := Underlying_Full_View (Full_Base);
12441 Underlying_Full : constant Entity_Id
12442 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12443 begin
12444 Set_Is_Itype (Underlying_Full);
12445 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12446 Complete_Private_Subtype
12447 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12448 Set_Underlying_Full_View (Full, Underlying_Full);
12449 Set_Is_Underlying_Full_View (Underlying_Full);
12450 end;
12451
12452 elsif Is_Record_Type (Full_Base) then
12453
12454 -- Show Full is simply a renaming of Full_Base
12455
12456 Set_Cloned_Subtype (Full, Full_Base);
12457
12458 -- Propagate predicates
12459
12460 if Has_Predicates (Full_Base) then
12461 Set_Has_Predicates (Full);
12462
12463 if Present (Predicate_Function (Full_Base))
12464 and then No (Predicate_Function (Full))
12465 then
12466 Set_Predicate_Function (Full, Predicate_Function (Full_Base));
12467 end if;
12468 end if;
12469 end if;
12470
12471 -- It is unsafe to share the bounds of a scalar type, because the Itype
12472 -- is elaborated on demand, and if a bound is nonstatic, then different
12473 -- orders of elaboration in different units will lead to different
12474 -- external symbols.
12475
12476 if Is_Scalar_Type (Full_Base) then
12477 Set_Scalar_Range (Full,
12478 Make_Range (Sloc (Related_Nod),
12479 Low_Bound =>
12480 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12481 High_Bound =>
12482 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12483
12484 -- This completion inherits the bounds of the full parent, but if
12485 -- the parent is an unconstrained floating point type, so is the
12486 -- completion.
12487
12488 if Is_Floating_Point_Type (Full_Base) then
12489 Set_Includes_Infinities
12490 (Scalar_Range (Full), Has_Infinities (Full_Base));
12491 end if;
12492 end if;
12493
12494 -- ??? It seems that a lot of fields are missing that should be copied
12495 -- from Full_Base to Full. Here are some that are introduced in a
12496 -- non-disruptive way but a cleanup is necessary.
12497
12498 if Is_Tagged_Type (Full_Base) then
12499 Set_Is_Tagged_Type (Full);
12500 Set_Direct_Primitive_Operations
12501 (Full, Direct_Primitive_Operations (Full_Base));
12502 Set_No_Tagged_Streams_Pragma
12503 (Full, No_Tagged_Streams_Pragma (Full_Base));
12504
12505 -- Inherit class_wide type of full_base in case the partial view was
12506 -- not tagged. Otherwise it has already been created when the private
12507 -- subtype was analyzed.
12508
12509 if No (Class_Wide_Type (Full)) then
12510 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12511 end if;
12512
12513 -- If this is a subtype of a protected or task type, constrain its
12514 -- corresponding record, unless this is a subtype without constraints,
12515 -- i.e. a simple renaming as with an actual subtype in an instance.
12516
12517 elsif Is_Concurrent_Type (Full_Base) then
12518 if Has_Discriminants (Full)
12519 and then Present (Corresponding_Record_Type (Full_Base))
12520 and then
12521 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12522 then
12523 Set_Corresponding_Record_Type (Full,
12524 Constrain_Corresponding_Record
12525 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12526
12527 else
12528 Set_Corresponding_Record_Type (Full,
12529 Corresponding_Record_Type (Full_Base));
12530 end if;
12531 end if;
12532
12533 -- Link rep item chain, and also setting of Has_Predicates from private
12534 -- subtype to full subtype, since we will need these on the full subtype
12535 -- to create the predicate function. Note that the full subtype may
12536 -- already have rep items, inherited from the full view of the base
12537 -- type, so we must be sure not to overwrite these entries.
12538
12539 declare
12540 Append : Boolean;
12541 Item : Node_Id;
12542 Next_Item : Node_Id;
12543 Priv_Item : Node_Id;
12544
12545 begin
12546 Item := First_Rep_Item (Full);
12547 Priv_Item := First_Rep_Item (Priv);
12548
12549 -- If no existing rep items on full type, we can just link directly
12550 -- to the list of items on the private type, if any exist.. Same if
12551 -- the rep items are only those inherited from the base
12552
12553 if (No (Item)
12554 or else Nkind (Item) /= N_Aspect_Specification
12555 or else Entity (Item) = Full_Base)
12556 and then Present (First_Rep_Item (Priv))
12557 then
12558 Set_First_Rep_Item (Full, Priv_Item);
12559
12560 -- Otherwise, search to the end of items currently linked to the full
12561 -- subtype and append the private items to the end. However, if Priv
12562 -- and Full already have the same list of rep items, then the append
12563 -- is not done, as that would create a circularity.
12564 --
12565 -- The partial view may have a predicate and the rep item lists of
12566 -- both views agree when inherited from the same ancestor. In that
12567 -- case, simply propagate the list from one view to the other.
12568 -- A more complex analysis needed here ???
12569
12570 elsif Present (Priv_Item)
12571 and then Item = Next_Rep_Item (Priv_Item)
12572 then
12573 Set_First_Rep_Item (Full, Priv_Item);
12574
12575 elsif Item /= Priv_Item then
12576 Append := True;
12577 loop
12578 Next_Item := Next_Rep_Item (Item);
12579 exit when No (Next_Item);
12580 Item := Next_Item;
12581
12582 -- If the private view has aspect specifications, the full view
12583 -- inherits them. Since these aspects may already have been
12584 -- attached to the full view during derivation, do not append
12585 -- them if already present.
12586
12587 if Item = First_Rep_Item (Priv) then
12588 Append := False;
12589 exit;
12590 end if;
12591 end loop;
12592
12593 -- And link the private type items at the end of the chain
12594
12595 if Append then
12596 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12597 end if;
12598 end if;
12599 end;
12600
12601 -- Make sure Has_Predicates is set on full type if it is set on the
12602 -- private type. Note that it may already be set on the full type and
12603 -- if so, we don't want to unset it. Similarly, propagate information
12604 -- about delayed aspects, because the corresponding pragmas must be
12605 -- analyzed when one of the views is frozen. This last step is needed
12606 -- in particular when the full type is a scalar type for which an
12607 -- anonymous base type is constructed.
12608
12609 -- The predicate functions are generated either at the freeze point
12610 -- of the type or at the end of the visible part, and we must avoid
12611 -- generating them twice.
12612
12613 if Has_Predicates (Priv) then
12614 Set_Has_Predicates (Full);
12615
12616 if Present (Predicate_Function (Priv))
12617 and then No (Predicate_Function (Full))
12618 then
12619 Set_Predicate_Function (Full, Predicate_Function (Priv));
12620 end if;
12621 end if;
12622
12623 if Has_Delayed_Aspects (Priv) then
12624 Set_Has_Delayed_Aspects (Full);
12625 end if;
12626 end Complete_Private_Subtype;
12627
12628 ----------------------------
12629 -- Constant_Redeclaration --
12630 ----------------------------
12631
12632 procedure Constant_Redeclaration
12633 (Id : Entity_Id;
12634 N : Node_Id;
12635 T : out Entity_Id)
12636 is
12637 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12638 Obj_Def : constant Node_Id := Object_Definition (N);
12639 New_T : Entity_Id;
12640
12641 procedure Check_Possible_Deferred_Completion
12642 (Prev_Id : Entity_Id;
12643 Prev_Obj_Def : Node_Id;
12644 Curr_Obj_Def : Node_Id);
12645 -- Determine whether the two object definitions describe the partial
12646 -- and the full view of a constrained deferred constant. Generate
12647 -- a subtype for the full view and verify that it statically matches
12648 -- the subtype of the partial view.
12649
12650 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12651 -- If deferred constant is an access type initialized with an allocator,
12652 -- check whether there is an illegal recursion in the definition,
12653 -- through a default value of some record subcomponent. This is normally
12654 -- detected when generating init procs, but requires this additional
12655 -- mechanism when expansion is disabled.
12656
12657 ----------------------------------------
12658 -- Check_Possible_Deferred_Completion --
12659 ----------------------------------------
12660
12661 procedure Check_Possible_Deferred_Completion
12662 (Prev_Id : Entity_Id;
12663 Prev_Obj_Def : Node_Id;
12664 Curr_Obj_Def : Node_Id)
12665 is
12666 begin
12667 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12668 and then Present (Constraint (Prev_Obj_Def))
12669 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12670 and then Present (Constraint (Curr_Obj_Def))
12671 then
12672 declare
12673 Loc : constant Source_Ptr := Sloc (N);
12674 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12675 Decl : constant Node_Id :=
12676 Make_Subtype_Declaration (Loc,
12677 Defining_Identifier => Def_Id,
12678 Subtype_Indication =>
12679 Relocate_Node (Curr_Obj_Def));
12680
12681 begin
12682 Insert_Before_And_Analyze (N, Decl);
12683 Set_Etype (Id, Def_Id);
12684
12685 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12686 Error_Msg_Sloc := Sloc (Prev_Id);
12687 Error_Msg_N ("subtype does not statically match deferred "
12688 & "declaration #", N);
12689 end if;
12690 end;
12691 end if;
12692 end Check_Possible_Deferred_Completion;
12693
12694 ---------------------------------
12695 -- Check_Recursive_Declaration --
12696 ---------------------------------
12697
12698 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12699 Comp : Entity_Id;
12700
12701 begin
12702 if Is_Record_Type (Typ) then
12703 Comp := First_Component (Typ);
12704 while Present (Comp) loop
12705 if Comes_From_Source (Comp) then
12706 if Present (Expression (Parent (Comp)))
12707 and then Is_Entity_Name (Expression (Parent (Comp)))
12708 and then Entity (Expression (Parent (Comp))) = Prev
12709 then
12710 Error_Msg_Sloc := Sloc (Parent (Comp));
12711 Error_Msg_NE
12712 ("illegal circularity with declaration for & #",
12713 N, Comp);
12714 return;
12715
12716 elsif Is_Record_Type (Etype (Comp)) then
12717 Check_Recursive_Declaration (Etype (Comp));
12718 end if;
12719 end if;
12720
12721 Next_Component (Comp);
12722 end loop;
12723 end if;
12724 end Check_Recursive_Declaration;
12725
12726 -- Start of processing for Constant_Redeclaration
12727
12728 begin
12729 if Nkind (Parent (Prev)) = N_Object_Declaration then
12730 if Nkind (Object_Definition
12731 (Parent (Prev))) = N_Subtype_Indication
12732 then
12733 -- Find type of new declaration. The constraints of the two
12734 -- views must match statically, but there is no point in
12735 -- creating an itype for the full view.
12736
12737 if Nkind (Obj_Def) = N_Subtype_Indication then
12738 Find_Type (Subtype_Mark (Obj_Def));
12739 New_T := Entity (Subtype_Mark (Obj_Def));
12740
12741 else
12742 Find_Type (Obj_Def);
12743 New_T := Entity (Obj_Def);
12744 end if;
12745
12746 T := Etype (Prev);
12747
12748 else
12749 -- The full view may impose a constraint, even if the partial
12750 -- view does not, so construct the subtype.
12751
12752 New_T := Find_Type_Of_Object (Obj_Def, N);
12753 T := New_T;
12754 end if;
12755
12756 else
12757 -- Current declaration is illegal, diagnosed below in Enter_Name
12758
12759 T := Empty;
12760 New_T := Any_Type;
12761 end if;
12762
12763 -- If previous full declaration or a renaming declaration exists, or if
12764 -- a homograph is present, let Enter_Name handle it, either with an
12765 -- error or with the removal of an overridden implicit subprogram.
12766 -- The previous one is a full declaration if it has an expression
12767 -- (which in the case of an aggregate is indicated by the Init flag).
12768
12769 if Ekind (Prev) /= E_Constant
12770 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12771 or else Present (Expression (Parent (Prev)))
12772 or else Has_Init_Expression (Parent (Prev))
12773 or else Present (Full_View (Prev))
12774 then
12775 Enter_Name (Id);
12776
12777 -- Verify that types of both declarations match, or else that both types
12778 -- are anonymous access types whose designated subtypes statically match
12779 -- (as allowed in Ada 2005 by AI-385).
12780
12781 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12782 and then
12783 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12784 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12785 or else Is_Access_Constant (Etype (New_T)) /=
12786 Is_Access_Constant (Etype (Prev))
12787 or else Can_Never_Be_Null (Etype (New_T)) /=
12788 Can_Never_Be_Null (Etype (Prev))
12789 or else Null_Exclusion_Present (Parent (Prev)) /=
12790 Null_Exclusion_Present (Parent (Id))
12791 or else not Subtypes_Statically_Match
12792 (Designated_Type (Etype (Prev)),
12793 Designated_Type (Etype (New_T))))
12794 then
12795 Error_Msg_Sloc := Sloc (Prev);
12796 Error_Msg_N ("type does not match declaration#", N);
12797 Set_Full_View (Prev, Id);
12798 Set_Etype (Id, Any_Type);
12799
12800 -- A deferred constant whose type is an anonymous array is always
12801 -- illegal (unless imported). A detailed error message might be
12802 -- helpful for Ada beginners.
12803
12804 if Nkind (Object_Definition (Parent (Prev)))
12805 = N_Constrained_Array_Definition
12806 and then Nkind (Object_Definition (N))
12807 = N_Constrained_Array_Definition
12808 then
12809 Error_Msg_N ("\each anonymous array is a distinct type", N);
12810 Error_Msg_N ("a deferred constant must have a named type",
12811 Object_Definition (Parent (Prev)));
12812 end if;
12813
12814 elsif
12815 Null_Exclusion_Present (Parent (Prev))
12816 and then not Null_Exclusion_Present (N)
12817 then
12818 Error_Msg_Sloc := Sloc (Prev);
12819 Error_Msg_N ("null-exclusion does not match declaration#", N);
12820 Set_Full_View (Prev, Id);
12821 Set_Etype (Id, Any_Type);
12822
12823 -- If so, process the full constant declaration
12824
12825 else
12826 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12827 -- the deferred declaration is constrained, then the subtype defined
12828 -- by the subtype_indication in the full declaration shall match it
12829 -- statically.
12830
12831 Check_Possible_Deferred_Completion
12832 (Prev_Id => Prev,
12833 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12834 Curr_Obj_Def => Obj_Def);
12835
12836 Set_Full_View (Prev, Id);
12837 Set_Is_Public (Id, Is_Public (Prev));
12838 Set_Is_Internal (Id);
12839 Append_Entity (Id, Current_Scope);
12840
12841 -- Check ALIASED present if present before (RM 7.4(7))
12842
12843 if Is_Aliased (Prev)
12844 and then not Aliased_Present (N)
12845 then
12846 Error_Msg_Sloc := Sloc (Prev);
12847 Error_Msg_N ("ALIASED required (see declaration #)", N);
12848 end if;
12849
12850 -- Check that placement is in private part and that the incomplete
12851 -- declaration appeared in the visible part.
12852
12853 if Ekind (Current_Scope) = E_Package
12854 and then not In_Private_Part (Current_Scope)
12855 then
12856 Error_Msg_Sloc := Sloc (Prev);
12857 Error_Msg_N
12858 ("full constant for declaration # must be in private part", N);
12859
12860 elsif Ekind (Current_Scope) = E_Package
12861 and then
12862 List_Containing (Parent (Prev)) /=
12863 Visible_Declarations (Package_Specification (Current_Scope))
12864 then
12865 Error_Msg_N
12866 ("deferred constant must be declared in visible part",
12867 Parent (Prev));
12868 end if;
12869
12870 if Is_Access_Type (T)
12871 and then Nkind (Expression (N)) = N_Allocator
12872 then
12873 Check_Recursive_Declaration (Designated_Type (T));
12874 end if;
12875
12876 -- A deferred constant is a visible entity. If type has invariants,
12877 -- verify that the initial value satisfies them. This is not done in
12878 -- GNATprove mode, as GNATprove handles invariant checks itself.
12879
12880 if Has_Invariants (T)
12881 and then Present (Invariant_Procedure (T))
12882 and then not GNATprove_Mode
12883 then
12884 Insert_After (N,
12885 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12886 end if;
12887 end if;
12888 end Constant_Redeclaration;
12889
12890 ----------------------
12891 -- Constrain_Access --
12892 ----------------------
12893
12894 procedure Constrain_Access
12895 (Def_Id : in out Entity_Id;
12896 S : Node_Id;
12897 Related_Nod : Node_Id)
12898 is
12899 T : constant Entity_Id := Entity (Subtype_Mark (S));
12900 Desig_Type : constant Entity_Id := Designated_Type (T);
12901 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12902 Constraint_OK : Boolean := True;
12903
12904 begin
12905 if Is_Array_Type (Desig_Type) then
12906 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12907
12908 elsif (Is_Record_Type (Desig_Type)
12909 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12910 and then not Is_Constrained (Desig_Type)
12911 then
12912 -- If this is a constrained access definition for a record
12913 -- component, we leave the type as an unconstrained access,
12914 -- and mark the component so that its actual type is built
12915 -- at a point of use (e.g., an assignment statement). This
12916 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
12917
12918 if Desig_Type = Current_Scope
12919 and then No (Def_Id)
12920 then
12921 Desig_Subtype :=
12922 Create_Itype
12923 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
12924 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12925 Def_Id := Entity (Subtype_Mark (S));
12926
12927 -- We indicate that the component has a per-object constraint
12928 -- for treatment at a point of use, even though the constraint
12929 -- may be independent of discriminants of the enclosing type.
12930
12931 if Nkind (Related_Nod) = N_Component_Declaration then
12932 Set_Has_Per_Object_Constraint
12933 (Defining_Identifier (Related_Nod));
12934 end if;
12935
12936 -- This call added to ensure that the constraint is analyzed
12937 -- (needed for a B test). Note that we still return early from
12938 -- this procedure to avoid recursive processing.
12939
12940 Constrain_Discriminated_Type
12941 (Desig_Subtype, S, Related_Nod, For_Access => True);
12942 return;
12943 end if;
12944
12945 -- Enforce rule that the constraint is illegal if there is an
12946 -- unconstrained view of the designated type. This means that the
12947 -- partial view (either a private type declaration or a derivation
12948 -- from a private type) has no discriminants. (Defect Report
12949 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12950
12951 -- Rule updated for Ada 2005: The private type is said to have
12952 -- a constrained partial view, given that objects of the type
12953 -- can be declared. Furthermore, the rule applies to all access
12954 -- types, unlike the rule concerning default discriminants (see
12955 -- RM 3.7.1(7/3))
12956
12957 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12958 and then Has_Private_Declaration (Desig_Type)
12959 and then In_Open_Scopes (Scope (Desig_Type))
12960 and then Has_Discriminants (Desig_Type)
12961 then
12962 declare
12963 Pack : constant Node_Id :=
12964 Unit_Declaration_Node (Scope (Desig_Type));
12965 Decls : List_Id;
12966 Decl : Node_Id;
12967
12968 begin
12969 if Nkind (Pack) = N_Package_Declaration then
12970 Decls := Visible_Declarations (Specification (Pack));
12971 Decl := First (Decls);
12972 while Present (Decl) loop
12973 if (Nkind (Decl) = N_Private_Type_Declaration
12974 and then Chars (Defining_Identifier (Decl)) =
12975 Chars (Desig_Type))
12976
12977 or else
12978 (Nkind (Decl) = N_Full_Type_Declaration
12979 and then
12980 Chars (Defining_Identifier (Decl)) =
12981 Chars (Desig_Type)
12982 and then Is_Derived_Type (Desig_Type)
12983 and then
12984 Has_Private_Declaration (Etype (Desig_Type)))
12985 then
12986 if No (Discriminant_Specifications (Decl)) then
12987 Error_Msg_N
12988 ("cannot constrain access type if designated "
12989 & "type has constrained partial view", S);
12990 end if;
12991
12992 exit;
12993 end if;
12994
12995 Next (Decl);
12996 end loop;
12997 end if;
12998 end;
12999 end if;
13000
13001 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13002 For_Access => True);
13003
13004 elsif Is_Concurrent_Type (Desig_Type)
13005 and then not Is_Constrained (Desig_Type)
13006 then
13007 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13008
13009 else
13010 Error_Msg_N ("invalid constraint on access type", S);
13011
13012 -- We simply ignore an invalid constraint
13013
13014 Desig_Subtype := Desig_Type;
13015 Constraint_OK := False;
13016 end if;
13017
13018 if No (Def_Id) then
13019 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13020 else
13021 Set_Ekind (Def_Id, E_Access_Subtype);
13022 end if;
13023
13024 if Constraint_OK then
13025 Set_Etype (Def_Id, Base_Type (T));
13026
13027 if Is_Private_Type (Desig_Type) then
13028 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13029 end if;
13030 else
13031 Set_Etype (Def_Id, Any_Type);
13032 end if;
13033
13034 Set_Size_Info (Def_Id, T);
13035 Set_Is_Constrained (Def_Id, Constraint_OK);
13036 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13037 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13038 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13039
13040 Conditional_Delay (Def_Id, T);
13041
13042 -- AI-363 : Subtypes of general access types whose designated types have
13043 -- default discriminants are disallowed. In instances, the rule has to
13044 -- be checked against the actual, of which T is the subtype. In a
13045 -- generic body, the rule is checked assuming that the actual type has
13046 -- defaulted discriminants.
13047
13048 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13049 if Ekind (Base_Type (T)) = E_General_Access_Type
13050 and then Has_Defaulted_Discriminants (Desig_Type)
13051 then
13052 if Ada_Version < Ada_2005 then
13053 Error_Msg_N
13054 ("access subtype of general access type would not " &
13055 "be allowed in Ada 2005?y?", S);
13056 else
13057 Error_Msg_N
13058 ("access subtype of general access type not allowed", S);
13059 end if;
13060
13061 Error_Msg_N ("\discriminants have defaults", S);
13062
13063 elsif Is_Access_Type (T)
13064 and then Is_Generic_Type (Desig_Type)
13065 and then Has_Discriminants (Desig_Type)
13066 and then In_Package_Body (Current_Scope)
13067 then
13068 if Ada_Version < Ada_2005 then
13069 Error_Msg_N
13070 ("access subtype would not be allowed in generic body "
13071 & "in Ada 2005?y?", S);
13072 else
13073 Error_Msg_N
13074 ("access subtype not allowed in generic body", S);
13075 end if;
13076
13077 Error_Msg_N
13078 ("\designated type is a discriminated formal", S);
13079 end if;
13080 end if;
13081 end Constrain_Access;
13082
13083 ---------------------
13084 -- Constrain_Array --
13085 ---------------------
13086
13087 procedure Constrain_Array
13088 (Def_Id : in out Entity_Id;
13089 SI : Node_Id;
13090 Related_Nod : Node_Id;
13091 Related_Id : Entity_Id;
13092 Suffix : Character)
13093 is
13094 C : constant Node_Id := Constraint (SI);
13095 Number_Of_Constraints : Nat := 0;
13096 Index : Node_Id;
13097 S, T : Entity_Id;
13098 Constraint_OK : Boolean := True;
13099
13100 begin
13101 T := Entity (Subtype_Mark (SI));
13102
13103 if Is_Access_Type (T) then
13104 T := Designated_Type (T);
13105 end if;
13106
13107 -- If an index constraint follows a subtype mark in a subtype indication
13108 -- then the type or subtype denoted by the subtype mark must not already
13109 -- impose an index constraint. The subtype mark must denote either an
13110 -- unconstrained array type or an access type whose designated type
13111 -- is such an array type... (RM 3.6.1)
13112
13113 if Is_Constrained (T) then
13114 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13115 Constraint_OK := False;
13116
13117 else
13118 S := First (Constraints (C));
13119 while Present (S) loop
13120 Number_Of_Constraints := Number_Of_Constraints + 1;
13121 Next (S);
13122 end loop;
13123
13124 -- In either case, the index constraint must provide a discrete
13125 -- range for each index of the array type and the type of each
13126 -- discrete range must be the same as that of the corresponding
13127 -- index. (RM 3.6.1)
13128
13129 if Number_Of_Constraints /= Number_Dimensions (T) then
13130 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13131 Constraint_OK := False;
13132
13133 else
13134 S := First (Constraints (C));
13135 Index := First_Index (T);
13136 Analyze (Index);
13137
13138 -- Apply constraints to each index type
13139
13140 for J in 1 .. Number_Of_Constraints loop
13141 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13142 Next (Index);
13143 Next (S);
13144 end loop;
13145
13146 end if;
13147 end if;
13148
13149 if No (Def_Id) then
13150 Def_Id :=
13151 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13152 Set_Parent (Def_Id, Related_Nod);
13153
13154 else
13155 Set_Ekind (Def_Id, E_Array_Subtype);
13156 end if;
13157
13158 Set_Size_Info (Def_Id, (T));
13159 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13160 Set_Etype (Def_Id, Base_Type (T));
13161
13162 if Constraint_OK then
13163 Set_First_Index (Def_Id, First (Constraints (C)));
13164 else
13165 Set_First_Index (Def_Id, First_Index (T));
13166 end if;
13167
13168 Set_Is_Constrained (Def_Id, True);
13169 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13170 Set_Is_Independent (Def_Id, Is_Independent (T));
13171 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13172
13173 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13174 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13175
13176 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13177 -- We need to initialize the attribute because if Def_Id is previously
13178 -- analyzed through a limited_with clause, it will have the attributes
13179 -- of an incomplete type, one of which is an Elist that overlaps the
13180 -- Packed_Array_Impl_Type field.
13181
13182 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13183
13184 -- Build a freeze node if parent still needs one. Also make sure that
13185 -- the Depends_On_Private status is set because the subtype will need
13186 -- reprocessing at the time the base type does, and also we must set a
13187 -- conditional delay.
13188
13189 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13190 Conditional_Delay (Def_Id, T);
13191 end Constrain_Array;
13192
13193 ------------------------------
13194 -- Constrain_Component_Type --
13195 ------------------------------
13196
13197 function Constrain_Component_Type
13198 (Comp : Entity_Id;
13199 Constrained_Typ : Entity_Id;
13200 Related_Node : Node_Id;
13201 Typ : Entity_Id;
13202 Constraints : Elist_Id) return Entity_Id
13203 is
13204 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13205 Compon_Type : constant Entity_Id := Etype (Comp);
13206
13207 function Build_Constrained_Array_Type
13208 (Old_Type : Entity_Id) return Entity_Id;
13209 -- If Old_Type is an array type, one of whose indexes is constrained
13210 -- by a discriminant, build an Itype whose constraint replaces the
13211 -- discriminant with its value in the constraint.
13212
13213 function Build_Constrained_Discriminated_Type
13214 (Old_Type : Entity_Id) return Entity_Id;
13215 -- Ditto for record components. Handle the case where the constraint
13216 -- is a conversion of the discriminant value, introduced during
13217 -- expansion.
13218
13219 function Build_Constrained_Access_Type
13220 (Old_Type : Entity_Id) return Entity_Id;
13221 -- Ditto for access types. Makes use of previous two functions, to
13222 -- constrain designated type.
13223
13224 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13225 -- T is an array or discriminated type, C is a list of constraints
13226 -- that apply to T. This routine builds the constrained subtype.
13227
13228 function Is_Discriminant (Expr : Node_Id) return Boolean;
13229 -- Returns True if Expr is a discriminant
13230
13231 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13232 -- Find the value of discriminant Discrim in Constraint
13233
13234 -----------------------------------
13235 -- Build_Constrained_Access_Type --
13236 -----------------------------------
13237
13238 function Build_Constrained_Access_Type
13239 (Old_Type : Entity_Id) return Entity_Id
13240 is
13241 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13242 Itype : Entity_Id;
13243 Desig_Subtype : Entity_Id;
13244 Scop : Entity_Id;
13245
13246 begin
13247 -- if the original access type was not embedded in the enclosing
13248 -- type definition, there is no need to produce a new access
13249 -- subtype. In fact every access type with an explicit constraint
13250 -- generates an itype whose scope is the enclosing record.
13251
13252 if not Is_Type (Scope (Old_Type)) then
13253 return Old_Type;
13254
13255 elsif Is_Array_Type (Desig_Type) then
13256 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13257
13258 elsif Has_Discriminants (Desig_Type) then
13259
13260 -- This may be an access type to an enclosing record type for
13261 -- which we are constructing the constrained components. Return
13262 -- the enclosing record subtype. This is not always correct,
13263 -- but avoids infinite recursion. ???
13264
13265 Desig_Subtype := Any_Type;
13266
13267 for J in reverse 0 .. Scope_Stack.Last loop
13268 Scop := Scope_Stack.Table (J).Entity;
13269
13270 if Is_Type (Scop)
13271 and then Base_Type (Scop) = Base_Type (Desig_Type)
13272 then
13273 Desig_Subtype := Scop;
13274 end if;
13275
13276 exit when not Is_Type (Scop);
13277 end loop;
13278
13279 if Desig_Subtype = Any_Type then
13280 Desig_Subtype :=
13281 Build_Constrained_Discriminated_Type (Desig_Type);
13282 end if;
13283
13284 else
13285 return Old_Type;
13286 end if;
13287
13288 if Desig_Subtype /= Desig_Type then
13289
13290 -- The Related_Node better be here or else we won't be able
13291 -- to attach new itypes to a node in the tree.
13292
13293 pragma Assert (Present (Related_Node));
13294
13295 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13296
13297 Set_Etype (Itype, Base_Type (Old_Type));
13298 Set_Size_Info (Itype, (Old_Type));
13299 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13300 Set_Depends_On_Private (Itype, Has_Private_Component
13301 (Old_Type));
13302 Set_Is_Access_Constant (Itype, Is_Access_Constant
13303 (Old_Type));
13304
13305 -- The new itype needs freezing when it depends on a not frozen
13306 -- type and the enclosing subtype needs freezing.
13307
13308 if Has_Delayed_Freeze (Constrained_Typ)
13309 and then not Is_Frozen (Constrained_Typ)
13310 then
13311 Conditional_Delay (Itype, Base_Type (Old_Type));
13312 end if;
13313
13314 return Itype;
13315
13316 else
13317 return Old_Type;
13318 end if;
13319 end Build_Constrained_Access_Type;
13320
13321 ----------------------------------
13322 -- Build_Constrained_Array_Type --
13323 ----------------------------------
13324
13325 function Build_Constrained_Array_Type
13326 (Old_Type : Entity_Id) return Entity_Id
13327 is
13328 Lo_Expr : Node_Id;
13329 Hi_Expr : Node_Id;
13330 Old_Index : Node_Id;
13331 Range_Node : Node_Id;
13332 Constr_List : List_Id;
13333
13334 Need_To_Create_Itype : Boolean := False;
13335
13336 begin
13337 Old_Index := First_Index (Old_Type);
13338 while Present (Old_Index) loop
13339 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13340
13341 if Is_Discriminant (Lo_Expr)
13342 or else
13343 Is_Discriminant (Hi_Expr)
13344 then
13345 Need_To_Create_Itype := True;
13346 end if;
13347
13348 Next_Index (Old_Index);
13349 end loop;
13350
13351 if Need_To_Create_Itype then
13352 Constr_List := New_List;
13353
13354 Old_Index := First_Index (Old_Type);
13355 while Present (Old_Index) loop
13356 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13357
13358 if Is_Discriminant (Lo_Expr) then
13359 Lo_Expr := Get_Discr_Value (Lo_Expr);
13360 end if;
13361
13362 if Is_Discriminant (Hi_Expr) then
13363 Hi_Expr := Get_Discr_Value (Hi_Expr);
13364 end if;
13365
13366 Range_Node :=
13367 Make_Range
13368 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13369
13370 Append (Range_Node, To => Constr_List);
13371
13372 Next_Index (Old_Index);
13373 end loop;
13374
13375 return Build_Subtype (Old_Type, Constr_List);
13376
13377 else
13378 return Old_Type;
13379 end if;
13380 end Build_Constrained_Array_Type;
13381
13382 ------------------------------------------
13383 -- Build_Constrained_Discriminated_Type --
13384 ------------------------------------------
13385
13386 function Build_Constrained_Discriminated_Type
13387 (Old_Type : Entity_Id) return Entity_Id
13388 is
13389 Expr : Node_Id;
13390 Constr_List : List_Id;
13391 Old_Constraint : Elmt_Id;
13392
13393 Need_To_Create_Itype : Boolean := False;
13394
13395 begin
13396 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13397 while Present (Old_Constraint) loop
13398 Expr := Node (Old_Constraint);
13399
13400 if Is_Discriminant (Expr) then
13401 Need_To_Create_Itype := True;
13402
13403 -- After expansion of discriminated task types, the value
13404 -- of the discriminant may be converted to a run-time type
13405 -- for restricted run-times. Propagate the value of the
13406 -- discriminant as well, so that e.g. the secondary stack
13407 -- component has a static constraint. Necessary for LLVM.
13408
13409 elsif Nkind (Expr) = N_Type_Conversion
13410 and then Is_Discriminant (Expression (Expr))
13411 then
13412 Need_To_Create_Itype := True;
13413 end if;
13414
13415 Next_Elmt (Old_Constraint);
13416 end loop;
13417
13418 if Need_To_Create_Itype then
13419 Constr_List := New_List;
13420
13421 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13422 while Present (Old_Constraint) loop
13423 Expr := Node (Old_Constraint);
13424
13425 if Is_Discriminant (Expr) then
13426 Expr := Get_Discr_Value (Expr);
13427
13428 elsif Nkind (Expr) = N_Type_Conversion
13429 and then Is_Discriminant (Expression (Expr))
13430 then
13431 Expr := New_Copy_Tree (Expr);
13432 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13433 end if;
13434
13435 Append (New_Copy_Tree (Expr), To => Constr_List);
13436
13437 Next_Elmt (Old_Constraint);
13438 end loop;
13439
13440 return Build_Subtype (Old_Type, Constr_List);
13441
13442 else
13443 return Old_Type;
13444 end if;
13445 end Build_Constrained_Discriminated_Type;
13446
13447 -------------------
13448 -- Build_Subtype --
13449 -------------------
13450
13451 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13452 Indic : Node_Id;
13453 Subtyp_Decl : Node_Id;
13454 Def_Id : Entity_Id;
13455 Btyp : Entity_Id := Base_Type (T);
13456
13457 begin
13458 -- The Related_Node better be here or else we won't be able to
13459 -- attach new itypes to a node in the tree.
13460
13461 pragma Assert (Present (Related_Node));
13462
13463 -- If the view of the component's type is incomplete or private
13464 -- with unknown discriminants, then the constraint must be applied
13465 -- to the full type.
13466
13467 if Has_Unknown_Discriminants (Btyp)
13468 and then Present (Underlying_Type (Btyp))
13469 then
13470 Btyp := Underlying_Type (Btyp);
13471 end if;
13472
13473 Indic :=
13474 Make_Subtype_Indication (Loc,
13475 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13476 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13477
13478 Def_Id := Create_Itype (Ekind (T), Related_Node);
13479
13480 Subtyp_Decl :=
13481 Make_Subtype_Declaration (Loc,
13482 Defining_Identifier => Def_Id,
13483 Subtype_Indication => Indic);
13484
13485 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13486
13487 -- Itypes must be analyzed with checks off (see package Itypes)
13488
13489 Analyze (Subtyp_Decl, Suppress => All_Checks);
13490
13491 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13492 Inherit_Predicate_Flags (Def_Id, T);
13493
13494 -- Indicate where the predicate function may be found
13495
13496 if Is_Itype (T) then
13497 if Present (Predicate_Function (Def_Id)) then
13498 null;
13499
13500 elsif Present (Predicate_Function (T)) then
13501 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13502
13503 else
13504 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13505 end if;
13506
13507 elsif No (Predicate_Function (Def_Id)) then
13508 Set_Predicated_Parent (Def_Id, T);
13509 end if;
13510 end if;
13511
13512 return Def_Id;
13513 end Build_Subtype;
13514
13515 ---------------------
13516 -- Get_Discr_Value --
13517 ---------------------
13518
13519 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13520 D : Entity_Id;
13521 E : Elmt_Id;
13522
13523 begin
13524 -- The discriminant may be declared for the type, in which case we
13525 -- find it by iterating over the list of discriminants. If the
13526 -- discriminant is inherited from a parent type, it appears as the
13527 -- corresponding discriminant of the current type. This will be the
13528 -- case when constraining an inherited component whose constraint is
13529 -- given by a discriminant of the parent.
13530
13531 D := First_Discriminant (Typ);
13532 E := First_Elmt (Constraints);
13533
13534 while Present (D) loop
13535 if D = Entity (Discrim)
13536 or else D = CR_Discriminant (Entity (Discrim))
13537 or else Corresponding_Discriminant (D) = Entity (Discrim)
13538 then
13539 return Node (E);
13540 end if;
13541
13542 Next_Discriminant (D);
13543 Next_Elmt (E);
13544 end loop;
13545
13546 -- The Corresponding_Discriminant mechanism is incomplete, because
13547 -- the correspondence between new and old discriminants is not one
13548 -- to one: one new discriminant can constrain several old ones. In
13549 -- that case, scan sequentially the stored_constraint, the list of
13550 -- discriminants of the parents, and the constraints.
13551
13552 -- Previous code checked for the present of the Stored_Constraint
13553 -- list for the derived type, but did not use it at all. Should it
13554 -- be present when the component is a discriminated task type?
13555
13556 if Is_Derived_Type (Typ)
13557 and then Scope (Entity (Discrim)) = Etype (Typ)
13558 then
13559 D := First_Discriminant (Etype (Typ));
13560 E := First_Elmt (Constraints);
13561 while Present (D) loop
13562 if D = Entity (Discrim) then
13563 return Node (E);
13564 end if;
13565
13566 Next_Discriminant (D);
13567 Next_Elmt (E);
13568 end loop;
13569 end if;
13570
13571 -- Something is wrong if we did not find the value
13572
13573 raise Program_Error;
13574 end Get_Discr_Value;
13575
13576 ---------------------
13577 -- Is_Discriminant --
13578 ---------------------
13579
13580 function Is_Discriminant (Expr : Node_Id) return Boolean is
13581 Discrim_Scope : Entity_Id;
13582
13583 begin
13584 if Denotes_Discriminant (Expr) then
13585 Discrim_Scope := Scope (Entity (Expr));
13586
13587 -- Either we have a reference to one of Typ's discriminants,
13588
13589 pragma Assert (Discrim_Scope = Typ
13590
13591 -- or to the discriminants of the parent type, in the case
13592 -- of a derivation of a tagged type with variants.
13593
13594 or else Discrim_Scope = Etype (Typ)
13595 or else Full_View (Discrim_Scope) = Etype (Typ)
13596
13597 -- or same as above for the case where the discriminants
13598 -- were declared in Typ's private view.
13599
13600 or else (Is_Private_Type (Discrim_Scope)
13601 and then Chars (Discrim_Scope) = Chars (Typ))
13602
13603 -- or else we are deriving from the full view and the
13604 -- discriminant is declared in the private entity.
13605
13606 or else (Is_Private_Type (Typ)
13607 and then Chars (Discrim_Scope) = Chars (Typ))
13608
13609 -- Or we are constrained the corresponding record of a
13610 -- synchronized type that completes a private declaration.
13611
13612 or else (Is_Concurrent_Record_Type (Typ)
13613 and then
13614 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13615
13616 -- or we have a class-wide type, in which case make sure the
13617 -- discriminant found belongs to the root type.
13618
13619 or else (Is_Class_Wide_Type (Typ)
13620 and then Etype (Typ) = Discrim_Scope));
13621
13622 return True;
13623 end if;
13624
13625 -- In all other cases we have something wrong
13626
13627 return False;
13628 end Is_Discriminant;
13629
13630 -- Start of processing for Constrain_Component_Type
13631
13632 begin
13633 if Nkind (Parent (Comp)) = N_Component_Declaration
13634 and then Comes_From_Source (Parent (Comp))
13635 and then Comes_From_Source
13636 (Subtype_Indication (Component_Definition (Parent (Comp))))
13637 and then
13638 Is_Entity_Name
13639 (Subtype_Indication (Component_Definition (Parent (Comp))))
13640 then
13641 return Compon_Type;
13642
13643 elsif Is_Array_Type (Compon_Type) then
13644 return Build_Constrained_Array_Type (Compon_Type);
13645
13646 elsif Has_Discriminants (Compon_Type) then
13647 return Build_Constrained_Discriminated_Type (Compon_Type);
13648
13649 elsif Is_Access_Type (Compon_Type) then
13650 return Build_Constrained_Access_Type (Compon_Type);
13651
13652 else
13653 return Compon_Type;
13654 end if;
13655 end Constrain_Component_Type;
13656
13657 --------------------------
13658 -- Constrain_Concurrent --
13659 --------------------------
13660
13661 -- For concurrent types, the associated record value type carries the same
13662 -- discriminants, so when we constrain a concurrent type, we must constrain
13663 -- the corresponding record type as well.
13664
13665 procedure Constrain_Concurrent
13666 (Def_Id : in out Entity_Id;
13667 SI : Node_Id;
13668 Related_Nod : Node_Id;
13669 Related_Id : Entity_Id;
13670 Suffix : Character)
13671 is
13672 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13673 -- case of a private subtype (needed when only doing semantic analysis).
13674
13675 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13676 T_Val : Entity_Id;
13677
13678 begin
13679 if Is_Access_Type (T_Ent) then
13680 T_Ent := Designated_Type (T_Ent);
13681 end if;
13682
13683 T_Val := Corresponding_Record_Type (T_Ent);
13684
13685 if Present (T_Val) then
13686
13687 if No (Def_Id) then
13688 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13689
13690 -- Elaborate itype now, as it may be used in a subsequent
13691 -- synchronized operation in another scope.
13692
13693 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13694 Build_Itype_Reference (Def_Id, Related_Nod);
13695 end if;
13696 end if;
13697
13698 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13699 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13700
13701 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13702 Set_Corresponding_Record_Type (Def_Id,
13703 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13704
13705 else
13706 -- If there is no associated record, expansion is disabled and this
13707 -- is a generic context. Create a subtype in any case, so that
13708 -- semantic analysis can proceed.
13709
13710 if No (Def_Id) then
13711 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13712 end if;
13713
13714 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13715 end if;
13716 end Constrain_Concurrent;
13717
13718 ------------------------------------
13719 -- Constrain_Corresponding_Record --
13720 ------------------------------------
13721
13722 function Constrain_Corresponding_Record
13723 (Prot_Subt : Entity_Id;
13724 Corr_Rec : Entity_Id;
13725 Related_Nod : Node_Id) return Entity_Id
13726 is
13727 T_Sub : constant Entity_Id :=
13728 Create_Itype
13729 (Ekind => E_Record_Subtype,
13730 Related_Nod => Related_Nod,
13731 Related_Id => Corr_Rec,
13732 Suffix => 'C',
13733 Suffix_Index => -1);
13734
13735 begin
13736 Set_Etype (T_Sub, Corr_Rec);
13737 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13738 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
13739 Set_Is_Constrained (T_Sub, True);
13740 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13741 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13742
13743 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13744 Set_Discriminant_Constraint
13745 (T_Sub, Discriminant_Constraint (Prot_Subt));
13746 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13747 Create_Constrained_Components
13748 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13749 end if;
13750
13751 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13752
13753 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13754 Conditional_Delay (T_Sub, Corr_Rec);
13755
13756 else
13757 -- This is a component subtype: it will be frozen in the context of
13758 -- the enclosing record's init_proc, so that discriminant references
13759 -- are resolved to discriminals. (Note: we used to skip freezing
13760 -- altogether in that case, which caused errors downstream for
13761 -- components of a bit packed array type).
13762
13763 Set_Has_Delayed_Freeze (T_Sub);
13764 end if;
13765
13766 return T_Sub;
13767 end Constrain_Corresponding_Record;
13768
13769 -----------------------
13770 -- Constrain_Decimal --
13771 -----------------------
13772
13773 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13774 T : constant Entity_Id := Entity (Subtype_Mark (S));
13775 C : constant Node_Id := Constraint (S);
13776 Loc : constant Source_Ptr := Sloc (C);
13777 Range_Expr : Node_Id;
13778 Digits_Expr : Node_Id;
13779 Digits_Val : Uint;
13780 Bound_Val : Ureal;
13781
13782 begin
13783 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13784
13785 if Nkind (C) = N_Range_Constraint then
13786 Range_Expr := Range_Expression (C);
13787 Digits_Val := Digits_Value (T);
13788
13789 else
13790 pragma Assert (Nkind (C) = N_Digits_Constraint);
13791
13792 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13793
13794 Digits_Expr := Digits_Expression (C);
13795 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13796
13797 Check_Digits_Expression (Digits_Expr);
13798 Digits_Val := Expr_Value (Digits_Expr);
13799
13800 if Digits_Val > Digits_Value (T) then
13801 Error_Msg_N
13802 ("digits expression is incompatible with subtype", C);
13803 Digits_Val := Digits_Value (T);
13804 end if;
13805
13806 if Present (Range_Constraint (C)) then
13807 Range_Expr := Range_Expression (Range_Constraint (C));
13808 else
13809 Range_Expr := Empty;
13810 end if;
13811 end if;
13812
13813 Set_Etype (Def_Id, Base_Type (T));
13814 Set_Size_Info (Def_Id, (T));
13815 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13816 Set_Delta_Value (Def_Id, Delta_Value (T));
13817 Set_Scale_Value (Def_Id, Scale_Value (T));
13818 Set_Small_Value (Def_Id, Small_Value (T));
13819 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13820 Set_Digits_Value (Def_Id, Digits_Val);
13821
13822 -- Manufacture range from given digits value if no range present
13823
13824 if No (Range_Expr) then
13825 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13826 Range_Expr :=
13827 Make_Range (Loc,
13828 Low_Bound =>
13829 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13830 High_Bound =>
13831 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13832 end if;
13833
13834 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13835 Set_Discrete_RM_Size (Def_Id);
13836
13837 -- Unconditionally delay the freeze, since we cannot set size
13838 -- information in all cases correctly until the freeze point.
13839
13840 Set_Has_Delayed_Freeze (Def_Id);
13841 end Constrain_Decimal;
13842
13843 ----------------------------------
13844 -- Constrain_Discriminated_Type --
13845 ----------------------------------
13846
13847 procedure Constrain_Discriminated_Type
13848 (Def_Id : Entity_Id;
13849 S : Node_Id;
13850 Related_Nod : Node_Id;
13851 For_Access : Boolean := False)
13852 is
13853 E : Entity_Id := Entity (Subtype_Mark (S));
13854 T : Entity_Id;
13855
13856 procedure Fixup_Bad_Constraint;
13857 -- Called after finding a bad constraint, and after having posted an
13858 -- appropriate error message. The goal is to leave type Def_Id in as
13859 -- reasonable state as possible.
13860
13861 --------------------------
13862 -- Fixup_Bad_Constraint --
13863 --------------------------
13864
13865 procedure Fixup_Bad_Constraint is
13866 begin
13867 -- Set a reasonable Ekind for the entity, including incomplete types.
13868
13869 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13870
13871 -- Set Etype to the known type, to reduce chances of cascaded errors
13872
13873 Set_Etype (Def_Id, E);
13874 Set_Error_Posted (Def_Id);
13875 end Fixup_Bad_Constraint;
13876
13877 -- Local variables
13878
13879 C : Node_Id;
13880 Constr : Elist_Id := New_Elmt_List;
13881
13882 -- Start of processing for Constrain_Discriminated_Type
13883
13884 begin
13885 C := Constraint (S);
13886
13887 -- A discriminant constraint is only allowed in a subtype indication,
13888 -- after a subtype mark. This subtype mark must denote either a type
13889 -- with discriminants, or an access type whose designated type is a
13890 -- type with discriminants. A discriminant constraint specifies the
13891 -- values of these discriminants (RM 3.7.2(5)).
13892
13893 T := Base_Type (Entity (Subtype_Mark (S)));
13894
13895 if Is_Access_Type (T) then
13896 T := Designated_Type (T);
13897 end if;
13898
13899 -- In an instance it may be necessary to retrieve the full view of a
13900 -- type with unknown discriminants, or a full view with defaulted
13901 -- discriminants. In other contexts the constraint is illegal.
13902
13903 if In_Instance
13904 and then Is_Private_Type (T)
13905 and then Present (Full_View (T))
13906 and then
13907 (Has_Unknown_Discriminants (T)
13908 or else
13909 (not Has_Discriminants (T)
13910 and then Has_Discriminants (Full_View (T))
13911 and then Present (Discriminant_Default_Value
13912 (First_Discriminant (Full_View (T))))))
13913 then
13914 T := Full_View (T);
13915 E := Full_View (E);
13916 end if;
13917
13918 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13919 -- generating an error for access-to-incomplete subtypes.
13920
13921 if Ada_Version >= Ada_2005
13922 and then Ekind (T) = E_Incomplete_Type
13923 and then Nkind (Parent (S)) = N_Subtype_Declaration
13924 and then not Is_Itype (Def_Id)
13925 then
13926 -- A little sanity check: emit an error message if the type has
13927 -- discriminants to begin with. Type T may be a regular incomplete
13928 -- type or imported via a limited with clause.
13929
13930 if Has_Discriminants (T)
13931 or else (From_Limited_With (T)
13932 and then Present (Non_Limited_View (T))
13933 and then Nkind (Parent (Non_Limited_View (T))) =
13934 N_Full_Type_Declaration
13935 and then Present (Discriminant_Specifications
13936 (Parent (Non_Limited_View (T)))))
13937 then
13938 Error_Msg_N
13939 ("(Ada 2005) incomplete subtype may not be constrained", C);
13940 else
13941 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13942 end if;
13943
13944 Fixup_Bad_Constraint;
13945 return;
13946
13947 -- Check that the type has visible discriminants. The type may be
13948 -- a private type with unknown discriminants whose full view has
13949 -- discriminants which are invisible.
13950
13951 elsif not Has_Discriminants (T)
13952 or else
13953 (Has_Unknown_Discriminants (T)
13954 and then Is_Private_Type (T))
13955 then
13956 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13957 Fixup_Bad_Constraint;
13958 return;
13959
13960 elsif Is_Constrained (E)
13961 or else (Ekind (E) = E_Class_Wide_Subtype
13962 and then Present (Discriminant_Constraint (E)))
13963 then
13964 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13965 Fixup_Bad_Constraint;
13966 return;
13967 end if;
13968
13969 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13970 -- applies to the base type.
13971
13972 T := Base_Type (T);
13973
13974 Constr := Build_Discriminant_Constraints (T, S);
13975
13976 -- If the list returned was empty we had an error in building the
13977 -- discriminant constraint. We have also already signalled an error
13978 -- in the incomplete type case
13979
13980 if Is_Empty_Elmt_List (Constr) then
13981 Fixup_Bad_Constraint;
13982 return;
13983 end if;
13984
13985 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13986 end Constrain_Discriminated_Type;
13987
13988 ---------------------------
13989 -- Constrain_Enumeration --
13990 ---------------------------
13991
13992 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13993 T : constant Entity_Id := Entity (Subtype_Mark (S));
13994 C : constant Node_Id := Constraint (S);
13995
13996 begin
13997 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13998
13999 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14000
14001 Set_Etype (Def_Id, Base_Type (T));
14002 Set_Size_Info (Def_Id, (T));
14003 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14004 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14005
14006 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14007
14008 Set_Discrete_RM_Size (Def_Id);
14009 end Constrain_Enumeration;
14010
14011 ----------------------
14012 -- Constrain_Float --
14013 ----------------------
14014
14015 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14016 T : constant Entity_Id := Entity (Subtype_Mark (S));
14017 C : Node_Id;
14018 D : Node_Id;
14019 Rais : Node_Id;
14020
14021 begin
14022 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14023
14024 Set_Etype (Def_Id, Base_Type (T));
14025 Set_Size_Info (Def_Id, (T));
14026 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14027
14028 -- Process the constraint
14029
14030 C := Constraint (S);
14031
14032 -- Digits constraint present
14033
14034 if Nkind (C) = N_Digits_Constraint then
14035
14036 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
14037 Check_Restriction (No_Obsolescent_Features, C);
14038
14039 if Warn_On_Obsolescent_Feature then
14040 Error_Msg_N
14041 ("subtype digits constraint is an " &
14042 "obsolescent feature (RM J.3(8))?j?", C);
14043 end if;
14044
14045 D := Digits_Expression (C);
14046 Analyze_And_Resolve (D, Any_Integer);
14047 Check_Digits_Expression (D);
14048 Set_Digits_Value (Def_Id, Expr_Value (D));
14049
14050 -- Check that digits value is in range. Obviously we can do this
14051 -- at compile time, but it is strictly a runtime check, and of
14052 -- course there is an ACVC test that checks this.
14053
14054 if Digits_Value (Def_Id) > Digits_Value (T) then
14055 Error_Msg_Uint_1 := Digits_Value (T);
14056 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14057 Rais :=
14058 Make_Raise_Constraint_Error (Sloc (D),
14059 Reason => CE_Range_Check_Failed);
14060 Insert_Action (Declaration_Node (Def_Id), Rais);
14061 end if;
14062
14063 C := Range_Constraint (C);
14064
14065 -- No digits constraint present
14066
14067 else
14068 Set_Digits_Value (Def_Id, Digits_Value (T));
14069 end if;
14070
14071 -- Range constraint present
14072
14073 if Nkind (C) = N_Range_Constraint then
14074 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14075
14076 -- No range constraint present
14077
14078 else
14079 pragma Assert (No (C));
14080 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14081 end if;
14082
14083 Set_Is_Constrained (Def_Id);
14084 end Constrain_Float;
14085
14086 ---------------------
14087 -- Constrain_Index --
14088 ---------------------
14089
14090 procedure Constrain_Index
14091 (Index : Node_Id;
14092 S : Node_Id;
14093 Related_Nod : Node_Id;
14094 Related_Id : Entity_Id;
14095 Suffix : Character;
14096 Suffix_Index : Nat)
14097 is
14098 Def_Id : Entity_Id;
14099 R : Node_Id := Empty;
14100 T : constant Entity_Id := Etype (Index);
14101
14102 begin
14103 Def_Id :=
14104 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14105 Set_Etype (Def_Id, Base_Type (T));
14106
14107 if Nkind (S) = N_Range
14108 or else
14109 (Nkind (S) = N_Attribute_Reference
14110 and then Attribute_Name (S) = Name_Range)
14111 then
14112 -- A Range attribute will be transformed into N_Range by Resolve
14113
14114 Analyze (S);
14115 Set_Etype (S, T);
14116 R := S;
14117
14118 Process_Range_Expr_In_Decl (R, T);
14119
14120 if not Error_Posted (S)
14121 and then
14122 (Nkind (S) /= N_Range
14123 or else not Covers (T, (Etype (Low_Bound (S))))
14124 or else not Covers (T, (Etype (High_Bound (S)))))
14125 then
14126 if Base_Type (T) /= Any_Type
14127 and then Etype (Low_Bound (S)) /= Any_Type
14128 and then Etype (High_Bound (S)) /= Any_Type
14129 then
14130 Error_Msg_N ("range expected", S);
14131 end if;
14132 end if;
14133
14134 elsif Nkind (S) = N_Subtype_Indication then
14135
14136 -- The parser has verified that this is a discrete indication
14137
14138 Resolve_Discrete_Subtype_Indication (S, T);
14139 Bad_Predicated_Subtype_Use
14140 ("subtype& has predicate, not allowed in index constraint",
14141 S, Entity (Subtype_Mark (S)));
14142
14143 R := Range_Expression (Constraint (S));
14144
14145 -- Capture values of bounds and generate temporaries for them if
14146 -- needed, since checks may cause duplication of the expressions
14147 -- which must not be reevaluated.
14148
14149 -- The forced evaluation removes side effects from expressions, which
14150 -- should occur also in GNATprove mode. Otherwise, we end up with
14151 -- unexpected insertions of actions at places where this is not
14152 -- supposed to occur, e.g. on default parameters of a call.
14153
14154 if Expander_Active or GNATprove_Mode then
14155 Force_Evaluation
14156 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14157 Force_Evaluation
14158 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14159 end if;
14160
14161 elsif Nkind (S) = N_Discriminant_Association then
14162
14163 -- Syntactically valid in subtype indication
14164
14165 Error_Msg_N ("invalid index constraint", S);
14166 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14167 return;
14168
14169 -- Subtype_Mark case, no anonymous subtypes to construct
14170
14171 else
14172 Analyze (S);
14173
14174 if Is_Entity_Name (S) then
14175 if not Is_Type (Entity (S)) then
14176 Error_Msg_N ("expect subtype mark for index constraint", S);
14177
14178 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14179 Wrong_Type (S, Base_Type (T));
14180
14181 -- Check error of subtype with predicate in index constraint
14182
14183 else
14184 Bad_Predicated_Subtype_Use
14185 ("subtype& has predicate, not allowed in index constraint",
14186 S, Entity (S));
14187 end if;
14188
14189 return;
14190
14191 else
14192 Error_Msg_N ("invalid index constraint", S);
14193 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14194 return;
14195 end if;
14196 end if;
14197
14198 -- Complete construction of the Itype
14199
14200 if Is_Modular_Integer_Type (T) then
14201 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14202
14203 elsif Is_Integer_Type (T) then
14204 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14205
14206 else
14207 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14208 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14209 Set_First_Literal (Def_Id, First_Literal (T));
14210 end if;
14211
14212 Set_Size_Info (Def_Id, (T));
14213 Set_RM_Size (Def_Id, RM_Size (T));
14214 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14215
14216 Set_Scalar_Range (Def_Id, R);
14217
14218 Set_Etype (S, Def_Id);
14219 Set_Discrete_RM_Size (Def_Id);
14220 end Constrain_Index;
14221
14222 -----------------------
14223 -- Constrain_Integer --
14224 -----------------------
14225
14226 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14227 T : constant Entity_Id := Entity (Subtype_Mark (S));
14228 C : constant Node_Id := Constraint (S);
14229
14230 begin
14231 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14232
14233 if Is_Modular_Integer_Type (T) then
14234 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14235 else
14236 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14237 end if;
14238
14239 Set_Etype (Def_Id, Base_Type (T));
14240 Set_Size_Info (Def_Id, (T));
14241 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14242 Set_Discrete_RM_Size (Def_Id);
14243 end Constrain_Integer;
14244
14245 ------------------------------
14246 -- Constrain_Ordinary_Fixed --
14247 ------------------------------
14248
14249 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14250 T : constant Entity_Id := Entity (Subtype_Mark (S));
14251 C : Node_Id;
14252 D : Node_Id;
14253 Rais : Node_Id;
14254
14255 begin
14256 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14257 Set_Etype (Def_Id, Base_Type (T));
14258 Set_Size_Info (Def_Id, (T));
14259 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14260 Set_Small_Value (Def_Id, Small_Value (T));
14261
14262 -- Process the constraint
14263
14264 C := Constraint (S);
14265
14266 -- Delta constraint present
14267
14268 if Nkind (C) = N_Delta_Constraint then
14269
14270 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14271 Check_Restriction (No_Obsolescent_Features, C);
14272
14273 if Warn_On_Obsolescent_Feature then
14274 Error_Msg_S
14275 ("subtype delta constraint is an " &
14276 "obsolescent feature (RM J.3(7))?j?");
14277 end if;
14278
14279 D := Delta_Expression (C);
14280 Analyze_And_Resolve (D, Any_Real);
14281 Check_Delta_Expression (D);
14282 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14283
14284 -- Check that delta value is in range. Obviously we can do this
14285 -- at compile time, but it is strictly a runtime check, and of
14286 -- course there is an ACVC test that checks this.
14287
14288 if Delta_Value (Def_Id) < Delta_Value (T) then
14289 Error_Msg_N ("??delta value is too small", D);
14290 Rais :=
14291 Make_Raise_Constraint_Error (Sloc (D),
14292 Reason => CE_Range_Check_Failed);
14293 Insert_Action (Declaration_Node (Def_Id), Rais);
14294 end if;
14295
14296 C := Range_Constraint (C);
14297
14298 -- No delta constraint present
14299
14300 else
14301 Set_Delta_Value (Def_Id, Delta_Value (T));
14302 end if;
14303
14304 -- Range constraint present
14305
14306 if Nkind (C) = N_Range_Constraint then
14307 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14308
14309 -- No range constraint present
14310
14311 else
14312 pragma Assert (No (C));
14313 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14314 end if;
14315
14316 Set_Discrete_RM_Size (Def_Id);
14317
14318 -- Unconditionally delay the freeze, since we cannot set size
14319 -- information in all cases correctly until the freeze point.
14320
14321 Set_Has_Delayed_Freeze (Def_Id);
14322 end Constrain_Ordinary_Fixed;
14323
14324 -----------------------
14325 -- Contain_Interface --
14326 -----------------------
14327
14328 function Contain_Interface
14329 (Iface : Entity_Id;
14330 Ifaces : Elist_Id) return Boolean
14331 is
14332 Iface_Elmt : Elmt_Id;
14333
14334 begin
14335 if Present (Ifaces) then
14336 Iface_Elmt := First_Elmt (Ifaces);
14337 while Present (Iface_Elmt) loop
14338 if Node (Iface_Elmt) = Iface then
14339 return True;
14340 end if;
14341
14342 Next_Elmt (Iface_Elmt);
14343 end loop;
14344 end if;
14345
14346 return False;
14347 end Contain_Interface;
14348
14349 ---------------------------
14350 -- Convert_Scalar_Bounds --
14351 ---------------------------
14352
14353 procedure Convert_Scalar_Bounds
14354 (N : Node_Id;
14355 Parent_Type : Entity_Id;
14356 Derived_Type : Entity_Id;
14357 Loc : Source_Ptr)
14358 is
14359 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14360
14361 Lo : Node_Id;
14362 Hi : Node_Id;
14363 Rng : Node_Id;
14364
14365 begin
14366 -- Defend against previous errors
14367
14368 if No (Scalar_Range (Derived_Type)) then
14369 Check_Error_Detected;
14370 return;
14371 end if;
14372
14373 Lo := Build_Scalar_Bound
14374 (Type_Low_Bound (Derived_Type),
14375 Parent_Type, Implicit_Base);
14376
14377 Hi := Build_Scalar_Bound
14378 (Type_High_Bound (Derived_Type),
14379 Parent_Type, Implicit_Base);
14380
14381 Rng :=
14382 Make_Range (Loc,
14383 Low_Bound => Lo,
14384 High_Bound => Hi);
14385
14386 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14387
14388 Set_Parent (Rng, N);
14389 Set_Scalar_Range (Derived_Type, Rng);
14390
14391 -- Analyze the bounds
14392
14393 Analyze_And_Resolve (Lo, Implicit_Base);
14394 Analyze_And_Resolve (Hi, Implicit_Base);
14395
14396 -- Analyze the range itself, except that we do not analyze it if
14397 -- the bounds are real literals, and we have a fixed-point type.
14398 -- The reason for this is that we delay setting the bounds in this
14399 -- case till we know the final Small and Size values (see circuit
14400 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14401
14402 if Is_Fixed_Point_Type (Parent_Type)
14403 and then Nkind (Lo) = N_Real_Literal
14404 and then Nkind (Hi) = N_Real_Literal
14405 then
14406 return;
14407
14408 -- Here we do the analysis of the range
14409
14410 -- Note: we do this manually, since if we do a normal Analyze and
14411 -- Resolve call, there are problems with the conversions used for
14412 -- the derived type range.
14413
14414 else
14415 Set_Etype (Rng, Implicit_Base);
14416 Set_Analyzed (Rng, True);
14417 end if;
14418 end Convert_Scalar_Bounds;
14419
14420 -------------------
14421 -- Copy_And_Swap --
14422 -------------------
14423
14424 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14425 begin
14426 -- Initialize new full declaration entity by copying the pertinent
14427 -- fields of the corresponding private declaration entity.
14428
14429 -- We temporarily set Ekind to a value appropriate for a type to
14430 -- avoid assert failures in Einfo from checking for setting type
14431 -- attributes on something that is not a type. Ekind (Priv) is an
14432 -- appropriate choice, since it allowed the attributes to be set
14433 -- in the first place. This Ekind value will be modified later.
14434
14435 Set_Ekind (Full, Ekind (Priv));
14436
14437 -- Also set Etype temporarily to Any_Type, again, in the absence
14438 -- of errors, it will be properly reset, and if there are errors,
14439 -- then we want a value of Any_Type to remain.
14440
14441 Set_Etype (Full, Any_Type);
14442
14443 -- Now start copying attributes
14444
14445 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14446
14447 if Has_Discriminants (Full) then
14448 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14449 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14450 end if;
14451
14452 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14453 Set_Homonym (Full, Homonym (Priv));
14454 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14455 Set_Is_Public (Full, Is_Public (Priv));
14456 Set_Is_Pure (Full, Is_Pure (Priv));
14457 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14458 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14459 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14460 Set_Has_Pragma_Unreferenced_Objects
14461 (Full, Has_Pragma_Unreferenced_Objects
14462 (Priv));
14463
14464 Conditional_Delay (Full, Priv);
14465
14466 if Is_Tagged_Type (Full) then
14467 Set_Direct_Primitive_Operations
14468 (Full, Direct_Primitive_Operations (Priv));
14469 Set_No_Tagged_Streams_Pragma
14470 (Full, No_Tagged_Streams_Pragma (Priv));
14471
14472 if Is_Base_Type (Priv) then
14473 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14474 end if;
14475 end if;
14476
14477 Set_Is_Volatile (Full, Is_Volatile (Priv));
14478 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14479 Set_Scope (Full, Scope (Priv));
14480 Set_Prev_Entity (Full, Prev_Entity (Priv));
14481 Set_Next_Entity (Full, Next_Entity (Priv));
14482 Set_First_Entity (Full, First_Entity (Priv));
14483 Set_Last_Entity (Full, Last_Entity (Priv));
14484
14485 -- If access types have been recorded for later handling, keep them in
14486 -- the full view so that they get handled when the full view freeze
14487 -- node is expanded.
14488
14489 if Present (Freeze_Node (Priv))
14490 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14491 then
14492 Ensure_Freeze_Node (Full);
14493 Set_Access_Types_To_Process
14494 (Freeze_Node (Full),
14495 Access_Types_To_Process (Freeze_Node (Priv)));
14496 end if;
14497
14498 -- Swap the two entities. Now Private is the full type entity and Full
14499 -- is the private one. They will be swapped back at the end of the
14500 -- private part. This swapping ensures that the entity that is visible
14501 -- in the private part is the full declaration.
14502
14503 Exchange_Entities (Priv, Full);
14504 Append_Entity (Full, Scope (Full));
14505 end Copy_And_Swap;
14506
14507 -------------------------------------
14508 -- Copy_Array_Base_Type_Attributes --
14509 -------------------------------------
14510
14511 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14512 begin
14513 Set_Component_Alignment (T1, Component_Alignment (T2));
14514 Set_Component_Type (T1, Component_Type (T2));
14515 Set_Component_Size (T1, Component_Size (T2));
14516 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14517 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14518 Propagate_Concurrent_Flags (T1, T2);
14519 Set_Is_Packed (T1, Is_Packed (T2));
14520 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14521 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14522 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14523 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14524 end Copy_Array_Base_Type_Attributes;
14525
14526 -----------------------------------
14527 -- Copy_Array_Subtype_Attributes --
14528 -----------------------------------
14529
14530 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14531 begin
14532 Set_Size_Info (T1, T2);
14533
14534 Set_First_Index (T1, First_Index (T2));
14535 Set_Is_Aliased (T1, Is_Aliased (T2));
14536 Set_Is_Atomic (T1, Is_Atomic (T2));
14537 Set_Is_Independent (T1, Is_Independent (T2));
14538 Set_Is_Volatile (T1, Is_Volatile (T2));
14539 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14540 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14541 Set_Is_Constrained (T1, Is_Constrained (T2));
14542 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14543 Inherit_Rep_Item_Chain (T1, T2);
14544 Set_Convention (T1, Convention (T2));
14545 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14546 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14547 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14548 end Copy_Array_Subtype_Attributes;
14549
14550 -----------------------------------
14551 -- Create_Constrained_Components --
14552 -----------------------------------
14553
14554 procedure Create_Constrained_Components
14555 (Subt : Entity_Id;
14556 Decl_Node : Node_Id;
14557 Typ : Entity_Id;
14558 Constraints : Elist_Id)
14559 is
14560 Loc : constant Source_Ptr := Sloc (Subt);
14561 Comp_List : constant Elist_Id := New_Elmt_List;
14562 Parent_Type : constant Entity_Id := Etype (Typ);
14563 Assoc_List : constant List_Id := New_List;
14564 Discr_Val : Elmt_Id;
14565 Errors : Boolean;
14566 New_C : Entity_Id;
14567 Old_C : Entity_Id;
14568 Is_Static : Boolean := True;
14569
14570 procedure Collect_Fixed_Components (Typ : Entity_Id);
14571 -- Collect parent type components that do not appear in a variant part
14572
14573 procedure Create_All_Components;
14574 -- Iterate over Comp_List to create the components of the subtype
14575
14576 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14577 -- Creates a new component from Old_Compon, copying all the fields from
14578 -- it, including its Etype, inserts the new component in the Subt entity
14579 -- chain and returns the new component.
14580
14581 function Is_Variant_Record (T : Entity_Id) return Boolean;
14582 -- If true, and discriminants are static, collect only components from
14583 -- variants selected by discriminant values.
14584
14585 ------------------------------
14586 -- Collect_Fixed_Components --
14587 ------------------------------
14588
14589 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14590 begin
14591 -- Build association list for discriminants, and find components of the
14592 -- variant part selected by the values of the discriminants.
14593
14594 Old_C := First_Discriminant (Typ);
14595 Discr_Val := First_Elmt (Constraints);
14596 while Present (Old_C) loop
14597 Append_To (Assoc_List,
14598 Make_Component_Association (Loc,
14599 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14600 Expression => New_Copy (Node (Discr_Val))));
14601
14602 Next_Elmt (Discr_Val);
14603 Next_Discriminant (Old_C);
14604 end loop;
14605
14606 -- The tag and the possible parent component are unconditionally in
14607 -- the subtype.
14608
14609 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14610 Old_C := First_Component (Typ);
14611 while Present (Old_C) loop
14612 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14613 Append_Elmt (Old_C, Comp_List);
14614 end if;
14615
14616 Next_Component (Old_C);
14617 end loop;
14618 end if;
14619 end Collect_Fixed_Components;
14620
14621 ---------------------------
14622 -- Create_All_Components --
14623 ---------------------------
14624
14625 procedure Create_All_Components is
14626 Comp : Elmt_Id;
14627
14628 begin
14629 Comp := First_Elmt (Comp_List);
14630 while Present (Comp) loop
14631 Old_C := Node (Comp);
14632 New_C := Create_Component (Old_C);
14633
14634 Set_Etype
14635 (New_C,
14636 Constrain_Component_Type
14637 (Old_C, Subt, Decl_Node, Typ, Constraints));
14638 Set_Is_Public (New_C, Is_Public (Subt));
14639
14640 Next_Elmt (Comp);
14641 end loop;
14642 end Create_All_Components;
14643
14644 ----------------------
14645 -- Create_Component --
14646 ----------------------
14647
14648 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14649 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14650
14651 begin
14652 if Ekind (Old_Compon) = E_Discriminant
14653 and then Is_Completely_Hidden (Old_Compon)
14654 then
14655 -- This is a shadow discriminant created for a discriminant of
14656 -- the parent type, which needs to be present in the subtype.
14657 -- Give the shadow discriminant an internal name that cannot
14658 -- conflict with that of visible components.
14659
14660 Set_Chars (New_Compon, New_Internal_Name ('C'));
14661 end if;
14662
14663 -- Set the parent so we have a proper link for freezing etc. This is
14664 -- not a real parent pointer, since of course our parent does not own
14665 -- up to us and reference us, we are an illegitimate child of the
14666 -- original parent.
14667
14668 Set_Parent (New_Compon, Parent (Old_Compon));
14669
14670 -- We do not want this node marked as Comes_From_Source, since
14671 -- otherwise it would get first class status and a separate cross-
14672 -- reference line would be generated. Illegitimate children do not
14673 -- rate such recognition.
14674
14675 Set_Comes_From_Source (New_Compon, False);
14676
14677 -- But it is a real entity, and a birth certificate must be properly
14678 -- registered by entering it into the entity list, and setting its
14679 -- scope to the given subtype. This turns out to be useful for the
14680 -- LLVM code generator, but that scope is not used otherwise.
14681
14682 Enter_Name (New_Compon);
14683 Set_Scope (New_Compon, Subt);
14684
14685 return New_Compon;
14686 end Create_Component;
14687
14688 -----------------------
14689 -- Is_Variant_Record --
14690 -----------------------
14691
14692 function Is_Variant_Record (T : Entity_Id) return Boolean is
14693 begin
14694 return Nkind (Parent (T)) = N_Full_Type_Declaration
14695 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14696 and then Present (Component_List (Type_Definition (Parent (T))))
14697 and then
14698 Present
14699 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14700 end Is_Variant_Record;
14701
14702 -- Start of processing for Create_Constrained_Components
14703
14704 begin
14705 pragma Assert (Subt /= Base_Type (Subt));
14706 pragma Assert (Typ = Base_Type (Typ));
14707
14708 Set_First_Entity (Subt, Empty);
14709 Set_Last_Entity (Subt, Empty);
14710
14711 -- Check whether constraint is fully static, in which case we can
14712 -- optimize the list of components.
14713
14714 Discr_Val := First_Elmt (Constraints);
14715 while Present (Discr_Val) loop
14716 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14717 Is_Static := False;
14718 exit;
14719 end if;
14720
14721 Next_Elmt (Discr_Val);
14722 end loop;
14723
14724 Set_Has_Static_Discriminants (Subt, Is_Static);
14725
14726 Push_Scope (Subt);
14727
14728 -- Inherit the discriminants of the parent type
14729
14730 Add_Discriminants : declare
14731 Num_Disc : Nat;
14732 Num_Gird : Nat;
14733
14734 begin
14735 Num_Disc := 0;
14736 Old_C := First_Discriminant (Typ);
14737
14738 while Present (Old_C) loop
14739 Num_Disc := Num_Disc + 1;
14740 New_C := Create_Component (Old_C);
14741 Set_Is_Public (New_C, Is_Public (Subt));
14742 Next_Discriminant (Old_C);
14743 end loop;
14744
14745 -- For an untagged derived subtype, the number of discriminants may
14746 -- be smaller than the number of inherited discriminants, because
14747 -- several of them may be renamed by a single new discriminant or
14748 -- constrained. In this case, add the hidden discriminants back into
14749 -- the subtype, because they need to be present if the optimizer of
14750 -- the GCC 4.x back-end decides to break apart assignments between
14751 -- objects using the parent view into member-wise assignments.
14752
14753 Num_Gird := 0;
14754
14755 if Is_Derived_Type (Typ)
14756 and then not Is_Tagged_Type (Typ)
14757 then
14758 Old_C := First_Stored_Discriminant (Typ);
14759
14760 while Present (Old_C) loop
14761 Num_Gird := Num_Gird + 1;
14762 Next_Stored_Discriminant (Old_C);
14763 end loop;
14764 end if;
14765
14766 if Num_Gird > Num_Disc then
14767
14768 -- Find out multiple uses of new discriminants, and add hidden
14769 -- components for the extra renamed discriminants. We recognize
14770 -- multiple uses through the Corresponding_Discriminant of a
14771 -- new discriminant: if it constrains several old discriminants,
14772 -- this field points to the last one in the parent type. The
14773 -- stored discriminants of the derived type have the same name
14774 -- as those of the parent.
14775
14776 declare
14777 Constr : Elmt_Id;
14778 New_Discr : Entity_Id;
14779 Old_Discr : Entity_Id;
14780
14781 begin
14782 Constr := First_Elmt (Stored_Constraint (Typ));
14783 Old_Discr := First_Stored_Discriminant (Typ);
14784 while Present (Constr) loop
14785 if Is_Entity_Name (Node (Constr))
14786 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14787 then
14788 New_Discr := Entity (Node (Constr));
14789
14790 if Chars (Corresponding_Discriminant (New_Discr)) /=
14791 Chars (Old_Discr)
14792 then
14793 -- The new discriminant has been used to rename a
14794 -- subsequent old discriminant. Introduce a shadow
14795 -- component for the current old discriminant.
14796
14797 New_C := Create_Component (Old_Discr);
14798 Set_Original_Record_Component (New_C, Old_Discr);
14799 end if;
14800
14801 else
14802 -- The constraint has eliminated the old discriminant.
14803 -- Introduce a shadow component.
14804
14805 New_C := Create_Component (Old_Discr);
14806 Set_Original_Record_Component (New_C, Old_Discr);
14807 end if;
14808
14809 Next_Elmt (Constr);
14810 Next_Stored_Discriminant (Old_Discr);
14811 end loop;
14812 end;
14813 end if;
14814 end Add_Discriminants;
14815
14816 if Is_Static
14817 and then Is_Variant_Record (Typ)
14818 then
14819 Collect_Fixed_Components (Typ);
14820
14821 Gather_Components (
14822 Typ,
14823 Component_List (Type_Definition (Parent (Typ))),
14824 Governed_By => Assoc_List,
14825 Into => Comp_List,
14826 Report_Errors => Errors);
14827 pragma Assert (not Errors
14828 or else Serious_Errors_Detected > 0);
14829
14830 Create_All_Components;
14831
14832 -- If the subtype declaration is created for a tagged type derivation
14833 -- with constraints, we retrieve the record definition of the parent
14834 -- type to select the components of the proper variant.
14835
14836 elsif Is_Static
14837 and then Is_Tagged_Type (Typ)
14838 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14839 and then
14840 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14841 and then Is_Variant_Record (Parent_Type)
14842 then
14843 Collect_Fixed_Components (Typ);
14844
14845 Gather_Components
14846 (Typ,
14847 Component_List (Type_Definition (Parent (Parent_Type))),
14848 Governed_By => Assoc_List,
14849 Into => Comp_List,
14850 Report_Errors => Errors);
14851
14852 -- Note: previously there was a check at this point that no errors
14853 -- were detected. As a consequence of AI05-220 there may be an error
14854 -- if an inherited discriminant that controls a variant has a non-
14855 -- static constraint.
14856
14857 -- If the tagged derivation has a type extension, collect all the
14858 -- new components therein.
14859
14860 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14861 then
14862 Old_C := First_Component (Typ);
14863 while Present (Old_C) loop
14864 if Original_Record_Component (Old_C) = Old_C
14865 and then Chars (Old_C) /= Name_uTag
14866 and then Chars (Old_C) /= Name_uParent
14867 then
14868 Append_Elmt (Old_C, Comp_List);
14869 end if;
14870
14871 Next_Component (Old_C);
14872 end loop;
14873 end if;
14874
14875 Create_All_Components;
14876
14877 else
14878 -- If discriminants are not static, or if this is a multi-level type
14879 -- extension, we have to include all components of the parent type.
14880
14881 Old_C := First_Component (Typ);
14882 while Present (Old_C) loop
14883 New_C := Create_Component (Old_C);
14884
14885 Set_Etype
14886 (New_C,
14887 Constrain_Component_Type
14888 (Old_C, Subt, Decl_Node, Typ, Constraints));
14889 Set_Is_Public (New_C, Is_Public (Subt));
14890
14891 Next_Component (Old_C);
14892 end loop;
14893 end if;
14894
14895 End_Scope;
14896 end Create_Constrained_Components;
14897
14898 ------------------------------------------
14899 -- Decimal_Fixed_Point_Type_Declaration --
14900 ------------------------------------------
14901
14902 procedure Decimal_Fixed_Point_Type_Declaration
14903 (T : Entity_Id;
14904 Def : Node_Id)
14905 is
14906 Loc : constant Source_Ptr := Sloc (Def);
14907 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14908 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14909 Implicit_Base : Entity_Id;
14910 Digs_Val : Uint;
14911 Delta_Val : Ureal;
14912 Scale_Val : Uint;
14913 Bound_Val : Ureal;
14914
14915 begin
14916 Check_SPARK_05_Restriction
14917 ("decimal fixed point type is not allowed", Def);
14918 Check_Restriction (No_Fixed_Point, Def);
14919
14920 -- Create implicit base type
14921
14922 Implicit_Base :=
14923 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14924 Set_Etype (Implicit_Base, Implicit_Base);
14925
14926 -- Analyze and process delta expression
14927
14928 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14929
14930 Check_Delta_Expression (Delta_Expr);
14931 Delta_Val := Expr_Value_R (Delta_Expr);
14932
14933 -- Check delta is power of 10, and determine scale value from it
14934
14935 declare
14936 Val : Ureal;
14937
14938 begin
14939 Scale_Val := Uint_0;
14940 Val := Delta_Val;
14941
14942 if Val < Ureal_1 then
14943 while Val < Ureal_1 loop
14944 Val := Val * Ureal_10;
14945 Scale_Val := Scale_Val + 1;
14946 end loop;
14947
14948 if Scale_Val > 18 then
14949 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14950 Scale_Val := UI_From_Int (+18);
14951 end if;
14952
14953 else
14954 while Val > Ureal_1 loop
14955 Val := Val / Ureal_10;
14956 Scale_Val := Scale_Val - 1;
14957 end loop;
14958
14959 if Scale_Val < -18 then
14960 Error_Msg_N ("scale is less than minimum value of -18", Def);
14961 Scale_Val := UI_From_Int (-18);
14962 end if;
14963 end if;
14964
14965 if Val /= Ureal_1 then
14966 Error_Msg_N ("delta expression must be a power of 10", Def);
14967 Delta_Val := Ureal_10 ** (-Scale_Val);
14968 end if;
14969 end;
14970
14971 -- Set delta, scale and small (small = delta for decimal type)
14972
14973 Set_Delta_Value (Implicit_Base, Delta_Val);
14974 Set_Scale_Value (Implicit_Base, Scale_Val);
14975 Set_Small_Value (Implicit_Base, Delta_Val);
14976
14977 -- Analyze and process digits expression
14978
14979 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14980 Check_Digits_Expression (Digs_Expr);
14981 Digs_Val := Expr_Value (Digs_Expr);
14982
14983 if Digs_Val > 18 then
14984 Digs_Val := UI_From_Int (+18);
14985 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14986 end if;
14987
14988 Set_Digits_Value (Implicit_Base, Digs_Val);
14989 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14990
14991 -- Set range of base type from digits value for now. This will be
14992 -- expanded to represent the true underlying base range by Freeze.
14993
14994 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14995
14996 -- Note: We leave size as zero for now, size will be set at freeze
14997 -- time. We have to do this for ordinary fixed-point, because the size
14998 -- depends on the specified small, and we might as well do the same for
14999 -- decimal fixed-point.
15000
15001 pragma Assert (Esize (Implicit_Base) = Uint_0);
15002
15003 -- If there are bounds given in the declaration use them as the
15004 -- bounds of the first named subtype.
15005
15006 if Present (Real_Range_Specification (Def)) then
15007 declare
15008 RRS : constant Node_Id := Real_Range_Specification (Def);
15009 Low : constant Node_Id := Low_Bound (RRS);
15010 High : constant Node_Id := High_Bound (RRS);
15011 Low_Val : Ureal;
15012 High_Val : Ureal;
15013
15014 begin
15015 Analyze_And_Resolve (Low, Any_Real);
15016 Analyze_And_Resolve (High, Any_Real);
15017 Check_Real_Bound (Low);
15018 Check_Real_Bound (High);
15019 Low_Val := Expr_Value_R (Low);
15020 High_Val := Expr_Value_R (High);
15021
15022 if Low_Val < (-Bound_Val) then
15023 Error_Msg_N
15024 ("range low bound too small for digits value", Low);
15025 Low_Val := -Bound_Val;
15026 end if;
15027
15028 if High_Val > Bound_Val then
15029 Error_Msg_N
15030 ("range high bound too large for digits value", High);
15031 High_Val := Bound_Val;
15032 end if;
15033
15034 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15035 end;
15036
15037 -- If no explicit range, use range that corresponds to given
15038 -- digits value. This will end up as the final range for the
15039 -- first subtype.
15040
15041 else
15042 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15043 end if;
15044
15045 -- Complete entity for first subtype. The inheritance of the rep item
15046 -- chain ensures that SPARK-related pragmas are not clobbered when the
15047 -- decimal fixed point type acts as a full view of a private type.
15048
15049 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15050 Set_Etype (T, Implicit_Base);
15051 Set_Size_Info (T, Implicit_Base);
15052 Inherit_Rep_Item_Chain (T, Implicit_Base);
15053 Set_Digits_Value (T, Digs_Val);
15054 Set_Delta_Value (T, Delta_Val);
15055 Set_Small_Value (T, Delta_Val);
15056 Set_Scale_Value (T, Scale_Val);
15057 Set_Is_Constrained (T);
15058 end Decimal_Fixed_Point_Type_Declaration;
15059
15060 -----------------------------------
15061 -- Derive_Progenitor_Subprograms --
15062 -----------------------------------
15063
15064 procedure Derive_Progenitor_Subprograms
15065 (Parent_Type : Entity_Id;
15066 Tagged_Type : Entity_Id)
15067 is
15068 E : Entity_Id;
15069 Elmt : Elmt_Id;
15070 Iface : Entity_Id;
15071 Iface_Alias : Entity_Id;
15072 Iface_Elmt : Elmt_Id;
15073 Iface_Subp : Entity_Id;
15074 New_Subp : Entity_Id := Empty;
15075 Prim_Elmt : Elmt_Id;
15076 Subp : Entity_Id;
15077 Typ : Entity_Id;
15078
15079 begin
15080 pragma Assert (Ada_Version >= Ada_2005
15081 and then Is_Record_Type (Tagged_Type)
15082 and then Is_Tagged_Type (Tagged_Type)
15083 and then Has_Interfaces (Tagged_Type));
15084
15085 -- Step 1: Transfer to the full-view primitives associated with the
15086 -- partial-view that cover interface primitives. Conceptually this
15087 -- work should be done later by Process_Full_View; done here to
15088 -- simplify its implementation at later stages. It can be safely
15089 -- done here because interfaces must be visible in the partial and
15090 -- private view (RM 7.3(7.3/2)).
15091
15092 -- Small optimization: This work is only required if the parent may
15093 -- have entities whose Alias attribute reference an interface primitive.
15094 -- Such a situation may occur if the parent is an abstract type and the
15095 -- primitive has not been yet overridden or if the parent is a generic
15096 -- formal type covering interfaces.
15097
15098 -- If the tagged type is not abstract, it cannot have abstract
15099 -- primitives (the only entities in the list of primitives of
15100 -- non-abstract tagged types that can reference abstract primitives
15101 -- through its Alias attribute are the internal entities that have
15102 -- attribute Interface_Alias, and these entities are generated later
15103 -- by Add_Internal_Interface_Entities).
15104
15105 if In_Private_Part (Current_Scope)
15106 and then (Is_Abstract_Type (Parent_Type)
15107 or else
15108 Is_Generic_Type (Parent_Type))
15109 then
15110 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15111 while Present (Elmt) loop
15112 Subp := Node (Elmt);
15113
15114 -- At this stage it is not possible to have entities in the list
15115 -- of primitives that have attribute Interface_Alias.
15116
15117 pragma Assert (No (Interface_Alias (Subp)));
15118
15119 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15120
15121 if Is_Interface (Typ) then
15122 E := Find_Primitive_Covering_Interface
15123 (Tagged_Type => Tagged_Type,
15124 Iface_Prim => Subp);
15125
15126 if Present (E)
15127 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15128 then
15129 Replace_Elmt (Elmt, E);
15130 Remove_Homonym (Subp);
15131 end if;
15132 end if;
15133
15134 Next_Elmt (Elmt);
15135 end loop;
15136 end if;
15137
15138 -- Step 2: Add primitives of progenitors that are not implemented by
15139 -- parents of Tagged_Type.
15140
15141 if Present (Interfaces (Base_Type (Tagged_Type))) then
15142 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15143 while Present (Iface_Elmt) loop
15144 Iface := Node (Iface_Elmt);
15145
15146 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15147 while Present (Prim_Elmt) loop
15148 Iface_Subp := Node (Prim_Elmt);
15149 Iface_Alias := Ultimate_Alias (Iface_Subp);
15150
15151 -- Exclude derivation of predefined primitives except those
15152 -- that come from source, or are inherited from one that comes
15153 -- from source. Required to catch declarations of equality
15154 -- operators of interfaces. For example:
15155
15156 -- type Iface is interface;
15157 -- function "=" (Left, Right : Iface) return Boolean;
15158
15159 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15160 or else Comes_From_Source (Iface_Alias)
15161 then
15162 E :=
15163 Find_Primitive_Covering_Interface
15164 (Tagged_Type => Tagged_Type,
15165 Iface_Prim => Iface_Subp);
15166
15167 -- If not found we derive a new primitive leaving its alias
15168 -- attribute referencing the interface primitive.
15169
15170 if No (E) then
15171 Derive_Subprogram
15172 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15173
15174 -- Ada 2012 (AI05-0197): If the covering primitive's name
15175 -- differs from the name of the interface primitive then it
15176 -- is a private primitive inherited from a parent type. In
15177 -- such case, given that Tagged_Type covers the interface,
15178 -- the inherited private primitive becomes visible. For such
15179 -- purpose we add a new entity that renames the inherited
15180 -- private primitive.
15181
15182 elsif Chars (E) /= Chars (Iface_Subp) then
15183 pragma Assert (Has_Suffix (E, 'P'));
15184 Derive_Subprogram
15185 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15186 Set_Alias (New_Subp, E);
15187 Set_Is_Abstract_Subprogram (New_Subp,
15188 Is_Abstract_Subprogram (E));
15189
15190 -- Propagate to the full view interface entities associated
15191 -- with the partial view.
15192
15193 elsif In_Private_Part (Current_Scope)
15194 and then Present (Alias (E))
15195 and then Alias (E) = Iface_Subp
15196 and then
15197 List_Containing (Parent (E)) /=
15198 Private_Declarations
15199 (Specification
15200 (Unit_Declaration_Node (Current_Scope)))
15201 then
15202 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15203 end if;
15204 end if;
15205
15206 Next_Elmt (Prim_Elmt);
15207 end loop;
15208
15209 Next_Elmt (Iface_Elmt);
15210 end loop;
15211 end if;
15212 end Derive_Progenitor_Subprograms;
15213
15214 -----------------------
15215 -- Derive_Subprogram --
15216 -----------------------
15217
15218 procedure Derive_Subprogram
15219 (New_Subp : out Entity_Id;
15220 Parent_Subp : Entity_Id;
15221 Derived_Type : Entity_Id;
15222 Parent_Type : Entity_Id;
15223 Actual_Subp : Entity_Id := Empty)
15224 is
15225 Formal : Entity_Id;
15226 -- Formal parameter of parent primitive operation
15227
15228 Formal_Of_Actual : Entity_Id;
15229 -- Formal parameter of actual operation, when the derivation is to
15230 -- create a renaming for a primitive operation of an actual in an
15231 -- instantiation.
15232
15233 New_Formal : Entity_Id;
15234 -- Formal of inherited operation
15235
15236 Visible_Subp : Entity_Id := Parent_Subp;
15237
15238 function Is_Private_Overriding return Boolean;
15239 -- If Subp is a private overriding of a visible operation, the inherited
15240 -- operation derives from the overridden op (even though its body is the
15241 -- overriding one) and the inherited operation is visible now. See
15242 -- sem_disp to see the full details of the handling of the overridden
15243 -- subprogram, which is removed from the list of primitive operations of
15244 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15245 -- and used to diagnose abstract operations that need overriding in the
15246 -- derived type.
15247
15248 procedure Replace_Type (Id, New_Id : Entity_Id);
15249 -- When the type is an anonymous access type, create a new access type
15250 -- designating the derived type.
15251
15252 procedure Set_Derived_Name;
15253 -- This procedure sets the appropriate Chars name for New_Subp. This
15254 -- is normally just a copy of the parent name. An exception arises for
15255 -- type support subprograms, where the name is changed to reflect the
15256 -- name of the derived type, e.g. if type foo is derived from type bar,
15257 -- then a procedure barDA is derived with a name fooDA.
15258
15259 ---------------------------
15260 -- Is_Private_Overriding --
15261 ---------------------------
15262
15263 function Is_Private_Overriding return Boolean is
15264 Prev : Entity_Id;
15265
15266 begin
15267 -- If the parent is not a dispatching operation there is no
15268 -- need to investigate overridings
15269
15270 if not Is_Dispatching_Operation (Parent_Subp) then
15271 return False;
15272 end if;
15273
15274 -- The visible operation that is overridden is a homonym of the
15275 -- parent subprogram. We scan the homonym chain to find the one
15276 -- whose alias is the subprogram we are deriving.
15277
15278 Prev := Current_Entity (Parent_Subp);
15279 while Present (Prev) loop
15280 if Ekind (Prev) = Ekind (Parent_Subp)
15281 and then Alias (Prev) = Parent_Subp
15282 and then Scope (Parent_Subp) = Scope (Prev)
15283 and then not Is_Hidden (Prev)
15284 then
15285 Visible_Subp := Prev;
15286 return True;
15287 end if;
15288
15289 Prev := Homonym (Prev);
15290 end loop;
15291
15292 return False;
15293 end Is_Private_Overriding;
15294
15295 ------------------
15296 -- Replace_Type --
15297 ------------------
15298
15299 procedure Replace_Type (Id, New_Id : Entity_Id) is
15300 Id_Type : constant Entity_Id := Etype (Id);
15301 Acc_Type : Entity_Id;
15302 Par : constant Node_Id := Parent (Derived_Type);
15303
15304 begin
15305 -- When the type is an anonymous access type, create a new access
15306 -- type designating the derived type. This itype must be elaborated
15307 -- at the point of the derivation, not on subsequent calls that may
15308 -- be out of the proper scope for Gigi, so we insert a reference to
15309 -- it after the derivation.
15310
15311 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15312 declare
15313 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15314
15315 begin
15316 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15317 and then Present (Full_View (Desig_Typ))
15318 and then not Is_Private_Type (Parent_Type)
15319 then
15320 Desig_Typ := Full_View (Desig_Typ);
15321 end if;
15322
15323 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15324
15325 -- Ada 2005 (AI-251): Handle also derivations of abstract
15326 -- interface primitives.
15327
15328 or else (Is_Interface (Desig_Typ)
15329 and then not Is_Class_Wide_Type (Desig_Typ))
15330 then
15331 Acc_Type := New_Copy (Id_Type);
15332 Set_Etype (Acc_Type, Acc_Type);
15333 Set_Scope (Acc_Type, New_Subp);
15334
15335 -- Set size of anonymous access type. If we have an access
15336 -- to an unconstrained array, this is a fat pointer, so it
15337 -- is sizes at twice addtress size.
15338
15339 if Is_Array_Type (Desig_Typ)
15340 and then not Is_Constrained (Desig_Typ)
15341 then
15342 Init_Size (Acc_Type, 2 * System_Address_Size);
15343
15344 -- Other cases use a thin pointer
15345
15346 else
15347 Init_Size (Acc_Type, System_Address_Size);
15348 end if;
15349
15350 -- Set remaining characterstics of anonymous access type
15351
15352 Init_Alignment (Acc_Type);
15353 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15354
15355 Set_Etype (New_Id, Acc_Type);
15356 Set_Scope (New_Id, New_Subp);
15357
15358 -- Create a reference to it
15359
15360 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15361
15362 else
15363 Set_Etype (New_Id, Id_Type);
15364 end if;
15365 end;
15366
15367 -- In Ada2012, a formal may have an incomplete type but the type
15368 -- derivation that inherits the primitive follows the full view.
15369
15370 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15371 or else
15372 (Ekind (Id_Type) = E_Record_Type_With_Private
15373 and then Present (Full_View (Id_Type))
15374 and then
15375 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15376 or else
15377 (Ada_Version >= Ada_2012
15378 and then Ekind (Id_Type) = E_Incomplete_Type
15379 and then Full_View (Id_Type) = Parent_Type)
15380 then
15381 -- Constraint checks on formals are generated during expansion,
15382 -- based on the signature of the original subprogram. The bounds
15383 -- of the derived type are not relevant, and thus we can use
15384 -- the base type for the formals. However, the return type may be
15385 -- used in a context that requires that the proper static bounds
15386 -- be used (a case statement, for example) and for those cases
15387 -- we must use the derived type (first subtype), not its base.
15388
15389 -- If the derived_type_definition has no constraints, we know that
15390 -- the derived type has the same constraints as the first subtype
15391 -- of the parent, and we can also use it rather than its base,
15392 -- which can lead to more efficient code.
15393
15394 if Etype (Id) = Parent_Type then
15395 if Is_Scalar_Type (Parent_Type)
15396 and then
15397 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15398 then
15399 Set_Etype (New_Id, Derived_Type);
15400
15401 elsif Nkind (Par) = N_Full_Type_Declaration
15402 and then
15403 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15404 and then
15405 Is_Entity_Name
15406 (Subtype_Indication (Type_Definition (Par)))
15407 then
15408 Set_Etype (New_Id, Derived_Type);
15409
15410 else
15411 Set_Etype (New_Id, Base_Type (Derived_Type));
15412 end if;
15413
15414 else
15415 Set_Etype (New_Id, Base_Type (Derived_Type));
15416 end if;
15417
15418 else
15419 Set_Etype (New_Id, Etype (Id));
15420 end if;
15421 end Replace_Type;
15422
15423 ----------------------
15424 -- Set_Derived_Name --
15425 ----------------------
15426
15427 procedure Set_Derived_Name is
15428 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15429 begin
15430 if Nm = TSS_Null then
15431 Set_Chars (New_Subp, Chars (Parent_Subp));
15432 else
15433 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15434 end if;
15435 end Set_Derived_Name;
15436
15437 -- Start of processing for Derive_Subprogram
15438
15439 begin
15440 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15441 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15442
15443 -- Check whether the inherited subprogram is a private operation that
15444 -- should be inherited but not yet made visible. Such subprograms can
15445 -- become visible at a later point (e.g., the private part of a public
15446 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15447 -- following predicate is true, then this is not such a private
15448 -- operation and the subprogram simply inherits the name of the parent
15449 -- subprogram. Note the special check for the names of controlled
15450 -- operations, which are currently exempted from being inherited with
15451 -- a hidden name because they must be findable for generation of
15452 -- implicit run-time calls.
15453
15454 if not Is_Hidden (Parent_Subp)
15455 or else Is_Internal (Parent_Subp)
15456 or else Is_Private_Overriding
15457 or else Is_Internal_Name (Chars (Parent_Subp))
15458 or else (Is_Controlled (Parent_Type)
15459 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15460 Name_Finalize,
15461 Name_Initialize))
15462 then
15463 Set_Derived_Name;
15464
15465 -- An inherited dispatching equality will be overridden by an internally
15466 -- generated one, or by an explicit one, so preserve its name and thus
15467 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15468 -- private operation it may become invisible if the full view has
15469 -- progenitors, and the dispatch table will be malformed.
15470 -- We check that the type is limited to handle the anomalous declaration
15471 -- of Limited_Controlled, which is derived from a non-limited type, and
15472 -- which is handled specially elsewhere as well.
15473
15474 elsif Chars (Parent_Subp) = Name_Op_Eq
15475 and then Is_Dispatching_Operation (Parent_Subp)
15476 and then Etype (Parent_Subp) = Standard_Boolean
15477 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15478 and then
15479 Etype (First_Formal (Parent_Subp)) =
15480 Etype (Next_Formal (First_Formal (Parent_Subp)))
15481 then
15482 Set_Derived_Name;
15483
15484 -- If parent is hidden, this can be a regular derivation if the
15485 -- parent is immediately visible in a non-instantiating context,
15486 -- or if we are in the private part of an instance. This test
15487 -- should still be refined ???
15488
15489 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15490 -- operation as a non-visible operation in cases where the parent
15491 -- subprogram might not be visible now, but was visible within the
15492 -- original generic, so it would be wrong to make the inherited
15493 -- subprogram non-visible now. (Not clear if this test is fully
15494 -- correct; are there any cases where we should declare the inherited
15495 -- operation as not visible to avoid it being overridden, e.g., when
15496 -- the parent type is a generic actual with private primitives ???)
15497
15498 -- (they should be treated the same as other private inherited
15499 -- subprograms, but it's not clear how to do this cleanly). ???
15500
15501 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15502 and then Is_Immediately_Visible (Parent_Subp)
15503 and then not In_Instance)
15504 or else In_Instance_Not_Visible
15505 then
15506 Set_Derived_Name;
15507
15508 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15509 -- overrides an interface primitive because interface primitives
15510 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15511
15512 elsif Ada_Version >= Ada_2005
15513 and then Is_Dispatching_Operation (Parent_Subp)
15514 and then Present (Covered_Interface_Op (Parent_Subp))
15515 then
15516 Set_Derived_Name;
15517
15518 -- Otherwise, the type is inheriting a private operation, so enter it
15519 -- with a special name so it can't be overridden. See also below, where
15520 -- we check for this case, and if so avoid setting Requires_Overriding.
15521
15522 else
15523 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15524 end if;
15525
15526 Set_Parent (New_Subp, Parent (Derived_Type));
15527
15528 if Present (Actual_Subp) then
15529 Replace_Type (Actual_Subp, New_Subp);
15530 else
15531 Replace_Type (Parent_Subp, New_Subp);
15532 end if;
15533
15534 Conditional_Delay (New_Subp, Parent_Subp);
15535
15536 -- If we are creating a renaming for a primitive operation of an
15537 -- actual of a generic derived type, we must examine the signature
15538 -- of the actual primitive, not that of the generic formal, which for
15539 -- example may be an interface. However the name and initial value
15540 -- of the inherited operation are those of the formal primitive.
15541
15542 Formal := First_Formal (Parent_Subp);
15543
15544 if Present (Actual_Subp) then
15545 Formal_Of_Actual := First_Formal (Actual_Subp);
15546 else
15547 Formal_Of_Actual := Empty;
15548 end if;
15549
15550 while Present (Formal) loop
15551 New_Formal := New_Copy (Formal);
15552
15553 -- Normally we do not go copying parents, but in the case of
15554 -- formals, we need to link up to the declaration (which is the
15555 -- parameter specification), and it is fine to link up to the
15556 -- original formal's parameter specification in this case.
15557
15558 Set_Parent (New_Formal, Parent (Formal));
15559 Append_Entity (New_Formal, New_Subp);
15560
15561 if Present (Formal_Of_Actual) then
15562 Replace_Type (Formal_Of_Actual, New_Formal);
15563 Next_Formal (Formal_Of_Actual);
15564 else
15565 Replace_Type (Formal, New_Formal);
15566 end if;
15567
15568 Next_Formal (Formal);
15569 end loop;
15570
15571 -- If this derivation corresponds to a tagged generic actual, then
15572 -- primitive operations rename those of the actual. Otherwise the
15573 -- primitive operations rename those of the parent type, If the parent
15574 -- renames an intrinsic operator, so does the new subprogram. We except
15575 -- concatenation, which is always properly typed, and does not get
15576 -- expanded as other intrinsic operations.
15577
15578 if No (Actual_Subp) then
15579 if Is_Intrinsic_Subprogram (Parent_Subp) then
15580 Set_Is_Intrinsic_Subprogram (New_Subp);
15581
15582 if Present (Alias (Parent_Subp))
15583 and then Chars (Parent_Subp) /= Name_Op_Concat
15584 then
15585 Set_Alias (New_Subp, Alias (Parent_Subp));
15586 else
15587 Set_Alias (New_Subp, Parent_Subp);
15588 end if;
15589
15590 else
15591 Set_Alias (New_Subp, Parent_Subp);
15592 end if;
15593
15594 else
15595 Set_Alias (New_Subp, Actual_Subp);
15596 end if;
15597
15598 -- Derived subprograms of a tagged type must inherit the convention
15599 -- of the parent subprogram (a requirement of AI-117). Derived
15600 -- subprograms of untagged types simply get convention Ada by default.
15601
15602 -- If the derived type is a tagged generic formal type with unknown
15603 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15604
15605 -- However, if the type is derived from a generic formal, the further
15606 -- inherited subprogram has the convention of the non-generic ancestor.
15607 -- Otherwise there would be no way to override the operation.
15608 -- (This is subject to forthcoming ARG discussions).
15609
15610 if Is_Tagged_Type (Derived_Type) then
15611 if Is_Generic_Type (Derived_Type)
15612 and then Has_Unknown_Discriminants (Derived_Type)
15613 then
15614 Set_Convention (New_Subp, Convention_Intrinsic);
15615
15616 else
15617 if Is_Generic_Type (Parent_Type)
15618 and then Has_Unknown_Discriminants (Parent_Type)
15619 then
15620 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15621 else
15622 Set_Convention (New_Subp, Convention (Parent_Subp));
15623 end if;
15624 end if;
15625 end if;
15626
15627 -- Predefined controlled operations retain their name even if the parent
15628 -- is hidden (see above), but they are not primitive operations if the
15629 -- ancestor is not visible, for example if the parent is a private
15630 -- extension completed with a controlled extension. Note that a full
15631 -- type that is controlled can break privacy: the flag Is_Controlled is
15632 -- set on both views of the type.
15633
15634 if Is_Controlled (Parent_Type)
15635 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15636 Name_Adjust,
15637 Name_Finalize)
15638 and then Is_Hidden (Parent_Subp)
15639 and then not Is_Visibly_Controlled (Parent_Type)
15640 then
15641 Set_Is_Hidden (New_Subp);
15642 end if;
15643
15644 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15645 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15646
15647 if Ekind (Parent_Subp) = E_Procedure then
15648 Set_Is_Valued_Procedure
15649 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15650 else
15651 Set_Has_Controlling_Result
15652 (New_Subp, Has_Controlling_Result (Parent_Subp));
15653 end if;
15654
15655 -- No_Return must be inherited properly. If this is overridden in the
15656 -- case of a dispatching operation, then a check is made in Sem_Disp
15657 -- that the overriding operation is also No_Return (no such check is
15658 -- required for the case of non-dispatching operation.
15659
15660 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15661
15662 -- A derived function with a controlling result is abstract. If the
15663 -- Derived_Type is a nonabstract formal generic derived type, then
15664 -- inherited operations are not abstract: the required check is done at
15665 -- instantiation time. If the derivation is for a generic actual, the
15666 -- function is not abstract unless the actual is.
15667
15668 if Is_Generic_Type (Derived_Type)
15669 and then not Is_Abstract_Type (Derived_Type)
15670 then
15671 null;
15672
15673 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15674 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15675
15676 -- A subprogram subject to pragma Extensions_Visible with value False
15677 -- requires overriding if the subprogram has at least one controlling
15678 -- OUT parameter (SPARK RM 6.1.7(6)).
15679
15680 elsif Ada_Version >= Ada_2005
15681 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15682 or else (Is_Tagged_Type (Derived_Type)
15683 and then Etype (New_Subp) = Derived_Type
15684 and then not Is_Null_Extension (Derived_Type))
15685 or else (Is_Tagged_Type (Derived_Type)
15686 and then Ekind (Etype (New_Subp)) =
15687 E_Anonymous_Access_Type
15688 and then Designated_Type (Etype (New_Subp)) =
15689 Derived_Type
15690 and then not Is_Null_Extension (Derived_Type))
15691 or else (Comes_From_Source (Alias (New_Subp))
15692 and then Is_EVF_Procedure (Alias (New_Subp))))
15693 and then No (Actual_Subp)
15694 then
15695 if not Is_Tagged_Type (Derived_Type)
15696 or else Is_Abstract_Type (Derived_Type)
15697 or else Is_Abstract_Subprogram (Alias (New_Subp))
15698 then
15699 Set_Is_Abstract_Subprogram (New_Subp);
15700
15701 -- If the Chars of the new subprogram is different from that of the
15702 -- parent's one, it means that we entered it with a special name so
15703 -- it can't be overridden (see above). In that case we had better not
15704 -- *require* it to be overridden. This is the case where the parent
15705 -- type inherited the operation privately, so there's no danger of
15706 -- dangling dispatching.
15707
15708 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
15709 Set_Requires_Overriding (New_Subp);
15710 end if;
15711
15712 elsif Ada_Version < Ada_2005
15713 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15714 or else (Is_Tagged_Type (Derived_Type)
15715 and then Etype (New_Subp) = Derived_Type
15716 and then No (Actual_Subp)))
15717 then
15718 Set_Is_Abstract_Subprogram (New_Subp);
15719
15720 -- AI05-0097 : an inherited operation that dispatches on result is
15721 -- abstract if the derived type is abstract, even if the parent type
15722 -- is concrete and the derived type is a null extension.
15723
15724 elsif Has_Controlling_Result (Alias (New_Subp))
15725 and then Is_Abstract_Type (Etype (New_Subp))
15726 then
15727 Set_Is_Abstract_Subprogram (New_Subp);
15728
15729 -- Finally, if the parent type is abstract we must verify that all
15730 -- inherited operations are either non-abstract or overridden, or that
15731 -- the derived type itself is abstract (this check is performed at the
15732 -- end of a package declaration, in Check_Abstract_Overriding). A
15733 -- private overriding in the parent type will not be visible in the
15734 -- derivation if we are not in an inner package or in a child unit of
15735 -- the parent type, in which case the abstractness of the inherited
15736 -- operation is carried to the new subprogram.
15737
15738 elsif Is_Abstract_Type (Parent_Type)
15739 and then not In_Open_Scopes (Scope (Parent_Type))
15740 and then Is_Private_Overriding
15741 and then Is_Abstract_Subprogram (Visible_Subp)
15742 then
15743 if No (Actual_Subp) then
15744 Set_Alias (New_Subp, Visible_Subp);
15745 Set_Is_Abstract_Subprogram (New_Subp, True);
15746
15747 else
15748 -- If this is a derivation for an instance of a formal derived
15749 -- type, abstractness comes from the primitive operation of the
15750 -- actual, not from the operation inherited from the ancestor.
15751
15752 Set_Is_Abstract_Subprogram
15753 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15754 end if;
15755 end if;
15756
15757 New_Overloaded_Entity (New_Subp, Derived_Type);
15758
15759 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15760 -- preconditions and the derived type is abstract, the derived operation
15761 -- is abstract as well if parent subprogram is not abstract or null.
15762
15763 if Is_Abstract_Type (Derived_Type)
15764 and then Has_Non_Trivial_Precondition (Parent_Subp)
15765 and then Present (Interfaces (Derived_Type))
15766 then
15767
15768 -- Add useful attributes of subprogram before the freeze point,
15769 -- in case freezing is delayed or there are previous errors.
15770
15771 Set_Is_Dispatching_Operation (New_Subp);
15772
15773 declare
15774 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15775
15776 begin
15777 if Present (Iface_Prim)
15778 and then Has_Non_Trivial_Precondition (Iface_Prim)
15779 then
15780 Set_Is_Abstract_Subprogram (New_Subp);
15781 end if;
15782 end;
15783 end if;
15784
15785 -- Check for case of a derived subprogram for the instantiation of a
15786 -- formal derived tagged type, if so mark the subprogram as dispatching
15787 -- and inherit the dispatching attributes of the actual subprogram. The
15788 -- derived subprogram is effectively renaming of the actual subprogram,
15789 -- so it needs to have the same attributes as the actual.
15790
15791 if Present (Actual_Subp)
15792 and then Is_Dispatching_Operation (Actual_Subp)
15793 then
15794 Set_Is_Dispatching_Operation (New_Subp);
15795
15796 if Present (DTC_Entity (Actual_Subp)) then
15797 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15798 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15799 end if;
15800 end if;
15801
15802 -- Indicate that a derived subprogram does not require a body and that
15803 -- it does not require processing of default expressions.
15804
15805 Set_Has_Completion (New_Subp);
15806 Set_Default_Expressions_Processed (New_Subp);
15807
15808 if Ekind (New_Subp) = E_Function then
15809 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15810 end if;
15811 end Derive_Subprogram;
15812
15813 ------------------------
15814 -- Derive_Subprograms --
15815 ------------------------
15816
15817 procedure Derive_Subprograms
15818 (Parent_Type : Entity_Id;
15819 Derived_Type : Entity_Id;
15820 Generic_Actual : Entity_Id := Empty)
15821 is
15822 Op_List : constant Elist_Id :=
15823 Collect_Primitive_Operations (Parent_Type);
15824
15825 function Check_Derived_Type return Boolean;
15826 -- Check that all the entities derived from Parent_Type are found in
15827 -- the list of primitives of Derived_Type exactly in the same order.
15828
15829 procedure Derive_Interface_Subprogram
15830 (New_Subp : out Entity_Id;
15831 Subp : Entity_Id;
15832 Actual_Subp : Entity_Id);
15833 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15834 -- (which is an interface primitive). If Generic_Actual is present then
15835 -- Actual_Subp is the actual subprogram corresponding with the generic
15836 -- subprogram Subp.
15837
15838 ------------------------
15839 -- Check_Derived_Type --
15840 ------------------------
15841
15842 function Check_Derived_Type return Boolean is
15843 E : Entity_Id;
15844 Elmt : Elmt_Id;
15845 List : Elist_Id;
15846 New_Subp : Entity_Id;
15847 Op_Elmt : Elmt_Id;
15848 Subp : Entity_Id;
15849
15850 begin
15851 -- Traverse list of entities in the current scope searching for
15852 -- an incomplete type whose full-view is derived type.
15853
15854 E := First_Entity (Scope (Derived_Type));
15855 while Present (E) and then E /= Derived_Type loop
15856 if Ekind (E) = E_Incomplete_Type
15857 and then Present (Full_View (E))
15858 and then Full_View (E) = Derived_Type
15859 then
15860 -- Disable this test if Derived_Type completes an incomplete
15861 -- type because in such case more primitives can be added
15862 -- later to the list of primitives of Derived_Type by routine
15863 -- Process_Incomplete_Dependents
15864
15865 return True;
15866 end if;
15867
15868 Next_Entity (E);
15869 end loop;
15870
15871 List := Collect_Primitive_Operations (Derived_Type);
15872 Elmt := First_Elmt (List);
15873
15874 Op_Elmt := First_Elmt (Op_List);
15875 while Present (Op_Elmt) loop
15876 Subp := Node (Op_Elmt);
15877 New_Subp := Node (Elmt);
15878
15879 -- At this early stage Derived_Type has no entities with attribute
15880 -- Interface_Alias. In addition, such primitives are always
15881 -- located at the end of the list of primitives of Parent_Type.
15882 -- Therefore, if found we can safely stop processing pending
15883 -- entities.
15884
15885 exit when Present (Interface_Alias (Subp));
15886
15887 -- Handle hidden entities
15888
15889 if not Is_Predefined_Dispatching_Operation (Subp)
15890 and then Is_Hidden (Subp)
15891 then
15892 if Present (New_Subp)
15893 and then Primitive_Names_Match (Subp, New_Subp)
15894 then
15895 Next_Elmt (Elmt);
15896 end if;
15897
15898 else
15899 if not Present (New_Subp)
15900 or else Ekind (Subp) /= Ekind (New_Subp)
15901 or else not Primitive_Names_Match (Subp, New_Subp)
15902 then
15903 return False;
15904 end if;
15905
15906 Next_Elmt (Elmt);
15907 end if;
15908
15909 Next_Elmt (Op_Elmt);
15910 end loop;
15911
15912 return True;
15913 end Check_Derived_Type;
15914
15915 ---------------------------------
15916 -- Derive_Interface_Subprogram --
15917 ---------------------------------
15918
15919 procedure Derive_Interface_Subprogram
15920 (New_Subp : out Entity_Id;
15921 Subp : Entity_Id;
15922 Actual_Subp : Entity_Id)
15923 is
15924 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15925 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15926
15927 begin
15928 pragma Assert (Is_Interface (Iface_Type));
15929
15930 Derive_Subprogram
15931 (New_Subp => New_Subp,
15932 Parent_Subp => Iface_Subp,
15933 Derived_Type => Derived_Type,
15934 Parent_Type => Iface_Type,
15935 Actual_Subp => Actual_Subp);
15936
15937 -- Given that this new interface entity corresponds with a primitive
15938 -- of the parent that was not overridden we must leave it associated
15939 -- with its parent primitive to ensure that it will share the same
15940 -- dispatch table slot when overridden. We must set the Alias to Subp
15941 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15942 -- (in case we inherited Subp from Iface_Type via a nonabstract
15943 -- generic formal type).
15944
15945 if No (Actual_Subp) then
15946 Set_Alias (New_Subp, Subp);
15947
15948 declare
15949 T : Entity_Id := Find_Dispatching_Type (Subp);
15950 begin
15951 while Etype (T) /= T loop
15952 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15953 Set_Is_Abstract_Subprogram (New_Subp, False);
15954 exit;
15955 end if;
15956
15957 T := Etype (T);
15958 end loop;
15959 end;
15960
15961 -- For instantiations this is not needed since the previous call to
15962 -- Derive_Subprogram leaves the entity well decorated.
15963
15964 else
15965 pragma Assert (Alias (New_Subp) = Actual_Subp);
15966 null;
15967 end if;
15968 end Derive_Interface_Subprogram;
15969
15970 -- Local variables
15971
15972 Alias_Subp : Entity_Id;
15973 Act_List : Elist_Id;
15974 Act_Elmt : Elmt_Id;
15975 Act_Subp : Entity_Id := Empty;
15976 Elmt : Elmt_Id;
15977 Need_Search : Boolean := False;
15978 New_Subp : Entity_Id := Empty;
15979 Parent_Base : Entity_Id;
15980 Subp : Entity_Id;
15981
15982 -- Start of processing for Derive_Subprograms
15983
15984 begin
15985 if Ekind (Parent_Type) = E_Record_Type_With_Private
15986 and then Has_Discriminants (Parent_Type)
15987 and then Present (Full_View (Parent_Type))
15988 then
15989 Parent_Base := Full_View (Parent_Type);
15990 else
15991 Parent_Base := Parent_Type;
15992 end if;
15993
15994 if Present (Generic_Actual) then
15995 Act_List := Collect_Primitive_Operations (Generic_Actual);
15996 Act_Elmt := First_Elmt (Act_List);
15997 else
15998 Act_List := No_Elist;
15999 Act_Elmt := No_Elmt;
16000 end if;
16001
16002 -- Derive primitives inherited from the parent. Note that if the generic
16003 -- actual is present, this is not really a type derivation, it is a
16004 -- completion within an instance.
16005
16006 -- Case 1: Derived_Type does not implement interfaces
16007
16008 if not Is_Tagged_Type (Derived_Type)
16009 or else (not Has_Interfaces (Derived_Type)
16010 and then not (Present (Generic_Actual)
16011 and then Has_Interfaces (Generic_Actual)))
16012 then
16013 Elmt := First_Elmt (Op_List);
16014 while Present (Elmt) loop
16015 Subp := Node (Elmt);
16016
16017 -- Literals are derived earlier in the process of building the
16018 -- derived type, and are skipped here.
16019
16020 if Ekind (Subp) = E_Enumeration_Literal then
16021 null;
16022
16023 -- The actual is a direct descendant and the common primitive
16024 -- operations appear in the same order.
16025
16026 -- If the generic parent type is present, the derived type is an
16027 -- instance of a formal derived type, and within the instance its
16028 -- operations are those of the actual. We derive from the formal
16029 -- type but make the inherited operations aliases of the
16030 -- corresponding operations of the actual.
16031
16032 else
16033 pragma Assert (No (Node (Act_Elmt))
16034 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16035 and then
16036 Type_Conformant
16037 (Subp, Node (Act_Elmt),
16038 Skip_Controlling_Formals => True)));
16039
16040 Derive_Subprogram
16041 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16042
16043 if Present (Act_Elmt) then
16044 Next_Elmt (Act_Elmt);
16045 end if;
16046 end if;
16047
16048 Next_Elmt (Elmt);
16049 end loop;
16050
16051 -- Case 2: Derived_Type implements interfaces
16052
16053 else
16054 -- If the parent type has no predefined primitives we remove
16055 -- predefined primitives from the list of primitives of generic
16056 -- actual to simplify the complexity of this algorithm.
16057
16058 if Present (Generic_Actual) then
16059 declare
16060 Has_Predefined_Primitives : Boolean := False;
16061
16062 begin
16063 -- Check if the parent type has predefined primitives
16064
16065 Elmt := First_Elmt (Op_List);
16066 while Present (Elmt) loop
16067 Subp := Node (Elmt);
16068
16069 if Is_Predefined_Dispatching_Operation (Subp)
16070 and then not Comes_From_Source (Ultimate_Alias (Subp))
16071 then
16072 Has_Predefined_Primitives := True;
16073 exit;
16074 end if;
16075
16076 Next_Elmt (Elmt);
16077 end loop;
16078
16079 -- Remove predefined primitives of Generic_Actual. We must use
16080 -- an auxiliary list because in case of tagged types the value
16081 -- returned by Collect_Primitive_Operations is the value stored
16082 -- in its Primitive_Operations attribute (and we don't want to
16083 -- modify its current contents).
16084
16085 if not Has_Predefined_Primitives then
16086 declare
16087 Aux_List : constant Elist_Id := New_Elmt_List;
16088
16089 begin
16090 Elmt := First_Elmt (Act_List);
16091 while Present (Elmt) loop
16092 Subp := Node (Elmt);
16093
16094 if not Is_Predefined_Dispatching_Operation (Subp)
16095 or else Comes_From_Source (Subp)
16096 then
16097 Append_Elmt (Subp, Aux_List);
16098 end if;
16099
16100 Next_Elmt (Elmt);
16101 end loop;
16102
16103 Act_List := Aux_List;
16104 end;
16105 end if;
16106
16107 Act_Elmt := First_Elmt (Act_List);
16108 Act_Subp := Node (Act_Elmt);
16109 end;
16110 end if;
16111
16112 -- Stage 1: If the generic actual is not present we derive the
16113 -- primitives inherited from the parent type. If the generic parent
16114 -- type is present, the derived type is an instance of a formal
16115 -- derived type, and within the instance its operations are those of
16116 -- the actual. We derive from the formal type but make the inherited
16117 -- operations aliases of the corresponding operations of the actual.
16118
16119 Elmt := First_Elmt (Op_List);
16120 while Present (Elmt) loop
16121 Subp := Node (Elmt);
16122 Alias_Subp := Ultimate_Alias (Subp);
16123
16124 -- Do not derive internal entities of the parent that link
16125 -- interface primitives with their covering primitive. These
16126 -- entities will be added to this type when frozen.
16127
16128 if Present (Interface_Alias (Subp)) then
16129 goto Continue;
16130 end if;
16131
16132 -- If the generic actual is present find the corresponding
16133 -- operation in the generic actual. If the parent type is a
16134 -- direct ancestor of the derived type then, even if it is an
16135 -- interface, the operations are inherited from the primary
16136 -- dispatch table and are in the proper order. If we detect here
16137 -- that primitives are not in the same order we traverse the list
16138 -- of primitive operations of the actual to find the one that
16139 -- implements the interface primitive.
16140
16141 if Need_Search
16142 or else
16143 (Present (Generic_Actual)
16144 and then Present (Act_Subp)
16145 and then not
16146 (Primitive_Names_Match (Subp, Act_Subp)
16147 and then
16148 Type_Conformant (Subp, Act_Subp,
16149 Skip_Controlling_Formals => True)))
16150 then
16151 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16152 Use_Full_View => True));
16153
16154 -- Remember that we need searching for all pending primitives
16155
16156 Need_Search := True;
16157
16158 -- Handle entities associated with interface primitives
16159
16160 if Present (Alias_Subp)
16161 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16162 and then not Is_Predefined_Dispatching_Operation (Subp)
16163 then
16164 -- Search for the primitive in the homonym chain
16165
16166 Act_Subp :=
16167 Find_Primitive_Covering_Interface
16168 (Tagged_Type => Generic_Actual,
16169 Iface_Prim => Alias_Subp);
16170
16171 -- Previous search may not locate primitives covering
16172 -- interfaces defined in generics units or instantiations.
16173 -- (it fails if the covering primitive has formals whose
16174 -- type is also defined in generics or instantiations).
16175 -- In such case we search in the list of primitives of the
16176 -- generic actual for the internal entity that links the
16177 -- interface primitive and the covering primitive.
16178
16179 if No (Act_Subp)
16180 and then Is_Generic_Type (Parent_Type)
16181 then
16182 -- This code has been designed to handle only generic
16183 -- formals that implement interfaces that are defined
16184 -- in a generic unit or instantiation. If this code is
16185 -- needed for other cases we must review it because
16186 -- (given that it relies on Original_Location to locate
16187 -- the primitive of Generic_Actual that covers the
16188 -- interface) it could leave linked through attribute
16189 -- Alias entities of unrelated instantiations).
16190
16191 pragma Assert
16192 (Is_Generic_Unit
16193 (Scope (Find_Dispatching_Type (Alias_Subp)))
16194 or else
16195 Instantiation_Depth
16196 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16197
16198 declare
16199 Iface_Prim_Loc : constant Source_Ptr :=
16200 Original_Location (Sloc (Alias_Subp));
16201
16202 Elmt : Elmt_Id;
16203 Prim : Entity_Id;
16204
16205 begin
16206 Elmt :=
16207 First_Elmt (Primitive_Operations (Generic_Actual));
16208
16209 Search : while Present (Elmt) loop
16210 Prim := Node (Elmt);
16211
16212 if Present (Interface_Alias (Prim))
16213 and then Original_Location
16214 (Sloc (Interface_Alias (Prim))) =
16215 Iface_Prim_Loc
16216 then
16217 Act_Subp := Alias (Prim);
16218 exit Search;
16219 end if;
16220
16221 Next_Elmt (Elmt);
16222 end loop Search;
16223 end;
16224 end if;
16225
16226 pragma Assert (Present (Act_Subp)
16227 or else Is_Abstract_Type (Generic_Actual)
16228 or else Serious_Errors_Detected > 0);
16229
16230 -- Handle predefined primitives plus the rest of user-defined
16231 -- primitives
16232
16233 else
16234 Act_Elmt := First_Elmt (Act_List);
16235 while Present (Act_Elmt) loop
16236 Act_Subp := Node (Act_Elmt);
16237
16238 exit when Primitive_Names_Match (Subp, Act_Subp)
16239 and then Type_Conformant
16240 (Subp, Act_Subp,
16241 Skip_Controlling_Formals => True)
16242 and then No (Interface_Alias (Act_Subp));
16243
16244 Next_Elmt (Act_Elmt);
16245 end loop;
16246
16247 if No (Act_Elmt) then
16248 Act_Subp := Empty;
16249 end if;
16250 end if;
16251 end if;
16252
16253 -- Case 1: If the parent is a limited interface then it has the
16254 -- predefined primitives of synchronized interfaces. However, the
16255 -- actual type may be a non-limited type and hence it does not
16256 -- have such primitives.
16257
16258 if Present (Generic_Actual)
16259 and then not Present (Act_Subp)
16260 and then Is_Limited_Interface (Parent_Base)
16261 and then Is_Predefined_Interface_Primitive (Subp)
16262 then
16263 null;
16264
16265 -- Case 2: Inherit entities associated with interfaces that were
16266 -- not covered by the parent type. We exclude here null interface
16267 -- primitives because they do not need special management.
16268
16269 -- We also exclude interface operations that are renamings. If the
16270 -- subprogram is an explicit renaming of an interface primitive,
16271 -- it is a regular primitive operation, and the presence of its
16272 -- alias is not relevant: it has to be derived like any other
16273 -- primitive.
16274
16275 elsif Present (Alias (Subp))
16276 and then Nkind (Unit_Declaration_Node (Subp)) /=
16277 N_Subprogram_Renaming_Declaration
16278 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16279 and then not
16280 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16281 and then Null_Present (Parent (Alias_Subp)))
16282 then
16283 -- If this is an abstract private type then we transfer the
16284 -- derivation of the interface primitive from the partial view
16285 -- to the full view. This is safe because all the interfaces
16286 -- must be visible in the partial view. Done to avoid adding
16287 -- a new interface derivation to the private part of the
16288 -- enclosing package; otherwise this new derivation would be
16289 -- decorated as hidden when the analysis of the enclosing
16290 -- package completes.
16291
16292 if Is_Abstract_Type (Derived_Type)
16293 and then In_Private_Part (Current_Scope)
16294 and then Has_Private_Declaration (Derived_Type)
16295 then
16296 declare
16297 Partial_View : Entity_Id;
16298 Elmt : Elmt_Id;
16299 Ent : Entity_Id;
16300
16301 begin
16302 Partial_View := First_Entity (Current_Scope);
16303 loop
16304 exit when No (Partial_View)
16305 or else (Has_Private_Declaration (Partial_View)
16306 and then
16307 Full_View (Partial_View) = Derived_Type);
16308
16309 Next_Entity (Partial_View);
16310 end loop;
16311
16312 -- If the partial view was not found then the source code
16313 -- has errors and the derivation is not needed.
16314
16315 if Present (Partial_View) then
16316 Elmt :=
16317 First_Elmt (Primitive_Operations (Partial_View));
16318 while Present (Elmt) loop
16319 Ent := Node (Elmt);
16320
16321 if Present (Alias (Ent))
16322 and then Ultimate_Alias (Ent) = Alias (Subp)
16323 then
16324 Append_Elmt
16325 (Ent, Primitive_Operations (Derived_Type));
16326 exit;
16327 end if;
16328
16329 Next_Elmt (Elmt);
16330 end loop;
16331
16332 -- If the interface primitive was not found in the
16333 -- partial view then this interface primitive was
16334 -- overridden. We add a derivation to activate in
16335 -- Derive_Progenitor_Subprograms the machinery to
16336 -- search for it.
16337
16338 if No (Elmt) then
16339 Derive_Interface_Subprogram
16340 (New_Subp => New_Subp,
16341 Subp => Subp,
16342 Actual_Subp => Act_Subp);
16343 end if;
16344 end if;
16345 end;
16346 else
16347 Derive_Interface_Subprogram
16348 (New_Subp => New_Subp,
16349 Subp => Subp,
16350 Actual_Subp => Act_Subp);
16351 end if;
16352
16353 -- Case 3: Common derivation
16354
16355 else
16356 Derive_Subprogram
16357 (New_Subp => New_Subp,
16358 Parent_Subp => Subp,
16359 Derived_Type => Derived_Type,
16360 Parent_Type => Parent_Base,
16361 Actual_Subp => Act_Subp);
16362 end if;
16363
16364 -- No need to update Act_Elm if we must search for the
16365 -- corresponding operation in the generic actual
16366
16367 if not Need_Search
16368 and then Present (Act_Elmt)
16369 then
16370 Next_Elmt (Act_Elmt);
16371 Act_Subp := Node (Act_Elmt);
16372 end if;
16373
16374 <<Continue>>
16375 Next_Elmt (Elmt);
16376 end loop;
16377
16378 -- Inherit additional operations from progenitors. If the derived
16379 -- type is a generic actual, there are not new primitive operations
16380 -- for the type because it has those of the actual, and therefore
16381 -- nothing needs to be done. The renamings generated above are not
16382 -- primitive operations, and their purpose is simply to make the
16383 -- proper operations visible within an instantiation.
16384
16385 if No (Generic_Actual) then
16386 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16387 end if;
16388 end if;
16389
16390 -- Final check: Direct descendants must have their primitives in the
16391 -- same order. We exclude from this test untagged types and instances
16392 -- of formal derived types. We skip this test if we have already
16393 -- reported serious errors in the sources.
16394
16395 pragma Assert (not Is_Tagged_Type (Derived_Type)
16396 or else Present (Generic_Actual)
16397 or else Serious_Errors_Detected > 0
16398 or else Check_Derived_Type);
16399 end Derive_Subprograms;
16400
16401 --------------------------------
16402 -- Derived_Standard_Character --
16403 --------------------------------
16404
16405 procedure Derived_Standard_Character
16406 (N : Node_Id;
16407 Parent_Type : Entity_Id;
16408 Derived_Type : Entity_Id)
16409 is
16410 Loc : constant Source_Ptr := Sloc (N);
16411 Def : constant Node_Id := Type_Definition (N);
16412 Indic : constant Node_Id := Subtype_Indication (Def);
16413 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16414 Implicit_Base : constant Entity_Id :=
16415 Create_Itype
16416 (E_Enumeration_Type, N, Derived_Type, 'B');
16417
16418 Lo : Node_Id;
16419 Hi : Node_Id;
16420
16421 begin
16422 Discard_Node (Process_Subtype (Indic, N));
16423
16424 Set_Etype (Implicit_Base, Parent_Base);
16425 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16426 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16427
16428 Set_Is_Character_Type (Implicit_Base, True);
16429 Set_Has_Delayed_Freeze (Implicit_Base);
16430
16431 -- The bounds of the implicit base are the bounds of the parent base.
16432 -- Note that their type is the parent base.
16433
16434 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16435 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16436
16437 Set_Scalar_Range (Implicit_Base,
16438 Make_Range (Loc,
16439 Low_Bound => Lo,
16440 High_Bound => Hi));
16441
16442 Conditional_Delay (Derived_Type, Parent_Type);
16443
16444 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16445 Set_Etype (Derived_Type, Implicit_Base);
16446 Set_Size_Info (Derived_Type, Parent_Type);
16447
16448 if Unknown_RM_Size (Derived_Type) then
16449 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16450 end if;
16451
16452 Set_Is_Character_Type (Derived_Type, True);
16453
16454 if Nkind (Indic) /= N_Subtype_Indication then
16455
16456 -- If no explicit constraint, the bounds are those
16457 -- of the parent type.
16458
16459 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16460 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16461 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16462 end if;
16463
16464 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16465
16466 -- Because the implicit base is used in the conversion of the bounds, we
16467 -- have to freeze it now. This is similar to what is done for numeric
16468 -- types, and it equally suspicious, but otherwise a nonstatic bound
16469 -- will have a reference to an unfrozen type, which is rejected by Gigi
16470 -- (???). This requires specific care for definition of stream
16471 -- attributes. For details, see comments at the end of
16472 -- Build_Derived_Numeric_Type.
16473
16474 Freeze_Before (N, Implicit_Base);
16475 end Derived_Standard_Character;
16476
16477 ------------------------------
16478 -- Derived_Type_Declaration --
16479 ------------------------------
16480
16481 procedure Derived_Type_Declaration
16482 (T : Entity_Id;
16483 N : Node_Id;
16484 Is_Completion : Boolean)
16485 is
16486 Parent_Type : Entity_Id;
16487
16488 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16489 -- Check whether the parent type is a generic formal, or derives
16490 -- directly or indirectly from one.
16491
16492 ------------------------
16493 -- Comes_From_Generic --
16494 ------------------------
16495
16496 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16497 begin
16498 if Is_Generic_Type (Typ) then
16499 return True;
16500
16501 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16502 return True;
16503
16504 elsif Is_Private_Type (Typ)
16505 and then Present (Full_View (Typ))
16506 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16507 then
16508 return True;
16509
16510 elsif Is_Generic_Actual_Type (Typ) then
16511 return True;
16512
16513 else
16514 return False;
16515 end if;
16516 end Comes_From_Generic;
16517
16518 -- Local variables
16519
16520 Def : constant Node_Id := Type_Definition (N);
16521 Iface_Def : Node_Id;
16522 Indic : constant Node_Id := Subtype_Indication (Def);
16523 Extension : constant Node_Id := Record_Extension_Part (Def);
16524 Parent_Node : Node_Id;
16525 Taggd : Boolean;
16526
16527 -- Start of processing for Derived_Type_Declaration
16528
16529 begin
16530 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16531
16532 if SPARK_Mode = On
16533 and then Is_Tagged_Type (Parent_Type)
16534 then
16535 declare
16536 Partial_View : constant Entity_Id :=
16537 Incomplete_Or_Partial_View (Parent_Type);
16538
16539 begin
16540 -- If the partial view was not found then the parent type is not
16541 -- a private type. Otherwise check if the partial view is a tagged
16542 -- private type.
16543
16544 if Present (Partial_View)
16545 and then Is_Private_Type (Partial_View)
16546 and then not Is_Tagged_Type (Partial_View)
16547 then
16548 Error_Msg_NE
16549 ("cannot derive from & declared as untagged private "
16550 & "(SPARK RM 3.4(1))", N, Partial_View);
16551 end if;
16552 end;
16553 end if;
16554
16555 -- Ada 2005 (AI-251): In case of interface derivation check that the
16556 -- parent is also an interface.
16557
16558 if Interface_Present (Def) then
16559 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16560
16561 if not Is_Interface (Parent_Type) then
16562 Diagnose_Interface (Indic, Parent_Type);
16563
16564 else
16565 Parent_Node := Parent (Base_Type (Parent_Type));
16566 Iface_Def := Type_Definition (Parent_Node);
16567
16568 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16569 -- other limited interfaces.
16570
16571 if Limited_Present (Def) then
16572 if Limited_Present (Iface_Def) then
16573 null;
16574
16575 elsif Protected_Present (Iface_Def) then
16576 Error_Msg_NE
16577 ("descendant of & must be declared as a protected "
16578 & "interface", N, Parent_Type);
16579
16580 elsif Synchronized_Present (Iface_Def) then
16581 Error_Msg_NE
16582 ("descendant of & must be declared as a synchronized "
16583 & "interface", N, Parent_Type);
16584
16585 elsif Task_Present (Iface_Def) then
16586 Error_Msg_NE
16587 ("descendant of & must be declared as a task interface",
16588 N, Parent_Type);
16589
16590 else
16591 Error_Msg_N
16592 ("(Ada 2005) limited interface cannot inherit from "
16593 & "non-limited interface", Indic);
16594 end if;
16595
16596 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16597 -- from non-limited or limited interfaces.
16598
16599 elsif not Protected_Present (Def)
16600 and then not Synchronized_Present (Def)
16601 and then not Task_Present (Def)
16602 then
16603 if Limited_Present (Iface_Def) then
16604 null;
16605
16606 elsif Protected_Present (Iface_Def) then
16607 Error_Msg_NE
16608 ("descendant of & must be declared as a protected "
16609 & "interface", N, Parent_Type);
16610
16611 elsif Synchronized_Present (Iface_Def) then
16612 Error_Msg_NE
16613 ("descendant of & must be declared as a synchronized "
16614 & "interface", N, Parent_Type);
16615
16616 elsif Task_Present (Iface_Def) then
16617 Error_Msg_NE
16618 ("descendant of & must be declared as a task interface",
16619 N, Parent_Type);
16620 else
16621 null;
16622 end if;
16623 end if;
16624 end if;
16625 end if;
16626
16627 if Is_Tagged_Type (Parent_Type)
16628 and then Is_Concurrent_Type (Parent_Type)
16629 and then not Is_Interface (Parent_Type)
16630 then
16631 Error_Msg_N
16632 ("parent type of a record extension cannot be a synchronized "
16633 & "tagged type (RM 3.9.1 (3/1))", N);
16634 Set_Etype (T, Any_Type);
16635 return;
16636 end if;
16637
16638 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16639 -- interfaces
16640
16641 if Is_Tagged_Type (Parent_Type)
16642 and then Is_Non_Empty_List (Interface_List (Def))
16643 then
16644 declare
16645 Intf : Node_Id;
16646 T : Entity_Id;
16647
16648 begin
16649 Intf := First (Interface_List (Def));
16650 while Present (Intf) loop
16651 T := Find_Type_Of_Subtype_Indic (Intf);
16652
16653 if not Is_Interface (T) then
16654 Diagnose_Interface (Intf, T);
16655
16656 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16657 -- a limited type from having a nonlimited progenitor.
16658
16659 elsif (Limited_Present (Def)
16660 or else (not Is_Interface (Parent_Type)
16661 and then Is_Limited_Type (Parent_Type)))
16662 and then not Is_Limited_Interface (T)
16663 then
16664 Error_Msg_NE
16665 ("progenitor interface& of limited type must be limited",
16666 N, T);
16667 end if;
16668
16669 Next (Intf);
16670 end loop;
16671 end;
16672 end if;
16673
16674 if Parent_Type = Any_Type
16675 or else Etype (Parent_Type) = Any_Type
16676 or else (Is_Class_Wide_Type (Parent_Type)
16677 and then Etype (Parent_Type) = T)
16678 then
16679 -- If Parent_Type is undefined or illegal, make new type into a
16680 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16681 -- errors. If this is a self-definition, emit error now.
16682
16683 if T = Parent_Type or else T = Etype (Parent_Type) then
16684 Error_Msg_N ("type cannot be used in its own definition", Indic);
16685 end if;
16686
16687 Set_Ekind (T, Ekind (Parent_Type));
16688 Set_Etype (T, Any_Type);
16689 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16690
16691 if Is_Tagged_Type (T)
16692 and then Is_Record_Type (T)
16693 then
16694 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16695 end if;
16696
16697 return;
16698 end if;
16699
16700 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16701 -- an interface is special because the list of interfaces in the full
16702 -- view can be given in any order. For example:
16703
16704 -- type A is interface;
16705 -- type B is interface and A;
16706 -- type D is new B with private;
16707 -- private
16708 -- type D is new A and B with null record; -- 1 --
16709
16710 -- In this case we perform the following transformation of -1-:
16711
16712 -- type D is new B and A with null record;
16713
16714 -- If the parent of the full-view covers the parent of the partial-view
16715 -- we have two possible cases:
16716
16717 -- 1) They have the same parent
16718 -- 2) The parent of the full-view implements some further interfaces
16719
16720 -- In both cases we do not need to perform the transformation. In the
16721 -- first case the source program is correct and the transformation is
16722 -- not needed; in the second case the source program does not fulfill
16723 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16724 -- later.
16725
16726 -- This transformation not only simplifies the rest of the analysis of
16727 -- this type declaration but also simplifies the correct generation of
16728 -- the object layout to the expander.
16729
16730 if In_Private_Part (Current_Scope)
16731 and then Is_Interface (Parent_Type)
16732 then
16733 declare
16734 Iface : Node_Id;
16735 Partial_View : Entity_Id;
16736 Partial_View_Parent : Entity_Id;
16737 New_Iface : Node_Id;
16738
16739 begin
16740 -- Look for the associated private type declaration
16741
16742 Partial_View := Incomplete_Or_Partial_View (T);
16743
16744 -- If the partial view was not found then the source code has
16745 -- errors and the transformation is not needed.
16746
16747 if Present (Partial_View) then
16748 Partial_View_Parent := Etype (Partial_View);
16749
16750 -- If the parent of the full-view covers the parent of the
16751 -- partial-view we have nothing else to do.
16752
16753 if Interface_Present_In_Ancestor
16754 (Parent_Type, Partial_View_Parent)
16755 then
16756 null;
16757
16758 -- Traverse the list of interfaces of the full-view to look
16759 -- for the parent of the partial-view and perform the tree
16760 -- transformation.
16761
16762 else
16763 Iface := First (Interface_List (Def));
16764 while Present (Iface) loop
16765 if Etype (Iface) = Etype (Partial_View) then
16766 Rewrite (Subtype_Indication (Def),
16767 New_Copy (Subtype_Indication
16768 (Parent (Partial_View))));
16769
16770 New_Iface :=
16771 Make_Identifier (Sloc (N), Chars (Parent_Type));
16772 Append (New_Iface, Interface_List (Def));
16773
16774 -- Analyze the transformed code
16775
16776 Derived_Type_Declaration (T, N, Is_Completion);
16777 return;
16778 end if;
16779
16780 Next (Iface);
16781 end loop;
16782 end if;
16783 end if;
16784 end;
16785 end if;
16786
16787 -- Only composite types other than array types are allowed to have
16788 -- discriminants.
16789
16790 if Present (Discriminant_Specifications (N)) then
16791 if (Is_Elementary_Type (Parent_Type)
16792 or else
16793 Is_Array_Type (Parent_Type))
16794 and then not Error_Posted (N)
16795 then
16796 Error_Msg_N
16797 ("elementary or array type cannot have discriminants",
16798 Defining_Identifier (First (Discriminant_Specifications (N))));
16799
16800 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16801 -- only if we are not already processing a malformed syntax tree.
16802
16803 if Is_Type (T) then
16804 Set_Has_Discriminants (T, False);
16805 end if;
16806
16807 -- The type is allowed to have discriminants
16808
16809 else
16810 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16811 end if;
16812 end if;
16813
16814 -- In Ada 83, a derived type defined in a package specification cannot
16815 -- be used for further derivation until the end of its visible part.
16816 -- Note that derivation in the private part of the package is allowed.
16817
16818 if Ada_Version = Ada_83
16819 and then Is_Derived_Type (Parent_Type)
16820 and then In_Visible_Part (Scope (Parent_Type))
16821 then
16822 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16823 Error_Msg_N
16824 ("(Ada 83): premature use of type for derivation", Indic);
16825 end if;
16826 end if;
16827
16828 -- Check for early use of incomplete or private type
16829
16830 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16831 Error_Msg_N ("premature derivation of incomplete type", Indic);
16832 return;
16833
16834 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16835 and then not Comes_From_Generic (Parent_Type))
16836 or else Has_Private_Component (Parent_Type)
16837 then
16838 -- The ancestor type of a formal type can be incomplete, in which
16839 -- case only the operations of the partial view are available in the
16840 -- generic. Subsequent checks may be required when the full view is
16841 -- analyzed to verify that a derivation from a tagged type has an
16842 -- extension.
16843
16844 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16845 null;
16846
16847 elsif No (Underlying_Type (Parent_Type))
16848 or else Has_Private_Component (Parent_Type)
16849 then
16850 Error_Msg_N
16851 ("premature derivation of derived or private type", Indic);
16852
16853 -- Flag the type itself as being in error, this prevents some
16854 -- nasty problems with subsequent uses of the malformed type.
16855
16856 Set_Error_Posted (T);
16857
16858 -- Check that within the immediate scope of an untagged partial
16859 -- view it's illegal to derive from the partial view if the
16860 -- full view is tagged. (7.3(7))
16861
16862 -- We verify that the Parent_Type is a partial view by checking
16863 -- that it is not a Full_Type_Declaration (i.e. a private type or
16864 -- private extension declaration), to distinguish a partial view
16865 -- from a derivation from a private type which also appears as
16866 -- E_Private_Type. If the parent base type is not declared in an
16867 -- enclosing scope there is no need to check.
16868
16869 elsif Present (Full_View (Parent_Type))
16870 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16871 and then not Is_Tagged_Type (Parent_Type)
16872 and then Is_Tagged_Type (Full_View (Parent_Type))
16873 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16874 then
16875 Error_Msg_N
16876 ("premature derivation from type with tagged full view",
16877 Indic);
16878 end if;
16879 end if;
16880
16881 -- Check that form of derivation is appropriate
16882
16883 Taggd := Is_Tagged_Type (Parent_Type);
16884
16885 -- Set the parent type to the class-wide type's specific type in this
16886 -- case to prevent cascading errors
16887
16888 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16889 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16890 Set_Etype (T, Etype (Parent_Type));
16891 return;
16892 end if;
16893
16894 if Present (Extension) and then not Taggd then
16895 Error_Msg_N
16896 ("type derived from untagged type cannot have extension", Indic);
16897
16898 elsif No (Extension) and then Taggd then
16899
16900 -- If this declaration is within a private part (or body) of a
16901 -- generic instantiation then the derivation is allowed (the parent
16902 -- type can only appear tagged in this case if it's a generic actual
16903 -- type, since it would otherwise have been rejected in the analysis
16904 -- of the generic template).
16905
16906 if not Is_Generic_Actual_Type (Parent_Type)
16907 or else In_Visible_Part (Scope (Parent_Type))
16908 then
16909 if Is_Class_Wide_Type (Parent_Type) then
16910 Error_Msg_N
16911 ("parent type must not be a class-wide type", Indic);
16912
16913 -- Use specific type to prevent cascaded errors.
16914
16915 Parent_Type := Etype (Parent_Type);
16916
16917 else
16918 Error_Msg_N
16919 ("type derived from tagged type must have extension", Indic);
16920 end if;
16921 end if;
16922 end if;
16923
16924 -- AI-443: Synchronized formal derived types require a private
16925 -- extension. There is no point in checking the ancestor type or
16926 -- the progenitors since the construct is wrong to begin with.
16927
16928 if Ada_Version >= Ada_2005
16929 and then Is_Generic_Type (T)
16930 and then Present (Original_Node (N))
16931 then
16932 declare
16933 Decl : constant Node_Id := Original_Node (N);
16934
16935 begin
16936 if Nkind (Decl) = N_Formal_Type_Declaration
16937 and then Nkind (Formal_Type_Definition (Decl)) =
16938 N_Formal_Derived_Type_Definition
16939 and then Synchronized_Present (Formal_Type_Definition (Decl))
16940 and then No (Extension)
16941
16942 -- Avoid emitting a duplicate error message
16943
16944 and then not Error_Posted (Indic)
16945 then
16946 Error_Msg_N
16947 ("synchronized derived type must have extension", N);
16948 end if;
16949 end;
16950 end if;
16951
16952 if Null_Exclusion_Present (Def)
16953 and then not Is_Access_Type (Parent_Type)
16954 then
16955 Error_Msg_N ("null exclusion can only apply to an access type", N);
16956 end if;
16957
16958 -- Avoid deriving parent primitives of underlying record views
16959
16960 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16961 Derive_Subps => not Is_Underlying_Record_View (T));
16962
16963 -- AI-419: The parent type of an explicitly limited derived type must
16964 -- be a limited type or a limited interface.
16965
16966 if Limited_Present (Def) then
16967 Set_Is_Limited_Record (T);
16968
16969 if Is_Interface (T) then
16970 Set_Is_Limited_Interface (T);
16971 end if;
16972
16973 if not Is_Limited_Type (Parent_Type)
16974 and then
16975 (not Is_Interface (Parent_Type)
16976 or else not Is_Limited_Interface (Parent_Type))
16977 then
16978 -- AI05-0096: a derivation in the private part of an instance is
16979 -- legal if the generic formal is untagged limited, and the actual
16980 -- is non-limited.
16981
16982 if Is_Generic_Actual_Type (Parent_Type)
16983 and then In_Private_Part (Current_Scope)
16984 and then
16985 not Is_Tagged_Type
16986 (Generic_Parent_Type (Parent (Parent_Type)))
16987 then
16988 null;
16989
16990 else
16991 Error_Msg_NE
16992 ("parent type& of limited type must be limited",
16993 N, Parent_Type);
16994 end if;
16995 end if;
16996 end if;
16997
16998 -- In SPARK, there are no derived type definitions other than type
16999 -- extensions of tagged record types.
17000
17001 if No (Extension) then
17002 Check_SPARK_05_Restriction
17003 ("derived type is not allowed", Original_Node (N));
17004 end if;
17005 end Derived_Type_Declaration;
17006
17007 ------------------------
17008 -- Diagnose_Interface --
17009 ------------------------
17010
17011 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17012 begin
17013 if not Is_Interface (E) and then E /= Any_Type then
17014 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17015 end if;
17016 end Diagnose_Interface;
17017
17018 ----------------------------------
17019 -- Enumeration_Type_Declaration --
17020 ----------------------------------
17021
17022 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17023 Ev : Uint;
17024 L : Node_Id;
17025 R_Node : Node_Id;
17026 B_Node : Node_Id;
17027
17028 begin
17029 -- Create identifier node representing lower bound
17030
17031 B_Node := New_Node (N_Identifier, Sloc (Def));
17032 L := First (Literals (Def));
17033 Set_Chars (B_Node, Chars (L));
17034 Set_Entity (B_Node, L);
17035 Set_Etype (B_Node, T);
17036 Set_Is_Static_Expression (B_Node, True);
17037
17038 R_Node := New_Node (N_Range, Sloc (Def));
17039 Set_Low_Bound (R_Node, B_Node);
17040
17041 Set_Ekind (T, E_Enumeration_Type);
17042 Set_First_Literal (T, L);
17043 Set_Etype (T, T);
17044 Set_Is_Constrained (T);
17045
17046 Ev := Uint_0;
17047
17048 -- Loop through literals of enumeration type setting pos and rep values
17049 -- except that if the Ekind is already set, then it means the literal
17050 -- was already constructed (case of a derived type declaration and we
17051 -- should not disturb the Pos and Rep values.
17052
17053 while Present (L) loop
17054 if Ekind (L) /= E_Enumeration_Literal then
17055 Set_Ekind (L, E_Enumeration_Literal);
17056 Set_Enumeration_Pos (L, Ev);
17057 Set_Enumeration_Rep (L, Ev);
17058 Set_Is_Known_Valid (L, True);
17059 end if;
17060
17061 Set_Etype (L, T);
17062 New_Overloaded_Entity (L);
17063 Generate_Definition (L);
17064 Set_Convention (L, Convention_Intrinsic);
17065
17066 -- Case of character literal
17067
17068 if Nkind (L) = N_Defining_Character_Literal then
17069 Set_Is_Character_Type (T, True);
17070
17071 -- Check violation of No_Wide_Characters
17072
17073 if Restriction_Check_Required (No_Wide_Characters) then
17074 Get_Name_String (Chars (L));
17075
17076 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17077 Check_Restriction (No_Wide_Characters, L);
17078 end if;
17079 end if;
17080 end if;
17081
17082 Ev := Ev + 1;
17083 Next (L);
17084 end loop;
17085
17086 -- Now create a node representing upper bound
17087
17088 B_Node := New_Node (N_Identifier, Sloc (Def));
17089 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17090 Set_Entity (B_Node, Last (Literals (Def)));
17091 Set_Etype (B_Node, T);
17092 Set_Is_Static_Expression (B_Node, True);
17093
17094 Set_High_Bound (R_Node, B_Node);
17095
17096 -- Initialize various fields of the type. Some of this information
17097 -- may be overwritten later through rep.clauses.
17098
17099 Set_Scalar_Range (T, R_Node);
17100 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17101 Set_Enum_Esize (T);
17102 Set_Enum_Pos_To_Rep (T, Empty);
17103
17104 -- Set Discard_Names if configuration pragma set, or if there is
17105 -- a parameterless pragma in the current declarative region
17106
17107 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17108 Set_Discard_Names (T);
17109 end if;
17110
17111 -- Process end label if there is one
17112
17113 if Present (Def) then
17114 Process_End_Label (Def, 'e', T);
17115 end if;
17116 end Enumeration_Type_Declaration;
17117
17118 ---------------------------------
17119 -- Expand_To_Stored_Constraint --
17120 ---------------------------------
17121
17122 function Expand_To_Stored_Constraint
17123 (Typ : Entity_Id;
17124 Constraint : Elist_Id) return Elist_Id
17125 is
17126 Explicitly_Discriminated_Type : Entity_Id;
17127 Expansion : Elist_Id;
17128 Discriminant : Entity_Id;
17129
17130 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17131 -- Find the nearest type that actually specifies discriminants
17132
17133 ---------------------------------
17134 -- Type_With_Explicit_Discrims --
17135 ---------------------------------
17136
17137 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17138 Typ : constant E := Base_Type (Id);
17139
17140 begin
17141 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17142 if Present (Full_View (Typ)) then
17143 return Type_With_Explicit_Discrims (Full_View (Typ));
17144 end if;
17145
17146 else
17147 if Has_Discriminants (Typ) then
17148 return Typ;
17149 end if;
17150 end if;
17151
17152 if Etype (Typ) = Typ then
17153 return Empty;
17154 elsif Has_Discriminants (Typ) then
17155 return Typ;
17156 else
17157 return Type_With_Explicit_Discrims (Etype (Typ));
17158 end if;
17159
17160 end Type_With_Explicit_Discrims;
17161
17162 -- Start of processing for Expand_To_Stored_Constraint
17163
17164 begin
17165 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17166 return No_Elist;
17167 end if;
17168
17169 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17170
17171 if No (Explicitly_Discriminated_Type) then
17172 return No_Elist;
17173 end if;
17174
17175 Expansion := New_Elmt_List;
17176
17177 Discriminant :=
17178 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17179 while Present (Discriminant) loop
17180 Append_Elmt
17181 (Get_Discriminant_Value
17182 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17183 To => Expansion);
17184 Next_Stored_Discriminant (Discriminant);
17185 end loop;
17186
17187 return Expansion;
17188 end Expand_To_Stored_Constraint;
17189
17190 ---------------------------
17191 -- Find_Hidden_Interface --
17192 ---------------------------
17193
17194 function Find_Hidden_Interface
17195 (Src : Elist_Id;
17196 Dest : Elist_Id) return Entity_Id
17197 is
17198 Iface : Entity_Id;
17199 Iface_Elmt : Elmt_Id;
17200
17201 begin
17202 if Present (Src) and then Present (Dest) then
17203 Iface_Elmt := First_Elmt (Src);
17204 while Present (Iface_Elmt) loop
17205 Iface := Node (Iface_Elmt);
17206
17207 if Is_Interface (Iface)
17208 and then not Contain_Interface (Iface, Dest)
17209 then
17210 return Iface;
17211 end if;
17212
17213 Next_Elmt (Iface_Elmt);
17214 end loop;
17215 end if;
17216
17217 return Empty;
17218 end Find_Hidden_Interface;
17219
17220 --------------------
17221 -- Find_Type_Name --
17222 --------------------
17223
17224 function Find_Type_Name (N : Node_Id) return Entity_Id is
17225 Id : constant Entity_Id := Defining_Identifier (N);
17226 New_Id : Entity_Id;
17227 Prev : Entity_Id;
17228 Prev_Par : Node_Id;
17229
17230 procedure Check_Duplicate_Aspects;
17231 -- Check that aspects specified in a completion have not been specified
17232 -- already in the partial view.
17233
17234 procedure Tag_Mismatch;
17235 -- Diagnose a tagged partial view whose full view is untagged. We post
17236 -- the message on the full view, with a reference to the previous
17237 -- partial view. The partial view can be private or incomplete, and
17238 -- these are handled in a different manner, so we determine the position
17239 -- of the error message from the respective slocs of both.
17240
17241 -----------------------------
17242 -- Check_Duplicate_Aspects --
17243 -----------------------------
17244
17245 procedure Check_Duplicate_Aspects is
17246 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17247 -- Return the corresponding aspect of the partial view which matches
17248 -- the aspect id of Asp. Return Empty is no such aspect exists.
17249
17250 -----------------------------
17251 -- Get_Partial_View_Aspect --
17252 -----------------------------
17253
17254 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17255 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17256 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17257 Prev_Asp : Node_Id;
17258
17259 begin
17260 if Present (Prev_Asps) then
17261 Prev_Asp := First (Prev_Asps);
17262 while Present (Prev_Asp) loop
17263 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17264 return Prev_Asp;
17265 end if;
17266
17267 Next (Prev_Asp);
17268 end loop;
17269 end if;
17270
17271 return Empty;
17272 end Get_Partial_View_Aspect;
17273
17274 -- Local variables
17275
17276 Full_Asps : constant List_Id := Aspect_Specifications (N);
17277 Full_Asp : Node_Id;
17278 Part_Asp : Node_Id;
17279
17280 -- Start of processing for Check_Duplicate_Aspects
17281
17282 begin
17283 if Present (Full_Asps) then
17284 Full_Asp := First (Full_Asps);
17285 while Present (Full_Asp) loop
17286 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17287
17288 -- An aspect and its class-wide counterpart are two distinct
17289 -- aspects and may apply to both views of an entity.
17290
17291 if Present (Part_Asp)
17292 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17293 then
17294 Error_Msg_N
17295 ("aspect already specified in private declaration",
17296 Full_Asp);
17297
17298 Remove (Full_Asp);
17299 return;
17300 end if;
17301
17302 if Has_Discriminants (Prev)
17303 and then not Has_Unknown_Discriminants (Prev)
17304 and then Get_Aspect_Id (Full_Asp) =
17305 Aspect_Implicit_Dereference
17306 then
17307 Error_Msg_N
17308 ("cannot specify aspect if partial view has known "
17309 & "discriminants", Full_Asp);
17310 end if;
17311
17312 Next (Full_Asp);
17313 end loop;
17314 end if;
17315 end Check_Duplicate_Aspects;
17316
17317 ------------------
17318 -- Tag_Mismatch --
17319 ------------------
17320
17321 procedure Tag_Mismatch is
17322 begin
17323 if Sloc (Prev) < Sloc (Id) then
17324 if Ada_Version >= Ada_2012
17325 and then Nkind (N) = N_Private_Type_Declaration
17326 then
17327 Error_Msg_NE
17328 ("declaration of private } must be a tagged type ", Id, Prev);
17329 else
17330 Error_Msg_NE
17331 ("full declaration of } must be a tagged type ", Id, Prev);
17332 end if;
17333
17334 else
17335 if Ada_Version >= Ada_2012
17336 and then Nkind (N) = N_Private_Type_Declaration
17337 then
17338 Error_Msg_NE
17339 ("declaration of private } must be a tagged type ", Prev, Id);
17340 else
17341 Error_Msg_NE
17342 ("full declaration of } must be a tagged type ", Prev, Id);
17343 end if;
17344 end if;
17345 end Tag_Mismatch;
17346
17347 -- Start of processing for Find_Type_Name
17348
17349 begin
17350 -- Find incomplete declaration, if one was given
17351
17352 Prev := Current_Entity_In_Scope (Id);
17353
17354 -- New type declaration
17355
17356 if No (Prev) then
17357 Enter_Name (Id);
17358 return Id;
17359
17360 -- Previous declaration exists
17361
17362 else
17363 Prev_Par := Parent (Prev);
17364
17365 -- Error if not incomplete/private case except if previous
17366 -- declaration is implicit, etc. Enter_Name will emit error if
17367 -- appropriate.
17368
17369 if not Is_Incomplete_Or_Private_Type (Prev) then
17370 Enter_Name (Id);
17371 New_Id := Id;
17372
17373 -- Check invalid completion of private or incomplete type
17374
17375 elsif not Nkind_In (N, N_Full_Type_Declaration,
17376 N_Task_Type_Declaration,
17377 N_Protected_Type_Declaration)
17378 and then
17379 (Ada_Version < Ada_2012
17380 or else not Is_Incomplete_Type (Prev)
17381 or else not Nkind_In (N, N_Private_Type_Declaration,
17382 N_Private_Extension_Declaration))
17383 then
17384 -- Completion must be a full type declarations (RM 7.3(4))
17385
17386 Error_Msg_Sloc := Sloc (Prev);
17387 Error_Msg_NE ("invalid completion of }", Id, Prev);
17388
17389 -- Set scope of Id to avoid cascaded errors. Entity is never
17390 -- examined again, except when saving globals in generics.
17391
17392 Set_Scope (Id, Current_Scope);
17393 New_Id := Id;
17394
17395 -- If this is a repeated incomplete declaration, no further
17396 -- checks are possible.
17397
17398 if Nkind (N) = N_Incomplete_Type_Declaration then
17399 return Prev;
17400 end if;
17401
17402 -- Case of full declaration of incomplete type
17403
17404 elsif Ekind (Prev) = E_Incomplete_Type
17405 and then (Ada_Version < Ada_2012
17406 or else No (Full_View (Prev))
17407 or else not Is_Private_Type (Full_View (Prev)))
17408 then
17409 -- Indicate that the incomplete declaration has a matching full
17410 -- declaration. The defining occurrence of the incomplete
17411 -- declaration remains the visible one, and the procedure
17412 -- Get_Full_View dereferences it whenever the type is used.
17413
17414 if Present (Full_View (Prev)) then
17415 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17416 end if;
17417
17418 Set_Full_View (Prev, Id);
17419 Append_Entity (Id, Current_Scope);
17420 Set_Is_Public (Id, Is_Public (Prev));
17421 Set_Is_Internal (Id);
17422 New_Id := Prev;
17423
17424 -- If the incomplete view is tagged, a class_wide type has been
17425 -- created already. Use it for the private type as well, in order
17426 -- to prevent multiple incompatible class-wide types that may be
17427 -- created for self-referential anonymous access components.
17428
17429 if Is_Tagged_Type (Prev)
17430 and then Present (Class_Wide_Type (Prev))
17431 then
17432 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17433 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17434
17435 -- Type of the class-wide type is the current Id. Previously
17436 -- this was not done for private declarations because of order-
17437 -- of-elaboration issues in the back end, but gigi now handles
17438 -- this properly.
17439
17440 Set_Etype (Class_Wide_Type (Id), Id);
17441 end if;
17442
17443 -- Case of full declaration of private type
17444
17445 else
17446 -- If the private type was a completion of an incomplete type then
17447 -- update Prev to reference the private type
17448
17449 if Ada_Version >= Ada_2012
17450 and then Ekind (Prev) = E_Incomplete_Type
17451 and then Present (Full_View (Prev))
17452 and then Is_Private_Type (Full_View (Prev))
17453 then
17454 Prev := Full_View (Prev);
17455 Prev_Par := Parent (Prev);
17456 end if;
17457
17458 if Nkind (N) = N_Full_Type_Declaration
17459 and then Nkind_In
17460 (Type_Definition (N), N_Record_Definition,
17461 N_Derived_Type_Definition)
17462 and then Interface_Present (Type_Definition (N))
17463 then
17464 Error_Msg_N
17465 ("completion of private type cannot be an interface", N);
17466 end if;
17467
17468 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17469 if Etype (Prev) /= Prev then
17470
17471 -- Prev is a private subtype or a derived type, and needs
17472 -- no completion.
17473
17474 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17475 New_Id := Id;
17476
17477 elsif Ekind (Prev) = E_Private_Type
17478 and then Nkind_In (N, N_Task_Type_Declaration,
17479 N_Protected_Type_Declaration)
17480 then
17481 Error_Msg_N
17482 ("completion of nonlimited type cannot be limited", N);
17483
17484 elsif Ekind (Prev) = E_Record_Type_With_Private
17485 and then Nkind_In (N, N_Task_Type_Declaration,
17486 N_Protected_Type_Declaration)
17487 then
17488 if not Is_Limited_Record (Prev) then
17489 Error_Msg_N
17490 ("completion of nonlimited type cannot be limited", N);
17491
17492 elsif No (Interface_List (N)) then
17493 Error_Msg_N
17494 ("completion of tagged private type must be tagged",
17495 N);
17496 end if;
17497 end if;
17498
17499 -- Ada 2005 (AI-251): Private extension declaration of a task
17500 -- type or a protected type. This case arises when covering
17501 -- interface types.
17502
17503 elsif Nkind_In (N, N_Task_Type_Declaration,
17504 N_Protected_Type_Declaration)
17505 then
17506 null;
17507
17508 elsif Nkind (N) /= N_Full_Type_Declaration
17509 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17510 then
17511 Error_Msg_N
17512 ("full view of private extension must be an extension", N);
17513
17514 elsif not (Abstract_Present (Parent (Prev)))
17515 and then Abstract_Present (Type_Definition (N))
17516 then
17517 Error_Msg_N
17518 ("full view of non-abstract extension cannot be abstract", N);
17519 end if;
17520
17521 if not In_Private_Part (Current_Scope) then
17522 Error_Msg_N
17523 ("declaration of full view must appear in private part", N);
17524 end if;
17525
17526 if Ada_Version >= Ada_2012 then
17527 Check_Duplicate_Aspects;
17528 end if;
17529
17530 Copy_And_Swap (Prev, Id);
17531 Set_Has_Private_Declaration (Prev);
17532 Set_Has_Private_Declaration (Id);
17533
17534 -- AI12-0133: Indicate whether we have a partial view with
17535 -- unknown discriminants, in which case initialization of objects
17536 -- of the type do not receive an invariant check.
17537
17538 Set_Partial_View_Has_Unknown_Discr
17539 (Prev, Has_Unknown_Discriminants (Id));
17540
17541 -- Preserve aspect and iterator flags that may have been set on
17542 -- the partial view.
17543
17544 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17545 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17546
17547 -- If no error, propagate freeze_node from private to full view.
17548 -- It may have been generated for an early operational item.
17549
17550 if Present (Freeze_Node (Id))
17551 and then Serious_Errors_Detected = 0
17552 and then No (Full_View (Id))
17553 then
17554 Set_Freeze_Node (Prev, Freeze_Node (Id));
17555 Set_Freeze_Node (Id, Empty);
17556 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17557 end if;
17558
17559 Set_Full_View (Id, Prev);
17560 New_Id := Prev;
17561 end if;
17562
17563 -- Verify that full declaration conforms to partial one
17564
17565 if Is_Incomplete_Or_Private_Type (Prev)
17566 and then Present (Discriminant_Specifications (Prev_Par))
17567 then
17568 if Present (Discriminant_Specifications (N)) then
17569 if Ekind (Prev) = E_Incomplete_Type then
17570 Check_Discriminant_Conformance (N, Prev, Prev);
17571 else
17572 Check_Discriminant_Conformance (N, Prev, Id);
17573 end if;
17574
17575 else
17576 Error_Msg_N
17577 ("missing discriminants in full type declaration", N);
17578
17579 -- To avoid cascaded errors on subsequent use, share the
17580 -- discriminants of the partial view.
17581
17582 Set_Discriminant_Specifications (N,
17583 Discriminant_Specifications (Prev_Par));
17584 end if;
17585 end if;
17586
17587 -- A prior untagged partial view can have an associated class-wide
17588 -- type due to use of the class attribute, and in this case the full
17589 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17590 -- of incomplete tagged declarations, but we check for it.
17591
17592 if Is_Type (Prev)
17593 and then (Is_Tagged_Type (Prev)
17594 or else Present (Class_Wide_Type (Prev)))
17595 then
17596 -- Ada 2012 (AI05-0162): A private type may be the completion of
17597 -- an incomplete type.
17598
17599 if Ada_Version >= Ada_2012
17600 and then Is_Incomplete_Type (Prev)
17601 and then Nkind_In (N, N_Private_Type_Declaration,
17602 N_Private_Extension_Declaration)
17603 then
17604 -- No need to check private extensions since they are tagged
17605
17606 if Nkind (N) = N_Private_Type_Declaration
17607 and then not Tagged_Present (N)
17608 then
17609 Tag_Mismatch;
17610 end if;
17611
17612 -- The full declaration is either a tagged type (including
17613 -- a synchronized type that implements interfaces) or a
17614 -- type extension, otherwise this is an error.
17615
17616 elsif Nkind_In (N, N_Task_Type_Declaration,
17617 N_Protected_Type_Declaration)
17618 then
17619 if No (Interface_List (N)) and then not Error_Posted (N) then
17620 Tag_Mismatch;
17621 end if;
17622
17623 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17624
17625 -- Indicate that the previous declaration (tagged incomplete
17626 -- or private declaration) requires the same on the full one.
17627
17628 if not Tagged_Present (Type_Definition (N)) then
17629 Tag_Mismatch;
17630 Set_Is_Tagged_Type (Id);
17631 end if;
17632
17633 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17634 if No (Record_Extension_Part (Type_Definition (N))) then
17635 Error_Msg_NE
17636 ("full declaration of } must be a record extension",
17637 Prev, Id);
17638
17639 -- Set some attributes to produce a usable full view
17640
17641 Set_Is_Tagged_Type (Id);
17642 end if;
17643
17644 else
17645 Tag_Mismatch;
17646 end if;
17647 end if;
17648
17649 if Present (Prev)
17650 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17651 and then Present (Premature_Use (Parent (Prev)))
17652 then
17653 Error_Msg_Sloc := Sloc (N);
17654 Error_Msg_N
17655 ("\full declaration #", Premature_Use (Parent (Prev)));
17656 end if;
17657
17658 return New_Id;
17659 end if;
17660 end Find_Type_Name;
17661
17662 -------------------------
17663 -- Find_Type_Of_Object --
17664 -------------------------
17665
17666 function Find_Type_Of_Object
17667 (Obj_Def : Node_Id;
17668 Related_Nod : Node_Id) return Entity_Id
17669 is
17670 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17671 P : Node_Id := Parent (Obj_Def);
17672 T : Entity_Id;
17673 Nam : Name_Id;
17674
17675 begin
17676 -- If the parent is a component_definition node we climb to the
17677 -- component_declaration node
17678
17679 if Nkind (P) = N_Component_Definition then
17680 P := Parent (P);
17681 end if;
17682
17683 -- Case of an anonymous array subtype
17684
17685 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17686 N_Unconstrained_Array_Definition)
17687 then
17688 T := Empty;
17689 Array_Type_Declaration (T, Obj_Def);
17690
17691 -- Create an explicit subtype whenever possible
17692
17693 elsif Nkind (P) /= N_Component_Declaration
17694 and then Def_Kind = N_Subtype_Indication
17695 then
17696 -- Base name of subtype on object name, which will be unique in
17697 -- the current scope.
17698
17699 -- If this is a duplicate declaration, return base type, to avoid
17700 -- generating duplicate anonymous types.
17701
17702 if Error_Posted (P) then
17703 Analyze (Subtype_Mark (Obj_Def));
17704 return Entity (Subtype_Mark (Obj_Def));
17705 end if;
17706
17707 Nam :=
17708 New_External_Name
17709 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17710
17711 T := Make_Defining_Identifier (Sloc (P), Nam);
17712
17713 Insert_Action (Obj_Def,
17714 Make_Subtype_Declaration (Sloc (P),
17715 Defining_Identifier => T,
17716 Subtype_Indication => Relocate_Node (Obj_Def)));
17717
17718 -- This subtype may need freezing, and this will not be done
17719 -- automatically if the object declaration is not in declarative
17720 -- part. Since this is an object declaration, the type cannot always
17721 -- be frozen here. Deferred constants do not freeze their type
17722 -- (which often enough will be private).
17723
17724 if Nkind (P) = N_Object_Declaration
17725 and then Constant_Present (P)
17726 and then No (Expression (P))
17727 then
17728 null;
17729
17730 -- Here we freeze the base type of object type to catch premature use
17731 -- of discriminated private type without a full view.
17732
17733 else
17734 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17735 end if;
17736
17737 -- Ada 2005 AI-406: the object definition in an object declaration
17738 -- can be an access definition.
17739
17740 elsif Def_Kind = N_Access_Definition then
17741 T := Access_Definition (Related_Nod, Obj_Def);
17742
17743 Set_Is_Local_Anonymous_Access
17744 (T,
17745 V => (Ada_Version < Ada_2012)
17746 or else (Nkind (P) /= N_Object_Declaration)
17747 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17748
17749 -- Otherwise, the object definition is just a subtype_mark
17750
17751 else
17752 T := Process_Subtype (Obj_Def, Related_Nod);
17753 end if;
17754
17755 return T;
17756 end Find_Type_Of_Object;
17757
17758 --------------------------------
17759 -- Find_Type_Of_Subtype_Indic --
17760 --------------------------------
17761
17762 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17763 Typ : Entity_Id;
17764
17765 begin
17766 -- Case of subtype mark with a constraint
17767
17768 if Nkind (S) = N_Subtype_Indication then
17769 Find_Type (Subtype_Mark (S));
17770 Typ := Entity (Subtype_Mark (S));
17771
17772 if not
17773 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17774 then
17775 Error_Msg_N
17776 ("incorrect constraint for this kind of type", Constraint (S));
17777 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17778 end if;
17779
17780 -- Otherwise we have a subtype mark without a constraint
17781
17782 elsif Error_Posted (S) then
17783 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17784 return Any_Type;
17785
17786 else
17787 Find_Type (S);
17788 Typ := Entity (S);
17789 end if;
17790
17791 -- Check No_Wide_Characters restriction
17792
17793 Check_Wide_Character_Restriction (Typ, S);
17794
17795 return Typ;
17796 end Find_Type_Of_Subtype_Indic;
17797
17798 -------------------------------------
17799 -- Floating_Point_Type_Declaration --
17800 -------------------------------------
17801
17802 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17803 Digs : constant Node_Id := Digits_Expression (Def);
17804 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17805 Digs_Val : Uint;
17806 Base_Typ : Entity_Id;
17807 Implicit_Base : Entity_Id;
17808
17809 function Can_Derive_From (E : Entity_Id) return Boolean;
17810 -- Find if given digits value, and possibly a specified range, allows
17811 -- derivation from specified type
17812
17813 procedure Convert_Bound (B : Node_Id);
17814 -- If specified, the bounds must be static but may be of different
17815 -- types. They must be converted into machine numbers of the base type,
17816 -- in accordance with RM 4.9(38).
17817
17818 function Find_Base_Type return Entity_Id;
17819 -- Find a predefined base type that Def can derive from, or generate
17820 -- an error and substitute Long_Long_Float if none exists.
17821
17822 ---------------------
17823 -- Can_Derive_From --
17824 ---------------------
17825
17826 function Can_Derive_From (E : Entity_Id) return Boolean is
17827 Spec : constant Entity_Id := Real_Range_Specification (Def);
17828
17829 begin
17830 -- Check specified "digits" constraint
17831
17832 if Digs_Val > Digits_Value (E) then
17833 return False;
17834 end if;
17835
17836 -- Check for matching range, if specified
17837
17838 if Present (Spec) then
17839 if Expr_Value_R (Type_Low_Bound (E)) >
17840 Expr_Value_R (Low_Bound (Spec))
17841 then
17842 return False;
17843 end if;
17844
17845 if Expr_Value_R (Type_High_Bound (E)) <
17846 Expr_Value_R (High_Bound (Spec))
17847 then
17848 return False;
17849 end if;
17850 end if;
17851
17852 return True;
17853 end Can_Derive_From;
17854
17855 -------------------
17856 -- Convert_Bound --
17857 --------------------
17858
17859 procedure Convert_Bound (B : Node_Id) is
17860 begin
17861 -- If the bound is not a literal it can only be static if it is
17862 -- a static constant, possibly of a specified type.
17863
17864 if Is_Entity_Name (B)
17865 and then Ekind (Entity (B)) = E_Constant
17866 then
17867 Rewrite (B, Constant_Value (Entity (B)));
17868 end if;
17869
17870 if Nkind (B) = N_Real_Literal then
17871 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17872 Set_Is_Machine_Number (B);
17873 Set_Etype (B, Base_Typ);
17874 end if;
17875 end Convert_Bound;
17876
17877 --------------------
17878 -- Find_Base_Type --
17879 --------------------
17880
17881 function Find_Base_Type return Entity_Id is
17882 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17883
17884 begin
17885 -- Iterate over the predefined types in order, returning the first
17886 -- one that Def can derive from.
17887
17888 while Present (Choice) loop
17889 if Can_Derive_From (Node (Choice)) then
17890 return Node (Choice);
17891 end if;
17892
17893 Next_Elmt (Choice);
17894 end loop;
17895
17896 -- If we can't derive from any existing type, use Long_Long_Float
17897 -- and give appropriate message explaining the problem.
17898
17899 if Digs_Val > Max_Digs_Val then
17900 -- It might be the case that there is a type with the requested
17901 -- range, just not the combination of digits and range.
17902
17903 Error_Msg_N
17904 ("no predefined type has requested range and precision",
17905 Real_Range_Specification (Def));
17906
17907 else
17908 Error_Msg_N
17909 ("range too large for any predefined type",
17910 Real_Range_Specification (Def));
17911 end if;
17912
17913 return Standard_Long_Long_Float;
17914 end Find_Base_Type;
17915
17916 -- Start of processing for Floating_Point_Type_Declaration
17917
17918 begin
17919 Check_Restriction (No_Floating_Point, Def);
17920
17921 -- Create an implicit base type
17922
17923 Implicit_Base :=
17924 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17925
17926 -- Analyze and verify digits value
17927
17928 Analyze_And_Resolve (Digs, Any_Integer);
17929 Check_Digits_Expression (Digs);
17930 Digs_Val := Expr_Value (Digs);
17931
17932 -- Process possible range spec and find correct type to derive from
17933
17934 Process_Real_Range_Specification (Def);
17935
17936 -- Check that requested number of digits is not too high.
17937
17938 if Digs_Val > Max_Digs_Val then
17939
17940 -- The check for Max_Base_Digits may be somewhat expensive, as it
17941 -- requires reading System, so only do it when necessary.
17942
17943 declare
17944 Max_Base_Digits : constant Uint :=
17945 Expr_Value
17946 (Expression
17947 (Parent (RTE (RE_Max_Base_Digits))));
17948
17949 begin
17950 if Digs_Val > Max_Base_Digits then
17951 Error_Msg_Uint_1 := Max_Base_Digits;
17952 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17953
17954 elsif No (Real_Range_Specification (Def)) then
17955 Error_Msg_Uint_1 := Max_Digs_Val;
17956 Error_Msg_N ("types with more than ^ digits need range spec "
17957 & "(RM 3.5.7(6))", Digs);
17958 end if;
17959 end;
17960 end if;
17961
17962 -- Find a suitable type to derive from or complain and use a substitute
17963
17964 Base_Typ := Find_Base_Type;
17965
17966 -- If there are bounds given in the declaration use them as the bounds
17967 -- of the type, otherwise use the bounds of the predefined base type
17968 -- that was chosen based on the Digits value.
17969
17970 if Present (Real_Range_Specification (Def)) then
17971 Set_Scalar_Range (T, Real_Range_Specification (Def));
17972 Set_Is_Constrained (T);
17973
17974 Convert_Bound (Type_Low_Bound (T));
17975 Convert_Bound (Type_High_Bound (T));
17976
17977 else
17978 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17979 end if;
17980
17981 -- Complete definition of implicit base and declared first subtype. The
17982 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17983 -- are not clobbered when the floating point type acts as a full view of
17984 -- a private type.
17985
17986 Set_Etype (Implicit_Base, Base_Typ);
17987 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17988 Set_Size_Info (Implicit_Base, Base_Typ);
17989 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17990 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17991 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17992 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17993
17994 Set_Ekind (T, E_Floating_Point_Subtype);
17995 Set_Etype (T, Implicit_Base);
17996 Set_Size_Info (T, Implicit_Base);
17997 Set_RM_Size (T, RM_Size (Implicit_Base));
17998 Inherit_Rep_Item_Chain (T, Implicit_Base);
17999 Set_Digits_Value (T, Digs_Val);
18000 end Floating_Point_Type_Declaration;
18001
18002 ----------------------------
18003 -- Get_Discriminant_Value --
18004 ----------------------------
18005
18006 -- This is the situation:
18007
18008 -- There is a non-derived type
18009
18010 -- type T0 (Dx, Dy, Dz...)
18011
18012 -- There are zero or more levels of derivation, with each derivation
18013 -- either purely inheriting the discriminants, or defining its own.
18014
18015 -- type Ti is new Ti-1
18016 -- or
18017 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18018 -- or
18019 -- subtype Ti is ...
18020
18021 -- The subtype issue is avoided by the use of Original_Record_Component,
18022 -- and the fact that derived subtypes also derive the constraints.
18023
18024 -- This chain leads back from
18025
18026 -- Typ_For_Constraint
18027
18028 -- Typ_For_Constraint has discriminants, and the value for each
18029 -- discriminant is given by its corresponding Elmt of Constraints.
18030
18031 -- Discriminant is some discriminant in this hierarchy
18032
18033 -- We need to return its value
18034
18035 -- We do this by recursively searching each level, and looking for
18036 -- Discriminant. Once we get to the bottom, we start backing up
18037 -- returning the value for it which may in turn be a discriminant
18038 -- further up, so on the backup we continue the substitution.
18039
18040 function Get_Discriminant_Value
18041 (Discriminant : Entity_Id;
18042 Typ_For_Constraint : Entity_Id;
18043 Constraint : Elist_Id) return Node_Id
18044 is
18045 function Root_Corresponding_Discriminant
18046 (Discr : Entity_Id) return Entity_Id;
18047 -- Given a discriminant, traverse the chain of inherited discriminants
18048 -- and return the topmost discriminant.
18049
18050 function Search_Derivation_Levels
18051 (Ti : Entity_Id;
18052 Discrim_Values : Elist_Id;
18053 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18054 -- This is the routine that performs the recursive search of levels
18055 -- as described above.
18056
18057 -------------------------------------
18058 -- Root_Corresponding_Discriminant --
18059 -------------------------------------
18060
18061 function Root_Corresponding_Discriminant
18062 (Discr : Entity_Id) return Entity_Id
18063 is
18064 D : Entity_Id;
18065
18066 begin
18067 D := Discr;
18068 while Present (Corresponding_Discriminant (D)) loop
18069 D := Corresponding_Discriminant (D);
18070 end loop;
18071
18072 return D;
18073 end Root_Corresponding_Discriminant;
18074
18075 ------------------------------
18076 -- Search_Derivation_Levels --
18077 ------------------------------
18078
18079 function Search_Derivation_Levels
18080 (Ti : Entity_Id;
18081 Discrim_Values : Elist_Id;
18082 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18083 is
18084 Assoc : Elmt_Id;
18085 Disc : Entity_Id;
18086 Result : Node_Or_Entity_Id;
18087 Result_Entity : Node_Id;
18088
18089 begin
18090 -- If inappropriate type, return Error, this happens only in
18091 -- cascaded error situations, and we want to avoid a blow up.
18092
18093 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18094 return Error;
18095 end if;
18096
18097 -- Look deeper if possible. Use Stored_Constraints only for
18098 -- untagged types. For tagged types use the given constraint.
18099 -- This asymmetry needs explanation???
18100
18101 if not Stored_Discrim_Values
18102 and then Present (Stored_Constraint (Ti))
18103 and then not Is_Tagged_Type (Ti)
18104 then
18105 Result :=
18106 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18107
18108 else
18109 declare
18110 Td : Entity_Id := Etype (Ti);
18111
18112 begin
18113 -- If the parent type is private, the full view may include
18114 -- renamed discriminants, and it is those stored values that
18115 -- may be needed (the partial view never has more information
18116 -- than the full view).
18117
18118 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18119 Td := Full_View (Td);
18120 end if;
18121
18122 if Td = Ti then
18123 Result := Discriminant;
18124
18125 else
18126 if Present (Stored_Constraint (Ti)) then
18127 Result :=
18128 Search_Derivation_Levels
18129 (Td, Stored_Constraint (Ti), True);
18130 else
18131 Result :=
18132 Search_Derivation_Levels
18133 (Td, Discrim_Values, Stored_Discrim_Values);
18134 end if;
18135 end if;
18136 end;
18137 end if;
18138
18139 -- Extra underlying places to search, if not found above. For
18140 -- concurrent types, the relevant discriminant appears in the
18141 -- corresponding record. For a type derived from a private type
18142 -- without discriminant, the full view inherits the discriminants
18143 -- of the full view of the parent.
18144
18145 if Result = Discriminant then
18146 if Is_Concurrent_Type (Ti)
18147 and then Present (Corresponding_Record_Type (Ti))
18148 then
18149 Result :=
18150 Search_Derivation_Levels (
18151 Corresponding_Record_Type (Ti),
18152 Discrim_Values,
18153 Stored_Discrim_Values);
18154
18155 elsif Is_Private_Type (Ti)
18156 and then not Has_Discriminants (Ti)
18157 and then Present (Full_View (Ti))
18158 and then Etype (Full_View (Ti)) /= Ti
18159 then
18160 Result :=
18161 Search_Derivation_Levels (
18162 Full_View (Ti),
18163 Discrim_Values,
18164 Stored_Discrim_Values);
18165 end if;
18166 end if;
18167
18168 -- If Result is not a (reference to a) discriminant, return it,
18169 -- otherwise set Result_Entity to the discriminant.
18170
18171 if Nkind (Result) = N_Defining_Identifier then
18172 pragma Assert (Result = Discriminant);
18173 Result_Entity := Result;
18174
18175 else
18176 if not Denotes_Discriminant (Result) then
18177 return Result;
18178 end if;
18179
18180 Result_Entity := Entity (Result);
18181 end if;
18182
18183 -- See if this level of derivation actually has discriminants because
18184 -- tagged derivations can add them, hence the lower levels need not
18185 -- have any.
18186
18187 if not Has_Discriminants (Ti) then
18188 return Result;
18189 end if;
18190
18191 -- Scan Ti's discriminants for Result_Entity, and return its
18192 -- corresponding value, if any.
18193
18194 Result_Entity := Original_Record_Component (Result_Entity);
18195
18196 Assoc := First_Elmt (Discrim_Values);
18197
18198 if Stored_Discrim_Values then
18199 Disc := First_Stored_Discriminant (Ti);
18200 else
18201 Disc := First_Discriminant (Ti);
18202 end if;
18203
18204 while Present (Disc) loop
18205
18206 -- If no further associations return the discriminant, value will
18207 -- be found on the second pass.
18208
18209 if No (Assoc) then
18210 return Result;
18211 end if;
18212
18213 if Original_Record_Component (Disc) = Result_Entity then
18214 return Node (Assoc);
18215 end if;
18216
18217 Next_Elmt (Assoc);
18218
18219 if Stored_Discrim_Values then
18220 Next_Stored_Discriminant (Disc);
18221 else
18222 Next_Discriminant (Disc);
18223 end if;
18224 end loop;
18225
18226 -- Could not find it
18227
18228 return Result;
18229 end Search_Derivation_Levels;
18230
18231 -- Local Variables
18232
18233 Result : Node_Or_Entity_Id;
18234
18235 -- Start of processing for Get_Discriminant_Value
18236
18237 begin
18238 -- ??? This routine is a gigantic mess and will be deleted. For the
18239 -- time being just test for the trivial case before calling recurse.
18240
18241 -- We are now celebrating the 20th anniversary of this comment!
18242
18243 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18244 declare
18245 D : Entity_Id;
18246 E : Elmt_Id;
18247
18248 begin
18249 D := First_Discriminant (Typ_For_Constraint);
18250 E := First_Elmt (Constraint);
18251 while Present (D) loop
18252 if Chars (D) = Chars (Discriminant) then
18253 return Node (E);
18254 end if;
18255
18256 Next_Discriminant (D);
18257 Next_Elmt (E);
18258 end loop;
18259 end;
18260 end if;
18261
18262 Result := Search_Derivation_Levels
18263 (Typ_For_Constraint, Constraint, False);
18264
18265 -- ??? hack to disappear when this routine is gone
18266
18267 if Nkind (Result) = N_Defining_Identifier then
18268 declare
18269 D : Entity_Id;
18270 E : Elmt_Id;
18271
18272 begin
18273 D := First_Discriminant (Typ_For_Constraint);
18274 E := First_Elmt (Constraint);
18275 while Present (D) loop
18276 if Root_Corresponding_Discriminant (D) = Discriminant then
18277 return Node (E);
18278 end if;
18279
18280 Next_Discriminant (D);
18281 Next_Elmt (E);
18282 end loop;
18283 end;
18284 end if;
18285
18286 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18287 return Result;
18288 end Get_Discriminant_Value;
18289
18290 --------------------------
18291 -- Has_Range_Constraint --
18292 --------------------------
18293
18294 function Has_Range_Constraint (N : Node_Id) return Boolean is
18295 C : constant Node_Id := Constraint (N);
18296
18297 begin
18298 if Nkind (C) = N_Range_Constraint then
18299 return True;
18300
18301 elsif Nkind (C) = N_Digits_Constraint then
18302 return
18303 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18304 or else Present (Range_Constraint (C));
18305
18306 elsif Nkind (C) = N_Delta_Constraint then
18307 return Present (Range_Constraint (C));
18308
18309 else
18310 return False;
18311 end if;
18312 end Has_Range_Constraint;
18313
18314 ------------------------
18315 -- Inherit_Components --
18316 ------------------------
18317
18318 function Inherit_Components
18319 (N : Node_Id;
18320 Parent_Base : Entity_Id;
18321 Derived_Base : Entity_Id;
18322 Is_Tagged : Boolean;
18323 Inherit_Discr : Boolean;
18324 Discs : Elist_Id) return Elist_Id
18325 is
18326 Assoc_List : constant Elist_Id := New_Elmt_List;
18327
18328 procedure Inherit_Component
18329 (Old_C : Entity_Id;
18330 Plain_Discrim : Boolean := False;
18331 Stored_Discrim : Boolean := False);
18332 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18333 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18334 -- True, Old_C is a stored discriminant. If they are both false then
18335 -- Old_C is a regular component.
18336
18337 -----------------------
18338 -- Inherit_Component --
18339 -----------------------
18340
18341 procedure Inherit_Component
18342 (Old_C : Entity_Id;
18343 Plain_Discrim : Boolean := False;
18344 Stored_Discrim : Boolean := False)
18345 is
18346 procedure Set_Anonymous_Type (Id : Entity_Id);
18347 -- Id denotes the entity of an access discriminant or anonymous
18348 -- access component. Set the type of Id to either the same type of
18349 -- Old_C or create a new one depending on whether the parent and
18350 -- the child types are in the same scope.
18351
18352 ------------------------
18353 -- Set_Anonymous_Type --
18354 ------------------------
18355
18356 procedure Set_Anonymous_Type (Id : Entity_Id) is
18357 Old_Typ : constant Entity_Id := Etype (Old_C);
18358
18359 begin
18360 if Scope (Parent_Base) = Scope (Derived_Base) then
18361 Set_Etype (Id, Old_Typ);
18362
18363 -- The parent and the derived type are in two different scopes.
18364 -- Reuse the type of the original discriminant / component by
18365 -- copying it in order to preserve all attributes.
18366
18367 else
18368 declare
18369 Typ : constant Entity_Id := New_Copy (Old_Typ);
18370
18371 begin
18372 Set_Etype (Id, Typ);
18373
18374 -- Since we do not generate component declarations for
18375 -- inherited components, associate the itype with the
18376 -- derived type.
18377
18378 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18379 Set_Scope (Typ, Derived_Base);
18380 end;
18381 end if;
18382 end Set_Anonymous_Type;
18383
18384 -- Local variables and constants
18385
18386 New_C : constant Entity_Id := New_Copy (Old_C);
18387
18388 Corr_Discrim : Entity_Id;
18389 Discrim : Entity_Id;
18390
18391 -- Start of processing for Inherit_Component
18392
18393 begin
18394 pragma Assert (not Is_Tagged or not Stored_Discrim);
18395
18396 Set_Parent (New_C, Parent (Old_C));
18397
18398 -- Regular discriminants and components must be inserted in the scope
18399 -- of the Derived_Base. Do it here.
18400
18401 if not Stored_Discrim then
18402 Enter_Name (New_C);
18403 end if;
18404
18405 -- For tagged types the Original_Record_Component must point to
18406 -- whatever this field was pointing to in the parent type. This has
18407 -- already been achieved by the call to New_Copy above.
18408
18409 if not Is_Tagged then
18410 Set_Original_Record_Component (New_C, New_C);
18411 Set_Corresponding_Record_Component (New_C, Old_C);
18412 end if;
18413
18414 -- Set the proper type of an access discriminant
18415
18416 if Ekind (New_C) = E_Discriminant
18417 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18418 then
18419 Set_Anonymous_Type (New_C);
18420 end if;
18421
18422 -- If we have inherited a component then see if its Etype contains
18423 -- references to Parent_Base discriminants. In this case, replace
18424 -- these references with the constraints given in Discs. We do not
18425 -- do this for the partial view of private types because this is
18426 -- not needed (only the components of the full view will be used
18427 -- for code generation) and cause problem. We also avoid this
18428 -- transformation in some error situations.
18429
18430 if Ekind (New_C) = E_Component then
18431
18432 -- Set the proper type of an anonymous access component
18433
18434 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18435 Set_Anonymous_Type (New_C);
18436
18437 elsif (Is_Private_Type (Derived_Base)
18438 and then not Is_Generic_Type (Derived_Base))
18439 or else (Is_Empty_Elmt_List (Discs)
18440 and then not Expander_Active)
18441 then
18442 Set_Etype (New_C, Etype (Old_C));
18443
18444 else
18445 -- The current component introduces a circularity of the
18446 -- following kind:
18447
18448 -- limited with Pack_2;
18449 -- package Pack_1 is
18450 -- type T_1 is tagged record
18451 -- Comp : access Pack_2.T_2;
18452 -- ...
18453 -- end record;
18454 -- end Pack_1;
18455
18456 -- with Pack_1;
18457 -- package Pack_2 is
18458 -- type T_2 is new Pack_1.T_1 with ...;
18459 -- end Pack_2;
18460
18461 Set_Etype
18462 (New_C,
18463 Constrain_Component_Type
18464 (Old_C, Derived_Base, N, Parent_Base, Discs));
18465 end if;
18466 end if;
18467
18468 -- In derived tagged types it is illegal to reference a non
18469 -- discriminant component in the parent type. To catch this, mark
18470 -- these components with an Ekind of E_Void. This will be reset in
18471 -- Record_Type_Definition after processing the record extension of
18472 -- the derived type.
18473
18474 -- If the declaration is a private extension, there is no further
18475 -- record extension to process, and the components retain their
18476 -- current kind, because they are visible at this point.
18477
18478 if Is_Tagged and then Ekind (New_C) = E_Component
18479 and then Nkind (N) /= N_Private_Extension_Declaration
18480 then
18481 Set_Ekind (New_C, E_Void);
18482 end if;
18483
18484 if Plain_Discrim then
18485 Set_Corresponding_Discriminant (New_C, Old_C);
18486 Build_Discriminal (New_C);
18487
18488 -- If we are explicitly inheriting a stored discriminant it will be
18489 -- completely hidden.
18490
18491 elsif Stored_Discrim then
18492 Set_Corresponding_Discriminant (New_C, Empty);
18493 Set_Discriminal (New_C, Empty);
18494 Set_Is_Completely_Hidden (New_C);
18495
18496 -- Set the Original_Record_Component of each discriminant in the
18497 -- derived base to point to the corresponding stored that we just
18498 -- created.
18499
18500 Discrim := First_Discriminant (Derived_Base);
18501 while Present (Discrim) loop
18502 Corr_Discrim := Corresponding_Discriminant (Discrim);
18503
18504 -- Corr_Discrim could be missing in an error situation
18505
18506 if Present (Corr_Discrim)
18507 and then Original_Record_Component (Corr_Discrim) = Old_C
18508 then
18509 Set_Original_Record_Component (Discrim, New_C);
18510 Set_Corresponding_Record_Component (Discrim, Empty);
18511 end if;
18512
18513 Next_Discriminant (Discrim);
18514 end loop;
18515
18516 Append_Entity (New_C, Derived_Base);
18517 end if;
18518
18519 if not Is_Tagged then
18520 Append_Elmt (Old_C, Assoc_List);
18521 Append_Elmt (New_C, Assoc_List);
18522 end if;
18523 end Inherit_Component;
18524
18525 -- Variables local to Inherit_Component
18526
18527 Loc : constant Source_Ptr := Sloc (N);
18528
18529 Parent_Discrim : Entity_Id;
18530 Stored_Discrim : Entity_Id;
18531 D : Entity_Id;
18532 Component : Entity_Id;
18533
18534 -- Start of processing for Inherit_Components
18535
18536 begin
18537 if not Is_Tagged then
18538 Append_Elmt (Parent_Base, Assoc_List);
18539 Append_Elmt (Derived_Base, Assoc_List);
18540 end if;
18541
18542 -- Inherit parent discriminants if needed
18543
18544 if Inherit_Discr then
18545 Parent_Discrim := First_Discriminant (Parent_Base);
18546 while Present (Parent_Discrim) loop
18547 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18548 Next_Discriminant (Parent_Discrim);
18549 end loop;
18550 end if;
18551
18552 -- Create explicit stored discrims for untagged types when necessary
18553
18554 if not Has_Unknown_Discriminants (Derived_Base)
18555 and then Has_Discriminants (Parent_Base)
18556 and then not Is_Tagged
18557 and then
18558 (not Inherit_Discr
18559 or else First_Discriminant (Parent_Base) /=
18560 First_Stored_Discriminant (Parent_Base))
18561 then
18562 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18563 while Present (Stored_Discrim) loop
18564 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18565 Next_Stored_Discriminant (Stored_Discrim);
18566 end loop;
18567 end if;
18568
18569 -- See if we can apply the second transformation for derived types, as
18570 -- explained in point 6. in the comments above Build_Derived_Record_Type
18571 -- This is achieved by appending Derived_Base discriminants into Discs,
18572 -- which has the side effect of returning a non empty Discs list to the
18573 -- caller of Inherit_Components, which is what we want. This must be
18574 -- done for private derived types if there are explicit stored
18575 -- discriminants, to ensure that we can retrieve the values of the
18576 -- constraints provided in the ancestors.
18577
18578 if Inherit_Discr
18579 and then Is_Empty_Elmt_List (Discs)
18580 and then Present (First_Discriminant (Derived_Base))
18581 and then
18582 (not Is_Private_Type (Derived_Base)
18583 or else Is_Completely_Hidden
18584 (First_Stored_Discriminant (Derived_Base))
18585 or else Is_Generic_Type (Derived_Base))
18586 then
18587 D := First_Discriminant (Derived_Base);
18588 while Present (D) loop
18589 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18590 Next_Discriminant (D);
18591 end loop;
18592 end if;
18593
18594 -- Finally, inherit non-discriminant components unless they are not
18595 -- visible because defined or inherited from the full view of the
18596 -- parent. Don't inherit the _parent field of the parent type.
18597
18598 Component := First_Entity (Parent_Base);
18599 while Present (Component) loop
18600
18601 -- Ada 2005 (AI-251): Do not inherit components associated with
18602 -- secondary tags of the parent.
18603
18604 if Ekind (Component) = E_Component
18605 and then Present (Related_Type (Component))
18606 then
18607 null;
18608
18609 elsif Ekind (Component) /= E_Component
18610 or else Chars (Component) = Name_uParent
18611 then
18612 null;
18613
18614 -- If the derived type is within the parent type's declarative
18615 -- region, then the components can still be inherited even though
18616 -- they aren't visible at this point. This can occur for cases
18617 -- such as within public child units where the components must
18618 -- become visible upon entering the child unit's private part.
18619
18620 elsif not Is_Visible_Component (Component)
18621 and then not In_Open_Scopes (Scope (Parent_Base))
18622 then
18623 null;
18624
18625 elsif Ekind_In (Derived_Base, E_Private_Type,
18626 E_Limited_Private_Type)
18627 then
18628 null;
18629
18630 else
18631 Inherit_Component (Component);
18632 end if;
18633
18634 Next_Entity (Component);
18635 end loop;
18636
18637 -- For tagged derived types, inherited discriminants cannot be used in
18638 -- component declarations of the record extension part. To achieve this
18639 -- we mark the inherited discriminants as not visible.
18640
18641 if Is_Tagged and then Inherit_Discr then
18642 D := First_Discriminant (Derived_Base);
18643 while Present (D) loop
18644 Set_Is_Immediately_Visible (D, False);
18645 Next_Discriminant (D);
18646 end loop;
18647 end if;
18648
18649 return Assoc_List;
18650 end Inherit_Components;
18651
18652 -----------------------------
18653 -- Inherit_Predicate_Flags --
18654 -----------------------------
18655
18656 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18657 begin
18658 if Present (Predicate_Function (Subt)) then
18659 return;
18660 end if;
18661
18662 Set_Has_Predicates (Subt, Has_Predicates (Par));
18663 Set_Has_Static_Predicate_Aspect
18664 (Subt, Has_Static_Predicate_Aspect (Par));
18665 Set_Has_Dynamic_Predicate_Aspect
18666 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18667
18668 -- A named subtype does not inherit the predicate function of its
18669 -- parent but an itype declared for a loop index needs the discrete
18670 -- predicate information of its parent to execute the loop properly.
18671 -- A non-discrete type may has a static predicate (for example True)
18672 -- but has no static_discrete_predicate.
18673
18674 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18675 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18676
18677 if Has_Static_Predicate (Par) and then Is_Discrete_Type (Par) then
18678 Set_Static_Discrete_Predicate
18679 (Subt, Static_Discrete_Predicate (Par));
18680 end if;
18681 end if;
18682 end Inherit_Predicate_Flags;
18683
18684 ----------------------
18685 -- Is_EVF_Procedure --
18686 ----------------------
18687
18688 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18689 Formal : Entity_Id;
18690
18691 begin
18692 -- Examine the formals of an Extensions_Visible False procedure looking
18693 -- for a controlling OUT parameter.
18694
18695 if Ekind (Subp) = E_Procedure
18696 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18697 then
18698 Formal := First_Formal (Subp);
18699 while Present (Formal) loop
18700 if Ekind (Formal) = E_Out_Parameter
18701 and then Is_Controlling_Formal (Formal)
18702 then
18703 return True;
18704 end if;
18705
18706 Next_Formal (Formal);
18707 end loop;
18708 end if;
18709
18710 return False;
18711 end Is_EVF_Procedure;
18712
18713 -----------------------
18714 -- Is_Null_Extension --
18715 -----------------------
18716
18717 function Is_Null_Extension (T : Entity_Id) return Boolean is
18718 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18719 Comp_List : Node_Id;
18720 Comp : Node_Id;
18721
18722 begin
18723 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18724 or else not Is_Tagged_Type (T)
18725 or else Nkind (Type_Definition (Type_Decl)) /=
18726 N_Derived_Type_Definition
18727 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18728 then
18729 return False;
18730 end if;
18731
18732 Comp_List :=
18733 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18734
18735 if Present (Discriminant_Specifications (Type_Decl)) then
18736 return False;
18737
18738 elsif Present (Comp_List)
18739 and then Is_Non_Empty_List (Component_Items (Comp_List))
18740 then
18741 Comp := First (Component_Items (Comp_List));
18742
18743 -- Only user-defined components are relevant. The component list
18744 -- may also contain a parent component and internal components
18745 -- corresponding to secondary tags, but these do not determine
18746 -- whether this is a null extension.
18747
18748 while Present (Comp) loop
18749 if Comes_From_Source (Comp) then
18750 return False;
18751 end if;
18752
18753 Next (Comp);
18754 end loop;
18755
18756 return True;
18757
18758 else
18759 return True;
18760 end if;
18761 end Is_Null_Extension;
18762
18763 ------------------------------
18764 -- Is_Valid_Constraint_Kind --
18765 ------------------------------
18766
18767 function Is_Valid_Constraint_Kind
18768 (T_Kind : Type_Kind;
18769 Constraint_Kind : Node_Kind) return Boolean
18770 is
18771 begin
18772 case T_Kind is
18773 when Enumeration_Kind
18774 | Integer_Kind
18775 =>
18776 return Constraint_Kind = N_Range_Constraint;
18777
18778 when Decimal_Fixed_Point_Kind =>
18779 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18780 N_Range_Constraint);
18781
18782 when Ordinary_Fixed_Point_Kind =>
18783 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18784 N_Range_Constraint);
18785
18786 when Float_Kind =>
18787 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18788 N_Range_Constraint);
18789
18790 when Access_Kind
18791 | Array_Kind
18792 | Class_Wide_Kind
18793 | Concurrent_Kind
18794 | Private_Kind
18795 | E_Incomplete_Type
18796 | E_Record_Subtype
18797 | E_Record_Type
18798 =>
18799 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18800
18801 when others =>
18802 return True; -- Error will be detected later
18803 end case;
18804 end Is_Valid_Constraint_Kind;
18805
18806 --------------------------
18807 -- Is_Visible_Component --
18808 --------------------------
18809
18810 function Is_Visible_Component
18811 (C : Entity_Id;
18812 N : Node_Id := Empty) return Boolean
18813 is
18814 Original_Comp : Entity_Id := Empty;
18815 Original_Type : Entity_Id;
18816 Type_Scope : Entity_Id;
18817
18818 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18819 -- Check whether parent type of inherited component is declared locally,
18820 -- possibly within a nested package or instance. The current scope is
18821 -- the derived record itself.
18822
18823 -------------------
18824 -- Is_Local_Type --
18825 -------------------
18826
18827 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18828 Scop : Entity_Id;
18829
18830 begin
18831 Scop := Scope (Typ);
18832 while Present (Scop)
18833 and then Scop /= Standard_Standard
18834 loop
18835 if Scop = Scope (Current_Scope) then
18836 return True;
18837 end if;
18838
18839 Scop := Scope (Scop);
18840 end loop;
18841
18842 return False;
18843 end Is_Local_Type;
18844
18845 -- Start of processing for Is_Visible_Component
18846
18847 begin
18848 if Ekind_In (C, E_Component, E_Discriminant) then
18849 Original_Comp := Original_Record_Component (C);
18850 end if;
18851
18852 if No (Original_Comp) then
18853
18854 -- Premature usage, or previous error
18855
18856 return False;
18857
18858 else
18859 Original_Type := Scope (Original_Comp);
18860 Type_Scope := Scope (Base_Type (Scope (C)));
18861 end if;
18862
18863 -- This test only concerns tagged types
18864
18865 if not Is_Tagged_Type (Original_Type) then
18866
18867 -- Check if this is a renamed discriminant (hidden either by the
18868 -- derived type or by some ancestor), unless we are analyzing code
18869 -- generated by the expander since it may reference such components
18870 -- (for example see the expansion of Deep_Adjust).
18871
18872 if Ekind (C) = E_Discriminant and then Present (N) then
18873 return
18874 not Comes_From_Source (N)
18875 or else not Is_Completely_Hidden (C);
18876 else
18877 return True;
18878 end if;
18879
18880 -- If it is _Parent or _Tag, there is no visibility issue
18881
18882 elsif not Comes_From_Source (Original_Comp) then
18883 return True;
18884
18885 -- Discriminants are visible unless the (private) type has unknown
18886 -- discriminants. If the discriminant reference is inserted for a
18887 -- discriminant check on a full view it is also visible.
18888
18889 elsif Ekind (Original_Comp) = E_Discriminant
18890 and then
18891 (not Has_Unknown_Discriminants (Original_Type)
18892 or else (Present (N)
18893 and then Nkind (N) = N_Selected_Component
18894 and then Nkind (Prefix (N)) = N_Type_Conversion
18895 and then not Comes_From_Source (Prefix (N))))
18896 then
18897 return True;
18898
18899 -- In the body of an instantiation, check the visibility of a component
18900 -- in case it has a homograph that is a primitive operation of a private
18901 -- type which was not visible in the generic unit.
18902
18903 -- Should Is_Prefixed_Call be propagated from template to instance???
18904
18905 elsif In_Instance_Body then
18906 if not Is_Tagged_Type (Original_Type)
18907 or else not Is_Private_Type (Original_Type)
18908 then
18909 return True;
18910
18911 else
18912 declare
18913 Subp_Elmt : Elmt_Id;
18914
18915 begin
18916 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18917 while Present (Subp_Elmt) loop
18918
18919 -- The component is hidden by a primitive operation
18920
18921 if Chars (Node (Subp_Elmt)) = Chars (C) then
18922 return False;
18923 end if;
18924
18925 Next_Elmt (Subp_Elmt);
18926 end loop;
18927
18928 return True;
18929 end;
18930 end if;
18931
18932 -- If the component has been declared in an ancestor which is currently
18933 -- a private type, then it is not visible. The same applies if the
18934 -- component's containing type is not in an open scope and the original
18935 -- component's enclosing type is a visible full view of a private type
18936 -- (which can occur in cases where an attempt is being made to reference
18937 -- a component in a sibling package that is inherited from a visible
18938 -- component of a type in an ancestor package; the component in the
18939 -- sibling package should not be visible even though the component it
18940 -- inherited from is visible). This does not apply however in the case
18941 -- where the scope of the type is a private child unit, or when the
18942 -- parent comes from a local package in which the ancestor is currently
18943 -- visible. The latter suppression of visibility is needed for cases
18944 -- that are tested in B730006.
18945
18946 elsif Is_Private_Type (Original_Type)
18947 or else
18948 (not Is_Private_Descendant (Type_Scope)
18949 and then not In_Open_Scopes (Type_Scope)
18950 and then Has_Private_Declaration (Original_Type))
18951 then
18952 -- If the type derives from an entity in a formal package, there
18953 -- are no additional visible components.
18954
18955 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18956 N_Formal_Package_Declaration
18957 then
18958 return False;
18959
18960 -- if we are not in the private part of the current package, there
18961 -- are no additional visible components.
18962
18963 elsif Ekind (Scope (Current_Scope)) = E_Package
18964 and then not In_Private_Part (Scope (Current_Scope))
18965 then
18966 return False;
18967 else
18968 return
18969 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18970 and then In_Open_Scopes (Scope (Original_Type))
18971 and then Is_Local_Type (Type_Scope);
18972 end if;
18973
18974 -- There is another weird way in which a component may be invisible when
18975 -- the private and the full view are not derived from the same ancestor.
18976 -- Here is an example :
18977
18978 -- type A1 is tagged record F1 : integer; end record;
18979 -- type A2 is new A1 with record F2 : integer; end record;
18980 -- type T is new A1 with private;
18981 -- private
18982 -- type T is new A2 with null record;
18983
18984 -- In this case, the full view of T inherits F1 and F2 but the private
18985 -- view inherits only F1
18986
18987 else
18988 declare
18989 Ancestor : Entity_Id := Scope (C);
18990
18991 begin
18992 loop
18993 if Ancestor = Original_Type then
18994 return True;
18995
18996 -- The ancestor may have a partial view of the original type,
18997 -- but if the full view is in scope, as in a child body, the
18998 -- component is visible.
18999
19000 elsif In_Private_Part (Scope (Original_Type))
19001 and then Full_View (Ancestor) = Original_Type
19002 then
19003 return True;
19004
19005 elsif Ancestor = Etype (Ancestor) then
19006
19007 -- No further ancestors to examine
19008
19009 return False;
19010 end if;
19011
19012 Ancestor := Etype (Ancestor);
19013 end loop;
19014 end;
19015 end if;
19016 end Is_Visible_Component;
19017
19018 --------------------------
19019 -- Make_Class_Wide_Type --
19020 --------------------------
19021
19022 procedure Make_Class_Wide_Type (T : Entity_Id) is
19023 CW_Type : Entity_Id;
19024 CW_Name : Name_Id;
19025 Next_E : Entity_Id;
19026 Prev_E : Entity_Id;
19027
19028 begin
19029 if Present (Class_Wide_Type (T)) then
19030
19031 -- The class-wide type is a partially decorated entity created for a
19032 -- unanalyzed tagged type referenced through a limited with clause.
19033 -- When the tagged type is analyzed, its class-wide type needs to be
19034 -- redecorated. Note that we reuse the entity created by Decorate_
19035 -- Tagged_Type in order to preserve all links.
19036
19037 if Materialize_Entity (Class_Wide_Type (T)) then
19038 CW_Type := Class_Wide_Type (T);
19039 Set_Materialize_Entity (CW_Type, False);
19040
19041 -- The class wide type can have been defined by the partial view, in
19042 -- which case everything is already done.
19043
19044 else
19045 return;
19046 end if;
19047
19048 -- Default case, we need to create a new class-wide type
19049
19050 else
19051 CW_Type :=
19052 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19053 end if;
19054
19055 -- Inherit root type characteristics
19056
19057 CW_Name := Chars (CW_Type);
19058 Next_E := Next_Entity (CW_Type);
19059 Prev_E := Prev_Entity (CW_Type);
19060 Copy_Node (T, CW_Type);
19061 Set_Comes_From_Source (CW_Type, False);
19062 Set_Chars (CW_Type, CW_Name);
19063 Set_Parent (CW_Type, Parent (T));
19064 Set_Prev_Entity (CW_Type, Prev_E);
19065 Set_Next_Entity (CW_Type, Next_E);
19066
19067 -- Ensure we have a new freeze node for the class-wide type. The partial
19068 -- view may have freeze action of its own, requiring a proper freeze
19069 -- node, and the same freeze node cannot be shared between the two
19070 -- types.
19071
19072 Set_Has_Delayed_Freeze (CW_Type);
19073 Set_Freeze_Node (CW_Type, Empty);
19074
19075 -- Customize the class-wide type: It has no prim. op., it cannot be
19076 -- abstract, its Etype points back to the specific root type, and it
19077 -- cannot have any invariants.
19078
19079 Set_Ekind (CW_Type, E_Class_Wide_Type);
19080 Set_Is_Tagged_Type (CW_Type, True);
19081 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19082 Set_Is_Abstract_Type (CW_Type, False);
19083 Set_Is_Constrained (CW_Type, False);
19084 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19085 Set_Default_SSO (CW_Type);
19086 Set_Has_Inheritable_Invariants (CW_Type, False);
19087 Set_Has_Inherited_Invariants (CW_Type, False);
19088 Set_Has_Own_Invariants (CW_Type, False);
19089
19090 if Ekind (T) = E_Class_Wide_Subtype then
19091 Set_Etype (CW_Type, Etype (Base_Type (T)));
19092 else
19093 Set_Etype (CW_Type, T);
19094 end if;
19095
19096 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19097
19098 -- If this is the class_wide type of a constrained subtype, it does
19099 -- not have discriminants.
19100
19101 Set_Has_Discriminants (CW_Type,
19102 Has_Discriminants (T) and then not Is_Constrained (T));
19103
19104 Set_Has_Unknown_Discriminants (CW_Type, True);
19105 Set_Class_Wide_Type (T, CW_Type);
19106 Set_Equivalent_Type (CW_Type, Empty);
19107
19108 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19109
19110 Set_Class_Wide_Type (CW_Type, CW_Type);
19111 end Make_Class_Wide_Type;
19112
19113 ----------------
19114 -- Make_Index --
19115 ----------------
19116
19117 procedure Make_Index
19118 (N : Node_Id;
19119 Related_Nod : Node_Id;
19120 Related_Id : Entity_Id := Empty;
19121 Suffix_Index : Nat := 1;
19122 In_Iter_Schm : Boolean := False)
19123 is
19124 R : Node_Id;
19125 T : Entity_Id;
19126 Def_Id : Entity_Id := Empty;
19127 Found : Boolean := False;
19128
19129 begin
19130 -- For a discrete range used in a constrained array definition and
19131 -- defined by a range, an implicit conversion to the predefined type
19132 -- INTEGER is assumed if each bound is either a numeric literal, a named
19133 -- number, or an attribute, and the type of both bounds (prior to the
19134 -- implicit conversion) is the type universal_integer. Otherwise, both
19135 -- bounds must be of the same discrete type, other than universal
19136 -- integer; this type must be determinable independently of the
19137 -- context, but using the fact that the type must be discrete and that
19138 -- both bounds must have the same type.
19139
19140 -- Character literals also have a universal type in the absence of
19141 -- of additional context, and are resolved to Standard_Character.
19142
19143 if Nkind (N) = N_Range then
19144
19145 -- The index is given by a range constraint. The bounds are known
19146 -- to be of a consistent type.
19147
19148 if not Is_Overloaded (N) then
19149 T := Etype (N);
19150
19151 -- For universal bounds, choose the specific predefined type
19152
19153 if T = Universal_Integer then
19154 T := Standard_Integer;
19155
19156 elsif T = Any_Character then
19157 Ambiguous_Character (Low_Bound (N));
19158
19159 T := Standard_Character;
19160 end if;
19161
19162 -- The node may be overloaded because some user-defined operators
19163 -- are available, but if a universal interpretation exists it is
19164 -- also the selected one.
19165
19166 elsif Universal_Interpretation (N) = Universal_Integer then
19167 T := Standard_Integer;
19168
19169 else
19170 T := Any_Type;
19171
19172 declare
19173 Ind : Interp_Index;
19174 It : Interp;
19175
19176 begin
19177 Get_First_Interp (N, Ind, It);
19178 while Present (It.Typ) loop
19179 if Is_Discrete_Type (It.Typ) then
19180
19181 if Found
19182 and then not Covers (It.Typ, T)
19183 and then not Covers (T, It.Typ)
19184 then
19185 Error_Msg_N ("ambiguous bounds in discrete range", N);
19186 exit;
19187 else
19188 T := It.Typ;
19189 Found := True;
19190 end if;
19191 end if;
19192
19193 Get_Next_Interp (Ind, It);
19194 end loop;
19195
19196 if T = Any_Type then
19197 Error_Msg_N ("discrete type required for range", N);
19198 Set_Etype (N, Any_Type);
19199 return;
19200
19201 elsif T = Universal_Integer then
19202 T := Standard_Integer;
19203 end if;
19204 end;
19205 end if;
19206
19207 if not Is_Discrete_Type (T) then
19208 Error_Msg_N ("discrete type required for range", N);
19209 Set_Etype (N, Any_Type);
19210 return;
19211 end if;
19212
19213 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19214 and then Attribute_Name (Low_Bound (N)) = Name_First
19215 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19216 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19217 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19218 then
19219 -- The type of the index will be the type of the prefix, as long
19220 -- as the upper bound is 'Last of the same type.
19221
19222 Def_Id := Entity (Prefix (Low_Bound (N)));
19223
19224 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19225 or else Attribute_Name (High_Bound (N)) /= Name_Last
19226 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19227 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19228 then
19229 Def_Id := Empty;
19230 end if;
19231 end if;
19232
19233 R := N;
19234 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19235
19236 elsif Nkind (N) = N_Subtype_Indication then
19237
19238 -- The index is given by a subtype with a range constraint
19239
19240 T := Base_Type (Entity (Subtype_Mark (N)));
19241
19242 if not Is_Discrete_Type (T) then
19243 Error_Msg_N ("discrete type required for range", N);
19244 Set_Etype (N, Any_Type);
19245 return;
19246 end if;
19247
19248 R := Range_Expression (Constraint (N));
19249
19250 Resolve (R, T);
19251 Process_Range_Expr_In_Decl
19252 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19253
19254 elsif Nkind (N) = N_Attribute_Reference then
19255
19256 -- Catch beginner's error (use of attribute other than 'Range)
19257
19258 if Attribute_Name (N) /= Name_Range then
19259 Error_Msg_N ("expect attribute ''Range", N);
19260 Set_Etype (N, Any_Type);
19261 return;
19262 end if;
19263
19264 -- If the node denotes the range of a type mark, that is also the
19265 -- resulting type, and we do not need to create an Itype for it.
19266
19267 if Is_Entity_Name (Prefix (N))
19268 and then Comes_From_Source (N)
19269 and then Is_Type (Entity (Prefix (N)))
19270 and then Is_Discrete_Type (Entity (Prefix (N)))
19271 then
19272 Def_Id := Entity (Prefix (N));
19273 end if;
19274
19275 Analyze_And_Resolve (N);
19276 T := Etype (N);
19277 R := N;
19278
19279 -- If none of the above, must be a subtype. We convert this to a
19280 -- range attribute reference because in the case of declared first
19281 -- named subtypes, the types in the range reference can be different
19282 -- from the type of the entity. A range attribute normalizes the
19283 -- reference and obtains the correct types for the bounds.
19284
19285 -- This transformation is in the nature of an expansion, is only
19286 -- done if expansion is active. In particular, it is not done on
19287 -- formal generic types, because we need to retain the name of the
19288 -- original index for instantiation purposes.
19289
19290 else
19291 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19292 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19293 Set_Etype (N, Any_Integer);
19294 return;
19295
19296 else
19297 -- The type mark may be that of an incomplete type. It is only
19298 -- now that we can get the full view, previous analysis does
19299 -- not look specifically for a type mark.
19300
19301 Set_Entity (N, Get_Full_View (Entity (N)));
19302 Set_Etype (N, Entity (N));
19303 Def_Id := Entity (N);
19304
19305 if not Is_Discrete_Type (Def_Id) then
19306 Error_Msg_N ("discrete type required for index", N);
19307 Set_Etype (N, Any_Type);
19308 return;
19309 end if;
19310 end if;
19311
19312 if Expander_Active then
19313 Rewrite (N,
19314 Make_Attribute_Reference (Sloc (N),
19315 Attribute_Name => Name_Range,
19316 Prefix => Relocate_Node (N)));
19317
19318 -- The original was a subtype mark that does not freeze. This
19319 -- means that the rewritten version must not freeze either.
19320
19321 Set_Must_Not_Freeze (N);
19322 Set_Must_Not_Freeze (Prefix (N));
19323 Analyze_And_Resolve (N);
19324 T := Etype (N);
19325 R := N;
19326
19327 -- If expander is inactive, type is legal, nothing else to construct
19328
19329 else
19330 return;
19331 end if;
19332 end if;
19333
19334 if not Is_Discrete_Type (T) then
19335 Error_Msg_N ("discrete type required for range", N);
19336 Set_Etype (N, Any_Type);
19337 return;
19338
19339 elsif T = Any_Type then
19340 Set_Etype (N, Any_Type);
19341 return;
19342 end if;
19343
19344 -- We will now create the appropriate Itype to describe the range, but
19345 -- first a check. If we originally had a subtype, then we just label
19346 -- the range with this subtype. Not only is there no need to construct
19347 -- a new subtype, but it is wrong to do so for two reasons:
19348
19349 -- 1. A legality concern, if we have a subtype, it must not freeze,
19350 -- and the Itype would cause freezing incorrectly
19351
19352 -- 2. An efficiency concern, if we created an Itype, it would not be
19353 -- recognized as the same type for the purposes of eliminating
19354 -- checks in some circumstances.
19355
19356 -- We signal this case by setting the subtype entity in Def_Id
19357
19358 if No (Def_Id) then
19359 Def_Id :=
19360 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19361 Set_Etype (Def_Id, Base_Type (T));
19362
19363 if Is_Signed_Integer_Type (T) then
19364 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19365
19366 elsif Is_Modular_Integer_Type (T) then
19367 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19368
19369 else
19370 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19371 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19372 Set_First_Literal (Def_Id, First_Literal (T));
19373 end if;
19374
19375 Set_Size_Info (Def_Id, (T));
19376 Set_RM_Size (Def_Id, RM_Size (T));
19377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19378
19379 Set_Scalar_Range (Def_Id, R);
19380 Conditional_Delay (Def_Id, T);
19381
19382 if Nkind (N) = N_Subtype_Indication then
19383 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19384 end if;
19385
19386 -- In the subtype indication case, if the immediate parent of the
19387 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19388 -- even if its bounds are static.
19389
19390 if Nkind (N) = N_Subtype_Indication
19391 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19392 then
19393 Set_Is_Non_Static_Subtype (Def_Id);
19394 end if;
19395 end if;
19396
19397 -- Final step is to label the index with this constructed type
19398
19399 Set_Etype (N, Def_Id);
19400 end Make_Index;
19401
19402 ------------------------------
19403 -- Modular_Type_Declaration --
19404 ------------------------------
19405
19406 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19407 Mod_Expr : constant Node_Id := Expression (Def);
19408 M_Val : Uint;
19409
19410 procedure Set_Modular_Size (Bits : Int);
19411 -- Sets RM_Size to Bits, and Esize to normal word size above this
19412
19413 ----------------------
19414 -- Set_Modular_Size --
19415 ----------------------
19416
19417 procedure Set_Modular_Size (Bits : Int) is
19418 begin
19419 Set_RM_Size (T, UI_From_Int (Bits));
19420
19421 if Bits <= 8 then
19422 Init_Esize (T, 8);
19423
19424 elsif Bits <= 16 then
19425 Init_Esize (T, 16);
19426
19427 elsif Bits <= 32 then
19428 Init_Esize (T, 32);
19429
19430 else
19431 Init_Esize (T, System_Max_Binary_Modulus_Power);
19432 end if;
19433
19434 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19435 Set_Is_Known_Valid (T);
19436 end if;
19437 end Set_Modular_Size;
19438
19439 -- Start of processing for Modular_Type_Declaration
19440
19441 begin
19442 -- If the mod expression is (exactly) 2 * literal, where literal is
19443 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19444
19445 if Warn_On_Suspicious_Modulus_Value
19446 and then Nkind (Mod_Expr) = N_Op_Multiply
19447 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19448 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19449 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19450 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19451 then
19452 Error_Msg_N
19453 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19454 end if;
19455
19456 -- Proceed with analysis of mod expression
19457
19458 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19459 Set_Etype (T, T);
19460 Set_Ekind (T, E_Modular_Integer_Type);
19461 Init_Alignment (T);
19462 Set_Is_Constrained (T);
19463
19464 if not Is_OK_Static_Expression (Mod_Expr) then
19465 Flag_Non_Static_Expr
19466 ("non-static expression used for modular type bound!", Mod_Expr);
19467 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19468 else
19469 M_Val := Expr_Value (Mod_Expr);
19470 end if;
19471
19472 if M_Val < 1 then
19473 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19474 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19475 end if;
19476
19477 if M_Val > 2 ** Standard_Long_Integer_Size then
19478 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19479 end if;
19480
19481 Set_Modulus (T, M_Val);
19482
19483 -- Create bounds for the modular type based on the modulus given in
19484 -- the type declaration and then analyze and resolve those bounds.
19485
19486 Set_Scalar_Range (T,
19487 Make_Range (Sloc (Mod_Expr),
19488 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19489 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19490
19491 -- Properly analyze the literals for the range. We do this manually
19492 -- because we can't go calling Resolve, since we are resolving these
19493 -- bounds with the type, and this type is certainly not complete yet.
19494
19495 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19496 Set_Etype (High_Bound (Scalar_Range (T)), T);
19497 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19498 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19499
19500 -- Loop through powers of two to find number of bits required
19501
19502 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19503
19504 -- Binary case
19505
19506 if M_Val = 2 ** Bits then
19507 Set_Modular_Size (Bits);
19508 return;
19509
19510 -- Nonbinary case
19511
19512 elsif M_Val < 2 ** Bits then
19513 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19514 Set_Non_Binary_Modulus (T);
19515
19516 if Bits > System_Max_Nonbinary_Modulus_Power then
19517 Error_Msg_Uint_1 :=
19518 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19519 Error_Msg_F
19520 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19521 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19522 return;
19523
19524 else
19525 -- In the nonbinary case, set size as per RM 13.3(55)
19526
19527 Set_Modular_Size (Bits);
19528 return;
19529 end if;
19530 end if;
19531
19532 end loop;
19533
19534 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19535 -- so we just signal an error and set the maximum size.
19536
19537 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19538 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19539
19540 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19541 Init_Alignment (T);
19542
19543 end Modular_Type_Declaration;
19544
19545 --------------------------
19546 -- New_Concatenation_Op --
19547 --------------------------
19548
19549 procedure New_Concatenation_Op (Typ : Entity_Id) is
19550 Loc : constant Source_Ptr := Sloc (Typ);
19551 Op : Entity_Id;
19552
19553 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19554 -- Create abbreviated declaration for the formal of a predefined
19555 -- Operator 'Op' of type 'Typ'
19556
19557 --------------------
19558 -- Make_Op_Formal --
19559 --------------------
19560
19561 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19562 Formal : Entity_Id;
19563 begin
19564 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19565 Set_Etype (Formal, Typ);
19566 Set_Mechanism (Formal, Default_Mechanism);
19567 return Formal;
19568 end Make_Op_Formal;
19569
19570 -- Start of processing for New_Concatenation_Op
19571
19572 begin
19573 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19574
19575 Set_Ekind (Op, E_Operator);
19576 Set_Scope (Op, Current_Scope);
19577 Set_Etype (Op, Typ);
19578 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19579 Set_Is_Immediately_Visible (Op);
19580 Set_Is_Intrinsic_Subprogram (Op);
19581 Set_Has_Completion (Op);
19582 Append_Entity (Op, Current_Scope);
19583
19584 Set_Name_Entity_Id (Name_Op_Concat, Op);
19585
19586 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19587 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19588 end New_Concatenation_Op;
19589
19590 -------------------------
19591 -- OK_For_Limited_Init --
19592 -------------------------
19593
19594 -- ???Check all calls of this, and compare the conditions under which it's
19595 -- called.
19596
19597 function OK_For_Limited_Init
19598 (Typ : Entity_Id;
19599 Exp : Node_Id) return Boolean
19600 is
19601 begin
19602 return Is_CPP_Constructor_Call (Exp)
19603 or else (Ada_Version >= Ada_2005
19604 and then not Debug_Flag_Dot_L
19605 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19606 end OK_For_Limited_Init;
19607
19608 -------------------------------
19609 -- OK_For_Limited_Init_In_05 --
19610 -------------------------------
19611
19612 function OK_For_Limited_Init_In_05
19613 (Typ : Entity_Id;
19614 Exp : Node_Id) return Boolean
19615 is
19616 begin
19617 -- An object of a limited interface type can be initialized with any
19618 -- expression of a nonlimited descendant type. However this does not
19619 -- apply if this is a view conversion of some other expression. This
19620 -- is checked below.
19621
19622 if Is_Class_Wide_Type (Typ)
19623 and then Is_Limited_Interface (Typ)
19624 and then not Is_Limited_Type (Etype (Exp))
19625 and then Nkind (Exp) /= N_Type_Conversion
19626 then
19627 return True;
19628 end if;
19629
19630 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19631 -- case of limited aggregates (including extension aggregates), and
19632 -- function calls. The function call may have been given in prefixed
19633 -- notation, in which case the original node is an indexed component.
19634 -- If the function is parameterless, the original node was an explicit
19635 -- dereference. The function may also be parameterless, in which case
19636 -- the source node is just an identifier.
19637
19638 -- A branch of a conditional expression may have been removed if the
19639 -- condition is statically known. This happens during expansion, and
19640 -- thus will not happen if previous errors were encountered. The check
19641 -- will have been performed on the chosen branch, which replaces the
19642 -- original conditional expression.
19643
19644 if No (Exp) then
19645 return True;
19646 end if;
19647
19648 case Nkind (Original_Node (Exp)) is
19649 when N_Aggregate
19650 | N_Extension_Aggregate
19651 | N_Function_Call
19652 | N_Op
19653 =>
19654 return True;
19655
19656 when N_Identifier =>
19657 return Present (Entity (Original_Node (Exp)))
19658 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19659
19660 when N_Qualified_Expression =>
19661 return
19662 OK_For_Limited_Init_In_05
19663 (Typ, Expression (Original_Node (Exp)));
19664
19665 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19666 -- with a function call, the expander has rewritten the call into an
19667 -- N_Type_Conversion node to force displacement of the pointer to
19668 -- reference the component containing the secondary dispatch table.
19669 -- Otherwise a type conversion is not a legal context.
19670 -- A return statement for a build-in-place function returning a
19671 -- synchronized type also introduces an unchecked conversion.
19672
19673 when N_Type_Conversion
19674 | N_Unchecked_Type_Conversion
19675 =>
19676 return not Comes_From_Source (Exp)
19677 and then
19678 -- If the conversion has been rewritten, check Original_Node
19679
19680 ((Original_Node (Exp) /= Exp
19681 and then
19682 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
19683
19684 -- Otherwise, check the expression of the compiler-generated
19685 -- conversion (which is a conversion that we want to ignore
19686 -- for purposes of the limited-initialization restrictions).
19687
19688 or else
19689 (Original_Node (Exp) = Exp
19690 and then
19691 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
19692
19693 when N_Explicit_Dereference
19694 | N_Indexed_Component
19695 | N_Selected_Component
19696 =>
19697 return Nkind (Exp) = N_Function_Call;
19698
19699 -- A use of 'Input is a function call, hence allowed. Normally the
19700 -- attribute will be changed to a call, but the attribute by itself
19701 -- can occur with -gnatc.
19702
19703 when N_Attribute_Reference =>
19704 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19705
19706 -- "return raise ..." is OK
19707
19708 when N_Raise_Expression =>
19709 return True;
19710
19711 -- For a case expression, all dependent expressions must be legal
19712
19713 when N_Case_Expression =>
19714 declare
19715 Alt : Node_Id;
19716
19717 begin
19718 Alt := First (Alternatives (Original_Node (Exp)));
19719 while Present (Alt) loop
19720 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19721 return False;
19722 end if;
19723
19724 Next (Alt);
19725 end loop;
19726
19727 return True;
19728 end;
19729
19730 -- For an if expression, all dependent expressions must be legal
19731
19732 when N_If_Expression =>
19733 declare
19734 Then_Expr : constant Node_Id :=
19735 Next (First (Expressions (Original_Node (Exp))));
19736 Else_Expr : constant Node_Id := Next (Then_Expr);
19737 begin
19738 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19739 and then
19740 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19741 end;
19742
19743 when others =>
19744 return False;
19745 end case;
19746 end OK_For_Limited_Init_In_05;
19747
19748 -------------------------------------------
19749 -- Ordinary_Fixed_Point_Type_Declaration --
19750 -------------------------------------------
19751
19752 procedure Ordinary_Fixed_Point_Type_Declaration
19753 (T : Entity_Id;
19754 Def : Node_Id)
19755 is
19756 Loc : constant Source_Ptr := Sloc (Def);
19757 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19758 RRS : constant Node_Id := Real_Range_Specification (Def);
19759 Implicit_Base : Entity_Id;
19760 Delta_Val : Ureal;
19761 Small_Val : Ureal;
19762 Low_Val : Ureal;
19763 High_Val : Ureal;
19764
19765 begin
19766 Check_Restriction (No_Fixed_Point, Def);
19767
19768 -- Create implicit base type
19769
19770 Implicit_Base :=
19771 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19772 Set_Etype (Implicit_Base, Implicit_Base);
19773
19774 -- Analyze and process delta expression
19775
19776 Analyze_And_Resolve (Delta_Expr, Any_Real);
19777
19778 Check_Delta_Expression (Delta_Expr);
19779 Delta_Val := Expr_Value_R (Delta_Expr);
19780
19781 Set_Delta_Value (Implicit_Base, Delta_Val);
19782
19783 -- Compute default small from given delta, which is the largest power
19784 -- of two that does not exceed the given delta value.
19785
19786 declare
19787 Tmp : Ureal;
19788 Scale : Int;
19789
19790 begin
19791 Tmp := Ureal_1;
19792 Scale := 0;
19793
19794 if Delta_Val < Ureal_1 then
19795 while Delta_Val < Tmp loop
19796 Tmp := Tmp / Ureal_2;
19797 Scale := Scale + 1;
19798 end loop;
19799
19800 else
19801 loop
19802 Tmp := Tmp * Ureal_2;
19803 exit when Tmp > Delta_Val;
19804 Scale := Scale - 1;
19805 end loop;
19806 end if;
19807
19808 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19809 end;
19810
19811 Set_Small_Value (Implicit_Base, Small_Val);
19812
19813 -- If no range was given, set a dummy range
19814
19815 if RRS <= Empty_Or_Error then
19816 Low_Val := -Small_Val;
19817 High_Val := Small_Val;
19818
19819 -- Otherwise analyze and process given range
19820
19821 else
19822 declare
19823 Low : constant Node_Id := Low_Bound (RRS);
19824 High : constant Node_Id := High_Bound (RRS);
19825
19826 begin
19827 Analyze_And_Resolve (Low, Any_Real);
19828 Analyze_And_Resolve (High, Any_Real);
19829 Check_Real_Bound (Low);
19830 Check_Real_Bound (High);
19831
19832 -- Obtain and set the range
19833
19834 Low_Val := Expr_Value_R (Low);
19835 High_Val := Expr_Value_R (High);
19836
19837 if Low_Val > High_Val then
19838 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19839 end if;
19840 end;
19841 end if;
19842
19843 -- The range for both the implicit base and the declared first subtype
19844 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19845 -- set a temporary range in place. Note that the bounds of the base
19846 -- type will be widened to be symmetrical and to fill the available
19847 -- bits when the type is frozen.
19848
19849 -- We could do this with all discrete types, and probably should, but
19850 -- we absolutely have to do it for fixed-point, since the end-points
19851 -- of the range and the size are determined by the small value, which
19852 -- could be reset before the freeze point.
19853
19854 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19855 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19856
19857 -- Complete definition of first subtype. The inheritance of the rep item
19858 -- chain ensures that SPARK-related pragmas are not clobbered when the
19859 -- ordinary fixed point type acts as a full view of a private type.
19860
19861 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19862 Set_Etype (T, Implicit_Base);
19863 Init_Size_Align (T);
19864 Inherit_Rep_Item_Chain (T, Implicit_Base);
19865 Set_Small_Value (T, Small_Val);
19866 Set_Delta_Value (T, Delta_Val);
19867 Set_Is_Constrained (T);
19868 end Ordinary_Fixed_Point_Type_Declaration;
19869
19870 ----------------------------------
19871 -- Preanalyze_Assert_Expression --
19872 ----------------------------------
19873
19874 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19875 begin
19876 In_Assertion_Expr := In_Assertion_Expr + 1;
19877 Preanalyze_Spec_Expression (N, T);
19878 In_Assertion_Expr := In_Assertion_Expr - 1;
19879 end Preanalyze_Assert_Expression;
19880
19881 -----------------------------------
19882 -- Preanalyze_Default_Expression --
19883 -----------------------------------
19884
19885 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19886 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19887 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19888
19889 begin
19890 In_Default_Expr := True;
19891 In_Spec_Expression := True;
19892
19893 Preanalyze_With_Freezing_And_Resolve (N, T);
19894
19895 In_Default_Expr := Save_In_Default_Expr;
19896 In_Spec_Expression := Save_In_Spec_Expression;
19897 end Preanalyze_Default_Expression;
19898
19899 --------------------------------
19900 -- Preanalyze_Spec_Expression --
19901 --------------------------------
19902
19903 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19904 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19905 begin
19906 In_Spec_Expression := True;
19907 Preanalyze_And_Resolve (N, T);
19908 In_Spec_Expression := Save_In_Spec_Expression;
19909 end Preanalyze_Spec_Expression;
19910
19911 ----------------------------------------
19912 -- Prepare_Private_Subtype_Completion --
19913 ----------------------------------------
19914
19915 procedure Prepare_Private_Subtype_Completion
19916 (Id : Entity_Id;
19917 Related_Nod : Node_Id)
19918 is
19919 Id_B : constant Entity_Id := Base_Type (Id);
19920 Full_B : constant Entity_Id := Full_View (Id_B);
19921 Full : Entity_Id;
19922
19923 begin
19924 if Present (Full_B) then
19925
19926 -- The Base_Type is already completed, we can complete the subtype
19927 -- now. We have to create a new entity with the same name, Thus we
19928 -- can't use Create_Itype.
19929
19930 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19931 Set_Is_Itype (Full);
19932 Set_Associated_Node_For_Itype (Full, Related_Nod);
19933 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19934 Set_Full_View (Id, Full);
19935 end if;
19936
19937 -- The parent subtype may be private, but the base might not, in some
19938 -- nested instances. In that case, the subtype does not need to be
19939 -- exchanged. It would still be nice to make private subtypes and their
19940 -- bases consistent at all times ???
19941
19942 if Is_Private_Type (Id_B) then
19943 Append_Elmt (Id, Private_Dependents (Id_B));
19944 end if;
19945 end Prepare_Private_Subtype_Completion;
19946
19947 ---------------------------
19948 -- Process_Discriminants --
19949 ---------------------------
19950
19951 procedure Process_Discriminants
19952 (N : Node_Id;
19953 Prev : Entity_Id := Empty)
19954 is
19955 Elist : constant Elist_Id := New_Elmt_List;
19956 Id : Node_Id;
19957 Discr : Node_Id;
19958 Discr_Number : Uint;
19959 Discr_Type : Entity_Id;
19960 Default_Present : Boolean := False;
19961 Default_Not_Present : Boolean := False;
19962
19963 begin
19964 -- A composite type other than an array type can have discriminants.
19965 -- On entry, the current scope is the composite type.
19966
19967 -- The discriminants are initially entered into the scope of the type
19968 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19969 -- use, as explained at the end of this procedure.
19970
19971 Discr := First (Discriminant_Specifications (N));
19972 while Present (Discr) loop
19973 Enter_Name (Defining_Identifier (Discr));
19974
19975 -- For navigation purposes we add a reference to the discriminant
19976 -- in the entity for the type. If the current declaration is a
19977 -- completion, place references on the partial view. Otherwise the
19978 -- type is the current scope.
19979
19980 if Present (Prev) then
19981
19982 -- The references go on the partial view, if present. If the
19983 -- partial view has discriminants, the references have been
19984 -- generated already.
19985
19986 if not Has_Discriminants (Prev) then
19987 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19988 end if;
19989 else
19990 Generate_Reference
19991 (Current_Scope, Defining_Identifier (Discr), 'd');
19992 end if;
19993
19994 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19995 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19996
19997 -- Ada 2005 (AI-254)
19998
19999 if Present (Access_To_Subprogram_Definition
20000 (Discriminant_Type (Discr)))
20001 and then Protected_Present (Access_To_Subprogram_Definition
20002 (Discriminant_Type (Discr)))
20003 then
20004 Discr_Type :=
20005 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20006 end if;
20007
20008 else
20009 Find_Type (Discriminant_Type (Discr));
20010 Discr_Type := Etype (Discriminant_Type (Discr));
20011
20012 if Error_Posted (Discriminant_Type (Discr)) then
20013 Discr_Type := Any_Type;
20014 end if;
20015 end if;
20016
20017 -- Handling of discriminants that are access types
20018
20019 if Is_Access_Type (Discr_Type) then
20020
20021 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20022 -- limited record types
20023
20024 if Ada_Version < Ada_2005 then
20025 Check_Access_Discriminant_Requires_Limited
20026 (Discr, Discriminant_Type (Discr));
20027 end if;
20028
20029 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20030 Error_Msg_N
20031 ("(Ada 83) access discriminant not allowed", Discr);
20032 end if;
20033
20034 -- If not access type, must be a discrete type
20035
20036 elsif not Is_Discrete_Type (Discr_Type) then
20037 Error_Msg_N
20038 ("discriminants must have a discrete or access type",
20039 Discriminant_Type (Discr));
20040 end if;
20041
20042 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20043
20044 -- If a discriminant specification includes the assignment compound
20045 -- delimiter followed by an expression, the expression is the default
20046 -- expression of the discriminant; the default expression must be of
20047 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20048 -- a default expression, we do the special preanalysis, since this
20049 -- expression does not freeze (see section "Handling of Default and
20050 -- Per-Object Expressions" in spec of package Sem).
20051
20052 if Present (Expression (Discr)) then
20053 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
20054
20055 -- Legaity checks
20056
20057 if Nkind (N) = N_Formal_Type_Declaration then
20058 Error_Msg_N
20059 ("discriminant defaults not allowed for formal type",
20060 Expression (Discr));
20061
20062 -- Flag an error for a tagged type with defaulted discriminants,
20063 -- excluding limited tagged types when compiling for Ada 2012
20064 -- (see AI05-0214).
20065
20066 elsif Is_Tagged_Type (Current_Scope)
20067 and then (not Is_Limited_Type (Current_Scope)
20068 or else Ada_Version < Ada_2012)
20069 and then Comes_From_Source (N)
20070 then
20071 -- Note: see similar test in Check_Or_Process_Discriminants, to
20072 -- handle the (illegal) case of the completion of an untagged
20073 -- view with discriminants with defaults by a tagged full view.
20074 -- We skip the check if Discr does not come from source, to
20075 -- account for the case of an untagged derived type providing
20076 -- defaults for a renamed discriminant from a private untagged
20077 -- ancestor with a tagged full view (ACATS B460006).
20078
20079 if Ada_Version >= Ada_2012 then
20080 Error_Msg_N
20081 ("discriminants of nonlimited tagged type cannot have"
20082 & " defaults",
20083 Expression (Discr));
20084 else
20085 Error_Msg_N
20086 ("discriminants of tagged type cannot have defaults",
20087 Expression (Discr));
20088 end if;
20089
20090 else
20091 Default_Present := True;
20092 Append_Elmt (Expression (Discr), Elist);
20093
20094 -- Tag the defining identifiers for the discriminants with
20095 -- their corresponding default expressions from the tree.
20096
20097 Set_Discriminant_Default_Value
20098 (Defining_Identifier (Discr), Expression (Discr));
20099 end if;
20100
20101 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20102 -- gets set unless we can be sure that no range check is required.
20103
20104 if (GNATprove_Mode or not Expander_Active)
20105 and then not
20106 Is_In_Range
20107 (Expression (Discr), Discr_Type, Assume_Valid => True)
20108 then
20109 Set_Do_Range_Check (Expression (Discr));
20110 end if;
20111
20112 -- No default discriminant value given
20113
20114 else
20115 Default_Not_Present := True;
20116 end if;
20117
20118 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20119 -- Discr_Type but with the null-exclusion attribute
20120
20121 if Ada_Version >= Ada_2005 then
20122
20123 -- Ada 2005 (AI-231): Static checks
20124
20125 if Can_Never_Be_Null (Discr_Type) then
20126 Null_Exclusion_Static_Checks (Discr);
20127
20128 elsif Is_Access_Type (Discr_Type)
20129 and then Null_Exclusion_Present (Discr)
20130
20131 -- No need to check itypes because in their case this check
20132 -- was done at their point of creation
20133
20134 and then not Is_Itype (Discr_Type)
20135 then
20136 if Can_Never_Be_Null (Discr_Type) then
20137 Error_Msg_NE
20138 ("`NOT NULL` not allowed (& already excludes null)",
20139 Discr,
20140 Discr_Type);
20141 end if;
20142
20143 Set_Etype (Defining_Identifier (Discr),
20144 Create_Null_Excluding_Itype
20145 (T => Discr_Type,
20146 Related_Nod => Discr));
20147
20148 -- Check for improper null exclusion if the type is otherwise
20149 -- legal for a discriminant.
20150
20151 elsif Null_Exclusion_Present (Discr)
20152 and then Is_Discrete_Type (Discr_Type)
20153 then
20154 Error_Msg_N
20155 ("null exclusion can only apply to an access type", Discr);
20156 end if;
20157
20158 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20159 -- can't have defaults. Synchronized types, or types that are
20160 -- explicitly limited are fine, but special tests apply to derived
20161 -- types in generics: in a generic body we have to assume the
20162 -- worst, and therefore defaults are not allowed if the parent is
20163 -- a generic formal private type (see ACATS B370001).
20164
20165 if Is_Access_Type (Discr_Type) and then Default_Present then
20166 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20167 or else Is_Limited_Record (Current_Scope)
20168 or else Is_Concurrent_Type (Current_Scope)
20169 or else Is_Concurrent_Record_Type (Current_Scope)
20170 or else Ekind (Current_Scope) = E_Limited_Private_Type
20171 then
20172 if not Is_Derived_Type (Current_Scope)
20173 or else not Is_Generic_Type (Etype (Current_Scope))
20174 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20175 or else Limited_Present
20176 (Type_Definition (Parent (Current_Scope)))
20177 then
20178 null;
20179
20180 else
20181 Error_Msg_N
20182 ("access discriminants of nonlimited types cannot "
20183 & "have defaults", Expression (Discr));
20184 end if;
20185
20186 elsif Present (Expression (Discr)) then
20187 Error_Msg_N
20188 ("(Ada 2005) access discriminants of nonlimited types "
20189 & "cannot have defaults", Expression (Discr));
20190 end if;
20191 end if;
20192 end if;
20193
20194 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20195 -- This check is relevant only when SPARK_Mode is on as it is not a
20196 -- standard Ada legality rule.
20197
20198 if SPARK_Mode = On
20199 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20200 then
20201 Error_Msg_N ("discriminant cannot be volatile", Discr);
20202 end if;
20203
20204 Next (Discr);
20205 end loop;
20206
20207 -- An element list consisting of the default expressions of the
20208 -- discriminants is constructed in the above loop and used to set
20209 -- the Discriminant_Constraint attribute for the type. If an object
20210 -- is declared of this (record or task) type without any explicit
20211 -- discriminant constraint given, this element list will form the
20212 -- actual parameters for the corresponding initialization procedure
20213 -- for the type.
20214
20215 Set_Discriminant_Constraint (Current_Scope, Elist);
20216 Set_Stored_Constraint (Current_Scope, No_Elist);
20217
20218 -- Default expressions must be provided either for all or for none
20219 -- of the discriminants of a discriminant part. (RM 3.7.1)
20220
20221 if Default_Present and then Default_Not_Present then
20222 Error_Msg_N
20223 ("incomplete specification of defaults for discriminants", N);
20224 end if;
20225
20226 -- The use of the name of a discriminant is not allowed in default
20227 -- expressions of a discriminant part if the specification of the
20228 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20229
20230 -- To detect this, the discriminant names are entered initially with an
20231 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20232 -- attempt to use a void entity (for example in an expression that is
20233 -- type-checked) produces the error message: premature usage. Now after
20234 -- completing the semantic analysis of the discriminant part, we can set
20235 -- the Ekind of all the discriminants appropriately.
20236
20237 Discr := First (Discriminant_Specifications (N));
20238 Discr_Number := Uint_1;
20239 while Present (Discr) loop
20240 Id := Defining_Identifier (Discr);
20241 Set_Ekind (Id, E_Discriminant);
20242 Init_Component_Location (Id);
20243 Init_Esize (Id);
20244 Set_Discriminant_Number (Id, Discr_Number);
20245
20246 -- Make sure this is always set, even in illegal programs
20247
20248 Set_Corresponding_Discriminant (Id, Empty);
20249
20250 -- Initialize the Original_Record_Component to the entity itself.
20251 -- Inherit_Components will propagate the right value to
20252 -- discriminants in derived record types.
20253
20254 Set_Original_Record_Component (Id, Id);
20255
20256 -- Create the discriminal for the discriminant
20257
20258 Build_Discriminal (Id);
20259
20260 Next (Discr);
20261 Discr_Number := Discr_Number + 1;
20262 end loop;
20263
20264 Set_Has_Discriminants (Current_Scope);
20265 end Process_Discriminants;
20266
20267 -----------------------
20268 -- Process_Full_View --
20269 -----------------------
20270
20271 -- WARNING: This routine manages Ghost regions. Return statements must be
20272 -- replaced by gotos which jump to the end of the routine and restore the
20273 -- Ghost mode.
20274
20275 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20276 procedure Collect_Implemented_Interfaces
20277 (Typ : Entity_Id;
20278 Ifaces : Elist_Id);
20279 -- Ada 2005: Gather all the interfaces that Typ directly or
20280 -- inherently implements. Duplicate entries are not added to
20281 -- the list Ifaces.
20282
20283 ------------------------------------
20284 -- Collect_Implemented_Interfaces --
20285 ------------------------------------
20286
20287 procedure Collect_Implemented_Interfaces
20288 (Typ : Entity_Id;
20289 Ifaces : Elist_Id)
20290 is
20291 Iface : Entity_Id;
20292 Iface_Elmt : Elmt_Id;
20293
20294 begin
20295 -- Abstract interfaces are only associated with tagged record types
20296
20297 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20298 return;
20299 end if;
20300
20301 -- Recursively climb to the ancestors
20302
20303 if Etype (Typ) /= Typ
20304
20305 -- Protect the frontend against wrong cyclic declarations like:
20306
20307 -- type B is new A with private;
20308 -- type C is new A with private;
20309 -- private
20310 -- type B is new C with null record;
20311 -- type C is new B with null record;
20312
20313 and then Etype (Typ) /= Priv_T
20314 and then Etype (Typ) /= Full_T
20315 then
20316 -- Keep separate the management of private type declarations
20317
20318 if Ekind (Typ) = E_Record_Type_With_Private then
20319
20320 -- Handle the following illegal usage:
20321 -- type Private_Type is tagged private;
20322 -- private
20323 -- type Private_Type is new Type_Implementing_Iface;
20324
20325 if Present (Full_View (Typ))
20326 and then Etype (Typ) /= Full_View (Typ)
20327 then
20328 if Is_Interface (Etype (Typ)) then
20329 Append_Unique_Elmt (Etype (Typ), Ifaces);
20330 end if;
20331
20332 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20333 end if;
20334
20335 -- Non-private types
20336
20337 else
20338 if Is_Interface (Etype (Typ)) then
20339 Append_Unique_Elmt (Etype (Typ), Ifaces);
20340 end if;
20341
20342 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20343 end if;
20344 end if;
20345
20346 -- Handle entities in the list of abstract interfaces
20347
20348 if Present (Interfaces (Typ)) then
20349 Iface_Elmt := First_Elmt (Interfaces (Typ));
20350 while Present (Iface_Elmt) loop
20351 Iface := Node (Iface_Elmt);
20352
20353 pragma Assert (Is_Interface (Iface));
20354
20355 if not Contain_Interface (Iface, Ifaces) then
20356 Append_Elmt (Iface, Ifaces);
20357 Collect_Implemented_Interfaces (Iface, Ifaces);
20358 end if;
20359
20360 Next_Elmt (Iface_Elmt);
20361 end loop;
20362 end if;
20363 end Collect_Implemented_Interfaces;
20364
20365 -- Local variables
20366
20367 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20368 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20369 -- Save the Ghost-related attributes to restore on exit
20370
20371 Full_Indic : Node_Id;
20372 Full_Parent : Entity_Id;
20373 Priv_Parent : Entity_Id;
20374
20375 -- Start of processing for Process_Full_View
20376
20377 begin
20378 Mark_And_Set_Ghost_Completion (N, Priv_T);
20379
20380 -- First some sanity checks that must be done after semantic
20381 -- decoration of the full view and thus cannot be placed with other
20382 -- similar checks in Find_Type_Name
20383
20384 if not Is_Limited_Type (Priv_T)
20385 and then (Is_Limited_Type (Full_T)
20386 or else Is_Limited_Composite (Full_T))
20387 then
20388 if In_Instance then
20389 null;
20390 else
20391 Error_Msg_N
20392 ("completion of nonlimited type cannot be limited", Full_T);
20393 Explain_Limited_Type (Full_T, Full_T);
20394 end if;
20395
20396 elsif Is_Abstract_Type (Full_T)
20397 and then not Is_Abstract_Type (Priv_T)
20398 then
20399 Error_Msg_N
20400 ("completion of nonabstract type cannot be abstract", Full_T);
20401
20402 elsif Is_Tagged_Type (Priv_T)
20403 and then Is_Limited_Type (Priv_T)
20404 and then not Is_Limited_Type (Full_T)
20405 then
20406 -- If pragma CPP_Class was applied to the private declaration
20407 -- propagate the limitedness to the full-view
20408
20409 if Is_CPP_Class (Priv_T) then
20410 Set_Is_Limited_Record (Full_T);
20411
20412 -- GNAT allow its own definition of Limited_Controlled to disobey
20413 -- this rule in order in ease the implementation. This test is safe
20414 -- because Root_Controlled is defined in a child of System that
20415 -- normal programs are not supposed to use.
20416
20417 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20418 Set_Is_Limited_Composite (Full_T);
20419 else
20420 Error_Msg_N
20421 ("completion of limited tagged type must be limited", Full_T);
20422 end if;
20423
20424 elsif Is_Generic_Type (Priv_T) then
20425 Error_Msg_N ("generic type cannot have a completion", Full_T);
20426 end if;
20427
20428 -- Check that ancestor interfaces of private and full views are
20429 -- consistent. We omit this check for synchronized types because
20430 -- they are performed on the corresponding record type when frozen.
20431
20432 if Ada_Version >= Ada_2005
20433 and then Is_Tagged_Type (Priv_T)
20434 and then Is_Tagged_Type (Full_T)
20435 and then not Is_Concurrent_Type (Full_T)
20436 then
20437 declare
20438 Iface : Entity_Id;
20439 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20440 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20441
20442 begin
20443 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20444 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20445
20446 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20447 -- an interface type if and only if the full type is descendant
20448 -- of the interface type (AARM 7.3 (7.3/2)).
20449
20450 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20451
20452 if Present (Iface) then
20453 Error_Msg_NE
20454 ("interface in partial view& not implemented by full type "
20455 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20456 end if;
20457
20458 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20459
20460 if Present (Iface) then
20461 Error_Msg_NE
20462 ("interface & not implemented by partial view "
20463 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20464 end if;
20465 end;
20466 end if;
20467
20468 if Is_Tagged_Type (Priv_T)
20469 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20470 and then Is_Derived_Type (Full_T)
20471 then
20472 Priv_Parent := Etype (Priv_T);
20473
20474 -- The full view of a private extension may have been transformed
20475 -- into an unconstrained derived type declaration and a subtype
20476 -- declaration (see build_derived_record_type for details).
20477
20478 if Nkind (N) = N_Subtype_Declaration then
20479 Full_Indic := Subtype_Indication (N);
20480 Full_Parent := Etype (Base_Type (Full_T));
20481 else
20482 Full_Indic := Subtype_Indication (Type_Definition (N));
20483 Full_Parent := Etype (Full_T);
20484 end if;
20485
20486 -- Check that the parent type of the full type is a descendant of
20487 -- the ancestor subtype given in the private extension. If either
20488 -- entity has an Etype equal to Any_Type then we had some previous
20489 -- error situation [7.3(8)].
20490
20491 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20492 goto Leave;
20493
20494 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20495 -- any order. Therefore we don't have to check that its parent must
20496 -- be a descendant of the parent of the private type declaration.
20497
20498 elsif Is_Interface (Priv_Parent)
20499 and then Is_Interface (Full_Parent)
20500 then
20501 null;
20502
20503 -- Ada 2005 (AI-251): If the parent of the private type declaration
20504 -- is an interface there is no need to check that it is an ancestor
20505 -- of the associated full type declaration. The required tests for
20506 -- this case are performed by Build_Derived_Record_Type.
20507
20508 elsif not Is_Interface (Base_Type (Priv_Parent))
20509 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20510 then
20511 Error_Msg_N
20512 ("parent of full type must descend from parent of private "
20513 & "extension", Full_Indic);
20514
20515 -- First check a formal restriction, and then proceed with checking
20516 -- Ada rules. Since the formal restriction is not a serious error, we
20517 -- don't prevent further error detection for this check, hence the
20518 -- ELSE.
20519
20520 else
20521 -- In formal mode, when completing a private extension the type
20522 -- named in the private part must be exactly the same as that
20523 -- named in the visible part.
20524
20525 if Priv_Parent /= Full_Parent then
20526 Error_Msg_Name_1 := Chars (Priv_Parent);
20527 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20528 end if;
20529
20530 -- Check the rules of 7.3(10): if the private extension inherits
20531 -- known discriminants, then the full type must also inherit those
20532 -- discriminants from the same (ancestor) type, and the parent
20533 -- subtype of the full type must be constrained if and only if
20534 -- the ancestor subtype of the private extension is constrained.
20535
20536 if No (Discriminant_Specifications (Parent (Priv_T)))
20537 and then not Has_Unknown_Discriminants (Priv_T)
20538 and then Has_Discriminants (Base_Type (Priv_Parent))
20539 then
20540 declare
20541 Priv_Indic : constant Node_Id :=
20542 Subtype_Indication (Parent (Priv_T));
20543
20544 Priv_Constr : constant Boolean :=
20545 Is_Constrained (Priv_Parent)
20546 or else
20547 Nkind (Priv_Indic) = N_Subtype_Indication
20548 or else
20549 Is_Constrained (Entity (Priv_Indic));
20550
20551 Full_Constr : constant Boolean :=
20552 Is_Constrained (Full_Parent)
20553 or else
20554 Nkind (Full_Indic) = N_Subtype_Indication
20555 or else
20556 Is_Constrained (Entity (Full_Indic));
20557
20558 Priv_Discr : Entity_Id;
20559 Full_Discr : Entity_Id;
20560
20561 begin
20562 Priv_Discr := First_Discriminant (Priv_Parent);
20563 Full_Discr := First_Discriminant (Full_Parent);
20564 while Present (Priv_Discr) and then Present (Full_Discr) loop
20565 if Original_Record_Component (Priv_Discr) =
20566 Original_Record_Component (Full_Discr)
20567 or else
20568 Corresponding_Discriminant (Priv_Discr) =
20569 Corresponding_Discriminant (Full_Discr)
20570 then
20571 null;
20572 else
20573 exit;
20574 end if;
20575
20576 Next_Discriminant (Priv_Discr);
20577 Next_Discriminant (Full_Discr);
20578 end loop;
20579
20580 if Present (Priv_Discr) or else Present (Full_Discr) then
20581 Error_Msg_N
20582 ("full view must inherit discriminants of the parent "
20583 & "type used in the private extension", Full_Indic);
20584
20585 elsif Priv_Constr and then not Full_Constr then
20586 Error_Msg_N
20587 ("parent subtype of full type must be constrained",
20588 Full_Indic);
20589
20590 elsif Full_Constr and then not Priv_Constr then
20591 Error_Msg_N
20592 ("parent subtype of full type must be unconstrained",
20593 Full_Indic);
20594 end if;
20595 end;
20596
20597 -- Check the rules of 7.3(12): if a partial view has neither
20598 -- known or unknown discriminants, then the full type
20599 -- declaration shall define a definite subtype.
20600
20601 elsif not Has_Unknown_Discriminants (Priv_T)
20602 and then not Has_Discriminants (Priv_T)
20603 and then not Is_Constrained (Full_T)
20604 then
20605 Error_Msg_N
20606 ("full view must define a constrained type if partial view "
20607 & "has no discriminants", Full_T);
20608 end if;
20609
20610 -- ??????? Do we implement the following properly ?????
20611 -- If the ancestor subtype of a private extension has constrained
20612 -- discriminants, then the parent subtype of the full view shall
20613 -- impose a statically matching constraint on those discriminants
20614 -- [7.3(13)].
20615 end if;
20616
20617 else
20618 -- For untagged types, verify that a type without discriminants is
20619 -- not completed with an unconstrained type. A separate error message
20620 -- is produced if the full type has defaulted discriminants.
20621
20622 if Is_Definite_Subtype (Priv_T)
20623 and then not Is_Definite_Subtype (Full_T)
20624 then
20625 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20626 Error_Msg_NE
20627 ("full view of& not compatible with declaration#",
20628 Full_T, Priv_T);
20629
20630 if not Is_Tagged_Type (Full_T) then
20631 Error_Msg_N
20632 ("\one is constrained, the other unconstrained", Full_T);
20633 end if;
20634 end if;
20635 end if;
20636
20637 -- AI-419: verify that the use of "limited" is consistent
20638
20639 declare
20640 Orig_Decl : constant Node_Id := Original_Node (N);
20641
20642 begin
20643 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20644 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20645 and then Nkind
20646 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20647 then
20648 if not Limited_Present (Parent (Priv_T))
20649 and then not Synchronized_Present (Parent (Priv_T))
20650 and then Limited_Present (Type_Definition (Orig_Decl))
20651 then
20652 Error_Msg_N
20653 ("full view of non-limited extension cannot be limited", N);
20654
20655 -- Conversely, if the partial view carries the limited keyword,
20656 -- the full view must as well, even if it may be redundant.
20657
20658 elsif Limited_Present (Parent (Priv_T))
20659 and then not Limited_Present (Type_Definition (Orig_Decl))
20660 then
20661 Error_Msg_N
20662 ("full view of limited extension must be explicitly limited",
20663 N);
20664 end if;
20665 end if;
20666 end;
20667
20668 -- Ada 2005 (AI-443): A synchronized private extension must be
20669 -- completed by a task or protected type.
20670
20671 if Ada_Version >= Ada_2005
20672 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20673 and then Synchronized_Present (Parent (Priv_T))
20674 and then not Is_Concurrent_Type (Full_T)
20675 then
20676 Error_Msg_N ("full view of synchronized extension must " &
20677 "be synchronized type", N);
20678 end if;
20679
20680 -- Ada 2005 AI-363: if the full view has discriminants with
20681 -- defaults, it is illegal to declare constrained access subtypes
20682 -- whose designated type is the current type. This allows objects
20683 -- of the type that are declared in the heap to be unconstrained.
20684
20685 if not Has_Unknown_Discriminants (Priv_T)
20686 and then not Has_Discriminants (Priv_T)
20687 and then Has_Discriminants (Full_T)
20688 and then
20689 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20690 then
20691 Set_Has_Constrained_Partial_View (Full_T);
20692 Set_Has_Constrained_Partial_View (Priv_T);
20693 end if;
20694
20695 -- Create a full declaration for all its subtypes recorded in
20696 -- Private_Dependents and swap them similarly to the base type. These
20697 -- are subtypes that have been define before the full declaration of
20698 -- the private type. We also swap the entry in Private_Dependents list
20699 -- so we can properly restore the private view on exit from the scope.
20700
20701 declare
20702 Priv_Elmt : Elmt_Id;
20703 Priv_Scop : Entity_Id;
20704 Priv : Entity_Id;
20705 Full : Entity_Id;
20706
20707 begin
20708 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20709 while Present (Priv_Elmt) loop
20710 Priv := Node (Priv_Elmt);
20711 Priv_Scop := Scope (Priv);
20712
20713 if Ekind_In (Priv, E_Private_Subtype,
20714 E_Limited_Private_Subtype,
20715 E_Record_Subtype_With_Private)
20716 then
20717 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20718 Set_Is_Itype (Full);
20719 Set_Parent (Full, Parent (Priv));
20720 Set_Associated_Node_For_Itype (Full, N);
20721
20722 -- Now we need to complete the private subtype, but since the
20723 -- base type has already been swapped, we must also swap the
20724 -- subtypes (and thus, reverse the arguments in the call to
20725 -- Complete_Private_Subtype). Also note that we may need to
20726 -- re-establish the scope of the private subtype.
20727
20728 Copy_And_Swap (Priv, Full);
20729
20730 if not In_Open_Scopes (Priv_Scop) then
20731 Push_Scope (Priv_Scop);
20732
20733 else
20734 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20735
20736 Priv_Scop := Empty;
20737 end if;
20738
20739 Complete_Private_Subtype (Full, Priv, Full_T, N);
20740 Set_Full_View (Full, Priv);
20741
20742 if Present (Priv_Scop) then
20743 Pop_Scope;
20744 end if;
20745
20746 Replace_Elmt (Priv_Elmt, Full);
20747 end if;
20748
20749 Next_Elmt (Priv_Elmt);
20750 end loop;
20751 end;
20752
20753 -- If the private view was tagged, copy the new primitive operations
20754 -- from the private view to the full view.
20755
20756 if Is_Tagged_Type (Full_T) then
20757 declare
20758 Disp_Typ : Entity_Id;
20759 Full_List : Elist_Id;
20760 Prim : Entity_Id;
20761 Prim_Elmt : Elmt_Id;
20762 Priv_List : Elist_Id;
20763
20764 function Contains
20765 (E : Entity_Id;
20766 L : Elist_Id) return Boolean;
20767 -- Determine whether list L contains element E
20768
20769 --------------
20770 -- Contains --
20771 --------------
20772
20773 function Contains
20774 (E : Entity_Id;
20775 L : Elist_Id) return Boolean
20776 is
20777 List_Elmt : Elmt_Id;
20778
20779 begin
20780 List_Elmt := First_Elmt (L);
20781 while Present (List_Elmt) loop
20782 if Node (List_Elmt) = E then
20783 return True;
20784 end if;
20785
20786 Next_Elmt (List_Elmt);
20787 end loop;
20788
20789 return False;
20790 end Contains;
20791
20792 -- Start of processing
20793
20794 begin
20795 if Is_Tagged_Type (Priv_T) then
20796 Priv_List := Primitive_Operations (Priv_T);
20797 Prim_Elmt := First_Elmt (Priv_List);
20798
20799 -- In the case of a concurrent type completing a private tagged
20800 -- type, primitives may have been declared in between the two
20801 -- views. These subprograms need to be wrapped the same way
20802 -- entries and protected procedures are handled because they
20803 -- cannot be directly shared by the two views.
20804
20805 if Is_Concurrent_Type (Full_T) then
20806 declare
20807 Conc_Typ : constant Entity_Id :=
20808 Corresponding_Record_Type (Full_T);
20809 Curr_Nod : Node_Id := Parent (Conc_Typ);
20810 Wrap_Spec : Node_Id;
20811
20812 begin
20813 while Present (Prim_Elmt) loop
20814 Prim := Node (Prim_Elmt);
20815
20816 if Comes_From_Source (Prim)
20817 and then not Is_Abstract_Subprogram (Prim)
20818 then
20819 Wrap_Spec :=
20820 Make_Subprogram_Declaration (Sloc (Prim),
20821 Specification =>
20822 Build_Wrapper_Spec
20823 (Subp_Id => Prim,
20824 Obj_Typ => Conc_Typ,
20825 Formals =>
20826 Parameter_Specifications
20827 (Parent (Prim))));
20828
20829 Insert_After (Curr_Nod, Wrap_Spec);
20830 Curr_Nod := Wrap_Spec;
20831
20832 Analyze (Wrap_Spec);
20833
20834 -- Remove the wrapper from visibility to avoid
20835 -- spurious conflict with the wrapped entity.
20836
20837 Set_Is_Immediately_Visible
20838 (Defining_Entity (Specification (Wrap_Spec)),
20839 False);
20840 end if;
20841
20842 Next_Elmt (Prim_Elmt);
20843 end loop;
20844
20845 goto Leave;
20846 end;
20847
20848 -- For non-concurrent types, transfer explicit primitives, but
20849 -- omit those inherited from the parent of the private view
20850 -- since they will be re-inherited later on.
20851
20852 else
20853 Full_List := Primitive_Operations (Full_T);
20854 while Present (Prim_Elmt) loop
20855 Prim := Node (Prim_Elmt);
20856
20857 if Comes_From_Source (Prim)
20858 and then not Contains (Prim, Full_List)
20859 then
20860 Append_Elmt (Prim, Full_List);
20861 end if;
20862
20863 Next_Elmt (Prim_Elmt);
20864 end loop;
20865 end if;
20866
20867 -- Untagged private view
20868
20869 else
20870 Full_List := Primitive_Operations (Full_T);
20871
20872 -- In this case the partial view is untagged, so here we locate
20873 -- all of the earlier primitives that need to be treated as
20874 -- dispatching (those that appear between the two views). Note
20875 -- that these additional operations must all be new operations
20876 -- (any earlier operations that override inherited operations
20877 -- of the full view will already have been inserted in the
20878 -- primitives list, marked by Check_Operation_From_Private_View
20879 -- as dispatching. Note that implicit "/=" operators are
20880 -- excluded from being added to the primitives list since they
20881 -- shouldn't be treated as dispatching (tagged "/=" is handled
20882 -- specially).
20883
20884 Prim := Next_Entity (Full_T);
20885 while Present (Prim) and then Prim /= Priv_T loop
20886 if Ekind_In (Prim, E_Procedure, E_Function) then
20887 Disp_Typ := Find_Dispatching_Type (Prim);
20888
20889 if Disp_Typ = Full_T
20890 and then (Chars (Prim) /= Name_Op_Ne
20891 or else Comes_From_Source (Prim))
20892 then
20893 Check_Controlling_Formals (Full_T, Prim);
20894
20895 if Is_Suitable_Primitive (Prim)
20896 and then not Is_Dispatching_Operation (Prim)
20897 then
20898 Append_Elmt (Prim, Full_List);
20899 Set_Is_Dispatching_Operation (Prim);
20900 Set_DT_Position_Value (Prim, No_Uint);
20901 end if;
20902
20903 elsif Is_Dispatching_Operation (Prim)
20904 and then Disp_Typ /= Full_T
20905 then
20906 -- Verify that it is not otherwise controlled by a
20907 -- formal or a return value of type T.
20908
20909 Check_Controlling_Formals (Disp_Typ, Prim);
20910 end if;
20911 end if;
20912
20913 Next_Entity (Prim);
20914 end loop;
20915 end if;
20916
20917 -- For the tagged case, the two views can share the same primitive
20918 -- operations list and the same class-wide type. Update attributes
20919 -- of the class-wide type which depend on the full declaration.
20920
20921 if Is_Tagged_Type (Priv_T) then
20922 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20923 Set_Class_Wide_Type
20924 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20925
20926 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20927 end if;
20928 end;
20929 end if;
20930
20931 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20932
20933 if Known_To_Have_Preelab_Init (Priv_T) then
20934
20935 -- Case where there is a pragma Preelaborable_Initialization. We
20936 -- always allow this in predefined units, which is cheating a bit,
20937 -- but it means we don't have to struggle to meet the requirements in
20938 -- the RM for having Preelaborable Initialization. Otherwise we
20939 -- require that the type meets the RM rules. But we can't check that
20940 -- yet, because of the rule about overriding Initialize, so we simply
20941 -- set a flag that will be checked at freeze time.
20942
20943 if not In_Predefined_Unit (Full_T) then
20944 Set_Must_Have_Preelab_Init (Full_T);
20945 end if;
20946 end if;
20947
20948 -- If pragma CPP_Class was applied to the private type declaration,
20949 -- propagate it now to the full type declaration.
20950
20951 if Is_CPP_Class (Priv_T) then
20952 Set_Is_CPP_Class (Full_T);
20953 Set_Convention (Full_T, Convention_CPP);
20954
20955 -- Check that components of imported CPP types do not have default
20956 -- expressions.
20957
20958 Check_CPP_Type_Has_No_Defaults (Full_T);
20959 end if;
20960
20961 -- If the private view has user specified stream attributes, then so has
20962 -- the full view.
20963
20964 -- Why the test, how could these flags be already set in Full_T ???
20965
20966 if Has_Specified_Stream_Read (Priv_T) then
20967 Set_Has_Specified_Stream_Read (Full_T);
20968 end if;
20969
20970 if Has_Specified_Stream_Write (Priv_T) then
20971 Set_Has_Specified_Stream_Write (Full_T);
20972 end if;
20973
20974 if Has_Specified_Stream_Input (Priv_T) then
20975 Set_Has_Specified_Stream_Input (Full_T);
20976 end if;
20977
20978 if Has_Specified_Stream_Output (Priv_T) then
20979 Set_Has_Specified_Stream_Output (Full_T);
20980 end if;
20981
20982 -- Propagate Default_Initial_Condition-related attributes from the
20983 -- partial view to the full view and its base type.
20984
20985 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20986 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20987
20988 -- Propagate invariant-related attributes from the partial view to the
20989 -- full view and its base type.
20990
20991 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20992 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20993
20994 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20995 -- in the full view without advertising the inheritance in the partial
20996 -- view. This can only occur when the partial view has no parent type
20997 -- and the full view has an interface as a parent. Any other scenarios
20998 -- are illegal because implemented interfaces must match between the
20999 -- two views.
21000
21001 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21002 declare
21003 Full_Par : constant Entity_Id := Etype (Full_T);
21004 Priv_Par : constant Entity_Id := Etype (Priv_T);
21005
21006 begin
21007 if not Is_Interface (Priv_Par)
21008 and then Is_Interface (Full_Par)
21009 and then Has_Inheritable_Invariants (Full_Par)
21010 then
21011 Error_Msg_N
21012 ("hidden inheritance of class-wide type invariants not "
21013 & "allowed", N);
21014 end if;
21015 end;
21016 end if;
21017
21018 -- Propagate predicates to full type, and predicate function if already
21019 -- defined. It is not clear that this can actually happen? the partial
21020 -- view cannot be frozen yet, and the predicate function has not been
21021 -- built. Still it is a cheap check and seems safer to make it.
21022
21023 if Has_Predicates (Priv_T) then
21024 Set_Has_Predicates (Full_T);
21025
21026 if Present (Predicate_Function (Priv_T)) then
21027 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
21028 end if;
21029 end if;
21030
21031 <<Leave>>
21032 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21033 end Process_Full_View;
21034
21035 -----------------------------------
21036 -- Process_Incomplete_Dependents --
21037 -----------------------------------
21038
21039 procedure Process_Incomplete_Dependents
21040 (N : Node_Id;
21041 Full_T : Entity_Id;
21042 Inc_T : Entity_Id)
21043 is
21044 Inc_Elmt : Elmt_Id;
21045 Priv_Dep : Entity_Id;
21046 New_Subt : Entity_Id;
21047
21048 Disc_Constraint : Elist_Id;
21049
21050 begin
21051 if No (Private_Dependents (Inc_T)) then
21052 return;
21053 end if;
21054
21055 -- Itypes that may be generated by the completion of an incomplete
21056 -- subtype are not used by the back-end and not attached to the tree.
21057 -- They are created only for constraint-checking purposes.
21058
21059 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21060 while Present (Inc_Elmt) loop
21061 Priv_Dep := Node (Inc_Elmt);
21062
21063 if Ekind (Priv_Dep) = E_Subprogram_Type then
21064
21065 -- An Access_To_Subprogram type may have a return type or a
21066 -- parameter type that is incomplete. Replace with the full view.
21067
21068 if Etype (Priv_Dep) = Inc_T then
21069 Set_Etype (Priv_Dep, Full_T);
21070 end if;
21071
21072 declare
21073 Formal : Entity_Id;
21074
21075 begin
21076 Formal := First_Formal (Priv_Dep);
21077 while Present (Formal) loop
21078 if Etype (Formal) = Inc_T then
21079 Set_Etype (Formal, Full_T);
21080 end if;
21081
21082 Next_Formal (Formal);
21083 end loop;
21084 end;
21085
21086 elsif Is_Overloadable (Priv_Dep) then
21087
21088 -- If a subprogram in the incomplete dependents list is primitive
21089 -- for a tagged full type then mark it as a dispatching operation,
21090 -- check whether it overrides an inherited subprogram, and check
21091 -- restrictions on its controlling formals. Note that a protected
21092 -- operation is never dispatching: only its wrapper operation
21093 -- (which has convention Ada) is.
21094
21095 if Is_Tagged_Type (Full_T)
21096 and then Is_Primitive (Priv_Dep)
21097 and then Convention (Priv_Dep) /= Convention_Protected
21098 then
21099 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21100 Set_Is_Dispatching_Operation (Priv_Dep);
21101 Check_Controlling_Formals (Full_T, Priv_Dep);
21102 end if;
21103
21104 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21105
21106 -- Can happen during processing of a body before the completion
21107 -- of a TA type. Ignore, because spec is also on dependent list.
21108
21109 return;
21110
21111 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21112 -- corresponding subtype of the full view.
21113
21114 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21115 and then Comes_From_Source (Priv_Dep)
21116 then
21117 Set_Subtype_Indication
21118 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21119 Set_Etype (Priv_Dep, Full_T);
21120 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21121 Set_Analyzed (Parent (Priv_Dep), False);
21122
21123 -- Reanalyze the declaration, suppressing the call to Enter_Name
21124 -- to avoid duplicate names.
21125
21126 Analyze_Subtype_Declaration
21127 (N => Parent (Priv_Dep),
21128 Skip => True);
21129
21130 -- Dependent is a subtype
21131
21132 else
21133 -- We build a new subtype indication using the full view of the
21134 -- incomplete parent. The discriminant constraints have been
21135 -- elaborated already at the point of the subtype declaration.
21136
21137 New_Subt := Create_Itype (E_Void, N);
21138
21139 if Has_Discriminants (Full_T) then
21140 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21141 else
21142 Disc_Constraint := No_Elist;
21143 end if;
21144
21145 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21146 Set_Full_View (Priv_Dep, New_Subt);
21147 end if;
21148
21149 Next_Elmt (Inc_Elmt);
21150 end loop;
21151 end Process_Incomplete_Dependents;
21152
21153 --------------------------------
21154 -- Process_Range_Expr_In_Decl --
21155 --------------------------------
21156
21157 procedure Process_Range_Expr_In_Decl
21158 (R : Node_Id;
21159 T : Entity_Id;
21160 Subtyp : Entity_Id := Empty;
21161 Check_List : List_Id := Empty_List;
21162 R_Check_Off : Boolean := False;
21163 In_Iter_Schm : Boolean := False)
21164 is
21165 Lo, Hi : Node_Id;
21166 R_Checks : Check_Result;
21167 Insert_Node : Node_Id;
21168 Def_Id : Entity_Id;
21169
21170 begin
21171 Analyze_And_Resolve (R, Base_Type (T));
21172
21173 if Nkind (R) = N_Range then
21174
21175 -- In SPARK, all ranges should be static, with the exception of the
21176 -- discrete type definition of a loop parameter specification.
21177
21178 if not In_Iter_Schm
21179 and then not Is_OK_Static_Range (R)
21180 then
21181 Check_SPARK_05_Restriction ("range should be static", R);
21182 end if;
21183
21184 Lo := Low_Bound (R);
21185 Hi := High_Bound (R);
21186
21187 -- Validity checks on the range of a quantified expression are
21188 -- delayed until the construct is transformed into a loop.
21189
21190 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21191 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21192 then
21193 null;
21194
21195 -- We need to ensure validity of the bounds here, because if we
21196 -- go ahead and do the expansion, then the expanded code will get
21197 -- analyzed with range checks suppressed and we miss the check.
21198
21199 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21200 -- the temporaries generated by routine Remove_Side_Effects by means
21201 -- of validity checks must use the same names. When a range appears
21202 -- in the parent of a generic, the range is processed with checks
21203 -- disabled as part of the generic context and with checks enabled
21204 -- for code generation purposes. This leads to link issues as the
21205 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21206 -- template sees the temporaries generated by Remove_Side_Effects.
21207
21208 else
21209 Validity_Check_Range (R, Subtyp);
21210 end if;
21211
21212 -- If there were errors in the declaration, try and patch up some
21213 -- common mistakes in the bounds. The cases handled are literals
21214 -- which are Integer where the expected type is Real and vice versa.
21215 -- These corrections allow the compilation process to proceed further
21216 -- along since some basic assumptions of the format of the bounds
21217 -- are guaranteed.
21218
21219 if Etype (R) = Any_Type then
21220 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21221 Rewrite (Lo,
21222 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21223
21224 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21225 Rewrite (Hi,
21226 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21227
21228 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21229 Rewrite (Lo,
21230 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21231
21232 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21233 Rewrite (Hi,
21234 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21235 end if;
21236
21237 Set_Etype (Lo, T);
21238 Set_Etype (Hi, T);
21239 end if;
21240
21241 -- If the bounds of the range have been mistakenly given as string
21242 -- literals (perhaps in place of character literals), then an error
21243 -- has already been reported, but we rewrite the string literal as a
21244 -- bound of the range's type to avoid blowups in later processing
21245 -- that looks at static values.
21246
21247 if Nkind (Lo) = N_String_Literal then
21248 Rewrite (Lo,
21249 Make_Attribute_Reference (Sloc (Lo),
21250 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21251 Attribute_Name => Name_First));
21252 Analyze_And_Resolve (Lo);
21253 end if;
21254
21255 if Nkind (Hi) = N_String_Literal then
21256 Rewrite (Hi,
21257 Make_Attribute_Reference (Sloc (Hi),
21258 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21259 Attribute_Name => Name_First));
21260 Analyze_And_Resolve (Hi);
21261 end if;
21262
21263 -- If bounds aren't scalar at this point then exit, avoiding
21264 -- problems with further processing of the range in this procedure.
21265
21266 if not Is_Scalar_Type (Etype (Lo)) then
21267 return;
21268 end if;
21269
21270 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21271 -- then range of the base type. Here we check whether the bounds
21272 -- are in the range of the subtype itself. Note that if the bounds
21273 -- represent the null range the Constraint_Error exception should
21274 -- not be raised.
21275
21276 -- ??? The following code should be cleaned up as follows
21277
21278 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21279 -- is done in the call to Range_Check (R, T); below
21280
21281 -- 2. The use of R_Check_Off should be investigated and possibly
21282 -- removed, this would clean up things a bit.
21283
21284 if Is_Null_Range (Lo, Hi) then
21285 null;
21286
21287 else
21288 -- Capture values of bounds and generate temporaries for them
21289 -- if needed, before applying checks, since checks may cause
21290 -- duplication of the expression without forcing evaluation.
21291
21292 -- The forced evaluation removes side effects from expressions,
21293 -- which should occur also in GNATprove mode. Otherwise, we end up
21294 -- with unexpected insertions of actions at places where this is
21295 -- not supposed to occur, e.g. on default parameters of a call.
21296
21297 if Expander_Active or GNATprove_Mode then
21298
21299 -- Call Force_Evaluation to create declarations as needed to
21300 -- deal with side effects, and also create typ_FIRST/LAST
21301 -- entities for bounds if we have a subtype name.
21302
21303 -- Note: we do this transformation even if expansion is not
21304 -- active if we are in GNATprove_Mode since the transformation
21305 -- is in general required to ensure that the resulting tree has
21306 -- proper Ada semantics.
21307
21308 Force_Evaluation
21309 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21310 Force_Evaluation
21311 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21312 end if;
21313
21314 -- We use a flag here instead of suppressing checks on the type
21315 -- because the type we check against isn't necessarily the place
21316 -- where we put the check.
21317
21318 if not R_Check_Off then
21319 R_Checks := Get_Range_Checks (R, T);
21320
21321 -- Look up tree to find an appropriate insertion point. We
21322 -- can't just use insert_actions because later processing
21323 -- depends on the insertion node. Prior to Ada 2012 the
21324 -- insertion point could only be a declaration or a loop, but
21325 -- quantified expressions can appear within any context in an
21326 -- expression, and the insertion point can be any statement,
21327 -- pragma, or declaration.
21328
21329 Insert_Node := Parent (R);
21330 while Present (Insert_Node) loop
21331 exit when
21332 Nkind (Insert_Node) in N_Declaration
21333 and then
21334 not Nkind_In
21335 (Insert_Node, N_Component_Declaration,
21336 N_Loop_Parameter_Specification,
21337 N_Function_Specification,
21338 N_Procedure_Specification);
21339
21340 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21341 or else Nkind (Insert_Node) in
21342 N_Statement_Other_Than_Procedure_Call
21343 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21344 N_Pragma);
21345
21346 Insert_Node := Parent (Insert_Node);
21347 end loop;
21348
21349 -- Why would Type_Decl not be present??? Without this test,
21350 -- short regression tests fail.
21351
21352 if Present (Insert_Node) then
21353
21354 -- Case of loop statement. Verify that the range is part
21355 -- of the subtype indication of the iteration scheme.
21356
21357 if Nkind (Insert_Node) = N_Loop_Statement then
21358 declare
21359 Indic : Node_Id;
21360
21361 begin
21362 Indic := Parent (R);
21363 while Present (Indic)
21364 and then Nkind (Indic) /= N_Subtype_Indication
21365 loop
21366 Indic := Parent (Indic);
21367 end loop;
21368
21369 if Present (Indic) then
21370 Def_Id := Etype (Subtype_Mark (Indic));
21371
21372 Insert_Range_Checks
21373 (R_Checks,
21374 Insert_Node,
21375 Def_Id,
21376 Sloc (Insert_Node),
21377 R,
21378 Do_Before => True);
21379 end if;
21380 end;
21381
21382 -- Insertion before a declaration. If the declaration
21383 -- includes discriminants, the list of applicable checks
21384 -- is given by the caller.
21385
21386 elsif Nkind (Insert_Node) in N_Declaration then
21387 Def_Id := Defining_Identifier (Insert_Node);
21388
21389 if (Ekind (Def_Id) = E_Record_Type
21390 and then Depends_On_Discriminant (R))
21391 or else
21392 (Ekind (Def_Id) = E_Protected_Type
21393 and then Has_Discriminants (Def_Id))
21394 then
21395 Append_Range_Checks
21396 (R_Checks,
21397 Check_List, Def_Id, Sloc (Insert_Node), R);
21398
21399 else
21400 Insert_Range_Checks
21401 (R_Checks,
21402 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21403
21404 end if;
21405
21406 -- Insertion before a statement. Range appears in the
21407 -- context of a quantified expression. Insertion will
21408 -- take place when expression is expanded.
21409
21410 else
21411 null;
21412 end if;
21413 end if;
21414 end if;
21415 end if;
21416
21417 -- Case of other than an explicit N_Range node
21418
21419 -- The forced evaluation removes side effects from expressions, which
21420 -- should occur also in GNATprove mode. Otherwise, we end up with
21421 -- unexpected insertions of actions at places where this is not
21422 -- supposed to occur, e.g. on default parameters of a call.
21423
21424 elsif Expander_Active or GNATprove_Mode then
21425 Get_Index_Bounds (R, Lo, Hi);
21426 Force_Evaluation (Lo);
21427 Force_Evaluation (Hi);
21428 end if;
21429 end Process_Range_Expr_In_Decl;
21430
21431 --------------------------------------
21432 -- Process_Real_Range_Specification --
21433 --------------------------------------
21434
21435 procedure Process_Real_Range_Specification (Def : Node_Id) is
21436 Spec : constant Node_Id := Real_Range_Specification (Def);
21437 Lo : Node_Id;
21438 Hi : Node_Id;
21439 Err : Boolean := False;
21440
21441 procedure Analyze_Bound (N : Node_Id);
21442 -- Analyze and check one bound
21443
21444 -------------------
21445 -- Analyze_Bound --
21446 -------------------
21447
21448 procedure Analyze_Bound (N : Node_Id) is
21449 begin
21450 Analyze_And_Resolve (N, Any_Real);
21451
21452 if not Is_OK_Static_Expression (N) then
21453 Flag_Non_Static_Expr
21454 ("bound in real type definition is not static!", N);
21455 Err := True;
21456 end if;
21457 end Analyze_Bound;
21458
21459 -- Start of processing for Process_Real_Range_Specification
21460
21461 begin
21462 if Present (Spec) then
21463 Lo := Low_Bound (Spec);
21464 Hi := High_Bound (Spec);
21465 Analyze_Bound (Lo);
21466 Analyze_Bound (Hi);
21467
21468 -- If error, clear away junk range specification
21469
21470 if Err then
21471 Set_Real_Range_Specification (Def, Empty);
21472 end if;
21473 end if;
21474 end Process_Real_Range_Specification;
21475
21476 ---------------------
21477 -- Process_Subtype --
21478 ---------------------
21479
21480 function Process_Subtype
21481 (S : Node_Id;
21482 Related_Nod : Node_Id;
21483 Related_Id : Entity_Id := Empty;
21484 Suffix : Character := ' ') return Entity_Id
21485 is
21486 P : Node_Id;
21487 Def_Id : Entity_Id;
21488 Error_Node : Node_Id;
21489 Full_View_Id : Entity_Id;
21490 Subtype_Mark_Id : Entity_Id;
21491
21492 May_Have_Null_Exclusion : Boolean;
21493
21494 procedure Check_Incomplete (T : Node_Id);
21495 -- Called to verify that an incomplete type is not used prematurely
21496
21497 ----------------------
21498 -- Check_Incomplete --
21499 ----------------------
21500
21501 procedure Check_Incomplete (T : Node_Id) is
21502 begin
21503 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21504
21505 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21506 and then
21507 not (Ada_Version >= Ada_2005
21508 and then
21509 (Nkind (Parent (T)) = N_Subtype_Declaration
21510 or else (Nkind (Parent (T)) = N_Subtype_Indication
21511 and then Nkind (Parent (Parent (T))) =
21512 N_Subtype_Declaration)))
21513 then
21514 Error_Msg_N ("invalid use of type before its full declaration", T);
21515 end if;
21516 end Check_Incomplete;
21517
21518 -- Start of processing for Process_Subtype
21519
21520 begin
21521 -- Case of no constraints present
21522
21523 if Nkind (S) /= N_Subtype_Indication then
21524 Find_Type (S);
21525
21526 -- No way to proceed if the subtype indication is malformed. This
21527 -- will happen for example when the subtype indication in an object
21528 -- declaration is missing altogether and the expression is analyzed
21529 -- as if it were that indication.
21530
21531 if not Is_Entity_Name (S) then
21532 return Any_Type;
21533 end if;
21534
21535 Check_Incomplete (S);
21536 P := Parent (S);
21537
21538 -- Ada 2005 (AI-231): Static check
21539
21540 if Ada_Version >= Ada_2005
21541 and then Present (P)
21542 and then Null_Exclusion_Present (P)
21543 and then Nkind (P) /= N_Access_To_Object_Definition
21544 and then not Is_Access_Type (Entity (S))
21545 then
21546 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21547 end if;
21548
21549 -- The following is ugly, can't we have a range or even a flag???
21550
21551 May_Have_Null_Exclusion :=
21552 Nkind_In (P, N_Access_Definition,
21553 N_Access_Function_Definition,
21554 N_Access_Procedure_Definition,
21555 N_Access_To_Object_Definition,
21556 N_Allocator,
21557 N_Component_Definition)
21558 or else
21559 Nkind_In (P, N_Derived_Type_Definition,
21560 N_Discriminant_Specification,
21561 N_Formal_Object_Declaration,
21562 N_Object_Declaration,
21563 N_Object_Renaming_Declaration,
21564 N_Parameter_Specification,
21565 N_Subtype_Declaration);
21566
21567 -- Create an Itype that is a duplicate of Entity (S) but with the
21568 -- null-exclusion attribute.
21569
21570 if May_Have_Null_Exclusion
21571 and then Is_Access_Type (Entity (S))
21572 and then Null_Exclusion_Present (P)
21573
21574 -- No need to check the case of an access to object definition.
21575 -- It is correct to define double not-null pointers.
21576
21577 -- Example:
21578 -- type Not_Null_Int_Ptr is not null access Integer;
21579 -- type Acc is not null access Not_Null_Int_Ptr;
21580
21581 and then Nkind (P) /= N_Access_To_Object_Definition
21582 then
21583 if Can_Never_Be_Null (Entity (S)) then
21584 case Nkind (Related_Nod) is
21585 when N_Full_Type_Declaration =>
21586 if Nkind (Type_Definition (Related_Nod))
21587 in N_Array_Type_Definition
21588 then
21589 Error_Node :=
21590 Subtype_Indication
21591 (Component_Definition
21592 (Type_Definition (Related_Nod)));
21593 else
21594 Error_Node :=
21595 Subtype_Indication (Type_Definition (Related_Nod));
21596 end if;
21597
21598 when N_Subtype_Declaration =>
21599 Error_Node := Subtype_Indication (Related_Nod);
21600
21601 when N_Object_Declaration =>
21602 Error_Node := Object_Definition (Related_Nod);
21603
21604 when N_Component_Declaration =>
21605 Error_Node :=
21606 Subtype_Indication (Component_Definition (Related_Nod));
21607
21608 when N_Allocator =>
21609 Error_Node := Expression (Related_Nod);
21610
21611 when others =>
21612 pragma Assert (False);
21613 Error_Node := Related_Nod;
21614 end case;
21615
21616 Error_Msg_NE
21617 ("`NOT NULL` not allowed (& already excludes null)",
21618 Error_Node,
21619 Entity (S));
21620 end if;
21621
21622 Set_Etype (S,
21623 Create_Null_Excluding_Itype
21624 (T => Entity (S),
21625 Related_Nod => P));
21626 Set_Entity (S, Etype (S));
21627 end if;
21628
21629 return Entity (S);
21630
21631 -- Case of constraint present, so that we have an N_Subtype_Indication
21632 -- node (this node is created only if constraints are present).
21633
21634 else
21635 Find_Type (Subtype_Mark (S));
21636
21637 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21638 and then not
21639 (Nkind (Parent (S)) = N_Subtype_Declaration
21640 and then Is_Itype (Defining_Identifier (Parent (S))))
21641 then
21642 Check_Incomplete (Subtype_Mark (S));
21643 end if;
21644
21645 P := Parent (S);
21646 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21647
21648 -- Explicit subtype declaration case
21649
21650 if Nkind (P) = N_Subtype_Declaration then
21651 Def_Id := Defining_Identifier (P);
21652
21653 -- Explicit derived type definition case
21654
21655 elsif Nkind (P) = N_Derived_Type_Definition then
21656 Def_Id := Defining_Identifier (Parent (P));
21657
21658 -- Implicit case, the Def_Id must be created as an implicit type.
21659 -- The one exception arises in the case of concurrent types, array
21660 -- and access types, where other subsidiary implicit types may be
21661 -- created and must appear before the main implicit type. In these
21662 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21663 -- has not yet been called to create Def_Id.
21664
21665 else
21666 if Is_Array_Type (Subtype_Mark_Id)
21667 or else Is_Concurrent_Type (Subtype_Mark_Id)
21668 or else Is_Access_Type (Subtype_Mark_Id)
21669 then
21670 Def_Id := Empty;
21671
21672 -- For the other cases, we create a new unattached Itype,
21673 -- and set the indication to ensure it gets attached later.
21674
21675 else
21676 Def_Id :=
21677 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21678 end if;
21679 end if;
21680
21681 -- If the kind of constraint is invalid for this kind of type,
21682 -- then give an error, and then pretend no constraint was given.
21683
21684 if not Is_Valid_Constraint_Kind
21685 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21686 then
21687 Error_Msg_N
21688 ("incorrect constraint for this kind of type", Constraint (S));
21689
21690 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21691
21692 -- Set Ekind of orphan itype, to prevent cascaded errors
21693
21694 if Present (Def_Id) then
21695 Set_Ekind (Def_Id, Ekind (Any_Type));
21696 end if;
21697
21698 -- Make recursive call, having got rid of the bogus constraint
21699
21700 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21701 end if;
21702
21703 -- Remaining processing depends on type. Select on Base_Type kind to
21704 -- ensure getting to the concrete type kind in the case of a private
21705 -- subtype (needed when only doing semantic analysis).
21706
21707 case Ekind (Base_Type (Subtype_Mark_Id)) is
21708 when Access_Kind =>
21709
21710 -- If this is a constraint on a class-wide type, discard it.
21711 -- There is currently no way to express a partial discriminant
21712 -- constraint on a type with unknown discriminants. This is
21713 -- a pathology that the ACATS wisely decides not to test.
21714
21715 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21716 if Comes_From_Source (S) then
21717 Error_Msg_N
21718 ("constraint on class-wide type ignored??",
21719 Constraint (S));
21720 end if;
21721
21722 if Nkind (P) = N_Subtype_Declaration then
21723 Set_Subtype_Indication (P,
21724 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21725 end if;
21726
21727 return Subtype_Mark_Id;
21728 end if;
21729
21730 Constrain_Access (Def_Id, S, Related_Nod);
21731
21732 if Expander_Active
21733 and then Is_Itype (Designated_Type (Def_Id))
21734 and then Nkind (Related_Nod) = N_Subtype_Declaration
21735 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21736 then
21737 Build_Itype_Reference
21738 (Designated_Type (Def_Id), Related_Nod);
21739 end if;
21740
21741 when Array_Kind =>
21742 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21743
21744 when Decimal_Fixed_Point_Kind =>
21745 Constrain_Decimal (Def_Id, S);
21746
21747 when Enumeration_Kind =>
21748 Constrain_Enumeration (Def_Id, S);
21749
21750 when Ordinary_Fixed_Point_Kind =>
21751 Constrain_Ordinary_Fixed (Def_Id, S);
21752
21753 when Float_Kind =>
21754 Constrain_Float (Def_Id, S);
21755
21756 when Integer_Kind =>
21757 Constrain_Integer (Def_Id, S);
21758
21759 when Class_Wide_Kind
21760 | E_Incomplete_Type
21761 | E_Record_Subtype
21762 | E_Record_Type
21763 =>
21764 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21765
21766 if Ekind (Def_Id) = E_Incomplete_Type then
21767 Set_Private_Dependents (Def_Id, New_Elmt_List);
21768 end if;
21769
21770 when Private_Kind =>
21771
21772 -- A private type with unknown discriminants may be completed
21773 -- by an unconstrained array type.
21774
21775 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21776 and then Present (Full_View (Subtype_Mark_Id))
21777 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21778 then
21779 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21780
21781 -- ... but more commonly is completed by a discriminated record
21782 -- type.
21783
21784 else
21785 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21786 end if;
21787
21788 -- The base type may be private but Def_Id may be a full view
21789 -- in an instance.
21790
21791 if Is_Private_Type (Def_Id) then
21792 Set_Private_Dependents (Def_Id, New_Elmt_List);
21793 end if;
21794
21795 -- In case of an invalid constraint prevent further processing
21796 -- since the type constructed is missing expected fields.
21797
21798 if Etype (Def_Id) = Any_Type then
21799 return Def_Id;
21800 end if;
21801
21802 -- If the full view is that of a task with discriminants,
21803 -- we must constrain both the concurrent type and its
21804 -- corresponding record type. Otherwise we will just propagate
21805 -- the constraint to the full view, if available.
21806
21807 if Present (Full_View (Subtype_Mark_Id))
21808 and then Has_Discriminants (Subtype_Mark_Id)
21809 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21810 then
21811 Full_View_Id :=
21812 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21813
21814 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21815 Constrain_Concurrent (Full_View_Id, S,
21816 Related_Nod, Related_Id, Suffix);
21817 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21818 Set_Full_View (Def_Id, Full_View_Id);
21819
21820 -- Introduce an explicit reference to the private subtype,
21821 -- to prevent scope anomalies in gigi if first use appears
21822 -- in a nested context, e.g. a later function body.
21823 -- Should this be generated in other contexts than a full
21824 -- type declaration?
21825
21826 if Is_Itype (Def_Id)
21827 and then
21828 Nkind (Parent (P)) = N_Full_Type_Declaration
21829 then
21830 Build_Itype_Reference (Def_Id, Parent (P));
21831 end if;
21832
21833 else
21834 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21835 end if;
21836
21837 when Concurrent_Kind =>
21838 Constrain_Concurrent (Def_Id, S,
21839 Related_Nod, Related_Id, Suffix);
21840
21841 when others =>
21842 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21843 end case;
21844
21845 -- Size, Alignment, Representation aspects and Convention are always
21846 -- inherited from the base type.
21847
21848 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21849 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21850 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21851
21852 -- The anonymous subtype created for the subtype indication
21853 -- inherits the predicates of the parent.
21854
21855 if Has_Predicates (Subtype_Mark_Id) then
21856 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21857
21858 -- Indicate where the predicate function may be found
21859
21860 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21861 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21862 end if;
21863 end if;
21864
21865 return Def_Id;
21866 end if;
21867 end Process_Subtype;
21868
21869 -----------------------------
21870 -- Record_Type_Declaration --
21871 -----------------------------
21872
21873 procedure Record_Type_Declaration
21874 (T : Entity_Id;
21875 N : Node_Id;
21876 Prev : Entity_Id)
21877 is
21878 Def : constant Node_Id := Type_Definition (N);
21879 Is_Tagged : Boolean;
21880 Tag_Comp : Entity_Id;
21881
21882 begin
21883 -- These flags must be initialized before calling Process_Discriminants
21884 -- because this routine makes use of them.
21885
21886 Set_Ekind (T, E_Record_Type);
21887 Set_Etype (T, T);
21888 Init_Size_Align (T);
21889 Set_Interfaces (T, No_Elist);
21890 Set_Stored_Constraint (T, No_Elist);
21891 Set_Default_SSO (T);
21892 Set_No_Reordering (T, No_Component_Reordering);
21893
21894 -- Normal case
21895
21896 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21897 if Limited_Present (Def) then
21898 Check_SPARK_05_Restriction ("limited is not allowed", N);
21899 end if;
21900
21901 if Abstract_Present (Def) then
21902 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21903 end if;
21904
21905 -- The flag Is_Tagged_Type might have already been set by
21906 -- Find_Type_Name if it detected an error for declaration T. This
21907 -- arises in the case of private tagged types where the full view
21908 -- omits the word tagged.
21909
21910 Is_Tagged :=
21911 Tagged_Present (Def)
21912 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21913
21914 Set_Is_Limited_Record (T, Limited_Present (Def));
21915
21916 if Is_Tagged then
21917 Set_Is_Tagged_Type (T, True);
21918 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21919 end if;
21920
21921 -- Type is abstract if full declaration carries keyword, or if
21922 -- previous partial view did.
21923
21924 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21925 or else Abstract_Present (Def));
21926
21927 else
21928 Check_SPARK_05_Restriction ("interface is not allowed", N);
21929
21930 Is_Tagged := True;
21931 Analyze_Interface_Declaration (T, Def);
21932
21933 if Present (Discriminant_Specifications (N)) then
21934 Error_Msg_N
21935 ("interface types cannot have discriminants",
21936 Defining_Identifier
21937 (First (Discriminant_Specifications (N))));
21938 end if;
21939 end if;
21940
21941 -- First pass: if there are self-referential access components,
21942 -- create the required anonymous access type declarations, and if
21943 -- need be an incomplete type declaration for T itself.
21944
21945 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21946
21947 if Ada_Version >= Ada_2005
21948 and then Present (Interface_List (Def))
21949 then
21950 Check_Interfaces (N, Def);
21951
21952 declare
21953 Ifaces_List : Elist_Id;
21954
21955 begin
21956 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21957 -- already in the parents.
21958
21959 Collect_Interfaces
21960 (T => T,
21961 Ifaces_List => Ifaces_List,
21962 Exclude_Parents => True);
21963
21964 Set_Interfaces (T, Ifaces_List);
21965 end;
21966 end if;
21967
21968 -- Records constitute a scope for the component declarations within.
21969 -- The scope is created prior to the processing of these declarations.
21970 -- Discriminants are processed first, so that they are visible when
21971 -- processing the other components. The Ekind of the record type itself
21972 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21973
21974 -- Enter record scope
21975
21976 Push_Scope (T);
21977
21978 -- If an incomplete or private type declaration was already given for
21979 -- the type, then this scope already exists, and the discriminants have
21980 -- been declared within. We must verify that the full declaration
21981 -- matches the incomplete one.
21982
21983 Check_Or_Process_Discriminants (N, T, Prev);
21984
21985 Set_Is_Constrained (T, not Has_Discriminants (T));
21986 Set_Has_Delayed_Freeze (T, True);
21987
21988 -- For tagged types add a manually analyzed component corresponding
21989 -- to the component _tag, the corresponding piece of tree will be
21990 -- expanded as part of the freezing actions if it is not a CPP_Class.
21991
21992 if Is_Tagged then
21993
21994 -- Do not add the tag unless we are in expansion mode
21995
21996 if Expander_Active then
21997 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21998 Enter_Name (Tag_Comp);
21999
22000 Set_Ekind (Tag_Comp, E_Component);
22001 Set_Is_Tag (Tag_Comp);
22002 Set_Is_Aliased (Tag_Comp);
22003 Set_Is_Independent (Tag_Comp);
22004 Set_Etype (Tag_Comp, RTE (RE_Tag));
22005 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22006 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22007 Init_Component_Location (Tag_Comp);
22008
22009 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22010 -- implemented interfaces.
22011
22012 if Has_Interfaces (T) then
22013 Add_Interface_Tag_Components (N, T);
22014 end if;
22015 end if;
22016
22017 Make_Class_Wide_Type (T);
22018 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22019 end if;
22020
22021 -- We must suppress range checks when processing record components in
22022 -- the presence of discriminants, since we don't want spurious checks to
22023 -- be generated during their analysis, but Suppress_Range_Checks flags
22024 -- must be reset the after processing the record definition.
22025
22026 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22027 -- couldn't we just use the normal range check suppression method here.
22028 -- That would seem cleaner ???
22029
22030 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22031 Set_Kill_Range_Checks (T, True);
22032 Record_Type_Definition (Def, Prev);
22033 Set_Kill_Range_Checks (T, False);
22034 else
22035 Record_Type_Definition (Def, Prev);
22036 end if;
22037
22038 -- Exit from record scope
22039
22040 End_Scope;
22041
22042 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22043 -- the implemented interfaces and associate them an aliased entity.
22044
22045 if Is_Tagged
22046 and then not Is_Empty_List (Interface_List (Def))
22047 then
22048 Derive_Progenitor_Subprograms (T, T);
22049 end if;
22050
22051 Check_Function_Writable_Actuals (N);
22052 end Record_Type_Declaration;
22053
22054 ----------------------------
22055 -- Record_Type_Definition --
22056 ----------------------------
22057
22058 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22059 Component : Entity_Id;
22060 Ctrl_Components : Boolean := False;
22061 Final_Storage_Only : Boolean;
22062 T : Entity_Id;
22063
22064 begin
22065 if Ekind (Prev_T) = E_Incomplete_Type then
22066 T := Full_View (Prev_T);
22067 else
22068 T := Prev_T;
22069 end if;
22070
22071 -- In SPARK, tagged types and type extensions may only be declared in
22072 -- the specification of library unit packages.
22073
22074 if Present (Def) and then Is_Tagged_Type (T) then
22075 declare
22076 Typ : Node_Id;
22077 Ctxt : Node_Id;
22078
22079 begin
22080 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22081 Typ := Parent (Def);
22082 else
22083 pragma Assert
22084 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22085 Typ := Parent (Parent (Def));
22086 end if;
22087
22088 Ctxt := Parent (Typ);
22089
22090 if Nkind (Ctxt) = N_Package_Body
22091 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22092 then
22093 Check_SPARK_05_Restriction
22094 ("type should be defined in package specification", Typ);
22095
22096 elsif Nkind (Ctxt) /= N_Package_Specification
22097 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22098 then
22099 Check_SPARK_05_Restriction
22100 ("type should be defined in library unit package", Typ);
22101 end if;
22102 end;
22103 end if;
22104
22105 Final_Storage_Only := not Is_Controlled (T);
22106
22107 -- Ada 2005: Check whether an explicit Limited is present in a derived
22108 -- type declaration.
22109
22110 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22111 and then Limited_Present (Parent (Def))
22112 then
22113 Set_Is_Limited_Record (T);
22114 end if;
22115
22116 -- If the component list of a record type is defined by the reserved
22117 -- word null and there is no discriminant part, then the record type has
22118 -- no components and all records of the type are null records (RM 3.7)
22119 -- This procedure is also called to process the extension part of a
22120 -- record extension, in which case the current scope may have inherited
22121 -- components.
22122
22123 if No (Def)
22124 or else No (Component_List (Def))
22125 or else Null_Present (Component_List (Def))
22126 then
22127 if not Is_Tagged_Type (T) then
22128 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22129 end if;
22130
22131 else
22132 Analyze_Declarations (Component_Items (Component_List (Def)));
22133
22134 if Present (Variant_Part (Component_List (Def))) then
22135 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22136 Analyze (Variant_Part (Component_List (Def)));
22137 end if;
22138 end if;
22139
22140 -- After completing the semantic analysis of the record definition,
22141 -- record components, both new and inherited, are accessible. Set their
22142 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22143 -- whose Ekind may be void.
22144
22145 Component := First_Entity (Current_Scope);
22146 while Present (Component) loop
22147 if Ekind (Component) = E_Void
22148 and then not Is_Itype (Component)
22149 then
22150 Set_Ekind (Component, E_Component);
22151 Init_Component_Location (Component);
22152 end if;
22153
22154 Propagate_Concurrent_Flags (T, Etype (Component));
22155
22156 if Ekind (Component) /= E_Component then
22157 null;
22158
22159 -- Do not set Has_Controlled_Component on a class-wide equivalent
22160 -- type. See Make_CW_Equivalent_Type.
22161
22162 elsif not Is_Class_Wide_Equivalent_Type (T)
22163 and then (Has_Controlled_Component (Etype (Component))
22164 or else (Chars (Component) /= Name_uParent
22165 and then Is_Controlled (Etype (Component))))
22166 then
22167 Set_Has_Controlled_Component (T, True);
22168 Final_Storage_Only :=
22169 Final_Storage_Only
22170 and then Finalize_Storage_Only (Etype (Component));
22171 Ctrl_Components := True;
22172 end if;
22173
22174 Next_Entity (Component);
22175 end loop;
22176
22177 -- A Type is Finalize_Storage_Only only if all its controlled components
22178 -- are also.
22179
22180 if Ctrl_Components then
22181 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22182 end if;
22183
22184 -- Place reference to end record on the proper entity, which may
22185 -- be a partial view.
22186
22187 if Present (Def) then
22188 Process_End_Label (Def, 'e', Prev_T);
22189 end if;
22190 end Record_Type_Definition;
22191
22192 ---------------------------
22193 -- Replace_Discriminants --
22194 ---------------------------
22195
22196 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22197 function Process (N : Node_Id) return Traverse_Result;
22198
22199 -------------
22200 -- Process --
22201 -------------
22202
22203 function Process (N : Node_Id) return Traverse_Result is
22204 Comp : Entity_Id;
22205
22206 begin
22207 if Nkind (N) = N_Discriminant_Specification then
22208 Comp := First_Discriminant (Typ);
22209 while Present (Comp) loop
22210 if Original_Record_Component (Comp) = Defining_Identifier (N)
22211 or else Chars (Comp) = Chars (Defining_Identifier (N))
22212 then
22213 Set_Defining_Identifier (N, Comp);
22214 exit;
22215 end if;
22216
22217 Next_Discriminant (Comp);
22218 end loop;
22219
22220 elsif Nkind (N) = N_Variant_Part then
22221 Comp := First_Discriminant (Typ);
22222 while Present (Comp) loop
22223 if Original_Record_Component (Comp) = Entity (Name (N))
22224 or else Chars (Comp) = Chars (Name (N))
22225 then
22226 -- Make sure to preserve the type coming from the parent on
22227 -- the Name, even if the subtype of the discriminant can be
22228 -- constrained, so that discrete choices inherited from the
22229 -- parent in the variant part are not flagged as violating
22230 -- the constraints of the subtype.
22231
22232 declare
22233 Typ : constant Entity_Id := Etype (Name (N));
22234 begin
22235 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22236 Set_Etype (Name (N), Typ);
22237 end;
22238 exit;
22239 end if;
22240
22241 Next_Discriminant (Comp);
22242 end loop;
22243 end if;
22244
22245 return OK;
22246 end Process;
22247
22248 procedure Replace is new Traverse_Proc (Process);
22249
22250 -- Start of processing for Replace_Discriminants
22251
22252 begin
22253 Replace (Decl);
22254 end Replace_Discriminants;
22255
22256 -------------------------------
22257 -- Set_Completion_Referenced --
22258 -------------------------------
22259
22260 procedure Set_Completion_Referenced (E : Entity_Id) is
22261 begin
22262 -- If in main unit, mark entity that is a completion as referenced,
22263 -- warnings go on the partial view when needed.
22264
22265 if In_Extended_Main_Source_Unit (E) then
22266 Set_Referenced (E);
22267 end if;
22268 end Set_Completion_Referenced;
22269
22270 ---------------------
22271 -- Set_Default_SSO --
22272 ---------------------
22273
22274 procedure Set_Default_SSO (T : Entity_Id) is
22275 begin
22276 case Opt.Default_SSO is
22277 when ' ' =>
22278 null;
22279 when 'L' =>
22280 Set_SSO_Set_Low_By_Default (T, True);
22281 when 'H' =>
22282 Set_SSO_Set_High_By_Default (T, True);
22283 when others =>
22284 raise Program_Error;
22285 end case;
22286 end Set_Default_SSO;
22287
22288 ---------------------
22289 -- Set_Fixed_Range --
22290 ---------------------
22291
22292 -- The range for fixed-point types is complicated by the fact that we
22293 -- do not know the exact end points at the time of the declaration. This
22294 -- is true for three reasons:
22295
22296 -- A size clause may affect the fudging of the end-points.
22297 -- A small clause may affect the values of the end-points.
22298 -- We try to include the end-points if it does not affect the size.
22299
22300 -- This means that the actual end-points must be established at the
22301 -- point when the type is frozen. Meanwhile, we first narrow the range
22302 -- as permitted (so that it will fit if necessary in a small specified
22303 -- size), and then build a range subtree with these narrowed bounds.
22304 -- Set_Fixed_Range constructs the range from real literal values, and
22305 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22306
22307 -- The parent of this range is set to point to the entity so that it is
22308 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22309 -- other scalar types, which are just pointers to the range in the
22310 -- original tree, this would otherwise be an orphan).
22311
22312 -- The tree is left unanalyzed. When the type is frozen, the processing
22313 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22314 -- analyzed, and uses this as an indication that it should complete
22315 -- work on the range (it will know the final small and size values).
22316
22317 procedure Set_Fixed_Range
22318 (E : Entity_Id;
22319 Loc : Source_Ptr;
22320 Lo : Ureal;
22321 Hi : Ureal)
22322 is
22323 S : constant Node_Id :=
22324 Make_Range (Loc,
22325 Low_Bound => Make_Real_Literal (Loc, Lo),
22326 High_Bound => Make_Real_Literal (Loc, Hi));
22327 begin
22328 Set_Scalar_Range (E, S);
22329 Set_Parent (S, E);
22330
22331 -- Before the freeze point, the bounds of a fixed point are universal
22332 -- and carry the corresponding type.
22333
22334 Set_Etype (Low_Bound (S), Universal_Real);
22335 Set_Etype (High_Bound (S), Universal_Real);
22336 end Set_Fixed_Range;
22337
22338 ----------------------------------
22339 -- Set_Scalar_Range_For_Subtype --
22340 ----------------------------------
22341
22342 procedure Set_Scalar_Range_For_Subtype
22343 (Def_Id : Entity_Id;
22344 R : Node_Id;
22345 Subt : Entity_Id)
22346 is
22347 Kind : constant Entity_Kind := Ekind (Def_Id);
22348
22349 begin
22350 -- Defend against previous error
22351
22352 if Nkind (R) = N_Error then
22353 return;
22354 end if;
22355
22356 Set_Scalar_Range (Def_Id, R);
22357
22358 -- We need to link the range into the tree before resolving it so
22359 -- that types that are referenced, including importantly the subtype
22360 -- itself, are properly frozen (Freeze_Expression requires that the
22361 -- expression be properly linked into the tree). Of course if it is
22362 -- already linked in, then we do not disturb the current link.
22363
22364 if No (Parent (R)) then
22365 Set_Parent (R, Def_Id);
22366 end if;
22367
22368 -- Reset the kind of the subtype during analysis of the range, to
22369 -- catch possible premature use in the bounds themselves.
22370
22371 Set_Ekind (Def_Id, E_Void);
22372 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22373 Set_Ekind (Def_Id, Kind);
22374 end Set_Scalar_Range_For_Subtype;
22375
22376 --------------------------------------------------------
22377 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22378 --------------------------------------------------------
22379
22380 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22381 (E : Entity_Id)
22382 is
22383 begin
22384 -- Make sure set if encountered during Expand_To_Stored_Constraint
22385
22386 Set_Stored_Constraint (E, No_Elist);
22387
22388 -- Give it the right value
22389
22390 if Is_Constrained (E) and then Has_Discriminants (E) then
22391 Set_Stored_Constraint (E,
22392 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22393 end if;
22394 end Set_Stored_Constraint_From_Discriminant_Constraint;
22395
22396 -------------------------------------
22397 -- Signed_Integer_Type_Declaration --
22398 -------------------------------------
22399
22400 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22401 Implicit_Base : Entity_Id;
22402 Base_Typ : Entity_Id;
22403 Lo_Val : Uint;
22404 Hi_Val : Uint;
22405 Errs : Boolean := False;
22406 Lo : Node_Id;
22407 Hi : Node_Id;
22408
22409 function Can_Derive_From (E : Entity_Id) return Boolean;
22410 -- Determine whether given bounds allow derivation from specified type
22411
22412 procedure Check_Bound (Expr : Node_Id);
22413 -- Check bound to make sure it is integral and static. If not, post
22414 -- appropriate error message and set Errs flag
22415
22416 ---------------------
22417 -- Can_Derive_From --
22418 ---------------------
22419
22420 -- Note we check both bounds against both end values, to deal with
22421 -- strange types like ones with a range of 0 .. -12341234.
22422
22423 function Can_Derive_From (E : Entity_Id) return Boolean is
22424 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22425 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22426 begin
22427 return Lo <= Lo_Val and then Lo_Val <= Hi
22428 and then
22429 Lo <= Hi_Val and then Hi_Val <= Hi;
22430 end Can_Derive_From;
22431
22432 -----------------
22433 -- Check_Bound --
22434 -----------------
22435
22436 procedure Check_Bound (Expr : Node_Id) is
22437 begin
22438 -- If a range constraint is used as an integer type definition, each
22439 -- bound of the range must be defined by a static expression of some
22440 -- integer type, but the two bounds need not have the same integer
22441 -- type (Negative bounds are allowed.) (RM 3.5.4)
22442
22443 if not Is_Integer_Type (Etype (Expr)) then
22444 Error_Msg_N
22445 ("integer type definition bounds must be of integer type", Expr);
22446 Errs := True;
22447
22448 elsif not Is_OK_Static_Expression (Expr) then
22449 Flag_Non_Static_Expr
22450 ("non-static expression used for integer type bound!", Expr);
22451 Errs := True;
22452
22453 -- Otherwise the bounds are folded into literals
22454
22455 elsif Is_Entity_Name (Expr) then
22456 Fold_Uint (Expr, Expr_Value (Expr), True);
22457 end if;
22458 end Check_Bound;
22459
22460 -- Start of processing for Signed_Integer_Type_Declaration
22461
22462 begin
22463 -- Create an anonymous base type
22464
22465 Implicit_Base :=
22466 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22467
22468 -- Analyze and check the bounds, they can be of any integer type
22469
22470 Lo := Low_Bound (Def);
22471 Hi := High_Bound (Def);
22472
22473 -- Arbitrarily use Integer as the type if either bound had an error
22474
22475 if Hi = Error or else Lo = Error then
22476 Base_Typ := Any_Integer;
22477 Set_Error_Posted (T, True);
22478 Errs := True;
22479
22480 -- Here both bounds are OK expressions
22481
22482 else
22483 Analyze_And_Resolve (Lo, Any_Integer);
22484 Analyze_And_Resolve (Hi, Any_Integer);
22485
22486 Check_Bound (Lo);
22487 Check_Bound (Hi);
22488
22489 if Errs then
22490 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22491 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22492 end if;
22493
22494 -- Find type to derive from
22495
22496 Lo_Val := Expr_Value (Lo);
22497 Hi_Val := Expr_Value (Hi);
22498
22499 if Can_Derive_From (Standard_Short_Short_Integer) then
22500 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22501
22502 elsif Can_Derive_From (Standard_Short_Integer) then
22503 Base_Typ := Base_Type (Standard_Short_Integer);
22504
22505 elsif Can_Derive_From (Standard_Integer) then
22506 Base_Typ := Base_Type (Standard_Integer);
22507
22508 elsif Can_Derive_From (Standard_Long_Integer) then
22509 Base_Typ := Base_Type (Standard_Long_Integer);
22510
22511 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22512 Check_Restriction (No_Long_Long_Integers, Def);
22513 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22514
22515 else
22516 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22517 Error_Msg_N ("integer type definition bounds out of range", Def);
22518 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22519 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22520 end if;
22521 end if;
22522
22523 -- Set the type of the bounds to the implicit base: we cannot set it to
22524 -- the new type, because this would be a forward reference for the code
22525 -- generator and, if the original type is user-defined, this could even
22526 -- lead to spurious semantic errors. Furthermore we do not set it to be
22527 -- universal, because this could make it much larger than needed here.
22528
22529 if not Errs then
22530 Set_Etype (Lo, Implicit_Base);
22531 Set_Etype (Hi, Implicit_Base);
22532 end if;
22533
22534 -- Complete both implicit base and declared first subtype entities. The
22535 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22536 -- are not clobbered when the signed integer type acts as a full view of
22537 -- a private type.
22538
22539 Set_Etype (Implicit_Base, Base_Typ);
22540 Set_Size_Info (Implicit_Base, Base_Typ);
22541 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22542 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22543 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22544
22545 Set_Ekind (T, E_Signed_Integer_Subtype);
22546 Set_Etype (T, Implicit_Base);
22547 Set_Size_Info (T, Implicit_Base);
22548 Inherit_Rep_Item_Chain (T, Implicit_Base);
22549 Set_Scalar_Range (T, Def);
22550 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22551 Set_Is_Constrained (T);
22552 end Signed_Integer_Type_Declaration;
22553
22554 end Sem_Ch3;