]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch3.adb
[Ada] Fix small oversight in latest change for Replace_Discriminants
[thirdparty/gcc.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
82
83 package body Sem_Ch3 is
84
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
88
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
92 -- record type.
93
94 procedure Build_Derived_Type
95 (N : Node_Id;
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
114 -- the type).
115 --
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
118
119 procedure Build_Derived_Access_Type
120 (N : Node_Id;
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
126
127 procedure Build_Derived_Array_Type
128 (N : Node_Id;
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
134
135 procedure Build_Derived_Concurrent_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
142
143 procedure Build_Derived_Enumeration_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
150
151 procedure Build_Derived_Numeric_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
157
158 procedure Build_Derived_Private_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
167
168 procedure Build_Derived_Record_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
182
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
190
191 function Build_Discriminant_Constraints
192 (T : Entity_Id;
193 Def : Node_Id;
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
204
205 procedure Build_Discriminated_Subtype
206 (T : Entity_Id;
207 Def_Id : Entity_Id;
208 Elist : Elist_Id;
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
213 --
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
216 --
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
218 --
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type.
225
226 function Build_Scalar_Bound
227 (Bound : Node_Id;
228 Par_T : Entity_Id;
229 Der_T : Entity_Id) return Node_Id;
230 -- The bounds of a derived scalar type are conversions of the bounds of
231 -- the parent type. Optimize the representation if the bounds are literals.
232 -- Needs a more complete spec--what are the parameters exactly, and what
233 -- exactly is the returned value, and how is Bound affected???
234
235 procedure 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 by used to validate record
666 -- representation claused 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 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1210 -- when it is part of an unconstrained type and subtype expansion
1211 -- is disabled. To avoid back-end problems with shared profiles,
1212 -- use previous subprogram type as the designated type, and then
1213 -- remove scope added above.
1214
1215 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1216 then
1217 Set_Etype (T_Name, T_Name);
1218 Init_Size_Align (T_Name);
1219 Set_Directly_Designated_Type (T_Name,
1220 Scope (Defining_Identifier (F)));
1221 End_Scope;
1222 return;
1223 end if;
1224
1225 while Present (F) loop
1226 if No (Parent (Defining_Identifier (F))) then
1227 Set_Parent (Defining_Identifier (F), F);
1228 end if;
1229
1230 Next (F);
1231 end loop;
1232 end;
1233
1234 Process_Formals (Formals, Parent (T_Def));
1235
1236 -- Special test 2) End_Scope requires that the parent pointer be set
1237 -- to something reasonable, but Itypes don't have parent pointers. So
1238 -- we set it and then unset it ???
1239
1240 Set_Parent (Desig_Type, T_Name);
1241 End_Scope;
1242 Set_Parent (Desig_Type, Empty);
1243 end if;
1244
1245 -- Check for premature usage of the type being defined
1246
1247 Check_For_Premature_Usage (T_Def);
1248
1249 -- The return type and/or any parameter type may be incomplete. Mark the
1250 -- subprogram_type as depending on the incomplete type, so that it can
1251 -- be updated when the full type declaration is seen. This only applies
1252 -- to incomplete types declared in some enclosing scope, not to limited
1253 -- views from other packages.
1254
1255 -- Prior to Ada 2012, access to functions can only have in_parameters.
1256
1257 if Present (Formals) then
1258 Formal := First_Formal (Desig_Type);
1259 while Present (Formal) loop
1260 if Ekind (Formal) /= E_In_Parameter
1261 and then Nkind (T_Def) = N_Access_Function_Definition
1262 and then Ada_Version < Ada_2012
1263 then
1264 Error_Msg_N ("functions can only have IN parameters", Formal);
1265 end if;
1266
1267 if Ekind (Etype (Formal)) = E_Incomplete_Type
1268 and then In_Open_Scopes (Scope (Etype (Formal)))
1269 then
1270 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1271 Set_Has_Delayed_Freeze (Desig_Type);
1272 end if;
1273
1274 Next_Formal (Formal);
1275 end loop;
1276 end if;
1277
1278 -- Check whether an indirect call without actuals may be possible. This
1279 -- is used when resolving calls whose result is then indexed.
1280
1281 May_Need_Actuals (Desig_Type);
1282
1283 -- If the return type is incomplete, this is legal as long as the type
1284 -- is declared in the current scope and will be completed in it (rather
1285 -- than being part of limited view).
1286
1287 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1288 and then not Has_Delayed_Freeze (Desig_Type)
1289 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1290 then
1291 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1292 Set_Has_Delayed_Freeze (Desig_Type);
1293 end if;
1294
1295 Check_Delayed_Subprogram (Desig_Type);
1296
1297 if Protected_Present (T_Def) then
1298 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1299 Set_Convention (Desig_Type, Convention_Protected);
1300 else
1301 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1302 end if;
1303
1304 Set_Can_Use_Internal_Rep (T_Name,
1305 not Always_Compatible_Rep_On_Target);
1306 Set_Etype (T_Name, T_Name);
1307 Init_Size_Align (T_Name);
1308 Set_Directly_Designated_Type (T_Name, Desig_Type);
1309
1310 -- If the access_to_subprogram is not declared at the library level,
1311 -- it can only point to subprograms that are at the same or deeper
1312 -- accessibility level. The corresponding subprogram type might
1313 -- require an activation record when compiling for C.
1314
1315 Set_Needs_Activation_Record (Desig_Type,
1316 not Is_Library_Level_Entity (T_Name));
1317
1318 Generate_Reference_To_Formals (T_Name);
1319
1320 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1321
1322 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1323
1324 Check_Restriction (No_Access_Subprograms, T_Def);
1325 end Access_Subprogram_Declaration;
1326
1327 ----------------------------
1328 -- Access_Type_Declaration --
1329 ----------------------------
1330
1331 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1332 P : constant Node_Id := Parent (Def);
1333 S : constant Node_Id := Subtype_Indication (Def);
1334
1335 Full_Desig : Entity_Id;
1336
1337 begin
1338 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1339
1340 -- Check for permissible use of incomplete type
1341
1342 if Nkind (S) /= N_Subtype_Indication then
1343 Analyze (S);
1344
1345 if Present (Entity (S))
1346 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1347 then
1348 Set_Directly_Designated_Type (T, Entity (S));
1349
1350 -- If the designated type is a limited view, we cannot tell if
1351 -- the full view contains tasks, and there is no way to handle
1352 -- that full view in a client. We create a master entity for the
1353 -- scope, which will be used when a client determines that one
1354 -- is needed.
1355
1356 if From_Limited_With (Entity (S))
1357 and then not Is_Class_Wide_Type (Entity (S))
1358 then
1359 Set_Ekind (T, E_Access_Type);
1360 Build_Master_Entity (T);
1361 Build_Master_Renaming (T);
1362 end if;
1363
1364 else
1365 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1366 end if;
1367
1368 -- If the access definition is of the form: ACCESS NOT NULL ..
1369 -- the subtype indication must be of an access type. Create
1370 -- a null-excluding subtype of it.
1371
1372 if Null_Excluding_Subtype (Def) then
1373 if not Is_Access_Type (Entity (S)) then
1374 Error_Msg_N ("null exclusion must apply to access type", Def);
1375
1376 else
1377 declare
1378 Loc : constant Source_Ptr := Sloc (S);
1379 Decl : Node_Id;
1380 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1381
1382 begin
1383 Decl :=
1384 Make_Subtype_Declaration (Loc,
1385 Defining_Identifier => Nam,
1386 Subtype_Indication =>
1387 New_Occurrence_Of (Entity (S), Loc));
1388 Set_Null_Exclusion_Present (Decl);
1389 Insert_Before (Parent (Def), Decl);
1390 Analyze (Decl);
1391 Set_Entity (S, Nam);
1392 end;
1393 end if;
1394 end if;
1395
1396 else
1397 Set_Directly_Designated_Type (T,
1398 Process_Subtype (S, P, T, 'P'));
1399 end if;
1400
1401 if All_Present (Def) or Constant_Present (Def) then
1402 Set_Ekind (T, E_General_Access_Type);
1403 else
1404 Set_Ekind (T, E_Access_Type);
1405 end if;
1406
1407 Full_Desig := Designated_Type (T);
1408
1409 if Base_Type (Full_Desig) = T then
1410 Error_Msg_N ("access type cannot designate itself", S);
1411
1412 -- In Ada 2005, the type may have a limited view through some unit in
1413 -- its own context, allowing the following circularity that cannot be
1414 -- detected earlier.
1415
1416 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1417 then
1418 Error_Msg_N
1419 ("access type cannot designate its own class-wide type", S);
1420
1421 -- Clean up indication of tagged status to prevent cascaded errors
1422
1423 Set_Is_Tagged_Type (T, False);
1424 end if;
1425
1426 Set_Etype (T, T);
1427
1428 -- If the type has appeared already in a with_type clause, it is frozen
1429 -- and the pointer size is already set. Else, initialize.
1430
1431 if not From_Limited_With (T) then
1432 Init_Size_Align (T);
1433 end if;
1434
1435 -- Note that Has_Task is always false, since the access type itself
1436 -- is not a task type. See Einfo for more description on this point.
1437 -- Exactly the same consideration applies to Has_Controlled_Component
1438 -- and to Has_Protected.
1439
1440 Set_Has_Task (T, False);
1441 Set_Has_Protected (T, False);
1442 Set_Has_Timing_Event (T, False);
1443 Set_Has_Controlled_Component (T, False);
1444
1445 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1446 -- problems where an incomplete view of this entity has been previously
1447 -- established by a limited with and an overlaid version of this field
1448 -- (Stored_Constraint) was initialized for the incomplete view.
1449
1450 -- This reset is performed in most cases except where the access type
1451 -- has been created for the purposes of allocating or deallocating a
1452 -- build-in-place object. Such access types have explicitly set pools
1453 -- and finalization masters.
1454
1455 if No (Associated_Storage_Pool (T)) then
1456 Set_Finalization_Master (T, Empty);
1457 end if;
1458
1459 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1460 -- attributes
1461
1462 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1463 Set_Is_Access_Constant (T, Constant_Present (Def));
1464 end Access_Type_Declaration;
1465
1466 ----------------------------------
1467 -- Add_Interface_Tag_Components --
1468 ----------------------------------
1469
1470 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1471 Loc : constant Source_Ptr := Sloc (N);
1472 L : List_Id;
1473 Last_Tag : Node_Id;
1474
1475 procedure Add_Tag (Iface : Entity_Id);
1476 -- Add tag for one of the progenitor interfaces
1477
1478 -------------
1479 -- Add_Tag --
1480 -------------
1481
1482 procedure Add_Tag (Iface : Entity_Id) is
1483 Decl : Node_Id;
1484 Def : Node_Id;
1485 Tag : Entity_Id;
1486 Offset : Entity_Id;
1487
1488 begin
1489 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1490
1491 -- This is a reasonable place to propagate predicates
1492
1493 if Has_Predicates (Iface) then
1494 Set_Has_Predicates (Typ);
1495 end if;
1496
1497 Def :=
1498 Make_Component_Definition (Loc,
1499 Aliased_Present => True,
1500 Subtype_Indication =>
1501 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1502
1503 Tag := Make_Temporary (Loc, 'V');
1504
1505 Decl :=
1506 Make_Component_Declaration (Loc,
1507 Defining_Identifier => Tag,
1508 Component_Definition => Def);
1509
1510 Analyze_Component_Declaration (Decl);
1511
1512 Set_Analyzed (Decl);
1513 Set_Ekind (Tag, E_Component);
1514 Set_Is_Tag (Tag);
1515 Set_Is_Aliased (Tag);
1516 Set_Is_Independent (Tag);
1517 Set_Related_Type (Tag, Iface);
1518 Init_Component_Location (Tag);
1519
1520 pragma Assert (Is_Frozen (Iface));
1521
1522 Set_DT_Entry_Count (Tag,
1523 DT_Entry_Count (First_Entity (Iface)));
1524
1525 if No (Last_Tag) then
1526 Prepend (Decl, L);
1527 else
1528 Insert_After (Last_Tag, Decl);
1529 end if;
1530
1531 Last_Tag := Decl;
1532
1533 -- If the ancestor has discriminants we need to give special support
1534 -- to store the offset_to_top value of the secondary dispatch tables.
1535 -- For this purpose we add a supplementary component just after the
1536 -- field that contains the tag associated with each secondary DT.
1537
1538 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1539 Def :=
1540 Make_Component_Definition (Loc,
1541 Subtype_Indication =>
1542 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1543
1544 Offset := Make_Temporary (Loc, 'V');
1545
1546 Decl :=
1547 Make_Component_Declaration (Loc,
1548 Defining_Identifier => Offset,
1549 Component_Definition => Def);
1550
1551 Analyze_Component_Declaration (Decl);
1552
1553 Set_Analyzed (Decl);
1554 Set_Ekind (Offset, E_Component);
1555 Set_Is_Aliased (Offset);
1556 Set_Is_Independent (Offset);
1557 Set_Related_Type (Offset, Iface);
1558 Init_Component_Location (Offset);
1559 Insert_After (Last_Tag, Decl);
1560 Last_Tag := Decl;
1561 end if;
1562 end Add_Tag;
1563
1564 -- Local variables
1565
1566 Elmt : Elmt_Id;
1567 Ext : Node_Id;
1568 Comp : Node_Id;
1569
1570 -- Start of processing for Add_Interface_Tag_Components
1571
1572 begin
1573 if not RTE_Available (RE_Interface_Tag) then
1574 Error_Msg
1575 ("(Ada 2005) interface types not supported by this run-time!",
1576 Sloc (N));
1577 return;
1578 end if;
1579
1580 if Ekind (Typ) /= E_Record_Type
1581 or else (Is_Concurrent_Record_Type (Typ)
1582 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1583 or else (not Is_Concurrent_Record_Type (Typ)
1584 and then No (Interfaces (Typ))
1585 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1586 then
1587 return;
1588 end if;
1589
1590 -- Find the current last tag
1591
1592 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1593 Ext := Record_Extension_Part (Type_Definition (N));
1594 else
1595 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1596 Ext := Type_Definition (N);
1597 end if;
1598
1599 Last_Tag := Empty;
1600
1601 if not (Present (Component_List (Ext))) then
1602 Set_Null_Present (Ext, False);
1603 L := New_List;
1604 Set_Component_List (Ext,
1605 Make_Component_List (Loc,
1606 Component_Items => L,
1607 Null_Present => False));
1608 else
1609 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1610 L := Component_Items
1611 (Component_List
1612 (Record_Extension_Part
1613 (Type_Definition (N))));
1614 else
1615 L := Component_Items
1616 (Component_List
1617 (Type_Definition (N)));
1618 end if;
1619
1620 -- Find the last tag component
1621
1622 Comp := First (L);
1623 while Present (Comp) loop
1624 if Nkind (Comp) = N_Component_Declaration
1625 and then Is_Tag (Defining_Identifier (Comp))
1626 then
1627 Last_Tag := Comp;
1628 end if;
1629
1630 Next (Comp);
1631 end loop;
1632 end if;
1633
1634 -- At this point L references the list of components and Last_Tag
1635 -- references the current last tag (if any). Now we add the tag
1636 -- corresponding with all the interfaces that are not implemented
1637 -- by the parent.
1638
1639 if Present (Interfaces (Typ)) then
1640 Elmt := First_Elmt (Interfaces (Typ));
1641 while Present (Elmt) loop
1642 Add_Tag (Node (Elmt));
1643 Next_Elmt (Elmt);
1644 end loop;
1645 end if;
1646 end Add_Interface_Tag_Components;
1647
1648 -------------------------------------
1649 -- Add_Internal_Interface_Entities --
1650 -------------------------------------
1651
1652 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1653 Elmt : Elmt_Id;
1654 Iface : Entity_Id;
1655 Iface_Elmt : Elmt_Id;
1656 Iface_Prim : Entity_Id;
1657 Ifaces_List : Elist_Id;
1658 New_Subp : Entity_Id := Empty;
1659 Prim : Entity_Id;
1660 Restore_Scope : Boolean := False;
1661
1662 begin
1663 pragma Assert (Ada_Version >= Ada_2005
1664 and then Is_Record_Type (Tagged_Type)
1665 and then Is_Tagged_Type (Tagged_Type)
1666 and then Has_Interfaces (Tagged_Type)
1667 and then not Is_Interface (Tagged_Type));
1668
1669 -- Ensure that the internal entities are added to the scope of the type
1670
1671 if Scope (Tagged_Type) /= Current_Scope then
1672 Push_Scope (Scope (Tagged_Type));
1673 Restore_Scope := True;
1674 end if;
1675
1676 Collect_Interfaces (Tagged_Type, Ifaces_List);
1677
1678 Iface_Elmt := First_Elmt (Ifaces_List);
1679 while Present (Iface_Elmt) loop
1680 Iface := Node (Iface_Elmt);
1681
1682 -- Originally we excluded here from this processing interfaces that
1683 -- are parents of Tagged_Type because their primitives are located
1684 -- in the primary dispatch table (and hence no auxiliary internal
1685 -- entities are required to handle secondary dispatch tables in such
1686 -- case). However, these auxiliary entities are also required to
1687 -- handle derivations of interfaces in formals of generics (see
1688 -- Derive_Subprograms).
1689
1690 Elmt := First_Elmt (Primitive_Operations (Iface));
1691 while Present (Elmt) loop
1692 Iface_Prim := Node (Elmt);
1693
1694 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1695 Prim :=
1696 Find_Primitive_Covering_Interface
1697 (Tagged_Type => Tagged_Type,
1698 Iface_Prim => Iface_Prim);
1699
1700 if No (Prim) and then Serious_Errors_Detected > 0 then
1701 goto Continue;
1702 end if;
1703
1704 pragma Assert (Present (Prim));
1705
1706 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1707 -- differs from the name of the interface primitive then it is
1708 -- a private primitive inherited from a parent type. In such
1709 -- case, given that Tagged_Type covers the interface, the
1710 -- inherited private primitive becomes visible. For such
1711 -- purpose we add a new entity that renames the inherited
1712 -- private primitive.
1713
1714 if Chars (Prim) /= Chars (Iface_Prim) then
1715 pragma Assert (Has_Suffix (Prim, 'P'));
1716 Derive_Subprogram
1717 (New_Subp => New_Subp,
1718 Parent_Subp => Iface_Prim,
1719 Derived_Type => Tagged_Type,
1720 Parent_Type => Iface);
1721 Set_Alias (New_Subp, Prim);
1722 Set_Is_Abstract_Subprogram
1723 (New_Subp, Is_Abstract_Subprogram (Prim));
1724 end if;
1725
1726 Derive_Subprogram
1727 (New_Subp => New_Subp,
1728 Parent_Subp => Iface_Prim,
1729 Derived_Type => Tagged_Type,
1730 Parent_Type => Iface);
1731
1732 declare
1733 Anc : Entity_Id;
1734 begin
1735 if Is_Inherited_Operation (Prim)
1736 and then Present (Alias (Prim))
1737 then
1738 Anc := Alias (Prim);
1739 else
1740 Anc := Overridden_Operation (Prim);
1741 end if;
1742
1743 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1744 -- nonconforming preconditions in both an ancestor and
1745 -- a progenitor operation.
1746
1747 -- If the operation is a primitive wrapper it is an explicit
1748 -- (overriding) operqtion and all is fine.
1749
1750 if Present (Anc)
1751 and then Has_Non_Trivial_Precondition (Anc)
1752 and then Has_Non_Trivial_Precondition (Iface_Prim)
1753 then
1754 if Is_Abstract_Subprogram (Prim)
1755 or else
1756 (Ekind (Prim) = E_Procedure
1757 and then Nkind (Parent (Prim)) =
1758 N_Procedure_Specification
1759 and then Null_Present (Parent (Prim)))
1760 or else Is_Primitive_Wrapper (Prim)
1761 then
1762 null;
1763
1764 -- The operation is inherited and must be overridden
1765
1766 elsif not Comes_From_Source (Prim) then
1767 Error_Msg_NE
1768 ("&inherits non-conforming preconditions and must "
1769 & "be overridden (RM 6.1.1 (10-16)",
1770 Parent (Tagged_Type), Prim);
1771 end if;
1772 end if;
1773 end;
1774
1775 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1776 -- associated with interface types. These entities are
1777 -- only registered in the list of primitives of its
1778 -- corresponding tagged type because they are only used
1779 -- to fill the contents of the secondary dispatch tables.
1780 -- Therefore they are removed from the homonym chains.
1781
1782 Set_Is_Hidden (New_Subp);
1783 Set_Is_Internal (New_Subp);
1784 Set_Alias (New_Subp, Prim);
1785 Set_Is_Abstract_Subprogram
1786 (New_Subp, Is_Abstract_Subprogram (Prim));
1787 Set_Interface_Alias (New_Subp, Iface_Prim);
1788
1789 -- If the returned type is an interface then propagate it to
1790 -- the returned type. Needed by the thunk to generate the code
1791 -- which displaces "this" to reference the corresponding
1792 -- secondary dispatch table in the returned object.
1793
1794 if Is_Interface (Etype (Iface_Prim)) then
1795 Set_Etype (New_Subp, Etype (Iface_Prim));
1796 end if;
1797
1798 -- Internal entities associated with interface types are only
1799 -- registered in the list of primitives of the tagged type.
1800 -- They are only used to fill the contents of the secondary
1801 -- dispatch tables. Therefore they are not needed in the
1802 -- homonym chains.
1803
1804 Remove_Homonym (New_Subp);
1805
1806 -- Hidden entities associated with interfaces must have set
1807 -- the Has_Delay_Freeze attribute to ensure that, in case
1808 -- of locally defined tagged types (or compiling with static
1809 -- dispatch tables generation disabled) the corresponding
1810 -- entry of the secondary dispatch table is filled when such
1811 -- an entity is frozen. This is an expansion activity that must
1812 -- be suppressed for ASIS because it leads to gigi elaboration
1813 -- issues in annotate mode.
1814
1815 if not ASIS_Mode then
1816 Set_Has_Delayed_Freeze (New_Subp);
1817 end if;
1818 end if;
1819
1820 <<Continue>>
1821 Next_Elmt (Elmt);
1822 end loop;
1823
1824 Next_Elmt (Iface_Elmt);
1825 end loop;
1826
1827 if Restore_Scope then
1828 Pop_Scope;
1829 end if;
1830 end Add_Internal_Interface_Entities;
1831
1832 -----------------------------------
1833 -- Analyze_Component_Declaration --
1834 -----------------------------------
1835
1836 procedure Analyze_Component_Declaration (N : Node_Id) is
1837 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1838 Id : constant Entity_Id := Defining_Identifier (N);
1839 E : constant Node_Id := Expression (N);
1840 Typ : constant Node_Id :=
1841 Subtype_Indication (Component_Definition (N));
1842 T : Entity_Id;
1843 P : Entity_Id;
1844
1845 function Contains_POC (Constr : Node_Id) return Boolean;
1846 -- Determines whether a constraint uses the discriminant of a record
1847 -- type thus becoming a per-object constraint (POC).
1848
1849 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1850 -- Typ is the type of the current component, check whether this type is
1851 -- a limited type. Used to validate declaration against that of
1852 -- enclosing record.
1853
1854 ------------------
1855 -- Contains_POC --
1856 ------------------
1857
1858 function Contains_POC (Constr : Node_Id) return Boolean is
1859 begin
1860 -- Prevent cascaded errors
1861
1862 if Error_Posted (Constr) then
1863 return False;
1864 end if;
1865
1866 case Nkind (Constr) is
1867 when N_Attribute_Reference =>
1868 return Attribute_Name (Constr) = Name_Access
1869 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1870
1871 when N_Discriminant_Association =>
1872 return Denotes_Discriminant (Expression (Constr));
1873
1874 when N_Identifier =>
1875 return Denotes_Discriminant (Constr);
1876
1877 when N_Index_Or_Discriminant_Constraint =>
1878 declare
1879 IDC : Node_Id;
1880
1881 begin
1882 IDC := First (Constraints (Constr));
1883 while Present (IDC) loop
1884
1885 -- One per-object constraint is sufficient
1886
1887 if Contains_POC (IDC) then
1888 return True;
1889 end if;
1890
1891 Next (IDC);
1892 end loop;
1893
1894 return False;
1895 end;
1896
1897 when N_Range =>
1898 return Denotes_Discriminant (Low_Bound (Constr))
1899 or else
1900 Denotes_Discriminant (High_Bound (Constr));
1901
1902 when N_Range_Constraint =>
1903 return Denotes_Discriminant (Range_Expression (Constr));
1904
1905 when others =>
1906 return False;
1907 end case;
1908 end Contains_POC;
1909
1910 ----------------------
1911 -- Is_Known_Limited --
1912 ----------------------
1913
1914 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1915 P : constant Entity_Id := Etype (Typ);
1916 R : constant Entity_Id := Root_Type (Typ);
1917
1918 begin
1919 if Is_Limited_Record (Typ) then
1920 return True;
1921
1922 -- If the root type is limited (and not a limited interface) so is
1923 -- the current type.
1924
1925 elsif Is_Limited_Record (R)
1926 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1927 then
1928 return True;
1929
1930 -- Else the type may have a limited interface progenitor, but a
1931 -- limited record parent that is not an interface.
1932
1933 elsif R /= P
1934 and then Is_Limited_Record (P)
1935 and then not Is_Interface (P)
1936 then
1937 return True;
1938
1939 else
1940 return False;
1941 end if;
1942 end Is_Known_Limited;
1943
1944 -- Start of processing for Analyze_Component_Declaration
1945
1946 begin
1947 Generate_Definition (Id);
1948 Enter_Name (Id);
1949
1950 if Present (Typ) then
1951 T := Find_Type_Of_Object
1952 (Subtype_Indication (Component_Definition (N)), N);
1953
1954 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1955 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1956 end if;
1957
1958 -- Ada 2005 (AI-230): Access Definition case
1959
1960 else
1961 pragma Assert (Present
1962 (Access_Definition (Component_Definition (N))));
1963
1964 T := Access_Definition
1965 (Related_Nod => N,
1966 N => Access_Definition (Component_Definition (N)));
1967 Set_Is_Local_Anonymous_Access (T);
1968
1969 -- Ada 2005 (AI-254)
1970
1971 if Present (Access_To_Subprogram_Definition
1972 (Access_Definition (Component_Definition (N))))
1973 and then Protected_Present (Access_To_Subprogram_Definition
1974 (Access_Definition
1975 (Component_Definition (N))))
1976 then
1977 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1978 end if;
1979 end if;
1980
1981 -- If the subtype is a constrained subtype of the enclosing record,
1982 -- (which must have a partial view) the back-end does not properly
1983 -- handle the recursion. Rewrite the component declaration with an
1984 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1985 -- the tree directly because side effects have already been removed from
1986 -- discriminant constraints.
1987
1988 if Ekind (T) = E_Access_Subtype
1989 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1990 and then Comes_From_Source (T)
1991 and then Nkind (Parent (T)) = N_Subtype_Declaration
1992 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1993 then
1994 Rewrite
1995 (Subtype_Indication (Component_Definition (N)),
1996 New_Copy_Tree (Subtype_Indication (Parent (T))));
1997 T := Find_Type_Of_Object
1998 (Subtype_Indication (Component_Definition (N)), N);
1999 end if;
2000
2001 -- If the component declaration includes a default expression, then we
2002 -- check that the component is not of a limited type (RM 3.7(5)),
2003 -- and do the special preanalysis of the expression (see section on
2004 -- "Handling of Default and Per-Object Expressions" in the spec of
2005 -- package Sem).
2006
2007 if Present (E) then
2008 Check_SPARK_05_Restriction ("default expression is not allowed", E);
2009 Preanalyze_Default_Expression (E, T);
2010 Check_Initialization (T, E);
2011
2012 if Ada_Version >= Ada_2005
2013 and then Ekind (T) = E_Anonymous_Access_Type
2014 and then Etype (E) /= Any_Type
2015 then
2016 -- Check RM 3.9.2(9): "if the expected type for an expression is
2017 -- an anonymous access-to-specific tagged type, then the object
2018 -- designated by the expression shall not be dynamically tagged
2019 -- unless it is a controlling operand in a call on a dispatching
2020 -- operation"
2021
2022 if Is_Tagged_Type (Directly_Designated_Type (T))
2023 and then
2024 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2025 and then
2026 Ekind (Directly_Designated_Type (Etype (E))) =
2027 E_Class_Wide_Type
2028 then
2029 Error_Msg_N
2030 ("access to specific tagged type required (RM 3.9.2(9))", E);
2031 end if;
2032
2033 -- (Ada 2005: AI-230): Accessibility check for anonymous
2034 -- components
2035
2036 if Type_Access_Level (Etype (E)) >
2037 Deepest_Type_Access_Level (T)
2038 then
2039 Error_Msg_N
2040 ("expression has deeper access level than component " &
2041 "(RM 3.10.2 (12.2))", E);
2042 end if;
2043
2044 -- The initialization expression is a reference to an access
2045 -- discriminant. The type of the discriminant is always deeper
2046 -- than any access type.
2047
2048 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2049 and then Is_Entity_Name (E)
2050 and then Ekind (Entity (E)) = E_In_Parameter
2051 and then Present (Discriminal_Link (Entity (E)))
2052 then
2053 Error_Msg_N
2054 ("discriminant has deeper accessibility level than target",
2055 E);
2056 end if;
2057 end if;
2058 end if;
2059
2060 -- Avoid reporting spurious errors if the component is initialized with
2061 -- a raise expression (which is legal in any expression context)
2062
2063 if Present (E)
2064 and then
2065 (Nkind (E) = N_Raise_Expression
2066 or else (Nkind (E) = N_Qualified_Expression
2067 and then Nkind (Expression (E)) = N_Raise_Expression))
2068 then
2069 null;
2070
2071 -- The parent type may be a private view with unknown discriminants,
2072 -- and thus unconstrained. Regular components must be constrained.
2073
2074 elsif not Is_Definite_Subtype (T)
2075 and then Chars (Id) /= Name_uParent
2076 then
2077 if Is_Class_Wide_Type (T) then
2078 Error_Msg_N
2079 ("class-wide subtype with unknown discriminants" &
2080 " in component declaration",
2081 Subtype_Indication (Component_Definition (N)));
2082 else
2083 Error_Msg_N
2084 ("unconstrained subtype in component declaration",
2085 Subtype_Indication (Component_Definition (N)));
2086 end if;
2087
2088 -- Components cannot be abstract, except for the special case of
2089 -- the _Parent field (case of extending an abstract tagged type)
2090
2091 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2092 Error_Msg_N ("type of a component cannot be abstract", N);
2093 end if;
2094
2095 Set_Etype (Id, T);
2096
2097 if Aliased_Present (Component_Definition (N)) then
2098 Set_Is_Aliased (Id);
2099
2100 -- AI12-001: All aliased objects are considered to be specified as
2101 -- independently addressable (RM C.6(8.1/4)).
2102
2103 Set_Is_Independent (Id);
2104 end if;
2105
2106 -- The component declaration may have a per-object constraint, set
2107 -- the appropriate flag in the defining identifier of the subtype.
2108
2109 if Present (Subtype_Indication (Component_Definition (N))) then
2110 declare
2111 Sindic : constant Node_Id :=
2112 Subtype_Indication (Component_Definition (N));
2113 begin
2114 if Nkind (Sindic) = N_Subtype_Indication
2115 and then Present (Constraint (Sindic))
2116 and then Contains_POC (Constraint (Sindic))
2117 then
2118 Set_Has_Per_Object_Constraint (Id);
2119 end if;
2120 end;
2121 end if;
2122
2123 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2124 -- out some static checks.
2125
2126 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2127 Null_Exclusion_Static_Checks (N);
2128 end if;
2129
2130 -- If this component is private (or depends on a private type), flag the
2131 -- record type to indicate that some operations are not available.
2132
2133 P := Private_Component (T);
2134
2135 if Present (P) then
2136
2137 -- Check for circular definitions
2138
2139 if P = Any_Type then
2140 Set_Etype (Id, Any_Type);
2141
2142 -- There is a gap in the visibility of operations only if the
2143 -- component type is not defined in the scope of the record type.
2144
2145 elsif Scope (P) = Scope (Current_Scope) then
2146 null;
2147
2148 elsif Is_Limited_Type (P) then
2149 Set_Is_Limited_Composite (Current_Scope);
2150
2151 else
2152 Set_Is_Private_Composite (Current_Scope);
2153 end if;
2154 end if;
2155
2156 if P /= Any_Type
2157 and then Is_Limited_Type (T)
2158 and then Chars (Id) /= Name_uParent
2159 and then Is_Tagged_Type (Current_Scope)
2160 then
2161 if Is_Derived_Type (Current_Scope)
2162 and then not Is_Known_Limited (Current_Scope)
2163 then
2164 Error_Msg_N
2165 ("extension of nonlimited type cannot have limited components",
2166 N);
2167
2168 if Is_Interface (Root_Type (Current_Scope)) then
2169 Error_Msg_N
2170 ("\limitedness is not inherited from limited interface", N);
2171 Error_Msg_N ("\add LIMITED to type indication", N);
2172 end if;
2173
2174 Explain_Limited_Type (T, N);
2175 Set_Etype (Id, Any_Type);
2176 Set_Is_Limited_Composite (Current_Scope, False);
2177
2178 elsif not Is_Derived_Type (Current_Scope)
2179 and then not Is_Limited_Record (Current_Scope)
2180 and then not Is_Concurrent_Type (Current_Scope)
2181 then
2182 Error_Msg_N
2183 ("nonlimited tagged type cannot have limited components", N);
2184 Explain_Limited_Type (T, N);
2185 Set_Etype (Id, Any_Type);
2186 Set_Is_Limited_Composite (Current_Scope, False);
2187 end if;
2188 end if;
2189
2190 -- If the component is an unconstrained task or protected type with
2191 -- discriminants, the component and the enclosing record are limited
2192 -- and the component is constrained by its default values. Compute
2193 -- its actual subtype, else it may be allocated the maximum size by
2194 -- the backend, and possibly overflow.
2195
2196 if Is_Concurrent_Type (T)
2197 and then not Is_Constrained (T)
2198 and then Has_Discriminants (T)
2199 and then not Has_Discriminants (Current_Scope)
2200 then
2201 declare
2202 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2203
2204 begin
2205 Set_Etype (Id, Act_T);
2206
2207 -- Rewrite component definition to use the constrained subtype
2208
2209 Rewrite (Component_Definition (N),
2210 Make_Component_Definition (Loc,
2211 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2212 end;
2213 end if;
2214
2215 Set_Original_Record_Component (Id, Id);
2216
2217 if Has_Aspects (N) then
2218 Analyze_Aspect_Specifications (N, Id);
2219 end if;
2220
2221 Analyze_Dimension (N);
2222 end Analyze_Component_Declaration;
2223
2224 --------------------------
2225 -- Analyze_Declarations --
2226 --------------------------
2227
2228 procedure Analyze_Declarations (L : List_Id) is
2229 Decl : Node_Id;
2230
2231 procedure Adjust_Decl;
2232 -- Adjust Decl not to include implicit label declarations, since these
2233 -- have strange Sloc values that result in elaboration check problems.
2234 -- (They have the sloc of the label as found in the source, and that
2235 -- is ahead of the current declarative part).
2236
2237 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2238 -- Create the subprogram bodies which verify the run-time semantics of
2239 -- the pragmas listed below for each elibigle type found in declarative
2240 -- list Decls. The pragmas are:
2241 --
2242 -- Default_Initial_Condition
2243 -- Invariant
2244 -- Type_Invariant
2245 --
2246 -- Context denotes the owner of the declarative list.
2247
2248 procedure Check_Entry_Contracts;
2249 -- Perform a preanalysis of the pre- and postconditions of an entry
2250 -- declaration. This must be done before full resolution and creation
2251 -- of the parameter block, etc. to catch illegal uses within the
2252 -- contract expression. Full analysis of the expression is done when
2253 -- the contract is processed.
2254
2255 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2256 -- Check if a nested package has entities within it that rely on library
2257 -- level private types where the full view has not been completed for
2258 -- the purposes of checking if it is acceptable to freeze an expression
2259 -- function at the point of declaration.
2260
2261 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2262 -- Determine whether Body_Decl denotes the body of a late controlled
2263 -- primitive (either Initialize, Adjust or Finalize). If this is the
2264 -- case, add a proper spec if the body lacks one. The spec is inserted
2265 -- before Body_Decl and immediately analyzed.
2266
2267 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2268 -- Spec_Id is the entity of a package that may define abstract states,
2269 -- and in the case of a child unit, whose ancestors may define abstract
2270 -- states. If the states have partial visible refinement, remove the
2271 -- partial visibility of each constituent at the end of the package
2272 -- spec and body declarations.
2273
2274 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2275 -- Spec_Id is the entity of a package that may define abstract states.
2276 -- If the states have visible refinement, remove the visibility of each
2277 -- constituent at the end of the package body declaration.
2278
2279 procedure Resolve_Aspects;
2280 -- Utility to resolve the expressions of aspects at the end of a list of
2281 -- declarations, or before a declaration that freezes previous entities,
2282 -- such as in a subprogram body.
2283
2284 -----------------
2285 -- Adjust_Decl --
2286 -----------------
2287
2288 procedure Adjust_Decl is
2289 begin
2290 while Present (Prev (Decl))
2291 and then Nkind (Decl) = N_Implicit_Label_Declaration
2292 loop
2293 Prev (Decl);
2294 end loop;
2295 end Adjust_Decl;
2296
2297 ----------------------------
2298 -- Build_Assertion_Bodies --
2299 ----------------------------
2300
2301 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2302 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2303 -- Create the subprogram bodies which verify the run-time semantics
2304 -- of the pragmas listed below for type Typ. The pragmas are:
2305 --
2306 -- Default_Initial_Condition
2307 -- Invariant
2308 -- Type_Invariant
2309
2310 -------------------------------------
2311 -- Build_Assertion_Bodies_For_Type --
2312 -------------------------------------
2313
2314 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2315 begin
2316 -- Preanalyze and resolve the Default_Initial_Condition assertion
2317 -- expression at the end of the declarations to catch any errors.
2318
2319 if Has_DIC (Typ) then
2320 Build_DIC_Procedure_Body (Typ);
2321 end if;
2322
2323 if Nkind (Context) = N_Package_Specification then
2324
2325 -- Preanalyze and resolve the class-wide invariants of an
2326 -- interface at the end of whichever declarative part has the
2327 -- interface type. Note that an interface may be declared in
2328 -- any non-package declarative part, but reaching the end of
2329 -- such a declarative part will always freeze the type and
2330 -- generate the invariant procedure (see Freeze_Type).
2331
2332 if Is_Interface (Typ) then
2333
2334 -- Interfaces are treated as the partial view of a private
2335 -- type, in order to achieve uniformity with the general
2336 -- case. As a result, an interface receives only a "partial"
2337 -- invariant procedure, which is never called.
2338
2339 if Has_Own_Invariants (Typ) then
2340 Build_Invariant_Procedure_Body
2341 (Typ => Typ,
2342 Partial_Invariant => True);
2343 end if;
2344
2345 -- Preanalyze and resolve the invariants of a private type
2346 -- at the end of the visible declarations to catch potential
2347 -- errors. Inherited class-wide invariants are not included
2348 -- because they have already been resolved.
2349
2350 elsif Decls = Visible_Declarations (Context)
2351 and then Ekind_In (Typ, E_Limited_Private_Type,
2352 E_Private_Type,
2353 E_Record_Type_With_Private)
2354 and then Has_Own_Invariants (Typ)
2355 then
2356 Build_Invariant_Procedure_Body
2357 (Typ => Typ,
2358 Partial_Invariant => True);
2359
2360 -- Preanalyze and resolve the invariants of a private type's
2361 -- full view at the end of the private declarations to catch
2362 -- potential errors.
2363
2364 elsif Decls = Private_Declarations (Context)
2365 and then not Is_Private_Type (Typ)
2366 and then Has_Private_Declaration (Typ)
2367 and then Has_Invariants (Typ)
2368 then
2369 Build_Invariant_Procedure_Body (Typ);
2370 end if;
2371 end if;
2372 end Build_Assertion_Bodies_For_Type;
2373
2374 -- Local variables
2375
2376 Decl : Node_Id;
2377 Decl_Id : Entity_Id;
2378
2379 -- Start of processing for Build_Assertion_Bodies
2380
2381 begin
2382 Decl := First (Decls);
2383 while Present (Decl) loop
2384 if Is_Declaration (Decl) then
2385 Decl_Id := Defining_Entity (Decl);
2386
2387 if Is_Type (Decl_Id) then
2388 Build_Assertion_Bodies_For_Type (Decl_Id);
2389 end if;
2390 end if;
2391
2392 Next (Decl);
2393 end loop;
2394 end Build_Assertion_Bodies;
2395
2396 ---------------------------
2397 -- Check_Entry_Contracts --
2398 ---------------------------
2399
2400 procedure Check_Entry_Contracts is
2401 ASN : Node_Id;
2402 Ent : Entity_Id;
2403 Exp : Node_Id;
2404
2405 begin
2406 Ent := First_Entity (Current_Scope);
2407 while Present (Ent) loop
2408
2409 -- This only concerns entries with pre/postconditions
2410
2411 if Ekind (Ent) = E_Entry
2412 and then Present (Contract (Ent))
2413 and then Present (Pre_Post_Conditions (Contract (Ent)))
2414 then
2415 ASN := Pre_Post_Conditions (Contract (Ent));
2416 Push_Scope (Ent);
2417 Install_Formals (Ent);
2418
2419 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2420 -- is performed on a copy of the pragma expression, to prevent
2421 -- modifying the original expression.
2422
2423 while Present (ASN) loop
2424 if Nkind (ASN) = N_Pragma then
2425 Exp :=
2426 New_Copy_Tree
2427 (Expression
2428 (First (Pragma_Argument_Associations (ASN))));
2429 Set_Parent (Exp, ASN);
2430
2431 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2432 end if;
2433
2434 ASN := Next_Pragma (ASN);
2435 end loop;
2436
2437 End_Scope;
2438 end if;
2439
2440 Next_Entity (Ent);
2441 end loop;
2442 end Check_Entry_Contracts;
2443
2444 ----------------------------------
2445 -- Contains_Lib_Incomplete_Type --
2446 ----------------------------------
2447
2448 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2449 Curr : Entity_Id;
2450
2451 begin
2452 -- Avoid looking through scopes that do not meet the precondition of
2453 -- Pkg not being within a library unit spec.
2454
2455 if not Is_Compilation_Unit (Pkg)
2456 and then not Is_Generic_Instance (Pkg)
2457 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2458 then
2459 -- Loop through all entities in the current scope to identify
2460 -- an entity that depends on a private type.
2461
2462 Curr := First_Entity (Pkg);
2463 loop
2464 if Nkind (Curr) in N_Entity
2465 and then Depends_On_Private (Curr)
2466 then
2467 return True;
2468 end if;
2469
2470 exit when Last_Entity (Current_Scope) = Curr;
2471 Curr := Next_Entity (Curr);
2472 end loop;
2473 end if;
2474
2475 return False;
2476 end Contains_Lib_Incomplete_Type;
2477
2478 --------------------------------------
2479 -- Handle_Late_Controlled_Primitive --
2480 --------------------------------------
2481
2482 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2483 Body_Spec : constant Node_Id := Specification (Body_Decl);
2484 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2485 Loc : constant Source_Ptr := Sloc (Body_Id);
2486 Params : constant List_Id :=
2487 Parameter_Specifications (Body_Spec);
2488 Spec : Node_Id;
2489 Spec_Id : Entity_Id;
2490 Typ : Node_Id;
2491
2492 begin
2493 -- Consider only procedure bodies whose name matches one of the three
2494 -- controlled primitives.
2495
2496 if Nkind (Body_Spec) /= N_Procedure_Specification
2497 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2498 Name_Finalize,
2499 Name_Initialize)
2500 then
2501 return;
2502
2503 -- A controlled primitive must have exactly one formal which is not
2504 -- an anonymous access type.
2505
2506 elsif List_Length (Params) /= 1 then
2507 return;
2508 end if;
2509
2510 Typ := Parameter_Type (First (Params));
2511
2512 if Nkind (Typ) = N_Access_Definition then
2513 return;
2514 end if;
2515
2516 Find_Type (Typ);
2517
2518 -- The type of the formal must be derived from [Limited_]Controlled
2519
2520 if not Is_Controlled (Entity (Typ)) then
2521 return;
2522 end if;
2523
2524 -- Check whether a specification exists for this body. We do not
2525 -- analyze the spec of the body in full, because it will be analyzed
2526 -- again when the body is properly analyzed, and we cannot create
2527 -- duplicate entries in the formals chain. We look for an explicit
2528 -- specification because the body may be an overriding operation and
2529 -- an inherited spec may be present.
2530
2531 Spec_Id := Current_Entity (Body_Id);
2532
2533 while Present (Spec_Id) loop
2534 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2535 and then Scope (Spec_Id) = Current_Scope
2536 and then Present (First_Formal (Spec_Id))
2537 and then No (Next_Formal (First_Formal (Spec_Id)))
2538 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2539 and then Comes_From_Source (Spec_Id)
2540 then
2541 return;
2542 end if;
2543
2544 Spec_Id := Homonym (Spec_Id);
2545 end loop;
2546
2547 -- At this point the body is known to be a late controlled primitive.
2548 -- Generate a matching spec and insert it before the body. Note the
2549 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2550 -- tree in this case.
2551
2552 Spec := Copy_Separate_Tree (Body_Spec);
2553
2554 -- Ensure that the subprogram declaration does not inherit the null
2555 -- indicator from the body as we now have a proper spec/body pair.
2556
2557 Set_Null_Present (Spec, False);
2558
2559 -- Ensure that the freeze node is inserted after the declaration of
2560 -- the primitive since its expansion will freeze the primitive.
2561
2562 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2563
2564 Insert_Before_And_Analyze (Body_Decl, Decl);
2565 end Handle_Late_Controlled_Primitive;
2566
2567 ----------------------------------------
2568 -- Remove_Partial_Visible_Refinements --
2569 ----------------------------------------
2570
2571 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2572 State_Elmt : Elmt_Id;
2573 begin
2574 if Present (Abstract_States (Spec_Id)) then
2575 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2576 while Present (State_Elmt) loop
2577 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2578 Next_Elmt (State_Elmt);
2579 end loop;
2580 end if;
2581
2582 -- For a child unit, also hide the partial state refinement from
2583 -- ancestor packages.
2584
2585 if Is_Child_Unit (Spec_Id) then
2586 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2587 end if;
2588 end Remove_Partial_Visible_Refinements;
2589
2590 --------------------------------
2591 -- Remove_Visible_Refinements --
2592 --------------------------------
2593
2594 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2595 State_Elmt : Elmt_Id;
2596 begin
2597 if Present (Abstract_States (Spec_Id)) then
2598 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2599 while Present (State_Elmt) loop
2600 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2601 Next_Elmt (State_Elmt);
2602 end loop;
2603 end if;
2604 end Remove_Visible_Refinements;
2605
2606 ---------------------
2607 -- Resolve_Aspects --
2608 ---------------------
2609
2610 procedure Resolve_Aspects is
2611 E : Entity_Id;
2612
2613 begin
2614 E := First_Entity (Current_Scope);
2615 while Present (E) loop
2616 Resolve_Aspect_Expressions (E);
2617 Next_Entity (E);
2618 end loop;
2619 end Resolve_Aspects;
2620
2621 -- Local variables
2622
2623 Context : Node_Id := Empty;
2624 Freeze_From : Entity_Id := Empty;
2625 Next_Decl : Node_Id;
2626
2627 Body_Seen : Boolean := False;
2628 -- Flag set when the first body [stub] is encountered
2629
2630 -- Start of processing for Analyze_Declarations
2631
2632 begin
2633 if Restriction_Check_Required (SPARK_05) then
2634 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2635 end if;
2636
2637 Decl := First (L);
2638 while Present (Decl) loop
2639
2640 -- Package spec cannot contain a package declaration in SPARK
2641
2642 if Nkind (Decl) = N_Package_Declaration
2643 and then Nkind (Parent (L)) = N_Package_Specification
2644 then
2645 Check_SPARK_05_Restriction
2646 ("package specification cannot contain a package declaration",
2647 Decl);
2648 end if;
2649
2650 -- Complete analysis of declaration
2651
2652 Analyze (Decl);
2653 Next_Decl := Next (Decl);
2654
2655 if No (Freeze_From) then
2656 Freeze_From := First_Entity (Current_Scope);
2657 end if;
2658
2659 -- At the end of a declarative part, freeze remaining entities
2660 -- declared in it. The end of the visible declarations of package
2661 -- specification is not the end of a declarative part if private
2662 -- declarations are present. The end of a package declaration is a
2663 -- freezing point only if it a library package. A task definition or
2664 -- protected type definition is not a freeze point either. Finally,
2665 -- we do not freeze entities in generic scopes, because there is no
2666 -- code generated for them and freeze nodes will be generated for
2667 -- the instance.
2668
2669 -- The end of a package instantiation is not a freeze point, but
2670 -- for now we make it one, because the generic body is inserted
2671 -- (currently) immediately after. Generic instantiations will not
2672 -- be a freeze point once delayed freezing of bodies is implemented.
2673 -- (This is needed in any case for early instantiations ???).
2674
2675 if No (Next_Decl) then
2676 if Nkind (Parent (L)) = N_Component_List then
2677 null;
2678
2679 elsif Nkind_In (Parent (L), N_Protected_Definition,
2680 N_Task_Definition)
2681 then
2682 Check_Entry_Contracts;
2683
2684 elsif Nkind (Parent (L)) /= N_Package_Specification then
2685 if Nkind (Parent (L)) = N_Package_Body then
2686 Freeze_From := First_Entity (Current_Scope);
2687 end if;
2688
2689 -- There may have been several freezing points previously,
2690 -- for example object declarations or subprogram bodies, but
2691 -- at the end of a declarative part we check freezing from
2692 -- the beginning, even though entities may already be frozen,
2693 -- in order to perform visibility checks on delayed aspects.
2694
2695 Adjust_Decl;
2696
2697 -- If the current scope is a generic subprogram body. Skip the
2698 -- generic formal parameters that are not frozen here.
2699
2700 if Is_Subprogram (Current_Scope)
2701 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2702 N_Generic_Subprogram_Declaration
2703 and then Present (First_Entity (Current_Scope))
2704 then
2705 while Is_Generic_Formal (Freeze_From) loop
2706 Freeze_From := Next_Entity (Freeze_From);
2707 end loop;
2708
2709 Freeze_All (Freeze_From, Decl);
2710 Freeze_From := Last_Entity (Current_Scope);
2711
2712 else
2713 -- For declarations in a subprogram body there is no issue
2714 -- with name resolution in aspect specifications, but in
2715 -- ASIS mode we need to preanalyze aspect specifications
2716 -- that may otherwise only be analyzed during expansion
2717 -- (e.g. during generation of a related subprogram).
2718
2719 if ASIS_Mode then
2720 Resolve_Aspects;
2721 end if;
2722
2723 Freeze_All (First_Entity (Current_Scope), Decl);
2724 Freeze_From := Last_Entity (Current_Scope);
2725 end if;
2726
2727 -- Current scope is a package specification
2728
2729 elsif Scope (Current_Scope) /= Standard_Standard
2730 and then not Is_Child_Unit (Current_Scope)
2731 and then No (Generic_Parent (Parent (L)))
2732 then
2733 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2734 -- resolved at the end of the immediately enclosing declaration
2735 -- list (AI05-0183-1).
2736
2737 Resolve_Aspects;
2738
2739 elsif L /= Visible_Declarations (Parent (L))
2740 or else No (Private_Declarations (Parent (L)))
2741 or else Is_Empty_List (Private_Declarations (Parent (L)))
2742 then
2743 Adjust_Decl;
2744
2745 -- End of a package declaration
2746
2747 -- In compilation mode the expansion of freeze node takes care
2748 -- of resolving expressions of all aspects in the list. In ASIS
2749 -- mode this must be done explicitly.
2750
2751 if ASIS_Mode
2752 and then Scope (Current_Scope) = Standard_Standard
2753 then
2754 Resolve_Aspects;
2755 end if;
2756
2757 -- This is a freeze point because it is the end of a
2758 -- compilation unit.
2759
2760 Freeze_All (First_Entity (Current_Scope), Decl);
2761 Freeze_From := Last_Entity (Current_Scope);
2762
2763 -- At the end of the visible declarations the expressions in
2764 -- aspects of all entities declared so far must be resolved.
2765 -- The entities themselves might be frozen later, and the
2766 -- generated pragmas and attribute definition clauses analyzed
2767 -- in full at that point, but name resolution must take place
2768 -- now.
2769 -- In addition to being the proper semantics, this is mandatory
2770 -- within generic units, because global name capture requires
2771 -- those expressions to be analyzed, given that the generated
2772 -- pragmas do not appear in the original generic tree.
2773
2774 elsif Serious_Errors_Detected = 0 then
2775 Resolve_Aspects;
2776 end if;
2777
2778 -- If next node is a body then freeze all types before the body.
2779 -- An exception occurs for some expander-generated bodies. If these
2780 -- are generated at places where in general language rules would not
2781 -- allow a freeze point, then we assume that the expander has
2782 -- explicitly checked that all required types are properly frozen,
2783 -- and we do not cause general freezing here. This special circuit
2784 -- is used when the encountered body is marked as having already
2785 -- been analyzed.
2786
2787 -- In all other cases (bodies that come from source, and expander
2788 -- generated bodies that have not been analyzed yet), freeze all
2789 -- types now. Note that in the latter case, the expander must take
2790 -- care to attach the bodies at a proper place in the tree so as to
2791 -- not cause unwanted freezing at that point.
2792
2793 -- It is also necessary to check for a case where both an expression
2794 -- function is used and the current scope depends on an incomplete
2795 -- private type from a library unit, otherwise premature freezing of
2796 -- the private type will occur.
2797
2798 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2799 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2800 or else not Was_Expression_Function (Next_Decl))
2801 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2802 and then not Contains_Lib_Incomplete_Type
2803 (Current_Scope)))
2804 then
2805 -- When a controlled type is frozen, the expander generates stream
2806 -- and controlled-type support routines. If the freeze is caused
2807 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2808 -- expander will end up using the wrong version of these routines,
2809 -- as the body has not been processed yet. To remedy this, detect
2810 -- a late controlled primitive and create a proper spec for it.
2811 -- This ensures that the primitive will override its inherited
2812 -- counterpart before the freeze takes place.
2813
2814 -- If the declaration we just processed is a body, do not attempt
2815 -- to examine Next_Decl as the late primitive idiom can only apply
2816 -- to the first encountered body.
2817
2818 -- The spec of the late primitive is not generated in ASIS mode to
2819 -- ensure a consistent list of primitives that indicates the true
2820 -- semantic structure of the program (which is not relevant when
2821 -- generating executable code).
2822
2823 -- ??? A cleaner approach may be possible and/or this solution
2824 -- could be extended to general-purpose late primitives, TBD.
2825
2826 if not ASIS_Mode
2827 and then not Body_Seen
2828 and then not Is_Body (Decl)
2829 then
2830 Body_Seen := True;
2831
2832 if Nkind (Next_Decl) = N_Subprogram_Body then
2833 Handle_Late_Controlled_Primitive (Next_Decl);
2834 end if;
2835
2836 else
2837 -- In ASIS mode, if the next declaration is a body, complete
2838 -- the analysis of declarations so far.
2839
2840 Resolve_Aspects;
2841 end if;
2842
2843 Adjust_Decl;
2844
2845 -- The generated body of an expression function does not freeze,
2846 -- unless it is a completion, in which case only the expression
2847 -- itself freezes. This is handled when the body itself is
2848 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2849
2850 Freeze_All (Freeze_From, Decl);
2851 Freeze_From := Last_Entity (Current_Scope);
2852 end if;
2853
2854 Decl := Next_Decl;
2855 end loop;
2856
2857 -- Post-freezing actions
2858
2859 if Present (L) then
2860 Context := Parent (L);
2861
2862 -- Certain contract annocations have forward visibility semantics and
2863 -- must be analyzed after all declarative items have been processed.
2864 -- This timing ensures that entities referenced by such contracts are
2865 -- visible.
2866
2867 -- Analyze the contract of an immediately enclosing package spec or
2868 -- body first because other contracts may depend on its information.
2869
2870 if Nkind (Context) = N_Package_Body then
2871 Analyze_Package_Body_Contract (Defining_Entity (Context));
2872
2873 elsif Nkind (Context) = N_Package_Specification then
2874 Analyze_Package_Contract (Defining_Entity (Context));
2875 end if;
2876
2877 -- Analyze the contracts of various constructs in the declarative
2878 -- list.
2879
2880 Analyze_Contracts (L);
2881
2882 if Nkind (Context) = N_Package_Body then
2883
2884 -- Ensure that all abstract states and objects declared in the
2885 -- state space of a package body are utilized as constituents.
2886
2887 Check_Unused_Body_States (Defining_Entity (Context));
2888
2889 -- State refinements are visible up to the end of the package body
2890 -- declarations. Hide the state refinements from visibility to
2891 -- restore the original state conditions.
2892
2893 Remove_Visible_Refinements (Corresponding_Spec (Context));
2894 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2895
2896 elsif Nkind (Context) = N_Package_Specification then
2897
2898 -- Partial state refinements are visible up to the end of the
2899 -- package spec declarations. Hide the partial state refinements
2900 -- from visibility to restore the original state conditions.
2901
2902 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2903 end if;
2904
2905 -- Verify that all abstract states found in any package declared in
2906 -- the input declarative list have proper refinements. The check is
2907 -- performed only when the context denotes a block, entry, package,
2908 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2909
2910 Check_State_Refinements (Context);
2911
2912 -- Create the subprogram bodies which verify the run-time semantics
2913 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2914 -- types within the current declarative list. This ensures that all
2915 -- assertion expressions are preanalyzed and resolved at the end of
2916 -- the declarative part. Note that the resolution happens even when
2917 -- freezing does not take place.
2918
2919 Build_Assertion_Bodies (L, Context);
2920 end if;
2921 end Analyze_Declarations;
2922
2923 -----------------------------------
2924 -- Analyze_Full_Type_Declaration --
2925 -----------------------------------
2926
2927 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2928 Def : constant Node_Id := Type_Definition (N);
2929 Def_Id : constant Entity_Id := Defining_Identifier (N);
2930 T : Entity_Id;
2931 Prev : Entity_Id;
2932
2933 Is_Remote : constant Boolean :=
2934 (Is_Remote_Types (Current_Scope)
2935 or else Is_Remote_Call_Interface (Current_Scope))
2936 and then not (In_Private_Part (Current_Scope)
2937 or else In_Package_Body (Current_Scope));
2938
2939 procedure Check_Nonoverridable_Aspects;
2940 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2941 -- be overridden, and can only be confirmed on derivation.
2942
2943 procedure Check_Ops_From_Incomplete_Type;
2944 -- If there is a tagged incomplete partial view of the type, traverse
2945 -- the primitives of the incomplete view and change the type of any
2946 -- controlling formals and result to indicate the full view. The
2947 -- primitives will be added to the full type's primitive operations
2948 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2949 -- is called from Process_Incomplete_Dependents).
2950
2951 ----------------------------------
2952 -- Check_Nonoverridable_Aspects --
2953 ----------------------------------
2954
2955 procedure Check_Nonoverridable_Aspects is
2956 function Get_Aspect_Spec
2957 (Specs : List_Id;
2958 Aspect_Name : Name_Id) return Node_Id;
2959 -- Check whether a list of aspect specifications includes an entry
2960 -- for a specific aspect. The list is either that of a partial or
2961 -- a full view.
2962
2963 ---------------------
2964 -- Get_Aspect_Spec --
2965 ---------------------
2966
2967 function Get_Aspect_Spec
2968 (Specs : List_Id;
2969 Aspect_Name : Name_Id) return Node_Id
2970 is
2971 Spec : Node_Id;
2972
2973 begin
2974 Spec := First (Specs);
2975 while Present (Spec) loop
2976 if Chars (Identifier (Spec)) = Aspect_Name then
2977 return Spec;
2978 end if;
2979 Next (Spec);
2980 end loop;
2981
2982 return Empty;
2983 end Get_Aspect_Spec;
2984
2985 -- Local variables
2986
2987 Prev_Aspects : constant List_Id :=
2988 Aspect_Specifications (Parent (Def_Id));
2989 Par_Type : Entity_Id;
2990 Prev_Aspect : Node_Id;
2991
2992 -- Start of processing for Check_Nonoverridable_Aspects
2993
2994 begin
2995 -- Get parent type of derived type. Note that Prev is the entity in
2996 -- the partial declaration, but its contents are now those of full
2997 -- view, while Def_Id reflects the partial view.
2998
2999 if Is_Private_Type (Def_Id) then
3000 Par_Type := Etype (Full_View (Def_Id));
3001 else
3002 Par_Type := Etype (Def_Id);
3003 end if;
3004
3005 -- If there is an inherited Implicit_Dereference, verify that it is
3006 -- made explicit in the partial view.
3007
3008 if Has_Discriminants (Base_Type (Par_Type))
3009 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3010 and then Present (Discriminant_Specifications (Parent (Prev)))
3011 and then Present (Get_Reference_Discriminant (Par_Type))
3012 then
3013 Prev_Aspect :=
3014 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3015
3016 if No (Prev_Aspect)
3017 and then Present
3018 (Discriminant_Specifications
3019 (Original_Node (Parent (Prev))))
3020 then
3021 Error_Msg_N
3022 ("type does not inherit implicit dereference", Prev);
3023
3024 else
3025 -- If one of the views has the aspect specified, verify that it
3026 -- is consistent with that of the parent.
3027
3028 declare
3029 Cur_Discr : constant Entity_Id :=
3030 Get_Reference_Discriminant (Prev);
3031 Par_Discr : constant Entity_Id :=
3032 Get_Reference_Discriminant (Par_Type);
3033
3034 begin
3035 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3036 Error_Msg_N
3037 ("aspect inconsistent with that of parent", N);
3038 end if;
3039
3040 -- Check that specification in partial view matches the
3041 -- inherited aspect. Compare names directly because aspect
3042 -- expression may not be analyzed.
3043
3044 if Present (Prev_Aspect)
3045 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3046 and then Chars (Expression (Prev_Aspect)) /=
3047 Chars (Cur_Discr)
3048 then
3049 Error_Msg_N
3050 ("aspect inconsistent with that of parent", N);
3051 end if;
3052 end;
3053 end if;
3054 end if;
3055
3056 -- TBD : other nonoverridable aspects.
3057 end Check_Nonoverridable_Aspects;
3058
3059 ------------------------------------
3060 -- Check_Ops_From_Incomplete_Type --
3061 ------------------------------------
3062
3063 procedure Check_Ops_From_Incomplete_Type is
3064 Elmt : Elmt_Id;
3065 Formal : Entity_Id;
3066 Op : Entity_Id;
3067
3068 begin
3069 if Prev /= T
3070 and then Ekind (Prev) = E_Incomplete_Type
3071 and then Is_Tagged_Type (Prev)
3072 and then Is_Tagged_Type (T)
3073 then
3074 Elmt := First_Elmt (Primitive_Operations (Prev));
3075 while Present (Elmt) loop
3076 Op := Node (Elmt);
3077
3078 Formal := First_Formal (Op);
3079 while Present (Formal) loop
3080 if Etype (Formal) = Prev then
3081 Set_Etype (Formal, T);
3082 end if;
3083
3084 Next_Formal (Formal);
3085 end loop;
3086
3087 if Etype (Op) = Prev then
3088 Set_Etype (Op, T);
3089 end if;
3090
3091 Next_Elmt (Elmt);
3092 end loop;
3093 end if;
3094 end Check_Ops_From_Incomplete_Type;
3095
3096 -- Start of processing for Analyze_Full_Type_Declaration
3097
3098 begin
3099 Prev := Find_Type_Name (N);
3100
3101 -- The full view, if present, now points to the current type. If there
3102 -- is an incomplete partial view, set a link to it, to simplify the
3103 -- retrieval of primitive operations of the type.
3104
3105 -- Ada 2005 (AI-50217): If the type was previously decorated when
3106 -- imported through a LIMITED WITH clause, it appears as incomplete
3107 -- but has no full view.
3108
3109 if Ekind (Prev) = E_Incomplete_Type
3110 and then Present (Full_View (Prev))
3111 then
3112 T := Full_View (Prev);
3113 Set_Incomplete_View (N, Parent (Prev));
3114 else
3115 T := Prev;
3116 end if;
3117
3118 Set_Is_Pure (T, Is_Pure (Current_Scope));
3119
3120 -- We set the flag Is_First_Subtype here. It is needed to set the
3121 -- corresponding flag for the Implicit class-wide-type created
3122 -- during tagged types processing.
3123
3124 Set_Is_First_Subtype (T, True);
3125
3126 -- Only composite types other than array types are allowed to have
3127 -- discriminants.
3128
3129 case Nkind (Def) is
3130
3131 -- For derived types, the rule will be checked once we've figured
3132 -- out the parent type.
3133
3134 when N_Derived_Type_Definition =>
3135 null;
3136
3137 -- For record types, discriminants are allowed, unless we are in
3138 -- SPARK.
3139
3140 when N_Record_Definition =>
3141 if Present (Discriminant_Specifications (N)) then
3142 Check_SPARK_05_Restriction
3143 ("discriminant type is not allowed",
3144 Defining_Identifier
3145 (First (Discriminant_Specifications (N))));
3146 end if;
3147
3148 when others =>
3149 if Present (Discriminant_Specifications (N)) then
3150 Error_Msg_N
3151 ("elementary or array type cannot have discriminants",
3152 Defining_Identifier
3153 (First (Discriminant_Specifications (N))));
3154 end if;
3155 end case;
3156
3157 -- Elaborate the type definition according to kind, and generate
3158 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3159 -- already done (this happens during the reanalysis that follows a call
3160 -- to the high level optimizer).
3161
3162 if not Analyzed (T) then
3163 Set_Analyzed (T);
3164
3165 -- Set the SPARK mode from the current context
3166
3167 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3168 Set_SPARK_Pragma_Inherited (T);
3169
3170 case Nkind (Def) is
3171 when N_Access_To_Subprogram_Definition =>
3172 Access_Subprogram_Declaration (T, Def);
3173
3174 -- If this is a remote access to subprogram, we must create the
3175 -- equivalent fat pointer type, and related subprograms.
3176
3177 if Is_Remote then
3178 Process_Remote_AST_Declaration (N);
3179 end if;
3180
3181 -- Validate categorization rule against access type declaration
3182 -- usually a violation in Pure unit, Shared_Passive unit.
3183
3184 Validate_Access_Type_Declaration (T, N);
3185
3186 when N_Access_To_Object_Definition =>
3187 Access_Type_Declaration (T, Def);
3188
3189 -- Validate categorization rule against access type declaration
3190 -- usually a violation in Pure unit, Shared_Passive unit.
3191
3192 Validate_Access_Type_Declaration (T, N);
3193
3194 -- If we are in a Remote_Call_Interface package and define a
3195 -- RACW, then calling stubs and specific stream attributes
3196 -- must be added.
3197
3198 if Is_Remote
3199 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3200 then
3201 Add_RACW_Features (Def_Id);
3202 end if;
3203
3204 when N_Array_Type_Definition =>
3205 Array_Type_Declaration (T, Def);
3206
3207 when N_Derived_Type_Definition =>
3208 Derived_Type_Declaration (T, N, T /= Def_Id);
3209
3210 -- Inherit predicates from parent, and protect against illegal
3211 -- derivations.
3212
3213 if Is_Type (T) and then Has_Predicates (T) then
3214 Set_Has_Predicates (Def_Id);
3215 end if;
3216
3217 -- Save the scenario for examination by the ABE Processing
3218 -- phase.
3219
3220 Record_Elaboration_Scenario (N);
3221
3222 when N_Enumeration_Type_Definition =>
3223 Enumeration_Type_Declaration (T, Def);
3224
3225 when N_Floating_Point_Definition =>
3226 Floating_Point_Type_Declaration (T, Def);
3227
3228 when N_Decimal_Fixed_Point_Definition =>
3229 Decimal_Fixed_Point_Type_Declaration (T, Def);
3230
3231 when N_Ordinary_Fixed_Point_Definition =>
3232 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3233
3234 when N_Signed_Integer_Type_Definition =>
3235 Signed_Integer_Type_Declaration (T, Def);
3236
3237 when N_Modular_Type_Definition =>
3238 Modular_Type_Declaration (T, Def);
3239
3240 when N_Record_Definition =>
3241 Record_Type_Declaration (T, N, Prev);
3242
3243 -- If declaration has a parse error, nothing to elaborate.
3244
3245 when N_Error =>
3246 null;
3247
3248 when others =>
3249 raise Program_Error;
3250 end case;
3251 end if;
3252
3253 if Etype (T) = Any_Type then
3254 return;
3255 end if;
3256
3257 -- Controlled type is not allowed in SPARK
3258
3259 if Is_Visibly_Controlled (T) then
3260 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3261 end if;
3262
3263 -- Some common processing for all types
3264
3265 Set_Depends_On_Private (T, Has_Private_Component (T));
3266 Check_Ops_From_Incomplete_Type;
3267
3268 -- Both the declared entity, and its anonymous base type if one was
3269 -- created, need freeze nodes allocated.
3270
3271 declare
3272 B : constant Entity_Id := Base_Type (T);
3273
3274 begin
3275 -- In the case where the base type differs from the first subtype, we
3276 -- pre-allocate a freeze node, and set the proper link to the first
3277 -- subtype. Freeze_Entity will use this preallocated freeze node when
3278 -- it freezes the entity.
3279
3280 -- This does not apply if the base type is a generic type, whose
3281 -- declaration is independent of the current derived definition.
3282
3283 if B /= T and then not Is_Generic_Type (B) then
3284 Ensure_Freeze_Node (B);
3285 Set_First_Subtype_Link (Freeze_Node (B), T);
3286 end if;
3287
3288 -- A type that is imported through a limited_with clause cannot
3289 -- generate any code, and thus need not be frozen. However, an access
3290 -- type with an imported designated type needs a finalization list,
3291 -- which may be referenced in some other package that has non-limited
3292 -- visibility on the designated type. Thus we must create the
3293 -- finalization list at the point the access type is frozen, to
3294 -- prevent unsatisfied references at link time.
3295
3296 if not From_Limited_With (T) or else Is_Access_Type (T) then
3297 Set_Has_Delayed_Freeze (T);
3298 end if;
3299 end;
3300
3301 -- Case where T is the full declaration of some private type which has
3302 -- been swapped in Defining_Identifier (N).
3303
3304 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3305 Process_Full_View (N, T, Def_Id);
3306
3307 -- Record the reference. The form of this is a little strange, since
3308 -- the full declaration has been swapped in. So the first parameter
3309 -- here represents the entity to which a reference is made which is
3310 -- the "real" entity, i.e. the one swapped in, and the second
3311 -- parameter provides the reference location.
3312
3313 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3314 -- since we don't want a complaint about the full type being an
3315 -- unwanted reference to the private type
3316
3317 declare
3318 B : constant Boolean := Has_Pragma_Unreferenced (T);
3319 begin
3320 Set_Has_Pragma_Unreferenced (T, False);
3321 Generate_Reference (T, T, 'c');
3322 Set_Has_Pragma_Unreferenced (T, B);
3323 end;
3324
3325 Set_Completion_Referenced (Def_Id);
3326
3327 -- For completion of incomplete type, process incomplete dependents
3328 -- and always mark the full type as referenced (it is the incomplete
3329 -- type that we get for any real reference).
3330
3331 elsif Ekind (Prev) = E_Incomplete_Type then
3332 Process_Incomplete_Dependents (N, T, Prev);
3333 Generate_Reference (Prev, Def_Id, 'c');
3334 Set_Completion_Referenced (Def_Id);
3335
3336 -- If not private type or incomplete type completion, this is a real
3337 -- definition of a new entity, so record it.
3338
3339 else
3340 Generate_Definition (Def_Id);
3341 end if;
3342
3343 -- Propagate any pending access types whose finalization masters need to
3344 -- be fully initialized from the partial to the full view. Guard against
3345 -- an illegal full view that remains unanalyzed.
3346
3347 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3348 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3349 end if;
3350
3351 if Chars (Scope (Def_Id)) = Name_System
3352 and then Chars (Def_Id) = Name_Address
3353 and then In_Predefined_Unit (N)
3354 then
3355 Set_Is_Descendant_Of_Address (Def_Id);
3356 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3357 Set_Is_Descendant_Of_Address (Prev);
3358 end if;
3359
3360 Set_Optimize_Alignment_Flags (Def_Id);
3361 Check_Eliminated (Def_Id);
3362
3363 -- If the declaration is a completion and aspects are present, apply
3364 -- them to the entity for the type which is currently the partial
3365 -- view, but which is the one that will be frozen.
3366
3367 if Has_Aspects (N) then
3368
3369 -- In most cases the partial view is a private type, and both views
3370 -- appear in different declarative parts. In the unusual case where
3371 -- the partial view is incomplete, perform the analysis on the
3372 -- full view, to prevent freezing anomalies with the corresponding
3373 -- class-wide type, which otherwise might be frozen before the
3374 -- dispatch table is built.
3375
3376 if Prev /= Def_Id
3377 and then Ekind (Prev) /= E_Incomplete_Type
3378 then
3379 Analyze_Aspect_Specifications (N, Prev);
3380
3381 -- Normal case
3382
3383 else
3384 Analyze_Aspect_Specifications (N, Def_Id);
3385 end if;
3386 end if;
3387
3388 if Is_Derived_Type (Prev)
3389 and then Def_Id /= Prev
3390 then
3391 Check_Nonoverridable_Aspects;
3392 end if;
3393 end Analyze_Full_Type_Declaration;
3394
3395 ----------------------------------
3396 -- Analyze_Incomplete_Type_Decl --
3397 ----------------------------------
3398
3399 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3400 F : constant Boolean := Is_Pure (Current_Scope);
3401 T : Entity_Id;
3402
3403 begin
3404 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3405
3406 Generate_Definition (Defining_Identifier (N));
3407
3408 -- Process an incomplete declaration. The identifier must not have been
3409 -- declared already in the scope. However, an incomplete declaration may
3410 -- appear in the private part of a package, for a private type that has
3411 -- already been declared.
3412
3413 -- In this case, the discriminants (if any) must match
3414
3415 T := Find_Type_Name (N);
3416
3417 Set_Ekind (T, E_Incomplete_Type);
3418 Set_Etype (T, T);
3419 Set_Is_First_Subtype (T);
3420 Init_Size_Align (T);
3421
3422 -- Set the SPARK mode from the current context
3423
3424 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3425 Set_SPARK_Pragma_Inherited (T);
3426
3427 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3428 -- incomplete types.
3429
3430 if Tagged_Present (N) then
3431 Set_Is_Tagged_Type (T, True);
3432 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3433 Make_Class_Wide_Type (T);
3434 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3435 end if;
3436
3437 Set_Stored_Constraint (T, No_Elist);
3438
3439 if Present (Discriminant_Specifications (N)) then
3440 Push_Scope (T);
3441 Process_Discriminants (N);
3442 End_Scope;
3443 end if;
3444
3445 -- If the type has discriminants, nontrivial subtypes may be declared
3446 -- before the full view of the type. The full views of those subtypes
3447 -- will be built after the full view of the type.
3448
3449 Set_Private_Dependents (T, New_Elmt_List);
3450 Set_Is_Pure (T, F);
3451 end Analyze_Incomplete_Type_Decl;
3452
3453 -----------------------------------
3454 -- Analyze_Interface_Declaration --
3455 -----------------------------------
3456
3457 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3458 CW : constant Entity_Id := Class_Wide_Type (T);
3459
3460 begin
3461 Set_Is_Tagged_Type (T);
3462 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3463
3464 Set_Is_Limited_Record (T, Limited_Present (Def)
3465 or else Task_Present (Def)
3466 or else Protected_Present (Def)
3467 or else Synchronized_Present (Def));
3468
3469 -- Type is abstract if full declaration carries keyword, or if previous
3470 -- partial view did.
3471
3472 Set_Is_Abstract_Type (T);
3473 Set_Is_Interface (T);
3474
3475 -- Type is a limited interface if it includes the keyword limited, task,
3476 -- protected, or synchronized.
3477
3478 Set_Is_Limited_Interface
3479 (T, Limited_Present (Def)
3480 or else Protected_Present (Def)
3481 or else Synchronized_Present (Def)
3482 or else Task_Present (Def));
3483
3484 Set_Interfaces (T, New_Elmt_List);
3485 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3486
3487 -- Complete the decoration of the class-wide entity if it was already
3488 -- built (i.e. during the creation of the limited view)
3489
3490 if Present (CW) then
3491 Set_Is_Interface (CW);
3492 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3493 end if;
3494
3495 -- Check runtime support for synchronized interfaces
3496
3497 if (Is_Task_Interface (T)
3498 or else Is_Protected_Interface (T)
3499 or else Is_Synchronized_Interface (T))
3500 and then not RTE_Available (RE_Select_Specific_Data)
3501 then
3502 Error_Msg_CRT ("synchronized interfaces", T);
3503 end if;
3504 end Analyze_Interface_Declaration;
3505
3506 -----------------------------
3507 -- Analyze_Itype_Reference --
3508 -----------------------------
3509
3510 -- Nothing to do. This node is placed in the tree only for the benefit of
3511 -- back end processing, and has no effect on the semantic processing.
3512
3513 procedure Analyze_Itype_Reference (N : Node_Id) is
3514 begin
3515 pragma Assert (Is_Itype (Itype (N)));
3516 null;
3517 end Analyze_Itype_Reference;
3518
3519 --------------------------------
3520 -- Analyze_Number_Declaration --
3521 --------------------------------
3522
3523 procedure Analyze_Number_Declaration (N : Node_Id) is
3524 E : constant Node_Id := Expression (N);
3525 Id : constant Entity_Id := Defining_Identifier (N);
3526 Index : Interp_Index;
3527 It : Interp;
3528 T : Entity_Id;
3529
3530 begin
3531 Generate_Definition (Id);
3532 Enter_Name (Id);
3533
3534 -- This is an optimization of a common case of an integer literal
3535
3536 if Nkind (E) = N_Integer_Literal then
3537 Set_Is_Static_Expression (E, True);
3538 Set_Etype (E, Universal_Integer);
3539
3540 Set_Etype (Id, Universal_Integer);
3541 Set_Ekind (Id, E_Named_Integer);
3542 Set_Is_Frozen (Id, True);
3543
3544 Set_Debug_Info_Needed (Id);
3545 return;
3546 end if;
3547
3548 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3549
3550 -- Process expression, replacing error by integer zero, to avoid
3551 -- cascaded errors or aborts further along in the processing
3552
3553 -- Replace Error by integer zero, which seems least likely to cause
3554 -- cascaded errors.
3555
3556 if E = Error then
3557 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3558 Set_Error_Posted (E);
3559 end if;
3560
3561 Analyze (E);
3562
3563 -- Verify that the expression is static and numeric. If
3564 -- the expression is overloaded, we apply the preference
3565 -- rule that favors root numeric types.
3566
3567 if not Is_Overloaded (E) then
3568 T := Etype (E);
3569 if Has_Dynamic_Predicate_Aspect (T) then
3570 Error_Msg_N
3571 ("subtype has dynamic predicate, "
3572 & "not allowed in number declaration", N);
3573 end if;
3574
3575 else
3576 T := Any_Type;
3577
3578 Get_First_Interp (E, Index, It);
3579 while Present (It.Typ) loop
3580 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3581 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3582 then
3583 if T = Any_Type then
3584 T := It.Typ;
3585
3586 elsif It.Typ = Universal_Real
3587 or else
3588 It.Typ = Universal_Integer
3589 then
3590 -- Choose universal interpretation over any other
3591
3592 T := It.Typ;
3593 exit;
3594 end if;
3595 end if;
3596
3597 Get_Next_Interp (Index, It);
3598 end loop;
3599 end if;
3600
3601 if Is_Integer_Type (T) then
3602 Resolve (E, T);
3603 Set_Etype (Id, Universal_Integer);
3604 Set_Ekind (Id, E_Named_Integer);
3605
3606 elsif Is_Real_Type (T) then
3607
3608 -- Because the real value is converted to universal_real, this is a
3609 -- legal context for a universal fixed expression.
3610
3611 if T = Universal_Fixed then
3612 declare
3613 Loc : constant Source_Ptr := Sloc (N);
3614 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3615 Subtype_Mark =>
3616 New_Occurrence_Of (Universal_Real, Loc),
3617 Expression => Relocate_Node (E));
3618
3619 begin
3620 Rewrite (E, Conv);
3621 Analyze (E);
3622 end;
3623
3624 elsif T = Any_Fixed then
3625 Error_Msg_N ("illegal context for mixed mode operation", E);
3626
3627 -- Expression is of the form : universal_fixed * integer. Try to
3628 -- resolve as universal_real.
3629
3630 T := Universal_Real;
3631 Set_Etype (E, T);
3632 end if;
3633
3634 Resolve (E, T);
3635 Set_Etype (Id, Universal_Real);
3636 Set_Ekind (Id, E_Named_Real);
3637
3638 else
3639 Wrong_Type (E, Any_Numeric);
3640 Resolve (E, T);
3641
3642 Set_Etype (Id, T);
3643 Set_Ekind (Id, E_Constant);
3644 Set_Never_Set_In_Source (Id, True);
3645 Set_Is_True_Constant (Id, True);
3646 return;
3647 end if;
3648
3649 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3650 Set_Etype (E, Etype (Id));
3651 end if;
3652
3653 if not Is_OK_Static_Expression (E) then
3654 Flag_Non_Static_Expr
3655 ("non-static expression used in number declaration!", E);
3656 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3657 Set_Etype (E, Any_Type);
3658 end if;
3659
3660 Analyze_Dimension (N);
3661 end Analyze_Number_Declaration;
3662
3663 --------------------------------
3664 -- Analyze_Object_Declaration --
3665 --------------------------------
3666
3667 -- WARNING: This routine manages Ghost regions. Return statements must be
3668 -- replaced by gotos which jump to the end of the routine and restore the
3669 -- Ghost mode.
3670
3671 procedure Analyze_Object_Declaration (N : Node_Id) is
3672 Loc : constant Source_Ptr := Sloc (N);
3673 Id : constant Entity_Id := Defining_Identifier (N);
3674 Next_Decl : constant Node_Id := Next (N);
3675
3676 Act_T : Entity_Id;
3677 T : Entity_Id;
3678
3679 E : Node_Id := Expression (N);
3680 -- E is set to Expression (N) throughout this routine. When Expression
3681 -- (N) is modified, E is changed accordingly.
3682
3683 Prev_Entity : Entity_Id := Empty;
3684
3685 procedure Check_Dynamic_Object (Typ : Entity_Id);
3686 -- A library-level object with nonstatic discriminant constraints may
3687 -- require dynamic allocation. The declaration is illegal if the
3688 -- profile includes the restriction No_Implicit_Heap_Allocations.
3689
3690 procedure Check_For_Null_Excluding_Components
3691 (Obj_Typ : Entity_Id;
3692 Obj_Decl : Node_Id);
3693 -- Verify that each null-excluding component of object declaration
3694 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3695 -- a compile-time warning if this is not the case.
3696
3697 function Count_Tasks (T : Entity_Id) return Uint;
3698 -- This function is called when a non-generic library level object of a
3699 -- task type is declared. Its function is to count the static number of
3700 -- tasks declared within the type (it is only called if Has_Task is set
3701 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3702 -- or a variant record type is encountered, Check_Restriction is called
3703 -- indicating the count is unknown.
3704
3705 function Delayed_Aspect_Present return Boolean;
3706 -- If the declaration has an expression that is an aggregate, and it
3707 -- has aspects that require delayed analysis, the resolution of the
3708 -- aggregate must be deferred to the freeze point of the object. This
3709 -- special processing was created for address clauses, but it must
3710 -- also apply to Alignment. This must be done before the aspect
3711 -- specifications are analyzed because we must handle the aggregate
3712 -- before the analysis of the object declaration is complete.
3713
3714 -- Any other relevant delayed aspects on object declarations ???
3715
3716 --------------------------
3717 -- Check_Dynamic_Object --
3718 --------------------------
3719
3720 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3721 Comp : Entity_Id;
3722 Obj_Type : Entity_Id;
3723
3724 begin
3725 Obj_Type := Typ;
3726
3727 if Is_Private_Type (Obj_Type)
3728 and then Present (Full_View (Obj_Type))
3729 then
3730 Obj_Type := Full_View (Obj_Type);
3731 end if;
3732
3733 if Known_Static_Esize (Obj_Type) then
3734 return;
3735 end if;
3736
3737 if Restriction_Active (No_Implicit_Heap_Allocations)
3738 and then Expander_Active
3739 and then Has_Discriminants (Obj_Type)
3740 then
3741 Comp := First_Component (Obj_Type);
3742 while Present (Comp) loop
3743 if Known_Static_Esize (Etype (Comp))
3744 or else Size_Known_At_Compile_Time (Etype (Comp))
3745 then
3746 null;
3747
3748 elsif not Discriminated_Size (Comp)
3749 and then Comes_From_Source (Comp)
3750 then
3751 Error_Msg_NE
3752 ("component& of non-static size will violate restriction "
3753 & "No_Implicit_Heap_Allocation?", N, Comp);
3754
3755 elsif Is_Record_Type (Etype (Comp)) then
3756 Check_Dynamic_Object (Etype (Comp));
3757 end if;
3758
3759 Next_Component (Comp);
3760 end loop;
3761 end if;
3762 end Check_Dynamic_Object;
3763
3764 -----------------------------------------
3765 -- Check_For_Null_Excluding_Components --
3766 -----------------------------------------
3767
3768 procedure Check_For_Null_Excluding_Components
3769 (Obj_Typ : Entity_Id;
3770 Obj_Decl : Node_Id)
3771 is
3772 procedure Check_Component
3773 (Comp_Typ : Entity_Id;
3774 Comp_Decl : Node_Id := Empty;
3775 Array_Comp : Boolean := False);
3776 -- Apply a compile-time null-exclusion check on a component denoted
3777 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3778 -- subcomponents (if any).
3779
3780 ---------------------
3781 -- Check_Component --
3782 ---------------------
3783
3784 procedure Check_Component
3785 (Comp_Typ : Entity_Id;
3786 Comp_Decl : Node_Id := Empty;
3787 Array_Comp : Boolean := False)
3788 is
3789 Comp : Entity_Id;
3790 T : Entity_Id;
3791
3792 begin
3793 -- Do not consider internally-generated components or those that
3794 -- are already initialized.
3795
3796 if Present (Comp_Decl)
3797 and then (not Comes_From_Source (Comp_Decl)
3798 or else Present (Expression (Comp_Decl)))
3799 then
3800 return;
3801 end if;
3802
3803 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3804 and then Present (Full_View (Comp_Typ))
3805 then
3806 T := Full_View (Comp_Typ);
3807 else
3808 T := Comp_Typ;
3809 end if;
3810
3811 -- Verify a component of a null-excluding access type
3812
3813 if Is_Access_Type (T)
3814 and then Can_Never_Be_Null (T)
3815 then
3816 if Comp_Decl = Obj_Decl then
3817 Null_Exclusion_Static_Checks
3818 (N => Obj_Decl,
3819 Comp => Empty,
3820 Array_Comp => Array_Comp);
3821
3822 else
3823 Null_Exclusion_Static_Checks
3824 (N => Obj_Decl,
3825 Comp => Comp_Decl,
3826 Array_Comp => Array_Comp);
3827 end if;
3828
3829 -- Check array components
3830
3831 elsif Is_Array_Type (T) then
3832
3833 -- There is no suitable component when the object is of an
3834 -- array type. However, a namable component may appear at some
3835 -- point during the recursive inspection, but not at the top
3836 -- level. At the top level just indicate array component case.
3837
3838 if Comp_Decl = Obj_Decl then
3839 Check_Component (Component_Type (T), Array_Comp => True);
3840 else
3841 Check_Component (Component_Type (T), Comp_Decl);
3842 end if;
3843
3844 -- Verify all components of type T
3845
3846 -- Note: No checks are performed on types with discriminants due
3847 -- to complexities involving variants. ???
3848
3849 elsif (Is_Concurrent_Type (T)
3850 or else Is_Incomplete_Or_Private_Type (T)
3851 or else Is_Record_Type (T))
3852 and then not Has_Discriminants (T)
3853 then
3854 Comp := First_Component (T);
3855 while Present (Comp) loop
3856 Check_Component (Etype (Comp), Parent (Comp));
3857
3858 Comp := Next_Component (Comp);
3859 end loop;
3860 end if;
3861 end Check_Component;
3862
3863 -- Start processing for Check_For_Null_Excluding_Components
3864
3865 begin
3866 Check_Component (Obj_Typ, Obj_Decl);
3867 end Check_For_Null_Excluding_Components;
3868
3869 -----------------
3870 -- Count_Tasks --
3871 -----------------
3872
3873 function Count_Tasks (T : Entity_Id) return Uint is
3874 C : Entity_Id;
3875 X : Node_Id;
3876 V : Uint;
3877
3878 begin
3879 if Is_Task_Type (T) then
3880 return Uint_1;
3881
3882 elsif Is_Record_Type (T) then
3883 if Has_Discriminants (T) then
3884 Check_Restriction (Max_Tasks, N);
3885 return Uint_0;
3886
3887 else
3888 V := Uint_0;
3889 C := First_Component (T);
3890 while Present (C) loop
3891 V := V + Count_Tasks (Etype (C));
3892 Next_Component (C);
3893 end loop;
3894
3895 return V;
3896 end if;
3897
3898 elsif Is_Array_Type (T) then
3899 X := First_Index (T);
3900 V := Count_Tasks (Component_Type (T));
3901 while Present (X) loop
3902 C := Etype (X);
3903
3904 if not Is_OK_Static_Subtype (C) then
3905 Check_Restriction (Max_Tasks, N);
3906 return Uint_0;
3907 else
3908 V := V * (UI_Max (Uint_0,
3909 Expr_Value (Type_High_Bound (C)) -
3910 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3911 end if;
3912
3913 Next_Index (X);
3914 end loop;
3915
3916 return V;
3917
3918 else
3919 return Uint_0;
3920 end if;
3921 end Count_Tasks;
3922
3923 ----------------------------
3924 -- Delayed_Aspect_Present --
3925 ----------------------------
3926
3927 function Delayed_Aspect_Present return Boolean is
3928 A : Node_Id;
3929 A_Id : Aspect_Id;
3930
3931 begin
3932 if Present (Aspect_Specifications (N)) then
3933 A := First (Aspect_Specifications (N));
3934 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3935 while Present (A) loop
3936 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3937
3938 -- Set flag on object entity, for later processing at
3939 -- the freeze point.
3940
3941 Set_Has_Delayed_Aspects (Id);
3942 return True;
3943 end if;
3944
3945 Next (A);
3946 end loop;
3947 end if;
3948
3949 return False;
3950 end Delayed_Aspect_Present;
3951
3952 -- Local variables
3953
3954 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3955 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3956 -- Save the Ghost-related attributes to restore on exit
3957
3958 Related_Id : Entity_Id;
3959 Full_View_Present : Boolean := False;
3960
3961 -- Start of processing for Analyze_Object_Declaration
3962
3963 begin
3964 -- There are three kinds of implicit types generated by an
3965 -- object declaration:
3966
3967 -- 1. Those generated by the original Object Definition
3968
3969 -- 2. Those generated by the Expression
3970
3971 -- 3. Those used to constrain the Object Definition with the
3972 -- expression constraints when the definition is unconstrained.
3973
3974 -- They must be generated in this order to avoid order of elaboration
3975 -- issues. Thus the first step (after entering the name) is to analyze
3976 -- the object definition.
3977
3978 if Constant_Present (N) then
3979 Prev_Entity := Current_Entity_In_Scope (Id);
3980
3981 if Present (Prev_Entity)
3982 and then
3983 -- If the homograph is an implicit subprogram, it is overridden
3984 -- by the current declaration.
3985
3986 ((Is_Overloadable (Prev_Entity)
3987 and then Is_Inherited_Operation (Prev_Entity))
3988
3989 -- The current object is a discriminal generated for an entry
3990 -- family index. Even though the index is a constant, in this
3991 -- particular context there is no true constant redeclaration.
3992 -- Enter_Name will handle the visibility.
3993
3994 or else
3995 (Is_Discriminal (Id)
3996 and then Ekind (Discriminal_Link (Id)) =
3997 E_Entry_Index_Parameter)
3998
3999 -- The current object is the renaming for a generic declared
4000 -- within the instance.
4001
4002 or else
4003 (Ekind (Prev_Entity) = E_Package
4004 and then Nkind (Parent (Prev_Entity)) =
4005 N_Package_Renaming_Declaration
4006 and then not Comes_From_Source (Prev_Entity)
4007 and then
4008 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4009
4010 -- The entity may be a homonym of a private component of the
4011 -- enclosing protected object, for which we create a local
4012 -- renaming declaration. The declaration is legal, even if
4013 -- useless when it just captures that component.
4014
4015 or else
4016 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4017 and then Nkind (Parent (Prev_Entity)) =
4018 N_Object_Renaming_Declaration))
4019 then
4020 Prev_Entity := Empty;
4021 end if;
4022 end if;
4023
4024 if Present (Prev_Entity) then
4025
4026 -- The object declaration is Ghost when it completes a deferred Ghost
4027 -- constant.
4028
4029 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4030
4031 Constant_Redeclaration (Id, N, T);
4032
4033 Generate_Reference (Prev_Entity, Id, 'c');
4034 Set_Completion_Referenced (Id);
4035
4036 if Error_Posted (N) then
4037
4038 -- Type mismatch or illegal redeclaration; do not analyze
4039 -- expression to avoid cascaded errors.
4040
4041 T := Find_Type_Of_Object (Object_Definition (N), N);
4042 Set_Etype (Id, T);
4043 Set_Ekind (Id, E_Variable);
4044 goto Leave;
4045 end if;
4046
4047 -- In the normal case, enter identifier at the start to catch premature
4048 -- usage in the initialization expression.
4049
4050 else
4051 Generate_Definition (Id);
4052 Enter_Name (Id);
4053
4054 Mark_Coextensions (N, Object_Definition (N));
4055
4056 T := Find_Type_Of_Object (Object_Definition (N), N);
4057
4058 if Nkind (Object_Definition (N)) = N_Access_Definition
4059 and then Present
4060 (Access_To_Subprogram_Definition (Object_Definition (N)))
4061 and then Protected_Present
4062 (Access_To_Subprogram_Definition (Object_Definition (N)))
4063 then
4064 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4065 end if;
4066
4067 if Error_Posted (Id) then
4068 Set_Etype (Id, T);
4069 Set_Ekind (Id, E_Variable);
4070 goto Leave;
4071 end if;
4072 end if;
4073
4074 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4075 -- out some static checks.
4076
4077 if Ada_Version >= Ada_2005 then
4078
4079 -- In case of aggregates we must also take care of the correct
4080 -- initialization of nested aggregates bug this is done at the
4081 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4082
4083 if Can_Never_Be_Null (T) then
4084 if Present (Expression (N))
4085 and then Nkind (Expression (N)) = N_Aggregate
4086 then
4087 null;
4088
4089 else
4090 declare
4091 Save_Typ : constant Entity_Id := Etype (Id);
4092 begin
4093 Set_Etype (Id, T); -- Temp. decoration for static checks
4094 Null_Exclusion_Static_Checks (N);
4095 Set_Etype (Id, Save_Typ);
4096 end;
4097 end if;
4098
4099 -- We might be dealing with an object of a composite type containing
4100 -- null-excluding components without an aggregate, so we must verify
4101 -- that such components have default initialization.
4102
4103 else
4104 Check_For_Null_Excluding_Components (T, N);
4105 end if;
4106 end if;
4107
4108 -- Object is marked pure if it is in a pure scope
4109
4110 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4111
4112 -- If deferred constant, make sure context is appropriate. We detect
4113 -- a deferred constant as a constant declaration with no expression.
4114 -- A deferred constant can appear in a package body if its completion
4115 -- is by means of an interface pragma.
4116
4117 if Constant_Present (N) and then No (E) then
4118
4119 -- A deferred constant may appear in the declarative part of the
4120 -- following constructs:
4121
4122 -- blocks
4123 -- entry bodies
4124 -- extended return statements
4125 -- package specs
4126 -- package bodies
4127 -- subprogram bodies
4128 -- task bodies
4129
4130 -- When declared inside a package spec, a deferred constant must be
4131 -- completed by a full constant declaration or pragma Import. In all
4132 -- other cases, the only proper completion is pragma Import. Extended
4133 -- return statements are flagged as invalid contexts because they do
4134 -- not have a declarative part and so cannot accommodate the pragma.
4135
4136 if Ekind (Current_Scope) = E_Return_Statement then
4137 Error_Msg_N
4138 ("invalid context for deferred constant declaration (RM 7.4)",
4139 N);
4140 Error_Msg_N
4141 ("\declaration requires an initialization expression",
4142 N);
4143 Set_Constant_Present (N, False);
4144
4145 -- In Ada 83, deferred constant must be of private type
4146
4147 elsif not Is_Private_Type (T) then
4148 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4149 Error_Msg_N
4150 ("(Ada 83) deferred constant must be private type", N);
4151 end if;
4152 end if;
4153
4154 -- If not a deferred constant, then the object declaration freezes
4155 -- its type, unless the object is of an anonymous type and has delayed
4156 -- aspects. In that case the type is frozen when the object itself is.
4157
4158 else
4159 Check_Fully_Declared (T, N);
4160
4161 if Has_Delayed_Aspects (Id)
4162 and then Is_Array_Type (T)
4163 and then Is_Itype (T)
4164 then
4165 Set_Has_Delayed_Freeze (T);
4166 else
4167 Freeze_Before (N, T);
4168 end if;
4169 end if;
4170
4171 -- If the object was created by a constrained array definition, then
4172 -- set the link in both the anonymous base type and anonymous subtype
4173 -- that are built to represent the array type to point to the object.
4174
4175 if Nkind (Object_Definition (Declaration_Node (Id))) =
4176 N_Constrained_Array_Definition
4177 then
4178 Set_Related_Array_Object (T, Id);
4179 Set_Related_Array_Object (Base_Type (T), Id);
4180 end if;
4181
4182 -- Special checks for protected objects not at library level
4183
4184 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4185 Check_Restriction (No_Local_Protected_Objects, Id);
4186
4187 -- Protected objects with interrupt handlers must be at library level
4188
4189 -- Ada 2005: This test is not needed (and the corresponding clause
4190 -- in the RM is removed) because accessibility checks are sufficient
4191 -- to make handlers not at the library level illegal.
4192
4193 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4194 -- applies to the '95 version of the language as well.
4195
4196 if Is_Protected_Type (T)
4197 and then Has_Interrupt_Handler (T)
4198 and then Ada_Version < Ada_95
4199 then
4200 Error_Msg_N
4201 ("interrupt object can only be declared at library level", Id);
4202 end if;
4203 end if;
4204
4205 -- Check for violation of No_Local_Timing_Events
4206
4207 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4208 Check_Restriction (No_Local_Timing_Events, Id);
4209 end if;
4210
4211 -- The actual subtype of the object is the nominal subtype, unless
4212 -- the nominal one is unconstrained and obtained from the expression.
4213
4214 Act_T := T;
4215
4216 -- These checks should be performed before the initialization expression
4217 -- is considered, so that the Object_Definition node is still the same
4218 -- as in source code.
4219
4220 -- In SPARK, the nominal subtype is always given by a subtype mark
4221 -- and must not be unconstrained. (The only exception to this is the
4222 -- acceptance of declarations of constants of type String.)
4223
4224 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4225 then
4226 Check_SPARK_05_Restriction
4227 ("subtype mark required", Object_Definition (N));
4228
4229 elsif Is_Array_Type (T)
4230 and then not Is_Constrained (T)
4231 and then T /= Standard_String
4232 then
4233 Check_SPARK_05_Restriction
4234 ("subtype mark of constrained type expected",
4235 Object_Definition (N));
4236 end if;
4237
4238 if Is_Library_Level_Entity (Id) then
4239 Check_Dynamic_Object (T);
4240 end if;
4241
4242 -- There are no aliased objects in SPARK
4243
4244 if Aliased_Present (N) then
4245 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4246 end if;
4247
4248 -- Process initialization expression if present and not in error
4249
4250 if Present (E) and then E /= Error then
4251
4252 -- Generate an error in case of CPP class-wide object initialization.
4253 -- Required because otherwise the expansion of the class-wide
4254 -- assignment would try to use 'size to initialize the object
4255 -- (primitive that is not available in CPP tagged types).
4256
4257 if Is_Class_Wide_Type (Act_T)
4258 and then
4259 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4260 or else
4261 (Present (Full_View (Root_Type (Etype (Act_T))))
4262 and then
4263 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4264 then
4265 Error_Msg_N
4266 ("predefined assignment not available for 'C'P'P tagged types",
4267 E);
4268 end if;
4269
4270 Mark_Coextensions (N, E);
4271 Analyze (E);
4272
4273 -- In case of errors detected in the analysis of the expression,
4274 -- decorate it with the expected type to avoid cascaded errors
4275
4276 if No (Etype (E)) then
4277 Set_Etype (E, T);
4278 end if;
4279
4280 -- If an initialization expression is present, then we set the
4281 -- Is_True_Constant flag. It will be reset if this is a variable
4282 -- and it is indeed modified.
4283
4284 Set_Is_True_Constant (Id, True);
4285
4286 -- If we are analyzing a constant declaration, set its completion
4287 -- flag after analyzing and resolving the expression.
4288
4289 if Constant_Present (N) then
4290 Set_Has_Completion (Id);
4291 end if;
4292
4293 -- Set type and resolve (type may be overridden later on). Note:
4294 -- Ekind (Id) must still be E_Void at this point so that incorrect
4295 -- early usage within E is properly diagnosed.
4296
4297 Set_Etype (Id, T);
4298
4299 -- If the expression is an aggregate we must look ahead to detect
4300 -- the possible presence of an address clause, and defer resolution
4301 -- and expansion of the aggregate to the freeze point of the entity.
4302
4303 -- This is not always legal because the aggregate may contain other
4304 -- references that need freezing, e.g. references to other entities
4305 -- with address clauses. In any case, when compiling with -gnatI the
4306 -- presence of the address clause must be ignored.
4307
4308 if Comes_From_Source (N)
4309 and then Expander_Active
4310 and then Nkind (E) = N_Aggregate
4311 and then
4312 ((Present (Following_Address_Clause (N))
4313 and then not Ignore_Rep_Clauses)
4314 or else Delayed_Aspect_Present)
4315 then
4316 Set_Etype (E, T);
4317
4318 -- If the aggregate is limited it will be built in place, and its
4319 -- expansion is deferred until the object declaration is expanded.
4320
4321 if Is_Limited_Type (T) then
4322 Set_Expansion_Delayed (E);
4323 end if;
4324
4325 else
4326 -- If the expression is a formal that is a "subprogram pointer"
4327 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4328 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4329 -- the corresponding check, as is done for assignments.
4330
4331 if Is_Entity_Name (E)
4332 and then Present (Entity (E))
4333 and then Is_Formal (Entity (E))
4334 and then
4335 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4336 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4337 then
4338 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4339 end if;
4340
4341 Resolve (E, T);
4342 end if;
4343
4344 -- No further action needed if E is a call to an inlined function
4345 -- which returns an unconstrained type and it has been expanded into
4346 -- a procedure call. In that case N has been replaced by an object
4347 -- declaration without initializing expression and it has been
4348 -- analyzed (see Expand_Inlined_Call).
4349
4350 if Back_End_Inlining
4351 and then Expander_Active
4352 and then Nkind (E) = N_Function_Call
4353 and then Nkind (Name (E)) in N_Has_Entity
4354 and then Is_Inlined (Entity (Name (E)))
4355 and then not Is_Constrained (Etype (E))
4356 and then Analyzed (N)
4357 and then No (Expression (N))
4358 then
4359 goto Leave;
4360 end if;
4361
4362 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4363 -- node (which was marked already-analyzed), we need to set the type
4364 -- to something other than Any_Access in order to keep gigi happy.
4365
4366 if Etype (E) = Any_Access then
4367 Set_Etype (E, T);
4368 end if;
4369
4370 -- If the object is an access to variable, the initialization
4371 -- expression cannot be an access to constant.
4372
4373 if Is_Access_Type (T)
4374 and then not Is_Access_Constant (T)
4375 and then Is_Access_Type (Etype (E))
4376 and then Is_Access_Constant (Etype (E))
4377 then
4378 Error_Msg_N
4379 ("access to variable cannot be initialized with an "
4380 & "access-to-constant expression", E);
4381 end if;
4382
4383 if not Assignment_OK (N) then
4384 Check_Initialization (T, E);
4385 end if;
4386
4387 Check_Unset_Reference (E);
4388
4389 -- If this is a variable, then set current value. If this is a
4390 -- declared constant of a scalar type with a static expression,
4391 -- indicate that it is always valid.
4392
4393 if not Constant_Present (N) then
4394 if Compile_Time_Known_Value (E) then
4395 Set_Current_Value (Id, E);
4396 end if;
4397
4398 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4399 Set_Is_Known_Valid (Id);
4400
4401 -- If it is a constant initialized with a valid nonstatic entity,
4402 -- the constant is known valid as well, and can inherit the subtype
4403 -- of the entity if it is a subtype of the given type. This info
4404 -- is preserved on the actual subtype of the constant.
4405
4406 elsif Is_Scalar_Type (T)
4407 and then Is_Entity_Name (E)
4408 and then Is_Known_Valid (Entity (E))
4409 and then In_Subrange_Of (Etype (Entity (E)), T)
4410 then
4411 Set_Is_Known_Valid (Id);
4412 Set_Ekind (Id, E_Constant);
4413 Set_Actual_Subtype (Id, Etype (Entity (E)));
4414 end if;
4415
4416 -- Deal with setting of null flags
4417
4418 if Is_Access_Type (T) then
4419 if Known_Non_Null (E) then
4420 Set_Is_Known_Non_Null (Id, True);
4421 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4422 Set_Is_Known_Null (Id, True);
4423 end if;
4424 end if;
4425
4426 -- Check incorrect use of dynamically tagged expressions
4427
4428 if Is_Tagged_Type (T) then
4429 Check_Dynamically_Tagged_Expression
4430 (Expr => E,
4431 Typ => T,
4432 Related_Nod => N);
4433 end if;
4434
4435 Apply_Scalar_Range_Check (E, T);
4436 Apply_Static_Length_Check (E, T);
4437
4438 if Nkind (Original_Node (N)) = N_Object_Declaration
4439 and then Comes_From_Source (Original_Node (N))
4440
4441 -- Only call test if needed
4442
4443 and then Restriction_Check_Required (SPARK_05)
4444 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4445 then
4446 Check_SPARK_05_Restriction
4447 ("initialization expression is not appropriate", E);
4448 end if;
4449
4450 -- A formal parameter of a specific tagged type whose related
4451 -- subprogram is subject to pragma Extensions_Visible with value
4452 -- "False" cannot be implicitly converted to a class-wide type by
4453 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4454 -- not consider internally generated expressions.
4455
4456 if Is_Class_Wide_Type (T)
4457 and then Comes_From_Source (E)
4458 and then Is_EVF_Expression (E)
4459 then
4460 Error_Msg_N
4461 ("formal parameter cannot be implicitly converted to "
4462 & "class-wide type when Extensions_Visible is False", E);
4463 end if;
4464 end if;
4465
4466 -- If the No_Streams restriction is set, check that the type of the
4467 -- object is not, and does not contain, any subtype derived from
4468 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4469 -- Has_Stream just for efficiency reasons. There is no point in
4470 -- spending time on a Has_Stream check if the restriction is not set.
4471
4472 if Restriction_Check_Required (No_Streams) then
4473 if Has_Stream (T) then
4474 Check_Restriction (No_Streams, N);
4475 end if;
4476 end if;
4477
4478 -- Deal with predicate check before we start to do major rewriting. It
4479 -- is OK to initialize and then check the initialized value, since the
4480 -- object goes out of scope if we get a predicate failure. Note that we
4481 -- do this in the analyzer and not the expander because the analyzer
4482 -- does some substantial rewriting in some cases.
4483
4484 -- We need a predicate check if the type has predicates that are not
4485 -- ignored, and if either there is an initializing expression, or for
4486 -- default initialization when we have at least one case of an explicit
4487 -- default initial value and then this is not an internal declaration
4488 -- whose initialization comes later (as for an aggregate expansion).
4489 -- If expression is an aggregate it may be expanded into assignments
4490 -- and the declaration itself is marked with No_Initialization, but
4491 -- the predicate still applies.
4492
4493 if not Suppress_Assignment_Checks (N)
4494 and then Present (Predicate_Function (T))
4495 and then not Predicates_Ignored (T)
4496 and then
4497 (not No_Initialization (N)
4498 or else (Present (E) and then Nkind (E) = N_Aggregate))
4499 and then
4500 (Present (E)
4501 or else
4502 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4503 then
4504 -- If the type has a static predicate and the expression is known at
4505 -- compile time, see if the expression satisfies the predicate.
4506
4507 if Present (E) then
4508 Check_Expression_Against_Static_Predicate (E, T);
4509 end if;
4510
4511 -- If the type is a null record and there is no explicit initial
4512 -- expression, no predicate check applies.
4513
4514 if No (E) and then Is_Null_Record_Type (T) then
4515 null;
4516
4517 -- Do not generate a predicate check if the initialization expression
4518 -- is a type conversion because the conversion has been subjected to
4519 -- the same check. This is a small optimization which avoid redundant
4520 -- checks.
4521
4522 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4523 null;
4524
4525 else
4526 -- The check must be inserted after the expanded aggregate
4527 -- expansion code, if any.
4528
4529 declare
4530 Check : constant Node_Id :=
4531 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4532
4533 begin
4534 if No (Next_Decl) then
4535 Append_To (List_Containing (N), Check);
4536 else
4537 Insert_Before (Next_Decl, Check);
4538 end if;
4539 end;
4540 end if;
4541 end if;
4542
4543 -- Case of unconstrained type
4544
4545 if not Is_Definite_Subtype (T) then
4546
4547 -- In SPARK, a declaration of unconstrained type is allowed
4548 -- only for constants of type string.
4549
4550 if Is_String_Type (T) and then not Constant_Present (N) then
4551 Check_SPARK_05_Restriction
4552 ("declaration of object of unconstrained type not allowed", N);
4553 end if;
4554
4555 -- Nothing to do in deferred constant case
4556
4557 if Constant_Present (N) and then No (E) then
4558 null;
4559
4560 -- Case of no initialization present
4561
4562 elsif No (E) then
4563 if No_Initialization (N) then
4564 null;
4565
4566 elsif Is_Class_Wide_Type (T) then
4567 Error_Msg_N
4568 ("initialization required in class-wide declaration ", N);
4569
4570 else
4571 Error_Msg_N
4572 ("unconstrained subtype not allowed (need initialization)",
4573 Object_Definition (N));
4574
4575 if Is_Record_Type (T) and then Has_Discriminants (T) then
4576 Error_Msg_N
4577 ("\provide initial value or explicit discriminant values",
4578 Object_Definition (N));
4579
4580 Error_Msg_NE
4581 ("\or give default discriminant values for type&",
4582 Object_Definition (N), T);
4583
4584 elsif Is_Array_Type (T) then
4585 Error_Msg_N
4586 ("\provide initial value or explicit array bounds",
4587 Object_Definition (N));
4588 end if;
4589 end if;
4590
4591 -- Case of initialization present but in error. Set initial
4592 -- expression as absent (but do not make above complaints)
4593
4594 elsif E = Error then
4595 Set_Expression (N, Empty);
4596 E := Empty;
4597
4598 -- Case of initialization present
4599
4600 else
4601 -- Check restrictions in Ada 83
4602
4603 if not Constant_Present (N) then
4604
4605 -- Unconstrained variables not allowed in Ada 83 mode
4606
4607 if Ada_Version = Ada_83
4608 and then Comes_From_Source (Object_Definition (N))
4609 then
4610 Error_Msg_N
4611 ("(Ada 83) unconstrained variable not allowed",
4612 Object_Definition (N));
4613 end if;
4614 end if;
4615
4616 -- Now we constrain the variable from the initializing expression
4617
4618 -- If the expression is an aggregate, it has been expanded into
4619 -- individual assignments. Retrieve the actual type from the
4620 -- expanded construct.
4621
4622 if Is_Array_Type (T)
4623 and then No_Initialization (N)
4624 and then Nkind (Original_Node (E)) = N_Aggregate
4625 then
4626 Act_T := Etype (E);
4627
4628 -- In case of class-wide interface object declarations we delay
4629 -- the generation of the equivalent record type declarations until
4630 -- its expansion because there are cases in they are not required.
4631
4632 elsif Is_Interface (T) then
4633 null;
4634
4635 -- If the type is an unchecked union, no subtype can be built from
4636 -- the expression. Rewrite declaration as a renaming, which the
4637 -- back-end can handle properly. This is a rather unusual case,
4638 -- because most unchecked_union declarations have default values
4639 -- for discriminants and are thus not indefinite.
4640
4641 elsif Is_Unchecked_Union (T) then
4642 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4643 Set_Ekind (Id, E_Constant);
4644 else
4645 Set_Ekind (Id, E_Variable);
4646 end if;
4647
4648 Rewrite (N,
4649 Make_Object_Renaming_Declaration (Loc,
4650 Defining_Identifier => Id,
4651 Subtype_Mark => New_Occurrence_Of (T, Loc),
4652 Name => E));
4653
4654 Set_Renamed_Object (Id, E);
4655 Freeze_Before (N, T);
4656 Set_Is_Frozen (Id);
4657 goto Leave;
4658
4659 else
4660 -- Ensure that the generated subtype has a unique external name
4661 -- when the related object is public. This guarantees that the
4662 -- subtype and its bounds will not be affected by switches or
4663 -- pragmas that may offset the internal counter due to extra
4664 -- generated code.
4665
4666 if Is_Public (Id) then
4667 Related_Id := Id;
4668 else
4669 Related_Id := Empty;
4670 end if;
4671
4672 Expand_Subtype_From_Expr
4673 (N => N,
4674 Unc_Type => T,
4675 Subtype_Indic => Object_Definition (N),
4676 Exp => E,
4677 Related_Id => Related_Id);
4678
4679 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4680 end if;
4681
4682 -- Propagate attributes to full view when needed.
4683
4684 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4685
4686 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4687 then
4688 Full_View_Present := True;
4689 end if;
4690
4691 if Full_View_Present then
4692 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4693 end if;
4694
4695 if Aliased_Present (N) then
4696 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4697
4698 if Full_View_Present then
4699 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4700 end if;
4701 end if;
4702
4703 Freeze_Before (N, Act_T);
4704 Freeze_Before (N, T);
4705 end if;
4706
4707 elsif Is_Array_Type (T)
4708 and then No_Initialization (N)
4709 and then (Nkind (Original_Node (E)) = N_Aggregate
4710 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4711 and then Nkind (Original_Node (Expression
4712 (Original_Node (E)))) = N_Aggregate))
4713 then
4714 if not Is_Entity_Name (Object_Definition (N)) then
4715 Act_T := Etype (E);
4716 Check_Compile_Time_Size (Act_T);
4717
4718 if Aliased_Present (N) then
4719 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4720 end if;
4721 end if;
4722
4723 -- When the given object definition and the aggregate are specified
4724 -- independently, and their lengths might differ do a length check.
4725 -- This cannot happen if the aggregate is of the form (others =>...)
4726
4727 if not Is_Constrained (T) then
4728 null;
4729
4730 elsif Nkind (E) = N_Raise_Constraint_Error then
4731
4732 -- Aggregate is statically illegal. Place back in declaration
4733
4734 Set_Expression (N, E);
4735 Set_No_Initialization (N, False);
4736
4737 elsif T = Etype (E) then
4738 null;
4739
4740 elsif Nkind (E) = N_Aggregate
4741 and then Present (Component_Associations (E))
4742 and then Present (Choice_List (First (Component_Associations (E))))
4743 and then
4744 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4745 N_Others_Choice
4746 then
4747 null;
4748
4749 else
4750 Apply_Length_Check (E, T);
4751 end if;
4752
4753 -- If the type is limited unconstrained with defaulted discriminants and
4754 -- there is no expression, then the object is constrained by the
4755 -- defaults, so it is worthwhile building the corresponding subtype.
4756
4757 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4758 and then not Is_Constrained (T)
4759 and then Has_Discriminants (T)
4760 then
4761 if No (E) then
4762 Act_T := Build_Default_Subtype (T, N);
4763 else
4764 -- Ada 2005: A limited object may be initialized by means of an
4765 -- aggregate. If the type has default discriminants it has an
4766 -- unconstrained nominal type, Its actual subtype will be obtained
4767 -- from the aggregate, and not from the default discriminants.
4768
4769 Act_T := Etype (E);
4770 end if;
4771
4772 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4773
4774 elsif Nkind (E) = N_Function_Call
4775 and then Constant_Present (N)
4776 and then Has_Unconstrained_Elements (Etype (E))
4777 then
4778 -- The back-end has problems with constants of a discriminated type
4779 -- with defaults, if the initial value is a function call. We
4780 -- generate an intermediate temporary that will receive a reference
4781 -- to the result of the call. The initialization expression then
4782 -- becomes a dereference of that temporary.
4783
4784 Remove_Side_Effects (E);
4785
4786 -- If this is a constant declaration of an unconstrained type and
4787 -- the initialization is an aggregate, we can use the subtype of the
4788 -- aggregate for the declared entity because it is immutable.
4789
4790 elsif not Is_Constrained (T)
4791 and then Has_Discriminants (T)
4792 and then Constant_Present (N)
4793 and then not Has_Unchecked_Union (T)
4794 and then Nkind (E) = N_Aggregate
4795 then
4796 Act_T := Etype (E);
4797 end if;
4798
4799 -- Check No_Wide_Characters restriction
4800
4801 Check_Wide_Character_Restriction (T, Object_Definition (N));
4802
4803 -- Indicate this is not set in source. Certainly true for constants, and
4804 -- true for variables so far (will be reset for a variable if and when
4805 -- we encounter a modification in the source).
4806
4807 Set_Never_Set_In_Source (Id);
4808
4809 -- Now establish the proper kind and type of the object
4810
4811 if Constant_Present (N) then
4812 Set_Ekind (Id, E_Constant);
4813 Set_Is_True_Constant (Id);
4814
4815 else
4816 Set_Ekind (Id, E_Variable);
4817
4818 -- A variable is set as shared passive if it appears in a shared
4819 -- passive package, and is at the outer level. This is not done for
4820 -- entities generated during expansion, because those are always
4821 -- manipulated locally.
4822
4823 if Is_Shared_Passive (Current_Scope)
4824 and then Is_Library_Level_Entity (Id)
4825 and then Comes_From_Source (Id)
4826 then
4827 Set_Is_Shared_Passive (Id);
4828 Check_Shared_Var (Id, T, N);
4829 end if;
4830
4831 -- Set Has_Initial_Value if initializing expression present. Note
4832 -- that if there is no initializing expression, we leave the state
4833 -- of this flag unchanged (usually it will be False, but notably in
4834 -- the case of exception choice variables, it will already be true).
4835
4836 if Present (E) then
4837 Set_Has_Initial_Value (Id);
4838 end if;
4839 end if;
4840
4841 -- Set the SPARK mode from the current context (may be overwritten later
4842 -- with explicit pragma).
4843
4844 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4845 Set_SPARK_Pragma_Inherited (Id);
4846
4847 -- Preserve relevant elaboration-related attributes of the context which
4848 -- are no longer available or very expensive to recompute once analysis,
4849 -- resolution, and expansion are over.
4850
4851 Mark_Elaboration_Attributes
4852 (N_Id => Id,
4853 Checks => True,
4854 Warnings => True);
4855
4856 -- Initialize alignment and size and capture alignment setting
4857
4858 Init_Alignment (Id);
4859 Init_Esize (Id);
4860 Set_Optimize_Alignment_Flags (Id);
4861
4862 -- Deal with aliased case
4863
4864 if Aliased_Present (N) then
4865 Set_Is_Aliased (Id);
4866
4867 -- AI12-001: All aliased objects are considered to be specified as
4868 -- independently addressable (RM C.6(8.1/4)).
4869
4870 Set_Is_Independent (Id);
4871
4872 -- If the object is aliased and the type is unconstrained with
4873 -- defaulted discriminants and there is no expression, then the
4874 -- object is constrained by the defaults, so it is worthwhile
4875 -- building the corresponding subtype.
4876
4877 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4878 -- unconstrained, then only establish an actual subtype if the
4879 -- nominal subtype is indefinite. In definite cases the object is
4880 -- unconstrained in Ada 2005.
4881
4882 if No (E)
4883 and then Is_Record_Type (T)
4884 and then not Is_Constrained (T)
4885 and then Has_Discriminants (T)
4886 and then (Ada_Version < Ada_2005
4887 or else not Is_Definite_Subtype (T))
4888 then
4889 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4890 end if;
4891 end if;
4892
4893 -- Now we can set the type of the object
4894
4895 Set_Etype (Id, Act_T);
4896
4897 -- Non-constant object is marked to be treated as volatile if type is
4898 -- volatile and we clear the Current_Value setting that may have been
4899 -- set above. Doing so for constants isn't required and might interfere
4900 -- with possible uses of the object as a static expression in contexts
4901 -- incompatible with volatility (e.g. as a case-statement alternative).
4902
4903 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4904 Set_Treat_As_Volatile (Id);
4905 Set_Current_Value (Id, Empty);
4906 end if;
4907
4908 -- Deal with controlled types
4909
4910 if Has_Controlled_Component (Etype (Id))
4911 or else Is_Controlled (Etype (Id))
4912 then
4913 if not Is_Library_Level_Entity (Id) then
4914 Check_Restriction (No_Nested_Finalization, N);
4915 else
4916 Validate_Controlled_Object (Id);
4917 end if;
4918 end if;
4919
4920 if Has_Task (Etype (Id)) then
4921 Check_Restriction (No_Tasking, N);
4922
4923 -- Deal with counting max tasks
4924
4925 -- Nothing to do if inside a generic
4926
4927 if Inside_A_Generic then
4928 null;
4929
4930 -- If library level entity, then count tasks
4931
4932 elsif Is_Library_Level_Entity (Id) then
4933 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4934
4935 -- If not library level entity, then indicate we don't know max
4936 -- tasks and also check task hierarchy restriction and blocking
4937 -- operation (since starting a task is definitely blocking).
4938
4939 else
4940 Check_Restriction (Max_Tasks, N);
4941 Check_Restriction (No_Task_Hierarchy, N);
4942 Check_Potentially_Blocking_Operation (N);
4943 end if;
4944
4945 -- A rather specialized test. If we see two tasks being declared
4946 -- of the same type in the same object declaration, and the task
4947 -- has an entry with an address clause, we know that program error
4948 -- will be raised at run time since we can't have two tasks with
4949 -- entries at the same address.
4950
4951 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4952 declare
4953 E : Entity_Id;
4954
4955 begin
4956 E := First_Entity (Etype (Id));
4957 while Present (E) loop
4958 if Ekind (E) = E_Entry
4959 and then Present (Get_Attribute_Definition_Clause
4960 (E, Attribute_Address))
4961 then
4962 Error_Msg_Warn := SPARK_Mode /= On;
4963 Error_Msg_N
4964 ("more than one task with same entry address<<", N);
4965 Error_Msg_N ("\Program_Error [<<", N);
4966 Insert_Action (N,
4967 Make_Raise_Program_Error (Loc,
4968 Reason => PE_Duplicated_Entry_Address));
4969 exit;
4970 end if;
4971
4972 Next_Entity (E);
4973 end loop;
4974 end;
4975 end if;
4976 end if;
4977
4978 -- Some simple constant-propagation: if the expression is a constant
4979 -- string initialized with a literal, share the literal. This avoids
4980 -- a run-time copy.
4981
4982 if Present (E)
4983 and then Is_Entity_Name (E)
4984 and then Ekind (Entity (E)) = E_Constant
4985 and then Base_Type (Etype (E)) = Standard_String
4986 then
4987 declare
4988 Val : constant Node_Id := Constant_Value (Entity (E));
4989 begin
4990 if Present (Val) and then Nkind (Val) = N_String_Literal then
4991 Rewrite (E, New_Copy (Val));
4992 end if;
4993 end;
4994 end if;
4995
4996 -- Another optimization: if the nominal subtype is unconstrained and
4997 -- the expression is a function call that returns an unconstrained
4998 -- type, rewrite the declaration as a renaming of the result of the
4999 -- call. The exceptions below are cases where the copy is expected,
5000 -- either by the back end (Aliased case) or by the semantics, as for
5001 -- initializing controlled types or copying tags for class-wide types.
5002
5003 if Present (E)
5004 and then Nkind (E) = N_Explicit_Dereference
5005 and then Nkind (Original_Node (E)) = N_Function_Call
5006 and then not Is_Library_Level_Entity (Id)
5007 and then not Is_Constrained (Underlying_Type (T))
5008 and then not Is_Aliased (Id)
5009 and then not Is_Class_Wide_Type (T)
5010 and then not Is_Controlled (T)
5011 and then not Has_Controlled_Component (Base_Type (T))
5012 and then Expander_Active
5013 then
5014 Rewrite (N,
5015 Make_Object_Renaming_Declaration (Loc,
5016 Defining_Identifier => Id,
5017 Access_Definition => Empty,
5018 Subtype_Mark => New_Occurrence_Of
5019 (Base_Type (Etype (Id)), Loc),
5020 Name => E));
5021
5022 Set_Renamed_Object (Id, E);
5023
5024 -- Force generation of debugging information for the constant and for
5025 -- the renamed function call.
5026
5027 Set_Debug_Info_Needed (Id);
5028 Set_Debug_Info_Needed (Entity (Prefix (E)));
5029 end if;
5030
5031 if Present (Prev_Entity)
5032 and then Is_Frozen (Prev_Entity)
5033 and then not Error_Posted (Id)
5034 then
5035 Error_Msg_N ("full constant declaration appears too late", N);
5036 end if;
5037
5038 Check_Eliminated (Id);
5039
5040 -- Deal with setting In_Private_Part flag if in private part
5041
5042 if Ekind (Scope (Id)) = E_Package
5043 and then In_Private_Part (Scope (Id))
5044 then
5045 Set_In_Private_Part (Id);
5046 end if;
5047
5048 <<Leave>>
5049 -- Initialize the refined state of a variable here because this is a
5050 -- common destination for legal and illegal object declarations.
5051
5052 if Ekind (Id) = E_Variable then
5053 Set_Encapsulating_State (Id, Empty);
5054 end if;
5055
5056 if Has_Aspects (N) then
5057 Analyze_Aspect_Specifications (N, Id);
5058 end if;
5059
5060 Analyze_Dimension (N);
5061
5062 -- Verify whether the object declaration introduces an illegal hidden
5063 -- state within a package subject to a null abstract state.
5064
5065 if Ekind (Id) = E_Variable then
5066 Check_No_Hidden_State (Id);
5067 end if;
5068
5069 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5070 end Analyze_Object_Declaration;
5071
5072 ---------------------------
5073 -- Analyze_Others_Choice --
5074 ---------------------------
5075
5076 -- Nothing to do for the others choice node itself, the semantic analysis
5077 -- of the others choice will occur as part of the processing of the parent
5078
5079 procedure Analyze_Others_Choice (N : Node_Id) is
5080 pragma Warnings (Off, N);
5081 begin
5082 null;
5083 end Analyze_Others_Choice;
5084
5085 -------------------------------------------
5086 -- Analyze_Private_Extension_Declaration --
5087 -------------------------------------------
5088
5089 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5090 Indic : constant Node_Id := Subtype_Indication (N);
5091 T : constant Entity_Id := Defining_Identifier (N);
5092 Iface : Entity_Id;
5093 Iface_Elmt : Elmt_Id;
5094 Parent_Base : Entity_Id;
5095 Parent_Type : Entity_Id;
5096
5097 begin
5098 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5099
5100 if Is_Non_Empty_List (Interface_List (N)) then
5101 declare
5102 Intf : Node_Id;
5103 T : Entity_Id;
5104
5105 begin
5106 Intf := First (Interface_List (N));
5107 while Present (Intf) loop
5108 T := Find_Type_Of_Subtype_Indic (Intf);
5109
5110 Diagnose_Interface (Intf, T);
5111 Next (Intf);
5112 end loop;
5113 end;
5114 end if;
5115
5116 Generate_Definition (T);
5117
5118 -- For other than Ada 2012, just enter the name in the current scope
5119
5120 if Ada_Version < Ada_2012 then
5121 Enter_Name (T);
5122
5123 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5124 -- case of private type that completes an incomplete type.
5125
5126 else
5127 declare
5128 Prev : Entity_Id;
5129
5130 begin
5131 Prev := Find_Type_Name (N);
5132
5133 pragma Assert (Prev = T
5134 or else (Ekind (Prev) = E_Incomplete_Type
5135 and then Present (Full_View (Prev))
5136 and then Full_View (Prev) = T));
5137 end;
5138 end if;
5139
5140 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5141 Parent_Base := Base_Type (Parent_Type);
5142
5143 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5144 Set_Ekind (T, Ekind (Parent_Type));
5145 Set_Etype (T, Any_Type);
5146 goto Leave;
5147
5148 elsif not Is_Tagged_Type (Parent_Type) then
5149 Error_Msg_N
5150 ("parent of type extension must be a tagged type ", Indic);
5151 goto Leave;
5152
5153 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5154 Error_Msg_N ("premature derivation of incomplete type", Indic);
5155 goto Leave;
5156
5157 elsif Is_Concurrent_Type (Parent_Type) then
5158 Error_Msg_N
5159 ("parent type of a private extension cannot be a synchronized "
5160 & "tagged type (RM 3.9.1 (3/1))", N);
5161
5162 Set_Etype (T, Any_Type);
5163 Set_Ekind (T, E_Limited_Private_Type);
5164 Set_Private_Dependents (T, New_Elmt_List);
5165 Set_Error_Posted (T);
5166 goto Leave;
5167 end if;
5168
5169 -- Perhaps the parent type should be changed to the class-wide type's
5170 -- specific type in this case to prevent cascading errors ???
5171
5172 if Is_Class_Wide_Type (Parent_Type) then
5173 Error_Msg_N
5174 ("parent of type extension must not be a class-wide type", Indic);
5175 goto Leave;
5176 end if;
5177
5178 if (not Is_Package_Or_Generic_Package (Current_Scope)
5179 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5180 or else In_Private_Part (Current_Scope)
5181 then
5182 Error_Msg_N ("invalid context for private extension", N);
5183 end if;
5184
5185 -- Set common attributes
5186
5187 Set_Is_Pure (T, Is_Pure (Current_Scope));
5188 Set_Scope (T, Current_Scope);
5189 Set_Ekind (T, E_Record_Type_With_Private);
5190 Init_Size_Align (T);
5191 Set_Default_SSO (T);
5192 Set_No_Reordering (T, No_Component_Reordering);
5193
5194 Set_Etype (T, Parent_Base);
5195 Propagate_Concurrent_Flags (T, Parent_Base);
5196
5197 Set_Convention (T, Convention (Parent_Type));
5198 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5199 Set_Is_First_Subtype (T);
5200 Make_Class_Wide_Type (T);
5201
5202 -- Set the SPARK mode from the current context
5203
5204 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5205 Set_SPARK_Pragma_Inherited (T);
5206
5207 if Unknown_Discriminants_Present (N) then
5208 Set_Discriminant_Constraint (T, No_Elist);
5209 end if;
5210
5211 Build_Derived_Record_Type (N, Parent_Type, T);
5212
5213 -- A private extension inherits the Default_Initial_Condition pragma
5214 -- coming from any parent type within the derivation chain.
5215
5216 if Has_DIC (Parent_Type) then
5217 Set_Has_Inherited_DIC (T);
5218 end if;
5219
5220 -- A private extension inherits any class-wide invariants coming from a
5221 -- parent type or an interface. Note that the invariant procedure of the
5222 -- parent type should not be inherited because the private extension may
5223 -- define invariants of its own.
5224
5225 if Has_Inherited_Invariants (Parent_Type)
5226 or else Has_Inheritable_Invariants (Parent_Type)
5227 then
5228 Set_Has_Inherited_Invariants (T);
5229
5230 elsif Present (Interfaces (T)) then
5231 Iface_Elmt := First_Elmt (Interfaces (T));
5232 while Present (Iface_Elmt) loop
5233 Iface := Node (Iface_Elmt);
5234
5235 if Has_Inheritable_Invariants (Iface) then
5236 Set_Has_Inherited_Invariants (T);
5237 exit;
5238 end if;
5239
5240 Next_Elmt (Iface_Elmt);
5241 end loop;
5242 end if;
5243
5244 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5245 -- synchronized formal derived type.
5246
5247 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5248 Set_Is_Limited_Record (T);
5249
5250 -- Formal derived type case
5251
5252 if Is_Generic_Type (T) then
5253
5254 -- The parent must be a tagged limited type or a synchronized
5255 -- interface.
5256
5257 if (not Is_Tagged_Type (Parent_Type)
5258 or else not Is_Limited_Type (Parent_Type))
5259 and then
5260 (not Is_Interface (Parent_Type)
5261 or else not Is_Synchronized_Interface (Parent_Type))
5262 then
5263 Error_Msg_NE
5264 ("parent type of & must be tagged limited or synchronized",
5265 N, T);
5266 end if;
5267
5268 -- The progenitors (if any) must be limited or synchronized
5269 -- interfaces.
5270
5271 if Present (Interfaces (T)) then
5272 Iface_Elmt := First_Elmt (Interfaces (T));
5273 while Present (Iface_Elmt) loop
5274 Iface := Node (Iface_Elmt);
5275
5276 if not Is_Limited_Interface (Iface)
5277 and then not Is_Synchronized_Interface (Iface)
5278 then
5279 Error_Msg_NE
5280 ("progenitor & must be limited or synchronized",
5281 N, Iface);
5282 end if;
5283
5284 Next_Elmt (Iface_Elmt);
5285 end loop;
5286 end if;
5287
5288 -- Regular derived extension, the parent must be a limited or
5289 -- synchronized interface.
5290
5291 else
5292 if not Is_Interface (Parent_Type)
5293 or else (not Is_Limited_Interface (Parent_Type)
5294 and then not Is_Synchronized_Interface (Parent_Type))
5295 then
5296 Error_Msg_NE
5297 ("parent type of & must be limited interface", N, T);
5298 end if;
5299 end if;
5300
5301 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5302 -- extension with a synchronized parent must be explicitly declared
5303 -- synchronized, because the full view will be a synchronized type.
5304 -- This must be checked before the check for limited types below,
5305 -- to ensure that types declared limited are not allowed to extend
5306 -- synchronized interfaces.
5307
5308 elsif Is_Interface (Parent_Type)
5309 and then Is_Synchronized_Interface (Parent_Type)
5310 and then not Synchronized_Present (N)
5311 then
5312 Error_Msg_NE
5313 ("private extension of& must be explicitly synchronized",
5314 N, Parent_Type);
5315
5316 elsif Limited_Present (N) then
5317 Set_Is_Limited_Record (T);
5318
5319 if not Is_Limited_Type (Parent_Type)
5320 and then
5321 (not Is_Interface (Parent_Type)
5322 or else not Is_Limited_Interface (Parent_Type))
5323 then
5324 Error_Msg_NE ("parent type& of limited extension must be limited",
5325 N, Parent_Type);
5326 end if;
5327 end if;
5328
5329 -- Remember that its parent type has a private extension. Used to warn
5330 -- on public primitives of the parent type defined after its private
5331 -- extensions (see Check_Dispatching_Operation).
5332
5333 Set_Has_Private_Extension (Parent_Type);
5334
5335 <<Leave>>
5336 if Has_Aspects (N) then
5337 Analyze_Aspect_Specifications (N, T);
5338 end if;
5339 end Analyze_Private_Extension_Declaration;
5340
5341 ---------------------------------
5342 -- Analyze_Subtype_Declaration --
5343 ---------------------------------
5344
5345 procedure Analyze_Subtype_Declaration
5346 (N : Node_Id;
5347 Skip : Boolean := False)
5348 is
5349 Id : constant Entity_Id := Defining_Identifier (N);
5350 R_Checks : Check_Result;
5351 T : Entity_Id;
5352
5353 begin
5354 Generate_Definition (Id);
5355 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5356 Init_Size_Align (Id);
5357
5358 -- The following guard condition on Enter_Name is to handle cases where
5359 -- the defining identifier has already been entered into the scope but
5360 -- the declaration as a whole needs to be analyzed.
5361
5362 -- This case in particular happens for derived enumeration types. The
5363 -- derived enumeration type is processed as an inserted enumeration type
5364 -- declaration followed by a rewritten subtype declaration. The defining
5365 -- identifier, however, is entered into the name scope very early in the
5366 -- processing of the original type declaration and therefore needs to be
5367 -- avoided here, when the created subtype declaration is analyzed. (See
5368 -- Build_Derived_Types)
5369
5370 -- This also happens when the full view of a private type is derived
5371 -- type with constraints. In this case the entity has been introduced
5372 -- in the private declaration.
5373
5374 -- Finally this happens in some complex cases when validity checks are
5375 -- enabled, where the same subtype declaration may be analyzed twice.
5376 -- This can happen if the subtype is created by the preanalysis of
5377 -- an attribute tht gives the range of a loop statement, and the loop
5378 -- itself appears within an if_statement that will be rewritten during
5379 -- expansion.
5380
5381 if Skip
5382 or else (Present (Etype (Id))
5383 and then (Is_Private_Type (Etype (Id))
5384 or else Is_Task_Type (Etype (Id))
5385 or else Is_Rewrite_Substitution (N)))
5386 then
5387 null;
5388
5389 elsif Current_Entity (Id) = Id then
5390 null;
5391
5392 else
5393 Enter_Name (Id);
5394 end if;
5395
5396 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5397
5398 -- Class-wide equivalent types of records with unknown discriminants
5399 -- involve the generation of an itype which serves as the private view
5400 -- of a constrained record subtype. In such cases the base type of the
5401 -- current subtype we are processing is the private itype. Use the full
5402 -- of the private itype when decorating various attributes.
5403
5404 if Is_Itype (T)
5405 and then Is_Private_Type (T)
5406 and then Present (Full_View (T))
5407 then
5408 T := Full_View (T);
5409 end if;
5410
5411 -- Inherit common attributes
5412
5413 Set_Is_Volatile (Id, Is_Volatile (T));
5414 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5415 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5416 Set_Convention (Id, Convention (T));
5417
5418 -- If ancestor has predicates then so does the subtype, and in addition
5419 -- we must delay the freeze to properly arrange predicate inheritance.
5420
5421 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5422 -- in which T = ID, so the above tests and assignments do nothing???
5423
5424 if Has_Predicates (T)
5425 or else (Present (Ancestor_Subtype (T))
5426 and then Has_Predicates (Ancestor_Subtype (T)))
5427 then
5428 Set_Has_Predicates (Id);
5429 Set_Has_Delayed_Freeze (Id);
5430
5431 -- Generated subtypes inherit the predicate function from the parent
5432 -- (no aspects to examine on the generated declaration).
5433
5434 if not Comes_From_Source (N) then
5435 Set_Ekind (Id, Ekind (T));
5436
5437 if Present (Predicate_Function (Id)) then
5438 null;
5439
5440 elsif Present (Predicate_Function (T)) then
5441 Set_Predicate_Function (Id, Predicate_Function (T));
5442
5443 elsif Present (Ancestor_Subtype (T))
5444 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5445 then
5446 Set_Predicate_Function (Id,
5447 Predicate_Function (Ancestor_Subtype (T)));
5448 end if;
5449 end if;
5450 end if;
5451
5452 -- Subtype of Boolean cannot have a constraint in SPARK
5453
5454 if Is_Boolean_Type (T)
5455 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5456 then
5457 Check_SPARK_05_Restriction
5458 ("subtype of Boolean cannot have constraint", N);
5459 end if;
5460
5461 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5462 declare
5463 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5464 One_Cstr : Node_Id;
5465 Low : Node_Id;
5466 High : Node_Id;
5467
5468 begin
5469 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5470 One_Cstr := First (Constraints (Cstr));
5471 while Present (One_Cstr) loop
5472
5473 -- Index or discriminant constraint in SPARK must be a
5474 -- subtype mark.
5475
5476 if not
5477 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5478 then
5479 Check_SPARK_05_Restriction
5480 ("subtype mark required", One_Cstr);
5481
5482 -- String subtype must have a lower bound of 1 in SPARK.
5483 -- Note that we do not need to test for the nonstatic case
5484 -- here, since that was already taken care of in
5485 -- Process_Range_Expr_In_Decl.
5486
5487 elsif Base_Type (T) = Standard_String then
5488 Get_Index_Bounds (One_Cstr, Low, High);
5489
5490 if Is_OK_Static_Expression (Low)
5491 and then Expr_Value (Low) /= 1
5492 then
5493 Check_SPARK_05_Restriction
5494 ("String subtype must have lower bound of 1", N);
5495 end if;
5496 end if;
5497
5498 Next (One_Cstr);
5499 end loop;
5500 end if;
5501 end;
5502 end if;
5503
5504 -- In the case where there is no constraint given in the subtype
5505 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5506 -- semantic attributes must be established here.
5507
5508 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5509 Set_Etype (Id, Base_Type (T));
5510
5511 -- Subtype of unconstrained array without constraint is not allowed
5512 -- in SPARK.
5513
5514 if Is_Array_Type (T) and then not Is_Constrained (T) then
5515 Check_SPARK_05_Restriction
5516 ("subtype of unconstrained array must have constraint", N);
5517 end if;
5518
5519 case Ekind (T) is
5520 when Array_Kind =>
5521 Set_Ekind (Id, E_Array_Subtype);
5522 Copy_Array_Subtype_Attributes (Id, T);
5523
5524 when Decimal_Fixed_Point_Kind =>
5525 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5526 Set_Digits_Value (Id, Digits_Value (T));
5527 Set_Delta_Value (Id, Delta_Value (T));
5528 Set_Scale_Value (Id, Scale_Value (T));
5529 Set_Small_Value (Id, Small_Value (T));
5530 Set_Scalar_Range (Id, Scalar_Range (T));
5531 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5532 Set_Is_Constrained (Id, Is_Constrained (T));
5533 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5534 Set_RM_Size (Id, RM_Size (T));
5535
5536 when Enumeration_Kind =>
5537 Set_Ekind (Id, E_Enumeration_Subtype);
5538 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5539 Set_Scalar_Range (Id, Scalar_Range (T));
5540 Set_Is_Character_Type (Id, Is_Character_Type (T));
5541 Set_Is_Constrained (Id, Is_Constrained (T));
5542 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5543 Set_RM_Size (Id, RM_Size (T));
5544
5545 when Ordinary_Fixed_Point_Kind =>
5546 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5547 Set_Scalar_Range (Id, Scalar_Range (T));
5548 Set_Small_Value (Id, Small_Value (T));
5549 Set_Delta_Value (Id, Delta_Value (T));
5550 Set_Is_Constrained (Id, Is_Constrained (T));
5551 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5552 Set_RM_Size (Id, RM_Size (T));
5553
5554 when Float_Kind =>
5555 Set_Ekind (Id, E_Floating_Point_Subtype);
5556 Set_Scalar_Range (Id, Scalar_Range (T));
5557 Set_Digits_Value (Id, Digits_Value (T));
5558 Set_Is_Constrained (Id, Is_Constrained (T));
5559
5560 -- If the floating point type has dimensions, these will be
5561 -- inherited subsequently when Analyze_Dimensions is called.
5562
5563 when Signed_Integer_Kind =>
5564 Set_Ekind (Id, E_Signed_Integer_Subtype);
5565 Set_Scalar_Range (Id, Scalar_Range (T));
5566 Set_Is_Constrained (Id, Is_Constrained (T));
5567 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5568 Set_RM_Size (Id, RM_Size (T));
5569
5570 when Modular_Integer_Kind =>
5571 Set_Ekind (Id, E_Modular_Integer_Subtype);
5572 Set_Scalar_Range (Id, Scalar_Range (T));
5573 Set_Is_Constrained (Id, Is_Constrained (T));
5574 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5575 Set_RM_Size (Id, RM_Size (T));
5576
5577 when Class_Wide_Kind =>
5578 Set_Ekind (Id, E_Class_Wide_Subtype);
5579 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5580 Set_Cloned_Subtype (Id, T);
5581 Set_Is_Tagged_Type (Id, True);
5582 Set_Has_Unknown_Discriminants
5583 (Id, True);
5584 Set_No_Tagged_Streams_Pragma
5585 (Id, No_Tagged_Streams_Pragma (T));
5586
5587 if Ekind (T) = E_Class_Wide_Subtype then
5588 Set_Equivalent_Type (Id, Equivalent_Type (T));
5589 end if;
5590
5591 when E_Record_Subtype
5592 | E_Record_Type
5593 =>
5594 Set_Ekind (Id, E_Record_Subtype);
5595
5596 -- Subtype declarations introduced for formal type parameters
5597 -- in generic instantiations should inherit the Size value of
5598 -- the type they rename.
5599
5600 if Present (Generic_Parent_Type (N)) then
5601 Set_RM_Size (Id, RM_Size (T));
5602 end if;
5603
5604 if Ekind (T) = E_Record_Subtype
5605 and then Present (Cloned_Subtype (T))
5606 then
5607 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5608 else
5609 Set_Cloned_Subtype (Id, T);
5610 end if;
5611
5612 Set_First_Entity (Id, First_Entity (T));
5613 Set_Last_Entity (Id, Last_Entity (T));
5614 Set_Has_Discriminants (Id, Has_Discriminants (T));
5615 Set_Is_Constrained (Id, Is_Constrained (T));
5616 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5617 Set_Has_Implicit_Dereference
5618 (Id, Has_Implicit_Dereference (T));
5619 Set_Has_Unknown_Discriminants
5620 (Id, Has_Unknown_Discriminants (T));
5621
5622 if Has_Discriminants (T) then
5623 Set_Discriminant_Constraint
5624 (Id, Discriminant_Constraint (T));
5625 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5626
5627 elsif Has_Unknown_Discriminants (Id) then
5628 Set_Discriminant_Constraint (Id, No_Elist);
5629 end if;
5630
5631 if Is_Tagged_Type (T) then
5632 Set_Is_Tagged_Type (Id, True);
5633 Set_No_Tagged_Streams_Pragma
5634 (Id, No_Tagged_Streams_Pragma (T));
5635 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5636 Set_Direct_Primitive_Operations
5637 (Id, Direct_Primitive_Operations (T));
5638 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5639
5640 if Is_Interface (T) then
5641 Set_Is_Interface (Id);
5642 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5643 end if;
5644 end if;
5645
5646 when Private_Kind =>
5647 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5648 Set_Has_Discriminants (Id, Has_Discriminants (T));
5649 Set_Is_Constrained (Id, Is_Constrained (T));
5650 Set_First_Entity (Id, First_Entity (T));
5651 Set_Last_Entity (Id, Last_Entity (T));
5652 Set_Private_Dependents (Id, New_Elmt_List);
5653 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5654 Set_Has_Implicit_Dereference
5655 (Id, Has_Implicit_Dereference (T));
5656 Set_Has_Unknown_Discriminants
5657 (Id, Has_Unknown_Discriminants (T));
5658 Set_Known_To_Have_Preelab_Init
5659 (Id, Known_To_Have_Preelab_Init (T));
5660
5661 if Is_Tagged_Type (T) then
5662 Set_Is_Tagged_Type (Id);
5663 Set_No_Tagged_Streams_Pragma (Id,
5664 No_Tagged_Streams_Pragma (T));
5665 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5666 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5667 Set_Direct_Primitive_Operations (Id,
5668 Direct_Primitive_Operations (T));
5669 end if;
5670
5671 -- In general the attributes of the subtype of a private type
5672 -- are the attributes of the partial view of parent. However,
5673 -- the full view may be a discriminated type, and the subtype
5674 -- must share the discriminant constraint to generate correct
5675 -- calls to initialization procedures.
5676
5677 if Has_Discriminants (T) then
5678 Set_Discriminant_Constraint
5679 (Id, Discriminant_Constraint (T));
5680 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5681
5682 elsif Present (Full_View (T))
5683 and then Has_Discriminants (Full_View (T))
5684 then
5685 Set_Discriminant_Constraint
5686 (Id, Discriminant_Constraint (Full_View (T)));
5687 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5688
5689 -- This would seem semantically correct, but apparently
5690 -- generates spurious errors about missing components ???
5691
5692 -- Set_Has_Discriminants (Id);
5693 end if;
5694
5695 Prepare_Private_Subtype_Completion (Id, N);
5696
5697 -- If this is the subtype of a constrained private type with
5698 -- discriminants that has got a full view and we also have
5699 -- built a completion just above, show that the completion
5700 -- is a clone of the full view to the back-end.
5701
5702 if Has_Discriminants (T)
5703 and then not Has_Unknown_Discriminants (T)
5704 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5705 and then Present (Full_View (T))
5706 and then Present (Full_View (Id))
5707 then
5708 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5709 end if;
5710
5711 when Access_Kind =>
5712 Set_Ekind (Id, E_Access_Subtype);
5713 Set_Is_Constrained (Id, Is_Constrained (T));
5714 Set_Is_Access_Constant
5715 (Id, Is_Access_Constant (T));
5716 Set_Directly_Designated_Type
5717 (Id, Designated_Type (T));
5718 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5719
5720 -- A Pure library_item must not contain the declaration of a
5721 -- named access type, except within a subprogram, generic
5722 -- subprogram, task unit, or protected unit, or if it has
5723 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5724
5725 if Comes_From_Source (Id)
5726 and then In_Pure_Unit
5727 and then not In_Subprogram_Task_Protected_Unit
5728 and then not No_Pool_Assigned (Id)
5729 then
5730 Error_Msg_N
5731 ("named access types not allowed in pure unit", N);
5732 end if;
5733
5734 when Concurrent_Kind =>
5735 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5736 Set_Corresponding_Record_Type (Id,
5737 Corresponding_Record_Type (T));
5738 Set_First_Entity (Id, First_Entity (T));
5739 Set_First_Private_Entity (Id, First_Private_Entity (T));
5740 Set_Has_Discriminants (Id, Has_Discriminants (T));
5741 Set_Is_Constrained (Id, Is_Constrained (T));
5742 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5743 Set_Last_Entity (Id, Last_Entity (T));
5744
5745 if Is_Tagged_Type (T) then
5746 Set_No_Tagged_Streams_Pragma
5747 (Id, No_Tagged_Streams_Pragma (T));
5748 end if;
5749
5750 if Has_Discriminants (T) then
5751 Set_Discriminant_Constraint
5752 (Id, Discriminant_Constraint (T));
5753 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5754 end if;
5755
5756 when Incomplete_Kind =>
5757 if Ada_Version >= Ada_2005 then
5758
5759 -- In Ada 2005 an incomplete type can be explicitly tagged:
5760 -- propagate indication. Note that we also have to include
5761 -- subtypes for Ada 2012 extended use of incomplete types.
5762
5763 Set_Ekind (Id, E_Incomplete_Subtype);
5764 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5765 Set_Private_Dependents (Id, New_Elmt_List);
5766
5767 if Is_Tagged_Type (Id) then
5768 Set_No_Tagged_Streams_Pragma
5769 (Id, No_Tagged_Streams_Pragma (T));
5770 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5771 end if;
5772
5773 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5774 -- incomplete type visible through a limited with clause.
5775
5776 if From_Limited_With (T)
5777 and then Present (Non_Limited_View (T))
5778 then
5779 Set_From_Limited_With (Id);
5780 Set_Non_Limited_View (Id, Non_Limited_View (T));
5781
5782 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5783 -- to the private dependents of the original incomplete
5784 -- type for future transformation.
5785
5786 else
5787 Append_Elmt (Id, Private_Dependents (T));
5788 end if;
5789
5790 -- If the subtype name denotes an incomplete type an error
5791 -- was already reported by Process_Subtype.
5792
5793 else
5794 Set_Etype (Id, Any_Type);
5795 end if;
5796
5797 when others =>
5798 raise Program_Error;
5799 end case;
5800
5801 -- If there is no constraint in the subtype indication, the
5802 -- declared entity inherits predicates from the parent.
5803
5804 Inherit_Predicate_Flags (Id, T);
5805 end if;
5806
5807 if Etype (Id) = Any_Type then
5808 goto Leave;
5809 end if;
5810
5811 -- Some common processing on all types
5812
5813 Set_Size_Info (Id, T);
5814 Set_First_Rep_Item (Id, First_Rep_Item (T));
5815
5816 -- If the parent type is a generic actual, so is the subtype. This may
5817 -- happen in a nested instance. Why Comes_From_Source test???
5818
5819 if not Comes_From_Source (N) then
5820 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5821 end if;
5822
5823 -- If this is a subtype declaration for an actual in an instance,
5824 -- inherit static and dynamic predicates if any.
5825
5826 -- If declaration has no aspect specifications, inherit predicate
5827 -- info as well. Unclear how to handle the case of both specified
5828 -- and inherited predicates ??? Other inherited aspects, such as
5829 -- invariants, should be OK, but the combination with later pragmas
5830 -- may also require special merging.
5831
5832 if Has_Predicates (T)
5833 and then Present (Predicate_Function (T))
5834 and then
5835 ((In_Instance and then not Comes_From_Source (N))
5836 or else No (Aspect_Specifications (N)))
5837 then
5838 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5839
5840 if Has_Static_Predicate (T) then
5841 Set_Has_Static_Predicate (Id);
5842 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5843 end if;
5844 end if;
5845
5846 -- Remaining processing depends on characteristics of base type
5847
5848 T := Etype (Id);
5849
5850 Set_Is_Immediately_Visible (Id, True);
5851 Set_Depends_On_Private (Id, Has_Private_Component (T));
5852 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5853
5854 if Is_Interface (T) then
5855 Set_Is_Interface (Id);
5856 end if;
5857
5858 if Present (Generic_Parent_Type (N))
5859 and then
5860 (Nkind (Parent (Generic_Parent_Type (N))) /=
5861 N_Formal_Type_Declaration
5862 or else Nkind (Formal_Type_Definition
5863 (Parent (Generic_Parent_Type (N)))) /=
5864 N_Formal_Private_Type_Definition)
5865 then
5866 if Is_Tagged_Type (Id) then
5867
5868 -- If this is a generic actual subtype for a synchronized type,
5869 -- the primitive operations are those of the corresponding record
5870 -- for which there is a separate subtype declaration.
5871
5872 if Is_Concurrent_Type (Id) then
5873 null;
5874 elsif Is_Class_Wide_Type (Id) then
5875 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5876 else
5877 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5878 end if;
5879
5880 elsif Scope (Etype (Id)) /= Standard_Standard then
5881 Derive_Subprograms (Generic_Parent_Type (N), Id);
5882 end if;
5883 end if;
5884
5885 if Is_Private_Type (T) and then Present (Full_View (T)) then
5886 Conditional_Delay (Id, Full_View (T));
5887
5888 -- The subtypes of components or subcomponents of protected types
5889 -- do not need freeze nodes, which would otherwise appear in the
5890 -- wrong scope (before the freeze node for the protected type). The
5891 -- proper subtypes are those of the subcomponents of the corresponding
5892 -- record.
5893
5894 elsif Ekind (Scope (Id)) /= E_Protected_Type
5895 and then Present (Scope (Scope (Id))) -- error defense
5896 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5897 then
5898 Conditional_Delay (Id, T);
5899 end if;
5900
5901 -- If we have a subtype of an incomplete type whose full type is a
5902 -- derived numeric type, we need to have a freeze node for the subtype.
5903 -- Otherwise gigi will complain while computing the (static) bounds of
5904 -- the subtype.
5905
5906 if Is_Itype (T)
5907 and then Is_Elementary_Type (Id)
5908 and then Etype (Id) /= Id
5909 then
5910 declare
5911 Partial : constant Entity_Id :=
5912 Incomplete_Or_Partial_View (First_Subtype (Id));
5913 begin
5914 if Present (Partial)
5915 and then Ekind (Partial) = E_Incomplete_Type
5916 then
5917 Set_Has_Delayed_Freeze (Id);
5918 end if;
5919 end;
5920 end if;
5921
5922 -- Check that Constraint_Error is raised for a scalar subtype indication
5923 -- when the lower or upper bound of a non-null range lies outside the
5924 -- range of the type mark.
5925
5926 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5927 if Is_Scalar_Type (Etype (Id))
5928 and then Scalar_Range (Id) /=
5929 Scalar_Range
5930 (Etype (Subtype_Mark (Subtype_Indication (N))))
5931 then
5932 Apply_Range_Check
5933 (Scalar_Range (Id),
5934 Etype (Subtype_Mark (Subtype_Indication (N))));
5935
5936 -- In the array case, check compatibility for each index
5937
5938 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5939 then
5940 -- This really should be a subprogram that finds the indications
5941 -- to check???
5942
5943 declare
5944 Subt_Index : Node_Id := First_Index (Id);
5945 Target_Index : Node_Id :=
5946 First_Index (Etype
5947 (Subtype_Mark (Subtype_Indication (N))));
5948 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5949
5950 begin
5951 while Present (Subt_Index) loop
5952 if ((Nkind (Subt_Index) = N_Identifier
5953 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5954 or else Nkind (Subt_Index) = N_Subtype_Indication)
5955 and then
5956 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5957 then
5958 declare
5959 Target_Typ : constant Entity_Id :=
5960 Etype (Target_Index);
5961 begin
5962 R_Checks :=
5963 Get_Range_Checks
5964 (Scalar_Range (Etype (Subt_Index)),
5965 Target_Typ,
5966 Etype (Subt_Index),
5967 Defining_Identifier (N));
5968
5969 -- Reset Has_Dynamic_Range_Check on the subtype to
5970 -- prevent elision of the index check due to a dynamic
5971 -- check generated for a preceding index (needed since
5972 -- Insert_Range_Checks tries to avoid generating
5973 -- redundant checks on a given declaration).
5974
5975 Set_Has_Dynamic_Range_Check (N, False);
5976
5977 Insert_Range_Checks
5978 (R_Checks,
5979 N,
5980 Target_Typ,
5981 Sloc (Defining_Identifier (N)));
5982
5983 -- Record whether this index involved a dynamic check
5984
5985 Has_Dyn_Chk :=
5986 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5987 end;
5988 end if;
5989
5990 Next_Index (Subt_Index);
5991 Next_Index (Target_Index);
5992 end loop;
5993
5994 -- Finally, mark whether the subtype involves dynamic checks
5995
5996 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5997 end;
5998 end if;
5999 end if;
6000
6001 Set_Optimize_Alignment_Flags (Id);
6002 Check_Eliminated (Id);
6003
6004 <<Leave>>
6005 if Has_Aspects (N) then
6006 Analyze_Aspect_Specifications (N, Id);
6007 end if;
6008
6009 Analyze_Dimension (N);
6010
6011 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6012 -- indications on composite types where the constraints are dynamic.
6013 -- Note that object declarations and aggregates generate implicit
6014 -- subtype declarations, which this covers. One special case is that the
6015 -- implicitly generated "=" for discriminated types includes an
6016 -- offending subtype declaration, which is harmless, so we ignore it
6017 -- here.
6018
6019 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6020 declare
6021 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6022 begin
6023 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6024 and then not (Is_Internal (Id)
6025 and then Is_TSS (Scope (Id),
6026 TSS_Composite_Equality))
6027 and then not Within_Init_Proc
6028 and then not All_Composite_Constraints_Static (Cstr)
6029 then
6030 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6031 end if;
6032 end;
6033 end if;
6034 end Analyze_Subtype_Declaration;
6035
6036 --------------------------------
6037 -- Analyze_Subtype_Indication --
6038 --------------------------------
6039
6040 procedure Analyze_Subtype_Indication (N : Node_Id) is
6041 T : constant Entity_Id := Subtype_Mark (N);
6042 R : constant Node_Id := Range_Expression (Constraint (N));
6043
6044 begin
6045 Analyze (T);
6046
6047 if R /= Error then
6048 Analyze (R);
6049 Set_Etype (N, Etype (R));
6050 Resolve (R, Entity (T));
6051 else
6052 Set_Error_Posted (R);
6053 Set_Error_Posted (T);
6054 end if;
6055 end Analyze_Subtype_Indication;
6056
6057 --------------------------
6058 -- Analyze_Variant_Part --
6059 --------------------------
6060
6061 procedure Analyze_Variant_Part (N : Node_Id) is
6062 Discr_Name : Node_Id;
6063 Discr_Type : Entity_Id;
6064
6065 procedure Process_Variant (A : Node_Id);
6066 -- Analyze declarations for a single variant
6067
6068 package Analyze_Variant_Choices is
6069 new Generic_Analyze_Choices (Process_Variant);
6070 use Analyze_Variant_Choices;
6071
6072 ---------------------
6073 -- Process_Variant --
6074 ---------------------
6075
6076 procedure Process_Variant (A : Node_Id) is
6077 CL : constant Node_Id := Component_List (A);
6078 begin
6079 if not Null_Present (CL) then
6080 Analyze_Declarations (Component_Items (CL));
6081
6082 if Present (Variant_Part (CL)) then
6083 Analyze (Variant_Part (CL));
6084 end if;
6085 end if;
6086 end Process_Variant;
6087
6088 -- Start of processing for Analyze_Variant_Part
6089
6090 begin
6091 Discr_Name := Name (N);
6092 Analyze (Discr_Name);
6093
6094 -- If Discr_Name bad, get out (prevent cascaded errors)
6095
6096 if Etype (Discr_Name) = Any_Type then
6097 return;
6098 end if;
6099
6100 -- Check invalid discriminant in variant part
6101
6102 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6103 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6104 end if;
6105
6106 Discr_Type := Etype (Entity (Discr_Name));
6107
6108 if not Is_Discrete_Type (Discr_Type) then
6109 Error_Msg_N
6110 ("discriminant in a variant part must be of a discrete type",
6111 Name (N));
6112 return;
6113 end if;
6114
6115 -- Now analyze the choices, which also analyzes the declarations that
6116 -- are associated with each choice.
6117
6118 Analyze_Choices (Variants (N), Discr_Type);
6119
6120 -- Note: we used to instantiate and call Check_Choices here to check
6121 -- that the choices covered the discriminant, but it's too early to do
6122 -- that because of statically predicated subtypes, whose analysis may
6123 -- be deferred to their freeze point which may be as late as the freeze
6124 -- point of the containing record. So this call is now to be found in
6125 -- Freeze_Record_Declaration.
6126
6127 end Analyze_Variant_Part;
6128
6129 ----------------------------
6130 -- Array_Type_Declaration --
6131 ----------------------------
6132
6133 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6134 Component_Def : constant Node_Id := Component_Definition (Def);
6135 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6136 P : constant Node_Id := Parent (Def);
6137 Element_Type : Entity_Id;
6138 Implicit_Base : Entity_Id;
6139 Index : Node_Id;
6140 Nb_Index : Nat;
6141 Priv : Entity_Id;
6142 Related_Id : Entity_Id := Empty;
6143
6144 begin
6145 if Nkind (Def) = N_Constrained_Array_Definition then
6146 Index := First (Discrete_Subtype_Definitions (Def));
6147 else
6148 Index := First (Subtype_Marks (Def));
6149 end if;
6150
6151 -- Find proper names for the implicit types which may be public. In case
6152 -- of anonymous arrays we use the name of the first object of that type
6153 -- as prefix.
6154
6155 if No (T) then
6156 Related_Id := Defining_Identifier (P);
6157 else
6158 Related_Id := T;
6159 end if;
6160
6161 Nb_Index := 1;
6162 while Present (Index) loop
6163 Analyze (Index);
6164
6165 -- Test for odd case of trying to index a type by the type itself
6166
6167 if Is_Entity_Name (Index) and then Entity (Index) = T then
6168 Error_Msg_N ("type& cannot be indexed by itself", Index);
6169 Set_Entity (Index, Standard_Boolean);
6170 Set_Etype (Index, Standard_Boolean);
6171 end if;
6172
6173 -- Check SPARK restriction requiring a subtype mark
6174
6175 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6176 Check_SPARK_05_Restriction ("subtype mark required", Index);
6177 end if;
6178
6179 -- Add a subtype declaration for each index of private array type
6180 -- declaration whose etype is also private. For example:
6181
6182 -- package Pkg is
6183 -- type Index is private;
6184 -- private
6185 -- type Table is array (Index) of ...
6186 -- end;
6187
6188 -- This is currently required by the expander for the internally
6189 -- generated equality subprogram of records with variant parts in
6190 -- which the etype of some component is such private type.
6191
6192 if Ekind (Current_Scope) = E_Package
6193 and then In_Private_Part (Current_Scope)
6194 and then Has_Private_Declaration (Etype (Index))
6195 then
6196 declare
6197 Loc : constant Source_Ptr := Sloc (Def);
6198 Decl : Entity_Id;
6199 New_E : Entity_Id;
6200
6201 begin
6202 New_E := Make_Temporary (Loc, 'T');
6203 Set_Is_Internal (New_E);
6204
6205 Decl :=
6206 Make_Subtype_Declaration (Loc,
6207 Defining_Identifier => New_E,
6208 Subtype_Indication =>
6209 New_Occurrence_Of (Etype (Index), Loc));
6210
6211 Insert_Before (Parent (Def), Decl);
6212 Analyze (Decl);
6213 Set_Etype (Index, New_E);
6214
6215 -- If the index is a range or a subtype indication it carries
6216 -- no entity. Example:
6217
6218 -- package Pkg is
6219 -- type T is private;
6220 -- private
6221 -- type T is new Natural;
6222 -- Table : array (T(1) .. T(10)) of Boolean;
6223 -- end Pkg;
6224
6225 -- Otherwise the type of the reference is its entity.
6226
6227 if Is_Entity_Name (Index) then
6228 Set_Entity (Index, New_E);
6229 end if;
6230 end;
6231 end if;
6232
6233 Make_Index (Index, P, Related_Id, Nb_Index);
6234
6235 -- Check error of subtype with predicate for index type
6236
6237 Bad_Predicated_Subtype_Use
6238 ("subtype& has predicate, not allowed as index subtype",
6239 Index, Etype (Index));
6240
6241 -- Move to next index
6242
6243 Next_Index (Index);
6244 Nb_Index := Nb_Index + 1;
6245 end loop;
6246
6247 -- Process subtype indication if one is present
6248
6249 if Present (Component_Typ) then
6250 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6251
6252 Set_Etype (Component_Typ, Element_Type);
6253
6254 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6255 Check_SPARK_05_Restriction
6256 ("subtype mark required", Component_Typ);
6257 end if;
6258
6259 -- Ada 2005 (AI-230): Access Definition case
6260
6261 else pragma Assert (Present (Access_Definition (Component_Def)));
6262
6263 -- Indicate that the anonymous access type is created by the
6264 -- array type declaration.
6265
6266 Element_Type := Access_Definition
6267 (Related_Nod => P,
6268 N => Access_Definition (Component_Def));
6269 Set_Is_Local_Anonymous_Access (Element_Type);
6270
6271 -- Propagate the parent. This field is needed if we have to generate
6272 -- the master_id associated with an anonymous access to task type
6273 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6274
6275 Set_Parent (Element_Type, Parent (T));
6276
6277 -- Ada 2005 (AI-230): In case of components that are anonymous access
6278 -- types the level of accessibility depends on the enclosing type
6279 -- declaration
6280
6281 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6282
6283 -- Ada 2005 (AI-254)
6284
6285 declare
6286 CD : constant Node_Id :=
6287 Access_To_Subprogram_Definition
6288 (Access_Definition (Component_Def));
6289 begin
6290 if Present (CD) and then Protected_Present (CD) then
6291 Element_Type :=
6292 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6293 end if;
6294 end;
6295 end if;
6296
6297 -- Constrained array case
6298
6299 if No (T) then
6300 T := Create_Itype (E_Void, P, Related_Id, 'T');
6301 end if;
6302
6303 if Nkind (Def) = N_Constrained_Array_Definition then
6304
6305 -- Establish Implicit_Base as unconstrained base type
6306
6307 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6308
6309 Set_Etype (Implicit_Base, Implicit_Base);
6310 Set_Scope (Implicit_Base, Current_Scope);
6311 Set_Has_Delayed_Freeze (Implicit_Base);
6312 Set_Default_SSO (Implicit_Base);
6313
6314 -- The constrained array type is a subtype of the unconstrained one
6315
6316 Set_Ekind (T, E_Array_Subtype);
6317 Init_Size_Align (T);
6318 Set_Etype (T, Implicit_Base);
6319 Set_Scope (T, Current_Scope);
6320 Set_Is_Constrained (T);
6321 Set_First_Index (T,
6322 First (Discrete_Subtype_Definitions (Def)));
6323 Set_Has_Delayed_Freeze (T);
6324
6325 -- Complete setup of implicit base type
6326
6327 Set_Component_Size (Implicit_Base, Uint_0);
6328 Set_Component_Type (Implicit_Base, Element_Type);
6329 Set_Finalize_Storage_Only
6330 (Implicit_Base,
6331 Finalize_Storage_Only (Element_Type));
6332 Set_First_Index (Implicit_Base, First_Index (T));
6333 Set_Has_Controlled_Component
6334 (Implicit_Base,
6335 Has_Controlled_Component (Element_Type)
6336 or else Is_Controlled (Element_Type));
6337 Set_Packed_Array_Impl_Type
6338 (Implicit_Base, Empty);
6339
6340 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6341
6342 -- Unconstrained array case
6343
6344 else
6345 Set_Ekind (T, E_Array_Type);
6346 Init_Size_Align (T);
6347 Set_Etype (T, T);
6348 Set_Scope (T, Current_Scope);
6349 Set_Component_Size (T, Uint_0);
6350 Set_Is_Constrained (T, False);
6351 Set_First_Index (T, First (Subtype_Marks (Def)));
6352 Set_Has_Delayed_Freeze (T, True);
6353 Propagate_Concurrent_Flags (T, Element_Type);
6354 Set_Has_Controlled_Component (T, Has_Controlled_Component
6355 (Element_Type)
6356 or else
6357 Is_Controlled (Element_Type));
6358 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6359 (Element_Type));
6360 Set_Default_SSO (T);
6361 end if;
6362
6363 -- Common attributes for both cases
6364
6365 Set_Component_Type (Base_Type (T), Element_Type);
6366 Set_Packed_Array_Impl_Type (T, Empty);
6367
6368 if Aliased_Present (Component_Definition (Def)) then
6369 Check_SPARK_05_Restriction
6370 ("aliased is not allowed", Component_Definition (Def));
6371 Set_Has_Aliased_Components (Etype (T));
6372
6373 -- AI12-001: All aliased objects are considered to be specified as
6374 -- independently addressable (RM C.6(8.1/4)).
6375
6376 Set_Has_Independent_Components (Etype (T));
6377 end if;
6378
6379 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6380 -- array type to ensure that objects of this type are initialized.
6381
6382 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6383 Set_Can_Never_Be_Null (T);
6384
6385 if Null_Exclusion_Present (Component_Definition (Def))
6386
6387 -- No need to check itypes because in their case this check was
6388 -- done at their point of creation
6389
6390 and then not Is_Itype (Element_Type)
6391 then
6392 Error_Msg_N
6393 ("`NOT NULL` not allowed (null already excluded)",
6394 Subtype_Indication (Component_Definition (Def)));
6395 end if;
6396 end if;
6397
6398 Priv := Private_Component (Element_Type);
6399
6400 if Present (Priv) then
6401
6402 -- Check for circular definitions
6403
6404 if Priv = Any_Type then
6405 Set_Component_Type (Etype (T), Any_Type);
6406
6407 -- There is a gap in the visibility of operations on the composite
6408 -- type only if the component type is defined in a different scope.
6409
6410 elsif Scope (Priv) = Current_Scope then
6411 null;
6412
6413 elsif Is_Limited_Type (Priv) then
6414 Set_Is_Limited_Composite (Etype (T));
6415 Set_Is_Limited_Composite (T);
6416 else
6417 Set_Is_Private_Composite (Etype (T));
6418 Set_Is_Private_Composite (T);
6419 end if;
6420 end if;
6421
6422 -- A syntax error in the declaration itself may lead to an empty index
6423 -- list, in which case do a minimal patch.
6424
6425 if No (First_Index (T)) then
6426 Error_Msg_N ("missing index definition in array type declaration", T);
6427
6428 declare
6429 Indexes : constant List_Id :=
6430 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6431 begin
6432 Set_Discrete_Subtype_Definitions (Def, Indexes);
6433 Set_First_Index (T, First (Indexes));
6434 return;
6435 end;
6436 end if;
6437
6438 -- Create a concatenation operator for the new type. Internal array
6439 -- types created for packed entities do not need such, they are
6440 -- compatible with the user-defined type.
6441
6442 if Number_Dimensions (T) = 1
6443 and then not Is_Packed_Array_Impl_Type (T)
6444 then
6445 New_Concatenation_Op (T);
6446 end if;
6447
6448 -- In the case of an unconstrained array the parser has already verified
6449 -- that all the indexes are unconstrained but we still need to make sure
6450 -- that the element type is constrained.
6451
6452 if not Is_Definite_Subtype (Element_Type) then
6453 Error_Msg_N
6454 ("unconstrained element type in array declaration",
6455 Subtype_Indication (Component_Def));
6456
6457 elsif Is_Abstract_Type (Element_Type) then
6458 Error_Msg_N
6459 ("the type of a component cannot be abstract",
6460 Subtype_Indication (Component_Def));
6461 end if;
6462
6463 -- There may be an invariant declared for the component type, but
6464 -- the construction of the component invariant checking procedure
6465 -- takes place during expansion.
6466 end Array_Type_Declaration;
6467
6468 ------------------------------------------------------
6469 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6470 ------------------------------------------------------
6471
6472 function Replace_Anonymous_Access_To_Protected_Subprogram
6473 (N : Node_Id) return Entity_Id
6474 is
6475 Loc : constant Source_Ptr := Sloc (N);
6476
6477 Curr_Scope : constant Scope_Stack_Entry :=
6478 Scope_Stack.Table (Scope_Stack.Last);
6479
6480 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6481
6482 Acc : Node_Id;
6483 -- Access definition in declaration
6484
6485 Comp : Node_Id;
6486 -- Object definition or formal definition with an access definition
6487
6488 Decl : Node_Id;
6489 -- Declaration of anonymous access to subprogram type
6490
6491 Spec : Node_Id;
6492 -- Original specification in access to subprogram
6493
6494 P : Node_Id;
6495
6496 begin
6497 Set_Is_Internal (Anon);
6498
6499 case Nkind (N) is
6500 when N_Constrained_Array_Definition
6501 | N_Component_Declaration
6502 | N_Unconstrained_Array_Definition
6503 =>
6504 Comp := Component_Definition (N);
6505 Acc := Access_Definition (Comp);
6506
6507 when N_Discriminant_Specification =>
6508 Comp := Discriminant_Type (N);
6509 Acc := Comp;
6510
6511 when N_Parameter_Specification =>
6512 Comp := Parameter_Type (N);
6513 Acc := Comp;
6514
6515 when N_Access_Function_Definition =>
6516 Comp := Result_Definition (N);
6517 Acc := Comp;
6518
6519 when N_Object_Declaration =>
6520 Comp := Object_Definition (N);
6521 Acc := Comp;
6522
6523 when N_Function_Specification =>
6524 Comp := Result_Definition (N);
6525 Acc := Comp;
6526
6527 when others =>
6528 raise Program_Error;
6529 end case;
6530
6531 Spec := Access_To_Subprogram_Definition (Acc);
6532
6533 Decl :=
6534 Make_Full_Type_Declaration (Loc,
6535 Defining_Identifier => Anon,
6536 Type_Definition => Copy_Separate_Tree (Spec));
6537
6538 Mark_Rewrite_Insertion (Decl);
6539
6540 -- In ASIS mode, analyze the profile on the original node, because
6541 -- the separate copy does not provide enough links to recover the
6542 -- original tree. Analysis is limited to type annotations, within
6543 -- a temporary scope that serves as an anonymous subprogram to collect
6544 -- otherwise useless temporaries and itypes.
6545
6546 if ASIS_Mode then
6547 declare
6548 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6549
6550 begin
6551 if Nkind (Spec) = N_Access_Function_Definition then
6552 Set_Ekind (Typ, E_Function);
6553 else
6554 Set_Ekind (Typ, E_Procedure);
6555 end if;
6556
6557 Set_Parent (Typ, N);
6558 Set_Scope (Typ, Current_Scope);
6559 Push_Scope (Typ);
6560
6561 -- Nothing to do if procedure is parameterless
6562
6563 if Present (Parameter_Specifications (Spec)) then
6564 Process_Formals (Parameter_Specifications (Spec), Spec);
6565 end if;
6566
6567 if Nkind (Spec) = N_Access_Function_Definition then
6568 declare
6569 Def : constant Node_Id := Result_Definition (Spec);
6570
6571 begin
6572 -- The result might itself be an anonymous access type, so
6573 -- have to recurse.
6574
6575 if Nkind (Def) = N_Access_Definition then
6576 if Present (Access_To_Subprogram_Definition (Def)) then
6577 Set_Etype
6578 (Def,
6579 Replace_Anonymous_Access_To_Protected_Subprogram
6580 (Spec));
6581 else
6582 Find_Type (Subtype_Mark (Def));
6583 end if;
6584
6585 else
6586 Find_Type (Def);
6587 end if;
6588 end;
6589 end if;
6590
6591 End_Scope;
6592 end;
6593 end if;
6594
6595 -- Insert the new declaration in the nearest enclosing scope. If the
6596 -- parent is a body and N is its return type, the declaration belongs
6597 -- in the enclosing scope. Likewise if N is the type of a parameter.
6598
6599 P := Parent (N);
6600
6601 if Nkind (N) = N_Function_Specification
6602 and then Nkind (P) = N_Subprogram_Body
6603 then
6604 P := Parent (P);
6605 elsif Nkind (N) = N_Parameter_Specification
6606 and then Nkind (P) in N_Subprogram_Specification
6607 and then Nkind (Parent (P)) = N_Subprogram_Body
6608 then
6609 P := Parent (Parent (P));
6610 end if;
6611
6612 while Present (P) and then not Has_Declarations (P) loop
6613 P := Parent (P);
6614 end loop;
6615
6616 pragma Assert (Present (P));
6617
6618 if Nkind (P) = N_Package_Specification then
6619 Prepend (Decl, Visible_Declarations (P));
6620 else
6621 Prepend (Decl, Declarations (P));
6622 end if;
6623
6624 -- Replace the anonymous type with an occurrence of the new declaration.
6625 -- In all cases the rewritten node does not have the null-exclusion
6626 -- attribute because (if present) it was already inherited by the
6627 -- anonymous entity (Anon). Thus, in case of components we do not
6628 -- inherit this attribute.
6629
6630 if Nkind (N) = N_Parameter_Specification then
6631 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6632 Set_Etype (Defining_Identifier (N), Anon);
6633 Set_Null_Exclusion_Present (N, False);
6634
6635 elsif Nkind (N) = N_Object_Declaration then
6636 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6637 Set_Etype (Defining_Identifier (N), Anon);
6638
6639 elsif Nkind (N) = N_Access_Function_Definition then
6640 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6641
6642 elsif Nkind (N) = N_Function_Specification then
6643 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6644 Set_Etype (Defining_Unit_Name (N), Anon);
6645
6646 else
6647 Rewrite (Comp,
6648 Make_Component_Definition (Loc,
6649 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6650 end if;
6651
6652 Mark_Rewrite_Insertion (Comp);
6653
6654 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6655 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6656 and then not Is_Type (Current_Scope))
6657 then
6658
6659 -- Declaration can be analyzed in the current scope.
6660
6661 Analyze (Decl);
6662
6663 else
6664 -- Temporarily remove the current scope (record or subprogram) from
6665 -- the stack to add the new declarations to the enclosing scope.
6666 -- The anonymous entity is an Itype with the proper attributes.
6667
6668 Scope_Stack.Decrement_Last;
6669 Analyze (Decl);
6670 Set_Is_Itype (Anon);
6671 Set_Associated_Node_For_Itype (Anon, N);
6672 Scope_Stack.Append (Curr_Scope);
6673 end if;
6674
6675 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6676 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6677 return Anon;
6678 end Replace_Anonymous_Access_To_Protected_Subprogram;
6679
6680 -------------------------------
6681 -- Build_Derived_Access_Type --
6682 -------------------------------
6683
6684 procedure Build_Derived_Access_Type
6685 (N : Node_Id;
6686 Parent_Type : Entity_Id;
6687 Derived_Type : Entity_Id)
6688 is
6689 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6690
6691 Desig_Type : Entity_Id;
6692 Discr : Entity_Id;
6693 Discr_Con_Elist : Elist_Id;
6694 Discr_Con_El : Elmt_Id;
6695 Subt : Entity_Id;
6696
6697 begin
6698 -- Set the designated type so it is available in case this is an access
6699 -- to a self-referential type, e.g. a standard list type with a next
6700 -- pointer. Will be reset after subtype is built.
6701
6702 Set_Directly_Designated_Type
6703 (Derived_Type, Designated_Type (Parent_Type));
6704
6705 Subt := Process_Subtype (S, N);
6706
6707 if Nkind (S) /= N_Subtype_Indication
6708 and then Subt /= Base_Type (Subt)
6709 then
6710 Set_Ekind (Derived_Type, E_Access_Subtype);
6711 end if;
6712
6713 if Ekind (Derived_Type) = E_Access_Subtype then
6714 declare
6715 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6716 Ibase : constant Entity_Id :=
6717 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6718 Svg_Chars : constant Name_Id := Chars (Ibase);
6719 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6720 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6721
6722 begin
6723 Copy_Node (Pbase, Ibase);
6724
6725 -- Restore Itype status after Copy_Node
6726
6727 Set_Is_Itype (Ibase);
6728 Set_Associated_Node_For_Itype (Ibase, N);
6729
6730 Set_Chars (Ibase, Svg_Chars);
6731 Set_Prev_Entity (Ibase, Svg_Prev_E);
6732 Set_Next_Entity (Ibase, Svg_Next_E);
6733 Set_Sloc (Ibase, Sloc (Derived_Type));
6734 Set_Scope (Ibase, Scope (Derived_Type));
6735 Set_Freeze_Node (Ibase, Empty);
6736 Set_Is_Frozen (Ibase, False);
6737 Set_Comes_From_Source (Ibase, False);
6738 Set_Is_First_Subtype (Ibase, False);
6739
6740 Set_Etype (Ibase, Pbase);
6741 Set_Etype (Derived_Type, Ibase);
6742 end;
6743 end if;
6744
6745 Set_Directly_Designated_Type
6746 (Derived_Type, Designated_Type (Subt));
6747
6748 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6749 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6750 Set_Size_Info (Derived_Type, Parent_Type);
6751 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6752 Set_Depends_On_Private (Derived_Type,
6753 Has_Private_Component (Derived_Type));
6754 Conditional_Delay (Derived_Type, Subt);
6755
6756 if Is_Access_Subprogram_Type (Derived_Type) then
6757 Set_Can_Use_Internal_Rep
6758 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6759 end if;
6760
6761 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6762 -- that it is not redundant.
6763
6764 if Null_Exclusion_Present (Type_Definition (N)) then
6765 Set_Can_Never_Be_Null (Derived_Type);
6766
6767 elsif Can_Never_Be_Null (Parent_Type) then
6768 Set_Can_Never_Be_Null (Derived_Type);
6769 end if;
6770
6771 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6772 -- the root type for this information.
6773
6774 -- Apply range checks to discriminants for derived record case
6775 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6776
6777 Desig_Type := Designated_Type (Derived_Type);
6778
6779 if Is_Composite_Type (Desig_Type)
6780 and then (not Is_Array_Type (Desig_Type))
6781 and then Has_Discriminants (Desig_Type)
6782 and then Base_Type (Desig_Type) /= Desig_Type
6783 then
6784 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6785 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6786
6787 Discr := First_Discriminant (Base_Type (Desig_Type));
6788 while Present (Discr_Con_El) loop
6789 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6790 Next_Elmt (Discr_Con_El);
6791 Next_Discriminant (Discr);
6792 end loop;
6793 end if;
6794 end Build_Derived_Access_Type;
6795
6796 ------------------------------
6797 -- Build_Derived_Array_Type --
6798 ------------------------------
6799
6800 procedure Build_Derived_Array_Type
6801 (N : Node_Id;
6802 Parent_Type : Entity_Id;
6803 Derived_Type : Entity_Id)
6804 is
6805 Loc : constant Source_Ptr := Sloc (N);
6806 Tdef : constant Node_Id := Type_Definition (N);
6807 Indic : constant Node_Id := Subtype_Indication (Tdef);
6808 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6809 Implicit_Base : Entity_Id := Empty;
6810 New_Indic : Node_Id;
6811
6812 procedure Make_Implicit_Base;
6813 -- If the parent subtype is constrained, the derived type is a subtype
6814 -- of an implicit base type derived from the parent base.
6815
6816 ------------------------
6817 -- Make_Implicit_Base --
6818 ------------------------
6819
6820 procedure Make_Implicit_Base is
6821 begin
6822 Implicit_Base :=
6823 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6824
6825 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6826 Set_Etype (Implicit_Base, Parent_Base);
6827
6828 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6829 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6830
6831 Set_Has_Delayed_Freeze (Implicit_Base, True);
6832 end Make_Implicit_Base;
6833
6834 -- Start of processing for Build_Derived_Array_Type
6835
6836 begin
6837 if not Is_Constrained (Parent_Type) then
6838 if Nkind (Indic) /= N_Subtype_Indication then
6839 Set_Ekind (Derived_Type, E_Array_Type);
6840
6841 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6842 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6843
6844 Set_Has_Delayed_Freeze (Derived_Type, True);
6845
6846 else
6847 Make_Implicit_Base;
6848 Set_Etype (Derived_Type, Implicit_Base);
6849
6850 New_Indic :=
6851 Make_Subtype_Declaration (Loc,
6852 Defining_Identifier => Derived_Type,
6853 Subtype_Indication =>
6854 Make_Subtype_Indication (Loc,
6855 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6856 Constraint => Constraint (Indic)));
6857
6858 Rewrite (N, New_Indic);
6859 Analyze (N);
6860 end if;
6861
6862 else
6863 if Nkind (Indic) /= N_Subtype_Indication then
6864 Make_Implicit_Base;
6865
6866 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6867 Set_Etype (Derived_Type, Implicit_Base);
6868 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6869
6870 else
6871 Error_Msg_N ("illegal constraint on constrained type", Indic);
6872 end if;
6873 end if;
6874
6875 -- If parent type is not a derived type itself, and is declared in
6876 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6877 -- the new type's concatenation operator since Derive_Subprograms
6878 -- will not inherit the parent's operator. If the parent type is
6879 -- unconstrained, the operator is of the unconstrained base type.
6880
6881 if Number_Dimensions (Parent_Type) = 1
6882 and then not Is_Limited_Type (Parent_Type)
6883 and then not Is_Derived_Type (Parent_Type)
6884 and then not Is_Package_Or_Generic_Package
6885 (Scope (Base_Type (Parent_Type)))
6886 then
6887 if not Is_Constrained (Parent_Type)
6888 and then Is_Constrained (Derived_Type)
6889 then
6890 New_Concatenation_Op (Implicit_Base);
6891 else
6892 New_Concatenation_Op (Derived_Type);
6893 end if;
6894 end if;
6895 end Build_Derived_Array_Type;
6896
6897 -----------------------------------
6898 -- Build_Derived_Concurrent_Type --
6899 -----------------------------------
6900
6901 procedure Build_Derived_Concurrent_Type
6902 (N : Node_Id;
6903 Parent_Type : Entity_Id;
6904 Derived_Type : Entity_Id)
6905 is
6906 Loc : constant Source_Ptr := Sloc (N);
6907 Def : constant Node_Id := Type_Definition (N);
6908 Indic : constant Node_Id := Subtype_Indication (Def);
6909
6910 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6911 Corr_Decl : Node_Id;
6912 Corr_Decl_Needed : Boolean;
6913 -- If the derived type has fewer discriminants than its parent, the
6914 -- corresponding record is also a derived type, in order to account for
6915 -- the bound discriminants. We create a full type declaration for it in
6916 -- this case.
6917
6918 Constraint_Present : constant Boolean :=
6919 Nkind (Indic) = N_Subtype_Indication;
6920
6921 D_Constraint : Node_Id;
6922 New_Constraint : Elist_Id := No_Elist;
6923 Old_Disc : Entity_Id;
6924 New_Disc : Entity_Id;
6925 New_N : Node_Id;
6926
6927 begin
6928 Set_Stored_Constraint (Derived_Type, No_Elist);
6929 Corr_Decl_Needed := False;
6930 Old_Disc := Empty;
6931
6932 if Present (Discriminant_Specifications (N))
6933 and then Constraint_Present
6934 then
6935 Old_Disc := First_Discriminant (Parent_Type);
6936 New_Disc := First (Discriminant_Specifications (N));
6937 while Present (New_Disc) and then Present (Old_Disc) loop
6938 Next_Discriminant (Old_Disc);
6939 Next (New_Disc);
6940 end loop;
6941 end if;
6942
6943 if Present (Old_Disc) and then Expander_Active then
6944
6945 -- The new type has fewer discriminants, so we need to create a new
6946 -- corresponding record, which is derived from the corresponding
6947 -- record of the parent, and has a stored constraint that captures
6948 -- the values of the discriminant constraints. The corresponding
6949 -- record is needed only if expander is active and code generation is
6950 -- enabled.
6951
6952 -- The type declaration for the derived corresponding record has the
6953 -- same discriminant part and constraints as the current declaration.
6954 -- Copy the unanalyzed tree to build declaration.
6955
6956 Corr_Decl_Needed := True;
6957 New_N := Copy_Separate_Tree (N);
6958
6959 Corr_Decl :=
6960 Make_Full_Type_Declaration (Loc,
6961 Defining_Identifier => Corr_Record,
6962 Discriminant_Specifications =>
6963 Discriminant_Specifications (New_N),
6964 Type_Definition =>
6965 Make_Derived_Type_Definition (Loc,
6966 Subtype_Indication =>
6967 Make_Subtype_Indication (Loc,
6968 Subtype_Mark =>
6969 New_Occurrence_Of
6970 (Corresponding_Record_Type (Parent_Type), Loc),
6971 Constraint =>
6972 Constraint
6973 (Subtype_Indication (Type_Definition (New_N))))));
6974 end if;
6975
6976 -- Copy Storage_Size and Relative_Deadline variables if task case
6977
6978 if Is_Task_Type (Parent_Type) then
6979 Set_Storage_Size_Variable (Derived_Type,
6980 Storage_Size_Variable (Parent_Type));
6981 Set_Relative_Deadline_Variable (Derived_Type,
6982 Relative_Deadline_Variable (Parent_Type));
6983 end if;
6984
6985 if Present (Discriminant_Specifications (N)) then
6986 Push_Scope (Derived_Type);
6987 Check_Or_Process_Discriminants (N, Derived_Type);
6988
6989 if Constraint_Present then
6990 New_Constraint :=
6991 Expand_To_Stored_Constraint
6992 (Parent_Type,
6993 Build_Discriminant_Constraints
6994 (Parent_Type, Indic, True));
6995 end if;
6996
6997 End_Scope;
6998
6999 elsif Constraint_Present then
7000
7001 -- Build an unconstrained derived type and rewrite the derived type
7002 -- as a subtype of this new base type.
7003
7004 declare
7005 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7006 New_Base : Entity_Id;
7007 New_Decl : Node_Id;
7008 New_Indic : Node_Id;
7009
7010 begin
7011 New_Base :=
7012 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7013
7014 New_Decl :=
7015 Make_Full_Type_Declaration (Loc,
7016 Defining_Identifier => New_Base,
7017 Type_Definition =>
7018 Make_Derived_Type_Definition (Loc,
7019 Abstract_Present => Abstract_Present (Def),
7020 Limited_Present => Limited_Present (Def),
7021 Subtype_Indication =>
7022 New_Occurrence_Of (Parent_Base, Loc)));
7023
7024 Mark_Rewrite_Insertion (New_Decl);
7025 Insert_Before (N, New_Decl);
7026 Analyze (New_Decl);
7027
7028 New_Indic :=
7029 Make_Subtype_Indication (Loc,
7030 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7031 Constraint => Relocate_Node (Constraint (Indic)));
7032
7033 Rewrite (N,
7034 Make_Subtype_Declaration (Loc,
7035 Defining_Identifier => Derived_Type,
7036 Subtype_Indication => New_Indic));
7037
7038 Analyze (N);
7039 return;
7040 end;
7041 end if;
7042
7043 -- By default, operations and private data are inherited from parent.
7044 -- However, in the presence of bound discriminants, a new corresponding
7045 -- record will be created, see below.
7046
7047 Set_Has_Discriminants
7048 (Derived_Type, Has_Discriminants (Parent_Type));
7049 Set_Corresponding_Record_Type
7050 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7051
7052 -- Is_Constrained is set according the parent subtype, but is set to
7053 -- False if the derived type is declared with new discriminants.
7054
7055 Set_Is_Constrained
7056 (Derived_Type,
7057 (Is_Constrained (Parent_Type) or else Constraint_Present)
7058 and then not Present (Discriminant_Specifications (N)));
7059
7060 if Constraint_Present then
7061 if not Has_Discriminants (Parent_Type) then
7062 Error_Msg_N ("untagged parent must have discriminants", N);
7063
7064 elsif Present (Discriminant_Specifications (N)) then
7065
7066 -- Verify that new discriminants are used to constrain old ones
7067
7068 D_Constraint := First (Constraints (Constraint (Indic)));
7069
7070 Old_Disc := First_Discriminant (Parent_Type);
7071
7072 while Present (D_Constraint) loop
7073 if Nkind (D_Constraint) /= N_Discriminant_Association then
7074
7075 -- Positional constraint. If it is a reference to a new
7076 -- discriminant, it constrains the corresponding old one.
7077
7078 if Nkind (D_Constraint) = N_Identifier then
7079 New_Disc := First_Discriminant (Derived_Type);
7080 while Present (New_Disc) loop
7081 exit when Chars (New_Disc) = Chars (D_Constraint);
7082 Next_Discriminant (New_Disc);
7083 end loop;
7084
7085 if Present (New_Disc) then
7086 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7087 end if;
7088 end if;
7089
7090 Next_Discriminant (Old_Disc);
7091
7092 -- if this is a named constraint, search by name for the old
7093 -- discriminants constrained by the new one.
7094
7095 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7096
7097 -- Find new discriminant with that name
7098
7099 New_Disc := First_Discriminant (Derived_Type);
7100 while Present (New_Disc) loop
7101 exit when
7102 Chars (New_Disc) = Chars (Expression (D_Constraint));
7103 Next_Discriminant (New_Disc);
7104 end loop;
7105
7106 if Present (New_Disc) then
7107
7108 -- Verify that new discriminant renames some discriminant
7109 -- of the parent type, and associate the new discriminant
7110 -- with one or more old ones that it renames.
7111
7112 declare
7113 Selector : Node_Id;
7114
7115 begin
7116 Selector := First (Selector_Names (D_Constraint));
7117 while Present (Selector) loop
7118 Old_Disc := First_Discriminant (Parent_Type);
7119 while Present (Old_Disc) loop
7120 exit when Chars (Old_Disc) = Chars (Selector);
7121 Next_Discriminant (Old_Disc);
7122 end loop;
7123
7124 if Present (Old_Disc) then
7125 Set_Corresponding_Discriminant
7126 (New_Disc, Old_Disc);
7127 end if;
7128
7129 Next (Selector);
7130 end loop;
7131 end;
7132 end if;
7133 end if;
7134
7135 Next (D_Constraint);
7136 end loop;
7137
7138 New_Disc := First_Discriminant (Derived_Type);
7139 while Present (New_Disc) loop
7140 if No (Corresponding_Discriminant (New_Disc)) then
7141 Error_Msg_NE
7142 ("new discriminant& must constrain old one", N, New_Disc);
7143
7144 elsif not
7145 Subtypes_Statically_Compatible
7146 (Etype (New_Disc),
7147 Etype (Corresponding_Discriminant (New_Disc)))
7148 then
7149 Error_Msg_NE
7150 ("& not statically compatible with parent discriminant",
7151 N, New_Disc);
7152 end if;
7153
7154 Next_Discriminant (New_Disc);
7155 end loop;
7156 end if;
7157
7158 elsif Present (Discriminant_Specifications (N)) then
7159 Error_Msg_N
7160 ("missing discriminant constraint in untagged derivation", N);
7161 end if;
7162
7163 -- The entity chain of the derived type includes the new discriminants
7164 -- but shares operations with the parent.
7165
7166 if Present (Discriminant_Specifications (N)) then
7167 Old_Disc := First_Discriminant (Parent_Type);
7168 while Present (Old_Disc) loop
7169 if No (Next_Entity (Old_Disc))
7170 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7171 then
7172 Link_Entities
7173 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7174 exit;
7175 end if;
7176
7177 Next_Discriminant (Old_Disc);
7178 end loop;
7179
7180 else
7181 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7182 if Has_Discriminants (Parent_Type) then
7183 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7184 Set_Discriminant_Constraint (
7185 Derived_Type, Discriminant_Constraint (Parent_Type));
7186 end if;
7187 end if;
7188
7189 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7190
7191 Set_Has_Completion (Derived_Type);
7192
7193 if Corr_Decl_Needed then
7194 Set_Stored_Constraint (Derived_Type, New_Constraint);
7195 Insert_After (N, Corr_Decl);
7196 Analyze (Corr_Decl);
7197 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7198 end if;
7199 end Build_Derived_Concurrent_Type;
7200
7201 ------------------------------------
7202 -- Build_Derived_Enumeration_Type --
7203 ------------------------------------
7204
7205 procedure Build_Derived_Enumeration_Type
7206 (N : Node_Id;
7207 Parent_Type : Entity_Id;
7208 Derived_Type : Entity_Id)
7209 is
7210 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7211 -- When the type declaration includes a constraint, we generate
7212 -- a subtype declaration of an anonymous base type, with the constraint
7213 -- given in the original type declaration. Conceptually, the bounds
7214 -- are converted to the new base type, and this conversion freezes
7215 -- (prematurely) that base type, when the bounds are simply literals.
7216 -- As a result, a representation clause for the derived type is then
7217 -- rejected or ignored. This procedure recognizes the simple case of
7218 -- literal bounds, which allows us to indicate that the conversions
7219 -- are not freeze points, and the subsequent representation clause
7220 -- can be accepted.
7221 -- A similar approach might be used to resolve the long-standing
7222 -- problem of premature freezing of derived numeric types ???
7223
7224 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7225 begin
7226 return Nkind (B) = N_Type_Conversion
7227 and then Is_Entity_Name (Expression (B))
7228 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7229 end Bound_Belongs_To_Type;
7230
7231 Loc : constant Source_Ptr := Sloc (N);
7232 Def : constant Node_Id := Type_Definition (N);
7233 Indic : constant Node_Id := Subtype_Indication (Def);
7234 Implicit_Base : Entity_Id;
7235 Literal : Entity_Id;
7236 New_Lit : Entity_Id;
7237 Literals_List : List_Id;
7238 Type_Decl : Node_Id;
7239 Hi, Lo : Node_Id;
7240 Rang_Expr : Node_Id;
7241
7242 begin
7243 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7244 -- not have explicit literals lists we need to process types derived
7245 -- from them specially. This is handled by Derived_Standard_Character.
7246 -- If the parent type is a generic type, there are no literals either,
7247 -- and we construct the same skeletal representation as for the generic
7248 -- parent type.
7249
7250 if Is_Standard_Character_Type (Parent_Type) then
7251 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7252
7253 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7254 declare
7255 Lo : Node_Id;
7256 Hi : Node_Id;
7257
7258 begin
7259 if Nkind (Indic) /= N_Subtype_Indication then
7260 Lo :=
7261 Make_Attribute_Reference (Loc,
7262 Attribute_Name => Name_First,
7263 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7264 Set_Etype (Lo, Derived_Type);
7265
7266 Hi :=
7267 Make_Attribute_Reference (Loc,
7268 Attribute_Name => Name_Last,
7269 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7270 Set_Etype (Hi, Derived_Type);
7271
7272 Set_Scalar_Range (Derived_Type,
7273 Make_Range (Loc,
7274 Low_Bound => Lo,
7275 High_Bound => Hi));
7276 else
7277
7278 -- Analyze subtype indication and verify compatibility
7279 -- with parent type.
7280
7281 if Base_Type (Process_Subtype (Indic, N)) /=
7282 Base_Type (Parent_Type)
7283 then
7284 Error_Msg_N
7285 ("illegal constraint for formal discrete type", N);
7286 end if;
7287 end if;
7288 end;
7289
7290 else
7291 -- If a constraint is present, analyze the bounds to catch
7292 -- premature usage of the derived literals.
7293
7294 if Nkind (Indic) = N_Subtype_Indication
7295 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7296 then
7297 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7298 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7299 end if;
7300
7301 -- Introduce an implicit base type for the derived type even if there
7302 -- is no constraint attached to it, since this seems closer to the
7303 -- Ada semantics. Build a full type declaration tree for the derived
7304 -- type using the implicit base type as the defining identifier. The
7305 -- build a subtype declaration tree which applies the constraint (if
7306 -- any) have it replace the derived type declaration.
7307
7308 Literal := First_Literal (Parent_Type);
7309 Literals_List := New_List;
7310 while Present (Literal)
7311 and then Ekind (Literal) = E_Enumeration_Literal
7312 loop
7313 -- Literals of the derived type have the same representation as
7314 -- those of the parent type, but this representation can be
7315 -- overridden by an explicit representation clause. Indicate
7316 -- that there is no explicit representation given yet. These
7317 -- derived literals are implicit operations of the new type,
7318 -- and can be overridden by explicit ones.
7319
7320 if Nkind (Literal) = N_Defining_Character_Literal then
7321 New_Lit :=
7322 Make_Defining_Character_Literal (Loc, Chars (Literal));
7323 else
7324 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7325 end if;
7326
7327 Set_Ekind (New_Lit, E_Enumeration_Literal);
7328 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7329 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7330 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7331 Set_Alias (New_Lit, Literal);
7332 Set_Is_Known_Valid (New_Lit, True);
7333
7334 Append (New_Lit, Literals_List);
7335 Next_Literal (Literal);
7336 end loop;
7337
7338 Implicit_Base :=
7339 Make_Defining_Identifier (Sloc (Derived_Type),
7340 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7341
7342 -- Indicate the proper nature of the derived type. This must be done
7343 -- before analysis of the literals, to recognize cases when a literal
7344 -- may be hidden by a previous explicit function definition (cf.
7345 -- c83031a).
7346
7347 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7348 Set_Etype (Derived_Type, Implicit_Base);
7349
7350 Type_Decl :=
7351 Make_Full_Type_Declaration (Loc,
7352 Defining_Identifier => Implicit_Base,
7353 Discriminant_Specifications => No_List,
7354 Type_Definition =>
7355 Make_Enumeration_Type_Definition (Loc, Literals_List));
7356
7357 Mark_Rewrite_Insertion (Type_Decl);
7358 Insert_Before (N, Type_Decl);
7359 Analyze (Type_Decl);
7360
7361 -- The anonymous base now has a full declaration, but this base
7362 -- is not a first subtype.
7363
7364 Set_Is_First_Subtype (Implicit_Base, False);
7365
7366 -- After the implicit base is analyzed its Etype needs to be changed
7367 -- to reflect the fact that it is derived from the parent type which
7368 -- was ignored during analysis. We also set the size at this point.
7369
7370 Set_Etype (Implicit_Base, Parent_Type);
7371
7372 Set_Size_Info (Implicit_Base, Parent_Type);
7373 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7374 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7375
7376 -- Copy other flags from parent type
7377
7378 Set_Has_Non_Standard_Rep
7379 (Implicit_Base, Has_Non_Standard_Rep
7380 (Parent_Type));
7381 Set_Has_Pragma_Ordered
7382 (Implicit_Base, Has_Pragma_Ordered
7383 (Parent_Type));
7384 Set_Has_Delayed_Freeze (Implicit_Base);
7385
7386 -- Process the subtype indication including a validation check on the
7387 -- constraint, if any. If a constraint is given, its bounds must be
7388 -- implicitly converted to the new type.
7389
7390 if Nkind (Indic) = N_Subtype_Indication then
7391 declare
7392 R : constant Node_Id :=
7393 Range_Expression (Constraint (Indic));
7394
7395 begin
7396 if Nkind (R) = N_Range then
7397 Hi := Build_Scalar_Bound
7398 (High_Bound (R), Parent_Type, Implicit_Base);
7399 Lo := Build_Scalar_Bound
7400 (Low_Bound (R), Parent_Type, Implicit_Base);
7401
7402 else
7403 -- Constraint is a Range attribute. Replace with explicit
7404 -- mention of the bounds of the prefix, which must be a
7405 -- subtype.
7406
7407 Analyze (Prefix (R));
7408 Hi :=
7409 Convert_To (Implicit_Base,
7410 Make_Attribute_Reference (Loc,
7411 Attribute_Name => Name_Last,
7412 Prefix =>
7413 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7414
7415 Lo :=
7416 Convert_To (Implicit_Base,
7417 Make_Attribute_Reference (Loc,
7418 Attribute_Name => Name_First,
7419 Prefix =>
7420 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7421 end if;
7422 end;
7423
7424 else
7425 Hi :=
7426 Build_Scalar_Bound
7427 (Type_High_Bound (Parent_Type),
7428 Parent_Type, Implicit_Base);
7429 Lo :=
7430 Build_Scalar_Bound
7431 (Type_Low_Bound (Parent_Type),
7432 Parent_Type, Implicit_Base);
7433 end if;
7434
7435 Rang_Expr :=
7436 Make_Range (Loc,
7437 Low_Bound => Lo,
7438 High_Bound => Hi);
7439
7440 -- If we constructed a default range for the case where no range
7441 -- was given, then the expressions in the range must not freeze
7442 -- since they do not correspond to expressions in the source.
7443 -- However, if the type inherits predicates the expressions will
7444 -- be elaborated earlier and must freeze.
7445
7446 if (Nkind (Indic) /= N_Subtype_Indication
7447 or else
7448 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7449 and then not Has_Predicates (Derived_Type)
7450 then
7451 Set_Must_Not_Freeze (Lo);
7452 Set_Must_Not_Freeze (Hi);
7453 Set_Must_Not_Freeze (Rang_Expr);
7454 end if;
7455
7456 Rewrite (N,
7457 Make_Subtype_Declaration (Loc,
7458 Defining_Identifier => Derived_Type,
7459 Subtype_Indication =>
7460 Make_Subtype_Indication (Loc,
7461 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7462 Constraint =>
7463 Make_Range_Constraint (Loc,
7464 Range_Expression => Rang_Expr))));
7465
7466 Analyze (N);
7467
7468 -- Propagate the aspects from the original type declaration to the
7469 -- declaration of the implicit base.
7470
7471 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7472
7473 -- Apply a range check. Since this range expression doesn't have an
7474 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7475 -- this right???
7476
7477 if Nkind (Indic) = N_Subtype_Indication then
7478 Apply_Range_Check
7479 (Range_Expression (Constraint (Indic)), Parent_Type,
7480 Source_Typ => Entity (Subtype_Mark (Indic)));
7481 end if;
7482 end if;
7483 end Build_Derived_Enumeration_Type;
7484
7485 --------------------------------
7486 -- Build_Derived_Numeric_Type --
7487 --------------------------------
7488
7489 procedure Build_Derived_Numeric_Type
7490 (N : Node_Id;
7491 Parent_Type : Entity_Id;
7492 Derived_Type : Entity_Id)
7493 is
7494 Loc : constant Source_Ptr := Sloc (N);
7495 Tdef : constant Node_Id := Type_Definition (N);
7496 Indic : constant Node_Id := Subtype_Indication (Tdef);
7497 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7498 No_Constraint : constant Boolean := Nkind (Indic) /=
7499 N_Subtype_Indication;
7500 Implicit_Base : Entity_Id;
7501
7502 Lo : Node_Id;
7503 Hi : Node_Id;
7504
7505 begin
7506 -- Process the subtype indication including a validation check on
7507 -- the constraint if any.
7508
7509 Discard_Node (Process_Subtype (Indic, N));
7510
7511 -- Introduce an implicit base type for the derived type even if there
7512 -- is no constraint attached to it, since this seems closer to the Ada
7513 -- semantics.
7514
7515 Implicit_Base :=
7516 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7517
7518 Set_Etype (Implicit_Base, Parent_Base);
7519 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7520 Set_Size_Info (Implicit_Base, Parent_Base);
7521 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7522 Set_Parent (Implicit_Base, Parent (Derived_Type));
7523 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7524
7525 -- Set RM Size for discrete type or decimal fixed-point type
7526 -- Ordinary fixed-point is excluded, why???
7527
7528 if Is_Discrete_Type (Parent_Base)
7529 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7530 then
7531 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7532 end if;
7533
7534 Set_Has_Delayed_Freeze (Implicit_Base);
7535
7536 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7537 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7538
7539 Set_Scalar_Range (Implicit_Base,
7540 Make_Range (Loc,
7541 Low_Bound => Lo,
7542 High_Bound => Hi));
7543
7544 if Has_Infinities (Parent_Base) then
7545 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7546 end if;
7547
7548 -- The Derived_Type, which is the entity of the declaration, is a
7549 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7550 -- absence of an explicit constraint.
7551
7552 Set_Etype (Derived_Type, Implicit_Base);
7553
7554 -- If we did not have a constraint, then the Ekind is set from the
7555 -- parent type (otherwise Process_Subtype has set the bounds)
7556
7557 if No_Constraint then
7558 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7559 end if;
7560
7561 -- If we did not have a range constraint, then set the range from the
7562 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7563
7564 if No_Constraint or else not Has_Range_Constraint (Indic) then
7565 Set_Scalar_Range (Derived_Type,
7566 Make_Range (Loc,
7567 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7568 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7569 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7570
7571 if Has_Infinities (Parent_Type) then
7572 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7573 end if;
7574
7575 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7576 end if;
7577
7578 Set_Is_Descendant_Of_Address (Derived_Type,
7579 Is_Descendant_Of_Address (Parent_Type));
7580 Set_Is_Descendant_Of_Address (Implicit_Base,
7581 Is_Descendant_Of_Address (Parent_Type));
7582
7583 -- Set remaining type-specific fields, depending on numeric type
7584
7585 if Is_Modular_Integer_Type (Parent_Type) then
7586 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7587
7588 Set_Non_Binary_Modulus
7589 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7590
7591 Set_Is_Known_Valid
7592 (Implicit_Base, Is_Known_Valid (Parent_Base));
7593
7594 elsif Is_Floating_Point_Type (Parent_Type) then
7595
7596 -- Digits of base type is always copied from the digits value of
7597 -- the parent base type, but the digits of the derived type will
7598 -- already have been set if there was a constraint present.
7599
7600 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7601 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7602
7603 if No_Constraint then
7604 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7605 end if;
7606
7607 elsif Is_Fixed_Point_Type (Parent_Type) then
7608
7609 -- Small of base type and derived type are always copied from the
7610 -- parent base type, since smalls never change. The delta of the
7611 -- base type is also copied from the parent base type. However the
7612 -- delta of the derived type will have been set already if a
7613 -- constraint was present.
7614
7615 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7616 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7617 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7618
7619 if No_Constraint then
7620 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7621 end if;
7622
7623 -- The scale and machine radix in the decimal case are always
7624 -- copied from the parent base type.
7625
7626 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7627 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7628 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7629
7630 Set_Machine_Radix_10
7631 (Derived_Type, Machine_Radix_10 (Parent_Base));
7632 Set_Machine_Radix_10
7633 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7634
7635 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7636
7637 if No_Constraint then
7638 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7639
7640 else
7641 -- the analysis of the subtype_indication sets the
7642 -- digits value of the derived type.
7643
7644 null;
7645 end if;
7646 end if;
7647 end if;
7648
7649 if Is_Integer_Type (Parent_Type) then
7650 Set_Has_Shift_Operator
7651 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7652 end if;
7653
7654 -- The type of the bounds is that of the parent type, and they
7655 -- must be converted to the derived type.
7656
7657 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7658
7659 -- The implicit_base should be frozen when the derived type is frozen,
7660 -- but note that it is used in the conversions of the bounds. For fixed
7661 -- types we delay the determination of the bounds until the proper
7662 -- freezing point. For other numeric types this is rejected by GCC, for
7663 -- reasons that are currently unclear (???), so we choose to freeze the
7664 -- implicit base now. In the case of integers and floating point types
7665 -- this is harmless because subsequent representation clauses cannot
7666 -- affect anything, but it is still baffling that we cannot use the
7667 -- same mechanism for all derived numeric types.
7668
7669 -- There is a further complication: actually some representation
7670 -- clauses can affect the implicit base type. For example, attribute
7671 -- definition clauses for stream-oriented attributes need to set the
7672 -- corresponding TSS entries on the base type, and this normally
7673 -- cannot be done after the base type is frozen, so the circuitry in
7674 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7675 -- and not use Set_TSS in this case.
7676
7677 -- There are also consequences for the case of delayed representation
7678 -- aspects for some cases. For example, a Size aspect is delayed and
7679 -- should not be evaluated to the freeze point. This early freezing
7680 -- means that the size attribute evaluation happens too early???
7681
7682 if Is_Fixed_Point_Type (Parent_Type) then
7683 Conditional_Delay (Implicit_Base, Parent_Type);
7684 else
7685 Freeze_Before (N, Implicit_Base);
7686 end if;
7687 end Build_Derived_Numeric_Type;
7688
7689 --------------------------------
7690 -- Build_Derived_Private_Type --
7691 --------------------------------
7692
7693 procedure Build_Derived_Private_Type
7694 (N : Node_Id;
7695 Parent_Type : Entity_Id;
7696 Derived_Type : Entity_Id;
7697 Is_Completion : Boolean;
7698 Derive_Subps : Boolean := True)
7699 is
7700 Loc : constant Source_Ptr := Sloc (N);
7701 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7702 Par_Scope : constant Entity_Id := Scope (Par_Base);
7703 Full_N : constant Node_Id := New_Copy_Tree (N);
7704 Full_Der : Entity_Id := New_Copy (Derived_Type);
7705 Full_P : Entity_Id;
7706
7707 procedure Build_Full_Derivation;
7708 -- Build full derivation, i.e. derive from the full view
7709
7710 procedure Copy_And_Build;
7711 -- Copy derived type declaration, replace parent with its full view,
7712 -- and build derivation
7713
7714 ---------------------------
7715 -- Build_Full_Derivation --
7716 ---------------------------
7717
7718 procedure Build_Full_Derivation is
7719 begin
7720 -- If parent scope is not open, install the declarations
7721
7722 if not In_Open_Scopes (Par_Scope) then
7723 Install_Private_Declarations (Par_Scope);
7724 Install_Visible_Declarations (Par_Scope);
7725 Copy_And_Build;
7726 Uninstall_Declarations (Par_Scope);
7727
7728 -- If parent scope is open and in another unit, and parent has a
7729 -- completion, then the derivation is taking place in the visible
7730 -- part of a child unit. In that case retrieve the full view of
7731 -- the parent momentarily.
7732
7733 elsif not In_Same_Source_Unit (N, Parent_Type) then
7734 Full_P := Full_View (Parent_Type);
7735 Exchange_Declarations (Parent_Type);
7736 Copy_And_Build;
7737 Exchange_Declarations (Full_P);
7738
7739 -- Otherwise it is a local derivation
7740
7741 else
7742 Copy_And_Build;
7743 end if;
7744 end Build_Full_Derivation;
7745
7746 --------------------
7747 -- Copy_And_Build --
7748 --------------------
7749
7750 procedure Copy_And_Build is
7751 Full_Parent : Entity_Id := Parent_Type;
7752
7753 begin
7754 -- If the parent is itself derived from another private type,
7755 -- installing the private declarations has not affected its
7756 -- privacy status, so use its own full view explicitly.
7757
7758 if Is_Private_Type (Full_Parent)
7759 and then Present (Full_View (Full_Parent))
7760 then
7761 Full_Parent := Full_View (Full_Parent);
7762 end if;
7763
7764 -- And its underlying full view if necessary
7765
7766 if Is_Private_Type (Full_Parent)
7767 and then Present (Underlying_Full_View (Full_Parent))
7768 then
7769 Full_Parent := Underlying_Full_View (Full_Parent);
7770 end if;
7771
7772 -- For record, concurrent, access and most enumeration types, the
7773 -- derivation from full view requires a fully-fledged declaration.
7774 -- In the other cases, just use an itype.
7775
7776 if Is_Record_Type (Full_Parent)
7777 or else Is_Concurrent_Type (Full_Parent)
7778 or else Is_Access_Type (Full_Parent)
7779 or else
7780 (Is_Enumeration_Type (Full_Parent)
7781 and then not Is_Standard_Character_Type (Full_Parent)
7782 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7783 then
7784 -- Copy and adjust declaration to provide a completion for what
7785 -- is originally a private declaration. Indicate that full view
7786 -- is internally generated.
7787
7788 Set_Comes_From_Source (Full_N, False);
7789 Set_Comes_From_Source (Full_Der, False);
7790 Set_Parent (Full_Der, Full_N);
7791 Set_Defining_Identifier (Full_N, Full_Der);
7792
7793 -- If there are no constraints, adjust the subtype mark
7794
7795 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7796 N_Subtype_Indication
7797 then
7798 Set_Subtype_Indication
7799 (Type_Definition (Full_N),
7800 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7801 end if;
7802
7803 Insert_After (N, Full_N);
7804
7805 -- Build full view of derived type from full view of parent which
7806 -- is now installed. Subprograms have been derived on the partial
7807 -- view, the completion does not derive them anew.
7808
7809 if Is_Record_Type (Full_Parent) then
7810
7811 -- If parent type is tagged, the completion inherits the proper
7812 -- primitive operations.
7813
7814 if Is_Tagged_Type (Parent_Type) then
7815 Build_Derived_Record_Type
7816 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7817 else
7818 Build_Derived_Record_Type
7819 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7820 end if;
7821
7822 else
7823 Build_Derived_Type
7824 (Full_N, Full_Parent, Full_Der,
7825 Is_Completion => False, Derive_Subps => False);
7826 end if;
7827
7828 -- The full declaration has been introduced into the tree and
7829 -- processed in the step above. It should not be analyzed again
7830 -- (when encountered later in the current list of declarations)
7831 -- to prevent spurious name conflicts. The full entity remains
7832 -- invisible.
7833
7834 Set_Analyzed (Full_N);
7835
7836 else
7837 Full_Der :=
7838 Make_Defining_Identifier (Sloc (Derived_Type),
7839 Chars => Chars (Derived_Type));
7840 Set_Is_Itype (Full_Der);
7841 Set_Associated_Node_For_Itype (Full_Der, N);
7842 Set_Parent (Full_Der, N);
7843 Build_Derived_Type
7844 (N, Full_Parent, Full_Der,
7845 Is_Completion => False, Derive_Subps => False);
7846 end if;
7847
7848 Set_Has_Private_Declaration (Full_Der);
7849 Set_Has_Private_Declaration (Derived_Type);
7850
7851 Set_Scope (Full_Der, Scope (Derived_Type));
7852 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7853 Set_Has_Size_Clause (Full_Der, False);
7854 Set_Has_Alignment_Clause (Full_Der, False);
7855 Set_Has_Delayed_Freeze (Full_Der);
7856 Set_Is_Frozen (Full_Der, False);
7857 Set_Freeze_Node (Full_Der, Empty);
7858 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7859 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7860
7861 -- The convention on the base type may be set in the private part
7862 -- and not propagated to the subtype until later, so we obtain the
7863 -- convention from the base type of the parent.
7864
7865 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7866 end Copy_And_Build;
7867
7868 -- Start of processing for Build_Derived_Private_Type
7869
7870 begin
7871 if Is_Tagged_Type (Parent_Type) then
7872 Full_P := Full_View (Parent_Type);
7873
7874 -- A type extension of a type with unknown discriminants is an
7875 -- indefinite type that the back-end cannot handle directly.
7876 -- We treat it as a private type, and build a completion that is
7877 -- derived from the full view of the parent, and hopefully has
7878 -- known discriminants.
7879
7880 -- If the full view of the parent type has an underlying record view,
7881 -- use it to generate the underlying record view of this derived type
7882 -- (required for chains of derivations with unknown discriminants).
7883
7884 -- Minor optimization: we avoid the generation of useless underlying
7885 -- record view entities if the private type declaration has unknown
7886 -- discriminants but its corresponding full view has no
7887 -- discriminants.
7888
7889 if Has_Unknown_Discriminants (Parent_Type)
7890 and then Present (Full_P)
7891 and then (Has_Discriminants (Full_P)
7892 or else Present (Underlying_Record_View (Full_P)))
7893 and then not In_Open_Scopes (Par_Scope)
7894 and then Expander_Active
7895 then
7896 declare
7897 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7898 New_Ext : constant Node_Id :=
7899 Copy_Separate_Tree
7900 (Record_Extension_Part (Type_Definition (N)));
7901 Decl : Node_Id;
7902
7903 begin
7904 Build_Derived_Record_Type
7905 (N, Parent_Type, Derived_Type, Derive_Subps);
7906
7907 -- Build anonymous completion, as a derivation from the full
7908 -- view of the parent. This is not a completion in the usual
7909 -- sense, because the current type is not private.
7910
7911 Decl :=
7912 Make_Full_Type_Declaration (Loc,
7913 Defining_Identifier => Full_Der,
7914 Type_Definition =>
7915 Make_Derived_Type_Definition (Loc,
7916 Subtype_Indication =>
7917 New_Copy_Tree
7918 (Subtype_Indication (Type_Definition (N))),
7919 Record_Extension_Part => New_Ext));
7920
7921 -- If the parent type has an underlying record view, use it
7922 -- here to build the new underlying record view.
7923
7924 if Present (Underlying_Record_View (Full_P)) then
7925 pragma Assert
7926 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7927 = N_Identifier);
7928 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7929 Underlying_Record_View (Full_P));
7930 end if;
7931
7932 Install_Private_Declarations (Par_Scope);
7933 Install_Visible_Declarations (Par_Scope);
7934 Insert_Before (N, Decl);
7935
7936 -- Mark entity as an underlying record view before analysis,
7937 -- to avoid generating the list of its primitive operations
7938 -- (which is not really required for this entity) and thus
7939 -- prevent spurious errors associated with missing overriding
7940 -- of abstract primitives (overridden only for Derived_Type).
7941
7942 Set_Ekind (Full_Der, E_Record_Type);
7943 Set_Is_Underlying_Record_View (Full_Der);
7944 Set_Default_SSO (Full_Der);
7945 Set_No_Reordering (Full_Der, No_Component_Reordering);
7946
7947 Analyze (Decl);
7948
7949 pragma Assert (Has_Discriminants (Full_Der)
7950 and then not Has_Unknown_Discriminants (Full_Der));
7951
7952 Uninstall_Declarations (Par_Scope);
7953
7954 -- Freeze the underlying record view, to prevent generation of
7955 -- useless dispatching information, which is simply shared with
7956 -- the real derived type.
7957
7958 Set_Is_Frozen (Full_Der);
7959
7960 -- If the derived type has access discriminants, create
7961 -- references to their anonymous types now, to prevent
7962 -- back-end problems when their first use is in generated
7963 -- bodies of primitives.
7964
7965 declare
7966 E : Entity_Id;
7967
7968 begin
7969 E := First_Entity (Full_Der);
7970
7971 while Present (E) loop
7972 if Ekind (E) = E_Discriminant
7973 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7974 then
7975 Build_Itype_Reference (Etype (E), Decl);
7976 end if;
7977
7978 Next_Entity (E);
7979 end loop;
7980 end;
7981
7982 -- Set up links between real entity and underlying record view
7983
7984 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7985 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7986 end;
7987
7988 -- If discriminants are known, build derived record
7989
7990 else
7991 Build_Derived_Record_Type
7992 (N, Parent_Type, Derived_Type, Derive_Subps);
7993 end if;
7994
7995 return;
7996
7997 elsif Has_Discriminants (Parent_Type) then
7998
7999 -- Build partial view of derived type from partial view of parent.
8000 -- This must be done before building the full derivation because the
8001 -- second derivation will modify the discriminants of the first and
8002 -- the discriminants are chained with the rest of the components in
8003 -- the full derivation.
8004
8005 Build_Derived_Record_Type
8006 (N, Parent_Type, Derived_Type, Derive_Subps);
8007
8008 -- Build the full derivation if this is not the anonymous derived
8009 -- base type created by Build_Derived_Record_Type in the constrained
8010 -- case (see point 5. of its head comment) since we build it for the
8011 -- derived subtype.
8012
8013 if Present (Full_View (Parent_Type))
8014 and then not Is_Itype (Derived_Type)
8015 then
8016 declare
8017 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8018 Discr : Entity_Id;
8019 Last_Discr : Entity_Id;
8020
8021 begin
8022 -- If this is not a completion, construct the implicit full
8023 -- view by deriving from the full view of the parent type.
8024 -- But if this is a completion, the derived private type
8025 -- being built is a full view and the full derivation can
8026 -- only be its underlying full view.
8027
8028 Build_Full_Derivation;
8029
8030 if not Is_Completion then
8031 Set_Full_View (Derived_Type, Full_Der);
8032 else
8033 Set_Underlying_Full_View (Derived_Type, Full_Der);
8034 Set_Is_Underlying_Full_View (Full_Der);
8035 end if;
8036
8037 if not Is_Base_Type (Derived_Type) then
8038 Set_Full_View (Der_Base, Base_Type (Full_Der));
8039 end if;
8040
8041 -- Copy the discriminant list from full view to the partial
8042 -- view (base type and its subtype). Gigi requires that the
8043 -- partial and full views have the same discriminants.
8044
8045 -- Note that since the partial view points to discriminants
8046 -- in the full view, their scope will be that of the full
8047 -- view. This might cause some front end problems and need
8048 -- adjustment???
8049
8050 Discr := First_Discriminant (Base_Type (Full_Der));
8051 Set_First_Entity (Der_Base, Discr);
8052
8053 loop
8054 Last_Discr := Discr;
8055 Next_Discriminant (Discr);
8056 exit when No (Discr);
8057 end loop;
8058
8059 Set_Last_Entity (Der_Base, Last_Discr);
8060 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8061 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8062 end;
8063 end if;
8064
8065 elsif Present (Full_View (Parent_Type))
8066 and then Has_Discriminants (Full_View (Parent_Type))
8067 then
8068 if Has_Unknown_Discriminants (Parent_Type)
8069 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8070 N_Subtype_Indication
8071 then
8072 Error_Msg_N
8073 ("cannot constrain type with unknown discriminants",
8074 Subtype_Indication (Type_Definition (N)));
8075 return;
8076 end if;
8077
8078 -- If this is not a completion, construct the implicit full view by
8079 -- deriving from the full view of the parent type. But if this is a
8080 -- completion, the derived private type being built is a full view
8081 -- and the full derivation can only be its underlying full view.
8082
8083 Build_Full_Derivation;
8084
8085 if not Is_Completion then
8086 Set_Full_View (Derived_Type, Full_Der);
8087 else
8088 Set_Underlying_Full_View (Derived_Type, Full_Der);
8089 Set_Is_Underlying_Full_View (Full_Der);
8090 end if;
8091
8092 -- In any case, the primitive operations are inherited from the
8093 -- parent type, not from the internal full view.
8094
8095 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8096
8097 if Derive_Subps then
8098 Derive_Subprograms (Parent_Type, Derived_Type);
8099 end if;
8100
8101 Set_Stored_Constraint (Derived_Type, No_Elist);
8102 Set_Is_Constrained
8103 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
8104
8105 else
8106 -- Untagged type, No discriminants on either view
8107
8108 if Nkind (Subtype_Indication (Type_Definition (N))) =
8109 N_Subtype_Indication
8110 then
8111 Error_Msg_N
8112 ("illegal constraint on type without discriminants", N);
8113 end if;
8114
8115 if Present (Discriminant_Specifications (N))
8116 and then Present (Full_View (Parent_Type))
8117 and then not Is_Tagged_Type (Full_View (Parent_Type))
8118 then
8119 Error_Msg_N ("cannot add discriminants to untagged type", N);
8120 end if;
8121
8122 Set_Stored_Constraint (Derived_Type, No_Elist);
8123 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8124
8125 Set_Is_Controlled_Active
8126 (Derived_Type, Is_Controlled_Active (Parent_Type));
8127
8128 Set_Disable_Controlled
8129 (Derived_Type, Disable_Controlled (Parent_Type));
8130
8131 Set_Has_Controlled_Component
8132 (Derived_Type, Has_Controlled_Component (Parent_Type));
8133
8134 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8135
8136 if not Is_Controlled (Parent_Type) then
8137 Set_Finalize_Storage_Only
8138 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8139 end if;
8140
8141 -- If this is not a completion, construct the implicit full view by
8142 -- deriving from the full view of the parent type.
8143
8144 -- ??? If the parent is untagged private and its completion is
8145 -- tagged, this mechanism will not work because we cannot derive from
8146 -- the tagged full view unless we have an extension.
8147
8148 if Present (Full_View (Parent_Type))
8149 and then not Is_Tagged_Type (Full_View (Parent_Type))
8150 and then not Is_Completion
8151 then
8152 Build_Full_Derivation;
8153 Set_Full_View (Derived_Type, Full_Der);
8154 end if;
8155 end if;
8156
8157 Set_Has_Unknown_Discriminants (Derived_Type,
8158 Has_Unknown_Discriminants (Parent_Type));
8159
8160 if Is_Private_Type (Derived_Type) then
8161 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8162 end if;
8163
8164 -- If the parent base type is in scope, add the derived type to its
8165 -- list of private dependents, because its full view may become
8166 -- visible subsequently (in a nested private part, a body, or in a
8167 -- further child unit).
8168
8169 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8170 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8171
8172 -- Check for unusual case where a type completed by a private
8173 -- derivation occurs within a package nested in a child unit, and
8174 -- the parent is declared in an ancestor.
8175
8176 if Is_Child_Unit (Scope (Current_Scope))
8177 and then Is_Completion
8178 and then In_Private_Part (Current_Scope)
8179 and then Scope (Parent_Type) /= Current_Scope
8180
8181 -- Note that if the parent has a completion in the private part,
8182 -- (which is itself a derivation from some other private type)
8183 -- it is that completion that is visible, there is no full view
8184 -- available, and no special processing is needed.
8185
8186 and then Present (Full_View (Parent_Type))
8187 then
8188 -- In this case, the full view of the parent type will become
8189 -- visible in the body of the enclosing child, and only then will
8190 -- the current type be possibly non-private. Build an underlying
8191 -- full view that will be installed when the enclosing child body
8192 -- is compiled.
8193
8194 if Present (Underlying_Full_View (Derived_Type)) then
8195 Full_Der := Underlying_Full_View (Derived_Type);
8196 else
8197 Build_Full_Derivation;
8198 Set_Underlying_Full_View (Derived_Type, Full_Der);
8199 Set_Is_Underlying_Full_View (Full_Der);
8200 end if;
8201
8202 -- The full view will be used to swap entities on entry/exit to
8203 -- the body, and must appear in the entity list for the package.
8204
8205 Append_Entity (Full_Der, Scope (Derived_Type));
8206 end if;
8207 end if;
8208 end Build_Derived_Private_Type;
8209
8210 -------------------------------
8211 -- Build_Derived_Record_Type --
8212 -------------------------------
8213
8214 -- 1. INTRODUCTION
8215
8216 -- Ideally we would like to use the same model of type derivation for
8217 -- tagged and untagged record types. Unfortunately this is not quite
8218 -- possible because the semantics of representation clauses is different
8219 -- for tagged and untagged records under inheritance. Consider the
8220 -- following:
8221
8222 -- type R (...) is [tagged] record ... end record;
8223 -- type T (...) is new R (...) [with ...];
8224
8225 -- The representation clauses for T can specify a completely different
8226 -- record layout from R's. Hence the same component can be placed in two
8227 -- very different positions in objects of type T and R. If R and T are
8228 -- tagged types, representation clauses for T can only specify the layout
8229 -- of non inherited components, thus components that are common in R and T
8230 -- have the same position in objects of type R and T.
8231
8232 -- This has two implications. The first is that the entire tree for R's
8233 -- declaration needs to be copied for T in the untagged case, so that T
8234 -- can be viewed as a record type of its own with its own representation
8235 -- clauses. The second implication is the way we handle discriminants.
8236 -- Specifically, in the untagged case we need a way to communicate to Gigi
8237 -- what are the real discriminants in the record, while for the semantics
8238 -- we need to consider those introduced by the user to rename the
8239 -- discriminants in the parent type. This is handled by introducing the
8240 -- notion of stored discriminants. See below for more.
8241
8242 -- Fortunately the way regular components are inherited can be handled in
8243 -- the same way in tagged and untagged types.
8244
8245 -- To complicate things a bit more the private view of a private extension
8246 -- cannot be handled in the same way as the full view (for one thing the
8247 -- semantic rules are somewhat different). We will explain what differs
8248 -- below.
8249
8250 -- 2. DISCRIMINANTS UNDER INHERITANCE
8251
8252 -- The semantic rules governing the discriminants of derived types are
8253 -- quite subtle.
8254
8255 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8256 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8257
8258 -- If parent type has discriminants, then the discriminants that are
8259 -- declared in the derived type are [3.4 (11)]:
8260
8261 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8262 -- there is one;
8263
8264 -- o Otherwise, each discriminant of the parent type (implicitly declared
8265 -- in the same order with the same specifications). In this case, the
8266 -- discriminants are said to be "inherited", or if unknown in the parent
8267 -- are also unknown in the derived type.
8268
8269 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8270
8271 -- o The parent subtype must be constrained;
8272
8273 -- o If the parent type is not a tagged type, then each discriminant of
8274 -- the derived type must be used in the constraint defining a parent
8275 -- subtype. [Implementation note: This ensures that the new discriminant
8276 -- can share storage with an existing discriminant.]
8277
8278 -- For the derived type each discriminant of the parent type is either
8279 -- inherited, constrained to equal some new discriminant of the derived
8280 -- type, or constrained to the value of an expression.
8281
8282 -- When inherited or constrained to equal some new discriminant, the
8283 -- parent discriminant and the discriminant of the derived type are said
8284 -- to "correspond".
8285
8286 -- If a discriminant of the parent type is constrained to a specific value
8287 -- in the derived type definition, then the discriminant is said to be
8288 -- "specified" by that derived type definition.
8289
8290 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8291
8292 -- We have spoken about stored discriminants in point 1 (introduction)
8293 -- above. There are two sorts of stored discriminants: implicit and
8294 -- explicit. As long as the derived type inherits the same discriminants as
8295 -- the root record type, stored discriminants are the same as regular
8296 -- discriminants, and are said to be implicit. However, if any discriminant
8297 -- in the root type was renamed in the derived type, then the derived
8298 -- type will contain explicit stored discriminants. Explicit stored
8299 -- discriminants are discriminants in addition to the semantically visible
8300 -- discriminants defined for the derived type. Stored discriminants are
8301 -- used by Gigi to figure out what are the physical discriminants in
8302 -- objects of the derived type (see precise definition in einfo.ads).
8303 -- As an example, consider the following:
8304
8305 -- type R (D1, D2, D3 : Int) is record ... end record;
8306 -- type T1 is new R;
8307 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8308 -- type T3 is new T2;
8309 -- type T4 (Y : Int) is new T3 (Y, 99);
8310
8311 -- The following table summarizes the discriminants and stored
8312 -- discriminants in R and T1 through T4:
8313
8314 -- Type Discrim Stored Discrim Comment
8315 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8316 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8317 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8318 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8319 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8320
8321 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8322 -- find the corresponding discriminant in the parent type, while
8323 -- Original_Record_Component (abbreviated ORC below) the actual physical
8324 -- component that is renamed. Finally the field Is_Completely_Hidden
8325 -- (abbreviated ICH below) is set for all explicit stored discriminants
8326 -- (see einfo.ads for more info). For the above example this gives:
8327
8328 -- Discrim CD ORC ICH
8329 -- ^^^^^^^ ^^ ^^^ ^^^
8330 -- D1 in R empty itself no
8331 -- D2 in R empty itself no
8332 -- D3 in R empty itself no
8333
8334 -- D1 in T1 D1 in R itself no
8335 -- D2 in T1 D2 in R itself no
8336 -- D3 in T1 D3 in R itself no
8337
8338 -- X1 in T2 D3 in T1 D3 in T2 no
8339 -- X2 in T2 D1 in T1 D1 in T2 no
8340 -- D1 in T2 empty itself yes
8341 -- D2 in T2 empty itself yes
8342 -- D3 in T2 empty itself yes
8343
8344 -- X1 in T3 X1 in T2 D3 in T3 no
8345 -- X2 in T3 X2 in T2 D1 in T3 no
8346 -- D1 in T3 empty itself yes
8347 -- D2 in T3 empty itself yes
8348 -- D3 in T3 empty itself yes
8349
8350 -- Y in T4 X1 in T3 D3 in T4 no
8351 -- D1 in T4 empty itself yes
8352 -- D2 in T4 empty itself yes
8353 -- D3 in T4 empty itself yes
8354
8355 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8356
8357 -- Type derivation for tagged types is fairly straightforward. If no
8358 -- discriminants are specified by the derived type, these are inherited
8359 -- from the parent. No explicit stored discriminants are ever necessary.
8360 -- The only manipulation that is done to the tree is that of adding a
8361 -- _parent field with parent type and constrained to the same constraint
8362 -- specified for the parent in the derived type definition. For instance:
8363
8364 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8365 -- type T1 is new R with null record;
8366 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8367
8368 -- are changed into:
8369
8370 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8371 -- _parent : R (D1, D2, D3);
8372 -- end record;
8373
8374 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8375 -- _parent : T1 (X2, 88, X1);
8376 -- end record;
8377
8378 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8379 -- ORC and ICH fields are:
8380
8381 -- Discrim CD ORC ICH
8382 -- ^^^^^^^ ^^ ^^^ ^^^
8383 -- D1 in R empty itself no
8384 -- D2 in R empty itself no
8385 -- D3 in R empty itself no
8386
8387 -- D1 in T1 D1 in R D1 in R no
8388 -- D2 in T1 D2 in R D2 in R no
8389 -- D3 in T1 D3 in R D3 in R no
8390
8391 -- X1 in T2 D3 in T1 D3 in R no
8392 -- X2 in T2 D1 in T1 D1 in R no
8393
8394 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8395 --
8396 -- Regardless of whether we dealing with a tagged or untagged type
8397 -- we will transform all derived type declarations of the form
8398 --
8399 -- type T is new R (...) [with ...];
8400 -- or
8401 -- subtype S is R (...);
8402 -- type T is new S [with ...];
8403 -- into
8404 -- type BT is new R [with ...];
8405 -- subtype T is BT (...);
8406 --
8407 -- That is, the base derived type is constrained only if it has no
8408 -- discriminants. The reason for doing this is that GNAT's semantic model
8409 -- assumes that a base type with discriminants is unconstrained.
8410 --
8411 -- Note that, strictly speaking, the above transformation is not always
8412 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8413 --
8414 -- procedure B34011A is
8415 -- type REC (D : integer := 0) is record
8416 -- I : Integer;
8417 -- end record;
8418
8419 -- package P is
8420 -- type T6 is new Rec;
8421 -- function F return T6;
8422 -- end P;
8423
8424 -- use P;
8425 -- package Q6 is
8426 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8427 -- end Q6;
8428 --
8429 -- The definition of Q6.U is illegal. However transforming Q6.U into
8430
8431 -- type BaseU is new T6;
8432 -- subtype U is BaseU (Q6.F.I)
8433
8434 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8435 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8436 -- the transformation described above.
8437
8438 -- There is another instance where the above transformation is incorrect.
8439 -- Consider:
8440
8441 -- package Pack is
8442 -- type Base (D : Integer) is tagged null record;
8443 -- procedure P (X : Base);
8444
8445 -- type Der is new Base (2) with null record;
8446 -- procedure P (X : Der);
8447 -- end Pack;
8448
8449 -- Then the above transformation turns this into
8450
8451 -- type Der_Base is new Base with null record;
8452 -- -- procedure P (X : Base) is implicitly inherited here
8453 -- -- as procedure P (X : Der_Base).
8454
8455 -- subtype Der is Der_Base (2);
8456 -- procedure P (X : Der);
8457 -- -- The overriding of P (X : Der_Base) is illegal since we
8458 -- -- have a parameter conformance problem.
8459
8460 -- To get around this problem, after having semantically processed Der_Base
8461 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8462 -- Discriminant_Constraint from Der so that when parameter conformance is
8463 -- checked when P is overridden, no semantic errors are flagged.
8464
8465 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8466
8467 -- Regardless of whether we are dealing with a tagged or untagged type
8468 -- we will transform all derived type declarations of the form
8469
8470 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8471 -- type T is new R [with ...];
8472 -- into
8473 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8474
8475 -- The reason for such transformation is that it allows us to implement a
8476 -- very clean form of component inheritance as explained below.
8477
8478 -- Note that this transformation is not achieved by direct tree rewriting
8479 -- and manipulation, but rather by redoing the semantic actions that the
8480 -- above transformation will entail. This is done directly in routine
8481 -- Inherit_Components.
8482
8483 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8484
8485 -- In both tagged and untagged derived types, regular non discriminant
8486 -- components are inherited in the derived type from the parent type. In
8487 -- the absence of discriminants component, inheritance is straightforward
8488 -- as components can simply be copied from the parent.
8489
8490 -- If the parent has discriminants, inheriting components constrained with
8491 -- these discriminants requires caution. Consider the following example:
8492
8493 -- type R (D1, D2 : Positive) is [tagged] record
8494 -- S : String (D1 .. D2);
8495 -- end record;
8496
8497 -- type T1 is new R [with null record];
8498 -- type T2 (X : positive) is new R (1, X) [with null record];
8499
8500 -- As explained in 6. above, T1 is rewritten as
8501 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8502 -- which makes the treatment for T1 and T2 identical.
8503
8504 -- What we want when inheriting S, is that references to D1 and D2 in R are
8505 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8506 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8507 -- with either discriminant references in the derived type or expressions.
8508 -- This replacement is achieved as follows: before inheriting R's
8509 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8510 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8511 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8512 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8513 -- by String (1 .. X).
8514
8515 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8516
8517 -- We explain here the rules governing private type extensions relevant to
8518 -- type derivation. These rules are explained on the following example:
8519
8520 -- type D [(...)] is new A [(...)] with private; <-- partial view
8521 -- type D [(...)] is new P [(...)] with null record; <-- full view
8522
8523 -- Type A is called the ancestor subtype of the private extension.
8524 -- Type P is the parent type of the full view of the private extension. It
8525 -- must be A or a type derived from A.
8526
8527 -- The rules concerning the discriminants of private type extensions are
8528 -- [7.3(10-13)]:
8529
8530 -- o If a private extension inherits known discriminants from the ancestor
8531 -- subtype, then the full view must also inherit its discriminants from
8532 -- the ancestor subtype and the parent subtype of the full view must be
8533 -- constrained if and only if the ancestor subtype is constrained.
8534
8535 -- o If a partial view has unknown discriminants, then the full view may
8536 -- define a definite or an indefinite subtype, with or without
8537 -- discriminants.
8538
8539 -- o If a partial view has neither known nor unknown discriminants, then
8540 -- the full view must define a definite subtype.
8541
8542 -- o If the ancestor subtype of a private extension has constrained
8543 -- discriminants, then the parent subtype of the full view must impose a
8544 -- statically matching constraint on those discriminants.
8545
8546 -- This means that only the following forms of private extensions are
8547 -- allowed:
8548
8549 -- type D is new A with private; <-- partial view
8550 -- type D is new P with null record; <-- full view
8551
8552 -- If A has no discriminants than P has no discriminants, otherwise P must
8553 -- inherit A's discriminants.
8554
8555 -- type D is new A (...) with private; <-- partial view
8556 -- type D is new P (:::) with null record; <-- full view
8557
8558 -- P must inherit A's discriminants and (...) and (:::) must statically
8559 -- match.
8560
8561 -- subtype A is R (...);
8562 -- type D is new A with private; <-- partial view
8563 -- type D is new P with null record; <-- full view
8564
8565 -- P must have inherited R's discriminants and must be derived from A or
8566 -- any of its subtypes.
8567
8568 -- type D (..) is new A with private; <-- partial view
8569 -- type D (..) is new P [(:::)] with null record; <-- full view
8570
8571 -- No specific constraints on P's discriminants or constraint (:::).
8572 -- Note that A can be unconstrained, but the parent subtype P must either
8573 -- be constrained or (:::) must be present.
8574
8575 -- type D (..) is new A [(...)] with private; <-- partial view
8576 -- type D (..) is new P [(:::)] with null record; <-- full view
8577
8578 -- P's constraints on A's discriminants must statically match those
8579 -- imposed by (...).
8580
8581 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8582
8583 -- The full view of a private extension is handled exactly as described
8584 -- above. The model chose for the private view of a private extension is
8585 -- the same for what concerns discriminants (i.e. they receive the same
8586 -- treatment as in the tagged case). However, the private view of the
8587 -- private extension always inherits the components of the parent base,
8588 -- without replacing any discriminant reference. Strictly speaking this is
8589 -- incorrect. However, Gigi never uses this view to generate code so this
8590 -- is a purely semantic issue. In theory, a set of transformations similar
8591 -- to those given in 5. and 6. above could be applied to private views of
8592 -- private extensions to have the same model of component inheritance as
8593 -- for non private extensions. However, this is not done because it would
8594 -- further complicate private type processing. Semantically speaking, this
8595 -- leaves us in an uncomfortable situation. As an example consider:
8596
8597 -- package Pack is
8598 -- type R (D : integer) is tagged record
8599 -- S : String (1 .. D);
8600 -- end record;
8601 -- procedure P (X : R);
8602 -- type T is new R (1) with private;
8603 -- private
8604 -- type T is new R (1) with null record;
8605 -- end;
8606
8607 -- This is transformed into:
8608
8609 -- package Pack is
8610 -- type R (D : integer) is tagged record
8611 -- S : String (1 .. D);
8612 -- end record;
8613 -- procedure P (X : R);
8614 -- type T is new R (1) with private;
8615 -- private
8616 -- type BaseT is new R with null record;
8617 -- subtype T is BaseT (1);
8618 -- end;
8619
8620 -- (strictly speaking the above is incorrect Ada)
8621
8622 -- From the semantic standpoint the private view of private extension T
8623 -- should be flagged as constrained since one can clearly have
8624 --
8625 -- Obj : T;
8626 --
8627 -- in a unit withing Pack. However, when deriving subprograms for the
8628 -- private view of private extension T, T must be seen as unconstrained
8629 -- since T has discriminants (this is a constraint of the current
8630 -- subprogram derivation model). Thus, when processing the private view of
8631 -- a private extension such as T, we first mark T as unconstrained, we
8632 -- process it, we perform program derivation and just before returning from
8633 -- Build_Derived_Record_Type we mark T as constrained.
8634
8635 -- ??? Are there are other uncomfortable cases that we will have to
8636 -- deal with.
8637
8638 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8639
8640 -- Types that are derived from a visible record type and have a private
8641 -- extension present other peculiarities. They behave mostly like private
8642 -- types, but if they have primitive operations defined, these will not
8643 -- have the proper signatures for further inheritance, because other
8644 -- primitive operations will use the implicit base that we define for
8645 -- private derivations below. This affect subprogram inheritance (see
8646 -- Derive_Subprograms for details). We also derive the implicit base from
8647 -- the base type of the full view, so that the implicit base is a record
8648 -- type and not another private type, This avoids infinite loops.
8649
8650 procedure Build_Derived_Record_Type
8651 (N : Node_Id;
8652 Parent_Type : Entity_Id;
8653 Derived_Type : Entity_Id;
8654 Derive_Subps : Boolean := True)
8655 is
8656 Discriminant_Specs : constant Boolean :=
8657 Present (Discriminant_Specifications (N));
8658 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8659 Loc : constant Source_Ptr := Sloc (N);
8660 Private_Extension : constant Boolean :=
8661 Nkind (N) = N_Private_Extension_Declaration;
8662 Assoc_List : Elist_Id;
8663 Constraint_Present : Boolean;
8664 Constrs : Elist_Id;
8665 Discrim : Entity_Id;
8666 Indic : Node_Id;
8667 Inherit_Discrims : Boolean := False;
8668 Last_Discrim : Entity_Id;
8669 New_Base : Entity_Id;
8670 New_Decl : Node_Id;
8671 New_Discrs : Elist_Id;
8672 New_Indic : Node_Id;
8673 Parent_Base : Entity_Id;
8674 Save_Etype : Entity_Id;
8675 Save_Discr_Constr : Elist_Id;
8676 Save_Next_Entity : Entity_Id;
8677 Type_Def : Node_Id;
8678
8679 Discs : Elist_Id := New_Elmt_List;
8680 -- An empty Discs list means that there were no constraints in the
8681 -- subtype indication or that there was an error processing it.
8682
8683 procedure Check_Generic_Ancestors;
8684 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8685 -- cannot be declared at a deeper level than its parent type is
8686 -- removed. The check on derivation within a generic body is also
8687 -- relaxed, but there's a restriction that a derived tagged type
8688 -- cannot be declared in a generic body if it's derived directly
8689 -- or indirectly from a formal type of that generic. This applies
8690 -- to progenitors as well.
8691
8692 -----------------------------
8693 -- Check_Generic_Ancestors --
8694 -----------------------------
8695
8696 procedure Check_Generic_Ancestors is
8697 Ancestor_Type : Entity_Id;
8698 Intf_List : List_Id;
8699 Intf_Name : Node_Id;
8700
8701 procedure Check_Ancestor;
8702 -- For parent and progenitors.
8703
8704 --------------------
8705 -- Check_Ancestor --
8706 --------------------
8707
8708 procedure Check_Ancestor is
8709 begin
8710 -- If the derived type does have a formal type as an ancestor
8711 -- then it's an error if the derived type is declared within
8712 -- the body of the generic unit that declares the formal type
8713 -- in its generic formal part. It's sufficient to check whether
8714 -- the ancestor type is declared inside the same generic body
8715 -- as the derived type (such as within a nested generic spec),
8716 -- in which case the derivation is legal. If the formal type is
8717 -- declared outside of that generic body, then it's certain
8718 -- that the derived type is declared within the generic body
8719 -- of the generic unit declaring the formal type.
8720
8721 if Is_Generic_Type (Ancestor_Type)
8722 and then Enclosing_Generic_Body (Ancestor_Type) /=
8723 Enclosing_Generic_Body (Derived_Type)
8724 then
8725 Error_Msg_NE
8726 ("ancestor type& is formal type of enclosing"
8727 & " generic unit (RM 3.9.1 (4/2))",
8728 Indic, Ancestor_Type);
8729 end if;
8730 end Check_Ancestor;
8731
8732 begin
8733 if Nkind (N) = N_Private_Extension_Declaration then
8734 Intf_List := Interface_List (N);
8735 else
8736 Intf_List := Interface_List (Type_Definition (N));
8737 end if;
8738
8739 if Present (Enclosing_Generic_Body (Derived_Type)) then
8740 Ancestor_Type := Parent_Type;
8741
8742 while not Is_Generic_Type (Ancestor_Type)
8743 and then Etype (Ancestor_Type) /= Ancestor_Type
8744 loop
8745 Ancestor_Type := Etype (Ancestor_Type);
8746 end loop;
8747
8748 Check_Ancestor;
8749
8750 if Present (Intf_List) then
8751 Intf_Name := First (Intf_List);
8752 while Present (Intf_Name) loop
8753 Ancestor_Type := Entity (Intf_Name);
8754 Check_Ancestor;
8755 Next (Intf_Name);
8756 end loop;
8757 end if;
8758 end if;
8759 end Check_Generic_Ancestors;
8760
8761 -- Start of processing for Build_Derived_Record_Type
8762
8763 begin
8764 if Ekind (Parent_Type) = E_Record_Type_With_Private
8765 and then Present (Full_View (Parent_Type))
8766 and then Has_Discriminants (Parent_Type)
8767 then
8768 Parent_Base := Base_Type (Full_View (Parent_Type));
8769 else
8770 Parent_Base := Base_Type (Parent_Type);
8771 end if;
8772
8773 -- If the parent type is declared as a subtype of another private
8774 -- type with inherited discriminants, its generated base type is
8775 -- itself a record subtype. To further inherit the constraint we
8776 -- need to use its own base to have an unconstrained type on which
8777 -- to apply the inherited constraint.
8778
8779 if Ekind (Parent_Base) = E_Record_Subtype then
8780 Parent_Base := Base_Type (Parent_Base);
8781 end if;
8782
8783 -- AI05-0115: if this is a derivation from a private type in some
8784 -- other scope that may lead to invisible components for the derived
8785 -- type, mark it accordingly.
8786
8787 if Is_Private_Type (Parent_Type) then
8788 if Scope (Parent_Base) = Scope (Derived_Type) then
8789 null;
8790
8791 elsif In_Open_Scopes (Scope (Parent_Base))
8792 and then In_Private_Part (Scope (Parent_Base))
8793 then
8794 null;
8795
8796 else
8797 Set_Has_Private_Ancestor (Derived_Type);
8798 end if;
8799
8800 else
8801 Set_Has_Private_Ancestor
8802 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8803 end if;
8804
8805 -- Before we start the previously documented transformations, here is
8806 -- little fix for size and alignment of tagged types. Normally when we
8807 -- derive type D from type P, we copy the size and alignment of P as the
8808 -- default for D, and in the absence of explicit representation clauses
8809 -- for D, the size and alignment are indeed the same as the parent.
8810
8811 -- But this is wrong for tagged types, since fields may be added, and
8812 -- the default size may need to be larger, and the default alignment may
8813 -- need to be larger.
8814
8815 -- We therefore reset the size and alignment fields in the tagged case.
8816 -- Note that the size and alignment will in any case be at least as
8817 -- large as the parent type (since the derived type has a copy of the
8818 -- parent type in the _parent field)
8819
8820 -- The type is also marked as being tagged here, which is needed when
8821 -- processing components with a self-referential anonymous access type
8822 -- in the call to Check_Anonymous_Access_Components below. Note that
8823 -- this flag is also set later on for completeness.
8824
8825 if Is_Tagged then
8826 Set_Is_Tagged_Type (Derived_Type);
8827 Init_Size_Align (Derived_Type);
8828 end if;
8829
8830 -- STEP 0a: figure out what kind of derived type declaration we have
8831
8832 if Private_Extension then
8833 Type_Def := N;
8834 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8835 Set_Default_SSO (Derived_Type);
8836 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8837
8838 else
8839 Type_Def := Type_Definition (N);
8840
8841 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8842 -- Parent_Base can be a private type or private extension. However,
8843 -- for tagged types with an extension the newly added fields are
8844 -- visible and hence the Derived_Type is always an E_Record_Type.
8845 -- (except that the parent may have its own private fields).
8846 -- For untagged types we preserve the Ekind of the Parent_Base.
8847
8848 if Present (Record_Extension_Part (Type_Def)) then
8849 Set_Ekind (Derived_Type, E_Record_Type);
8850 Set_Default_SSO (Derived_Type);
8851 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8852
8853 -- Create internal access types for components with anonymous
8854 -- access types.
8855
8856 if Ada_Version >= Ada_2005 then
8857 Check_Anonymous_Access_Components
8858 (N, Derived_Type, Derived_Type,
8859 Component_List (Record_Extension_Part (Type_Def)));
8860 end if;
8861
8862 else
8863 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8864 end if;
8865 end if;
8866
8867 -- Indic can either be an N_Identifier if the subtype indication
8868 -- contains no constraint or an N_Subtype_Indication if the subtype
8869 -- indication has a constraint. In either case it can include an
8870 -- interface list.
8871
8872 Indic := Subtype_Indication (Type_Def);
8873 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8874
8875 -- Check that the type has visible discriminants. The type may be
8876 -- a private type with unknown discriminants whose full view has
8877 -- discriminants which are invisible.
8878
8879 if Constraint_Present then
8880 if not Has_Discriminants (Parent_Base)
8881 or else
8882 (Has_Unknown_Discriminants (Parent_Base)
8883 and then Is_Private_Type (Parent_Base))
8884 then
8885 Error_Msg_N
8886 ("invalid constraint: type has no discriminant",
8887 Constraint (Indic));
8888
8889 Constraint_Present := False;
8890 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8891
8892 elsif Is_Constrained (Parent_Type) then
8893 Error_Msg_N
8894 ("invalid constraint: parent type is already constrained",
8895 Constraint (Indic));
8896
8897 Constraint_Present := False;
8898 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8899 end if;
8900 end if;
8901
8902 -- STEP 0b: If needed, apply transformation given in point 5. above
8903
8904 if not Private_Extension
8905 and then Has_Discriminants (Parent_Type)
8906 and then not Discriminant_Specs
8907 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8908 then
8909 -- First, we must analyze the constraint (see comment in point 5.)
8910 -- The constraint may come from the subtype indication of the full
8911 -- declaration.
8912
8913 if Constraint_Present then
8914 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8915
8916 -- If there is no explicit constraint, there might be one that is
8917 -- inherited from a constrained parent type. In that case verify that
8918 -- it conforms to the constraint in the partial view. In perverse
8919 -- cases the parent subtypes of the partial and full view can have
8920 -- different constraints.
8921
8922 elsif Present (Stored_Constraint (Parent_Type)) then
8923 New_Discrs := Stored_Constraint (Parent_Type);
8924
8925 else
8926 New_Discrs := No_Elist;
8927 end if;
8928
8929 if Has_Discriminants (Derived_Type)
8930 and then Has_Private_Declaration (Derived_Type)
8931 and then Present (Discriminant_Constraint (Derived_Type))
8932 and then Present (New_Discrs)
8933 then
8934 -- Verify that constraints of the full view statically match
8935 -- those given in the partial view.
8936
8937 declare
8938 C1, C2 : Elmt_Id;
8939
8940 begin
8941 C1 := First_Elmt (New_Discrs);
8942 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8943 while Present (C1) and then Present (C2) loop
8944 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8945 or else
8946 (Is_OK_Static_Expression (Node (C1))
8947 and then Is_OK_Static_Expression (Node (C2))
8948 and then
8949 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8950 then
8951 null;
8952
8953 else
8954 if Constraint_Present then
8955 Error_Msg_N
8956 ("constraint not conformant to previous declaration",
8957 Node (C1));
8958 else
8959 Error_Msg_N
8960 ("constraint of full view is incompatible "
8961 & "with partial view", N);
8962 end if;
8963 end if;
8964
8965 Next_Elmt (C1);
8966 Next_Elmt (C2);
8967 end loop;
8968 end;
8969 end if;
8970
8971 -- Insert and analyze the declaration for the unconstrained base type
8972
8973 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8974
8975 New_Decl :=
8976 Make_Full_Type_Declaration (Loc,
8977 Defining_Identifier => New_Base,
8978 Type_Definition =>
8979 Make_Derived_Type_Definition (Loc,
8980 Abstract_Present => Abstract_Present (Type_Def),
8981 Limited_Present => Limited_Present (Type_Def),
8982 Subtype_Indication =>
8983 New_Occurrence_Of (Parent_Base, Loc),
8984 Record_Extension_Part =>
8985 Relocate_Node (Record_Extension_Part (Type_Def)),
8986 Interface_List => Interface_List (Type_Def)));
8987
8988 Set_Parent (New_Decl, Parent (N));
8989 Mark_Rewrite_Insertion (New_Decl);
8990 Insert_Before (N, New_Decl);
8991
8992 -- In the extension case, make sure ancestor is frozen appropriately
8993 -- (see also non-discriminated case below).
8994
8995 if Present (Record_Extension_Part (Type_Def))
8996 or else Is_Interface (Parent_Base)
8997 then
8998 Freeze_Before (New_Decl, Parent_Type);
8999 end if;
9000
9001 -- Note that this call passes False for the Derive_Subps parameter
9002 -- because subprogram derivation is deferred until after creating
9003 -- the subtype (see below).
9004
9005 Build_Derived_Type
9006 (New_Decl, Parent_Base, New_Base,
9007 Is_Completion => False, Derive_Subps => False);
9008
9009 -- ??? This needs re-examination to determine whether the
9010 -- above call can simply be replaced by a call to Analyze.
9011
9012 Set_Analyzed (New_Decl);
9013
9014 -- Insert and analyze the declaration for the constrained subtype
9015
9016 if Constraint_Present then
9017 New_Indic :=
9018 Make_Subtype_Indication (Loc,
9019 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9020 Constraint => Relocate_Node (Constraint (Indic)));
9021
9022 else
9023 declare
9024 Constr_List : constant List_Id := New_List;
9025 C : Elmt_Id;
9026 Expr : Node_Id;
9027
9028 begin
9029 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9030 while Present (C) loop
9031 Expr := Node (C);
9032
9033 -- It is safe here to call New_Copy_Tree since we called
9034 -- Force_Evaluation on each constraint previously
9035 -- in Build_Discriminant_Constraints.
9036
9037 Append (New_Copy_Tree (Expr), To => Constr_List);
9038
9039 Next_Elmt (C);
9040 end loop;
9041
9042 New_Indic :=
9043 Make_Subtype_Indication (Loc,
9044 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9045 Constraint =>
9046 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9047 end;
9048 end if;
9049
9050 Rewrite (N,
9051 Make_Subtype_Declaration (Loc,
9052 Defining_Identifier => Derived_Type,
9053 Subtype_Indication => New_Indic));
9054
9055 Analyze (N);
9056
9057 -- Derivation of subprograms must be delayed until the full subtype
9058 -- has been established, to ensure proper overriding of subprograms
9059 -- inherited by full types. If the derivations occurred as part of
9060 -- the call to Build_Derived_Type above, then the check for type
9061 -- conformance would fail because earlier primitive subprograms
9062 -- could still refer to the full type prior the change to the new
9063 -- subtype and hence would not match the new base type created here.
9064 -- Subprograms are not derived, however, when Derive_Subps is False
9065 -- (since otherwise there could be redundant derivations).
9066
9067 if Derive_Subps then
9068 Derive_Subprograms (Parent_Type, Derived_Type);
9069 end if;
9070
9071 -- For tagged types the Discriminant_Constraint of the new base itype
9072 -- is inherited from the first subtype so that no subtype conformance
9073 -- problem arise when the first subtype overrides primitive
9074 -- operations inherited by the implicit base type.
9075
9076 if Is_Tagged then
9077 Set_Discriminant_Constraint
9078 (New_Base, Discriminant_Constraint (Derived_Type));
9079 end if;
9080
9081 return;
9082 end if;
9083
9084 -- If we get here Derived_Type will have no discriminants or it will be
9085 -- a discriminated unconstrained base type.
9086
9087 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9088
9089 if Is_Tagged then
9090
9091 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9092 -- The declaration of a specific descendant of an interface type
9093 -- freezes the interface type (RM 13.14).
9094
9095 if not Private_Extension or else Is_Interface (Parent_Base) then
9096 Freeze_Before (N, Parent_Type);
9097 end if;
9098
9099 if Ada_Version >= Ada_2005 then
9100 Check_Generic_Ancestors;
9101
9102 elsif Type_Access_Level (Derived_Type) /=
9103 Type_Access_Level (Parent_Type)
9104 and then not Is_Generic_Type (Derived_Type)
9105 then
9106 if Is_Controlled (Parent_Type) then
9107 Error_Msg_N
9108 ("controlled type must be declared at the library level",
9109 Indic);
9110 else
9111 Error_Msg_N
9112 ("type extension at deeper accessibility level than parent",
9113 Indic);
9114 end if;
9115
9116 else
9117 declare
9118 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9119 begin
9120 if Present (GB)
9121 and then GB /= Enclosing_Generic_Body (Parent_Base)
9122 then
9123 Error_Msg_NE
9124 ("parent type of& must not be outside generic body"
9125 & " (RM 3.9.1(4))",
9126 Indic, Derived_Type);
9127 end if;
9128 end;
9129 end if;
9130 end if;
9131
9132 -- Ada 2005 (AI-251)
9133
9134 if Ada_Version >= Ada_2005 and then Is_Tagged then
9135
9136 -- "The declaration of a specific descendant of an interface type
9137 -- freezes the interface type" (RM 13.14).
9138
9139 declare
9140 Iface : Node_Id;
9141 begin
9142 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9143 Iface := First (Interface_List (Type_Def));
9144 while Present (Iface) loop
9145 Freeze_Before (N, Etype (Iface));
9146 Next (Iface);
9147 end loop;
9148 end if;
9149 end;
9150 end if;
9151
9152 -- STEP 1b : preliminary cleanup of the full view of private types
9153
9154 -- If the type is already marked as having discriminants, then it's the
9155 -- completion of a private type or private extension and we need to
9156 -- retain the discriminants from the partial view if the current
9157 -- declaration has Discriminant_Specifications so that we can verify
9158 -- conformance. However, we must remove any existing components that
9159 -- were inherited from the parent (and attached in Copy_And_Swap)
9160 -- because the full type inherits all appropriate components anyway, and
9161 -- we do not want the partial view's components interfering.
9162
9163 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9164 Discrim := First_Discriminant (Derived_Type);
9165 loop
9166 Last_Discrim := Discrim;
9167 Next_Discriminant (Discrim);
9168 exit when No (Discrim);
9169 end loop;
9170
9171 Set_Last_Entity (Derived_Type, Last_Discrim);
9172
9173 -- In all other cases wipe out the list of inherited components (even
9174 -- inherited discriminants), it will be properly rebuilt here.
9175
9176 else
9177 Set_First_Entity (Derived_Type, Empty);
9178 Set_Last_Entity (Derived_Type, Empty);
9179 end if;
9180
9181 -- STEP 1c: Initialize some flags for the Derived_Type
9182
9183 -- The following flags must be initialized here so that
9184 -- Process_Discriminants can check that discriminants of tagged types do
9185 -- not have a default initial value and that access discriminants are
9186 -- only specified for limited records. For completeness, these flags are
9187 -- also initialized along with all the other flags below.
9188
9189 -- AI-419: Limitedness is not inherited from an interface parent, so to
9190 -- be limited in that case the type must be explicitly declared as
9191 -- limited. However, task and protected interfaces are always limited.
9192
9193 if Limited_Present (Type_Def) then
9194 Set_Is_Limited_Record (Derived_Type);
9195
9196 elsif Is_Limited_Record (Parent_Type)
9197 or else (Present (Full_View (Parent_Type))
9198 and then Is_Limited_Record (Full_View (Parent_Type)))
9199 then
9200 if not Is_Interface (Parent_Type)
9201 or else Is_Synchronized_Interface (Parent_Type)
9202 or else Is_Protected_Interface (Parent_Type)
9203 or else Is_Task_Interface (Parent_Type)
9204 then
9205 Set_Is_Limited_Record (Derived_Type);
9206 end if;
9207 end if;
9208
9209 -- STEP 2a: process discriminants of derived type if any
9210
9211 Push_Scope (Derived_Type);
9212
9213 if Discriminant_Specs then
9214 Set_Has_Unknown_Discriminants (Derived_Type, False);
9215
9216 -- The following call initializes fields Has_Discriminants and
9217 -- Discriminant_Constraint, unless we are processing the completion
9218 -- of a private type declaration.
9219
9220 Check_Or_Process_Discriminants (N, Derived_Type);
9221
9222 -- For untagged types, the constraint on the Parent_Type must be
9223 -- present and is used to rename the discriminants.
9224
9225 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9226 Error_Msg_N ("untagged parent must have discriminants", Indic);
9227
9228 elsif not Is_Tagged and then not Constraint_Present then
9229 Error_Msg_N
9230 ("discriminant constraint needed for derived untagged records",
9231 Indic);
9232
9233 -- Otherwise the parent subtype must be constrained unless we have a
9234 -- private extension.
9235
9236 elsif not Constraint_Present
9237 and then not Private_Extension
9238 and then not Is_Constrained (Parent_Type)
9239 then
9240 Error_Msg_N
9241 ("unconstrained type not allowed in this context", Indic);
9242
9243 elsif Constraint_Present then
9244 -- The following call sets the field Corresponding_Discriminant
9245 -- for the discriminants in the Derived_Type.
9246
9247 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9248
9249 -- For untagged types all new discriminants must rename
9250 -- discriminants in the parent. For private extensions new
9251 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9252
9253 Discrim := First_Discriminant (Derived_Type);
9254 while Present (Discrim) loop
9255 if not Is_Tagged
9256 and then No (Corresponding_Discriminant (Discrim))
9257 then
9258 Error_Msg_N
9259 ("new discriminants must constrain old ones", Discrim);
9260
9261 elsif Private_Extension
9262 and then Present (Corresponding_Discriminant (Discrim))
9263 then
9264 Error_Msg_N
9265 ("only static constraints allowed for parent"
9266 & " discriminants in the partial view", Indic);
9267 exit;
9268 end if;
9269
9270 -- If a new discriminant is used in the constraint, then its
9271 -- subtype must be statically compatible with the parent
9272 -- discriminant's subtype (3.7(15)).
9273
9274 -- However, if the record contains an array constrained by
9275 -- the discriminant but with some different bound, the compiler
9276 -- tries to create a smaller range for the discriminant type.
9277 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9278 -- the discriminant type is a scalar type, the check must use
9279 -- the original discriminant type in the parent declaration.
9280
9281 declare
9282 Corr_Disc : constant Entity_Id :=
9283 Corresponding_Discriminant (Discrim);
9284 Disc_Type : constant Entity_Id := Etype (Discrim);
9285 Corr_Type : Entity_Id;
9286
9287 begin
9288 if Present (Corr_Disc) then
9289 if Is_Scalar_Type (Disc_Type) then
9290 Corr_Type :=
9291 Entity (Discriminant_Type (Parent (Corr_Disc)));
9292 else
9293 Corr_Type := Etype (Corr_Disc);
9294 end if;
9295
9296 if not
9297 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9298 then
9299 Error_Msg_N
9300 ("subtype must be compatible "
9301 & "with parent discriminant",
9302 Discrim);
9303 end if;
9304 end if;
9305 end;
9306
9307 Next_Discriminant (Discrim);
9308 end loop;
9309
9310 -- Check whether the constraints of the full view statically
9311 -- match those imposed by the parent subtype [7.3(13)].
9312
9313 if Present (Stored_Constraint (Derived_Type)) then
9314 declare
9315 C1, C2 : Elmt_Id;
9316
9317 begin
9318 C1 := First_Elmt (Discs);
9319 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9320 while Present (C1) and then Present (C2) loop
9321 if not
9322 Fully_Conformant_Expressions (Node (C1), Node (C2))
9323 then
9324 Error_Msg_N
9325 ("not conformant with previous declaration",
9326 Node (C1));
9327 end if;
9328
9329 Next_Elmt (C1);
9330 Next_Elmt (C2);
9331 end loop;
9332 end;
9333 end if;
9334 end if;
9335
9336 -- STEP 2b: No new discriminants, inherit discriminants if any
9337
9338 else
9339 if Private_Extension then
9340 Set_Has_Unknown_Discriminants
9341 (Derived_Type,
9342 Has_Unknown_Discriminants (Parent_Type)
9343 or else Unknown_Discriminants_Present (N));
9344
9345 -- The partial view of the parent may have unknown discriminants,
9346 -- but if the full view has discriminants and the parent type is
9347 -- in scope they must be inherited.
9348
9349 elsif Has_Unknown_Discriminants (Parent_Type)
9350 and then
9351 (not Has_Discriminants (Parent_Type)
9352 or else not In_Open_Scopes (Scope (Parent_Base)))
9353 then
9354 Set_Has_Unknown_Discriminants (Derived_Type);
9355 end if;
9356
9357 if not Has_Unknown_Discriminants (Derived_Type)
9358 and then not Has_Unknown_Discriminants (Parent_Base)
9359 and then Has_Discriminants (Parent_Type)
9360 then
9361 Inherit_Discrims := True;
9362 Set_Has_Discriminants
9363 (Derived_Type, True);
9364 Set_Discriminant_Constraint
9365 (Derived_Type, Discriminant_Constraint (Parent_Base));
9366 end if;
9367
9368 -- The following test is true for private types (remember
9369 -- transformation 5. is not applied to those) and in an error
9370 -- situation.
9371
9372 if Constraint_Present then
9373 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9374 end if;
9375
9376 -- For now mark a new derived type as constrained only if it has no
9377 -- discriminants. At the end of Build_Derived_Record_Type we properly
9378 -- set this flag in the case of private extensions. See comments in
9379 -- point 9. just before body of Build_Derived_Record_Type.
9380
9381 Set_Is_Constrained
9382 (Derived_Type,
9383 not (Inherit_Discrims
9384 or else Has_Unknown_Discriminants (Derived_Type)));
9385 end if;
9386
9387 -- STEP 3: initialize fields of derived type
9388
9389 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9390 Set_Stored_Constraint (Derived_Type, No_Elist);
9391
9392 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9393 -- but cannot be interfaces
9394
9395 if not Private_Extension
9396 and then Ekind (Derived_Type) /= E_Private_Type
9397 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9398 then
9399 if Interface_Present (Type_Def) then
9400 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9401 end if;
9402
9403 Set_Interfaces (Derived_Type, No_Elist);
9404 end if;
9405
9406 -- Fields inherited from the Parent_Type
9407
9408 Set_Has_Specified_Layout
9409 (Derived_Type, Has_Specified_Layout (Parent_Type));
9410 Set_Is_Limited_Composite
9411 (Derived_Type, Is_Limited_Composite (Parent_Type));
9412 Set_Is_Private_Composite
9413 (Derived_Type, Is_Private_Composite (Parent_Type));
9414
9415 if Is_Tagged_Type (Parent_Type) then
9416 Set_No_Tagged_Streams_Pragma
9417 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9418 end if;
9419
9420 -- Fields inherited from the Parent_Base
9421
9422 Set_Has_Controlled_Component
9423 (Derived_Type, Has_Controlled_Component (Parent_Base));
9424 Set_Has_Non_Standard_Rep
9425 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9426 Set_Has_Primitive_Operations
9427 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9428
9429 -- Set fields for private derived types
9430
9431 if Is_Private_Type (Derived_Type) then
9432 Set_Depends_On_Private (Derived_Type, True);
9433 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9434 end if;
9435
9436 -- Inherit fields for non-private types. If this is the completion of a
9437 -- derivation from a private type, the parent itself is private and the
9438 -- attributes come from its full view, which must be present.
9439
9440 if Is_Record_Type (Derived_Type) then
9441 declare
9442 Parent_Full : Entity_Id;
9443
9444 begin
9445 if Is_Private_Type (Parent_Base)
9446 and then not Is_Record_Type (Parent_Base)
9447 then
9448 Parent_Full := Full_View (Parent_Base);
9449 else
9450 Parent_Full := Parent_Base;
9451 end if;
9452
9453 Set_Component_Alignment
9454 (Derived_Type, Component_Alignment (Parent_Full));
9455 Set_C_Pass_By_Copy
9456 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9457 Set_Has_Complex_Representation
9458 (Derived_Type, Has_Complex_Representation (Parent_Full));
9459
9460 -- For untagged types, inherit the layout by default to avoid
9461 -- costly changes of representation for type conversions.
9462
9463 if not Is_Tagged then
9464 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9465 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9466 end if;
9467 end;
9468 end if;
9469
9470 -- Set fields for tagged types
9471
9472 if Is_Tagged then
9473 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9474
9475 -- All tagged types defined in Ada.Finalization are controlled
9476
9477 if Chars (Scope (Derived_Type)) = Name_Finalization
9478 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9479 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9480 then
9481 Set_Is_Controlled_Active (Derived_Type);
9482 else
9483 Set_Is_Controlled_Active
9484 (Derived_Type, Is_Controlled_Active (Parent_Base));
9485 end if;
9486
9487 -- Minor optimization: there is no need to generate the class-wide
9488 -- entity associated with an underlying record view.
9489
9490 if not Is_Underlying_Record_View (Derived_Type) then
9491 Make_Class_Wide_Type (Derived_Type);
9492 end if;
9493
9494 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9495
9496 if Has_Discriminants (Derived_Type)
9497 and then Constraint_Present
9498 then
9499 Set_Stored_Constraint
9500 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9501 end if;
9502
9503 if Ada_Version >= Ada_2005 then
9504 declare
9505 Ifaces_List : Elist_Id;
9506
9507 begin
9508 -- Checks rules 3.9.4 (13/2 and 14/2)
9509
9510 if Comes_From_Source (Derived_Type)
9511 and then not Is_Private_Type (Derived_Type)
9512 and then Is_Interface (Parent_Type)
9513 and then not Is_Interface (Derived_Type)
9514 then
9515 if Is_Task_Interface (Parent_Type) then
9516 Error_Msg_N
9517 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9518 Derived_Type);
9519
9520 elsif Is_Protected_Interface (Parent_Type) then
9521 Error_Msg_N
9522 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9523 Derived_Type);
9524 end if;
9525 end if;
9526
9527 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9528
9529 Check_Interfaces (N, Type_Def);
9530
9531 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9532 -- not already in the parents.
9533
9534 Collect_Interfaces
9535 (T => Derived_Type,
9536 Ifaces_List => Ifaces_List,
9537 Exclude_Parents => True);
9538
9539 Set_Interfaces (Derived_Type, Ifaces_List);
9540
9541 -- If the derived type is the anonymous type created for
9542 -- a declaration whose parent has a constraint, propagate
9543 -- the interface list to the source type. This must be done
9544 -- prior to the completion of the analysis of the source type
9545 -- because the components in the extension may contain current
9546 -- instances whose legality depends on some ancestor.
9547
9548 if Is_Itype (Derived_Type) then
9549 declare
9550 Def : constant Node_Id :=
9551 Associated_Node_For_Itype (Derived_Type);
9552 begin
9553 if Present (Def)
9554 and then Nkind (Def) = N_Full_Type_Declaration
9555 then
9556 Set_Interfaces
9557 (Defining_Identifier (Def), Ifaces_List);
9558 end if;
9559 end;
9560 end if;
9561
9562 -- A type extension is automatically Ghost when one of its
9563 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9564 -- also inherited when the parent type is Ghost, but this is
9565 -- done in Build_Derived_Type as the mechanism also handles
9566 -- untagged derivations.
9567
9568 if Implements_Ghost_Interface (Derived_Type) then
9569 Set_Is_Ghost_Entity (Derived_Type);
9570 end if;
9571 end;
9572 end if;
9573 end if;
9574
9575 -- STEP 4: Inherit components from the parent base and constrain them.
9576 -- Apply the second transformation described in point 6. above.
9577
9578 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9579 or else not Has_Discriminants (Parent_Type)
9580 or else not Is_Constrained (Parent_Type)
9581 then
9582 Constrs := Discs;
9583 else
9584 Constrs := Discriminant_Constraint (Parent_Type);
9585 end if;
9586
9587 Assoc_List :=
9588 Inherit_Components
9589 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9590
9591 -- STEP 5a: Copy the parent record declaration for untagged types
9592
9593 Set_Has_Implicit_Dereference
9594 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9595
9596 if not Is_Tagged then
9597
9598 -- Discriminant_Constraint (Derived_Type) has been properly
9599 -- constructed. Save it and temporarily set it to Empty because we
9600 -- do not want the call to New_Copy_Tree below to mess this list.
9601
9602 if Has_Discriminants (Derived_Type) then
9603 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9604 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9605 else
9606 Save_Discr_Constr := No_Elist;
9607 end if;
9608
9609 -- Save the Etype field of Derived_Type. It is correctly set now,
9610 -- but the call to New_Copy tree may remap it to point to itself,
9611 -- which is not what we want. Ditto for the Next_Entity field.
9612
9613 Save_Etype := Etype (Derived_Type);
9614 Save_Next_Entity := Next_Entity (Derived_Type);
9615
9616 -- Assoc_List maps all stored discriminants in the Parent_Base to
9617 -- stored discriminants in the Derived_Type. It is fundamental that
9618 -- no types or itypes with discriminants other than the stored
9619 -- discriminants appear in the entities declared inside
9620 -- Derived_Type, since the back end cannot deal with it.
9621
9622 New_Decl :=
9623 New_Copy_Tree
9624 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9625 Copy_Dimensions_Of_Components (Derived_Type);
9626
9627 -- Restore the fields saved prior to the New_Copy_Tree call
9628 -- and compute the stored constraint.
9629
9630 Set_Etype (Derived_Type, Save_Etype);
9631 Link_Entities (Derived_Type, Save_Next_Entity);
9632
9633 if Has_Discriminants (Derived_Type) then
9634 Set_Discriminant_Constraint
9635 (Derived_Type, Save_Discr_Constr);
9636 Set_Stored_Constraint
9637 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9638
9639 Replace_Discriminants (Derived_Type, New_Decl);
9640 end if;
9641
9642 -- Insert the new derived type declaration
9643
9644 Rewrite (N, New_Decl);
9645
9646 -- STEP 5b: Complete the processing for record extensions in generics
9647
9648 -- There is no completion for record extensions declared in the
9649 -- parameter part of a generic, so we need to complete processing for
9650 -- these generic record extensions here. The Record_Type_Definition call
9651 -- will change the Ekind of the components from E_Void to E_Component.
9652
9653 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9654 Record_Type_Definition (Empty, Derived_Type);
9655
9656 -- STEP 5c: Process the record extension for non private tagged types
9657
9658 elsif not Private_Extension then
9659 Expand_Record_Extension (Derived_Type, Type_Def);
9660
9661 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9662 -- derived type to propagate some semantic information. This led
9663 -- to other ASIS failures and has been removed.
9664
9665 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9666 -- implemented interfaces if we are in expansion mode
9667
9668 if Expander_Active
9669 and then Has_Interfaces (Derived_Type)
9670 then
9671 Add_Interface_Tag_Components (N, Derived_Type);
9672 end if;
9673
9674 -- Analyze the record extension
9675
9676 Record_Type_Definition
9677 (Record_Extension_Part (Type_Def), Derived_Type);
9678 end if;
9679
9680 End_Scope;
9681
9682 -- Nothing else to do if there is an error in the derivation.
9683 -- An unusual case: the full view may be derived from a type in an
9684 -- instance, when the partial view was used illegally as an actual
9685 -- in that instance, leading to a circular definition.
9686
9687 if Etype (Derived_Type) = Any_Type
9688 or else Etype (Parent_Type) = Derived_Type
9689 then
9690 return;
9691 end if;
9692
9693 -- Set delayed freeze and then derive subprograms, we need to do
9694 -- this in this order so that derived subprograms inherit the
9695 -- derived freeze if necessary.
9696
9697 Set_Has_Delayed_Freeze (Derived_Type);
9698
9699 if Derive_Subps then
9700 Derive_Subprograms (Parent_Type, Derived_Type);
9701 end if;
9702
9703 -- If we have a private extension which defines a constrained derived
9704 -- type mark as constrained here after we have derived subprograms. See
9705 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9706
9707 if Private_Extension and then Inherit_Discrims then
9708 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9709 Set_Is_Constrained (Derived_Type, True);
9710 Set_Discriminant_Constraint (Derived_Type, Discs);
9711
9712 elsif Is_Constrained (Parent_Type) then
9713 Set_Is_Constrained
9714 (Derived_Type, True);
9715 Set_Discriminant_Constraint
9716 (Derived_Type, Discriminant_Constraint (Parent_Type));
9717 end if;
9718 end if;
9719
9720 -- Update the class-wide type, which shares the now-completed entity
9721 -- list with its specific type. In case of underlying record views,
9722 -- we do not generate the corresponding class wide entity.
9723
9724 if Is_Tagged
9725 and then not Is_Underlying_Record_View (Derived_Type)
9726 then
9727 Set_First_Entity
9728 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9729 Set_Last_Entity
9730 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9731 end if;
9732
9733 Check_Function_Writable_Actuals (N);
9734 end Build_Derived_Record_Type;
9735
9736 ------------------------
9737 -- Build_Derived_Type --
9738 ------------------------
9739
9740 procedure Build_Derived_Type
9741 (N : Node_Id;
9742 Parent_Type : Entity_Id;
9743 Derived_Type : Entity_Id;
9744 Is_Completion : Boolean;
9745 Derive_Subps : Boolean := True)
9746 is
9747 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9748
9749 begin
9750 -- Set common attributes
9751
9752 Set_Scope (Derived_Type, Current_Scope);
9753 Set_Etype (Derived_Type, Parent_Base);
9754 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9755 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9756
9757 Set_Size_Info (Derived_Type, Parent_Type);
9758 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9759
9760 Set_Is_Controlled_Active
9761 (Derived_Type, Is_Controlled_Active (Parent_Type));
9762
9763 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9764 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9765 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9766
9767 if Is_Tagged_Type (Derived_Type) then
9768 Set_No_Tagged_Streams_Pragma
9769 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9770 end if;
9771
9772 -- If the parent has primitive routines and may have not-seen-yet aspect
9773 -- specifications (e.g., a Pack pragma), then set the derived type link
9774 -- in order to later diagnose "early derivation" issues. If in different
9775 -- compilation units, then "early derivation" cannot be an issue (and we
9776 -- don't like interunit references that go in the opposite direction of
9777 -- semantic dependencies).
9778
9779 if Has_Primitive_Operations (Parent_Type)
9780 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9781 Enclosing_Comp_Unit_Node (Derived_Type)
9782 then
9783 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9784 end if;
9785
9786 -- If the parent type is a private subtype, the convention on the base
9787 -- type may be set in the private part, and not propagated to the
9788 -- subtype until later, so we obtain the convention from the base type.
9789
9790 Set_Convention (Derived_Type, Convention (Parent_Base));
9791
9792 -- Set SSO default for record or array type
9793
9794 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9795 and then Is_Base_Type (Derived_Type)
9796 then
9797 Set_Default_SSO (Derived_Type);
9798 end if;
9799
9800 -- A derived type inherits the Default_Initial_Condition pragma coming
9801 -- from any parent type within the derivation chain.
9802
9803 if Has_DIC (Parent_Type) then
9804 Set_Has_Inherited_DIC (Derived_Type);
9805 end if;
9806
9807 -- A derived type inherits any class-wide invariants coming from a
9808 -- parent type or an interface. Note that the invariant procedure of
9809 -- the parent type should not be inherited because the derived type may
9810 -- define invariants of its own.
9811
9812 if not Is_Interface (Derived_Type) then
9813 if Has_Inherited_Invariants (Parent_Type)
9814 or else Has_Inheritable_Invariants (Parent_Type)
9815 then
9816 Set_Has_Inherited_Invariants (Derived_Type);
9817
9818 elsif Is_Concurrent_Type (Derived_Type)
9819 or else Is_Tagged_Type (Derived_Type)
9820 then
9821 declare
9822 Iface : Entity_Id;
9823 Ifaces : Elist_Id;
9824 Iface_Elmt : Elmt_Id;
9825
9826 begin
9827 Collect_Interfaces
9828 (T => Derived_Type,
9829 Ifaces_List => Ifaces,
9830 Exclude_Parents => True);
9831
9832 if Present (Ifaces) then
9833 Iface_Elmt := First_Elmt (Ifaces);
9834 while Present (Iface_Elmt) loop
9835 Iface := Node (Iface_Elmt);
9836
9837 if Has_Inheritable_Invariants (Iface) then
9838 Set_Has_Inherited_Invariants (Derived_Type);
9839 exit;
9840 end if;
9841
9842 Next_Elmt (Iface_Elmt);
9843 end loop;
9844 end if;
9845 end;
9846 end if;
9847 end if;
9848
9849 -- We similarly inherit predicates. Note that for scalar derived types
9850 -- the predicate is inherited from the first subtype, and not from its
9851 -- (anonymous) base type.
9852
9853 if Has_Predicates (Parent_Type)
9854 or else Has_Predicates (First_Subtype (Parent_Type))
9855 then
9856 Set_Has_Predicates (Derived_Type);
9857 end if;
9858
9859 -- The derived type inherits representation clauses from the parent
9860 -- type, and from any interfaces.
9861
9862 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9863
9864 declare
9865 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9866 begin
9867 while Present (Iface) loop
9868 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9869 Next (Iface);
9870 end loop;
9871 end;
9872
9873 -- If the parent type has delayed rep aspects, then mark the derived
9874 -- type as possibly inheriting a delayed rep aspect.
9875
9876 if Has_Delayed_Rep_Aspects (Parent_Type) then
9877 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9878 end if;
9879
9880 -- A derived type becomes Ghost when its parent type is also Ghost
9881 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9882 -- directly inherited because the Ghost policy in effect may differ.
9883
9884 if Is_Ghost_Entity (Parent_Type) then
9885 Set_Is_Ghost_Entity (Derived_Type);
9886 end if;
9887
9888 -- Type dependent processing
9889
9890 case Ekind (Parent_Type) is
9891 when Numeric_Kind =>
9892 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9893
9894 when Array_Kind =>
9895 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9896
9897 when Class_Wide_Kind
9898 | E_Record_Subtype
9899 | E_Record_Type
9900 =>
9901 Build_Derived_Record_Type
9902 (N, Parent_Type, Derived_Type, Derive_Subps);
9903 return;
9904
9905 when Enumeration_Kind =>
9906 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9907
9908 when Access_Kind =>
9909 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9910
9911 when Incomplete_Or_Private_Kind =>
9912 Build_Derived_Private_Type
9913 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9914
9915 -- For discriminated types, the derivation includes deriving
9916 -- primitive operations. For others it is done below.
9917
9918 if Is_Tagged_Type (Parent_Type)
9919 or else Has_Discriminants (Parent_Type)
9920 or else (Present (Full_View (Parent_Type))
9921 and then Has_Discriminants (Full_View (Parent_Type)))
9922 then
9923 return;
9924 end if;
9925
9926 when Concurrent_Kind =>
9927 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9928
9929 when others =>
9930 raise Program_Error;
9931 end case;
9932
9933 -- Nothing more to do if some error occurred
9934
9935 if Etype (Derived_Type) = Any_Type then
9936 return;
9937 end if;
9938
9939 -- Set delayed freeze and then derive subprograms, we need to do this
9940 -- in this order so that derived subprograms inherit the derived freeze
9941 -- if necessary.
9942
9943 Set_Has_Delayed_Freeze (Derived_Type);
9944
9945 if Derive_Subps then
9946 Derive_Subprograms (Parent_Type, Derived_Type);
9947 end if;
9948
9949 Set_Has_Primitive_Operations
9950 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9951 end Build_Derived_Type;
9952
9953 -----------------------
9954 -- Build_Discriminal --
9955 -----------------------
9956
9957 procedure Build_Discriminal (Discrim : Entity_Id) is
9958 D_Minal : Entity_Id;
9959 CR_Disc : Entity_Id;
9960
9961 begin
9962 -- A discriminal has the same name as the discriminant
9963
9964 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9965
9966 Set_Ekind (D_Minal, E_In_Parameter);
9967 Set_Mechanism (D_Minal, Default_Mechanism);
9968 Set_Etype (D_Minal, Etype (Discrim));
9969 Set_Scope (D_Minal, Current_Scope);
9970 Set_Parent (D_Minal, Parent (Discrim));
9971
9972 Set_Discriminal (Discrim, D_Minal);
9973 Set_Discriminal_Link (D_Minal, Discrim);
9974
9975 -- For task types, build at once the discriminants of the corresponding
9976 -- record, which are needed if discriminants are used in entry defaults
9977 -- and in family bounds.
9978
9979 if Is_Concurrent_Type (Current_Scope)
9980 or else
9981 Is_Limited_Type (Current_Scope)
9982 then
9983 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9984
9985 Set_Ekind (CR_Disc, E_In_Parameter);
9986 Set_Mechanism (CR_Disc, Default_Mechanism);
9987 Set_Etype (CR_Disc, Etype (Discrim));
9988 Set_Scope (CR_Disc, Current_Scope);
9989 Set_Discriminal_Link (CR_Disc, Discrim);
9990 Set_CR_Discriminant (Discrim, CR_Disc);
9991 end if;
9992 end Build_Discriminal;
9993
9994 ------------------------------------
9995 -- Build_Discriminant_Constraints --
9996 ------------------------------------
9997
9998 function Build_Discriminant_Constraints
9999 (T : Entity_Id;
10000 Def : Node_Id;
10001 Derived_Def : Boolean := False) return Elist_Id
10002 is
10003 C : constant Node_Id := Constraint (Def);
10004 Nb_Discr : constant Nat := Number_Discriminants (T);
10005
10006 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10007 -- Saves the expression corresponding to a given discriminant in T
10008
10009 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10010 -- Return the Position number within array Discr_Expr of a discriminant
10011 -- D within the discriminant list of the discriminated type T.
10012
10013 procedure Process_Discriminant_Expression
10014 (Expr : Node_Id;
10015 D : Entity_Id);
10016 -- If this is a discriminant constraint on a partial view, do not
10017 -- generate an overflow check on the discriminant expression. The check
10018 -- will be generated when constraining the full view. Otherwise the
10019 -- backend creates duplicate symbols for the temporaries corresponding
10020 -- to the expressions to be checked, causing spurious assembler errors.
10021
10022 ------------------
10023 -- Pos_Of_Discr --
10024 ------------------
10025
10026 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10027 Disc : Entity_Id;
10028
10029 begin
10030 Disc := First_Discriminant (T);
10031 for J in Discr_Expr'Range loop
10032 if Disc = D then
10033 return J;
10034 end if;
10035
10036 Next_Discriminant (Disc);
10037 end loop;
10038
10039 -- Note: Since this function is called on discriminants that are
10040 -- known to belong to the discriminated type, falling through the
10041 -- loop with no match signals an internal compiler error.
10042
10043 raise Program_Error;
10044 end Pos_Of_Discr;
10045
10046 -------------------------------------
10047 -- Process_Discriminant_Expression --
10048 -------------------------------------
10049
10050 procedure Process_Discriminant_Expression
10051 (Expr : Node_Id;
10052 D : Entity_Id)
10053 is
10054 BDT : constant Entity_Id := Base_Type (Etype (D));
10055
10056 begin
10057 -- If this is a discriminant constraint on a partial view, do
10058 -- not generate an overflow on the discriminant expression. The
10059 -- check will be generated when constraining the full view.
10060
10061 if Is_Private_Type (T)
10062 and then Present (Full_View (T))
10063 then
10064 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10065 else
10066 Analyze_And_Resolve (Expr, BDT);
10067 end if;
10068 end Process_Discriminant_Expression;
10069
10070 -- Declarations local to Build_Discriminant_Constraints
10071
10072 Discr : Entity_Id;
10073 E : Entity_Id;
10074 Elist : constant Elist_Id := New_Elmt_List;
10075
10076 Constr : Node_Id;
10077 Expr : Node_Id;
10078 Id : Node_Id;
10079 Position : Nat;
10080 Found : Boolean;
10081
10082 Discrim_Present : Boolean := False;
10083
10084 -- Start of processing for Build_Discriminant_Constraints
10085
10086 begin
10087 -- The following loop will process positional associations only.
10088 -- For a positional association, the (single) discriminant is
10089 -- implicitly specified by position, in textual order (RM 3.7.2).
10090
10091 Discr := First_Discriminant (T);
10092 Constr := First (Constraints (C));
10093 for D in Discr_Expr'Range loop
10094 exit when Nkind (Constr) = N_Discriminant_Association;
10095
10096 if No (Constr) then
10097 Error_Msg_N ("too few discriminants given in constraint", C);
10098 return New_Elmt_List;
10099
10100 elsif Nkind (Constr) = N_Range
10101 or else (Nkind (Constr) = N_Attribute_Reference
10102 and then Attribute_Name (Constr) = Name_Range)
10103 then
10104 Error_Msg_N
10105 ("a range is not a valid discriminant constraint", Constr);
10106 Discr_Expr (D) := Error;
10107
10108 elsif Nkind (Constr) = N_Subtype_Indication then
10109 Error_Msg_N
10110 ("a subtype indication is not a valid discriminant constraint",
10111 Constr);
10112 Discr_Expr (D) := Error;
10113
10114 else
10115 Process_Discriminant_Expression (Constr, Discr);
10116 Discr_Expr (D) := Constr;
10117 end if;
10118
10119 Next_Discriminant (Discr);
10120 Next (Constr);
10121 end loop;
10122
10123 if No (Discr) and then Present (Constr) then
10124 Error_Msg_N ("too many discriminants given in constraint", Constr);
10125 return New_Elmt_List;
10126 end if;
10127
10128 -- Named associations can be given in any order, but if both positional
10129 -- and named associations are used in the same discriminant constraint,
10130 -- then positional associations must occur first, at their normal
10131 -- position. Hence once a named association is used, the rest of the
10132 -- discriminant constraint must use only named associations.
10133
10134 while Present (Constr) loop
10135
10136 -- Positional association forbidden after a named association
10137
10138 if Nkind (Constr) /= N_Discriminant_Association then
10139 Error_Msg_N ("positional association follows named one", Constr);
10140 return New_Elmt_List;
10141
10142 -- Otherwise it is a named association
10143
10144 else
10145 -- E records the type of the discriminants in the named
10146 -- association. All the discriminants specified in the same name
10147 -- association must have the same type.
10148
10149 E := Empty;
10150
10151 -- Search the list of discriminants in T to see if the simple name
10152 -- given in the constraint matches any of them.
10153
10154 Id := First (Selector_Names (Constr));
10155 while Present (Id) loop
10156 Found := False;
10157
10158 -- If Original_Discriminant is present, we are processing a
10159 -- generic instantiation and this is an instance node. We need
10160 -- to find the name of the corresponding discriminant in the
10161 -- actual record type T and not the name of the discriminant in
10162 -- the generic formal. Example:
10163
10164 -- generic
10165 -- type G (D : int) is private;
10166 -- package P is
10167 -- subtype W is G (D => 1);
10168 -- end package;
10169 -- type Rec (X : int) is record ... end record;
10170 -- package Q is new P (G => Rec);
10171
10172 -- At the point of the instantiation, formal type G is Rec
10173 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10174 -- which really looks like "subtype W is Rec (D => 1);" at
10175 -- the point of instantiation, we want to find the discriminant
10176 -- that corresponds to D in Rec, i.e. X.
10177
10178 if Present (Original_Discriminant (Id))
10179 and then In_Instance
10180 then
10181 Discr := Find_Corresponding_Discriminant (Id, T);
10182 Found := True;
10183
10184 else
10185 Discr := First_Discriminant (T);
10186 while Present (Discr) loop
10187 if Chars (Discr) = Chars (Id) then
10188 Found := True;
10189 exit;
10190 end if;
10191
10192 Next_Discriminant (Discr);
10193 end loop;
10194
10195 if not Found then
10196 Error_Msg_N ("& does not match any discriminant", Id);
10197 return New_Elmt_List;
10198
10199 -- If the parent type is a generic formal, preserve the
10200 -- name of the discriminant for subsequent instances.
10201 -- see comment at the beginning of this if statement.
10202
10203 elsif Is_Generic_Type (Root_Type (T)) then
10204 Set_Original_Discriminant (Id, Discr);
10205 end if;
10206 end if;
10207
10208 Position := Pos_Of_Discr (T, Discr);
10209
10210 if Present (Discr_Expr (Position)) then
10211 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10212
10213 else
10214 -- Each discriminant specified in the same named association
10215 -- must be associated with a separate copy of the
10216 -- corresponding expression.
10217
10218 if Present (Next (Id)) then
10219 Expr := New_Copy_Tree (Expression (Constr));
10220 Set_Parent (Expr, Parent (Expression (Constr)));
10221 else
10222 Expr := Expression (Constr);
10223 end if;
10224
10225 Discr_Expr (Position) := Expr;
10226 Process_Discriminant_Expression (Expr, Discr);
10227 end if;
10228
10229 -- A discriminant association with more than one discriminant
10230 -- name is only allowed if the named discriminants are all of
10231 -- the same type (RM 3.7.1(8)).
10232
10233 if E = Empty then
10234 E := Base_Type (Etype (Discr));
10235
10236 elsif Base_Type (Etype (Discr)) /= E then
10237 Error_Msg_N
10238 ("all discriminants in an association " &
10239 "must have the same type", Id);
10240 end if;
10241
10242 Next (Id);
10243 end loop;
10244 end if;
10245
10246 Next (Constr);
10247 end loop;
10248
10249 -- A discriminant constraint must provide exactly one value for each
10250 -- discriminant of the type (RM 3.7.1(8)).
10251
10252 for J in Discr_Expr'Range loop
10253 if No (Discr_Expr (J)) then
10254 Error_Msg_N ("too few discriminants given in constraint", C);
10255 return New_Elmt_List;
10256 end if;
10257 end loop;
10258
10259 -- Determine if there are discriminant expressions in the constraint
10260
10261 for J in Discr_Expr'Range loop
10262 if Denotes_Discriminant
10263 (Discr_Expr (J), Check_Concurrent => True)
10264 then
10265 Discrim_Present := True;
10266 end if;
10267 end loop;
10268
10269 -- Build an element list consisting of the expressions given in the
10270 -- discriminant constraint and apply the appropriate checks. The list
10271 -- is constructed after resolving any named discriminant associations
10272 -- and therefore the expressions appear in the textual order of the
10273 -- discriminants.
10274
10275 Discr := First_Discriminant (T);
10276 for J in Discr_Expr'Range loop
10277 if Discr_Expr (J) /= Error then
10278 Append_Elmt (Discr_Expr (J), Elist);
10279
10280 -- If any of the discriminant constraints is given by a
10281 -- discriminant and we are in a derived type declaration we
10282 -- have a discriminant renaming. Establish link between new
10283 -- and old discriminant. The new discriminant has an implicit
10284 -- dereference if the old one does.
10285
10286 if Denotes_Discriminant (Discr_Expr (J)) then
10287 if Derived_Def then
10288 declare
10289 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10290
10291 begin
10292 Set_Corresponding_Discriminant (New_Discr, Discr);
10293 Set_Has_Implicit_Dereference (New_Discr,
10294 Has_Implicit_Dereference (Discr));
10295 end;
10296 end if;
10297
10298 -- Force the evaluation of non-discriminant expressions.
10299 -- If we have found a discriminant in the constraint 3.4(26)
10300 -- and 3.8(18) demand that no range checks are performed are
10301 -- after evaluation. If the constraint is for a component
10302 -- definition that has a per-object constraint, expressions are
10303 -- evaluated but not checked either. In all other cases perform
10304 -- a range check.
10305
10306 else
10307 if Discrim_Present then
10308 null;
10309
10310 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10311 and then Has_Per_Object_Constraint
10312 (Defining_Identifier (Parent (Parent (Def))))
10313 then
10314 null;
10315
10316 elsif Is_Access_Type (Etype (Discr)) then
10317 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10318
10319 else
10320 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10321 end if;
10322
10323 Force_Evaluation (Discr_Expr (J));
10324 end if;
10325
10326 -- Check that the designated type of an access discriminant's
10327 -- expression is not a class-wide type unless the discriminant's
10328 -- designated type is also class-wide.
10329
10330 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10331 and then not Is_Class_Wide_Type
10332 (Designated_Type (Etype (Discr)))
10333 and then Etype (Discr_Expr (J)) /= Any_Type
10334 and then Is_Class_Wide_Type
10335 (Designated_Type (Etype (Discr_Expr (J))))
10336 then
10337 Wrong_Type (Discr_Expr (J), Etype (Discr));
10338
10339 elsif Is_Access_Type (Etype (Discr))
10340 and then not Is_Access_Constant (Etype (Discr))
10341 and then Is_Access_Type (Etype (Discr_Expr (J)))
10342 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10343 then
10344 Error_Msg_NE
10345 ("constraint for discriminant& must be access to variable",
10346 Def, Discr);
10347 end if;
10348 end if;
10349
10350 Next_Discriminant (Discr);
10351 end loop;
10352
10353 return Elist;
10354 end Build_Discriminant_Constraints;
10355
10356 ---------------------------------
10357 -- Build_Discriminated_Subtype --
10358 ---------------------------------
10359
10360 procedure Build_Discriminated_Subtype
10361 (T : Entity_Id;
10362 Def_Id : Entity_Id;
10363 Elist : Elist_Id;
10364 Related_Nod : Node_Id;
10365 For_Access : Boolean := False)
10366 is
10367 Has_Discrs : constant Boolean := Has_Discriminants (T);
10368 Constrained : constant Boolean :=
10369 (Has_Discrs
10370 and then not Is_Empty_Elmt_List (Elist)
10371 and then not Is_Class_Wide_Type (T))
10372 or else Is_Constrained (T);
10373
10374 begin
10375 if Ekind (T) = E_Record_Type then
10376 Set_Ekind (Def_Id, E_Record_Subtype);
10377
10378 -- Inherit preelaboration flag from base, for types for which it
10379 -- may have been set: records, private types, protected types.
10380
10381 Set_Known_To_Have_Preelab_Init
10382 (Def_Id, Known_To_Have_Preelab_Init (T));
10383
10384 elsif Ekind (T) = E_Task_Type then
10385 Set_Ekind (Def_Id, E_Task_Subtype);
10386
10387 elsif Ekind (T) = E_Protected_Type then
10388 Set_Ekind (Def_Id, E_Protected_Subtype);
10389 Set_Known_To_Have_Preelab_Init
10390 (Def_Id, Known_To_Have_Preelab_Init (T));
10391
10392 elsif Is_Private_Type (T) then
10393 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10394 Set_Known_To_Have_Preelab_Init
10395 (Def_Id, Known_To_Have_Preelab_Init (T));
10396
10397 -- Private subtypes may have private dependents
10398
10399 Set_Private_Dependents (Def_Id, New_Elmt_List);
10400
10401 elsif Is_Class_Wide_Type (T) then
10402 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10403
10404 else
10405 -- Incomplete type. Attach subtype to list of dependents, to be
10406 -- completed with full view of parent type, unless is it the
10407 -- designated subtype of a record component within an init_proc.
10408 -- This last case arises for a component of an access type whose
10409 -- designated type is incomplete (e.g. a Taft Amendment type).
10410 -- The designated subtype is within an inner scope, and needs no
10411 -- elaboration, because only the access type is needed in the
10412 -- initialization procedure.
10413
10414 if Ekind (T) = E_Incomplete_Type then
10415 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10416 else
10417 Set_Ekind (Def_Id, Ekind (T));
10418 end if;
10419
10420 if For_Access and then Within_Init_Proc then
10421 null;
10422 else
10423 Append_Elmt (Def_Id, Private_Dependents (T));
10424 end if;
10425 end if;
10426
10427 Set_Etype (Def_Id, T);
10428 Init_Size_Align (Def_Id);
10429 Set_Has_Discriminants (Def_Id, Has_Discrs);
10430 Set_Is_Constrained (Def_Id, Constrained);
10431
10432 Set_First_Entity (Def_Id, First_Entity (T));
10433 Set_Last_Entity (Def_Id, Last_Entity (T));
10434 Set_Has_Implicit_Dereference
10435 (Def_Id, Has_Implicit_Dereference (T));
10436 Set_Has_Pragma_Unreferenced_Objects
10437 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10438
10439 -- If the subtype is the completion of a private declaration, there may
10440 -- have been representation clauses for the partial view, and they must
10441 -- be preserved. Build_Derived_Type chains the inherited clauses with
10442 -- the ones appearing on the extension. If this comes from a subtype
10443 -- declaration, all clauses are inherited.
10444
10445 if No (First_Rep_Item (Def_Id)) then
10446 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10447 end if;
10448
10449 if Is_Tagged_Type (T) then
10450 Set_Is_Tagged_Type (Def_Id);
10451 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10452 Make_Class_Wide_Type (Def_Id);
10453 end if;
10454
10455 Set_Stored_Constraint (Def_Id, No_Elist);
10456
10457 if Has_Discrs then
10458 Set_Discriminant_Constraint (Def_Id, Elist);
10459 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10460 end if;
10461
10462 if Is_Tagged_Type (T) then
10463
10464 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10465 -- concurrent record type (which has the list of primitive
10466 -- operations).
10467
10468 if Ada_Version >= Ada_2005
10469 and then Is_Concurrent_Type (T)
10470 then
10471 Set_Corresponding_Record_Type (Def_Id,
10472 Corresponding_Record_Type (T));
10473 else
10474 Set_Direct_Primitive_Operations (Def_Id,
10475 Direct_Primitive_Operations (T));
10476 end if;
10477
10478 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10479 end if;
10480
10481 -- Subtypes introduced by component declarations do not need to be
10482 -- marked as delayed, and do not get freeze nodes, because the semantics
10483 -- verifies that the parents of the subtypes are frozen before the
10484 -- enclosing record is frozen.
10485
10486 if not Is_Type (Scope (Def_Id)) then
10487 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10488
10489 if Is_Private_Type (T)
10490 and then Present (Full_View (T))
10491 then
10492 Conditional_Delay (Def_Id, Full_View (T));
10493 else
10494 Conditional_Delay (Def_Id, T);
10495 end if;
10496 end if;
10497
10498 if Is_Record_Type (T) then
10499 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10500
10501 if Has_Discrs
10502 and then not Is_Empty_Elmt_List (Elist)
10503 and then not For_Access
10504 then
10505 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10506
10507 else
10508 Set_Cloned_Subtype (Def_Id, T);
10509 end if;
10510 end if;
10511 end Build_Discriminated_Subtype;
10512
10513 ---------------------------
10514 -- Build_Itype_Reference --
10515 ---------------------------
10516
10517 procedure Build_Itype_Reference
10518 (Ityp : Entity_Id;
10519 Nod : Node_Id)
10520 is
10521 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10522 begin
10523
10524 -- Itype references are only created for use by the back-end
10525
10526 if Inside_A_Generic then
10527 return;
10528 else
10529 Set_Itype (IR, Ityp);
10530
10531 -- If Nod is a library unit entity, then Insert_After won't work,
10532 -- because Nod is not a member of any list. Therefore, we use
10533 -- Add_Global_Declaration in this case. This can happen if we have a
10534 -- build-in-place library function, child unit or not.
10535
10536 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10537 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10538 N_Subprogram_Declaration)
10539 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10540 then
10541 Add_Global_Declaration (IR);
10542 else
10543 Insert_After (Nod, IR);
10544 end if;
10545 end if;
10546 end Build_Itype_Reference;
10547
10548 ------------------------
10549 -- Build_Scalar_Bound --
10550 ------------------------
10551
10552 function Build_Scalar_Bound
10553 (Bound : Node_Id;
10554 Par_T : Entity_Id;
10555 Der_T : Entity_Id) return Node_Id
10556 is
10557 New_Bound : Entity_Id;
10558
10559 begin
10560 -- Note: not clear why this is needed, how can the original bound
10561 -- be unanalyzed at this point? and if it is, what business do we
10562 -- have messing around with it? and why is the base type of the
10563 -- parent type the right type for the resolution. It probably is
10564 -- not. It is OK for the new bound we are creating, but not for
10565 -- the old one??? Still if it never happens, no problem.
10566
10567 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10568
10569 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10570 New_Bound := New_Copy (Bound);
10571 Set_Etype (New_Bound, Der_T);
10572 Set_Analyzed (New_Bound);
10573
10574 elsif Is_Entity_Name (Bound) then
10575 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10576
10577 -- The following is almost certainly wrong. What business do we have
10578 -- relocating a node (Bound) that is presumably still attached to
10579 -- the tree elsewhere???
10580
10581 else
10582 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10583 end if;
10584
10585 Set_Etype (New_Bound, Der_T);
10586 return New_Bound;
10587 end Build_Scalar_Bound;
10588
10589 -------------------------------
10590 -- Check_Abstract_Overriding --
10591 -------------------------------
10592
10593 procedure Check_Abstract_Overriding (T : Entity_Id) is
10594 Alias_Subp : Entity_Id;
10595 Elmt : Elmt_Id;
10596 Op_List : Elist_Id;
10597 Subp : Entity_Id;
10598 Type_Def : Node_Id;
10599
10600 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10601 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10602 -- which has pragma Implemented already set. Check whether Subp's entity
10603 -- kind conforms to the implementation kind of the overridden routine.
10604
10605 procedure Check_Pragma_Implemented
10606 (Subp : Entity_Id;
10607 Iface_Subp : Entity_Id);
10608 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10609 -- Iface_Subp and both entities have pragma Implemented already set on
10610 -- them. Check whether the two implementation kinds are conforming.
10611
10612 procedure Inherit_Pragma_Implemented
10613 (Subp : Entity_Id;
10614 Iface_Subp : Entity_Id);
10615 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10616 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10617 -- Propagate the implementation kind of Iface_Subp to Subp.
10618
10619 ------------------------------
10620 -- Check_Pragma_Implemented --
10621 ------------------------------
10622
10623 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10624 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10625 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10626 Subp_Alias : constant Entity_Id := Alias (Subp);
10627 Contr_Typ : Entity_Id;
10628 Impl_Subp : Entity_Id;
10629
10630 begin
10631 -- Subp must have an alias since it is a hidden entity used to link
10632 -- an interface subprogram to its overriding counterpart.
10633
10634 pragma Assert (Present (Subp_Alias));
10635
10636 -- Handle aliases to synchronized wrappers
10637
10638 Impl_Subp := Subp_Alias;
10639
10640 if Is_Primitive_Wrapper (Impl_Subp) then
10641 Impl_Subp := Wrapped_Entity (Impl_Subp);
10642 end if;
10643
10644 -- Extract the type of the controlling formal
10645
10646 Contr_Typ := Etype (First_Formal (Subp_Alias));
10647
10648 if Is_Concurrent_Record_Type (Contr_Typ) then
10649 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10650 end if;
10651
10652 -- An interface subprogram whose implementation kind is By_Entry must
10653 -- be implemented by an entry.
10654
10655 if Impl_Kind = Name_By_Entry
10656 and then Ekind (Impl_Subp) /= E_Entry
10657 then
10658 Error_Msg_Node_2 := Iface_Alias;
10659 Error_Msg_NE
10660 ("type & must implement abstract subprogram & with an entry",
10661 Subp_Alias, Contr_Typ);
10662
10663 elsif Impl_Kind = Name_By_Protected_Procedure then
10664
10665 -- An interface subprogram whose implementation kind is By_
10666 -- Protected_Procedure cannot be implemented by a primitive
10667 -- procedure of a task type.
10668
10669 if Ekind (Contr_Typ) /= E_Protected_Type then
10670 Error_Msg_Node_2 := Contr_Typ;
10671 Error_Msg_NE
10672 ("interface subprogram & cannot be implemented by a "
10673 & "primitive procedure of task type &",
10674 Subp_Alias, Iface_Alias);
10675
10676 -- An interface subprogram whose implementation kind is By_
10677 -- Protected_Procedure must be implemented by a procedure.
10678
10679 elsif Ekind (Impl_Subp) /= E_Procedure then
10680 Error_Msg_Node_2 := Iface_Alias;
10681 Error_Msg_NE
10682 ("type & must implement abstract subprogram & with a "
10683 & "procedure", Subp_Alias, Contr_Typ);
10684
10685 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10686 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10687 then
10688 Error_Msg_Name_1 := Impl_Kind;
10689 Error_Msg_N
10690 ("overriding operation& must have synchronization%",
10691 Subp_Alias);
10692 end if;
10693
10694 -- If primitive has Optional synchronization, overriding operation
10695 -- must match if it has an explicit synchronization.
10696
10697 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10698 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10699 then
10700 Error_Msg_Name_1 := Impl_Kind;
10701 Error_Msg_N
10702 ("overriding operation& must have synchronization%", Subp_Alias);
10703 end if;
10704 end Check_Pragma_Implemented;
10705
10706 ------------------------------
10707 -- Check_Pragma_Implemented --
10708 ------------------------------
10709
10710 procedure Check_Pragma_Implemented
10711 (Subp : Entity_Id;
10712 Iface_Subp : Entity_Id)
10713 is
10714 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10715 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10716
10717 begin
10718 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10719 -- and overriding subprogram are different. In general this is an
10720 -- error except when the implementation kind of the overridden
10721 -- subprograms is By_Any or Optional.
10722
10723 if Iface_Kind /= Subp_Kind
10724 and then Iface_Kind /= Name_By_Any
10725 and then Iface_Kind /= Name_Optional
10726 then
10727 if Iface_Kind = Name_By_Entry then
10728 Error_Msg_N
10729 ("incompatible implementation kind, overridden subprogram " &
10730 "is marked By_Entry", Subp);
10731 else
10732 Error_Msg_N
10733 ("incompatible implementation kind, overridden subprogram " &
10734 "is marked By_Protected_Procedure", Subp);
10735 end if;
10736 end if;
10737 end Check_Pragma_Implemented;
10738
10739 --------------------------------
10740 -- Inherit_Pragma_Implemented --
10741 --------------------------------
10742
10743 procedure Inherit_Pragma_Implemented
10744 (Subp : Entity_Id;
10745 Iface_Subp : Entity_Id)
10746 is
10747 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10748 Loc : constant Source_Ptr := Sloc (Subp);
10749 Impl_Prag : Node_Id;
10750
10751 begin
10752 -- Since the implementation kind is stored as a representation item
10753 -- rather than a flag, create a pragma node.
10754
10755 Impl_Prag :=
10756 Make_Pragma (Loc,
10757 Chars => Name_Implemented,
10758 Pragma_Argument_Associations => New_List (
10759 Make_Pragma_Argument_Association (Loc,
10760 Expression => New_Occurrence_Of (Subp, Loc)),
10761
10762 Make_Pragma_Argument_Association (Loc,
10763 Expression => Make_Identifier (Loc, Iface_Kind))));
10764
10765 -- The pragma doesn't need to be analyzed because it is internally
10766 -- built. It is safe to directly register it as a rep item since we
10767 -- are only interested in the characters of the implementation kind.
10768
10769 Record_Rep_Item (Subp, Impl_Prag);
10770 end Inherit_Pragma_Implemented;
10771
10772 -- Start of processing for Check_Abstract_Overriding
10773
10774 begin
10775 Op_List := Primitive_Operations (T);
10776
10777 -- Loop to check primitive operations
10778
10779 Elmt := First_Elmt (Op_List);
10780 while Present (Elmt) loop
10781 Subp := Node (Elmt);
10782 Alias_Subp := Alias (Subp);
10783
10784 -- Inherited subprograms are identified by the fact that they do not
10785 -- come from source, and the associated source location is the
10786 -- location of the first subtype of the derived type.
10787
10788 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10789 -- subprograms that "require overriding".
10790
10791 -- Special exception, do not complain about failure to override the
10792 -- stream routines _Input and _Output, as well as the primitive
10793 -- operations used in dispatching selects since we always provide
10794 -- automatic overridings for these subprograms.
10795
10796 -- The partial view of T may have been a private extension, for
10797 -- which inherited functions dispatching on result are abstract.
10798 -- If the full view is a null extension, there is no need for
10799 -- overriding in Ada 2005, but wrappers need to be built for them
10800 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10801
10802 if Is_Null_Extension (T)
10803 and then Has_Controlling_Result (Subp)
10804 and then Ada_Version >= Ada_2005
10805 and then Present (Alias_Subp)
10806 and then not Comes_From_Source (Subp)
10807 and then not Is_Abstract_Subprogram (Alias_Subp)
10808 and then not Is_Access_Type (Etype (Subp))
10809 then
10810 null;
10811
10812 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10813 -- processing because this check is done with the aliased
10814 -- entity
10815
10816 elsif Present (Interface_Alias (Subp)) then
10817 null;
10818
10819 elsif (Is_Abstract_Subprogram (Subp)
10820 or else Requires_Overriding (Subp)
10821 or else
10822 (Has_Controlling_Result (Subp)
10823 and then Present (Alias_Subp)
10824 and then not Comes_From_Source (Subp)
10825 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10826 and then not Is_TSS (Subp, TSS_Stream_Input)
10827 and then not Is_TSS (Subp, TSS_Stream_Output)
10828 and then not Is_Abstract_Type (T)
10829 and then not Is_Predefined_Interface_Primitive (Subp)
10830
10831 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10832 -- with abstract interface types because the check will be done
10833 -- with the aliased entity (otherwise we generate a duplicated
10834 -- error message).
10835
10836 and then not Present (Interface_Alias (Subp))
10837 then
10838 if Present (Alias_Subp) then
10839
10840 -- Only perform the check for a derived subprogram when the
10841 -- type has an explicit record extension. This avoids incorrect
10842 -- flagging of abstract subprograms for the case of a type
10843 -- without an extension that is derived from a formal type
10844 -- with a tagged actual (can occur within a private part).
10845
10846 -- Ada 2005 (AI-391): In the case of an inherited function with
10847 -- a controlling result of the type, the rule does not apply if
10848 -- the type is a null extension (unless the parent function
10849 -- itself is abstract, in which case the function must still be
10850 -- be overridden). The expander will generate an overriding
10851 -- wrapper function calling the parent subprogram (see
10852 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10853
10854 Type_Def := Type_Definition (Parent (T));
10855
10856 if Nkind (Type_Def) = N_Derived_Type_Definition
10857 and then Present (Record_Extension_Part (Type_Def))
10858 and then
10859 (Ada_Version < Ada_2005
10860 or else not Is_Null_Extension (T)
10861 or else Ekind (Subp) = E_Procedure
10862 or else not Has_Controlling_Result (Subp)
10863 or else Is_Abstract_Subprogram (Alias_Subp)
10864 or else Requires_Overriding (Subp)
10865 or else Is_Access_Type (Etype (Subp)))
10866 then
10867 -- Avoid reporting error in case of abstract predefined
10868 -- primitive inherited from interface type because the
10869 -- body of internally generated predefined primitives
10870 -- of tagged types are generated later by Freeze_Type
10871
10872 if Is_Interface (Root_Type (T))
10873 and then Is_Abstract_Subprogram (Subp)
10874 and then Is_Predefined_Dispatching_Operation (Subp)
10875 and then not Comes_From_Source (Ultimate_Alias (Subp))
10876 then
10877 null;
10878
10879 -- A null extension is not obliged to override an inherited
10880 -- procedure subject to pragma Extensions_Visible with value
10881 -- False and at least one controlling OUT parameter
10882 -- (SPARK RM 6.1.7(6)).
10883
10884 elsif Is_Null_Extension (T)
10885 and then Is_EVF_Procedure (Subp)
10886 then
10887 null;
10888
10889 else
10890 Error_Msg_NE
10891 ("type must be declared abstract or & overridden",
10892 T, Subp);
10893
10894 -- Traverse the whole chain of aliased subprograms to
10895 -- complete the error notification. This is especially
10896 -- useful for traceability of the chain of entities when
10897 -- the subprogram corresponds with an interface
10898 -- subprogram (which may be defined in another package).
10899
10900 if Present (Alias_Subp) then
10901 declare
10902 E : Entity_Id;
10903
10904 begin
10905 E := Subp;
10906 while Present (Alias (E)) loop
10907
10908 -- Avoid reporting redundant errors on entities
10909 -- inherited from interfaces
10910
10911 if Sloc (E) /= Sloc (T) then
10912 Error_Msg_Sloc := Sloc (E);
10913 Error_Msg_NE
10914 ("\& has been inherited #", T, Subp);
10915 end if;
10916
10917 E := Alias (E);
10918 end loop;
10919
10920 Error_Msg_Sloc := Sloc (E);
10921
10922 -- AI05-0068: report if there is an overriding
10923 -- non-abstract subprogram that is invisible.
10924
10925 if Is_Hidden (E)
10926 and then not Is_Abstract_Subprogram (E)
10927 then
10928 Error_Msg_NE
10929 ("\& subprogram# is not visible",
10930 T, Subp);
10931
10932 -- Clarify the case where a non-null extension must
10933 -- override inherited procedure subject to pragma
10934 -- Extensions_Visible with value False and at least
10935 -- one controlling OUT param.
10936
10937 elsif Is_EVF_Procedure (E) then
10938 Error_Msg_NE
10939 ("\& # is subject to Extensions_Visible False",
10940 T, Subp);
10941
10942 else
10943 Error_Msg_NE
10944 ("\& has been inherited from subprogram #",
10945 T, Subp);
10946 end if;
10947 end;
10948 end if;
10949 end if;
10950
10951 -- Ada 2005 (AI-345): Protected or task type implementing
10952 -- abstract interfaces.
10953
10954 elsif Is_Concurrent_Record_Type (T)
10955 and then Present (Interfaces (T))
10956 then
10957 -- There is no need to check here RM 9.4(11.9/3) since we
10958 -- are processing the corresponding record type and the
10959 -- mode of the overriding subprograms was verified by
10960 -- Check_Conformance when the corresponding concurrent
10961 -- type declaration was analyzed.
10962
10963 Error_Msg_NE
10964 ("interface subprogram & must be overridden", T, Subp);
10965
10966 -- Examine primitive operations of synchronized type to find
10967 -- homonyms that have the wrong profile.
10968
10969 declare
10970 Prim : Entity_Id;
10971
10972 begin
10973 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10974 while Present (Prim) loop
10975 if Chars (Prim) = Chars (Subp) then
10976 Error_Msg_NE
10977 ("profile is not type conformant with prefixed "
10978 & "view profile of inherited operation&",
10979 Prim, Subp);
10980 end if;
10981
10982 Next_Entity (Prim);
10983 end loop;
10984 end;
10985 end if;
10986
10987 else
10988 Error_Msg_Node_2 := T;
10989 Error_Msg_N
10990 ("abstract subprogram& not allowed for type&", Subp);
10991
10992 -- Also post unconditional warning on the type (unconditional
10993 -- so that if there are more than one of these cases, we get
10994 -- them all, and not just the first one).
10995
10996 Error_Msg_Node_2 := Subp;
10997 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10998 end if;
10999
11000 -- A subprogram subject to pragma Extensions_Visible with value
11001 -- "True" cannot override a subprogram subject to the same pragma
11002 -- with value "False" (SPARK RM 6.1.7(5)).
11003
11004 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11005 and then Present (Overridden_Operation (Subp))
11006 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11007 Extensions_Visible_False
11008 then
11009 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11010 Error_Msg_N
11011 ("subprogram & with Extensions_Visible True cannot override "
11012 & "subprogram # with Extensions_Visible False", Subp);
11013 end if;
11014
11015 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11016
11017 -- Subp is an expander-generated procedure which maps an interface
11018 -- alias to a protected wrapper. The interface alias is flagged by
11019 -- pragma Implemented. Ensure that Subp is a procedure when the
11020 -- implementation kind is By_Protected_Procedure or an entry when
11021 -- By_Entry.
11022
11023 if Ada_Version >= Ada_2012
11024 and then Is_Hidden (Subp)
11025 and then Present (Interface_Alias (Subp))
11026 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11027 then
11028 Check_Pragma_Implemented (Subp);
11029 end if;
11030
11031 -- Subp is an interface primitive which overrides another interface
11032 -- primitive marked with pragma Implemented.
11033
11034 if Ada_Version >= Ada_2012
11035 and then Present (Overridden_Operation (Subp))
11036 and then Has_Rep_Pragma
11037 (Overridden_Operation (Subp), Name_Implemented)
11038 then
11039 -- If the overriding routine is also marked by Implemented, check
11040 -- that the two implementation kinds are conforming.
11041
11042 if Has_Rep_Pragma (Subp, Name_Implemented) then
11043 Check_Pragma_Implemented
11044 (Subp => Subp,
11045 Iface_Subp => Overridden_Operation (Subp));
11046
11047 -- Otherwise the overriding routine inherits the implementation
11048 -- kind from the overridden subprogram.
11049
11050 else
11051 Inherit_Pragma_Implemented
11052 (Subp => Subp,
11053 Iface_Subp => Overridden_Operation (Subp));
11054 end if;
11055 end if;
11056
11057 -- If the operation is a wrapper for a synchronized primitive, it
11058 -- may be called indirectly through a dispatching select. We assume
11059 -- that it will be referenced elsewhere indirectly, and suppress
11060 -- warnings about an unused entity.
11061
11062 if Is_Primitive_Wrapper (Subp)
11063 and then Present (Wrapped_Entity (Subp))
11064 then
11065 Set_Referenced (Wrapped_Entity (Subp));
11066 end if;
11067
11068 Next_Elmt (Elmt);
11069 end loop;
11070 end Check_Abstract_Overriding;
11071
11072 ------------------------------------------------
11073 -- Check_Access_Discriminant_Requires_Limited --
11074 ------------------------------------------------
11075
11076 procedure Check_Access_Discriminant_Requires_Limited
11077 (D : Node_Id;
11078 Loc : Node_Id)
11079 is
11080 begin
11081 -- A discriminant_specification for an access discriminant shall appear
11082 -- only in the declaration for a task or protected type, or for a type
11083 -- with the reserved word 'limited' in its definition or in one of its
11084 -- ancestors (RM 3.7(10)).
11085
11086 -- AI-0063: The proper condition is that type must be immutably limited,
11087 -- or else be a partial view.
11088
11089 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11090 if Is_Limited_View (Current_Scope)
11091 or else
11092 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11093 and then Limited_Present (Parent (Current_Scope)))
11094 then
11095 null;
11096
11097 else
11098 Error_Msg_N
11099 ("access discriminants allowed only for limited types", Loc);
11100 end if;
11101 end if;
11102 end Check_Access_Discriminant_Requires_Limited;
11103
11104 -----------------------------------
11105 -- Check_Aliased_Component_Types --
11106 -----------------------------------
11107
11108 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11109 C : Entity_Id;
11110
11111 begin
11112 -- ??? Also need to check components of record extensions, but not
11113 -- components of protected types (which are always limited).
11114
11115 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11116 -- types to be unconstrained. This is safe because it is illegal to
11117 -- create access subtypes to such types with explicit discriminant
11118 -- constraints.
11119
11120 if not Is_Limited_Type (T) then
11121 if Ekind (T) = E_Record_Type then
11122 C := First_Component (T);
11123 while Present (C) loop
11124 if Is_Aliased (C)
11125 and then Has_Discriminants (Etype (C))
11126 and then not Is_Constrained (Etype (C))
11127 and then not In_Instance_Body
11128 and then Ada_Version < Ada_2005
11129 then
11130 Error_Msg_N
11131 ("aliased component must be constrained (RM 3.6(11))",
11132 C);
11133 end if;
11134
11135 Next_Component (C);
11136 end loop;
11137
11138 elsif Ekind (T) = E_Array_Type then
11139 if Has_Aliased_Components (T)
11140 and then Has_Discriminants (Component_Type (T))
11141 and then not Is_Constrained (Component_Type (T))
11142 and then not In_Instance_Body
11143 and then Ada_Version < Ada_2005
11144 then
11145 Error_Msg_N
11146 ("aliased component type must be constrained (RM 3.6(11))",
11147 T);
11148 end if;
11149 end if;
11150 end if;
11151 end Check_Aliased_Component_Types;
11152
11153 ---------------------------------------
11154 -- Check_Anonymous_Access_Components --
11155 ---------------------------------------
11156
11157 procedure Check_Anonymous_Access_Components
11158 (Typ_Decl : Node_Id;
11159 Typ : Entity_Id;
11160 Prev : Entity_Id;
11161 Comp_List : Node_Id)
11162 is
11163 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11164 Anon_Access : Entity_Id;
11165 Acc_Def : Node_Id;
11166 Comp : Node_Id;
11167 Comp_Def : Node_Id;
11168 Decl : Node_Id;
11169 Type_Def : Node_Id;
11170
11171 procedure Build_Incomplete_Type_Declaration;
11172 -- If the record type contains components that include an access to the
11173 -- current record, then create an incomplete type declaration for the
11174 -- record, to be used as the designated type of the anonymous access.
11175 -- This is done only once, and only if there is no previous partial
11176 -- view of the type.
11177
11178 function Designates_T (Subt : Node_Id) return Boolean;
11179 -- Check whether a node designates the enclosing record type, or 'Class
11180 -- of that type
11181
11182 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11183 -- Check whether an access definition includes a reference to
11184 -- the enclosing record type. The reference can be a subtype mark
11185 -- in the access definition itself, a 'Class attribute reference, or
11186 -- recursively a reference appearing in a parameter specification
11187 -- or result definition of an access_to_subprogram definition.
11188
11189 --------------------------------------
11190 -- Build_Incomplete_Type_Declaration --
11191 --------------------------------------
11192
11193 procedure Build_Incomplete_Type_Declaration is
11194 Decl : Node_Id;
11195 Inc_T : Entity_Id;
11196 H : Entity_Id;
11197
11198 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11199 -- it's "is new ... with record" or else "is tagged record ...".
11200
11201 Is_Tagged : constant Boolean :=
11202 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11203 and then
11204 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11205 or else
11206 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11207 and then Tagged_Present (Type_Definition (Typ_Decl)));
11208
11209 begin
11210 -- If there is a previous partial view, no need to create a new one
11211 -- If the partial view, given by Prev, is incomplete, If Prev is
11212 -- a private declaration, full declaration is flagged accordingly.
11213
11214 if Prev /= Typ then
11215 if Is_Tagged then
11216 Make_Class_Wide_Type (Prev);
11217 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11218 Set_Etype (Class_Wide_Type (Typ), Typ);
11219 end if;
11220
11221 return;
11222
11223 elsif Has_Private_Declaration (Typ) then
11224
11225 -- If we refer to T'Class inside T, and T is the completion of a
11226 -- private type, then make sure the class-wide type exists.
11227
11228 if Is_Tagged then
11229 Make_Class_Wide_Type (Typ);
11230 end if;
11231
11232 return;
11233
11234 -- If there was a previous anonymous access type, the incomplete
11235 -- type declaration will have been created already.
11236
11237 elsif Present (Current_Entity (Typ))
11238 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11239 and then Full_View (Current_Entity (Typ)) = Typ
11240 then
11241 if Is_Tagged
11242 and then Comes_From_Source (Current_Entity (Typ))
11243 and then not Is_Tagged_Type (Current_Entity (Typ))
11244 then
11245 Make_Class_Wide_Type (Typ);
11246 Error_Msg_N
11247 ("incomplete view of tagged type should be declared tagged??",
11248 Parent (Current_Entity (Typ)));
11249 end if;
11250 return;
11251
11252 else
11253 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11254 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11255
11256 -- Type has already been inserted into the current scope. Remove
11257 -- it, and add incomplete declaration for type, so that subsequent
11258 -- anonymous access types can use it. The entity is unchained from
11259 -- the homonym list and from immediate visibility. After analysis,
11260 -- the entity in the incomplete declaration becomes immediately
11261 -- visible in the record declaration that follows.
11262
11263 H := Current_Entity (Typ);
11264
11265 if H = Typ then
11266 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11267 else
11268 while Present (H)
11269 and then Homonym (H) /= Typ
11270 loop
11271 H := Homonym (Typ);
11272 end loop;
11273
11274 Set_Homonym (H, Homonym (Typ));
11275 end if;
11276
11277 Insert_Before (Typ_Decl, Decl);
11278 Analyze (Decl);
11279 Set_Full_View (Inc_T, Typ);
11280
11281 if Is_Tagged then
11282
11283 -- Create a common class-wide type for both views, and set the
11284 -- Etype of the class-wide type to the full view.
11285
11286 Make_Class_Wide_Type (Inc_T);
11287 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11288 Set_Etype (Class_Wide_Type (Typ), Typ);
11289 end if;
11290 end if;
11291 end Build_Incomplete_Type_Declaration;
11292
11293 ------------------
11294 -- Designates_T --
11295 ------------------
11296
11297 function Designates_T (Subt : Node_Id) return Boolean is
11298 Type_Id : constant Name_Id := Chars (Typ);
11299
11300 function Names_T (Nam : Node_Id) return Boolean;
11301 -- The record type has not been introduced in the current scope
11302 -- yet, so we must examine the name of the type itself, either
11303 -- an identifier T, or an expanded name of the form P.T, where
11304 -- P denotes the current scope.
11305
11306 -------------
11307 -- Names_T --
11308 -------------
11309
11310 function Names_T (Nam : Node_Id) return Boolean is
11311 begin
11312 if Nkind (Nam) = N_Identifier then
11313 return Chars (Nam) = Type_Id;
11314
11315 elsif Nkind (Nam) = N_Selected_Component then
11316 if Chars (Selector_Name (Nam)) = Type_Id then
11317 if Nkind (Prefix (Nam)) = N_Identifier then
11318 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11319
11320 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11321 return Chars (Selector_Name (Prefix (Nam))) =
11322 Chars (Current_Scope);
11323 else
11324 return False;
11325 end if;
11326
11327 else
11328 return False;
11329 end if;
11330
11331 else
11332 return False;
11333 end if;
11334 end Names_T;
11335
11336 -- Start of processing for Designates_T
11337
11338 begin
11339 if Nkind (Subt) = N_Identifier then
11340 return Chars (Subt) = Type_Id;
11341
11342 -- Reference can be through an expanded name which has not been
11343 -- analyzed yet, and which designates enclosing scopes.
11344
11345 elsif Nkind (Subt) = N_Selected_Component then
11346 if Names_T (Subt) then
11347 return True;
11348
11349 -- Otherwise it must denote an entity that is already visible.
11350 -- The access definition may name a subtype of the enclosing
11351 -- type, if there is a previous incomplete declaration for it.
11352
11353 else
11354 Find_Selected_Component (Subt);
11355 return
11356 Is_Entity_Name (Subt)
11357 and then Scope (Entity (Subt)) = Current_Scope
11358 and then
11359 (Chars (Base_Type (Entity (Subt))) = Type_Id
11360 or else
11361 (Is_Class_Wide_Type (Entity (Subt))
11362 and then
11363 Chars (Etype (Base_Type (Entity (Subt)))) =
11364 Type_Id));
11365 end if;
11366
11367 -- A reference to the current type may appear as the prefix of
11368 -- a 'Class attribute.
11369
11370 elsif Nkind (Subt) = N_Attribute_Reference
11371 and then Attribute_Name (Subt) = Name_Class
11372 then
11373 return Names_T (Prefix (Subt));
11374
11375 else
11376 return False;
11377 end if;
11378 end Designates_T;
11379
11380 ----------------
11381 -- Mentions_T --
11382 ----------------
11383
11384 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11385 Param_Spec : Node_Id;
11386
11387 Acc_Subprg : constant Node_Id :=
11388 Access_To_Subprogram_Definition (Acc_Def);
11389
11390 begin
11391 if No (Acc_Subprg) then
11392 return Designates_T (Subtype_Mark (Acc_Def));
11393 end if;
11394
11395 -- Component is an access_to_subprogram: examine its formals,
11396 -- and result definition in the case of an access_to_function.
11397
11398 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11399 while Present (Param_Spec) loop
11400 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11401 and then Mentions_T (Parameter_Type (Param_Spec))
11402 then
11403 return True;
11404
11405 elsif Designates_T (Parameter_Type (Param_Spec)) then
11406 return True;
11407 end if;
11408
11409 Next (Param_Spec);
11410 end loop;
11411
11412 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11413 if Nkind (Result_Definition (Acc_Subprg)) =
11414 N_Access_Definition
11415 then
11416 return Mentions_T (Result_Definition (Acc_Subprg));
11417 else
11418 return Designates_T (Result_Definition (Acc_Subprg));
11419 end if;
11420 end if;
11421
11422 return False;
11423 end Mentions_T;
11424
11425 -- Start of processing for Check_Anonymous_Access_Components
11426
11427 begin
11428 if No (Comp_List) then
11429 return;
11430 end if;
11431
11432 Comp := First (Component_Items (Comp_List));
11433 while Present (Comp) loop
11434 if Nkind (Comp) = N_Component_Declaration
11435 and then Present
11436 (Access_Definition (Component_Definition (Comp)))
11437 and then
11438 Mentions_T (Access_Definition (Component_Definition (Comp)))
11439 then
11440 Comp_Def := Component_Definition (Comp);
11441 Acc_Def :=
11442 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11443
11444 Build_Incomplete_Type_Declaration;
11445 Anon_Access := Make_Temporary (Loc, 'S');
11446
11447 -- Create a declaration for the anonymous access type: either
11448 -- an access_to_object or an access_to_subprogram.
11449
11450 if Present (Acc_Def) then
11451 if Nkind (Acc_Def) = N_Access_Function_Definition then
11452 Type_Def :=
11453 Make_Access_Function_Definition (Loc,
11454 Parameter_Specifications =>
11455 Parameter_Specifications (Acc_Def),
11456 Result_Definition => Result_Definition (Acc_Def));
11457 else
11458 Type_Def :=
11459 Make_Access_Procedure_Definition (Loc,
11460 Parameter_Specifications =>
11461 Parameter_Specifications (Acc_Def));
11462 end if;
11463
11464 else
11465 Type_Def :=
11466 Make_Access_To_Object_Definition (Loc,
11467 Subtype_Indication =>
11468 Relocate_Node
11469 (Subtype_Mark (Access_Definition (Comp_Def))));
11470
11471 Set_Constant_Present
11472 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11473 Set_All_Present
11474 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11475 end if;
11476
11477 Set_Null_Exclusion_Present
11478 (Type_Def,
11479 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11480
11481 Decl :=
11482 Make_Full_Type_Declaration (Loc,
11483 Defining_Identifier => Anon_Access,
11484 Type_Definition => Type_Def);
11485
11486 Insert_Before (Typ_Decl, Decl);
11487 Analyze (Decl);
11488
11489 -- If an access to subprogram, create the extra formals
11490
11491 if Present (Acc_Def) then
11492 Create_Extra_Formals (Designated_Type (Anon_Access));
11493
11494 -- If an access to object, preserve entity of designated type,
11495 -- for ASIS use, before rewriting the component definition.
11496
11497 else
11498 declare
11499 Desig : Entity_Id;
11500
11501 begin
11502 Desig := Entity (Subtype_Indication (Type_Def));
11503
11504 -- If the access definition is to the current record,
11505 -- the visible entity at this point is an incomplete
11506 -- type. Retrieve the full view to simplify ASIS queries
11507
11508 if Ekind (Desig) = E_Incomplete_Type then
11509 Desig := Full_View (Desig);
11510 end if;
11511
11512 Set_Entity
11513 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11514 end;
11515 end if;
11516
11517 Rewrite (Comp_Def,
11518 Make_Component_Definition (Loc,
11519 Subtype_Indication =>
11520 New_Occurrence_Of (Anon_Access, Loc)));
11521
11522 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11523 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11524 else
11525 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11526 end if;
11527
11528 Set_Is_Local_Anonymous_Access (Anon_Access);
11529 end if;
11530
11531 Next (Comp);
11532 end loop;
11533
11534 if Present (Variant_Part (Comp_List)) then
11535 declare
11536 V : Node_Id;
11537 begin
11538 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11539 while Present (V) loop
11540 Check_Anonymous_Access_Components
11541 (Typ_Decl, Typ, Prev, Component_List (V));
11542 Next_Non_Pragma (V);
11543 end loop;
11544 end;
11545 end if;
11546 end Check_Anonymous_Access_Components;
11547
11548 ----------------------
11549 -- Check_Completion --
11550 ----------------------
11551
11552 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11553 E : Entity_Id;
11554
11555 procedure Post_Error;
11556 -- Post error message for lack of completion for entity E
11557
11558 ----------------
11559 -- Post_Error --
11560 ----------------
11561
11562 procedure Post_Error is
11563 procedure Missing_Body;
11564 -- Output missing body message
11565
11566 ------------------
11567 -- Missing_Body --
11568 ------------------
11569
11570 procedure Missing_Body is
11571 begin
11572 -- Spec is in same unit, so we can post on spec
11573
11574 if In_Same_Source_Unit (Body_Id, E) then
11575 Error_Msg_N ("missing body for &", E);
11576
11577 -- Spec is in a separate unit, so we have to post on the body
11578
11579 else
11580 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11581 end if;
11582 end Missing_Body;
11583
11584 -- Start of processing for Post_Error
11585
11586 begin
11587 if not Comes_From_Source (E) then
11588 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11589
11590 -- It may be an anonymous protected type created for a
11591 -- single variable. Post error on variable, if present.
11592
11593 declare
11594 Var : Entity_Id;
11595
11596 begin
11597 Var := First_Entity (Current_Scope);
11598 while Present (Var) loop
11599 exit when Etype (Var) = E
11600 and then Comes_From_Source (Var);
11601
11602 Next_Entity (Var);
11603 end loop;
11604
11605 if Present (Var) then
11606 E := Var;
11607 end if;
11608 end;
11609 end if;
11610 end if;
11611
11612 -- If a generated entity has no completion, then either previous
11613 -- semantic errors have disabled the expansion phase, or else we had
11614 -- missing subunits, or else we are compiling without expansion,
11615 -- or else something is very wrong.
11616
11617 if not Comes_From_Source (E) then
11618 pragma Assert
11619 (Serious_Errors_Detected > 0
11620 or else Configurable_Run_Time_Violations > 0
11621 or else Subunits_Missing
11622 or else not Expander_Active);
11623 return;
11624
11625 -- Here for source entity
11626
11627 else
11628 -- Here if no body to post the error message, so we post the error
11629 -- on the declaration that has no completion. This is not really
11630 -- the right place to post it, think about this later ???
11631
11632 if No (Body_Id) then
11633 if Is_Type (E) then
11634 Error_Msg_NE
11635 ("missing full declaration for }", Parent (E), E);
11636 else
11637 Error_Msg_NE ("missing body for &", Parent (E), E);
11638 end if;
11639
11640 -- Package body has no completion for a declaration that appears
11641 -- in the corresponding spec. Post error on the body, with a
11642 -- reference to the non-completed declaration.
11643
11644 else
11645 Error_Msg_Sloc := Sloc (E);
11646
11647 if Is_Type (E) then
11648 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11649
11650 elsif Is_Overloadable (E)
11651 and then Current_Entity_In_Scope (E) /= E
11652 then
11653 -- It may be that the completion is mistyped and appears as
11654 -- a distinct overloading of the entity.
11655
11656 declare
11657 Candidate : constant Entity_Id :=
11658 Current_Entity_In_Scope (E);
11659 Decl : constant Node_Id :=
11660 Unit_Declaration_Node (Candidate);
11661
11662 begin
11663 if Is_Overloadable (Candidate)
11664 and then Ekind (Candidate) = Ekind (E)
11665 and then Nkind (Decl) = N_Subprogram_Body
11666 and then Acts_As_Spec (Decl)
11667 then
11668 Check_Type_Conformant (Candidate, E);
11669
11670 else
11671 Missing_Body;
11672 end if;
11673 end;
11674
11675 else
11676 Missing_Body;
11677 end if;
11678 end if;
11679 end if;
11680 end Post_Error;
11681
11682 -- Local variables
11683
11684 Pack_Id : constant Entity_Id := Current_Scope;
11685
11686 -- Start of processing for Check_Completion
11687
11688 begin
11689 E := First_Entity (Pack_Id);
11690 while Present (E) loop
11691 if Is_Intrinsic_Subprogram (E) then
11692 null;
11693
11694 -- The following situation requires special handling: a child unit
11695 -- that appears in the context clause of the body of its parent:
11696
11697 -- procedure Parent.Child (...);
11698
11699 -- with Parent.Child;
11700 -- package body Parent is
11701
11702 -- Here Parent.Child appears as a local entity, but should not be
11703 -- flagged as requiring completion, because it is a compilation
11704 -- unit.
11705
11706 -- Ignore missing completion for a subprogram that does not come from
11707 -- source (including the _Call primitive operation of RAS types,
11708 -- which has to have the flag Comes_From_Source for other purposes):
11709 -- we assume that the expander will provide the missing completion.
11710 -- In case of previous errors, other expansion actions that provide
11711 -- bodies for null procedures with not be invoked, so inhibit message
11712 -- in those cases.
11713
11714 -- Note that E_Operator is not in the list that follows, because
11715 -- this kind is reserved for predefined operators, that are
11716 -- intrinsic and do not need completion.
11717
11718 elsif Ekind_In (E, E_Function,
11719 E_Procedure,
11720 E_Generic_Function,
11721 E_Generic_Procedure)
11722 then
11723 if Has_Completion (E) then
11724 null;
11725
11726 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11727 null;
11728
11729 elsif Is_Subprogram (E)
11730 and then (not Comes_From_Source (E)
11731 or else Chars (E) = Name_uCall)
11732 then
11733 null;
11734
11735 elsif
11736 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11737 then
11738 null;
11739
11740 elsif Nkind (Parent (E)) = N_Procedure_Specification
11741 and then Null_Present (Parent (E))
11742 and then Serious_Errors_Detected > 0
11743 then
11744 null;
11745
11746 else
11747 Post_Error;
11748 end if;
11749
11750 elsif Is_Entry (E) then
11751 if not Has_Completion (E) and then
11752 (Ekind (Scope (E)) = E_Protected_Object
11753 or else Ekind (Scope (E)) = E_Protected_Type)
11754 then
11755 Post_Error;
11756 end if;
11757
11758 elsif Is_Package_Or_Generic_Package (E) then
11759 if Unit_Requires_Body (E) then
11760 if not Has_Completion (E)
11761 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11762 N_Compilation_Unit
11763 then
11764 Post_Error;
11765 end if;
11766
11767 elsif not Is_Child_Unit (E) then
11768 May_Need_Implicit_Body (E);
11769 end if;
11770
11771 -- A formal incomplete type (Ada 2012) does not require a completion;
11772 -- other incomplete type declarations do.
11773
11774 elsif Ekind (E) = E_Incomplete_Type
11775 and then No (Underlying_Type (E))
11776 and then not Is_Generic_Type (E)
11777 then
11778 Post_Error;
11779
11780 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11781 and then not Has_Completion (E)
11782 then
11783 Post_Error;
11784
11785 -- A single task declared in the current scope is a constant, verify
11786 -- that the body of its anonymous type is in the same scope. If the
11787 -- task is defined elsewhere, this may be a renaming declaration for
11788 -- which no completion is needed.
11789
11790 elsif Ekind (E) = E_Constant
11791 and then Ekind (Etype (E)) = E_Task_Type
11792 and then not Has_Completion (Etype (E))
11793 and then Scope (Etype (E)) = Current_Scope
11794 then
11795 Post_Error;
11796
11797 elsif Ekind (E) = E_Protected_Object
11798 and then not Has_Completion (Etype (E))
11799 then
11800 Post_Error;
11801
11802 elsif Ekind (E) = E_Record_Type then
11803 if Is_Tagged_Type (E) then
11804 Check_Abstract_Overriding (E);
11805 Check_Conventions (E);
11806 end if;
11807
11808 Check_Aliased_Component_Types (E);
11809
11810 elsif Ekind (E) = E_Array_Type then
11811 Check_Aliased_Component_Types (E);
11812
11813 end if;
11814
11815 Next_Entity (E);
11816 end loop;
11817 end Check_Completion;
11818
11819 ------------------------------------
11820 -- Check_CPP_Type_Has_No_Defaults --
11821 ------------------------------------
11822
11823 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11824 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11825 Clist : Node_Id;
11826 Comp : Node_Id;
11827
11828 begin
11829 -- Obtain the component list
11830
11831 if Nkind (Tdef) = N_Record_Definition then
11832 Clist := Component_List (Tdef);
11833 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11834 Clist := Component_List (Record_Extension_Part (Tdef));
11835 end if;
11836
11837 -- Check all components to ensure no default expressions
11838
11839 if Present (Clist) then
11840 Comp := First (Component_Items (Clist));
11841 while Present (Comp) loop
11842 if Present (Expression (Comp)) then
11843 Error_Msg_N
11844 ("component of imported 'C'P'P type cannot have "
11845 & "default expression", Expression (Comp));
11846 end if;
11847
11848 Next (Comp);
11849 end loop;
11850 end if;
11851 end Check_CPP_Type_Has_No_Defaults;
11852
11853 ----------------------------
11854 -- Check_Delta_Expression --
11855 ----------------------------
11856
11857 procedure Check_Delta_Expression (E : Node_Id) is
11858 begin
11859 if not (Is_Real_Type (Etype (E))) then
11860 Wrong_Type (E, Any_Real);
11861
11862 elsif not Is_OK_Static_Expression (E) then
11863 Flag_Non_Static_Expr
11864 ("non-static expression used for delta value!", E);
11865
11866 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11867 Error_Msg_N ("delta expression must be positive", E);
11868
11869 else
11870 return;
11871 end if;
11872
11873 -- If any of above errors occurred, then replace the incorrect
11874 -- expression by the real 0.1, which should prevent further errors.
11875
11876 Rewrite (E,
11877 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11878 Analyze_And_Resolve (E, Standard_Float);
11879 end Check_Delta_Expression;
11880
11881 -----------------------------
11882 -- Check_Digits_Expression --
11883 -----------------------------
11884
11885 procedure Check_Digits_Expression (E : Node_Id) is
11886 begin
11887 if not (Is_Integer_Type (Etype (E))) then
11888 Wrong_Type (E, Any_Integer);
11889
11890 elsif not Is_OK_Static_Expression (E) then
11891 Flag_Non_Static_Expr
11892 ("non-static expression used for digits value!", E);
11893
11894 elsif Expr_Value (E) <= 0 then
11895 Error_Msg_N ("digits value must be greater than zero", E);
11896
11897 else
11898 return;
11899 end if;
11900
11901 -- If any of above errors occurred, then replace the incorrect
11902 -- expression by the integer 1, which should prevent further errors.
11903
11904 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11905 Analyze_And_Resolve (E, Standard_Integer);
11906
11907 end Check_Digits_Expression;
11908
11909 --------------------------
11910 -- Check_Initialization --
11911 --------------------------
11912
11913 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11914 begin
11915 -- Special processing for limited types
11916
11917 if Is_Limited_Type (T)
11918 and then not In_Instance
11919 and then not In_Inlined_Body
11920 then
11921 if not OK_For_Limited_Init (T, Exp) then
11922
11923 -- In GNAT mode, this is just a warning, to allow it to be evilly
11924 -- turned off. Otherwise it is a real error.
11925
11926 if GNAT_Mode then
11927 Error_Msg_N
11928 ("??cannot initialize entities of limited type!", Exp);
11929
11930 elsif Ada_Version < Ada_2005 then
11931
11932 -- The side effect removal machinery may generate illegal Ada
11933 -- code to avoid the usage of access types and 'reference in
11934 -- SPARK mode. Since this is legal code with respect to theorem
11935 -- proving, do not emit the error.
11936
11937 if GNATprove_Mode
11938 and then Nkind (Exp) = N_Function_Call
11939 and then Nkind (Parent (Exp)) = N_Object_Declaration
11940 and then not Comes_From_Source
11941 (Defining_Identifier (Parent (Exp)))
11942 then
11943 null;
11944
11945 else
11946 Error_Msg_N
11947 ("cannot initialize entities of limited type", Exp);
11948 Explain_Limited_Type (T, Exp);
11949 end if;
11950
11951 else
11952 -- Specialize error message according to kind of illegal
11953 -- initial expression. We check the Original_Node to cover
11954 -- cases where the initialization expression of an object
11955 -- declaration generated by the compiler has been rewritten
11956 -- (such as for dispatching calls).
11957
11958 if Nkind (Original_Node (Exp)) = N_Type_Conversion
11959 and then
11960 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
11961 then
11962 -- No error for internally-generated object declarations,
11963 -- which can come from build-in-place assignment statements.
11964
11965 if Nkind (Parent (Exp)) = N_Object_Declaration
11966 and then not Comes_From_Source
11967 (Defining_Identifier (Parent (Exp)))
11968 then
11969 null;
11970
11971 else
11972 Error_Msg_N
11973 ("illegal context for call to function with limited "
11974 & "result", Exp);
11975 end if;
11976
11977 else
11978 Error_Msg_N
11979 ("initialization of limited object requires aggregate or "
11980 & "function call", Exp);
11981 end if;
11982 end if;
11983 end if;
11984 end if;
11985
11986 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11987 -- set unless we can be sure that no range check is required.
11988
11989 if (GNATprove_Mode or not Expander_Active)
11990 and then Is_Scalar_Type (T)
11991 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11992 then
11993 Set_Do_Range_Check (Exp);
11994 end if;
11995 end Check_Initialization;
11996
11997 ----------------------
11998 -- Check_Interfaces --
11999 ----------------------
12000
12001 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12002 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12003
12004 Iface : Node_Id;
12005 Iface_Def : Node_Id;
12006 Iface_Typ : Entity_Id;
12007 Parent_Node : Node_Id;
12008
12009 Is_Task : Boolean := False;
12010 -- Set True if parent type or any progenitor is a task interface
12011
12012 Is_Protected : Boolean := False;
12013 -- Set True if parent type or any progenitor is a protected interface
12014
12015 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12016 -- Check that a progenitor is compatible with declaration. If an error
12017 -- message is output, it is posted on Error_Node.
12018
12019 ------------------
12020 -- Check_Ifaces --
12021 ------------------
12022
12023 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12024 Iface_Id : constant Entity_Id :=
12025 Defining_Identifier (Parent (Iface_Def));
12026 Type_Def : Node_Id;
12027
12028 begin
12029 if Nkind (N) = N_Private_Extension_Declaration then
12030 Type_Def := N;
12031 else
12032 Type_Def := Type_Definition (N);
12033 end if;
12034
12035 if Is_Task_Interface (Iface_Id) then
12036 Is_Task := True;
12037
12038 elsif Is_Protected_Interface (Iface_Id) then
12039 Is_Protected := True;
12040 end if;
12041
12042 if Is_Synchronized_Interface (Iface_Id) then
12043
12044 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12045 -- extension derived from a synchronized interface must explicitly
12046 -- be declared synchronized, because the full view will be a
12047 -- synchronized type.
12048
12049 if Nkind (N) = N_Private_Extension_Declaration then
12050 if not Synchronized_Present (N) then
12051 Error_Msg_NE
12052 ("private extension of& must be explicitly synchronized",
12053 N, Iface_Id);
12054 end if;
12055
12056 -- However, by 3.9.4(16/2), a full type that is a record extension
12057 -- is never allowed to derive from a synchronized interface (note
12058 -- that interfaces must be excluded from this check, because those
12059 -- are represented by derived type definitions in some cases).
12060
12061 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12062 and then not Interface_Present (Type_Definition (N))
12063 then
12064 Error_Msg_N ("record extension cannot derive from synchronized "
12065 & "interface", Error_Node);
12066 end if;
12067 end if;
12068
12069 -- Check that the characteristics of the progenitor are compatible
12070 -- with the explicit qualifier in the declaration.
12071 -- The check only applies to qualifiers that come from source.
12072 -- Limited_Present also appears in the declaration of corresponding
12073 -- records, and the check does not apply to them.
12074
12075 if Limited_Present (Type_Def)
12076 and then not
12077 Is_Concurrent_Record_Type (Defining_Identifier (N))
12078 then
12079 if Is_Limited_Interface (Parent_Type)
12080 and then not Is_Limited_Interface (Iface_Id)
12081 then
12082 Error_Msg_NE
12083 ("progenitor & must be limited interface",
12084 Error_Node, Iface_Id);
12085
12086 elsif
12087 (Task_Present (Iface_Def)
12088 or else Protected_Present (Iface_Def)
12089 or else Synchronized_Present (Iface_Def))
12090 and then Nkind (N) /= N_Private_Extension_Declaration
12091 and then not Error_Posted (N)
12092 then
12093 Error_Msg_NE
12094 ("progenitor & must be limited interface",
12095 Error_Node, Iface_Id);
12096 end if;
12097
12098 -- Protected interfaces can only inherit from limited, synchronized
12099 -- or protected interfaces.
12100
12101 elsif Nkind (N) = N_Full_Type_Declaration
12102 and then Protected_Present (Type_Def)
12103 then
12104 if Limited_Present (Iface_Def)
12105 or else Synchronized_Present (Iface_Def)
12106 or else Protected_Present (Iface_Def)
12107 then
12108 null;
12109
12110 elsif Task_Present (Iface_Def) then
12111 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12112 & "from task interface", Error_Node);
12113
12114 else
12115 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12116 & "from non-limited interface", Error_Node);
12117 end if;
12118
12119 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12120 -- limited and synchronized.
12121
12122 elsif Synchronized_Present (Type_Def) then
12123 if Limited_Present (Iface_Def)
12124 or else Synchronized_Present (Iface_Def)
12125 then
12126 null;
12127
12128 elsif Protected_Present (Iface_Def)
12129 and then Nkind (N) /= N_Private_Extension_Declaration
12130 then
12131 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12132 & "from protected interface", Error_Node);
12133
12134 elsif Task_Present (Iface_Def)
12135 and then Nkind (N) /= N_Private_Extension_Declaration
12136 then
12137 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12138 & "from task interface", Error_Node);
12139
12140 elsif not Is_Limited_Interface (Iface_Id) then
12141 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12142 & "from non-limited interface", Error_Node);
12143 end if;
12144
12145 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12146 -- synchronized or task interfaces.
12147
12148 elsif Nkind (N) = N_Full_Type_Declaration
12149 and then Task_Present (Type_Def)
12150 then
12151 if Limited_Present (Iface_Def)
12152 or else Synchronized_Present (Iface_Def)
12153 or else Task_Present (Iface_Def)
12154 then
12155 null;
12156
12157 elsif Protected_Present (Iface_Def) then
12158 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12159 & "protected interface", Error_Node);
12160
12161 else
12162 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12163 & "non-limited interface", Error_Node);
12164 end if;
12165 end if;
12166 end Check_Ifaces;
12167
12168 -- Start of processing for Check_Interfaces
12169
12170 begin
12171 if Is_Interface (Parent_Type) then
12172 if Is_Task_Interface (Parent_Type) then
12173 Is_Task := True;
12174
12175 elsif Is_Protected_Interface (Parent_Type) then
12176 Is_Protected := True;
12177 end if;
12178 end if;
12179
12180 if Nkind (N) = N_Private_Extension_Declaration then
12181
12182 -- Check that progenitors are compatible with declaration
12183
12184 Iface := First (Interface_List (Def));
12185 while Present (Iface) loop
12186 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12187
12188 Parent_Node := Parent (Base_Type (Iface_Typ));
12189 Iface_Def := Type_Definition (Parent_Node);
12190
12191 if not Is_Interface (Iface_Typ) then
12192 Diagnose_Interface (Iface, Iface_Typ);
12193 else
12194 Check_Ifaces (Iface_Def, Iface);
12195 end if;
12196
12197 Next (Iface);
12198 end loop;
12199
12200 if Is_Task and Is_Protected then
12201 Error_Msg_N
12202 ("type cannot derive from task and protected interface", N);
12203 end if;
12204
12205 return;
12206 end if;
12207
12208 -- Full type declaration of derived type.
12209 -- Check compatibility with parent if it is interface type
12210
12211 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12212 and then Is_Interface (Parent_Type)
12213 then
12214 Parent_Node := Parent (Parent_Type);
12215
12216 -- More detailed checks for interface varieties
12217
12218 Check_Ifaces
12219 (Iface_Def => Type_Definition (Parent_Node),
12220 Error_Node => Subtype_Indication (Type_Definition (N)));
12221 end if;
12222
12223 Iface := First (Interface_List (Def));
12224 while Present (Iface) loop
12225 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12226
12227 Parent_Node := Parent (Base_Type (Iface_Typ));
12228 Iface_Def := Type_Definition (Parent_Node);
12229
12230 if not Is_Interface (Iface_Typ) then
12231 Diagnose_Interface (Iface, Iface_Typ);
12232
12233 else
12234 -- "The declaration of a specific descendant of an interface
12235 -- type freezes the interface type" RM 13.14
12236
12237 Freeze_Before (N, Iface_Typ);
12238 Check_Ifaces (Iface_Def, Error_Node => Iface);
12239 end if;
12240
12241 Next (Iface);
12242 end loop;
12243
12244 if Is_Task and Is_Protected then
12245 Error_Msg_N
12246 ("type cannot derive from task and protected interface", N);
12247 end if;
12248 end Check_Interfaces;
12249
12250 ------------------------------------
12251 -- Check_Or_Process_Discriminants --
12252 ------------------------------------
12253
12254 -- If an incomplete or private type declaration was already given for the
12255 -- type, the discriminants may have already been processed if they were
12256 -- present on the incomplete declaration. In this case a full conformance
12257 -- check has been performed in Find_Type_Name, and we then recheck here
12258 -- some properties that can't be checked on the partial view alone.
12259 -- Otherwise we call Process_Discriminants.
12260
12261 procedure Check_Or_Process_Discriminants
12262 (N : Node_Id;
12263 T : Entity_Id;
12264 Prev : Entity_Id := Empty)
12265 is
12266 begin
12267 if Has_Discriminants (T) then
12268
12269 -- Discriminants are already set on T if they were already present
12270 -- on the partial view. Make them visible to component declarations.
12271
12272 declare
12273 D : Entity_Id;
12274 -- Discriminant on T (full view) referencing expr on partial view
12275
12276 Prev_D : Entity_Id;
12277 -- Entity of corresponding discriminant on partial view
12278
12279 New_D : Node_Id;
12280 -- Discriminant specification for full view, expression is
12281 -- the syntactic copy on full view (which has been checked for
12282 -- conformance with partial view), only used here to post error
12283 -- message.
12284
12285 begin
12286 D := First_Discriminant (T);
12287 New_D := First (Discriminant_Specifications (N));
12288 while Present (D) loop
12289 Prev_D := Current_Entity (D);
12290 Set_Current_Entity (D);
12291 Set_Is_Immediately_Visible (D);
12292 Set_Homonym (D, Prev_D);
12293
12294 -- Handle the case where there is an untagged partial view and
12295 -- the full view is tagged: must disallow discriminants with
12296 -- defaults, unless compiling for Ada 2012, which allows a
12297 -- limited tagged type to have defaulted discriminants (see
12298 -- AI05-0214). However, suppress error here if it was already
12299 -- reported on the default expression of the partial view.
12300
12301 if Is_Tagged_Type (T)
12302 and then Present (Expression (Parent (D)))
12303 and then (not Is_Limited_Type (Current_Scope)
12304 or else Ada_Version < Ada_2012)
12305 and then not Error_Posted (Expression (Parent (D)))
12306 then
12307 if Ada_Version >= Ada_2012 then
12308 Error_Msg_N
12309 ("discriminants of nonlimited tagged type cannot have "
12310 & "defaults",
12311 Expression (New_D));
12312 else
12313 Error_Msg_N
12314 ("discriminants of tagged type cannot have defaults",
12315 Expression (New_D));
12316 end if;
12317 end if;
12318
12319 -- Ada 2005 (AI-230): Access discriminant allowed in
12320 -- non-limited record types.
12321
12322 if Ada_Version < Ada_2005 then
12323
12324 -- This restriction gets applied to the full type here. It
12325 -- has already been applied earlier to the partial view.
12326
12327 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12328 end if;
12329
12330 Next_Discriminant (D);
12331 Next (New_D);
12332 end loop;
12333 end;
12334
12335 elsif Present (Discriminant_Specifications (N)) then
12336 Process_Discriminants (N, Prev);
12337 end if;
12338 end Check_Or_Process_Discriminants;
12339
12340 ----------------------
12341 -- Check_Real_Bound --
12342 ----------------------
12343
12344 procedure Check_Real_Bound (Bound : Node_Id) is
12345 begin
12346 if not Is_Real_Type (Etype (Bound)) then
12347 Error_Msg_N
12348 ("bound in real type definition must be of real type", Bound);
12349
12350 elsif not Is_OK_Static_Expression (Bound) then
12351 Flag_Non_Static_Expr
12352 ("non-static expression used for real type bound!", Bound);
12353
12354 else
12355 return;
12356 end if;
12357
12358 Rewrite
12359 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12360 Analyze (Bound);
12361 Resolve (Bound, Standard_Float);
12362 end Check_Real_Bound;
12363
12364 ------------------------------
12365 -- Complete_Private_Subtype --
12366 ------------------------------
12367
12368 procedure Complete_Private_Subtype
12369 (Priv : Entity_Id;
12370 Full : Entity_Id;
12371 Full_Base : Entity_Id;
12372 Related_Nod : Node_Id)
12373 is
12374 Save_Next_Entity : Entity_Id;
12375 Save_Homonym : Entity_Id;
12376
12377 begin
12378 -- Set semantic attributes for (implicit) private subtype completion.
12379 -- If the full type has no discriminants, then it is a copy of the
12380 -- full view of the base. Otherwise, it is a subtype of the base with
12381 -- a possible discriminant constraint. Save and restore the original
12382 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12383 -- not corrupt the entity chain.
12384
12385 Save_Next_Entity := Next_Entity (Full);
12386 Save_Homonym := Homonym (Priv);
12387
12388 if Is_Private_Type (Full_Base)
12389 or else Is_Record_Type (Full_Base)
12390 or else Is_Concurrent_Type (Full_Base)
12391 then
12392 Copy_Node (Priv, Full);
12393
12394 -- Note that the Etype of the full view is the same as the Etype of
12395 -- the partial view. In this fashion, the subtype has access to the
12396 -- correct view of the parent.
12397
12398 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12399 Set_Has_Unknown_Discriminants
12400 (Full, Has_Unknown_Discriminants (Full_Base));
12401 Set_First_Entity (Full, First_Entity (Full_Base));
12402 Set_Last_Entity (Full, Last_Entity (Full_Base));
12403
12404 -- If the underlying base type is constrained, we know that the
12405 -- full view of the subtype is constrained as well (the converse
12406 -- is not necessarily true).
12407
12408 if Is_Constrained (Full_Base) then
12409 Set_Is_Constrained (Full);
12410 end if;
12411
12412 else
12413 Copy_Node (Full_Base, Full);
12414
12415 -- The following subtlety with the Etype of the full view needs to be
12416 -- taken into account here. One could think that it must naturally be
12417 -- set to the base type of the full base:
12418
12419 -- Set_Etype (Full, Base_Type (Full_Base));
12420
12421 -- so that the full view becomes a subtype of the full base when the
12422 -- latter is a base type, which must for example happen when the full
12423 -- base is declared as derived type. That's also correct if the full
12424 -- base is declared as an array type, or a floating-point type, or a
12425 -- fixed-point type, or a signed integer type, as these declarations
12426 -- create an implicit base type and a first subtype so the Etype of
12427 -- the full views must be the implicit base type. But that's wrong
12428 -- if the full base is declared as an access type, or an enumeration
12429 -- type, or a modular integer type, as these declarations directly
12430 -- create a base type, i.e. with Etype pointing to itself. Moreover
12431 -- the full base being declared in the private part, i.e. when the
12432 -- views are swapped, the end result is that the Etype of the full
12433 -- base is set to its private view in this case and that we need to
12434 -- propagate this setting to the full view in order for the subtype
12435 -- to be compatible with the base type.
12436
12437 if Is_Base_Type (Full_Base)
12438 and then (Is_Derived_Type (Full_Base)
12439 or else Ekind (Full_Base) in Array_Kind
12440 or else Ekind (Full_Base) in Fixed_Point_Kind
12441 or else Ekind (Full_Base) in Float_Kind
12442 or else Ekind (Full_Base) in Signed_Integer_Kind)
12443 then
12444 Set_Etype (Full, Full_Base);
12445 end if;
12446
12447 Set_Chars (Full, Chars (Priv));
12448 Set_Sloc (Full, Sloc (Priv));
12449 Conditional_Delay (Full, Priv);
12450 end if;
12451
12452 Link_Entities (Full, Save_Next_Entity);
12453 Set_Homonym (Full, Save_Homonym);
12454 Set_Associated_Node_For_Itype (Full, Related_Nod);
12455
12456 -- Set common attributes for all subtypes: kind, convention, etc.
12457
12458 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12459 Set_Convention (Full, Convention (Full_Base));
12460 Set_Is_First_Subtype (Full, False);
12461 Set_Scope (Full, Scope (Priv));
12462 Set_Size_Info (Full, Full_Base);
12463 Set_RM_Size (Full, RM_Size (Full_Base));
12464 Set_Is_Itype (Full);
12465
12466 -- A subtype of a private-type-without-discriminants, whose full-view
12467 -- has discriminants with default expressions, is not constrained.
12468
12469 if not Has_Discriminants (Priv) then
12470 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12471
12472 if Has_Discriminants (Full_Base) then
12473 Set_Discriminant_Constraint
12474 (Full, Discriminant_Constraint (Full_Base));
12475
12476 -- The partial view may have been indefinite, the full view
12477 -- might not be.
12478
12479 Set_Has_Unknown_Discriminants
12480 (Full, Has_Unknown_Discriminants (Full_Base));
12481 end if;
12482 end if;
12483
12484 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12485 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12486
12487 -- Freeze the private subtype entity if its parent is delayed, and not
12488 -- already frozen. We skip this processing if the type is an anonymous
12489 -- subtype of a record component, or is the corresponding record of a
12490 -- protected type, since these are processed when the enclosing type
12491 -- is frozen. If the parent type is declared in a nested package then
12492 -- the freezing of the private and full views also happens later.
12493
12494 if not Is_Type (Scope (Full)) then
12495 if Is_Itype (Priv)
12496 and then In_Same_Source_Unit (Full, Full_Base)
12497 and then Scope (Full_Base) /= Scope (Full)
12498 then
12499 Set_Has_Delayed_Freeze (Full);
12500 Set_Has_Delayed_Freeze (Priv);
12501
12502 else
12503 Set_Has_Delayed_Freeze (Full,
12504 Has_Delayed_Freeze (Full_Base)
12505 and then not Is_Frozen (Full_Base));
12506 end if;
12507 end if;
12508
12509 Set_Freeze_Node (Full, Empty);
12510 Set_Is_Frozen (Full, False);
12511
12512 if Has_Discriminants (Full) then
12513 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12514 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12515
12516 if Has_Unknown_Discriminants (Full) then
12517 Set_Discriminant_Constraint (Full, No_Elist);
12518 end if;
12519 end if;
12520
12521 if Ekind (Full_Base) = E_Record_Type
12522 and then Has_Discriminants (Full_Base)
12523 and then Has_Discriminants (Priv) -- might not, if errors
12524 and then not Has_Unknown_Discriminants (Priv)
12525 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12526 then
12527 Create_Constrained_Components
12528 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12529
12530 -- If the full base is itself derived from private, build a congruent
12531 -- subtype of its underlying full view, for use by the back end.
12532
12533 elsif Is_Private_Type (Full_Base)
12534 and then Present (Underlying_Full_View (Full_Base))
12535 then
12536 declare
12537 Underlying_Full_Base : constant Entity_Id
12538 := Underlying_Full_View (Full_Base);
12539 Underlying_Full : constant Entity_Id
12540 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12541 begin
12542 Set_Is_Itype (Underlying_Full);
12543 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12544 Complete_Private_Subtype
12545 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12546 Set_Underlying_Full_View (Full, Underlying_Full);
12547 Set_Is_Underlying_Full_View (Underlying_Full);
12548 end;
12549
12550 elsif Is_Record_Type (Full_Base) then
12551
12552 -- Show Full is simply a renaming of Full_Base
12553
12554 Set_Cloned_Subtype (Full, Full_Base);
12555
12556 -- Propagate predicates
12557
12558 if Has_Predicates (Full_Base) then
12559 Set_Has_Predicates (Full);
12560
12561 if Present (Predicate_Function (Full_Base))
12562 and then No (Predicate_Function (Full))
12563 then
12564 Set_Predicate_Function (Full, Predicate_Function (Full_Base));
12565 end if;
12566 end if;
12567 end if;
12568
12569 -- It is unsafe to share the bounds of a scalar type, because the Itype
12570 -- is elaborated on demand, and if a bound is nonstatic, then different
12571 -- orders of elaboration in different units will lead to different
12572 -- external symbols.
12573
12574 if Is_Scalar_Type (Full_Base) then
12575 Set_Scalar_Range (Full,
12576 Make_Range (Sloc (Related_Nod),
12577 Low_Bound =>
12578 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12579 High_Bound =>
12580 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12581
12582 -- This completion inherits the bounds of the full parent, but if
12583 -- the parent is an unconstrained floating point type, so is the
12584 -- completion.
12585
12586 if Is_Floating_Point_Type (Full_Base) then
12587 Set_Includes_Infinities
12588 (Scalar_Range (Full), Has_Infinities (Full_Base));
12589 end if;
12590 end if;
12591
12592 -- ??? It seems that a lot of fields are missing that should be copied
12593 -- from Full_Base to Full. Here are some that are introduced in a
12594 -- non-disruptive way but a cleanup is necessary.
12595
12596 if Is_Tagged_Type (Full_Base) then
12597 Set_Is_Tagged_Type (Full);
12598 Set_Direct_Primitive_Operations
12599 (Full, Direct_Primitive_Operations (Full_Base));
12600 Set_No_Tagged_Streams_Pragma
12601 (Full, No_Tagged_Streams_Pragma (Full_Base));
12602
12603 -- Inherit class_wide type of full_base in case the partial view was
12604 -- not tagged. Otherwise it has already been created when the private
12605 -- subtype was analyzed.
12606
12607 if No (Class_Wide_Type (Full)) then
12608 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12609 end if;
12610
12611 -- If this is a subtype of a protected or task type, constrain its
12612 -- corresponding record, unless this is a subtype without constraints,
12613 -- i.e. a simple renaming as with an actual subtype in an instance.
12614
12615 elsif Is_Concurrent_Type (Full_Base) then
12616 if Has_Discriminants (Full)
12617 and then Present (Corresponding_Record_Type (Full_Base))
12618 and then
12619 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12620 then
12621 Set_Corresponding_Record_Type (Full,
12622 Constrain_Corresponding_Record
12623 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12624
12625 else
12626 Set_Corresponding_Record_Type (Full,
12627 Corresponding_Record_Type (Full_Base));
12628 end if;
12629 end if;
12630
12631 -- Link rep item chain, and also setting of Has_Predicates from private
12632 -- subtype to full subtype, since we will need these on the full subtype
12633 -- to create the predicate function. Note that the full subtype may
12634 -- already have rep items, inherited from the full view of the base
12635 -- type, so we must be sure not to overwrite these entries.
12636
12637 declare
12638 Append : Boolean;
12639 Item : Node_Id;
12640 Next_Item : Node_Id;
12641 Priv_Item : Node_Id;
12642
12643 begin
12644 Item := First_Rep_Item (Full);
12645 Priv_Item := First_Rep_Item (Priv);
12646
12647 -- If no existing rep items on full type, we can just link directly
12648 -- to the list of items on the private type, if any exist.. Same if
12649 -- the rep items are only those inherited from the base
12650
12651 if (No (Item)
12652 or else Nkind (Item) /= N_Aspect_Specification
12653 or else Entity (Item) = Full_Base)
12654 and then Present (First_Rep_Item (Priv))
12655 then
12656 Set_First_Rep_Item (Full, Priv_Item);
12657
12658 -- Otherwise, search to the end of items currently linked to the full
12659 -- subtype and append the private items to the end. However, if Priv
12660 -- and Full already have the same list of rep items, then the append
12661 -- is not done, as that would create a circularity.
12662 --
12663 -- The partial view may have a predicate and the rep item lists of
12664 -- both views agree when inherited from the same ancestor. In that
12665 -- case, simply propagate the list from one view to the other.
12666 -- A more complex analysis needed here ???
12667
12668 elsif Present (Priv_Item)
12669 and then Item = Next_Rep_Item (Priv_Item)
12670 then
12671 Set_First_Rep_Item (Full, Priv_Item);
12672
12673 elsif Item /= Priv_Item then
12674 Append := True;
12675 loop
12676 Next_Item := Next_Rep_Item (Item);
12677 exit when No (Next_Item);
12678 Item := Next_Item;
12679
12680 -- If the private view has aspect specifications, the full view
12681 -- inherits them. Since these aspects may already have been
12682 -- attached to the full view during derivation, do not append
12683 -- them if already present.
12684
12685 if Item = First_Rep_Item (Priv) then
12686 Append := False;
12687 exit;
12688 end if;
12689 end loop;
12690
12691 -- And link the private type items at the end of the chain
12692
12693 if Append then
12694 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12695 end if;
12696 end if;
12697 end;
12698
12699 -- Make sure Has_Predicates is set on full type if it is set on the
12700 -- private type. Note that it may already be set on the full type and
12701 -- if so, we don't want to unset it. Similarly, propagate information
12702 -- about delayed aspects, because the corresponding pragmas must be
12703 -- analyzed when one of the views is frozen. This last step is needed
12704 -- in particular when the full type is a scalar type for which an
12705 -- anonymous base type is constructed.
12706
12707 -- The predicate functions are generated either at the freeze point
12708 -- of the type or at the end of the visible part, and we must avoid
12709 -- generating them twice.
12710
12711 if Has_Predicates (Priv) then
12712 Set_Has_Predicates (Full);
12713
12714 if Present (Predicate_Function (Priv))
12715 and then No (Predicate_Function (Full))
12716 then
12717 Set_Predicate_Function (Full, Predicate_Function (Priv));
12718 end if;
12719 end if;
12720
12721 if Has_Delayed_Aspects (Priv) then
12722 Set_Has_Delayed_Aspects (Full);
12723 end if;
12724 end Complete_Private_Subtype;
12725
12726 ----------------------------
12727 -- Constant_Redeclaration --
12728 ----------------------------
12729
12730 procedure Constant_Redeclaration
12731 (Id : Entity_Id;
12732 N : Node_Id;
12733 T : out Entity_Id)
12734 is
12735 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12736 Obj_Def : constant Node_Id := Object_Definition (N);
12737 New_T : Entity_Id;
12738
12739 procedure Check_Possible_Deferred_Completion
12740 (Prev_Id : Entity_Id;
12741 Prev_Obj_Def : Node_Id;
12742 Curr_Obj_Def : Node_Id);
12743 -- Determine whether the two object definitions describe the partial
12744 -- and the full view of a constrained deferred constant. Generate
12745 -- a subtype for the full view and verify that it statically matches
12746 -- the subtype of the partial view.
12747
12748 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12749 -- If deferred constant is an access type initialized with an allocator,
12750 -- check whether there is an illegal recursion in the definition,
12751 -- through a default value of some record subcomponent. This is normally
12752 -- detected when generating init procs, but requires this additional
12753 -- mechanism when expansion is disabled.
12754
12755 ----------------------------------------
12756 -- Check_Possible_Deferred_Completion --
12757 ----------------------------------------
12758
12759 procedure Check_Possible_Deferred_Completion
12760 (Prev_Id : Entity_Id;
12761 Prev_Obj_Def : Node_Id;
12762 Curr_Obj_Def : Node_Id)
12763 is
12764 begin
12765 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12766 and then Present (Constraint (Prev_Obj_Def))
12767 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12768 and then Present (Constraint (Curr_Obj_Def))
12769 then
12770 declare
12771 Loc : constant Source_Ptr := Sloc (N);
12772 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12773 Decl : constant Node_Id :=
12774 Make_Subtype_Declaration (Loc,
12775 Defining_Identifier => Def_Id,
12776 Subtype_Indication =>
12777 Relocate_Node (Curr_Obj_Def));
12778
12779 begin
12780 Insert_Before_And_Analyze (N, Decl);
12781 Set_Etype (Id, Def_Id);
12782
12783 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12784 Error_Msg_Sloc := Sloc (Prev_Id);
12785 Error_Msg_N ("subtype does not statically match deferred "
12786 & "declaration #", N);
12787 end if;
12788 end;
12789 end if;
12790 end Check_Possible_Deferred_Completion;
12791
12792 ---------------------------------
12793 -- Check_Recursive_Declaration --
12794 ---------------------------------
12795
12796 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12797 Comp : Entity_Id;
12798
12799 begin
12800 if Is_Record_Type (Typ) then
12801 Comp := First_Component (Typ);
12802 while Present (Comp) loop
12803 if Comes_From_Source (Comp) then
12804 if Present (Expression (Parent (Comp)))
12805 and then Is_Entity_Name (Expression (Parent (Comp)))
12806 and then Entity (Expression (Parent (Comp))) = Prev
12807 then
12808 Error_Msg_Sloc := Sloc (Parent (Comp));
12809 Error_Msg_NE
12810 ("illegal circularity with declaration for & #",
12811 N, Comp);
12812 return;
12813
12814 elsif Is_Record_Type (Etype (Comp)) then
12815 Check_Recursive_Declaration (Etype (Comp));
12816 end if;
12817 end if;
12818
12819 Next_Component (Comp);
12820 end loop;
12821 end if;
12822 end Check_Recursive_Declaration;
12823
12824 -- Start of processing for Constant_Redeclaration
12825
12826 begin
12827 if Nkind (Parent (Prev)) = N_Object_Declaration then
12828 if Nkind (Object_Definition
12829 (Parent (Prev))) = N_Subtype_Indication
12830 then
12831 -- Find type of new declaration. The constraints of the two
12832 -- views must match statically, but there is no point in
12833 -- creating an itype for the full view.
12834
12835 if Nkind (Obj_Def) = N_Subtype_Indication then
12836 Find_Type (Subtype_Mark (Obj_Def));
12837 New_T := Entity (Subtype_Mark (Obj_Def));
12838
12839 else
12840 Find_Type (Obj_Def);
12841 New_T := Entity (Obj_Def);
12842 end if;
12843
12844 T := Etype (Prev);
12845
12846 else
12847 -- The full view may impose a constraint, even if the partial
12848 -- view does not, so construct the subtype.
12849
12850 New_T := Find_Type_Of_Object (Obj_Def, N);
12851 T := New_T;
12852 end if;
12853
12854 else
12855 -- Current declaration is illegal, diagnosed below in Enter_Name
12856
12857 T := Empty;
12858 New_T := Any_Type;
12859 end if;
12860
12861 -- If previous full declaration or a renaming declaration exists, or if
12862 -- a homograph is present, let Enter_Name handle it, either with an
12863 -- error or with the removal of an overridden implicit subprogram.
12864 -- The previous one is a full declaration if it has an expression
12865 -- (which in the case of an aggregate is indicated by the Init flag).
12866
12867 if Ekind (Prev) /= E_Constant
12868 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12869 or else Present (Expression (Parent (Prev)))
12870 or else Has_Init_Expression (Parent (Prev))
12871 or else Present (Full_View (Prev))
12872 then
12873 Enter_Name (Id);
12874
12875 -- Verify that types of both declarations match, or else that both types
12876 -- are anonymous access types whose designated subtypes statically match
12877 -- (as allowed in Ada 2005 by AI-385).
12878
12879 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12880 and then
12881 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12882 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12883 or else Is_Access_Constant (Etype (New_T)) /=
12884 Is_Access_Constant (Etype (Prev))
12885 or else Can_Never_Be_Null (Etype (New_T)) /=
12886 Can_Never_Be_Null (Etype (Prev))
12887 or else Null_Exclusion_Present (Parent (Prev)) /=
12888 Null_Exclusion_Present (Parent (Id))
12889 or else not Subtypes_Statically_Match
12890 (Designated_Type (Etype (Prev)),
12891 Designated_Type (Etype (New_T))))
12892 then
12893 Error_Msg_Sloc := Sloc (Prev);
12894 Error_Msg_N ("type does not match declaration#", N);
12895 Set_Full_View (Prev, Id);
12896 Set_Etype (Id, Any_Type);
12897
12898 -- A deferred constant whose type is an anonymous array is always
12899 -- illegal (unless imported). A detailed error message might be
12900 -- helpful for Ada beginners.
12901
12902 if Nkind (Object_Definition (Parent (Prev)))
12903 = N_Constrained_Array_Definition
12904 and then Nkind (Object_Definition (N))
12905 = N_Constrained_Array_Definition
12906 then
12907 Error_Msg_N ("\each anonymous array is a distinct type", N);
12908 Error_Msg_N ("a deferred constant must have a named type",
12909 Object_Definition (Parent (Prev)));
12910 end if;
12911
12912 elsif
12913 Null_Exclusion_Present (Parent (Prev))
12914 and then not Null_Exclusion_Present (N)
12915 then
12916 Error_Msg_Sloc := Sloc (Prev);
12917 Error_Msg_N ("null-exclusion does not match declaration#", N);
12918 Set_Full_View (Prev, Id);
12919 Set_Etype (Id, Any_Type);
12920
12921 -- If so, process the full constant declaration
12922
12923 else
12924 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12925 -- the deferred declaration is constrained, then the subtype defined
12926 -- by the subtype_indication in the full declaration shall match it
12927 -- statically.
12928
12929 Check_Possible_Deferred_Completion
12930 (Prev_Id => Prev,
12931 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12932 Curr_Obj_Def => Obj_Def);
12933
12934 Set_Full_View (Prev, Id);
12935 Set_Is_Public (Id, Is_Public (Prev));
12936 Set_Is_Internal (Id);
12937 Append_Entity (Id, Current_Scope);
12938
12939 -- Check ALIASED present if present before (RM 7.4(7))
12940
12941 if Is_Aliased (Prev)
12942 and then not Aliased_Present (N)
12943 then
12944 Error_Msg_Sloc := Sloc (Prev);
12945 Error_Msg_N ("ALIASED required (see declaration #)", N);
12946 end if;
12947
12948 -- Check that placement is in private part and that the incomplete
12949 -- declaration appeared in the visible part.
12950
12951 if Ekind (Current_Scope) = E_Package
12952 and then not In_Private_Part (Current_Scope)
12953 then
12954 Error_Msg_Sloc := Sloc (Prev);
12955 Error_Msg_N
12956 ("full constant for declaration # must be in private part", N);
12957
12958 elsif Ekind (Current_Scope) = E_Package
12959 and then
12960 List_Containing (Parent (Prev)) /=
12961 Visible_Declarations (Package_Specification (Current_Scope))
12962 then
12963 Error_Msg_N
12964 ("deferred constant must be declared in visible part",
12965 Parent (Prev));
12966 end if;
12967
12968 if Is_Access_Type (T)
12969 and then Nkind (Expression (N)) = N_Allocator
12970 then
12971 Check_Recursive_Declaration (Designated_Type (T));
12972 end if;
12973
12974 -- A deferred constant is a visible entity. If type has invariants,
12975 -- verify that the initial value satisfies them. This is not done in
12976 -- GNATprove mode, as GNATprove handles invariant checks itself.
12977
12978 if Has_Invariants (T)
12979 and then Present (Invariant_Procedure (T))
12980 and then not GNATprove_Mode
12981 then
12982 Insert_After (N,
12983 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12984 end if;
12985 end if;
12986 end Constant_Redeclaration;
12987
12988 ----------------------
12989 -- Constrain_Access --
12990 ----------------------
12991
12992 procedure Constrain_Access
12993 (Def_Id : in out Entity_Id;
12994 S : Node_Id;
12995 Related_Nod : Node_Id)
12996 is
12997 T : constant Entity_Id := Entity (Subtype_Mark (S));
12998 Desig_Type : constant Entity_Id := Designated_Type (T);
12999 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
13000 Constraint_OK : Boolean := True;
13001
13002 begin
13003 if Is_Array_Type (Desig_Type) then
13004 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13005
13006 elsif (Is_Record_Type (Desig_Type)
13007 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13008 and then not Is_Constrained (Desig_Type)
13009 then
13010 -- If this is a constrained access definition for a record
13011 -- component, we leave the type as an unconstrained access,
13012 -- and mark the component so that its actual type is built
13013 -- at a point of use (e.g., an assignment statement). This
13014 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13015
13016 if Desig_Type = Current_Scope
13017 and then No (Def_Id)
13018 then
13019 Desig_Subtype :=
13020 Create_Itype
13021 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13022 Set_Ekind (Desig_Subtype, E_Record_Subtype);
13023 Def_Id := Entity (Subtype_Mark (S));
13024
13025 -- We indicate that the component has a per-object constraint
13026 -- for treatment at a point of use, even though the constraint
13027 -- may be independent of discriminants of the enclosing type.
13028
13029 if Nkind (Related_Nod) = N_Component_Declaration then
13030 Set_Has_Per_Object_Constraint
13031 (Defining_Identifier (Related_Nod));
13032 end if;
13033
13034 -- This call added to ensure that the constraint is analyzed
13035 -- (needed for a B test). Note that we still return early from
13036 -- this procedure to avoid recursive processing.
13037
13038 Constrain_Discriminated_Type
13039 (Desig_Subtype, S, Related_Nod, For_Access => True);
13040 return;
13041 end if;
13042
13043 -- Enforce rule that the constraint is illegal if there is an
13044 -- unconstrained view of the designated type. This means that the
13045 -- partial view (either a private type declaration or a derivation
13046 -- from a private type) has no discriminants. (Defect Report
13047 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13048
13049 -- Rule updated for Ada 2005: The private type is said to have
13050 -- a constrained partial view, given that objects of the type
13051 -- can be declared. Furthermore, the rule applies to all access
13052 -- types, unlike the rule concerning default discriminants (see
13053 -- RM 3.7.1(7/3))
13054
13055 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13056 and then Has_Private_Declaration (Desig_Type)
13057 and then In_Open_Scopes (Scope (Desig_Type))
13058 and then Has_Discriminants (Desig_Type)
13059 then
13060 declare
13061 Pack : constant Node_Id :=
13062 Unit_Declaration_Node (Scope (Desig_Type));
13063 Decls : List_Id;
13064 Decl : Node_Id;
13065
13066 begin
13067 if Nkind (Pack) = N_Package_Declaration then
13068 Decls := Visible_Declarations (Specification (Pack));
13069 Decl := First (Decls);
13070 while Present (Decl) loop
13071 if (Nkind (Decl) = N_Private_Type_Declaration
13072 and then Chars (Defining_Identifier (Decl)) =
13073 Chars (Desig_Type))
13074
13075 or else
13076 (Nkind (Decl) = N_Full_Type_Declaration
13077 and then
13078 Chars (Defining_Identifier (Decl)) =
13079 Chars (Desig_Type)
13080 and then Is_Derived_Type (Desig_Type)
13081 and then
13082 Has_Private_Declaration (Etype (Desig_Type)))
13083 then
13084 if No (Discriminant_Specifications (Decl)) then
13085 Error_Msg_N
13086 ("cannot constrain access type if designated "
13087 & "type has constrained partial view", S);
13088 end if;
13089
13090 exit;
13091 end if;
13092
13093 Next (Decl);
13094 end loop;
13095 end if;
13096 end;
13097 end if;
13098
13099 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13100 For_Access => True);
13101
13102 elsif Is_Concurrent_Type (Desig_Type)
13103 and then not Is_Constrained (Desig_Type)
13104 then
13105 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13106
13107 else
13108 Error_Msg_N ("invalid constraint on access type", S);
13109
13110 -- We simply ignore an invalid constraint
13111
13112 Desig_Subtype := Desig_Type;
13113 Constraint_OK := False;
13114 end if;
13115
13116 if No (Def_Id) then
13117 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13118 else
13119 Set_Ekind (Def_Id, E_Access_Subtype);
13120 end if;
13121
13122 if Constraint_OK then
13123 Set_Etype (Def_Id, Base_Type (T));
13124
13125 if Is_Private_Type (Desig_Type) then
13126 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13127 end if;
13128 else
13129 Set_Etype (Def_Id, Any_Type);
13130 end if;
13131
13132 Set_Size_Info (Def_Id, T);
13133 Set_Is_Constrained (Def_Id, Constraint_OK);
13134 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13135 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13136 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13137
13138 Conditional_Delay (Def_Id, T);
13139
13140 -- AI-363 : Subtypes of general access types whose designated types have
13141 -- default discriminants are disallowed. In instances, the rule has to
13142 -- be checked against the actual, of which T is the subtype. In a
13143 -- generic body, the rule is checked assuming that the actual type has
13144 -- defaulted discriminants.
13145
13146 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13147 if Ekind (Base_Type (T)) = E_General_Access_Type
13148 and then Has_Defaulted_Discriminants (Desig_Type)
13149 then
13150 if Ada_Version < Ada_2005 then
13151 Error_Msg_N
13152 ("access subtype of general access type would not " &
13153 "be allowed in Ada 2005?y?", S);
13154 else
13155 Error_Msg_N
13156 ("access subtype of general access type not allowed", S);
13157 end if;
13158
13159 Error_Msg_N ("\discriminants have defaults", S);
13160
13161 elsif Is_Access_Type (T)
13162 and then Is_Generic_Type (Desig_Type)
13163 and then Has_Discriminants (Desig_Type)
13164 and then In_Package_Body (Current_Scope)
13165 then
13166 if Ada_Version < Ada_2005 then
13167 Error_Msg_N
13168 ("access subtype would not be allowed in generic body "
13169 & "in Ada 2005?y?", S);
13170 else
13171 Error_Msg_N
13172 ("access subtype not allowed in generic body", S);
13173 end if;
13174
13175 Error_Msg_N
13176 ("\designated type is a discriminated formal", S);
13177 end if;
13178 end if;
13179 end Constrain_Access;
13180
13181 ---------------------
13182 -- Constrain_Array --
13183 ---------------------
13184
13185 procedure Constrain_Array
13186 (Def_Id : in out Entity_Id;
13187 SI : Node_Id;
13188 Related_Nod : Node_Id;
13189 Related_Id : Entity_Id;
13190 Suffix : Character)
13191 is
13192 C : constant Node_Id := Constraint (SI);
13193 Number_Of_Constraints : Nat := 0;
13194 Index : Node_Id;
13195 S, T : Entity_Id;
13196 Constraint_OK : Boolean := True;
13197
13198 begin
13199 T := Entity (Subtype_Mark (SI));
13200
13201 if Is_Access_Type (T) then
13202 T := Designated_Type (T);
13203 end if;
13204
13205 -- If an index constraint follows a subtype mark in a subtype indication
13206 -- then the type or subtype denoted by the subtype mark must not already
13207 -- impose an index constraint. The subtype mark must denote either an
13208 -- unconstrained array type or an access type whose designated type
13209 -- is such an array type... (RM 3.6.1)
13210
13211 if Is_Constrained (T) then
13212 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13213 Constraint_OK := False;
13214
13215 else
13216 S := First (Constraints (C));
13217 while Present (S) loop
13218 Number_Of_Constraints := Number_Of_Constraints + 1;
13219 Next (S);
13220 end loop;
13221
13222 -- In either case, the index constraint must provide a discrete
13223 -- range for each index of the array type and the type of each
13224 -- discrete range must be the same as that of the corresponding
13225 -- index. (RM 3.6.1)
13226
13227 if Number_Of_Constraints /= Number_Dimensions (T) then
13228 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13229 Constraint_OK := False;
13230
13231 else
13232 S := First (Constraints (C));
13233 Index := First_Index (T);
13234 Analyze (Index);
13235
13236 -- Apply constraints to each index type
13237
13238 for J in 1 .. Number_Of_Constraints loop
13239 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13240 Next (Index);
13241 Next (S);
13242 end loop;
13243
13244 end if;
13245 end if;
13246
13247 if No (Def_Id) then
13248 Def_Id :=
13249 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13250 Set_Parent (Def_Id, Related_Nod);
13251
13252 else
13253 Set_Ekind (Def_Id, E_Array_Subtype);
13254 end if;
13255
13256 Set_Size_Info (Def_Id, (T));
13257 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13258 Set_Etype (Def_Id, Base_Type (T));
13259
13260 if Constraint_OK then
13261 Set_First_Index (Def_Id, First (Constraints (C)));
13262 else
13263 Set_First_Index (Def_Id, First_Index (T));
13264 end if;
13265
13266 Set_Is_Constrained (Def_Id, True);
13267 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13268 Set_Is_Independent (Def_Id, Is_Independent (T));
13269 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13270
13271 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13272 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13273
13274 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13275 -- We need to initialize the attribute because if Def_Id is previously
13276 -- analyzed through a limited_with clause, it will have the attributes
13277 -- of an incomplete type, one of which is an Elist that overlaps the
13278 -- Packed_Array_Impl_Type field.
13279
13280 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13281
13282 -- Build a freeze node if parent still needs one. Also make sure that
13283 -- the Depends_On_Private status is set because the subtype will need
13284 -- reprocessing at the time the base type does, and also we must set a
13285 -- conditional delay.
13286
13287 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13288 Conditional_Delay (Def_Id, T);
13289 end Constrain_Array;
13290
13291 ------------------------------
13292 -- Constrain_Component_Type --
13293 ------------------------------
13294
13295 function Constrain_Component_Type
13296 (Comp : Entity_Id;
13297 Constrained_Typ : Entity_Id;
13298 Related_Node : Node_Id;
13299 Typ : Entity_Id;
13300 Constraints : Elist_Id) return Entity_Id
13301 is
13302 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13303 Compon_Type : constant Entity_Id := Etype (Comp);
13304
13305 function Build_Constrained_Array_Type
13306 (Old_Type : Entity_Id) return Entity_Id;
13307 -- If Old_Type is an array type, one of whose indexes is constrained
13308 -- by a discriminant, build an Itype whose constraint replaces the
13309 -- discriminant with its value in the constraint.
13310
13311 function Build_Constrained_Discriminated_Type
13312 (Old_Type : Entity_Id) return Entity_Id;
13313 -- Ditto for record components. Handle the case where the constraint
13314 -- is a conversion of the discriminant value, introduced during
13315 -- expansion.
13316
13317 function Build_Constrained_Access_Type
13318 (Old_Type : Entity_Id) return Entity_Id;
13319 -- Ditto for access types. Makes use of previous two functions, to
13320 -- constrain designated type.
13321
13322 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13323 -- T is an array or discriminated type, C is a list of constraints
13324 -- that apply to T. This routine builds the constrained subtype.
13325
13326 function Is_Discriminant (Expr : Node_Id) return Boolean;
13327 -- Returns True if Expr is a discriminant
13328
13329 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13330 -- Find the value of discriminant Discrim in Constraint
13331
13332 -----------------------------------
13333 -- Build_Constrained_Access_Type --
13334 -----------------------------------
13335
13336 function Build_Constrained_Access_Type
13337 (Old_Type : Entity_Id) return Entity_Id
13338 is
13339 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13340 Itype : Entity_Id;
13341 Desig_Subtype : Entity_Id;
13342 Scop : Entity_Id;
13343
13344 begin
13345 -- if the original access type was not embedded in the enclosing
13346 -- type definition, there is no need to produce a new access
13347 -- subtype. In fact every access type with an explicit constraint
13348 -- generates an itype whose scope is the enclosing record.
13349
13350 if not Is_Type (Scope (Old_Type)) then
13351 return Old_Type;
13352
13353 elsif Is_Array_Type (Desig_Type) then
13354 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13355
13356 elsif Has_Discriminants (Desig_Type) then
13357
13358 -- This may be an access type to an enclosing record type for
13359 -- which we are constructing the constrained components. Return
13360 -- the enclosing record subtype. This is not always correct,
13361 -- but avoids infinite recursion. ???
13362
13363 Desig_Subtype := Any_Type;
13364
13365 for J in reverse 0 .. Scope_Stack.Last loop
13366 Scop := Scope_Stack.Table (J).Entity;
13367
13368 if Is_Type (Scop)
13369 and then Base_Type (Scop) = Base_Type (Desig_Type)
13370 then
13371 Desig_Subtype := Scop;
13372 end if;
13373
13374 exit when not Is_Type (Scop);
13375 end loop;
13376
13377 if Desig_Subtype = Any_Type then
13378 Desig_Subtype :=
13379 Build_Constrained_Discriminated_Type (Desig_Type);
13380 end if;
13381
13382 else
13383 return Old_Type;
13384 end if;
13385
13386 if Desig_Subtype /= Desig_Type then
13387
13388 -- The Related_Node better be here or else we won't be able
13389 -- to attach new itypes to a node in the tree.
13390
13391 pragma Assert (Present (Related_Node));
13392
13393 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13394
13395 Set_Etype (Itype, Base_Type (Old_Type));
13396 Set_Size_Info (Itype, (Old_Type));
13397 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13398 Set_Depends_On_Private (Itype, Has_Private_Component
13399 (Old_Type));
13400 Set_Is_Access_Constant (Itype, Is_Access_Constant
13401 (Old_Type));
13402
13403 -- The new itype needs freezing when it depends on a not frozen
13404 -- type and the enclosing subtype needs freezing.
13405
13406 if Has_Delayed_Freeze (Constrained_Typ)
13407 and then not Is_Frozen (Constrained_Typ)
13408 then
13409 Conditional_Delay (Itype, Base_Type (Old_Type));
13410 end if;
13411
13412 return Itype;
13413
13414 else
13415 return Old_Type;
13416 end if;
13417 end Build_Constrained_Access_Type;
13418
13419 ----------------------------------
13420 -- Build_Constrained_Array_Type --
13421 ----------------------------------
13422
13423 function Build_Constrained_Array_Type
13424 (Old_Type : Entity_Id) return Entity_Id
13425 is
13426 Lo_Expr : Node_Id;
13427 Hi_Expr : Node_Id;
13428 Old_Index : Node_Id;
13429 Range_Node : Node_Id;
13430 Constr_List : List_Id;
13431
13432 Need_To_Create_Itype : Boolean := False;
13433
13434 begin
13435 Old_Index := First_Index (Old_Type);
13436 while Present (Old_Index) loop
13437 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13438
13439 if Is_Discriminant (Lo_Expr)
13440 or else
13441 Is_Discriminant (Hi_Expr)
13442 then
13443 Need_To_Create_Itype := True;
13444 end if;
13445
13446 Next_Index (Old_Index);
13447 end loop;
13448
13449 if Need_To_Create_Itype then
13450 Constr_List := New_List;
13451
13452 Old_Index := First_Index (Old_Type);
13453 while Present (Old_Index) loop
13454 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13455
13456 if Is_Discriminant (Lo_Expr) then
13457 Lo_Expr := Get_Discr_Value (Lo_Expr);
13458 end if;
13459
13460 if Is_Discriminant (Hi_Expr) then
13461 Hi_Expr := Get_Discr_Value (Hi_Expr);
13462 end if;
13463
13464 Range_Node :=
13465 Make_Range
13466 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13467
13468 Append (Range_Node, To => Constr_List);
13469
13470 Next_Index (Old_Index);
13471 end loop;
13472
13473 return Build_Subtype (Old_Type, Constr_List);
13474
13475 else
13476 return Old_Type;
13477 end if;
13478 end Build_Constrained_Array_Type;
13479
13480 ------------------------------------------
13481 -- Build_Constrained_Discriminated_Type --
13482 ------------------------------------------
13483
13484 function Build_Constrained_Discriminated_Type
13485 (Old_Type : Entity_Id) return Entity_Id
13486 is
13487 Expr : Node_Id;
13488 Constr_List : List_Id;
13489 Old_Constraint : Elmt_Id;
13490
13491 Need_To_Create_Itype : Boolean := False;
13492
13493 begin
13494 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13495 while Present (Old_Constraint) loop
13496 Expr := Node (Old_Constraint);
13497
13498 if Is_Discriminant (Expr) then
13499 Need_To_Create_Itype := True;
13500
13501 -- After expansion of discriminated task types, the value
13502 -- of the discriminant may be converted to a run-time type
13503 -- for restricted run-times. Propagate the value of the
13504 -- discriminant as well, so that e.g. the secondary stack
13505 -- component has a static constraint. Necessary for LLVM.
13506
13507 elsif Nkind (Expr) = N_Type_Conversion
13508 and then Is_Discriminant (Expression (Expr))
13509 then
13510 Need_To_Create_Itype := True;
13511 end if;
13512
13513 Next_Elmt (Old_Constraint);
13514 end loop;
13515
13516 if Need_To_Create_Itype then
13517 Constr_List := New_List;
13518
13519 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13520 while Present (Old_Constraint) loop
13521 Expr := Node (Old_Constraint);
13522
13523 if Is_Discriminant (Expr) then
13524 Expr := Get_Discr_Value (Expr);
13525
13526 elsif Nkind (Expr) = N_Type_Conversion
13527 and then Is_Discriminant (Expression (Expr))
13528 then
13529 Expr := New_Copy_Tree (Expr);
13530 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13531 end if;
13532
13533 Append (New_Copy_Tree (Expr), To => Constr_List);
13534
13535 Next_Elmt (Old_Constraint);
13536 end loop;
13537
13538 return Build_Subtype (Old_Type, Constr_List);
13539
13540 else
13541 return Old_Type;
13542 end if;
13543 end Build_Constrained_Discriminated_Type;
13544
13545 -------------------
13546 -- Build_Subtype --
13547 -------------------
13548
13549 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13550 Indic : Node_Id;
13551 Subtyp_Decl : Node_Id;
13552 Def_Id : Entity_Id;
13553 Btyp : Entity_Id := Base_Type (T);
13554
13555 begin
13556 -- The Related_Node better be here or else we won't be able to
13557 -- attach new itypes to a node in the tree.
13558
13559 pragma Assert (Present (Related_Node));
13560
13561 -- If the view of the component's type is incomplete or private
13562 -- with unknown discriminants, then the constraint must be applied
13563 -- to the full type.
13564
13565 if Has_Unknown_Discriminants (Btyp)
13566 and then Present (Underlying_Type (Btyp))
13567 then
13568 Btyp := Underlying_Type (Btyp);
13569 end if;
13570
13571 Indic :=
13572 Make_Subtype_Indication (Loc,
13573 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13574 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13575
13576 Def_Id := Create_Itype (Ekind (T), Related_Node);
13577
13578 Subtyp_Decl :=
13579 Make_Subtype_Declaration (Loc,
13580 Defining_Identifier => Def_Id,
13581 Subtype_Indication => Indic);
13582
13583 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13584
13585 -- Itypes must be analyzed with checks off (see package Itypes)
13586
13587 Analyze (Subtyp_Decl, Suppress => All_Checks);
13588
13589 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13590 Inherit_Predicate_Flags (Def_Id, T);
13591
13592 -- Indicate where the predicate function may be found
13593
13594 if Is_Itype (T) then
13595 if Present (Predicate_Function (Def_Id)) then
13596 null;
13597
13598 elsif Present (Predicate_Function (T)) then
13599 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13600
13601 else
13602 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13603 end if;
13604
13605 elsif No (Predicate_Function (Def_Id)) then
13606 Set_Predicated_Parent (Def_Id, T);
13607 end if;
13608 end if;
13609
13610 return Def_Id;
13611 end Build_Subtype;
13612
13613 ---------------------
13614 -- Get_Discr_Value --
13615 ---------------------
13616
13617 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13618 D : Entity_Id;
13619 E : Elmt_Id;
13620
13621 begin
13622 -- The discriminant may be declared for the type, in which case we
13623 -- find it by iterating over the list of discriminants. If the
13624 -- discriminant is inherited from a parent type, it appears as the
13625 -- corresponding discriminant of the current type. This will be the
13626 -- case when constraining an inherited component whose constraint is
13627 -- given by a discriminant of the parent.
13628
13629 D := First_Discriminant (Typ);
13630 E := First_Elmt (Constraints);
13631
13632 while Present (D) loop
13633 if D = Entity (Discrim)
13634 or else D = CR_Discriminant (Entity (Discrim))
13635 or else Corresponding_Discriminant (D) = Entity (Discrim)
13636 then
13637 return Node (E);
13638 end if;
13639
13640 Next_Discriminant (D);
13641 Next_Elmt (E);
13642 end loop;
13643
13644 -- The Corresponding_Discriminant mechanism is incomplete, because
13645 -- the correspondence between new and old discriminants is not one
13646 -- to one: one new discriminant can constrain several old ones. In
13647 -- that case, scan sequentially the stored_constraint, the list of
13648 -- discriminants of the parents, and the constraints.
13649
13650 -- Previous code checked for the present of the Stored_Constraint
13651 -- list for the derived type, but did not use it at all. Should it
13652 -- be present when the component is a discriminated task type?
13653
13654 if Is_Derived_Type (Typ)
13655 and then Scope (Entity (Discrim)) = Etype (Typ)
13656 then
13657 D := First_Discriminant (Etype (Typ));
13658 E := First_Elmt (Constraints);
13659 while Present (D) loop
13660 if D = Entity (Discrim) then
13661 return Node (E);
13662 end if;
13663
13664 Next_Discriminant (D);
13665 Next_Elmt (E);
13666 end loop;
13667 end if;
13668
13669 -- Something is wrong if we did not find the value
13670
13671 raise Program_Error;
13672 end Get_Discr_Value;
13673
13674 ---------------------
13675 -- Is_Discriminant --
13676 ---------------------
13677
13678 function Is_Discriminant (Expr : Node_Id) return Boolean is
13679 Discrim_Scope : Entity_Id;
13680
13681 begin
13682 if Denotes_Discriminant (Expr) then
13683 Discrim_Scope := Scope (Entity (Expr));
13684
13685 -- Either we have a reference to one of Typ's discriminants,
13686
13687 pragma Assert (Discrim_Scope = Typ
13688
13689 -- or to the discriminants of the parent type, in the case
13690 -- of a derivation of a tagged type with variants.
13691
13692 or else Discrim_Scope = Etype (Typ)
13693 or else Full_View (Discrim_Scope) = Etype (Typ)
13694
13695 -- or same as above for the case where the discriminants
13696 -- were declared in Typ's private view.
13697
13698 or else (Is_Private_Type (Discrim_Scope)
13699 and then Chars (Discrim_Scope) = Chars (Typ))
13700
13701 -- or else we are deriving from the full view and the
13702 -- discriminant is declared in the private entity.
13703
13704 or else (Is_Private_Type (Typ)
13705 and then Chars (Discrim_Scope) = Chars (Typ))
13706
13707 -- Or we are constrained the corresponding record of a
13708 -- synchronized type that completes a private declaration.
13709
13710 or else (Is_Concurrent_Record_Type (Typ)
13711 and then
13712 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13713
13714 -- or we have a class-wide type, in which case make sure the
13715 -- discriminant found belongs to the root type.
13716
13717 or else (Is_Class_Wide_Type (Typ)
13718 and then Etype (Typ) = Discrim_Scope));
13719
13720 return True;
13721 end if;
13722
13723 -- In all other cases we have something wrong
13724
13725 return False;
13726 end Is_Discriminant;
13727
13728 -- Start of processing for Constrain_Component_Type
13729
13730 begin
13731 if Nkind (Parent (Comp)) = N_Component_Declaration
13732 and then Comes_From_Source (Parent (Comp))
13733 and then Comes_From_Source
13734 (Subtype_Indication (Component_Definition (Parent (Comp))))
13735 and then
13736 Is_Entity_Name
13737 (Subtype_Indication (Component_Definition (Parent (Comp))))
13738 then
13739 return Compon_Type;
13740
13741 elsif Is_Array_Type (Compon_Type) then
13742 return Build_Constrained_Array_Type (Compon_Type);
13743
13744 elsif Has_Discriminants (Compon_Type) then
13745 return Build_Constrained_Discriminated_Type (Compon_Type);
13746
13747 elsif Is_Access_Type (Compon_Type) then
13748 return Build_Constrained_Access_Type (Compon_Type);
13749
13750 else
13751 return Compon_Type;
13752 end if;
13753 end Constrain_Component_Type;
13754
13755 --------------------------
13756 -- Constrain_Concurrent --
13757 --------------------------
13758
13759 -- For concurrent types, the associated record value type carries the same
13760 -- discriminants, so when we constrain a concurrent type, we must constrain
13761 -- the corresponding record type as well.
13762
13763 procedure Constrain_Concurrent
13764 (Def_Id : in out Entity_Id;
13765 SI : Node_Id;
13766 Related_Nod : Node_Id;
13767 Related_Id : Entity_Id;
13768 Suffix : Character)
13769 is
13770 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13771 -- case of a private subtype (needed when only doing semantic analysis).
13772
13773 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13774 T_Val : Entity_Id;
13775
13776 begin
13777 if Is_Access_Type (T_Ent) then
13778 T_Ent := Designated_Type (T_Ent);
13779 end if;
13780
13781 T_Val := Corresponding_Record_Type (T_Ent);
13782
13783 if Present (T_Val) then
13784
13785 if No (Def_Id) then
13786 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13787
13788 -- Elaborate itype now, as it may be used in a subsequent
13789 -- synchronized operation in another scope.
13790
13791 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13792 Build_Itype_Reference (Def_Id, Related_Nod);
13793 end if;
13794 end if;
13795
13796 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13797 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13798
13799 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13800 Set_Corresponding_Record_Type (Def_Id,
13801 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13802
13803 else
13804 -- If there is no associated record, expansion is disabled and this
13805 -- is a generic context. Create a subtype in any case, so that
13806 -- semantic analysis can proceed.
13807
13808 if No (Def_Id) then
13809 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13810 end if;
13811
13812 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13813 end if;
13814 end Constrain_Concurrent;
13815
13816 ------------------------------------
13817 -- Constrain_Corresponding_Record --
13818 ------------------------------------
13819
13820 function Constrain_Corresponding_Record
13821 (Prot_Subt : Entity_Id;
13822 Corr_Rec : Entity_Id;
13823 Related_Nod : Node_Id) return Entity_Id
13824 is
13825 T_Sub : constant Entity_Id :=
13826 Create_Itype
13827 (Ekind => E_Record_Subtype,
13828 Related_Nod => Related_Nod,
13829 Related_Id => Corr_Rec,
13830 Suffix => 'C',
13831 Suffix_Index => -1);
13832
13833 begin
13834 Set_Etype (T_Sub, Corr_Rec);
13835 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13836 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
13837 Set_Is_Constrained (T_Sub, True);
13838 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13839 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13840
13841 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13842 Set_Discriminant_Constraint
13843 (T_Sub, Discriminant_Constraint (Prot_Subt));
13844 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13845 Create_Constrained_Components
13846 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13847 end if;
13848
13849 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13850
13851 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13852 Conditional_Delay (T_Sub, Corr_Rec);
13853
13854 else
13855 -- This is a component subtype: it will be frozen in the context of
13856 -- the enclosing record's init_proc, so that discriminant references
13857 -- are resolved to discriminals. (Note: we used to skip freezing
13858 -- altogether in that case, which caused errors downstream for
13859 -- components of a bit packed array type).
13860
13861 Set_Has_Delayed_Freeze (T_Sub);
13862 end if;
13863
13864 return T_Sub;
13865 end Constrain_Corresponding_Record;
13866
13867 -----------------------
13868 -- Constrain_Decimal --
13869 -----------------------
13870
13871 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13872 T : constant Entity_Id := Entity (Subtype_Mark (S));
13873 C : constant Node_Id := Constraint (S);
13874 Loc : constant Source_Ptr := Sloc (C);
13875 Range_Expr : Node_Id;
13876 Digits_Expr : Node_Id;
13877 Digits_Val : Uint;
13878 Bound_Val : Ureal;
13879
13880 begin
13881 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13882
13883 if Nkind (C) = N_Range_Constraint then
13884 Range_Expr := Range_Expression (C);
13885 Digits_Val := Digits_Value (T);
13886
13887 else
13888 pragma Assert (Nkind (C) = N_Digits_Constraint);
13889
13890 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13891
13892 Digits_Expr := Digits_Expression (C);
13893 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13894
13895 Check_Digits_Expression (Digits_Expr);
13896 Digits_Val := Expr_Value (Digits_Expr);
13897
13898 if Digits_Val > Digits_Value (T) then
13899 Error_Msg_N
13900 ("digits expression is incompatible with subtype", C);
13901 Digits_Val := Digits_Value (T);
13902 end if;
13903
13904 if Present (Range_Constraint (C)) then
13905 Range_Expr := Range_Expression (Range_Constraint (C));
13906 else
13907 Range_Expr := Empty;
13908 end if;
13909 end if;
13910
13911 Set_Etype (Def_Id, Base_Type (T));
13912 Set_Size_Info (Def_Id, (T));
13913 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13914 Set_Delta_Value (Def_Id, Delta_Value (T));
13915 Set_Scale_Value (Def_Id, Scale_Value (T));
13916 Set_Small_Value (Def_Id, Small_Value (T));
13917 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13918 Set_Digits_Value (Def_Id, Digits_Val);
13919
13920 -- Manufacture range from given digits value if no range present
13921
13922 if No (Range_Expr) then
13923 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13924 Range_Expr :=
13925 Make_Range (Loc,
13926 Low_Bound =>
13927 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13928 High_Bound =>
13929 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13930 end if;
13931
13932 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13933 Set_Discrete_RM_Size (Def_Id);
13934
13935 -- Unconditionally delay the freeze, since we cannot set size
13936 -- information in all cases correctly until the freeze point.
13937
13938 Set_Has_Delayed_Freeze (Def_Id);
13939 end Constrain_Decimal;
13940
13941 ----------------------------------
13942 -- Constrain_Discriminated_Type --
13943 ----------------------------------
13944
13945 procedure Constrain_Discriminated_Type
13946 (Def_Id : Entity_Id;
13947 S : Node_Id;
13948 Related_Nod : Node_Id;
13949 For_Access : Boolean := False)
13950 is
13951 E : Entity_Id := Entity (Subtype_Mark (S));
13952 T : Entity_Id;
13953
13954 procedure Fixup_Bad_Constraint;
13955 -- Called after finding a bad constraint, and after having posted an
13956 -- appropriate error message. The goal is to leave type Def_Id in as
13957 -- reasonable state as possible.
13958
13959 --------------------------
13960 -- Fixup_Bad_Constraint --
13961 --------------------------
13962
13963 procedure Fixup_Bad_Constraint is
13964 begin
13965 -- Set a reasonable Ekind for the entity, including incomplete types.
13966
13967 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13968
13969 -- Set Etype to the known type, to reduce chances of cascaded errors
13970
13971 Set_Etype (Def_Id, E);
13972 Set_Error_Posted (Def_Id);
13973 end Fixup_Bad_Constraint;
13974
13975 -- Local variables
13976
13977 C : Node_Id;
13978 Constr : Elist_Id := New_Elmt_List;
13979
13980 -- Start of processing for Constrain_Discriminated_Type
13981
13982 begin
13983 C := Constraint (S);
13984
13985 -- A discriminant constraint is only allowed in a subtype indication,
13986 -- after a subtype mark. This subtype mark must denote either a type
13987 -- with discriminants, or an access type whose designated type is a
13988 -- type with discriminants. A discriminant constraint specifies the
13989 -- values of these discriminants (RM 3.7.2(5)).
13990
13991 T := Base_Type (Entity (Subtype_Mark (S)));
13992
13993 if Is_Access_Type (T) then
13994 T := Designated_Type (T);
13995 end if;
13996
13997 -- In an instance it may be necessary to retrieve the full view of a
13998 -- type with unknown discriminants, or a full view with defaulted
13999 -- discriminants. In other contexts the constraint is illegal.
14000
14001 if In_Instance
14002 and then Is_Private_Type (T)
14003 and then Present (Full_View (T))
14004 and then
14005 (Has_Unknown_Discriminants (T)
14006 or else
14007 (not Has_Discriminants (T)
14008 and then Has_Discriminants (Full_View (T))
14009 and then Present (Discriminant_Default_Value
14010 (First_Discriminant (Full_View (T))))))
14011 then
14012 T := Full_View (T);
14013 E := Full_View (E);
14014 end if;
14015
14016 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14017 -- generating an error for access-to-incomplete subtypes.
14018
14019 if Ada_Version >= Ada_2005
14020 and then Ekind (T) = E_Incomplete_Type
14021 and then Nkind (Parent (S)) = N_Subtype_Declaration
14022 and then not Is_Itype (Def_Id)
14023 then
14024 -- A little sanity check: emit an error message if the type has
14025 -- discriminants to begin with. Type T may be a regular incomplete
14026 -- type or imported via a limited with clause.
14027
14028 if Has_Discriminants (T)
14029 or else (From_Limited_With (T)
14030 and then Present (Non_Limited_View (T))
14031 and then Nkind (Parent (Non_Limited_View (T))) =
14032 N_Full_Type_Declaration
14033 and then Present (Discriminant_Specifications
14034 (Parent (Non_Limited_View (T)))))
14035 then
14036 Error_Msg_N
14037 ("(Ada 2005) incomplete subtype may not be constrained", C);
14038 else
14039 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14040 end if;
14041
14042 Fixup_Bad_Constraint;
14043 return;
14044
14045 -- Check that the type has visible discriminants. The type may be
14046 -- a private type with unknown discriminants whose full view has
14047 -- discriminants which are invisible.
14048
14049 elsif not Has_Discriminants (T)
14050 or else
14051 (Has_Unknown_Discriminants (T)
14052 and then Is_Private_Type (T))
14053 then
14054 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14055 Fixup_Bad_Constraint;
14056 return;
14057
14058 elsif Is_Constrained (E)
14059 or else (Ekind (E) = E_Class_Wide_Subtype
14060 and then Present (Discriminant_Constraint (E)))
14061 then
14062 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14063 Fixup_Bad_Constraint;
14064 return;
14065 end if;
14066
14067 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14068 -- applies to the base type.
14069
14070 T := Base_Type (T);
14071
14072 Constr := Build_Discriminant_Constraints (T, S);
14073
14074 -- If the list returned was empty we had an error in building the
14075 -- discriminant constraint. We have also already signalled an error
14076 -- in the incomplete type case
14077
14078 if Is_Empty_Elmt_List (Constr) then
14079 Fixup_Bad_Constraint;
14080 return;
14081 end if;
14082
14083 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14084 end Constrain_Discriminated_Type;
14085
14086 ---------------------------
14087 -- Constrain_Enumeration --
14088 ---------------------------
14089
14090 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
14091 T : constant Entity_Id := Entity (Subtype_Mark (S));
14092 C : constant Node_Id := Constraint (S);
14093
14094 begin
14095 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14096
14097 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14098
14099 Set_Etype (Def_Id, Base_Type (T));
14100 Set_Size_Info (Def_Id, (T));
14101 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14102 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14103
14104 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14105
14106 Set_Discrete_RM_Size (Def_Id);
14107 end Constrain_Enumeration;
14108
14109 ----------------------
14110 -- Constrain_Float --
14111 ----------------------
14112
14113 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14114 T : constant Entity_Id := Entity (Subtype_Mark (S));
14115 C : Node_Id;
14116 D : Node_Id;
14117 Rais : Node_Id;
14118
14119 begin
14120 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14121
14122 Set_Etype (Def_Id, Base_Type (T));
14123 Set_Size_Info (Def_Id, (T));
14124 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14125
14126 -- Process the constraint
14127
14128 C := Constraint (S);
14129
14130 -- Digits constraint present
14131
14132 if Nkind (C) = N_Digits_Constraint then
14133
14134 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
14135 Check_Restriction (No_Obsolescent_Features, C);
14136
14137 if Warn_On_Obsolescent_Feature then
14138 Error_Msg_N
14139 ("subtype digits constraint is an " &
14140 "obsolescent feature (RM J.3(8))?j?", C);
14141 end if;
14142
14143 D := Digits_Expression (C);
14144 Analyze_And_Resolve (D, Any_Integer);
14145 Check_Digits_Expression (D);
14146 Set_Digits_Value (Def_Id, Expr_Value (D));
14147
14148 -- Check that digits value is in range. Obviously we can do this
14149 -- at compile time, but it is strictly a runtime check, and of
14150 -- course there is an ACVC test that checks this.
14151
14152 if Digits_Value (Def_Id) > Digits_Value (T) then
14153 Error_Msg_Uint_1 := Digits_Value (T);
14154 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14155 Rais :=
14156 Make_Raise_Constraint_Error (Sloc (D),
14157 Reason => CE_Range_Check_Failed);
14158 Insert_Action (Declaration_Node (Def_Id), Rais);
14159 end if;
14160
14161 C := Range_Constraint (C);
14162
14163 -- No digits constraint present
14164
14165 else
14166 Set_Digits_Value (Def_Id, Digits_Value (T));
14167 end if;
14168
14169 -- Range constraint present
14170
14171 if Nkind (C) = N_Range_Constraint then
14172 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14173
14174 -- No range constraint present
14175
14176 else
14177 pragma Assert (No (C));
14178 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14179 end if;
14180
14181 Set_Is_Constrained (Def_Id);
14182 end Constrain_Float;
14183
14184 ---------------------
14185 -- Constrain_Index --
14186 ---------------------
14187
14188 procedure Constrain_Index
14189 (Index : Node_Id;
14190 S : Node_Id;
14191 Related_Nod : Node_Id;
14192 Related_Id : Entity_Id;
14193 Suffix : Character;
14194 Suffix_Index : Nat)
14195 is
14196 Def_Id : Entity_Id;
14197 R : Node_Id := Empty;
14198 T : constant Entity_Id := Etype (Index);
14199
14200 begin
14201 Def_Id :=
14202 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14203 Set_Etype (Def_Id, Base_Type (T));
14204
14205 if Nkind (S) = N_Range
14206 or else
14207 (Nkind (S) = N_Attribute_Reference
14208 and then Attribute_Name (S) = Name_Range)
14209 then
14210 -- A Range attribute will be transformed into N_Range by Resolve
14211
14212 Analyze (S);
14213 Set_Etype (S, T);
14214 R := S;
14215
14216 Process_Range_Expr_In_Decl (R, T);
14217
14218 if not Error_Posted (S)
14219 and then
14220 (Nkind (S) /= N_Range
14221 or else not Covers (T, (Etype (Low_Bound (S))))
14222 or else not Covers (T, (Etype (High_Bound (S)))))
14223 then
14224 if Base_Type (T) /= Any_Type
14225 and then Etype (Low_Bound (S)) /= Any_Type
14226 and then Etype (High_Bound (S)) /= Any_Type
14227 then
14228 Error_Msg_N ("range expected", S);
14229 end if;
14230 end if;
14231
14232 elsif Nkind (S) = N_Subtype_Indication then
14233
14234 -- The parser has verified that this is a discrete indication
14235
14236 Resolve_Discrete_Subtype_Indication (S, T);
14237 Bad_Predicated_Subtype_Use
14238 ("subtype& has predicate, not allowed in index constraint",
14239 S, Entity (Subtype_Mark (S)));
14240
14241 R := Range_Expression (Constraint (S));
14242
14243 -- Capture values of bounds and generate temporaries for them if
14244 -- needed, since checks may cause duplication of the expressions
14245 -- which must not be reevaluated.
14246
14247 -- The forced evaluation removes side effects from expressions, which
14248 -- should occur also in GNATprove mode. Otherwise, we end up with
14249 -- unexpected insertions of actions at places where this is not
14250 -- supposed to occur, e.g. on default parameters of a call.
14251
14252 if Expander_Active or GNATprove_Mode then
14253 Force_Evaluation
14254 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14255 Force_Evaluation
14256 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14257 end if;
14258
14259 elsif Nkind (S) = N_Discriminant_Association then
14260
14261 -- Syntactically valid in subtype indication
14262
14263 Error_Msg_N ("invalid index constraint", S);
14264 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14265 return;
14266
14267 -- Subtype_Mark case, no anonymous subtypes to construct
14268
14269 else
14270 Analyze (S);
14271
14272 if Is_Entity_Name (S) then
14273 if not Is_Type (Entity (S)) then
14274 Error_Msg_N ("expect subtype mark for index constraint", S);
14275
14276 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14277 Wrong_Type (S, Base_Type (T));
14278
14279 -- Check error of subtype with predicate in index constraint
14280
14281 else
14282 Bad_Predicated_Subtype_Use
14283 ("subtype& has predicate, not allowed in index constraint",
14284 S, Entity (S));
14285 end if;
14286
14287 return;
14288
14289 else
14290 Error_Msg_N ("invalid index constraint", S);
14291 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14292 return;
14293 end if;
14294 end if;
14295
14296 -- Complete construction of the Itype
14297
14298 if Is_Modular_Integer_Type (T) then
14299 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14300
14301 elsif Is_Integer_Type (T) then
14302 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14303
14304 else
14305 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14306 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14307 Set_First_Literal (Def_Id, First_Literal (T));
14308 end if;
14309
14310 Set_Size_Info (Def_Id, (T));
14311 Set_RM_Size (Def_Id, RM_Size (T));
14312 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14313
14314 Set_Scalar_Range (Def_Id, R);
14315
14316 Set_Etype (S, Def_Id);
14317 Set_Discrete_RM_Size (Def_Id);
14318 end Constrain_Index;
14319
14320 -----------------------
14321 -- Constrain_Integer --
14322 -----------------------
14323
14324 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14325 T : constant Entity_Id := Entity (Subtype_Mark (S));
14326 C : constant Node_Id := Constraint (S);
14327
14328 begin
14329 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14330
14331 if Is_Modular_Integer_Type (T) then
14332 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14333 else
14334 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14335 end if;
14336
14337 Set_Etype (Def_Id, Base_Type (T));
14338 Set_Size_Info (Def_Id, (T));
14339 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14340 Set_Discrete_RM_Size (Def_Id);
14341 end Constrain_Integer;
14342
14343 ------------------------------
14344 -- Constrain_Ordinary_Fixed --
14345 ------------------------------
14346
14347 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14348 T : constant Entity_Id := Entity (Subtype_Mark (S));
14349 C : Node_Id;
14350 D : Node_Id;
14351 Rais : Node_Id;
14352
14353 begin
14354 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14355 Set_Etype (Def_Id, Base_Type (T));
14356 Set_Size_Info (Def_Id, (T));
14357 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14358 Set_Small_Value (Def_Id, Small_Value (T));
14359
14360 -- Process the constraint
14361
14362 C := Constraint (S);
14363
14364 -- Delta constraint present
14365
14366 if Nkind (C) = N_Delta_Constraint then
14367
14368 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14369 Check_Restriction (No_Obsolescent_Features, C);
14370
14371 if Warn_On_Obsolescent_Feature then
14372 Error_Msg_S
14373 ("subtype delta constraint is an " &
14374 "obsolescent feature (RM J.3(7))?j?");
14375 end if;
14376
14377 D := Delta_Expression (C);
14378 Analyze_And_Resolve (D, Any_Real);
14379 Check_Delta_Expression (D);
14380 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14381
14382 -- Check that delta value is in range. Obviously we can do this
14383 -- at compile time, but it is strictly a runtime check, and of
14384 -- course there is an ACVC test that checks this.
14385
14386 if Delta_Value (Def_Id) < Delta_Value (T) then
14387 Error_Msg_N ("??delta value is too small", D);
14388 Rais :=
14389 Make_Raise_Constraint_Error (Sloc (D),
14390 Reason => CE_Range_Check_Failed);
14391 Insert_Action (Declaration_Node (Def_Id), Rais);
14392 end if;
14393
14394 C := Range_Constraint (C);
14395
14396 -- No delta constraint present
14397
14398 else
14399 Set_Delta_Value (Def_Id, Delta_Value (T));
14400 end if;
14401
14402 -- Range constraint present
14403
14404 if Nkind (C) = N_Range_Constraint then
14405 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14406
14407 -- No range constraint present
14408
14409 else
14410 pragma Assert (No (C));
14411 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14412 end if;
14413
14414 Set_Discrete_RM_Size (Def_Id);
14415
14416 -- Unconditionally delay the freeze, since we cannot set size
14417 -- information in all cases correctly until the freeze point.
14418
14419 Set_Has_Delayed_Freeze (Def_Id);
14420 end Constrain_Ordinary_Fixed;
14421
14422 -----------------------
14423 -- Contain_Interface --
14424 -----------------------
14425
14426 function Contain_Interface
14427 (Iface : Entity_Id;
14428 Ifaces : Elist_Id) return Boolean
14429 is
14430 Iface_Elmt : Elmt_Id;
14431
14432 begin
14433 if Present (Ifaces) then
14434 Iface_Elmt := First_Elmt (Ifaces);
14435 while Present (Iface_Elmt) loop
14436 if Node (Iface_Elmt) = Iface then
14437 return True;
14438 end if;
14439
14440 Next_Elmt (Iface_Elmt);
14441 end loop;
14442 end if;
14443
14444 return False;
14445 end Contain_Interface;
14446
14447 ---------------------------
14448 -- Convert_Scalar_Bounds --
14449 ---------------------------
14450
14451 procedure Convert_Scalar_Bounds
14452 (N : Node_Id;
14453 Parent_Type : Entity_Id;
14454 Derived_Type : Entity_Id;
14455 Loc : Source_Ptr)
14456 is
14457 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14458
14459 Lo : Node_Id;
14460 Hi : Node_Id;
14461 Rng : Node_Id;
14462
14463 begin
14464 -- Defend against previous errors
14465
14466 if No (Scalar_Range (Derived_Type)) then
14467 Check_Error_Detected;
14468 return;
14469 end if;
14470
14471 Lo := Build_Scalar_Bound
14472 (Type_Low_Bound (Derived_Type),
14473 Parent_Type, Implicit_Base);
14474
14475 Hi := Build_Scalar_Bound
14476 (Type_High_Bound (Derived_Type),
14477 Parent_Type, Implicit_Base);
14478
14479 Rng :=
14480 Make_Range (Loc,
14481 Low_Bound => Lo,
14482 High_Bound => Hi);
14483
14484 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14485
14486 Set_Parent (Rng, N);
14487 Set_Scalar_Range (Derived_Type, Rng);
14488
14489 -- Analyze the bounds
14490
14491 Analyze_And_Resolve (Lo, Implicit_Base);
14492 Analyze_And_Resolve (Hi, Implicit_Base);
14493
14494 -- Analyze the range itself, except that we do not analyze it if
14495 -- the bounds are real literals, and we have a fixed-point type.
14496 -- The reason for this is that we delay setting the bounds in this
14497 -- case till we know the final Small and Size values (see circuit
14498 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14499
14500 if Is_Fixed_Point_Type (Parent_Type)
14501 and then Nkind (Lo) = N_Real_Literal
14502 and then Nkind (Hi) = N_Real_Literal
14503 then
14504 return;
14505
14506 -- Here we do the analysis of the range
14507
14508 -- Note: we do this manually, since if we do a normal Analyze and
14509 -- Resolve call, there are problems with the conversions used for
14510 -- the derived type range.
14511
14512 else
14513 Set_Etype (Rng, Implicit_Base);
14514 Set_Analyzed (Rng, True);
14515 end if;
14516 end Convert_Scalar_Bounds;
14517
14518 -------------------
14519 -- Copy_And_Swap --
14520 -------------------
14521
14522 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14523 begin
14524 -- Initialize new full declaration entity by copying the pertinent
14525 -- fields of the corresponding private declaration entity.
14526
14527 -- We temporarily set Ekind to a value appropriate for a type to
14528 -- avoid assert failures in Einfo from checking for setting type
14529 -- attributes on something that is not a type. Ekind (Priv) is an
14530 -- appropriate choice, since it allowed the attributes to be set
14531 -- in the first place. This Ekind value will be modified later.
14532
14533 Set_Ekind (Full, Ekind (Priv));
14534
14535 -- Also set Etype temporarily to Any_Type, again, in the absence
14536 -- of errors, it will be properly reset, and if there are errors,
14537 -- then we want a value of Any_Type to remain.
14538
14539 Set_Etype (Full, Any_Type);
14540
14541 -- Now start copying attributes
14542
14543 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14544
14545 if Has_Discriminants (Full) then
14546 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14547 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14548 end if;
14549
14550 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14551 Set_Homonym (Full, Homonym (Priv));
14552 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14553 Set_Is_Public (Full, Is_Public (Priv));
14554 Set_Is_Pure (Full, Is_Pure (Priv));
14555 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14556 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14557 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14558 Set_Has_Pragma_Unreferenced_Objects
14559 (Full, Has_Pragma_Unreferenced_Objects
14560 (Priv));
14561
14562 Conditional_Delay (Full, Priv);
14563
14564 if Is_Tagged_Type (Full) then
14565 Set_Direct_Primitive_Operations
14566 (Full, Direct_Primitive_Operations (Priv));
14567 Set_No_Tagged_Streams_Pragma
14568 (Full, No_Tagged_Streams_Pragma (Priv));
14569
14570 if Is_Base_Type (Priv) then
14571 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14572 end if;
14573 end if;
14574
14575 Set_Is_Volatile (Full, Is_Volatile (Priv));
14576 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14577 Set_Scope (Full, Scope (Priv));
14578 Set_Prev_Entity (Full, Prev_Entity (Priv));
14579 Set_Next_Entity (Full, Next_Entity (Priv));
14580 Set_First_Entity (Full, First_Entity (Priv));
14581 Set_Last_Entity (Full, Last_Entity (Priv));
14582
14583 -- If access types have been recorded for later handling, keep them in
14584 -- the full view so that they get handled when the full view freeze
14585 -- node is expanded.
14586
14587 if Present (Freeze_Node (Priv))
14588 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14589 then
14590 Ensure_Freeze_Node (Full);
14591 Set_Access_Types_To_Process
14592 (Freeze_Node (Full),
14593 Access_Types_To_Process (Freeze_Node (Priv)));
14594 end if;
14595
14596 -- Swap the two entities. Now Private is the full type entity and Full
14597 -- is the private one. They will be swapped back at the end of the
14598 -- private part. This swapping ensures that the entity that is visible
14599 -- in the private part is the full declaration.
14600
14601 Exchange_Entities (Priv, Full);
14602 Append_Entity (Full, Scope (Full));
14603 end Copy_And_Swap;
14604
14605 -------------------------------------
14606 -- Copy_Array_Base_Type_Attributes --
14607 -------------------------------------
14608
14609 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14610 begin
14611 Set_Component_Alignment (T1, Component_Alignment (T2));
14612 Set_Component_Type (T1, Component_Type (T2));
14613 Set_Component_Size (T1, Component_Size (T2));
14614 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14615 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14616 Propagate_Concurrent_Flags (T1, T2);
14617 Set_Is_Packed (T1, Is_Packed (T2));
14618 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14619 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14620 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14621 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14622 end Copy_Array_Base_Type_Attributes;
14623
14624 -----------------------------------
14625 -- Copy_Array_Subtype_Attributes --
14626 -----------------------------------
14627
14628 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14629 begin
14630 Set_Size_Info (T1, T2);
14631
14632 Set_First_Index (T1, First_Index (T2));
14633 Set_Is_Aliased (T1, Is_Aliased (T2));
14634 Set_Is_Atomic (T1, Is_Atomic (T2));
14635 Set_Is_Independent (T1, Is_Independent (T2));
14636 Set_Is_Volatile (T1, Is_Volatile (T2));
14637 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14638 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14639 Set_Is_Constrained (T1, Is_Constrained (T2));
14640 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14641 Inherit_Rep_Item_Chain (T1, T2);
14642 Set_Convention (T1, Convention (T2));
14643 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14644 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14645 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14646 end Copy_Array_Subtype_Attributes;
14647
14648 -----------------------------------
14649 -- Create_Constrained_Components --
14650 -----------------------------------
14651
14652 procedure Create_Constrained_Components
14653 (Subt : Entity_Id;
14654 Decl_Node : Node_Id;
14655 Typ : Entity_Id;
14656 Constraints : Elist_Id)
14657 is
14658 Loc : constant Source_Ptr := Sloc (Subt);
14659 Comp_List : constant Elist_Id := New_Elmt_List;
14660 Parent_Type : constant Entity_Id := Etype (Typ);
14661 Assoc_List : constant List_Id := New_List;
14662 Discr_Val : Elmt_Id;
14663 Errors : Boolean;
14664 New_C : Entity_Id;
14665 Old_C : Entity_Id;
14666 Is_Static : Boolean := True;
14667
14668 procedure Collect_Fixed_Components (Typ : Entity_Id);
14669 -- Collect parent type components that do not appear in a variant part
14670
14671 procedure Create_All_Components;
14672 -- Iterate over Comp_List to create the components of the subtype
14673
14674 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14675 -- Creates a new component from Old_Compon, copying all the fields from
14676 -- it, including its Etype, inserts the new component in the Subt entity
14677 -- chain and returns the new component.
14678
14679 function Is_Variant_Record (T : Entity_Id) return Boolean;
14680 -- If true, and discriminants are static, collect only components from
14681 -- variants selected by discriminant values.
14682
14683 ------------------------------
14684 -- Collect_Fixed_Components --
14685 ------------------------------
14686
14687 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14688 begin
14689 -- Build association list for discriminants, and find components of the
14690 -- variant part selected by the values of the discriminants.
14691
14692 Old_C := First_Discriminant (Typ);
14693 Discr_Val := First_Elmt (Constraints);
14694 while Present (Old_C) loop
14695 Append_To (Assoc_List,
14696 Make_Component_Association (Loc,
14697 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14698 Expression => New_Copy (Node (Discr_Val))));
14699
14700 Next_Elmt (Discr_Val);
14701 Next_Discriminant (Old_C);
14702 end loop;
14703
14704 -- The tag and the possible parent component are unconditionally in
14705 -- the subtype.
14706
14707 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14708 Old_C := First_Component (Typ);
14709 while Present (Old_C) loop
14710 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14711 Append_Elmt (Old_C, Comp_List);
14712 end if;
14713
14714 Next_Component (Old_C);
14715 end loop;
14716 end if;
14717 end Collect_Fixed_Components;
14718
14719 ---------------------------
14720 -- Create_All_Components --
14721 ---------------------------
14722
14723 procedure Create_All_Components is
14724 Comp : Elmt_Id;
14725
14726 begin
14727 Comp := First_Elmt (Comp_List);
14728 while Present (Comp) loop
14729 Old_C := Node (Comp);
14730 New_C := Create_Component (Old_C);
14731
14732 Set_Etype
14733 (New_C,
14734 Constrain_Component_Type
14735 (Old_C, Subt, Decl_Node, Typ, Constraints));
14736 Set_Is_Public (New_C, Is_Public (Subt));
14737
14738 Next_Elmt (Comp);
14739 end loop;
14740 end Create_All_Components;
14741
14742 ----------------------
14743 -- Create_Component --
14744 ----------------------
14745
14746 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14747 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14748
14749 begin
14750 if Ekind (Old_Compon) = E_Discriminant
14751 and then Is_Completely_Hidden (Old_Compon)
14752 then
14753 -- This is a shadow discriminant created for a discriminant of
14754 -- the parent type, which needs to be present in the subtype.
14755 -- Give the shadow discriminant an internal name that cannot
14756 -- conflict with that of visible components.
14757
14758 Set_Chars (New_Compon, New_Internal_Name ('C'));
14759 end if;
14760
14761 -- Set the parent so we have a proper link for freezing etc. This is
14762 -- not a real parent pointer, since of course our parent does not own
14763 -- up to us and reference us, we are an illegitimate child of the
14764 -- original parent.
14765
14766 Set_Parent (New_Compon, Parent (Old_Compon));
14767
14768 -- We do not want this node marked as Comes_From_Source, since
14769 -- otherwise it would get first class status and a separate cross-
14770 -- reference line would be generated. Illegitimate children do not
14771 -- rate such recognition.
14772
14773 Set_Comes_From_Source (New_Compon, False);
14774
14775 -- But it is a real entity, and a birth certificate must be properly
14776 -- registered by entering it into the entity list, and setting its
14777 -- scope to the given subtype. This turns out to be useful for the
14778 -- LLVM code generator, but that scope is not used otherwise.
14779
14780 Enter_Name (New_Compon);
14781 Set_Scope (New_Compon, Subt);
14782
14783 return New_Compon;
14784 end Create_Component;
14785
14786 -----------------------
14787 -- Is_Variant_Record --
14788 -----------------------
14789
14790 function Is_Variant_Record (T : Entity_Id) return Boolean is
14791 begin
14792 return Nkind (Parent (T)) = N_Full_Type_Declaration
14793 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14794 and then Present (Component_List (Type_Definition (Parent (T))))
14795 and then
14796 Present
14797 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14798 end Is_Variant_Record;
14799
14800 -- Start of processing for Create_Constrained_Components
14801
14802 begin
14803 pragma Assert (Subt /= Base_Type (Subt));
14804 pragma Assert (Typ = Base_Type (Typ));
14805
14806 Set_First_Entity (Subt, Empty);
14807 Set_Last_Entity (Subt, Empty);
14808
14809 -- Check whether constraint is fully static, in which case we can
14810 -- optimize the list of components.
14811
14812 Discr_Val := First_Elmt (Constraints);
14813 while Present (Discr_Val) loop
14814 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14815 Is_Static := False;
14816 exit;
14817 end if;
14818
14819 Next_Elmt (Discr_Val);
14820 end loop;
14821
14822 Set_Has_Static_Discriminants (Subt, Is_Static);
14823
14824 Push_Scope (Subt);
14825
14826 -- Inherit the discriminants of the parent type
14827
14828 Add_Discriminants : declare
14829 Num_Disc : Nat;
14830 Num_Gird : Nat;
14831
14832 begin
14833 Num_Disc := 0;
14834 Old_C := First_Discriminant (Typ);
14835
14836 while Present (Old_C) loop
14837 Num_Disc := Num_Disc + 1;
14838 New_C := Create_Component (Old_C);
14839 Set_Is_Public (New_C, Is_Public (Subt));
14840 Next_Discriminant (Old_C);
14841 end loop;
14842
14843 -- For an untagged derived subtype, the number of discriminants may
14844 -- be smaller than the number of inherited discriminants, because
14845 -- several of them may be renamed by a single new discriminant or
14846 -- constrained. In this case, add the hidden discriminants back into
14847 -- the subtype, because they need to be present if the optimizer of
14848 -- the GCC 4.x back-end decides to break apart assignments between
14849 -- objects using the parent view into member-wise assignments.
14850
14851 Num_Gird := 0;
14852
14853 if Is_Derived_Type (Typ)
14854 and then not Is_Tagged_Type (Typ)
14855 then
14856 Old_C := First_Stored_Discriminant (Typ);
14857
14858 while Present (Old_C) loop
14859 Num_Gird := Num_Gird + 1;
14860 Next_Stored_Discriminant (Old_C);
14861 end loop;
14862 end if;
14863
14864 if Num_Gird > Num_Disc then
14865
14866 -- Find out multiple uses of new discriminants, and add hidden
14867 -- components for the extra renamed discriminants. We recognize
14868 -- multiple uses through the Corresponding_Discriminant of a
14869 -- new discriminant: if it constrains several old discriminants,
14870 -- this field points to the last one in the parent type. The
14871 -- stored discriminants of the derived type have the same name
14872 -- as those of the parent.
14873
14874 declare
14875 Constr : Elmt_Id;
14876 New_Discr : Entity_Id;
14877 Old_Discr : Entity_Id;
14878
14879 begin
14880 Constr := First_Elmt (Stored_Constraint (Typ));
14881 Old_Discr := First_Stored_Discriminant (Typ);
14882 while Present (Constr) loop
14883 if Is_Entity_Name (Node (Constr))
14884 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14885 then
14886 New_Discr := Entity (Node (Constr));
14887
14888 if Chars (Corresponding_Discriminant (New_Discr)) /=
14889 Chars (Old_Discr)
14890 then
14891 -- The new discriminant has been used to rename a
14892 -- subsequent old discriminant. Introduce a shadow
14893 -- component for the current old discriminant.
14894
14895 New_C := Create_Component (Old_Discr);
14896 Set_Original_Record_Component (New_C, Old_Discr);
14897 end if;
14898
14899 else
14900 -- The constraint has eliminated the old discriminant.
14901 -- Introduce a shadow component.
14902
14903 New_C := Create_Component (Old_Discr);
14904 Set_Original_Record_Component (New_C, Old_Discr);
14905 end if;
14906
14907 Next_Elmt (Constr);
14908 Next_Stored_Discriminant (Old_Discr);
14909 end loop;
14910 end;
14911 end if;
14912 end Add_Discriminants;
14913
14914 if Is_Static
14915 and then Is_Variant_Record (Typ)
14916 then
14917 Collect_Fixed_Components (Typ);
14918
14919 Gather_Components (
14920 Typ,
14921 Component_List (Type_Definition (Parent (Typ))),
14922 Governed_By => Assoc_List,
14923 Into => Comp_List,
14924 Report_Errors => Errors);
14925 pragma Assert (not Errors
14926 or else Serious_Errors_Detected > 0);
14927
14928 Create_All_Components;
14929
14930 -- If the subtype declaration is created for a tagged type derivation
14931 -- with constraints, we retrieve the record definition of the parent
14932 -- type to select the components of the proper variant.
14933
14934 elsif Is_Static
14935 and then Is_Tagged_Type (Typ)
14936 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14937 and then
14938 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14939 and then Is_Variant_Record (Parent_Type)
14940 then
14941 Collect_Fixed_Components (Typ);
14942
14943 Gather_Components
14944 (Typ,
14945 Component_List (Type_Definition (Parent (Parent_Type))),
14946 Governed_By => Assoc_List,
14947 Into => Comp_List,
14948 Report_Errors => Errors);
14949
14950 -- Note: previously there was a check at this point that no errors
14951 -- were detected. As a consequence of AI05-220 there may be an error
14952 -- if an inherited discriminant that controls a variant has a non-
14953 -- static constraint.
14954
14955 -- If the tagged derivation has a type extension, collect all the
14956 -- new components therein.
14957
14958 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14959 then
14960 Old_C := First_Component (Typ);
14961 while Present (Old_C) loop
14962 if Original_Record_Component (Old_C) = Old_C
14963 and then Chars (Old_C) /= Name_uTag
14964 and then Chars (Old_C) /= Name_uParent
14965 then
14966 Append_Elmt (Old_C, Comp_List);
14967 end if;
14968
14969 Next_Component (Old_C);
14970 end loop;
14971 end if;
14972
14973 Create_All_Components;
14974
14975 else
14976 -- If discriminants are not static, or if this is a multi-level type
14977 -- extension, we have to include all components of the parent type.
14978
14979 Old_C := First_Component (Typ);
14980 while Present (Old_C) loop
14981 New_C := Create_Component (Old_C);
14982
14983 Set_Etype
14984 (New_C,
14985 Constrain_Component_Type
14986 (Old_C, Subt, Decl_Node, Typ, Constraints));
14987 Set_Is_Public (New_C, Is_Public (Subt));
14988
14989 Next_Component (Old_C);
14990 end loop;
14991 end if;
14992
14993 End_Scope;
14994 end Create_Constrained_Components;
14995
14996 ------------------------------------------
14997 -- Decimal_Fixed_Point_Type_Declaration --
14998 ------------------------------------------
14999
15000 procedure Decimal_Fixed_Point_Type_Declaration
15001 (T : Entity_Id;
15002 Def : Node_Id)
15003 is
15004 Loc : constant Source_Ptr := Sloc (Def);
15005 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15006 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15007 Implicit_Base : Entity_Id;
15008 Digs_Val : Uint;
15009 Delta_Val : Ureal;
15010 Scale_Val : Uint;
15011 Bound_Val : Ureal;
15012
15013 begin
15014 Check_SPARK_05_Restriction
15015 ("decimal fixed point type is not allowed", Def);
15016 Check_Restriction (No_Fixed_Point, Def);
15017
15018 -- Create implicit base type
15019
15020 Implicit_Base :=
15021 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15022 Set_Etype (Implicit_Base, Implicit_Base);
15023
15024 -- Analyze and process delta expression
15025
15026 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15027
15028 Check_Delta_Expression (Delta_Expr);
15029 Delta_Val := Expr_Value_R (Delta_Expr);
15030
15031 -- Check delta is power of 10, and determine scale value from it
15032
15033 declare
15034 Val : Ureal;
15035
15036 begin
15037 Scale_Val := Uint_0;
15038 Val := Delta_Val;
15039
15040 if Val < Ureal_1 then
15041 while Val < Ureal_1 loop
15042 Val := Val * Ureal_10;
15043 Scale_Val := Scale_Val + 1;
15044 end loop;
15045
15046 if Scale_Val > 18 then
15047 Error_Msg_N ("scale exceeds maximum value of 18", Def);
15048 Scale_Val := UI_From_Int (+18);
15049 end if;
15050
15051 else
15052 while Val > Ureal_1 loop
15053 Val := Val / Ureal_10;
15054 Scale_Val := Scale_Val - 1;
15055 end loop;
15056
15057 if Scale_Val < -18 then
15058 Error_Msg_N ("scale is less than minimum value of -18", Def);
15059 Scale_Val := UI_From_Int (-18);
15060 end if;
15061 end if;
15062
15063 if Val /= Ureal_1 then
15064 Error_Msg_N ("delta expression must be a power of 10", Def);
15065 Delta_Val := Ureal_10 ** (-Scale_Val);
15066 end if;
15067 end;
15068
15069 -- Set delta, scale and small (small = delta for decimal type)
15070
15071 Set_Delta_Value (Implicit_Base, Delta_Val);
15072 Set_Scale_Value (Implicit_Base, Scale_Val);
15073 Set_Small_Value (Implicit_Base, Delta_Val);
15074
15075 -- Analyze and process digits expression
15076
15077 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15078 Check_Digits_Expression (Digs_Expr);
15079 Digs_Val := Expr_Value (Digs_Expr);
15080
15081 if Digs_Val > 18 then
15082 Digs_Val := UI_From_Int (+18);
15083 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
15084 end if;
15085
15086 Set_Digits_Value (Implicit_Base, Digs_Val);
15087 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15088
15089 -- Set range of base type from digits value for now. This will be
15090 -- expanded to represent the true underlying base range by Freeze.
15091
15092 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15093
15094 -- Note: We leave size as zero for now, size will be set at freeze
15095 -- time. We have to do this for ordinary fixed-point, because the size
15096 -- depends on the specified small, and we might as well do the same for
15097 -- decimal fixed-point.
15098
15099 pragma Assert (Esize (Implicit_Base) = Uint_0);
15100
15101 -- If there are bounds given in the declaration use them as the
15102 -- bounds of the first named subtype.
15103
15104 if Present (Real_Range_Specification (Def)) then
15105 declare
15106 RRS : constant Node_Id := Real_Range_Specification (Def);
15107 Low : constant Node_Id := Low_Bound (RRS);
15108 High : constant Node_Id := High_Bound (RRS);
15109 Low_Val : Ureal;
15110 High_Val : Ureal;
15111
15112 begin
15113 Analyze_And_Resolve (Low, Any_Real);
15114 Analyze_And_Resolve (High, Any_Real);
15115 Check_Real_Bound (Low);
15116 Check_Real_Bound (High);
15117 Low_Val := Expr_Value_R (Low);
15118 High_Val := Expr_Value_R (High);
15119
15120 if Low_Val < (-Bound_Val) then
15121 Error_Msg_N
15122 ("range low bound too small for digits value", Low);
15123 Low_Val := -Bound_Val;
15124 end if;
15125
15126 if High_Val > Bound_Val then
15127 Error_Msg_N
15128 ("range high bound too large for digits value", High);
15129 High_Val := Bound_Val;
15130 end if;
15131
15132 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15133 end;
15134
15135 -- If no explicit range, use range that corresponds to given
15136 -- digits value. This will end up as the final range for the
15137 -- first subtype.
15138
15139 else
15140 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15141 end if;
15142
15143 -- Complete entity for first subtype. The inheritance of the rep item
15144 -- chain ensures that SPARK-related pragmas are not clobbered when the
15145 -- decimal fixed point type acts as a full view of a private type.
15146
15147 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15148 Set_Etype (T, Implicit_Base);
15149 Set_Size_Info (T, Implicit_Base);
15150 Inherit_Rep_Item_Chain (T, Implicit_Base);
15151 Set_Digits_Value (T, Digs_Val);
15152 Set_Delta_Value (T, Delta_Val);
15153 Set_Small_Value (T, Delta_Val);
15154 Set_Scale_Value (T, Scale_Val);
15155 Set_Is_Constrained (T);
15156 end Decimal_Fixed_Point_Type_Declaration;
15157
15158 -----------------------------------
15159 -- Derive_Progenitor_Subprograms --
15160 -----------------------------------
15161
15162 procedure Derive_Progenitor_Subprograms
15163 (Parent_Type : Entity_Id;
15164 Tagged_Type : Entity_Id)
15165 is
15166 E : Entity_Id;
15167 Elmt : Elmt_Id;
15168 Iface : Entity_Id;
15169 Iface_Alias : Entity_Id;
15170 Iface_Elmt : Elmt_Id;
15171 Iface_Subp : Entity_Id;
15172 New_Subp : Entity_Id := Empty;
15173 Prim_Elmt : Elmt_Id;
15174 Subp : Entity_Id;
15175 Typ : Entity_Id;
15176
15177 begin
15178 pragma Assert (Ada_Version >= Ada_2005
15179 and then Is_Record_Type (Tagged_Type)
15180 and then Is_Tagged_Type (Tagged_Type)
15181 and then Has_Interfaces (Tagged_Type));
15182
15183 -- Step 1: Transfer to the full-view primitives associated with the
15184 -- partial-view that cover interface primitives. Conceptually this
15185 -- work should be done later by Process_Full_View; done here to
15186 -- simplify its implementation at later stages. It can be safely
15187 -- done here because interfaces must be visible in the partial and
15188 -- private view (RM 7.3(7.3/2)).
15189
15190 -- Small optimization: This work is only required if the parent may
15191 -- have entities whose Alias attribute reference an interface primitive.
15192 -- Such a situation may occur if the parent is an abstract type and the
15193 -- primitive has not been yet overridden or if the parent is a generic
15194 -- formal type covering interfaces.
15195
15196 -- If the tagged type is not abstract, it cannot have abstract
15197 -- primitives (the only entities in the list of primitives of
15198 -- non-abstract tagged types that can reference abstract primitives
15199 -- through its Alias attribute are the internal entities that have
15200 -- attribute Interface_Alias, and these entities are generated later
15201 -- by Add_Internal_Interface_Entities).
15202
15203 if In_Private_Part (Current_Scope)
15204 and then (Is_Abstract_Type (Parent_Type)
15205 or else
15206 Is_Generic_Type (Parent_Type))
15207 then
15208 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15209 while Present (Elmt) loop
15210 Subp := Node (Elmt);
15211
15212 -- At this stage it is not possible to have entities in the list
15213 -- of primitives that have attribute Interface_Alias.
15214
15215 pragma Assert (No (Interface_Alias (Subp)));
15216
15217 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15218
15219 if Is_Interface (Typ) then
15220 E := Find_Primitive_Covering_Interface
15221 (Tagged_Type => Tagged_Type,
15222 Iface_Prim => Subp);
15223
15224 if Present (E)
15225 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15226 then
15227 Replace_Elmt (Elmt, E);
15228 Remove_Homonym (Subp);
15229 end if;
15230 end if;
15231
15232 Next_Elmt (Elmt);
15233 end loop;
15234 end if;
15235
15236 -- Step 2: Add primitives of progenitors that are not implemented by
15237 -- parents of Tagged_Type.
15238
15239 if Present (Interfaces (Base_Type (Tagged_Type))) then
15240 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15241 while Present (Iface_Elmt) loop
15242 Iface := Node (Iface_Elmt);
15243
15244 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15245 while Present (Prim_Elmt) loop
15246 Iface_Subp := Node (Prim_Elmt);
15247 Iface_Alias := Ultimate_Alias (Iface_Subp);
15248
15249 -- Exclude derivation of predefined primitives except those
15250 -- that come from source, or are inherited from one that comes
15251 -- from source. Required to catch declarations of equality
15252 -- operators of interfaces. For example:
15253
15254 -- type Iface is interface;
15255 -- function "=" (Left, Right : Iface) return Boolean;
15256
15257 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15258 or else Comes_From_Source (Iface_Alias)
15259 then
15260 E :=
15261 Find_Primitive_Covering_Interface
15262 (Tagged_Type => Tagged_Type,
15263 Iface_Prim => Iface_Subp);
15264
15265 -- If not found we derive a new primitive leaving its alias
15266 -- attribute referencing the interface primitive.
15267
15268 if No (E) then
15269 Derive_Subprogram
15270 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15271
15272 -- Ada 2012 (AI05-0197): If the covering primitive's name
15273 -- differs from the name of the interface primitive then it
15274 -- is a private primitive inherited from a parent type. In
15275 -- such case, given that Tagged_Type covers the interface,
15276 -- the inherited private primitive becomes visible. For such
15277 -- purpose we add a new entity that renames the inherited
15278 -- private primitive.
15279
15280 elsif Chars (E) /= Chars (Iface_Subp) then
15281 pragma Assert (Has_Suffix (E, 'P'));
15282 Derive_Subprogram
15283 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15284 Set_Alias (New_Subp, E);
15285 Set_Is_Abstract_Subprogram (New_Subp,
15286 Is_Abstract_Subprogram (E));
15287
15288 -- Propagate to the full view interface entities associated
15289 -- with the partial view.
15290
15291 elsif In_Private_Part (Current_Scope)
15292 and then Present (Alias (E))
15293 and then Alias (E) = Iface_Subp
15294 and then
15295 List_Containing (Parent (E)) /=
15296 Private_Declarations
15297 (Specification
15298 (Unit_Declaration_Node (Current_Scope)))
15299 then
15300 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15301 end if;
15302 end if;
15303
15304 Next_Elmt (Prim_Elmt);
15305 end loop;
15306
15307 Next_Elmt (Iface_Elmt);
15308 end loop;
15309 end if;
15310 end Derive_Progenitor_Subprograms;
15311
15312 -----------------------
15313 -- Derive_Subprogram --
15314 -----------------------
15315
15316 procedure Derive_Subprogram
15317 (New_Subp : out Entity_Id;
15318 Parent_Subp : Entity_Id;
15319 Derived_Type : Entity_Id;
15320 Parent_Type : Entity_Id;
15321 Actual_Subp : Entity_Id := Empty)
15322 is
15323 Formal : Entity_Id;
15324 -- Formal parameter of parent primitive operation
15325
15326 Formal_Of_Actual : Entity_Id;
15327 -- Formal parameter of actual operation, when the derivation is to
15328 -- create a renaming for a primitive operation of an actual in an
15329 -- instantiation.
15330
15331 New_Formal : Entity_Id;
15332 -- Formal of inherited operation
15333
15334 Visible_Subp : Entity_Id := Parent_Subp;
15335
15336 function Is_Private_Overriding return Boolean;
15337 -- If Subp is a private overriding of a visible operation, the inherited
15338 -- operation derives from the overridden op (even though its body is the
15339 -- overriding one) and the inherited operation is visible now. See
15340 -- sem_disp to see the full details of the handling of the overridden
15341 -- subprogram, which is removed from the list of primitive operations of
15342 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15343 -- and used to diagnose abstract operations that need overriding in the
15344 -- derived type.
15345
15346 procedure Replace_Type (Id, New_Id : Entity_Id);
15347 -- When the type is an anonymous access type, create a new access type
15348 -- designating the derived type.
15349
15350 procedure Set_Derived_Name;
15351 -- This procedure sets the appropriate Chars name for New_Subp. This
15352 -- is normally just a copy of the parent name. An exception arises for
15353 -- type support subprograms, where the name is changed to reflect the
15354 -- name of the derived type, e.g. if type foo is derived from type bar,
15355 -- then a procedure barDA is derived with a name fooDA.
15356
15357 ---------------------------
15358 -- Is_Private_Overriding --
15359 ---------------------------
15360
15361 function Is_Private_Overriding return Boolean is
15362 Prev : Entity_Id;
15363
15364 begin
15365 -- If the parent is not a dispatching operation there is no
15366 -- need to investigate overridings
15367
15368 if not Is_Dispatching_Operation (Parent_Subp) then
15369 return False;
15370 end if;
15371
15372 -- The visible operation that is overridden is a homonym of the
15373 -- parent subprogram. We scan the homonym chain to find the one
15374 -- whose alias is the subprogram we are deriving.
15375
15376 Prev := Current_Entity (Parent_Subp);
15377 while Present (Prev) loop
15378 if Ekind (Prev) = Ekind (Parent_Subp)
15379 and then Alias (Prev) = Parent_Subp
15380 and then Scope (Parent_Subp) = Scope (Prev)
15381 and then not Is_Hidden (Prev)
15382 then
15383 Visible_Subp := Prev;
15384 return True;
15385 end if;
15386
15387 Prev := Homonym (Prev);
15388 end loop;
15389
15390 return False;
15391 end Is_Private_Overriding;
15392
15393 ------------------
15394 -- Replace_Type --
15395 ------------------
15396
15397 procedure Replace_Type (Id, New_Id : Entity_Id) is
15398 Id_Type : constant Entity_Id := Etype (Id);
15399 Acc_Type : Entity_Id;
15400 Par : constant Node_Id := Parent (Derived_Type);
15401
15402 begin
15403 -- When the type is an anonymous access type, create a new access
15404 -- type designating the derived type. This itype must be elaborated
15405 -- at the point of the derivation, not on subsequent calls that may
15406 -- be out of the proper scope for Gigi, so we insert a reference to
15407 -- it after the derivation.
15408
15409 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15410 declare
15411 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15412
15413 begin
15414 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15415 and then Present (Full_View (Desig_Typ))
15416 and then not Is_Private_Type (Parent_Type)
15417 then
15418 Desig_Typ := Full_View (Desig_Typ);
15419 end if;
15420
15421 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15422
15423 -- Ada 2005 (AI-251): Handle also derivations of abstract
15424 -- interface primitives.
15425
15426 or else (Is_Interface (Desig_Typ)
15427 and then not Is_Class_Wide_Type (Desig_Typ))
15428 then
15429 Acc_Type := New_Copy (Id_Type);
15430 Set_Etype (Acc_Type, Acc_Type);
15431 Set_Scope (Acc_Type, New_Subp);
15432
15433 -- Set size of anonymous access type. If we have an access
15434 -- to an unconstrained array, this is a fat pointer, so it
15435 -- is sizes at twice addtress size.
15436
15437 if Is_Array_Type (Desig_Typ)
15438 and then not Is_Constrained (Desig_Typ)
15439 then
15440 Init_Size (Acc_Type, 2 * System_Address_Size);
15441
15442 -- Other cases use a thin pointer
15443
15444 else
15445 Init_Size (Acc_Type, System_Address_Size);
15446 end if;
15447
15448 -- Set remaining characterstics of anonymous access type
15449
15450 Init_Alignment (Acc_Type);
15451 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15452
15453 Set_Etype (New_Id, Acc_Type);
15454 Set_Scope (New_Id, New_Subp);
15455
15456 -- Create a reference to it
15457
15458 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15459
15460 else
15461 Set_Etype (New_Id, Id_Type);
15462 end if;
15463 end;
15464
15465 -- In Ada2012, a formal may have an incomplete type but the type
15466 -- derivation that inherits the primitive follows the full view.
15467
15468 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15469 or else
15470 (Ekind (Id_Type) = E_Record_Type_With_Private
15471 and then Present (Full_View (Id_Type))
15472 and then
15473 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15474 or else
15475 (Ada_Version >= Ada_2012
15476 and then Ekind (Id_Type) = E_Incomplete_Type
15477 and then Full_View (Id_Type) = Parent_Type)
15478 then
15479 -- Constraint checks on formals are generated during expansion,
15480 -- based on the signature of the original subprogram. The bounds
15481 -- of the derived type are not relevant, and thus we can use
15482 -- the base type for the formals. However, the return type may be
15483 -- used in a context that requires that the proper static bounds
15484 -- be used (a case statement, for example) and for those cases
15485 -- we must use the derived type (first subtype), not its base.
15486
15487 -- If the derived_type_definition has no constraints, we know that
15488 -- the derived type has the same constraints as the first subtype
15489 -- of the parent, and we can also use it rather than its base,
15490 -- which can lead to more efficient code.
15491
15492 if Etype (Id) = Parent_Type then
15493 if Is_Scalar_Type (Parent_Type)
15494 and then
15495 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15496 then
15497 Set_Etype (New_Id, Derived_Type);
15498
15499 elsif Nkind (Par) = N_Full_Type_Declaration
15500 and then
15501 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15502 and then
15503 Is_Entity_Name
15504 (Subtype_Indication (Type_Definition (Par)))
15505 then
15506 Set_Etype (New_Id, Derived_Type);
15507
15508 else
15509 Set_Etype (New_Id, Base_Type (Derived_Type));
15510 end if;
15511
15512 else
15513 Set_Etype (New_Id, Base_Type (Derived_Type));
15514 end if;
15515
15516 else
15517 Set_Etype (New_Id, Etype (Id));
15518 end if;
15519 end Replace_Type;
15520
15521 ----------------------
15522 -- Set_Derived_Name --
15523 ----------------------
15524
15525 procedure Set_Derived_Name is
15526 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15527 begin
15528 if Nm = TSS_Null then
15529 Set_Chars (New_Subp, Chars (Parent_Subp));
15530 else
15531 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15532 end if;
15533 end Set_Derived_Name;
15534
15535 -- Start of processing for Derive_Subprogram
15536
15537 begin
15538 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15539 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15540
15541 -- Check whether the inherited subprogram is a private operation that
15542 -- should be inherited but not yet made visible. Such subprograms can
15543 -- become visible at a later point (e.g., the private part of a public
15544 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15545 -- following predicate is true, then this is not such a private
15546 -- operation and the subprogram simply inherits the name of the parent
15547 -- subprogram. Note the special check for the names of controlled
15548 -- operations, which are currently exempted from being inherited with
15549 -- a hidden name because they must be findable for generation of
15550 -- implicit run-time calls.
15551
15552 if not Is_Hidden (Parent_Subp)
15553 or else Is_Internal (Parent_Subp)
15554 or else Is_Private_Overriding
15555 or else Is_Internal_Name (Chars (Parent_Subp))
15556 or else (Is_Controlled (Parent_Type)
15557 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15558 Name_Finalize,
15559 Name_Initialize))
15560 then
15561 Set_Derived_Name;
15562
15563 -- An inherited dispatching equality will be overridden by an internally
15564 -- generated one, or by an explicit one, so preserve its name and thus
15565 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15566 -- private operation it may become invisible if the full view has
15567 -- progenitors, and the dispatch table will be malformed.
15568 -- We check that the type is limited to handle the anomalous declaration
15569 -- of Limited_Controlled, which is derived from a non-limited type, and
15570 -- which is handled specially elsewhere as well.
15571
15572 elsif Chars (Parent_Subp) = Name_Op_Eq
15573 and then Is_Dispatching_Operation (Parent_Subp)
15574 and then Etype (Parent_Subp) = Standard_Boolean
15575 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15576 and then
15577 Etype (First_Formal (Parent_Subp)) =
15578 Etype (Next_Formal (First_Formal (Parent_Subp)))
15579 then
15580 Set_Derived_Name;
15581
15582 -- If parent is hidden, this can be a regular derivation if the
15583 -- parent is immediately visible in a non-instantiating context,
15584 -- or if we are in the private part of an instance. This test
15585 -- should still be refined ???
15586
15587 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15588 -- operation as a non-visible operation in cases where the parent
15589 -- subprogram might not be visible now, but was visible within the
15590 -- original generic, so it would be wrong to make the inherited
15591 -- subprogram non-visible now. (Not clear if this test is fully
15592 -- correct; are there any cases where we should declare the inherited
15593 -- operation as not visible to avoid it being overridden, e.g., when
15594 -- the parent type is a generic actual with private primitives ???)
15595
15596 -- (they should be treated the same as other private inherited
15597 -- subprograms, but it's not clear how to do this cleanly). ???
15598
15599 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15600 and then Is_Immediately_Visible (Parent_Subp)
15601 and then not In_Instance)
15602 or else In_Instance_Not_Visible
15603 then
15604 Set_Derived_Name;
15605
15606 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15607 -- overrides an interface primitive because interface primitives
15608 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15609
15610 elsif Ada_Version >= Ada_2005
15611 and then Is_Dispatching_Operation (Parent_Subp)
15612 and then Present (Covered_Interface_Op (Parent_Subp))
15613 then
15614 Set_Derived_Name;
15615
15616 -- Otherwise, the type is inheriting a private operation, so enter it
15617 -- with a special name so it can't be overridden. See also below, where
15618 -- we check for this case, and if so avoid setting Requires_Overriding.
15619
15620 else
15621 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15622 end if;
15623
15624 Set_Parent (New_Subp, Parent (Derived_Type));
15625
15626 if Present (Actual_Subp) then
15627 Replace_Type (Actual_Subp, New_Subp);
15628 else
15629 Replace_Type (Parent_Subp, New_Subp);
15630 end if;
15631
15632 Conditional_Delay (New_Subp, Parent_Subp);
15633
15634 -- If we are creating a renaming for a primitive operation of an
15635 -- actual of a generic derived type, we must examine the signature
15636 -- of the actual primitive, not that of the generic formal, which for
15637 -- example may be an interface. However the name and initial value
15638 -- of the inherited operation are those of the formal primitive.
15639
15640 Formal := First_Formal (Parent_Subp);
15641
15642 if Present (Actual_Subp) then
15643 Formal_Of_Actual := First_Formal (Actual_Subp);
15644 else
15645 Formal_Of_Actual := Empty;
15646 end if;
15647
15648 while Present (Formal) loop
15649 New_Formal := New_Copy (Formal);
15650
15651 -- Normally we do not go copying parents, but in the case of
15652 -- formals, we need to link up to the declaration (which is the
15653 -- parameter specification), and it is fine to link up to the
15654 -- original formal's parameter specification in this case.
15655
15656 Set_Parent (New_Formal, Parent (Formal));
15657 Append_Entity (New_Formal, New_Subp);
15658
15659 if Present (Formal_Of_Actual) then
15660 Replace_Type (Formal_Of_Actual, New_Formal);
15661 Next_Formal (Formal_Of_Actual);
15662 else
15663 Replace_Type (Formal, New_Formal);
15664 end if;
15665
15666 Next_Formal (Formal);
15667 end loop;
15668
15669 -- If this derivation corresponds to a tagged generic actual, then
15670 -- primitive operations rename those of the actual. Otherwise the
15671 -- primitive operations rename those of the parent type, If the parent
15672 -- renames an intrinsic operator, so does the new subprogram. We except
15673 -- concatenation, which is always properly typed, and does not get
15674 -- expanded as other intrinsic operations.
15675
15676 if No (Actual_Subp) then
15677 if Is_Intrinsic_Subprogram (Parent_Subp) then
15678 Set_Is_Intrinsic_Subprogram (New_Subp);
15679
15680 if Present (Alias (Parent_Subp))
15681 and then Chars (Parent_Subp) /= Name_Op_Concat
15682 then
15683 Set_Alias (New_Subp, Alias (Parent_Subp));
15684 else
15685 Set_Alias (New_Subp, Parent_Subp);
15686 end if;
15687
15688 else
15689 Set_Alias (New_Subp, Parent_Subp);
15690 end if;
15691
15692 else
15693 Set_Alias (New_Subp, Actual_Subp);
15694 end if;
15695
15696 -- Derived subprograms of a tagged type must inherit the convention
15697 -- of the parent subprogram (a requirement of AI-117). Derived
15698 -- subprograms of untagged types simply get convention Ada by default.
15699
15700 -- If the derived type is a tagged generic formal type with unknown
15701 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15702
15703 -- However, if the type is derived from a generic formal, the further
15704 -- inherited subprogram has the convention of the non-generic ancestor.
15705 -- Otherwise there would be no way to override the operation.
15706 -- (This is subject to forthcoming ARG discussions).
15707
15708 if Is_Tagged_Type (Derived_Type) then
15709 if Is_Generic_Type (Derived_Type)
15710 and then Has_Unknown_Discriminants (Derived_Type)
15711 then
15712 Set_Convention (New_Subp, Convention_Intrinsic);
15713
15714 else
15715 if Is_Generic_Type (Parent_Type)
15716 and then Has_Unknown_Discriminants (Parent_Type)
15717 then
15718 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15719 else
15720 Set_Convention (New_Subp, Convention (Parent_Subp));
15721 end if;
15722 end if;
15723 end if;
15724
15725 -- Predefined controlled operations retain their name even if the parent
15726 -- is hidden (see above), but they are not primitive operations if the
15727 -- ancestor is not visible, for example if the parent is a private
15728 -- extension completed with a controlled extension. Note that a full
15729 -- type that is controlled can break privacy: the flag Is_Controlled is
15730 -- set on both views of the type.
15731
15732 if Is_Controlled (Parent_Type)
15733 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15734 Name_Adjust,
15735 Name_Finalize)
15736 and then Is_Hidden (Parent_Subp)
15737 and then not Is_Visibly_Controlled (Parent_Type)
15738 then
15739 Set_Is_Hidden (New_Subp);
15740 end if;
15741
15742 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15743 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15744
15745 if Ekind (Parent_Subp) = E_Procedure then
15746 Set_Is_Valued_Procedure
15747 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15748 else
15749 Set_Has_Controlling_Result
15750 (New_Subp, Has_Controlling_Result (Parent_Subp));
15751 end if;
15752
15753 -- No_Return must be inherited properly. If this is overridden in the
15754 -- case of a dispatching operation, then a check is made in Sem_Disp
15755 -- that the overriding operation is also No_Return (no such check is
15756 -- required for the case of non-dispatching operation.
15757
15758 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15759
15760 -- A derived function with a controlling result is abstract. If the
15761 -- Derived_Type is a nonabstract formal generic derived type, then
15762 -- inherited operations are not abstract: the required check is done at
15763 -- instantiation time. If the derivation is for a generic actual, the
15764 -- function is not abstract unless the actual is.
15765
15766 if Is_Generic_Type (Derived_Type)
15767 and then not Is_Abstract_Type (Derived_Type)
15768 then
15769 null;
15770
15771 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15772 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15773
15774 -- A subprogram subject to pragma Extensions_Visible with value False
15775 -- requires overriding if the subprogram has at least one controlling
15776 -- OUT parameter (SPARK RM 6.1.7(6)).
15777
15778 elsif Ada_Version >= Ada_2005
15779 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15780 or else (Is_Tagged_Type (Derived_Type)
15781 and then Etype (New_Subp) = Derived_Type
15782 and then not Is_Null_Extension (Derived_Type))
15783 or else (Is_Tagged_Type (Derived_Type)
15784 and then Ekind (Etype (New_Subp)) =
15785 E_Anonymous_Access_Type
15786 and then Designated_Type (Etype (New_Subp)) =
15787 Derived_Type
15788 and then not Is_Null_Extension (Derived_Type))
15789 or else (Comes_From_Source (Alias (New_Subp))
15790 and then Is_EVF_Procedure (Alias (New_Subp))))
15791 and then No (Actual_Subp)
15792 then
15793 if not Is_Tagged_Type (Derived_Type)
15794 or else Is_Abstract_Type (Derived_Type)
15795 or else Is_Abstract_Subprogram (Alias (New_Subp))
15796 then
15797 Set_Is_Abstract_Subprogram (New_Subp);
15798
15799 -- If the Chars of the new subprogram is different from that of the
15800 -- parent's one, it means that we entered it with a special name so
15801 -- it can't be overridden (see above). In that case we had better not
15802 -- *require* it to be overridden. This is the case where the parent
15803 -- type inherited the operation privately, so there's no danger of
15804 -- dangling dispatching.
15805
15806 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
15807 Set_Requires_Overriding (New_Subp);
15808 end if;
15809
15810 elsif Ada_Version < Ada_2005
15811 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15812 or else (Is_Tagged_Type (Derived_Type)
15813 and then Etype (New_Subp) = Derived_Type
15814 and then No (Actual_Subp)))
15815 then
15816 Set_Is_Abstract_Subprogram (New_Subp);
15817
15818 -- AI05-0097 : an inherited operation that dispatches on result is
15819 -- abstract if the derived type is abstract, even if the parent type
15820 -- is concrete and the derived type is a null extension.
15821
15822 elsif Has_Controlling_Result (Alias (New_Subp))
15823 and then Is_Abstract_Type (Etype (New_Subp))
15824 then
15825 Set_Is_Abstract_Subprogram (New_Subp);
15826
15827 -- Finally, if the parent type is abstract we must verify that all
15828 -- inherited operations are either non-abstract or overridden, or that
15829 -- the derived type itself is abstract (this check is performed at the
15830 -- end of a package declaration, in Check_Abstract_Overriding). A
15831 -- private overriding in the parent type will not be visible in the
15832 -- derivation if we are not in an inner package or in a child unit of
15833 -- the parent type, in which case the abstractness of the inherited
15834 -- operation is carried to the new subprogram.
15835
15836 elsif Is_Abstract_Type (Parent_Type)
15837 and then not In_Open_Scopes (Scope (Parent_Type))
15838 and then Is_Private_Overriding
15839 and then Is_Abstract_Subprogram (Visible_Subp)
15840 then
15841 if No (Actual_Subp) then
15842 Set_Alias (New_Subp, Visible_Subp);
15843 Set_Is_Abstract_Subprogram (New_Subp, True);
15844
15845 else
15846 -- If this is a derivation for an instance of a formal derived
15847 -- type, abstractness comes from the primitive operation of the
15848 -- actual, not from the operation inherited from the ancestor.
15849
15850 Set_Is_Abstract_Subprogram
15851 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15852 end if;
15853 end if;
15854
15855 New_Overloaded_Entity (New_Subp, Derived_Type);
15856
15857 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15858 -- preconditions and the derived type is abstract, the derived operation
15859 -- is abstract as well if parent subprogram is not abstract or null.
15860
15861 if Is_Abstract_Type (Derived_Type)
15862 and then Has_Non_Trivial_Precondition (Parent_Subp)
15863 and then Present (Interfaces (Derived_Type))
15864 then
15865
15866 -- Add useful attributes of subprogram before the freeze point,
15867 -- in case freezing is delayed or there are previous errors.
15868
15869 Set_Is_Dispatching_Operation (New_Subp);
15870
15871 declare
15872 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15873
15874 begin
15875 if Present (Iface_Prim)
15876 and then Has_Non_Trivial_Precondition (Iface_Prim)
15877 then
15878 Set_Is_Abstract_Subprogram (New_Subp);
15879 end if;
15880 end;
15881 end if;
15882
15883 -- Check for case of a derived subprogram for the instantiation of a
15884 -- formal derived tagged type, if so mark the subprogram as dispatching
15885 -- and inherit the dispatching attributes of the actual subprogram. The
15886 -- derived subprogram is effectively renaming of the actual subprogram,
15887 -- so it needs to have the same attributes as the actual.
15888
15889 if Present (Actual_Subp)
15890 and then Is_Dispatching_Operation (Actual_Subp)
15891 then
15892 Set_Is_Dispatching_Operation (New_Subp);
15893
15894 if Present (DTC_Entity (Actual_Subp)) then
15895 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15896 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15897 end if;
15898 end if;
15899
15900 -- Indicate that a derived subprogram does not require a body and that
15901 -- it does not require processing of default expressions.
15902
15903 Set_Has_Completion (New_Subp);
15904 Set_Default_Expressions_Processed (New_Subp);
15905
15906 if Ekind (New_Subp) = E_Function then
15907 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15908 end if;
15909 end Derive_Subprogram;
15910
15911 ------------------------
15912 -- Derive_Subprograms --
15913 ------------------------
15914
15915 procedure Derive_Subprograms
15916 (Parent_Type : Entity_Id;
15917 Derived_Type : Entity_Id;
15918 Generic_Actual : Entity_Id := Empty)
15919 is
15920 Op_List : constant Elist_Id :=
15921 Collect_Primitive_Operations (Parent_Type);
15922
15923 function Check_Derived_Type return Boolean;
15924 -- Check that all the entities derived from Parent_Type are found in
15925 -- the list of primitives of Derived_Type exactly in the same order.
15926
15927 procedure Derive_Interface_Subprogram
15928 (New_Subp : out Entity_Id;
15929 Subp : Entity_Id;
15930 Actual_Subp : Entity_Id);
15931 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15932 -- (which is an interface primitive). If Generic_Actual is present then
15933 -- Actual_Subp is the actual subprogram corresponding with the generic
15934 -- subprogram Subp.
15935
15936 ------------------------
15937 -- Check_Derived_Type --
15938 ------------------------
15939
15940 function Check_Derived_Type return Boolean is
15941 E : Entity_Id;
15942 Elmt : Elmt_Id;
15943 List : Elist_Id;
15944 New_Subp : Entity_Id;
15945 Op_Elmt : Elmt_Id;
15946 Subp : Entity_Id;
15947
15948 begin
15949 -- Traverse list of entities in the current scope searching for
15950 -- an incomplete type whose full-view is derived type.
15951
15952 E := First_Entity (Scope (Derived_Type));
15953 while Present (E) and then E /= Derived_Type loop
15954 if Ekind (E) = E_Incomplete_Type
15955 and then Present (Full_View (E))
15956 and then Full_View (E) = Derived_Type
15957 then
15958 -- Disable this test if Derived_Type completes an incomplete
15959 -- type because in such case more primitives can be added
15960 -- later to the list of primitives of Derived_Type by routine
15961 -- Process_Incomplete_Dependents
15962
15963 return True;
15964 end if;
15965
15966 E := Next_Entity (E);
15967 end loop;
15968
15969 List := Collect_Primitive_Operations (Derived_Type);
15970 Elmt := First_Elmt (List);
15971
15972 Op_Elmt := First_Elmt (Op_List);
15973 while Present (Op_Elmt) loop
15974 Subp := Node (Op_Elmt);
15975 New_Subp := Node (Elmt);
15976
15977 -- At this early stage Derived_Type has no entities with attribute
15978 -- Interface_Alias. In addition, such primitives are always
15979 -- located at the end of the list of primitives of Parent_Type.
15980 -- Therefore, if found we can safely stop processing pending
15981 -- entities.
15982
15983 exit when Present (Interface_Alias (Subp));
15984
15985 -- Handle hidden entities
15986
15987 if not Is_Predefined_Dispatching_Operation (Subp)
15988 and then Is_Hidden (Subp)
15989 then
15990 if Present (New_Subp)
15991 and then Primitive_Names_Match (Subp, New_Subp)
15992 then
15993 Next_Elmt (Elmt);
15994 end if;
15995
15996 else
15997 if not Present (New_Subp)
15998 or else Ekind (Subp) /= Ekind (New_Subp)
15999 or else not Primitive_Names_Match (Subp, New_Subp)
16000 then
16001 return False;
16002 end if;
16003
16004 Next_Elmt (Elmt);
16005 end if;
16006
16007 Next_Elmt (Op_Elmt);
16008 end loop;
16009
16010 return True;
16011 end Check_Derived_Type;
16012
16013 ---------------------------------
16014 -- Derive_Interface_Subprogram --
16015 ---------------------------------
16016
16017 procedure Derive_Interface_Subprogram
16018 (New_Subp : out Entity_Id;
16019 Subp : Entity_Id;
16020 Actual_Subp : Entity_Id)
16021 is
16022 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16023 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16024
16025 begin
16026 pragma Assert (Is_Interface (Iface_Type));
16027
16028 Derive_Subprogram
16029 (New_Subp => New_Subp,
16030 Parent_Subp => Iface_Subp,
16031 Derived_Type => Derived_Type,
16032 Parent_Type => Iface_Type,
16033 Actual_Subp => Actual_Subp);
16034
16035 -- Given that this new interface entity corresponds with a primitive
16036 -- of the parent that was not overridden we must leave it associated
16037 -- with its parent primitive to ensure that it will share the same
16038 -- dispatch table slot when overridden. We must set the Alias to Subp
16039 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16040 -- (in case we inherited Subp from Iface_Type via a nonabstract
16041 -- generic formal type).
16042
16043 if No (Actual_Subp) then
16044 Set_Alias (New_Subp, Subp);
16045
16046 declare
16047 T : Entity_Id := Find_Dispatching_Type (Subp);
16048 begin
16049 while Etype (T) /= T loop
16050 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16051 Set_Is_Abstract_Subprogram (New_Subp, False);
16052 exit;
16053 end if;
16054
16055 T := Etype (T);
16056 end loop;
16057 end;
16058
16059 -- For instantiations this is not needed since the previous call to
16060 -- Derive_Subprogram leaves the entity well decorated.
16061
16062 else
16063 pragma Assert (Alias (New_Subp) = Actual_Subp);
16064 null;
16065 end if;
16066 end Derive_Interface_Subprogram;
16067
16068 -- Local variables
16069
16070 Alias_Subp : Entity_Id;
16071 Act_List : Elist_Id;
16072 Act_Elmt : Elmt_Id;
16073 Act_Subp : Entity_Id := Empty;
16074 Elmt : Elmt_Id;
16075 Need_Search : Boolean := False;
16076 New_Subp : Entity_Id := Empty;
16077 Parent_Base : Entity_Id;
16078 Subp : Entity_Id;
16079
16080 -- Start of processing for Derive_Subprograms
16081
16082 begin
16083 if Ekind (Parent_Type) = E_Record_Type_With_Private
16084 and then Has_Discriminants (Parent_Type)
16085 and then Present (Full_View (Parent_Type))
16086 then
16087 Parent_Base := Full_View (Parent_Type);
16088 else
16089 Parent_Base := Parent_Type;
16090 end if;
16091
16092 if Present (Generic_Actual) then
16093 Act_List := Collect_Primitive_Operations (Generic_Actual);
16094 Act_Elmt := First_Elmt (Act_List);
16095 else
16096 Act_List := No_Elist;
16097 Act_Elmt := No_Elmt;
16098 end if;
16099
16100 -- Derive primitives inherited from the parent. Note that if the generic
16101 -- actual is present, this is not really a type derivation, it is a
16102 -- completion within an instance.
16103
16104 -- Case 1: Derived_Type does not implement interfaces
16105
16106 if not Is_Tagged_Type (Derived_Type)
16107 or else (not Has_Interfaces (Derived_Type)
16108 and then not (Present (Generic_Actual)
16109 and then Has_Interfaces (Generic_Actual)))
16110 then
16111 Elmt := First_Elmt (Op_List);
16112 while Present (Elmt) loop
16113 Subp := Node (Elmt);
16114
16115 -- Literals are derived earlier in the process of building the
16116 -- derived type, and are skipped here.
16117
16118 if Ekind (Subp) = E_Enumeration_Literal then
16119 null;
16120
16121 -- The actual is a direct descendant and the common primitive
16122 -- operations appear in the same order.
16123
16124 -- If the generic parent type is present, the derived type is an
16125 -- instance of a formal derived type, and within the instance its
16126 -- operations are those of the actual. We derive from the formal
16127 -- type but make the inherited operations aliases of the
16128 -- corresponding operations of the actual.
16129
16130 else
16131 pragma Assert (No (Node (Act_Elmt))
16132 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16133 and then
16134 Type_Conformant
16135 (Subp, Node (Act_Elmt),
16136 Skip_Controlling_Formals => True)));
16137
16138 Derive_Subprogram
16139 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16140
16141 if Present (Act_Elmt) then
16142 Next_Elmt (Act_Elmt);
16143 end if;
16144 end if;
16145
16146 Next_Elmt (Elmt);
16147 end loop;
16148
16149 -- Case 2: Derived_Type implements interfaces
16150
16151 else
16152 -- If the parent type has no predefined primitives we remove
16153 -- predefined primitives from the list of primitives of generic
16154 -- actual to simplify the complexity of this algorithm.
16155
16156 if Present (Generic_Actual) then
16157 declare
16158 Has_Predefined_Primitives : Boolean := False;
16159
16160 begin
16161 -- Check if the parent type has predefined primitives
16162
16163 Elmt := First_Elmt (Op_List);
16164 while Present (Elmt) loop
16165 Subp := Node (Elmt);
16166
16167 if Is_Predefined_Dispatching_Operation (Subp)
16168 and then not Comes_From_Source (Ultimate_Alias (Subp))
16169 then
16170 Has_Predefined_Primitives := True;
16171 exit;
16172 end if;
16173
16174 Next_Elmt (Elmt);
16175 end loop;
16176
16177 -- Remove predefined primitives of Generic_Actual. We must use
16178 -- an auxiliary list because in case of tagged types the value
16179 -- returned by Collect_Primitive_Operations is the value stored
16180 -- in its Primitive_Operations attribute (and we don't want to
16181 -- modify its current contents).
16182
16183 if not Has_Predefined_Primitives then
16184 declare
16185 Aux_List : constant Elist_Id := New_Elmt_List;
16186
16187 begin
16188 Elmt := First_Elmt (Act_List);
16189 while Present (Elmt) loop
16190 Subp := Node (Elmt);
16191
16192 if not Is_Predefined_Dispatching_Operation (Subp)
16193 or else Comes_From_Source (Subp)
16194 then
16195 Append_Elmt (Subp, Aux_List);
16196 end if;
16197
16198 Next_Elmt (Elmt);
16199 end loop;
16200
16201 Act_List := Aux_List;
16202 end;
16203 end if;
16204
16205 Act_Elmt := First_Elmt (Act_List);
16206 Act_Subp := Node (Act_Elmt);
16207 end;
16208 end if;
16209
16210 -- Stage 1: If the generic actual is not present we derive the
16211 -- primitives inherited from the parent type. If the generic parent
16212 -- type is present, the derived type is an instance of a formal
16213 -- derived type, and within the instance its operations are those of
16214 -- the actual. We derive from the formal type but make the inherited
16215 -- operations aliases of the corresponding operations of the actual.
16216
16217 Elmt := First_Elmt (Op_List);
16218 while Present (Elmt) loop
16219 Subp := Node (Elmt);
16220 Alias_Subp := Ultimate_Alias (Subp);
16221
16222 -- Do not derive internal entities of the parent that link
16223 -- interface primitives with their covering primitive. These
16224 -- entities will be added to this type when frozen.
16225
16226 if Present (Interface_Alias (Subp)) then
16227 goto Continue;
16228 end if;
16229
16230 -- If the generic actual is present find the corresponding
16231 -- operation in the generic actual. If the parent type is a
16232 -- direct ancestor of the derived type then, even if it is an
16233 -- interface, the operations are inherited from the primary
16234 -- dispatch table and are in the proper order. If we detect here
16235 -- that primitives are not in the same order we traverse the list
16236 -- of primitive operations of the actual to find the one that
16237 -- implements the interface primitive.
16238
16239 if Need_Search
16240 or else
16241 (Present (Generic_Actual)
16242 and then Present (Act_Subp)
16243 and then not
16244 (Primitive_Names_Match (Subp, Act_Subp)
16245 and then
16246 Type_Conformant (Subp, Act_Subp,
16247 Skip_Controlling_Formals => True)))
16248 then
16249 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16250 Use_Full_View => True));
16251
16252 -- Remember that we need searching for all pending primitives
16253
16254 Need_Search := True;
16255
16256 -- Handle entities associated with interface primitives
16257
16258 if Present (Alias_Subp)
16259 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16260 and then not Is_Predefined_Dispatching_Operation (Subp)
16261 then
16262 -- Search for the primitive in the homonym chain
16263
16264 Act_Subp :=
16265 Find_Primitive_Covering_Interface
16266 (Tagged_Type => Generic_Actual,
16267 Iface_Prim => Alias_Subp);
16268
16269 -- Previous search may not locate primitives covering
16270 -- interfaces defined in generics units or instantiations.
16271 -- (it fails if the covering primitive has formals whose
16272 -- type is also defined in generics or instantiations).
16273 -- In such case we search in the list of primitives of the
16274 -- generic actual for the internal entity that links the
16275 -- interface primitive and the covering primitive.
16276
16277 if No (Act_Subp)
16278 and then Is_Generic_Type (Parent_Type)
16279 then
16280 -- This code has been designed to handle only generic
16281 -- formals that implement interfaces that are defined
16282 -- in a generic unit or instantiation. If this code is
16283 -- needed for other cases we must review it because
16284 -- (given that it relies on Original_Location to locate
16285 -- the primitive of Generic_Actual that covers the
16286 -- interface) it could leave linked through attribute
16287 -- Alias entities of unrelated instantiations).
16288
16289 pragma Assert
16290 (Is_Generic_Unit
16291 (Scope (Find_Dispatching_Type (Alias_Subp)))
16292 or else
16293 Instantiation_Depth
16294 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16295
16296 declare
16297 Iface_Prim_Loc : constant Source_Ptr :=
16298 Original_Location (Sloc (Alias_Subp));
16299
16300 Elmt : Elmt_Id;
16301 Prim : Entity_Id;
16302
16303 begin
16304 Elmt :=
16305 First_Elmt (Primitive_Operations (Generic_Actual));
16306
16307 Search : while Present (Elmt) loop
16308 Prim := Node (Elmt);
16309
16310 if Present (Interface_Alias (Prim))
16311 and then Original_Location
16312 (Sloc (Interface_Alias (Prim))) =
16313 Iface_Prim_Loc
16314 then
16315 Act_Subp := Alias (Prim);
16316 exit Search;
16317 end if;
16318
16319 Next_Elmt (Elmt);
16320 end loop Search;
16321 end;
16322 end if;
16323
16324 pragma Assert (Present (Act_Subp)
16325 or else Is_Abstract_Type (Generic_Actual)
16326 or else Serious_Errors_Detected > 0);
16327
16328 -- Handle predefined primitives plus the rest of user-defined
16329 -- primitives
16330
16331 else
16332 Act_Elmt := First_Elmt (Act_List);
16333 while Present (Act_Elmt) loop
16334 Act_Subp := Node (Act_Elmt);
16335
16336 exit when Primitive_Names_Match (Subp, Act_Subp)
16337 and then Type_Conformant
16338 (Subp, Act_Subp,
16339 Skip_Controlling_Formals => True)
16340 and then No (Interface_Alias (Act_Subp));
16341
16342 Next_Elmt (Act_Elmt);
16343 end loop;
16344
16345 if No (Act_Elmt) then
16346 Act_Subp := Empty;
16347 end if;
16348 end if;
16349 end if;
16350
16351 -- Case 1: If the parent is a limited interface then it has the
16352 -- predefined primitives of synchronized interfaces. However, the
16353 -- actual type may be a non-limited type and hence it does not
16354 -- have such primitives.
16355
16356 if Present (Generic_Actual)
16357 and then not Present (Act_Subp)
16358 and then Is_Limited_Interface (Parent_Base)
16359 and then Is_Predefined_Interface_Primitive (Subp)
16360 then
16361 null;
16362
16363 -- Case 2: Inherit entities associated with interfaces that were
16364 -- not covered by the parent type. We exclude here null interface
16365 -- primitives because they do not need special management.
16366
16367 -- We also exclude interface operations that are renamings. If the
16368 -- subprogram is an explicit renaming of an interface primitive,
16369 -- it is a regular primitive operation, and the presence of its
16370 -- alias is not relevant: it has to be derived like any other
16371 -- primitive.
16372
16373 elsif Present (Alias (Subp))
16374 and then Nkind (Unit_Declaration_Node (Subp)) /=
16375 N_Subprogram_Renaming_Declaration
16376 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16377 and then not
16378 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16379 and then Null_Present (Parent (Alias_Subp)))
16380 then
16381 -- If this is an abstract private type then we transfer the
16382 -- derivation of the interface primitive from the partial view
16383 -- to the full view. This is safe because all the interfaces
16384 -- must be visible in the partial view. Done to avoid adding
16385 -- a new interface derivation to the private part of the
16386 -- enclosing package; otherwise this new derivation would be
16387 -- decorated as hidden when the analysis of the enclosing
16388 -- package completes.
16389
16390 if Is_Abstract_Type (Derived_Type)
16391 and then In_Private_Part (Current_Scope)
16392 and then Has_Private_Declaration (Derived_Type)
16393 then
16394 declare
16395 Partial_View : Entity_Id;
16396 Elmt : Elmt_Id;
16397 Ent : Entity_Id;
16398
16399 begin
16400 Partial_View := First_Entity (Current_Scope);
16401 loop
16402 exit when No (Partial_View)
16403 or else (Has_Private_Declaration (Partial_View)
16404 and then
16405 Full_View (Partial_View) = Derived_Type);
16406
16407 Next_Entity (Partial_View);
16408 end loop;
16409
16410 -- If the partial view was not found then the source code
16411 -- has errors and the derivation is not needed.
16412
16413 if Present (Partial_View) then
16414 Elmt :=
16415 First_Elmt (Primitive_Operations (Partial_View));
16416 while Present (Elmt) loop
16417 Ent := Node (Elmt);
16418
16419 if Present (Alias (Ent))
16420 and then Ultimate_Alias (Ent) = Alias (Subp)
16421 then
16422 Append_Elmt
16423 (Ent, Primitive_Operations (Derived_Type));
16424 exit;
16425 end if;
16426
16427 Next_Elmt (Elmt);
16428 end loop;
16429
16430 -- If the interface primitive was not found in the
16431 -- partial view then this interface primitive was
16432 -- overridden. We add a derivation to activate in
16433 -- Derive_Progenitor_Subprograms the machinery to
16434 -- search for it.
16435
16436 if No (Elmt) then
16437 Derive_Interface_Subprogram
16438 (New_Subp => New_Subp,
16439 Subp => Subp,
16440 Actual_Subp => Act_Subp);
16441 end if;
16442 end if;
16443 end;
16444 else
16445 Derive_Interface_Subprogram
16446 (New_Subp => New_Subp,
16447 Subp => Subp,
16448 Actual_Subp => Act_Subp);
16449 end if;
16450
16451 -- Case 3: Common derivation
16452
16453 else
16454 Derive_Subprogram
16455 (New_Subp => New_Subp,
16456 Parent_Subp => Subp,
16457 Derived_Type => Derived_Type,
16458 Parent_Type => Parent_Base,
16459 Actual_Subp => Act_Subp);
16460 end if;
16461
16462 -- No need to update Act_Elm if we must search for the
16463 -- corresponding operation in the generic actual
16464
16465 if not Need_Search
16466 and then Present (Act_Elmt)
16467 then
16468 Next_Elmt (Act_Elmt);
16469 Act_Subp := Node (Act_Elmt);
16470 end if;
16471
16472 <<Continue>>
16473 Next_Elmt (Elmt);
16474 end loop;
16475
16476 -- Inherit additional operations from progenitors. If the derived
16477 -- type is a generic actual, there are not new primitive operations
16478 -- for the type because it has those of the actual, and therefore
16479 -- nothing needs to be done. The renamings generated above are not
16480 -- primitive operations, and their purpose is simply to make the
16481 -- proper operations visible within an instantiation.
16482
16483 if No (Generic_Actual) then
16484 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16485 end if;
16486 end if;
16487
16488 -- Final check: Direct descendants must have their primitives in the
16489 -- same order. We exclude from this test untagged types and instances
16490 -- of formal derived types. We skip this test if we have already
16491 -- reported serious errors in the sources.
16492
16493 pragma Assert (not Is_Tagged_Type (Derived_Type)
16494 or else Present (Generic_Actual)
16495 or else Serious_Errors_Detected > 0
16496 or else Check_Derived_Type);
16497 end Derive_Subprograms;
16498
16499 --------------------------------
16500 -- Derived_Standard_Character --
16501 --------------------------------
16502
16503 procedure Derived_Standard_Character
16504 (N : Node_Id;
16505 Parent_Type : Entity_Id;
16506 Derived_Type : Entity_Id)
16507 is
16508 Loc : constant Source_Ptr := Sloc (N);
16509 Def : constant Node_Id := Type_Definition (N);
16510 Indic : constant Node_Id := Subtype_Indication (Def);
16511 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16512 Implicit_Base : constant Entity_Id :=
16513 Create_Itype
16514 (E_Enumeration_Type, N, Derived_Type, 'B');
16515
16516 Lo : Node_Id;
16517 Hi : Node_Id;
16518
16519 begin
16520 Discard_Node (Process_Subtype (Indic, N));
16521
16522 Set_Etype (Implicit_Base, Parent_Base);
16523 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16524 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16525
16526 Set_Is_Character_Type (Implicit_Base, True);
16527 Set_Has_Delayed_Freeze (Implicit_Base);
16528
16529 -- The bounds of the implicit base are the bounds of the parent base.
16530 -- Note that their type is the parent base.
16531
16532 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16533 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16534
16535 Set_Scalar_Range (Implicit_Base,
16536 Make_Range (Loc,
16537 Low_Bound => Lo,
16538 High_Bound => Hi));
16539
16540 Conditional_Delay (Derived_Type, Parent_Type);
16541
16542 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16543 Set_Etype (Derived_Type, Implicit_Base);
16544 Set_Size_Info (Derived_Type, Parent_Type);
16545
16546 if Unknown_RM_Size (Derived_Type) then
16547 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16548 end if;
16549
16550 Set_Is_Character_Type (Derived_Type, True);
16551
16552 if Nkind (Indic) /= N_Subtype_Indication then
16553
16554 -- If no explicit constraint, the bounds are those
16555 -- of the parent type.
16556
16557 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16558 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16559 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16560 end if;
16561
16562 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16563
16564 -- Because the implicit base is used in the conversion of the bounds, we
16565 -- have to freeze it now. This is similar to what is done for numeric
16566 -- types, and it equally suspicious, but otherwise a nonstatic bound
16567 -- will have a reference to an unfrozen type, which is rejected by Gigi
16568 -- (???). This requires specific care for definition of stream
16569 -- attributes. For details, see comments at the end of
16570 -- Build_Derived_Numeric_Type.
16571
16572 Freeze_Before (N, Implicit_Base);
16573 end Derived_Standard_Character;
16574
16575 ------------------------------
16576 -- Derived_Type_Declaration --
16577 ------------------------------
16578
16579 procedure Derived_Type_Declaration
16580 (T : Entity_Id;
16581 N : Node_Id;
16582 Is_Completion : Boolean)
16583 is
16584 Parent_Type : Entity_Id;
16585
16586 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16587 -- Check whether the parent type is a generic formal, or derives
16588 -- directly or indirectly from one.
16589
16590 ------------------------
16591 -- Comes_From_Generic --
16592 ------------------------
16593
16594 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16595 begin
16596 if Is_Generic_Type (Typ) then
16597 return True;
16598
16599 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16600 return True;
16601
16602 elsif Is_Private_Type (Typ)
16603 and then Present (Full_View (Typ))
16604 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16605 then
16606 return True;
16607
16608 elsif Is_Generic_Actual_Type (Typ) then
16609 return True;
16610
16611 else
16612 return False;
16613 end if;
16614 end Comes_From_Generic;
16615
16616 -- Local variables
16617
16618 Def : constant Node_Id := Type_Definition (N);
16619 Iface_Def : Node_Id;
16620 Indic : constant Node_Id := Subtype_Indication (Def);
16621 Extension : constant Node_Id := Record_Extension_Part (Def);
16622 Parent_Node : Node_Id;
16623 Taggd : Boolean;
16624
16625 -- Start of processing for Derived_Type_Declaration
16626
16627 begin
16628 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16629
16630 if SPARK_Mode = On
16631 and then Is_Tagged_Type (Parent_Type)
16632 then
16633 declare
16634 Partial_View : constant Entity_Id :=
16635 Incomplete_Or_Partial_View (Parent_Type);
16636
16637 begin
16638 -- If the partial view was not found then the parent type is not
16639 -- a private type. Otherwise check if the partial view is a tagged
16640 -- private type.
16641
16642 if Present (Partial_View)
16643 and then Is_Private_Type (Partial_View)
16644 and then not Is_Tagged_Type (Partial_View)
16645 then
16646 Error_Msg_NE
16647 ("cannot derive from & declared as untagged private "
16648 & "(SPARK RM 3.4(1))", N, Partial_View);
16649 end if;
16650 end;
16651 end if;
16652
16653 -- Ada 2005 (AI-251): In case of interface derivation check that the
16654 -- parent is also an interface.
16655
16656 if Interface_Present (Def) then
16657 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16658
16659 if not Is_Interface (Parent_Type) then
16660 Diagnose_Interface (Indic, Parent_Type);
16661
16662 else
16663 Parent_Node := Parent (Base_Type (Parent_Type));
16664 Iface_Def := Type_Definition (Parent_Node);
16665
16666 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16667 -- other limited interfaces.
16668
16669 if Limited_Present (Def) then
16670 if Limited_Present (Iface_Def) then
16671 null;
16672
16673 elsif Protected_Present (Iface_Def) then
16674 Error_Msg_NE
16675 ("descendant of & must be declared as a protected "
16676 & "interface", N, Parent_Type);
16677
16678 elsif Synchronized_Present (Iface_Def) then
16679 Error_Msg_NE
16680 ("descendant of & must be declared as a synchronized "
16681 & "interface", N, Parent_Type);
16682
16683 elsif Task_Present (Iface_Def) then
16684 Error_Msg_NE
16685 ("descendant of & must be declared as a task interface",
16686 N, Parent_Type);
16687
16688 else
16689 Error_Msg_N
16690 ("(Ada 2005) limited interface cannot inherit from "
16691 & "non-limited interface", Indic);
16692 end if;
16693
16694 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16695 -- from non-limited or limited interfaces.
16696
16697 elsif not Protected_Present (Def)
16698 and then not Synchronized_Present (Def)
16699 and then not Task_Present (Def)
16700 then
16701 if Limited_Present (Iface_Def) then
16702 null;
16703
16704 elsif Protected_Present (Iface_Def) then
16705 Error_Msg_NE
16706 ("descendant of & must be declared as a protected "
16707 & "interface", N, Parent_Type);
16708
16709 elsif Synchronized_Present (Iface_Def) then
16710 Error_Msg_NE
16711 ("descendant of & must be declared as a synchronized "
16712 & "interface", N, Parent_Type);
16713
16714 elsif Task_Present (Iface_Def) then
16715 Error_Msg_NE
16716 ("descendant of & must be declared as a task interface",
16717 N, Parent_Type);
16718 else
16719 null;
16720 end if;
16721 end if;
16722 end if;
16723 end if;
16724
16725 if Is_Tagged_Type (Parent_Type)
16726 and then Is_Concurrent_Type (Parent_Type)
16727 and then not Is_Interface (Parent_Type)
16728 then
16729 Error_Msg_N
16730 ("parent type of a record extension cannot be a synchronized "
16731 & "tagged type (RM 3.9.1 (3/1))", N);
16732 Set_Etype (T, Any_Type);
16733 return;
16734 end if;
16735
16736 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16737 -- interfaces
16738
16739 if Is_Tagged_Type (Parent_Type)
16740 and then Is_Non_Empty_List (Interface_List (Def))
16741 then
16742 declare
16743 Intf : Node_Id;
16744 T : Entity_Id;
16745
16746 begin
16747 Intf := First (Interface_List (Def));
16748 while Present (Intf) loop
16749 T := Find_Type_Of_Subtype_Indic (Intf);
16750
16751 if not Is_Interface (T) then
16752 Diagnose_Interface (Intf, T);
16753
16754 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16755 -- a limited type from having a nonlimited progenitor.
16756
16757 elsif (Limited_Present (Def)
16758 or else (not Is_Interface (Parent_Type)
16759 and then Is_Limited_Type (Parent_Type)))
16760 and then not Is_Limited_Interface (T)
16761 then
16762 Error_Msg_NE
16763 ("progenitor interface& of limited type must be limited",
16764 N, T);
16765 end if;
16766
16767 Next (Intf);
16768 end loop;
16769 end;
16770 end if;
16771
16772 if Parent_Type = Any_Type
16773 or else Etype (Parent_Type) = Any_Type
16774 or else (Is_Class_Wide_Type (Parent_Type)
16775 and then Etype (Parent_Type) = T)
16776 then
16777 -- If Parent_Type is undefined or illegal, make new type into a
16778 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16779 -- errors. If this is a self-definition, emit error now.
16780
16781 if T = Parent_Type or else T = Etype (Parent_Type) then
16782 Error_Msg_N ("type cannot be used in its own definition", Indic);
16783 end if;
16784
16785 Set_Ekind (T, Ekind (Parent_Type));
16786 Set_Etype (T, Any_Type);
16787 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16788
16789 if Is_Tagged_Type (T)
16790 and then Is_Record_Type (T)
16791 then
16792 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16793 end if;
16794
16795 return;
16796 end if;
16797
16798 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16799 -- an interface is special because the list of interfaces in the full
16800 -- view can be given in any order. For example:
16801
16802 -- type A is interface;
16803 -- type B is interface and A;
16804 -- type D is new B with private;
16805 -- private
16806 -- type D is new A and B with null record; -- 1 --
16807
16808 -- In this case we perform the following transformation of -1-:
16809
16810 -- type D is new B and A with null record;
16811
16812 -- If the parent of the full-view covers the parent of the partial-view
16813 -- we have two possible cases:
16814
16815 -- 1) They have the same parent
16816 -- 2) The parent of the full-view implements some further interfaces
16817
16818 -- In both cases we do not need to perform the transformation. In the
16819 -- first case the source program is correct and the transformation is
16820 -- not needed; in the second case the source program does not fulfill
16821 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16822 -- later.
16823
16824 -- This transformation not only simplifies the rest of the analysis of
16825 -- this type declaration but also simplifies the correct generation of
16826 -- the object layout to the expander.
16827
16828 if In_Private_Part (Current_Scope)
16829 and then Is_Interface (Parent_Type)
16830 then
16831 declare
16832 Iface : Node_Id;
16833 Partial_View : Entity_Id;
16834 Partial_View_Parent : Entity_Id;
16835 New_Iface : Node_Id;
16836
16837 begin
16838 -- Look for the associated private type declaration
16839
16840 Partial_View := Incomplete_Or_Partial_View (T);
16841
16842 -- If the partial view was not found then the source code has
16843 -- errors and the transformation is not needed.
16844
16845 if Present (Partial_View) then
16846 Partial_View_Parent := Etype (Partial_View);
16847
16848 -- If the parent of the full-view covers the parent of the
16849 -- partial-view we have nothing else to do.
16850
16851 if Interface_Present_In_Ancestor
16852 (Parent_Type, Partial_View_Parent)
16853 then
16854 null;
16855
16856 -- Traverse the list of interfaces of the full-view to look
16857 -- for the parent of the partial-view and perform the tree
16858 -- transformation.
16859
16860 else
16861 Iface := First (Interface_List (Def));
16862 while Present (Iface) loop
16863 if Etype (Iface) = Etype (Partial_View) then
16864 Rewrite (Subtype_Indication (Def),
16865 New_Copy (Subtype_Indication
16866 (Parent (Partial_View))));
16867
16868 New_Iface :=
16869 Make_Identifier (Sloc (N), Chars (Parent_Type));
16870 Append (New_Iface, Interface_List (Def));
16871
16872 -- Analyze the transformed code
16873
16874 Derived_Type_Declaration (T, N, Is_Completion);
16875 return;
16876 end if;
16877
16878 Next (Iface);
16879 end loop;
16880 end if;
16881 end if;
16882 end;
16883 end if;
16884
16885 -- Only composite types other than array types are allowed to have
16886 -- discriminants.
16887
16888 if Present (Discriminant_Specifications (N)) then
16889 if (Is_Elementary_Type (Parent_Type)
16890 or else
16891 Is_Array_Type (Parent_Type))
16892 and then not Error_Posted (N)
16893 then
16894 Error_Msg_N
16895 ("elementary or array type cannot have discriminants",
16896 Defining_Identifier (First (Discriminant_Specifications (N))));
16897
16898 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16899 -- only if we are not already processing a malformed syntax tree.
16900
16901 if Is_Type (T) then
16902 Set_Has_Discriminants (T, False);
16903 end if;
16904
16905 -- The type is allowed to have discriminants
16906
16907 else
16908 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16909 end if;
16910 end if;
16911
16912 -- In Ada 83, a derived type defined in a package specification cannot
16913 -- be used for further derivation until the end of its visible part.
16914 -- Note that derivation in the private part of the package is allowed.
16915
16916 if Ada_Version = Ada_83
16917 and then Is_Derived_Type (Parent_Type)
16918 and then In_Visible_Part (Scope (Parent_Type))
16919 then
16920 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16921 Error_Msg_N
16922 ("(Ada 83): premature use of type for derivation", Indic);
16923 end if;
16924 end if;
16925
16926 -- Check for early use of incomplete or private type
16927
16928 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16929 Error_Msg_N ("premature derivation of incomplete type", Indic);
16930 return;
16931
16932 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16933 and then not Comes_From_Generic (Parent_Type))
16934 or else Has_Private_Component (Parent_Type)
16935 then
16936 -- The ancestor type of a formal type can be incomplete, in which
16937 -- case only the operations of the partial view are available in the
16938 -- generic. Subsequent checks may be required when the full view is
16939 -- analyzed to verify that a derivation from a tagged type has an
16940 -- extension.
16941
16942 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16943 null;
16944
16945 elsif No (Underlying_Type (Parent_Type))
16946 or else Has_Private_Component (Parent_Type)
16947 then
16948 Error_Msg_N
16949 ("premature derivation of derived or private type", Indic);
16950
16951 -- Flag the type itself as being in error, this prevents some
16952 -- nasty problems with subsequent uses of the malformed type.
16953
16954 Set_Error_Posted (T);
16955
16956 -- Check that within the immediate scope of an untagged partial
16957 -- view it's illegal to derive from the partial view if the
16958 -- full view is tagged. (7.3(7))
16959
16960 -- We verify that the Parent_Type is a partial view by checking
16961 -- that it is not a Full_Type_Declaration (i.e. a private type or
16962 -- private extension declaration), to distinguish a partial view
16963 -- from a derivation from a private type which also appears as
16964 -- E_Private_Type. If the parent base type is not declared in an
16965 -- enclosing scope there is no need to check.
16966
16967 elsif Present (Full_View (Parent_Type))
16968 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16969 and then not Is_Tagged_Type (Parent_Type)
16970 and then Is_Tagged_Type (Full_View (Parent_Type))
16971 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16972 then
16973 Error_Msg_N
16974 ("premature derivation from type with tagged full view",
16975 Indic);
16976 end if;
16977 end if;
16978
16979 -- Check that form of derivation is appropriate
16980
16981 Taggd := Is_Tagged_Type (Parent_Type);
16982
16983 -- Set the parent type to the class-wide type's specific type in this
16984 -- case to prevent cascading errors
16985
16986 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16987 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16988 Set_Etype (T, Etype (Parent_Type));
16989 return;
16990 end if;
16991
16992 if Present (Extension) and then not Taggd then
16993 Error_Msg_N
16994 ("type derived from untagged type cannot have extension", Indic);
16995
16996 elsif No (Extension) and then Taggd then
16997
16998 -- If this declaration is within a private part (or body) of a
16999 -- generic instantiation then the derivation is allowed (the parent
17000 -- type can only appear tagged in this case if it's a generic actual
17001 -- type, since it would otherwise have been rejected in the analysis
17002 -- of the generic template).
17003
17004 if not Is_Generic_Actual_Type (Parent_Type)
17005 or else In_Visible_Part (Scope (Parent_Type))
17006 then
17007 if Is_Class_Wide_Type (Parent_Type) then
17008 Error_Msg_N
17009 ("parent type must not be a class-wide type", Indic);
17010
17011 -- Use specific type to prevent cascaded errors.
17012
17013 Parent_Type := Etype (Parent_Type);
17014
17015 else
17016 Error_Msg_N
17017 ("type derived from tagged type must have extension", Indic);
17018 end if;
17019 end if;
17020 end if;
17021
17022 -- AI-443: Synchronized formal derived types require a private
17023 -- extension. There is no point in checking the ancestor type or
17024 -- the progenitors since the construct is wrong to begin with.
17025
17026 if Ada_Version >= Ada_2005
17027 and then Is_Generic_Type (T)
17028 and then Present (Original_Node (N))
17029 then
17030 declare
17031 Decl : constant Node_Id := Original_Node (N);
17032
17033 begin
17034 if Nkind (Decl) = N_Formal_Type_Declaration
17035 and then Nkind (Formal_Type_Definition (Decl)) =
17036 N_Formal_Derived_Type_Definition
17037 and then Synchronized_Present (Formal_Type_Definition (Decl))
17038 and then No (Extension)
17039
17040 -- Avoid emitting a duplicate error message
17041
17042 and then not Error_Posted (Indic)
17043 then
17044 Error_Msg_N
17045 ("synchronized derived type must have extension", N);
17046 end if;
17047 end;
17048 end if;
17049
17050 if Null_Exclusion_Present (Def)
17051 and then not Is_Access_Type (Parent_Type)
17052 then
17053 Error_Msg_N ("null exclusion can only apply to an access type", N);
17054 end if;
17055
17056 -- Avoid deriving parent primitives of underlying record views
17057
17058 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17059 Derive_Subps => not Is_Underlying_Record_View (T));
17060
17061 -- AI-419: The parent type of an explicitly limited derived type must
17062 -- be a limited type or a limited interface.
17063
17064 if Limited_Present (Def) then
17065 Set_Is_Limited_Record (T);
17066
17067 if Is_Interface (T) then
17068 Set_Is_Limited_Interface (T);
17069 end if;
17070
17071 if not Is_Limited_Type (Parent_Type)
17072 and then
17073 (not Is_Interface (Parent_Type)
17074 or else not Is_Limited_Interface (Parent_Type))
17075 then
17076 -- AI05-0096: a derivation in the private part of an instance is
17077 -- legal if the generic formal is untagged limited, and the actual
17078 -- is non-limited.
17079
17080 if Is_Generic_Actual_Type (Parent_Type)
17081 and then In_Private_Part (Current_Scope)
17082 and then
17083 not Is_Tagged_Type
17084 (Generic_Parent_Type (Parent (Parent_Type)))
17085 then
17086 null;
17087
17088 else
17089 Error_Msg_NE
17090 ("parent type& of limited type must be limited",
17091 N, Parent_Type);
17092 end if;
17093 end if;
17094 end if;
17095
17096 -- In SPARK, there are no derived type definitions other than type
17097 -- extensions of tagged record types.
17098
17099 if No (Extension) then
17100 Check_SPARK_05_Restriction
17101 ("derived type is not allowed", Original_Node (N));
17102 end if;
17103 end Derived_Type_Declaration;
17104
17105 ------------------------
17106 -- Diagnose_Interface --
17107 ------------------------
17108
17109 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17110 begin
17111 if not Is_Interface (E) and then E /= Any_Type then
17112 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17113 end if;
17114 end Diagnose_Interface;
17115
17116 ----------------------------------
17117 -- Enumeration_Type_Declaration --
17118 ----------------------------------
17119
17120 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17121 Ev : Uint;
17122 L : Node_Id;
17123 R_Node : Node_Id;
17124 B_Node : Node_Id;
17125
17126 begin
17127 -- Create identifier node representing lower bound
17128
17129 B_Node := New_Node (N_Identifier, Sloc (Def));
17130 L := First (Literals (Def));
17131 Set_Chars (B_Node, Chars (L));
17132 Set_Entity (B_Node, L);
17133 Set_Etype (B_Node, T);
17134 Set_Is_Static_Expression (B_Node, True);
17135
17136 R_Node := New_Node (N_Range, Sloc (Def));
17137 Set_Low_Bound (R_Node, B_Node);
17138
17139 Set_Ekind (T, E_Enumeration_Type);
17140 Set_First_Literal (T, L);
17141 Set_Etype (T, T);
17142 Set_Is_Constrained (T);
17143
17144 Ev := Uint_0;
17145
17146 -- Loop through literals of enumeration type setting pos and rep values
17147 -- except that if the Ekind is already set, then it means the literal
17148 -- was already constructed (case of a derived type declaration and we
17149 -- should not disturb the Pos and Rep values.
17150
17151 while Present (L) loop
17152 if Ekind (L) /= E_Enumeration_Literal then
17153 Set_Ekind (L, E_Enumeration_Literal);
17154 Set_Enumeration_Pos (L, Ev);
17155 Set_Enumeration_Rep (L, Ev);
17156 Set_Is_Known_Valid (L, True);
17157 end if;
17158
17159 Set_Etype (L, T);
17160 New_Overloaded_Entity (L);
17161 Generate_Definition (L);
17162 Set_Convention (L, Convention_Intrinsic);
17163
17164 -- Case of character literal
17165
17166 if Nkind (L) = N_Defining_Character_Literal then
17167 Set_Is_Character_Type (T, True);
17168
17169 -- Check violation of No_Wide_Characters
17170
17171 if Restriction_Check_Required (No_Wide_Characters) then
17172 Get_Name_String (Chars (L));
17173
17174 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17175 Check_Restriction (No_Wide_Characters, L);
17176 end if;
17177 end if;
17178 end if;
17179
17180 Ev := Ev + 1;
17181 Next (L);
17182 end loop;
17183
17184 -- Now create a node representing upper bound
17185
17186 B_Node := New_Node (N_Identifier, Sloc (Def));
17187 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17188 Set_Entity (B_Node, Last (Literals (Def)));
17189 Set_Etype (B_Node, T);
17190 Set_Is_Static_Expression (B_Node, True);
17191
17192 Set_High_Bound (R_Node, B_Node);
17193
17194 -- Initialize various fields of the type. Some of this information
17195 -- may be overwritten later through rep.clauses.
17196
17197 Set_Scalar_Range (T, R_Node);
17198 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17199 Set_Enum_Esize (T);
17200 Set_Enum_Pos_To_Rep (T, Empty);
17201
17202 -- Set Discard_Names if configuration pragma set, or if there is
17203 -- a parameterless pragma in the current declarative region
17204
17205 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17206 Set_Discard_Names (T);
17207 end if;
17208
17209 -- Process end label if there is one
17210
17211 if Present (Def) then
17212 Process_End_Label (Def, 'e', T);
17213 end if;
17214 end Enumeration_Type_Declaration;
17215
17216 ---------------------------------
17217 -- Expand_To_Stored_Constraint --
17218 ---------------------------------
17219
17220 function Expand_To_Stored_Constraint
17221 (Typ : Entity_Id;
17222 Constraint : Elist_Id) return Elist_Id
17223 is
17224 Explicitly_Discriminated_Type : Entity_Id;
17225 Expansion : Elist_Id;
17226 Discriminant : Entity_Id;
17227
17228 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17229 -- Find the nearest type that actually specifies discriminants
17230
17231 ---------------------------------
17232 -- Type_With_Explicit_Discrims --
17233 ---------------------------------
17234
17235 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17236 Typ : constant E := Base_Type (Id);
17237
17238 begin
17239 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17240 if Present (Full_View (Typ)) then
17241 return Type_With_Explicit_Discrims (Full_View (Typ));
17242 end if;
17243
17244 else
17245 if Has_Discriminants (Typ) then
17246 return Typ;
17247 end if;
17248 end if;
17249
17250 if Etype (Typ) = Typ then
17251 return Empty;
17252 elsif Has_Discriminants (Typ) then
17253 return Typ;
17254 else
17255 return Type_With_Explicit_Discrims (Etype (Typ));
17256 end if;
17257
17258 end Type_With_Explicit_Discrims;
17259
17260 -- Start of processing for Expand_To_Stored_Constraint
17261
17262 begin
17263 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17264 return No_Elist;
17265 end if;
17266
17267 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17268
17269 if No (Explicitly_Discriminated_Type) then
17270 return No_Elist;
17271 end if;
17272
17273 Expansion := New_Elmt_List;
17274
17275 Discriminant :=
17276 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17277 while Present (Discriminant) loop
17278 Append_Elmt
17279 (Get_Discriminant_Value
17280 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17281 To => Expansion);
17282 Next_Stored_Discriminant (Discriminant);
17283 end loop;
17284
17285 return Expansion;
17286 end Expand_To_Stored_Constraint;
17287
17288 ---------------------------
17289 -- Find_Hidden_Interface --
17290 ---------------------------
17291
17292 function Find_Hidden_Interface
17293 (Src : Elist_Id;
17294 Dest : Elist_Id) return Entity_Id
17295 is
17296 Iface : Entity_Id;
17297 Iface_Elmt : Elmt_Id;
17298
17299 begin
17300 if Present (Src) and then Present (Dest) then
17301 Iface_Elmt := First_Elmt (Src);
17302 while Present (Iface_Elmt) loop
17303 Iface := Node (Iface_Elmt);
17304
17305 if Is_Interface (Iface)
17306 and then not Contain_Interface (Iface, Dest)
17307 then
17308 return Iface;
17309 end if;
17310
17311 Next_Elmt (Iface_Elmt);
17312 end loop;
17313 end if;
17314
17315 return Empty;
17316 end Find_Hidden_Interface;
17317
17318 --------------------
17319 -- Find_Type_Name --
17320 --------------------
17321
17322 function Find_Type_Name (N : Node_Id) return Entity_Id is
17323 Id : constant Entity_Id := Defining_Identifier (N);
17324 New_Id : Entity_Id;
17325 Prev : Entity_Id;
17326 Prev_Par : Node_Id;
17327
17328 procedure Check_Duplicate_Aspects;
17329 -- Check that aspects specified in a completion have not been specified
17330 -- already in the partial view.
17331
17332 procedure Tag_Mismatch;
17333 -- Diagnose a tagged partial view whose full view is untagged. We post
17334 -- the message on the full view, with a reference to the previous
17335 -- partial view. The partial view can be private or incomplete, and
17336 -- these are handled in a different manner, so we determine the position
17337 -- of the error message from the respective slocs of both.
17338
17339 -----------------------------
17340 -- Check_Duplicate_Aspects --
17341 -----------------------------
17342
17343 procedure Check_Duplicate_Aspects is
17344 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17345 -- Return the corresponding aspect of the partial view which matches
17346 -- the aspect id of Asp. Return Empty is no such aspect exists.
17347
17348 -----------------------------
17349 -- Get_Partial_View_Aspect --
17350 -----------------------------
17351
17352 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17353 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17354 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17355 Prev_Asp : Node_Id;
17356
17357 begin
17358 if Present (Prev_Asps) then
17359 Prev_Asp := First (Prev_Asps);
17360 while Present (Prev_Asp) loop
17361 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17362 return Prev_Asp;
17363 end if;
17364
17365 Next (Prev_Asp);
17366 end loop;
17367 end if;
17368
17369 return Empty;
17370 end Get_Partial_View_Aspect;
17371
17372 -- Local variables
17373
17374 Full_Asps : constant List_Id := Aspect_Specifications (N);
17375 Full_Asp : Node_Id;
17376 Part_Asp : Node_Id;
17377
17378 -- Start of processing for Check_Duplicate_Aspects
17379
17380 begin
17381 if Present (Full_Asps) then
17382 Full_Asp := First (Full_Asps);
17383 while Present (Full_Asp) loop
17384 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17385
17386 -- An aspect and its class-wide counterpart are two distinct
17387 -- aspects and may apply to both views of an entity.
17388
17389 if Present (Part_Asp)
17390 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17391 then
17392 Error_Msg_N
17393 ("aspect already specified in private declaration",
17394 Full_Asp);
17395
17396 Remove (Full_Asp);
17397 return;
17398 end if;
17399
17400 if Has_Discriminants (Prev)
17401 and then not Has_Unknown_Discriminants (Prev)
17402 and then Get_Aspect_Id (Full_Asp) =
17403 Aspect_Implicit_Dereference
17404 then
17405 Error_Msg_N
17406 ("cannot specify aspect if partial view has known "
17407 & "discriminants", Full_Asp);
17408 end if;
17409
17410 Next (Full_Asp);
17411 end loop;
17412 end if;
17413 end Check_Duplicate_Aspects;
17414
17415 ------------------
17416 -- Tag_Mismatch --
17417 ------------------
17418
17419 procedure Tag_Mismatch is
17420 begin
17421 if Sloc (Prev) < Sloc (Id) then
17422 if Ada_Version >= Ada_2012
17423 and then Nkind (N) = N_Private_Type_Declaration
17424 then
17425 Error_Msg_NE
17426 ("declaration of private } must be a tagged type ", Id, Prev);
17427 else
17428 Error_Msg_NE
17429 ("full declaration of } must be a tagged type ", Id, Prev);
17430 end if;
17431
17432 else
17433 if Ada_Version >= Ada_2012
17434 and then Nkind (N) = N_Private_Type_Declaration
17435 then
17436 Error_Msg_NE
17437 ("declaration of private } must be a tagged type ", Prev, Id);
17438 else
17439 Error_Msg_NE
17440 ("full declaration of } must be a tagged type ", Prev, Id);
17441 end if;
17442 end if;
17443 end Tag_Mismatch;
17444
17445 -- Start of processing for Find_Type_Name
17446
17447 begin
17448 -- Find incomplete declaration, if one was given
17449
17450 Prev := Current_Entity_In_Scope (Id);
17451
17452 -- New type declaration
17453
17454 if No (Prev) then
17455 Enter_Name (Id);
17456 return Id;
17457
17458 -- Previous declaration exists
17459
17460 else
17461 Prev_Par := Parent (Prev);
17462
17463 -- Error if not incomplete/private case except if previous
17464 -- declaration is implicit, etc. Enter_Name will emit error if
17465 -- appropriate.
17466
17467 if not Is_Incomplete_Or_Private_Type (Prev) then
17468 Enter_Name (Id);
17469 New_Id := Id;
17470
17471 -- Check invalid completion of private or incomplete type
17472
17473 elsif not Nkind_In (N, N_Full_Type_Declaration,
17474 N_Task_Type_Declaration,
17475 N_Protected_Type_Declaration)
17476 and then
17477 (Ada_Version < Ada_2012
17478 or else not Is_Incomplete_Type (Prev)
17479 or else not Nkind_In (N, N_Private_Type_Declaration,
17480 N_Private_Extension_Declaration))
17481 then
17482 -- Completion must be a full type declarations (RM 7.3(4))
17483
17484 Error_Msg_Sloc := Sloc (Prev);
17485 Error_Msg_NE ("invalid completion of }", Id, Prev);
17486
17487 -- Set scope of Id to avoid cascaded errors. Entity is never
17488 -- examined again, except when saving globals in generics.
17489
17490 Set_Scope (Id, Current_Scope);
17491 New_Id := Id;
17492
17493 -- If this is a repeated incomplete declaration, no further
17494 -- checks are possible.
17495
17496 if Nkind (N) = N_Incomplete_Type_Declaration then
17497 return Prev;
17498 end if;
17499
17500 -- Case of full declaration of incomplete type
17501
17502 elsif Ekind (Prev) = E_Incomplete_Type
17503 and then (Ada_Version < Ada_2012
17504 or else No (Full_View (Prev))
17505 or else not Is_Private_Type (Full_View (Prev)))
17506 then
17507 -- Indicate that the incomplete declaration has a matching full
17508 -- declaration. The defining occurrence of the incomplete
17509 -- declaration remains the visible one, and the procedure
17510 -- Get_Full_View dereferences it whenever the type is used.
17511
17512 if Present (Full_View (Prev)) then
17513 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17514 end if;
17515
17516 Set_Full_View (Prev, Id);
17517 Append_Entity (Id, Current_Scope);
17518 Set_Is_Public (Id, Is_Public (Prev));
17519 Set_Is_Internal (Id);
17520 New_Id := Prev;
17521
17522 -- If the incomplete view is tagged, a class_wide type has been
17523 -- created already. Use it for the private type as well, in order
17524 -- to prevent multiple incompatible class-wide types that may be
17525 -- created for self-referential anonymous access components.
17526
17527 if Is_Tagged_Type (Prev)
17528 and then Present (Class_Wide_Type (Prev))
17529 then
17530 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17531 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17532
17533 -- Type of the class-wide type is the current Id. Previously
17534 -- this was not done for private declarations because of order-
17535 -- of-elaboration issues in the back end, but gigi now handles
17536 -- this properly.
17537
17538 Set_Etype (Class_Wide_Type (Id), Id);
17539 end if;
17540
17541 -- Case of full declaration of private type
17542
17543 else
17544 -- If the private type was a completion of an incomplete type then
17545 -- update Prev to reference the private type
17546
17547 if Ada_Version >= Ada_2012
17548 and then Ekind (Prev) = E_Incomplete_Type
17549 and then Present (Full_View (Prev))
17550 and then Is_Private_Type (Full_View (Prev))
17551 then
17552 Prev := Full_View (Prev);
17553 Prev_Par := Parent (Prev);
17554 end if;
17555
17556 if Nkind (N) = N_Full_Type_Declaration
17557 and then Nkind_In
17558 (Type_Definition (N), N_Record_Definition,
17559 N_Derived_Type_Definition)
17560 and then Interface_Present (Type_Definition (N))
17561 then
17562 Error_Msg_N
17563 ("completion of private type cannot be an interface", N);
17564 end if;
17565
17566 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17567 if Etype (Prev) /= Prev then
17568
17569 -- Prev is a private subtype or a derived type, and needs
17570 -- no completion.
17571
17572 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17573 New_Id := Id;
17574
17575 elsif Ekind (Prev) = E_Private_Type
17576 and then Nkind_In (N, N_Task_Type_Declaration,
17577 N_Protected_Type_Declaration)
17578 then
17579 Error_Msg_N
17580 ("completion of nonlimited type cannot be limited", N);
17581
17582 elsif Ekind (Prev) = E_Record_Type_With_Private
17583 and then Nkind_In (N, N_Task_Type_Declaration,
17584 N_Protected_Type_Declaration)
17585 then
17586 if not Is_Limited_Record (Prev) then
17587 Error_Msg_N
17588 ("completion of nonlimited type cannot be limited", N);
17589
17590 elsif No (Interface_List (N)) then
17591 Error_Msg_N
17592 ("completion of tagged private type must be tagged",
17593 N);
17594 end if;
17595 end if;
17596
17597 -- Ada 2005 (AI-251): Private extension declaration of a task
17598 -- type or a protected type. This case arises when covering
17599 -- interface types.
17600
17601 elsif Nkind_In (N, N_Task_Type_Declaration,
17602 N_Protected_Type_Declaration)
17603 then
17604 null;
17605
17606 elsif Nkind (N) /= N_Full_Type_Declaration
17607 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17608 then
17609 Error_Msg_N
17610 ("full view of private extension must be an extension", N);
17611
17612 elsif not (Abstract_Present (Parent (Prev)))
17613 and then Abstract_Present (Type_Definition (N))
17614 then
17615 Error_Msg_N
17616 ("full view of non-abstract extension cannot be abstract", N);
17617 end if;
17618
17619 if not In_Private_Part (Current_Scope) then
17620 Error_Msg_N
17621 ("declaration of full view must appear in private part", N);
17622 end if;
17623
17624 if Ada_Version >= Ada_2012 then
17625 Check_Duplicate_Aspects;
17626 end if;
17627
17628 Copy_And_Swap (Prev, Id);
17629 Set_Has_Private_Declaration (Prev);
17630 Set_Has_Private_Declaration (Id);
17631
17632 -- AI12-0133: Indicate whether we have a partial view with
17633 -- unknown discriminants, in which case initialization of objects
17634 -- of the type do not receive an invariant check.
17635
17636 Set_Partial_View_Has_Unknown_Discr
17637 (Prev, Has_Unknown_Discriminants (Id));
17638
17639 -- Preserve aspect and iterator flags that may have been set on
17640 -- the partial view.
17641
17642 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17643 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17644
17645 -- If no error, propagate freeze_node from private to full view.
17646 -- It may have been generated for an early operational item.
17647
17648 if Present (Freeze_Node (Id))
17649 and then Serious_Errors_Detected = 0
17650 and then No (Full_View (Id))
17651 then
17652 Set_Freeze_Node (Prev, Freeze_Node (Id));
17653 Set_Freeze_Node (Id, Empty);
17654 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17655 end if;
17656
17657 Set_Full_View (Id, Prev);
17658 New_Id := Prev;
17659 end if;
17660
17661 -- Verify that full declaration conforms to partial one
17662
17663 if Is_Incomplete_Or_Private_Type (Prev)
17664 and then Present (Discriminant_Specifications (Prev_Par))
17665 then
17666 if Present (Discriminant_Specifications (N)) then
17667 if Ekind (Prev) = E_Incomplete_Type then
17668 Check_Discriminant_Conformance (N, Prev, Prev);
17669 else
17670 Check_Discriminant_Conformance (N, Prev, Id);
17671 end if;
17672
17673 else
17674 Error_Msg_N
17675 ("missing discriminants in full type declaration", N);
17676
17677 -- To avoid cascaded errors on subsequent use, share the
17678 -- discriminants of the partial view.
17679
17680 Set_Discriminant_Specifications (N,
17681 Discriminant_Specifications (Prev_Par));
17682 end if;
17683 end if;
17684
17685 -- A prior untagged partial view can have an associated class-wide
17686 -- type due to use of the class attribute, and in this case the full
17687 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17688 -- of incomplete tagged declarations, but we check for it.
17689
17690 if Is_Type (Prev)
17691 and then (Is_Tagged_Type (Prev)
17692 or else Present (Class_Wide_Type (Prev)))
17693 then
17694 -- Ada 2012 (AI05-0162): A private type may be the completion of
17695 -- an incomplete type.
17696
17697 if Ada_Version >= Ada_2012
17698 and then Is_Incomplete_Type (Prev)
17699 and then Nkind_In (N, N_Private_Type_Declaration,
17700 N_Private_Extension_Declaration)
17701 then
17702 -- No need to check private extensions since they are tagged
17703
17704 if Nkind (N) = N_Private_Type_Declaration
17705 and then not Tagged_Present (N)
17706 then
17707 Tag_Mismatch;
17708 end if;
17709
17710 -- The full declaration is either a tagged type (including
17711 -- a synchronized type that implements interfaces) or a
17712 -- type extension, otherwise this is an error.
17713
17714 elsif Nkind_In (N, N_Task_Type_Declaration,
17715 N_Protected_Type_Declaration)
17716 then
17717 if No (Interface_List (N)) and then not Error_Posted (N) then
17718 Tag_Mismatch;
17719 end if;
17720
17721 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17722
17723 -- Indicate that the previous declaration (tagged incomplete
17724 -- or private declaration) requires the same on the full one.
17725
17726 if not Tagged_Present (Type_Definition (N)) then
17727 Tag_Mismatch;
17728 Set_Is_Tagged_Type (Id);
17729 end if;
17730
17731 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17732 if No (Record_Extension_Part (Type_Definition (N))) then
17733 Error_Msg_NE
17734 ("full declaration of } must be a record extension",
17735 Prev, Id);
17736
17737 -- Set some attributes to produce a usable full view
17738
17739 Set_Is_Tagged_Type (Id);
17740 end if;
17741
17742 else
17743 Tag_Mismatch;
17744 end if;
17745 end if;
17746
17747 if Present (Prev)
17748 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17749 and then Present (Premature_Use (Parent (Prev)))
17750 then
17751 Error_Msg_Sloc := Sloc (N);
17752 Error_Msg_N
17753 ("\full declaration #", Premature_Use (Parent (Prev)));
17754 end if;
17755
17756 return New_Id;
17757 end if;
17758 end Find_Type_Name;
17759
17760 -------------------------
17761 -- Find_Type_Of_Object --
17762 -------------------------
17763
17764 function Find_Type_Of_Object
17765 (Obj_Def : Node_Id;
17766 Related_Nod : Node_Id) return Entity_Id
17767 is
17768 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17769 P : Node_Id := Parent (Obj_Def);
17770 T : Entity_Id;
17771 Nam : Name_Id;
17772
17773 begin
17774 -- If the parent is a component_definition node we climb to the
17775 -- component_declaration node
17776
17777 if Nkind (P) = N_Component_Definition then
17778 P := Parent (P);
17779 end if;
17780
17781 -- Case of an anonymous array subtype
17782
17783 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17784 N_Unconstrained_Array_Definition)
17785 then
17786 T := Empty;
17787 Array_Type_Declaration (T, Obj_Def);
17788
17789 -- Create an explicit subtype whenever possible
17790
17791 elsif Nkind (P) /= N_Component_Declaration
17792 and then Def_Kind = N_Subtype_Indication
17793 then
17794 -- Base name of subtype on object name, which will be unique in
17795 -- the current scope.
17796
17797 -- If this is a duplicate declaration, return base type, to avoid
17798 -- generating duplicate anonymous types.
17799
17800 if Error_Posted (P) then
17801 Analyze (Subtype_Mark (Obj_Def));
17802 return Entity (Subtype_Mark (Obj_Def));
17803 end if;
17804
17805 Nam :=
17806 New_External_Name
17807 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17808
17809 T := Make_Defining_Identifier (Sloc (P), Nam);
17810
17811 Insert_Action (Obj_Def,
17812 Make_Subtype_Declaration (Sloc (P),
17813 Defining_Identifier => T,
17814 Subtype_Indication => Relocate_Node (Obj_Def)));
17815
17816 -- This subtype may need freezing, and this will not be done
17817 -- automatically if the object declaration is not in declarative
17818 -- part. Since this is an object declaration, the type cannot always
17819 -- be frozen here. Deferred constants do not freeze their type
17820 -- (which often enough will be private).
17821
17822 if Nkind (P) = N_Object_Declaration
17823 and then Constant_Present (P)
17824 and then No (Expression (P))
17825 then
17826 null;
17827
17828 -- Here we freeze the base type of object type to catch premature use
17829 -- of discriminated private type without a full view.
17830
17831 else
17832 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17833 end if;
17834
17835 -- Ada 2005 AI-406: the object definition in an object declaration
17836 -- can be an access definition.
17837
17838 elsif Def_Kind = N_Access_Definition then
17839 T := Access_Definition (Related_Nod, Obj_Def);
17840
17841 Set_Is_Local_Anonymous_Access
17842 (T,
17843 V => (Ada_Version < Ada_2012)
17844 or else (Nkind (P) /= N_Object_Declaration)
17845 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17846
17847 -- Otherwise, the object definition is just a subtype_mark
17848
17849 else
17850 T := Process_Subtype (Obj_Def, Related_Nod);
17851
17852 -- If expansion is disabled an object definition that is an aggregate
17853 -- will not get expanded and may lead to scoping problems in the back
17854 -- end, if the object is referenced in an inner scope. In that case
17855 -- create an itype reference for the object definition now. This
17856 -- may be redundant in some cases, but harmless.
17857
17858 if Is_Itype (T)
17859 and then Nkind (Related_Nod) = N_Object_Declaration
17860 and then ASIS_Mode
17861 then
17862 Build_Itype_Reference (T, Related_Nod);
17863 end if;
17864 end if;
17865
17866 return T;
17867 end Find_Type_Of_Object;
17868
17869 --------------------------------
17870 -- Find_Type_Of_Subtype_Indic --
17871 --------------------------------
17872
17873 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17874 Typ : Entity_Id;
17875
17876 begin
17877 -- Case of subtype mark with a constraint
17878
17879 if Nkind (S) = N_Subtype_Indication then
17880 Find_Type (Subtype_Mark (S));
17881 Typ := Entity (Subtype_Mark (S));
17882
17883 if not
17884 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17885 then
17886 Error_Msg_N
17887 ("incorrect constraint for this kind of type", Constraint (S));
17888 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17889 end if;
17890
17891 -- Otherwise we have a subtype mark without a constraint
17892
17893 elsif Error_Posted (S) then
17894 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17895 return Any_Type;
17896
17897 else
17898 Find_Type (S);
17899 Typ := Entity (S);
17900 end if;
17901
17902 -- Check No_Wide_Characters restriction
17903
17904 Check_Wide_Character_Restriction (Typ, S);
17905
17906 return Typ;
17907 end Find_Type_Of_Subtype_Indic;
17908
17909 -------------------------------------
17910 -- Floating_Point_Type_Declaration --
17911 -------------------------------------
17912
17913 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17914 Digs : constant Node_Id := Digits_Expression (Def);
17915 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17916 Digs_Val : Uint;
17917 Base_Typ : Entity_Id;
17918 Implicit_Base : Entity_Id;
17919
17920 function Can_Derive_From (E : Entity_Id) return Boolean;
17921 -- Find if given digits value, and possibly a specified range, allows
17922 -- derivation from specified type
17923
17924 procedure Convert_Bound (B : Node_Id);
17925 -- If specified, the bounds must be static but may be of different
17926 -- types. They must be converted into machine numbers of the base type,
17927 -- in accordance with RM 4.9(38).
17928
17929 function Find_Base_Type return Entity_Id;
17930 -- Find a predefined base type that Def can derive from, or generate
17931 -- an error and substitute Long_Long_Float if none exists.
17932
17933 ---------------------
17934 -- Can_Derive_From --
17935 ---------------------
17936
17937 function Can_Derive_From (E : Entity_Id) return Boolean is
17938 Spec : constant Entity_Id := Real_Range_Specification (Def);
17939
17940 begin
17941 -- Check specified "digits" constraint
17942
17943 if Digs_Val > Digits_Value (E) then
17944 return False;
17945 end if;
17946
17947 -- Check for matching range, if specified
17948
17949 if Present (Spec) then
17950 if Expr_Value_R (Type_Low_Bound (E)) >
17951 Expr_Value_R (Low_Bound (Spec))
17952 then
17953 return False;
17954 end if;
17955
17956 if Expr_Value_R (Type_High_Bound (E)) <
17957 Expr_Value_R (High_Bound (Spec))
17958 then
17959 return False;
17960 end if;
17961 end if;
17962
17963 return True;
17964 end Can_Derive_From;
17965
17966 -------------------
17967 -- Convert_Bound --
17968 --------------------
17969
17970 procedure Convert_Bound (B : Node_Id) is
17971 begin
17972 -- If the bound is not a literal it can only be static if it is
17973 -- a static constant, possibly of a specified type.
17974
17975 if Is_Entity_Name (B)
17976 and then Ekind (Entity (B)) = E_Constant
17977 then
17978 Rewrite (B, Constant_Value (Entity (B)));
17979 end if;
17980
17981 if Nkind (B) = N_Real_Literal then
17982 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17983 Set_Is_Machine_Number (B);
17984 Set_Etype (B, Base_Typ);
17985 end if;
17986 end Convert_Bound;
17987
17988 --------------------
17989 -- Find_Base_Type --
17990 --------------------
17991
17992 function Find_Base_Type return Entity_Id is
17993 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17994
17995 begin
17996 -- Iterate over the predefined types in order, returning the first
17997 -- one that Def can derive from.
17998
17999 while Present (Choice) loop
18000 if Can_Derive_From (Node (Choice)) then
18001 return Node (Choice);
18002 end if;
18003
18004 Next_Elmt (Choice);
18005 end loop;
18006
18007 -- If we can't derive from any existing type, use Long_Long_Float
18008 -- and give appropriate message explaining the problem.
18009
18010 if Digs_Val > Max_Digs_Val then
18011 -- It might be the case that there is a type with the requested
18012 -- range, just not the combination of digits and range.
18013
18014 Error_Msg_N
18015 ("no predefined type has requested range and precision",
18016 Real_Range_Specification (Def));
18017
18018 else
18019 Error_Msg_N
18020 ("range too large for any predefined type",
18021 Real_Range_Specification (Def));
18022 end if;
18023
18024 return Standard_Long_Long_Float;
18025 end Find_Base_Type;
18026
18027 -- Start of processing for Floating_Point_Type_Declaration
18028
18029 begin
18030 Check_Restriction (No_Floating_Point, Def);
18031
18032 -- Create an implicit base type
18033
18034 Implicit_Base :=
18035 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18036
18037 -- Analyze and verify digits value
18038
18039 Analyze_And_Resolve (Digs, Any_Integer);
18040 Check_Digits_Expression (Digs);
18041 Digs_Val := Expr_Value (Digs);
18042
18043 -- Process possible range spec and find correct type to derive from
18044
18045 Process_Real_Range_Specification (Def);
18046
18047 -- Check that requested number of digits is not too high.
18048
18049 if Digs_Val > Max_Digs_Val then
18050
18051 -- The check for Max_Base_Digits may be somewhat expensive, as it
18052 -- requires reading System, so only do it when necessary.
18053
18054 declare
18055 Max_Base_Digits : constant Uint :=
18056 Expr_Value
18057 (Expression
18058 (Parent (RTE (RE_Max_Base_Digits))));
18059
18060 begin
18061 if Digs_Val > Max_Base_Digits then
18062 Error_Msg_Uint_1 := Max_Base_Digits;
18063 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18064
18065 elsif No (Real_Range_Specification (Def)) then
18066 Error_Msg_Uint_1 := Max_Digs_Val;
18067 Error_Msg_N ("types with more than ^ digits need range spec "
18068 & "(RM 3.5.7(6))", Digs);
18069 end if;
18070 end;
18071 end if;
18072
18073 -- Find a suitable type to derive from or complain and use a substitute
18074
18075 Base_Typ := Find_Base_Type;
18076
18077 -- If there are bounds given in the declaration use them as the bounds
18078 -- of the type, otherwise use the bounds of the predefined base type
18079 -- that was chosen based on the Digits value.
18080
18081 if Present (Real_Range_Specification (Def)) then
18082 Set_Scalar_Range (T, Real_Range_Specification (Def));
18083 Set_Is_Constrained (T);
18084
18085 Convert_Bound (Type_Low_Bound (T));
18086 Convert_Bound (Type_High_Bound (T));
18087
18088 else
18089 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18090 end if;
18091
18092 -- Complete definition of implicit base and declared first subtype. The
18093 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18094 -- are not clobbered when the floating point type acts as a full view of
18095 -- a private type.
18096
18097 Set_Etype (Implicit_Base, Base_Typ);
18098 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18099 Set_Size_Info (Implicit_Base, Base_Typ);
18100 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18101 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18102 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18103 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18104
18105 Set_Ekind (T, E_Floating_Point_Subtype);
18106 Set_Etype (T, Implicit_Base);
18107 Set_Size_Info (T, Implicit_Base);
18108 Set_RM_Size (T, RM_Size (Implicit_Base));
18109 Inherit_Rep_Item_Chain (T, Implicit_Base);
18110 Set_Digits_Value (T, Digs_Val);
18111 end Floating_Point_Type_Declaration;
18112
18113 ----------------------------
18114 -- Get_Discriminant_Value --
18115 ----------------------------
18116
18117 -- This is the situation:
18118
18119 -- There is a non-derived type
18120
18121 -- type T0 (Dx, Dy, Dz...)
18122
18123 -- There are zero or more levels of derivation, with each derivation
18124 -- either purely inheriting the discriminants, or defining its own.
18125
18126 -- type Ti is new Ti-1
18127 -- or
18128 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18129 -- or
18130 -- subtype Ti is ...
18131
18132 -- The subtype issue is avoided by the use of Original_Record_Component,
18133 -- and the fact that derived subtypes also derive the constraints.
18134
18135 -- This chain leads back from
18136
18137 -- Typ_For_Constraint
18138
18139 -- Typ_For_Constraint has discriminants, and the value for each
18140 -- discriminant is given by its corresponding Elmt of Constraints.
18141
18142 -- Discriminant is some discriminant in this hierarchy
18143
18144 -- We need to return its value
18145
18146 -- We do this by recursively searching each level, and looking for
18147 -- Discriminant. Once we get to the bottom, we start backing up
18148 -- returning the value for it which may in turn be a discriminant
18149 -- further up, so on the backup we continue the substitution.
18150
18151 function Get_Discriminant_Value
18152 (Discriminant : Entity_Id;
18153 Typ_For_Constraint : Entity_Id;
18154 Constraint : Elist_Id) return Node_Id
18155 is
18156 function Root_Corresponding_Discriminant
18157 (Discr : Entity_Id) return Entity_Id;
18158 -- Given a discriminant, traverse the chain of inherited discriminants
18159 -- and return the topmost discriminant.
18160
18161 function Search_Derivation_Levels
18162 (Ti : Entity_Id;
18163 Discrim_Values : Elist_Id;
18164 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18165 -- This is the routine that performs the recursive search of levels
18166 -- as described above.
18167
18168 -------------------------------------
18169 -- Root_Corresponding_Discriminant --
18170 -------------------------------------
18171
18172 function Root_Corresponding_Discriminant
18173 (Discr : Entity_Id) return Entity_Id
18174 is
18175 D : Entity_Id;
18176
18177 begin
18178 D := Discr;
18179 while Present (Corresponding_Discriminant (D)) loop
18180 D := Corresponding_Discriminant (D);
18181 end loop;
18182
18183 return D;
18184 end Root_Corresponding_Discriminant;
18185
18186 ------------------------------
18187 -- Search_Derivation_Levels --
18188 ------------------------------
18189
18190 function Search_Derivation_Levels
18191 (Ti : Entity_Id;
18192 Discrim_Values : Elist_Id;
18193 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18194 is
18195 Assoc : Elmt_Id;
18196 Disc : Entity_Id;
18197 Result : Node_Or_Entity_Id;
18198 Result_Entity : Node_Id;
18199
18200 begin
18201 -- If inappropriate type, return Error, this happens only in
18202 -- cascaded error situations, and we want to avoid a blow up.
18203
18204 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18205 return Error;
18206 end if;
18207
18208 -- Look deeper if possible. Use Stored_Constraints only for
18209 -- untagged types. For tagged types use the given constraint.
18210 -- This asymmetry needs explanation???
18211
18212 if not Stored_Discrim_Values
18213 and then Present (Stored_Constraint (Ti))
18214 and then not Is_Tagged_Type (Ti)
18215 then
18216 Result :=
18217 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18218
18219 else
18220 declare
18221 Td : Entity_Id := Etype (Ti);
18222
18223 begin
18224 -- If the parent type is private, the full view may include
18225 -- renamed discriminants, and it is those stored values that
18226 -- may be needed (the partial view never has more information
18227 -- than the full view).
18228
18229 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18230 Td := Full_View (Td);
18231 end if;
18232
18233 if Td = Ti then
18234 Result := Discriminant;
18235
18236 else
18237 if Present (Stored_Constraint (Ti)) then
18238 Result :=
18239 Search_Derivation_Levels
18240 (Td, Stored_Constraint (Ti), True);
18241 else
18242 Result :=
18243 Search_Derivation_Levels
18244 (Td, Discrim_Values, Stored_Discrim_Values);
18245 end if;
18246 end if;
18247 end;
18248 end if;
18249
18250 -- Extra underlying places to search, if not found above. For
18251 -- concurrent types, the relevant discriminant appears in the
18252 -- corresponding record. For a type derived from a private type
18253 -- without discriminant, the full view inherits the discriminants
18254 -- of the full view of the parent.
18255
18256 if Result = Discriminant then
18257 if Is_Concurrent_Type (Ti)
18258 and then Present (Corresponding_Record_Type (Ti))
18259 then
18260 Result :=
18261 Search_Derivation_Levels (
18262 Corresponding_Record_Type (Ti),
18263 Discrim_Values,
18264 Stored_Discrim_Values);
18265
18266 elsif Is_Private_Type (Ti)
18267 and then not Has_Discriminants (Ti)
18268 and then Present (Full_View (Ti))
18269 and then Etype (Full_View (Ti)) /= Ti
18270 then
18271 Result :=
18272 Search_Derivation_Levels (
18273 Full_View (Ti),
18274 Discrim_Values,
18275 Stored_Discrim_Values);
18276 end if;
18277 end if;
18278
18279 -- If Result is not a (reference to a) discriminant, return it,
18280 -- otherwise set Result_Entity to the discriminant.
18281
18282 if Nkind (Result) = N_Defining_Identifier then
18283 pragma Assert (Result = Discriminant);
18284 Result_Entity := Result;
18285
18286 else
18287 if not Denotes_Discriminant (Result) then
18288 return Result;
18289 end if;
18290
18291 Result_Entity := Entity (Result);
18292 end if;
18293
18294 -- See if this level of derivation actually has discriminants because
18295 -- tagged derivations can add them, hence the lower levels need not
18296 -- have any.
18297
18298 if not Has_Discriminants (Ti) then
18299 return Result;
18300 end if;
18301
18302 -- Scan Ti's discriminants for Result_Entity, and return its
18303 -- corresponding value, if any.
18304
18305 Result_Entity := Original_Record_Component (Result_Entity);
18306
18307 Assoc := First_Elmt (Discrim_Values);
18308
18309 if Stored_Discrim_Values then
18310 Disc := First_Stored_Discriminant (Ti);
18311 else
18312 Disc := First_Discriminant (Ti);
18313 end if;
18314
18315 while Present (Disc) loop
18316
18317 -- If no further associations return the discriminant, value will
18318 -- be found on the second pass.
18319
18320 if No (Assoc) then
18321 return Result;
18322 end if;
18323
18324 if Original_Record_Component (Disc) = Result_Entity then
18325 return Node (Assoc);
18326 end if;
18327
18328 Next_Elmt (Assoc);
18329
18330 if Stored_Discrim_Values then
18331 Next_Stored_Discriminant (Disc);
18332 else
18333 Next_Discriminant (Disc);
18334 end if;
18335 end loop;
18336
18337 -- Could not find it
18338
18339 return Result;
18340 end Search_Derivation_Levels;
18341
18342 -- Local Variables
18343
18344 Result : Node_Or_Entity_Id;
18345
18346 -- Start of processing for Get_Discriminant_Value
18347
18348 begin
18349 -- ??? This routine is a gigantic mess and will be deleted. For the
18350 -- time being just test for the trivial case before calling recurse.
18351
18352 -- We are now celebrating the 20th anniversary of this comment!
18353
18354 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18355 declare
18356 D : Entity_Id;
18357 E : Elmt_Id;
18358
18359 begin
18360 D := First_Discriminant (Typ_For_Constraint);
18361 E := First_Elmt (Constraint);
18362 while Present (D) loop
18363 if Chars (D) = Chars (Discriminant) then
18364 return Node (E);
18365 end if;
18366
18367 Next_Discriminant (D);
18368 Next_Elmt (E);
18369 end loop;
18370 end;
18371 end if;
18372
18373 Result := Search_Derivation_Levels
18374 (Typ_For_Constraint, Constraint, False);
18375
18376 -- ??? hack to disappear when this routine is gone
18377
18378 if Nkind (Result) = N_Defining_Identifier then
18379 declare
18380 D : Entity_Id;
18381 E : Elmt_Id;
18382
18383 begin
18384 D := First_Discriminant (Typ_For_Constraint);
18385 E := First_Elmt (Constraint);
18386 while Present (D) loop
18387 if Root_Corresponding_Discriminant (D) = Discriminant then
18388 return Node (E);
18389 end if;
18390
18391 Next_Discriminant (D);
18392 Next_Elmt (E);
18393 end loop;
18394 end;
18395 end if;
18396
18397 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18398 return Result;
18399 end Get_Discriminant_Value;
18400
18401 --------------------------
18402 -- Has_Range_Constraint --
18403 --------------------------
18404
18405 function Has_Range_Constraint (N : Node_Id) return Boolean is
18406 C : constant Node_Id := Constraint (N);
18407
18408 begin
18409 if Nkind (C) = N_Range_Constraint then
18410 return True;
18411
18412 elsif Nkind (C) = N_Digits_Constraint then
18413 return
18414 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18415 or else Present (Range_Constraint (C));
18416
18417 elsif Nkind (C) = N_Delta_Constraint then
18418 return Present (Range_Constraint (C));
18419
18420 else
18421 return False;
18422 end if;
18423 end Has_Range_Constraint;
18424
18425 ------------------------
18426 -- Inherit_Components --
18427 ------------------------
18428
18429 function Inherit_Components
18430 (N : Node_Id;
18431 Parent_Base : Entity_Id;
18432 Derived_Base : Entity_Id;
18433 Is_Tagged : Boolean;
18434 Inherit_Discr : Boolean;
18435 Discs : Elist_Id) return Elist_Id
18436 is
18437 Assoc_List : constant Elist_Id := New_Elmt_List;
18438
18439 procedure Inherit_Component
18440 (Old_C : Entity_Id;
18441 Plain_Discrim : Boolean := False;
18442 Stored_Discrim : Boolean := False);
18443 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18444 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18445 -- True, Old_C is a stored discriminant. If they are both false then
18446 -- Old_C is a regular component.
18447
18448 -----------------------
18449 -- Inherit_Component --
18450 -----------------------
18451
18452 procedure Inherit_Component
18453 (Old_C : Entity_Id;
18454 Plain_Discrim : Boolean := False;
18455 Stored_Discrim : Boolean := False)
18456 is
18457 procedure Set_Anonymous_Type (Id : Entity_Id);
18458 -- Id denotes the entity of an access discriminant or anonymous
18459 -- access component. Set the type of Id to either the same type of
18460 -- Old_C or create a new one depending on whether the parent and
18461 -- the child types are in the same scope.
18462
18463 ------------------------
18464 -- Set_Anonymous_Type --
18465 ------------------------
18466
18467 procedure Set_Anonymous_Type (Id : Entity_Id) is
18468 Old_Typ : constant Entity_Id := Etype (Old_C);
18469
18470 begin
18471 if Scope (Parent_Base) = Scope (Derived_Base) then
18472 Set_Etype (Id, Old_Typ);
18473
18474 -- The parent and the derived type are in two different scopes.
18475 -- Reuse the type of the original discriminant / component by
18476 -- copying it in order to preserve all attributes.
18477
18478 else
18479 declare
18480 Typ : constant Entity_Id := New_Copy (Old_Typ);
18481
18482 begin
18483 Set_Etype (Id, Typ);
18484
18485 -- Since we do not generate component declarations for
18486 -- inherited components, associate the itype with the
18487 -- derived type.
18488
18489 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18490 Set_Scope (Typ, Derived_Base);
18491 end;
18492 end if;
18493 end Set_Anonymous_Type;
18494
18495 -- Local variables and constants
18496
18497 New_C : constant Entity_Id := New_Copy (Old_C);
18498
18499 Corr_Discrim : Entity_Id;
18500 Discrim : Entity_Id;
18501
18502 -- Start of processing for Inherit_Component
18503
18504 begin
18505 pragma Assert (not Is_Tagged or not Stored_Discrim);
18506
18507 Set_Parent (New_C, Parent (Old_C));
18508
18509 -- Regular discriminants and components must be inserted in the scope
18510 -- of the Derived_Base. Do it here.
18511
18512 if not Stored_Discrim then
18513 Enter_Name (New_C);
18514 end if;
18515
18516 -- For tagged types the Original_Record_Component must point to
18517 -- whatever this field was pointing to in the parent type. This has
18518 -- already been achieved by the call to New_Copy above.
18519
18520 if not Is_Tagged then
18521 Set_Original_Record_Component (New_C, New_C);
18522 Set_Corresponding_Record_Component (New_C, Old_C);
18523 end if;
18524
18525 -- Set the proper type of an access discriminant
18526
18527 if Ekind (New_C) = E_Discriminant
18528 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18529 then
18530 Set_Anonymous_Type (New_C);
18531 end if;
18532
18533 -- If we have inherited a component then see if its Etype contains
18534 -- references to Parent_Base discriminants. In this case, replace
18535 -- these references with the constraints given in Discs. We do not
18536 -- do this for the partial view of private types because this is
18537 -- not needed (only the components of the full view will be used
18538 -- for code generation) and cause problem. We also avoid this
18539 -- transformation in some error situations.
18540
18541 if Ekind (New_C) = E_Component then
18542
18543 -- Set the proper type of an anonymous access component
18544
18545 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18546 Set_Anonymous_Type (New_C);
18547
18548 elsif (Is_Private_Type (Derived_Base)
18549 and then not Is_Generic_Type (Derived_Base))
18550 or else (Is_Empty_Elmt_List (Discs)
18551 and then not Expander_Active)
18552 then
18553 Set_Etype (New_C, Etype (Old_C));
18554
18555 else
18556 -- The current component introduces a circularity of the
18557 -- following kind:
18558
18559 -- limited with Pack_2;
18560 -- package Pack_1 is
18561 -- type T_1 is tagged record
18562 -- Comp : access Pack_2.T_2;
18563 -- ...
18564 -- end record;
18565 -- end Pack_1;
18566
18567 -- with Pack_1;
18568 -- package Pack_2 is
18569 -- type T_2 is new Pack_1.T_1 with ...;
18570 -- end Pack_2;
18571
18572 Set_Etype
18573 (New_C,
18574 Constrain_Component_Type
18575 (Old_C, Derived_Base, N, Parent_Base, Discs));
18576 end if;
18577 end if;
18578
18579 -- In derived tagged types it is illegal to reference a non
18580 -- discriminant component in the parent type. To catch this, mark
18581 -- these components with an Ekind of E_Void. This will be reset in
18582 -- Record_Type_Definition after processing the record extension of
18583 -- the derived type.
18584
18585 -- If the declaration is a private extension, there is no further
18586 -- record extension to process, and the components retain their
18587 -- current kind, because they are visible at this point.
18588
18589 if Is_Tagged and then Ekind (New_C) = E_Component
18590 and then Nkind (N) /= N_Private_Extension_Declaration
18591 then
18592 Set_Ekind (New_C, E_Void);
18593 end if;
18594
18595 if Plain_Discrim then
18596 Set_Corresponding_Discriminant (New_C, Old_C);
18597 Build_Discriminal (New_C);
18598
18599 -- If we are explicitly inheriting a stored discriminant it will be
18600 -- completely hidden.
18601
18602 elsif Stored_Discrim then
18603 Set_Corresponding_Discriminant (New_C, Empty);
18604 Set_Discriminal (New_C, Empty);
18605 Set_Is_Completely_Hidden (New_C);
18606
18607 -- Set the Original_Record_Component of each discriminant in the
18608 -- derived base to point to the corresponding stored that we just
18609 -- created.
18610
18611 Discrim := First_Discriminant (Derived_Base);
18612 while Present (Discrim) loop
18613 Corr_Discrim := Corresponding_Discriminant (Discrim);
18614
18615 -- Corr_Discrim could be missing in an error situation
18616
18617 if Present (Corr_Discrim)
18618 and then Original_Record_Component (Corr_Discrim) = Old_C
18619 then
18620 Set_Original_Record_Component (Discrim, New_C);
18621 Set_Corresponding_Record_Component (Discrim, Empty);
18622 end if;
18623
18624 Next_Discriminant (Discrim);
18625 end loop;
18626
18627 Append_Entity (New_C, Derived_Base);
18628 end if;
18629
18630 if not Is_Tagged then
18631 Append_Elmt (Old_C, Assoc_List);
18632 Append_Elmt (New_C, Assoc_List);
18633 end if;
18634 end Inherit_Component;
18635
18636 -- Variables local to Inherit_Component
18637
18638 Loc : constant Source_Ptr := Sloc (N);
18639
18640 Parent_Discrim : Entity_Id;
18641 Stored_Discrim : Entity_Id;
18642 D : Entity_Id;
18643 Component : Entity_Id;
18644
18645 -- Start of processing for Inherit_Components
18646
18647 begin
18648 if not Is_Tagged then
18649 Append_Elmt (Parent_Base, Assoc_List);
18650 Append_Elmt (Derived_Base, Assoc_List);
18651 end if;
18652
18653 -- Inherit parent discriminants if needed
18654
18655 if Inherit_Discr then
18656 Parent_Discrim := First_Discriminant (Parent_Base);
18657 while Present (Parent_Discrim) loop
18658 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18659 Next_Discriminant (Parent_Discrim);
18660 end loop;
18661 end if;
18662
18663 -- Create explicit stored discrims for untagged types when necessary
18664
18665 if not Has_Unknown_Discriminants (Derived_Base)
18666 and then Has_Discriminants (Parent_Base)
18667 and then not Is_Tagged
18668 and then
18669 (not Inherit_Discr
18670 or else First_Discriminant (Parent_Base) /=
18671 First_Stored_Discriminant (Parent_Base))
18672 then
18673 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18674 while Present (Stored_Discrim) loop
18675 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18676 Next_Stored_Discriminant (Stored_Discrim);
18677 end loop;
18678 end if;
18679
18680 -- See if we can apply the second transformation for derived types, as
18681 -- explained in point 6. in the comments above Build_Derived_Record_Type
18682 -- This is achieved by appending Derived_Base discriminants into Discs,
18683 -- which has the side effect of returning a non empty Discs list to the
18684 -- caller of Inherit_Components, which is what we want. This must be
18685 -- done for private derived types if there are explicit stored
18686 -- discriminants, to ensure that we can retrieve the values of the
18687 -- constraints provided in the ancestors.
18688
18689 if Inherit_Discr
18690 and then Is_Empty_Elmt_List (Discs)
18691 and then Present (First_Discriminant (Derived_Base))
18692 and then
18693 (not Is_Private_Type (Derived_Base)
18694 or else Is_Completely_Hidden
18695 (First_Stored_Discriminant (Derived_Base))
18696 or else Is_Generic_Type (Derived_Base))
18697 then
18698 D := First_Discriminant (Derived_Base);
18699 while Present (D) loop
18700 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18701 Next_Discriminant (D);
18702 end loop;
18703 end if;
18704
18705 -- Finally, inherit non-discriminant components unless they are not
18706 -- visible because defined or inherited from the full view of the
18707 -- parent. Don't inherit the _parent field of the parent type.
18708
18709 Component := First_Entity (Parent_Base);
18710 while Present (Component) loop
18711
18712 -- Ada 2005 (AI-251): Do not inherit components associated with
18713 -- secondary tags of the parent.
18714
18715 if Ekind (Component) = E_Component
18716 and then Present (Related_Type (Component))
18717 then
18718 null;
18719
18720 elsif Ekind (Component) /= E_Component
18721 or else Chars (Component) = Name_uParent
18722 then
18723 null;
18724
18725 -- If the derived type is within the parent type's declarative
18726 -- region, then the components can still be inherited even though
18727 -- they aren't visible at this point. This can occur for cases
18728 -- such as within public child units where the components must
18729 -- become visible upon entering the child unit's private part.
18730
18731 elsif not Is_Visible_Component (Component)
18732 and then not In_Open_Scopes (Scope (Parent_Base))
18733 then
18734 null;
18735
18736 elsif Ekind_In (Derived_Base, E_Private_Type,
18737 E_Limited_Private_Type)
18738 then
18739 null;
18740
18741 else
18742 Inherit_Component (Component);
18743 end if;
18744
18745 Next_Entity (Component);
18746 end loop;
18747
18748 -- For tagged derived types, inherited discriminants cannot be used in
18749 -- component declarations of the record extension part. To achieve this
18750 -- we mark the inherited discriminants as not visible.
18751
18752 if Is_Tagged and then Inherit_Discr then
18753 D := First_Discriminant (Derived_Base);
18754 while Present (D) loop
18755 Set_Is_Immediately_Visible (D, False);
18756 Next_Discriminant (D);
18757 end loop;
18758 end if;
18759
18760 return Assoc_List;
18761 end Inherit_Components;
18762
18763 -----------------------------
18764 -- Inherit_Predicate_Flags --
18765 -----------------------------
18766
18767 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18768 begin
18769 if Present (Predicate_Function (Subt)) then
18770 return;
18771 end if;
18772
18773 Set_Has_Predicates (Subt, Has_Predicates (Par));
18774 Set_Has_Static_Predicate_Aspect
18775 (Subt, Has_Static_Predicate_Aspect (Par));
18776 Set_Has_Dynamic_Predicate_Aspect
18777 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18778
18779 -- A named subtype does not inherit the predicate function of its
18780 -- parent but an itype declared for a loop index needs the discrete
18781 -- predicate information of its parent to execute the loop properly.
18782 -- A non-discrete type may has a static predicate (for example True)
18783 -- but has no static_discrete_predicate.
18784
18785 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18786 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18787
18788 if Has_Static_Predicate (Par) and then Is_Discrete_Type (Par) then
18789 Set_Static_Discrete_Predicate
18790 (Subt, Static_Discrete_Predicate (Par));
18791 end if;
18792 end if;
18793 end Inherit_Predicate_Flags;
18794
18795 ----------------------
18796 -- Is_EVF_Procedure --
18797 ----------------------
18798
18799 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18800 Formal : Entity_Id;
18801
18802 begin
18803 -- Examine the formals of an Extensions_Visible False procedure looking
18804 -- for a controlling OUT parameter.
18805
18806 if Ekind (Subp) = E_Procedure
18807 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18808 then
18809 Formal := First_Formal (Subp);
18810 while Present (Formal) loop
18811 if Ekind (Formal) = E_Out_Parameter
18812 and then Is_Controlling_Formal (Formal)
18813 then
18814 return True;
18815 end if;
18816
18817 Next_Formal (Formal);
18818 end loop;
18819 end if;
18820
18821 return False;
18822 end Is_EVF_Procedure;
18823
18824 -----------------------
18825 -- Is_Null_Extension --
18826 -----------------------
18827
18828 function Is_Null_Extension (T : Entity_Id) return Boolean is
18829 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18830 Comp_List : Node_Id;
18831 Comp : Node_Id;
18832
18833 begin
18834 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18835 or else not Is_Tagged_Type (T)
18836 or else Nkind (Type_Definition (Type_Decl)) /=
18837 N_Derived_Type_Definition
18838 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18839 then
18840 return False;
18841 end if;
18842
18843 Comp_List :=
18844 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18845
18846 if Present (Discriminant_Specifications (Type_Decl)) then
18847 return False;
18848
18849 elsif Present (Comp_List)
18850 and then Is_Non_Empty_List (Component_Items (Comp_List))
18851 then
18852 Comp := First (Component_Items (Comp_List));
18853
18854 -- Only user-defined components are relevant. The component list
18855 -- may also contain a parent component and internal components
18856 -- corresponding to secondary tags, but these do not determine
18857 -- whether this is a null extension.
18858
18859 while Present (Comp) loop
18860 if Comes_From_Source (Comp) then
18861 return False;
18862 end if;
18863
18864 Next (Comp);
18865 end loop;
18866
18867 return True;
18868
18869 else
18870 return True;
18871 end if;
18872 end Is_Null_Extension;
18873
18874 ------------------------------
18875 -- Is_Valid_Constraint_Kind --
18876 ------------------------------
18877
18878 function Is_Valid_Constraint_Kind
18879 (T_Kind : Type_Kind;
18880 Constraint_Kind : Node_Kind) return Boolean
18881 is
18882 begin
18883 case T_Kind is
18884 when Enumeration_Kind
18885 | Integer_Kind
18886 =>
18887 return Constraint_Kind = N_Range_Constraint;
18888
18889 when Decimal_Fixed_Point_Kind =>
18890 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18891 N_Range_Constraint);
18892
18893 when Ordinary_Fixed_Point_Kind =>
18894 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18895 N_Range_Constraint);
18896
18897 when Float_Kind =>
18898 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18899 N_Range_Constraint);
18900
18901 when Access_Kind
18902 | Array_Kind
18903 | Class_Wide_Kind
18904 | Concurrent_Kind
18905 | Private_Kind
18906 | E_Incomplete_Type
18907 | E_Record_Subtype
18908 | E_Record_Type
18909 =>
18910 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18911
18912 when others =>
18913 return True; -- Error will be detected later
18914 end case;
18915 end Is_Valid_Constraint_Kind;
18916
18917 --------------------------
18918 -- Is_Visible_Component --
18919 --------------------------
18920
18921 function Is_Visible_Component
18922 (C : Entity_Id;
18923 N : Node_Id := Empty) return Boolean
18924 is
18925 Original_Comp : Entity_Id := Empty;
18926 Original_Type : Entity_Id;
18927 Type_Scope : Entity_Id;
18928
18929 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18930 -- Check whether parent type of inherited component is declared locally,
18931 -- possibly within a nested package or instance. The current scope is
18932 -- the derived record itself.
18933
18934 -------------------
18935 -- Is_Local_Type --
18936 -------------------
18937
18938 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18939 Scop : Entity_Id;
18940
18941 begin
18942 Scop := Scope (Typ);
18943 while Present (Scop)
18944 and then Scop /= Standard_Standard
18945 loop
18946 if Scop = Scope (Current_Scope) then
18947 return True;
18948 end if;
18949
18950 Scop := Scope (Scop);
18951 end loop;
18952
18953 return False;
18954 end Is_Local_Type;
18955
18956 -- Start of processing for Is_Visible_Component
18957
18958 begin
18959 if Ekind_In (C, E_Component, E_Discriminant) then
18960 Original_Comp := Original_Record_Component (C);
18961 end if;
18962
18963 if No (Original_Comp) then
18964
18965 -- Premature usage, or previous error
18966
18967 return False;
18968
18969 else
18970 Original_Type := Scope (Original_Comp);
18971 Type_Scope := Scope (Base_Type (Scope (C)));
18972 end if;
18973
18974 -- This test only concerns tagged types
18975
18976 if not Is_Tagged_Type (Original_Type) then
18977
18978 -- Check if this is a renamed discriminant (hidden either by the
18979 -- derived type or by some ancestor), unless we are analyzing code
18980 -- generated by the expander since it may reference such components
18981 -- (for example see the expansion of Deep_Adjust).
18982
18983 if Ekind (C) = E_Discriminant and then Present (N) then
18984 return
18985 not Comes_From_Source (N)
18986 or else not Is_Completely_Hidden (C);
18987 else
18988 return True;
18989 end if;
18990
18991 -- If it is _Parent or _Tag, there is no visibility issue
18992
18993 elsif not Comes_From_Source (Original_Comp) then
18994 return True;
18995
18996 -- Discriminants are visible unless the (private) type has unknown
18997 -- discriminants. If the discriminant reference is inserted for a
18998 -- discriminant check on a full view it is also visible.
18999
19000 elsif Ekind (Original_Comp) = E_Discriminant
19001 and then
19002 (not Has_Unknown_Discriminants (Original_Type)
19003 or else (Present (N)
19004 and then Nkind (N) = N_Selected_Component
19005 and then Nkind (Prefix (N)) = N_Type_Conversion
19006 and then not Comes_From_Source (Prefix (N))))
19007 then
19008 return True;
19009
19010 -- In the body of an instantiation, check the visibility of a component
19011 -- in case it has a homograph that is a primitive operation of a private
19012 -- type which was not visible in the generic unit.
19013
19014 -- Should Is_Prefixed_Call be propagated from template to instance???
19015
19016 elsif In_Instance_Body then
19017 if not Is_Tagged_Type (Original_Type)
19018 or else not Is_Private_Type (Original_Type)
19019 then
19020 return True;
19021
19022 else
19023 declare
19024 Subp_Elmt : Elmt_Id;
19025
19026 begin
19027 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
19028 while Present (Subp_Elmt) loop
19029
19030 -- The component is hidden by a primitive operation
19031
19032 if Chars (Node (Subp_Elmt)) = Chars (C) then
19033 return False;
19034 end if;
19035
19036 Next_Elmt (Subp_Elmt);
19037 end loop;
19038
19039 return True;
19040 end;
19041 end if;
19042
19043 -- If the component has been declared in an ancestor which is currently
19044 -- a private type, then it is not visible. The same applies if the
19045 -- component's containing type is not in an open scope and the original
19046 -- component's enclosing type is a visible full view of a private type
19047 -- (which can occur in cases where an attempt is being made to reference
19048 -- a component in a sibling package that is inherited from a visible
19049 -- component of a type in an ancestor package; the component in the
19050 -- sibling package should not be visible even though the component it
19051 -- inherited from is visible). This does not apply however in the case
19052 -- where the scope of the type is a private child unit, or when the
19053 -- parent comes from a local package in which the ancestor is currently
19054 -- visible. The latter suppression of visibility is needed for cases
19055 -- that are tested in B730006.
19056
19057 elsif Is_Private_Type (Original_Type)
19058 or else
19059 (not Is_Private_Descendant (Type_Scope)
19060 and then not In_Open_Scopes (Type_Scope)
19061 and then Has_Private_Declaration (Original_Type))
19062 then
19063 -- If the type derives from an entity in a formal package, there
19064 -- are no additional visible components.
19065
19066 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19067 N_Formal_Package_Declaration
19068 then
19069 return False;
19070
19071 -- if we are not in the private part of the current package, there
19072 -- are no additional visible components.
19073
19074 elsif Ekind (Scope (Current_Scope)) = E_Package
19075 and then not In_Private_Part (Scope (Current_Scope))
19076 then
19077 return False;
19078 else
19079 return
19080 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19081 and then In_Open_Scopes (Scope (Original_Type))
19082 and then Is_Local_Type (Type_Scope);
19083 end if;
19084
19085 -- There is another weird way in which a component may be invisible when
19086 -- the private and the full view are not derived from the same ancestor.
19087 -- Here is an example :
19088
19089 -- type A1 is tagged record F1 : integer; end record;
19090 -- type A2 is new A1 with record F2 : integer; end record;
19091 -- type T is new A1 with private;
19092 -- private
19093 -- type T is new A2 with null record;
19094
19095 -- In this case, the full view of T inherits F1 and F2 but the private
19096 -- view inherits only F1
19097
19098 else
19099 declare
19100 Ancestor : Entity_Id := Scope (C);
19101
19102 begin
19103 loop
19104 if Ancestor = Original_Type then
19105 return True;
19106
19107 -- The ancestor may have a partial view of the original type,
19108 -- but if the full view is in scope, as in a child body, the
19109 -- component is visible.
19110
19111 elsif In_Private_Part (Scope (Original_Type))
19112 and then Full_View (Ancestor) = Original_Type
19113 then
19114 return True;
19115
19116 elsif Ancestor = Etype (Ancestor) then
19117
19118 -- No further ancestors to examine
19119
19120 return False;
19121 end if;
19122
19123 Ancestor := Etype (Ancestor);
19124 end loop;
19125 end;
19126 end if;
19127 end Is_Visible_Component;
19128
19129 --------------------------
19130 -- Make_Class_Wide_Type --
19131 --------------------------
19132
19133 procedure Make_Class_Wide_Type (T : Entity_Id) is
19134 CW_Type : Entity_Id;
19135 CW_Name : Name_Id;
19136 Next_E : Entity_Id;
19137 Prev_E : Entity_Id;
19138
19139 begin
19140 if Present (Class_Wide_Type (T)) then
19141
19142 -- The class-wide type is a partially decorated entity created for a
19143 -- unanalyzed tagged type referenced through a limited with clause.
19144 -- When the tagged type is analyzed, its class-wide type needs to be
19145 -- redecorated. Note that we reuse the entity created by Decorate_
19146 -- Tagged_Type in order to preserve all links.
19147
19148 if Materialize_Entity (Class_Wide_Type (T)) then
19149 CW_Type := Class_Wide_Type (T);
19150 Set_Materialize_Entity (CW_Type, False);
19151
19152 -- The class wide type can have been defined by the partial view, in
19153 -- which case everything is already done.
19154
19155 else
19156 return;
19157 end if;
19158
19159 -- Default case, we need to create a new class-wide type
19160
19161 else
19162 CW_Type :=
19163 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19164 end if;
19165
19166 -- Inherit root type characteristics
19167
19168 CW_Name := Chars (CW_Type);
19169 Next_E := Next_Entity (CW_Type);
19170 Prev_E := Prev_Entity (CW_Type);
19171 Copy_Node (T, CW_Type);
19172 Set_Comes_From_Source (CW_Type, False);
19173 Set_Chars (CW_Type, CW_Name);
19174 Set_Parent (CW_Type, Parent (T));
19175 Set_Prev_Entity (CW_Type, Prev_E);
19176 Set_Next_Entity (CW_Type, Next_E);
19177
19178 -- Ensure we have a new freeze node for the class-wide type. The partial
19179 -- view may have freeze action of its own, requiring a proper freeze
19180 -- node, and the same freeze node cannot be shared between the two
19181 -- types.
19182
19183 Set_Has_Delayed_Freeze (CW_Type);
19184 Set_Freeze_Node (CW_Type, Empty);
19185
19186 -- Customize the class-wide type: It has no prim. op., it cannot be
19187 -- abstract, its Etype points back to the specific root type, and it
19188 -- cannot have any invariants.
19189
19190 Set_Ekind (CW_Type, E_Class_Wide_Type);
19191 Set_Is_Tagged_Type (CW_Type, True);
19192 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19193 Set_Is_Abstract_Type (CW_Type, False);
19194 Set_Is_Constrained (CW_Type, False);
19195 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19196 Set_Default_SSO (CW_Type);
19197 Set_Has_Inheritable_Invariants (CW_Type, False);
19198 Set_Has_Inherited_Invariants (CW_Type, False);
19199 Set_Has_Own_Invariants (CW_Type, False);
19200
19201 if Ekind (T) = E_Class_Wide_Subtype then
19202 Set_Etype (CW_Type, Etype (Base_Type (T)));
19203 else
19204 Set_Etype (CW_Type, T);
19205 end if;
19206
19207 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19208
19209 -- If this is the class_wide type of a constrained subtype, it does
19210 -- not have discriminants.
19211
19212 Set_Has_Discriminants (CW_Type,
19213 Has_Discriminants (T) and then not Is_Constrained (T));
19214
19215 Set_Has_Unknown_Discriminants (CW_Type, True);
19216 Set_Class_Wide_Type (T, CW_Type);
19217 Set_Equivalent_Type (CW_Type, Empty);
19218
19219 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19220
19221 Set_Class_Wide_Type (CW_Type, CW_Type);
19222 end Make_Class_Wide_Type;
19223
19224 ----------------
19225 -- Make_Index --
19226 ----------------
19227
19228 procedure Make_Index
19229 (N : Node_Id;
19230 Related_Nod : Node_Id;
19231 Related_Id : Entity_Id := Empty;
19232 Suffix_Index : Nat := 1;
19233 In_Iter_Schm : Boolean := False)
19234 is
19235 R : Node_Id;
19236 T : Entity_Id;
19237 Def_Id : Entity_Id := Empty;
19238 Found : Boolean := False;
19239
19240 begin
19241 -- For a discrete range used in a constrained array definition and
19242 -- defined by a range, an implicit conversion to the predefined type
19243 -- INTEGER is assumed if each bound is either a numeric literal, a named
19244 -- number, or an attribute, and the type of both bounds (prior to the
19245 -- implicit conversion) is the type universal_integer. Otherwise, both
19246 -- bounds must be of the same discrete type, other than universal
19247 -- integer; this type must be determinable independently of the
19248 -- context, but using the fact that the type must be discrete and that
19249 -- both bounds must have the same type.
19250
19251 -- Character literals also have a universal type in the absence of
19252 -- of additional context, and are resolved to Standard_Character.
19253
19254 if Nkind (N) = N_Range then
19255
19256 -- The index is given by a range constraint. The bounds are known
19257 -- to be of a consistent type.
19258
19259 if not Is_Overloaded (N) then
19260 T := Etype (N);
19261
19262 -- For universal bounds, choose the specific predefined type
19263
19264 if T = Universal_Integer then
19265 T := Standard_Integer;
19266
19267 elsif T = Any_Character then
19268 Ambiguous_Character (Low_Bound (N));
19269
19270 T := Standard_Character;
19271 end if;
19272
19273 -- The node may be overloaded because some user-defined operators
19274 -- are available, but if a universal interpretation exists it is
19275 -- also the selected one.
19276
19277 elsif Universal_Interpretation (N) = Universal_Integer then
19278 T := Standard_Integer;
19279
19280 else
19281 T := Any_Type;
19282
19283 declare
19284 Ind : Interp_Index;
19285 It : Interp;
19286
19287 begin
19288 Get_First_Interp (N, Ind, It);
19289 while Present (It.Typ) loop
19290 if Is_Discrete_Type (It.Typ) then
19291
19292 if Found
19293 and then not Covers (It.Typ, T)
19294 and then not Covers (T, It.Typ)
19295 then
19296 Error_Msg_N ("ambiguous bounds in discrete range", N);
19297 exit;
19298 else
19299 T := It.Typ;
19300 Found := True;
19301 end if;
19302 end if;
19303
19304 Get_Next_Interp (Ind, It);
19305 end loop;
19306
19307 if T = Any_Type then
19308 Error_Msg_N ("discrete type required for range", N);
19309 Set_Etype (N, Any_Type);
19310 return;
19311
19312 elsif T = Universal_Integer then
19313 T := Standard_Integer;
19314 end if;
19315 end;
19316 end if;
19317
19318 if not Is_Discrete_Type (T) then
19319 Error_Msg_N ("discrete type required for range", N);
19320 Set_Etype (N, Any_Type);
19321 return;
19322 end if;
19323
19324 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19325 and then Attribute_Name (Low_Bound (N)) = Name_First
19326 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19327 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19328 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19329 then
19330 -- The type of the index will be the type of the prefix, as long
19331 -- as the upper bound is 'Last of the same type.
19332
19333 Def_Id := Entity (Prefix (Low_Bound (N)));
19334
19335 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19336 or else Attribute_Name (High_Bound (N)) /= Name_Last
19337 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19338 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19339 then
19340 Def_Id := Empty;
19341 end if;
19342 end if;
19343
19344 R := N;
19345 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19346
19347 elsif Nkind (N) = N_Subtype_Indication then
19348
19349 -- The index is given by a subtype with a range constraint
19350
19351 T := Base_Type (Entity (Subtype_Mark (N)));
19352
19353 if not Is_Discrete_Type (T) then
19354 Error_Msg_N ("discrete type required for range", N);
19355 Set_Etype (N, Any_Type);
19356 return;
19357 end if;
19358
19359 R := Range_Expression (Constraint (N));
19360
19361 Resolve (R, T);
19362 Process_Range_Expr_In_Decl
19363 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19364
19365 elsif Nkind (N) = N_Attribute_Reference then
19366
19367 -- Catch beginner's error (use of attribute other than 'Range)
19368
19369 if Attribute_Name (N) /= Name_Range then
19370 Error_Msg_N ("expect attribute ''Range", N);
19371 Set_Etype (N, Any_Type);
19372 return;
19373 end if;
19374
19375 -- If the node denotes the range of a type mark, that is also the
19376 -- resulting type, and we do not need to create an Itype for it.
19377
19378 if Is_Entity_Name (Prefix (N))
19379 and then Comes_From_Source (N)
19380 and then Is_Type (Entity (Prefix (N)))
19381 and then Is_Discrete_Type (Entity (Prefix (N)))
19382 then
19383 Def_Id := Entity (Prefix (N));
19384 end if;
19385
19386 Analyze_And_Resolve (N);
19387 T := Etype (N);
19388 R := N;
19389
19390 -- If none of the above, must be a subtype. We convert this to a
19391 -- range attribute reference because in the case of declared first
19392 -- named subtypes, the types in the range reference can be different
19393 -- from the type of the entity. A range attribute normalizes the
19394 -- reference and obtains the correct types for the bounds.
19395
19396 -- This transformation is in the nature of an expansion, is only
19397 -- done if expansion is active. In particular, it is not done on
19398 -- formal generic types, because we need to retain the name of the
19399 -- original index for instantiation purposes.
19400
19401 else
19402 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19403 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19404 Set_Etype (N, Any_Integer);
19405 return;
19406
19407 else
19408 -- The type mark may be that of an incomplete type. It is only
19409 -- now that we can get the full view, previous analysis does
19410 -- not look specifically for a type mark.
19411
19412 Set_Entity (N, Get_Full_View (Entity (N)));
19413 Set_Etype (N, Entity (N));
19414 Def_Id := Entity (N);
19415
19416 if not Is_Discrete_Type (Def_Id) then
19417 Error_Msg_N ("discrete type required for index", N);
19418 Set_Etype (N, Any_Type);
19419 return;
19420 end if;
19421 end if;
19422
19423 if Expander_Active then
19424 Rewrite (N,
19425 Make_Attribute_Reference (Sloc (N),
19426 Attribute_Name => Name_Range,
19427 Prefix => Relocate_Node (N)));
19428
19429 -- The original was a subtype mark that does not freeze. This
19430 -- means that the rewritten version must not freeze either.
19431
19432 Set_Must_Not_Freeze (N);
19433 Set_Must_Not_Freeze (Prefix (N));
19434 Analyze_And_Resolve (N);
19435 T := Etype (N);
19436 R := N;
19437
19438 -- If expander is inactive, type is legal, nothing else to construct
19439
19440 else
19441 return;
19442 end if;
19443 end if;
19444
19445 if not Is_Discrete_Type (T) then
19446 Error_Msg_N ("discrete type required for range", N);
19447 Set_Etype (N, Any_Type);
19448 return;
19449
19450 elsif T = Any_Type then
19451 Set_Etype (N, Any_Type);
19452 return;
19453 end if;
19454
19455 -- We will now create the appropriate Itype to describe the range, but
19456 -- first a check. If we originally had a subtype, then we just label
19457 -- the range with this subtype. Not only is there no need to construct
19458 -- a new subtype, but it is wrong to do so for two reasons:
19459
19460 -- 1. A legality concern, if we have a subtype, it must not freeze,
19461 -- and the Itype would cause freezing incorrectly
19462
19463 -- 2. An efficiency concern, if we created an Itype, it would not be
19464 -- recognized as the same type for the purposes of eliminating
19465 -- checks in some circumstances.
19466
19467 -- We signal this case by setting the subtype entity in Def_Id
19468
19469 if No (Def_Id) then
19470 Def_Id :=
19471 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19472 Set_Etype (Def_Id, Base_Type (T));
19473
19474 if Is_Signed_Integer_Type (T) then
19475 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19476
19477 elsif Is_Modular_Integer_Type (T) then
19478 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19479
19480 else
19481 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19482 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19483 Set_First_Literal (Def_Id, First_Literal (T));
19484 end if;
19485
19486 Set_Size_Info (Def_Id, (T));
19487 Set_RM_Size (Def_Id, RM_Size (T));
19488 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19489
19490 Set_Scalar_Range (Def_Id, R);
19491 Conditional_Delay (Def_Id, T);
19492
19493 if Nkind (N) = N_Subtype_Indication then
19494 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19495 end if;
19496
19497 -- In the subtype indication case, if the immediate parent of the
19498 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19499 -- even if its bounds are static.
19500
19501 if Nkind (N) = N_Subtype_Indication
19502 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19503 then
19504 Set_Is_Non_Static_Subtype (Def_Id);
19505 end if;
19506 end if;
19507
19508 -- Final step is to label the index with this constructed type
19509
19510 Set_Etype (N, Def_Id);
19511 end Make_Index;
19512
19513 ------------------------------
19514 -- Modular_Type_Declaration --
19515 ------------------------------
19516
19517 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19518 Mod_Expr : constant Node_Id := Expression (Def);
19519 M_Val : Uint;
19520
19521 procedure Set_Modular_Size (Bits : Int);
19522 -- Sets RM_Size to Bits, and Esize to normal word size above this
19523
19524 ----------------------
19525 -- Set_Modular_Size --
19526 ----------------------
19527
19528 procedure Set_Modular_Size (Bits : Int) is
19529 begin
19530 Set_RM_Size (T, UI_From_Int (Bits));
19531
19532 if Bits <= 8 then
19533 Init_Esize (T, 8);
19534
19535 elsif Bits <= 16 then
19536 Init_Esize (T, 16);
19537
19538 elsif Bits <= 32 then
19539 Init_Esize (T, 32);
19540
19541 else
19542 Init_Esize (T, System_Max_Binary_Modulus_Power);
19543 end if;
19544
19545 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19546 Set_Is_Known_Valid (T);
19547 end if;
19548 end Set_Modular_Size;
19549
19550 -- Start of processing for Modular_Type_Declaration
19551
19552 begin
19553 -- If the mod expression is (exactly) 2 * literal, where literal is
19554 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19555
19556 if Warn_On_Suspicious_Modulus_Value
19557 and then Nkind (Mod_Expr) = N_Op_Multiply
19558 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19559 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19560 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19561 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19562 then
19563 Error_Msg_N
19564 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19565 end if;
19566
19567 -- Proceed with analysis of mod expression
19568
19569 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19570 Set_Etype (T, T);
19571 Set_Ekind (T, E_Modular_Integer_Type);
19572 Init_Alignment (T);
19573 Set_Is_Constrained (T);
19574
19575 if not Is_OK_Static_Expression (Mod_Expr) then
19576 Flag_Non_Static_Expr
19577 ("non-static expression used for modular type bound!", Mod_Expr);
19578 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19579 else
19580 M_Val := Expr_Value (Mod_Expr);
19581 end if;
19582
19583 if M_Val < 1 then
19584 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19585 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19586 end if;
19587
19588 if M_Val > 2 ** Standard_Long_Integer_Size then
19589 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19590 end if;
19591
19592 Set_Modulus (T, M_Val);
19593
19594 -- Create bounds for the modular type based on the modulus given in
19595 -- the type declaration and then analyze and resolve those bounds.
19596
19597 Set_Scalar_Range (T,
19598 Make_Range (Sloc (Mod_Expr),
19599 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19600 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19601
19602 -- Properly analyze the literals for the range. We do this manually
19603 -- because we can't go calling Resolve, since we are resolving these
19604 -- bounds with the type, and this type is certainly not complete yet.
19605
19606 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19607 Set_Etype (High_Bound (Scalar_Range (T)), T);
19608 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19609 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19610
19611 -- Loop through powers of two to find number of bits required
19612
19613 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19614
19615 -- Binary case
19616
19617 if M_Val = 2 ** Bits then
19618 Set_Modular_Size (Bits);
19619 return;
19620
19621 -- Nonbinary case
19622
19623 elsif M_Val < 2 ** Bits then
19624 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19625 Set_Non_Binary_Modulus (T);
19626
19627 if Bits > System_Max_Nonbinary_Modulus_Power then
19628 Error_Msg_Uint_1 :=
19629 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19630 Error_Msg_F
19631 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19632 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19633 return;
19634
19635 else
19636 -- In the nonbinary case, set size as per RM 13.3(55)
19637
19638 Set_Modular_Size (Bits);
19639 return;
19640 end if;
19641 end if;
19642
19643 end loop;
19644
19645 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19646 -- so we just signal an error and set the maximum size.
19647
19648 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19649 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19650
19651 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19652 Init_Alignment (T);
19653
19654 end Modular_Type_Declaration;
19655
19656 --------------------------
19657 -- New_Concatenation_Op --
19658 --------------------------
19659
19660 procedure New_Concatenation_Op (Typ : Entity_Id) is
19661 Loc : constant Source_Ptr := Sloc (Typ);
19662 Op : Entity_Id;
19663
19664 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19665 -- Create abbreviated declaration for the formal of a predefined
19666 -- Operator 'Op' of type 'Typ'
19667
19668 --------------------
19669 -- Make_Op_Formal --
19670 --------------------
19671
19672 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19673 Formal : Entity_Id;
19674 begin
19675 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19676 Set_Etype (Formal, Typ);
19677 Set_Mechanism (Formal, Default_Mechanism);
19678 return Formal;
19679 end Make_Op_Formal;
19680
19681 -- Start of processing for New_Concatenation_Op
19682
19683 begin
19684 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19685
19686 Set_Ekind (Op, E_Operator);
19687 Set_Scope (Op, Current_Scope);
19688 Set_Etype (Op, Typ);
19689 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19690 Set_Is_Immediately_Visible (Op);
19691 Set_Is_Intrinsic_Subprogram (Op);
19692 Set_Has_Completion (Op);
19693 Append_Entity (Op, Current_Scope);
19694
19695 Set_Name_Entity_Id (Name_Op_Concat, Op);
19696
19697 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19698 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19699 end New_Concatenation_Op;
19700
19701 -------------------------
19702 -- OK_For_Limited_Init --
19703 -------------------------
19704
19705 -- ???Check all calls of this, and compare the conditions under which it's
19706 -- called.
19707
19708 function OK_For_Limited_Init
19709 (Typ : Entity_Id;
19710 Exp : Node_Id) return Boolean
19711 is
19712 begin
19713 return Is_CPP_Constructor_Call (Exp)
19714 or else (Ada_Version >= Ada_2005
19715 and then not Debug_Flag_Dot_L
19716 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19717 end OK_For_Limited_Init;
19718
19719 -------------------------------
19720 -- OK_For_Limited_Init_In_05 --
19721 -------------------------------
19722
19723 function OK_For_Limited_Init_In_05
19724 (Typ : Entity_Id;
19725 Exp : Node_Id) return Boolean
19726 is
19727 begin
19728 -- An object of a limited interface type can be initialized with any
19729 -- expression of a nonlimited descendant type. However this does not
19730 -- apply if this is a view conversion of some other expression. This
19731 -- is checked below.
19732
19733 if Is_Class_Wide_Type (Typ)
19734 and then Is_Limited_Interface (Typ)
19735 and then not Is_Limited_Type (Etype (Exp))
19736 and then Nkind (Exp) /= N_Type_Conversion
19737 then
19738 return True;
19739 end if;
19740
19741 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19742 -- case of limited aggregates (including extension aggregates), and
19743 -- function calls. The function call may have been given in prefixed
19744 -- notation, in which case the original node is an indexed component.
19745 -- If the function is parameterless, the original node was an explicit
19746 -- dereference. The function may also be parameterless, in which case
19747 -- the source node is just an identifier.
19748
19749 -- A branch of a conditional expression may have been removed if the
19750 -- condition is statically known. This happens during expansion, and
19751 -- thus will not happen if previous errors were encountered. The check
19752 -- will have been performed on the chosen branch, which replaces the
19753 -- original conditional expression.
19754
19755 if No (Exp) then
19756 return True;
19757 end if;
19758
19759 case Nkind (Original_Node (Exp)) is
19760 when N_Aggregate
19761 | N_Extension_Aggregate
19762 | N_Function_Call
19763 | N_Op
19764 =>
19765 return True;
19766
19767 when N_Identifier =>
19768 return Present (Entity (Original_Node (Exp)))
19769 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19770
19771 when N_Qualified_Expression =>
19772 return
19773 OK_For_Limited_Init_In_05
19774 (Typ, Expression (Original_Node (Exp)));
19775
19776 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19777 -- with a function call, the expander has rewritten the call into an
19778 -- N_Type_Conversion node to force displacement of the pointer to
19779 -- reference the component containing the secondary dispatch table.
19780 -- Otherwise a type conversion is not a legal context.
19781 -- A return statement for a build-in-place function returning a
19782 -- synchronized type also introduces an unchecked conversion.
19783
19784 when N_Type_Conversion
19785 | N_Unchecked_Type_Conversion
19786 =>
19787 return not Comes_From_Source (Exp)
19788 and then
19789 -- If the conversion has been rewritten, check Original_Node
19790
19791 ((Original_Node (Exp) /= Exp
19792 and then
19793 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
19794
19795 -- Otherwise, check the expression of the compiler-generated
19796 -- conversion (which is a conversion that we want to ignore
19797 -- for purposes of the limited-initialization restrictions).
19798
19799 or else
19800 (Original_Node (Exp) = Exp
19801 and then
19802 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
19803
19804 when N_Explicit_Dereference
19805 | N_Indexed_Component
19806 | N_Selected_Component
19807 =>
19808 return Nkind (Exp) = N_Function_Call;
19809
19810 -- A use of 'Input is a function call, hence allowed. Normally the
19811 -- attribute will be changed to a call, but the attribute by itself
19812 -- can occur with -gnatc.
19813
19814 when N_Attribute_Reference =>
19815 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19816
19817 -- "return raise ..." is OK
19818
19819 when N_Raise_Expression =>
19820 return True;
19821
19822 -- For a case expression, all dependent expressions must be legal
19823
19824 when N_Case_Expression =>
19825 declare
19826 Alt : Node_Id;
19827
19828 begin
19829 Alt := First (Alternatives (Original_Node (Exp)));
19830 while Present (Alt) loop
19831 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19832 return False;
19833 end if;
19834
19835 Next (Alt);
19836 end loop;
19837
19838 return True;
19839 end;
19840
19841 -- For an if expression, all dependent expressions must be legal
19842
19843 when N_If_Expression =>
19844 declare
19845 Then_Expr : constant Node_Id :=
19846 Next (First (Expressions (Original_Node (Exp))));
19847 Else_Expr : constant Node_Id := Next (Then_Expr);
19848 begin
19849 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19850 and then
19851 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19852 end;
19853
19854 when others =>
19855 return False;
19856 end case;
19857 end OK_For_Limited_Init_In_05;
19858
19859 -------------------------------------------
19860 -- Ordinary_Fixed_Point_Type_Declaration --
19861 -------------------------------------------
19862
19863 procedure Ordinary_Fixed_Point_Type_Declaration
19864 (T : Entity_Id;
19865 Def : Node_Id)
19866 is
19867 Loc : constant Source_Ptr := Sloc (Def);
19868 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19869 RRS : constant Node_Id := Real_Range_Specification (Def);
19870 Implicit_Base : Entity_Id;
19871 Delta_Val : Ureal;
19872 Small_Val : Ureal;
19873 Low_Val : Ureal;
19874 High_Val : Ureal;
19875
19876 begin
19877 Check_Restriction (No_Fixed_Point, Def);
19878
19879 -- Create implicit base type
19880
19881 Implicit_Base :=
19882 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19883 Set_Etype (Implicit_Base, Implicit_Base);
19884
19885 -- Analyze and process delta expression
19886
19887 Analyze_And_Resolve (Delta_Expr, Any_Real);
19888
19889 Check_Delta_Expression (Delta_Expr);
19890 Delta_Val := Expr_Value_R (Delta_Expr);
19891
19892 Set_Delta_Value (Implicit_Base, Delta_Val);
19893
19894 -- Compute default small from given delta, which is the largest power
19895 -- of two that does not exceed the given delta value.
19896
19897 declare
19898 Tmp : Ureal;
19899 Scale : Int;
19900
19901 begin
19902 Tmp := Ureal_1;
19903 Scale := 0;
19904
19905 if Delta_Val < Ureal_1 then
19906 while Delta_Val < Tmp loop
19907 Tmp := Tmp / Ureal_2;
19908 Scale := Scale + 1;
19909 end loop;
19910
19911 else
19912 loop
19913 Tmp := Tmp * Ureal_2;
19914 exit when Tmp > Delta_Val;
19915 Scale := Scale - 1;
19916 end loop;
19917 end if;
19918
19919 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19920 end;
19921
19922 Set_Small_Value (Implicit_Base, Small_Val);
19923
19924 -- If no range was given, set a dummy range
19925
19926 if RRS <= Empty_Or_Error then
19927 Low_Val := -Small_Val;
19928 High_Val := Small_Val;
19929
19930 -- Otherwise analyze and process given range
19931
19932 else
19933 declare
19934 Low : constant Node_Id := Low_Bound (RRS);
19935 High : constant Node_Id := High_Bound (RRS);
19936
19937 begin
19938 Analyze_And_Resolve (Low, Any_Real);
19939 Analyze_And_Resolve (High, Any_Real);
19940 Check_Real_Bound (Low);
19941 Check_Real_Bound (High);
19942
19943 -- Obtain and set the range
19944
19945 Low_Val := Expr_Value_R (Low);
19946 High_Val := Expr_Value_R (High);
19947
19948 if Low_Val > High_Val then
19949 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19950 end if;
19951 end;
19952 end if;
19953
19954 -- The range for both the implicit base and the declared first subtype
19955 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19956 -- set a temporary range in place. Note that the bounds of the base
19957 -- type will be widened to be symmetrical and to fill the available
19958 -- bits when the type is frozen.
19959
19960 -- We could do this with all discrete types, and probably should, but
19961 -- we absolutely have to do it for fixed-point, since the end-points
19962 -- of the range and the size are determined by the small value, which
19963 -- could be reset before the freeze point.
19964
19965 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19966 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19967
19968 -- Complete definition of first subtype. The inheritance of the rep item
19969 -- chain ensures that SPARK-related pragmas are not clobbered when the
19970 -- ordinary fixed point type acts as a full view of a private type.
19971
19972 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19973 Set_Etype (T, Implicit_Base);
19974 Init_Size_Align (T);
19975 Inherit_Rep_Item_Chain (T, Implicit_Base);
19976 Set_Small_Value (T, Small_Val);
19977 Set_Delta_Value (T, Delta_Val);
19978 Set_Is_Constrained (T);
19979 end Ordinary_Fixed_Point_Type_Declaration;
19980
19981 ----------------------------------
19982 -- Preanalyze_Assert_Expression --
19983 ----------------------------------
19984
19985 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19986 begin
19987 In_Assertion_Expr := In_Assertion_Expr + 1;
19988 Preanalyze_Spec_Expression (N, T);
19989 In_Assertion_Expr := In_Assertion_Expr - 1;
19990 end Preanalyze_Assert_Expression;
19991
19992 -----------------------------------
19993 -- Preanalyze_Default_Expression --
19994 -----------------------------------
19995
19996 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19997 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19998 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19999
20000 begin
20001 In_Default_Expr := True;
20002 In_Spec_Expression := True;
20003
20004 Preanalyze_With_Freezing_And_Resolve (N, T);
20005
20006 In_Default_Expr := Save_In_Default_Expr;
20007 In_Spec_Expression := Save_In_Spec_Expression;
20008 end Preanalyze_Default_Expression;
20009
20010 --------------------------------
20011 -- Preanalyze_Spec_Expression --
20012 --------------------------------
20013
20014 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20015 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20016 begin
20017 In_Spec_Expression := True;
20018 Preanalyze_And_Resolve (N, T);
20019 In_Spec_Expression := Save_In_Spec_Expression;
20020 end Preanalyze_Spec_Expression;
20021
20022 ----------------------------------------
20023 -- Prepare_Private_Subtype_Completion --
20024 ----------------------------------------
20025
20026 procedure Prepare_Private_Subtype_Completion
20027 (Id : Entity_Id;
20028 Related_Nod : Node_Id)
20029 is
20030 Id_B : constant Entity_Id := Base_Type (Id);
20031 Full_B : constant Entity_Id := Full_View (Id_B);
20032 Full : Entity_Id;
20033
20034 begin
20035 if Present (Full_B) then
20036
20037 -- The Base_Type is already completed, we can complete the subtype
20038 -- now. We have to create a new entity with the same name, Thus we
20039 -- can't use Create_Itype.
20040
20041 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20042 Set_Is_Itype (Full);
20043 Set_Associated_Node_For_Itype (Full, Related_Nod);
20044 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20045 Set_Full_View (Id, Full);
20046 end if;
20047
20048 -- The parent subtype may be private, but the base might not, in some
20049 -- nested instances. In that case, the subtype does not need to be
20050 -- exchanged. It would still be nice to make private subtypes and their
20051 -- bases consistent at all times ???
20052
20053 if Is_Private_Type (Id_B) then
20054 Append_Elmt (Id, Private_Dependents (Id_B));
20055 end if;
20056 end Prepare_Private_Subtype_Completion;
20057
20058 ---------------------------
20059 -- Process_Discriminants --
20060 ---------------------------
20061
20062 procedure Process_Discriminants
20063 (N : Node_Id;
20064 Prev : Entity_Id := Empty)
20065 is
20066 Elist : constant Elist_Id := New_Elmt_List;
20067 Id : Node_Id;
20068 Discr : Node_Id;
20069 Discr_Number : Uint;
20070 Discr_Type : Entity_Id;
20071 Default_Present : Boolean := False;
20072 Default_Not_Present : Boolean := False;
20073
20074 begin
20075 -- A composite type other than an array type can have discriminants.
20076 -- On entry, the current scope is the composite type.
20077
20078 -- The discriminants are initially entered into the scope of the type
20079 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20080 -- use, as explained at the end of this procedure.
20081
20082 Discr := First (Discriminant_Specifications (N));
20083 while Present (Discr) loop
20084 Enter_Name (Defining_Identifier (Discr));
20085
20086 -- For navigation purposes we add a reference to the discriminant
20087 -- in the entity for the type. If the current declaration is a
20088 -- completion, place references on the partial view. Otherwise the
20089 -- type is the current scope.
20090
20091 if Present (Prev) then
20092
20093 -- The references go on the partial view, if present. If the
20094 -- partial view has discriminants, the references have been
20095 -- generated already.
20096
20097 if not Has_Discriminants (Prev) then
20098 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20099 end if;
20100 else
20101 Generate_Reference
20102 (Current_Scope, Defining_Identifier (Discr), 'd');
20103 end if;
20104
20105 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20106 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
20107
20108 -- Ada 2005 (AI-254)
20109
20110 if Present (Access_To_Subprogram_Definition
20111 (Discriminant_Type (Discr)))
20112 and then Protected_Present (Access_To_Subprogram_Definition
20113 (Discriminant_Type (Discr)))
20114 then
20115 Discr_Type :=
20116 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20117 end if;
20118
20119 else
20120 Find_Type (Discriminant_Type (Discr));
20121 Discr_Type := Etype (Discriminant_Type (Discr));
20122
20123 if Error_Posted (Discriminant_Type (Discr)) then
20124 Discr_Type := Any_Type;
20125 end if;
20126 end if;
20127
20128 -- Handling of discriminants that are access types
20129
20130 if Is_Access_Type (Discr_Type) then
20131
20132 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20133 -- limited record types
20134
20135 if Ada_Version < Ada_2005 then
20136 Check_Access_Discriminant_Requires_Limited
20137 (Discr, Discriminant_Type (Discr));
20138 end if;
20139
20140 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20141 Error_Msg_N
20142 ("(Ada 83) access discriminant not allowed", Discr);
20143 end if;
20144
20145 -- If not access type, must be a discrete type
20146
20147 elsif not Is_Discrete_Type (Discr_Type) then
20148 Error_Msg_N
20149 ("discriminants must have a discrete or access type",
20150 Discriminant_Type (Discr));
20151 end if;
20152
20153 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20154
20155 -- If a discriminant specification includes the assignment compound
20156 -- delimiter followed by an expression, the expression is the default
20157 -- expression of the discriminant; the default expression must be of
20158 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20159 -- a default expression, we do the special preanalysis, since this
20160 -- expression does not freeze (see section "Handling of Default and
20161 -- Per-Object Expressions" in spec of package Sem).
20162
20163 if Present (Expression (Discr)) then
20164 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
20165
20166 -- Legaity checks
20167
20168 if Nkind (N) = N_Formal_Type_Declaration then
20169 Error_Msg_N
20170 ("discriminant defaults not allowed for formal type",
20171 Expression (Discr));
20172
20173 -- Flag an error for a tagged type with defaulted discriminants,
20174 -- excluding limited tagged types when compiling for Ada 2012
20175 -- (see AI05-0214).
20176
20177 elsif Is_Tagged_Type (Current_Scope)
20178 and then (not Is_Limited_Type (Current_Scope)
20179 or else Ada_Version < Ada_2012)
20180 and then Comes_From_Source (N)
20181 then
20182 -- Note: see similar test in Check_Or_Process_Discriminants, to
20183 -- handle the (illegal) case of the completion of an untagged
20184 -- view with discriminants with defaults by a tagged full view.
20185 -- We skip the check if Discr does not come from source, to
20186 -- account for the case of an untagged derived type providing
20187 -- defaults for a renamed discriminant from a private untagged
20188 -- ancestor with a tagged full view (ACATS B460006).
20189
20190 if Ada_Version >= Ada_2012 then
20191 Error_Msg_N
20192 ("discriminants of nonlimited tagged type cannot have"
20193 & " defaults",
20194 Expression (Discr));
20195 else
20196 Error_Msg_N
20197 ("discriminants of tagged type cannot have defaults",
20198 Expression (Discr));
20199 end if;
20200
20201 else
20202 Default_Present := True;
20203 Append_Elmt (Expression (Discr), Elist);
20204
20205 -- Tag the defining identifiers for the discriminants with
20206 -- their corresponding default expressions from the tree.
20207
20208 Set_Discriminant_Default_Value
20209 (Defining_Identifier (Discr), Expression (Discr));
20210 end if;
20211
20212 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20213 -- gets set unless we can be sure that no range check is required.
20214
20215 if (GNATprove_Mode or not Expander_Active)
20216 and then not
20217 Is_In_Range
20218 (Expression (Discr), Discr_Type, Assume_Valid => True)
20219 then
20220 Set_Do_Range_Check (Expression (Discr));
20221 end if;
20222
20223 -- No default discriminant value given
20224
20225 else
20226 Default_Not_Present := True;
20227 end if;
20228
20229 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20230 -- Discr_Type but with the null-exclusion attribute
20231
20232 if Ada_Version >= Ada_2005 then
20233
20234 -- Ada 2005 (AI-231): Static checks
20235
20236 if Can_Never_Be_Null (Discr_Type) then
20237 Null_Exclusion_Static_Checks (Discr);
20238
20239 elsif Is_Access_Type (Discr_Type)
20240 and then Null_Exclusion_Present (Discr)
20241
20242 -- No need to check itypes because in their case this check
20243 -- was done at their point of creation
20244
20245 and then not Is_Itype (Discr_Type)
20246 then
20247 if Can_Never_Be_Null (Discr_Type) then
20248 Error_Msg_NE
20249 ("`NOT NULL` not allowed (& already excludes null)",
20250 Discr,
20251 Discr_Type);
20252 end if;
20253
20254 Set_Etype (Defining_Identifier (Discr),
20255 Create_Null_Excluding_Itype
20256 (T => Discr_Type,
20257 Related_Nod => Discr));
20258
20259 -- Check for improper null exclusion if the type is otherwise
20260 -- legal for a discriminant.
20261
20262 elsif Null_Exclusion_Present (Discr)
20263 and then Is_Discrete_Type (Discr_Type)
20264 then
20265 Error_Msg_N
20266 ("null exclusion can only apply to an access type", Discr);
20267 end if;
20268
20269 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20270 -- can't have defaults. Synchronized types, or types that are
20271 -- explicitly limited are fine, but special tests apply to derived
20272 -- types in generics: in a generic body we have to assume the
20273 -- worst, and therefore defaults are not allowed if the parent is
20274 -- a generic formal private type (see ACATS B370001).
20275
20276 if Is_Access_Type (Discr_Type) and then Default_Present then
20277 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20278 or else Is_Limited_Record (Current_Scope)
20279 or else Is_Concurrent_Type (Current_Scope)
20280 or else Is_Concurrent_Record_Type (Current_Scope)
20281 or else Ekind (Current_Scope) = E_Limited_Private_Type
20282 then
20283 if not Is_Derived_Type (Current_Scope)
20284 or else not Is_Generic_Type (Etype (Current_Scope))
20285 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20286 or else Limited_Present
20287 (Type_Definition (Parent (Current_Scope)))
20288 then
20289 null;
20290
20291 else
20292 Error_Msg_N
20293 ("access discriminants of nonlimited types cannot "
20294 & "have defaults", Expression (Discr));
20295 end if;
20296
20297 elsif Present (Expression (Discr)) then
20298 Error_Msg_N
20299 ("(Ada 2005) access discriminants of nonlimited types "
20300 & "cannot have defaults", Expression (Discr));
20301 end if;
20302 end if;
20303 end if;
20304
20305 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20306 -- This check is relevant only when SPARK_Mode is on as it is not a
20307 -- standard Ada legality rule.
20308
20309 if SPARK_Mode = On
20310 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20311 then
20312 Error_Msg_N ("discriminant cannot be volatile", Discr);
20313 end if;
20314
20315 Next (Discr);
20316 end loop;
20317
20318 -- An element list consisting of the default expressions of the
20319 -- discriminants is constructed in the above loop and used to set
20320 -- the Discriminant_Constraint attribute for the type. If an object
20321 -- is declared of this (record or task) type without any explicit
20322 -- discriminant constraint given, this element list will form the
20323 -- actual parameters for the corresponding initialization procedure
20324 -- for the type.
20325
20326 Set_Discriminant_Constraint (Current_Scope, Elist);
20327 Set_Stored_Constraint (Current_Scope, No_Elist);
20328
20329 -- Default expressions must be provided either for all or for none
20330 -- of the discriminants of a discriminant part. (RM 3.7.1)
20331
20332 if Default_Present and then Default_Not_Present then
20333 Error_Msg_N
20334 ("incomplete specification of defaults for discriminants", N);
20335 end if;
20336
20337 -- The use of the name of a discriminant is not allowed in default
20338 -- expressions of a discriminant part if the specification of the
20339 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20340
20341 -- To detect this, the discriminant names are entered initially with an
20342 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20343 -- attempt to use a void entity (for example in an expression that is
20344 -- type-checked) produces the error message: premature usage. Now after
20345 -- completing the semantic analysis of the discriminant part, we can set
20346 -- the Ekind of all the discriminants appropriately.
20347
20348 Discr := First (Discriminant_Specifications (N));
20349 Discr_Number := Uint_1;
20350 while Present (Discr) loop
20351 Id := Defining_Identifier (Discr);
20352 Set_Ekind (Id, E_Discriminant);
20353 Init_Component_Location (Id);
20354 Init_Esize (Id);
20355 Set_Discriminant_Number (Id, Discr_Number);
20356
20357 -- Make sure this is always set, even in illegal programs
20358
20359 Set_Corresponding_Discriminant (Id, Empty);
20360
20361 -- Initialize the Original_Record_Component to the entity itself.
20362 -- Inherit_Components will propagate the right value to
20363 -- discriminants in derived record types.
20364
20365 Set_Original_Record_Component (Id, Id);
20366
20367 -- Create the discriminal for the discriminant
20368
20369 Build_Discriminal (Id);
20370
20371 Next (Discr);
20372 Discr_Number := Discr_Number + 1;
20373 end loop;
20374
20375 Set_Has_Discriminants (Current_Scope);
20376 end Process_Discriminants;
20377
20378 -----------------------
20379 -- Process_Full_View --
20380 -----------------------
20381
20382 -- WARNING: This routine manages Ghost regions. Return statements must be
20383 -- replaced by gotos which jump to the end of the routine and restore the
20384 -- Ghost mode.
20385
20386 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20387 procedure Collect_Implemented_Interfaces
20388 (Typ : Entity_Id;
20389 Ifaces : Elist_Id);
20390 -- Ada 2005: Gather all the interfaces that Typ directly or
20391 -- inherently implements. Duplicate entries are not added to
20392 -- the list Ifaces.
20393
20394 ------------------------------------
20395 -- Collect_Implemented_Interfaces --
20396 ------------------------------------
20397
20398 procedure Collect_Implemented_Interfaces
20399 (Typ : Entity_Id;
20400 Ifaces : Elist_Id)
20401 is
20402 Iface : Entity_Id;
20403 Iface_Elmt : Elmt_Id;
20404
20405 begin
20406 -- Abstract interfaces are only associated with tagged record types
20407
20408 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20409 return;
20410 end if;
20411
20412 -- Recursively climb to the ancestors
20413
20414 if Etype (Typ) /= Typ
20415
20416 -- Protect the frontend against wrong cyclic declarations like:
20417
20418 -- type B is new A with private;
20419 -- type C is new A with private;
20420 -- private
20421 -- type B is new C with null record;
20422 -- type C is new B with null record;
20423
20424 and then Etype (Typ) /= Priv_T
20425 and then Etype (Typ) /= Full_T
20426 then
20427 -- Keep separate the management of private type declarations
20428
20429 if Ekind (Typ) = E_Record_Type_With_Private then
20430
20431 -- Handle the following illegal usage:
20432 -- type Private_Type is tagged private;
20433 -- private
20434 -- type Private_Type is new Type_Implementing_Iface;
20435
20436 if Present (Full_View (Typ))
20437 and then Etype (Typ) /= Full_View (Typ)
20438 then
20439 if Is_Interface (Etype (Typ)) then
20440 Append_Unique_Elmt (Etype (Typ), Ifaces);
20441 end if;
20442
20443 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20444 end if;
20445
20446 -- Non-private types
20447
20448 else
20449 if Is_Interface (Etype (Typ)) then
20450 Append_Unique_Elmt (Etype (Typ), Ifaces);
20451 end if;
20452
20453 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20454 end if;
20455 end if;
20456
20457 -- Handle entities in the list of abstract interfaces
20458
20459 if Present (Interfaces (Typ)) then
20460 Iface_Elmt := First_Elmt (Interfaces (Typ));
20461 while Present (Iface_Elmt) loop
20462 Iface := Node (Iface_Elmt);
20463
20464 pragma Assert (Is_Interface (Iface));
20465
20466 if not Contain_Interface (Iface, Ifaces) then
20467 Append_Elmt (Iface, Ifaces);
20468 Collect_Implemented_Interfaces (Iface, Ifaces);
20469 end if;
20470
20471 Next_Elmt (Iface_Elmt);
20472 end loop;
20473 end if;
20474 end Collect_Implemented_Interfaces;
20475
20476 -- Local variables
20477
20478 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20479 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20480 -- Save the Ghost-related attributes to restore on exit
20481
20482 Full_Indic : Node_Id;
20483 Full_Parent : Entity_Id;
20484 Priv_Parent : Entity_Id;
20485
20486 -- Start of processing for Process_Full_View
20487
20488 begin
20489 Mark_And_Set_Ghost_Completion (N, Priv_T);
20490
20491 -- First some sanity checks that must be done after semantic
20492 -- decoration of the full view and thus cannot be placed with other
20493 -- similar checks in Find_Type_Name
20494
20495 if not Is_Limited_Type (Priv_T)
20496 and then (Is_Limited_Type (Full_T)
20497 or else Is_Limited_Composite (Full_T))
20498 then
20499 if In_Instance then
20500 null;
20501 else
20502 Error_Msg_N
20503 ("completion of nonlimited type cannot be limited", Full_T);
20504 Explain_Limited_Type (Full_T, Full_T);
20505 end if;
20506
20507 elsif Is_Abstract_Type (Full_T)
20508 and then not Is_Abstract_Type (Priv_T)
20509 then
20510 Error_Msg_N
20511 ("completion of nonabstract type cannot be abstract", Full_T);
20512
20513 elsif Is_Tagged_Type (Priv_T)
20514 and then Is_Limited_Type (Priv_T)
20515 and then not Is_Limited_Type (Full_T)
20516 then
20517 -- If pragma CPP_Class was applied to the private declaration
20518 -- propagate the limitedness to the full-view
20519
20520 if Is_CPP_Class (Priv_T) then
20521 Set_Is_Limited_Record (Full_T);
20522
20523 -- GNAT allow its own definition of Limited_Controlled to disobey
20524 -- this rule in order in ease the implementation. This test is safe
20525 -- because Root_Controlled is defined in a child of System that
20526 -- normal programs are not supposed to use.
20527
20528 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20529 Set_Is_Limited_Composite (Full_T);
20530 else
20531 Error_Msg_N
20532 ("completion of limited tagged type must be limited", Full_T);
20533 end if;
20534
20535 elsif Is_Generic_Type (Priv_T) then
20536 Error_Msg_N ("generic type cannot have a completion", Full_T);
20537 end if;
20538
20539 -- Check that ancestor interfaces of private and full views are
20540 -- consistent. We omit this check for synchronized types because
20541 -- they are performed on the corresponding record type when frozen.
20542
20543 if Ada_Version >= Ada_2005
20544 and then Is_Tagged_Type (Priv_T)
20545 and then Is_Tagged_Type (Full_T)
20546 and then not Is_Concurrent_Type (Full_T)
20547 then
20548 declare
20549 Iface : Entity_Id;
20550 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20551 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20552
20553 begin
20554 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20555 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20556
20557 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20558 -- an interface type if and only if the full type is descendant
20559 -- of the interface type (AARM 7.3 (7.3/2)).
20560
20561 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20562
20563 if Present (Iface) then
20564 Error_Msg_NE
20565 ("interface in partial view& not implemented by full type "
20566 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20567 end if;
20568
20569 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20570
20571 if Present (Iface) then
20572 Error_Msg_NE
20573 ("interface & not implemented by partial view "
20574 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20575 end if;
20576 end;
20577 end if;
20578
20579 if Is_Tagged_Type (Priv_T)
20580 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20581 and then Is_Derived_Type (Full_T)
20582 then
20583 Priv_Parent := Etype (Priv_T);
20584
20585 -- The full view of a private extension may have been transformed
20586 -- into an unconstrained derived type declaration and a subtype
20587 -- declaration (see build_derived_record_type for details).
20588
20589 if Nkind (N) = N_Subtype_Declaration then
20590 Full_Indic := Subtype_Indication (N);
20591 Full_Parent := Etype (Base_Type (Full_T));
20592 else
20593 Full_Indic := Subtype_Indication (Type_Definition (N));
20594 Full_Parent := Etype (Full_T);
20595 end if;
20596
20597 -- Check that the parent type of the full type is a descendant of
20598 -- the ancestor subtype given in the private extension. If either
20599 -- entity has an Etype equal to Any_Type then we had some previous
20600 -- error situation [7.3(8)].
20601
20602 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20603 goto Leave;
20604
20605 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20606 -- any order. Therefore we don't have to check that its parent must
20607 -- be a descendant of the parent of the private type declaration.
20608
20609 elsif Is_Interface (Priv_Parent)
20610 and then Is_Interface (Full_Parent)
20611 then
20612 null;
20613
20614 -- Ada 2005 (AI-251): If the parent of the private type declaration
20615 -- is an interface there is no need to check that it is an ancestor
20616 -- of the associated full type declaration. The required tests for
20617 -- this case are performed by Build_Derived_Record_Type.
20618
20619 elsif not Is_Interface (Base_Type (Priv_Parent))
20620 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20621 then
20622 Error_Msg_N
20623 ("parent of full type must descend from parent of private "
20624 & "extension", Full_Indic);
20625
20626 -- First check a formal restriction, and then proceed with checking
20627 -- Ada rules. Since the formal restriction is not a serious error, we
20628 -- don't prevent further error detection for this check, hence the
20629 -- ELSE.
20630
20631 else
20632 -- In formal mode, when completing a private extension the type
20633 -- named in the private part must be exactly the same as that
20634 -- named in the visible part.
20635
20636 if Priv_Parent /= Full_Parent then
20637 Error_Msg_Name_1 := Chars (Priv_Parent);
20638 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20639 end if;
20640
20641 -- Check the rules of 7.3(10): if the private extension inherits
20642 -- known discriminants, then the full type must also inherit those
20643 -- discriminants from the same (ancestor) type, and the parent
20644 -- subtype of the full type must be constrained if and only if
20645 -- the ancestor subtype of the private extension is constrained.
20646
20647 if No (Discriminant_Specifications (Parent (Priv_T)))
20648 and then not Has_Unknown_Discriminants (Priv_T)
20649 and then Has_Discriminants (Base_Type (Priv_Parent))
20650 then
20651 declare
20652 Priv_Indic : constant Node_Id :=
20653 Subtype_Indication (Parent (Priv_T));
20654
20655 Priv_Constr : constant Boolean :=
20656 Is_Constrained (Priv_Parent)
20657 or else
20658 Nkind (Priv_Indic) = N_Subtype_Indication
20659 or else
20660 Is_Constrained (Entity (Priv_Indic));
20661
20662 Full_Constr : constant Boolean :=
20663 Is_Constrained (Full_Parent)
20664 or else
20665 Nkind (Full_Indic) = N_Subtype_Indication
20666 or else
20667 Is_Constrained (Entity (Full_Indic));
20668
20669 Priv_Discr : Entity_Id;
20670 Full_Discr : Entity_Id;
20671
20672 begin
20673 Priv_Discr := First_Discriminant (Priv_Parent);
20674 Full_Discr := First_Discriminant (Full_Parent);
20675 while Present (Priv_Discr) and then Present (Full_Discr) loop
20676 if Original_Record_Component (Priv_Discr) =
20677 Original_Record_Component (Full_Discr)
20678 or else
20679 Corresponding_Discriminant (Priv_Discr) =
20680 Corresponding_Discriminant (Full_Discr)
20681 then
20682 null;
20683 else
20684 exit;
20685 end if;
20686
20687 Next_Discriminant (Priv_Discr);
20688 Next_Discriminant (Full_Discr);
20689 end loop;
20690
20691 if Present (Priv_Discr) or else Present (Full_Discr) then
20692 Error_Msg_N
20693 ("full view must inherit discriminants of the parent "
20694 & "type used in the private extension", Full_Indic);
20695
20696 elsif Priv_Constr and then not Full_Constr then
20697 Error_Msg_N
20698 ("parent subtype of full type must be constrained",
20699 Full_Indic);
20700
20701 elsif Full_Constr and then not Priv_Constr then
20702 Error_Msg_N
20703 ("parent subtype of full type must be unconstrained",
20704 Full_Indic);
20705 end if;
20706 end;
20707
20708 -- Check the rules of 7.3(12): if a partial view has neither
20709 -- known or unknown discriminants, then the full type
20710 -- declaration shall define a definite subtype.
20711
20712 elsif not Has_Unknown_Discriminants (Priv_T)
20713 and then not Has_Discriminants (Priv_T)
20714 and then not Is_Constrained (Full_T)
20715 then
20716 Error_Msg_N
20717 ("full view must define a constrained type if partial view "
20718 & "has no discriminants", Full_T);
20719 end if;
20720
20721 -- ??????? Do we implement the following properly ?????
20722 -- If the ancestor subtype of a private extension has constrained
20723 -- discriminants, then the parent subtype of the full view shall
20724 -- impose a statically matching constraint on those discriminants
20725 -- [7.3(13)].
20726 end if;
20727
20728 else
20729 -- For untagged types, verify that a type without discriminants is
20730 -- not completed with an unconstrained type. A separate error message
20731 -- is produced if the full type has defaulted discriminants.
20732
20733 if Is_Definite_Subtype (Priv_T)
20734 and then not Is_Definite_Subtype (Full_T)
20735 then
20736 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20737 Error_Msg_NE
20738 ("full view of& not compatible with declaration#",
20739 Full_T, Priv_T);
20740
20741 if not Is_Tagged_Type (Full_T) then
20742 Error_Msg_N
20743 ("\one is constrained, the other unconstrained", Full_T);
20744 end if;
20745 end if;
20746 end if;
20747
20748 -- AI-419: verify that the use of "limited" is consistent
20749
20750 declare
20751 Orig_Decl : constant Node_Id := Original_Node (N);
20752
20753 begin
20754 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20755 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20756 and then Nkind
20757 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20758 then
20759 if not Limited_Present (Parent (Priv_T))
20760 and then not Synchronized_Present (Parent (Priv_T))
20761 and then Limited_Present (Type_Definition (Orig_Decl))
20762 then
20763 Error_Msg_N
20764 ("full view of non-limited extension cannot be limited", N);
20765
20766 -- Conversely, if the partial view carries the limited keyword,
20767 -- the full view must as well, even if it may be redundant.
20768
20769 elsif Limited_Present (Parent (Priv_T))
20770 and then not Limited_Present (Type_Definition (Orig_Decl))
20771 then
20772 Error_Msg_N
20773 ("full view of limited extension must be explicitly limited",
20774 N);
20775 end if;
20776 end if;
20777 end;
20778
20779 -- Ada 2005 (AI-443): A synchronized private extension must be
20780 -- completed by a task or protected type.
20781
20782 if Ada_Version >= Ada_2005
20783 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20784 and then Synchronized_Present (Parent (Priv_T))
20785 and then not Is_Concurrent_Type (Full_T)
20786 then
20787 Error_Msg_N ("full view of synchronized extension must " &
20788 "be synchronized type", N);
20789 end if;
20790
20791 -- Ada 2005 AI-363: if the full view has discriminants with
20792 -- defaults, it is illegal to declare constrained access subtypes
20793 -- whose designated type is the current type. This allows objects
20794 -- of the type that are declared in the heap to be unconstrained.
20795
20796 if not Has_Unknown_Discriminants (Priv_T)
20797 and then not Has_Discriminants (Priv_T)
20798 and then Has_Discriminants (Full_T)
20799 and then
20800 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20801 then
20802 Set_Has_Constrained_Partial_View (Full_T);
20803 Set_Has_Constrained_Partial_View (Priv_T);
20804 end if;
20805
20806 -- Create a full declaration for all its subtypes recorded in
20807 -- Private_Dependents and swap them similarly to the base type. These
20808 -- are subtypes that have been define before the full declaration of
20809 -- the private type. We also swap the entry in Private_Dependents list
20810 -- so we can properly restore the private view on exit from the scope.
20811
20812 declare
20813 Priv_Elmt : Elmt_Id;
20814 Priv_Scop : Entity_Id;
20815 Priv : Entity_Id;
20816 Full : Entity_Id;
20817
20818 begin
20819 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20820 while Present (Priv_Elmt) loop
20821 Priv := Node (Priv_Elmt);
20822 Priv_Scop := Scope (Priv);
20823
20824 if Ekind_In (Priv, E_Private_Subtype,
20825 E_Limited_Private_Subtype,
20826 E_Record_Subtype_With_Private)
20827 then
20828 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20829 Set_Is_Itype (Full);
20830 Set_Parent (Full, Parent (Priv));
20831 Set_Associated_Node_For_Itype (Full, N);
20832
20833 -- Now we need to complete the private subtype, but since the
20834 -- base type has already been swapped, we must also swap the
20835 -- subtypes (and thus, reverse the arguments in the call to
20836 -- Complete_Private_Subtype). Also note that we may need to
20837 -- re-establish the scope of the private subtype.
20838
20839 Copy_And_Swap (Priv, Full);
20840
20841 if not In_Open_Scopes (Priv_Scop) then
20842 Push_Scope (Priv_Scop);
20843
20844 else
20845 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20846
20847 Priv_Scop := Empty;
20848 end if;
20849
20850 Complete_Private_Subtype (Full, Priv, Full_T, N);
20851 Set_Full_View (Full, Priv);
20852
20853 if Present (Priv_Scop) then
20854 Pop_Scope;
20855 end if;
20856
20857 Replace_Elmt (Priv_Elmt, Full);
20858 end if;
20859
20860 Next_Elmt (Priv_Elmt);
20861 end loop;
20862 end;
20863
20864 -- If the private view was tagged, copy the new primitive operations
20865 -- from the private view to the full view.
20866
20867 if Is_Tagged_Type (Full_T) then
20868 declare
20869 Disp_Typ : Entity_Id;
20870 Full_List : Elist_Id;
20871 Prim : Entity_Id;
20872 Prim_Elmt : Elmt_Id;
20873 Priv_List : Elist_Id;
20874
20875 function Contains
20876 (E : Entity_Id;
20877 L : Elist_Id) return Boolean;
20878 -- Determine whether list L contains element E
20879
20880 --------------
20881 -- Contains --
20882 --------------
20883
20884 function Contains
20885 (E : Entity_Id;
20886 L : Elist_Id) return Boolean
20887 is
20888 List_Elmt : Elmt_Id;
20889
20890 begin
20891 List_Elmt := First_Elmt (L);
20892 while Present (List_Elmt) loop
20893 if Node (List_Elmt) = E then
20894 return True;
20895 end if;
20896
20897 Next_Elmt (List_Elmt);
20898 end loop;
20899
20900 return False;
20901 end Contains;
20902
20903 -- Start of processing
20904
20905 begin
20906 if Is_Tagged_Type (Priv_T) then
20907 Priv_List := Primitive_Operations (Priv_T);
20908 Prim_Elmt := First_Elmt (Priv_List);
20909
20910 -- In the case of a concurrent type completing a private tagged
20911 -- type, primitives may have been declared in between the two
20912 -- views. These subprograms need to be wrapped the same way
20913 -- entries and protected procedures are handled because they
20914 -- cannot be directly shared by the two views.
20915
20916 if Is_Concurrent_Type (Full_T) then
20917 declare
20918 Conc_Typ : constant Entity_Id :=
20919 Corresponding_Record_Type (Full_T);
20920 Curr_Nod : Node_Id := Parent (Conc_Typ);
20921 Wrap_Spec : Node_Id;
20922
20923 begin
20924 while Present (Prim_Elmt) loop
20925 Prim := Node (Prim_Elmt);
20926
20927 if Comes_From_Source (Prim)
20928 and then not Is_Abstract_Subprogram (Prim)
20929 then
20930 Wrap_Spec :=
20931 Make_Subprogram_Declaration (Sloc (Prim),
20932 Specification =>
20933 Build_Wrapper_Spec
20934 (Subp_Id => Prim,
20935 Obj_Typ => Conc_Typ,
20936 Formals =>
20937 Parameter_Specifications
20938 (Parent (Prim))));
20939
20940 Insert_After (Curr_Nod, Wrap_Spec);
20941 Curr_Nod := Wrap_Spec;
20942
20943 Analyze (Wrap_Spec);
20944
20945 -- Remove the wrapper from visibility to avoid
20946 -- spurious conflict with the wrapped entity.
20947
20948 Set_Is_Immediately_Visible
20949 (Defining_Entity (Specification (Wrap_Spec)),
20950 False);
20951 end if;
20952
20953 Next_Elmt (Prim_Elmt);
20954 end loop;
20955
20956 goto Leave;
20957 end;
20958
20959 -- For non-concurrent types, transfer explicit primitives, but
20960 -- omit those inherited from the parent of the private view
20961 -- since they will be re-inherited later on.
20962
20963 else
20964 Full_List := Primitive_Operations (Full_T);
20965 while Present (Prim_Elmt) loop
20966 Prim := Node (Prim_Elmt);
20967
20968 if Comes_From_Source (Prim)
20969 and then not Contains (Prim, Full_List)
20970 then
20971 Append_Elmt (Prim, Full_List);
20972 end if;
20973
20974 Next_Elmt (Prim_Elmt);
20975 end loop;
20976 end if;
20977
20978 -- Untagged private view
20979
20980 else
20981 Full_List := Primitive_Operations (Full_T);
20982
20983 -- In this case the partial view is untagged, so here we locate
20984 -- all of the earlier primitives that need to be treated as
20985 -- dispatching (those that appear between the two views). Note
20986 -- that these additional operations must all be new operations
20987 -- (any earlier operations that override inherited operations
20988 -- of the full view will already have been inserted in the
20989 -- primitives list, marked by Check_Operation_From_Private_View
20990 -- as dispatching. Note that implicit "/=" operators are
20991 -- excluded from being added to the primitives list since they
20992 -- shouldn't be treated as dispatching (tagged "/=" is handled
20993 -- specially).
20994
20995 Prim := Next_Entity (Full_T);
20996 while Present (Prim) and then Prim /= Priv_T loop
20997 if Ekind_In (Prim, E_Procedure, E_Function) then
20998 Disp_Typ := Find_Dispatching_Type (Prim);
20999
21000 if Disp_Typ = Full_T
21001 and then (Chars (Prim) /= Name_Op_Ne
21002 or else Comes_From_Source (Prim))
21003 then
21004 Check_Controlling_Formals (Full_T, Prim);
21005
21006 if Is_Suitable_Primitive (Prim)
21007 and then not Is_Dispatching_Operation (Prim)
21008 then
21009 Append_Elmt (Prim, Full_List);
21010 Set_Is_Dispatching_Operation (Prim);
21011 Set_DT_Position_Value (Prim, No_Uint);
21012 end if;
21013
21014 elsif Is_Dispatching_Operation (Prim)
21015 and then Disp_Typ /= Full_T
21016 then
21017 -- Verify that it is not otherwise controlled by a
21018 -- formal or a return value of type T.
21019
21020 Check_Controlling_Formals (Disp_Typ, Prim);
21021 end if;
21022 end if;
21023
21024 Next_Entity (Prim);
21025 end loop;
21026 end if;
21027
21028 -- For the tagged case, the two views can share the same primitive
21029 -- operations list and the same class-wide type. Update attributes
21030 -- of the class-wide type which depend on the full declaration.
21031
21032 if Is_Tagged_Type (Priv_T) then
21033 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21034 Set_Class_Wide_Type
21035 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21036
21037 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21038 end if;
21039 end;
21040 end if;
21041
21042 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21043
21044 if Known_To_Have_Preelab_Init (Priv_T) then
21045
21046 -- Case where there is a pragma Preelaborable_Initialization. We
21047 -- always allow this in predefined units, which is cheating a bit,
21048 -- but it means we don't have to struggle to meet the requirements in
21049 -- the RM for having Preelaborable Initialization. Otherwise we
21050 -- require that the type meets the RM rules. But we can't check that
21051 -- yet, because of the rule about overriding Initialize, so we simply
21052 -- set a flag that will be checked at freeze time.
21053
21054 if not In_Predefined_Unit (Full_T) then
21055 Set_Must_Have_Preelab_Init (Full_T);
21056 end if;
21057 end if;
21058
21059 -- If pragma CPP_Class was applied to the private type declaration,
21060 -- propagate it now to the full type declaration.
21061
21062 if Is_CPP_Class (Priv_T) then
21063 Set_Is_CPP_Class (Full_T);
21064 Set_Convention (Full_T, Convention_CPP);
21065
21066 -- Check that components of imported CPP types do not have default
21067 -- expressions.
21068
21069 Check_CPP_Type_Has_No_Defaults (Full_T);
21070 end if;
21071
21072 -- If the private view has user specified stream attributes, then so has
21073 -- the full view.
21074
21075 -- Why the test, how could these flags be already set in Full_T ???
21076
21077 if Has_Specified_Stream_Read (Priv_T) then
21078 Set_Has_Specified_Stream_Read (Full_T);
21079 end if;
21080
21081 if Has_Specified_Stream_Write (Priv_T) then
21082 Set_Has_Specified_Stream_Write (Full_T);
21083 end if;
21084
21085 if Has_Specified_Stream_Input (Priv_T) then
21086 Set_Has_Specified_Stream_Input (Full_T);
21087 end if;
21088
21089 if Has_Specified_Stream_Output (Priv_T) then
21090 Set_Has_Specified_Stream_Output (Full_T);
21091 end if;
21092
21093 -- Propagate Default_Initial_Condition-related attributes from the
21094 -- partial view to the full view and its base type.
21095
21096 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21097 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
21098
21099 -- Propagate invariant-related attributes from the partial view to the
21100 -- full view and its base type.
21101
21102 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21103 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
21104
21105 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21106 -- in the full view without advertising the inheritance in the partial
21107 -- view. This can only occur when the partial view has no parent type
21108 -- and the full view has an interface as a parent. Any other scenarios
21109 -- are illegal because implemented interfaces must match between the
21110 -- two views.
21111
21112 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21113 declare
21114 Full_Par : constant Entity_Id := Etype (Full_T);
21115 Priv_Par : constant Entity_Id := Etype (Priv_T);
21116
21117 begin
21118 if not Is_Interface (Priv_Par)
21119 and then Is_Interface (Full_Par)
21120 and then Has_Inheritable_Invariants (Full_Par)
21121 then
21122 Error_Msg_N
21123 ("hidden inheritance of class-wide type invariants not "
21124 & "allowed", N);
21125 end if;
21126 end;
21127 end if;
21128
21129 -- Propagate predicates to full type, and predicate function if already
21130 -- defined. It is not clear that this can actually happen? the partial
21131 -- view cannot be frozen yet, and the predicate function has not been
21132 -- built. Still it is a cheap check and seems safer to make it.
21133
21134 if Has_Predicates (Priv_T) then
21135 Set_Has_Predicates (Full_T);
21136
21137 if Present (Predicate_Function (Priv_T)) then
21138 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
21139 end if;
21140 end if;
21141
21142 <<Leave>>
21143 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21144 end Process_Full_View;
21145
21146 -----------------------------------
21147 -- Process_Incomplete_Dependents --
21148 -----------------------------------
21149
21150 procedure Process_Incomplete_Dependents
21151 (N : Node_Id;
21152 Full_T : Entity_Id;
21153 Inc_T : Entity_Id)
21154 is
21155 Inc_Elmt : Elmt_Id;
21156 Priv_Dep : Entity_Id;
21157 New_Subt : Entity_Id;
21158
21159 Disc_Constraint : Elist_Id;
21160
21161 begin
21162 if No (Private_Dependents (Inc_T)) then
21163 return;
21164 end if;
21165
21166 -- Itypes that may be generated by the completion of an incomplete
21167 -- subtype are not used by the back-end and not attached to the tree.
21168 -- They are created only for constraint-checking purposes.
21169
21170 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21171 while Present (Inc_Elmt) loop
21172 Priv_Dep := Node (Inc_Elmt);
21173
21174 if Ekind (Priv_Dep) = E_Subprogram_Type then
21175
21176 -- An Access_To_Subprogram type may have a return type or a
21177 -- parameter type that is incomplete. Replace with the full view.
21178
21179 if Etype (Priv_Dep) = Inc_T then
21180 Set_Etype (Priv_Dep, Full_T);
21181 end if;
21182
21183 declare
21184 Formal : Entity_Id;
21185
21186 begin
21187 Formal := First_Formal (Priv_Dep);
21188 while Present (Formal) loop
21189 if Etype (Formal) = Inc_T then
21190 Set_Etype (Formal, Full_T);
21191 end if;
21192
21193 Next_Formal (Formal);
21194 end loop;
21195 end;
21196
21197 elsif Is_Overloadable (Priv_Dep) then
21198
21199 -- If a subprogram in the incomplete dependents list is primitive
21200 -- for a tagged full type then mark it as a dispatching operation,
21201 -- check whether it overrides an inherited subprogram, and check
21202 -- restrictions on its controlling formals. Note that a protected
21203 -- operation is never dispatching: only its wrapper operation
21204 -- (which has convention Ada) is.
21205
21206 if Is_Tagged_Type (Full_T)
21207 and then Is_Primitive (Priv_Dep)
21208 and then Convention (Priv_Dep) /= Convention_Protected
21209 then
21210 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21211 Set_Is_Dispatching_Operation (Priv_Dep);
21212 Check_Controlling_Formals (Full_T, Priv_Dep);
21213 end if;
21214
21215 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21216
21217 -- Can happen during processing of a body before the completion
21218 -- of a TA type. Ignore, because spec is also on dependent list.
21219
21220 return;
21221
21222 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21223 -- corresponding subtype of the full view.
21224
21225 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21226 and then Comes_From_Source (Priv_Dep)
21227 then
21228 Set_Subtype_Indication
21229 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21230 Set_Etype (Priv_Dep, Full_T);
21231 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21232 Set_Analyzed (Parent (Priv_Dep), False);
21233
21234 -- Reanalyze the declaration, suppressing the call to Enter_Name
21235 -- to avoid duplicate names.
21236
21237 Analyze_Subtype_Declaration
21238 (N => Parent (Priv_Dep),
21239 Skip => True);
21240
21241 -- Dependent is a subtype
21242
21243 else
21244 -- We build a new subtype indication using the full view of the
21245 -- incomplete parent. The discriminant constraints have been
21246 -- elaborated already at the point of the subtype declaration.
21247
21248 New_Subt := Create_Itype (E_Void, N);
21249
21250 if Has_Discriminants (Full_T) then
21251 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21252 else
21253 Disc_Constraint := No_Elist;
21254 end if;
21255
21256 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21257 Set_Full_View (Priv_Dep, New_Subt);
21258 end if;
21259
21260 Next_Elmt (Inc_Elmt);
21261 end loop;
21262 end Process_Incomplete_Dependents;
21263
21264 --------------------------------
21265 -- Process_Range_Expr_In_Decl --
21266 --------------------------------
21267
21268 procedure Process_Range_Expr_In_Decl
21269 (R : Node_Id;
21270 T : Entity_Id;
21271 Subtyp : Entity_Id := Empty;
21272 Check_List : List_Id := Empty_List;
21273 R_Check_Off : Boolean := False;
21274 In_Iter_Schm : Boolean := False)
21275 is
21276 Lo, Hi : Node_Id;
21277 R_Checks : Check_Result;
21278 Insert_Node : Node_Id;
21279 Def_Id : Entity_Id;
21280
21281 begin
21282 Analyze_And_Resolve (R, Base_Type (T));
21283
21284 if Nkind (R) = N_Range then
21285
21286 -- In SPARK, all ranges should be static, with the exception of the
21287 -- discrete type definition of a loop parameter specification.
21288
21289 if not In_Iter_Schm
21290 and then not Is_OK_Static_Range (R)
21291 then
21292 Check_SPARK_05_Restriction ("range should be static", R);
21293 end if;
21294
21295 Lo := Low_Bound (R);
21296 Hi := High_Bound (R);
21297
21298 -- Validity checks on the range of a quantified expression are
21299 -- delayed until the construct is transformed into a loop.
21300
21301 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21302 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21303 then
21304 null;
21305
21306 -- We need to ensure validity of the bounds here, because if we
21307 -- go ahead and do the expansion, then the expanded code will get
21308 -- analyzed with range checks suppressed and we miss the check.
21309
21310 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21311 -- the temporaries generated by routine Remove_Side_Effects by means
21312 -- of validity checks must use the same names. When a range appears
21313 -- in the parent of a generic, the range is processed with checks
21314 -- disabled as part of the generic context and with checks enabled
21315 -- for code generation purposes. This leads to link issues as the
21316 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21317 -- template sees the temporaries generated by Remove_Side_Effects.
21318
21319 else
21320 Validity_Check_Range (R, Subtyp);
21321 end if;
21322
21323 -- If there were errors in the declaration, try and patch up some
21324 -- common mistakes in the bounds. The cases handled are literals
21325 -- which are Integer where the expected type is Real and vice versa.
21326 -- These corrections allow the compilation process to proceed further
21327 -- along since some basic assumptions of the format of the bounds
21328 -- are guaranteed.
21329
21330 if Etype (R) = Any_Type then
21331 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21332 Rewrite (Lo,
21333 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21334
21335 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21336 Rewrite (Hi,
21337 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21338
21339 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21340 Rewrite (Lo,
21341 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21342
21343 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21344 Rewrite (Hi,
21345 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21346 end if;
21347
21348 Set_Etype (Lo, T);
21349 Set_Etype (Hi, T);
21350 end if;
21351
21352 -- If the bounds of the range have been mistakenly given as string
21353 -- literals (perhaps in place of character literals), then an error
21354 -- has already been reported, but we rewrite the string literal as a
21355 -- bound of the range's type to avoid blowups in later processing
21356 -- that looks at static values.
21357
21358 if Nkind (Lo) = N_String_Literal then
21359 Rewrite (Lo,
21360 Make_Attribute_Reference (Sloc (Lo),
21361 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21362 Attribute_Name => Name_First));
21363 Analyze_And_Resolve (Lo);
21364 end if;
21365
21366 if Nkind (Hi) = N_String_Literal then
21367 Rewrite (Hi,
21368 Make_Attribute_Reference (Sloc (Hi),
21369 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21370 Attribute_Name => Name_First));
21371 Analyze_And_Resolve (Hi);
21372 end if;
21373
21374 -- If bounds aren't scalar at this point then exit, avoiding
21375 -- problems with further processing of the range in this procedure.
21376
21377 if not Is_Scalar_Type (Etype (Lo)) then
21378 return;
21379 end if;
21380
21381 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21382 -- then range of the base type. Here we check whether the bounds
21383 -- are in the range of the subtype itself. Note that if the bounds
21384 -- represent the null range the Constraint_Error exception should
21385 -- not be raised.
21386
21387 -- ??? The following code should be cleaned up as follows
21388
21389 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21390 -- is done in the call to Range_Check (R, T); below
21391
21392 -- 2. The use of R_Check_Off should be investigated and possibly
21393 -- removed, this would clean up things a bit.
21394
21395 if Is_Null_Range (Lo, Hi) then
21396 null;
21397
21398 else
21399 -- Capture values of bounds and generate temporaries for them
21400 -- if needed, before applying checks, since checks may cause
21401 -- duplication of the expression without forcing evaluation.
21402
21403 -- The forced evaluation removes side effects from expressions,
21404 -- which should occur also in GNATprove mode. Otherwise, we end up
21405 -- with unexpected insertions of actions at places where this is
21406 -- not supposed to occur, e.g. on default parameters of a call.
21407
21408 if Expander_Active or GNATprove_Mode then
21409
21410 -- Call Force_Evaluation to create declarations as needed to
21411 -- deal with side effects, and also create typ_FIRST/LAST
21412 -- entities for bounds if we have a subtype name.
21413
21414 -- Note: we do this transformation even if expansion is not
21415 -- active if we are in GNATprove_Mode since the transformation
21416 -- is in general required to ensure that the resulting tree has
21417 -- proper Ada semantics.
21418
21419 Force_Evaluation
21420 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21421 Force_Evaluation
21422 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21423 end if;
21424
21425 -- We use a flag here instead of suppressing checks on the type
21426 -- because the type we check against isn't necessarily the place
21427 -- where we put the check.
21428
21429 if not R_Check_Off then
21430 R_Checks := Get_Range_Checks (R, T);
21431
21432 -- Look up tree to find an appropriate insertion point. We
21433 -- can't just use insert_actions because later processing
21434 -- depends on the insertion node. Prior to Ada 2012 the
21435 -- insertion point could only be a declaration or a loop, but
21436 -- quantified expressions can appear within any context in an
21437 -- expression, and the insertion point can be any statement,
21438 -- pragma, or declaration.
21439
21440 Insert_Node := Parent (R);
21441 while Present (Insert_Node) loop
21442 exit when
21443 Nkind (Insert_Node) in N_Declaration
21444 and then
21445 not Nkind_In
21446 (Insert_Node, N_Component_Declaration,
21447 N_Loop_Parameter_Specification,
21448 N_Function_Specification,
21449 N_Procedure_Specification);
21450
21451 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21452 or else Nkind (Insert_Node) in
21453 N_Statement_Other_Than_Procedure_Call
21454 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21455 N_Pragma);
21456
21457 Insert_Node := Parent (Insert_Node);
21458 end loop;
21459
21460 -- Why would Type_Decl not be present??? Without this test,
21461 -- short regression tests fail.
21462
21463 if Present (Insert_Node) then
21464
21465 -- Case of loop statement. Verify that the range is part
21466 -- of the subtype indication of the iteration scheme.
21467
21468 if Nkind (Insert_Node) = N_Loop_Statement then
21469 declare
21470 Indic : Node_Id;
21471
21472 begin
21473 Indic := Parent (R);
21474 while Present (Indic)
21475 and then Nkind (Indic) /= N_Subtype_Indication
21476 loop
21477 Indic := Parent (Indic);
21478 end loop;
21479
21480 if Present (Indic) then
21481 Def_Id := Etype (Subtype_Mark (Indic));
21482
21483 Insert_Range_Checks
21484 (R_Checks,
21485 Insert_Node,
21486 Def_Id,
21487 Sloc (Insert_Node),
21488 R,
21489 Do_Before => True);
21490 end if;
21491 end;
21492
21493 -- Insertion before a declaration. If the declaration
21494 -- includes discriminants, the list of applicable checks
21495 -- is given by the caller.
21496
21497 elsif Nkind (Insert_Node) in N_Declaration then
21498 Def_Id := Defining_Identifier (Insert_Node);
21499
21500 if (Ekind (Def_Id) = E_Record_Type
21501 and then Depends_On_Discriminant (R))
21502 or else
21503 (Ekind (Def_Id) = E_Protected_Type
21504 and then Has_Discriminants (Def_Id))
21505 then
21506 Append_Range_Checks
21507 (R_Checks,
21508 Check_List, Def_Id, Sloc (Insert_Node), R);
21509
21510 else
21511 Insert_Range_Checks
21512 (R_Checks,
21513 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21514
21515 end if;
21516
21517 -- Insertion before a statement. Range appears in the
21518 -- context of a quantified expression. Insertion will
21519 -- take place when expression is expanded.
21520
21521 else
21522 null;
21523 end if;
21524 end if;
21525 end if;
21526 end if;
21527
21528 -- Case of other than an explicit N_Range node
21529
21530 -- The forced evaluation removes side effects from expressions, which
21531 -- should occur also in GNATprove mode. Otherwise, we end up with
21532 -- unexpected insertions of actions at places where this is not
21533 -- supposed to occur, e.g. on default parameters of a call.
21534
21535 elsif Expander_Active or GNATprove_Mode then
21536 Get_Index_Bounds (R, Lo, Hi);
21537 Force_Evaluation (Lo);
21538 Force_Evaluation (Hi);
21539 end if;
21540 end Process_Range_Expr_In_Decl;
21541
21542 --------------------------------------
21543 -- Process_Real_Range_Specification --
21544 --------------------------------------
21545
21546 procedure Process_Real_Range_Specification (Def : Node_Id) is
21547 Spec : constant Node_Id := Real_Range_Specification (Def);
21548 Lo : Node_Id;
21549 Hi : Node_Id;
21550 Err : Boolean := False;
21551
21552 procedure Analyze_Bound (N : Node_Id);
21553 -- Analyze and check one bound
21554
21555 -------------------
21556 -- Analyze_Bound --
21557 -------------------
21558
21559 procedure Analyze_Bound (N : Node_Id) is
21560 begin
21561 Analyze_And_Resolve (N, Any_Real);
21562
21563 if not Is_OK_Static_Expression (N) then
21564 Flag_Non_Static_Expr
21565 ("bound in real type definition is not static!", N);
21566 Err := True;
21567 end if;
21568 end Analyze_Bound;
21569
21570 -- Start of processing for Process_Real_Range_Specification
21571
21572 begin
21573 if Present (Spec) then
21574 Lo := Low_Bound (Spec);
21575 Hi := High_Bound (Spec);
21576 Analyze_Bound (Lo);
21577 Analyze_Bound (Hi);
21578
21579 -- If error, clear away junk range specification
21580
21581 if Err then
21582 Set_Real_Range_Specification (Def, Empty);
21583 end if;
21584 end if;
21585 end Process_Real_Range_Specification;
21586
21587 ---------------------
21588 -- Process_Subtype --
21589 ---------------------
21590
21591 function Process_Subtype
21592 (S : Node_Id;
21593 Related_Nod : Node_Id;
21594 Related_Id : Entity_Id := Empty;
21595 Suffix : Character := ' ') return Entity_Id
21596 is
21597 P : Node_Id;
21598 Def_Id : Entity_Id;
21599 Error_Node : Node_Id;
21600 Full_View_Id : Entity_Id;
21601 Subtype_Mark_Id : Entity_Id;
21602
21603 May_Have_Null_Exclusion : Boolean;
21604
21605 procedure Check_Incomplete (T : Node_Id);
21606 -- Called to verify that an incomplete type is not used prematurely
21607
21608 ----------------------
21609 -- Check_Incomplete --
21610 ----------------------
21611
21612 procedure Check_Incomplete (T : Node_Id) is
21613 begin
21614 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21615
21616 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21617 and then
21618 not (Ada_Version >= Ada_2005
21619 and then
21620 (Nkind (Parent (T)) = N_Subtype_Declaration
21621 or else (Nkind (Parent (T)) = N_Subtype_Indication
21622 and then Nkind (Parent (Parent (T))) =
21623 N_Subtype_Declaration)))
21624 then
21625 Error_Msg_N ("invalid use of type before its full declaration", T);
21626 end if;
21627 end Check_Incomplete;
21628
21629 -- Start of processing for Process_Subtype
21630
21631 begin
21632 -- Case of no constraints present
21633
21634 if Nkind (S) /= N_Subtype_Indication then
21635 Find_Type (S);
21636
21637 -- No way to proceed if the subtype indication is malformed. This
21638 -- will happen for example when the subtype indication in an object
21639 -- declaration is missing altogether and the expression is analyzed
21640 -- as if it were that indication.
21641
21642 if not Is_Entity_Name (S) then
21643 return Any_Type;
21644 end if;
21645
21646 Check_Incomplete (S);
21647 P := Parent (S);
21648
21649 -- Ada 2005 (AI-231): Static check
21650
21651 if Ada_Version >= Ada_2005
21652 and then Present (P)
21653 and then Null_Exclusion_Present (P)
21654 and then Nkind (P) /= N_Access_To_Object_Definition
21655 and then not Is_Access_Type (Entity (S))
21656 then
21657 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21658 end if;
21659
21660 -- The following is ugly, can't we have a range or even a flag???
21661
21662 May_Have_Null_Exclusion :=
21663 Nkind_In (P, N_Access_Definition,
21664 N_Access_Function_Definition,
21665 N_Access_Procedure_Definition,
21666 N_Access_To_Object_Definition,
21667 N_Allocator,
21668 N_Component_Definition)
21669 or else
21670 Nkind_In (P, N_Derived_Type_Definition,
21671 N_Discriminant_Specification,
21672 N_Formal_Object_Declaration,
21673 N_Object_Declaration,
21674 N_Object_Renaming_Declaration,
21675 N_Parameter_Specification,
21676 N_Subtype_Declaration);
21677
21678 -- Create an Itype that is a duplicate of Entity (S) but with the
21679 -- null-exclusion attribute.
21680
21681 if May_Have_Null_Exclusion
21682 and then Is_Access_Type (Entity (S))
21683 and then Null_Exclusion_Present (P)
21684
21685 -- No need to check the case of an access to object definition.
21686 -- It is correct to define double not-null pointers.
21687
21688 -- Example:
21689 -- type Not_Null_Int_Ptr is not null access Integer;
21690 -- type Acc is not null access Not_Null_Int_Ptr;
21691
21692 and then Nkind (P) /= N_Access_To_Object_Definition
21693 then
21694 if Can_Never_Be_Null (Entity (S)) then
21695 case Nkind (Related_Nod) is
21696 when N_Full_Type_Declaration =>
21697 if Nkind (Type_Definition (Related_Nod))
21698 in N_Array_Type_Definition
21699 then
21700 Error_Node :=
21701 Subtype_Indication
21702 (Component_Definition
21703 (Type_Definition (Related_Nod)));
21704 else
21705 Error_Node :=
21706 Subtype_Indication (Type_Definition (Related_Nod));
21707 end if;
21708
21709 when N_Subtype_Declaration =>
21710 Error_Node := Subtype_Indication (Related_Nod);
21711
21712 when N_Object_Declaration =>
21713 Error_Node := Object_Definition (Related_Nod);
21714
21715 when N_Component_Declaration =>
21716 Error_Node :=
21717 Subtype_Indication (Component_Definition (Related_Nod));
21718
21719 when N_Allocator =>
21720 Error_Node := Expression (Related_Nod);
21721
21722 when others =>
21723 pragma Assert (False);
21724 Error_Node := Related_Nod;
21725 end case;
21726
21727 Error_Msg_NE
21728 ("`NOT NULL` not allowed (& already excludes null)",
21729 Error_Node,
21730 Entity (S));
21731 end if;
21732
21733 Set_Etype (S,
21734 Create_Null_Excluding_Itype
21735 (T => Entity (S),
21736 Related_Nod => P));
21737 Set_Entity (S, Etype (S));
21738 end if;
21739
21740 return Entity (S);
21741
21742 -- Case of constraint present, so that we have an N_Subtype_Indication
21743 -- node (this node is created only if constraints are present).
21744
21745 else
21746 Find_Type (Subtype_Mark (S));
21747
21748 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21749 and then not
21750 (Nkind (Parent (S)) = N_Subtype_Declaration
21751 and then Is_Itype (Defining_Identifier (Parent (S))))
21752 then
21753 Check_Incomplete (Subtype_Mark (S));
21754 end if;
21755
21756 P := Parent (S);
21757 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21758
21759 -- Explicit subtype declaration case
21760
21761 if Nkind (P) = N_Subtype_Declaration then
21762 Def_Id := Defining_Identifier (P);
21763
21764 -- Explicit derived type definition case
21765
21766 elsif Nkind (P) = N_Derived_Type_Definition then
21767 Def_Id := Defining_Identifier (Parent (P));
21768
21769 -- Implicit case, the Def_Id must be created as an implicit type.
21770 -- The one exception arises in the case of concurrent types, array
21771 -- and access types, where other subsidiary implicit types may be
21772 -- created and must appear before the main implicit type. In these
21773 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21774 -- has not yet been called to create Def_Id.
21775
21776 else
21777 if Is_Array_Type (Subtype_Mark_Id)
21778 or else Is_Concurrent_Type (Subtype_Mark_Id)
21779 or else Is_Access_Type (Subtype_Mark_Id)
21780 then
21781 Def_Id := Empty;
21782
21783 -- For the other cases, we create a new unattached Itype,
21784 -- and set the indication to ensure it gets attached later.
21785
21786 else
21787 Def_Id :=
21788 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21789 end if;
21790 end if;
21791
21792 -- If the kind of constraint is invalid for this kind of type,
21793 -- then give an error, and then pretend no constraint was given.
21794
21795 if not Is_Valid_Constraint_Kind
21796 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21797 then
21798 Error_Msg_N
21799 ("incorrect constraint for this kind of type", Constraint (S));
21800
21801 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21802
21803 -- Set Ekind of orphan itype, to prevent cascaded errors
21804
21805 if Present (Def_Id) then
21806 Set_Ekind (Def_Id, Ekind (Any_Type));
21807 end if;
21808
21809 -- Make recursive call, having got rid of the bogus constraint
21810
21811 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21812 end if;
21813
21814 -- Remaining processing depends on type. Select on Base_Type kind to
21815 -- ensure getting to the concrete type kind in the case of a private
21816 -- subtype (needed when only doing semantic analysis).
21817
21818 case Ekind (Base_Type (Subtype_Mark_Id)) is
21819 when Access_Kind =>
21820
21821 -- If this is a constraint on a class-wide type, discard it.
21822 -- There is currently no way to express a partial discriminant
21823 -- constraint on a type with unknown discriminants. This is
21824 -- a pathology that the ACATS wisely decides not to test.
21825
21826 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21827 if Comes_From_Source (S) then
21828 Error_Msg_N
21829 ("constraint on class-wide type ignored??",
21830 Constraint (S));
21831 end if;
21832
21833 if Nkind (P) = N_Subtype_Declaration then
21834 Set_Subtype_Indication (P,
21835 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21836 end if;
21837
21838 return Subtype_Mark_Id;
21839 end if;
21840
21841 Constrain_Access (Def_Id, S, Related_Nod);
21842
21843 if Expander_Active
21844 and then Is_Itype (Designated_Type (Def_Id))
21845 and then Nkind (Related_Nod) = N_Subtype_Declaration
21846 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21847 then
21848 Build_Itype_Reference
21849 (Designated_Type (Def_Id), Related_Nod);
21850 end if;
21851
21852 when Array_Kind =>
21853 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21854
21855 when Decimal_Fixed_Point_Kind =>
21856 Constrain_Decimal (Def_Id, S);
21857
21858 when Enumeration_Kind =>
21859 Constrain_Enumeration (Def_Id, S);
21860
21861 when Ordinary_Fixed_Point_Kind =>
21862 Constrain_Ordinary_Fixed (Def_Id, S);
21863
21864 when Float_Kind =>
21865 Constrain_Float (Def_Id, S);
21866
21867 when Integer_Kind =>
21868 Constrain_Integer (Def_Id, S);
21869
21870 when Class_Wide_Kind
21871 | E_Incomplete_Type
21872 | E_Record_Subtype
21873 | E_Record_Type
21874 =>
21875 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21876
21877 if Ekind (Def_Id) = E_Incomplete_Type then
21878 Set_Private_Dependents (Def_Id, New_Elmt_List);
21879 end if;
21880
21881 when Private_Kind =>
21882
21883 -- A private type with unknown discriminants may be completed
21884 -- by an unconstrained array type.
21885
21886 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21887 and then Present (Full_View (Subtype_Mark_Id))
21888 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21889 then
21890 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21891
21892 -- ... but more commonly is completed by a discriminated record
21893 -- type.
21894
21895 else
21896 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21897 end if;
21898
21899 -- The base type may be private but Def_Id may be a full view
21900 -- in an instance.
21901
21902 if Is_Private_Type (Def_Id) then
21903 Set_Private_Dependents (Def_Id, New_Elmt_List);
21904 end if;
21905
21906 -- In case of an invalid constraint prevent further processing
21907 -- since the type constructed is missing expected fields.
21908
21909 if Etype (Def_Id) = Any_Type then
21910 return Def_Id;
21911 end if;
21912
21913 -- If the full view is that of a task with discriminants,
21914 -- we must constrain both the concurrent type and its
21915 -- corresponding record type. Otherwise we will just propagate
21916 -- the constraint to the full view, if available.
21917
21918 if Present (Full_View (Subtype_Mark_Id))
21919 and then Has_Discriminants (Subtype_Mark_Id)
21920 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21921 then
21922 Full_View_Id :=
21923 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21924
21925 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21926 Constrain_Concurrent (Full_View_Id, S,
21927 Related_Nod, Related_Id, Suffix);
21928 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21929 Set_Full_View (Def_Id, Full_View_Id);
21930
21931 -- Introduce an explicit reference to the private subtype,
21932 -- to prevent scope anomalies in gigi if first use appears
21933 -- in a nested context, e.g. a later function body.
21934 -- Should this be generated in other contexts than a full
21935 -- type declaration?
21936
21937 if Is_Itype (Def_Id)
21938 and then
21939 Nkind (Parent (P)) = N_Full_Type_Declaration
21940 then
21941 Build_Itype_Reference (Def_Id, Parent (P));
21942 end if;
21943
21944 else
21945 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21946 end if;
21947
21948 when Concurrent_Kind =>
21949 Constrain_Concurrent (Def_Id, S,
21950 Related_Nod, Related_Id, Suffix);
21951
21952 when others =>
21953 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21954 end case;
21955
21956 -- Size, Alignment, Representation aspects and Convention are always
21957 -- inherited from the base type.
21958
21959 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21960 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21961 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21962
21963 -- The anonymous subtype created for the subtype indication
21964 -- inherits the predicates of the parent.
21965
21966 if Has_Predicates (Subtype_Mark_Id) then
21967 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21968
21969 -- Indicate where the predicate function may be found
21970
21971 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21972 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21973 end if;
21974 end if;
21975
21976 return Def_Id;
21977 end if;
21978 end Process_Subtype;
21979
21980 -----------------------------
21981 -- Record_Type_Declaration --
21982 -----------------------------
21983
21984 procedure Record_Type_Declaration
21985 (T : Entity_Id;
21986 N : Node_Id;
21987 Prev : Entity_Id)
21988 is
21989 Def : constant Node_Id := Type_Definition (N);
21990 Is_Tagged : Boolean;
21991 Tag_Comp : Entity_Id;
21992
21993 begin
21994 -- These flags must be initialized before calling Process_Discriminants
21995 -- because this routine makes use of them.
21996
21997 Set_Ekind (T, E_Record_Type);
21998 Set_Etype (T, T);
21999 Init_Size_Align (T);
22000 Set_Interfaces (T, No_Elist);
22001 Set_Stored_Constraint (T, No_Elist);
22002 Set_Default_SSO (T);
22003 Set_No_Reordering (T, No_Component_Reordering);
22004
22005 -- Normal case
22006
22007 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22008 if Limited_Present (Def) then
22009 Check_SPARK_05_Restriction ("limited is not allowed", N);
22010 end if;
22011
22012 if Abstract_Present (Def) then
22013 Check_SPARK_05_Restriction ("abstract is not allowed", N);
22014 end if;
22015
22016 -- The flag Is_Tagged_Type might have already been set by
22017 -- Find_Type_Name if it detected an error for declaration T. This
22018 -- arises in the case of private tagged types where the full view
22019 -- omits the word tagged.
22020
22021 Is_Tagged :=
22022 Tagged_Present (Def)
22023 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22024
22025 Set_Is_Limited_Record (T, Limited_Present (Def));
22026
22027 if Is_Tagged then
22028 Set_Is_Tagged_Type (T, True);
22029 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22030 end if;
22031
22032 -- Type is abstract if full declaration carries keyword, or if
22033 -- previous partial view did.
22034
22035 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22036 or else Abstract_Present (Def));
22037
22038 else
22039 Check_SPARK_05_Restriction ("interface is not allowed", N);
22040
22041 Is_Tagged := True;
22042 Analyze_Interface_Declaration (T, Def);
22043
22044 if Present (Discriminant_Specifications (N)) then
22045 Error_Msg_N
22046 ("interface types cannot have discriminants",
22047 Defining_Identifier
22048 (First (Discriminant_Specifications (N))));
22049 end if;
22050 end if;
22051
22052 -- First pass: if there are self-referential access components,
22053 -- create the required anonymous access type declarations, and if
22054 -- need be an incomplete type declaration for T itself.
22055
22056 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22057
22058 if Ada_Version >= Ada_2005
22059 and then Present (Interface_List (Def))
22060 then
22061 Check_Interfaces (N, Def);
22062
22063 declare
22064 Ifaces_List : Elist_Id;
22065
22066 begin
22067 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22068 -- already in the parents.
22069
22070 Collect_Interfaces
22071 (T => T,
22072 Ifaces_List => Ifaces_List,
22073 Exclude_Parents => True);
22074
22075 Set_Interfaces (T, Ifaces_List);
22076 end;
22077 end if;
22078
22079 -- Records constitute a scope for the component declarations within.
22080 -- The scope is created prior to the processing of these declarations.
22081 -- Discriminants are processed first, so that they are visible when
22082 -- processing the other components. The Ekind of the record type itself
22083 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22084
22085 -- Enter record scope
22086
22087 Push_Scope (T);
22088
22089 -- If an incomplete or private type declaration was already given for
22090 -- the type, then this scope already exists, and the discriminants have
22091 -- been declared within. We must verify that the full declaration
22092 -- matches the incomplete one.
22093
22094 Check_Or_Process_Discriminants (N, T, Prev);
22095
22096 Set_Is_Constrained (T, not Has_Discriminants (T));
22097 Set_Has_Delayed_Freeze (T, True);
22098
22099 -- For tagged types add a manually analyzed component corresponding
22100 -- to the component _tag, the corresponding piece of tree will be
22101 -- expanded as part of the freezing actions if it is not a CPP_Class.
22102
22103 if Is_Tagged then
22104
22105 -- Do not add the tag unless we are in expansion mode
22106
22107 if Expander_Active then
22108 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22109 Enter_Name (Tag_Comp);
22110
22111 Set_Ekind (Tag_Comp, E_Component);
22112 Set_Is_Tag (Tag_Comp);
22113 Set_Is_Aliased (Tag_Comp);
22114 Set_Is_Independent (Tag_Comp);
22115 Set_Etype (Tag_Comp, RTE (RE_Tag));
22116 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22117 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22118 Init_Component_Location (Tag_Comp);
22119
22120 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22121 -- implemented interfaces.
22122
22123 if Has_Interfaces (T) then
22124 Add_Interface_Tag_Components (N, T);
22125 end if;
22126 end if;
22127
22128 Make_Class_Wide_Type (T);
22129 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22130 end if;
22131
22132 -- We must suppress range checks when processing record components in
22133 -- the presence of discriminants, since we don't want spurious checks to
22134 -- be generated during their analysis, but Suppress_Range_Checks flags
22135 -- must be reset the after processing the record definition.
22136
22137 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22138 -- couldn't we just use the normal range check suppression method here.
22139 -- That would seem cleaner ???
22140
22141 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22142 Set_Kill_Range_Checks (T, True);
22143 Record_Type_Definition (Def, Prev);
22144 Set_Kill_Range_Checks (T, False);
22145 else
22146 Record_Type_Definition (Def, Prev);
22147 end if;
22148
22149 -- Exit from record scope
22150
22151 End_Scope;
22152
22153 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22154 -- the implemented interfaces and associate them an aliased entity.
22155
22156 if Is_Tagged
22157 and then not Is_Empty_List (Interface_List (Def))
22158 then
22159 Derive_Progenitor_Subprograms (T, T);
22160 end if;
22161
22162 Check_Function_Writable_Actuals (N);
22163 end Record_Type_Declaration;
22164
22165 ----------------------------
22166 -- Record_Type_Definition --
22167 ----------------------------
22168
22169 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22170 Component : Entity_Id;
22171 Ctrl_Components : Boolean := False;
22172 Final_Storage_Only : Boolean;
22173 T : Entity_Id;
22174
22175 begin
22176 if Ekind (Prev_T) = E_Incomplete_Type then
22177 T := Full_View (Prev_T);
22178 else
22179 T := Prev_T;
22180 end if;
22181
22182 -- In SPARK, tagged types and type extensions may only be declared in
22183 -- the specification of library unit packages.
22184
22185 if Present (Def) and then Is_Tagged_Type (T) then
22186 declare
22187 Typ : Node_Id;
22188 Ctxt : Node_Id;
22189
22190 begin
22191 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22192 Typ := Parent (Def);
22193 else
22194 pragma Assert
22195 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22196 Typ := Parent (Parent (Def));
22197 end if;
22198
22199 Ctxt := Parent (Typ);
22200
22201 if Nkind (Ctxt) = N_Package_Body
22202 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22203 then
22204 Check_SPARK_05_Restriction
22205 ("type should be defined in package specification", Typ);
22206
22207 elsif Nkind (Ctxt) /= N_Package_Specification
22208 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22209 then
22210 Check_SPARK_05_Restriction
22211 ("type should be defined in library unit package", Typ);
22212 end if;
22213 end;
22214 end if;
22215
22216 Final_Storage_Only := not Is_Controlled (T);
22217
22218 -- Ada 2005: Check whether an explicit Limited is present in a derived
22219 -- type declaration.
22220
22221 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22222 and then Limited_Present (Parent (Def))
22223 then
22224 Set_Is_Limited_Record (T);
22225 end if;
22226
22227 -- If the component list of a record type is defined by the reserved
22228 -- word null and there is no discriminant part, then the record type has
22229 -- no components and all records of the type are null records (RM 3.7)
22230 -- This procedure is also called to process the extension part of a
22231 -- record extension, in which case the current scope may have inherited
22232 -- components.
22233
22234 if No (Def)
22235 or else No (Component_List (Def))
22236 or else Null_Present (Component_List (Def))
22237 then
22238 if not Is_Tagged_Type (T) then
22239 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22240 end if;
22241
22242 else
22243 Analyze_Declarations (Component_Items (Component_List (Def)));
22244
22245 if Present (Variant_Part (Component_List (Def))) then
22246 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22247 Analyze (Variant_Part (Component_List (Def)));
22248 end if;
22249 end if;
22250
22251 -- After completing the semantic analysis of the record definition,
22252 -- record components, both new and inherited, are accessible. Set their
22253 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22254 -- whose Ekind may be void.
22255
22256 Component := First_Entity (Current_Scope);
22257 while Present (Component) loop
22258 if Ekind (Component) = E_Void
22259 and then not Is_Itype (Component)
22260 then
22261 Set_Ekind (Component, E_Component);
22262 Init_Component_Location (Component);
22263 end if;
22264
22265 Propagate_Concurrent_Flags (T, Etype (Component));
22266
22267 if Ekind (Component) /= E_Component then
22268 null;
22269
22270 -- Do not set Has_Controlled_Component on a class-wide equivalent
22271 -- type. See Make_CW_Equivalent_Type.
22272
22273 elsif not Is_Class_Wide_Equivalent_Type (T)
22274 and then (Has_Controlled_Component (Etype (Component))
22275 or else (Chars (Component) /= Name_uParent
22276 and then Is_Controlled (Etype (Component))))
22277 then
22278 Set_Has_Controlled_Component (T, True);
22279 Final_Storage_Only :=
22280 Final_Storage_Only
22281 and then Finalize_Storage_Only (Etype (Component));
22282 Ctrl_Components := True;
22283 end if;
22284
22285 Next_Entity (Component);
22286 end loop;
22287
22288 -- A Type is Finalize_Storage_Only only if all its controlled components
22289 -- are also.
22290
22291 if Ctrl_Components then
22292 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22293 end if;
22294
22295 -- Place reference to end record on the proper entity, which may
22296 -- be a partial view.
22297
22298 if Present (Def) then
22299 Process_End_Label (Def, 'e', Prev_T);
22300 end if;
22301 end Record_Type_Definition;
22302
22303 ---------------------------
22304 -- Replace_Discriminants --
22305 ---------------------------
22306
22307 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22308 function Process (N : Node_Id) return Traverse_Result;
22309
22310 -------------
22311 -- Process --
22312 -------------
22313
22314 function Process (N : Node_Id) return Traverse_Result is
22315 Comp : Entity_Id;
22316
22317 begin
22318 if Nkind (N) = N_Discriminant_Specification then
22319 Comp := First_Discriminant (Typ);
22320 while Present (Comp) loop
22321 if Original_Record_Component (Comp) = Defining_Identifier (N)
22322 or else Chars (Comp) = Chars (Defining_Identifier (N))
22323 then
22324 Set_Defining_Identifier (N, Comp);
22325 exit;
22326 end if;
22327
22328 Next_Discriminant (Comp);
22329 end loop;
22330
22331 elsif Nkind (N) = N_Variant_Part then
22332 Comp := First_Discriminant (Typ);
22333 while Present (Comp) loop
22334 if Original_Record_Component (Comp) = Entity (Name (N))
22335 or else Chars (Comp) = Chars (Name (N))
22336 then
22337 -- Make sure to preserve the type coming from the parent on
22338 -- the Name, even if the subtype of the discriminant can be
22339 -- constrained, so that discrete choices inherited from the
22340 -- parent in the variant part are not flagged as violating
22341 -- the constraints of the subtype.
22342
22343 declare
22344 Typ : constant Entity_Id := Etype (Name (N));
22345 begin
22346 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22347 Set_Etype (Name (N), Typ);
22348 end;
22349 exit;
22350 end if;
22351
22352 Next_Discriminant (Comp);
22353 end loop;
22354 end if;
22355
22356 return OK;
22357 end Process;
22358
22359 procedure Replace is new Traverse_Proc (Process);
22360
22361 -- Start of processing for Replace_Discriminants
22362
22363 begin
22364 Replace (Decl);
22365 end Replace_Discriminants;
22366
22367 -------------------------------
22368 -- Set_Completion_Referenced --
22369 -------------------------------
22370
22371 procedure Set_Completion_Referenced (E : Entity_Id) is
22372 begin
22373 -- If in main unit, mark entity that is a completion as referenced,
22374 -- warnings go on the partial view when needed.
22375
22376 if In_Extended_Main_Source_Unit (E) then
22377 Set_Referenced (E);
22378 end if;
22379 end Set_Completion_Referenced;
22380
22381 ---------------------
22382 -- Set_Default_SSO --
22383 ---------------------
22384
22385 procedure Set_Default_SSO (T : Entity_Id) is
22386 begin
22387 case Opt.Default_SSO is
22388 when ' ' =>
22389 null;
22390 when 'L' =>
22391 Set_SSO_Set_Low_By_Default (T, True);
22392 when 'H' =>
22393 Set_SSO_Set_High_By_Default (T, True);
22394 when others =>
22395 raise Program_Error;
22396 end case;
22397 end Set_Default_SSO;
22398
22399 ---------------------
22400 -- Set_Fixed_Range --
22401 ---------------------
22402
22403 -- The range for fixed-point types is complicated by the fact that we
22404 -- do not know the exact end points at the time of the declaration. This
22405 -- is true for three reasons:
22406
22407 -- A size clause may affect the fudging of the end-points.
22408 -- A small clause may affect the values of the end-points.
22409 -- We try to include the end-points if it does not affect the size.
22410
22411 -- This means that the actual end-points must be established at the
22412 -- point when the type is frozen. Meanwhile, we first narrow the range
22413 -- as permitted (so that it will fit if necessary in a small specified
22414 -- size), and then build a range subtree with these narrowed bounds.
22415 -- Set_Fixed_Range constructs the range from real literal values, and
22416 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22417
22418 -- The parent of this range is set to point to the entity so that it is
22419 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22420 -- other scalar types, which are just pointers to the range in the
22421 -- original tree, this would otherwise be an orphan).
22422
22423 -- The tree is left unanalyzed. When the type is frozen, the processing
22424 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22425 -- analyzed, and uses this as an indication that it should complete
22426 -- work on the range (it will know the final small and size values).
22427
22428 procedure Set_Fixed_Range
22429 (E : Entity_Id;
22430 Loc : Source_Ptr;
22431 Lo : Ureal;
22432 Hi : Ureal)
22433 is
22434 S : constant Node_Id :=
22435 Make_Range (Loc,
22436 Low_Bound => Make_Real_Literal (Loc, Lo),
22437 High_Bound => Make_Real_Literal (Loc, Hi));
22438 begin
22439 Set_Scalar_Range (E, S);
22440 Set_Parent (S, E);
22441
22442 -- Before the freeze point, the bounds of a fixed point are universal
22443 -- and carry the corresponding type.
22444
22445 Set_Etype (Low_Bound (S), Universal_Real);
22446 Set_Etype (High_Bound (S), Universal_Real);
22447 end Set_Fixed_Range;
22448
22449 ----------------------------------
22450 -- Set_Scalar_Range_For_Subtype --
22451 ----------------------------------
22452
22453 procedure Set_Scalar_Range_For_Subtype
22454 (Def_Id : Entity_Id;
22455 R : Node_Id;
22456 Subt : Entity_Id)
22457 is
22458 Kind : constant Entity_Kind := Ekind (Def_Id);
22459
22460 begin
22461 -- Defend against previous error
22462
22463 if Nkind (R) = N_Error then
22464 return;
22465 end if;
22466
22467 Set_Scalar_Range (Def_Id, R);
22468
22469 -- We need to link the range into the tree before resolving it so
22470 -- that types that are referenced, including importantly the subtype
22471 -- itself, are properly frozen (Freeze_Expression requires that the
22472 -- expression be properly linked into the tree). Of course if it is
22473 -- already linked in, then we do not disturb the current link.
22474
22475 if No (Parent (R)) then
22476 Set_Parent (R, Def_Id);
22477 end if;
22478
22479 -- Reset the kind of the subtype during analysis of the range, to
22480 -- catch possible premature use in the bounds themselves.
22481
22482 Set_Ekind (Def_Id, E_Void);
22483 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22484 Set_Ekind (Def_Id, Kind);
22485 end Set_Scalar_Range_For_Subtype;
22486
22487 --------------------------------------------------------
22488 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22489 --------------------------------------------------------
22490
22491 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22492 (E : Entity_Id)
22493 is
22494 begin
22495 -- Make sure set if encountered during Expand_To_Stored_Constraint
22496
22497 Set_Stored_Constraint (E, No_Elist);
22498
22499 -- Give it the right value
22500
22501 if Is_Constrained (E) and then Has_Discriminants (E) then
22502 Set_Stored_Constraint (E,
22503 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22504 end if;
22505 end Set_Stored_Constraint_From_Discriminant_Constraint;
22506
22507 -------------------------------------
22508 -- Signed_Integer_Type_Declaration --
22509 -------------------------------------
22510
22511 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22512 Implicit_Base : Entity_Id;
22513 Base_Typ : Entity_Id;
22514 Lo_Val : Uint;
22515 Hi_Val : Uint;
22516 Errs : Boolean := False;
22517 Lo : Node_Id;
22518 Hi : Node_Id;
22519
22520 function Can_Derive_From (E : Entity_Id) return Boolean;
22521 -- Determine whether given bounds allow derivation from specified type
22522
22523 procedure Check_Bound (Expr : Node_Id);
22524 -- Check bound to make sure it is integral and static. If not, post
22525 -- appropriate error message and set Errs flag
22526
22527 ---------------------
22528 -- Can_Derive_From --
22529 ---------------------
22530
22531 -- Note we check both bounds against both end values, to deal with
22532 -- strange types like ones with a range of 0 .. -12341234.
22533
22534 function Can_Derive_From (E : Entity_Id) return Boolean is
22535 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22536 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22537 begin
22538 return Lo <= Lo_Val and then Lo_Val <= Hi
22539 and then
22540 Lo <= Hi_Val and then Hi_Val <= Hi;
22541 end Can_Derive_From;
22542
22543 -----------------
22544 -- Check_Bound --
22545 -----------------
22546
22547 procedure Check_Bound (Expr : Node_Id) is
22548 begin
22549 -- If a range constraint is used as an integer type definition, each
22550 -- bound of the range must be defined by a static expression of some
22551 -- integer type, but the two bounds need not have the same integer
22552 -- type (Negative bounds are allowed.) (RM 3.5.4)
22553
22554 if not Is_Integer_Type (Etype (Expr)) then
22555 Error_Msg_N
22556 ("integer type definition bounds must be of integer type", Expr);
22557 Errs := True;
22558
22559 elsif not Is_OK_Static_Expression (Expr) then
22560 Flag_Non_Static_Expr
22561 ("non-static expression used for integer type bound!", Expr);
22562 Errs := True;
22563
22564 -- Otherwise the bounds are folded into literals
22565
22566 elsif Is_Entity_Name (Expr) then
22567 Fold_Uint (Expr, Expr_Value (Expr), True);
22568 end if;
22569 end Check_Bound;
22570
22571 -- Start of processing for Signed_Integer_Type_Declaration
22572
22573 begin
22574 -- Create an anonymous base type
22575
22576 Implicit_Base :=
22577 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22578
22579 -- Analyze and check the bounds, they can be of any integer type
22580
22581 Lo := Low_Bound (Def);
22582 Hi := High_Bound (Def);
22583
22584 -- Arbitrarily use Integer as the type if either bound had an error
22585
22586 if Hi = Error or else Lo = Error then
22587 Base_Typ := Any_Integer;
22588 Set_Error_Posted (T, True);
22589 Errs := True;
22590
22591 -- Here both bounds are OK expressions
22592
22593 else
22594 Analyze_And_Resolve (Lo, Any_Integer);
22595 Analyze_And_Resolve (Hi, Any_Integer);
22596
22597 Check_Bound (Lo);
22598 Check_Bound (Hi);
22599
22600 if Errs then
22601 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22602 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22603 end if;
22604
22605 -- Find type to derive from
22606
22607 Lo_Val := Expr_Value (Lo);
22608 Hi_Val := Expr_Value (Hi);
22609
22610 if Can_Derive_From (Standard_Short_Short_Integer) then
22611 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22612
22613 elsif Can_Derive_From (Standard_Short_Integer) then
22614 Base_Typ := Base_Type (Standard_Short_Integer);
22615
22616 elsif Can_Derive_From (Standard_Integer) then
22617 Base_Typ := Base_Type (Standard_Integer);
22618
22619 elsif Can_Derive_From (Standard_Long_Integer) then
22620 Base_Typ := Base_Type (Standard_Long_Integer);
22621
22622 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22623 Check_Restriction (No_Long_Long_Integers, Def);
22624 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22625
22626 else
22627 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22628 Error_Msg_N ("integer type definition bounds out of range", Def);
22629 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22630 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22631 end if;
22632 end if;
22633
22634 -- Set the type of the bounds to the implicit base: we cannot set it to
22635 -- the new type, because this would be a forward reference for the code
22636 -- generator and, if the original type is user-defined, this could even
22637 -- lead to spurious semantic errors. Furthermore we do not set it to be
22638 -- universal, because this could make it much larger than needed here.
22639
22640 if not Errs then
22641 Set_Etype (Lo, Implicit_Base);
22642 Set_Etype (Hi, Implicit_Base);
22643 end if;
22644
22645 -- Complete both implicit base and declared first subtype entities. The
22646 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22647 -- are not clobbered when the signed integer type acts as a full view of
22648 -- a private type.
22649
22650 Set_Etype (Implicit_Base, Base_Typ);
22651 Set_Size_Info (Implicit_Base, Base_Typ);
22652 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22653 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22654 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22655
22656 Set_Ekind (T, E_Signed_Integer_Subtype);
22657 Set_Etype (T, Implicit_Base);
22658 Set_Size_Info (T, Implicit_Base);
22659 Inherit_Rep_Item_Chain (T, Implicit_Base);
22660 Set_Scalar_Range (T, Def);
22661 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22662 Set_Is_Constrained (T);
22663 end Signed_Integer_Type_Declaration;
22664
22665 end Sem_Ch3;