testsuite: Fix expand-return CMSE test for Armv8.1-M [PR115253]
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobcbe2ef8be543490945ffcc6627b7bb2f7a788fe3
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-2024, 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 ------------------------------------------------------------------------------
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Debug; use Debug;
32 with Elists; use Elists;
33 with Einfo; use Einfo;
34 with Einfo.Entities; use Einfo.Entities;
35 with Einfo.Utils; use Einfo.Utils;
36 with Errout; use Errout;
37 with Eval_Fat; use Eval_Fat;
38 with Exp_Ch3; use Exp_Ch3;
39 with Exp_Ch9; use Exp_Ch9;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Expander; use Expander;
45 with Freeze; use Freeze;
46 with Ghost; use Ghost;
47 with Itypes; use Itypes;
48 with Layout; use Layout;
49 with Lib; use Lib;
50 with Lib.Xref; use Lib.Xref;
51 with Namet; use Namet;
52 with Nlists; use Nlists;
53 with Nmake; use Nmake;
54 with Opt; use Opt;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Case; use Sem_Case;
61 with Sem_Cat; use Sem_Cat;
62 with Sem_Ch6; use Sem_Ch6;
63 with Sem_Ch7; use Sem_Ch7;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch10; use Sem_Ch10;
66 with Sem_Ch13; use Sem_Ch13;
67 with Sem_Dim; use Sem_Dim;
68 with Sem_Disp; use Sem_Disp;
69 with Sem_Dist; use Sem_Dist;
70 with Sem_Elab; use Sem_Elab;
71 with Sem_Elim; use Sem_Elim;
72 with Sem_Eval; use Sem_Eval;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Res; use Sem_Res;
75 with Sem_Smem; use Sem_Smem;
76 with Sem_Type; use Sem_Type;
77 with Sem_Util; use Sem_Util;
78 with Sem_Warn; use Sem_Warn;
79 with Stand; use Stand;
80 with Sinfo; use Sinfo;
81 with Sinfo.Nodes; use Sinfo.Nodes;
82 with Sinfo.Utils; use Sinfo.Utils;
83 with Sinput; use Sinput;
84 with Snames; use Snames;
85 with Strub; use Strub;
86 with Targparm; use Targparm;
87 with Tbuild; use Tbuild;
88 with Ttypes; use Ttypes;
89 with Uintp; use Uintp;
90 with Urealp; use Urealp;
91 with Warnsw; use Warnsw;
93 package body Sem_Ch3 is
95 -----------------------
96 -- Local Subprograms --
97 -----------------------
99 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
100 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
101 -- abstract interface types implemented by a record type or a derived
102 -- record type.
104 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
105 -- When an access-to-subprogram type has pre/postconditions, we build a
106 -- subprogram that includes these contracts and is invoked by an indirect
107 -- call through the corresponding access type.
109 procedure Build_Derived_Type
110 (N : Node_Id;
111 Parent_Type : Entity_Id;
112 Derived_Type : Entity_Id;
113 Is_Completion : Boolean;
114 Derive_Subps : Boolean := True);
115 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
116 -- the N_Full_Type_Declaration node containing the derived type definition.
117 -- Parent_Type is the entity for the parent type in the derived type
118 -- definition and Derived_Type the actual derived type. Is_Completion must
119 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
120 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
121 -- completion of a private type declaration. If Is_Completion is set to
122 -- True, N is the completion of a private type declaration and Derived_Type
123 -- is different from the defining identifier inside N (i.e. Derived_Type /=
124 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
125 -- subprograms should be derived. The only case where this parameter is
126 -- False is when Build_Derived_Type is recursively called to process an
127 -- implicit derived full type for a type derived from a private type (in
128 -- that case the subprograms must only be derived for the private view of
129 -- the type).
131 -- ??? These flags need a bit of re-examination and re-documentation:
132 -- ??? are they both necessary (both seem related to the recursion)?
134 procedure Build_Derived_Access_Type
135 (N : Node_Id;
136 Parent_Type : Entity_Id;
137 Derived_Type : Entity_Id);
138 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
139 -- create an implicit base if the parent type is constrained or if the
140 -- subtype indication has a constraint.
142 procedure Build_Derived_Array_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id);
146 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
147 -- create an implicit base if the parent type is constrained or if the
148 -- subtype indication has a constraint.
150 procedure Build_Derived_Concurrent_Type
151 (N : Node_Id;
152 Parent_Type : Entity_Id;
153 Derived_Type : Entity_Id);
154 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
155 -- protected type, inherit entries and protected subprograms, check
156 -- legality of discriminant constraints if any.
158 procedure Build_Derived_Enumeration_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id);
162 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
163 -- type, we must create a new list of literals. Types derived from
164 -- Character and [Wide_]Wide_Character are special-cased.
166 procedure Build_Derived_Numeric_Type
167 (N : Node_Id;
168 Parent_Type : Entity_Id;
169 Derived_Type : Entity_Id);
170 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
171 -- an anonymous base type, and propagate constraint to subtype if needed.
173 procedure Build_Derived_Private_Type
174 (N : Node_Id;
175 Parent_Type : Entity_Id;
176 Derived_Type : Entity_Id;
177 Is_Completion : Boolean;
178 Derive_Subps : Boolean := True);
179 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
180 -- because the parent may or may not have a completion, and the derivation
181 -- may itself be a completion.
183 procedure Build_Derived_Record_Type
184 (N : Node_Id;
185 Parent_Type : Entity_Id;
186 Derived_Type : Entity_Id;
187 Derive_Subps : Boolean := True);
188 -- Subsidiary procedure used for tagged and untagged record types
189 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
190 -- All parameters are as in Build_Derived_Type except that N, in
191 -- addition to being an N_Full_Type_Declaration node, can also be an
192 -- N_Private_Extension_Declaration node. See the definition of this routine
193 -- for much more info. Derive_Subps indicates whether subprograms should be
194 -- derived from the parent type. The only case where Derive_Subps is False
195 -- is for an implicit derived full type for a type derived from a private
196 -- type (see Build_Derived_Type).
198 procedure Build_Discriminal (Discrim : Entity_Id);
199 -- Create the discriminal corresponding to discriminant Discrim, that is
200 -- the parameter corresponding to Discrim to be used in initialization
201 -- procedures for the type where Discrim is a discriminant. Discriminals
202 -- are not used during semantic analysis, and are not fully defined
203 -- entities until expansion. Thus they are not given a scope until
204 -- initialization procedures are built.
206 function Build_Discriminant_Constraints
207 (T : Entity_Id;
208 Def : Node_Id;
209 Derived_Def : Boolean := False) return Elist_Id;
210 -- Validate discriminant constraints and return the list of the constraints
211 -- in order of discriminant declarations, where T is the discriminated
212 -- unconstrained type. Def is the N_Subtype_Indication node where the
213 -- discriminants constraints for T are specified. Derived_Def is True
214 -- when building the discriminant constraints in a derived type definition
215 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
216 -- type and Def is the constraint "(xxx)" on T and this routine sets the
217 -- Corresponding_Discriminant field of the discriminants in the derived
218 -- type D to point to the corresponding discriminants in the parent type T.
220 procedure Build_Discriminated_Subtype
221 (T : Entity_Id;
222 Def_Id : Entity_Id;
223 Elist : Elist_Id;
224 Related_Nod : Node_Id;
225 For_Access : Boolean := False);
226 -- Subsidiary procedure to Constrain_Discriminated_Type and to
227 -- Process_Incomplete_Dependents. Given
229 -- T (a possibly discriminated base type)
230 -- Def_Id (a very partially built subtype for T),
232 -- the call completes Def_Id to be the appropriate E_*_Subtype.
234 -- The Elist is the list of discriminant constraints if any (it is set
235 -- to No_Elist if T is not a discriminated type, and to an empty list if
236 -- T has discriminants but there are no discriminant constraints). The
237 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
238 -- The For_Access says whether or not this subtype is really constraining
239 -- an access type.
241 function Build_Scalar_Bound
242 (Bound : Node_Id;
243 Par_T : Entity_Id;
244 Der_T : Entity_Id) return Node_Id;
245 -- The bounds of a derived scalar type are conversions of the bounds of
246 -- the parent type. Optimize the representation if the bounds are literals.
247 -- Needs a more complete spec--what are the parameters exactly, and what
248 -- exactly is the returned value, and how is Bound affected???
250 procedure Check_Access_Discriminant_Requires_Limited
251 (D : Node_Id;
252 Loc : Node_Id);
253 -- Check the restriction that the type to which an access discriminant
254 -- belongs must be a concurrent type or a descendant of a type with
255 -- the reserved word 'limited' in its declaration.
257 procedure Check_Anonymous_Access_Component
258 (Typ_Decl : Node_Id;
259 Typ : Entity_Id;
260 Prev : Entity_Id;
261 Comp_Def : Node_Id;
262 Access_Def : Node_Id);
263 -- Ada 2005 AI-382: an access component in a record definition can refer to
264 -- the enclosing record, in which case it denotes the type itself, and not
265 -- the current instance of the type. We create an anonymous access type for
266 -- the component, and flag it as an access to a component, so accessibility
267 -- checks are properly performed on it. The declaration of the access type
268 -- is placed ahead of that of the record to prevent order-of-elaboration
269 -- circularity issues in Gigi. We create an incomplete type for the record
270 -- declaration, which is the designated type of the anonymous access.
272 procedure Check_Anonymous_Access_Components
273 (Typ_Decl : Node_Id;
274 Typ : Entity_Id;
275 Prev : Entity_Id;
276 Comp_List : Node_Id);
277 -- Call Check_Anonymous_Access_Component on Comp_List
279 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
280 -- Check that, if a new discriminant is used in a constraint defining the
281 -- parent subtype of a derivation, its subtype is statically compatible
282 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
284 procedure Check_Delta_Expression (E : Node_Id);
285 -- Check that the expression represented by E is suitable for use as a
286 -- delta expression, i.e. it is of real type and is static.
288 procedure Check_Digits_Expression (E : Node_Id);
289 -- Check that the expression represented by E is suitable for use as a
290 -- digits expression, i.e. it is of integer type, positive and static.
292 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
293 -- Validate the initialization of an object declaration. T is the required
294 -- type, and Exp is the initialization expression.
296 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
297 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
299 procedure Check_Or_Process_Discriminants
300 (N : Node_Id;
301 T : Entity_Id;
302 Prev : Entity_Id := Empty);
303 -- If N is the full declaration of the completion T of an incomplete or
304 -- private type, check its discriminants (which are already known to be
305 -- conformant with those of the partial view, see Find_Type_Name),
306 -- otherwise process them. Prev is the entity of the partial declaration,
307 -- if any.
309 procedure Check_Real_Bound (Bound : Node_Id);
310 -- Check given bound for being of real type and static. If not, post an
311 -- appropriate message, and rewrite the bound with the real literal zero.
313 procedure Constant_Redeclaration
314 (Id : Entity_Id;
315 N : Node_Id;
316 T : out Entity_Id);
317 -- Various checks on legality of full declaration of deferred constant.
318 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
319 -- node. The caller has not yet set any attributes of this entity.
321 function Contain_Interface
322 (Iface : Entity_Id;
323 Ifaces : Elist_Id) return Boolean;
324 -- Ada 2005: Determine whether Iface is present in the list Ifaces
326 procedure Convert_Scalar_Bounds
327 (N : Node_Id;
328 Parent_Type : Entity_Id;
329 Derived_Type : Entity_Id;
330 Loc : Source_Ptr);
331 -- For derived scalar types, convert the bounds in the type definition to
332 -- the derived type, and complete their analysis. Given a constraint of the
333 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
334 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
335 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
336 -- subtype are conversions of those bounds to the derived_type, so that
337 -- their typing is consistent.
339 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
340 -- Copies attributes from array base type T2 to array base type T1. Copies
341 -- only attributes that apply to base types, but not subtypes.
343 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
344 -- Copies attributes from array subtype T2 to array subtype T1. Copies
345 -- attributes that apply to both subtypes and base types.
347 procedure Create_Constrained_Components
348 (Subt : Entity_Id;
349 Decl_Node : Node_Id;
350 Typ : Entity_Id;
351 Constraints : Elist_Id);
352 -- Build the list of entities for a constrained discriminated record
353 -- subtype. If a component depends on a discriminant, replace its subtype
354 -- using the discriminant values in the discriminant constraint. Subt
355 -- is the defining identifier for the subtype whose list of constrained
356 -- entities we will create. Decl_Node is the type declaration node where
357 -- we will attach all the itypes created. Typ is the base discriminated
358 -- type for the subtype Subt. Constraints is the list of discriminant
359 -- constraints for Typ.
361 function Constrain_Component_Type
362 (Comp : Entity_Id;
363 Constrained_Typ : Entity_Id;
364 Related_Node : Node_Id;
365 Typ : Entity_Id;
366 Constraints : Elist_Id) return Entity_Id;
367 -- Given a discriminated base type Typ, a list of discriminant constraints,
368 -- Constraints, for Typ and a component Comp of Typ, create and return the
369 -- type corresponding to Etype (Comp) where all discriminant references
370 -- are replaced with the corresponding constraint. If Etype (Comp) contains
371 -- no discriminant references then it is returned as-is. Constrained_Typ
372 -- is the final constrained subtype to which the constrained component
373 -- belongs. Related_Node is the node where we attach all created itypes.
375 procedure Constrain_Access
376 (Def_Id : in out Entity_Id;
377 S : Node_Id;
378 Related_Nod : Node_Id);
379 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
380 -- an anonymous type created for a subtype indication. In that case it is
381 -- created in the procedure and attached to Related_Nod.
383 procedure Constrain_Array
384 (Def_Id : in out Entity_Id;
385 SI : Node_Id;
386 Related_Nod : Node_Id;
387 Related_Id : Entity_Id;
388 Suffix : Character);
389 -- Apply a list of index constraints to an unconstrained array type. The
390 -- first parameter is the entity for the resulting subtype. A value of
391 -- Empty for Def_Id indicates that an implicit type must be created, but
392 -- creation is delayed (and must be done by this procedure) because other
393 -- subsidiary implicit types must be created first (which is why Def_Id
394 -- is an in/out parameter). The second parameter is a subtype indication
395 -- node for the constrained array to be created (e.g. something of the
396 -- form string (1 .. 10)). Related_Nod gives the place where this type
397 -- has to be inserted in the tree. The Related_Id and Suffix parameters
398 -- are used to build the associated Implicit type name.
400 procedure Constrain_Concurrent
401 (Def_Id : in out Entity_Id;
402 SI : Node_Id;
403 Related_Nod : Node_Id;
404 Related_Id : Entity_Id;
405 Suffix : Character);
406 -- Apply list of discriminant constraints to an unconstrained concurrent
407 -- type.
409 -- SI is the N_Subtype_Indication node containing the constraint and
410 -- the unconstrained type to constrain.
412 -- Def_Id is the entity for the resulting constrained subtype. A value
413 -- of Empty for Def_Id indicates that an implicit type must be created,
414 -- but creation is delayed (and must be done by this procedure) because
415 -- other subsidiary implicit types must be created first (which is why
416 -- Def_Id is an in/out parameter).
418 -- Related_Nod gives the place where this type has to be inserted
419 -- in the tree.
421 -- The last two arguments are used to create its external name if needed.
423 function Constrain_Corresponding_Record
424 (Prot_Subt : Entity_Id;
425 Corr_Rec : Entity_Id;
426 Related_Nod : Node_Id) return Entity_Id;
427 -- When constraining a protected type or task type with discriminants,
428 -- constrain the corresponding record with the same discriminant values.
430 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id);
431 -- Constrain a decimal fixed point type with a digits constraint and/or a
432 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
434 procedure Constrain_Discriminated_Type
435 (Def_Id : Entity_Id;
436 S : Node_Id;
437 Related_Nod : Node_Id;
438 For_Access : Boolean := False);
439 -- Process discriminant constraints of composite type. Verify that values
440 -- have been provided for all discriminants, that the original type is
441 -- unconstrained, and that the types of the supplied expressions match
442 -- the discriminant types. The first three parameters are like in routine
443 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
444 -- of For_Access.
446 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id);
447 -- Constrain an enumeration type with a range constraint. This is identical
448 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
450 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id);
451 -- Constrain a floating point type with either a digits constraint
452 -- and/or a range constraint, building a E_Floating_Point_Subtype.
454 procedure Constrain_Index
455 (Index : Node_Id;
456 S : Node_Id;
457 Related_Nod : Node_Id;
458 Related_Id : Entity_Id;
459 Suffix : Character;
460 Suffix_Index : Pos);
461 -- Process an index constraint S in a constrained array declaration. The
462 -- constraint can be a subtype name, or a range with or without an explicit
463 -- subtype mark. The index is the corresponding index of the unconstrained
464 -- array. The Related_Id and Suffix parameters are used to build the
465 -- associated Implicit type name.
467 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id);
468 -- Build subtype of a signed or modular integer type
470 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id);
471 -- Constrain an ordinary fixed point type with a range constraint, and
472 -- build an E_Ordinary_Fixed_Point_Subtype entity.
474 procedure Copy_And_Swap (Priv, Full : Entity_Id);
475 -- Copy the Priv entity into the entity of its full declaration then swap
476 -- the two entities in such a manner that the former private type is now
477 -- seen as a full type.
479 procedure Decimal_Fixed_Point_Type_Declaration
480 (T : Entity_Id;
481 Def : Node_Id);
482 -- Create a new decimal fixed point type, and apply the constraint to
483 -- obtain a subtype of this new type.
485 procedure Complete_Private_Subtype
486 (Priv : Entity_Id;
487 Full : Entity_Id;
488 Full_Base : Entity_Id;
489 Related_Nod : Node_Id);
490 -- Complete the implicit full view of a private subtype by setting the
491 -- appropriate semantic fields. If the full view of the parent is a record
492 -- type, build constrained components of subtype.
494 procedure Derive_Progenitor_Subprograms
495 (Parent_Type : Entity_Id;
496 Tagged_Type : Entity_Id);
497 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
498 -- operations of progenitors of Tagged_Type, and replace the subsidiary
499 -- subtypes with Tagged_Type, to build the specs of the inherited interface
500 -- primitives. The derived primitives are aliased to those of the
501 -- interface. This routine takes care also of transferring to the full view
502 -- subprograms associated with the partial view of Tagged_Type that cover
503 -- interface primitives.
505 procedure Derived_Standard_Character
506 (N : Node_Id;
507 Parent_Type : Entity_Id;
508 Derived_Type : Entity_Id);
509 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
510 -- derivations from types Standard.Character and Standard.Wide_Character.
512 procedure Derived_Type_Declaration
513 (T : Entity_Id;
514 N : Node_Id;
515 Is_Completion : Boolean);
516 -- Process a derived type declaration. Build_Derived_Type is invoked
517 -- to process the actual derived type definition. Parameters N and
518 -- Is_Completion have the same meaning as in Build_Derived_Type.
519 -- T is the N_Defining_Identifier for the entity defined in the
520 -- N_Full_Type_Declaration node N, that is T is the derived type.
522 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
523 -- Insert each literal in symbol table, as an overloadable identifier. Each
524 -- enumeration type is mapped into a sequence of integers, and each literal
525 -- is defined as a constant with integer value. If any of the literals are
526 -- character literals, the type is a character type, which means that
527 -- strings are legal aggregates for arrays of components of the type.
529 function Expand_To_Stored_Constraint
530 (Typ : Entity_Id;
531 Constraint : Elist_Id) return Elist_Id;
532 -- Given a constraint (i.e. a list of expressions) on the discriminants of
533 -- Typ, expand it into a constraint on the stored discriminants and return
534 -- the new list of expressions constraining the stored discriminants.
536 function Find_Type_Of_Object
537 (Obj_Def : Node_Id;
538 Related_Nod : Node_Id) return Entity_Id;
539 -- Get type entity for object referenced by Obj_Def, attaching the implicit
540 -- types generated to Related_Nod.
542 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
543 -- Create a new float and apply the constraint to obtain subtype of it
545 function Has_Range_Constraint (N : Node_Id) return Boolean;
546 -- Given an N_Subtype_Indication node N, return True if a range constraint
547 -- is present, either directly, or as part of a digits or delta constraint.
548 -- In addition, a digits constraint in the decimal case returns True, since
549 -- it establishes a default range if no explicit range is present.
551 function Inherit_Components
552 (N : Node_Id;
553 Parent_Base : Entity_Id;
554 Derived_Base : Entity_Id;
555 Is_Tagged : Boolean;
556 Inherit_Discr : Boolean;
557 Discs : Elist_Id) return Elist_Id;
558 -- Called from Build_Derived_Record_Type to inherit the components of
559 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
560 -- For more information on derived types and component inheritance please
561 -- consult the comment above the body of Build_Derived_Record_Type.
563 -- N is the original derived type declaration
565 -- Is_Tagged is set if we are dealing with tagged types
567 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
568 -- Parent_Base, otherwise no discriminants are inherited.
570 -- Discs gives the list of constraints that apply to Parent_Base in the
571 -- derived type declaration. If Discs is set to No_Elist, then we have
572 -- the following situation:
574 -- type Parent (D1..Dn : ..) is [tagged] record ...;
575 -- type Derived is new Parent [with ...];
577 -- which gets treated as
579 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
581 -- For untagged types the returned value is an association list. The list
582 -- starts from the association (Parent_Base => Derived_Base), and then it
583 -- contains a sequence of the associations of the form
585 -- (Old_Component => New_Component),
587 -- where Old_Component is the Entity_Id of a component in Parent_Base and
588 -- New_Component is the Entity_Id of the corresponding component in
589 -- Derived_Base. For untagged records, this association list is needed when
590 -- copying the record declaration for the derived base. In the tagged case
591 -- the value returned is irrelevant.
593 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
594 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
595 -- Determine whether subprogram Subp is a procedure subject to pragma
596 -- Extensions_Visible with value False and has at least one controlling
597 -- parameter of mode OUT.
599 function Is_Private_Primitive (Prim : Entity_Id) return Boolean;
600 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
601 -- When applied to a primitive subprogram Prim, returns True if Prim is
602 -- declared as a private operation within a package or generic package,
603 -- and returns False otherwise.
605 function Is_Valid_Constraint_Kind
606 (T_Kind : Type_Kind;
607 Constraint_Kind : Node_Kind) return Boolean;
608 -- Returns True if it is legal to apply the given kind of constraint to the
609 -- given kind of type (index constraint to an array type, for example).
611 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
612 -- Create new modular type. Verify that modulus is in bounds
614 procedure New_Concatenation_Op (Typ : Entity_Id);
615 -- Create an abbreviated declaration for an operator in order to
616 -- materialize concatenation on array types.
618 procedure Ordinary_Fixed_Point_Type_Declaration
619 (T : Entity_Id;
620 Def : Node_Id);
621 -- Create a new ordinary fixed point type, and apply the constraint to
622 -- obtain subtype of it.
624 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
625 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
626 -- In_Default_Expr can be properly adjusted.
628 procedure Prepare_Private_Subtype_Completion
629 (Id : Entity_Id;
630 Related_Nod : Node_Id);
631 -- Id is a subtype of some private type. Creates the full declaration
632 -- associated with Id whenever possible, i.e. when the full declaration
633 -- of the base type is already known. Records each subtype into
634 -- Private_Dependents of the base type.
636 procedure Process_Incomplete_Dependents
637 (N : Node_Id;
638 Full_T : Entity_Id;
639 Inc_T : Entity_Id);
640 -- Process all entities that depend on an incomplete type. There include
641 -- subtypes, subprogram types that mention the incomplete type in their
642 -- profiles, and subprogram with access parameters that designate the
643 -- incomplete type.
645 -- Inc_T is the defining identifier of an incomplete type declaration, its
646 -- Ekind is E_Incomplete_Type.
648 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
650 -- Full_T is N's defining identifier.
652 -- Subtypes of incomplete types with discriminants are completed when the
653 -- parent type is. This is simpler than private subtypes, because they can
654 -- only appear in the same scope, and there is no need to exchange views.
655 -- Similarly, access_to_subprogram types may have a parameter or a return
656 -- type that is an incomplete type, and that must be replaced with the
657 -- full type.
659 -- If the full type is tagged, subprogram with access parameters that
660 -- designated the incomplete may be primitive operations of the full type,
661 -- and have to be processed accordingly.
663 procedure Process_Real_Range_Specification (Def : Node_Id);
664 -- Given the type definition for a real type, this procedure processes and
665 -- checks the real range specification of this type definition if one is
666 -- present. If errors are found, error messages are posted, and the
667 -- Real_Range_Specification of Def is reset to Empty.
669 procedure Record_Type_Declaration
670 (T : Entity_Id;
671 N : Node_Id;
672 Prev : Entity_Id);
673 -- Process a record type declaration (for both untagged and tagged
674 -- records). Parameters T and N are exactly like in procedure
675 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
676 -- for this routine. If this is the completion of an incomplete type
677 -- declaration, Prev is the entity of the incomplete declaration, used for
678 -- cross-referencing. Otherwise Prev = T.
680 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
681 -- This routine is used to process the actual record type definition (both
682 -- for untagged and tagged records). Def is a record type definition node.
683 -- This procedure analyzes the components in this record type definition.
684 -- Prev_T is the entity for the enclosing record type. It is provided so
685 -- that its Has_Task flag can be set if any of the component have Has_Task
686 -- set. If the declaration is the completion of an incomplete type
687 -- declaration, Prev_T is the original incomplete type, whose full view is
688 -- the record type.
690 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
691 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
692 -- first create the list of components for the derived type from that of
693 -- the parent by means of Inherit_Components and then build a copy of the
694 -- declaration tree of the parent with the help of the mapping returned by
695 -- Inherit_Components, which will for example be used to validate record
696 -- representation clauses given for the derived type. If the parent type
697 -- is private and has discriminants, the ancestor discriminants used in the
698 -- inheritance are that of the private declaration, whereas the ancestor
699 -- discriminants present in the declaration tree of the parent are that of
700 -- the full declaration; as a consequence, the remapping done during the
701 -- copy will leave the references to the ancestor discriminants unchanged
702 -- in the declaration tree and they need to be fixed up. If the derived
703 -- type has a known discriminant part, then the remapping done during the
704 -- copy will only create references to the stored discriminants and they
705 -- need to be replaced with references to the non-stored discriminants.
707 procedure Set_Fixed_Range
708 (E : Entity_Id;
709 Loc : Source_Ptr;
710 Lo : Ureal;
711 Hi : Ureal);
712 -- Build a range node with the given bounds and set it as the Scalar_Range
713 -- of the given fixed-point type entity. Loc is the source location used
714 -- for the constructed range. See body for further details.
716 procedure Set_Scalar_Range_For_Subtype
717 (Def_Id : Entity_Id;
718 R : Node_Id;
719 Subt : Entity_Id);
720 -- This routine is used to set the scalar range field for a subtype given
721 -- Def_Id, the entity for the subtype, and R, the range expression for the
722 -- scalar range. Subt provides the parent subtype to be used to analyze,
723 -- resolve, and check the given range.
725 procedure Set_Default_SSO (T : Entity_Id);
726 -- T is the entity for an array or record being declared. This procedure
727 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
728 -- to the setting of Opt.Default_SSO.
730 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
731 -- Create a new signed integer entity, and apply the constraint to obtain
732 -- the required first named subtype of this type.
734 procedure Set_Stored_Constraint_From_Discriminant_Constraint
735 (E : Entity_Id);
736 -- E is some record type. This routine computes E's Stored_Constraint
737 -- from its Discriminant_Constraint.
739 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
740 -- Check that an entity in a list of progenitors is an interface,
741 -- emit error otherwise.
743 -----------------------
744 -- Access_Definition --
745 -----------------------
747 function Access_Definition
748 (Related_Nod : Node_Id;
749 N : Node_Id) return Entity_Id
751 Anon_Type : Entity_Id;
752 Anon_Scope : Entity_Id;
753 Desig_Type : Entity_Id;
754 Enclosing_Prot_Type : Entity_Id := Empty;
756 begin
757 if Is_Entry (Current_Scope)
758 and then Is_Task_Type (Etype (Scope (Current_Scope)))
759 then
760 Error_Msg_N ("task entries cannot have access parameters", N);
761 return Empty;
762 end if;
764 -- Ada 2005: For an object declaration the corresponding anonymous
765 -- type is declared in the current scope.
767 -- If the access definition is the return type of another access to
768 -- function, scope is the current one, because it is the one of the
769 -- current type declaration, except for the pathological case below.
771 if Nkind (Related_Nod) in
772 N_Object_Declaration | N_Access_Function_Definition
773 then
774 Anon_Scope := Current_Scope;
776 -- A pathological case: function returning access functions that
777 -- return access functions, etc. Each anonymous access type created
778 -- is in the enclosing scope of the outermost function.
780 declare
781 Par : Node_Id;
783 begin
784 Par := Related_Nod;
785 while Nkind (Par) in
786 N_Access_Function_Definition | N_Access_Definition
787 loop
788 Par := Parent (Par);
789 end loop;
791 if Nkind (Par) = N_Function_Specification then
792 Anon_Scope := Scope (Defining_Entity (Par));
793 end if;
794 end;
796 -- For the anonymous function result case, retrieve the scope of the
797 -- function specification's associated entity rather than using the
798 -- current scope. The current scope will be the function itself if the
799 -- formal part is currently being analyzed, but will be the parent scope
800 -- in the case of a parameterless function, and we always want to use
801 -- the function's parent scope. Finally, if the function is a child
802 -- unit, we must traverse the tree to retrieve the proper entity.
804 elsif Nkind (Related_Nod) = N_Function_Specification
805 and then Nkind (Parent (N)) /= N_Parameter_Specification
806 then
807 -- If the current scope is a protected type, the anonymous access
808 -- is associated with one of the protected operations, and must
809 -- be available in the scope that encloses the protected declaration.
810 -- Otherwise the type is in the scope enclosing the subprogram.
812 -- If the function has formals, the return type of a subprogram
813 -- declaration is analyzed in the scope of the subprogram (see
814 -- Process_Formals) and thus the protected type, if present, is
815 -- the scope of the current function scope.
817 if Ekind (Current_Scope) = E_Protected_Type then
818 Enclosing_Prot_Type := Current_Scope;
820 elsif Ekind (Current_Scope) = E_Function
821 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
822 then
823 Enclosing_Prot_Type := Scope (Current_Scope);
824 end if;
826 if Present (Enclosing_Prot_Type) then
827 Anon_Scope := Scope (Enclosing_Prot_Type);
829 else
830 Anon_Scope := Scope (Defining_Entity (Related_Nod));
831 end if;
833 -- For an access type definition, if the current scope is a child
834 -- unit it is the scope of the type.
836 elsif Is_Compilation_Unit (Current_Scope) then
837 Anon_Scope := Current_Scope;
839 -- For access formals, access components, and access discriminants, the
840 -- scope is that of the enclosing declaration,
842 else
843 Anon_Scope := Scope (Current_Scope);
844 end if;
846 Anon_Type :=
847 Create_Itype
848 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
850 if All_Present (N)
851 and then Ada_Version >= Ada_2005
852 then
853 Error_Msg_N ("ALL not permitted for anonymous access types", N);
854 end if;
856 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
857 -- the corresponding semantic routine
859 if Present (Access_To_Subprogram_Definition (N)) then
860 Access_Subprogram_Declaration
861 (T_Name => Anon_Type,
862 T_Def => Access_To_Subprogram_Definition (N));
864 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
865 Mutate_Ekind
866 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
867 else
868 Mutate_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
869 end if;
871 -- If the anonymous access is associated with a protected operation,
872 -- create a reference to it after the enclosing protected definition
873 -- because the itype will be used in the subsequent bodies.
875 -- If the anonymous access itself is protected, a full type
876 -- declaratiton will be created for it, so that the equivalent
877 -- record type can be constructed. For further details, see
878 -- Replace_Anonymous_Access_To_Protected-Subprogram.
880 if Ekind (Current_Scope) = E_Protected_Type
881 and then not Protected_Present (Access_To_Subprogram_Definition (N))
882 then
883 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
884 end if;
886 return Anon_Type;
887 end if;
889 Find_Type (Subtype_Mark (N));
890 Desig_Type := Entity (Subtype_Mark (N));
892 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
893 Set_Etype (Anon_Type, Anon_Type);
895 -- Make sure the anonymous access type has size and alignment fields
896 -- set, as required by gigi. This is necessary in the case of the
897 -- Task_Body_Procedure.
899 if not Has_Private_Component (Desig_Type) then
900 Layout_Type (Anon_Type);
901 end if;
903 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
904 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
905 -- the null value is allowed. In Ada 95 the null value is never allowed.
907 if Ada_Version >= Ada_2005 then
908 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
909 else
910 Set_Can_Never_Be_Null (Anon_Type, True);
911 end if;
913 -- The anonymous access type is as public as the discriminated type or
914 -- subprogram that defines it. It is imported (for back-end purposes)
915 -- if the designated type is.
917 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
919 -- Ada 2005 (AI-231): Propagate the access-constant attribute
921 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
923 -- The context is either a subprogram declaration, object declaration,
924 -- or an access discriminant, in a private or a full type declaration.
925 -- In the case of a subprogram, if the designated type is incomplete,
926 -- the operation will be a primitive operation of the full type, to be
927 -- updated subsequently. If the type is imported through a limited_with
928 -- clause, the subprogram is not a primitive operation of the type
929 -- (which is declared elsewhere in some other scope).
931 if Ekind (Desig_Type) = E_Incomplete_Type
932 and then not From_Limited_With (Desig_Type)
933 and then Is_Overloadable (Current_Scope)
934 then
935 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
936 Set_Has_Delayed_Freeze (Current_Scope);
937 end if;
939 -- If the designated type is limited and class-wide, the object might
940 -- contain tasks, so we create a Master entity for the declaration. This
941 -- must be done before expansion of the full declaration, because the
942 -- declaration may include an expression that is an allocator, whose
943 -- expansion needs the proper Master for the created tasks.
945 if Expander_Active
946 and then Nkind (Related_Nod) = N_Object_Declaration
947 then
948 if Is_Limited_Record (Desig_Type)
949 and then Is_Class_Wide_Type (Desig_Type)
950 then
951 Build_Class_Wide_Master (Anon_Type);
953 -- Similarly, if the type is an anonymous access that designates
954 -- tasks, create a master entity for it in the current context.
956 elsif Has_Task (Desig_Type)
957 and then Comes_From_Source (Related_Nod)
958 then
959 Build_Master_Entity (Defining_Identifier (Related_Nod));
960 Build_Master_Renaming (Anon_Type);
961 end if;
962 end if;
964 -- For a private component of a protected type, it is imperative that
965 -- the back-end elaborate the type immediately after the protected
966 -- declaration, because this type will be used in the declarations
967 -- created for the component within each protected body, so we must
968 -- create an itype reference for it now.
970 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
971 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
973 -- Similarly, if the access definition is the return result of a
974 -- function, create an itype reference for it because it will be used
975 -- within the function body. For a regular function that is not a
976 -- compilation unit, insert reference after the declaration. For a
977 -- protected operation, insert it after the enclosing protected type
978 -- declaration. In either case, do not create a reference for a type
979 -- obtained through a limited_with clause, because this would introduce
980 -- semantic dependencies.
982 -- Similarly, do not create a reference if the designated type is a
983 -- generic formal, because no use of it will reach the backend.
985 elsif Nkind (Related_Nod) = N_Function_Specification
986 and then not From_Limited_With (Desig_Type)
987 and then not Is_Generic_Type (Desig_Type)
988 then
989 if Present (Enclosing_Prot_Type) then
990 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
992 elsif Is_List_Member (Parent (Related_Nod))
993 and then Nkind (Parent (N)) /= N_Parameter_Specification
994 then
995 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
996 end if;
998 -- Finally, create an itype reference for an object declaration of an
999 -- anonymous access type. This is strictly necessary only for deferred
1000 -- constants, but in any case will avoid out-of-scope problems in the
1001 -- back-end.
1003 elsif Nkind (Related_Nod) = N_Object_Declaration then
1004 Build_Itype_Reference (Anon_Type, Related_Nod);
1005 end if;
1007 return Anon_Type;
1008 end Access_Definition;
1010 -----------------------------------
1011 -- Access_Subprogram_Declaration --
1012 -----------------------------------
1014 procedure Access_Subprogram_Declaration
1015 (T_Name : Entity_Id;
1016 T_Def : Node_Id)
1018 procedure Check_For_Premature_Usage (Def : Node_Id);
1019 -- Check that type T_Name is not used, directly or recursively, as a
1020 -- parameter or a return type in Def. Def is either a subtype, an
1021 -- access_definition, or an access_to_subprogram_definition.
1023 -------------------------------
1024 -- Check_For_Premature_Usage --
1025 -------------------------------
1027 procedure Check_For_Premature_Usage (Def : Node_Id) is
1028 Param : Node_Id;
1030 begin
1031 -- Check for a subtype mark
1033 if Nkind (Def) in N_Has_Etype then
1034 if Etype (Def) = T_Name then
1035 Error_Msg_N
1036 ("type& cannot be used before the end of its declaration",
1037 Def);
1038 end if;
1040 -- If this is not a subtype, then this is an access_definition
1042 elsif Nkind (Def) = N_Access_Definition then
1043 if Present (Access_To_Subprogram_Definition (Def)) then
1044 Check_For_Premature_Usage
1045 (Access_To_Subprogram_Definition (Def));
1046 else
1047 Check_For_Premature_Usage (Subtype_Mark (Def));
1048 end if;
1050 -- The only cases left are N_Access_Function_Definition and
1051 -- N_Access_Procedure_Definition.
1053 else
1054 if Present (Parameter_Specifications (Def)) then
1055 Param := First (Parameter_Specifications (Def));
1056 while Present (Param) loop
1057 Check_For_Premature_Usage (Parameter_Type (Param));
1058 Next (Param);
1059 end loop;
1060 end if;
1062 if Nkind (Def) = N_Access_Function_Definition then
1063 Check_For_Premature_Usage (Result_Definition (Def));
1064 end if;
1065 end if;
1066 end Check_For_Premature_Usage;
1068 -- Local variables
1070 Formals : constant List_Id := Parameter_Specifications (T_Def);
1071 Formal : Entity_Id;
1072 D_Ityp : Node_Id;
1073 Desig_Type : constant Entity_Id :=
1074 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1076 -- Start of processing for Access_Subprogram_Declaration
1078 begin
1079 -- Associate the Itype node with the inner full-type declaration or
1080 -- subprogram spec or entry body. This is required to handle nested
1081 -- anonymous declarations. For example:
1083 -- procedure P
1084 -- (X : access procedure
1085 -- (Y : access procedure
1086 -- (Z : access T)))
1088 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1089 while Nkind (D_Ityp) not in N_Full_Type_Declaration
1090 | N_Private_Type_Declaration
1091 | N_Private_Extension_Declaration
1092 | N_Procedure_Specification
1093 | N_Function_Specification
1094 | N_Entry_Body
1095 | N_Object_Declaration
1096 | N_Object_Renaming_Declaration
1097 | N_Formal_Object_Declaration
1098 | N_Formal_Type_Declaration
1099 | N_Task_Type_Declaration
1100 | N_Protected_Type_Declaration
1101 loop
1102 D_Ityp := Parent (D_Ityp);
1103 pragma Assert (D_Ityp /= Empty);
1104 end loop;
1106 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1108 if Nkind (D_Ityp) in N_Procedure_Specification | N_Function_Specification
1109 then
1110 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1112 elsif Nkind (D_Ityp) in N_Full_Type_Declaration
1113 | N_Object_Declaration
1114 | N_Object_Renaming_Declaration
1115 | N_Formal_Type_Declaration
1116 then
1117 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1118 end if;
1120 if Nkind (T_Def) = N_Access_Function_Definition then
1121 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1122 declare
1123 Acc : constant Node_Id := Result_Definition (T_Def);
1125 begin
1126 if Present (Access_To_Subprogram_Definition (Acc))
1127 and then
1128 Protected_Present (Access_To_Subprogram_Definition (Acc))
1129 then
1130 Set_Etype
1131 (Desig_Type,
1132 Replace_Anonymous_Access_To_Protected_Subprogram
1133 (T_Def));
1135 else
1136 Set_Etype
1137 (Desig_Type,
1138 Access_Definition (T_Def, Result_Definition (T_Def)));
1139 end if;
1140 end;
1142 else
1143 Analyze (Result_Definition (T_Def));
1145 declare
1146 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1148 begin
1149 -- If a null exclusion is imposed on the result type, then
1150 -- create a null-excluding itype (an access subtype) and use
1151 -- it as the function's Etype.
1153 if Is_Access_Type (Typ)
1154 and then Null_Exclusion_In_Return_Present (T_Def)
1155 then
1156 Set_Etype (Desig_Type,
1157 Create_Null_Excluding_Itype
1158 (T => Typ,
1159 Related_Nod => T_Def,
1160 Scope_Id => Current_Scope));
1162 else
1163 if From_Limited_With (Typ) then
1165 -- AI05-151: Incomplete types are allowed in all basic
1166 -- declarations, including access to subprograms.
1168 if Ada_Version >= Ada_2012 then
1169 null;
1171 else
1172 Error_Msg_NE
1173 ("illegal use of incomplete type&",
1174 Result_Definition (T_Def), Typ);
1175 end if;
1177 elsif Ekind (Current_Scope) = E_Package
1178 and then In_Private_Part (Current_Scope)
1179 then
1180 if Ekind (Typ) = E_Incomplete_Type then
1181 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1183 elsif Is_Class_Wide_Type (Typ)
1184 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1185 then
1186 Append_Elmt
1187 (Desig_Type, Private_Dependents (Etype (Typ)));
1188 end if;
1189 end if;
1191 Set_Etype (Desig_Type, Typ);
1192 end if;
1193 end;
1194 end if;
1196 if not Is_Type (Etype (Desig_Type)) then
1197 Error_Msg_N
1198 ("expect type in function specification",
1199 Result_Definition (T_Def));
1200 end if;
1202 else
1203 Set_Etype (Desig_Type, Standard_Void_Type);
1204 end if;
1206 if Present (Formals) then
1207 Push_Scope (Desig_Type);
1209 -- Some special tests here. These special tests can be removed
1210 -- if and when Itypes always have proper parent pointers to their
1211 -- declarations???
1213 -- Special test 1) Link defining_identifier of formals. Required by
1214 -- First_Formal to provide its functionality.
1216 declare
1217 F : Node_Id;
1219 begin
1220 F := First (Formals);
1222 while Present (F) loop
1223 if No (Parent (Defining_Identifier (F))) then
1224 Set_Parent (Defining_Identifier (F), F);
1225 end if;
1227 Next (F);
1228 end loop;
1229 end;
1231 Process_Formals (Formals, Parent (T_Def));
1233 -- Special test 2) End_Scope requires that the parent pointer be set
1234 -- to something reasonable, but Itypes don't have parent pointers. So
1235 -- we set it and then unset it ???
1237 Set_Parent (Desig_Type, T_Name);
1238 End_Scope;
1239 Set_Parent (Desig_Type, Empty);
1240 end if;
1242 -- Check for premature usage of the type being defined
1244 Check_For_Premature_Usage (T_Def);
1246 -- The return type and/or any parameter type may be incomplete. Mark the
1247 -- subprogram_type as depending on the incomplete type, so that it can
1248 -- be updated when the full type declaration is seen. This only applies
1249 -- to incomplete types declared in some enclosing scope, not to limited
1250 -- views from other packages.
1252 -- Prior to Ada 2012, access to functions can only have in_parameters.
1254 if Present (Formals) then
1255 Formal := First_Formal (Desig_Type);
1256 while Present (Formal) loop
1257 if Ekind (Formal) /= E_In_Parameter
1258 and then Nkind (T_Def) = N_Access_Function_Definition
1259 and then Ada_Version < Ada_2012
1260 then
1261 Error_Msg_N ("functions can only have IN parameters", Formal);
1262 end if;
1264 if Ekind (Etype (Formal)) = E_Incomplete_Type
1265 and then In_Open_Scopes (Scope (Etype (Formal)))
1266 then
1267 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1268 Set_Has_Delayed_Freeze (Desig_Type);
1269 end if;
1271 Next_Formal (Formal);
1272 end loop;
1273 end if;
1275 -- Check whether an indirect call without actuals may be possible. This
1276 -- is used when resolving calls whose result is then indexed.
1278 May_Need_Actuals (Desig_Type);
1280 -- If the return type is incomplete, this is legal as long as the type
1281 -- is declared in the current scope and will be completed in it (rather
1282 -- than being part of limited view).
1284 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1285 and then not Has_Delayed_Freeze (Desig_Type)
1286 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1287 then
1288 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1289 Set_Has_Delayed_Freeze (Desig_Type);
1290 end if;
1292 Check_Delayed_Subprogram (Desig_Type);
1294 if Protected_Present (T_Def) then
1295 Mutate_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1296 Set_Convention (Desig_Type, Convention_Protected);
1297 else
1298 Mutate_Ekind (T_Name, E_Access_Subprogram_Type);
1299 end if;
1301 Set_Can_Use_Internal_Rep (T_Name,
1302 not Always_Compatible_Rep_On_Target);
1303 Set_Etype (T_Name, T_Name);
1304 Reinit_Size_Align (T_Name);
1305 Set_Directly_Designated_Type (T_Name, Desig_Type);
1307 -- If the access_to_subprogram is not declared at the library level,
1308 -- it can only point to subprograms that are at the same or deeper
1309 -- accessibility level. The corresponding subprogram type might
1310 -- require an activation record when compiling for C.
1312 Set_Needs_Activation_Record (Desig_Type,
1313 not Is_Library_Level_Entity (T_Name));
1315 Generate_Reference_To_Formals (T_Name);
1317 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1319 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1321 Check_Restriction (No_Access_Subprograms, T_Def);
1323 -- Addition of extra formals must be delayed till the freeze point so
1324 -- that we know the convention.
1325 end Access_Subprogram_Declaration;
1327 ----------------------------
1328 -- Access_Type_Declaration --
1329 ----------------------------
1331 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1333 procedure Setup_Access_Type (Desig_Typ : Entity_Id);
1334 -- After type declaration is analysed with T being an incomplete type,
1335 -- this routine will mutate the kind of T to the appropriate access type
1336 -- and set its directly designated type to Desig_Typ.
1338 -----------------------
1339 -- Setup_Access_Type --
1340 -----------------------
1342 procedure Setup_Access_Type (Desig_Typ : Entity_Id) is
1343 begin
1344 if All_Present (Def) or else Constant_Present (Def) then
1345 Mutate_Ekind (T, E_General_Access_Type);
1346 else
1347 Mutate_Ekind (T, E_Access_Type);
1348 end if;
1350 Set_Directly_Designated_Type (T, Desig_Typ);
1351 end Setup_Access_Type;
1353 -- Local variables
1355 P : constant Node_Id := Parent (Def);
1356 S : constant Node_Id := Subtype_Indication (Def);
1358 Full_Desig : Entity_Id;
1360 -- Start of processing for Access_Type_Declaration
1362 begin
1363 -- Check for permissible use of incomplete type
1365 if Nkind (S) /= N_Subtype_Indication then
1367 Analyze (S);
1369 if Nkind (S) in N_Has_Entity
1370 and then Present (Entity (S))
1371 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1372 then
1373 Setup_Access_Type (Desig_Typ => Entity (S));
1375 -- If the designated type is a limited view, we cannot tell if
1376 -- the full view contains tasks, and there is no way to handle
1377 -- that full view in a client. We create a master entity for the
1378 -- scope, which will be used when a client determines that one
1379 -- is needed.
1381 if From_Limited_With (Entity (S))
1382 and then not Is_Class_Wide_Type (Entity (S))
1383 then
1384 Build_Master_Entity (T);
1385 Build_Master_Renaming (T);
1386 end if;
1388 else
1389 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1390 end if;
1392 -- If the access definition is of the form: ACCESS NOT NULL ..
1393 -- the subtype indication must be of an access type. Create
1394 -- a null-excluding subtype of it.
1396 if Null_Excluding_Subtype (Def) then
1397 if not Is_Access_Type (Entity (S)) then
1398 Error_Msg_N ("null exclusion must apply to access type", Def);
1400 else
1401 declare
1402 Loc : constant Source_Ptr := Sloc (S);
1403 Decl : Node_Id;
1404 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1406 begin
1407 Decl :=
1408 Make_Subtype_Declaration (Loc,
1409 Defining_Identifier => Nam,
1410 Subtype_Indication =>
1411 New_Occurrence_Of (Entity (S), Loc));
1412 Set_Null_Exclusion_Present (Decl);
1413 Insert_Before (Parent (Def), Decl);
1414 Analyze (Decl);
1415 Set_Entity (S, Nam);
1416 end;
1417 end if;
1418 end if;
1420 else
1421 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1422 end if;
1424 if not Error_Posted (T) then
1425 Full_Desig := Designated_Type (T);
1427 if Base_Type (Full_Desig) = T then
1428 Error_Msg_N ("access type cannot designate itself", S);
1430 -- In Ada 2005, the type may have a limited view through some unit in
1431 -- its own context, allowing the following circularity that cannot be
1432 -- detected earlier.
1434 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1435 then
1436 Error_Msg_N
1437 ("access type cannot designate its own class-wide type", S);
1439 -- Clean up indication of tagged status to prevent cascaded errors
1441 Set_Is_Tagged_Type (T, False);
1442 end if;
1444 Set_Etype (T, T);
1445 end if;
1447 -- If the type has appeared already in a with_type clause, it is frozen
1448 -- and the pointer size is already set. Else, initialize.
1450 if not From_Limited_With (T) then
1451 Reinit_Size_Align (T);
1452 end if;
1454 -- Note that Has_Task is always false, since the access type itself
1455 -- is not a task type. See Einfo for more description on this point.
1456 -- Exactly the same consideration applies to Has_Controlled_Component
1457 -- and to Has_Protected.
1459 Set_Has_Task (T, False);
1460 Set_Has_Protected (T, False);
1461 Set_Has_Timing_Event (T, False);
1462 Set_Has_Controlled_Component (T, False);
1464 -- Initialize field Finalization_Collection explicitly to Empty to avoid
1465 -- problems where an incomplete view of this entity has been previously
1466 -- established by a limited with and an overlaid version of this field
1467 -- (Stored_Constraint) was initialized for the incomplete view.
1469 -- This reset is performed in most cases except where the access type
1470 -- has been created for the purposes of allocating or deallocating a
1471 -- build-in-place object. Such access types have explicitly set pools
1472 -- and finalization collections.
1474 if No (Associated_Storage_Pool (T)) then
1475 Set_Finalization_Collection (T, Empty);
1476 end if;
1478 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1479 -- attributes
1481 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1482 Set_Is_Access_Constant (T, Constant_Present (Def));
1483 end Access_Type_Declaration;
1485 ----------------------------------
1486 -- Add_Interface_Tag_Components --
1487 ----------------------------------
1489 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1490 Loc : constant Source_Ptr := Sloc (N);
1491 L : List_Id;
1492 Last_Tag : Node_Id;
1494 procedure Add_Tag (Iface : Entity_Id);
1495 -- Add tag for one of the progenitor interfaces
1497 -------------
1498 -- Add_Tag --
1499 -------------
1501 procedure Add_Tag (Iface : Entity_Id) is
1502 Decl : Node_Id;
1503 Def : Node_Id;
1504 Tag : Entity_Id;
1505 Offset : Entity_Id;
1507 begin
1508 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1510 -- This is a reasonable place to propagate predicates
1512 if Has_Predicates (Iface) then
1513 Set_Has_Predicates (Typ);
1514 end if;
1516 Def :=
1517 Make_Component_Definition (Loc,
1518 Aliased_Present => True,
1519 Subtype_Indication =>
1520 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1522 Tag := Make_Temporary (Loc, 'V');
1524 Decl :=
1525 Make_Component_Declaration (Loc,
1526 Defining_Identifier => Tag,
1527 Component_Definition => Def);
1529 Analyze_Component_Declaration (Decl);
1531 Set_Analyzed (Decl);
1532 Mutate_Ekind (Tag, E_Component);
1533 Set_Is_Tag (Tag);
1534 Set_Is_Aliased (Tag);
1535 Set_Is_Independent (Tag);
1536 Set_Related_Type (Tag, Iface);
1537 Reinit_Component_Location (Tag);
1539 pragma Assert (Is_Frozen (Iface));
1541 Set_DT_Entry_Count (Tag,
1542 DT_Entry_Count (First_Entity (Iface)));
1544 if No (Last_Tag) then
1545 Prepend (Decl, L);
1546 else
1547 Insert_After (Last_Tag, Decl);
1548 end if;
1550 Last_Tag := Decl;
1552 -- If the ancestor has discriminants we need to give special support
1553 -- to store the offset_to_top value of the secondary dispatch tables.
1554 -- For this purpose we add a supplementary component just after the
1555 -- field that contains the tag associated with each secondary DT.
1557 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1558 Def :=
1559 Make_Component_Definition (Loc,
1560 Subtype_Indication =>
1561 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1563 Offset := Make_Temporary (Loc, 'V');
1565 Decl :=
1566 Make_Component_Declaration (Loc,
1567 Defining_Identifier => Offset,
1568 Component_Definition => Def);
1570 Analyze_Component_Declaration (Decl);
1572 Set_Analyzed (Decl);
1573 Mutate_Ekind (Offset, E_Component);
1574 Set_Is_Aliased (Offset);
1575 Set_Is_Independent (Offset);
1576 Set_Related_Type (Offset, Iface);
1577 Reinit_Component_Location (Offset);
1578 Insert_After (Last_Tag, Decl);
1579 Last_Tag := Decl;
1580 end if;
1581 end Add_Tag;
1583 -- Local variables
1585 Elmt : Elmt_Id;
1586 Ext : Node_Id;
1587 Comp : Node_Id;
1589 -- Start of processing for Add_Interface_Tag_Components
1591 begin
1592 if not RTE_Available (RE_Interface_Tag) then
1593 Error_Msg_N
1594 ("(Ada 2005) interface types not supported by this run-time!", N);
1595 return;
1596 end if;
1598 if Ekind (Typ) /= E_Record_Type
1599 or else (Is_Concurrent_Record_Type (Typ)
1600 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1601 or else (not Is_Concurrent_Record_Type (Typ)
1602 and then No (Interfaces (Typ))
1603 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1604 then
1605 return;
1606 end if;
1608 -- Find the current last tag
1610 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1611 Ext := Record_Extension_Part (Type_Definition (N));
1612 else
1613 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1614 Ext := Type_Definition (N);
1615 end if;
1617 Last_Tag := Empty;
1619 if not Present (Component_List (Ext)) then
1620 Set_Null_Present (Ext, False);
1621 L := New_List;
1622 Set_Component_List (Ext,
1623 Make_Component_List (Loc,
1624 Component_Items => L,
1625 Null_Present => False));
1626 else
1627 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1628 L := Component_Items
1629 (Component_List
1630 (Record_Extension_Part
1631 (Type_Definition (N))));
1632 else
1633 L := Component_Items
1634 (Component_List
1635 (Type_Definition (N)));
1636 end if;
1638 -- Find the last tag component
1640 Comp := First (L);
1641 while Present (Comp) loop
1642 if Nkind (Comp) = N_Component_Declaration
1643 and then Is_Tag (Defining_Identifier (Comp))
1644 then
1645 Last_Tag := Comp;
1646 end if;
1648 Next (Comp);
1649 end loop;
1650 end if;
1652 -- At this point L references the list of components and Last_Tag
1653 -- references the current last tag (if any). Now we add the tag
1654 -- corresponding with all the interfaces that are not implemented
1655 -- by the parent.
1657 if Present (Interfaces (Typ)) then
1658 Elmt := First_Elmt (Interfaces (Typ));
1659 while Present (Elmt) loop
1660 Add_Tag (Node (Elmt));
1661 Next_Elmt (Elmt);
1662 end loop;
1663 end if;
1664 end Add_Interface_Tag_Components;
1666 -------------------------------------
1667 -- Add_Internal_Interface_Entities --
1668 -------------------------------------
1670 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1672 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean;
1673 -- Determine if an error has been posted in some formal of Subp.
1675 -----------------------------
1676 -- Error_Posted_In_Formals --
1677 -----------------------------
1679 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean is
1680 Formal : Entity_Id := First_Formal (Subp);
1682 begin
1683 while Present (Formal) loop
1684 if Error_Posted (Formal) then
1685 return True;
1686 end if;
1688 Next_Formal (Formal);
1689 end loop;
1691 return False;
1692 end Error_Posted_In_Formals;
1694 -- Local variables
1696 Elmt : Elmt_Id;
1697 Iface : Entity_Id;
1698 Iface_Elmt : Elmt_Id;
1699 Iface_Prim : Entity_Id;
1700 Ifaces_List : Elist_Id;
1701 New_Subp : Entity_Id := Empty;
1702 Prim : Entity_Id;
1703 Restore_Scope : Boolean := False;
1705 begin
1706 pragma Assert (Ada_Version >= Ada_2005
1707 and then Is_Record_Type (Tagged_Type)
1708 and then Is_Tagged_Type (Tagged_Type)
1709 and then Has_Interfaces (Tagged_Type)
1710 and then not Is_Interface (Tagged_Type));
1712 -- Ensure that the internal entities are added to the scope of the type
1714 if Scope (Tagged_Type) /= Current_Scope then
1715 Push_Scope (Scope (Tagged_Type));
1716 Restore_Scope := True;
1717 end if;
1719 Collect_Interfaces (Tagged_Type, Ifaces_List);
1721 Iface_Elmt := First_Elmt (Ifaces_List);
1722 while Present (Iface_Elmt) loop
1723 Iface := Node (Iface_Elmt);
1725 -- Originally we excluded here from this processing interfaces that
1726 -- are parents of Tagged_Type because their primitives are located
1727 -- in the primary dispatch table (and hence no auxiliary internal
1728 -- entities are required to handle secondary dispatch tables in such
1729 -- case). However, these auxiliary entities are also required to
1730 -- handle derivations of interfaces in formals of generics (see
1731 -- Derive_Subprograms).
1733 Elmt := First_Elmt (Primitive_Operations (Iface));
1734 while Present (Elmt) loop
1735 Iface_Prim := Node (Elmt);
1737 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1738 Prim :=
1739 Find_Primitive_Covering_Interface
1740 (Tagged_Type => Tagged_Type,
1741 Iface_Prim => Iface_Prim);
1743 if No (Prim) and then Serious_Errors_Detected > 0 then
1744 goto Continue;
1745 end if;
1747 pragma Assert (Present (Prim));
1749 -- Check subtype conformance; we skip this check if errors have
1750 -- been reported in the primitive (or in the formals of the
1751 -- primitive) because Find_Primitive_Covering_Interface relies
1752 -- on the subprogram Type_Conformant to locate the primitive,
1753 -- and reports errors if the formals don't match.
1755 if not Error_Posted (Prim)
1756 and then not Error_Posted_In_Formals (Prim)
1757 then
1758 declare
1759 Alias_Prim : Entity_Id;
1760 Alias_Typ : Entity_Id;
1761 Err_Loc : Node_Id := Empty;
1762 Ret_Type : Entity_Id;
1764 begin
1765 -- For inherited primitives, in case of reporting an
1766 -- error, the error must be reported on this primitive
1767 -- (i.e. in the name of its type declaration); otherwise
1768 -- the error would be reported in the formal of the
1769 -- alias primitive defined on its parent type.
1771 if Nkind (Parent (Prim)) = N_Full_Type_Declaration then
1772 Err_Loc := Prim;
1773 end if;
1775 -- Check subtype conformance of procedures, functions
1776 -- with matching return type, or functions not returning
1777 -- interface types.
1779 if Ekind (Prim) = E_Procedure
1780 or else Etype (Iface_Prim) = Etype (Prim)
1781 or else not Is_Interface (Etype (Iface_Prim))
1782 then
1783 Check_Subtype_Conformant
1784 (New_Id => Prim,
1785 Old_Id => Iface_Prim,
1786 Err_Loc => Err_Loc,
1787 Skip_Controlling_Formals => True);
1789 -- Check subtype conformance of functions returning an
1790 -- interface type; temporarily force both entities to
1791 -- return the same type. Required because subprogram
1792 -- Subtype_Conformant does not handle this case.
1794 else
1795 Ret_Type := Etype (Iface_Prim);
1796 Set_Etype (Iface_Prim, Etype (Prim));
1798 Check_Subtype_Conformant
1799 (New_Id => Prim,
1800 Old_Id => Iface_Prim,
1801 Err_Loc => Err_Loc,
1802 Skip_Controlling_Formals => True);
1804 Set_Etype (Iface_Prim, Ret_Type);
1805 end if;
1807 -- Complete the error when reported on inherited
1808 -- primitives.
1810 if Nkind (Parent (Prim)) = N_Full_Type_Declaration
1811 and then (Error_Posted (Prim)
1812 or else Error_Posted_In_Formals (Prim))
1813 and then Present (Alias (Prim))
1814 then
1815 Alias_Prim := Ultimate_Alias (Prim);
1816 Alias_Typ := Find_Dispatching_Type (Alias_Prim);
1818 if Alias_Typ /= Tagged_Type
1819 and then Is_Ancestor (Alias_Typ, Tagged_Type)
1820 then
1821 Error_Msg_Sloc := Sloc (Alias_Prim);
1822 Error_Msg_N
1823 ("in primitive inherited from #!", Prim);
1824 end if;
1825 end if;
1826 end;
1827 end if;
1829 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1830 -- differs from the name of the interface primitive then it is
1831 -- a private primitive inherited from a parent type. In such
1832 -- case, given that Tagged_Type covers the interface, the
1833 -- inherited private primitive becomes visible. For such
1834 -- purpose we add a new entity that renames the inherited
1835 -- private primitive.
1837 if Chars (Prim) /= Chars (Iface_Prim) then
1838 pragma Assert (Has_Suffix (Prim, 'P'));
1839 Derive_Subprogram
1840 (New_Subp => New_Subp,
1841 Parent_Subp => Iface_Prim,
1842 Derived_Type => Tagged_Type,
1843 Parent_Type => Iface);
1844 Set_Alias (New_Subp, Prim);
1845 Set_Is_Abstract_Subprogram
1846 (New_Subp, Is_Abstract_Subprogram (Prim));
1847 end if;
1849 Derive_Subprogram
1850 (New_Subp => New_Subp,
1851 Parent_Subp => Iface_Prim,
1852 Derived_Type => Tagged_Type,
1853 Parent_Type => Iface);
1855 declare
1856 Anc : Entity_Id;
1857 begin
1858 if Is_Inherited_Operation (Prim)
1859 and then Present (Alias (Prim))
1860 then
1861 Anc := Alias (Prim);
1862 else
1863 Anc := Overridden_Operation (Prim);
1864 end if;
1866 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1867 -- nonconforming preconditions in both an ancestor and
1868 -- a progenitor operation.
1870 -- If the operation is a primitive wrapper it is an explicit
1871 -- (overriding) operqtion and all is fine.
1873 if Present (Anc)
1874 and then Has_Non_Trivial_Precondition (Anc)
1875 and then Has_Non_Trivial_Precondition (Iface_Prim)
1876 then
1877 if Is_Abstract_Subprogram (Prim)
1878 or else
1879 (Ekind (Prim) = E_Procedure
1880 and then Nkind (Parent (Prim)) =
1881 N_Procedure_Specification
1882 and then Null_Present (Parent (Prim)))
1883 or else Is_Primitive_Wrapper (Prim)
1884 then
1885 null;
1887 -- The operation is inherited and must be overridden
1889 elsif not Comes_From_Source (Prim) then
1890 Error_Msg_NE
1891 ("&inherits non-conforming preconditions and must "
1892 & "be overridden (RM 6.1.1 (10-16))",
1893 Parent (Tagged_Type), Prim);
1894 end if;
1895 end if;
1896 end;
1898 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1899 -- associated with interface types. These entities are
1900 -- only registered in the list of primitives of its
1901 -- corresponding tagged type because they are only used
1902 -- to fill the contents of the secondary dispatch tables.
1903 -- Therefore they are removed from the homonym chains.
1905 Set_Is_Hidden (New_Subp);
1906 Set_Is_Internal (New_Subp);
1907 Set_Alias (New_Subp, Prim);
1908 Set_Is_Abstract_Subprogram
1909 (New_Subp, Is_Abstract_Subprogram (Prim));
1910 Set_Interface_Alias (New_Subp, Iface_Prim);
1912 -- If the returned type is an interface then propagate it to
1913 -- the returned type. Needed by the thunk to generate the code
1914 -- which displaces "this" to reference the corresponding
1915 -- secondary dispatch table in the returned object.
1917 if Is_Interface (Etype (Iface_Prim)) then
1918 Set_Etype (New_Subp, Etype (Iface_Prim));
1919 end if;
1921 -- Internal entities associated with interface types are only
1922 -- registered in the list of primitives of the tagged type.
1923 -- They are only used to fill the contents of the secondary
1924 -- dispatch tables. Therefore they are not needed in the
1925 -- homonym chains.
1927 Remove_Homonym (New_Subp);
1929 -- Hidden entities associated with interfaces must have set
1930 -- the Has_Delay_Freeze attribute to ensure that, in case
1931 -- of locally defined tagged types (or compiling with static
1932 -- dispatch tables generation disabled) the corresponding
1933 -- entry of the secondary dispatch table is filled when such
1934 -- an entity is frozen.
1936 Set_Has_Delayed_Freeze (New_Subp);
1937 end if;
1939 <<Continue>>
1940 Next_Elmt (Elmt);
1941 end loop;
1943 Next_Elmt (Iface_Elmt);
1944 end loop;
1946 if Restore_Scope then
1947 Pop_Scope;
1948 end if;
1949 end Add_Internal_Interface_Entities;
1951 -----------------------------------
1952 -- Analyze_Component_Declaration --
1953 -----------------------------------
1955 procedure Analyze_Component_Declaration (N : Node_Id) is
1956 Id : constant Entity_Id := Defining_Identifier (N);
1957 E : constant Node_Id := Expression (N);
1958 Typ : constant Node_Id :=
1959 Subtype_Indication (Component_Definition (N));
1960 T : Entity_Id;
1961 P : Entity_Id;
1963 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1964 -- Typ is the type of the current component, check whether this type is
1965 -- a limited type. Used to validate declaration against that of
1966 -- enclosing record.
1968 procedure Add_Range_Checks (Subt_Indic : Node_Id);
1969 -- Adds range constraint checks for a subtype indication
1971 ----------------------
1972 -- Is_Known_Limited --
1973 ----------------------
1975 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1976 P : constant Entity_Id := Etype (Typ);
1977 R : constant Entity_Id := Root_Type (Typ);
1979 begin
1980 if Is_Limited_Record (Typ) then
1981 return True;
1983 -- If the root type is limited (and not a limited interface) so is
1984 -- the current type.
1986 elsif Is_Limited_Record (R)
1987 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1988 then
1989 return True;
1991 -- Else the type may have a limited interface progenitor, but a
1992 -- limited record parent that is not an interface.
1994 elsif R /= P
1995 and then Is_Limited_Record (P)
1996 and then not Is_Interface (P)
1997 then
1998 return True;
2000 else
2001 return False;
2002 end if;
2003 end Is_Known_Limited;
2005 ----------------------
2006 -- Add_Range_Checks --
2007 ----------------------
2009 procedure Add_Range_Checks (Subt_Indic : Node_Id)
2012 begin
2013 if Present (Subt_Indic) and then
2014 Nkind (Subt_Indic) = N_Subtype_Indication and then
2015 Nkind (Constraint (Subt_Indic)) = N_Index_Or_Discriminant_Constraint
2016 then
2018 declare
2019 Typ : constant Entity_Id := Entity (Subtype_Mark (Subt_Indic));
2020 Indic_Typ : constant Entity_Id := Underlying_Type (Typ);
2021 Subt_Index : Node_Id;
2022 Target_Index : Node_Id;
2023 begin
2025 if Present (Indic_Typ) and then Is_Array_Type (Indic_Typ) then
2027 Target_Index := First_Index (Indic_Typ);
2028 Subt_Index := First (Constraints (Constraint (Subt_Indic)));
2030 while Present (Target_Index) loop
2031 if Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
2032 and then Is_Scalar_Type (Entity (Subt_Index))
2033 and then
2034 Nkind (Scalar_Range (Entity (Subt_Index))) = N_Range
2035 then
2036 Apply_Range_Check
2037 (Expr => Scalar_Range (Entity (Subt_Index)),
2038 Target_Typ => Etype (Target_Index),
2039 Insert_Node => Subt_Indic);
2040 end if;
2042 Next (Subt_Index);
2043 Next_Index (Target_Index);
2044 end loop;
2045 end if;
2046 end;
2047 end if;
2048 end Add_Range_Checks;
2050 -- Start of processing for Analyze_Component_Declaration
2052 begin
2053 Generate_Definition (Id);
2054 Enter_Name (Id);
2056 if Present (Typ) then
2057 T := Find_Type_Of_Object
2058 (Subtype_Indication (Component_Definition (N)), N);
2060 -- Ada 2005 (AI-230): Access Definition case
2062 else
2063 pragma Assert (Present
2064 (Access_Definition (Component_Definition (N))));
2066 T := Access_Definition
2067 (Related_Nod => N,
2068 N => Access_Definition (Component_Definition (N)));
2069 Set_Is_Local_Anonymous_Access (T);
2071 -- Ada 2005 (AI-254)
2073 if Present (Access_To_Subprogram_Definition
2074 (Access_Definition (Component_Definition (N))))
2075 and then Protected_Present (Access_To_Subprogram_Definition
2076 (Access_Definition
2077 (Component_Definition (N))))
2078 then
2079 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2080 end if;
2081 end if;
2083 -- If the subtype is a constrained subtype of the enclosing record,
2084 -- (which must have a partial view) the back-end does not properly
2085 -- handle the recursion. Rewrite the component declaration with an
2086 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2087 -- the tree directly because side effects have already been removed from
2088 -- discriminant constraints.
2090 if Ekind (T) = E_Access_Subtype
2091 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2092 and then Comes_From_Source (T)
2093 and then Nkind (Parent (T)) = N_Subtype_Declaration
2094 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2095 then
2096 Rewrite
2097 (Subtype_Indication (Component_Definition (N)),
2098 New_Copy_Tree (Subtype_Indication (Parent (T))));
2099 T := Find_Type_Of_Object
2100 (Subtype_Indication (Component_Definition (N)), N);
2101 end if;
2103 -- If the component declaration includes a default expression, then we
2104 -- check that the component is not of a limited type (RM 3.7(5)),
2105 -- and do the special preanalysis of the expression (see section on
2106 -- "Handling of Default and Per-Object Expressions" in the spec of
2107 -- package Sem).
2109 if Present (E) then
2110 Preanalyze_Default_Expression (E, T);
2111 Check_Initialization (T, E);
2113 if Ada_Version >= Ada_2005
2114 and then Ekind (T) = E_Anonymous_Access_Type
2115 and then Etype (E) /= Any_Type
2116 then
2117 -- Check RM 3.9.2(9): "if the expected type for an expression is
2118 -- an anonymous access-to-specific tagged type, then the object
2119 -- designated by the expression shall not be dynamically tagged
2120 -- unless it is a controlling operand in a call on a dispatching
2121 -- operation"
2123 if Is_Tagged_Type (Directly_Designated_Type (T))
2124 and then
2125 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2126 and then
2127 Ekind (Directly_Designated_Type (Etype (E))) =
2128 E_Class_Wide_Type
2129 then
2130 Error_Msg_N
2131 ("access to specific tagged type required (RM 3.9.2(9))", E);
2132 end if;
2134 -- (Ada 2005: AI-230): Accessibility check for anonymous
2135 -- components
2137 if Type_Access_Level (Etype (E)) >
2138 Deepest_Type_Access_Level (T)
2139 then
2140 Error_Msg_N
2141 ("expression has deeper access level than component " &
2142 "(RM 3.10.2 (12.2))", E);
2143 end if;
2145 -- The initialization expression is a reference to an access
2146 -- discriminant. The type of the discriminant is always deeper
2147 -- than any access type.
2149 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2150 and then Is_Entity_Name (E)
2151 and then Ekind (Entity (E)) = E_In_Parameter
2152 and then Present (Discriminal_Link (Entity (E)))
2153 then
2154 Error_Msg_N
2155 ("discriminant has deeper accessibility level than target",
2157 end if;
2158 end if;
2159 end if;
2161 -- The parent type may be a private view with unknown discriminants,
2162 -- and thus unconstrained. Regular components must be constrained.
2164 if not Is_Definite_Subtype (T)
2165 and then Chars (Id) /= Name_uParent
2166 then
2167 if Is_Class_Wide_Type (T) then
2168 Error_Msg_N
2169 ("class-wide subtype with unknown discriminants" &
2170 " in component declaration",
2171 Subtype_Indication (Component_Definition (N)));
2172 else
2173 Error_Msg_N
2174 ("unconstrained subtype in component declaration",
2175 Subtype_Indication (Component_Definition (N)));
2176 end if;
2178 -- Components cannot be abstract, except for the special case of
2179 -- the _Parent field (case of extending an abstract tagged type)
2181 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2182 Error_Msg_N ("type of a component cannot be abstract", N);
2183 end if;
2185 Set_Etype (Id, T);
2187 if Aliased_Present (Component_Definition (N)) then
2188 Set_Is_Aliased (Id);
2190 -- AI12-001: All aliased objects are considered to be specified as
2191 -- independently addressable (RM C.6(8.1/4)).
2193 Set_Is_Independent (Id);
2194 end if;
2196 -- The component declaration may have a per-object constraint, set
2197 -- the appropriate flag in the defining identifier of the subtype.
2199 if Has_Discriminant_Dependent_Constraint (Id) then
2200 Set_Has_Per_Object_Constraint (Id);
2201 end if;
2203 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2204 -- out some static checks.
2206 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2207 Null_Exclusion_Static_Checks (N);
2208 end if;
2210 -- If this component is private (or depends on a private type), flag the
2211 -- record type to indicate that some operations are not available.
2213 P := Private_Component (T);
2215 if Present (P) then
2217 -- Check for circular definitions
2219 if P = Any_Type then
2220 Set_Etype (Id, Any_Type);
2222 -- There is a gap in the visibility of operations only if the
2223 -- component type is not defined in the scope of the record type.
2225 elsif Scope (P) = Scope (Current_Scope) then
2226 null;
2228 elsif Is_Limited_Type (P) then
2229 Set_Is_Limited_Composite (Current_Scope);
2231 else
2232 Set_Is_Private_Composite (Current_Scope);
2233 end if;
2234 end if;
2236 if P /= Any_Type
2237 and then Is_Limited_Type (T)
2238 and then Chars (Id) /= Name_uParent
2239 and then Is_Tagged_Type (Current_Scope)
2240 then
2241 if Is_Derived_Type (Current_Scope)
2242 and then not Is_Known_Limited (Current_Scope)
2243 then
2244 Error_Msg_N
2245 ("extension of nonlimited type cannot have limited components",
2248 if Is_Interface (Root_Type (Current_Scope)) then
2249 Error_Msg_N
2250 ("\limitedness is not inherited from limited interface", N);
2251 Error_Msg_N ("\add LIMITED to type indication", N);
2252 end if;
2254 Explain_Limited_Type (T, N);
2255 Set_Etype (Id, Any_Type);
2256 Set_Is_Limited_Composite (Current_Scope, False);
2258 elsif not Is_Derived_Type (Current_Scope)
2259 and then not Is_Limited_Record (Current_Scope)
2260 and then not Is_Concurrent_Type (Current_Scope)
2261 then
2262 Error_Msg_N
2263 ("nonlimited tagged type cannot have limited components", N);
2264 Explain_Limited_Type (T, N);
2265 Set_Etype (Id, Any_Type);
2266 Set_Is_Limited_Composite (Current_Scope, False);
2267 end if;
2268 end if;
2270 Set_Original_Record_Component (Id, Id);
2272 Analyze_Aspect_Specifications (N, Id);
2274 Analyze_Dimension (N);
2276 Add_Range_Checks (Subtype_Indication (Component_Definition (N)));
2278 end Analyze_Component_Declaration;
2280 --------------------------
2281 -- Analyze_Declarations --
2282 --------------------------
2284 procedure Analyze_Declarations (L : List_Id) is
2285 Decl : Node_Id;
2287 procedure Adjust_Decl;
2288 -- Adjust Decl not to include implicit label declarations, since these
2289 -- have strange Sloc values that result in elaboration check problems.
2290 -- (They have the sloc of the label as found in the source, and that
2291 -- is ahead of the current declarative part).
2293 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2294 -- Create the subprogram bodies which verify the run-time semantics of
2295 -- the pragmas listed below for each elibigle type found in declarative
2296 -- list Decls. The pragmas are:
2298 -- Default_Initial_Condition
2299 -- Invariant
2300 -- Type_Invariant
2302 -- Context denotes the owner of the declarative list.
2304 procedure Check_Entry_Contracts;
2305 -- Perform a preanalysis of the pre- and postconditions of an entry
2306 -- declaration. This must be done before full resolution and creation
2307 -- of the parameter block, etc. to catch illegal uses within the
2308 -- contract expression. Full analysis of the expression is done when
2309 -- the contract is processed.
2311 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2312 -- Check if a nested package has entities within it that rely on library
2313 -- level private types where the full view has not been completed for
2314 -- the purposes of checking if it is acceptable to freeze an expression
2315 -- function at the point of declaration.
2317 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2318 -- Determine whether Body_Decl denotes the body of a late controlled
2319 -- primitive (either Initialize, Adjust or Finalize). If this is the
2320 -- case, add a proper spec if the body lacks one. The spec is inserted
2321 -- before Body_Decl and immediately analyzed.
2323 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2324 -- Spec_Id is the entity of a package that may define abstract states,
2325 -- and in the case of a child unit, whose ancestors may define abstract
2326 -- states. If the states have partial visible refinement, remove the
2327 -- partial visibility of each constituent at the end of the package
2328 -- spec and body declarations.
2330 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2331 -- Spec_Id is the entity of a package that may define abstract states.
2332 -- If the states have visible refinement, remove the visibility of each
2333 -- constituent at the end of the package body declaration.
2335 procedure Resolve_Aspects;
2336 -- Utility to resolve the expressions of aspects at the end of a list of
2337 -- declarations, or before a declaration that freezes previous entities,
2338 -- such as in a subprogram body.
2340 -----------------
2341 -- Adjust_Decl --
2342 -----------------
2344 procedure Adjust_Decl is
2345 begin
2346 while Present (Prev (Decl))
2347 and then Nkind (Decl) = N_Implicit_Label_Declaration
2348 loop
2349 Prev (Decl);
2350 end loop;
2351 end Adjust_Decl;
2353 ----------------------------
2354 -- Build_Assertion_Bodies --
2355 ----------------------------
2357 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2358 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2359 -- Create the subprogram bodies which verify the run-time semantics
2360 -- of the pragmas listed below for type Typ. The pragmas are:
2362 -- Default_Initial_Condition
2363 -- Invariant
2364 -- Type_Invariant
2366 -------------------------------------
2367 -- Build_Assertion_Bodies_For_Type --
2368 -------------------------------------
2370 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2371 begin
2372 if Nkind (Context) = N_Package_Specification then
2374 -- Preanalyze and resolve the class-wide invariants of an
2375 -- interface at the end of whichever declarative part has the
2376 -- interface type. Note that an interface may be declared in
2377 -- any non-package declarative part, but reaching the end of
2378 -- such a declarative part will always freeze the type and
2379 -- generate the invariant procedure (see Freeze_Type).
2381 if Is_Interface (Typ) then
2383 -- Interfaces are treated as the partial view of a private
2384 -- type, in order to achieve uniformity with the general
2385 -- case. As a result, an interface receives only a "partial"
2386 -- invariant procedure, which is never called.
2388 if Has_Own_Invariants (Typ) then
2389 Build_Invariant_Procedure_Body
2390 (Typ => Typ,
2391 Partial_Invariant => True);
2392 end if;
2394 elsif Decls = Visible_Declarations (Context) then
2395 -- Preanalyze and resolve the invariants of a private type
2396 -- at the end of the visible declarations to catch potential
2397 -- errors. Inherited class-wide invariants are not included
2398 -- because they have already been resolved.
2400 if Ekind (Typ) in E_Limited_Private_Type
2401 | E_Private_Type
2402 | E_Record_Type_With_Private
2403 and then Has_Own_Invariants (Typ)
2404 then
2405 Build_Invariant_Procedure_Body
2406 (Typ => Typ,
2407 Partial_Invariant => True);
2408 end if;
2410 -- Preanalyze and resolve the Default_Initial_Condition
2411 -- assertion expression at the end of the declarations to
2412 -- catch any errors.
2414 if Ekind (Typ) in E_Limited_Private_Type
2415 | E_Private_Type
2416 | E_Record_Type_With_Private
2417 and then Has_Own_DIC (Typ)
2418 then
2419 Build_DIC_Procedure_Body
2420 (Typ => Typ,
2421 Partial_DIC => True);
2422 end if;
2424 elsif Decls = Private_Declarations (Context) then
2426 -- Preanalyze and resolve the invariants of a private type's
2427 -- full view at the end of the private declarations to catch
2428 -- potential errors.
2430 if (not Is_Private_Type (Typ)
2431 or else Present (Underlying_Full_View (Typ)))
2432 and then Has_Private_Declaration (Typ)
2433 and then Has_Invariants (Typ)
2434 then
2435 Build_Invariant_Procedure_Body (Typ);
2436 end if;
2438 if (not Is_Private_Type (Typ)
2439 or else Present (Underlying_Full_View (Typ)))
2440 and then Has_Private_Declaration (Typ)
2441 and then Has_DIC (Typ)
2442 then
2443 Build_DIC_Procedure_Body (Typ);
2444 end if;
2445 end if;
2446 end if;
2447 end Build_Assertion_Bodies_For_Type;
2449 -- Local variables
2451 Decl : Node_Id;
2452 Decl_Id : Entity_Id;
2454 -- Start of processing for Build_Assertion_Bodies
2456 begin
2457 Decl := First (Decls);
2458 while Present (Decl) loop
2459 if Is_Declaration (Decl) then
2460 Decl_Id := Defining_Entity (Decl);
2462 if Is_Type (Decl_Id) then
2463 Build_Assertion_Bodies_For_Type (Decl_Id);
2464 end if;
2465 end if;
2467 Next (Decl);
2468 end loop;
2469 end Build_Assertion_Bodies;
2471 ---------------------------
2472 -- Check_Entry_Contracts --
2473 ---------------------------
2475 procedure Check_Entry_Contracts is
2476 ASN : Node_Id;
2477 Ent : Entity_Id;
2478 Exp : Node_Id;
2480 begin
2481 Ent := First_Entity (Current_Scope);
2482 while Present (Ent) loop
2484 -- This only concerns entries with pre/postconditions
2486 if Ekind (Ent) = E_Entry
2487 and then Present (Contract (Ent))
2488 and then Present (Pre_Post_Conditions (Contract (Ent)))
2489 then
2490 ASN := Pre_Post_Conditions (Contract (Ent));
2491 Push_Scope (Ent);
2492 Install_Formals (Ent);
2494 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2495 -- is performed on a copy of the pragma expression, to prevent
2496 -- modifying the original expression.
2498 while Present (ASN) loop
2499 if Nkind (ASN) = N_Pragma then
2500 Exp :=
2501 New_Copy_Tree
2502 (Expression
2503 (First (Pragma_Argument_Associations (ASN))));
2504 Set_Parent (Exp, ASN);
2506 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2507 end if;
2509 ASN := Next_Pragma (ASN);
2510 end loop;
2512 End_Scope;
2513 end if;
2515 Next_Entity (Ent);
2516 end loop;
2517 end Check_Entry_Contracts;
2519 ----------------------------------
2520 -- Contains_Lib_Incomplete_Type --
2521 ----------------------------------
2523 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2524 Curr : Entity_Id;
2526 begin
2527 -- Avoid looking through scopes that do not meet the precondition of
2528 -- Pkg not being within a library unit spec.
2530 if not Is_Compilation_Unit (Pkg)
2531 and then not Is_Generic_Instance (Pkg)
2532 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2533 then
2534 -- Loop through all entities in the current scope to identify
2535 -- an entity that depends on a private type.
2537 Curr := First_Entity (Pkg);
2538 loop
2539 if Nkind (Curr) in N_Entity
2540 and then Depends_On_Private (Curr)
2541 then
2542 return True;
2543 end if;
2545 exit when Last_Entity (Current_Scope) = Curr;
2546 Next_Entity (Curr);
2547 end loop;
2548 end if;
2550 return False;
2551 end Contains_Lib_Incomplete_Type;
2553 --------------------------------------
2554 -- Handle_Late_Controlled_Primitive --
2555 --------------------------------------
2557 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2558 Body_Spec : constant Node_Id := Specification (Body_Decl);
2559 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2560 Loc : constant Source_Ptr := Sloc (Body_Id);
2561 Params : constant List_Id :=
2562 Parameter_Specifications (Body_Spec);
2563 Spec : Node_Id;
2564 Spec_Id : Entity_Id;
2565 Typ : Node_Id;
2567 begin
2568 -- Consider only procedure bodies whose name matches one of the three
2569 -- controlled primitives.
2571 if Nkind (Body_Spec) /= N_Procedure_Specification
2572 or else Chars (Body_Id) not in Name_Adjust
2573 | Name_Finalize
2574 | Name_Initialize
2575 then
2576 return;
2578 -- A controlled primitive must have exactly one formal which is not
2579 -- an anonymous access type.
2581 elsif List_Length (Params) /= 1 then
2582 return;
2583 end if;
2585 Typ := Parameter_Type (First (Params));
2587 if Nkind (Typ) = N_Access_Definition then
2588 return;
2589 end if;
2591 Find_Type (Typ);
2593 -- The type of the formal must be derived from [Limited_]Controlled
2595 if not Is_Controlled (Entity (Typ)) then
2596 return;
2597 end if;
2599 -- Check whether a specification exists for this body. We do not
2600 -- analyze the spec of the body in full, because it will be analyzed
2601 -- again when the body is properly analyzed, and we cannot create
2602 -- duplicate entries in the formals chain. We look for an explicit
2603 -- specification because the body may be an overriding operation and
2604 -- an inherited spec may be present.
2606 Spec_Id := Current_Entity (Body_Id);
2608 while Present (Spec_Id) loop
2609 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2610 and then Scope (Spec_Id) = Current_Scope
2611 and then Present (First_Formal (Spec_Id))
2612 and then No (Next_Formal (First_Formal (Spec_Id)))
2613 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2614 and then Comes_From_Source (Spec_Id)
2615 then
2616 return;
2617 end if;
2619 Spec_Id := Homonym (Spec_Id);
2620 end loop;
2622 -- At this point the body is known to be a late controlled primitive.
2623 -- Generate a matching spec and insert it before the body. Note the
2624 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2625 -- tree in this case.
2627 Spec := Copy_Separate_Tree (Body_Spec);
2629 -- Ensure that the subprogram declaration does not inherit the null
2630 -- indicator from the body as we now have a proper spec/body pair.
2632 Set_Null_Present (Spec, False);
2634 -- Ensure that the freeze node is inserted after the declaration of
2635 -- the primitive since its expansion will freeze the primitive.
2637 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2639 Insert_Before_And_Analyze (Body_Decl, Decl);
2640 end Handle_Late_Controlled_Primitive;
2642 ----------------------------------------
2643 -- Remove_Partial_Visible_Refinements --
2644 ----------------------------------------
2646 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2647 State_Elmt : Elmt_Id;
2648 begin
2649 if Present (Abstract_States (Spec_Id)) then
2650 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2651 while Present (State_Elmt) loop
2652 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2653 Next_Elmt (State_Elmt);
2654 end loop;
2655 end if;
2657 -- For a child unit, also hide the partial state refinement from
2658 -- ancestor packages.
2660 if Is_Child_Unit (Spec_Id) then
2661 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2662 end if;
2663 end Remove_Partial_Visible_Refinements;
2665 --------------------------------
2666 -- Remove_Visible_Refinements --
2667 --------------------------------
2669 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2670 State_Elmt : Elmt_Id;
2671 begin
2672 if Present (Abstract_States (Spec_Id)) then
2673 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2674 while Present (State_Elmt) loop
2675 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2676 Next_Elmt (State_Elmt);
2677 end loop;
2678 end if;
2679 end Remove_Visible_Refinements;
2681 ---------------------
2682 -- Resolve_Aspects --
2683 ---------------------
2685 procedure Resolve_Aspects is
2686 E : Entity_Id;
2688 begin
2689 E := First_Entity (Current_Scope);
2690 while Present (E) loop
2691 Resolve_Aspect_Expressions (E);
2693 -- Now that the aspect expressions have been resolved, if this is
2694 -- at the end of the visible declarations, we can set the flag
2695 -- Known_To_Have_Preelab_Init properly on types declared in the
2696 -- visible part, which is needed for checking whether full types
2697 -- in the private part satisfy the Preelaborable_Initialization
2698 -- aspect of the partial view. We can't wait for the creation of
2699 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2700 -- freeze point may occur after the end of the package declaration
2701 -- (in the case of nested packages).
2703 if Is_Type (E)
2704 and then L = Visible_Declarations (Parent (L))
2705 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2706 then
2707 declare
2708 ASN : constant Node_Id :=
2709 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2710 Expr : constant Node_Id := Expression (ASN);
2711 begin
2712 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2713 -- expression, or if the expression is True (or was folded
2714 -- to True), or if the expression is a conjunction of one or
2715 -- more Preelaborable_Initialization attributes applied to
2716 -- formal types and wasn't folded to False. (Note that
2717 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2718 -- Original_Node if needed, hence test for Standard_False.)
2720 if No (Expr)
2721 or else (Is_Entity_Name (Expr)
2722 and then Entity (Expr) = Standard_True)
2723 or else
2724 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2725 and then
2726 not (Is_Entity_Name (Expr)
2727 and then Entity (Expr) = Standard_False))
2728 then
2729 Set_Known_To_Have_Preelab_Init (E);
2730 end if;
2731 end;
2732 end if;
2734 Next_Entity (E);
2735 end loop;
2736 end Resolve_Aspects;
2738 -- Local variables
2740 Context : Node_Id := Empty;
2741 Ctrl_Typ : Entity_Id := Empty;
2742 Freeze_From : Entity_Id := Empty;
2743 Next_Decl : Node_Id;
2745 -- Start of processing for Analyze_Declarations
2747 begin
2748 Decl := First (L);
2749 while Present (Decl) loop
2751 -- Complete analysis of declaration
2753 Analyze (Decl);
2754 Next_Decl := Next (Decl);
2756 if No (Freeze_From) then
2757 Freeze_From := First_Entity (Current_Scope);
2758 end if;
2760 -- Remember if the declaration we just processed is the full type
2761 -- declaration of a controlled type (to handle late overriding of
2762 -- initialize, adjust or finalize).
2764 if Nkind (Decl) = N_Full_Type_Declaration
2765 and then Is_Controlled (Defining_Identifier (Decl))
2766 then
2767 Ctrl_Typ := Defining_Identifier (Decl);
2768 end if;
2770 -- At the end of a declarative part, freeze remaining entities
2771 -- declared in it. The end of the visible declarations of package
2772 -- specification is not the end of a declarative part if private
2773 -- declarations are present. The end of a package declaration is a
2774 -- freezing point only if it a library package. A task definition or
2775 -- protected type definition is not a freeze point either. Finally,
2776 -- we do not freeze entities in generic scopes, because there is no
2777 -- code generated for them and freeze nodes will be generated for
2778 -- the instance.
2780 -- The end of a package instantiation is not a freeze point, but
2781 -- for now we make it one, because the generic body is inserted
2782 -- (currently) immediately after. Generic instantiations will not
2783 -- be a freeze point once delayed freezing of bodies is implemented.
2784 -- (This is needed in any case for early instantiations ???).
2786 if No (Next_Decl) then
2787 if Nkind (Parent (L)) = N_Component_List then
2788 null;
2790 elsif Nkind (Parent (L)) in
2791 N_Protected_Definition | N_Task_Definition
2792 then
2793 Check_Entry_Contracts;
2795 elsif Nkind (Parent (L)) /= N_Package_Specification then
2796 if Nkind (Parent (L)) = N_Package_Body then
2797 Freeze_From := First_Entity (Current_Scope);
2798 end if;
2800 -- There may have been several freezing points previously,
2801 -- for example object declarations or subprogram bodies, but
2802 -- at the end of a declarative part we check freezing from
2803 -- the beginning, even though entities may already be frozen,
2804 -- in order to perform visibility checks on delayed aspects.
2806 Adjust_Decl;
2808 -- If the current scope is a generic subprogram body. Skip the
2809 -- generic formal parameters that are not frozen here.
2811 if Is_Subprogram (Current_Scope)
2812 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2813 N_Generic_Subprogram_Declaration
2814 and then Present (First_Entity (Current_Scope))
2815 then
2816 while Is_Generic_Formal (Freeze_From) loop
2817 Next_Entity (Freeze_From);
2818 end loop;
2820 Freeze_All (Freeze_From, Decl);
2821 Freeze_From := Last_Entity (Current_Scope);
2823 else
2824 -- For declarations in a subprogram body there is no issue
2825 -- with name resolution in aspect specifications.
2827 Freeze_All (First_Entity (Current_Scope), Decl);
2828 Freeze_From := Last_Entity (Current_Scope);
2829 end if;
2831 -- Current scope is a package specification
2833 elsif Scope (Current_Scope) /= Standard_Standard
2834 and then not Is_Child_Unit (Current_Scope)
2835 and then No (Generic_Parent (Parent (L)))
2836 then
2837 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2838 -- resolved at the end of the immediately enclosing declaration
2839 -- list (AI05-0183-1).
2841 Resolve_Aspects;
2843 elsif L /= Visible_Declarations (Parent (L))
2844 or else Is_Empty_List (Private_Declarations (Parent (L)))
2845 then
2846 Adjust_Decl;
2848 -- End of a package declaration
2850 -- This is a freeze point because it is the end of a
2851 -- compilation unit.
2853 Freeze_All (First_Entity (Current_Scope), Decl);
2854 Freeze_From := Last_Entity (Current_Scope);
2856 -- At the end of the visible declarations the expressions in
2857 -- aspects of all entities declared so far must be resolved.
2858 -- The entities themselves might be frozen later, and the
2859 -- generated pragmas and attribute definition clauses analyzed
2860 -- in full at that point, but name resolution must take place
2861 -- now.
2862 -- In addition to being the proper semantics, this is mandatory
2863 -- within generic units, because global name capture requires
2864 -- those expressions to be analyzed, given that the generated
2865 -- pragmas do not appear in the original generic tree.
2867 elsif Serious_Errors_Detected = 0 then
2868 Resolve_Aspects;
2869 end if;
2871 -- If next node is a body then freeze all types before the body.
2872 -- An exception occurs for some expander-generated bodies. If these
2873 -- are generated at places where in general language rules would not
2874 -- allow a freeze point, then we assume that the expander has
2875 -- explicitly checked that all required types are properly frozen,
2876 -- and we do not cause general freezing here. This special circuit
2877 -- is used when the encountered body is marked as having already
2878 -- been analyzed.
2880 -- In all other cases (bodies that come from source, and expander
2881 -- generated bodies that have not been analyzed yet), freeze all
2882 -- types now. Note that in the latter case, the expander must take
2883 -- care to attach the bodies at a proper place in the tree so as to
2884 -- not cause unwanted freezing at that point.
2886 -- It is also necessary to check for a case where both an expression
2887 -- function is used and the current scope depends on an incomplete
2888 -- private type from a library unit, otherwise premature freezing of
2889 -- the private type will occur.
2891 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2892 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2893 or else not Was_Expression_Function (Next_Decl))
2894 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2895 and then not Contains_Lib_Incomplete_Type
2896 (Current_Scope)))
2897 then
2898 -- When a controlled type is frozen, the expander generates stream
2899 -- and controlled-type support routines. If the freeze is caused
2900 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2901 -- expander will end up using the wrong version of these routines,
2902 -- as the body has not been processed yet. To remedy this, detect
2903 -- a late controlled primitive and create a proper spec for it.
2904 -- This ensures that the primitive will override its inherited
2905 -- counterpart before the freeze takes place.
2907 -- If the declaration we just processed is a body, do not attempt
2908 -- to examine Next_Decl as the late primitive idiom can only apply
2909 -- to the first encountered body.
2911 -- ??? A cleaner approach may be possible and/or this solution
2912 -- could be extended to general-purpose late primitives.
2914 if Present (Ctrl_Typ) then
2916 -- No need to continue searching for late body overriding if
2917 -- the controlled type is already frozen.
2919 if Is_Frozen (Ctrl_Typ) then
2920 Ctrl_Typ := Empty;
2922 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2923 Handle_Late_Controlled_Primitive (Next_Decl);
2924 end if;
2925 end if;
2927 Adjust_Decl;
2929 -- The generated body of an expression function does not freeze,
2930 -- unless it is a completion, in which case only the expression
2931 -- itself freezes. This is handled when the body itself is
2932 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2934 Freeze_All (Freeze_From, Decl);
2935 Freeze_From := Last_Entity (Current_Scope);
2936 end if;
2938 Decl := Next_Decl;
2939 end loop;
2941 -- Post-freezing actions
2943 if Present (L) then
2944 Context := Parent (L);
2946 -- Certain contract annotations have forward visibility semantics and
2947 -- must be analyzed after all declarative items have been processed.
2948 -- This timing ensures that entities referenced by such contracts are
2949 -- visible.
2951 -- Analyze the contract of an immediately enclosing package spec or
2952 -- body first because other contracts may depend on its information.
2954 if Nkind (Context) = N_Package_Body then
2955 Analyze_Package_Body_Contract (Defining_Entity (Context));
2957 elsif Nkind (Context) = N_Package_Specification then
2958 Analyze_Package_Contract (Defining_Entity (Context));
2959 end if;
2961 -- Analyze the contracts of various constructs in the declarative
2962 -- list.
2964 Analyze_Contracts (L);
2966 if Nkind (Context) = N_Package_Body then
2968 -- Ensure that all abstract states and objects declared in the
2969 -- state space of a package body are utilized as constituents.
2971 Check_Unused_Body_States (Defining_Entity (Context));
2973 -- State refinements are visible up to the end of the package body
2974 -- declarations. Hide the state refinements from visibility to
2975 -- restore the original state conditions.
2977 Remove_Visible_Refinements (Corresponding_Spec (Context));
2978 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2980 elsif Nkind (Context) = N_Package_Specification then
2982 -- Partial state refinements are visible up to the end of the
2983 -- package spec declarations. Hide the partial state refinements
2984 -- from visibility to restore the original state conditions.
2986 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2987 end if;
2989 -- Verify that all abstract states found in any package declared in
2990 -- the input declarative list have proper refinements. The check is
2991 -- performed only when the context denotes a block, entry, package,
2992 -- protected, subprogram, or task body (SPARK RM 7.1.4(4) and SPARK
2993 -- RM 7.2.2(3)).
2995 Check_State_Refinements (Context);
2997 -- Create the subprogram bodies which verify the run-time semantics
2998 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2999 -- types within the current declarative list. This ensures that all
3000 -- assertion expressions are preanalyzed and resolved at the end of
3001 -- the declarative part. Note that the resolution happens even when
3002 -- freezing does not take place.
3004 Build_Assertion_Bodies (L, Context);
3005 end if;
3006 end Analyze_Declarations;
3008 -----------------------------------
3009 -- Analyze_Full_Type_Declaration --
3010 -----------------------------------
3012 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
3013 Def : constant Node_Id := Type_Definition (N);
3014 Def_Id : constant Entity_Id := Defining_Identifier (N);
3015 T : Entity_Id;
3016 Prev : Entity_Id;
3018 Is_Remote : constant Boolean :=
3019 (Is_Remote_Types (Current_Scope)
3020 or else Is_Remote_Call_Interface (Current_Scope))
3021 and then not (In_Private_Part (Current_Scope)
3022 or else In_Package_Body (Current_Scope));
3024 procedure Check_Nonoverridable_Aspects;
3025 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
3026 -- be overridden, and can only be confirmed on derivation.
3028 procedure Check_Ops_From_Incomplete_Type;
3029 -- If there is a tagged incomplete partial view of the type, traverse
3030 -- the primitives of the incomplete view and change the type of any
3031 -- controlling formals and result to indicate the full view. The
3032 -- primitives will be added to the full type's primitive operations
3033 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
3034 -- is called from Process_Incomplete_Dependents).
3036 ----------------------------------
3037 -- Check_Nonoverridable_Aspects --
3038 ----------------------------------
3040 procedure Check_Nonoverridable_Aspects is
3041 function Get_Aspect_Spec
3042 (Specs : List_Id;
3043 Aspect_Name : Name_Id) return Node_Id;
3044 -- Check whether a list of aspect specifications includes an entry
3045 -- for a specific aspect. The list is either that of a partial or
3046 -- a full view.
3048 ---------------------
3049 -- Get_Aspect_Spec --
3050 ---------------------
3052 function Get_Aspect_Spec
3053 (Specs : List_Id;
3054 Aspect_Name : Name_Id) return Node_Id
3056 Spec : Node_Id;
3058 begin
3059 Spec := First (Specs);
3060 while Present (Spec) loop
3061 if Chars (Identifier (Spec)) = Aspect_Name then
3062 return Spec;
3063 end if;
3064 Next (Spec);
3065 end loop;
3067 return Empty;
3068 end Get_Aspect_Spec;
3070 -- Local variables
3072 Prev_Aspects : constant List_Id :=
3073 Aspect_Specifications (Parent (Def_Id));
3074 Par_Type : Entity_Id;
3075 Prev_Aspect : Node_Id;
3077 -- Start of processing for Check_Nonoverridable_Aspects
3079 begin
3080 -- Get parent type of derived type. Note that Prev is the entity in
3081 -- the partial declaration, but its contents are now those of full
3082 -- view, while Def_Id reflects the partial view.
3084 if Is_Private_Type (Def_Id) then
3085 Par_Type := Etype (Full_View (Def_Id));
3086 else
3087 Par_Type := Etype (Def_Id);
3088 end if;
3090 -- If there is an inherited Implicit_Dereference, verify that it is
3091 -- made explicit in the partial view.
3093 if Has_Discriminants (Base_Type (Par_Type))
3094 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3095 and then Present (Discriminant_Specifications (Parent (Prev)))
3096 and then Present (Get_Reference_Discriminant (Par_Type))
3097 then
3098 Prev_Aspect :=
3099 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3101 if No (Prev_Aspect)
3102 and then Present
3103 (Discriminant_Specifications
3104 (Original_Node (Parent (Prev))))
3105 then
3106 Error_Msg_N
3107 ("type does not inherit implicit dereference", Prev);
3109 else
3110 -- If one of the views has the aspect specified, verify that it
3111 -- is consistent with that of the parent.
3113 declare
3114 Cur_Discr : constant Entity_Id :=
3115 Get_Reference_Discriminant (Prev);
3116 Par_Discr : constant Entity_Id :=
3117 Get_Reference_Discriminant (Par_Type);
3119 begin
3120 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3121 Error_Msg_N
3122 ("aspect inconsistent with that of parent", N);
3123 end if;
3125 -- Check that specification in partial view matches the
3126 -- inherited aspect. Compare names directly because aspect
3127 -- expression may not be analyzed.
3129 if Present (Prev_Aspect)
3130 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3131 and then Chars (Expression (Prev_Aspect)) /=
3132 Chars (Cur_Discr)
3133 then
3134 Error_Msg_N
3135 ("aspect inconsistent with that of parent", N);
3136 end if;
3137 end;
3138 end if;
3139 end if;
3141 -- What about other nonoverridable aspects???
3142 end Check_Nonoverridable_Aspects;
3144 ------------------------------------
3145 -- Check_Ops_From_Incomplete_Type --
3146 ------------------------------------
3148 procedure Check_Ops_From_Incomplete_Type is
3149 Elmt : Elmt_Id;
3150 Formal : Entity_Id;
3151 Op : Entity_Id;
3153 begin
3154 if Prev /= T
3155 and then Ekind (Prev) = E_Incomplete_Type
3156 and then Is_Tagged_Type (Prev)
3157 and then Is_Tagged_Type (T)
3158 and then Present (Primitive_Operations (Prev))
3159 then
3160 Elmt := First_Elmt (Primitive_Operations (Prev));
3161 while Present (Elmt) loop
3162 Op := Node (Elmt);
3164 Formal := First_Formal (Op);
3165 while Present (Formal) loop
3166 if Etype (Formal) = Prev then
3167 Set_Etype (Formal, T);
3168 end if;
3170 Next_Formal (Formal);
3171 end loop;
3173 if Etype (Op) = Prev then
3174 Set_Etype (Op, T);
3175 end if;
3177 Next_Elmt (Elmt);
3178 end loop;
3179 end if;
3180 end Check_Ops_From_Incomplete_Type;
3182 -- Start of processing for Analyze_Full_Type_Declaration
3184 begin
3185 Prev := Find_Type_Name (N);
3187 -- The full view, if present, now points to the current type. If there
3188 -- is an incomplete partial view, set a link to it, to simplify the
3189 -- retrieval of primitive operations of the type.
3191 -- Ada 2005 (AI-50217): If the type was previously decorated when
3192 -- imported through a LIMITED WITH clause, it appears as incomplete
3193 -- but has no full view.
3195 if Ekind (Prev) = E_Incomplete_Type
3196 and then Present (Full_View (Prev))
3197 then
3198 T := Full_View (Prev);
3199 Set_Incomplete_View (N, Prev);
3200 else
3201 T := Prev;
3202 end if;
3204 Set_Is_Pure (T, Is_Pure (Current_Scope));
3206 -- We set the flag Is_First_Subtype here. It is needed to set the
3207 -- corresponding flag for the Implicit class-wide-type created
3208 -- during tagged types processing.
3210 Set_Is_First_Subtype (T, True);
3212 -- Only composite types other than array types are allowed to have
3213 -- discriminants.
3215 case Nkind (Def) is
3217 -- For derived types, the rule will be checked once we've figured
3218 -- out the parent type.
3220 when N_Derived_Type_Definition =>
3221 null;
3223 -- For record types, discriminants are allowed.
3225 when N_Record_Definition =>
3226 null;
3228 when others =>
3229 if Present (Discriminant_Specifications (N)) then
3230 Error_Msg_N
3231 ("elementary or array type cannot have discriminants",
3232 Defining_Identifier
3233 (First (Discriminant_Specifications (N))));
3234 end if;
3235 end case;
3237 -- Elaborate the type definition according to kind, and generate
3238 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3239 -- already done (this happens during the reanalysis that follows a call
3240 -- to the high level optimizer).
3242 if not Analyzed (T) then
3243 Set_Analyzed (T);
3245 -- Set the SPARK mode from the current context
3247 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3248 Set_SPARK_Pragma_Inherited (T);
3250 case Nkind (Def) is
3251 when N_Access_To_Subprogram_Definition =>
3252 Access_Subprogram_Declaration (T, Def);
3254 -- If this is a remote access to subprogram, we must create the
3255 -- equivalent fat pointer type, and related subprograms.
3257 if Is_Remote then
3258 Process_Remote_AST_Declaration (N);
3259 end if;
3261 -- Validate categorization rule against access type declaration
3262 -- usually a violation in Pure unit, Shared_Passive unit.
3264 Validate_Access_Type_Declaration (T, N);
3266 -- If the type has contracts, we create the corresponding
3267 -- wrapper at once, before analyzing the aspect specifications,
3268 -- so that pre/postconditions can be handled directly on the
3269 -- generated wrapper.
3271 if Ada_Version >= Ada_2022
3272 and then Present (Aspect_Specifications (N))
3273 and then Expander_Active
3274 then
3275 Build_Access_Subprogram_Wrapper (N);
3276 end if;
3278 when N_Access_To_Object_Definition =>
3279 Access_Type_Declaration (T, Def);
3281 -- Validate categorization rule against access type declaration
3282 -- usually a violation in Pure unit, Shared_Passive unit.
3284 Validate_Access_Type_Declaration (T, N);
3286 -- If we are in a Remote_Call_Interface package and define a
3287 -- RACW, then calling stubs and specific stream attributes
3288 -- must be added.
3290 if Is_Remote
3291 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3292 then
3293 Add_RACW_Features (Def_Id);
3294 end if;
3296 when N_Array_Type_Definition =>
3297 Array_Type_Declaration (T, Def);
3299 when N_Derived_Type_Definition =>
3300 Derived_Type_Declaration (T, N, T /= Def_Id);
3302 -- Save the scenario for examination by the ABE Processing
3303 -- phase.
3305 Record_Elaboration_Scenario (N);
3307 when N_Enumeration_Type_Definition =>
3308 Enumeration_Type_Declaration (T, Def);
3310 when N_Floating_Point_Definition =>
3311 Floating_Point_Type_Declaration (T, Def);
3313 when N_Decimal_Fixed_Point_Definition =>
3314 Decimal_Fixed_Point_Type_Declaration (T, Def);
3316 when N_Ordinary_Fixed_Point_Definition =>
3317 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3319 when N_Signed_Integer_Type_Definition =>
3320 Signed_Integer_Type_Declaration (T, Def);
3322 when N_Modular_Type_Definition =>
3323 Modular_Type_Declaration (T, Def);
3325 when N_Record_Definition =>
3326 Record_Type_Declaration (T, N, Prev);
3328 -- If declaration has a parse error, nothing to elaborate.
3330 when N_Error =>
3331 null;
3333 when others =>
3334 raise Program_Error;
3335 end case;
3336 end if;
3338 if Etype (T) = Any_Type then
3339 return;
3340 end if;
3342 -- Set the primitives list of the full type and its base type when
3343 -- needed. T may be E_Void in cases of earlier errors, and in that
3344 -- case we bypass this.
3346 if Ekind (T) /= E_Void then
3347 if No (Direct_Primitive_Operations (T)) then
3348 if Etype (T) = T then
3349 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3351 -- If Etype of T is the base type (as opposed to a parent type)
3352 -- and already has an associated list of primitive operations,
3353 -- then set T's primitive list to the base type's list. Otherwise,
3354 -- create a new empty primitives list and share the list between
3355 -- T and its base type. The lists need to be shared in common.
3357 elsif Etype (T) = Base_Type (T) then
3359 if No (Direct_Primitive_Operations (Base_Type (T))) then
3360 Set_Direct_Primitive_Operations
3361 (Base_Type (T), New_Elmt_List);
3362 end if;
3364 Set_Direct_Primitive_Operations
3365 (T, Direct_Primitive_Operations (Base_Type (T)));
3367 -- Case where the Etype is a parent type, so we need a new
3368 -- primitives list for T.
3370 else
3371 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3372 end if;
3374 -- If T already has a Direct_Primitive_Operations list but its
3375 -- base type doesn't then set the base type's list to T's list.
3377 elsif No (Direct_Primitive_Operations (Base_Type (T))) then
3378 Set_Direct_Primitive_Operations
3379 (Base_Type (T), Direct_Primitive_Operations (T));
3380 end if;
3381 end if;
3383 -- Some common processing for all types
3385 Set_Depends_On_Private (T, Has_Private_Component (T));
3386 Check_Ops_From_Incomplete_Type;
3388 -- Both the declared entity, and its anonymous base type if one was
3389 -- created, need freeze nodes allocated.
3391 declare
3392 B : constant Entity_Id := Base_Type (T);
3394 begin
3395 -- In the case where the base type differs from the first subtype, we
3396 -- pre-allocate a freeze node, and set the proper link to the first
3397 -- subtype. Freeze_Entity will use this preallocated freeze node when
3398 -- it freezes the entity.
3400 -- This does not apply if the base type is a generic type, whose
3401 -- declaration is independent of the current derived definition.
3403 if B /= T and then not Is_Generic_Type (B) then
3404 Ensure_Freeze_Node (B);
3405 Set_First_Subtype_Link (Freeze_Node (B), T);
3406 end if;
3408 -- A type that is imported through a limited_with clause cannot
3409 -- generate any code, and thus need not be frozen. However, an access
3410 -- type with an imported designated type needs a finalization list,
3411 -- which may be referenced in some other package that has non-limited
3412 -- visibility on the designated type. Thus we must create the
3413 -- finalization list at the point the access type is frozen, to
3414 -- prevent unsatisfied references at link time.
3416 if not From_Limited_With (T) or else Is_Access_Type (T) then
3417 Set_Has_Delayed_Freeze (T);
3418 end if;
3419 end;
3421 -- Case where T is the full declaration of some private type which has
3422 -- been swapped in Defining_Identifier (N).
3424 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3425 Process_Full_View (N, T, Def_Id);
3427 -- Record the reference. The form of this is a little strange, since
3428 -- the full declaration has been swapped in. So the first parameter
3429 -- here represents the entity to which a reference is made which is
3430 -- the "real" entity, i.e. the one swapped in, and the second
3431 -- parameter provides the reference location.
3433 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3434 -- since we don't want a complaint about the full type being an
3435 -- unwanted reference to the private type
3437 declare
3438 B : constant Boolean := Has_Pragma_Unreferenced (T);
3439 begin
3440 Set_Has_Pragma_Unreferenced (T, False);
3441 Generate_Reference (T, T, 'c');
3442 Set_Has_Pragma_Unreferenced (T, B);
3443 end;
3445 Set_Completion_Referenced (Def_Id);
3447 -- For completion of incomplete type, process incomplete dependents
3448 -- and always mark the full type as referenced (it is the incomplete
3449 -- type that we get for any real reference).
3451 elsif Ekind (Prev) = E_Incomplete_Type then
3452 Process_Incomplete_Dependents (N, T, Prev);
3453 Generate_Reference (Prev, Def_Id, 'c');
3454 Set_Completion_Referenced (Def_Id);
3456 -- If not private type or incomplete type completion, this is a real
3457 -- definition of a new entity, so record it.
3459 else
3460 Generate_Definition (Def_Id);
3461 end if;
3463 if Chars (Scope (Def_Id)) = Name_System
3464 and then Chars (Def_Id) = Name_Address
3465 and then In_Predefined_Unit (N)
3466 then
3467 Set_Is_Descendant_Of_Address (Def_Id);
3468 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3469 Set_Is_Descendant_Of_Address (Prev);
3470 end if;
3472 Set_Optimize_Alignment_Flags (Def_Id);
3473 Check_Eliminated (Def_Id);
3475 -- If the declaration is a completion and aspects are present, apply
3476 -- them to the entity for the type which is currently the partial
3477 -- view, but which is the one that will be frozen.
3479 -- In most cases the partial view is a private type, and both views
3480 -- appear in different declarative parts. In the unusual case where
3481 -- the partial view is incomplete, perform the analysis on the
3482 -- full view, to prevent freezing anomalies with the corresponding
3483 -- class-wide type, which otherwise might be frozen before the
3484 -- dispatch table is built.
3486 if Prev /= Def_Id
3487 and then Ekind (Prev) /= E_Incomplete_Type
3488 then
3489 Analyze_Aspect_Specifications (N, Prev);
3491 -- Normal case
3493 else
3494 Analyze_Aspect_Specifications (N, Def_Id);
3495 end if;
3497 if Is_Derived_Type (Prev)
3498 and then Def_Id /= Prev
3499 then
3500 Check_Nonoverridable_Aspects;
3501 end if;
3503 -- Check for tagged type declaration at library level
3505 if Is_Tagged_Type (T)
3506 and then not Is_Library_Level_Entity (T)
3507 then
3508 Check_Restriction (No_Local_Tagged_Types, T);
3509 end if;
3510 end Analyze_Full_Type_Declaration;
3512 ----------------------------------
3513 -- Analyze_Incomplete_Type_Decl --
3514 ----------------------------------
3516 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3517 F : constant Boolean := Is_Pure (Current_Scope);
3518 T : Entity_Id;
3520 begin
3521 Generate_Definition (Defining_Identifier (N));
3523 -- Process an incomplete declaration. The identifier must not have been
3524 -- declared already in the scope. However, an incomplete declaration may
3525 -- appear in the private part of a package, for a private type that has
3526 -- already been declared.
3528 -- In this case, the discriminants (if any) must match
3530 T := Find_Type_Name (N);
3532 Mutate_Ekind (T, E_Incomplete_Type);
3533 Set_Etype (T, T);
3534 Set_Is_First_Subtype (T);
3535 Reinit_Size_Align (T);
3537 -- Set the SPARK mode from the current context
3539 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3540 Set_SPARK_Pragma_Inherited (T);
3542 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3543 -- incomplete types.
3545 if Tagged_Present (N) then
3546 Set_Is_Tagged_Type (T, True);
3547 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3548 Make_Class_Wide_Type (T);
3549 end if;
3551 -- Initialize the list of primitive operations to an empty list,
3552 -- to cover tagged types as well as untagged types. For untagged
3553 -- types this is used either to analyze the call as legal when
3554 -- Core_Extensions_Allowed is True, or to issue a better error message
3555 -- otherwise.
3557 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3559 Set_Stored_Constraint (T, No_Elist);
3561 if Present (Discriminant_Specifications (N)) then
3562 Push_Scope (T);
3563 Process_Discriminants (N);
3564 End_Scope;
3565 end if;
3567 -- If the type has discriminants, nontrivial subtypes may be declared
3568 -- before the full view of the type. The full views of those subtypes
3569 -- will be built after the full view of the type.
3571 Set_Private_Dependents (T, New_Elmt_List);
3572 Set_Is_Pure (T, F);
3573 end Analyze_Incomplete_Type_Decl;
3575 -----------------------------------
3576 -- Analyze_Interface_Declaration --
3577 -----------------------------------
3579 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3580 CW : constant Entity_Id := Class_Wide_Type (T);
3582 begin
3583 Set_Is_Tagged_Type (T);
3584 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3586 Set_Is_Limited_Record (T, Limited_Present (Def)
3587 or else Task_Present (Def)
3588 or else Protected_Present (Def)
3589 or else Synchronized_Present (Def));
3591 -- Type is abstract if full declaration carries keyword, or if previous
3592 -- partial view did.
3594 Set_Is_Abstract_Type (T);
3595 Set_Is_Interface (T);
3597 -- Type is a limited interface if it includes the keyword limited, task,
3598 -- protected, or synchronized.
3600 Set_Is_Limited_Interface
3601 (T, Limited_Present (Def)
3602 or else Protected_Present (Def)
3603 or else Synchronized_Present (Def)
3604 or else Task_Present (Def));
3606 Set_Interfaces (T, New_Elmt_List);
3607 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3609 -- Complete the decoration of the class-wide entity if it was already
3610 -- built (i.e. during the creation of the limited view)
3612 if Present (CW) then
3613 Set_Is_Interface (CW);
3614 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3615 end if;
3617 -- Check runtime support for synchronized interfaces
3619 if Is_Concurrent_Interface (T)
3620 and then not RTE_Available (RE_Select_Specific_Data)
3621 then
3622 Error_Msg_CRT ("synchronized interfaces", T);
3623 end if;
3624 end Analyze_Interface_Declaration;
3626 -----------------------------
3627 -- Analyze_Itype_Reference --
3628 -----------------------------
3630 -- Nothing to do. This node is placed in the tree only for the benefit of
3631 -- back end processing, and has no effect on the semantic processing.
3633 procedure Analyze_Itype_Reference (N : Node_Id) is
3634 begin
3635 pragma Assert (Is_Itype (Itype (N)));
3636 null;
3637 end Analyze_Itype_Reference;
3639 --------------------------------
3640 -- Analyze_Number_Declaration --
3641 --------------------------------
3643 procedure Analyze_Number_Declaration (N : Node_Id) is
3644 E : Node_Id := Expression (N);
3645 Id : constant Entity_Id := Defining_Identifier (N);
3646 Index : Interp_Index;
3647 It : Interp;
3648 T : Entity_Id;
3650 begin
3651 Generate_Definition (Id);
3652 Enter_Name (Id);
3654 -- This is an optimization of a common case of an integer literal
3656 if Nkind (E) = N_Integer_Literal then
3657 Set_Is_Static_Expression (E, True);
3658 Set_Etype (E, Universal_Integer);
3660 Set_Etype (Id, Universal_Integer);
3661 Mutate_Ekind (Id, E_Named_Integer);
3662 Set_Is_Frozen (Id, True);
3664 Set_Debug_Info_Needed (Id);
3665 return;
3666 end if;
3668 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3670 -- Replace Error by integer zero, which seems least likely to cause
3671 -- cascaded errors.
3673 if E = Error then
3674 pragma Assert (Serious_Errors_Detected > 0);
3675 E := Make_Integer_Literal (Sloc (N), Uint_0);
3676 Set_Expression (N, E);
3677 Set_Error_Posted (E);
3678 end if;
3680 Analyze (E);
3682 -- Verify that the expression is static and numeric. If
3683 -- the expression is overloaded, we apply the preference
3684 -- rule that favors root numeric types.
3686 if not Is_Overloaded (E) then
3687 T := Etype (E);
3688 if Has_Dynamic_Predicate_Aspect (T)
3689 or else Has_Ghost_Predicate_Aspect (T)
3690 then
3691 Error_Msg_N
3692 ("subtype has non-static predicate, "
3693 & "not allowed in number declaration", N);
3694 end if;
3696 else
3697 T := Any_Type;
3699 Get_First_Interp (E, Index, It);
3700 while Present (It.Typ) loop
3701 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3702 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3703 then
3704 if T = Any_Type then
3705 T := It.Typ;
3707 elsif Is_Universal_Numeric_Type (It.Typ) then
3708 -- Choose universal interpretation over any other
3710 T := It.Typ;
3711 exit;
3712 end if;
3713 end if;
3715 Get_Next_Interp (Index, It);
3716 end loop;
3717 end if;
3719 if Is_Integer_Type (T) then
3720 Resolve (E, T);
3721 Set_Etype (Id, Universal_Integer);
3722 Mutate_Ekind (Id, E_Named_Integer);
3724 elsif Is_Real_Type (T) then
3726 -- Because the real value is converted to universal_real, this is a
3727 -- legal context for a universal fixed expression.
3729 if T = Universal_Fixed then
3730 declare
3731 Loc : constant Source_Ptr := Sloc (N);
3732 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3733 Subtype_Mark =>
3734 New_Occurrence_Of (Universal_Real, Loc),
3735 Expression => Relocate_Node (E));
3737 begin
3738 Rewrite (E, Conv);
3739 Analyze (E);
3740 end;
3742 elsif T = Any_Fixed then
3743 Error_Msg_N ("illegal context for mixed mode operation", E);
3745 -- Expression is of the form : universal_fixed * integer. Try to
3746 -- resolve as universal_real.
3748 T := Universal_Real;
3749 Set_Etype (E, T);
3750 end if;
3752 Resolve (E, T);
3753 Set_Etype (Id, Universal_Real);
3754 Mutate_Ekind (Id, E_Named_Real);
3756 else
3757 Wrong_Type (E, Any_Numeric);
3758 Resolve (E, T);
3760 Set_Etype (Id, T);
3761 Mutate_Ekind (Id, E_Constant);
3762 Set_Never_Set_In_Source (Id, True);
3763 Set_Is_True_Constant (Id, True);
3764 return;
3765 end if;
3767 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3768 Set_Etype (E, Etype (Id));
3769 end if;
3771 if not Is_OK_Static_Expression (E) then
3772 Flag_Non_Static_Expr
3773 ("non-static expression used in number declaration!", E);
3774 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3775 Set_Etype (E, Any_Type);
3776 end if;
3778 Analyze_Dimension (N);
3779 end Analyze_Number_Declaration;
3781 --------------------------------
3782 -- Analyze_Object_Declaration --
3783 --------------------------------
3785 -- WARNING: This routine manages Ghost regions. Return statements must be
3786 -- replaced by gotos which jump to the end of the routine and restore the
3787 -- Ghost mode.
3789 procedure Analyze_Object_Declaration (N : Node_Id) is
3790 Loc : constant Source_Ptr := Sloc (N);
3791 Id : constant Entity_Id := Defining_Identifier (N);
3792 Next_Decl : constant Node_Id := Next (N);
3794 Act_T : Entity_Id;
3795 T : Entity_Id;
3797 E : Node_Id := Expression (N);
3798 -- E is set to Expression (N) throughout this routine. When Expression
3799 -- (N) is modified, E is changed accordingly.
3801 procedure Check_Dynamic_Object (Typ : Entity_Id);
3802 -- A library-level object with nonstatic discriminant constraints may
3803 -- require dynamic allocation. The declaration is illegal if the
3804 -- profile includes the restriction No_Implicit_Heap_Allocations.
3806 procedure Check_For_Null_Excluding_Components
3807 (Obj_Typ : Entity_Id;
3808 Obj_Decl : Node_Id);
3809 -- Verify that each null-excluding component of object declaration
3810 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3811 -- a compile-time warning if this is not the case.
3813 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
3814 -- Check that the return subtype indication properly matches the result
3815 -- subtype of the function in an extended return object declaration, as
3816 -- required by RM 6.5(5.1/2-5.3/2).
3818 function Count_Tasks (T : Entity_Id) return Uint;
3819 -- This function is called when a non-generic library level object of a
3820 -- task type is declared. Its function is to count the static number of
3821 -- tasks declared within the type (it is only called if Has_Task is set
3822 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3823 -- or a variant record type is encountered, Check_Restriction is called
3824 -- indicating the count is unknown.
3826 function Delayed_Aspect_Present return Boolean;
3827 -- If the declaration has an expression that is an aggregate, and it
3828 -- has aspects that require delayed analysis, the resolution of the
3829 -- aggregate must be deferred to the freeze point of the object. This
3830 -- special processing was created for address clauses, but it must
3831 -- also apply to address aspects. This must be done before the aspect
3832 -- specifications are analyzed because we must handle the aggregate
3833 -- before the analysis of the object declaration is complete.
3835 -- Any other relevant delayed aspects on object declarations ???
3837 --------------------------
3838 -- Check_Dynamic_Object --
3839 --------------------------
3841 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3842 Comp : Entity_Id;
3843 Obj_Type : Entity_Id;
3845 begin
3846 Obj_Type := Typ;
3848 if Is_Private_Type (Obj_Type)
3849 and then Present (Full_View (Obj_Type))
3850 then
3851 Obj_Type := Full_View (Obj_Type);
3852 end if;
3854 if Known_Static_Esize (Obj_Type) then
3855 return;
3856 end if;
3858 if Restriction_Active (No_Implicit_Heap_Allocations)
3859 and then Expander_Active
3860 and then Has_Discriminants (Obj_Type)
3861 then
3862 Comp := First_Component (Obj_Type);
3863 while Present (Comp) loop
3864 if Known_Static_Esize (Etype (Comp))
3865 or else Size_Known_At_Compile_Time (Etype (Comp))
3866 then
3867 null;
3869 elsif Is_Record_Type (Etype (Comp)) then
3870 Check_Dynamic_Object (Etype (Comp));
3872 elsif not Discriminated_Size (Comp)
3873 and then Comes_From_Source (Comp)
3874 then
3875 Error_Msg_NE
3876 ("component& of non-static size will violate restriction "
3877 & "No_Implicit_Heap_Allocation?", N, Comp);
3879 end if;
3881 Next_Component (Comp);
3882 end loop;
3883 end if;
3884 end Check_Dynamic_Object;
3886 -----------------------------------------
3887 -- Check_For_Null_Excluding_Components --
3888 -----------------------------------------
3890 procedure Check_For_Null_Excluding_Components
3891 (Obj_Typ : Entity_Id;
3892 Obj_Decl : Node_Id)
3894 procedure Check_Component
3895 (Comp_Typ : Entity_Id;
3896 Comp_Decl : Node_Id := Empty;
3897 Array_Comp : Boolean := False);
3898 -- Apply a compile-time null-exclusion check on a component denoted
3899 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3900 -- subcomponents (if any).
3902 ---------------------
3903 -- Check_Component --
3904 ---------------------
3906 procedure Check_Component
3907 (Comp_Typ : Entity_Id;
3908 Comp_Decl : Node_Id := Empty;
3909 Array_Comp : Boolean := False)
3911 Comp : Entity_Id;
3912 T : Entity_Id;
3914 begin
3915 -- Do not consider internally-generated components or those that
3916 -- are already initialized.
3918 if Present (Comp_Decl)
3919 and then (not Comes_From_Source (Comp_Decl)
3920 or else Present (Expression (Comp_Decl)))
3921 then
3922 return;
3923 end if;
3925 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3926 and then Present (Full_View (Comp_Typ))
3927 then
3928 T := Full_View (Comp_Typ);
3929 else
3930 T := Comp_Typ;
3931 end if;
3933 -- Verify a component of a null-excluding access type
3935 if Is_Access_Type (T)
3936 and then Can_Never_Be_Null (T)
3937 then
3938 if Comp_Decl = Obj_Decl then
3939 Null_Exclusion_Static_Checks
3940 (N => Obj_Decl,
3941 Comp => Empty,
3942 Array_Comp => Array_Comp);
3944 else
3945 Null_Exclusion_Static_Checks
3946 (N => Obj_Decl,
3947 Comp => Comp_Decl,
3948 Array_Comp => Array_Comp);
3949 end if;
3951 -- Check array components
3953 elsif Is_Array_Type (T) then
3955 -- There is no suitable component when the object is of an
3956 -- array type. However, a namable component may appear at some
3957 -- point during the recursive inspection, but not at the top
3958 -- level. At the top level just indicate array component case.
3960 if Comp_Decl = Obj_Decl then
3961 Check_Component (Component_Type (T), Array_Comp => True);
3962 else
3963 Check_Component (Component_Type (T), Comp_Decl);
3964 end if;
3966 -- Verify all components of type T
3968 -- Note: No checks are performed on types with discriminants due
3969 -- to complexities involving variants. ???
3971 elsif (Is_Concurrent_Type (T)
3972 or else Is_Incomplete_Or_Private_Type (T)
3973 or else Is_Record_Type (T))
3974 and then not Has_Discriminants (T)
3975 then
3976 Comp := First_Component (T);
3977 while Present (Comp) loop
3978 Check_Component (Etype (Comp), Parent (Comp));
3980 Next_Component (Comp);
3981 end loop;
3982 end if;
3983 end Check_Component;
3985 -- Start processing for Check_For_Null_Excluding_Components
3987 begin
3988 Check_Component (Obj_Typ, Obj_Decl);
3989 end Check_For_Null_Excluding_Components;
3991 -------------------------------------
3992 -- Check_Return_Subtype_Indication --
3993 -------------------------------------
3995 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
3996 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
3997 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3998 Func_Id : constant Entity_Id := Return_Applies_To (Scope (Obj_Id));
3999 R_Typ : constant Entity_Id := Etype (Func_Id);
4000 Indic : constant Node_Id :=
4001 Object_Definition (Original_Node (Obj_Decl));
4003 procedure Error_No_Match (N : Node_Id);
4004 -- Output error messages for case where types do not statically
4005 -- match. N is the location for the messages.
4007 --------------------
4008 -- Error_No_Match --
4009 --------------------
4011 procedure Error_No_Match (N : Node_Id) is
4012 begin
4013 Error_Msg_N
4014 ("subtype must statically match function result subtype", N);
4016 if not Predicates_Match (Obj_Typ, R_Typ) then
4017 Error_Msg_Node_2 := R_Typ;
4018 Error_Msg_NE
4019 ("\predicate of& does not match predicate of&",
4020 N, Obj_Typ);
4021 end if;
4022 end Error_No_Match;
4024 -- Start of processing for Check_Return_Subtype_Indication
4026 begin
4027 -- First, avoid cascaded errors
4029 if Error_Posted (Obj_Decl) or else Error_Posted (Indic) then
4030 return;
4031 end if;
4033 -- "return access T" case; check that the return statement also has
4034 -- "access T", and that the subtypes statically match:
4035 -- if this is an access to subprogram the signatures must match.
4037 if Is_Anonymous_Access_Type (R_Typ) then
4038 if Is_Anonymous_Access_Type (Obj_Typ) then
4039 if Ekind (Designated_Type (Obj_Typ)) /= E_Subprogram_Type
4040 then
4041 if Base_Type (Designated_Type (Obj_Typ)) /=
4042 Base_Type (Designated_Type (R_Typ))
4043 or else not Subtypes_Statically_Match (Obj_Typ, R_Typ)
4044 then
4045 Error_No_Match (Subtype_Mark (Indic));
4046 end if;
4048 else
4049 -- For two anonymous access to subprogram types, the types
4050 -- themselves must be type conformant.
4052 if not Conforming_Types
4053 (Obj_Typ, R_Typ, Fully_Conformant)
4054 then
4055 Error_No_Match (Indic);
4056 end if;
4057 end if;
4059 else
4060 Error_Msg_N ("must use anonymous access type", Indic);
4061 end if;
4063 -- If the return object is of an anonymous access type, then report
4064 -- an error if the function's result type is not also anonymous.
4066 elsif Is_Anonymous_Access_Type (Obj_Typ) then
4067 pragma Assert (not Is_Anonymous_Access_Type (R_Typ));
4068 Error_Msg_N
4069 ("anonymous access not allowed for function with named access "
4070 & "result", Indic);
4072 -- Subtype indication case: check that the return object's type is
4073 -- covered by the result type, and that the subtypes statically match
4074 -- when the result subtype is constrained. Also handle record types
4075 -- with unknown discriminants for which we have built the underlying
4076 -- record view. Coverage is needed to allow specific-type return
4077 -- objects when the result type is class-wide (see AI05-32).
4079 elsif Covers (Base_Type (R_Typ), Base_Type (Obj_Typ))
4080 or else (Is_Underlying_Record_View (Base_Type (Obj_Typ))
4081 and then
4082 Covers
4083 (Base_Type (R_Typ),
4084 Underlying_Record_View (Base_Type (Obj_Typ))))
4085 then
4086 -- A null exclusion may be present on the return type, on the
4087 -- function specification, on the object declaration or on the
4088 -- subtype itself.
4090 if Is_Access_Type (R_Typ)
4091 and then
4092 (Can_Never_Be_Null (R_Typ)
4093 or else Null_Exclusion_Present (Parent (Func_Id))) /=
4094 Can_Never_Be_Null (Obj_Typ)
4095 then
4096 Error_No_Match (Indic);
4097 end if;
4099 -- AI05-103: for elementary types, subtypes must statically match
4101 if Is_Constrained (R_Typ) or else Is_Access_Type (R_Typ) then
4102 if not Subtypes_Statically_Match (Obj_Typ, R_Typ) then
4103 Error_No_Match (Indic);
4104 end if;
4106 -- If the result subtype of the function is defined by a
4107 -- subtype_mark, the return_subtype_indication shall be a
4108 -- subtype_indication. The subtype defined by the subtype_
4109 -- indication shall be statically compatible with the result
4110 -- subtype of the function (RM 6.5(5.3/5)).
4112 -- We exclude the extended return statement of the predefined
4113 -- stream input to avoid reporting spurious errors, because its
4114 -- code is expanded on the basis of the base type (see subprogram
4115 -- Stream_Base_Type).
4117 elsif Nkind (Indic) = N_Subtype_Indication
4118 and then not Subtypes_Statically_Compatible (Obj_Typ, R_Typ)
4119 and then not Is_TSS (Func_Id, TSS_Stream_Input)
4120 then
4121 Error_Msg_N
4122 ("result subtype must be statically compatible with the " &
4123 "function result type", Indic);
4125 if not Predicates_Compatible (Obj_Typ, R_Typ) then
4126 Error_Msg_NE
4127 ("\predicate on result subtype is not compatible with &",
4128 Indic, R_Typ);
4129 end if;
4130 end if;
4132 -- All remaining cases are illegal
4134 -- Note: previous versions of this subprogram allowed the return
4135 -- value to be the ancestor of the return type if the return type
4136 -- was a null extension. This was plainly incorrect.
4138 else
4139 Error_Msg_N
4140 ("wrong type for return_subtype_indication", Indic);
4141 end if;
4142 end Check_Return_Subtype_Indication;
4144 -----------------
4145 -- Count_Tasks --
4146 -----------------
4148 function Count_Tasks (T : Entity_Id) return Uint is
4149 C : Entity_Id;
4150 X : Node_Id;
4151 V : Uint;
4153 begin
4154 if Is_Task_Type (T) then
4155 return Uint_1;
4157 elsif Is_Record_Type (T) then
4158 if Has_Discriminants (T) then
4159 Check_Restriction (Max_Tasks, N);
4160 return Uint_0;
4162 else
4163 V := Uint_0;
4164 C := First_Component (T);
4165 while Present (C) loop
4166 V := V + Count_Tasks (Etype (C));
4167 Next_Component (C);
4168 end loop;
4170 return V;
4171 end if;
4173 elsif Is_Array_Type (T) then
4174 X := First_Index (T);
4175 V := Count_Tasks (Component_Type (T));
4176 while Present (X) loop
4177 C := Etype (X);
4179 if not Is_OK_Static_Subtype (C) then
4180 Check_Restriction (Max_Tasks, N);
4181 return Uint_0;
4182 else
4183 V := V * (UI_Max (Uint_0,
4184 Expr_Value (Type_High_Bound (C)) -
4185 Expr_Value (Type_Low_Bound (C)) + Uint_1));
4186 end if;
4188 Next_Index (X);
4189 end loop;
4191 return V;
4193 else
4194 return Uint_0;
4195 end if;
4196 end Count_Tasks;
4198 ----------------------------
4199 -- Delayed_Aspect_Present --
4200 ----------------------------
4202 function Delayed_Aspect_Present return Boolean is
4203 A : Node_Id;
4204 A_Id : Aspect_Id;
4206 begin
4207 A := First (Aspect_Specifications (N));
4209 while Present (A) loop
4210 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4212 if A_Id = Aspect_Address then
4214 -- Set flag on object entity, for later processing at the
4215 -- freeze point.
4217 Set_Has_Delayed_Aspects (Id);
4218 return True;
4219 end if;
4221 Next (A);
4222 end loop;
4224 return False;
4225 end Delayed_Aspect_Present;
4227 -- Local variables
4229 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4230 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4231 -- Save the Ghost-related attributes to restore on exit
4233 Prev_Entity : Entity_Id := Empty;
4234 Related_Id : Entity_Id;
4236 -- Start of processing for Analyze_Object_Declaration
4238 begin
4239 -- There are three kinds of implicit types generated by an
4240 -- object declaration:
4242 -- 1. Those generated by the original Object Definition
4244 -- 2. Those generated by the Expression
4246 -- 3. Those used to constrain the Object Definition with the
4247 -- expression constraints when the definition is unconstrained.
4249 -- They must be generated in this order to avoid order of elaboration
4250 -- issues. Thus the first step (after entering the name) is to analyze
4251 -- the object definition.
4253 if Constant_Present (N) then
4254 Prev_Entity := Current_Entity_In_Scope (Id);
4256 if Present (Prev_Entity)
4257 and then
4258 -- If the homograph is an implicit subprogram, it is overridden
4259 -- by the current declaration.
4261 ((Is_Overloadable (Prev_Entity)
4262 and then Is_Inherited_Operation (Prev_Entity))
4264 -- The current object is a discriminal generated for an entry
4265 -- family index. Even though the index is a constant, in this
4266 -- particular context there is no true constant redeclaration.
4267 -- Enter_Name will handle the visibility.
4269 or else
4270 (Is_Discriminal (Id)
4271 and then Ekind (Discriminal_Link (Id)) =
4272 E_Entry_Index_Parameter)
4274 -- The current object is the renaming for a generic declared
4275 -- within the instance.
4277 or else
4278 (Ekind (Prev_Entity) = E_Package
4279 and then Nkind (Parent (Prev_Entity)) =
4280 N_Package_Renaming_Declaration
4281 and then not Comes_From_Source (Prev_Entity)
4282 and then
4283 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4285 -- The entity may be a homonym of a private component of the
4286 -- enclosing protected object, for which we create a local
4287 -- renaming declaration. The declaration is legal, even if
4288 -- useless when it just captures that component.
4290 or else
4291 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4292 and then Nkind (Parent (Prev_Entity)) =
4293 N_Object_Renaming_Declaration))
4294 then
4295 Prev_Entity := Empty;
4296 end if;
4297 end if;
4299 if Present (Prev_Entity) then
4301 -- The object declaration is Ghost when it completes a deferred Ghost
4302 -- constant.
4304 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4306 Constant_Redeclaration (Id, N, T);
4308 Generate_Reference (Prev_Entity, Id, 'c');
4309 Set_Completion_Referenced (Id);
4311 if Error_Posted (N) then
4313 -- Type mismatch or illegal redeclaration; do not analyze
4314 -- expression to avoid cascaded errors.
4316 T := Find_Type_Of_Object (Object_Definition (N), N);
4317 Set_Etype (Id, T);
4318 Mutate_Ekind (Id, E_Variable);
4319 goto Leave;
4320 end if;
4322 -- In the normal case, enter identifier at the start to catch premature
4323 -- usage in the initialization expression.
4325 else
4326 Generate_Definition (Id);
4327 Enter_Name (Id);
4329 Mark_Coextensions (N, Object_Definition (N));
4331 T := Find_Type_Of_Object (Object_Definition (N), N);
4333 if Nkind (Object_Definition (N)) = N_Access_Definition
4334 and then Present
4335 (Access_To_Subprogram_Definition (Object_Definition (N)))
4336 and then Protected_Present
4337 (Access_To_Subprogram_Definition (Object_Definition (N)))
4338 then
4339 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4340 end if;
4342 if Error_Posted (Id) then
4343 Set_Etype (Id, T);
4344 Mutate_Ekind (Id, E_Variable);
4345 goto Leave;
4346 end if;
4347 end if;
4349 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4350 -- out some static checks.
4352 if Ada_Version >= Ada_2005 then
4354 -- In case of aggregates we must also take care of the correct
4355 -- initialization of nested aggregates bug this is done at the
4356 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4358 if Can_Never_Be_Null (T) then
4359 if Present (Expression (N))
4360 and then Nkind (Expression (N)) = N_Aggregate
4361 then
4362 null;
4364 elsif Comes_From_Source (Id) then
4365 declare
4366 Save_Typ : constant Entity_Id := Etype (Id);
4367 begin
4368 Set_Etype (Id, T); -- Temp. decoration for static checks
4369 Null_Exclusion_Static_Checks (N);
4370 Set_Etype (Id, Save_Typ);
4371 end;
4372 end if;
4374 -- We might be dealing with an object of a composite type containing
4375 -- null-excluding components without an aggregate, so we must verify
4376 -- that such components have default initialization.
4378 else
4379 Check_For_Null_Excluding_Components (T, N);
4380 end if;
4381 end if;
4383 -- Object is marked pure if it is in a pure scope
4385 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4387 -- If deferred constant, make sure context is appropriate. We detect
4388 -- a deferred constant as a constant declaration with no expression.
4389 -- A deferred constant can appear in a package body if its completion
4390 -- is by means of an interface pragma.
4392 if Constant_Present (N) and then No (E) then
4394 -- A deferred constant may appear in the declarative part of the
4395 -- following constructs:
4397 -- blocks
4398 -- entry bodies
4399 -- extended return statements
4400 -- package specs
4401 -- package bodies
4402 -- subprogram bodies
4403 -- task bodies
4405 -- When declared inside a package spec, a deferred constant must be
4406 -- completed by a full constant declaration or pragma Import. In all
4407 -- other cases, the only proper completion is pragma Import. Extended
4408 -- return statements are flagged as invalid contexts because they do
4409 -- not have a declarative part and so cannot accommodate the pragma.
4411 if Ekind (Current_Scope) = E_Return_Statement then
4412 Error_Msg_N
4413 ("invalid context for deferred constant declaration (RM 7.4)",
4415 Error_Msg_N
4416 ("\declaration requires an initialization expression",
4418 Set_Constant_Present (N, False);
4420 -- In Ada 83, deferred constant must be of private type
4422 elsif not Is_Private_Type (T) then
4423 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4424 Error_Msg_N
4425 ("(Ada 83) deferred constant must be private type", N);
4426 end if;
4427 end if;
4429 -- If not a deferred constant, then the object declaration freezes
4430 -- its type, unless the object is of an anonymous type and has delayed
4431 -- aspects (in that case the type is frozen when the object itself is)
4432 -- or the context is a spec expression.
4434 else
4435 Check_Fully_Declared (T, N);
4437 if Has_Delayed_Aspects (Id)
4438 and then Is_Array_Type (T)
4439 and then Is_Itype (T)
4440 then
4441 Set_Has_Delayed_Freeze (T);
4442 elsif not In_Spec_Expression then
4443 Freeze_Before (N, T);
4444 end if;
4445 end if;
4447 -- If the object was created by a constrained array definition, then
4448 -- set the link in both the anonymous base type and anonymous subtype
4449 -- that are built to represent the array type to point to the object.
4451 if Nkind (Object_Definition (Declaration_Node (Id))) =
4452 N_Constrained_Array_Definition
4453 then
4454 Set_Related_Array_Object (T, Id);
4455 Set_Related_Array_Object (Base_Type (T), Id);
4456 end if;
4458 -- Check for protected objects not at library level
4460 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4461 Check_Restriction (No_Local_Protected_Objects, Id);
4462 end if;
4464 -- Check for violation of No_Local_Timing_Events
4466 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4467 Check_Restriction (No_Local_Timing_Events, Id);
4468 end if;
4470 -- The actual subtype of the object is the nominal subtype, unless
4471 -- the nominal one is unconstrained and obtained from the expression.
4473 Act_T := T;
4475 if Is_Library_Level_Entity (Id) then
4476 Check_Dynamic_Object (T);
4477 end if;
4479 -- Process initialization expression if present and not in error
4481 if Present (E) and then E /= Error then
4483 -- Generate an error in case of CPP class-wide object initialization.
4484 -- Required because otherwise the expansion of the class-wide
4485 -- assignment would try to use 'size to initialize the object
4486 -- (primitive that is not available in CPP tagged types).
4488 if Is_Class_Wide_Type (Act_T)
4489 and then
4490 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4491 or else
4492 (Present (Full_View (Root_Type (Etype (Act_T))))
4493 and then
4494 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4495 then
4496 Error_Msg_N
4497 ("predefined assignment not available for 'C'P'P tagged types",
4499 end if;
4501 Mark_Coextensions (N, E);
4502 Analyze (E);
4504 -- In case of errors detected in the analysis of the expression,
4505 -- decorate it with the expected type to avoid cascaded errors.
4507 if No (Etype (E)) then
4508 Set_Etype (E, T);
4509 end if;
4511 -- If an initialization expression is present, then we set the
4512 -- Is_True_Constant flag. It will be reset if this is a variable
4513 -- and it is indeed modified.
4515 Set_Is_True_Constant (Id, True);
4517 -- If we are analyzing a constant declaration, set its completion
4518 -- flag after analyzing and resolving the expression.
4520 if Constant_Present (N) then
4521 Set_Has_Completion (Id);
4522 end if;
4524 -- Set type and resolve (type may be overridden later on). Note:
4525 -- Ekind (Id) must still be E_Void at this point so that incorrect
4526 -- early usage within E is properly diagnosed.
4528 Set_Etype (Id, T);
4530 -- If the expression is an aggregate we must look ahead to detect
4531 -- the possible presence of an address clause, and defer resolution
4532 -- and expansion of the aggregate to the freeze point of the entity.
4534 -- This is not always legal because the aggregate may contain other
4535 -- references that need freezing, e.g. references to other entities
4536 -- with address clauses. In any case, when compiling with -gnatI the
4537 -- presence of the address clause must be ignored.
4539 if Comes_From_Source (N)
4540 and then Expander_Active
4541 and then Nkind (E) = N_Aggregate
4542 and then
4543 ((Present (Following_Address_Clause (N))
4544 and then not Ignore_Rep_Clauses)
4545 or else Delayed_Aspect_Present)
4546 then
4547 Set_Etype (E, T);
4549 -- If the aggregate is limited it will be built in place, and its
4550 -- expansion is deferred until the object declaration is expanded.
4552 -- This is also required when generating C code to ensure that an
4553 -- object with an alignment or address clause can be initialized
4554 -- by means of component by component assignments.
4556 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4557 Set_Expansion_Delayed (E);
4558 end if;
4560 else
4561 -- If the expression is a formal that is a "subprogram pointer"
4562 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4563 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4564 -- the corresponding check, as is done for assignments.
4566 if Is_Entity_Name (E)
4567 and then Present (Entity (E))
4568 and then Is_Formal (Entity (E))
4569 and then
4570 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4571 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4572 then
4573 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4574 end if;
4576 Resolve (E, T);
4577 end if;
4579 -- No further action needed if E is a call to an inlined function
4580 -- which returns an unconstrained type and it has been expanded into
4581 -- a procedure call. In that case N has been replaced by an object
4582 -- declaration without initializing expression and it has been
4583 -- analyzed (see Expand_Inlined_Call).
4585 if Back_End_Inlining
4586 and then Expander_Active
4587 and then Nkind (E) = N_Function_Call
4588 and then Nkind (Name (E)) in N_Has_Entity
4589 and then Is_Inlined (Entity (Name (E)))
4590 and then not Is_Constrained (Etype (E))
4591 and then Analyzed (N)
4592 and then No (Expression (N))
4593 then
4594 goto Leave;
4595 end if;
4597 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4598 -- node (which was marked already-analyzed), we need to set the type
4599 -- to something else than Universal_Access to keep gigi happy.
4601 if Etype (E) = Universal_Access then
4602 Set_Etype (E, T);
4603 end if;
4605 -- If the object is an access to variable, the initialization
4606 -- expression cannot be an access to constant.
4608 if Is_Access_Type (T)
4609 and then not Is_Access_Constant (T)
4610 and then Is_Access_Type (Etype (E))
4611 and then Is_Access_Constant (Etype (E))
4612 then
4613 Error_Msg_N
4614 ("access to variable cannot be initialized with an "
4615 & "access-to-constant expression", E);
4616 end if;
4618 if not Assignment_OK (N) then
4619 Check_Initialization (T, E);
4620 end if;
4622 Check_Unset_Reference (E);
4624 -- If this is a variable, then set current value. If this is a
4625 -- declared constant of a scalar type with a static expression,
4626 -- indicate that it is always valid.
4628 if not Constant_Present (N) then
4629 if Compile_Time_Known_Value (E) then
4630 Set_Current_Value (Id, E);
4631 end if;
4633 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4634 Set_Is_Known_Valid (Id);
4636 -- If it is a constant initialized with a valid nonstatic entity,
4637 -- the constant is known valid as well, and can inherit the subtype
4638 -- of the entity if it is a subtype of the given type. This info
4639 -- is preserved on the actual subtype of the constant.
4641 elsif Is_Scalar_Type (T)
4642 and then Is_Entity_Name (E)
4643 and then Is_Known_Valid (Entity (E))
4644 and then In_Subrange_Of (Etype (Entity (E)), T)
4645 then
4646 Set_Is_Known_Valid (Id);
4647 Mutate_Ekind (Id, E_Constant);
4648 Set_Actual_Subtype (Id, Etype (Entity (E)));
4649 end if;
4651 -- Deal with setting of null flags
4653 if Is_Access_Type (T) then
4654 if Known_Non_Null (E) then
4655 Set_Is_Known_Non_Null (Id, True);
4656 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4657 Set_Is_Known_Null (Id, True);
4658 end if;
4659 end if;
4661 -- Check incorrect use of dynamically tagged expressions
4663 if Is_Tagged_Type (T) then
4664 Check_Dynamically_Tagged_Expression
4665 (Expr => E,
4666 Typ => T,
4667 Related_Nod => N);
4668 end if;
4670 Apply_Scalar_Range_Check (E, T);
4671 Apply_Static_Length_Check (E, T);
4673 -- A formal parameter of a specific tagged type whose related
4674 -- subprogram is subject to pragma Extensions_Visible with value
4675 -- "False" cannot be implicitly converted to a class-wide type by
4676 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4677 -- not consider internally generated expressions.
4679 if Is_Class_Wide_Type (T)
4680 and then Comes_From_Source (E)
4681 and then Is_EVF_Expression (E)
4682 then
4683 Error_Msg_N
4684 ("formal parameter cannot be implicitly converted to "
4685 & "class-wide type when Extensions_Visible is False", E);
4686 end if;
4687 end if;
4689 -- If the No_Streams restriction is set, check that the type of the
4690 -- object is not, and does not contain, any subtype derived from
4691 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4692 -- Has_Stream just for efficiency reasons. There is no point in
4693 -- spending time on a Has_Stream check if the restriction is not set.
4695 if Restriction_Check_Required (No_Streams) then
4696 if Has_Stream (T) then
4697 Check_Restriction (No_Streams, N);
4698 end if;
4699 end if;
4701 -- Deal with predicate check before we start to do major rewriting. It
4702 -- is OK to initialize and then check the initialized value, since the
4703 -- object goes out of scope if we get a predicate failure. Note that we
4704 -- do this in the analyzer and not the expander because the analyzer
4705 -- does some substantial rewriting in some cases.
4707 -- We need a predicate check if the type has predicates that are not
4708 -- ignored, and if either there is an initializing expression, or for
4709 -- default initialization when we have at least one case of an explicit
4710 -- default initial value (including via a Default_Value or
4711 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4712 -- an internal declaration whose initialization comes later (as for an
4713 -- aggregate expansion) or a deferred constant.
4714 -- If expression is an aggregate it may be expanded into assignments
4715 -- and the declaration itself is marked with No_Initialization, but
4716 -- the predicate still applies.
4718 if not Suppress_Assignment_Checks (N)
4719 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4720 and then
4721 (not No_Initialization (N)
4722 or else (Present (E) and then Nkind (E) = N_Aggregate))
4723 and then
4724 (Present (E)
4725 or else
4726 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4727 and then not (Constant_Present (N) and then No (E))
4728 then
4729 -- If the type has a static predicate and the expression is known at
4730 -- compile time, see if the expression satisfies the predicate.
4731 -- In the case of a static expression, this must be done even if
4732 -- the predicate is not enabled (as per static expression rules).
4734 if Present (E) then
4735 Check_Expression_Against_Static_Predicate (E, T);
4736 end if;
4738 -- Do not perform further predicate-related checks unless
4739 -- predicates are enabled for the subtype.
4741 if not Predicate_Enabled (T) then
4742 null;
4744 -- If the type is a null record and there is no explicit initial
4745 -- expression, no predicate check applies.
4747 elsif No (E) and then Is_Null_Record_Type (T) then
4748 null;
4750 -- If there is an address clause for this object, do not generate a
4751 -- predicate check here. It will be generated later, at the freezng
4752 -- point. It would be wrong to generate references to the object
4753 -- here, before the address has been determined.
4755 elsif Has_Aspect (Id, Aspect_Address)
4756 or else Present (Following_Address_Clause (N))
4757 then
4758 null;
4760 -- Do not generate a predicate check if the initialization expression
4761 -- is a type conversion whose target subtype statically matches the
4762 -- object's subtype because the conversion has been subjected to the
4763 -- same check. This is a small optimization which avoids redundant
4764 -- checks.
4766 elsif Present (E)
4767 and then Nkind (E) in N_Type_Conversion
4768 and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
4769 then
4770 null;
4772 else
4773 -- The check must be inserted after the expanded aggregate
4774 -- expansion code, if any.
4776 declare
4777 Check : constant Node_Id :=
4778 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4779 begin
4780 if No (Next_Decl) then
4781 Append_To (List_Containing (N), Check);
4782 else
4783 Insert_Before (Next_Decl, Check);
4784 end if;
4785 end;
4786 end if;
4787 end if;
4789 -- Case of unconstrained type
4791 if not Is_Definite_Subtype (T) then
4793 -- Nothing to do in deferred constant case
4795 if Constant_Present (N) and then No (E) then
4796 null;
4798 -- Case of no initialization present
4800 elsif No (E) then
4801 if No_Initialization (N) then
4802 null;
4804 elsif Is_Class_Wide_Type (T) then
4805 Error_Msg_N
4806 ("initialization required in class-wide declaration", N);
4808 else
4809 Error_Msg_N
4810 ("unconstrained subtype not allowed (need initialization)",
4811 Object_Definition (N));
4813 if Is_Record_Type (T) and then Has_Discriminants (T) then
4814 Error_Msg_N
4815 ("\provide initial value or explicit discriminant values",
4816 Object_Definition (N));
4818 Error_Msg_NE
4819 ("\or give default discriminant values for type&",
4820 Object_Definition (N), T);
4822 elsif Is_Array_Type (T) then
4823 Error_Msg_N
4824 ("\provide initial value or explicit array bounds",
4825 Object_Definition (N));
4826 end if;
4827 end if;
4829 -- Case of initialization present but in error. Set initial
4830 -- expression as absent (but do not make above complaints).
4832 elsif E = Error then
4833 Set_Expression (N, Empty);
4834 E := Empty;
4836 -- Case of initialization present
4838 else
4839 -- Unconstrained variables not allowed in Ada 83
4841 if Ada_Version = Ada_83
4842 and then not Constant_Present (N)
4843 and then Comes_From_Source (Object_Definition (N))
4844 then
4845 Error_Msg_N
4846 ("(Ada 83) unconstrained variable not allowed",
4847 Object_Definition (N));
4848 end if;
4850 -- Now we constrain the variable from the initializing expression
4852 -- If the expression is an aggregate, it has been expanded into
4853 -- individual assignments. Retrieve the actual type from the
4854 -- expanded construct.
4856 if Is_Array_Type (T)
4857 and then No_Initialization (N)
4858 and then Nkind (Original_Node (E)) = N_Aggregate
4859 then
4860 Act_T := Etype (E);
4862 -- In case of class-wide interface object declarations we delay
4863 -- the generation of the equivalent record type declarations until
4864 -- its expansion because there are cases in they are not required.
4866 elsif Is_Interface (T) then
4867 null;
4869 -- If the type is an unchecked union, no subtype can be built from
4870 -- the expression. Rewrite declaration as a renaming, which the
4871 -- back-end can handle properly. This is a rather unusual case,
4872 -- because most unchecked_union declarations have default values
4873 -- for discriminants and are thus not indefinite.
4875 elsif Is_Unchecked_Union (T) then
4876 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4877 Mutate_Ekind (Id, E_Constant);
4878 else
4879 Mutate_Ekind (Id, E_Variable);
4880 end if;
4882 -- If the expression is an aggregate it contains the required
4883 -- discriminant values but it has not been resolved yet, so do
4884 -- it now, and treat it as the initial expression of an object
4885 -- declaration, rather than a renaming.
4887 if Nkind (E) = N_Aggregate then
4888 Analyze_And_Resolve (E, T);
4890 else
4891 Rewrite (N,
4892 Make_Object_Renaming_Declaration (Loc,
4893 Defining_Identifier => Id,
4894 Subtype_Mark => New_Occurrence_Of (T, Loc),
4895 Name => E));
4897 Set_Renamed_Object (Id, E);
4898 Freeze_Before (N, T);
4899 Set_Is_Frozen (Id);
4900 goto Leave;
4901 end if;
4903 else
4904 -- Ensure that the generated subtype has a unique external name
4905 -- when the related object is public. This guarantees that the
4906 -- subtype and its bounds will not be affected by switches or
4907 -- pragmas that may offset the internal counter due to extra
4908 -- generated code.
4910 if Is_Public (Id) then
4911 Related_Id := Id;
4912 else
4913 Related_Id := Empty;
4914 end if;
4916 -- If the object has an unconstrained array subtype with fixed
4917 -- lower bound, then sliding to that bound may be needed.
4919 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4920 Expand_Sliding_Conversion (E, T);
4921 end if;
4923 if In_Spec_Expression and then In_Declare_Expr > 0 then
4924 -- It is too early to be doing expansion-ish things,
4925 -- so exit early. But we have to set Ekind (Id) now so
4926 -- that subsequent uses of this entity are not rejected
4927 -- via the same mechanism that (correctly) rejects
4928 -- "X : Integer := X;".
4930 if Constant_Present (N) then
4931 Mutate_Ekind (Id, E_Constant);
4932 Set_Is_True_Constant (Id);
4933 else
4934 Mutate_Ekind (Id, E_Variable);
4935 if Present (E) then
4936 Set_Has_Initial_Value (Id);
4937 end if;
4938 end if;
4940 goto Leave;
4941 end if;
4943 Expand_Subtype_From_Expr
4944 (N => N,
4945 Unc_Type => T,
4946 Subtype_Indic => Object_Definition (N),
4947 Exp => E,
4948 Related_Id => Related_Id);
4950 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4951 end if;
4953 if Act_T /= T then
4954 declare
4955 Full_Act_T : constant Entity_Id :=
4956 (if Is_Private_Type (Act_T)
4957 then Full_View (Act_T)
4958 else Empty);
4959 -- Propagate attributes to full view when needed
4961 begin
4962 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4964 if Present (Full_Act_T) then
4965 Set_Is_Constr_Subt_For_U_Nominal (Full_Act_T);
4966 end if;
4968 -- If the object is aliased, then it may be pointed to by an
4969 -- access-to-unconstrained-array value, which means that it
4970 -- must be allocated with its bounds.
4972 if Aliased_Present (N)
4973 and then (Is_Array_Type (Act_T)
4974 or else (Present (Full_Act_T)
4975 and then Is_Array_Type (Full_Act_T)))
4976 then
4977 Set_Is_Constr_Array_Subt_With_Bounds (Act_T);
4979 if Present (Full_Act_T) then
4980 Set_Is_Constr_Array_Subt_With_Bounds (Full_Act_T);
4981 end if;
4982 end if;
4984 Freeze_Before (N, Act_T);
4985 end;
4986 end if;
4988 Freeze_Before (N, T);
4989 end if;
4991 elsif Is_Array_Type (T)
4992 and then No_Initialization (N)
4993 and then (Nkind (Original_Node (E)) = N_Aggregate
4994 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4995 and then Nkind (Original_Node (Expression
4996 (Original_Node (E)))) = N_Aggregate))
4997 then
4998 if not Is_Entity_Name (Object_Definition (N)) then
4999 Act_T := Etype (E);
5000 Check_Compile_Time_Size (Act_T);
5001 end if;
5003 -- When the given object definition and the aggregate are specified
5004 -- independently, and their lengths might differ do a length check.
5005 -- This cannot happen if the aggregate is of the form (others =>...)
5007 if Nkind (E) = N_Raise_Constraint_Error then
5009 -- Aggregate is statically illegal. Place back in declaration
5011 Set_Expression (N, E);
5012 Set_No_Initialization (N, False);
5014 elsif T = Etype (E) then
5015 null;
5017 elsif Nkind (E) = N_Aggregate
5018 and then Present (Component_Associations (E))
5019 and then Present (Choice_List (First (Component_Associations (E))))
5020 and then
5021 Nkind (First (Choice_List (First (Component_Associations (E))))) =
5022 N_Others_Choice
5023 then
5024 null;
5026 else
5027 Apply_Length_Check (E, T);
5028 end if;
5030 -- When possible, and not a deferred constant, build the default subtype
5032 elsif Build_Default_Subtype_OK (T)
5033 and then (not Constant_Present (N) or else Present (E))
5034 then
5035 if No (E) then
5036 Act_T := Build_Default_Subtype (T, N);
5037 else
5038 -- Ada 2005: A limited object may be initialized by means of an
5039 -- aggregate. If the type has default discriminants it has an
5040 -- unconstrained nominal type, Its actual subtype will be obtained
5041 -- from the aggregate, and not from the default discriminants.
5043 Act_T := Etype (E);
5044 end if;
5046 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
5047 Freeze_Before (N, Act_T);
5049 elsif Nkind (E) = N_Function_Call
5050 and then Constant_Present (N)
5051 and then Has_Unconstrained_Elements (Etype (E))
5052 then
5053 -- The back-end has problems with constants of a discriminated type
5054 -- with defaults, if the initial value is a function call. We
5055 -- generate an intermediate temporary that will receive a reference
5056 -- to the result of the call. The initialization expression then
5057 -- becomes a dereference of that temporary.
5059 Remove_Side_Effects (E);
5061 -- If this is a constant declaration of an unconstrained type and
5062 -- the initialization is an aggregate, we can use the subtype of the
5063 -- aggregate for the declared entity because it is immutable.
5065 elsif not Is_Constrained (T)
5066 and then Has_Discriminants (T)
5067 and then Constant_Present (N)
5068 and then not Has_Unchecked_Union (T)
5069 and then Nkind (E) = N_Aggregate
5070 then
5071 Act_T := Etype (E);
5072 end if;
5074 -- Check No_Wide_Characters restriction
5076 Check_Wide_Character_Restriction (T, Object_Definition (N));
5078 -- Indicate this is not set in source. Certainly true for constants, and
5079 -- true for variables so far (will be reset for a variable if and when
5080 -- we encounter a modification in the source).
5082 Set_Never_Set_In_Source (Id);
5084 -- Now establish the proper kind and type of the object
5086 if Ekind (Id) = E_Void then
5087 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5088 end if;
5090 if Constant_Present (N) then
5091 Mutate_Ekind (Id, E_Constant);
5092 Set_Is_True_Constant (Id);
5094 else
5095 Mutate_Ekind (Id, E_Variable);
5097 -- A variable is set as shared passive if it appears in a shared
5098 -- passive package, and is at the outer level. This is not done for
5099 -- entities generated during expansion, because those are always
5100 -- manipulated locally.
5102 if Is_Shared_Passive (Current_Scope)
5103 and then Is_Library_Level_Entity (Id)
5104 and then Comes_From_Source (Id)
5105 then
5106 Set_Is_Shared_Passive (Id);
5107 Check_Shared_Var (Id, T, N);
5108 end if;
5110 -- Set Has_Initial_Value if initializing expression present. Note
5111 -- that if there is no initializing expression, we leave the state
5112 -- of this flag unchanged (usually it will be False, but notably in
5113 -- the case of exception choice variables, it will already be true).
5115 if Present (E) then
5116 Set_Has_Initial_Value (Id);
5117 end if;
5118 end if;
5120 -- Set the SPARK mode from the current context (may be overwritten later
5121 -- with explicit pragma).
5123 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5124 Set_SPARK_Pragma_Inherited (Id);
5126 -- Preserve relevant elaboration-related attributes of the context which
5127 -- are no longer available or very expensive to recompute once analysis,
5128 -- resolution, and expansion are over.
5130 Mark_Elaboration_Attributes
5131 (N_Id => Id,
5132 Checks => True,
5133 Warnings => True);
5135 -- Initialize alignment and size and capture alignment setting
5137 Reinit_Alignment (Id);
5138 Reinit_Esize (Id);
5139 Set_Optimize_Alignment_Flags (Id);
5141 -- Deal with aliased case
5143 if Aliased_Present (N) then
5144 Set_Is_Aliased (Id);
5146 -- AI12-001: All aliased objects are considered to be specified as
5147 -- independently addressable (RM C.6(8.1/4)).
5149 Set_Is_Independent (Id);
5151 -- If the object is aliased and the type is unconstrained with
5152 -- defaulted discriminants and there is no expression, then the
5153 -- object is constrained by the defaults, so it is worthwhile
5154 -- building the corresponding subtype.
5156 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5157 -- unconstrained, then only establish an actual subtype if the
5158 -- nominal subtype is indefinite. In definite cases the object is
5159 -- unconstrained in Ada 2005.
5161 if No (E)
5162 and then Is_Record_Type (T)
5163 and then not Is_Constrained (T)
5164 and then Has_Discriminants (T)
5165 and then (Ada_Version < Ada_2005
5166 or else not Is_Definite_Subtype (T))
5167 then
5168 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5169 end if;
5170 end if;
5172 -- Now we can set the type of the object
5174 Set_Etype (Id, Act_T);
5176 -- Non-constant object is marked to be treated as volatile if type is
5177 -- volatile and we clear the Current_Value setting that may have been
5178 -- set above. Doing so for constants isn't required and might interfere
5179 -- with possible uses of the object as a static expression in contexts
5180 -- incompatible with volatility (e.g. as a case-statement alternative).
5182 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5183 Set_Treat_As_Volatile (Id);
5184 Set_Current_Value (Id, Empty);
5185 end if;
5187 -- Deal with controlled types
5189 if Has_Controlled_Component (Etype (Id))
5190 or else Is_Controlled (Etype (Id))
5191 then
5192 if not Is_Library_Level_Entity (Id) then
5193 Check_Restriction (No_Nested_Finalization, N);
5194 else
5195 Validate_Controlled_Object (Id);
5196 end if;
5198 -- If the type of a constrained array has an unconstrained first
5199 -- subtype, its Finalize_Address primitive expects the address of
5200 -- an object with a dope vector (see Make_Finalize_Address_Stmts).
5202 if Is_Array_Type (Etype (Id))
5203 and then Is_Constrained (Etype (Id))
5204 and then not Is_Constrained (First_Subtype (Etype (Id)))
5205 then
5206 Set_Is_Constr_Array_Subt_With_Bounds (Etype (Id));
5207 end if;
5208 end if;
5210 if Has_Task (Etype (Id)) then
5211 Check_Restriction (No_Tasking, N);
5213 -- Deal with counting max tasks
5215 -- Nothing to do if inside a generic
5217 if Inside_A_Generic then
5218 null;
5220 -- If library level entity, then count tasks
5222 elsif Is_Library_Level_Entity (Id) then
5223 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5225 -- If not library level entity, then indicate we don't know max
5226 -- tasks and also check task hierarchy restriction and blocking
5227 -- operation (since starting a task is definitely blocking).
5229 else
5230 Check_Restriction (Max_Tasks, N);
5231 Check_Restriction (No_Task_Hierarchy, N);
5232 Check_Potentially_Blocking_Operation (N);
5233 end if;
5235 -- A rather specialized test. If we see two tasks being declared
5236 -- of the same type in the same object declaration, and the task
5237 -- has an entry with an address clause, we know that program error
5238 -- will be raised at run time since we can't have two tasks with
5239 -- entries at the same address.
5241 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5242 declare
5243 E : Entity_Id;
5245 begin
5246 E := First_Entity (Etype (Id));
5247 while Present (E) loop
5248 if Ekind (E) = E_Entry
5249 and then Present (Get_Attribute_Definition_Clause
5250 (E, Attribute_Address))
5251 then
5252 Error_Msg_Warn := SPARK_Mode /= On;
5253 Error_Msg_N
5254 ("more than one task with same entry address<<", N);
5255 Error_Msg_N ("\Program_Error [<<", N);
5256 Insert_Action (N,
5257 Make_Raise_Program_Error (Loc,
5258 Reason => PE_Duplicated_Entry_Address));
5259 exit;
5260 end if;
5262 Next_Entity (E);
5263 end loop;
5264 end;
5265 end if;
5266 end if;
5268 -- Check specific legality rules for a return object
5270 if Is_Return_Object (Id) then
5271 Check_Return_Subtype_Indication (N);
5272 end if;
5274 -- Some simple constant-propagation: if the expression is a constant
5275 -- string initialized with a literal, share the literal. This avoids
5276 -- a run-time copy.
5278 if Present (E)
5279 and then Is_Entity_Name (E)
5280 and then Ekind (Entity (E)) = E_Constant
5281 and then Base_Type (Etype (E)) = Standard_String
5282 then
5283 declare
5284 Val : constant Node_Id := Constant_Value (Entity (E));
5285 begin
5286 if Present (Val) and then Nkind (Val) = N_String_Literal then
5287 Rewrite (E, New_Copy (Val));
5288 end if;
5289 end;
5290 end if;
5292 if Present (Prev_Entity)
5293 and then Is_Frozen (Prev_Entity)
5294 and then not Error_Posted (Id)
5295 then
5296 Error_Msg_N ("full constant declaration appears too late", N);
5297 end if;
5299 Check_Eliminated (Id);
5301 -- Deal with setting In_Private_Part flag if in private part
5303 if Ekind (Scope (Id)) = E_Package
5304 and then In_Private_Part (Scope (Id))
5305 then
5306 Set_In_Private_Part (Id);
5307 end if;
5309 <<Leave>>
5310 -- Initialize the refined state of a variable here because this is a
5311 -- common destination for legal and illegal object declarations.
5313 if Ekind (Id) = E_Variable then
5314 Set_Encapsulating_State (Id, Empty);
5315 end if;
5317 Analyze_Aspect_Specifications (N, Id);
5319 Analyze_Dimension (N);
5321 -- Verify whether the object declaration introduces an illegal hidden
5322 -- state within a package subject to a null abstract state.
5324 if Ekind (Id) = E_Variable then
5325 Check_No_Hidden_State (Id);
5326 end if;
5328 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5329 end Analyze_Object_Declaration;
5331 ---------------------------
5332 -- Analyze_Others_Choice --
5333 ---------------------------
5335 -- Nothing to do for the others choice node itself, the semantic analysis
5336 -- of the others choice will occur as part of the processing of the parent
5338 procedure Analyze_Others_Choice (N : Node_Id) is
5339 pragma Warnings (Off, N);
5340 begin
5341 null;
5342 end Analyze_Others_Choice;
5344 -------------------------------------------
5345 -- Analyze_Private_Extension_Declaration --
5346 -------------------------------------------
5348 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5349 Indic : constant Node_Id := Subtype_Indication (N);
5350 T : constant Entity_Id := Defining_Identifier (N);
5351 Iface : Entity_Id;
5352 Iface_Elmt : Elmt_Id;
5353 Parent_Base : Entity_Id;
5354 Parent_Type : Entity_Id;
5356 begin
5357 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5359 if Is_Non_Empty_List (Interface_List (N)) then
5360 declare
5361 Intf : Node_Id;
5362 T : Entity_Id;
5364 begin
5365 Intf := First (Interface_List (N));
5366 while Present (Intf) loop
5367 T := Find_Type_Of_Subtype_Indic (Intf);
5369 Diagnose_Interface (Intf, T);
5370 Next (Intf);
5371 end loop;
5372 end;
5373 end if;
5375 Generate_Definition (T);
5377 -- For other than Ada 2012, just enter the name in the current scope
5379 if Ada_Version < Ada_2012 then
5380 Enter_Name (T);
5382 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5383 -- case of private type that completes an incomplete type.
5385 else
5386 declare
5387 Prev : Entity_Id;
5389 begin
5390 Prev := Find_Type_Name (N);
5392 pragma Assert (Prev = T
5393 or else (Ekind (Prev) = E_Incomplete_Type
5394 and then Present (Full_View (Prev))
5395 and then Full_View (Prev) = T));
5396 end;
5397 end if;
5399 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5400 Parent_Base := Base_Type (Parent_Type);
5402 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5403 Mutate_Ekind (T, Ekind (Parent_Type));
5404 Set_Etype (T, Any_Type);
5405 goto Leave;
5407 elsif not Is_Tagged_Type (Parent_Type) then
5408 Error_Msg_N
5409 ("parent of type extension must be a tagged type", Indic);
5410 goto Leave;
5412 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5413 Error_Msg_N ("premature derivation of incomplete type", Indic);
5414 goto Leave;
5416 elsif Is_Concurrent_Type (Parent_Type) then
5417 Error_Msg_N
5418 ("parent type of a private extension cannot be a synchronized "
5419 & "tagged type (RM 3.9.1 (3/1))", N);
5421 Set_Etype (T, Any_Type);
5422 Mutate_Ekind (T, E_Limited_Private_Type);
5423 Set_Private_Dependents (T, New_Elmt_List);
5424 Set_Error_Posted (T);
5425 goto Leave;
5426 end if;
5428 Check_Wide_Character_Restriction (Parent_Type, Indic);
5430 -- Perhaps the parent type should be changed to the class-wide type's
5431 -- specific type in this case to prevent cascading errors ???
5433 if Is_Class_Wide_Type (Parent_Type) then
5434 Error_Msg_N
5435 ("parent of type extension must not be a class-wide type", Indic);
5436 goto Leave;
5437 end if;
5439 if (not Is_Package_Or_Generic_Package (Current_Scope)
5440 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5441 or else In_Private_Part (Current_Scope)
5442 then
5443 Error_Msg_N ("invalid context for private extension", N);
5444 end if;
5446 -- Set common attributes
5448 Set_Is_Pure (T, Is_Pure (Current_Scope));
5449 Set_Scope (T, Current_Scope);
5450 Mutate_Ekind (T, E_Record_Type_With_Private);
5451 Reinit_Size_Align (T);
5452 Set_Default_SSO (T);
5453 Set_No_Reordering (T, No_Component_Reordering);
5455 Set_Etype (T, Parent_Base);
5456 Propagate_Concurrent_Flags (T, Parent_Base);
5458 Set_Convention (T, Convention (Parent_Type));
5459 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5460 Set_Is_First_Subtype (T);
5462 -- Set the SPARK mode from the current context
5464 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5465 Set_SPARK_Pragma_Inherited (T);
5467 if Unknown_Discriminants_Present (N) then
5468 Set_Discriminant_Constraint (T, No_Elist);
5469 end if;
5471 Build_Derived_Record_Type (N, Parent_Type, T);
5473 -- A private extension inherits the Default_Initial_Condition pragma
5474 -- coming from any parent type within the derivation chain.
5476 if Has_DIC (Parent_Type) then
5477 Set_Has_Inherited_DIC (T);
5478 end if;
5480 -- A private extension inherits any class-wide invariants coming from a
5481 -- parent type or an interface. Note that the invariant procedure of the
5482 -- parent type should not be inherited because the private extension may
5483 -- define invariants of its own.
5485 if Has_Inherited_Invariants (Parent_Type)
5486 or else Has_Inheritable_Invariants (Parent_Type)
5487 then
5488 Set_Has_Inherited_Invariants (T);
5490 elsif Present (Interfaces (T)) then
5491 Iface_Elmt := First_Elmt (Interfaces (T));
5492 while Present (Iface_Elmt) loop
5493 Iface := Node (Iface_Elmt);
5495 if Has_Inheritable_Invariants (Iface) then
5496 Set_Has_Inherited_Invariants (T);
5497 exit;
5498 end if;
5500 Next_Elmt (Iface_Elmt);
5501 end loop;
5502 end if;
5504 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5505 -- synchronized formal derived type.
5507 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5508 Set_Is_Limited_Record (T);
5510 -- Formal derived type case
5512 if Is_Generic_Type (T) then
5514 -- The parent must be a tagged limited type or a synchronized
5515 -- interface.
5517 if (not Is_Tagged_Type (Parent_Type)
5518 or else not Is_Limited_Type (Parent_Type))
5519 and then
5520 (not Is_Interface (Parent_Type)
5521 or else not Is_Synchronized_Interface (Parent_Type))
5522 then
5523 Error_Msg_NE
5524 ("parent type of & must be tagged limited or synchronized",
5525 N, T);
5526 end if;
5528 -- The progenitors (if any) must be limited or synchronized
5529 -- interfaces.
5531 if Present (Interfaces (T)) then
5532 Iface_Elmt := First_Elmt (Interfaces (T));
5533 while Present (Iface_Elmt) loop
5534 Iface := Node (Iface_Elmt);
5536 if not Is_Limited_Interface (Iface)
5537 and then not Is_Synchronized_Interface (Iface)
5538 then
5539 Error_Msg_NE
5540 ("progenitor & must be limited or synchronized",
5541 N, Iface);
5542 end if;
5544 Next_Elmt (Iface_Elmt);
5545 end loop;
5546 end if;
5548 -- Regular derived extension, the parent must be a limited or
5549 -- synchronized interface.
5551 else
5552 if not Is_Interface (Parent_Type)
5553 or else (not Is_Limited_Interface (Parent_Type)
5554 and then not Is_Synchronized_Interface (Parent_Type))
5555 then
5556 Error_Msg_NE
5557 ("parent type of & must be limited interface", N, T);
5558 end if;
5559 end if;
5561 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5562 -- extension with a synchronized parent must be explicitly declared
5563 -- synchronized, because the full view will be a synchronized type.
5564 -- This must be checked before the check for limited types below,
5565 -- to ensure that types declared limited are not allowed to extend
5566 -- synchronized interfaces.
5568 elsif Is_Interface (Parent_Type)
5569 and then Is_Synchronized_Interface (Parent_Type)
5570 and then not Synchronized_Present (N)
5571 then
5572 Error_Msg_NE
5573 ("private extension of& must be explicitly synchronized",
5574 N, Parent_Type);
5576 elsif Limited_Present (N) then
5577 Set_Is_Limited_Record (T);
5579 if not Is_Limited_Type (Parent_Type)
5580 and then
5581 (not Is_Interface (Parent_Type)
5582 or else not Is_Limited_Interface (Parent_Type))
5583 then
5584 Error_Msg_NE ("parent type& of limited extension must be limited",
5585 N, Parent_Type);
5586 end if;
5587 end if;
5589 -- Remember that its parent type has a private extension. Used to warn
5590 -- on public primitives of the parent type defined after its private
5591 -- extensions (see Check_Dispatching_Operation).
5593 Set_Has_Private_Extension (Parent_Type);
5595 <<Leave>>
5596 Analyze_Aspect_Specifications (N, T);
5597 end Analyze_Private_Extension_Declaration;
5599 ---------------------------------
5600 -- Analyze_Subtype_Declaration --
5601 ---------------------------------
5603 procedure Analyze_Subtype_Declaration
5604 (N : Node_Id;
5605 Skip : Boolean := False)
5607 Id : constant Entity_Id := Defining_Identifier (N);
5608 T : Entity_Id;
5610 begin
5611 Generate_Definition (Id);
5612 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5613 Reinit_Size_Align (Id);
5615 -- The following guard condition on Enter_Name is to handle cases where
5616 -- the defining identifier has already been entered into the scope but
5617 -- the declaration as a whole needs to be analyzed.
5619 -- This case in particular happens for derived enumeration types. The
5620 -- derived enumeration type is processed as an inserted enumeration type
5621 -- declaration followed by a rewritten subtype declaration. The defining
5622 -- identifier, however, is entered into the name scope very early in the
5623 -- processing of the original type declaration and therefore needs to be
5624 -- avoided here, when the created subtype declaration is analyzed. (See
5625 -- Build_Derived_Types)
5627 -- This also happens when the full view of a private type is a derived
5628 -- type with constraints. In this case the entity has been introduced
5629 -- in the private declaration.
5631 -- Finally this happens in some complex cases when validity checks are
5632 -- enabled, where the same subtype declaration may be analyzed twice.
5633 -- This can happen if the subtype is created by the preanalysis of
5634 -- an attribute that gives the range of a loop statement, and the loop
5635 -- itself appears within an if_statement that will be rewritten during
5636 -- expansion.
5638 if Skip
5639 or else (Present (Etype (Id))
5640 and then (Is_Private_Type (Etype (Id))
5641 or else Is_Task_Type (Etype (Id))
5642 or else Is_Rewrite_Substitution (N)))
5643 then
5644 null;
5646 elsif Current_Entity (Id) = Id then
5647 null;
5649 else
5650 Enter_Name (Id);
5651 end if;
5653 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5655 -- Class-wide equivalent types of records with unknown discriminants
5656 -- involve the generation of an itype which serves as the private view
5657 -- of a constrained record subtype. In such cases the base type of the
5658 -- current subtype we are processing is the private itype. Use the full
5659 -- of the private itype when decorating various attributes.
5661 if Is_Itype (T)
5662 and then Is_Private_Type (T)
5663 and then Present (Full_View (T))
5664 then
5665 T := Full_View (T);
5666 end if;
5668 -- Inherit common attributes
5670 Set_Is_Volatile (Id, Is_Volatile (T));
5671 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5672 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5673 Set_Convention (Id, Convention (T));
5675 -- If ancestor has predicates then so does the subtype, and in addition
5676 -- we must delay the freeze to properly arrange predicate inheritance.
5678 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5679 -- in which T = ID, so the above tests and assignments do nothing???
5681 if Has_Predicates (T)
5682 or else (Present (Ancestor_Subtype (T))
5683 and then Has_Predicates (Ancestor_Subtype (T)))
5684 then
5685 Set_Has_Predicates (Id);
5686 Set_Has_Delayed_Freeze (Id);
5688 -- Generated subtypes inherit the predicate function from the parent
5689 -- (no aspects to examine on the generated declaration).
5691 if not Comes_From_Source (N) then
5692 Mutate_Ekind (Id, Ekind (T));
5694 if Present (Predicate_Function (Id)) then
5695 null;
5697 elsif Present (Predicate_Function (T)) then
5698 Set_Predicate_Function (Id, Predicate_Function (T));
5700 elsif Present (Ancestor_Subtype (T))
5701 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5702 then
5703 Set_Predicate_Function (Id,
5704 Predicate_Function (Ancestor_Subtype (T)));
5705 end if;
5706 end if;
5707 end if;
5709 -- In the case where there is no constraint given in the subtype
5710 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5711 -- semantic attributes must be established here.
5713 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5714 Set_Etype (Id, Base_Type (T));
5716 case Ekind (T) is
5717 when Array_Kind =>
5718 Mutate_Ekind (Id, E_Array_Subtype);
5719 Copy_Array_Subtype_Attributes (Id, T);
5720 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5722 when Decimal_Fixed_Point_Kind =>
5723 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5724 Set_Digits_Value (Id, Digits_Value (T));
5725 Set_Delta_Value (Id, Delta_Value (T));
5726 Set_Scale_Value (Id, Scale_Value (T));
5727 Set_Small_Value (Id, Small_Value (T));
5728 Set_Scalar_Range (Id, Scalar_Range (T));
5729 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5730 Set_Is_Constrained (Id, Is_Constrained (T));
5731 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5732 Copy_RM_Size (To => Id, From => T);
5734 when Enumeration_Kind =>
5735 Mutate_Ekind (Id, E_Enumeration_Subtype);
5736 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5737 Set_Scalar_Range (Id, Scalar_Range (T));
5738 Set_Is_Character_Type (Id, Is_Character_Type (T));
5739 Set_Is_Constrained (Id, Is_Constrained (T));
5740 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5741 Copy_RM_Size (To => Id, From => T);
5743 when Ordinary_Fixed_Point_Kind =>
5744 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5745 Set_Scalar_Range (Id, Scalar_Range (T));
5746 Set_Small_Value (Id, Small_Value (T));
5747 Set_Delta_Value (Id, Delta_Value (T));
5748 Set_Is_Constrained (Id, Is_Constrained (T));
5749 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5750 Copy_RM_Size (To => Id, From => T);
5752 when Float_Kind =>
5753 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5754 Set_Scalar_Range (Id, Scalar_Range (T));
5755 Set_Digits_Value (Id, Digits_Value (T));
5756 Set_Is_Constrained (Id, Is_Constrained (T));
5758 -- If the floating point type has dimensions, these will be
5759 -- inherited subsequently when Analyze_Dimensions is called.
5761 when Signed_Integer_Kind =>
5762 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5763 Set_Scalar_Range (Id, Scalar_Range (T));
5764 Set_Is_Constrained (Id, Is_Constrained (T));
5765 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5766 Copy_RM_Size (To => Id, From => T);
5768 when Modular_Integer_Kind =>
5769 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5770 Set_Scalar_Range (Id, Scalar_Range (T));
5771 Set_Is_Constrained (Id, Is_Constrained (T));
5772 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5773 Copy_RM_Size (To => Id, From => T);
5775 when Class_Wide_Kind =>
5776 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5777 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5778 Set_Cloned_Subtype (Id, T);
5779 Set_Is_Tagged_Type (Id, True);
5780 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5781 Set_Has_Unknown_Discriminants
5782 (Id, True);
5783 Set_No_Tagged_Streams_Pragma
5784 (Id, No_Tagged_Streams_Pragma (T));
5786 if Ekind (T) = E_Class_Wide_Subtype then
5787 Set_Equivalent_Type (Id, Equivalent_Type (T));
5788 end if;
5790 when E_Record_Subtype
5791 | E_Record_Type
5793 Mutate_Ekind (Id, E_Record_Subtype);
5795 -- Subtype declarations introduced for formal type parameters
5796 -- in generic instantiations should inherit the Size value of
5797 -- the type they rename.
5799 if Present (Generic_Parent_Type (N)) then
5800 Copy_RM_Size (To => Id, From => T);
5801 end if;
5803 if Ekind (T) = E_Record_Subtype
5804 and then Present (Cloned_Subtype (T))
5805 then
5806 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5807 else
5808 Set_Cloned_Subtype (Id, T);
5809 end if;
5811 Set_First_Entity (Id, First_Entity (T));
5812 Set_Last_Entity (Id, Last_Entity (T));
5813 Set_Has_Discriminants (Id, Has_Discriminants (T));
5814 Set_Is_Constrained (Id, Is_Constrained (T));
5815 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5816 Set_Has_Implicit_Dereference
5817 (Id, Has_Implicit_Dereference (T));
5818 Set_Has_Unknown_Discriminants
5819 (Id, Has_Unknown_Discriminants (T));
5821 if Has_Discriminants (T) then
5822 Set_Discriminant_Constraint
5823 (Id, Discriminant_Constraint (T));
5824 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5826 elsif Has_Unknown_Discriminants (Id) then
5827 Set_Discriminant_Constraint (Id, No_Elist);
5828 end if;
5830 if Is_Tagged_Type (T) then
5831 Set_Is_Tagged_Type (Id, True);
5832 Set_No_Tagged_Streams_Pragma
5833 (Id, No_Tagged_Streams_Pragma (T));
5834 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5835 Set_Direct_Primitive_Operations
5836 (Id, Direct_Primitive_Operations (T));
5837 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5839 if Is_Interface (T) then
5840 Set_Is_Interface (Id);
5841 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5842 end if;
5843 end if;
5845 when Private_Kind =>
5846 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5847 Set_Has_Discriminants (Id, Has_Discriminants (T));
5848 Set_Is_Constrained (Id, Is_Constrained (T));
5849 Set_First_Entity (Id, First_Entity (T));
5850 Set_Last_Entity (Id, Last_Entity (T));
5851 Set_Private_Dependents (Id, New_Elmt_List);
5852 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5853 Set_Has_Implicit_Dereference
5854 (Id, Has_Implicit_Dereference (T));
5855 Set_Has_Unknown_Discriminants
5856 (Id, Has_Unknown_Discriminants (T));
5857 Set_Known_To_Have_Preelab_Init
5858 (Id, Known_To_Have_Preelab_Init (T));
5860 if Is_Tagged_Type (T) then
5861 Set_Is_Tagged_Type (Id);
5862 Set_No_Tagged_Streams_Pragma (Id,
5863 No_Tagged_Streams_Pragma (T));
5864 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5865 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5866 Set_Direct_Primitive_Operations (Id,
5867 Direct_Primitive_Operations (T));
5868 end if;
5870 -- In general the attributes of the subtype of a private type
5871 -- are the attributes of the partial view of parent. However,
5872 -- the full view may be a discriminated type, and the subtype
5873 -- must share the discriminant constraint to generate correct
5874 -- calls to initialization procedures.
5876 if Has_Discriminants (T) then
5877 Set_Discriminant_Constraint
5878 (Id, Discriminant_Constraint (T));
5879 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5881 elsif Present (Full_View (T))
5882 and then Has_Discriminants (Full_View (T))
5883 then
5884 Set_Discriminant_Constraint
5885 (Id, Discriminant_Constraint (Full_View (T)));
5886 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5888 -- This would seem semantically correct, but apparently
5889 -- generates spurious errors about missing components ???
5891 -- Set_Has_Discriminants (Id);
5892 end if;
5894 Prepare_Private_Subtype_Completion (Id, N);
5896 -- If this is the subtype of a constrained private type with
5897 -- discriminants that has got a full view and we also have
5898 -- built a completion just above, show that the completion
5899 -- is a clone of the full view to the back-end.
5901 if Has_Discriminants (T)
5902 and then not Has_Unknown_Discriminants (T)
5903 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5904 and then Present (Full_View (T))
5905 and then Present (Full_View (Id))
5906 then
5907 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5908 end if;
5910 when Access_Kind =>
5911 Mutate_Ekind (Id, E_Access_Subtype);
5912 Set_Is_Constrained (Id, Is_Constrained (T));
5913 Set_Is_Access_Constant
5914 (Id, Is_Access_Constant (T));
5915 Set_Directly_Designated_Type
5916 (Id, Designated_Type (T));
5917 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5919 -- A Pure library_item must not contain the declaration of a
5920 -- named access type, except within a subprogram, generic
5921 -- subprogram, task unit, or protected unit, or if it has
5922 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5924 if Comes_From_Source (Id)
5925 and then In_Pure_Unit
5926 and then not In_Subprogram_Task_Protected_Unit
5927 and then not No_Pool_Assigned (Id)
5928 then
5929 Error_Msg_N
5930 ("named access types not allowed in pure unit", N);
5931 end if;
5933 when Concurrent_Kind =>
5934 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5935 Set_Corresponding_Record_Type (Id,
5936 Corresponding_Record_Type (T));
5937 Set_First_Entity (Id, First_Entity (T));
5938 Set_First_Private_Entity (Id, First_Private_Entity (T));
5939 Set_Has_Discriminants (Id, Has_Discriminants (T));
5940 Set_Is_Constrained (Id, Is_Constrained (T));
5941 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5942 Set_Last_Entity (Id, Last_Entity (T));
5944 if Is_Tagged_Type (T) then
5945 Set_No_Tagged_Streams_Pragma
5946 (Id, No_Tagged_Streams_Pragma (T));
5947 end if;
5949 if Has_Discriminants (T) then
5950 Set_Discriminant_Constraint
5951 (Id, Discriminant_Constraint (T));
5952 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5953 end if;
5955 when Incomplete_Kind =>
5956 if Ada_Version >= Ada_2005 then
5958 -- In Ada 2005 an incomplete type can be explicitly tagged:
5959 -- propagate indication. Note that we also have to include
5960 -- subtypes for Ada 2012 extended use of incomplete types.
5962 Mutate_Ekind (Id, E_Incomplete_Subtype);
5963 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5964 Set_Private_Dependents (Id, New_Elmt_List);
5966 if Is_Tagged_Type (Id) then
5967 Set_No_Tagged_Streams_Pragma
5968 (Id, No_Tagged_Streams_Pragma (T));
5969 end if;
5971 -- For tagged types, or when prefixed-call syntax is allowed
5972 -- for untagged types, initialize the list of primitive
5973 -- operations to an empty list.
5975 if Is_Tagged_Type (Id)
5976 or else Core_Extensions_Allowed
5977 then
5978 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5979 end if;
5981 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5982 -- incomplete type visible through a limited with clause.
5984 if From_Limited_With (T)
5985 and then Present (Non_Limited_View (T))
5986 then
5987 Set_From_Limited_With (Id);
5988 Set_Non_Limited_View (Id, Non_Limited_View (T));
5990 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5991 -- to the private dependents of the original incomplete
5992 -- type for future transformation.
5994 else
5995 Append_Elmt (Id, Private_Dependents (T));
5996 end if;
5998 -- If the subtype name denotes an incomplete type an error
5999 -- was already reported by Process_Subtype.
6001 else
6002 Set_Etype (Id, Any_Type);
6003 end if;
6005 when others =>
6006 raise Program_Error;
6007 end case;
6009 -- If there is no constraint in the subtype indication, the
6010 -- declared entity inherits predicates from the parent.
6012 Inherit_Predicate_Flags (Id, T);
6013 end if;
6015 if Etype (Id) = Any_Type then
6016 goto Leave;
6017 end if;
6019 -- When prefixed calls are enabled for untagged types, the subtype
6020 -- shares the primitive operations of its base type. Do this even
6021 -- when Extensions_Allowed is False to issue better error messages.
6023 Set_Direct_Primitive_Operations
6024 (Id, Direct_Primitive_Operations (Base_Type (T)));
6026 -- Some common processing on all types
6028 Set_Size_Info (Id, T);
6029 Set_First_Rep_Item (Id, First_Rep_Item (T));
6031 -- If the parent type is a generic actual, so is the subtype. This may
6032 -- happen in a nested instance. Why Comes_From_Source test???
6034 if not Comes_From_Source (N) then
6035 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
6036 end if;
6038 -- If this is a subtype declaration for an actual in an instance,
6039 -- inherit static and dynamic predicates if any.
6041 if Has_Predicates (T)
6042 and then Present (Predicate_Function (T))
6043 and then In_Instance
6044 and then not Comes_From_Source (N)
6045 then
6046 -- Inherit Subprograms_For_Type from the full view, if present
6048 if Present (Full_View (T))
6049 and then Present (Subprograms_For_Type (Full_View (T)))
6050 then
6051 Set_Subprograms_For_Type
6052 (Id, Subprograms_For_Type (Full_View (T)));
6053 else
6054 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
6055 end if;
6057 -- If the current declaration created both a private and a full view,
6058 -- then propagate Predicate_Function to the latter as well.
6060 if Present (Full_View (Id))
6061 and then No (Predicate_Function (Full_View (Id)))
6062 then
6063 Set_Subprograms_For_Type
6064 (Full_View (Id), Subprograms_For_Type (Id));
6065 end if;
6067 if Has_Static_Predicate (T) then
6068 Set_Has_Static_Predicate (Id);
6069 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
6070 end if;
6071 end if;
6073 -- If the base type is a scalar type, or else if there is no
6074 -- constraint, the atomic flag is inherited by the subtype.
6075 -- Ditto for the Independent aspect.
6077 if Is_Scalar_Type (Id)
6078 or else Is_Entity_Name (Subtype_Indication (N))
6079 then
6080 Set_Is_Atomic (Id, Is_Atomic (T));
6081 Set_Is_Independent (Id, Is_Independent (T));
6082 end if;
6084 -- Remaining processing depends on characteristics of base type
6086 T := Etype (Id);
6088 Set_Is_Immediately_Visible (Id, True);
6089 Set_Depends_On_Private (Id, Has_Private_Component (T));
6090 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6092 if Is_Interface (T) then
6093 Set_Is_Interface (Id);
6094 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6095 end if;
6097 if Present (Generic_Parent_Type (N))
6098 and then
6099 (Nkind (Parent (Generic_Parent_Type (N))) /=
6100 N_Formal_Type_Declaration
6101 or else Nkind (Formal_Type_Definition
6102 (Parent (Generic_Parent_Type (N)))) /=
6103 N_Formal_Private_Type_Definition)
6104 then
6105 if Is_Tagged_Type (Id) then
6107 -- If this is a generic actual subtype for a synchronized type,
6108 -- the primitive operations are those of the corresponding record
6109 -- for which there is a separate subtype declaration.
6111 if Is_Concurrent_Type (Id) then
6112 null;
6113 elsif Is_Class_Wide_Type (Id) then
6114 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6115 else
6116 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6117 end if;
6119 elsif Scope (Etype (Id)) /= Standard_Standard then
6120 Derive_Subprograms (Generic_Parent_Type (N), Id);
6121 end if;
6122 end if;
6124 if Is_Private_Type (T) and then Present (Full_View (T)) then
6125 Conditional_Delay (Id, Full_View (T));
6127 -- The subtypes of components or subcomponents of protected types
6128 -- do not need freeze nodes, which would otherwise appear in the
6129 -- wrong scope (before the freeze node for the protected type). The
6130 -- proper subtypes are those of the subcomponents of the corresponding
6131 -- record.
6133 elsif Ekind (Scope (Id)) /= E_Protected_Type
6134 and then Present (Scope (Scope (Id))) -- error defense
6135 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6136 then
6137 Conditional_Delay (Id, T);
6138 end if;
6140 -- If we have a subtype of an incomplete type whose full type is a
6141 -- derived numeric type, we need to have a freeze node for the subtype.
6142 -- Otherwise gigi will complain while computing the (static) bounds of
6143 -- the subtype.
6145 if Is_Itype (T)
6146 and then Is_Elementary_Type (Id)
6147 and then Etype (Id) /= Id
6148 then
6149 declare
6150 Partial : constant Entity_Id :=
6151 Incomplete_Or_Partial_View (First_Subtype (Id));
6152 begin
6153 if Present (Partial)
6154 and then Ekind (Partial) = E_Incomplete_Type
6155 then
6156 Set_Has_Delayed_Freeze (Id);
6157 end if;
6158 end;
6159 end if;
6161 -- Check that Constraint_Error is raised for a scalar subtype indication
6162 -- when the lower or upper bound of a non-null range lies outside the
6163 -- range of the type mark. Likewise for an array subtype, but check the
6164 -- compatibility for each index.
6166 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6167 declare
6168 Indic_Typ : constant Entity_Id :=
6169 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6170 Subt_Index : Node_Id;
6171 Target_Index : Node_Id;
6173 begin
6174 if Is_Scalar_Type (Etype (Id))
6175 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6176 then
6177 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6179 elsif Is_Array_Type (Etype (Id))
6180 and then Present (First_Index (Id))
6181 then
6182 Subt_Index := First_Index (Id);
6183 Target_Index := First_Index (Indic_Typ);
6185 while Present (Subt_Index) loop
6186 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6187 and then Is_Scalar_Type (Entity (Subt_Index)))
6188 or else Nkind (Subt_Index) = N_Subtype_Indication)
6189 and then
6190 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6191 then
6192 Apply_Range_Check
6193 (Scalar_Range (Etype (Subt_Index)),
6194 Etype (Target_Index),
6195 Insert_Node => N);
6196 end if;
6198 Next_Index (Subt_Index);
6199 Next_Index (Target_Index);
6200 end loop;
6201 end if;
6202 end;
6203 end if;
6205 Set_Optimize_Alignment_Flags (Id);
6206 Check_Eliminated (Id);
6208 <<Leave>>
6209 Analyze_Aspect_Specifications (N, Id);
6211 Analyze_Dimension (N);
6213 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6214 -- indications on composite types where the constraints are dynamic.
6215 -- Note that object declarations and aggregates generate implicit
6216 -- subtype declarations, which this covers. One special case is that the
6217 -- implicitly generated "=" for discriminated types includes an
6218 -- offending subtype declaration, which is harmless, so we ignore it
6219 -- here.
6221 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6222 declare
6223 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6224 begin
6225 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6226 and then not (Is_Internal (Id)
6227 and then Is_TSS (Scope (Id),
6228 TSS_Composite_Equality))
6229 and then not Within_Init_Proc
6230 and then not All_Composite_Constraints_Static (Cstr)
6231 then
6232 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6233 end if;
6234 end;
6235 end if;
6236 end Analyze_Subtype_Declaration;
6238 --------------------------------
6239 -- Analyze_Subtype_Indication --
6240 --------------------------------
6242 procedure Analyze_Subtype_Indication (N : Node_Id) is
6243 T : constant Entity_Id := Subtype_Mark (N);
6244 R : constant Node_Id := Range_Expression (Constraint (N));
6246 begin
6247 Analyze (T);
6249 if R = Error then
6250 Set_Error_Posted (R);
6251 Set_Error_Posted (T);
6252 else
6253 Analyze (R);
6254 Set_Etype (N, Etype (R));
6255 Resolve (R, Entity (T));
6256 end if;
6257 end Analyze_Subtype_Indication;
6259 --------------------------
6260 -- Analyze_Variant_Part --
6261 --------------------------
6263 procedure Analyze_Variant_Part (N : Node_Id) is
6264 Discr_Name : Node_Id;
6265 Discr_Type : Entity_Id;
6267 procedure Process_Variant (A : Node_Id);
6268 -- Analyze declarations for a single variant
6270 package Analyze_Variant_Choices is
6271 new Generic_Analyze_Choices (Process_Variant);
6272 use Analyze_Variant_Choices;
6274 ---------------------
6275 -- Process_Variant --
6276 ---------------------
6278 procedure Process_Variant (A : Node_Id) is
6279 CL : constant Node_Id := Component_List (A);
6280 begin
6281 if not Null_Present (CL) then
6282 Analyze_Declarations (Component_Items (CL));
6284 if Present (Variant_Part (CL)) then
6285 Analyze (Variant_Part (CL));
6286 end if;
6287 end if;
6288 end Process_Variant;
6290 -- Start of processing for Analyze_Variant_Part
6292 begin
6293 Discr_Name := Name (N);
6294 Analyze (Discr_Name);
6296 -- If Discr_Name bad, get out (prevent cascaded errors)
6298 if Etype (Discr_Name) = Any_Type then
6299 return;
6300 end if;
6302 -- Check invalid discriminant in variant part
6304 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6305 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6306 end if;
6308 Discr_Type := Etype (Entity (Discr_Name));
6310 if not Is_Discrete_Type (Discr_Type) then
6311 Error_Msg_N
6312 ("discriminant in a variant part must be of a discrete type",
6313 Name (N));
6314 return;
6315 end if;
6317 -- Now analyze the choices, which also analyzes the declarations that
6318 -- are associated with each choice.
6320 Analyze_Choices (Variants (N), Discr_Type);
6322 -- Note: we used to instantiate and call Check_Choices here to check
6323 -- that the choices covered the discriminant, but it's too early to do
6324 -- that because of statically predicated subtypes, whose analysis may
6325 -- be deferred to their freeze point which may be as late as the freeze
6326 -- point of the containing record. So this call is now to be found in
6327 -- Freeze_Record_Declaration.
6329 end Analyze_Variant_Part;
6331 ----------------------------
6332 -- Array_Type_Declaration --
6333 ----------------------------
6335 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6336 Component_Def : constant Node_Id := Component_Definition (Def);
6337 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6338 P : constant Node_Id := Parent (Def);
6339 Element_Type : Entity_Id;
6340 Implicit_Base : Entity_Id;
6341 Index : Node_Id;
6342 Nb_Index : Pos;
6343 Priv : Entity_Id;
6344 Related_Id : Entity_Id;
6345 Has_FLB_Index : Boolean := False;
6347 begin
6348 if Nkind (Def) = N_Constrained_Array_Definition then
6349 Index := First (Discrete_Subtype_Definitions (Def));
6350 else
6351 Index := First (Subtype_Marks (Def));
6352 end if;
6354 -- Find proper names for the implicit types which may be public. In case
6355 -- of anonymous arrays we use the name of the first object of that type
6356 -- as prefix.
6358 if No (T) then
6359 Related_Id := Defining_Identifier (P);
6360 else
6361 Related_Id := T;
6362 end if;
6364 Nb_Index := 1;
6365 while Present (Index) loop
6366 Analyze (Index);
6368 -- Test for odd case of trying to index a type by the type itself
6370 if Is_Entity_Name (Index) and then Entity (Index) = T then
6371 Error_Msg_N ("type& cannot be indexed by itself", Index);
6372 Set_Entity (Index, Standard_Boolean);
6373 Set_Etype (Index, Standard_Boolean);
6374 end if;
6376 -- Add a subtype declaration for each index of private array type
6377 -- declaration whose type is also private. For example:
6379 -- package Pkg is
6380 -- type Index is private;
6381 -- private
6382 -- type Table is array (Index) of ...
6383 -- end;
6385 -- This is currently required by the expander for the internally
6386 -- generated equality subprogram of records with variant parts in
6387 -- which the type of some component is such a private type. And it
6388 -- also helps semantic analysis in peculiar cases where the array
6389 -- type is referenced from an instance but not the index directly.
6391 if Is_Package_Or_Generic_Package (Current_Scope)
6392 and then In_Private_Part (Current_Scope)
6393 and then Has_Private_Declaration (Etype (Index))
6394 and then Scope (Etype (Index)) = Current_Scope
6395 then
6396 declare
6397 Loc : constant Source_Ptr := Sloc (Def);
6398 Decl : Node_Id;
6399 New_E : Entity_Id;
6401 begin
6402 New_E := Make_Temporary (Loc, 'T');
6403 Set_Is_Internal (New_E);
6405 Decl :=
6406 Make_Subtype_Declaration (Loc,
6407 Defining_Identifier => New_E,
6408 Subtype_Indication =>
6409 New_Occurrence_Of (Etype (Index), Loc));
6411 Insert_Before (Parent (Def), Decl);
6412 Analyze (Decl);
6413 Set_Etype (Index, New_E);
6415 -- If the index is a range or a subtype indication it carries
6416 -- no entity. Example:
6418 -- package Pkg is
6419 -- type T is private;
6420 -- private
6421 -- type T is new Natural;
6422 -- Table : array (T(1) .. T(10)) of Boolean;
6423 -- end Pkg;
6425 -- Otherwise the type of the reference is its entity.
6427 if Is_Entity_Name (Index) then
6428 Set_Entity (Index, New_E);
6429 end if;
6430 end;
6431 end if;
6433 Make_Index (Index, P, Related_Id, Nb_Index);
6435 -- In the case where we have an unconstrained array with an index
6436 -- given by a subtype_indication, this is necessarily a "fixed lower
6437 -- bound" index. We change the upper bound of that index to the upper
6438 -- bound of the index's subtype (denoted by the subtype_mark), since
6439 -- that upper bound was originally set by the parser to be the same
6440 -- as the lower bound. In truth, that upper bound corresponds to
6441 -- a box ("<>"), and could be set to Empty, but it's convenient to
6442 -- set it to the upper bound to avoid needing to add special tests
6443 -- in various places for an Empty upper bound, and in any case that
6444 -- accurately characterizes the index's range of values.
6446 if Nkind (Def) = N_Unconstrained_Array_Definition
6447 and then Nkind (Index) = N_Subtype_Indication
6448 then
6449 declare
6450 Index_Subtype_High_Bound : constant Entity_Id :=
6451 Type_High_Bound (Entity (Subtype_Mark (Index)));
6452 begin
6453 Set_High_Bound (Range_Expression (Constraint (Index)),
6454 Index_Subtype_High_Bound);
6456 -- Record that the array type has one or more indexes with
6457 -- a fixed lower bound.
6459 Has_FLB_Index := True;
6461 -- Mark the index as belonging to an array type with a fixed
6462 -- lower bound.
6464 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6465 end;
6466 end if;
6468 -- Check error of subtype with predicate for index type
6470 Bad_Predicated_Subtype_Use
6471 ("subtype& has predicate, not allowed as index subtype",
6472 Index, Etype (Index));
6474 -- Move to next index
6476 Next (Index);
6477 Nb_Index := Nb_Index + 1;
6478 end loop;
6480 -- Process subtype indication if one is present
6482 if Present (Component_Typ) then
6483 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6484 Set_Etype (Component_Typ, Element_Type);
6486 -- Ada 2005 (AI-230): Access Definition case
6488 else pragma Assert (Present (Access_Definition (Component_Def)));
6490 -- Indicate that the anonymous access type is created by the
6491 -- array type declaration.
6493 Element_Type := Access_Definition
6494 (Related_Nod => P,
6495 N => Access_Definition (Component_Def));
6496 Set_Is_Local_Anonymous_Access (Element_Type);
6498 -- Propagate the parent. This field is needed if we have to generate
6499 -- the master_id associated with an anonymous access to task type
6500 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6502 Copy_Parent (To => Element_Type, From => T);
6504 -- Ada 2005 (AI-230): In case of components that are anonymous access
6505 -- types the level of accessibility depends on the enclosing type
6506 -- declaration
6508 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6510 -- Ada 2005 (AI-254)
6512 declare
6513 CD : constant Node_Id :=
6514 Access_To_Subprogram_Definition
6515 (Access_Definition (Component_Def));
6516 begin
6517 if Present (CD) and then Protected_Present (CD) then
6518 Element_Type :=
6519 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6520 end if;
6521 end;
6522 end if;
6524 -- Constrained array case
6526 if No (T) then
6527 -- We might be creating more than one itype with the same Related_Id,
6528 -- e.g. for an array object definition and its initial value. Give
6529 -- them unique suffixes, because GNATprove require distinct types to
6530 -- have different names.
6532 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6533 end if;
6535 if Nkind (Def) = N_Constrained_Array_Definition then
6536 -- Establish Implicit_Base as unconstrained base type
6538 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6540 Set_Etype (Implicit_Base, Implicit_Base);
6541 Set_Scope (Implicit_Base, Current_Scope);
6542 Set_Has_Delayed_Freeze (Implicit_Base);
6543 Set_Default_SSO (Implicit_Base);
6545 -- The constrained array type is a subtype of the unconstrained one
6547 Mutate_Ekind (T, E_Array_Subtype);
6548 Reinit_Size_Align (T);
6549 Set_Etype (T, Implicit_Base);
6550 Set_Scope (T, Current_Scope);
6551 Set_Is_Constrained (T);
6552 Set_First_Index (T,
6553 First (Discrete_Subtype_Definitions (Def)));
6554 Set_Has_Delayed_Freeze (T);
6556 -- Complete setup of implicit base type
6558 pragma Assert (not Known_Component_Size (Implicit_Base));
6559 Set_Component_Type (Implicit_Base, Element_Type);
6560 Set_Finalize_Storage_Only
6561 (Implicit_Base,
6562 Finalize_Storage_Only (Element_Type));
6563 Set_First_Index (Implicit_Base, First_Index (T));
6564 Set_Has_Controlled_Component
6565 (Implicit_Base,
6566 Has_Controlled_Component (Element_Type)
6567 or else Is_Controlled (Element_Type));
6568 Set_Packed_Array_Impl_Type
6569 (Implicit_Base, Empty);
6571 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6573 -- Unconstrained array case
6575 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6576 Mutate_Ekind (T, E_Array_Type);
6577 Reinit_Size_Align (T);
6578 Set_Etype (T, T);
6579 Set_Scope (T, Current_Scope);
6580 pragma Assert (not Known_Component_Size (T));
6581 Set_Is_Constrained (T, False);
6582 Set_Is_Fixed_Lower_Bound_Array_Subtype
6583 (T, Has_FLB_Index);
6584 Set_First_Index (T, First (Subtype_Marks (Def)));
6585 Set_Has_Delayed_Freeze (T, True);
6586 Propagate_Concurrent_Flags (T, Element_Type);
6587 Set_Has_Controlled_Component (T, Has_Controlled_Component
6588 (Element_Type)
6589 or else
6590 Is_Controlled (Element_Type));
6591 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6592 (Element_Type));
6593 Set_Default_SSO (T);
6594 end if;
6596 -- Common attributes for both cases
6598 Set_Component_Type (Base_Type (T), Element_Type);
6599 Set_Packed_Array_Impl_Type (T, Empty);
6601 if Aliased_Present (Component_Definition (Def)) then
6602 Set_Has_Aliased_Components (Etype (T));
6604 -- AI12-001: All aliased objects are considered to be specified as
6605 -- independently addressable (RM C.6(8.1/4)).
6607 Set_Has_Independent_Components (Etype (T));
6608 end if;
6610 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6611 -- array type to ensure that objects of this type are initialized.
6613 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6614 Set_Can_Never_Be_Null (T);
6616 if Null_Exclusion_Present (Component_Definition (Def))
6618 -- No need to check itypes because in their case this check was
6619 -- done at their point of creation
6621 and then not Is_Itype (Element_Type)
6622 then
6623 Error_Msg_N
6624 ("`NOT NULL` not allowed (null already excluded)",
6625 Subtype_Indication (Component_Definition (Def)));
6626 end if;
6627 end if;
6629 Priv := Private_Component (Element_Type);
6631 if Present (Priv) then
6633 -- Check for circular definitions
6635 if Priv = Any_Type then
6636 Set_Component_Type (Etype (T), Any_Type);
6638 -- There is a gap in the visibility of operations on the composite
6639 -- type only if the component type is defined in a different scope.
6641 elsif Scope (Priv) = Current_Scope then
6642 null;
6644 elsif Is_Limited_Type (Priv) then
6645 Set_Is_Limited_Composite (Etype (T));
6646 Set_Is_Limited_Composite (T);
6647 else
6648 Set_Is_Private_Composite (Etype (T));
6649 Set_Is_Private_Composite (T);
6650 end if;
6651 end if;
6653 -- A syntax error in the declaration itself may lead to an empty index
6654 -- list, in which case do a minimal patch.
6656 if No (First_Index (T)) then
6657 Error_Msg_N ("missing index definition in array type declaration", T);
6659 declare
6660 Indexes : constant List_Id :=
6661 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6662 begin
6663 Set_Discrete_Subtype_Definitions (Def, Indexes);
6664 Set_First_Index (T, First (Indexes));
6665 return;
6666 end;
6667 end if;
6669 -- Create a concatenation operator for the new type. Internal array
6670 -- types created for packed entities do not need such, they are
6671 -- compatible with the user-defined type.
6673 if Number_Dimensions (T) = 1
6674 and then not Is_Packed_Array_Impl_Type (T)
6675 then
6676 New_Concatenation_Op (T);
6677 end if;
6679 -- In the case of an unconstrained array the parser has already verified
6680 -- that all the indexes are unconstrained but we still need to make sure
6681 -- that the element type is constrained.
6683 if not Is_Definite_Subtype (Element_Type) then
6684 Error_Msg_N
6685 ("unconstrained element type in array declaration",
6686 Subtype_Indication (Component_Def));
6688 elsif Is_Abstract_Type (Element_Type) then
6689 Error_Msg_N
6690 ("the type of a component cannot be abstract",
6691 Subtype_Indication (Component_Def));
6692 end if;
6694 -- There may be an invariant declared for the component type, but
6695 -- the construction of the component invariant checking procedure
6696 -- takes place during expansion.
6697 end Array_Type_Declaration;
6699 ------------------------------------------------------
6700 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6701 ------------------------------------------------------
6703 function Replace_Anonymous_Access_To_Protected_Subprogram
6704 (N : Node_Id) return Entity_Id
6706 Loc : constant Source_Ptr := Sloc (N);
6708 Curr_Scope : constant Scope_Stack_Entry :=
6709 Scope_Stack.Table (Scope_Stack.Last);
6711 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6713 Acc : Node_Id;
6714 -- Access definition in declaration
6716 Comp : Node_Id;
6717 -- Object definition or formal definition with an access definition
6719 Decl : Node_Id;
6720 -- Declaration of anonymous access to subprogram type
6722 Spec : Node_Id;
6723 -- Original specification in access to subprogram
6725 P : Node_Id;
6727 begin
6728 Set_Is_Internal (Anon);
6730 case Nkind (N) is
6731 when N_Constrained_Array_Definition
6732 | N_Component_Declaration
6733 | N_Unconstrained_Array_Definition
6735 Comp := Component_Definition (N);
6736 Acc := Access_Definition (Comp);
6738 when N_Discriminant_Specification =>
6739 Comp := Discriminant_Type (N);
6740 Acc := Comp;
6742 when N_Parameter_Specification =>
6743 Comp := Parameter_Type (N);
6744 Acc := Comp;
6746 when N_Access_Function_Definition =>
6747 Comp := Result_Definition (N);
6748 Acc := Comp;
6750 when N_Object_Declaration =>
6751 Comp := Object_Definition (N);
6752 Acc := Comp;
6754 when N_Function_Specification =>
6755 Comp := Result_Definition (N);
6756 Acc := Comp;
6758 when others =>
6759 raise Program_Error;
6760 end case;
6762 Spec := Access_To_Subprogram_Definition (Acc);
6764 Decl :=
6765 Make_Full_Type_Declaration (Loc,
6766 Defining_Identifier => Anon,
6767 Type_Definition => Copy_Separate_Tree (Spec));
6769 Mark_Rewrite_Insertion (Decl);
6771 -- Insert the new declaration in the nearest enclosing scope. If the
6772 -- parent is a body and N is its return type, the declaration belongs
6773 -- in the enclosing scope. Likewise if N is the type of a parameter.
6775 P := Parent (N);
6777 if Nkind (N) = N_Function_Specification
6778 and then Nkind (P) = N_Subprogram_Body
6779 then
6780 P := Parent (P);
6781 elsif Nkind (N) = N_Parameter_Specification
6782 and then Nkind (P) in N_Subprogram_Specification
6783 and then Nkind (Parent (P)) = N_Subprogram_Body
6784 then
6785 P := Parent (Parent (P));
6786 end if;
6788 while Present (P) and then not Has_Declarations (P) loop
6789 P := Parent (P);
6790 end loop;
6792 pragma Assert (Present (P));
6794 if Nkind (P) = N_Package_Specification then
6795 Prepend (Decl, Visible_Declarations (P));
6796 else
6797 Prepend (Decl, Declarations (P));
6798 end if;
6800 -- Replace the anonymous type with an occurrence of the new declaration.
6801 -- In all cases the rewritten node does not have the null-exclusion
6802 -- attribute because (if present) it was already inherited by the
6803 -- anonymous entity (Anon). Thus, in case of components we do not
6804 -- inherit this attribute.
6806 if Nkind (N) = N_Parameter_Specification then
6807 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6808 Set_Etype (Defining_Identifier (N), Anon);
6809 Set_Null_Exclusion_Present (N, False);
6811 elsif Nkind (N) = N_Object_Declaration then
6812 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6813 Set_Etype (Defining_Identifier (N), Anon);
6815 elsif Nkind (N) = N_Access_Function_Definition then
6816 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6818 elsif Nkind (N) = N_Function_Specification then
6819 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6820 Set_Etype (Defining_Unit_Name (N), Anon);
6822 else
6823 Rewrite (Comp,
6824 Make_Component_Definition (Loc,
6825 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6826 end if;
6828 Mark_Rewrite_Insertion (Comp);
6830 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6831 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6832 and then not Is_Type (Current_Scope))
6833 then
6835 -- Declaration can be analyzed in the current scope.
6837 Analyze (Decl);
6839 else
6840 -- Temporarily remove the current scope (record or subprogram) from
6841 -- the stack to add the new declarations to the enclosing scope.
6842 -- The anonymous entity is an Itype with the proper attributes.
6844 Scope_Stack.Decrement_Last;
6845 Analyze (Decl);
6846 Set_Is_Itype (Anon);
6847 Set_Associated_Node_For_Itype (Anon, N);
6848 Scope_Stack.Append (Curr_Scope);
6849 end if;
6851 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6852 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6853 return Anon;
6854 end Replace_Anonymous_Access_To_Protected_Subprogram;
6856 -------------------------------------
6857 -- Build_Access_Subprogram_Wrapper --
6858 -------------------------------------
6860 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6861 Loc : constant Source_Ptr := Sloc (Decl);
6862 Id : constant Entity_Id := Defining_Identifier (Decl);
6863 Type_Def : constant Node_Id := Type_Definition (Decl);
6864 Specs : constant List_Id :=
6865 Parameter_Specifications (Type_Def);
6866 Profile : constant List_Id := New_List;
6867 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6869 Contracts : constant List_Id := New_List;
6870 Form_P : Node_Id;
6871 New_P : Node_Id;
6872 New_Decl : Node_Id;
6873 Spec : Node_Id;
6875 procedure Replace_Type_Name (Expr : Node_Id);
6876 -- In the expressions for contract aspects, replace occurrences of the
6877 -- access type with the name of the subprogram entity, as needed, e.g.
6878 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6879 -- remain on the original access type declaration. What about expanded
6880 -- names denoting formals, whose prefix in source is the type name ???
6882 -----------------------
6883 -- Replace_Type_Name --
6884 -----------------------
6886 procedure Replace_Type_Name (Expr : Node_Id) is
6887 function Process (N : Node_Id) return Traverse_Result;
6888 function Process (N : Node_Id) return Traverse_Result is
6889 begin
6890 if Nkind (N) = N_Attribute_Reference
6891 and then Is_Entity_Name (Prefix (N))
6892 and then Chars (Prefix (N)) = Chars (Id)
6893 then
6894 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6895 end if;
6897 return OK;
6898 end Process;
6900 procedure Traverse is new Traverse_Proc (Process);
6901 begin
6902 Traverse (Expr);
6903 end Replace_Type_Name;
6905 begin
6906 if Ekind (Id) in E_Access_Subprogram_Type
6907 | E_Access_Protected_Subprogram_Type
6908 | E_Anonymous_Access_Protected_Subprogram_Type
6909 | E_Anonymous_Access_Subprogram_Type
6910 then
6911 null;
6913 else
6914 Error_Msg_N
6915 ("illegal pre/postcondition on access type", Decl);
6916 return;
6917 end if;
6919 declare
6920 Asp : Node_Id;
6921 A_Id : Aspect_Id;
6923 begin
6924 Asp := First (Aspect_Specifications (Decl));
6925 while Present (Asp) loop
6926 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6927 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6928 Append (New_Copy_Tree (Asp), Contracts);
6929 Replace_Type_Name (Expression (Last (Contracts)));
6930 end if;
6931 Next (Asp);
6932 end loop;
6933 end;
6935 -- If there are no contract aspects, no need for a wrapper.
6937 if Is_Empty_List (Contracts) then
6938 return;
6939 end if;
6941 Form_P := First (Specs);
6943 while Present (Form_P) loop
6944 New_P := New_Copy_Tree (Form_P);
6945 Set_Defining_Identifier (New_P,
6946 Make_Defining_Identifier
6947 (Loc, Chars (Defining_Identifier (Form_P))));
6948 Append (New_P, Profile);
6949 Next (Form_P);
6950 end loop;
6952 -- Add to parameter specifications the access parameter that is passed
6953 -- in from an indirect call.
6955 Append (
6956 Make_Parameter_Specification (Loc,
6957 Defining_Identifier => Make_Temporary (Loc, 'P'),
6958 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6959 Profile);
6961 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6962 Spec :=
6963 Make_Procedure_Specification (Loc,
6964 Defining_Unit_Name => Subp,
6965 Parameter_Specifications => Profile);
6966 Mutate_Ekind (Subp, E_Procedure);
6967 else
6968 Spec :=
6969 Make_Function_Specification (Loc,
6970 Defining_Unit_Name => Subp,
6971 Parameter_Specifications => Profile,
6972 Result_Definition =>
6973 New_Copy_Tree
6974 (Result_Definition (Type_Definition (Decl))));
6975 Mutate_Ekind (Subp, E_Function);
6976 end if;
6978 New_Decl :=
6979 Make_Subprogram_Declaration (Loc, Specification => Spec);
6980 Set_Aspect_Specifications (New_Decl, Contracts);
6981 Set_Is_Wrapper (Subp);
6983 -- The wrapper is declared in the freezing actions to facilitate its
6984 -- identification and thus avoid handling it as a primitive operation
6985 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
6986 -- may be handled as a dispatching operation and erroneously registered
6987 -- in a dispatch table.
6989 Append_Freeze_Action (Id, New_Decl);
6991 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6992 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6993 end Build_Access_Subprogram_Wrapper;
6995 -------------------------------
6996 -- Build_Derived_Access_Type --
6997 -------------------------------
6999 procedure Build_Derived_Access_Type
7000 (N : Node_Id;
7001 Parent_Type : Entity_Id;
7002 Derived_Type : Entity_Id)
7004 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
7006 Desig_Type : Entity_Id;
7007 Discr : Entity_Id;
7008 Discr_Con_Elist : Elist_Id;
7009 Discr_Con_El : Elmt_Id;
7010 Subt : Entity_Id;
7012 begin
7013 -- Set the designated type so it is available in case this is an access
7014 -- to a self-referential type, e.g. a standard list type with a next
7015 -- pointer. Will be reset after subtype is built.
7017 Set_Directly_Designated_Type
7018 (Derived_Type, Designated_Type (Parent_Type));
7020 Subt := Process_Subtype (S, N);
7022 if Nkind (S) /= N_Subtype_Indication
7023 and then Subt /= Base_Type (Subt)
7024 then
7025 Mutate_Ekind (Derived_Type, E_Access_Subtype);
7026 end if;
7028 if Ekind (Derived_Type) = E_Access_Subtype then
7029 declare
7030 Pbase : constant Entity_Id := Base_Type (Parent_Type);
7031 Ibase : constant Entity_Id :=
7032 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
7033 Svg_Chars : constant Name_Id := Chars (Ibase);
7034 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
7035 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
7037 begin
7038 Copy_Node (Pbase, Ibase);
7040 -- Restore Itype status after Copy_Node
7042 Set_Is_Itype (Ibase);
7043 Set_Associated_Node_For_Itype (Ibase, N);
7045 Set_Chars (Ibase, Svg_Chars);
7046 Set_Prev_Entity (Ibase, Svg_Prev_E);
7047 Set_Next_Entity (Ibase, Svg_Next_E);
7048 Set_Sloc (Ibase, Sloc (Derived_Type));
7049 Set_Scope (Ibase, Scope (Derived_Type));
7050 Set_Freeze_Node (Ibase, Empty);
7051 Set_Is_Frozen (Ibase, False);
7052 Set_Comes_From_Source (Ibase, False);
7053 Set_Is_First_Subtype (Ibase, False);
7055 Set_Etype (Ibase, Pbase);
7056 Set_Etype (Derived_Type, Ibase);
7057 end;
7058 end if;
7060 Set_Directly_Designated_Type
7061 (Derived_Type, Designated_Type (Subt));
7063 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7064 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7065 Set_Size_Info (Derived_Type, Parent_Type);
7066 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7067 Set_Depends_On_Private (Derived_Type,
7068 Has_Private_Component (Derived_Type));
7069 Conditional_Delay (Derived_Type, Subt);
7071 if Is_Access_Subprogram_Type (Derived_Type)
7072 and then Is_Base_Type (Derived_Type)
7073 then
7074 Set_Can_Use_Internal_Rep
7075 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7076 end if;
7078 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7079 -- that it is not redundant.
7081 if Null_Exclusion_Present (Type_Definition (N)) then
7082 Set_Can_Never_Be_Null (Derived_Type);
7084 elsif Can_Never_Be_Null (Parent_Type) then
7085 Set_Can_Never_Be_Null (Derived_Type);
7086 end if;
7088 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7089 -- the root type for this information.
7091 -- Apply range checks to discriminants for derived record case
7092 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7094 Desig_Type := Designated_Type (Derived_Type);
7096 if Is_Composite_Type (Desig_Type)
7097 and then not Is_Array_Type (Desig_Type)
7098 and then Has_Discriminants (Desig_Type)
7099 and then Base_Type (Desig_Type) /= Desig_Type
7100 then
7101 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7102 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7104 Discr := First_Discriminant (Base_Type (Desig_Type));
7105 while Present (Discr_Con_El) loop
7106 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7107 Next_Elmt (Discr_Con_El);
7108 Next_Discriminant (Discr);
7109 end loop;
7110 end if;
7111 end Build_Derived_Access_Type;
7113 ------------------------------
7114 -- Build_Derived_Array_Type --
7115 ------------------------------
7117 procedure Build_Derived_Array_Type
7118 (N : Node_Id;
7119 Parent_Type : Entity_Id;
7120 Derived_Type : Entity_Id)
7122 Loc : constant Source_Ptr := Sloc (N);
7123 Tdef : constant Node_Id := Type_Definition (N);
7124 Indic : constant Node_Id := Subtype_Indication (Tdef);
7125 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7126 Implicit_Base : Entity_Id := Empty;
7127 New_Indic : Node_Id;
7129 procedure Make_Implicit_Base;
7130 -- If the parent subtype is constrained, the derived type is a subtype
7131 -- of an implicit base type derived from the parent base.
7133 ------------------------
7134 -- Make_Implicit_Base --
7135 ------------------------
7137 procedure Make_Implicit_Base is
7138 begin
7139 Implicit_Base :=
7140 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7142 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7143 Set_Etype (Implicit_Base, Parent_Base);
7145 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7146 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7148 Set_Has_Delayed_Freeze (Implicit_Base, True);
7149 end Make_Implicit_Base;
7151 -- Start of processing for Build_Derived_Array_Type
7153 begin
7154 if not Is_Constrained (Parent_Type) then
7155 if Nkind (Indic) /= N_Subtype_Indication then
7156 Mutate_Ekind (Derived_Type, E_Array_Type);
7158 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7159 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7161 Set_Has_Delayed_Freeze (Derived_Type, True);
7163 else
7164 Make_Implicit_Base;
7165 Set_Etype (Derived_Type, Implicit_Base);
7167 New_Indic :=
7168 Make_Subtype_Declaration (Loc,
7169 Defining_Identifier => Derived_Type,
7170 Subtype_Indication =>
7171 Make_Subtype_Indication (Loc,
7172 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7173 Constraint => Constraint (Indic)));
7175 Rewrite (N, New_Indic);
7177 -- Keep the aspects from the original node
7179 Move_Aspects (Original_Node (N), N);
7181 Analyze (N);
7182 end if;
7184 else
7185 if Nkind (Indic) /= N_Subtype_Indication then
7186 Make_Implicit_Base;
7188 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7189 Set_Etype (Derived_Type, Implicit_Base);
7190 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7192 else
7193 Error_Msg_N ("illegal constraint on constrained type", Indic);
7194 end if;
7195 end if;
7197 -- If parent type is not a derived type itself, and is declared in
7198 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7199 -- the new type's concatenation operator since Derive_Subprograms
7200 -- will not inherit the parent's operator. If the parent type is
7201 -- unconstrained, the operator is of the unconstrained base type.
7203 if Number_Dimensions (Parent_Type) = 1
7204 and then not Is_Limited_Type (Parent_Type)
7205 and then not Is_Derived_Type (Parent_Type)
7206 and then not Is_Package_Or_Generic_Package
7207 (Scope (Base_Type (Parent_Type)))
7208 then
7209 if not Is_Constrained (Parent_Type)
7210 and then Is_Constrained (Derived_Type)
7211 then
7212 New_Concatenation_Op (Implicit_Base);
7213 else
7214 New_Concatenation_Op (Derived_Type);
7215 end if;
7216 end if;
7217 end Build_Derived_Array_Type;
7219 -----------------------------------
7220 -- Build_Derived_Concurrent_Type --
7221 -----------------------------------
7223 procedure Build_Derived_Concurrent_Type
7224 (N : Node_Id;
7225 Parent_Type : Entity_Id;
7226 Derived_Type : Entity_Id)
7228 Loc : constant Source_Ptr := Sloc (N);
7229 Def : constant Node_Id := Type_Definition (N);
7230 Indic : constant Node_Id := Subtype_Indication (Def);
7232 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7233 Corr_Decl : Node_Id := Empty;
7234 Corr_Decl_Needed : Boolean;
7235 -- If the derived type has fewer discriminants than its parent, the
7236 -- corresponding record is also a derived type, in order to account for
7237 -- the bound discriminants. We create a full type declaration for it in
7238 -- this case.
7240 Constraint_Present : constant Boolean :=
7241 Nkind (Indic) = N_Subtype_Indication;
7243 D_Constraint : Node_Id;
7244 New_Constraint : Elist_Id := No_Elist;
7245 Old_Disc : Entity_Id;
7246 New_Disc : Entity_Id;
7247 New_N : Node_Id;
7249 begin
7250 Set_Stored_Constraint (Derived_Type, No_Elist);
7251 Corr_Decl_Needed := False;
7252 Old_Disc := Empty;
7254 if Present (Discriminant_Specifications (N))
7255 and then Constraint_Present
7256 then
7257 Old_Disc := First_Discriminant (Parent_Type);
7258 New_Disc := First (Discriminant_Specifications (N));
7259 while Present (New_Disc) and then Present (Old_Disc) loop
7260 Next_Discriminant (Old_Disc);
7261 Next (New_Disc);
7262 end loop;
7263 end if;
7265 if Present (Old_Disc) and then Expander_Active then
7267 -- The new type has fewer discriminants, so we need to create a new
7268 -- corresponding record, which is derived from the corresponding
7269 -- record of the parent, and has a stored constraint that captures
7270 -- the values of the discriminant constraints. The corresponding
7271 -- record is needed only if expander is active and code generation is
7272 -- enabled.
7274 -- The type declaration for the derived corresponding record has the
7275 -- same discriminant part and constraints as the current declaration.
7276 -- Copy the unanalyzed tree to build declaration.
7278 Corr_Decl_Needed := True;
7279 New_N := Copy_Separate_Tree (N);
7281 Corr_Decl :=
7282 Make_Full_Type_Declaration (Loc,
7283 Defining_Identifier => Corr_Record,
7284 Discriminant_Specifications =>
7285 Discriminant_Specifications (New_N),
7286 Type_Definition =>
7287 Make_Derived_Type_Definition (Loc,
7288 Subtype_Indication =>
7289 Make_Subtype_Indication (Loc,
7290 Subtype_Mark =>
7291 New_Occurrence_Of
7292 (Corresponding_Record_Type (Parent_Type), Loc),
7293 Constraint =>
7294 Constraint
7295 (Subtype_Indication (Type_Definition (New_N))))));
7296 end if;
7298 -- Copy Storage_Size and Relative_Deadline variables if task case
7300 if Is_Task_Type (Parent_Type) then
7301 Set_Storage_Size_Variable (Derived_Type,
7302 Storage_Size_Variable (Parent_Type));
7303 Set_Relative_Deadline_Variable (Derived_Type,
7304 Relative_Deadline_Variable (Parent_Type));
7305 end if;
7307 if Present (Discriminant_Specifications (N)) then
7308 Push_Scope (Derived_Type);
7309 Check_Or_Process_Discriminants (N, Derived_Type);
7311 if Constraint_Present then
7312 New_Constraint :=
7313 Expand_To_Stored_Constraint
7314 (Parent_Type,
7315 Build_Discriminant_Constraints
7316 (Parent_Type, Indic, True));
7317 end if;
7319 End_Scope;
7321 elsif Constraint_Present then
7323 -- Build an unconstrained derived type and rewrite the derived type
7324 -- as a subtype of this new base type.
7326 declare
7327 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7328 New_Base : Entity_Id;
7329 New_Decl : Node_Id;
7330 New_Indic : Node_Id;
7332 begin
7333 New_Base :=
7334 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7336 New_Decl :=
7337 Make_Full_Type_Declaration (Loc,
7338 Defining_Identifier => New_Base,
7339 Type_Definition =>
7340 Make_Derived_Type_Definition (Loc,
7341 Abstract_Present => Abstract_Present (Def),
7342 Limited_Present => Limited_Present (Def),
7343 Subtype_Indication =>
7344 New_Occurrence_Of (Parent_Base, Loc)));
7346 Mark_Rewrite_Insertion (New_Decl);
7347 Insert_Before (N, New_Decl);
7348 Analyze (New_Decl);
7350 New_Indic :=
7351 Make_Subtype_Indication (Loc,
7352 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7353 Constraint => Relocate_Node (Constraint (Indic)));
7355 Rewrite (N,
7356 Make_Subtype_Declaration (Loc,
7357 Defining_Identifier => Derived_Type,
7358 Subtype_Indication => New_Indic));
7360 -- Keep the aspects from the original node
7362 Move_Aspects (Original_Node (N), N);
7364 Analyze (N);
7365 return;
7366 end;
7367 end if;
7369 -- By default, operations and private data are inherited from parent.
7370 -- However, in the presence of bound discriminants, a new corresponding
7371 -- record will be created, see below.
7373 Set_Has_Discriminants
7374 (Derived_Type, Has_Discriminants (Parent_Type));
7375 Set_Corresponding_Record_Type
7376 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7378 -- Is_Constrained is set according the parent subtype, but is set to
7379 -- False if the derived type is declared with new discriminants.
7381 Set_Is_Constrained
7382 (Derived_Type,
7383 (Is_Constrained (Parent_Type) or else Constraint_Present)
7384 and then No (Discriminant_Specifications (N)));
7386 if Constraint_Present then
7387 if not Has_Discriminants (Parent_Type) then
7388 Error_Msg_N ("untagged parent must have discriminants", N);
7390 elsif Present (Discriminant_Specifications (N)) then
7392 -- Verify that new discriminants are used to constrain old ones
7394 D_Constraint := First (Constraints (Constraint (Indic)));
7396 Old_Disc := First_Discriminant (Parent_Type);
7398 while Present (D_Constraint) loop
7399 if Nkind (D_Constraint) /= N_Discriminant_Association then
7401 -- Positional constraint. If it is a reference to a new
7402 -- discriminant, it constrains the corresponding old one.
7404 if Nkind (D_Constraint) = N_Identifier then
7405 New_Disc := First_Discriminant (Derived_Type);
7406 while Present (New_Disc) loop
7407 exit when Chars (New_Disc) = Chars (D_Constraint);
7408 Next_Discriminant (New_Disc);
7409 end loop;
7411 if Present (New_Disc) then
7412 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7413 end if;
7414 end if;
7416 Next_Discriminant (Old_Disc);
7418 -- if this is a named constraint, search by name for the old
7419 -- discriminants constrained by the new one.
7421 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7423 -- Find new discriminant with that name
7425 New_Disc := First_Discriminant (Derived_Type);
7426 while Present (New_Disc) loop
7427 exit when
7428 Chars (New_Disc) = Chars (Expression (D_Constraint));
7429 Next_Discriminant (New_Disc);
7430 end loop;
7432 if Present (New_Disc) then
7434 -- Verify that new discriminant renames some discriminant
7435 -- of the parent type, and associate the new discriminant
7436 -- with one or more old ones that it renames.
7438 declare
7439 Selector : Node_Id;
7441 begin
7442 Selector := First (Selector_Names (D_Constraint));
7443 while Present (Selector) loop
7444 Old_Disc := First_Discriminant (Parent_Type);
7445 while Present (Old_Disc) loop
7446 exit when Chars (Old_Disc) = Chars (Selector);
7447 Next_Discriminant (Old_Disc);
7448 end loop;
7450 if Present (Old_Disc) then
7451 Set_Corresponding_Discriminant
7452 (New_Disc, Old_Disc);
7453 end if;
7455 Next (Selector);
7456 end loop;
7457 end;
7458 end if;
7459 end if;
7461 Next (D_Constraint);
7462 end loop;
7464 New_Disc := First_Discriminant (Derived_Type);
7465 while Present (New_Disc) loop
7466 if No (Corresponding_Discriminant (New_Disc)) then
7467 Error_Msg_NE
7468 ("new discriminant& must constrain old one", N, New_Disc);
7470 -- If a new discriminant is used in the constraint, then its
7471 -- subtype must be statically compatible with the subtype of
7472 -- the parent discriminant (RM 3.7(15)).
7474 else
7475 Check_Constraining_Discriminant
7476 (New_Disc, Corresponding_Discriminant (New_Disc));
7477 end if;
7479 Next_Discriminant (New_Disc);
7480 end loop;
7481 end if;
7483 elsif Present (Discriminant_Specifications (N)) then
7484 Error_Msg_N
7485 ("missing discriminant constraint in untagged derivation", N);
7486 end if;
7488 -- The entity chain of the derived type includes the new discriminants
7489 -- but shares operations with the parent.
7491 if Present (Discriminant_Specifications (N)) then
7492 Old_Disc := First_Discriminant (Parent_Type);
7493 while Present (Old_Disc) loop
7494 if No (Next_Entity (Old_Disc))
7495 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7496 then
7497 Link_Entities
7498 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7499 exit;
7500 end if;
7502 Next_Discriminant (Old_Disc);
7503 end loop;
7505 else
7506 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7507 if Has_Discriminants (Parent_Type) then
7508 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7509 Set_Discriminant_Constraint (
7510 Derived_Type, Discriminant_Constraint (Parent_Type));
7511 end if;
7512 end if;
7514 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7516 Set_Has_Completion (Derived_Type);
7518 if Corr_Decl_Needed then
7519 Set_Stored_Constraint (Derived_Type, New_Constraint);
7520 Insert_After (N, Corr_Decl);
7521 Analyze (Corr_Decl);
7522 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7523 end if;
7524 end Build_Derived_Concurrent_Type;
7526 ------------------------------------
7527 -- Build_Derived_Enumeration_Type --
7528 ------------------------------------
7530 procedure Build_Derived_Enumeration_Type
7531 (N : Node_Id;
7532 Parent_Type : Entity_Id;
7533 Derived_Type : Entity_Id)
7535 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7536 -- When the type declaration includes a constraint, we generate
7537 -- a subtype declaration of an anonymous base type, with the constraint
7538 -- given in the original type declaration. Conceptually, the bounds
7539 -- are converted to the new base type, and this conversion freezes
7540 -- (prematurely) that base type, when the bounds are simply literals.
7541 -- As a result, a representation clause for the derived type is then
7542 -- rejected or ignored. This procedure recognizes the simple case of
7543 -- literal bounds, which allows us to indicate that the conversions
7544 -- are not freeze points, and the subsequent representation clause
7545 -- can be accepted.
7546 -- A similar approach might be used to resolve the long-standing
7547 -- problem of premature freezing of derived numeric types ???
7549 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7550 begin
7551 return Nkind (B) = N_Type_Conversion
7552 and then Is_Entity_Name (Expression (B))
7553 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7554 end Bound_Belongs_To_Type;
7556 Loc : constant Source_Ptr := Sloc (N);
7557 Def : constant Node_Id := Type_Definition (N);
7558 Indic : constant Node_Id := Subtype_Indication (Def);
7559 Implicit_Base : Entity_Id;
7560 Literal : Entity_Id;
7561 New_Lit : Entity_Id;
7562 Literals_List : List_Id;
7563 Type_Decl : Node_Id;
7564 Hi, Lo : Node_Id;
7565 Rang_Expr : Node_Id;
7567 begin
7568 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7569 -- not have explicit literals lists we need to process types derived
7570 -- from them specially. This is handled by Derived_Standard_Character.
7571 -- If the parent type is a generic type, there are no literals either,
7572 -- and we construct the same skeletal representation as for the generic
7573 -- parent type.
7575 if Is_Standard_Character_Type (Parent_Type) then
7576 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7578 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7579 declare
7580 Lo : Node_Id;
7581 Hi : Node_Id;
7583 begin
7584 if Nkind (Indic) /= N_Subtype_Indication then
7585 Lo :=
7586 Make_Attribute_Reference (Loc,
7587 Attribute_Name => Name_First,
7588 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7589 Set_Etype (Lo, Derived_Type);
7591 Hi :=
7592 Make_Attribute_Reference (Loc,
7593 Attribute_Name => Name_Last,
7594 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7595 Set_Etype (Hi, Derived_Type);
7597 Set_Scalar_Range (Derived_Type,
7598 Make_Range (Loc,
7599 Low_Bound => Lo,
7600 High_Bound => Hi));
7601 else
7603 -- Analyze subtype indication and verify compatibility
7604 -- with parent type.
7606 if Base_Type (Process_Subtype (Indic, N)) /=
7607 Base_Type (Parent_Type)
7608 then
7609 Error_Msg_N
7610 ("illegal constraint for formal discrete type", N);
7611 end if;
7612 end if;
7613 end;
7615 else
7616 -- If a constraint is present, analyze the bounds to catch
7617 -- premature usage of the derived literals.
7619 if Nkind (Indic) = N_Subtype_Indication
7620 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7621 then
7622 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7623 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7624 end if;
7626 -- Create an implicit base type for the derived type even if there
7627 -- is no constraint attached to it, since this seems closer to the
7628 -- Ada semantics. Use an Itype like for the implicit base type of
7629 -- other kinds of derived type, but build a full type declaration
7630 -- for it so as to analyze the new literals properly. Then build a
7631 -- subtype declaration tree which applies the constraint (if any)
7632 -- and have it replace the derived type declaration.
7634 Literal := First_Literal (Parent_Type);
7635 Literals_List := New_List;
7636 while Present (Literal)
7637 and then Ekind (Literal) = E_Enumeration_Literal
7638 loop
7639 -- Literals of the derived type have the same representation as
7640 -- those of the parent type, but this representation can be
7641 -- overridden by an explicit representation clause. Indicate
7642 -- that there is no explicit representation given yet. These
7643 -- derived literals are implicit operations of the new type,
7644 -- and can be overridden by explicit ones.
7646 if Nkind (Literal) = N_Defining_Character_Literal then
7647 New_Lit :=
7648 Make_Defining_Character_Literal (Loc, Chars (Literal));
7649 else
7650 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7651 end if;
7653 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7654 Set_Is_Not_Self_Hidden (New_Lit);
7655 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7656 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7657 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7658 Set_Alias (New_Lit, Literal);
7659 Set_Is_Known_Valid (New_Lit, True);
7661 Append (New_Lit, Literals_List);
7662 Next_Literal (Literal);
7663 end loop;
7665 Implicit_Base :=
7666 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7668 -- Indicate the proper nature of the derived type. This must be done
7669 -- before analysis of the literals, to recognize cases when a literal
7670 -- may be hidden by a previous explicit function definition (cf.
7671 -- c83031a).
7673 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7674 Set_Etype (Derived_Type, Implicit_Base);
7676 Type_Decl :=
7677 Make_Full_Type_Declaration (Loc,
7678 Defining_Identifier => Implicit_Base,
7679 Type_Definition =>
7680 Make_Enumeration_Type_Definition (Loc, Literals_List));
7682 -- Do not insert the declarationn, just analyze it in the context
7684 Set_Parent (Type_Decl, Parent (N));
7685 Analyze (Type_Decl);
7687 -- The anonymous base now has a full declaration, but this base
7688 -- is not a first subtype.
7690 Set_Is_First_Subtype (Implicit_Base, False);
7692 -- After the implicit base is analyzed its Etype needs to be changed
7693 -- to reflect the fact that it is derived from the parent type which
7694 -- was ignored during analysis. We also set the size at this point.
7696 Set_Etype (Implicit_Base, Parent_Type);
7698 Set_Size_Info (Implicit_Base, Parent_Type);
7699 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7700 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7702 -- Copy other flags from parent type
7704 Set_Has_Non_Standard_Rep
7705 (Implicit_Base, Has_Non_Standard_Rep
7706 (Parent_Type));
7707 Set_Has_Pragma_Ordered
7708 (Implicit_Base, Has_Pragma_Ordered
7709 (Parent_Type));
7710 Set_Has_Delayed_Freeze (Implicit_Base);
7712 -- Process the subtype indication including a validation check on the
7713 -- constraint, if any. If a constraint is given, its bounds must be
7714 -- implicitly converted to the new type.
7716 if Nkind (Indic) = N_Subtype_Indication then
7717 declare
7718 R : constant Node_Id :=
7719 Range_Expression (Constraint (Indic));
7721 begin
7722 if Nkind (R) = N_Range then
7723 Hi := Build_Scalar_Bound
7724 (High_Bound (R), Parent_Type, Implicit_Base);
7725 Lo := Build_Scalar_Bound
7726 (Low_Bound (R), Parent_Type, Implicit_Base);
7728 else
7729 -- Constraint is a Range attribute. Replace with explicit
7730 -- mention of the bounds of the prefix, which must be a
7731 -- subtype.
7733 Analyze (Prefix (R));
7734 Hi :=
7735 Convert_To (Implicit_Base,
7736 Make_Attribute_Reference (Loc,
7737 Attribute_Name => Name_Last,
7738 Prefix =>
7739 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7741 Lo :=
7742 Convert_To (Implicit_Base,
7743 Make_Attribute_Reference (Loc,
7744 Attribute_Name => Name_First,
7745 Prefix =>
7746 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7747 end if;
7748 end;
7750 else
7751 Hi :=
7752 Build_Scalar_Bound
7753 (Type_High_Bound (Parent_Type),
7754 Parent_Type, Implicit_Base);
7755 Lo :=
7756 Build_Scalar_Bound
7757 (Type_Low_Bound (Parent_Type),
7758 Parent_Type, Implicit_Base);
7759 end if;
7761 Rang_Expr :=
7762 Make_Range (Loc,
7763 Low_Bound => Lo,
7764 High_Bound => Hi);
7766 -- If we constructed a default range for the case where no range
7767 -- was given, then the expressions in the range must not freeze
7768 -- since they do not correspond to expressions in the source.
7769 -- However, if the type inherits predicates the expressions will
7770 -- be elaborated earlier and must freeze.
7772 if (Nkind (Indic) /= N_Subtype_Indication
7773 or else
7774 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7775 and then not Has_Predicates (Derived_Type)
7776 then
7777 Set_Must_Not_Freeze (Lo);
7778 Set_Must_Not_Freeze (Hi);
7779 Set_Must_Not_Freeze (Rang_Expr);
7780 end if;
7782 Rewrite (N,
7783 Make_Subtype_Declaration (Loc,
7784 Defining_Identifier => Derived_Type,
7785 Subtype_Indication =>
7786 Make_Subtype_Indication (Loc,
7787 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7788 Constraint =>
7789 Make_Range_Constraint (Loc,
7790 Range_Expression => Rang_Expr))));
7792 -- Keep the aspects from the orignal node
7794 Move_Aspects (Original_Node (N), N);
7796 Analyze (N);
7798 -- Propagate the aspects from the original type declaration to the
7799 -- declaration of the implicit base.
7801 Copy_Aspects (From => N, To => Type_Decl);
7803 -- Apply a range check. Since this range expression doesn't have an
7804 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7805 -- this right???
7807 if Nkind (Indic) = N_Subtype_Indication then
7808 Apply_Range_Check
7809 (Range_Expression (Constraint (Indic)), Parent_Type,
7810 Source_Typ => Entity (Subtype_Mark (Indic)));
7811 end if;
7812 end if;
7813 end Build_Derived_Enumeration_Type;
7815 --------------------------------
7816 -- Build_Derived_Numeric_Type --
7817 --------------------------------
7819 procedure Build_Derived_Numeric_Type
7820 (N : Node_Id;
7821 Parent_Type : Entity_Id;
7822 Derived_Type : Entity_Id)
7824 Loc : constant Source_Ptr := Sloc (N);
7825 Tdef : constant Node_Id := Type_Definition (N);
7826 Indic : constant Node_Id := Subtype_Indication (Tdef);
7827 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7828 No_Constraint : constant Boolean := Nkind (Indic) /=
7829 N_Subtype_Indication;
7830 Implicit_Base : Entity_Id;
7832 Lo : Node_Id;
7833 Hi : Node_Id;
7835 begin
7836 -- Process the subtype indication including a validation check on
7837 -- the constraint if any.
7839 Discard_Node (Process_Subtype (Indic, N));
7841 -- Introduce an implicit base type for the derived type even if there
7842 -- is no constraint attached to it, since this seems closer to the Ada
7843 -- semantics.
7845 Implicit_Base :=
7846 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7848 Set_Etype (Implicit_Base, Parent_Base);
7849 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7850 Set_Size_Info (Implicit_Base, Parent_Base);
7851 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7852 Set_Parent (Implicit_Base, Parent (Derived_Type));
7853 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7854 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7856 -- Set RM Size for discrete type or decimal fixed-point type
7857 -- Ordinary fixed-point is excluded, why???
7859 if Is_Discrete_Type (Parent_Base)
7860 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7861 then
7862 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7863 end if;
7865 Set_Has_Delayed_Freeze (Implicit_Base);
7867 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7868 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7870 Set_Scalar_Range (Implicit_Base,
7871 Make_Range (Loc,
7872 Low_Bound => Lo,
7873 High_Bound => Hi));
7875 if Has_Infinities (Parent_Base) then
7876 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7877 end if;
7879 -- The Derived_Type, which is the entity of the declaration, is a
7880 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7881 -- absence of an explicit constraint.
7883 Set_Etype (Derived_Type, Implicit_Base);
7885 -- If we did not have a constraint, then the Ekind is set from the
7886 -- parent type (otherwise Process_Subtype has set the bounds)
7888 if No_Constraint then
7889 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7890 end if;
7892 -- If we did not have a range constraint, then set the range from the
7893 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7895 if No_Constraint or else not Has_Range_Constraint (Indic) then
7896 Set_Scalar_Range (Derived_Type,
7897 Make_Range (Loc,
7898 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7899 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7900 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7902 if Has_Infinities (Parent_Type) then
7903 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7904 end if;
7906 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7907 end if;
7909 Set_Is_Descendant_Of_Address (Derived_Type,
7910 Is_Descendant_Of_Address (Parent_Type));
7911 Set_Is_Descendant_Of_Address (Implicit_Base,
7912 Is_Descendant_Of_Address (Parent_Type));
7914 -- Set remaining type-specific fields, depending on numeric type
7916 if Is_Modular_Integer_Type (Parent_Type) then
7917 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7919 Set_Non_Binary_Modulus
7920 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7922 Set_Is_Known_Valid
7923 (Implicit_Base, Is_Known_Valid (Parent_Base));
7925 elsif Is_Floating_Point_Type (Parent_Type) then
7927 -- Digits of base type is always copied from the digits value of
7928 -- the parent base type, but the digits of the derived type will
7929 -- already have been set if there was a constraint present.
7931 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7932 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7934 if No_Constraint then
7935 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7936 end if;
7938 elsif Is_Fixed_Point_Type (Parent_Type) then
7940 -- Small of base type and derived type are always copied from the
7941 -- parent base type, since smalls never change. The delta of the
7942 -- base type is also copied from the parent base type. However the
7943 -- delta of the derived type will have been set already if a
7944 -- constraint was present.
7946 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7947 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7948 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7950 if No_Constraint then
7951 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7952 end if;
7954 -- The scale and machine radix in the decimal case are always
7955 -- copied from the parent base type.
7957 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7958 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7959 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7961 Set_Machine_Radix_10
7962 (Derived_Type, Machine_Radix_10 (Parent_Base));
7963 Set_Machine_Radix_10
7964 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7966 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7968 if No_Constraint then
7969 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7971 else
7972 -- the analysis of the subtype_indication sets the
7973 -- digits value of the derived type.
7975 null;
7976 end if;
7977 end if;
7978 end if;
7980 if Is_Integer_Type (Parent_Type) then
7981 Set_Has_Shift_Operator
7982 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7983 end if;
7985 -- The type of the bounds is that of the parent type, and they
7986 -- must be converted to the derived type.
7988 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7989 end Build_Derived_Numeric_Type;
7991 --------------------------------
7992 -- Build_Derived_Private_Type --
7993 --------------------------------
7995 procedure Build_Derived_Private_Type
7996 (N : Node_Id;
7997 Parent_Type : Entity_Id;
7998 Derived_Type : Entity_Id;
7999 Is_Completion : Boolean;
8000 Derive_Subps : Boolean := True)
8002 Loc : constant Source_Ptr := Sloc (N);
8003 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
8004 Par_Scope : constant Entity_Id := Scope (Par_Base);
8005 Full_N : constant Node_Id := New_Copy_Tree (N);
8006 Full_Der : Entity_Id := New_Copy (Derived_Type);
8007 Full_P : Entity_Id;
8009 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
8010 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
8011 -- present (they cannot be both present for the same type), or Empty.
8013 procedure Build_Full_Derivation;
8014 -- Build full derivation, i.e. derive from the full view
8016 procedure Copy_And_Build;
8017 -- Copy derived type declaration, replace parent with its full view,
8018 -- and build derivation
8020 -------------------------
8021 -- Available_Full_View --
8022 -------------------------
8024 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
8025 begin
8026 if Present (Full_View (Typ)) then
8027 return Full_View (Typ);
8029 elsif Present (Underlying_Full_View (Typ)) then
8031 -- We should be called on a type with an underlying full view
8032 -- only by means of the recursive call made in Copy_And_Build
8033 -- through the first call to Build_Derived_Type, or else if
8034 -- the parent scope is being analyzed because we are deriving
8035 -- a completion.
8037 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
8039 return Underlying_Full_View (Typ);
8041 else
8042 return Empty;
8043 end if;
8044 end Available_Full_View;
8046 ---------------------------
8047 -- Build_Full_Derivation --
8048 ---------------------------
8050 procedure Build_Full_Derivation is
8051 begin
8052 -- If parent scope is not open, install the declarations
8054 if not In_Open_Scopes (Par_Scope) then
8055 Install_Private_Declarations (Par_Scope);
8056 Install_Visible_Declarations (Par_Scope);
8057 Copy_And_Build;
8058 Uninstall_Declarations (Par_Scope);
8060 -- If parent scope is open and in another unit, and parent has a
8061 -- completion, then the derivation is taking place in the visible
8062 -- part of a child unit. In that case retrieve the full view of
8063 -- the parent momentarily.
8065 elsif not In_Same_Source_Unit (N, Parent_Type)
8066 and then Present (Full_View (Parent_Type))
8067 then
8068 Full_P := Full_View (Parent_Type);
8069 Exchange_Declarations (Parent_Type);
8070 Copy_And_Build;
8071 Exchange_Declarations (Full_P);
8073 -- Otherwise it is a local derivation
8075 else
8076 Copy_And_Build;
8077 end if;
8078 end Build_Full_Derivation;
8080 --------------------
8081 -- Copy_And_Build --
8082 --------------------
8084 procedure Copy_And_Build is
8085 Full_Parent : Entity_Id := Parent_Type;
8087 begin
8088 -- If the parent is itself derived from another private type,
8089 -- installing the private declarations has not affected its
8090 -- privacy status, so use its own full view explicitly.
8092 if Is_Private_Type (Full_Parent)
8093 and then Present (Full_View (Full_Parent))
8094 then
8095 Full_Parent := Full_View (Full_Parent);
8096 end if;
8098 -- If the full view is itself derived from another private type
8099 -- and has got an underlying full view, and this is done for a
8100 -- completion, i.e. to build the underlying full view of the type,
8101 -- then use this underlying full view. We cannot do that if this
8102 -- is not a completion, i.e. to build the full view of the type,
8103 -- because this would break the privacy of the parent type, except
8104 -- if the parent scope is being analyzed because we are deriving a
8105 -- completion.
8107 if Is_Private_Type (Full_Parent)
8108 and then Present (Underlying_Full_View (Full_Parent))
8109 and then (Is_Completion or else In_Private_Part (Par_Scope))
8110 then
8111 Full_Parent := Underlying_Full_View (Full_Parent);
8112 end if;
8114 -- For private, record, concurrent, access and almost all enumeration
8115 -- types, the derivation from the full view requires a fully-fledged
8116 -- declaration. In the other cases, just use an itype.
8118 if Is_Private_Type (Full_Parent)
8119 or else Is_Record_Type (Full_Parent)
8120 or else Is_Concurrent_Type (Full_Parent)
8121 or else Is_Access_Type (Full_Parent)
8122 or else
8123 (Is_Enumeration_Type (Full_Parent)
8124 and then not Is_Standard_Character_Type (Full_Parent)
8125 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8126 then
8127 -- Copy and adjust declaration to provide a completion for what
8128 -- is originally a private declaration. Indicate that full view
8129 -- is internally generated.
8131 Set_Comes_From_Source (Full_N, False);
8132 Set_Comes_From_Source (Full_Der, False);
8133 Set_Parent (Full_Der, Full_N);
8134 Set_Defining_Identifier (Full_N, Full_Der);
8136 -- If there are no constraints, adjust the subtype mark
8138 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8139 N_Subtype_Indication
8140 then
8141 Set_Subtype_Indication
8142 (Type_Definition (Full_N),
8143 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8144 end if;
8146 Insert_After (N, Full_N);
8148 -- Build full view of derived type from full view of parent which
8149 -- is now installed. Subprograms have been derived on the partial
8150 -- view, the completion does not derive them anew.
8152 if Is_Record_Type (Full_Parent) then
8154 -- If parent type is tagged, the completion inherits the proper
8155 -- primitive operations.
8157 if Is_Tagged_Type (Parent_Type) then
8158 Build_Derived_Record_Type
8159 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8160 else
8161 Build_Derived_Record_Type
8162 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8163 end if;
8165 else
8166 -- If the parent type is private, this is not a completion and
8167 -- we build the full derivation recursively as a completion.
8169 Build_Derived_Type
8170 (Full_N, Full_Parent, Full_Der,
8171 Is_Completion => Is_Private_Type (Full_Parent),
8172 Derive_Subps => False);
8173 end if;
8175 -- The full declaration has been introduced into the tree and
8176 -- processed in the step above. It should not be analyzed again
8177 -- (when encountered later in the current list of declarations)
8178 -- to prevent spurious name conflicts. The full entity remains
8179 -- invisible.
8181 Set_Analyzed (Full_N);
8183 else
8184 Full_Der :=
8185 Make_Defining_Identifier (Sloc (Derived_Type),
8186 Chars => Chars (Derived_Type));
8187 Set_Is_Itype (Full_Der);
8188 Set_Associated_Node_For_Itype (Full_Der, N);
8189 Set_Parent (Full_Der, N);
8190 Build_Derived_Type
8191 (N, Full_Parent, Full_Der,
8192 Is_Completion => False, Derive_Subps => False);
8193 Set_Is_Not_Self_Hidden (Full_Der);
8194 end if;
8196 Set_Has_Private_Declaration (Full_Der);
8197 Set_Has_Private_Declaration (Derived_Type);
8199 Set_Scope (Full_Der, Scope (Derived_Type));
8200 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8201 Set_Has_Size_Clause (Full_Der, False);
8202 Set_Has_Alignment_Clause (Full_Der, False);
8203 Set_Has_Delayed_Freeze (Full_Der);
8204 Set_Is_Frozen (Full_Der, False);
8205 Set_Freeze_Node (Full_Der, Empty);
8206 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8207 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8209 -- The convention on the base type may be set in the private part
8210 -- and not propagated to the subtype until later, so we obtain the
8211 -- convention from the base type of the parent.
8213 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8214 end Copy_And_Build;
8216 -- Start of processing for Build_Derived_Private_Type
8218 begin
8219 if Is_Tagged_Type (Parent_Type) then
8220 Full_P := Full_View (Parent_Type);
8222 -- A type extension of a type with unknown discriminants is an
8223 -- indefinite type that the back-end cannot handle directly.
8224 -- We treat it as a private type, and build a completion that is
8225 -- derived from the full view of the parent, and hopefully has
8226 -- known discriminants.
8228 -- If the full view of the parent type has an underlying record view,
8229 -- use it to generate the underlying record view of this derived type
8230 -- (required for chains of derivations with unknown discriminants).
8232 -- Minor optimization: we avoid the generation of useless underlying
8233 -- record view entities if the private type declaration has unknown
8234 -- discriminants but its corresponding full view has no
8235 -- discriminants.
8237 if Has_Unknown_Discriminants (Parent_Type)
8238 and then Present (Full_P)
8239 and then (Has_Discriminants (Full_P)
8240 or else Present (Underlying_Record_View (Full_P)))
8241 and then not In_Open_Scopes (Par_Scope)
8242 and then Expander_Active
8243 then
8244 declare
8245 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8246 New_Ext : constant Node_Id :=
8247 Copy_Separate_Tree
8248 (Record_Extension_Part (Type_Definition (N)));
8249 Decl : Node_Id;
8251 begin
8252 Build_Derived_Record_Type
8253 (N, Parent_Type, Derived_Type, Derive_Subps);
8255 -- Build anonymous completion, as a derivation from the full
8256 -- view of the parent. This is not a completion in the usual
8257 -- sense, because the current type is not private.
8259 Decl :=
8260 Make_Full_Type_Declaration (Loc,
8261 Defining_Identifier => Full_Der,
8262 Type_Definition =>
8263 Make_Derived_Type_Definition (Loc,
8264 Subtype_Indication =>
8265 New_Copy_Tree
8266 (Subtype_Indication (Type_Definition (N))),
8267 Record_Extension_Part => New_Ext));
8269 -- If the parent type has an underlying record view, use it
8270 -- here to build the new underlying record view.
8272 if Present (Underlying_Record_View (Full_P)) then
8273 pragma Assert
8274 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8275 = N_Identifier);
8276 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8277 Underlying_Record_View (Full_P));
8278 end if;
8280 Install_Private_Declarations (Par_Scope);
8281 Install_Visible_Declarations (Par_Scope);
8282 Insert_Before (N, Decl);
8284 -- Mark entity as an underlying record view before analysis,
8285 -- to avoid generating the list of its primitive operations
8286 -- (which is not really required for this entity) and thus
8287 -- prevent spurious errors associated with missing overriding
8288 -- of abstract primitives (overridden only for Derived_Type).
8290 Mutate_Ekind (Full_Der, E_Record_Type);
8291 Set_Is_Underlying_Record_View (Full_Der);
8292 Set_Default_SSO (Full_Der);
8293 Set_No_Reordering (Full_Der, No_Component_Reordering);
8295 Analyze (Decl);
8297 pragma Assert (Has_Discriminants (Full_Der)
8298 and then not Has_Unknown_Discriminants (Full_Der));
8300 Uninstall_Declarations (Par_Scope);
8302 -- Freeze the underlying record view, to prevent generation of
8303 -- useless dispatching information, which is simply shared with
8304 -- the real derived type.
8306 Set_Is_Frozen (Full_Der);
8308 -- If the derived type has access discriminants, create
8309 -- references to their anonymous types now, to prevent
8310 -- back-end problems when their first use is in generated
8311 -- bodies of primitives.
8313 declare
8314 E : Entity_Id;
8316 begin
8317 E := First_Entity (Full_Der);
8319 while Present (E) loop
8320 if Ekind (E) = E_Discriminant
8321 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8322 then
8323 Build_Itype_Reference (Etype (E), Decl);
8324 end if;
8326 Next_Entity (E);
8327 end loop;
8328 end;
8330 -- Set up links between real entity and underlying record view
8332 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8333 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8334 end;
8336 -- If discriminants are known, build derived record
8338 else
8339 Build_Derived_Record_Type
8340 (N, Parent_Type, Derived_Type, Derive_Subps);
8341 end if;
8343 return;
8345 elsif Has_Discriminants (Parent_Type) then
8347 -- Build partial view of derived type from partial view of parent.
8348 -- This must be done before building the full derivation because the
8349 -- second derivation will modify the discriminants of the first and
8350 -- the discriminants are chained with the rest of the components in
8351 -- the full derivation.
8353 Build_Derived_Record_Type
8354 (N, Parent_Type, Derived_Type, Derive_Subps);
8356 -- Build the full derivation if this is not the anonymous derived
8357 -- base type created by Build_Derived_Record_Type in the constrained
8358 -- case (see point 5. of its head comment) since we build it for the
8359 -- derived subtype.
8361 if Present (Available_Full_View (Parent_Type))
8362 and then not Is_Itype (Derived_Type)
8363 then
8364 declare
8365 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8366 Discr : Entity_Id;
8367 Last_Discr : Entity_Id;
8369 begin
8370 -- If this is not a completion, construct the implicit full
8371 -- view by deriving from the full view of the parent type.
8372 -- But if this is a completion, the derived private type
8373 -- being built is a full view and the full derivation can
8374 -- only be its underlying full view.
8376 Build_Full_Derivation;
8378 if not Is_Completion then
8379 Set_Full_View (Derived_Type, Full_Der);
8380 else
8381 Set_Underlying_Full_View (Derived_Type, Full_Der);
8382 Set_Is_Underlying_Full_View (Full_Der);
8383 end if;
8385 if not Is_Base_Type (Derived_Type) then
8386 Set_Full_View (Der_Base, Base_Type (Full_Der));
8387 end if;
8389 -- Copy the discriminant list from full view to the partial
8390 -- view (base type and its subtype). Gigi requires that the
8391 -- partial and full views have the same discriminants.
8393 -- Note that since the partial view points to discriminants
8394 -- in the full view, their scope will be that of the full
8395 -- view. This might cause some front end problems and need
8396 -- adjustment???
8398 Discr := First_Discriminant (Base_Type (Full_Der));
8399 Set_First_Entity (Der_Base, Discr);
8401 loop
8402 Last_Discr := Discr;
8403 Next_Discriminant (Discr);
8404 exit when No (Discr);
8405 end loop;
8407 Set_Last_Entity (Der_Base, Last_Discr);
8408 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8409 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8410 end;
8411 end if;
8413 elsif Present (Available_Full_View (Parent_Type))
8414 and then Has_Discriminants (Available_Full_View (Parent_Type))
8415 then
8416 if Has_Unknown_Discriminants (Parent_Type)
8417 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8418 N_Subtype_Indication
8419 then
8420 Error_Msg_N
8421 ("cannot constrain type with unknown discriminants",
8422 Subtype_Indication (Type_Definition (N)));
8423 return;
8424 end if;
8426 -- If this is not a completion, construct the implicit full view by
8427 -- deriving from the full view of the parent type. But if this is a
8428 -- completion, the derived private type being built is a full view
8429 -- and the full derivation can only be its underlying full view.
8431 Build_Full_Derivation;
8433 if not Is_Completion then
8434 Set_Full_View (Derived_Type, Full_Der);
8435 else
8436 Set_Underlying_Full_View (Derived_Type, Full_Der);
8437 Set_Is_Underlying_Full_View (Full_Der);
8438 end if;
8440 -- In any case, the primitive operations are inherited from the
8441 -- parent type, not from the internal full view.
8443 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8445 if Derive_Subps then
8446 -- Initialize the list of primitive operations to an empty list,
8447 -- to cover tagged types as well as untagged types. For untagged
8448 -- types this is used either to analyze the call as legal when
8449 -- Extensions_Allowed is True, or to issue a better error message
8450 -- otherwise.
8452 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8454 Derive_Subprograms (Parent_Type, Derived_Type);
8455 end if;
8457 Set_Stored_Constraint (Derived_Type, No_Elist);
8458 Set_Is_Constrained
8459 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8461 else
8462 -- Untagged type, No discriminants on either view
8464 if Nkind (Subtype_Indication (Type_Definition (N))) =
8465 N_Subtype_Indication
8466 then
8467 Error_Msg_N
8468 ("illegal constraint on type without discriminants", N);
8469 end if;
8471 if Present (Discriminant_Specifications (N))
8472 and then Present (Available_Full_View (Parent_Type))
8473 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8474 then
8475 Error_Msg_N ("cannot add discriminants to untagged type", N);
8476 end if;
8478 Set_Stored_Constraint (Derived_Type, No_Elist);
8479 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8481 Set_Is_Controlled_Active
8482 (Derived_Type, Is_Controlled_Active (Parent_Type));
8484 Set_Disable_Controlled
8485 (Derived_Type, Disable_Controlled (Parent_Type));
8487 Set_Has_Controlled_Component
8488 (Derived_Type, Has_Controlled_Component (Parent_Type));
8490 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8492 if not Is_Controlled (Parent_Type) then
8493 Set_Finalize_Storage_Only
8494 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8495 end if;
8497 -- If this is not a completion, construct the implicit full view by
8498 -- deriving from the full view of the parent type. But if this is a
8499 -- completion, the derived private type being built is a full view
8500 -- and the full derivation can only be its underlying full view.
8502 -- ??? If the parent type is untagged private and its completion is
8503 -- tagged, this mechanism will not work because we cannot derive from
8504 -- the tagged full view unless we have an extension.
8506 if Present (Available_Full_View (Parent_Type))
8507 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8508 and then not Error_Posted (N)
8509 then
8510 Build_Full_Derivation;
8512 if not Is_Completion then
8513 Set_Full_View (Derived_Type, Full_Der);
8514 else
8515 Set_Underlying_Full_View (Derived_Type, Full_Der);
8516 Set_Is_Underlying_Full_View (Full_Der);
8517 end if;
8518 end if;
8519 end if;
8521 Set_Has_Unknown_Discriminants (Derived_Type,
8522 Has_Unknown_Discriminants (Parent_Type));
8524 if Is_Private_Type (Derived_Type) then
8525 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8526 end if;
8528 -- If the parent base type is in scope, add the derived type to its
8529 -- list of private dependents, because its full view may become
8530 -- visible subsequently (in a nested private part, a body, or in a
8531 -- further child unit).
8533 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8534 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8536 -- Check for unusual case where a type completed by a private
8537 -- derivation occurs within a package nested in a child unit, and
8538 -- the parent is declared in an ancestor.
8540 if Is_Child_Unit (Scope (Current_Scope))
8541 and then Is_Completion
8542 and then In_Private_Part (Current_Scope)
8543 and then Scope (Parent_Type) /= Current_Scope
8545 -- Note that if the parent has a completion in the private part,
8546 -- (which is itself a derivation from some other private type)
8547 -- it is that completion that is visible, there is no full view
8548 -- available, and no special processing is needed.
8550 and then Present (Full_View (Parent_Type))
8551 then
8552 -- In this case, the full view of the parent type will become
8553 -- visible in the body of the enclosing child, and only then will
8554 -- the current type be possibly non-private. Build an underlying
8555 -- full view that will be installed when the enclosing child body
8556 -- is compiled.
8558 if Present (Underlying_Full_View (Derived_Type)) then
8559 Full_Der := Underlying_Full_View (Derived_Type);
8560 else
8561 Build_Full_Derivation;
8562 Set_Underlying_Full_View (Derived_Type, Full_Der);
8563 Set_Is_Underlying_Full_View (Full_Der);
8564 end if;
8566 -- The full view will be used to swap entities on entry/exit to
8567 -- the body, and must appear in the entity list for the package.
8569 Append_Entity (Full_Der, Scope (Derived_Type));
8570 end if;
8571 end if;
8572 end Build_Derived_Private_Type;
8574 -------------------------------
8575 -- Build_Derived_Record_Type --
8576 -------------------------------
8578 -- 1. INTRODUCTION
8580 -- Ideally we would like to use the same model of type derivation for
8581 -- tagged and untagged record types. Unfortunately this is not quite
8582 -- possible because the semantics of representation clauses is different
8583 -- for tagged and untagged records under inheritance. Consider the
8584 -- following:
8586 -- type R (...) is [tagged] record ... end record;
8587 -- type T (...) is new R (...) [with ...];
8589 -- The representation clauses for T can specify a completely different
8590 -- record layout from R's. Hence the same component can be placed in two
8591 -- very different positions in objects of type T and R. If R and T are
8592 -- tagged types, representation clauses for T can only specify the layout
8593 -- of non inherited components, thus components that are common in R and T
8594 -- have the same position in objects of type R and T.
8596 -- This has two implications. The first is that the entire tree for R's
8597 -- declaration needs to be copied for T in the untagged case, so that T
8598 -- can be viewed as a record type of its own with its own representation
8599 -- clauses. The second implication is the way we handle discriminants.
8600 -- Specifically, in the untagged case we need a way to communicate to Gigi
8601 -- what are the real discriminants in the record, while for the semantics
8602 -- we need to consider those introduced by the user to rename the
8603 -- discriminants in the parent type. This is handled by introducing the
8604 -- notion of stored discriminants. See below for more.
8606 -- Fortunately the way regular components are inherited can be handled in
8607 -- the same way in tagged and untagged types.
8609 -- To complicate things a bit more the private view of a private extension
8610 -- cannot be handled in the same way as the full view (for one thing the
8611 -- semantic rules are somewhat different). We will explain what differs
8612 -- below.
8614 -- 2. DISCRIMINANTS UNDER INHERITANCE
8616 -- The semantic rules governing the discriminants of derived types are
8617 -- quite subtle.
8619 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8620 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8622 -- If parent type has discriminants, then the discriminants that are
8623 -- declared in the derived type are [3.4 (11)]:
8625 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8626 -- there is one;
8628 -- o Otherwise, each discriminant of the parent type (implicitly declared
8629 -- in the same order with the same specifications). In this case, the
8630 -- discriminants are said to be "inherited", or if unknown in the parent
8631 -- are also unknown in the derived type.
8633 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8635 -- o The parent subtype must be constrained;
8637 -- o If the parent type is not a tagged type, then each discriminant of
8638 -- the derived type must be used in the constraint defining a parent
8639 -- subtype. [Implementation note: This ensures that the new discriminant
8640 -- can share storage with an existing discriminant.]
8642 -- For the derived type each discriminant of the parent type is either
8643 -- inherited, constrained to equal some new discriminant of the derived
8644 -- type, or constrained to the value of an expression.
8646 -- When inherited or constrained to equal some new discriminant, the
8647 -- parent discriminant and the discriminant of the derived type are said
8648 -- to "correspond".
8650 -- If a discriminant of the parent type is constrained to a specific value
8651 -- in the derived type definition, then the discriminant is said to be
8652 -- "specified" by that derived type definition.
8654 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8656 -- We have spoken about stored discriminants in point 1 (introduction)
8657 -- above. There are two sorts of stored discriminants: implicit and
8658 -- explicit. As long as the derived type inherits the same discriminants as
8659 -- the root record type, stored discriminants are the same as regular
8660 -- discriminants, and are said to be implicit. However, if any discriminant
8661 -- in the root type was renamed in the derived type, then the derived
8662 -- type will contain explicit stored discriminants. Explicit stored
8663 -- discriminants are discriminants in addition to the semantically visible
8664 -- discriminants defined for the derived type. Stored discriminants are
8665 -- used by Gigi to figure out what are the physical discriminants in
8666 -- objects of the derived type (see precise definition in einfo.ads).
8667 -- As an example, consider the following:
8669 -- type R (D1, D2, D3 : Int) is record ... end record;
8670 -- type T1 is new R;
8671 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8672 -- type T3 is new T2;
8673 -- type T4 (Y : Int) is new T3 (Y, 99);
8675 -- The following table summarizes the discriminants and stored
8676 -- discriminants in R and T1 through T4:
8678 -- Type Discrim Stored Discrim Comment
8679 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8680 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8681 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8682 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8683 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8685 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8686 -- find the corresponding discriminant in the parent type, while
8687 -- Original_Record_Component (abbreviated ORC below) the actual physical
8688 -- component that is renamed. Finally the field Is_Completely_Hidden
8689 -- (abbreviated ICH below) is set for all explicit stored discriminants
8690 -- (see einfo.ads for more info). For the above example this gives:
8692 -- Discrim CD ORC ICH
8693 -- ^^^^^^^ ^^ ^^^ ^^^
8694 -- D1 in R empty itself no
8695 -- D2 in R empty itself no
8696 -- D3 in R empty itself no
8698 -- D1 in T1 D1 in R itself no
8699 -- D2 in T1 D2 in R itself no
8700 -- D3 in T1 D3 in R itself no
8702 -- X1 in T2 D3 in T1 D3 in T2 no
8703 -- X2 in T2 D1 in T1 D1 in T2 no
8704 -- D1 in T2 empty itself yes
8705 -- D2 in T2 empty itself yes
8706 -- D3 in T2 empty itself yes
8708 -- X1 in T3 X1 in T2 D3 in T3 no
8709 -- X2 in T3 X2 in T2 D1 in T3 no
8710 -- D1 in T3 empty itself yes
8711 -- D2 in T3 empty itself yes
8712 -- D3 in T3 empty itself yes
8714 -- Y in T4 X1 in T3 D3 in T4 no
8715 -- D1 in T4 empty itself yes
8716 -- D2 in T4 empty itself yes
8717 -- D3 in T4 empty itself yes
8719 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8721 -- Type derivation for tagged types is fairly straightforward. If no
8722 -- discriminants are specified by the derived type, these are inherited
8723 -- from the parent. No explicit stored discriminants are ever necessary.
8724 -- The only manipulation that is done to the tree is that of adding a
8725 -- _parent field with parent type and constrained to the same constraint
8726 -- specified for the parent in the derived type definition. For instance:
8728 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8729 -- type T1 is new R with null record;
8730 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8732 -- are changed into:
8734 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8735 -- _parent : R (D1, D2, D3);
8736 -- end record;
8738 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8739 -- _parent : T1 (X2, 88, X1);
8740 -- end record;
8742 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8743 -- ORC and ICH fields are:
8745 -- Discrim CD ORC ICH
8746 -- ^^^^^^^ ^^ ^^^ ^^^
8747 -- D1 in R empty itself no
8748 -- D2 in R empty itself no
8749 -- D3 in R empty itself no
8751 -- D1 in T1 D1 in R D1 in R no
8752 -- D2 in T1 D2 in R D2 in R no
8753 -- D3 in T1 D3 in R D3 in R no
8755 -- X1 in T2 D3 in T1 D3 in R no
8756 -- X2 in T2 D1 in T1 D1 in R no
8758 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8760 -- Regardless of whether we are dealing with a tagged or untagged type
8761 -- we will transform all derived type declarations of the form
8763 -- type T is new R (...) [with ...];
8764 -- or
8765 -- subtype S is R (...);
8766 -- type T is new S [with ...];
8767 -- into
8768 -- type BT is new R [with ...];
8769 -- subtype T is BT (...);
8771 -- That is, the base derived type is constrained only if it has no
8772 -- discriminants. The reason for doing this is that GNAT's semantic model
8773 -- assumes that a base type with discriminants is unconstrained.
8775 -- Note that, strictly speaking, the above transformation is not always
8776 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8778 -- procedure B34011A is
8779 -- type REC (D : integer := 0) is record
8780 -- I : Integer;
8781 -- end record;
8783 -- package P is
8784 -- type T6 is new Rec;
8785 -- function F return T6;
8786 -- end P;
8788 -- use P;
8789 -- package Q6 is
8790 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8791 -- end Q6;
8793 -- The definition of Q6.U is illegal. However transforming Q6.U into
8795 -- type BaseU is new T6;
8796 -- subtype U is BaseU (Q6.F.I)
8798 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8799 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8800 -- the transformation described above.
8802 -- There is another instance where the above transformation is incorrect.
8803 -- Consider:
8805 -- package Pack is
8806 -- type Base (D : Integer) is tagged null record;
8807 -- procedure P (X : Base);
8809 -- type Der is new Base (2) with null record;
8810 -- procedure P (X : Der);
8811 -- end Pack;
8813 -- Then the above transformation turns this into
8815 -- type Der_Base is new Base with null record;
8816 -- -- procedure P (X : Base) is implicitly inherited here
8817 -- -- as procedure P (X : Der_Base).
8819 -- subtype Der is Der_Base (2);
8820 -- procedure P (X : Der);
8821 -- -- The overriding of P (X : Der_Base) is illegal since we
8822 -- -- have a parameter conformance problem.
8824 -- To get around this problem, after having semantically processed Der_Base
8825 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8826 -- Discriminant_Constraint from Der so that when parameter conformance is
8827 -- checked when P is overridden, no semantic errors are flagged.
8829 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8831 -- Regardless of whether we are dealing with a tagged or untagged type
8832 -- we will transform all derived type declarations of the form
8834 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8835 -- type T is new R [with ...];
8836 -- into
8837 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8839 -- The reason for such transformation is that it allows us to implement a
8840 -- very clean form of component inheritance as explained below.
8842 -- Note that this transformation is not achieved by direct tree rewriting
8843 -- and manipulation, but rather by redoing the semantic actions that the
8844 -- above transformation will entail. This is done directly in routine
8845 -- Inherit_Components.
8847 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8849 -- In both tagged and untagged derived types, regular non discriminant
8850 -- components are inherited in the derived type from the parent type. In
8851 -- the absence of discriminants component, inheritance is straightforward
8852 -- as components can simply be copied from the parent.
8854 -- If the parent has discriminants, inheriting components constrained with
8855 -- these discriminants requires caution. Consider the following example:
8857 -- type R (D1, D2 : Positive) is [tagged] record
8858 -- S : String (D1 .. D2);
8859 -- end record;
8861 -- type T1 is new R [with null record];
8862 -- type T2 (X : positive) is new R (1, X) [with null record];
8864 -- As explained in 6. above, T1 is rewritten as
8865 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8866 -- which makes the treatment for T1 and T2 identical.
8868 -- What we want when inheriting S, is that references to D1 and D2 in R are
8869 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8870 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8871 -- with either discriminant references in the derived type or expressions.
8872 -- This replacement is achieved as follows: before inheriting R's
8873 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8874 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8875 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8876 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8877 -- by String (1 .. X).
8879 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8881 -- We explain here the rules governing private type extensions relevant to
8882 -- type derivation. These rules are explained on the following example:
8884 -- type D [(...)] is new A [(...)] with private; <-- partial view
8885 -- type D [(...)] is new P [(...)] with null record; <-- full view
8887 -- Type A is called the ancestor subtype of the private extension.
8888 -- Type P is the parent type of the full view of the private extension. It
8889 -- must be A or a type derived from A.
8891 -- The rules concerning the discriminants of private type extensions are
8892 -- [7.3(10-13)]:
8894 -- o If a private extension inherits known discriminants from the ancestor
8895 -- subtype, then the full view must also inherit its discriminants from
8896 -- the ancestor subtype and the parent subtype of the full view must be
8897 -- constrained if and only if the ancestor subtype is constrained.
8899 -- o If a partial view has unknown discriminants, then the full view may
8900 -- define a definite or an indefinite subtype, with or without
8901 -- discriminants.
8903 -- o If a partial view has neither known nor unknown discriminants, then
8904 -- the full view must define a definite subtype.
8906 -- o If the ancestor subtype of a private extension has constrained
8907 -- discriminants, then the parent subtype of the full view must impose a
8908 -- statically matching constraint on those discriminants.
8910 -- This means that only the following forms of private extensions are
8911 -- allowed:
8913 -- type D is new A with private; <-- partial view
8914 -- type D is new P with null record; <-- full view
8916 -- If A has no discriminants than P has no discriminants, otherwise P must
8917 -- inherit A's discriminants.
8919 -- type D is new A (...) with private; <-- partial view
8920 -- type D is new P (:::) with null record; <-- full view
8922 -- P must inherit A's discriminants and (...) and (:::) must statically
8923 -- match.
8925 -- subtype A is R (...);
8926 -- type D is new A with private; <-- partial view
8927 -- type D is new P with null record; <-- full view
8929 -- P must have inherited R's discriminants and must be derived from A or
8930 -- any of its subtypes.
8932 -- type D (..) is new A with private; <-- partial view
8933 -- type D (..) is new P [(:::)] with null record; <-- full view
8935 -- No specific constraints on P's discriminants or constraint (:::).
8936 -- Note that A can be unconstrained, but the parent subtype P must either
8937 -- be constrained or (:::) must be present.
8939 -- type D (..) is new A [(...)] with private; <-- partial view
8940 -- type D (..) is new P [(:::)] with null record; <-- full view
8942 -- P's constraints on A's discriminants must statically match those
8943 -- imposed by (...).
8945 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8947 -- The full view of a private extension is handled exactly as described
8948 -- above. The model chose for the private view of a private extension is
8949 -- the same for what concerns discriminants (i.e. they receive the same
8950 -- treatment as in the tagged case). However, the private view of the
8951 -- private extension always inherits the components of the parent base,
8952 -- without replacing any discriminant reference. Strictly speaking this is
8953 -- incorrect. However, Gigi never uses this view to generate code so this
8954 -- is a purely semantic issue. In theory, a set of transformations similar
8955 -- to those given in 5. and 6. above could be applied to private views of
8956 -- private extensions to have the same model of component inheritance as
8957 -- for non private extensions. However, this is not done because it would
8958 -- further complicate private type processing. Semantically speaking, this
8959 -- leaves us in an uncomfortable situation. As an example consider:
8961 -- package Pack is
8962 -- type R (D : integer) is tagged record
8963 -- S : String (1 .. D);
8964 -- end record;
8965 -- procedure P (X : R);
8966 -- type T is new R (1) with private;
8967 -- private
8968 -- type T is new R (1) with null record;
8969 -- end;
8971 -- This is transformed into:
8973 -- package Pack is
8974 -- type R (D : integer) is tagged record
8975 -- S : String (1 .. D);
8976 -- end record;
8977 -- procedure P (X : R);
8978 -- type T is new R (1) with private;
8979 -- private
8980 -- type BaseT is new R with null record;
8981 -- subtype T is BaseT (1);
8982 -- end;
8984 -- (strictly speaking the above is incorrect Ada)
8986 -- From the semantic standpoint the private view of private extension T
8987 -- should be flagged as constrained since one can clearly have
8989 -- Obj : T;
8991 -- in a unit withing Pack. However, when deriving subprograms for the
8992 -- private view of private extension T, T must be seen as unconstrained
8993 -- since T has discriminants (this is a constraint of the current
8994 -- subprogram derivation model). Thus, when processing the private view of
8995 -- a private extension such as T, we first mark T as unconstrained, we
8996 -- process it, we perform program derivation and just before returning from
8997 -- Build_Derived_Record_Type we mark T as constrained.
8999 -- ??? Are there are other uncomfortable cases that we will have to
9000 -- deal with.
9002 -- 10. RECORD_TYPE_WITH_PRIVATE complications
9004 -- Types that are derived from a visible record type and have a private
9005 -- extension present other peculiarities. They behave mostly like private
9006 -- types, but if they have primitive operations defined, these will not
9007 -- have the proper signatures for further inheritance, because other
9008 -- primitive operations will use the implicit base that we define for
9009 -- private derivations below. This affect subprogram inheritance (see
9010 -- Derive_Subprograms for details). We also derive the implicit base from
9011 -- the base type of the full view, so that the implicit base is a record
9012 -- type and not another private type, This avoids infinite loops.
9014 procedure Build_Derived_Record_Type
9015 (N : Node_Id;
9016 Parent_Type : Entity_Id;
9017 Derived_Type : Entity_Id;
9018 Derive_Subps : Boolean := True)
9020 Discriminant_Specs : constant Boolean :=
9021 Present (Discriminant_Specifications (N));
9022 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
9023 Loc : constant Source_Ptr := Sloc (N);
9024 Private_Extension : constant Boolean :=
9025 Nkind (N) = N_Private_Extension_Declaration;
9026 Assoc_List : Elist_Id;
9027 Constraint_Present : Boolean;
9028 Constrs : Elist_Id;
9029 Discrim : Entity_Id;
9030 Indic : Node_Id;
9031 Inherit_Discrims : Boolean := False;
9032 Last_Discrim : Entity_Id;
9033 New_Base : Entity_Id;
9034 New_Decl : Node_Id;
9035 New_Discrs : Elist_Id;
9036 New_Indic : Node_Id;
9037 Parent_Base : Entity_Id;
9038 Save_Etype : Entity_Id;
9039 Save_Discr_Constr : Elist_Id;
9040 Save_Next_Entity : Entity_Id;
9041 Type_Def : Node_Id;
9043 Discs : Elist_Id := New_Elmt_List;
9044 -- An empty Discs list means that there were no constraints in the
9045 -- subtype indication or that there was an error processing it.
9047 procedure Check_Generic_Ancestors;
9048 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
9049 -- cannot be declared at a deeper level than its parent type is
9050 -- removed. The check on derivation within a generic body is also
9051 -- relaxed, but there's a restriction that a derived tagged type
9052 -- cannot be declared in a generic body if it's derived directly
9053 -- or indirectly from a formal type of that generic. This applies
9054 -- to progenitors as well.
9056 -----------------------------
9057 -- Check_Generic_Ancestors --
9058 -----------------------------
9060 procedure Check_Generic_Ancestors is
9061 Ancestor_Type : Entity_Id;
9062 Intf_List : List_Id;
9063 Intf_Name : Node_Id;
9065 procedure Check_Ancestor;
9066 -- For parent and progenitors.
9068 --------------------
9069 -- Check_Ancestor --
9070 --------------------
9072 procedure Check_Ancestor is
9073 begin
9074 -- If the derived type does have a formal type as an ancestor
9075 -- then it's an error if the derived type is declared within
9076 -- the body of the generic unit that declares the formal type
9077 -- in its generic formal part. It's sufficient to check whether
9078 -- the ancestor type is declared inside the same generic body
9079 -- as the derived type (such as within a nested generic spec),
9080 -- in which case the derivation is legal. If the formal type is
9081 -- declared outside of that generic body, then it's certain
9082 -- that the derived type is declared within the generic body
9083 -- of the generic unit declaring the formal type.
9085 if Is_Generic_Type (Ancestor_Type)
9086 and then Enclosing_Generic_Body (Ancestor_Type) /=
9087 Enclosing_Generic_Body (Derived_Type)
9088 then
9089 Error_Msg_NE
9090 ("ancestor type& is formal type of enclosing"
9091 & " generic unit (RM 3.9.1 (4/2))",
9092 Indic, Ancestor_Type);
9093 end if;
9094 end Check_Ancestor;
9096 begin
9097 if Nkind (N) = N_Private_Extension_Declaration then
9098 Intf_List := Interface_List (N);
9099 else
9100 Intf_List := Interface_List (Type_Definition (N));
9101 end if;
9103 if Present (Enclosing_Generic_Body (Derived_Type)) then
9104 Ancestor_Type := Parent_Type;
9106 while not Is_Generic_Type (Ancestor_Type)
9107 and then Etype (Ancestor_Type) /= Ancestor_Type
9108 loop
9109 Ancestor_Type := Etype (Ancestor_Type);
9110 end loop;
9112 Check_Ancestor;
9114 if Present (Intf_List) then
9115 Intf_Name := First (Intf_List);
9116 while Present (Intf_Name) loop
9117 Ancestor_Type := Entity (Intf_Name);
9118 Check_Ancestor;
9119 Next (Intf_Name);
9120 end loop;
9121 end if;
9122 end if;
9123 end Check_Generic_Ancestors;
9125 -- Start of processing for Build_Derived_Record_Type
9127 begin
9128 -- If the parent type is a private extension with discriminants, we
9129 -- need to have an unconstrained type on which to apply the inherited
9130 -- constraint, so we get to the full view. However, this means that the
9131 -- derived type and its implicit base type created below will not point
9132 -- to the same view of their respective parent type and, thus, special
9133 -- glue code like Exp_Ch7.Convert_View is needed to bridge this gap.
9135 if Ekind (Parent_Type) = E_Record_Type_With_Private
9136 and then Has_Discriminants (Parent_Type)
9137 and then Present (Full_View (Parent_Type))
9138 then
9139 Parent_Base := Base_Type (Full_View (Parent_Type));
9140 else
9141 Parent_Base := Base_Type (Parent_Type);
9142 end if;
9144 -- If the parent type is declared as a subtype of another private
9145 -- type with inherited discriminants, its generated base type is
9146 -- itself a record subtype. To further inherit the constraint we
9147 -- need to use its own base to have an unconstrained type on which
9148 -- to apply the inherited constraint.
9150 if Ekind (Parent_Base) = E_Record_Subtype then
9151 Parent_Base := Base_Type (Parent_Base);
9152 end if;
9154 -- If the parent base is a private type and only its full view has
9155 -- discriminants, use the full view's base type.
9157 -- This can happen when we are deriving from a subtype of a derived type
9158 -- of a private type derived from a discriminated type with known
9159 -- discriminant:
9161 -- package Pkg;
9162 -- type Root_Type(I: Positive) is record
9163 -- ...
9164 -- end record;
9165 -- type Bounded_Root_Type is private;
9166 -- private
9167 -- type Bounded_Root_Type is new Root_Type(10);
9168 -- end Pkg;
9170 -- package Pkg2 is
9171 -- type Constrained_Root_Type is new Pkg.Bounded_Root_Type;
9172 -- end Pkg2;
9173 -- subtype Sub_Base is Pkg2.Constrained_Root_Type;
9174 -- type New_Der_Type is new Sub_Base;
9176 if Is_Private_Type (Parent_Base)
9177 and then Present (Full_View (Parent_Base))
9178 and then not Has_Discriminants (Parent_Base)
9179 and then Has_Discriminants (Full_View (Parent_Base))
9180 then
9181 Parent_Base := Base_Type (Full_View (Parent_Base));
9182 end if;
9184 -- AI05-0115: if this is a derivation from a private type in some
9185 -- other scope that may lead to invisible components for the derived
9186 -- type, mark it accordingly.
9188 if Is_Private_Type (Parent_Type) then
9189 if Scope (Parent_Base) = Scope (Derived_Type) then
9190 null;
9192 elsif In_Open_Scopes (Scope (Parent_Base))
9193 and then In_Private_Part (Scope (Parent_Base))
9194 then
9195 null;
9197 else
9198 Set_Has_Private_Ancestor (Derived_Type);
9199 end if;
9201 else
9202 Set_Has_Private_Ancestor
9203 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9204 end if;
9206 -- Before we start the previously documented transformations, here is
9207 -- little fix for size and alignment of tagged types. Normally when we
9208 -- derive type D from type P, we copy the size and alignment of P as the
9209 -- default for D, and in the absence of explicit representation clauses
9210 -- for D, the size and alignment are indeed the same as the parent.
9212 -- But this is wrong for tagged types, since fields may be added, and
9213 -- the default size may need to be larger, and the default alignment may
9214 -- need to be larger.
9216 -- We therefore reset the size and alignment fields in the tagged case.
9217 -- Note that the size and alignment will in any case be at least as
9218 -- large as the parent type (since the derived type has a copy of the
9219 -- parent type in the _parent field)
9221 -- The type is also marked as being tagged here, which is needed when
9222 -- processing components with a self-referential anonymous access type
9223 -- in the call to Check_Anonymous_Access_Components below. Note that
9224 -- this flag is also set later on for completeness.
9226 if Is_Tagged then
9227 Set_Is_Tagged_Type (Derived_Type);
9228 Reinit_Size_Align (Derived_Type);
9229 end if;
9231 -- STEP 0a: figure out what kind of derived type declaration we have
9233 if Private_Extension then
9234 Type_Def := N;
9235 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9236 Set_Default_SSO (Derived_Type);
9237 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9239 else
9240 Type_Def := Type_Definition (N);
9242 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9243 -- Parent_Base can be a private type or private extension. However,
9244 -- for tagged types with an extension the newly added fields are
9245 -- visible and hence the Derived_Type is always an E_Record_Type.
9246 -- (except that the parent may have its own private fields).
9247 -- For untagged types we preserve the Ekind of the Parent_Base.
9249 if Present (Record_Extension_Part (Type_Def)) then
9250 Mutate_Ekind (Derived_Type, E_Record_Type);
9251 Set_Default_SSO (Derived_Type);
9252 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9254 -- Create internal access types for components with anonymous
9255 -- access types.
9257 if Ada_Version >= Ada_2005 then
9258 Check_Anonymous_Access_Components
9259 (N, Derived_Type, Derived_Type,
9260 Component_List (Record_Extension_Part (Type_Def)));
9261 end if;
9263 else
9264 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9265 end if;
9266 end if;
9268 -- Indic can either be an N_Identifier if the subtype indication
9269 -- contains no constraint or an N_Subtype_Indication if the subtype
9270 -- indication has a constraint. In either case it can include an
9271 -- interface list.
9273 Indic := Subtype_Indication (Type_Def);
9274 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9276 -- Check that the type has visible discriminants. The type may be
9277 -- a private type with unknown discriminants whose full view has
9278 -- discriminants which are invisible.
9280 if Constraint_Present then
9281 if not Has_Discriminants (Parent_Base)
9282 or else
9283 (Has_Unknown_Discriminants (Parent_Base)
9284 and then Is_Private_Type (Parent_Base))
9285 then
9286 Error_Msg_N
9287 ("invalid constraint: type has no discriminant",
9288 Constraint (Indic));
9290 Constraint_Present := False;
9291 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9293 elsif Is_Constrained (Parent_Type) then
9294 Error_Msg_N
9295 ("invalid constraint: parent type is already constrained",
9296 Constraint (Indic));
9298 Constraint_Present := False;
9299 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9300 end if;
9301 end if;
9303 -- STEP 0b: If needed, apply transformation given in point 5. above
9305 if not Private_Extension
9306 and then Has_Discriminants (Parent_Type)
9307 and then not Discriminant_Specs
9308 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9309 then
9310 -- First, we must analyze the constraint (see comment in point 5.)
9311 -- The constraint may come from the subtype indication of the full
9312 -- declaration. Temporarily set the state of the Derived_Type to
9313 -- "self-hidden" (see RM-8.3(17)).
9315 if Constraint_Present then
9316 pragma Assert (Is_Not_Self_Hidden (Derived_Type));
9317 Set_Is_Not_Self_Hidden (Derived_Type, False);
9318 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9319 Set_Is_Not_Self_Hidden (Derived_Type);
9321 -- If there is no explicit constraint, there might be one that is
9322 -- inherited from a constrained parent type. In that case verify that
9323 -- it conforms to the constraint in the partial view. In perverse
9324 -- cases the parent subtypes of the partial and full view can have
9325 -- different constraints.
9327 elsif Present (Stored_Constraint (Parent_Type)) then
9328 New_Discrs := Stored_Constraint (Parent_Type);
9330 else
9331 New_Discrs := No_Elist;
9332 end if;
9334 if Has_Discriminants (Derived_Type)
9335 and then Has_Private_Declaration (Derived_Type)
9336 and then Present (Discriminant_Constraint (Derived_Type))
9337 and then Present (New_Discrs)
9338 then
9339 -- Verify that constraints of the full view statically match
9340 -- those given in the partial view.
9342 declare
9343 C1, C2 : Elmt_Id;
9345 begin
9346 C1 := First_Elmt (New_Discrs);
9347 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9348 while Present (C1) and then Present (C2) loop
9349 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9350 or else
9351 (Is_OK_Static_Expression (Node (C1))
9352 and then Is_OK_Static_Expression (Node (C2))
9353 and then
9354 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9355 then
9356 null;
9358 else
9359 if Constraint_Present then
9360 Error_Msg_N
9361 ("constraint not conformant to previous declaration",
9362 Node (C1));
9363 else
9364 Error_Msg_N
9365 ("constraint of full view is incompatible "
9366 & "with partial view", N);
9367 end if;
9368 end if;
9370 Next_Elmt (C1);
9371 Next_Elmt (C2);
9372 end loop;
9373 end;
9374 end if;
9376 -- Insert and analyze the declaration for the unconstrained base type
9378 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9380 New_Decl :=
9381 Make_Full_Type_Declaration (Loc,
9382 Defining_Identifier => New_Base,
9383 Type_Definition =>
9384 Make_Derived_Type_Definition (Loc,
9385 Abstract_Present => Abstract_Present (Type_Def),
9386 Limited_Present => Limited_Present (Type_Def),
9387 Subtype_Indication =>
9388 New_Occurrence_Of (Parent_Base, Loc),
9389 Record_Extension_Part =>
9390 Relocate_Node (Record_Extension_Part (Type_Def)),
9391 Interface_List => Interface_List (Type_Def)));
9393 Set_Parent (New_Decl, Parent (N));
9394 Mark_Rewrite_Insertion (New_Decl);
9395 Insert_Before (N, New_Decl);
9397 -- In the extension case, make sure ancestor is frozen appropriately
9398 -- (see also non-discriminated case below).
9400 if Present (Record_Extension_Part (Type_Def))
9401 or else Is_Interface (Parent_Base)
9402 then
9403 Freeze_Before (New_Decl, Parent_Type);
9404 end if;
9406 -- Note that this call passes False for the Derive_Subps parameter
9407 -- because subprogram derivation is deferred until after creating
9408 -- the subtype (see below).
9410 Build_Derived_Type
9411 (New_Decl, Parent_Base, New_Base,
9412 Is_Completion => False, Derive_Subps => False);
9414 -- ??? This needs re-examination to determine whether the
9415 -- following call can simply be replaced by a call to Analyze.
9417 Set_Analyzed (New_Decl);
9419 -- Insert and analyze the declaration for the constrained subtype
9421 if Constraint_Present then
9422 New_Indic :=
9423 Make_Subtype_Indication (Loc,
9424 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9425 Constraint => Relocate_Node (Constraint (Indic)));
9427 else
9428 declare
9429 Constr_List : constant List_Id := New_List;
9430 C : Elmt_Id;
9431 Expr : Node_Id;
9433 begin
9434 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9435 while Present (C) loop
9436 Expr := Node (C);
9438 -- It is safe here to call New_Copy_Tree since we called
9439 -- Force_Evaluation on each constraint previously
9440 -- in Build_Discriminant_Constraints.
9442 Append (New_Copy_Tree (Expr), To => Constr_List);
9444 Next_Elmt (C);
9445 end loop;
9447 New_Indic :=
9448 Make_Subtype_Indication (Loc,
9449 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9450 Constraint =>
9451 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9452 end;
9453 end if;
9455 Rewrite (N,
9456 Make_Subtype_Declaration (Loc,
9457 Defining_Identifier => Derived_Type,
9458 Subtype_Indication => New_Indic));
9460 -- Keep the aspects from the original node
9462 Move_Aspects (Original_Node (N), N);
9464 Analyze (N);
9466 -- Derivation of subprograms must be delayed until the full subtype
9467 -- has been established, to ensure proper overriding of subprograms
9468 -- inherited by full types. If the derivations occurred as part of
9469 -- the call to Build_Derived_Type above, then the check for type
9470 -- conformance would fail because earlier primitive subprograms
9471 -- could still refer to the full type prior the change to the new
9472 -- subtype and hence would not match the new base type created here.
9473 -- Subprograms are not derived, however, when Derive_Subps is False
9474 -- (since otherwise there could be redundant derivations).
9476 if Derive_Subps then
9477 Derive_Subprograms (Parent_Type, Derived_Type);
9478 end if;
9480 -- For tagged types the Discriminant_Constraint of the new base itype
9481 -- is inherited from the first subtype so that no subtype conformance
9482 -- problem arise when the first subtype overrides primitive
9483 -- operations inherited by the implicit base type.
9485 if Is_Tagged then
9486 Set_Discriminant_Constraint
9487 (New_Base, Discriminant_Constraint (Derived_Type));
9488 end if;
9490 return;
9491 end if;
9493 -- If we get here Derived_Type will have no discriminants or it will be
9494 -- a discriminated unconstrained base type.
9496 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9498 if Is_Tagged then
9500 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9501 -- The declaration of a specific descendant of an interface type
9502 -- freezes the interface type (RM 13.14).
9504 if not Private_Extension or else Is_Interface (Parent_Base) then
9505 Freeze_Before (N, Parent_Type);
9506 end if;
9508 if Ada_Version >= Ada_2005 then
9509 Check_Generic_Ancestors;
9511 elsif Type_Access_Level (Derived_Type) /=
9512 Type_Access_Level (Parent_Type)
9513 and then not Is_Generic_Type (Derived_Type)
9514 then
9515 if Is_Controlled (Parent_Type) then
9516 Error_Msg_N
9517 ("controlled type must be declared at the library level",
9518 Indic);
9519 else
9520 Error_Msg_N
9521 ("type extension at deeper accessibility level than parent",
9522 Indic);
9523 end if;
9525 else
9526 declare
9527 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9528 begin
9529 if Present (GB)
9530 and then GB /= Enclosing_Generic_Body (Parent_Base)
9531 then
9532 Error_Msg_NE
9533 ("parent type of& must not be outside generic body"
9534 & " (RM 3.9.1(4))",
9535 Indic, Derived_Type);
9536 end if;
9537 end;
9538 end if;
9539 end if;
9541 -- Ada 2005 (AI-251)
9543 if Ada_Version >= Ada_2005 and then Is_Tagged then
9545 -- "The declaration of a specific descendant of an interface type
9546 -- freezes the interface type" (RM 13.14).
9548 declare
9549 Iface : Node_Id;
9550 begin
9551 Iface := First (Interface_List (Type_Def));
9552 while Present (Iface) loop
9553 Freeze_Before (N, Etype (Iface));
9554 Next (Iface);
9555 end loop;
9556 end;
9557 end if;
9559 -- STEP 1b : preliminary cleanup of the full view of private types
9561 -- If the type is already marked as having discriminants, then it's the
9562 -- completion of a private type or private extension and we need to
9563 -- retain the discriminants from the partial view if the current
9564 -- declaration has Discriminant_Specifications so that we can verify
9565 -- conformance. However, we must remove any existing components that
9566 -- were inherited from the parent (and attached in Copy_And_Swap)
9567 -- because the full type inherits all appropriate components anyway, and
9568 -- we do not want the partial view's components interfering.
9570 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9571 Discrim := First_Discriminant (Derived_Type);
9572 loop
9573 Last_Discrim := Discrim;
9574 Next_Discriminant (Discrim);
9575 exit when No (Discrim);
9576 end loop;
9578 Set_Last_Entity (Derived_Type, Last_Discrim);
9580 -- In all other cases wipe out the list of inherited components (even
9581 -- inherited discriminants), it will be properly rebuilt here.
9583 else
9584 Set_First_Entity (Derived_Type, Empty);
9585 Set_Last_Entity (Derived_Type, Empty);
9586 end if;
9588 -- STEP 1c: Initialize some flags for the Derived_Type
9590 -- The following flags must be initialized here so that
9591 -- Process_Discriminants can check that discriminants of tagged types do
9592 -- not have a default initial value and that access discriminants are
9593 -- only specified for limited records. For completeness, these flags are
9594 -- also initialized along with all the other flags below.
9596 -- AI-419: Limitedness is not inherited from an interface parent, so to
9597 -- be limited in that case the type must be explicitly declared as
9598 -- limited, or synchronized. While task and protected interfaces are
9599 -- always limited, a synchronized private extension might not inherit
9600 -- from such interfaces, and so we also need to recognize the
9601 -- explicit limitedness implied by a synchronized private extension
9602 -- that does not derive from a synchronized interface (see RM-7.3(6/2)).
9604 if Limited_Present (Type_Def)
9605 or else Synchronized_Present (Type_Def)
9606 then
9607 Set_Is_Limited_Record (Derived_Type);
9609 elsif Is_Limited_Record (Parent_Type)
9610 or else (Present (Full_View (Parent_Type))
9611 and then Is_Limited_Record (Full_View (Parent_Type)))
9612 then
9613 if not Is_Interface (Parent_Type)
9614 or else Is_Concurrent_Interface (Parent_Type)
9615 then
9616 Set_Is_Limited_Record (Derived_Type);
9617 end if;
9618 end if;
9620 -- STEP 2a: process discriminants of derived type if any
9622 Push_Scope (Derived_Type);
9624 if Discriminant_Specs then
9625 Set_Has_Unknown_Discriminants (Derived_Type, False);
9627 -- The following call to Check_Or_Process_Discriminants initializes
9628 -- fields Has_Discriminants and Discriminant_Constraint, unless we
9629 -- are processing the completion of a private type declaration.
9630 -- Temporarily set the state of the Derived_Type to "self-hidden"
9631 -- (see RM-8.3(17)), unless it is already the case.
9633 if Is_Not_Self_Hidden (Derived_Type) then
9634 Set_Is_Not_Self_Hidden (Derived_Type, False);
9635 Check_Or_Process_Discriminants (N, Derived_Type);
9636 Set_Is_Not_Self_Hidden (Derived_Type);
9637 else
9638 Check_Or_Process_Discriminants (N, Derived_Type);
9639 end if;
9641 -- For untagged types, the constraint on the Parent_Type must be
9642 -- present and is used to rename the discriminants.
9644 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9645 Error_Msg_N ("untagged parent must have discriminants", Indic);
9647 elsif not Is_Tagged and then not Constraint_Present then
9648 Error_Msg_N
9649 ("discriminant constraint needed for derived untagged records",
9650 Indic);
9652 -- Otherwise the parent subtype must be constrained unless we have a
9653 -- private extension.
9655 elsif not Constraint_Present
9656 and then not Private_Extension
9657 and then not Is_Constrained (Parent_Type)
9658 then
9659 Error_Msg_N
9660 ("unconstrained type not allowed in this context", Indic);
9662 elsif Constraint_Present then
9663 -- The following call sets the field Corresponding_Discriminant
9664 -- for the discriminants in the Derived_Type.
9666 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9668 -- For untagged types all new discriminants must rename
9669 -- discriminants in the parent. For private extensions new
9670 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9672 Discrim := First_Discriminant (Derived_Type);
9673 while Present (Discrim) loop
9674 if not Is_Tagged
9675 and then No (Corresponding_Discriminant (Discrim))
9676 then
9677 Error_Msg_N
9678 ("new discriminants must constrain old ones", Discrim);
9680 elsif Private_Extension
9681 and then Present (Corresponding_Discriminant (Discrim))
9682 then
9683 Error_Msg_N
9684 ("only static constraints allowed for parent"
9685 & " discriminants in the partial view", Indic);
9686 exit;
9687 end if;
9689 -- If a new discriminant is used in the constraint, then its
9690 -- subtype must be statically compatible with the subtype of
9691 -- the parent discriminant (RM 3.7(15)).
9693 if Present (Corresponding_Discriminant (Discrim)) then
9694 Check_Constraining_Discriminant
9695 (Discrim, Corresponding_Discriminant (Discrim));
9696 end if;
9698 Next_Discriminant (Discrim);
9699 end loop;
9701 -- Check whether the constraints of the full view statically
9702 -- match those imposed by the parent subtype [7.3(13)].
9704 if Present (Stored_Constraint (Derived_Type)) then
9705 declare
9706 C1, C2 : Elmt_Id;
9708 begin
9709 C1 := First_Elmt (Discs);
9710 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9711 while Present (C1) and then Present (C2) loop
9712 if not
9713 Fully_Conformant_Expressions (Node (C1), Node (C2))
9714 then
9715 Error_Msg_N
9716 ("not conformant with previous declaration",
9717 Node (C1));
9718 end if;
9720 Next_Elmt (C1);
9721 Next_Elmt (C2);
9722 end loop;
9723 end;
9724 end if;
9725 end if;
9727 -- STEP 2b: No new discriminants, inherit discriminants if any
9729 else
9730 if Private_Extension then
9731 Set_Has_Unknown_Discriminants
9732 (Derived_Type,
9733 Has_Unknown_Discriminants (Parent_Type)
9734 or else Unknown_Discriminants_Present (N));
9736 -- The partial view of the parent may have unknown discriminants,
9737 -- but if the full view has discriminants and the parent type is
9738 -- in scope they must be inherited.
9740 elsif Has_Unknown_Discriminants (Parent_Type)
9741 and then
9742 (not Has_Discriminants (Parent_Type)
9743 or else not In_Open_Scopes (Scope (Parent_Base)))
9744 then
9745 Set_Has_Unknown_Discriminants (Derived_Type);
9746 end if;
9748 if not Has_Unknown_Discriminants (Derived_Type)
9749 and then not Has_Unknown_Discriminants (Parent_Base)
9750 and then Has_Discriminants (Parent_Type)
9751 then
9752 Inherit_Discrims := True;
9753 Set_Has_Discriminants
9754 (Derived_Type, True);
9755 Set_Discriminant_Constraint
9756 (Derived_Type, Discriminant_Constraint (Parent_Base));
9757 end if;
9759 -- The following test is true for private types (remember
9760 -- transformation 5. is not applied to those) and in an error
9761 -- situation.
9763 if Constraint_Present then
9764 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9765 end if;
9767 -- For now mark a new derived type as constrained only if it has no
9768 -- discriminants. At the end of Build_Derived_Record_Type we properly
9769 -- set this flag in the case of private extensions. See comments in
9770 -- point 9. just before body of Build_Derived_Record_Type.
9772 Set_Is_Constrained
9773 (Derived_Type,
9774 not (Inherit_Discrims
9775 or else Has_Unknown_Discriminants (Derived_Type)));
9776 end if;
9778 -- STEP 3: initialize fields of derived type
9780 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9781 Set_Stored_Constraint (Derived_Type, No_Elist);
9783 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9784 -- but cannot be interfaces
9786 if not Private_Extension
9787 and then Ekind (Derived_Type) /= E_Private_Type
9788 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9789 then
9790 if Interface_Present (Type_Def) then
9791 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9792 end if;
9794 Set_Interfaces (Derived_Type, No_Elist);
9795 end if;
9797 -- Fields inherited from the Parent_Type
9799 Set_Has_Specified_Layout
9800 (Derived_Type, Has_Specified_Layout (Parent_Type));
9801 Set_Is_Limited_Composite
9802 (Derived_Type, Is_Limited_Composite (Parent_Type));
9803 Set_Is_Private_Composite
9804 (Derived_Type, Is_Private_Composite (Parent_Type));
9806 if Is_Tagged_Type (Parent_Type) then
9807 Set_No_Tagged_Streams_Pragma
9808 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9809 end if;
9811 -- Fields inherited from the Parent_Base
9813 Set_Has_Controlled_Component
9814 (Derived_Type, Has_Controlled_Component (Parent_Base));
9815 Set_Has_Non_Standard_Rep
9816 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9817 Set_Has_Primitive_Operations
9818 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9820 -- Set fields for private derived types
9822 if Is_Private_Type (Derived_Type) then
9823 Set_Depends_On_Private (Derived_Type, True);
9824 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9825 end if;
9827 -- Inherit fields for non-private types. If this is the completion of a
9828 -- derivation from a private type, the parent itself is private and the
9829 -- attributes come from its full view, which must be present.
9831 if Is_Record_Type (Derived_Type) then
9832 declare
9833 Parent_Full : Entity_Id;
9835 begin
9836 if Is_Private_Type (Parent_Base)
9837 and then not Is_Record_Type (Parent_Base)
9838 then
9839 Parent_Full := Full_View (Parent_Base);
9840 else
9841 Parent_Full := Parent_Base;
9842 end if;
9844 Set_Component_Alignment
9845 (Derived_Type, Component_Alignment (Parent_Full));
9846 Set_C_Pass_By_Copy
9847 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9848 Set_Has_Complex_Representation
9849 (Derived_Type, Has_Complex_Representation (Parent_Full));
9851 -- For untagged types, inherit the layout by default to avoid
9852 -- costly changes of representation for type conversions.
9854 if not Is_Tagged then
9855 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9856 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9857 end if;
9858 end;
9859 end if;
9861 -- Initialize the list of primitive operations to an empty list,
9862 -- to cover tagged types as well as untagged types. For untagged
9863 -- types this is used either to analyze the call as legal when
9864 -- Extensions_Allowed is True, or to issue a better error message
9865 -- otherwise.
9867 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9869 -- Set fields for tagged types
9871 if Is_Tagged then
9872 -- All tagged types defined in Ada.Finalization are controlled
9874 if Chars (Scope (Derived_Type)) = Name_Finalization
9875 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9876 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9877 then
9878 Set_Is_Controlled_Active (Derived_Type);
9879 else
9880 Set_Is_Controlled_Active
9881 (Derived_Type, Is_Controlled_Active (Parent_Base));
9882 end if;
9884 -- Minor optimization: there is no need to generate the class-wide
9885 -- entity associated with an underlying record view.
9887 if not Is_Underlying_Record_View (Derived_Type) then
9888 Make_Class_Wide_Type (Derived_Type);
9889 end if;
9891 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9893 if Has_Discriminants (Derived_Type)
9894 and then Constraint_Present
9895 then
9896 Set_Stored_Constraint
9897 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9898 end if;
9900 if Ada_Version >= Ada_2005 then
9901 declare
9902 Ifaces_List : Elist_Id;
9904 begin
9905 -- Checks rules 3.9.4 (13/2 and 14/2)
9907 if Comes_From_Source (Derived_Type)
9908 and then not Is_Private_Type (Derived_Type)
9909 and then Is_Interface (Parent_Type)
9910 and then not Is_Interface (Derived_Type)
9911 then
9912 if Is_Task_Interface (Parent_Type) then
9913 Error_Msg_N
9914 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9915 Derived_Type);
9917 elsif Is_Protected_Interface (Parent_Type) then
9918 Error_Msg_N
9919 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9920 Derived_Type);
9921 end if;
9922 end if;
9924 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9926 Check_Interfaces (N, Type_Def);
9928 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9929 -- not already in the parents.
9931 Collect_Interfaces
9932 (T => Derived_Type,
9933 Ifaces_List => Ifaces_List,
9934 Exclude_Parents => True);
9936 Set_Interfaces (Derived_Type, Ifaces_List);
9938 -- If the derived type is the anonymous type created for
9939 -- a declaration whose parent has a constraint, propagate
9940 -- the interface list to the source type. This must be done
9941 -- prior to the completion of the analysis of the source type
9942 -- because the components in the extension may contain current
9943 -- instances whose legality depends on some ancestor.
9945 if Is_Itype (Derived_Type) then
9946 declare
9947 Def : constant Node_Id :=
9948 Associated_Node_For_Itype (Derived_Type);
9949 begin
9950 if Present (Def)
9951 and then Nkind (Def) = N_Full_Type_Declaration
9952 then
9953 Set_Interfaces
9954 (Defining_Identifier (Def), Ifaces_List);
9955 end if;
9956 end;
9957 end if;
9959 -- A type extension is automatically Ghost when one of its
9960 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9961 -- also inherited when the parent type is Ghost, but this is
9962 -- done in Build_Derived_Type as the mechanism also handles
9963 -- untagged derivations.
9965 if Implements_Ghost_Interface (Derived_Type) then
9966 Set_Is_Ghost_Entity (Derived_Type);
9967 end if;
9968 end;
9969 end if;
9970 end if;
9972 -- STEP 4: Inherit components from the parent base and constrain them.
9973 -- Apply the second transformation described in point 6. above.
9975 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9976 or else not Has_Discriminants (Parent_Type)
9977 or else not Is_Constrained (Parent_Type)
9978 then
9979 Constrs := Discs;
9980 else
9981 Constrs := Discriminant_Constraint (Parent_Type);
9982 end if;
9984 Assoc_List :=
9985 Inherit_Components
9986 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9988 -- STEP 5a: Copy the parent record declaration for untagged types
9990 Set_Has_Implicit_Dereference
9991 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9993 if not Is_Tagged then
9995 -- Discriminant_Constraint (Derived_Type) has been properly
9996 -- constructed. Save it and temporarily set it to Empty because we
9997 -- do not want the call to New_Copy_Tree below to mess this list.
9999 if Has_Discriminants (Derived_Type) then
10000 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
10001 Set_Discriminant_Constraint (Derived_Type, No_Elist);
10002 else
10003 Save_Discr_Constr := No_Elist;
10004 end if;
10006 -- Save the Etype field of Derived_Type. It is correctly set now,
10007 -- but the call to New_Copy tree may remap it to point to itself,
10008 -- which is not what we want. Ditto for the Next_Entity field.
10010 Save_Etype := Etype (Derived_Type);
10011 Save_Next_Entity := Next_Entity (Derived_Type);
10013 -- Assoc_List maps all stored discriminants in the Parent_Base to
10014 -- stored discriminants in the Derived_Type. It is fundamental that
10015 -- no types or itypes with discriminants other than the stored
10016 -- discriminants appear in the entities declared inside
10017 -- Derived_Type, since the back end cannot deal with it.
10019 New_Decl :=
10020 New_Copy_Tree
10021 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
10022 Copy_Dimensions_Of_Components (Derived_Type);
10024 -- Restore the fields saved prior to the New_Copy_Tree call
10025 -- and compute the stored constraint.
10027 Set_Etype (Derived_Type, Save_Etype);
10028 Link_Entities (Derived_Type, Save_Next_Entity);
10030 if Has_Discriminants (Derived_Type) then
10031 Set_Discriminant_Constraint
10032 (Derived_Type, Save_Discr_Constr);
10033 Set_Stored_Constraint
10034 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
10036 Replace_Discriminants (Derived_Type, New_Decl);
10037 end if;
10039 -- Relocate the aspects from the original type
10041 Remove_Aspects (New_Decl);
10042 Move_Aspects (N, New_Decl);
10044 -- Insert the new derived type declaration
10046 Rewrite (N, New_Decl);
10048 -- STEP 5b: Complete the processing for record extensions in generics
10050 -- There is no completion for record extensions declared in the
10051 -- parameter part of a generic, so we need to complete processing for
10052 -- these generic record extensions here. Record_Type_Definition will
10053 -- set the Is_Not_Self_Hidden flag.
10055 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
10056 Record_Type_Definition (Empty, Derived_Type);
10058 -- STEP 5c: Process the record extension for non private tagged types
10060 elsif not Private_Extension then
10061 Expand_Record_Extension (Derived_Type, Type_Def);
10063 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
10064 -- implemented interfaces if we are in expansion mode
10066 if Expander_Active
10067 and then Has_Interfaces (Derived_Type)
10068 then
10069 Add_Interface_Tag_Components (N, Derived_Type);
10070 end if;
10072 -- Analyze the record extension
10074 Record_Type_Definition
10075 (Record_Extension_Part (Type_Def), Derived_Type);
10076 end if;
10078 End_Scope;
10080 -- Nothing else to do if there is an error in the derivation.
10081 -- An unusual case: the full view may be derived from a type in an
10082 -- instance, when the partial view was used illegally as an actual
10083 -- in that instance, leading to a circular definition.
10085 if Etype (Derived_Type) = Any_Type
10086 or else Etype (Parent_Type) = Derived_Type
10087 then
10088 return;
10089 end if;
10091 -- Set delayed freeze and then derive subprograms, we need to do
10092 -- this in this order so that derived subprograms inherit the
10093 -- derived freeze if necessary.
10095 Set_Has_Delayed_Freeze (Derived_Type);
10097 if Derive_Subps then
10098 Derive_Subprograms (Parent_Type, Derived_Type);
10099 end if;
10101 -- If we have a private extension which defines a constrained derived
10102 -- type mark as constrained here after we have derived subprograms. See
10103 -- comment on point 9. just above the body of Build_Derived_Record_Type.
10105 if Private_Extension and then Inherit_Discrims then
10106 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
10107 Set_Is_Constrained (Derived_Type, True);
10108 Set_Discriminant_Constraint (Derived_Type, Discs);
10110 elsif Is_Constrained (Parent_Type) then
10111 Set_Is_Constrained
10112 (Derived_Type, True);
10113 Set_Discriminant_Constraint
10114 (Derived_Type, Discriminant_Constraint (Parent_Type));
10115 end if;
10116 end if;
10118 -- Update the class-wide type, which shares the now-completed entity
10119 -- list with its specific type. In case of underlying record views,
10120 -- we do not generate the corresponding class wide entity.
10122 if Is_Tagged
10123 and then not Is_Underlying_Record_View (Derived_Type)
10124 then
10125 Set_First_Entity
10126 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
10127 Set_Last_Entity
10128 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
10129 end if;
10131 Check_Function_Writable_Actuals (N);
10132 end Build_Derived_Record_Type;
10134 ------------------------
10135 -- Build_Derived_Type --
10136 ------------------------
10138 procedure Build_Derived_Type
10139 (N : Node_Id;
10140 Parent_Type : Entity_Id;
10141 Derived_Type : Entity_Id;
10142 Is_Completion : Boolean;
10143 Derive_Subps : Boolean := True)
10145 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10147 begin
10148 -- Set common attributes
10150 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10151 and then Ekind (Parent_Base) in Elementary_Kind
10152 then
10153 Reinit_Field_To_Zero (Derived_Type, F_Discriminant_Constraint);
10154 end if;
10156 Set_Scope (Derived_Type, Current_Scope);
10157 Set_Etype (Derived_Type, Parent_Base);
10158 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10159 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10161 Set_Size_Info (Derived_Type, Parent_Type);
10162 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10164 Set_Is_Controlled_Active
10165 (Derived_Type, Is_Controlled_Active (Parent_Type));
10167 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
10168 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10169 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10171 if Is_Tagged_Type (Derived_Type) then
10172 Set_No_Tagged_Streams_Pragma
10173 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10174 end if;
10176 -- If the parent has primitive routines and may have not-seen-yet aspect
10177 -- specifications (e.g., a Pack pragma), then set the derived type link
10178 -- in order to later diagnose "early derivation" issues. If in different
10179 -- compilation units, then "early derivation" cannot be an issue (and we
10180 -- don't like interunit references that go in the opposite direction of
10181 -- semantic dependencies).
10183 if Has_Primitive_Operations (Parent_Type)
10184 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10185 Enclosing_Comp_Unit_Node (Derived_Type)
10186 then
10187 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10188 end if;
10190 -- If the parent type is a private subtype, the convention on the base
10191 -- type may be set in the private part, and not propagated to the
10192 -- subtype until later, so we obtain the convention from the base type.
10194 Set_Convention (Derived_Type, Convention (Parent_Base));
10196 if Is_Tagged_Type (Derived_Type)
10197 and then Present (Class_Wide_Type (Derived_Type))
10198 then
10199 Set_Convention (Class_Wide_Type (Derived_Type),
10200 Convention (Class_Wide_Type (Parent_Base)));
10201 end if;
10203 -- Set SSO default for record or array type
10205 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10206 and then Is_Base_Type (Derived_Type)
10207 then
10208 Set_Default_SSO (Derived_Type);
10209 end if;
10211 -- A derived type inherits the Default_Initial_Condition pragma coming
10212 -- from any parent type within the derivation chain.
10214 if Has_DIC (Parent_Type) then
10215 Set_Has_Inherited_DIC (Derived_Type);
10216 end if;
10218 -- A derived type inherits any class-wide invariants coming from a
10219 -- parent type or an interface. Note that the invariant procedure of
10220 -- the parent type should not be inherited because the derived type may
10221 -- define invariants of its own.
10223 if not Is_Interface (Derived_Type) then
10224 if Has_Inherited_Invariants (Parent_Type)
10225 or else Has_Inheritable_Invariants (Parent_Type)
10226 then
10227 Set_Has_Inherited_Invariants (Derived_Type);
10229 elsif Is_Concurrent_Type (Derived_Type)
10230 or else Is_Tagged_Type (Derived_Type)
10231 then
10232 declare
10233 Iface : Entity_Id;
10234 Ifaces : Elist_Id;
10235 Iface_Elmt : Elmt_Id;
10237 begin
10238 Collect_Interfaces
10239 (T => Derived_Type,
10240 Ifaces_List => Ifaces,
10241 Exclude_Parents => True);
10243 if Present (Ifaces) then
10244 Iface_Elmt := First_Elmt (Ifaces);
10245 while Present (Iface_Elmt) loop
10246 Iface := Node (Iface_Elmt);
10248 if Has_Inheritable_Invariants (Iface) then
10249 Set_Has_Inherited_Invariants (Derived_Type);
10250 exit;
10251 end if;
10253 Next_Elmt (Iface_Elmt);
10254 end loop;
10255 end if;
10256 end;
10257 end if;
10258 end if;
10260 -- We similarly inherit predicates
10262 Inherit_Predicate_Flags (Derived_Type, Parent_Type, Only_Flags => True);
10264 -- The derived type inherits representation clauses from the parent
10265 -- type, and from any interfaces.
10267 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10269 declare
10270 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10271 begin
10272 while Present (Iface) loop
10273 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10274 Next (Iface);
10275 end loop;
10276 end;
10278 -- If the parent type has delayed rep aspects, then mark the derived
10279 -- type as possibly inheriting a delayed rep aspect.
10281 if Has_Delayed_Rep_Aspects (Parent_Type) then
10282 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10283 end if;
10285 -- A derived type becomes Ghost when its parent type is also Ghost
10286 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10287 -- directly inherited because the Ghost policy in effect may differ.
10289 if Is_Ghost_Entity (Parent_Type) then
10290 Set_Is_Ghost_Entity (Derived_Type);
10291 end if;
10293 -- Type dependent processing
10295 case Ekind (Parent_Type) is
10296 when Numeric_Kind =>
10297 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10299 when Array_Kind =>
10300 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10302 when Class_Wide_Kind
10303 | E_Record_Subtype
10304 | E_Record_Type
10306 Build_Derived_Record_Type
10307 (N, Parent_Type, Derived_Type, Derive_Subps);
10308 return;
10310 when Enumeration_Kind =>
10311 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10313 when Access_Kind =>
10314 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10316 when Incomplete_Or_Private_Kind =>
10317 Build_Derived_Private_Type
10318 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10320 -- For discriminated types, the derivation includes deriving
10321 -- primitive operations. For others it is done below.
10323 if Is_Tagged_Type (Parent_Type)
10324 or else Has_Discriminants (Parent_Type)
10325 or else (Present (Full_View (Parent_Type))
10326 and then Has_Discriminants (Full_View (Parent_Type)))
10327 then
10328 return;
10329 end if;
10331 when Concurrent_Kind =>
10332 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10334 when others =>
10335 raise Program_Error;
10336 end case;
10338 -- Nothing more to do if some error occurred
10340 if Etype (Derived_Type) = Any_Type then
10341 return;
10342 end if;
10344 -- If not already set, initialize the derived type's list of primitive
10345 -- operations to an empty element list.
10347 if No (Direct_Primitive_Operations (Derived_Type)) then
10348 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10350 -- If Etype of the derived type is the base type (as opposed to
10351 -- a parent type) and doesn't have an associated list of primitive
10352 -- operations, then set the base type's primitive list to the
10353 -- derived type's list. The lists need to be shared in common
10354 -- between the two.
10356 if Etype (Derived_Type) = Base_Type (Derived_Type)
10357 and then No (Direct_Primitive_Operations (Etype (Derived_Type)))
10358 then
10359 Set_Direct_Primitive_Operations
10360 (Etype (Derived_Type),
10361 Direct_Primitive_Operations (Derived_Type));
10362 end if;
10363 end if;
10365 -- Set delayed freeze and then derive subprograms, we need to do this
10366 -- in this order so that derived subprograms inherit the derived freeze
10367 -- if necessary.
10369 Set_Has_Delayed_Freeze (Derived_Type);
10371 if Derive_Subps then
10372 Derive_Subprograms (Parent_Type, Derived_Type);
10373 end if;
10375 Set_Has_Primitive_Operations
10376 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10377 end Build_Derived_Type;
10379 -----------------------
10380 -- Build_Discriminal --
10381 -----------------------
10383 procedure Build_Discriminal (Discrim : Entity_Id) is
10384 D_Minal : Entity_Id;
10385 CR_Disc : Entity_Id;
10387 begin
10388 -- A discriminal has the same name as the discriminant
10390 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10392 Mutate_Ekind (D_Minal, E_In_Parameter);
10393 Set_Mechanism (D_Minal, Default_Mechanism);
10394 Set_Etype (D_Minal, Etype (Discrim));
10395 Set_Scope (D_Minal, Current_Scope);
10396 Set_Parent (D_Minal, Parent (Discrim));
10398 Set_Discriminal (Discrim, D_Minal);
10399 Set_Discriminal_Link (D_Minal, Discrim);
10401 -- For task types, build at once the discriminants of the corresponding
10402 -- record, which are needed if discriminants are used in entry defaults
10403 -- and in family bounds.
10405 if Is_Concurrent_Type (Current_Scope)
10406 or else
10407 Is_Limited_Type (Current_Scope)
10408 then
10409 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10411 Mutate_Ekind (CR_Disc, E_In_Parameter);
10412 Set_Mechanism (CR_Disc, Default_Mechanism);
10413 Set_Etype (CR_Disc, Etype (Discrim));
10414 Set_Scope (CR_Disc, Current_Scope);
10415 Set_Discriminal_Link (CR_Disc, Discrim);
10416 Set_CR_Discriminant (Discrim, CR_Disc);
10417 end if;
10418 end Build_Discriminal;
10420 ------------------------------------
10421 -- Build_Discriminant_Constraints --
10422 ------------------------------------
10424 function Build_Discriminant_Constraints
10425 (T : Entity_Id;
10426 Def : Node_Id;
10427 Derived_Def : Boolean := False) return Elist_Id
10429 C : constant Node_Id := Constraint (Def);
10430 Nb_Discr : constant Nat := Number_Discriminants (T);
10432 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10433 -- Saves the expression corresponding to a given discriminant in T
10435 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10436 -- Return the Position number within array Discr_Expr of a discriminant
10437 -- D within the discriminant list of the discriminated type T.
10439 procedure Process_Discriminant_Expression
10440 (Expr : Node_Id;
10441 D : Entity_Id);
10442 -- If this is a discriminant constraint on a partial view, do not
10443 -- generate an overflow check on the discriminant expression. The check
10444 -- will be generated when constraining the full view. Otherwise the
10445 -- backend creates duplicate symbols for the temporaries corresponding
10446 -- to the expressions to be checked, causing spurious assembler errors.
10448 ------------------
10449 -- Pos_Of_Discr --
10450 ------------------
10452 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10453 Disc : Entity_Id;
10455 begin
10456 Disc := First_Discriminant (T);
10457 for J in Discr_Expr'Range loop
10458 if Disc = D then
10459 return J;
10460 end if;
10462 Next_Discriminant (Disc);
10463 end loop;
10465 -- Note: Since this function is called on discriminants that are
10466 -- known to belong to the discriminated type, falling through the
10467 -- loop with no match signals an internal compiler error.
10469 raise Program_Error;
10470 end Pos_Of_Discr;
10472 -------------------------------------
10473 -- Process_Discriminant_Expression --
10474 -------------------------------------
10476 procedure Process_Discriminant_Expression
10477 (Expr : Node_Id;
10478 D : Entity_Id)
10480 BDT : constant Entity_Id := Base_Type (Etype (D));
10482 begin
10483 -- If this is a discriminant constraint on a partial view, do
10484 -- not generate an overflow on the discriminant expression. The
10485 -- check will be generated when constraining the full view.
10487 if Is_Private_Type (T)
10488 and then Present (Full_View (T))
10489 then
10490 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10491 else
10492 Analyze_And_Resolve (Expr, BDT);
10493 end if;
10494 end Process_Discriminant_Expression;
10496 -- Declarations local to Build_Discriminant_Constraints
10498 Discr : Entity_Id;
10499 E : Entity_Id;
10500 Elist : constant Elist_Id := New_Elmt_List;
10502 Constr : Node_Id;
10503 Expr : Node_Id;
10504 Id : Node_Id;
10505 Position : Nat;
10506 Found : Boolean;
10508 Discrim_Present : Boolean := False;
10510 -- Start of processing for Build_Discriminant_Constraints
10512 begin
10513 -- The following loop will process positional associations only.
10514 -- For a positional association, the (single) discriminant is
10515 -- implicitly specified by position, in textual order (RM 3.7.2).
10517 Discr := First_Discriminant (T);
10518 Constr := First (Constraints (C));
10519 for D in Discr_Expr'Range loop
10520 exit when Nkind (Constr) = N_Discriminant_Association;
10522 if No (Constr) then
10523 Error_Msg_N ("too few discriminants given in constraint", C);
10524 return New_Elmt_List;
10526 elsif Nkind (Constr) = N_Range
10527 or else (Nkind (Constr) = N_Attribute_Reference
10528 and then Attribute_Name (Constr) = Name_Range)
10529 then
10530 Error_Msg_N
10531 ("a range is not a valid discriminant constraint", Constr);
10532 Discr_Expr (D) := Error;
10534 elsif Nkind (Constr) = N_Subtype_Indication then
10535 Error_Msg_N
10536 ("a subtype indication is not a valid discriminant constraint",
10537 Constr);
10538 Discr_Expr (D) := Error;
10540 else
10541 Process_Discriminant_Expression (Constr, Discr);
10542 Discr_Expr (D) := Constr;
10543 end if;
10545 Next_Discriminant (Discr);
10546 Next (Constr);
10547 end loop;
10549 if No (Discr) and then Present (Constr) then
10550 Error_Msg_N ("too many discriminants given in constraint", Constr);
10551 return New_Elmt_List;
10552 end if;
10554 -- Named associations can be given in any order, but if both positional
10555 -- and named associations are used in the same discriminant constraint,
10556 -- then positional associations must occur first, at their normal
10557 -- position. Hence once a named association is used, the rest of the
10558 -- discriminant constraint must use only named associations.
10560 while Present (Constr) loop
10562 -- Positional association forbidden after a named association
10564 if Nkind (Constr) /= N_Discriminant_Association then
10565 Error_Msg_N ("positional association follows named one", Constr);
10566 return New_Elmt_List;
10568 -- Otherwise it is a named association
10570 else
10571 -- E records the type of the discriminants in the named
10572 -- association. All the discriminants specified in the same name
10573 -- association must have the same type.
10575 E := Empty;
10577 -- Search the list of discriminants in T to see if the simple name
10578 -- given in the constraint matches any of them.
10580 Id := First (Selector_Names (Constr));
10581 while Present (Id) loop
10582 Found := False;
10584 -- If Original_Discriminant is present, we are processing a
10585 -- generic instantiation and this is an instance node. We need
10586 -- to find the name of the corresponding discriminant in the
10587 -- actual record type T and not the name of the discriminant in
10588 -- the generic formal. Example:
10590 -- generic
10591 -- type G (D : int) is private;
10592 -- package P is
10593 -- subtype W is G (D => 1);
10594 -- end package;
10595 -- type Rec (X : int) is record ... end record;
10596 -- package Q is new P (G => Rec);
10598 -- At the point of the instantiation, formal type G is Rec
10599 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10600 -- which really looks like "subtype W is Rec (D => 1);" at
10601 -- the point of instantiation, we want to find the discriminant
10602 -- that corresponds to D in Rec, i.e. X.
10604 if Present (Original_Discriminant (Id))
10605 and then In_Instance
10606 then
10607 Discr := Find_Corresponding_Discriminant (Id, T);
10608 Found := True;
10610 else
10611 Discr := First_Discriminant (T);
10612 while Present (Discr) loop
10613 if Chars (Discr) = Chars (Id) then
10614 Found := True;
10615 exit;
10616 end if;
10618 Next_Discriminant (Discr);
10619 end loop;
10621 if not Found then
10622 Error_Msg_N ("& does not match any discriminant", Id);
10623 return New_Elmt_List;
10625 -- If the parent type is a generic formal, preserve the
10626 -- name of the discriminant for subsequent instances.
10627 -- see comment at the beginning of this if statement.
10629 elsif Is_Generic_Type (Root_Type (T)) then
10630 Set_Original_Discriminant (Id, Discr);
10631 end if;
10632 end if;
10634 Position := Pos_Of_Discr (T, Discr);
10636 if Present (Discr_Expr (Position)) then
10637 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10639 else
10640 -- Each discriminant specified in the same named association
10641 -- must be associated with a separate copy of the
10642 -- corresponding expression.
10644 if Present (Next (Id)) then
10645 Expr := New_Copy_Tree (Expression (Constr));
10646 Set_Parent (Expr, Parent (Expression (Constr)));
10647 else
10648 Expr := Expression (Constr);
10649 end if;
10651 Discr_Expr (Position) := Expr;
10652 Process_Discriminant_Expression (Expr, Discr);
10653 end if;
10655 -- A discriminant association with more than one discriminant
10656 -- name is only allowed if the named discriminants are all of
10657 -- the same type (RM 3.7.1(8)).
10659 if E = Empty then
10660 E := Base_Type (Etype (Discr));
10662 elsif Base_Type (Etype (Discr)) /= E then
10663 Error_Msg_N
10664 ("all discriminants in an association " &
10665 "must have the same type", Id);
10666 end if;
10668 Next (Id);
10669 end loop;
10670 end if;
10672 Next (Constr);
10673 end loop;
10675 -- A discriminant constraint must provide exactly one value for each
10676 -- discriminant of the type (RM 3.7.1(8)).
10678 for J in Discr_Expr'Range loop
10679 if No (Discr_Expr (J)) then
10680 Error_Msg_N ("too few discriminants given in constraint", C);
10681 return New_Elmt_List;
10682 end if;
10683 end loop;
10685 -- Determine if there are discriminant expressions in the constraint
10687 for J in Discr_Expr'Range loop
10688 if Denotes_Discriminant
10689 (Discr_Expr (J), Check_Concurrent => True)
10690 then
10691 Discrim_Present := True;
10692 exit;
10693 end if;
10694 end loop;
10696 -- Build an element list consisting of the expressions given in the
10697 -- discriminant constraint and apply the appropriate checks. The list
10698 -- is constructed after resolving any named discriminant associations
10699 -- and therefore the expressions appear in the textual order of the
10700 -- discriminants.
10702 Discr := First_Discriminant (T);
10703 for J in Discr_Expr'Range loop
10704 if Discr_Expr (J) /= Error then
10705 Append_Elmt (Discr_Expr (J), Elist);
10707 -- If any of the discriminant constraints is given by a
10708 -- discriminant and we are in a derived type declaration we
10709 -- have a discriminant renaming. Establish link between new
10710 -- and old discriminant. The new discriminant has an implicit
10711 -- dereference if the old one does.
10713 if Denotes_Discriminant (Discr_Expr (J)) then
10714 if Derived_Def then
10715 declare
10716 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10718 begin
10719 Set_Corresponding_Discriminant (New_Discr, Discr);
10720 Set_Has_Implicit_Dereference (New_Discr,
10721 Has_Implicit_Dereference (Discr));
10722 end;
10723 end if;
10725 -- Force the evaluation of non-discriminant expressions.
10726 -- If we have found a discriminant in the constraint 3.4(26)
10727 -- and 3.8(18) demand that no range checks are performed are
10728 -- after evaluation. If the constraint is for a component
10729 -- definition that has a per-object constraint, expressions are
10730 -- evaluated but not checked either. In all other cases perform
10731 -- a range check.
10733 else
10734 if Discrim_Present then
10735 null;
10737 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10738 and then Has_Per_Object_Constraint
10739 (Defining_Identifier (Parent (Parent (Def))))
10740 then
10741 null;
10743 elsif Is_Access_Type (Etype (Discr)) then
10744 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10746 else
10747 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10748 end if;
10750 -- If the value of the discriminant may be visible in
10751 -- another unit or child unit, create an external name
10752 -- for it. We use the name of the object or component
10753 -- that carries the discriminated subtype. The code
10754 -- below may generate external symbols for the discriminant
10755 -- expression when not strictly needed, which is harmless.
10757 if Expander_Active
10758 and then Comes_From_Source (Def)
10759 and then not Is_Subprogram (Current_Scope)
10760 then
10761 declare
10762 Id : Entity_Id := Empty;
10763 begin
10764 if Nkind (Parent (Def)) = N_Object_Declaration then
10765 Id := Defining_Identifier (Parent (Def));
10767 elsif Nkind (Parent (Def)) = N_Component_Definition
10768 and then
10769 Nkind (Parent (Parent (Def)))
10770 = N_Component_Declaration
10771 then
10772 Id := Defining_Identifier (Parent (Parent (Def)));
10773 end if;
10775 if Present (Id) then
10776 Force_Evaluation (
10777 Discr_Expr (J),
10778 Related_Id => Id,
10779 Discr_Number => J);
10780 else
10781 Force_Evaluation (Discr_Expr (J));
10782 end if;
10783 end;
10784 else
10785 Force_Evaluation (Discr_Expr (J));
10786 end if;
10787 end if;
10789 -- Check that the designated type of an access discriminant's
10790 -- expression is not a class-wide type unless the discriminant's
10791 -- designated type is also class-wide.
10793 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10794 and then not Is_Class_Wide_Type
10795 (Designated_Type (Etype (Discr)))
10796 and then Etype (Discr_Expr (J)) /= Any_Type
10797 and then Is_Class_Wide_Type
10798 (Designated_Type (Etype (Discr_Expr (J))))
10799 then
10800 Wrong_Type (Discr_Expr (J), Etype (Discr));
10802 elsif Is_Access_Type (Etype (Discr))
10803 and then not Is_Access_Constant (Etype (Discr))
10804 and then Is_Access_Type (Etype (Discr_Expr (J)))
10805 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10806 then
10807 Error_Msg_NE
10808 ("constraint for discriminant& must be access to variable",
10809 Def, Discr);
10810 end if;
10811 end if;
10813 Next_Discriminant (Discr);
10814 end loop;
10816 return Elist;
10817 end Build_Discriminant_Constraints;
10819 ---------------------------------
10820 -- Build_Discriminated_Subtype --
10821 ---------------------------------
10823 procedure Build_Discriminated_Subtype
10824 (T : Entity_Id;
10825 Def_Id : Entity_Id;
10826 Elist : Elist_Id;
10827 Related_Nod : Node_Id;
10828 For_Access : Boolean := False)
10830 Has_Discrs : constant Boolean := Has_Discriminants (T);
10831 Constrained : constant Boolean :=
10832 (Has_Discrs
10833 and then not Is_Empty_Elmt_List (Elist)
10834 and then not Is_Class_Wide_Type (T))
10835 or else Is_Constrained (T);
10837 begin
10838 if Ekind (T) = E_Record_Type then
10839 Mutate_Ekind (Def_Id, E_Record_Subtype);
10841 -- Inherit preelaboration flag from base, for types for which it
10842 -- may have been set: records, private types, protected types.
10844 Set_Known_To_Have_Preelab_Init
10845 (Def_Id, Known_To_Have_Preelab_Init (T));
10847 elsif Ekind (T) = E_Task_Type then
10848 Mutate_Ekind (Def_Id, E_Task_Subtype);
10850 elsif Ekind (T) = E_Protected_Type then
10851 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10852 Set_Known_To_Have_Preelab_Init
10853 (Def_Id, Known_To_Have_Preelab_Init (T));
10855 elsif Is_Private_Type (T) then
10856 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10857 Set_Known_To_Have_Preelab_Init
10858 (Def_Id, Known_To_Have_Preelab_Init (T));
10860 -- Private subtypes may have private dependents
10862 Set_Private_Dependents (Def_Id, New_Elmt_List);
10864 elsif Is_Class_Wide_Type (T) then
10865 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10867 else
10868 -- Incomplete type. Attach subtype to list of dependents, to be
10869 -- completed with full view of parent type, unless is it the
10870 -- designated subtype of a record component within an init_proc.
10871 -- This last case arises for a component of an access type whose
10872 -- designated type is incomplete (e.g. a Taft Amendment type).
10873 -- The designated subtype is within an inner scope, and needs no
10874 -- elaboration, because only the access type is needed in the
10875 -- initialization procedure.
10877 if Ekind (T) = E_Incomplete_Type then
10878 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10879 else
10880 Mutate_Ekind (Def_Id, Ekind (T));
10881 end if;
10883 if For_Access and then Within_Init_Proc then
10884 null;
10885 else
10886 Append_Elmt (Def_Id, Private_Dependents (T));
10887 end if;
10888 end if;
10890 Set_Etype (Def_Id, T);
10891 Reinit_Size_Align (Def_Id);
10892 Set_Has_Discriminants (Def_Id, Has_Discrs);
10893 Set_Is_Constrained (Def_Id, Constrained);
10895 Set_First_Entity (Def_Id, First_Entity (T));
10896 Set_Last_Entity (Def_Id, Last_Entity (T));
10897 Set_Has_Implicit_Dereference
10898 (Def_Id, Has_Implicit_Dereference (T));
10899 Set_Has_Pragma_Unreferenced_Objects
10900 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10902 -- If the subtype is the completion of a private declaration, there may
10903 -- have been representation clauses for the partial view, and they must
10904 -- be preserved. Build_Derived_Type chains the inherited clauses with
10905 -- the ones appearing on the extension. If this comes from a subtype
10906 -- declaration, all clauses are inherited.
10908 if No (First_Rep_Item (Def_Id)) then
10909 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10910 end if;
10912 if Is_Tagged_Type (T) then
10913 Set_Is_Tagged_Type (Def_Id);
10914 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10915 Make_Class_Wide_Type (Def_Id);
10916 end if;
10918 Set_Stored_Constraint (Def_Id, No_Elist);
10920 if Has_Discrs then
10921 Set_Discriminant_Constraint (Def_Id, Elist);
10922 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10923 end if;
10925 if Is_Tagged_Type (T) then
10927 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10928 -- concurrent record type (which has the list of primitive
10929 -- operations).
10931 if Ada_Version >= Ada_2005
10932 and then Is_Concurrent_Type (T)
10933 then
10934 Set_Corresponding_Record_Type (Def_Id,
10935 Corresponding_Record_Type (T));
10936 else
10937 Set_Direct_Primitive_Operations (Def_Id,
10938 Direct_Primitive_Operations (T));
10939 end if;
10941 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10942 end if;
10944 -- Subtypes introduced by component declarations do not need to be
10945 -- marked as delayed, and do not get freeze nodes, because the semantics
10946 -- verifies that the parents of the subtypes are frozen before the
10947 -- enclosing record is frozen.
10949 if not Is_Type (Scope (Def_Id)) then
10950 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10952 if Is_Private_Type (T)
10953 and then Present (Full_View (T))
10954 then
10955 Conditional_Delay (Def_Id, Full_View (T));
10956 else
10957 Conditional_Delay (Def_Id, T);
10958 end if;
10959 end if;
10961 if Is_Record_Type (T) then
10962 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10964 if Has_Discrs
10965 and then not Is_Empty_Elmt_List (Elist)
10966 and then not For_Access
10967 then
10968 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10970 elsif not Is_Private_Type (T) then
10971 Set_Cloned_Subtype (Def_Id, T);
10972 end if;
10973 end if;
10974 end Build_Discriminated_Subtype;
10976 ---------------------------
10977 -- Build_Itype_Reference --
10978 ---------------------------
10980 procedure Build_Itype_Reference
10981 (Ityp : Entity_Id;
10982 Nod : Node_Id)
10984 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10985 begin
10987 -- Itype references are only created for use by the back-end
10989 if Inside_A_Generic then
10990 return;
10991 else
10992 Set_Itype (IR, Ityp);
10994 -- If Nod is a library unit entity, then Insert_After won't work,
10995 -- because Nod is not a member of any list. Therefore, we use
10996 -- Add_Global_Declaration in this case. This can happen if we have a
10997 -- build-in-place library function, child unit or not.
10999 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
11000 or else (Nkind (Nod) in
11001 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
11002 and then Is_Compilation_Unit (Defining_Entity (Nod)))
11003 then
11004 Add_Global_Declaration (IR);
11005 else
11006 Insert_After (Nod, IR);
11007 end if;
11008 end if;
11009 end Build_Itype_Reference;
11011 ------------------------
11012 -- Build_Scalar_Bound --
11013 ------------------------
11015 function Build_Scalar_Bound
11016 (Bound : Node_Id;
11017 Par_T : Entity_Id;
11018 Der_T : Entity_Id) return Node_Id
11020 New_Bound : Entity_Id;
11022 begin
11023 -- Note: not clear why this is needed, how can the original bound
11024 -- be unanalyzed at this point? and if it is, what business do we
11025 -- have messing around with it? and why is the base type of the
11026 -- parent type the right type for the resolution. It probably is
11027 -- not. It is OK for the new bound we are creating, but not for
11028 -- the old one??? Still if it never happens, no problem.
11030 Analyze_And_Resolve (Bound, Base_Type (Par_T));
11032 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
11033 New_Bound := New_Copy (Bound);
11034 Set_Etype (New_Bound, Der_T);
11035 Set_Analyzed (New_Bound);
11037 elsif Is_Entity_Name (Bound) then
11038 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
11040 -- The following is almost certainly wrong. What business do we have
11041 -- relocating a node (Bound) that is presumably still attached to
11042 -- the tree elsewhere???
11044 else
11045 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
11046 end if;
11048 Set_Etype (New_Bound, Der_T);
11049 return New_Bound;
11050 end Build_Scalar_Bound;
11052 -------------------------------
11053 -- Check_Abstract_Overriding --
11054 -------------------------------
11056 procedure Check_Abstract_Overriding (T : Entity_Id) is
11057 Alias_Subp : Entity_Id;
11058 Elmt : Elmt_Id;
11059 Op_List : Elist_Id;
11060 Subp : Entity_Id;
11061 Type_Def : Node_Id;
11063 procedure Check_Pragma_Implemented (Subp : Entity_Id);
11064 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
11065 -- which has pragma Implemented already set. Check whether Subp's entity
11066 -- kind conforms to the implementation kind of the overridden routine.
11068 procedure Check_Pragma_Implemented
11069 (Subp : Entity_Id;
11070 Iface_Subp : Entity_Id);
11071 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
11072 -- Iface_Subp and both entities have pragma Implemented already set on
11073 -- them. Check whether the two implementation kinds are conforming.
11075 procedure Inherit_Pragma_Implemented
11076 (Subp : Entity_Id;
11077 Iface_Subp : Entity_Id);
11078 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
11079 -- subprogram Iface_Subp which has been marked by pragma Implemented.
11080 -- Propagate the implementation kind of Iface_Subp to Subp.
11082 ------------------------------
11083 -- Check_Pragma_Implemented --
11084 ------------------------------
11086 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
11087 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
11088 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
11089 Subp_Alias : constant Entity_Id := Alias (Subp);
11090 Contr_Typ : Entity_Id;
11091 Impl_Subp : Entity_Id;
11093 begin
11094 -- Subp must have an alias since it is a hidden entity used to link
11095 -- an interface subprogram to its overriding counterpart.
11097 pragma Assert (Present (Subp_Alias));
11099 -- Handle aliases to synchronized wrappers
11101 Impl_Subp := Subp_Alias;
11103 if Is_Primitive_Wrapper (Impl_Subp) then
11104 Impl_Subp := Wrapped_Entity (Impl_Subp);
11105 end if;
11107 -- Extract the type of the controlling formal
11109 Contr_Typ := Etype (First_Formal (Subp_Alias));
11111 if Is_Concurrent_Record_Type (Contr_Typ) then
11112 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
11113 end if;
11115 -- An interface subprogram whose implementation kind is By_Entry must
11116 -- be implemented by an entry.
11118 if Impl_Kind = Name_By_Entry
11119 and then Ekind (Impl_Subp) /= E_Entry
11120 then
11121 Error_Msg_Node_2 := Iface_Alias;
11122 Error_Msg_NE
11123 ("type & must implement abstract subprogram & with an entry",
11124 Subp_Alias, Contr_Typ);
11126 elsif Impl_Kind = Name_By_Protected_Procedure then
11128 -- An interface subprogram whose implementation kind is By_
11129 -- Protected_Procedure cannot be implemented by a primitive
11130 -- procedure of a task type.
11132 if Ekind (Contr_Typ) /= E_Protected_Type then
11133 Error_Msg_Node_2 := Contr_Typ;
11134 Error_Msg_NE
11135 ("interface subprogram & cannot be implemented by a "
11136 & "primitive procedure of task type &",
11137 Subp_Alias, Iface_Alias);
11139 -- An interface subprogram whose implementation kind is By_
11140 -- Protected_Procedure must be implemented by a procedure.
11142 elsif Ekind (Impl_Subp) /= E_Procedure then
11143 Error_Msg_Node_2 := Iface_Alias;
11144 Error_Msg_NE
11145 ("type & must implement abstract subprogram & with a "
11146 & "procedure", Subp_Alias, Contr_Typ);
11148 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11149 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11150 then
11151 Error_Msg_Name_1 := Impl_Kind;
11152 Error_Msg_N
11153 ("overriding operation& must have synchronization%",
11154 Subp_Alias);
11155 end if;
11157 -- If primitive has Optional synchronization, overriding operation
11158 -- must match if it has an explicit synchronization.
11160 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11161 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11162 then
11163 Error_Msg_Name_1 := Impl_Kind;
11164 Error_Msg_N
11165 ("overriding operation& must have synchronization%", Subp_Alias);
11166 end if;
11167 end Check_Pragma_Implemented;
11169 ------------------------------
11170 -- Check_Pragma_Implemented --
11171 ------------------------------
11173 procedure Check_Pragma_Implemented
11174 (Subp : Entity_Id;
11175 Iface_Subp : Entity_Id)
11177 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11178 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11180 begin
11181 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11182 -- and overriding subprogram are different. In general this is an
11183 -- error except when the implementation kind of the overridden
11184 -- subprograms is By_Any or Optional.
11186 if Iface_Kind /= Subp_Kind
11187 and then Iface_Kind /= Name_By_Any
11188 and then Iface_Kind /= Name_Optional
11189 then
11190 if Iface_Kind = Name_By_Entry then
11191 Error_Msg_N
11192 ("incompatible implementation kind, overridden subprogram " &
11193 "is marked By_Entry", Subp);
11194 else
11195 Error_Msg_N
11196 ("incompatible implementation kind, overridden subprogram " &
11197 "is marked By_Protected_Procedure", Subp);
11198 end if;
11199 end if;
11200 end Check_Pragma_Implemented;
11202 --------------------------------
11203 -- Inherit_Pragma_Implemented --
11204 --------------------------------
11206 procedure Inherit_Pragma_Implemented
11207 (Subp : Entity_Id;
11208 Iface_Subp : Entity_Id)
11210 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11211 Loc : constant Source_Ptr := Sloc (Subp);
11212 Impl_Prag : Node_Id;
11214 begin
11215 -- Since the implementation kind is stored as a representation item
11216 -- rather than a flag, create a pragma node.
11218 Impl_Prag :=
11219 Make_Pragma (Loc,
11220 Chars => Name_Implemented,
11221 Pragma_Argument_Associations => New_List (
11222 Make_Pragma_Argument_Association (Loc,
11223 Expression => New_Occurrence_Of (Subp, Loc)),
11225 Make_Pragma_Argument_Association (Loc,
11226 Expression => Make_Identifier (Loc, Iface_Kind))));
11228 -- The pragma doesn't need to be analyzed because it is internally
11229 -- built. It is safe to directly register it as a rep item since we
11230 -- are only interested in the characters of the implementation kind.
11232 Record_Rep_Item (Subp, Impl_Prag);
11233 end Inherit_Pragma_Implemented;
11235 -- Start of processing for Check_Abstract_Overriding
11237 begin
11238 Op_List := Primitive_Operations (T);
11240 -- Loop to check primitive operations
11242 Elmt := First_Elmt (Op_List);
11243 while Present (Elmt) loop
11244 Subp := Node (Elmt);
11245 Alias_Subp := Alias (Subp);
11247 -- If the parent type is untagged, then no overriding error checks
11248 -- are needed (such as in the case of an implicit full type for
11249 -- a derived type whose parent is an untagged private type with
11250 -- a tagged full type).
11252 if not Is_Tagged_Type (Etype (T)) then
11253 null;
11255 -- Inherited subprograms are identified by the fact that they do not
11256 -- come from source, and the associated source location is the
11257 -- location of the first subtype of the derived type.
11259 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11260 -- subprograms that "require overriding".
11262 -- Special exception, do not complain about failure to override the
11263 -- stream routines _Input and _Output, as well as the primitive
11264 -- operations used in dispatching selects since we always provide
11265 -- automatic overridings for these subprograms.
11267 -- The partial view of T may have been a private extension, for
11268 -- which inherited functions dispatching on result are abstract.
11269 -- If the full view is a null extension, there is no need for
11270 -- overriding in Ada 2005, but wrappers need to be built for them
11271 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11273 elsif Is_Null_Extension (T)
11274 and then Has_Controlling_Result (Subp)
11275 and then Ada_Version >= Ada_2005
11276 and then Present (Alias_Subp)
11277 and then not Comes_From_Source (Subp)
11278 and then not Is_Abstract_Subprogram (Alias_Subp)
11279 and then not Is_Access_Type (Etype (Subp))
11280 then
11281 null;
11283 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11284 -- processing because this check is done with the aliased
11285 -- entity
11287 elsif Present (Interface_Alias (Subp)) then
11288 null;
11290 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11291 -- of a visible private primitive inherited from an ancestor with
11292 -- the aspect Type_Invariant'Class, unless the inherited primitive
11293 -- is abstract.
11295 elsif not Is_Abstract_Subprogram (Subp)
11296 and then not Comes_From_Source (Subp) -- An inherited subprogram
11297 and then Requires_Overriding (Subp)
11298 and then Present (Alias_Subp)
11299 and then Has_Invariants (Etype (T))
11300 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11301 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11302 and then Is_Private_Primitive (Alias_Subp)
11303 then
11304 Error_Msg_NE
11305 ("inherited private primitive & must be overridden", T, Subp);
11306 Error_Msg_N
11307 ("\because ancestor type has 'Type_'Invariant''Class " &
11308 "(RM 7.3.2(6.1))", T);
11310 elsif (Is_Abstract_Subprogram (Subp)
11311 or else Requires_Overriding (Subp)
11312 or else
11313 (Has_Controlling_Result (Subp)
11314 and then Present (Alias_Subp)
11315 and then not Comes_From_Source (Subp)
11316 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11317 and then not Is_TSS (Subp, TSS_Stream_Input)
11318 and then not Is_TSS (Subp, TSS_Stream_Output)
11319 and then not Is_Abstract_Type (T)
11320 and then not Is_Predefined_Interface_Primitive (Subp)
11322 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11323 -- with abstract interface types because the check will be done
11324 -- with the aliased entity (otherwise we generate a duplicated
11325 -- error message).
11327 and then No (Interface_Alias (Subp))
11328 then
11329 if Present (Alias_Subp) then
11331 -- Only perform the check for a derived subprogram when the
11332 -- type has an explicit record extension. This avoids incorrect
11333 -- flagging of abstract subprograms for the case of a type
11334 -- without an extension that is derived from a formal type
11335 -- with a tagged actual (can occur within a private part).
11337 -- Ada 2005 (AI-391): In the case of an inherited function with
11338 -- a controlling result of the type, the rule does not apply if
11339 -- the type is a null extension (unless the parent function
11340 -- itself is abstract, in which case the function must still be
11341 -- be overridden). The expander will generate an overriding
11342 -- wrapper function calling the parent subprogram (see
11343 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11345 Type_Def := Type_Definition (Parent (T));
11347 if Nkind (Type_Def) = N_Derived_Type_Definition
11348 and then Present (Record_Extension_Part (Type_Def))
11349 and then
11350 (Ada_Version < Ada_2005
11351 or else not Is_Null_Extension (T)
11352 or else Ekind (Subp) = E_Procedure
11353 or else not Has_Controlling_Result (Subp)
11354 or else Is_Abstract_Subprogram (Alias_Subp)
11355 or else Requires_Overriding (Subp)
11356 or else Is_Access_Type (Etype (Subp)))
11357 then
11358 -- Avoid reporting error in case of abstract predefined
11359 -- primitive inherited from interface type because the
11360 -- body of internally generated predefined primitives
11361 -- of tagged types are generated later by Freeze_Type
11363 if Is_Interface (Root_Type (T))
11364 and then Is_Abstract_Subprogram (Subp)
11365 and then Is_Predefined_Dispatching_Operation (Subp)
11366 and then not Comes_From_Source (Ultimate_Alias (Subp))
11367 then
11368 null;
11370 -- A null extension is not obliged to override an inherited
11371 -- procedure subject to pragma Extensions_Visible with value
11372 -- False and at least one controlling OUT parameter
11373 -- (SPARK RM 6.1.7(6)).
11375 elsif Is_Null_Extension (T)
11376 and then Is_EVF_Procedure (Subp)
11377 then
11378 null;
11380 -- Subprogram renamings cannot be overridden
11382 elsif Comes_From_Source (Subp)
11383 and then Present (Alias (Subp))
11384 then
11385 null;
11387 -- Skip reporting the error on Ada 2022 only subprograms
11388 -- that require overriding if we are not in Ada 2022 mode.
11390 elsif Ada_Version < Ada_2022
11391 and then Requires_Overriding (Subp)
11392 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11393 then
11394 null;
11396 else
11397 Error_Msg_NE
11398 ("type must be declared abstract or & overridden",
11399 T, Subp);
11401 -- Traverse the whole chain of aliased subprograms to
11402 -- complete the error notification. This is especially
11403 -- useful for traceability of the chain of entities when
11404 -- the subprogram corresponds with an interface
11405 -- subprogram (which may be defined in another package).
11407 if Present (Alias_Subp) then
11408 declare
11409 E : Entity_Id;
11411 begin
11412 E := Subp;
11413 while Present (Alias (E)) loop
11415 -- Avoid reporting redundant errors on entities
11416 -- inherited from interfaces
11418 if Sloc (E) /= Sloc (T) then
11419 Error_Msg_Sloc := Sloc (E);
11420 Error_Msg_NE
11421 ("\& has been inherited #", T, Subp);
11422 end if;
11424 E := Alias (E);
11425 end loop;
11427 Error_Msg_Sloc := Sloc (E);
11429 -- AI05-0068: report if there is an overriding
11430 -- non-abstract subprogram that is invisible.
11432 if Is_Hidden (E)
11433 and then not Is_Abstract_Subprogram (E)
11434 then
11435 Error_Msg_NE
11436 ("\& subprogram# is not visible",
11437 T, Subp);
11439 -- Clarify the case where a non-null extension must
11440 -- override inherited procedure subject to pragma
11441 -- Extensions_Visible with value False and at least
11442 -- one controlling OUT param.
11444 elsif Is_EVF_Procedure (E) then
11445 Error_Msg_NE
11446 ("\& # is subject to Extensions_Visible False",
11447 T, Subp);
11449 else
11450 Error_Msg_NE
11451 ("\& has been inherited from subprogram #",
11452 T, Subp);
11453 end if;
11454 end;
11455 end if;
11456 end if;
11458 -- Ada 2005 (AI-345): Protected or task type implementing
11459 -- abstract interfaces.
11461 elsif Is_Concurrent_Record_Type (T)
11462 and then Present (Interfaces (T))
11463 then
11464 -- There is no need to check here RM 9.4(11.9/3) since we
11465 -- are processing the corresponding record type and the
11466 -- mode of the overriding subprograms was verified by
11467 -- Check_Conformance when the corresponding concurrent
11468 -- type declaration was analyzed.
11470 Error_Msg_NE
11471 ("interface subprogram & must be overridden", T, Subp);
11473 -- Examine primitive operations of synchronized type to find
11474 -- homonyms that have the wrong profile.
11476 declare
11477 Prim : Entity_Id;
11479 begin
11480 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11481 while Present (Prim) loop
11482 if Chars (Prim) = Chars (Subp) then
11483 Error_Msg_NE
11484 ("profile is not type conformant with prefixed "
11485 & "view profile of inherited operation&",
11486 Prim, Subp);
11487 end if;
11489 Next_Entity (Prim);
11490 end loop;
11491 end;
11492 end if;
11494 else
11495 Error_Msg_Node_2 := T;
11496 Error_Msg_N
11497 ("abstract subprogram& not allowed for type&", Subp);
11499 -- Also post unconditional warning on the type (unconditional
11500 -- so that if there are more than one of these cases, we get
11501 -- them all, and not just the first one).
11503 Error_Msg_Node_2 := Subp;
11504 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11505 end if;
11507 -- A subprogram subject to pragma Extensions_Visible with value
11508 -- "True" cannot override a subprogram subject to the same pragma
11509 -- with value "False" (SPARK RM 6.1.7(5)).
11511 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11512 and then Present (Overridden_Operation (Subp))
11513 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11514 Extensions_Visible_False
11515 then
11516 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11517 Error_Msg_N
11518 ("subprogram & with Extensions_Visible True cannot override "
11519 & "subprogram # with Extensions_Visible False", Subp);
11520 end if;
11522 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11524 -- Subp is an expander-generated procedure which maps an interface
11525 -- alias to a protected wrapper. The interface alias is flagged by
11526 -- pragma Implemented. Ensure that Subp is a procedure when the
11527 -- implementation kind is By_Protected_Procedure or an entry when
11528 -- By_Entry.
11530 if Ada_Version >= Ada_2012
11531 and then Is_Hidden (Subp)
11532 and then Present (Interface_Alias (Subp))
11533 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11534 then
11535 Check_Pragma_Implemented (Subp);
11536 end if;
11538 -- Subp is an interface primitive which overrides another interface
11539 -- primitive marked with pragma Implemented.
11541 if Ada_Version >= Ada_2012
11542 and then Present (Overridden_Operation (Subp))
11543 and then Has_Rep_Pragma
11544 (Overridden_Operation (Subp), Name_Implemented)
11545 then
11546 -- If the overriding routine is also marked by Implemented, check
11547 -- that the two implementation kinds are conforming.
11549 if Has_Rep_Pragma (Subp, Name_Implemented) then
11550 Check_Pragma_Implemented
11551 (Subp => Subp,
11552 Iface_Subp => Overridden_Operation (Subp));
11554 -- Otherwise the overriding routine inherits the implementation
11555 -- kind from the overridden subprogram.
11557 else
11558 Inherit_Pragma_Implemented
11559 (Subp => Subp,
11560 Iface_Subp => Overridden_Operation (Subp));
11561 end if;
11562 end if;
11564 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11565 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11566 -- for procedures, since this is our pragma.
11568 if Present (Overridden_Operation (Subp))
11569 and then No_Return (Overridden_Operation (Subp))
11570 then
11572 -- If the subprogram is a renaming, check that the renamed
11573 -- subprogram is No_Return.
11575 if Present (Renamed_Or_Alias (Subp)) then
11576 if not No_Return (Renamed_Or_Alias (Subp)) then
11577 Error_Msg_NE ("subprogram & must be No_Return",
11578 Subp,
11579 Renamed_Or_Alias (Subp));
11580 Error_Msg_N ("\since renaming & overrides No_Return "
11581 & "subprogram (RM 6.5.1(6/2))",
11582 Subp);
11583 end if;
11585 -- Make sure that the subprogram itself is No_Return.
11587 elsif not No_Return (Subp) then
11588 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11589 Error_Msg_N
11590 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11591 Subp);
11592 end if;
11593 end if;
11595 -- If the operation is a wrapper for a synchronized primitive, it
11596 -- may be called indirectly through a dispatching select. We assume
11597 -- that it will be referenced elsewhere indirectly, and suppress
11598 -- warnings about an unused entity.
11600 if Is_Primitive_Wrapper (Subp)
11601 and then Present (Wrapped_Entity (Subp))
11602 then
11603 Set_Referenced (Wrapped_Entity (Subp));
11604 end if;
11606 Next_Elmt (Elmt);
11607 end loop;
11608 end Check_Abstract_Overriding;
11610 ------------------------------------------------
11611 -- Check_Access_Discriminant_Requires_Limited --
11612 ------------------------------------------------
11614 procedure Check_Access_Discriminant_Requires_Limited
11615 (D : Node_Id;
11616 Loc : Node_Id)
11618 begin
11619 -- A discriminant_specification for an access discriminant shall appear
11620 -- only in the declaration for a task or protected type, or for a type
11621 -- with the reserved word 'limited' in its definition or in one of its
11622 -- ancestors (RM 3.7(10)).
11624 -- AI-0063: The proper condition is that type must be immutably limited,
11625 -- or else be a partial view.
11627 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11628 if Is_Inherently_Limited_Type (Current_Scope)
11629 or else
11630 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11631 and then Limited_Present (Parent (Current_Scope)))
11632 then
11633 null;
11635 else
11636 Error_Msg_N
11637 ("access discriminants allowed only for limited types", Loc);
11638 end if;
11639 end if;
11640 end Check_Access_Discriminant_Requires_Limited;
11642 -----------------------------------
11643 -- Check_Aliased_Component_Types --
11644 -----------------------------------
11646 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11647 C : Entity_Id;
11649 begin
11650 -- ??? Also need to check components of record extensions, but not
11651 -- components of protected types (which are always limited).
11653 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11654 -- types to be unconstrained. This is safe because it is illegal to
11655 -- create access subtypes to such types with explicit discriminant
11656 -- constraints.
11658 if not Is_Limited_Type (T) then
11659 if Ekind (T) = E_Record_Type then
11660 C := First_Component (T);
11661 while Present (C) loop
11662 if Is_Aliased (C)
11663 and then Has_Discriminants (Etype (C))
11664 and then not Is_Constrained (Etype (C))
11665 and then not In_Instance_Body
11666 and then Ada_Version < Ada_2005
11667 then
11668 Error_Msg_N
11669 ("aliased component must be constrained (RM 3.6(11))",
11671 end if;
11673 Next_Component (C);
11674 end loop;
11676 elsif Ekind (T) = E_Array_Type then
11677 if Has_Aliased_Components (T)
11678 and then Has_Discriminants (Component_Type (T))
11679 and then not Is_Constrained (Component_Type (T))
11680 and then not In_Instance_Body
11681 and then Ada_Version < Ada_2005
11682 then
11683 Error_Msg_N
11684 ("aliased component type must be constrained (RM 3.6(11))",
11686 end if;
11687 end if;
11688 end if;
11689 end Check_Aliased_Component_Types;
11691 --------------------------------------
11692 -- Check_Anonymous_Access_Component --
11693 --------------------------------------
11695 procedure Check_Anonymous_Access_Component
11696 (Typ_Decl : Node_Id;
11697 Typ : Entity_Id;
11698 Prev : Entity_Id;
11699 Comp_Def : Node_Id;
11700 Access_Def : Node_Id)
11702 Loc : constant Source_Ptr := Sloc (Comp_Def);
11703 Anon_Access : Entity_Id;
11704 Acc_Def : Node_Id;
11705 Decl : Node_Id;
11706 Type_Def : Node_Id;
11708 procedure Build_Incomplete_Type_Declaration;
11709 -- If the record type contains components that include an access to the
11710 -- current record, then create an incomplete type declaration for the
11711 -- record, to be used as the designated type of the anonymous access.
11712 -- This is done only once, and only if there is no previous partial
11713 -- view of the type.
11715 function Designates_T (Subt : Node_Id) return Boolean;
11716 -- Check whether a node designates the enclosing record type, or 'Class
11717 -- of that type
11719 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11720 -- Check whether an access definition includes a reference to
11721 -- the enclosing record type. The reference can be a subtype mark
11722 -- in the access definition itself, a 'Class attribute reference, or
11723 -- recursively a reference appearing in a parameter specification
11724 -- or result definition of an access_to_subprogram definition.
11726 --------------------------------------
11727 -- Build_Incomplete_Type_Declaration --
11728 --------------------------------------
11730 procedure Build_Incomplete_Type_Declaration is
11731 Decl : Node_Id;
11732 Inc_T : Entity_Id;
11733 H : Entity_Id;
11735 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11736 -- it's "is new ... with record" or else "is tagged record ...".
11738 Typ_Def : constant Node_Id :=
11739 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11740 then Type_Definition (Typ_Decl) else Empty);
11741 Is_Tagged : constant Boolean :=
11742 Present (Typ_Def)
11743 and then
11744 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11745 and then
11746 Present (Record_Extension_Part (Typ_Def)))
11747 or else
11748 (Nkind (Typ_Def) = N_Record_Definition
11749 and then Tagged_Present (Typ_Def)));
11751 begin
11752 -- If there is a previous partial view, no need to create a new one
11753 -- If the partial view, given by Prev, is incomplete, If Prev is
11754 -- a private declaration, full declaration is flagged accordingly.
11756 if Prev /= Typ then
11757 if Is_Tagged then
11758 Make_Class_Wide_Type (Prev);
11759 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11760 Set_Etype (Class_Wide_Type (Typ), Typ);
11761 end if;
11763 return;
11765 elsif Has_Private_Declaration (Typ) then
11767 -- If we refer to T'Class inside T, and T is the completion of a
11768 -- private type, then make sure the class-wide type exists.
11770 if Is_Tagged then
11771 Make_Class_Wide_Type (Typ);
11772 end if;
11774 return;
11776 -- If there was a previous anonymous access type, the incomplete
11777 -- type declaration will have been created already.
11779 elsif Present (Current_Entity (Typ))
11780 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11781 and then Full_View (Current_Entity (Typ)) = Typ
11782 then
11783 if Is_Tagged
11784 and then Comes_From_Source (Current_Entity (Typ))
11785 and then not Is_Tagged_Type (Current_Entity (Typ))
11786 then
11787 Make_Class_Wide_Type (Typ);
11788 Error_Msg_N
11789 ("incomplete view of tagged type should be declared tagged??",
11790 Parent (Current_Entity (Typ)));
11791 end if;
11792 return;
11794 else
11795 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11796 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11798 -- Type has already been inserted into the current scope. Remove
11799 -- it, and add incomplete declaration for type, so that subsequent
11800 -- anonymous access types can use it. The entity is unchained from
11801 -- the homonym list and from immediate visibility. After analysis,
11802 -- the entity in the incomplete declaration becomes immediately
11803 -- visible in the record declaration that follows.
11805 H := Current_Entity (Typ);
11807 if H = Typ then
11808 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11810 else
11811 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11812 H := Homonym (Typ);
11813 end loop;
11815 Set_Homonym (H, Homonym (Typ));
11816 end if;
11818 Insert_Before (Typ_Decl, Decl);
11819 Analyze (Decl);
11820 Set_Full_View (Inc_T, Typ);
11821 Set_Incomplete_View (Typ_Decl, Inc_T);
11823 -- If the type is tagged, create a common class-wide type for
11824 -- both views, and set the Etype of the class-wide type to the
11825 -- full view.
11827 if Is_Tagged then
11828 Make_Class_Wide_Type (Inc_T);
11829 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11830 Set_Etype (Class_Wide_Type (Typ), Typ);
11831 end if;
11833 -- If the scope is a package with a limited view, create a shadow
11834 -- entity for the incomplete type like Build_Limited_Views, so as
11835 -- to make it possible for Remove_Limited_With_Unit to reinstall
11836 -- this incomplete type as the visible entity.
11838 if Ekind (Scope (Inc_T)) = E_Package
11839 and then Present (Limited_View (Scope (Inc_T)))
11840 then
11841 declare
11842 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11844 begin
11845 -- This is modeled on Build_Shadow_Entity
11847 Set_Chars (Shadow, Chars (Inc_T));
11848 Set_Parent (Shadow, Decl);
11849 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
11850 Set_Is_Internal (Shadow);
11851 Set_From_Limited_With (Shadow);
11852 Set_Non_Limited_View (Shadow, Inc_T);
11853 Set_Private_Dependents (Shadow, New_Elmt_List);
11855 if Is_Tagged then
11856 Set_Non_Limited_View
11857 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
11858 end if;
11860 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
11861 end;
11862 end if;
11863 end if;
11864 end Build_Incomplete_Type_Declaration;
11866 ------------------
11867 -- Designates_T --
11868 ------------------
11870 function Designates_T (Subt : Node_Id) return Boolean is
11871 Type_Id : constant Name_Id := Chars (Typ);
11873 function Names_T (Nam : Node_Id) return Boolean;
11874 -- The record type has not been introduced in the current scope
11875 -- yet, so we must examine the name of the type itself, either
11876 -- an identifier T, or an expanded name of the form P.T, where
11877 -- P denotes the current scope.
11879 -------------
11880 -- Names_T --
11881 -------------
11883 function Names_T (Nam : Node_Id) return Boolean is
11884 begin
11885 if Nkind (Nam) = N_Identifier then
11886 return Chars (Nam) = Type_Id;
11888 elsif Nkind (Nam) = N_Selected_Component then
11889 if Chars (Selector_Name (Nam)) = Type_Id then
11890 if Nkind (Prefix (Nam)) = N_Identifier then
11891 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11893 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11894 return Chars (Selector_Name (Prefix (Nam))) =
11895 Chars (Current_Scope);
11896 else
11897 return False;
11898 end if;
11900 else
11901 return False;
11902 end if;
11904 else
11905 return False;
11906 end if;
11907 end Names_T;
11909 -- Start of processing for Designates_T
11911 begin
11912 if Nkind (Subt) = N_Identifier then
11913 return Chars (Subt) = Type_Id;
11915 -- Reference can be through an expanded name which has not been
11916 -- analyzed yet, and which designates enclosing scopes.
11918 elsif Nkind (Subt) = N_Selected_Component then
11919 if Names_T (Subt) then
11920 return True;
11922 -- Otherwise it must denote an entity that is already visible.
11923 -- The access definition may name a subtype of the enclosing
11924 -- type, if there is a previous incomplete declaration for it.
11926 else
11927 Find_Selected_Component (Subt);
11928 return
11929 Is_Entity_Name (Subt)
11930 and then Scope (Entity (Subt)) = Current_Scope
11931 and then
11932 (Chars (Base_Type (Entity (Subt))) = Type_Id
11933 or else
11934 (Is_Class_Wide_Type (Entity (Subt))
11935 and then
11936 Chars (Etype (Base_Type (Entity (Subt)))) =
11937 Type_Id));
11938 end if;
11940 -- A reference to the current type may appear as the prefix of
11941 -- a 'Class attribute.
11943 elsif Nkind (Subt) = N_Attribute_Reference
11944 and then Attribute_Name (Subt) = Name_Class
11945 then
11946 return Names_T (Prefix (Subt));
11948 else
11949 return False;
11950 end if;
11951 end Designates_T;
11953 ----------------
11954 -- Mentions_T --
11955 ----------------
11957 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11958 Param_Spec : Node_Id;
11960 Acc_Subprg : constant Node_Id :=
11961 Access_To_Subprogram_Definition (Acc_Def);
11963 begin
11964 if No (Acc_Subprg) then
11965 return Designates_T (Subtype_Mark (Acc_Def));
11966 end if;
11968 -- Component is an access_to_subprogram: examine its formals,
11969 -- and result definition in the case of an access_to_function.
11971 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11972 while Present (Param_Spec) loop
11973 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11974 and then Mentions_T (Parameter_Type (Param_Spec))
11975 then
11976 return True;
11978 elsif Designates_T (Parameter_Type (Param_Spec)) then
11979 return True;
11980 end if;
11982 Next (Param_Spec);
11983 end loop;
11985 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11986 if Nkind (Result_Definition (Acc_Subprg)) =
11987 N_Access_Definition
11988 then
11989 return Mentions_T (Result_Definition (Acc_Subprg));
11990 else
11991 return Designates_T (Result_Definition (Acc_Subprg));
11992 end if;
11993 end if;
11995 return False;
11996 end Mentions_T;
11998 -- Start of processing for Check_Anonymous_Access_Component
12000 begin
12001 if Present (Access_Def) and then Mentions_T (Access_Def) then
12002 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
12004 Build_Incomplete_Type_Declaration;
12005 Anon_Access := Make_Temporary (Loc, 'S');
12007 -- Create a declaration for the anonymous access type: either
12008 -- an access_to_object or an access_to_subprogram.
12010 if Present (Acc_Def) then
12011 if Nkind (Acc_Def) = N_Access_Function_Definition then
12012 Type_Def :=
12013 Make_Access_Function_Definition (Loc,
12014 Parameter_Specifications =>
12015 Parameter_Specifications (Acc_Def),
12016 Result_Definition => Result_Definition (Acc_Def));
12017 else
12018 Type_Def :=
12019 Make_Access_Procedure_Definition (Loc,
12020 Parameter_Specifications =>
12021 Parameter_Specifications (Acc_Def));
12022 end if;
12024 else
12025 Type_Def :=
12026 Make_Access_To_Object_Definition (Loc,
12027 Subtype_Indication =>
12028 Relocate_Node (Subtype_Mark (Access_Def)));
12030 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
12031 Set_All_Present (Type_Def, All_Present (Access_Def));
12032 end if;
12034 Set_Null_Exclusion_Present
12035 (Type_Def, Null_Exclusion_Present (Access_Def));
12037 Decl :=
12038 Make_Full_Type_Declaration (Loc,
12039 Defining_Identifier => Anon_Access,
12040 Type_Definition => Type_Def);
12042 Insert_Before (Typ_Decl, Decl);
12043 Analyze (Decl);
12045 -- At first sight we could add here the extra formals of an access to
12046 -- subprogram; however, it must delayed till the freeze point so that
12047 -- we know the convention.
12049 if Nkind (Comp_Def) = N_Component_Definition then
12050 Rewrite (Comp_Def,
12051 Make_Component_Definition (Loc,
12052 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
12053 else
12054 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
12055 Rewrite (Comp_Def,
12056 Make_Discriminant_Specification (Loc,
12057 Defining_Identifier => Defining_Identifier (Comp_Def),
12058 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
12059 end if;
12061 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
12062 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
12063 else
12064 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
12065 end if;
12067 Set_Is_Local_Anonymous_Access (Anon_Access);
12068 end if;
12069 end Check_Anonymous_Access_Component;
12071 ---------------------------------------
12072 -- Check_Anonymous_Access_Components --
12073 ---------------------------------------
12075 procedure Check_Anonymous_Access_Components
12076 (Typ_Decl : Node_Id;
12077 Typ : Entity_Id;
12078 Prev : Entity_Id;
12079 Comp_List : Node_Id)
12081 Comp : Node_Id;
12082 begin
12083 if No (Comp_List) then
12084 return;
12085 end if;
12087 Set_Is_Not_Self_Hidden (Typ);
12089 Comp := First (Component_Items (Comp_List));
12090 while Present (Comp) loop
12091 if Nkind (Comp) = N_Component_Declaration then
12092 Check_Anonymous_Access_Component
12093 (Typ_Decl, Typ, Prev,
12094 Component_Definition (Comp),
12095 Access_Definition (Component_Definition (Comp)));
12096 end if;
12098 Next (Comp);
12099 end loop;
12101 if Present (Variant_Part (Comp_List)) then
12102 declare
12103 V : Node_Id;
12104 begin
12105 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
12106 while Present (V) loop
12107 Check_Anonymous_Access_Components
12108 (Typ_Decl, Typ, Prev, Component_List (V));
12109 Next_Non_Pragma (V);
12110 end loop;
12111 end;
12112 end if;
12113 end Check_Anonymous_Access_Components;
12115 ----------------------
12116 -- Check_Completion --
12117 ----------------------
12119 procedure Check_Completion (Body_Id : Node_Id := Empty) is
12120 E : Entity_Id;
12122 procedure Post_Error;
12123 -- Post error message for lack of completion for entity E
12125 ----------------
12126 -- Post_Error --
12127 ----------------
12129 procedure Post_Error is
12130 procedure Missing_Body;
12131 -- Output missing body message
12133 ------------------
12134 -- Missing_Body --
12135 ------------------
12137 procedure Missing_Body is
12138 begin
12139 -- Spec is in same unit, so we can post on spec
12141 if In_Same_Source_Unit (Body_Id, E) then
12142 Error_Msg_N ("missing body for &", E);
12144 -- Spec is in a separate unit, so we have to post on the body
12146 else
12147 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12148 end if;
12149 end Missing_Body;
12151 -- Start of processing for Post_Error
12153 begin
12154 if not Comes_From_Source (E) then
12155 if Ekind (E) in E_Task_Type | E_Protected_Type then
12157 -- It may be an anonymous protected type created for a
12158 -- single variable. Post error on variable, if present.
12160 declare
12161 Var : Entity_Id;
12163 begin
12164 Var := First_Entity (Current_Scope);
12165 while Present (Var) loop
12166 exit when Etype (Var) = E
12167 and then Comes_From_Source (Var);
12169 Next_Entity (Var);
12170 end loop;
12172 if Present (Var) then
12173 E := Var;
12174 end if;
12175 end;
12176 end if;
12177 end if;
12179 -- If a generated entity has no completion, then either previous
12180 -- semantic errors have disabled the expansion phase, or else we had
12181 -- missing subunits, or else we are compiling without expansion,
12182 -- or else something is very wrong.
12184 if not Comes_From_Source (E) then
12185 pragma Assert
12186 (Serious_Errors_Detected > 0
12187 or else Configurable_Run_Time_Violations > 0
12188 or else Subunits_Missing
12189 or else not Expander_Active);
12190 return;
12192 -- Here for source entity
12194 else
12195 -- Here if no body to post the error message, so we post the error
12196 -- on the declaration that has no completion. This is not really
12197 -- the right place to post it, think about this later ???
12199 if No (Body_Id) then
12200 if Is_Type (E) then
12201 Error_Msg_NE
12202 ("missing full declaration for }", Parent (E), E);
12203 else
12204 Error_Msg_NE ("missing body for &", Parent (E), E);
12205 end if;
12207 -- Package body has no completion for a declaration that appears
12208 -- in the corresponding spec. Post error on the body, with a
12209 -- reference to the non-completed declaration.
12211 else
12212 Error_Msg_Sloc := Sloc (E);
12214 if Is_Type (E) then
12215 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12217 elsif Is_Overloadable (E)
12218 and then Current_Entity_In_Scope (E) /= E
12219 then
12220 -- It may be that the completion is mistyped and appears as
12221 -- a distinct overloading of the entity.
12223 declare
12224 Candidate : constant Entity_Id :=
12225 Current_Entity_In_Scope (E);
12226 Decl : constant Node_Id :=
12227 Unit_Declaration_Node (Candidate);
12229 begin
12230 if Is_Overloadable (Candidate)
12231 and then Ekind (Candidate) = Ekind (E)
12232 and then Nkind (Decl) = N_Subprogram_Body
12233 and then Acts_As_Spec (Decl)
12234 then
12235 Check_Type_Conformant (Candidate, E);
12237 else
12238 Missing_Body;
12239 end if;
12240 end;
12242 else
12243 Missing_Body;
12244 end if;
12245 end if;
12246 end if;
12247 end Post_Error;
12249 -- Local variables
12251 Pack_Id : constant Entity_Id := Current_Scope;
12253 -- Start of processing for Check_Completion
12255 begin
12256 E := First_Entity (Pack_Id);
12257 while Present (E) loop
12258 if Is_Intrinsic_Subprogram (E) then
12259 null;
12261 -- The following situation requires special handling: a child unit
12262 -- that appears in the context clause of the body of its parent:
12264 -- procedure Parent.Child (...);
12266 -- with Parent.Child;
12267 -- package body Parent is
12269 -- Here Parent.Child appears as a local entity, but should not be
12270 -- flagged as requiring completion, because it is a compilation
12271 -- unit.
12273 -- Ignore missing completion for a subprogram that does not come from
12274 -- source (including the _Call primitive operation of RAS types,
12275 -- which has to have the flag Comes_From_Source for other purposes):
12276 -- we assume that the expander will provide the missing completion.
12277 -- In case of previous errors, other expansion actions that provide
12278 -- bodies for null procedures with not be invoked, so inhibit message
12279 -- in those cases.
12281 -- Note that E_Operator is not in the list that follows, because
12282 -- this kind is reserved for predefined operators, that are
12283 -- intrinsic and do not need completion.
12285 elsif Ekind (E) in E_Function
12286 | E_Procedure
12287 | E_Generic_Function
12288 | E_Generic_Procedure
12289 then
12290 if Has_Completion (E) then
12291 null;
12293 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12294 null;
12296 elsif Is_Subprogram (E)
12297 and then (not Comes_From_Source (E)
12298 or else Chars (E) = Name_uCall)
12299 then
12300 null;
12302 elsif
12303 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12304 then
12305 null;
12307 elsif Nkind (Parent (E)) = N_Procedure_Specification
12308 and then Null_Present (Parent (E))
12309 and then Serious_Errors_Detected > 0
12310 then
12311 null;
12313 else
12314 Post_Error;
12315 end if;
12317 elsif Is_Entry (E) then
12318 if not Has_Completion (E)
12319 and then Ekind (Scope (E)) = E_Protected_Type
12320 then
12321 Post_Error;
12322 end if;
12324 elsif Is_Package_Or_Generic_Package (E) then
12325 if Unit_Requires_Body (E) then
12326 if not Has_Completion (E)
12327 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12328 N_Compilation_Unit
12329 then
12330 Post_Error;
12331 end if;
12333 elsif not Is_Child_Unit (E) then
12334 May_Need_Implicit_Body (E);
12335 end if;
12337 -- A formal incomplete type (Ada 2012) does not require a completion;
12338 -- other incomplete type declarations do.
12340 elsif Ekind (E) = E_Incomplete_Type then
12341 if No (Underlying_Type (E))
12342 and then not Is_Generic_Type (E)
12343 then
12344 Post_Error;
12345 end if;
12347 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12348 if not Has_Completion (E) then
12349 Post_Error;
12350 end if;
12352 -- A single task declared in the current scope is a constant, verify
12353 -- that the body of its anonymous type is in the same scope. If the
12354 -- task is defined elsewhere, this may be a renaming declaration for
12355 -- which no completion is needed.
12357 elsif Ekind (E) = E_Constant then
12358 if Ekind (Etype (E)) = E_Task_Type
12359 and then not Has_Completion (Etype (E))
12360 and then Scope (Etype (E)) = Current_Scope
12361 then
12362 Post_Error;
12363 end if;
12365 elsif Ekind (E) = E_Record_Type then
12366 if Is_Tagged_Type (E) then
12367 Check_Abstract_Overriding (E);
12368 Check_Conventions (E);
12369 end if;
12371 Check_Aliased_Component_Types (E);
12373 elsif Ekind (E) = E_Array_Type then
12374 Check_Aliased_Component_Types (E);
12376 end if;
12378 Next_Entity (E);
12379 end loop;
12380 end Check_Completion;
12382 -------------------------------------
12383 -- Check_Constraining_Discriminant --
12384 -------------------------------------
12386 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12388 New_Type : constant Entity_Id := Etype (New_Disc);
12389 Old_Type : Entity_Id;
12391 begin
12392 -- If the record type contains an array constrained by the discriminant
12393 -- but with some different bound, the compiler tries to create a smaller
12394 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12395 -- In this case, where the discriminant type is a scalar type, the check
12396 -- must use the original discriminant type in the parent declaration.
12398 if Is_Scalar_Type (New_Type) then
12399 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12400 else
12401 Old_Type := Etype (Old_Disc);
12402 end if;
12404 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12405 Error_Msg_N
12406 ("subtype must be statically compatible with parent discriminant",
12407 New_Disc);
12409 if not Predicates_Compatible (New_Type, Old_Type) then
12410 Error_Msg_N
12411 ("\subtype predicate is not compatible with parent discriminant",
12412 New_Disc);
12413 end if;
12414 end if;
12415 end Check_Constraining_Discriminant;
12417 ------------------------------------
12418 -- Check_CPP_Type_Has_No_Defaults --
12419 ------------------------------------
12421 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12422 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12423 Clist : Node_Id;
12424 Comp : Node_Id;
12426 begin
12427 -- Obtain the component list
12429 if Nkind (Tdef) = N_Record_Definition then
12430 Clist := Component_List (Tdef);
12431 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12432 Clist := Component_List (Record_Extension_Part (Tdef));
12433 end if;
12435 -- Check all components to ensure no default expressions
12437 if Present (Clist) then
12438 Comp := First_Non_Pragma (Component_Items (Clist));
12439 while Present (Comp) loop
12440 if Present (Expression (Comp)) then
12441 Error_Msg_N
12442 ("component of imported 'C'P'P type cannot have "
12443 & "default expression", Expression (Comp));
12444 end if;
12446 Next_Non_Pragma (Comp);
12447 end loop;
12448 end if;
12449 end Check_CPP_Type_Has_No_Defaults;
12451 ----------------------------
12452 -- Check_Delta_Expression --
12453 ----------------------------
12455 procedure Check_Delta_Expression (E : Node_Id) is
12456 begin
12457 if not Is_Real_Type (Etype (E)) then
12458 Wrong_Type (E, Any_Real);
12460 elsif not Is_OK_Static_Expression (E) then
12461 Flag_Non_Static_Expr
12462 ("non-static expression used for delta value!", E);
12464 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12465 Error_Msg_N ("delta expression must be positive", E);
12467 else
12468 return;
12469 end if;
12471 -- If any of above errors occurred, then replace the incorrect
12472 -- expression by the real 0.1, which should prevent further errors.
12474 Rewrite (E,
12475 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12476 Analyze_And_Resolve (E, Standard_Float);
12477 end Check_Delta_Expression;
12479 -----------------------------
12480 -- Check_Digits_Expression --
12481 -----------------------------
12483 procedure Check_Digits_Expression (E : Node_Id) is
12484 begin
12485 if not Is_Integer_Type (Etype (E)) then
12486 Wrong_Type (E, Any_Integer);
12488 elsif not Is_OK_Static_Expression (E) then
12489 Flag_Non_Static_Expr
12490 ("non-static expression used for digits value!", E);
12492 elsif Expr_Value (E) <= 0 then
12493 Error_Msg_N ("digits value must be greater than zero", E);
12495 else
12496 return;
12497 end if;
12499 -- If any of above errors occurred, then replace the incorrect
12500 -- expression by the integer 1, which should prevent further errors.
12502 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12503 Analyze_And_Resolve (E, Standard_Integer);
12505 end Check_Digits_Expression;
12507 --------------------------
12508 -- Check_Initialization --
12509 --------------------------
12511 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12512 begin
12513 -- Special processing for limited types
12515 if Is_Limited_Type (T)
12516 and then not In_Instance
12517 and then not In_Inlined_Body
12518 then
12519 if not OK_For_Limited_Init (T, Exp) then
12521 -- In GNAT mode, this is just a warning, to allow it to be evilly
12522 -- turned off. Otherwise it is a real error.
12524 if GNAT_Mode then
12525 Error_Msg_N
12526 ("??cannot initialize entities of limited type!", Exp);
12528 elsif Ada_Version < Ada_2005 then
12530 -- The side effect removal machinery may generate illegal Ada
12531 -- code to avoid the usage of access types and 'reference in
12532 -- SPARK mode. Since this is legal code with respect to theorem
12533 -- proving, do not emit the error.
12535 if GNATprove_Mode
12536 and then Nkind (Exp) = N_Function_Call
12537 and then Nkind (Parent (Exp)) = N_Object_Declaration
12538 and then not Comes_From_Source
12539 (Defining_Identifier (Parent (Exp)))
12540 then
12541 null;
12543 else
12544 Error_Msg_N
12545 ("cannot initialize entities of limited type", Exp);
12546 Explain_Limited_Type (T, Exp);
12547 end if;
12549 else
12550 -- Specialize error message according to kind of illegal
12551 -- initial expression. We check the Original_Node to cover
12552 -- cases where the initialization expression of an object
12553 -- declaration generated by the compiler has been rewritten
12554 -- (such as for dispatching calls).
12556 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12557 and then
12558 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12559 then
12560 -- No error for internally-generated object declarations,
12561 -- which can come from build-in-place assignment statements.
12563 if Nkind (Parent (Exp)) = N_Object_Declaration
12564 and then not Comes_From_Source
12565 (Defining_Identifier (Parent (Exp)))
12566 then
12567 null;
12569 else
12570 Error_Msg_N
12571 ("illegal context for call to function with limited "
12572 & "result", Exp);
12573 end if;
12575 else
12576 Error_Msg_N
12577 ("initialization of limited object requires aggregate or "
12578 & "function call", Exp);
12579 end if;
12580 end if;
12581 end if;
12582 end if;
12584 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12585 -- set unless we can be sure that no range check is required.
12587 if not Expander_Active
12588 and then Is_Scalar_Type (T)
12589 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12590 then
12591 Set_Do_Range_Check (Exp);
12592 end if;
12593 end Check_Initialization;
12595 ----------------------
12596 -- Check_Interfaces --
12597 ----------------------
12599 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12600 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12602 Iface : Node_Id;
12603 Iface_Def : Node_Id;
12604 Iface_Typ : Entity_Id;
12605 Parent_Node : Node_Id;
12607 Is_Task : Boolean := False;
12608 -- Set True if parent type or any progenitor is a task interface
12610 Is_Protected : Boolean := False;
12611 -- Set True if parent type or any progenitor is a protected interface
12613 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12614 -- Check that a progenitor is compatible with declaration. If an error
12615 -- message is output, it is posted on Error_Node.
12617 ------------------
12618 -- Check_Ifaces --
12619 ------------------
12621 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12622 Iface_Id : constant Entity_Id :=
12623 Defining_Identifier (Parent (Iface_Def));
12624 Type_Def : Node_Id;
12626 begin
12627 if Nkind (N) = N_Private_Extension_Declaration then
12628 Type_Def := N;
12629 else
12630 Type_Def := Type_Definition (N);
12631 end if;
12633 if Is_Task_Interface (Iface_Id) then
12634 Is_Task := True;
12636 elsif Is_Protected_Interface (Iface_Id) then
12637 Is_Protected := True;
12638 end if;
12640 if Is_Synchronized_Interface (Iface_Id) then
12642 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12643 -- extension derived from a synchronized interface must explicitly
12644 -- be declared synchronized, because the full view will be a
12645 -- synchronized type.
12647 if Nkind (N) = N_Private_Extension_Declaration then
12648 if not Synchronized_Present (N) then
12649 Error_Msg_NE
12650 ("private extension of& must be explicitly synchronized",
12651 N, Iface_Id);
12652 end if;
12654 -- However, by 3.9.4(16/2), a full type that is a record extension
12655 -- is never allowed to derive from a synchronized interface (note
12656 -- that interfaces must be excluded from this check, because those
12657 -- are represented by derived type definitions in some cases).
12659 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12660 and then not Interface_Present (Type_Definition (N))
12661 then
12662 Error_Msg_N ("record extension cannot derive from synchronized "
12663 & "interface", Error_Node);
12664 end if;
12665 end if;
12667 -- Check that the characteristics of the progenitor are compatible
12668 -- with the explicit qualifier in the declaration.
12669 -- The check only applies to qualifiers that come from source.
12670 -- Limited_Present also appears in the declaration of corresponding
12671 -- records, and the check does not apply to them.
12673 if Limited_Present (Type_Def)
12674 and then not
12675 Is_Concurrent_Record_Type (Defining_Identifier (N))
12676 then
12677 if Is_Limited_Interface (Parent_Type)
12678 and then not Is_Limited_Interface (Iface_Id)
12679 then
12680 Error_Msg_NE
12681 ("progenitor & must be limited interface",
12682 Error_Node, Iface_Id);
12684 elsif
12685 (Task_Present (Iface_Def)
12686 or else Protected_Present (Iface_Def)
12687 or else Synchronized_Present (Iface_Def))
12688 and then Nkind (N) /= N_Private_Extension_Declaration
12689 and then not Error_Posted (N)
12690 then
12691 Error_Msg_NE
12692 ("progenitor & must be limited interface",
12693 Error_Node, Iface_Id);
12694 end if;
12696 -- Protected interfaces can only inherit from limited, synchronized
12697 -- or protected interfaces.
12699 elsif Nkind (N) = N_Full_Type_Declaration
12700 and then Protected_Present (Type_Def)
12701 then
12702 if Limited_Present (Iface_Def)
12703 or else Synchronized_Present (Iface_Def)
12704 or else Protected_Present (Iface_Def)
12705 then
12706 null;
12708 elsif Task_Present (Iface_Def) then
12709 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12710 & "from task interface", Error_Node);
12712 else
12713 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12714 & "from non-limited interface", Error_Node);
12715 end if;
12717 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12718 -- limited and synchronized.
12720 elsif Synchronized_Present (Type_Def) then
12721 if Limited_Present (Iface_Def)
12722 or else Synchronized_Present (Iface_Def)
12723 then
12724 null;
12726 elsif Protected_Present (Iface_Def)
12727 and then Nkind (N) /= N_Private_Extension_Declaration
12728 then
12729 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12730 & "from protected interface", Error_Node);
12732 elsif Task_Present (Iface_Def)
12733 and then Nkind (N) /= N_Private_Extension_Declaration
12734 then
12735 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12736 & "from task interface", Error_Node);
12738 elsif not Is_Limited_Interface (Iface_Id) then
12739 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12740 & "from non-limited interface", Error_Node);
12741 end if;
12743 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12744 -- synchronized or task interfaces.
12746 elsif Nkind (N) = N_Full_Type_Declaration
12747 and then Task_Present (Type_Def)
12748 then
12749 if Limited_Present (Iface_Def)
12750 or else Synchronized_Present (Iface_Def)
12751 or else Task_Present (Iface_Def)
12752 then
12753 null;
12755 elsif Protected_Present (Iface_Def) then
12756 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12757 & "protected interface", Error_Node);
12759 else
12760 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12761 & "non-limited interface", Error_Node);
12762 end if;
12763 end if;
12764 end Check_Ifaces;
12766 -- Start of processing for Check_Interfaces
12768 begin
12769 if Is_Interface (Parent_Type) then
12770 if Is_Task_Interface (Parent_Type) then
12771 Is_Task := True;
12773 elsif Is_Protected_Interface (Parent_Type) then
12774 Is_Protected := True;
12775 end if;
12776 end if;
12778 if Nkind (N) = N_Private_Extension_Declaration then
12780 -- Check that progenitors are compatible with declaration
12782 Iface := First (Interface_List (Def));
12783 while Present (Iface) loop
12784 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12786 Parent_Node := Parent (Base_Type (Iface_Typ));
12787 Iface_Def := Type_Definition (Parent_Node);
12789 if not Is_Interface (Iface_Typ) then
12790 Diagnose_Interface (Iface, Iface_Typ);
12791 else
12792 Check_Ifaces (Iface_Def, Iface);
12793 end if;
12795 Next (Iface);
12796 end loop;
12798 if Is_Task and Is_Protected then
12799 Error_Msg_N
12800 ("type cannot derive from task and protected interface", N);
12801 end if;
12803 return;
12804 end if;
12806 -- Full type declaration of derived type.
12807 -- Check compatibility with parent if it is interface type
12809 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12810 and then Is_Interface (Parent_Type)
12811 then
12812 Parent_Node := Parent (Parent_Type);
12814 -- More detailed checks for interface varieties
12816 Check_Ifaces
12817 (Iface_Def => Type_Definition (Parent_Node),
12818 Error_Node => Subtype_Indication (Type_Definition (N)));
12819 end if;
12821 Iface := First (Interface_List (Def));
12822 while Present (Iface) loop
12823 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12825 Parent_Node := Parent (Base_Type (Iface_Typ));
12826 Iface_Def := Type_Definition (Parent_Node);
12828 if not Is_Interface (Iface_Typ) then
12829 Diagnose_Interface (Iface, Iface_Typ);
12831 else
12832 -- "The declaration of a specific descendant of an interface
12833 -- type freezes the interface type" RM 13.14
12835 Freeze_Before (N, Iface_Typ);
12836 Check_Ifaces (Iface_Def, Error_Node => Iface);
12837 end if;
12839 Next (Iface);
12840 end loop;
12842 if Is_Task and Is_Protected then
12843 Error_Msg_N
12844 ("type cannot derive from task and protected interface", N);
12845 end if;
12846 end Check_Interfaces;
12848 ------------------------------------
12849 -- Check_Or_Process_Discriminants --
12850 ------------------------------------
12852 -- If an incomplete or private type declaration was already given for the
12853 -- type, the discriminants may have already been processed if they were
12854 -- present on the incomplete declaration. In this case a full conformance
12855 -- check has been performed in Find_Type_Name, and we then recheck here
12856 -- some properties that can't be checked on the partial view alone.
12857 -- Otherwise we call Process_Discriminants.
12859 procedure Check_Or_Process_Discriminants
12860 (N : Node_Id;
12861 T : Entity_Id;
12862 Prev : Entity_Id := Empty)
12864 begin
12865 if Has_Discriminants (T) then
12867 -- Discriminants are already set on T if they were already present
12868 -- on the partial view. Make them visible to component declarations.
12870 declare
12871 D : Entity_Id;
12872 -- Discriminant on T (full view) referencing expr on partial view
12874 Prev_D : Entity_Id;
12875 -- Entity of corresponding discriminant on partial view
12877 New_D : Node_Id;
12878 -- Discriminant specification for full view, expression is
12879 -- the syntactic copy on full view (which has been checked for
12880 -- conformance with partial view), only used here to post error
12881 -- message.
12883 begin
12884 D := First_Discriminant (T);
12885 New_D := First (Discriminant_Specifications (N));
12886 while Present (D) loop
12887 Prev_D := Current_Entity (D);
12888 Set_Current_Entity (D);
12889 Set_Is_Immediately_Visible (D);
12890 Set_Homonym (D, Prev_D);
12892 -- Handle the case where there is an untagged partial view and
12893 -- the full view is tagged: must disallow discriminants with
12894 -- defaults, unless compiling for Ada 2012, which allows a
12895 -- limited tagged type to have defaulted discriminants (see
12896 -- AI05-0214). However, suppress error here if it was already
12897 -- reported on the default expression of the partial view.
12899 if Is_Tagged_Type (T)
12900 and then Present (Expression (Parent (D)))
12901 and then (not Is_Limited_Type (Current_Scope)
12902 or else Ada_Version < Ada_2012)
12903 and then not Error_Posted (Expression (Parent (D)))
12904 then
12905 if Ada_Version >= Ada_2012 then
12906 Error_Msg_N
12907 ("discriminants of nonlimited tagged type cannot have "
12908 & "defaults",
12909 Expression (New_D));
12910 else
12911 Error_Msg_N
12912 ("discriminants of tagged type cannot have defaults",
12913 Expression (New_D));
12914 end if;
12915 end if;
12917 -- Ada 2005 (AI-230): Access discriminant allowed in
12918 -- non-limited record types.
12920 if Ada_Version < Ada_2005 then
12922 -- This restriction gets applied to the full type here. It
12923 -- has already been applied earlier to the partial view.
12925 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12926 end if;
12928 Next_Discriminant (D);
12929 Next (New_D);
12930 end loop;
12931 end;
12933 elsif Present (Discriminant_Specifications (N)) then
12934 Process_Discriminants (N, Prev);
12935 end if;
12936 end Check_Or_Process_Discriminants;
12938 ----------------------
12939 -- Check_Real_Bound --
12940 ----------------------
12942 procedure Check_Real_Bound (Bound : Node_Id) is
12943 begin
12944 if not Is_Real_Type (Etype (Bound)) then
12945 Error_Msg_N
12946 ("bound in real type definition must be of real type", Bound);
12948 elsif not Is_OK_Static_Expression (Bound) then
12949 Flag_Non_Static_Expr
12950 ("non-static expression used for real type bound!", Bound);
12952 else
12953 return;
12954 end if;
12956 Rewrite
12957 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12958 Analyze (Bound);
12959 Resolve (Bound, Standard_Float);
12960 end Check_Real_Bound;
12962 ------------------------------
12963 -- Complete_Private_Subtype --
12964 ------------------------------
12966 procedure Complete_Private_Subtype
12967 (Priv : Entity_Id;
12968 Full : Entity_Id;
12969 Full_Base : Entity_Id;
12970 Related_Nod : Node_Id)
12972 Save_Next_Entity : Entity_Id;
12973 Save_Homonym : Entity_Id;
12975 begin
12976 -- Set semantic attributes for (implicit) private subtype completion.
12977 -- If the full type has no discriminants, then it is a copy of the
12978 -- full view of the base. Otherwise, it is a subtype of the base with
12979 -- a possible discriminant constraint. Save and restore the original
12980 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12981 -- not corrupt the entity chain.
12983 Save_Next_Entity := Next_Entity (Full);
12984 Save_Homonym := Homonym (Priv);
12986 if Is_Private_Type (Full_Base)
12987 or else Is_Record_Type (Full_Base)
12988 or else Is_Concurrent_Type (Full_Base)
12989 then
12990 Copy_Node (Priv, Full);
12992 -- Note that the Etype of the full view is the same as the Etype of
12993 -- the partial view. In this fashion, the subtype has access to the
12994 -- correct view of the parent.
12996 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12997 Set_Has_Unknown_Discriminants
12998 (Full, Has_Unknown_Discriminants (Full_Base));
12999 Set_First_Entity (Full, First_Entity (Full_Base));
13000 Set_Last_Entity (Full, Last_Entity (Full_Base));
13002 -- If the underlying base type is constrained, we know that the
13003 -- full view of the subtype is constrained as well (the converse
13004 -- is not necessarily true).
13006 if Is_Constrained (Full_Base) then
13007 Set_Is_Constrained (Full);
13008 end if;
13010 else
13011 Copy_Node (Full_Base, Full);
13013 -- The following subtlety with the Etype of the full view needs to be
13014 -- taken into account here. One could think that it must naturally be
13015 -- set to the base type of the full base:
13017 -- Set_Etype (Full, Base_Type (Full_Base));
13019 -- so that the full view becomes a subtype of the full base when the
13020 -- latter is a base type, which must for example happen when the full
13021 -- base is declared as derived type. That's also correct if the full
13022 -- base is declared as an array type, or a floating-point type, or a
13023 -- fixed-point type, or a signed integer type, as these declarations
13024 -- create an implicit base type and a first subtype so the Etype of
13025 -- the full views must be the implicit base type. But that's wrong
13026 -- if the full base is declared as an access type, or an enumeration
13027 -- type, or a modular integer type, as these declarations directly
13028 -- create a base type, i.e. with Etype pointing to itself. Moreover
13029 -- the full base being declared in the private part, i.e. when the
13030 -- views are swapped, the end result is that the Etype of the full
13031 -- base is set to its private view in this case and that we need to
13032 -- propagate this setting to the full view in order for the subtype
13033 -- to be compatible with the base type.
13035 if Is_Base_Type (Full_Base)
13036 and then (Is_Derived_Type (Full_Base)
13037 or else Ekind (Full_Base) in Array_Kind
13038 or else Ekind (Full_Base) in Fixed_Point_Kind
13039 or else Ekind (Full_Base) in Float_Kind
13040 or else Ekind (Full_Base) in Signed_Integer_Kind)
13041 then
13042 Set_Etype (Full, Full_Base);
13043 end if;
13045 Set_Chars (Full, Chars (Priv));
13046 Set_Sloc (Full, Sloc (Priv));
13047 Conditional_Delay (Full, Priv);
13048 end if;
13050 Link_Entities (Full, Save_Next_Entity);
13051 Set_Homonym (Full, Save_Homonym);
13052 Set_Associated_Node_For_Itype (Full, Related_Nod);
13054 if Ekind (Full) in Incomplete_Or_Private_Kind then
13055 Reinit_Field_To_Zero (Full, F_Private_Dependents);
13056 end if;
13058 -- Set common attributes for all subtypes: kind, convention, etc.
13060 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
13061 Set_Is_Not_Self_Hidden (Full);
13062 Set_Convention (Full, Convention (Full_Base));
13063 Set_Is_First_Subtype (Full, False);
13064 Set_Scope (Full, Scope (Priv));
13065 Set_Size_Info (Full, Full_Base);
13066 Copy_RM_Size (To => Full, From => Full_Base);
13067 Set_Is_Itype (Full);
13069 -- A subtype of a private-type-without-discriminants, whose full-view
13070 -- has discriminants with default expressions, is not constrained.
13072 if not Has_Discriminants (Priv) then
13073 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
13075 if Has_Discriminants (Full_Base) then
13076 Set_Discriminant_Constraint
13077 (Full, Discriminant_Constraint (Full_Base));
13079 -- The partial view may have been indefinite, the full view
13080 -- might not be.
13082 Set_Has_Unknown_Discriminants
13083 (Full, Has_Unknown_Discriminants (Full_Base));
13084 end if;
13085 end if;
13087 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
13088 Set_Depends_On_Private (Full, Has_Private_Component (Full));
13090 -- Freeze the private subtype entity if its parent is delayed, and not
13091 -- already frozen. We skip this processing if the type is an anonymous
13092 -- subtype of a record component, or is the corresponding record of a
13093 -- protected type, since these are processed when the enclosing type
13094 -- is frozen. If the parent type is declared in a nested package then
13095 -- the freezing of the private and full views also happens later.
13097 if not Is_Type (Scope (Full)) then
13098 if Is_Itype (Priv)
13099 and then In_Same_Source_Unit (Full, Full_Base)
13100 and then Scope (Full_Base) /= Scope (Full)
13101 then
13102 Set_Has_Delayed_Freeze (Full);
13103 Set_Has_Delayed_Freeze (Priv);
13105 else
13106 Set_Has_Delayed_Freeze (Full,
13107 Has_Delayed_Freeze (Full_Base)
13108 and then not Is_Frozen (Full_Base));
13109 end if;
13110 end if;
13112 Set_Freeze_Node (Full, Empty);
13113 Set_Is_Frozen (Full, False);
13115 if Has_Discriminants (Full) then
13116 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
13117 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
13119 if Has_Unknown_Discriminants (Full) then
13120 Set_Discriminant_Constraint (Full, No_Elist);
13121 end if;
13122 end if;
13124 if Ekind (Full_Base) = E_Record_Type
13125 and then Has_Discriminants (Full_Base)
13126 and then Has_Discriminants (Priv) -- might not, if errors
13127 and then not Has_Unknown_Discriminants (Priv)
13128 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13129 then
13130 Create_Constrained_Components
13131 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13133 -- If the full base is itself derived from private, build a congruent
13134 -- subtype of its underlying full view, for use by the back end.
13136 elsif Is_Private_Type (Full_Base)
13137 and then Present (Underlying_Full_View (Full_Base))
13138 then
13139 declare
13140 Underlying_Full_Base : constant Entity_Id
13141 := Underlying_Full_View (Full_Base);
13142 Underlying_Full : constant Entity_Id
13143 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13144 begin
13145 Set_Is_Itype (Underlying_Full);
13146 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13147 Complete_Private_Subtype
13148 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13149 Set_Underlying_Full_View (Full, Underlying_Full);
13150 Set_Is_Underlying_Full_View (Underlying_Full);
13151 end;
13153 elsif Is_Record_Type (Full_Base) then
13155 -- Show Full is simply a renaming of Full_Base
13157 Set_Cloned_Subtype (Full, Full_Base);
13158 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13160 -- Propagate predicates
13162 Propagate_Predicate_Attributes (Full, Full_Base);
13163 end if;
13165 -- It is unsafe to share the bounds of a scalar type, because the Itype
13166 -- is elaborated on demand, and if a bound is nonstatic, then different
13167 -- orders of elaboration in different units will lead to different
13168 -- external symbols.
13170 if Is_Scalar_Type (Full_Base) then
13171 Set_Scalar_Range (Full,
13172 Make_Range (Sloc (Related_Nod),
13173 Low_Bound =>
13174 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13175 High_Bound =>
13176 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13178 -- This completion inherits the bounds of the full parent, but if
13179 -- the parent is an unconstrained floating point type, so is the
13180 -- completion.
13182 if Is_Floating_Point_Type (Full_Base) then
13183 Set_Includes_Infinities
13184 (Scalar_Range (Full), Has_Infinities (Full_Base));
13185 end if;
13186 end if;
13188 -- ??? It seems that a lot of fields are missing that should be copied
13189 -- from Full_Base to Full. Here are some that are introduced in a
13190 -- non-disruptive way but a cleanup is necessary.
13192 if Is_Tagged_Type (Full_Base) then
13193 Set_Is_Tagged_Type (Full);
13194 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13196 Set_Direct_Primitive_Operations
13197 (Full, Direct_Primitive_Operations (Full_Base));
13198 Set_No_Tagged_Streams_Pragma
13199 (Full, No_Tagged_Streams_Pragma (Full_Base));
13201 if Is_Interface (Full_Base) then
13202 Set_Is_Interface (Full);
13203 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13204 end if;
13206 -- Inherit class_wide type of full_base in case the partial view was
13207 -- not tagged. Otherwise it has already been created when the private
13208 -- subtype was analyzed.
13210 if No (Class_Wide_Type (Full)) then
13211 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13212 end if;
13214 -- If this is a subtype of a protected or task type, constrain its
13215 -- corresponding record, unless this is a subtype without constraints,
13216 -- i.e. a simple renaming as with an actual subtype in an instance.
13218 elsif Is_Concurrent_Type (Full_Base) then
13219 if Has_Discriminants (Full)
13220 and then Present (Corresponding_Record_Type (Full_Base))
13221 and then
13222 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13223 then
13224 Set_Corresponding_Record_Type (Full,
13225 Constrain_Corresponding_Record
13226 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13228 else
13229 Set_Corresponding_Record_Type (Full,
13230 Corresponding_Record_Type (Full_Base));
13231 end if;
13232 end if;
13234 -- Link rep item chain, and also setting of Has_Predicates from private
13235 -- subtype to full subtype, since we will need these on the full subtype
13236 -- to create the predicate function. Note that the full subtype may
13237 -- already have rep items, inherited from the full view of the base
13238 -- type, so we must be sure not to overwrite these entries.
13240 declare
13241 Append : Boolean;
13242 Item : Node_Id;
13243 Next_Item : Node_Id;
13244 Priv_Item : Node_Id;
13246 begin
13247 Item := First_Rep_Item (Full);
13248 Priv_Item := First_Rep_Item (Priv);
13250 -- If no existing rep items on full type, we can just link directly
13251 -- to the list of items on the private type, if any exist.. Same if
13252 -- the rep items are only those inherited from the base
13254 if (No (Item)
13255 or else Nkind (Item) /= N_Aspect_Specification
13256 or else Entity (Item) = Full_Base)
13257 and then Present (First_Rep_Item (Priv))
13258 then
13259 Set_First_Rep_Item (Full, Priv_Item);
13261 -- Otherwise, search to the end of items currently linked to the full
13262 -- subtype and append the private items to the end. However, if Priv
13263 -- and Full already have the same list of rep items, then the append
13264 -- is not done, as that would create a circularity.
13266 -- The partial view may have a predicate and the rep item lists of
13267 -- both views agree when inherited from the same ancestor. In that
13268 -- case, simply propagate the list from one view to the other.
13269 -- A more complex analysis needed here ???
13271 elsif Present (Priv_Item)
13272 and then Item = Next_Rep_Item (Priv_Item)
13273 then
13274 Set_First_Rep_Item (Full, Priv_Item);
13276 elsif Item /= Priv_Item then
13277 Append := True;
13278 loop
13279 Next_Item := Next_Rep_Item (Item);
13280 exit when No (Next_Item);
13281 Item := Next_Item;
13283 -- If the private view has aspect specifications, the full view
13284 -- inherits them. Since these aspects may already have been
13285 -- attached to the full view during derivation, do not append
13286 -- them if already present.
13288 if Item = First_Rep_Item (Priv) then
13289 Append := False;
13290 exit;
13291 end if;
13292 end loop;
13294 -- And link the private type items at the end of the chain
13296 if Append then
13297 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13298 end if;
13299 end if;
13300 end;
13302 -- Make sure Has_Predicates is set on full type if it is set on the
13303 -- private type. Note that it may already be set on the full type and
13304 -- if so, we don't want to unset it. Similarly, propagate information
13305 -- about delayed aspects, because the corresponding pragmas must be
13306 -- analyzed when one of the views is frozen. This last step is needed
13307 -- in particular when the full type is a scalar type for which an
13308 -- anonymous base type is constructed.
13310 -- The predicate functions are generated either at the freeze point
13311 -- of the type or at the end of the visible part, and we must avoid
13312 -- generating them twice.
13314 Propagate_Predicate_Attributes (Full, Priv);
13316 if Has_Delayed_Aspects (Priv) then
13317 Set_Has_Delayed_Aspects (Full);
13318 end if;
13319 end Complete_Private_Subtype;
13321 ----------------------------
13322 -- Constant_Redeclaration --
13323 ----------------------------
13325 procedure Constant_Redeclaration
13326 (Id : Entity_Id;
13327 N : Node_Id;
13328 T : out Entity_Id)
13330 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13331 Obj_Def : constant Node_Id := Object_Definition (N);
13332 New_T : Entity_Id;
13334 procedure Check_Possible_Deferred_Completion
13335 (Prev_Id : Entity_Id;
13336 Curr_Obj_Def : Node_Id);
13337 -- Determine whether the two object definitions describe the partial
13338 -- and the full view of a constrained deferred constant. Generate
13339 -- a subtype for the full view and verify that it statically matches
13340 -- the subtype of the partial view.
13342 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13343 -- If deferred constant is an access type initialized with an allocator,
13344 -- check whether there is an illegal recursion in the definition,
13345 -- through a default value of some record subcomponent. This is normally
13346 -- detected when generating init procs, but requires this additional
13347 -- mechanism when expansion is disabled.
13349 ----------------------------------------
13350 -- Check_Possible_Deferred_Completion --
13351 ----------------------------------------
13353 procedure Check_Possible_Deferred_Completion
13354 (Prev_Id : Entity_Id;
13355 Curr_Obj_Def : Node_Id)
13357 Curr_Typ : Entity_Id;
13358 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13359 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13360 Mismatch : Boolean := False;
13361 begin
13362 if Anon_Acc then
13363 null;
13364 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13365 declare
13366 Loc : constant Source_Ptr := Sloc (N);
13367 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13368 Decl : constant Node_Id :=
13369 Make_Subtype_Declaration (Loc,
13370 Defining_Identifier => Def_Id,
13371 Subtype_Indication =>
13372 Relocate_Node (Curr_Obj_Def));
13374 begin
13375 Insert_Before_And_Analyze (N, Decl);
13376 Set_Etype (Id, Def_Id);
13377 Curr_Typ := Def_Id;
13378 end;
13379 else
13380 Curr_Typ := Etype (Curr_Obj_Def);
13381 end if;
13383 if Anon_Acc then
13384 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13385 Mismatch := True;
13386 elsif Has_Null_Exclusion (Prev_Typ)
13387 and then not Null_Exclusion_Present (Curr_Obj_Def)
13388 then
13389 Mismatch := True;
13390 end if;
13391 -- ??? Another check needed: mismatch if disagreement
13392 -- between designated types/profiles .
13393 else
13394 Mismatch :=
13395 Is_Constrained (Prev_Typ)
13396 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13397 end if;
13399 if Mismatch then
13400 Error_Msg_Sloc := Sloc (Prev_Id);
13401 Error_Msg_N ("subtype does not statically match deferred "
13402 & "declaration #", N);
13403 end if;
13404 end Check_Possible_Deferred_Completion;
13406 ---------------------------------
13407 -- Check_Recursive_Declaration --
13408 ---------------------------------
13410 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13411 Comp : Entity_Id;
13413 begin
13414 if Is_Record_Type (Typ) then
13415 Comp := First_Component (Typ);
13416 while Present (Comp) loop
13417 if Comes_From_Source (Comp) then
13418 if Present (Expression (Parent (Comp)))
13419 and then Is_Entity_Name (Expression (Parent (Comp)))
13420 and then Entity (Expression (Parent (Comp))) = Prev
13421 then
13422 Error_Msg_Sloc := Sloc (Parent (Comp));
13423 Error_Msg_NE
13424 ("illegal circularity with declaration for & #",
13425 N, Comp);
13426 return;
13428 elsif Is_Record_Type (Etype (Comp)) then
13429 Check_Recursive_Declaration (Etype (Comp));
13430 end if;
13431 end if;
13433 Next_Component (Comp);
13434 end loop;
13435 end if;
13436 end Check_Recursive_Declaration;
13438 -- Start of processing for Constant_Redeclaration
13440 begin
13441 if Nkind (Parent (Prev)) = N_Object_Declaration then
13442 if Nkind (Object_Definition
13443 (Parent (Prev))) = N_Subtype_Indication
13444 then
13445 -- Find type of new declaration. The constraints of the two
13446 -- views must match statically, but there is no point in
13447 -- creating an itype for the full view.
13449 if Nkind (Obj_Def) = N_Subtype_Indication then
13450 Find_Type (Subtype_Mark (Obj_Def));
13451 New_T := Entity (Subtype_Mark (Obj_Def));
13453 else
13454 Find_Type (Obj_Def);
13455 New_T := Entity (Obj_Def);
13456 end if;
13458 T := Etype (Prev);
13460 else
13461 -- The full view may impose a constraint, even if the partial
13462 -- view does not, so construct the subtype.
13464 New_T := Find_Type_Of_Object (Obj_Def, N);
13465 T := New_T;
13466 end if;
13468 else
13469 -- Current declaration is illegal, diagnosed below in Enter_Name
13471 T := Empty;
13472 New_T := Any_Type;
13473 end if;
13475 -- If previous full declaration or a renaming declaration exists, or if
13476 -- a homograph is present, let Enter_Name handle it, either with an
13477 -- error or with the removal of an overridden implicit subprogram.
13478 -- The previous one is a full declaration if it has an expression
13479 -- (which in the case of an aggregate is indicated by the Init flag).
13481 if Ekind (Prev) /= E_Constant
13482 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13483 or else Present (Expression (Parent (Prev)))
13484 or else Has_Init_Expression (Parent (Prev))
13485 or else Present (Full_View (Prev))
13486 then
13487 Enter_Name (Id);
13489 -- Verify that types of both declarations match, or else that both types
13490 -- are anonymous access types whose designated subtypes statically match
13491 -- (as allowed in Ada 2005 by AI-385).
13493 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13494 and then
13495 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13496 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13497 or else Is_Access_Constant (Etype (New_T)) /=
13498 Is_Access_Constant (Etype (Prev))
13499 or else Can_Never_Be_Null (Etype (New_T)) /=
13500 Can_Never_Be_Null (Etype (Prev))
13501 or else Null_Exclusion_Present (Parent (Prev)) /=
13502 Null_Exclusion_Present (Parent (Id))
13503 or else not Subtypes_Statically_Match
13504 (Designated_Type (Etype (Prev)),
13505 Designated_Type (Etype (New_T))))
13506 then
13507 Error_Msg_Sloc := Sloc (Prev);
13508 Error_Msg_N ("type does not match declaration#", N);
13509 Set_Full_View (Prev, Id);
13510 Set_Etype (Id, Any_Type);
13512 -- A deferred constant whose type is an anonymous array is always
13513 -- illegal (unless imported). A detailed error message might be
13514 -- helpful for Ada beginners.
13516 if Nkind (Object_Definition (Parent (Prev)))
13517 = N_Constrained_Array_Definition
13518 and then Nkind (Object_Definition (N))
13519 = N_Constrained_Array_Definition
13520 then
13521 Error_Msg_N ("\each anonymous array is a distinct type", N);
13522 Error_Msg_N ("a deferred constant must have a named type",
13523 Object_Definition (Parent (Prev)));
13524 end if;
13526 elsif
13527 Null_Exclusion_Present (Parent (Prev))
13528 and then not Null_Exclusion_Present (N)
13529 then
13530 Error_Msg_Sloc := Sloc (Prev);
13531 Error_Msg_N ("null-exclusion does not match declaration#", N);
13532 Set_Full_View (Prev, Id);
13533 Set_Etype (Id, Any_Type);
13535 -- If so, process the full constant declaration
13537 else
13538 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13539 -- the deferred declaration is constrained, then the subtype defined
13540 -- by the subtype_indication in the full declaration shall match it
13541 -- statically.
13543 Check_Possible_Deferred_Completion
13544 (Prev_Id => Prev,
13545 Curr_Obj_Def => Obj_Def);
13547 Set_Full_View (Prev, Id);
13548 Set_Is_Public (Id, Is_Public (Prev));
13549 Set_Is_Internal (Id);
13550 Append_Entity (Id, Current_Scope);
13552 -- Check ALIASED present if present before (RM 7.4(7))
13554 if Is_Aliased (Prev)
13555 and then not Aliased_Present (N)
13556 then
13557 Error_Msg_Sloc := Sloc (Prev);
13558 Error_Msg_N ("ALIASED required (see declaration #)", N);
13559 end if;
13561 -- Check that placement is in private part and that the incomplete
13562 -- declaration appeared in the visible part.
13564 if Ekind (Current_Scope) = E_Package
13565 and then not In_Private_Part (Current_Scope)
13566 then
13567 Error_Msg_Sloc := Sloc (Prev);
13568 Error_Msg_N
13569 ("full constant for declaration # must be in private part", N);
13571 elsif Ekind (Current_Scope) = E_Package
13572 and then
13573 List_Containing (Parent (Prev)) /=
13574 Visible_Declarations (Package_Specification (Current_Scope))
13575 then
13576 Error_Msg_N
13577 ("deferred constant must be declared in visible part",
13578 Parent (Prev));
13579 end if;
13581 if Is_Access_Type (T)
13582 and then Nkind (Expression (N)) = N_Allocator
13583 then
13584 Check_Recursive_Declaration (Designated_Type (T));
13585 end if;
13587 -- A deferred constant is a visible entity. If type has invariants,
13588 -- verify that the initial value satisfies them. This is not done in
13589 -- GNATprove mode, as GNATprove handles invariant checks itself.
13591 if Has_Invariants (T)
13592 and then Present (Invariant_Procedure (T))
13593 and then not GNATprove_Mode
13594 then
13595 Insert_After (N,
13596 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13597 end if;
13598 end if;
13599 end Constant_Redeclaration;
13601 ----------------------
13602 -- Constrain_Access --
13603 ----------------------
13605 procedure Constrain_Access
13606 (Def_Id : in out Entity_Id;
13607 S : Node_Id;
13608 Related_Nod : Node_Id)
13610 T : constant Entity_Id := Entity (Subtype_Mark (S));
13611 Desig_Type : constant Entity_Id := Designated_Type (T);
13612 Desig_Subtype : Entity_Id;
13613 Constraint_OK : Boolean := True;
13615 begin
13616 if Is_Array_Type (Desig_Type) then
13617 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13618 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13620 elsif (Is_Record_Type (Desig_Type)
13621 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13622 and then not Is_Constrained (Desig_Type)
13623 then
13624 -- If this is a constrained access definition for a record
13625 -- component, we leave the type as an unconstrained access,
13626 -- and mark the component so that its actual type is built
13627 -- at a point of use (e.g., an assignment statement). This
13628 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13630 if Desig_Type = Current_Scope
13631 and then No (Def_Id)
13632 then
13633 Desig_Subtype :=
13634 Create_Itype
13635 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13636 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13637 Def_Id := Entity (Subtype_Mark (S));
13639 -- We indicate that the component has a per-object constraint
13640 -- for treatment at a point of use, even though the constraint
13641 -- may be independent of discriminants of the enclosing type.
13643 if Nkind (Related_Nod) = N_Component_Declaration then
13644 Set_Has_Per_Object_Constraint
13645 (Defining_Identifier (Related_Nod));
13646 end if;
13648 -- This call added to ensure that the constraint is analyzed
13649 -- (needed for a B test). Note that we still return early from
13650 -- this procedure to avoid recursive processing.
13652 Constrain_Discriminated_Type
13653 (Desig_Subtype, S, Related_Nod, For_Access => True);
13654 return;
13655 end if;
13657 -- Enforce rule that the constraint is illegal if there is an
13658 -- unconstrained view of the designated type. This means that the
13659 -- partial view (either a private type declaration or a derivation
13660 -- from a private type) has no discriminants. (Defect Report
13661 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13663 -- Rule updated for Ada 2005: The private type is said to have
13664 -- a constrained partial view, given that objects of the type
13665 -- can be declared. Furthermore, the rule applies to all access
13666 -- types, unlike the rule concerning default discriminants (see
13667 -- RM 3.7.1(7/3))
13669 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13670 and then Has_Private_Declaration (Desig_Type)
13671 and then In_Open_Scopes (Scope (Desig_Type))
13672 and then Has_Discriminants (Desig_Type)
13673 then
13674 declare
13675 Pack : constant Node_Id :=
13676 Unit_Declaration_Node (Scope (Desig_Type));
13677 Decls : List_Id;
13678 Decl : Node_Id;
13680 begin
13681 if Nkind (Pack) = N_Package_Declaration then
13682 Decls := Visible_Declarations (Specification (Pack));
13683 Decl := First (Decls);
13684 while Present (Decl) loop
13685 if (Nkind (Decl) = N_Private_Type_Declaration
13686 and then Chars (Defining_Identifier (Decl)) =
13687 Chars (Desig_Type))
13689 or else
13690 (Nkind (Decl) = N_Full_Type_Declaration
13691 and then
13692 Chars (Defining_Identifier (Decl)) =
13693 Chars (Desig_Type)
13694 and then Is_Derived_Type (Desig_Type)
13695 and then
13696 Has_Private_Declaration (Etype (Desig_Type)))
13697 then
13698 if No (Discriminant_Specifications (Decl)) then
13699 Error_Msg_N
13700 ("cannot constrain access type if designated "
13701 & "type has constrained partial view", S);
13702 end if;
13704 exit;
13705 end if;
13707 Next (Decl);
13708 end loop;
13709 end if;
13710 end;
13711 end if;
13713 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13714 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13715 For_Access => True);
13717 elsif Is_Concurrent_Type (Desig_Type)
13718 and then not Is_Constrained (Desig_Type)
13719 then
13720 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13721 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13723 else
13724 Error_Msg_N ("invalid constraint on access type", S);
13726 -- We simply ignore an invalid constraint
13728 Desig_Subtype := Desig_Type;
13729 Constraint_OK := False;
13730 end if;
13732 if No (Def_Id) then
13733 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13734 else
13735 Mutate_Ekind (Def_Id, E_Access_Subtype);
13736 end if;
13738 if Constraint_OK then
13739 Set_Etype (Def_Id, Base_Type (T));
13741 if Is_Private_Type (Desig_Type) then
13742 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13743 end if;
13744 else
13745 Set_Etype (Def_Id, Any_Type);
13746 end if;
13748 Set_Size_Info (Def_Id, T);
13749 Set_Is_Constrained (Def_Id, Constraint_OK);
13750 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13751 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13752 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13753 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13755 Conditional_Delay (Def_Id, T);
13757 -- AI-363 : Subtypes of general access types whose designated types have
13758 -- default discriminants are disallowed. In instances, the rule has to
13759 -- be checked against the actual, of which T is the subtype. In a
13760 -- generic body, the rule is checked assuming that the actual type has
13761 -- defaulted discriminants.
13763 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13764 if Ekind (Base_Type (T)) = E_General_Access_Type
13765 and then Has_Defaulted_Discriminants (Desig_Type)
13766 then
13767 if Ada_Version < Ada_2005 then
13768 Error_Msg_N
13769 ("access subtype of general access type would not " &
13770 "be allowed in Ada 2005?y?", S);
13771 else
13772 Error_Msg_N
13773 ("access subtype of general access type not allowed", S);
13774 end if;
13776 Error_Msg_N ("\discriminants have defaults", S);
13778 elsif Is_Access_Type (T)
13779 and then Is_Generic_Type (Desig_Type)
13780 and then Has_Discriminants (Desig_Type)
13781 and then In_Package_Body (Current_Scope)
13782 then
13783 if Ada_Version < Ada_2005 then
13784 Error_Msg_N
13785 ("access subtype would not be allowed in generic body "
13786 & "in Ada 2005?y?", S);
13787 else
13788 Error_Msg_N
13789 ("access subtype not allowed in generic body", S);
13790 end if;
13792 Error_Msg_N
13793 ("\designated type is a discriminated formal", S);
13794 end if;
13795 end if;
13796 end Constrain_Access;
13798 ---------------------
13799 -- Constrain_Array --
13800 ---------------------
13802 procedure Constrain_Array
13803 (Def_Id : in out Entity_Id;
13804 SI : Node_Id;
13805 Related_Nod : Node_Id;
13806 Related_Id : Entity_Id;
13807 Suffix : Character)
13809 C : constant Node_Id := Constraint (SI);
13810 Number_Of_Constraints : constant Nat := List_Length (Constraints (C));
13811 Index : Node_Id;
13812 S, T : Entity_Id;
13813 Constraint_OK : Boolean := True;
13814 Is_FLB_Array_Subtype : Boolean := False;
13816 begin
13817 T := Entity (Subtype_Mark (SI));
13819 if Is_Access_Type (T) then
13820 T := Designated_Type (T);
13821 end if;
13823 T := Underlying_Type (T);
13825 -- If an index constraint follows a subtype mark in a subtype indication
13826 -- then the type or subtype denoted by the subtype mark must not already
13827 -- impose an index constraint. The subtype mark must denote either an
13828 -- unconstrained array type or an access type whose designated type
13829 -- is such an array type... (RM 3.6.1)
13831 if Is_Constrained (T) then
13832 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13833 Constraint_OK := False;
13835 else
13836 -- In either case, the index constraint must provide a discrete
13837 -- range for each index of the array type and the type of each
13838 -- discrete range must be the same as that of the corresponding
13839 -- index. (RM 3.6.1)
13841 if Number_Of_Constraints /= Number_Dimensions (T) then
13842 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13843 Constraint_OK := False;
13845 else
13846 S := First (Constraints (C));
13847 Index := First_Index (T);
13848 Analyze (Index);
13850 -- Apply constraints to each index type
13852 for J in 1 .. Number_Of_Constraints loop
13853 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13855 -- If the subtype of the index has been set to indicate that
13856 -- it has a fixed lower bound, then record that the subtype's
13857 -- entity will need to be marked as being a fixed-lower-bound
13858 -- array subtype.
13860 if S = First (Constraints (C)) then
13861 Is_FLB_Array_Subtype :=
13862 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13864 -- If the parent subtype (or should this be Etype of that?)
13865 -- is an FLB array subtype, we flag an error, because we
13866 -- don't currently allow subtypes of such subtypes to
13867 -- specify a fixed lower bound for any of their indexes,
13868 -- even if the index of the parent subtype is a "range <>"
13869 -- index.
13871 if Is_FLB_Array_Subtype
13872 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13873 then
13874 Error_Msg_NE
13875 ("index with fixed lower bound not allowed for subtype "
13876 & "of fixed-lower-bound }", S, T);
13878 Is_FLB_Array_Subtype := False;
13879 end if;
13881 elsif Is_FLB_Array_Subtype
13882 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13883 then
13884 Error_Msg_NE
13885 ("constrained index not allowed for fixed-lower-bound "
13886 & "subtype of}", S, T);
13888 elsif not Is_FLB_Array_Subtype
13889 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13890 then
13891 Error_Msg_NE
13892 ("index with fixed lower bound not allowed for "
13893 & "constrained subtype of}", S, T);
13894 end if;
13896 Next (Index);
13897 Next (S);
13898 end loop;
13900 end if;
13901 end if;
13903 if No (Def_Id) then
13904 Def_Id :=
13905 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13906 Set_Parent (Def_Id, Related_Nod);
13908 else
13909 Mutate_Ekind (Def_Id, E_Array_Subtype);
13910 end if;
13912 Set_Size_Info (Def_Id, (T));
13913 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13914 Set_Etype (Def_Id, Base_Type (T));
13916 if Constraint_OK then
13917 Set_First_Index (Def_Id, First (Constraints (C)));
13918 else
13919 Set_First_Index (Def_Id, First_Index (T));
13920 end if;
13922 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13923 Set_Is_Fixed_Lower_Bound_Array_Subtype
13924 (Def_Id, Is_FLB_Array_Subtype);
13925 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13926 Set_Is_Independent (Def_Id, Is_Independent (T));
13927 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13929 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13930 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13932 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13933 -- We need to initialize the attribute because if Def_Id is previously
13934 -- analyzed through a limited_with clause, it will have the attributes
13935 -- of an incomplete type, one of which is an Elist that overlaps the
13936 -- Packed_Array_Impl_Type field.
13938 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13940 -- Build a freeze node if parent still needs one. Also make sure that
13941 -- the Depends_On_Private status is set because the subtype will need
13942 -- reprocessing at the time the base type does, and also we must set a
13943 -- conditional delay.
13945 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13946 Conditional_Delay (Def_Id, T);
13947 end Constrain_Array;
13949 ------------------------------
13950 -- Constrain_Component_Type --
13951 ------------------------------
13953 function Constrain_Component_Type
13954 (Comp : Entity_Id;
13955 Constrained_Typ : Entity_Id;
13956 Related_Node : Node_Id;
13957 Typ : Entity_Id;
13958 Constraints : Elist_Id) return Entity_Id
13960 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13961 Compon_Type : constant Entity_Id := Etype (Comp);
13963 function Build_Constrained_Array_Type
13964 (Old_Type : Entity_Id) return Entity_Id;
13965 -- If Old_Type is an array type, one of whose indexes is constrained
13966 -- by a discriminant, build an Itype whose constraint replaces the
13967 -- discriminant with its value in the constraint.
13969 function Build_Constrained_Discriminated_Type
13970 (Old_Type : Entity_Id) return Entity_Id;
13971 -- Ditto for record components. Handle the case where the constraint
13972 -- is a conversion of the discriminant value, introduced during
13973 -- expansion.
13975 function Build_Constrained_Access_Type
13976 (Old_Type : Entity_Id) return Entity_Id;
13977 -- Ditto for access types. Makes use of previous two functions, to
13978 -- constrain designated type.
13980 function Is_Discriminant (Expr : Node_Id) return Boolean;
13981 -- Returns True if Expr is a discriminant
13983 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13984 -- Find the value of a discriminant named by Discr_Expr in Constraints
13986 -----------------------------------
13987 -- Build_Constrained_Access_Type --
13988 -----------------------------------
13990 function Build_Constrained_Access_Type
13991 (Old_Type : Entity_Id) return Entity_Id
13993 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13994 Itype : Entity_Id;
13995 Desig_Subtype : Entity_Id;
13996 Scop : Entity_Id;
13998 begin
13999 -- If the original access type was not embedded in the enclosing
14000 -- type definition, there is no need to produce a new access
14001 -- subtype. In fact every access type with an explicit constraint
14002 -- generates an itype whose scope is the enclosing record.
14004 if not Is_Type (Scope (Old_Type)) then
14005 return Old_Type;
14007 elsif Is_Array_Type (Desig_Type) then
14008 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
14010 elsif Has_Discriminants (Desig_Type) then
14012 -- This may be an access type to an enclosing record type for
14013 -- which we are constructing the constrained components. Return
14014 -- the enclosing record subtype. This is not always correct,
14015 -- but avoids infinite recursion. ???
14017 Desig_Subtype := Any_Type;
14019 for J in reverse 0 .. Scope_Stack.Last loop
14020 Scop := Scope_Stack.Table (J).Entity;
14022 if Is_Type (Scop)
14023 and then Base_Type (Scop) = Base_Type (Desig_Type)
14024 then
14025 Desig_Subtype := Scop;
14026 end if;
14028 exit when not Is_Type (Scop);
14029 end loop;
14031 if Desig_Subtype = Any_Type then
14032 Desig_Subtype :=
14033 Build_Constrained_Discriminated_Type (Desig_Type);
14034 end if;
14036 else
14037 return Old_Type;
14038 end if;
14040 if Desig_Subtype /= Desig_Type then
14042 -- The Related_Node better be here or else we won't be able
14043 -- to attach new itypes to a node in the tree.
14045 pragma Assert (Present (Related_Node));
14047 Itype := Create_Itype (E_Access_Subtype, Related_Node);
14049 Set_Etype (Itype, Base_Type (Old_Type));
14050 Set_Size_Info (Itype, (Old_Type));
14051 Set_Directly_Designated_Type (Itype, Desig_Subtype);
14052 Set_Depends_On_Private (Itype, Has_Private_Component
14053 (Old_Type));
14054 Set_Is_Access_Constant (Itype, Is_Access_Constant
14055 (Old_Type));
14057 -- The new itype needs freezing when it depends on a not frozen
14058 -- type and the enclosing subtype needs freezing.
14060 if Has_Delayed_Freeze (Constrained_Typ)
14061 and then not Is_Frozen (Constrained_Typ)
14062 then
14063 Conditional_Delay (Itype, Base_Type (Old_Type));
14064 end if;
14066 return Itype;
14068 else
14069 return Old_Type;
14070 end if;
14071 end Build_Constrained_Access_Type;
14073 ----------------------------------
14074 -- Build_Constrained_Array_Type --
14075 ----------------------------------
14077 function Build_Constrained_Array_Type
14078 (Old_Type : Entity_Id) return Entity_Id
14080 Lo_Expr : Node_Id;
14081 Hi_Expr : Node_Id;
14082 Old_Index : Node_Id;
14083 Range_Node : Node_Id;
14084 Constr_List : List_Id;
14086 Need_To_Create_Itype : Boolean := False;
14088 begin
14089 Old_Index := First_Index (Old_Type);
14090 while Present (Old_Index) loop
14091 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14093 if Is_Discriminant (Lo_Expr)
14094 or else
14095 Is_Discriminant (Hi_Expr)
14096 then
14097 Need_To_Create_Itype := True;
14098 exit;
14099 end if;
14101 Next_Index (Old_Index);
14102 end loop;
14104 if Need_To_Create_Itype then
14105 Constr_List := New_List;
14107 Old_Index := First_Index (Old_Type);
14108 while Present (Old_Index) loop
14109 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14111 if Is_Discriminant (Lo_Expr) then
14112 Lo_Expr := Get_Discr_Value (Lo_Expr);
14113 end if;
14115 if Is_Discriminant (Hi_Expr) then
14116 Hi_Expr := Get_Discr_Value (Hi_Expr);
14117 end if;
14119 Range_Node :=
14120 Make_Range
14121 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14123 Append (Range_Node, To => Constr_List);
14125 Next_Index (Old_Index);
14126 end loop;
14128 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14130 else
14131 return Old_Type;
14132 end if;
14133 end Build_Constrained_Array_Type;
14135 ------------------------------------------
14136 -- Build_Constrained_Discriminated_Type --
14137 ------------------------------------------
14139 function Build_Constrained_Discriminated_Type
14140 (Old_Type : Entity_Id) return Entity_Id
14142 Expr : Node_Id;
14143 Constr_List : List_Id;
14144 Old_Constraint : Elmt_Id;
14146 Need_To_Create_Itype : Boolean := False;
14148 begin
14149 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14150 while Present (Old_Constraint) loop
14151 Expr := Node (Old_Constraint);
14153 if Is_Discriminant (Expr) then
14154 Need_To_Create_Itype := True;
14155 exit;
14157 -- After expansion of discriminated task types, the value
14158 -- of the discriminant may be converted to a run-time type
14159 -- for restricted run-times. Propagate the value of the
14160 -- discriminant as well, so that e.g. the secondary stack
14161 -- component has a static constraint. Necessary for LLVM.
14163 elsif Nkind (Expr) = N_Type_Conversion
14164 and then Is_Discriminant (Expression (Expr))
14165 then
14166 Need_To_Create_Itype := True;
14167 exit;
14168 end if;
14170 Next_Elmt (Old_Constraint);
14171 end loop;
14173 if Need_To_Create_Itype then
14174 Constr_List := New_List;
14176 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14177 while Present (Old_Constraint) loop
14178 Expr := Node (Old_Constraint);
14180 if Is_Discriminant (Expr) then
14181 Expr := Get_Discr_Value (Expr);
14183 elsif Nkind (Expr) = N_Type_Conversion
14184 and then Is_Discriminant (Expression (Expr))
14185 then
14186 Expr := New_Copy_Tree (Expr);
14187 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14188 end if;
14190 Append (New_Copy_Tree (Expr), To => Constr_List);
14192 Next_Elmt (Old_Constraint);
14193 end loop;
14195 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14197 else
14198 return Old_Type;
14199 end if;
14200 end Build_Constrained_Discriminated_Type;
14202 ---------------------
14203 -- Get_Discr_Value --
14204 ---------------------
14206 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14207 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14208 -- Entity of a discriminant that appear as a standalone expression in
14209 -- the constraint of a component.
14211 D : Entity_Id;
14212 E : Elmt_Id;
14214 begin
14215 -- The discriminant may be declared for the type, in which case we
14216 -- find it by iterating over the list of discriminants. If the
14217 -- discriminant is inherited from a parent type, it appears as the
14218 -- corresponding discriminant of the current type. This will be the
14219 -- case when constraining an inherited component whose constraint is
14220 -- given by a discriminant of the parent.
14222 D := First_Discriminant (Typ);
14223 E := First_Elmt (Constraints);
14225 while Present (D) loop
14226 if D = Discr_Id
14227 or else D = CR_Discriminant (Discr_Id)
14228 or else Corresponding_Discriminant (D) = Discr_Id
14229 then
14230 return New_Copy_Tree (Node (E));
14231 end if;
14233 Next_Discriminant (D);
14234 Next_Elmt (E);
14235 end loop;
14237 -- The Corresponding_Discriminant mechanism is incomplete, because
14238 -- the correspondence between new and old discriminants is not one
14239 -- to one: one new discriminant can constrain several old ones. In
14240 -- that case, scan sequentially the stored_constraint, the list of
14241 -- discriminants of the parents, and the constraints.
14243 -- Previous code checked for the present of the Stored_Constraint
14244 -- list for the derived type, but did not use it at all. Should it
14245 -- be present when the component is a discriminated task type?
14247 if Is_Derived_Type (Typ)
14248 and then Scope (Discr_Id) = Etype (Typ)
14249 then
14250 D := First_Discriminant (Etype (Typ));
14251 E := First_Elmt (Constraints);
14252 while Present (D) loop
14253 if D = Discr_Id then
14254 return New_Copy_Tree (Node (E));
14255 end if;
14257 Next_Discriminant (D);
14258 Next_Elmt (E);
14259 end loop;
14260 end if;
14262 -- Something is wrong if we did not find the value
14264 raise Program_Error;
14265 end Get_Discr_Value;
14267 ---------------------
14268 -- Is_Discriminant --
14269 ---------------------
14271 function Is_Discriminant (Expr : Node_Id) return Boolean is
14272 Discrim_Scope : Entity_Id;
14274 begin
14275 if Denotes_Discriminant (Expr) then
14276 Discrim_Scope := Scope (Entity (Expr));
14278 -- Either we have a reference to one of Typ's discriminants,
14280 pragma Assert (Discrim_Scope = Typ
14282 -- or to the discriminants of the parent type, in the case
14283 -- of a derivation of a tagged type with variants.
14285 or else Discrim_Scope = Etype (Typ)
14286 or else Full_View (Discrim_Scope) = Etype (Typ)
14288 -- or same as above for the case where the discriminants
14289 -- were declared in Typ's private view.
14291 or else (Is_Private_Type (Discrim_Scope)
14292 and then Chars (Discrim_Scope) = Chars (Typ))
14294 -- or else we are deriving from the full view and the
14295 -- discriminant is declared in the private entity.
14297 or else (Is_Private_Type (Typ)
14298 and then Chars (Discrim_Scope) = Chars (Typ))
14300 -- Or we are constrained the corresponding record of a
14301 -- synchronized type that completes a private declaration.
14303 or else (Is_Concurrent_Record_Type (Typ)
14304 and then
14305 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14307 -- or we have a class-wide type, in which case make sure the
14308 -- discriminant found belongs to the root type.
14310 or else (Is_Class_Wide_Type (Typ)
14311 and then Etype (Typ) = Discrim_Scope));
14313 return True;
14314 end if;
14316 -- In all other cases we have something wrong
14318 return False;
14319 end Is_Discriminant;
14321 -- Start of processing for Constrain_Component_Type
14323 begin
14324 if Nkind (Parent (Comp)) = N_Component_Declaration
14325 and then Comes_From_Source (Parent (Comp))
14326 and then Comes_From_Source
14327 (Subtype_Indication (Component_Definition (Parent (Comp))))
14328 and then
14329 Is_Entity_Name
14330 (Subtype_Indication (Component_Definition (Parent (Comp))))
14331 then
14332 return Compon_Type;
14334 elsif Is_Array_Type (Compon_Type) then
14335 return Build_Constrained_Array_Type (Compon_Type);
14337 elsif Has_Discriminants (Compon_Type) then
14338 return Build_Constrained_Discriminated_Type (Compon_Type);
14340 elsif Is_Access_Type (Compon_Type) then
14341 return Build_Constrained_Access_Type (Compon_Type);
14343 else
14344 return Compon_Type;
14345 end if;
14346 end Constrain_Component_Type;
14348 --------------------------
14349 -- Constrain_Concurrent --
14350 --------------------------
14352 -- For concurrent types, the associated record value type carries the same
14353 -- discriminants, so when we constrain a concurrent type, we must constrain
14354 -- the corresponding record type as well.
14356 procedure Constrain_Concurrent
14357 (Def_Id : in out Entity_Id;
14358 SI : Node_Id;
14359 Related_Nod : Node_Id;
14360 Related_Id : Entity_Id;
14361 Suffix : Character)
14363 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14364 -- case of a private subtype (needed when only doing semantic analysis).
14366 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14367 T_Val : Entity_Id;
14369 begin
14370 if Is_Access_Type (T_Ent) then
14371 T_Ent := Designated_Type (T_Ent);
14372 end if;
14374 T_Val := Corresponding_Record_Type (T_Ent);
14376 if Present (T_Val) then
14378 if No (Def_Id) then
14379 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14381 -- Elaborate itype now, as it may be used in a subsequent
14382 -- synchronized operation in another scope.
14384 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14385 Build_Itype_Reference (Def_Id, Related_Nod);
14386 end if;
14387 end if;
14389 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14390 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14392 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14393 Set_Corresponding_Record_Type (Def_Id,
14394 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14396 else
14397 -- If there is no associated record, expansion is disabled and this
14398 -- is a generic context. Create a subtype in any case, so that
14399 -- semantic analysis can proceed.
14401 if No (Def_Id) then
14402 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14403 end if;
14405 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14406 end if;
14407 end Constrain_Concurrent;
14409 ------------------------------------
14410 -- Constrain_Corresponding_Record --
14411 ------------------------------------
14413 function Constrain_Corresponding_Record
14414 (Prot_Subt : Entity_Id;
14415 Corr_Rec : Entity_Id;
14416 Related_Nod : Node_Id) return Entity_Id
14418 T_Sub : constant Entity_Id :=
14419 Create_Itype
14420 (Ekind => E_Record_Subtype,
14421 Related_Nod => Related_Nod,
14422 Related_Id => Corr_Rec,
14423 Suffix => 'C',
14424 Suffix_Index => -1);
14426 begin
14427 Set_Etype (T_Sub, Corr_Rec);
14428 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14429 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14430 Set_Is_Constrained (T_Sub, True);
14431 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14432 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14433 Set_Direct_Primitive_Operations
14434 (T_Sub, Direct_Primitive_Operations (Corr_Rec));
14436 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14437 Set_Discriminant_Constraint
14438 (T_Sub, Discriminant_Constraint (Prot_Subt));
14439 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14440 Create_Constrained_Components
14441 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14442 end if;
14444 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14446 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14447 Conditional_Delay (T_Sub, Corr_Rec);
14449 else
14450 -- This is a component subtype: it will be frozen in the context of
14451 -- the enclosing record's init_proc, so that discriminant references
14452 -- are resolved to discriminals. (Note: we used to skip freezing
14453 -- altogether in that case, which caused errors downstream for
14454 -- components of a bit packed array type).
14456 Set_Has_Delayed_Freeze (T_Sub);
14457 end if;
14459 return T_Sub;
14460 end Constrain_Corresponding_Record;
14462 -----------------------
14463 -- Constrain_Decimal --
14464 -----------------------
14466 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14467 T : constant Entity_Id := Entity (Subtype_Mark (S));
14468 C : constant Node_Id := Constraint (S);
14469 Loc : constant Source_Ptr := Sloc (C);
14470 Range_Expr : Node_Id;
14471 Digits_Expr : Node_Id;
14472 Digits_Val : Uint;
14473 Bound_Val : Ureal;
14475 begin
14476 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14478 if Nkind (C) = N_Range_Constraint then
14479 Range_Expr := Range_Expression (C);
14480 Digits_Val := Digits_Value (T);
14482 else
14483 pragma Assert (Nkind (C) = N_Digits_Constraint);
14485 Digits_Expr := Digits_Expression (C);
14486 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14488 Check_Digits_Expression (Digits_Expr);
14489 Digits_Val := Expr_Value (Digits_Expr);
14491 if Digits_Val > Digits_Value (T) then
14492 Error_Msg_N
14493 ("digits expression is incompatible with subtype", C);
14494 Digits_Val := Digits_Value (T);
14495 end if;
14497 if Present (Range_Constraint (C)) then
14498 Range_Expr := Range_Expression (Range_Constraint (C));
14499 else
14500 Range_Expr := Empty;
14501 end if;
14502 end if;
14504 Set_Etype (Def_Id, Base_Type (T));
14505 Set_Size_Info (Def_Id, (T));
14506 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14507 Set_Delta_Value (Def_Id, Delta_Value (T));
14508 Set_Scale_Value (Def_Id, Scale_Value (T));
14509 Set_Small_Value (Def_Id, Small_Value (T));
14510 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14511 Set_Digits_Value (Def_Id, Digits_Val);
14513 -- Manufacture range from given digits value if no range present
14515 if No (Range_Expr) then
14516 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14517 Range_Expr :=
14518 Make_Range (Loc,
14519 Low_Bound =>
14520 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14521 High_Bound =>
14522 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14523 end if;
14525 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14526 Set_Discrete_RM_Size (Def_Id);
14528 -- Unconditionally delay the freeze, since we cannot set size
14529 -- information in all cases correctly until the freeze point.
14531 Set_Has_Delayed_Freeze (Def_Id);
14532 end Constrain_Decimal;
14534 ----------------------------------
14535 -- Constrain_Discriminated_Type --
14536 ----------------------------------
14538 procedure Constrain_Discriminated_Type
14539 (Def_Id : Entity_Id;
14540 S : Node_Id;
14541 Related_Nod : Node_Id;
14542 For_Access : Boolean := False)
14544 E : Entity_Id := Entity (Subtype_Mark (S));
14545 T : Entity_Id;
14547 procedure Fixup_Bad_Constraint;
14548 -- Called after finding a bad constraint, and after having posted an
14549 -- appropriate error message. The goal is to leave type Def_Id in as
14550 -- reasonable state as possible.
14552 --------------------------
14553 -- Fixup_Bad_Constraint --
14554 --------------------------
14556 procedure Fixup_Bad_Constraint is
14557 begin
14558 -- Set a reasonable Ekind for the entity, including incomplete types.
14560 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14562 -- Set Etype to the known type, to reduce chances of cascaded errors
14564 Set_Etype (Def_Id, E);
14565 Set_Error_Posted (Def_Id);
14566 end Fixup_Bad_Constraint;
14568 -- Local variables
14570 C : Node_Id;
14571 Constr : Elist_Id := New_Elmt_List;
14573 -- Start of processing for Constrain_Discriminated_Type
14575 begin
14576 C := Constraint (S);
14578 -- A discriminant constraint is only allowed in a subtype indication,
14579 -- after a subtype mark. This subtype mark must denote either a type
14580 -- with discriminants, or an access type whose designated type is a
14581 -- type with discriminants. A discriminant constraint specifies the
14582 -- values of these discriminants (RM 3.7.2(5)).
14584 T := Base_Type (Entity (Subtype_Mark (S)));
14586 if Is_Access_Type (T) then
14587 T := Designated_Type (T);
14588 end if;
14590 -- In an instance it may be necessary to retrieve the full view of a
14591 -- type with unknown discriminants, or a full view with defaulted
14592 -- discriminants. In other contexts the constraint is illegal.
14593 -- This relaxation of legality checking may also be needed in
14594 -- compiler-generated Put_Image or streaming subprograms (hence
14595 -- the Comes_From_Source test).
14597 if (In_Instance or not Comes_From_Source (S))
14598 and then Is_Private_Type (T)
14599 and then Present (Full_View (T))
14600 and then
14601 (Has_Unknown_Discriminants (T)
14602 or else
14603 (not Has_Discriminants (T)
14604 and then Has_Defaulted_Discriminants (Full_View (T))))
14605 then
14606 T := Full_View (T);
14607 E := Full_View (E);
14608 end if;
14610 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14611 -- generating an error for access-to-incomplete subtypes.
14613 if Ada_Version >= Ada_2005
14614 and then Ekind (T) = E_Incomplete_Type
14615 and then Nkind (Parent (S)) = N_Subtype_Declaration
14616 and then not Is_Itype (Def_Id)
14617 then
14618 -- A little sanity check: emit an error message if the type has
14619 -- discriminants to begin with. Type T may be a regular incomplete
14620 -- type or imported via a limited with clause.
14622 if Has_Discriminants (T)
14623 or else (From_Limited_With (T)
14624 and then Present (Non_Limited_View (T))
14625 and then Nkind (Parent (Non_Limited_View (T))) =
14626 N_Full_Type_Declaration
14627 and then Present (Discriminant_Specifications
14628 (Parent (Non_Limited_View (T)))))
14629 then
14630 Error_Msg_N
14631 ("(Ada 2005) incomplete subtype may not be constrained", C);
14632 else
14633 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14634 end if;
14636 Fixup_Bad_Constraint;
14637 return;
14639 -- Check that the type has visible discriminants. The type may be
14640 -- a private type with unknown discriminants whose full view has
14641 -- discriminants which are invisible.
14643 elsif not Has_Discriminants (T)
14644 or else
14645 (Has_Unknown_Discriminants (T)
14646 and then Is_Private_Type (T))
14647 then
14648 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14649 Fixup_Bad_Constraint;
14650 return;
14652 elsif Is_Constrained (E)
14653 or else (Ekind (E) = E_Class_Wide_Subtype
14654 and then Present (Discriminant_Constraint (E)))
14655 then
14656 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14657 Fixup_Bad_Constraint;
14658 return;
14659 end if;
14661 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14662 -- applies to the base type.
14664 T := Base_Type (T);
14666 Constr := Build_Discriminant_Constraints (T, S);
14668 -- If the list returned was empty we had an error in building the
14669 -- discriminant constraint. We have also already signalled an error
14670 -- in the incomplete type case
14672 if Is_Empty_Elmt_List (Constr) then
14673 Fixup_Bad_Constraint;
14674 return;
14675 end if;
14677 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14678 end Constrain_Discriminated_Type;
14680 ---------------------------
14681 -- Constrain_Enumeration --
14682 ---------------------------
14684 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14685 T : constant Entity_Id := Entity (Subtype_Mark (S));
14686 C : constant Node_Id := Constraint (S);
14688 begin
14689 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14691 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14692 Set_Etype (Def_Id, Base_Type (T));
14693 Set_Size_Info (Def_Id, (T));
14694 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14695 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14697 -- Inherit the chain of representation items instead of replacing it
14698 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14699 -- the derived type as a subtype declaration and the former needs to
14700 -- preserve existing representation items (see Build_Derived_Type).
14702 Inherit_Rep_Item_Chain (Def_Id, T);
14704 Set_Discrete_RM_Size (Def_Id);
14705 end Constrain_Enumeration;
14707 ----------------------
14708 -- Constrain_Float --
14709 ----------------------
14711 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14712 T : constant Entity_Id := Entity (Subtype_Mark (S));
14713 C : Node_Id;
14714 D : Node_Id;
14715 Rais : Node_Id;
14717 begin
14718 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14720 Set_Etype (Def_Id, Base_Type (T));
14721 Set_Size_Info (Def_Id, (T));
14722 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14724 -- Process the constraint
14726 C := Constraint (S);
14728 -- Digits constraint present
14730 if Nkind (C) = N_Digits_Constraint then
14731 Check_Restriction (No_Obsolescent_Features, C);
14733 if Warn_On_Obsolescent_Feature then
14734 Error_Msg_N
14735 ("subtype digits constraint is an " &
14736 "obsolescent feature (RM J.3(8))?j?", C);
14737 end if;
14739 D := Digits_Expression (C);
14740 Analyze_And_Resolve (D, Any_Integer);
14741 Check_Digits_Expression (D);
14742 Set_Digits_Value (Def_Id, Expr_Value (D));
14744 -- Check that digits value is in range. Obviously we can do this
14745 -- at compile time, but it is strictly a runtime check, and of
14746 -- course there is an ACVC test that checks this.
14748 if Digits_Value (Def_Id) > Digits_Value (T) then
14749 Error_Msg_Uint_1 := Digits_Value (T);
14750 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14751 Rais :=
14752 Make_Raise_Constraint_Error (Sloc (D),
14753 Reason => CE_Range_Check_Failed);
14754 Insert_Action (Declaration_Node (Def_Id), Rais);
14755 end if;
14757 C := Range_Constraint (C);
14759 -- No digits constraint present
14761 else
14762 Set_Digits_Value (Def_Id, Digits_Value (T));
14763 end if;
14765 -- Range constraint present
14767 if Nkind (C) = N_Range_Constraint then
14768 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14770 -- No range constraint present
14772 else
14773 pragma Assert (No (C));
14774 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14775 end if;
14777 Set_Is_Constrained (Def_Id);
14778 end Constrain_Float;
14780 ---------------------
14781 -- Constrain_Index --
14782 ---------------------
14784 procedure Constrain_Index
14785 (Index : Node_Id;
14786 S : Node_Id;
14787 Related_Nod : Node_Id;
14788 Related_Id : Entity_Id;
14789 Suffix : Character;
14790 Suffix_Index : Pos)
14792 Def_Id : Entity_Id;
14793 R : Node_Id := Empty;
14794 T : constant Entity_Id := Etype (Index);
14795 Is_FLB_Index : Boolean := False;
14797 begin
14798 Def_Id :=
14799 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14800 Set_Etype (Def_Id, Base_Type (T));
14802 if Nkind (S) = N_Range
14803 or else
14804 (Nkind (S) = N_Attribute_Reference
14805 and then Attribute_Name (S) = Name_Range)
14806 then
14807 -- A Range attribute will be transformed into N_Range by Resolve
14809 -- If a range has an Empty upper bound, then remember that for later
14810 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14811 -- flag, and also set the upper bound of the range to the index
14812 -- subtype's upper bound rather than leaving it Empty. In truth,
14813 -- that upper bound corresponds to a box ("<>"), but it's convenient
14814 -- to set it to the upper bound to avoid needing to add special tests
14815 -- in various places for an Empty upper bound, and in any case it
14816 -- accurately characterizes the index's range of values.
14818 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14819 Is_FLB_Index := True;
14820 Set_High_Bound (S, Type_High_Bound (T));
14821 end if;
14823 R := S;
14825 Process_Range_Expr_In_Decl (R, T);
14827 if not Error_Posted (S)
14828 and then
14829 (Nkind (S) /= N_Range
14830 or else not Covers (T, (Etype (Low_Bound (S))))
14831 or else not Covers (T, (Etype (High_Bound (S)))))
14832 then
14833 if Base_Type (T) /= Any_Type
14834 and then Etype (Low_Bound (S)) /= Any_Type
14835 and then Etype (High_Bound (S)) /= Any_Type
14836 then
14837 Error_Msg_N ("range expected", S);
14838 end if;
14839 end if;
14841 elsif Nkind (S) = N_Subtype_Indication then
14843 -- The parser has verified that this is a discrete indication
14845 Resolve_Discrete_Subtype_Indication (S, T);
14846 Bad_Predicated_Subtype_Use
14847 ("subtype& has predicate, not allowed in index constraint",
14848 S, Entity (Subtype_Mark (S)));
14850 R := Range_Expression (Constraint (S));
14852 -- Capture values of bounds and generate temporaries for them if
14853 -- needed, since checks may cause duplication of the expressions
14854 -- which must not be reevaluated.
14856 -- The forced evaluation removes side effects from expressions, which
14857 -- should occur also in GNATprove mode. Otherwise, we end up with
14858 -- unexpected insertions of actions at places where this is not
14859 -- supposed to occur, e.g. on default parameters of a call.
14861 if Expander_Active or GNATprove_Mode then
14862 Force_Evaluation
14863 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14864 Force_Evaluation
14865 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14866 end if;
14868 elsif Nkind (S) = N_Discriminant_Association then
14870 -- Syntactically valid in subtype indication
14872 Error_Msg_N ("invalid index constraint", S);
14873 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14874 return;
14876 -- Subtype_Mark case, no anonymous subtypes to construct
14878 else
14879 Analyze (S);
14881 if Is_Entity_Name (S) then
14882 if not Is_Type (Entity (S)) then
14883 Error_Msg_N ("expect subtype mark for index constraint", S);
14885 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14886 Wrong_Type (S, Base_Type (T));
14888 -- Check error of subtype with predicate in index constraint
14890 else
14891 Bad_Predicated_Subtype_Use
14892 ("subtype& has predicate, not allowed in index constraint",
14893 S, Entity (S));
14894 end if;
14896 return;
14898 else
14899 Error_Msg_N ("invalid index constraint", S);
14900 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14901 return;
14902 end if;
14903 end if;
14905 -- Complete construction of the Itype
14907 if Is_Modular_Integer_Type (T) then
14908 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14910 elsif Is_Integer_Type (T) then
14911 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14913 else
14914 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14915 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14916 Set_First_Literal (Def_Id, First_Literal (T));
14917 end if;
14919 Set_Size_Info (Def_Id, (T));
14920 Copy_RM_Size (To => Def_Id, From => T);
14921 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14923 -- If this is a range for a fixed-lower-bound subtype, then set the
14924 -- index itype's low bound to the FLB and the index itype's upper bound
14925 -- to the high bound of the parent array type's index subtype. Also,
14926 -- mark the itype as an FLB index subtype.
14928 if Nkind (S) = N_Range and then Is_FLB_Index then
14929 Set_Scalar_Range
14930 (Def_Id,
14931 Make_Range (Sloc (S),
14932 Low_Bound => Low_Bound (S),
14933 High_Bound => Type_High_Bound (T)));
14934 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14936 else
14937 Set_Scalar_Range (Def_Id, R);
14938 end if;
14940 Set_Etype (S, Def_Id);
14941 Set_Discrete_RM_Size (Def_Id);
14942 end Constrain_Index;
14944 -----------------------
14945 -- Constrain_Integer --
14946 -----------------------
14948 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14949 T : constant Entity_Id := Entity (Subtype_Mark (S));
14950 C : constant Node_Id := Constraint (S);
14952 begin
14953 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14955 if Is_Modular_Integer_Type (T) then
14956 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14957 else
14958 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14959 end if;
14961 Set_Etype (Def_Id, Base_Type (T));
14962 Set_Size_Info (Def_Id, (T));
14963 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14964 Set_Discrete_RM_Size (Def_Id);
14965 end Constrain_Integer;
14967 ------------------------------
14968 -- Constrain_Ordinary_Fixed --
14969 ------------------------------
14971 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14972 T : constant Entity_Id := Entity (Subtype_Mark (S));
14973 C : Node_Id;
14974 D : Node_Id;
14975 Rais : Node_Id;
14977 begin
14978 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14979 Set_Etype (Def_Id, Base_Type (T));
14980 Set_Size_Info (Def_Id, (T));
14981 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14982 Set_Small_Value (Def_Id, Small_Value (T));
14984 -- Process the constraint
14986 C := Constraint (S);
14988 -- Delta constraint present
14990 if Nkind (C) = N_Delta_Constraint then
14991 Check_Restriction (No_Obsolescent_Features, C);
14993 if Warn_On_Obsolescent_Feature then
14994 Error_Msg_S
14995 ("subtype delta constraint is an " &
14996 "obsolescent feature (RM J.3(7))?j?");
14997 end if;
14999 D := Delta_Expression (C);
15000 Analyze_And_Resolve (D, Any_Real);
15001 Check_Delta_Expression (D);
15002 Set_Delta_Value (Def_Id, Expr_Value_R (D));
15004 -- Check that delta value is in range. Obviously we can do this
15005 -- at compile time, but it is strictly a runtime check, and of
15006 -- course there is an ACVC test that checks this.
15008 if Delta_Value (Def_Id) < Delta_Value (T) then
15009 Error_Msg_N ("??delta value is too small", D);
15010 Rais :=
15011 Make_Raise_Constraint_Error (Sloc (D),
15012 Reason => CE_Range_Check_Failed);
15013 Insert_Action (Declaration_Node (Def_Id), Rais);
15014 end if;
15016 C := Range_Constraint (C);
15018 -- No delta constraint present
15020 else
15021 Set_Delta_Value (Def_Id, Delta_Value (T));
15022 end if;
15024 -- Range constraint present
15026 if Nkind (C) = N_Range_Constraint then
15027 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
15029 -- No range constraint present
15031 else
15032 pragma Assert (No (C));
15033 Set_Scalar_Range (Def_Id, Scalar_Range (T));
15034 end if;
15036 Set_Discrete_RM_Size (Def_Id);
15038 -- Unconditionally delay the freeze, since we cannot set size
15039 -- information in all cases correctly until the freeze point.
15041 Set_Has_Delayed_Freeze (Def_Id);
15042 end Constrain_Ordinary_Fixed;
15044 -----------------------
15045 -- Contain_Interface --
15046 -----------------------
15048 function Contain_Interface
15049 (Iface : Entity_Id;
15050 Ifaces : Elist_Id) return Boolean
15052 Iface_Elmt : Elmt_Id;
15054 begin
15055 if Present (Ifaces) then
15056 Iface_Elmt := First_Elmt (Ifaces);
15057 while Present (Iface_Elmt) loop
15058 if Node (Iface_Elmt) = Iface then
15059 return True;
15060 end if;
15062 Next_Elmt (Iface_Elmt);
15063 end loop;
15064 end if;
15066 return False;
15067 end Contain_Interface;
15069 ---------------------------
15070 -- Convert_Scalar_Bounds --
15071 ---------------------------
15073 procedure Convert_Scalar_Bounds
15074 (N : Node_Id;
15075 Parent_Type : Entity_Id;
15076 Derived_Type : Entity_Id;
15077 Loc : Source_Ptr)
15079 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
15081 Lo : Node_Id;
15082 Hi : Node_Id;
15083 Rng : Node_Id;
15085 begin
15086 -- Defend against previous errors
15088 if No (Scalar_Range (Derived_Type)) then
15089 Check_Error_Detected;
15090 return;
15091 end if;
15093 Lo := Build_Scalar_Bound
15094 (Type_Low_Bound (Derived_Type),
15095 Parent_Type, Implicit_Base);
15097 Hi := Build_Scalar_Bound
15098 (Type_High_Bound (Derived_Type),
15099 Parent_Type, Implicit_Base);
15101 Rng :=
15102 Make_Range (Loc,
15103 Low_Bound => Lo,
15104 High_Bound => Hi);
15106 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
15108 Set_Parent (Rng, N);
15109 Set_Scalar_Range (Derived_Type, Rng);
15111 -- Analyze the bounds
15113 Analyze_And_Resolve (Lo, Implicit_Base);
15114 Analyze_And_Resolve (Hi, Implicit_Base);
15116 -- Analyze the range itself, except that we do not analyze it if
15117 -- the bounds are real literals, and we have a fixed-point type.
15118 -- The reason for this is that we delay setting the bounds in this
15119 -- case till we know the final Small and Size values (see circuit
15120 -- in Freeze.Freeze_Fixed_Point_Type for further details).
15122 if Is_Fixed_Point_Type (Parent_Type)
15123 and then Nkind (Lo) = N_Real_Literal
15124 and then Nkind (Hi) = N_Real_Literal
15125 then
15126 return;
15128 -- Here we do the analysis of the range
15130 -- Note: we do this manually, since if we do a normal Analyze and
15131 -- Resolve call, there are problems with the conversions used for
15132 -- the derived type range.
15134 else
15135 Set_Etype (Rng, Implicit_Base);
15136 Set_Analyzed (Rng, True);
15137 end if;
15138 end Convert_Scalar_Bounds;
15140 -------------------
15141 -- Copy_And_Swap --
15142 -------------------
15144 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15145 begin
15146 -- Initialize new full declaration entity by copying the pertinent
15147 -- fields of the corresponding private declaration entity.
15149 -- We temporarily set Ekind to a value appropriate for a type to
15150 -- avoid assert failures in Einfo from checking for setting type
15151 -- attributes on something that is not a type. Ekind (Priv) is an
15152 -- appropriate choice, since it allowed the attributes to be set
15153 -- in the first place. This Ekind value will be modified later.
15155 Mutate_Ekind (Full, Ekind (Priv));
15157 -- Also set Etype temporarily to Any_Type, again, in the absence
15158 -- of errors, it will be properly reset, and if there are errors,
15159 -- then we want a value of Any_Type to remain.
15161 Set_Etype (Full, Any_Type);
15163 -- Now start copying attributes
15165 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15167 if Has_Discriminants (Full) then
15168 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15169 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15170 end if;
15172 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15173 Set_Homonym (Full, Homonym (Priv));
15174 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15175 Set_Is_Public (Full, Is_Public (Priv));
15176 Set_Is_Pure (Full, Is_Pure (Priv));
15177 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15178 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15179 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15180 Set_Has_Pragma_Unreferenced_Objects
15181 (Full, Has_Pragma_Unreferenced_Objects
15182 (Priv));
15184 Conditional_Delay (Full, Priv);
15186 if Is_Tagged_Type (Full) then
15187 Set_Direct_Primitive_Operations
15188 (Full, Direct_Primitive_Operations (Priv));
15189 Set_No_Tagged_Streams_Pragma
15190 (Full, No_Tagged_Streams_Pragma (Priv));
15192 if Is_Base_Type (Priv) then
15193 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15194 end if;
15195 end if;
15197 Set_Is_Volatile (Full, Is_Volatile (Priv));
15198 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15199 Set_Scope (Full, Scope (Priv));
15200 Set_Prev_Entity (Full, Prev_Entity (Priv));
15201 Set_Next_Entity (Full, Next_Entity (Priv));
15202 Set_First_Entity (Full, First_Entity (Priv));
15203 Set_Last_Entity (Full, Last_Entity (Priv));
15205 -- If access types have been recorded for later handling, keep them in
15206 -- the full view so that they get handled when the full view freeze
15207 -- node is expanded.
15209 if Present (Freeze_Node (Priv))
15210 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15211 then
15212 Ensure_Freeze_Node (Full);
15213 Set_Access_Types_To_Process
15214 (Freeze_Node (Full),
15215 Access_Types_To_Process (Freeze_Node (Priv)));
15216 end if;
15218 -- Swap the two entities. Now Private is the full type entity and Full
15219 -- is the private one. They will be swapped back at the end of the
15220 -- private part. This swapping ensures that the entity that is visible
15221 -- in the private part is the full declaration.
15223 Exchange_Entities (Priv, Full);
15224 Set_Is_Not_Self_Hidden (Priv);
15225 Append_Entity (Full, Scope (Full));
15226 end Copy_And_Swap;
15228 -------------------------------------
15229 -- Copy_Array_Base_Type_Attributes --
15230 -------------------------------------
15232 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15233 begin
15234 Set_Component_Alignment (T1, Component_Alignment (T2));
15235 Set_Component_Type (T1, Component_Type (T2));
15236 Set_Component_Size (T1, Component_Size (T2));
15237 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
15238 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15239 Propagate_Concurrent_Flags (T1, T2);
15240 Set_Is_Packed (T1, Is_Packed (T2));
15241 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15242 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15243 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15244 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15245 end Copy_Array_Base_Type_Attributes;
15247 -----------------------------------
15248 -- Copy_Array_Subtype_Attributes --
15249 -----------------------------------
15251 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15252 -- let it be recreated during freezing for the sake of better debug info.
15254 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15255 begin
15256 Set_Size_Info (T1, T2);
15258 Set_First_Index (T1, First_Index (T2));
15259 Set_Is_Aliased (T1, Is_Aliased (T2));
15260 Set_Is_Atomic (T1, Is_Atomic (T2));
15261 Set_Is_Independent (T1, Is_Independent (T2));
15262 Set_Is_Volatile (T1, Is_Volatile (T2));
15263 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15264 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15265 Set_Is_Constrained (T1, Is_Constrained (T2));
15266 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15267 Inherit_Rep_Item_Chain (T1, T2);
15268 Set_Convention (T1, Convention (T2));
15269 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15270 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15271 end Copy_Array_Subtype_Attributes;
15273 -----------------------------------
15274 -- Create_Constrained_Components --
15275 -----------------------------------
15277 procedure Create_Constrained_Components
15278 (Subt : Entity_Id;
15279 Decl_Node : Node_Id;
15280 Typ : Entity_Id;
15281 Constraints : Elist_Id)
15283 Loc : constant Source_Ptr := Sloc (Subt);
15284 Comp_List : constant Elist_Id := New_Elmt_List;
15285 Parent_Type : constant Entity_Id := Etype (Typ);
15287 Assoc_List : List_Id;
15288 Discr_Val : Elmt_Id;
15289 Errors : Boolean;
15290 New_C : Entity_Id;
15291 Old_C : Entity_Id;
15292 Is_Static : Boolean := True;
15293 Is_Compile_Time_Known : Boolean := True;
15295 procedure Collect_Fixed_Components (Typ : Entity_Id);
15296 -- Collect parent type components that do not appear in a variant part
15298 procedure Create_All_Components;
15299 -- Iterate over Comp_List to create the components of the subtype
15301 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15302 -- Creates a new component from Old_Compon, copying all the fields from
15303 -- it, including its Etype, inserts the new component in the Subt entity
15304 -- chain and returns the new component.
15306 function Is_Variant_Record (T : Entity_Id) return Boolean;
15307 -- If true, and discriminants are static, collect only components from
15308 -- variants selected by discriminant values.
15310 ------------------------------
15311 -- Collect_Fixed_Components --
15312 ------------------------------
15314 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15315 begin
15316 -- Build association list for discriminants, and find components of
15317 -- the variant part selected by the values of the discriminants.
15319 Assoc_List := New_List;
15321 Old_C := First_Discriminant (Typ);
15322 Discr_Val := First_Elmt (Constraints);
15323 while Present (Old_C) loop
15324 Append_To (Assoc_List,
15325 Make_Component_Association (Loc,
15326 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15327 Expression => New_Copy (Node (Discr_Val))));
15329 Next_Elmt (Discr_Val);
15330 Next_Discriminant (Old_C);
15331 end loop;
15333 -- The tag and the possible parent component are unconditionally in
15334 -- the subtype.
15336 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15337 Old_C := First_Component (Typ);
15338 while Present (Old_C) loop
15339 if Chars (Old_C) in Name_uTag | Name_uParent then
15340 Append_Elmt (Old_C, Comp_List);
15341 end if;
15343 Next_Component (Old_C);
15344 end loop;
15345 end if;
15346 end Collect_Fixed_Components;
15348 ---------------------------
15349 -- Create_All_Components --
15350 ---------------------------
15352 procedure Create_All_Components is
15353 Comp : Elmt_Id;
15355 begin
15356 Comp := First_Elmt (Comp_List);
15357 while Present (Comp) loop
15358 Old_C := Node (Comp);
15359 New_C := Create_Component (Old_C);
15361 Set_Etype
15362 (New_C,
15363 Constrain_Component_Type
15364 (Old_C, Subt, Decl_Node, Typ, Constraints));
15365 Set_Is_Public (New_C, Is_Public (Subt));
15367 Next_Elmt (Comp);
15368 end loop;
15369 end Create_All_Components;
15371 ----------------------
15372 -- Create_Component --
15373 ----------------------
15375 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15376 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15378 begin
15379 if Ekind (Old_Compon) = E_Discriminant
15380 and then Is_Completely_Hidden (Old_Compon)
15381 then
15382 -- This is a shadow discriminant created for a discriminant of
15383 -- the parent type, which needs to be present in the subtype.
15384 -- Give the shadow discriminant an internal name that cannot
15385 -- conflict with that of visible components.
15387 Set_Chars (New_Compon, New_Internal_Name ('C'));
15388 end if;
15390 -- Set the parent so we have a proper link for freezing etc. This is
15391 -- not a real parent pointer, since of course our parent does not own
15392 -- up to us and reference us, we are an illegitimate child of the
15393 -- original parent.
15395 Set_Parent (New_Compon, Parent (Old_Compon));
15397 -- We do not want this node marked as Comes_From_Source, since
15398 -- otherwise it would get first class status and a separate cross-
15399 -- reference line would be generated. Illegitimate children do not
15400 -- rate such recognition.
15402 Set_Comes_From_Source (New_Compon, False);
15404 -- But it is a real entity, and a birth certificate must be properly
15405 -- registered by entering it into the entity list, and setting its
15406 -- scope to the given subtype. This turns out to be useful for the
15407 -- LLVM code generator, but that scope is not used otherwise.
15409 Enter_Name (New_Compon);
15410 Set_Scope (New_Compon, Subt);
15412 return New_Compon;
15413 end Create_Component;
15415 -----------------------
15416 -- Is_Variant_Record --
15417 -----------------------
15419 function Is_Variant_Record (T : Entity_Id) return Boolean is
15420 Decl : constant Node_Id := Parent (T);
15421 begin
15422 return Nkind (Decl) = N_Full_Type_Declaration
15423 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15424 and then Present (Component_List (Type_Definition (Decl)))
15425 and then
15426 Present (Variant_Part (Component_List (Type_Definition (Decl))));
15427 end Is_Variant_Record;
15429 -- Start of processing for Create_Constrained_Components
15431 begin
15432 pragma Assert (Subt /= Base_Type (Subt));
15433 pragma Assert (Typ = Base_Type (Typ));
15435 Set_First_Entity (Subt, Empty);
15436 Set_Last_Entity (Subt, Empty);
15438 -- Check whether constraint is fully static, in which case we can
15439 -- optimize the list of components.
15441 Discr_Val := First_Elmt (Constraints);
15442 while Present (Discr_Val) loop
15443 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15444 Is_Static := False;
15446 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15447 Is_Compile_Time_Known := False;
15448 exit;
15449 end if;
15450 end if;
15452 Next_Elmt (Discr_Val);
15453 end loop;
15455 Set_Has_Static_Discriminants (Subt, Is_Static);
15457 Push_Scope (Subt);
15459 -- Inherit the discriminants of the parent type
15461 Add_Discriminants : declare
15462 Num_Disc : Nat;
15463 Num_Stor : Nat;
15465 begin
15466 Num_Disc := 0;
15467 Old_C := First_Discriminant (Typ);
15469 while Present (Old_C) loop
15470 Num_Disc := Num_Disc + 1;
15471 New_C := Create_Component (Old_C);
15472 Set_Is_Public (New_C, Is_Public (Subt));
15473 Next_Discriminant (Old_C);
15474 end loop;
15476 -- For an untagged derived subtype, the number of discriminants may
15477 -- be smaller than the number of inherited discriminants, because
15478 -- several of them may be renamed by a single new discriminant or
15479 -- constrained. In this case, add the hidden discriminants back into
15480 -- the subtype, because they need to be present if the optimizer of
15481 -- the GCC 4.x back-end decides to break apart assignments between
15482 -- objects using the parent view into member-wise assignments.
15484 Num_Stor := 0;
15486 if Is_Derived_Type (Typ)
15487 and then not Is_Tagged_Type (Typ)
15488 then
15489 Old_C := First_Stored_Discriminant (Typ);
15491 while Present (Old_C) loop
15492 Num_Stor := Num_Stor + 1;
15493 Next_Stored_Discriminant (Old_C);
15494 end loop;
15495 end if;
15497 if Num_Stor > Num_Disc then
15499 -- Find out multiple uses of new discriminants, and add hidden
15500 -- components for the extra renamed discriminants. We recognize
15501 -- multiple uses through the Corresponding_Discriminant of a
15502 -- new discriminant: if it constrains several old discriminants,
15503 -- this field points to the last one in the parent type. The
15504 -- stored discriminants of the derived type have the same name
15505 -- as those of the parent.
15507 declare
15508 Constr : Elmt_Id;
15509 New_Discr : Entity_Id;
15510 Old_Discr : Entity_Id;
15512 begin
15513 Constr := First_Elmt (Stored_Constraint (Typ));
15514 Old_Discr := First_Stored_Discriminant (Typ);
15515 while Present (Constr) loop
15516 if Is_Entity_Name (Node (Constr))
15517 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15518 then
15519 New_Discr := Entity (Node (Constr));
15521 if Chars (Corresponding_Discriminant (New_Discr)) /=
15522 Chars (Old_Discr)
15523 then
15524 -- The new discriminant has been used to rename a
15525 -- subsequent old discriminant. Introduce a shadow
15526 -- component for the current old discriminant.
15528 New_C := Create_Component (Old_Discr);
15529 Set_Original_Record_Component (New_C, Old_Discr);
15530 end if;
15532 else
15533 -- The constraint has eliminated the old discriminant.
15534 -- Introduce a shadow component.
15536 New_C := Create_Component (Old_Discr);
15537 Set_Original_Record_Component (New_C, Old_Discr);
15538 end if;
15540 Next_Elmt (Constr);
15541 Next_Stored_Discriminant (Old_Discr);
15542 end loop;
15543 end;
15544 end if;
15545 end Add_Discriminants;
15547 if Is_Compile_Time_Known
15548 and then Is_Variant_Record (Typ)
15549 then
15550 Collect_Fixed_Components (Typ);
15551 Gather_Components
15552 (Typ,
15553 Component_List (Type_Definition (Parent (Typ))),
15554 Governed_By => Assoc_List,
15555 Into => Comp_List,
15556 Report_Errors => Errors,
15557 Allow_Compile_Time => True);
15558 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15560 Create_All_Components;
15562 -- If the subtype declaration is created for a tagged type derivation
15563 -- with constraints, we retrieve the record definition of the parent
15564 -- type to select the components of the proper variant.
15566 elsif Is_Compile_Time_Known
15567 and then Is_Tagged_Type (Typ)
15568 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15569 and then
15570 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15571 and then Is_Variant_Record (Parent_Type)
15572 then
15573 Collect_Fixed_Components (Typ);
15574 Gather_Components
15575 (Typ,
15576 Component_List (Type_Definition (Parent (Parent_Type))),
15577 Governed_By => Assoc_List,
15578 Into => Comp_List,
15579 Report_Errors => Errors,
15580 Allow_Compile_Time => True);
15582 -- Note: previously there was a check at this point that no errors
15583 -- were detected. As a consequence of AI05-220 there may be an error
15584 -- if an inherited discriminant that controls a variant has a non-
15585 -- static constraint.
15587 -- If the tagged derivation has a type extension, collect all the
15588 -- new relevant components therein via Gather_Components.
15590 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15591 then
15592 Gather_Components
15593 (Typ,
15594 Component_List
15595 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15596 Governed_By => Assoc_List,
15597 Into => Comp_List,
15598 Report_Errors => Errors,
15599 Allow_Compile_Time => True,
15600 Include_Interface_Tag => True);
15601 end if;
15603 Create_All_Components;
15605 else
15606 -- If discriminants are not static, or if this is a multi-level type
15607 -- extension, we have to include all components of the parent type.
15609 Old_C := First_Component (Typ);
15610 while Present (Old_C) loop
15611 New_C := Create_Component (Old_C);
15613 Set_Etype
15614 (New_C,
15615 Constrain_Component_Type
15616 (Old_C, Subt, Decl_Node, Typ, Constraints));
15617 Set_Is_Public (New_C, Is_Public (Subt));
15619 Next_Component (Old_C);
15620 end loop;
15621 end if;
15623 End_Scope;
15624 end Create_Constrained_Components;
15626 ------------------------------------------
15627 -- Decimal_Fixed_Point_Type_Declaration --
15628 ------------------------------------------
15630 procedure Decimal_Fixed_Point_Type_Declaration
15631 (T : Entity_Id;
15632 Def : Node_Id)
15634 Loc : constant Source_Ptr := Sloc (Def);
15635 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15636 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15637 Max_Digits : constant Nat :=
15638 (if System_Max_Integer_Size = 128 then 38 else 18);
15639 -- Maximum number of digits that can be represented in an integer
15641 Implicit_Base : Entity_Id;
15642 Digs_Val : Uint;
15643 Delta_Val : Ureal;
15644 Scale_Val : Uint;
15645 Bound_Val : Ureal;
15647 begin
15648 Check_Restriction (No_Fixed_Point, Def);
15650 -- Create implicit base type
15652 Implicit_Base :=
15653 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15654 Set_Etype (Implicit_Base, Implicit_Base);
15656 -- Analyze and process delta expression
15658 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15660 Check_Delta_Expression (Delta_Expr);
15661 Delta_Val := Expr_Value_R (Delta_Expr);
15663 -- Check delta is power of 10, and determine scale value from it
15665 declare
15666 Val : Ureal;
15668 begin
15669 Scale_Val := Uint_0;
15670 Val := Delta_Val;
15672 if Val < Ureal_1 then
15673 while Val < Ureal_1 loop
15674 Val := Val * Ureal_10;
15675 Scale_Val := Scale_Val + 1;
15676 end loop;
15678 if Scale_Val > Max_Digits then
15679 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15680 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15681 Scale_Val := UI_From_Int (Max_Digits);
15682 end if;
15684 else
15685 while Val > Ureal_1 loop
15686 Val := Val / Ureal_10;
15687 Scale_Val := Scale_Val - 1;
15688 end loop;
15690 if Scale_Val < -Max_Digits then
15691 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15692 Error_Msg_N ("scale is less than minimum value of ^", Def);
15693 Scale_Val := UI_From_Int (-Max_Digits);
15694 end if;
15695 end if;
15697 if Val /= Ureal_1 then
15698 Error_Msg_N ("delta expression must be a power of 10", Def);
15699 Delta_Val := Ureal_10 ** (-Scale_Val);
15700 end if;
15701 end;
15703 -- Set delta, scale and small (small = delta for decimal type)
15705 Set_Delta_Value (Implicit_Base, Delta_Val);
15706 Set_Scale_Value (Implicit_Base, Scale_Val);
15707 Set_Small_Value (Implicit_Base, Delta_Val);
15709 -- Analyze and process digits expression
15711 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15712 Check_Digits_Expression (Digs_Expr);
15713 Digs_Val := Expr_Value (Digs_Expr);
15715 if Digs_Val > Max_Digits then
15716 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15717 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15718 Digs_Val := UI_From_Int (Max_Digits);
15719 end if;
15721 Set_Digits_Value (Implicit_Base, Digs_Val);
15722 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15724 -- Set range of base type from digits value for now. This will be
15725 -- expanded to represent the true underlying base range by Freeze.
15727 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15729 -- Note: We leave Esize unset for now, size will be set at freeze
15730 -- time. We have to do this for ordinary fixed-point, because the size
15731 -- depends on the specified small, and we might as well do the same for
15732 -- decimal fixed-point.
15734 pragma Assert (not Known_Esize (Implicit_Base));
15736 -- If there are bounds given in the declaration use them as the
15737 -- bounds of the first named subtype.
15739 if Present (Real_Range_Specification (Def)) then
15740 declare
15741 RRS : constant Node_Id := Real_Range_Specification (Def);
15742 Low : constant Node_Id := Low_Bound (RRS);
15743 High : constant Node_Id := High_Bound (RRS);
15744 Low_Val : Ureal;
15745 High_Val : Ureal;
15747 begin
15748 Analyze_And_Resolve (Low, Any_Real);
15749 Analyze_And_Resolve (High, Any_Real);
15750 Check_Real_Bound (Low);
15751 Check_Real_Bound (High);
15752 Low_Val := Expr_Value_R (Low);
15753 High_Val := Expr_Value_R (High);
15755 if Low_Val < (-Bound_Val) then
15756 Error_Msg_N
15757 ("range low bound too small for digits value", Low);
15758 Low_Val := -Bound_Val;
15759 end if;
15761 if High_Val > Bound_Val then
15762 Error_Msg_N
15763 ("range high bound too large for digits value", High);
15764 High_Val := Bound_Val;
15765 end if;
15767 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15768 end;
15770 -- If no explicit range, use range that corresponds to given
15771 -- digits value. This will end up as the final range for the
15772 -- first subtype.
15774 else
15775 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15776 end if;
15778 -- Complete entity for first subtype. The inheritance of the rep item
15779 -- chain ensures that SPARK-related pragmas are not clobbered when the
15780 -- decimal fixed point type acts as a full view of a private type.
15782 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15783 Set_Etype (T, Implicit_Base);
15784 Set_Size_Info (T, Implicit_Base);
15785 Inherit_Rep_Item_Chain (T, Implicit_Base);
15786 Set_Digits_Value (T, Digs_Val);
15787 Set_Delta_Value (T, Delta_Val);
15788 Set_Small_Value (T, Delta_Val);
15789 Set_Scale_Value (T, Scale_Val);
15790 Set_Is_Constrained (T);
15791 end Decimal_Fixed_Point_Type_Declaration;
15793 -----------------------------------
15794 -- Derive_Progenitor_Subprograms --
15795 -----------------------------------
15797 procedure Derive_Progenitor_Subprograms
15798 (Parent_Type : Entity_Id;
15799 Tagged_Type : Entity_Id)
15801 E : Entity_Id;
15802 Elmt : Elmt_Id;
15803 Iface : Entity_Id;
15804 Iface_Alias : Entity_Id;
15805 Iface_Elmt : Elmt_Id;
15806 Iface_Subp : Entity_Id;
15807 New_Subp : Entity_Id := Empty;
15808 Prim_Elmt : Elmt_Id;
15809 Subp : Entity_Id;
15810 Typ : Entity_Id;
15812 begin
15813 pragma Assert (Ada_Version >= Ada_2005
15814 and then Is_Record_Type (Tagged_Type)
15815 and then Is_Tagged_Type (Tagged_Type)
15816 and then Has_Interfaces (Tagged_Type));
15818 -- Step 1: Transfer to the full-view primitives associated with the
15819 -- partial-view that cover interface primitives. Conceptually this
15820 -- work should be done later by Process_Full_View; done here to
15821 -- simplify its implementation at later stages. It can be safely
15822 -- done here because interfaces must be visible in the partial and
15823 -- private view (RM 7.3(7.3/2)).
15825 -- Small optimization: This work is only required if the parent may
15826 -- have entities whose Alias attribute reference an interface primitive.
15827 -- Such a situation may occur if the parent is an abstract type and the
15828 -- primitive has not been yet overridden or if the parent is a generic
15829 -- formal type covering interfaces.
15831 -- If the tagged type is not abstract, it cannot have abstract
15832 -- primitives (the only entities in the list of primitives of
15833 -- non-abstract tagged types that can reference abstract primitives
15834 -- through its Alias attribute are the internal entities that have
15835 -- attribute Interface_Alias, and these entities are generated later
15836 -- by Add_Internal_Interface_Entities).
15838 if In_Private_Part (Current_Scope)
15839 and then (Is_Abstract_Type (Parent_Type)
15840 or else
15841 Is_Generic_Type (Parent_Type))
15842 then
15843 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15844 while Present (Elmt) loop
15845 Subp := Node (Elmt);
15847 -- At this stage it is not possible to have entities in the list
15848 -- of primitives that have attribute Interface_Alias.
15850 pragma Assert (No (Interface_Alias (Subp)));
15852 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15854 if Is_Interface (Typ) then
15855 E := Find_Primitive_Covering_Interface
15856 (Tagged_Type => Tagged_Type,
15857 Iface_Prim => Subp);
15859 if Present (E)
15860 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15861 then
15862 Replace_Elmt (Elmt, E);
15863 Remove_Homonym (Subp);
15864 end if;
15865 end if;
15867 Next_Elmt (Elmt);
15868 end loop;
15869 end if;
15871 -- Step 2: Add primitives of progenitors that are not implemented by
15872 -- parents of Tagged_Type.
15874 if Present (Interfaces (Base_Type (Tagged_Type))) then
15875 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15876 while Present (Iface_Elmt) loop
15877 Iface := Node (Iface_Elmt);
15879 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15880 while Present (Prim_Elmt) loop
15881 Iface_Subp := Node (Prim_Elmt);
15882 Iface_Alias := Ultimate_Alias (Iface_Subp);
15884 -- Exclude derivation of predefined primitives except those
15885 -- that come from source, or are inherited from one that comes
15886 -- from source. Required to catch declarations of equality
15887 -- operators of interfaces. For example:
15889 -- type Iface is interface;
15890 -- function "=" (Left, Right : Iface) return Boolean;
15892 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15893 or else Comes_From_Source (Iface_Alias)
15894 then
15895 E :=
15896 Find_Primitive_Covering_Interface
15897 (Tagged_Type => Tagged_Type,
15898 Iface_Prim => Iface_Subp);
15900 -- If not found we derive a new primitive leaving its alias
15901 -- attribute referencing the interface primitive.
15903 if No (E) then
15904 Derive_Subprogram
15905 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15907 -- Ada 2012 (AI05-0197): If the covering primitive's name
15908 -- differs from the name of the interface primitive then it
15909 -- is a private primitive inherited from a parent type. In
15910 -- such case, given that Tagged_Type covers the interface,
15911 -- the inherited private primitive becomes visible. For such
15912 -- purpose we add a new entity that renames the inherited
15913 -- private primitive.
15915 elsif Chars (E) /= Chars (Iface_Subp) then
15916 pragma Assert (Has_Suffix (E, 'P'));
15917 Derive_Subprogram
15918 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15919 Set_Alias (New_Subp, E);
15920 Set_Is_Abstract_Subprogram (New_Subp,
15921 Is_Abstract_Subprogram (E));
15923 -- Propagate to the full view interface entities associated
15924 -- with the partial view.
15926 elsif In_Private_Part (Current_Scope)
15927 and then Present (Alias (E))
15928 and then Alias (E) = Iface_Subp
15929 and then
15930 List_Containing (Parent (E)) /=
15931 Private_Declarations
15932 (Specification
15933 (Unit_Declaration_Node (Current_Scope)))
15934 then
15935 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15936 end if;
15937 end if;
15939 Next_Elmt (Prim_Elmt);
15940 end loop;
15942 Next_Elmt (Iface_Elmt);
15943 end loop;
15944 end if;
15945 end Derive_Progenitor_Subprograms;
15947 -----------------------
15948 -- Derive_Subprogram --
15949 -----------------------
15951 procedure Derive_Subprogram
15952 (New_Subp : out Entity_Id;
15953 Parent_Subp : Entity_Id;
15954 Derived_Type : Entity_Id;
15955 Parent_Type : Entity_Id;
15956 Actual_Subp : Entity_Id := Empty)
15958 Formal : Entity_Id;
15959 -- Formal parameter of parent primitive operation
15961 Formal_Of_Actual : Entity_Id;
15962 -- Formal parameter of actual operation, when the derivation is to
15963 -- create a renaming for a primitive operation of an actual in an
15964 -- instantiation.
15966 New_Formal : Entity_Id;
15967 -- Formal of inherited operation
15969 Visible_Subp : Entity_Id := Parent_Subp;
15971 function Is_Private_Overriding return Boolean;
15972 -- If Subp is a private overriding of a visible operation, the inherited
15973 -- operation derives from the overridden op (even though its body is the
15974 -- overriding one) and the inherited operation is visible now. See
15975 -- sem_disp to see the full details of the handling of the overridden
15976 -- subprogram, which is removed from the list of primitive operations of
15977 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15978 -- and used to diagnose abstract operations that need overriding in the
15979 -- derived type.
15981 procedure Replace_Type (Id, New_Id : Entity_Id);
15982 -- Set the Etype of New_Id to the appropriate subtype determined from
15983 -- the Etype of Id, following (RM 3.4 (18, 19, 20, 21)). Id is either
15984 -- the parent type's primitive subprogram or one of its formals, and
15985 -- New_Id is the corresponding entity for the derived type. When the
15986 -- Etype of Id is an anonymous access type, create a new access type
15987 -- designating the derived type.
15989 procedure Set_Derived_Name;
15990 -- This procedure sets the appropriate Chars name for New_Subp. This
15991 -- is normally just a copy of the parent name. An exception arises for
15992 -- type support subprograms, where the name is changed to reflect the
15993 -- name of the derived type, e.g. if type foo is derived from type bar,
15994 -- then a procedure barDA is derived with a name fooDA.
15996 ---------------------------
15997 -- Is_Private_Overriding --
15998 ---------------------------
16000 function Is_Private_Overriding return Boolean is
16001 Prev : Entity_Id;
16003 begin
16004 -- If the parent is not a dispatching operation there is no
16005 -- need to investigate overridings
16007 if not Is_Dispatching_Operation (Parent_Subp) then
16008 return False;
16009 end if;
16011 -- The visible operation that is overridden is a homonym of the
16012 -- parent subprogram. We scan the homonym chain to find the one
16013 -- whose alias is the subprogram we are deriving.
16015 Prev := Current_Entity (Parent_Subp);
16016 while Present (Prev) loop
16017 if Ekind (Prev) = Ekind (Parent_Subp)
16018 and then Alias (Prev) = Parent_Subp
16019 and then Scope (Parent_Subp) = Scope (Prev)
16020 and then not Is_Hidden (Prev)
16021 then
16022 Visible_Subp := Prev;
16023 return True;
16024 end if;
16026 Prev := Homonym (Prev);
16027 end loop;
16029 return False;
16030 end Is_Private_Overriding;
16032 ------------------
16033 -- Replace_Type --
16034 ------------------
16036 procedure Replace_Type (Id, New_Id : Entity_Id) is
16037 Id_Type : constant Entity_Id := Etype (Id);
16038 Par : constant Node_Id := Parent (Derived_Type);
16040 begin
16041 -- When the type is an anonymous access type, create a new access
16042 -- type designating the derived type. This itype must be elaborated
16043 -- at the point of the derivation, not on subsequent calls that may
16044 -- be out of the proper scope for Gigi, so we insert a reference to
16045 -- it after the derivation.
16047 if Ekind (Id_Type) = E_Anonymous_Access_Type then
16048 declare
16049 Acc_Type : Entity_Id;
16050 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
16052 begin
16053 if Ekind (Desig_Typ) = E_Record_Type_With_Private
16054 and then Present (Full_View (Desig_Typ))
16055 and then not Is_Private_Type (Parent_Type)
16056 then
16057 Desig_Typ := Full_View (Desig_Typ);
16058 end if;
16060 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
16062 -- Ada 2005 (AI-251): Handle also derivations of abstract
16063 -- interface primitives.
16065 or else (Is_Interface (Desig_Typ)
16066 and then not Is_Class_Wide_Type (Desig_Typ))
16067 then
16068 Acc_Type := New_Copy (Id_Type);
16069 Set_Etype (Acc_Type, Acc_Type);
16070 Set_Scope (Acc_Type, New_Subp);
16072 -- Set size of anonymous access type. If we have an access
16073 -- to an unconstrained array, this is a fat pointer, so it
16074 -- is sizes at twice addtress size.
16076 if Is_Array_Type (Desig_Typ)
16077 and then not Is_Constrained (Desig_Typ)
16078 then
16079 Init_Size (Acc_Type, 2 * System_Address_Size);
16081 -- Other cases use a thin pointer
16083 else
16084 Init_Size (Acc_Type, System_Address_Size);
16085 end if;
16087 -- Set remaining characterstics of anonymous access type
16089 Reinit_Alignment (Acc_Type);
16090 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
16092 Set_Etype (New_Id, Acc_Type);
16093 Set_Scope (New_Id, New_Subp);
16095 -- Create a reference to it
16097 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
16099 else
16100 Set_Etype (New_Id, Id_Type);
16101 end if;
16102 end;
16104 -- In Ada2012, a formal may have an incomplete type but the type
16105 -- derivation that inherits the primitive follows the full view.
16107 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
16108 or else
16109 (Ekind (Id_Type) = E_Record_Type_With_Private
16110 and then Present (Full_View (Id_Type))
16111 and then
16112 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
16113 or else
16114 (Ada_Version >= Ada_2012
16115 and then Ekind (Id_Type) = E_Incomplete_Type
16116 and then Full_View (Id_Type) = Parent_Type)
16117 then
16118 -- Constraint checks on formals are generated during expansion,
16119 -- based on the signature of the original subprogram. The bounds
16120 -- of the derived type are not relevant, and thus we can use
16121 -- the base type for the formals. However, the return type may be
16122 -- used in a context that requires that the proper static bounds
16123 -- be used (a case statement, for example) and for those cases
16124 -- we must use the derived type (first subtype), not its base.
16126 -- If the derived_type_definition has no constraints, we know that
16127 -- the derived type has the same constraints as the first subtype
16128 -- of the parent, and we can also use it rather than its base,
16129 -- which can lead to more efficient code.
16131 if Id_Type = Parent_Type then
16132 if Is_Scalar_Type (Parent_Type)
16133 and then
16134 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16135 then
16136 Set_Etype (New_Id, Derived_Type);
16138 elsif Nkind (Par) = N_Full_Type_Declaration
16139 and then
16140 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16141 and then
16142 Is_Entity_Name
16143 (Subtype_Indication (Type_Definition (Par)))
16144 then
16145 Set_Etype (New_Id, Derived_Type);
16147 else
16148 Set_Etype (New_Id, Base_Type (Derived_Type));
16149 end if;
16151 else
16152 Set_Etype (New_Id, Base_Type (Derived_Type));
16153 end if;
16155 else
16156 Set_Etype (New_Id, Id_Type);
16157 end if;
16158 end Replace_Type;
16160 ----------------------
16161 -- Set_Derived_Name --
16162 ----------------------
16164 procedure Set_Derived_Name is
16165 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16166 begin
16167 if Nm = TSS_Null then
16168 Set_Chars (New_Subp, Chars (Parent_Subp));
16169 else
16170 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16171 end if;
16172 end Set_Derived_Name;
16174 -- Start of processing for Derive_Subprogram
16176 begin
16177 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16178 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16179 Set_Is_Not_Self_Hidden (New_Subp);
16181 -- Check whether the inherited subprogram is a private operation that
16182 -- should be inherited but not yet made visible. Such subprograms can
16183 -- become visible at a later point (e.g., the private part of a public
16184 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16185 -- following predicate is true, then this is not such a private
16186 -- operation and the subprogram simply inherits the name of the parent
16187 -- subprogram. Note the special check for the names of controlled
16188 -- operations, which are currently exempted from being inherited with
16189 -- a hidden name because they must be findable for generation of
16190 -- implicit run-time calls.
16192 if not Is_Hidden (Parent_Subp)
16193 or else Is_Internal (Parent_Subp)
16194 or else Is_Private_Overriding
16195 or else Is_Internal_Name (Chars (Parent_Subp))
16196 or else (Is_Controlled (Parent_Type)
16197 and then Chars (Parent_Subp) in Name_Adjust
16198 | Name_Finalize
16199 | Name_Initialize)
16200 then
16201 Set_Derived_Name;
16203 -- An inherited dispatching equality will be overridden by an internally
16204 -- generated one, or by an explicit one, so preserve its name and thus
16205 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16206 -- private operation it may become invisible if the full view has
16207 -- progenitors, and the dispatch table will be malformed.
16208 -- We check that the type is limited to handle the anomalous declaration
16209 -- of Limited_Controlled, which is derived from a non-limited type, and
16210 -- which is handled specially elsewhere as well.
16212 elsif Chars (Parent_Subp) = Name_Op_Eq
16213 and then Is_Dispatching_Operation (Parent_Subp)
16214 and then Etype (Parent_Subp) = Standard_Boolean
16215 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16216 and then
16217 Etype (First_Formal (Parent_Subp)) =
16218 Etype (Next_Formal (First_Formal (Parent_Subp)))
16219 then
16220 Set_Derived_Name;
16222 -- If parent is hidden, this can be a regular derivation if the
16223 -- parent is immediately visible in a non-instantiating context,
16224 -- or if we are in the private part of an instance. This test
16225 -- should still be refined ???
16227 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16228 -- operation as a non-visible operation in cases where the parent
16229 -- subprogram might not be visible now, but was visible within the
16230 -- original generic, so it would be wrong to make the inherited
16231 -- subprogram non-visible now. (Not clear if this test is fully
16232 -- correct; are there any cases where we should declare the inherited
16233 -- operation as not visible to avoid it being overridden, e.g., when
16234 -- the parent type is a generic actual with private primitives ???)
16236 -- (they should be treated the same as other private inherited
16237 -- subprograms, but it's not clear how to do this cleanly). ???
16239 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16240 and then Is_Immediately_Visible (Parent_Subp)
16241 and then not In_Instance)
16242 or else In_Instance_Not_Visible
16243 then
16244 Set_Derived_Name;
16246 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16247 -- overrides an interface primitive because interface primitives
16248 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16250 elsif Ada_Version >= Ada_2005
16251 and then Is_Dispatching_Operation (Parent_Subp)
16252 and then Present (Covered_Interface_Op (Parent_Subp))
16253 then
16254 Set_Derived_Name;
16256 -- Otherwise, the type is inheriting a private operation, so enter it
16257 -- with a special name so it can't be overridden. See also below, where
16258 -- we check for this case, and if so avoid setting Requires_Overriding.
16260 else
16261 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16262 end if;
16264 Set_Parent (New_Subp, Parent (Derived_Type));
16266 if Present (Actual_Subp) then
16267 Replace_Type (Actual_Subp, New_Subp);
16268 else
16269 Replace_Type (Parent_Subp, New_Subp);
16270 end if;
16272 Conditional_Delay (New_Subp, Parent_Subp);
16274 -- If we are creating a renaming for a primitive operation of an
16275 -- actual of a generic derived type, we must examine the signature
16276 -- of the actual primitive, not that of the generic formal, which for
16277 -- example may be an interface. However the name and initial value
16278 -- of the inherited operation are those of the formal primitive.
16280 Formal := First_Formal (Parent_Subp);
16282 if Present (Actual_Subp) then
16283 Formal_Of_Actual := First_Formal (Actual_Subp);
16284 else
16285 Formal_Of_Actual := Empty;
16286 end if;
16288 while Present (Formal) loop
16289 New_Formal := New_Copy (Formal);
16291 -- Extra formals are not inherited from a limited interface parent
16292 -- since limitedness is not inherited in such case (AI-419) and this
16293 -- affects the extra formals.
16295 if Is_Limited_Interface (Parent_Type) then
16296 Set_Extra_Formal (New_Formal, Empty);
16297 Set_Extra_Accessibility (New_Formal, Empty);
16298 end if;
16300 -- Normally we do not go copying parents, but in the case of
16301 -- formals, we need to link up to the declaration (which is the
16302 -- parameter specification), and it is fine to link up to the
16303 -- original formal's parameter specification in this case.
16305 Set_Parent (New_Formal, Parent (Formal));
16306 Append_Entity (New_Formal, New_Subp);
16308 if Present (Formal_Of_Actual) then
16309 Replace_Type (Formal_Of_Actual, New_Formal);
16310 Next_Formal (Formal_Of_Actual);
16311 else
16312 Replace_Type (Formal, New_Formal);
16313 end if;
16315 Next_Formal (Formal);
16316 end loop;
16318 -- Extra formals are shared between the parent subprogram and this
16319 -- internal entity built by Derive_Subprogram (implicit in the above
16320 -- copy of formals), unless the parent type is a limited interface type;
16321 -- hence we must inherit also the reference to the first extra formal.
16322 -- When the parent type is an interface, the extra formals will be added
16323 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16325 if not Is_Limited_Interface (Parent_Type) then
16326 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16328 if Ekind (New_Subp) = E_Function then
16329 Set_Extra_Accessibility_Of_Result (New_Subp,
16330 Extra_Accessibility_Of_Result (Parent_Subp));
16331 end if;
16332 end if;
16334 -- If this derivation corresponds to a tagged generic actual, then
16335 -- primitive operations rename those of the actual. Otherwise the
16336 -- primitive operations rename those of the parent type, If the parent
16337 -- renames an intrinsic operator, so does the new subprogram. We except
16338 -- concatenation, which is always properly typed, and does not get
16339 -- expanded as other intrinsic operations.
16341 if No (Actual_Subp) then
16342 if Is_Intrinsic_Subprogram (Parent_Subp) then
16343 Set_Convention (New_Subp, Convention_Intrinsic);
16344 Set_Is_Intrinsic_Subprogram (New_Subp);
16346 if Present (Alias (Parent_Subp))
16347 and then Chars (Parent_Subp) /= Name_Op_Concat
16348 then
16349 Set_Alias (New_Subp, Alias (Parent_Subp));
16350 else
16351 Set_Alias (New_Subp, Parent_Subp);
16352 end if;
16354 else
16355 Set_Alias (New_Subp, Parent_Subp);
16356 end if;
16358 else
16359 Set_Alias (New_Subp, Actual_Subp);
16360 end if;
16362 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16364 -- Derived subprograms of a tagged type must inherit the convention
16365 -- of the parent subprogram (a requirement of AI95-117). Derived
16366 -- subprograms of untagged types simply get convention Ada by default.
16368 -- If the derived type is a tagged generic formal type with unknown
16369 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16371 -- However, if the type is derived from a generic formal, the further
16372 -- inherited subprogram has the convention of the non-generic ancestor.
16373 -- Otherwise there would be no way to override the operation.
16374 -- (This is subject to forthcoming ARG discussions).
16376 if Is_Tagged_Type (Derived_Type) then
16377 if Is_Generic_Type (Derived_Type)
16378 and then Has_Unknown_Discriminants (Derived_Type)
16379 then
16380 Set_Convention (New_Subp, Convention_Intrinsic);
16382 else
16383 if Is_Generic_Type (Parent_Type)
16384 and then Has_Unknown_Discriminants (Parent_Type)
16385 then
16386 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16387 else
16388 Set_Convention (New_Subp, Convention (Parent_Subp));
16389 end if;
16390 end if;
16391 end if;
16393 -- Predefined controlled operations retain their name even if the parent
16394 -- is hidden (see above), but they are not primitive operations if the
16395 -- ancestor is not visible, for example if the parent is a private
16396 -- extension completed with a controlled extension. Note that a full
16397 -- type that is controlled can break privacy: the flag Is_Controlled is
16398 -- set on both views of the type.
16400 if Is_Controlled (Parent_Type)
16401 and then Chars (Parent_Subp) in Name_Initialize
16402 | Name_Adjust
16403 | Name_Finalize
16404 and then Is_Hidden (Parent_Subp)
16405 and then not Is_Visibly_Controlled (Parent_Type)
16406 then
16407 Set_Is_Hidden (New_Subp);
16408 end if;
16410 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16411 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16413 if Ekind (Parent_Subp) = E_Procedure then
16414 Set_Is_Valued_Procedure
16415 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16416 else
16417 Set_Has_Controlling_Result
16418 (New_Subp, Has_Controlling_Result (Parent_Subp));
16419 end if;
16421 -- No_Return must be inherited properly. If this is overridden in the
16422 -- case of a dispatching operation, then the check is made later in
16423 -- Check_Abstract_Overriding that the overriding operation is also
16424 -- No_Return (no such check is required for the nondispatching case).
16426 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16428 -- If the parent subprogram is marked as Ghost, then so is the derived
16429 -- subprogram. The ghost policy for the derived subprogram is set from
16430 -- the effective ghost policy at the point of derived type declaration.
16432 if Is_Ghost_Entity (Parent_Subp) then
16433 Set_Is_Ghost_Entity (New_Subp);
16434 end if;
16436 -- A derived function with a controlling result is abstract. If the
16437 -- Derived_Type is a nonabstract formal generic derived type, then
16438 -- inherited operations are not abstract: the required check is done at
16439 -- instantiation time. If the derivation is for a generic actual, the
16440 -- function is not abstract unless the actual is.
16442 if Is_Generic_Type (Derived_Type)
16443 and then not Is_Abstract_Type (Derived_Type)
16444 then
16445 null;
16447 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16448 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16449 -- that functions with controlling access results of record extensions
16450 -- with a null extension part require overriding (AI95-00391/06).
16452 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16453 -- implementing the rule of RM 7.3.2(6.1/4).
16455 -- A subprogram subject to pragma Extensions_Visible with value False
16456 -- requires overriding if the subprogram has at least one controlling
16457 -- OUT parameter (SPARK RM 6.1.7(6)).
16459 elsif Ada_Version >= Ada_2005
16460 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16461 or else (Is_Tagged_Type (Derived_Type)
16462 and then Etype (New_Subp) = Derived_Type
16463 and then not Is_Null_Extension (Derived_Type))
16464 or else (Is_Tagged_Type (Derived_Type)
16465 and then Ekind (Etype (New_Subp)) =
16466 E_Anonymous_Access_Type
16467 and then Designated_Type (Etype (New_Subp)) =
16468 Derived_Type)
16469 or else (Comes_From_Source (Alias (New_Subp))
16470 and then Is_EVF_Procedure (Alias (New_Subp)))
16472 -- AI12-0042: Set Requires_Overriding when a type extension
16473 -- inherits a private operation that is visible at the
16474 -- point of extension (Has_Private_Ancestor is False) from
16475 -- an ancestor that has Type_Invariant'Class, and when the
16476 -- type extension is in a visible part (the latter as
16477 -- clarified by AI12-0382).
16479 or else
16480 (not Has_Private_Ancestor (Derived_Type)
16481 and then Has_Invariants (Parent_Type)
16482 and then
16483 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16484 and then
16485 Class_Present
16486 (Get_Pragma (Parent_Type, Pragma_Invariant))
16487 and then Is_Private_Primitive (Parent_Subp)
16488 and then In_Visible_Part (Scope (Derived_Type))))
16490 and then No (Actual_Subp)
16491 then
16492 if not Is_Tagged_Type (Derived_Type)
16493 or else Is_Abstract_Type (Derived_Type)
16494 or else Is_Abstract_Subprogram (Alias (New_Subp))
16495 then
16496 Set_Is_Abstract_Subprogram (New_Subp);
16498 -- If the Chars of the new subprogram is different from that of the
16499 -- parent's one, it means that we entered it with a special name so
16500 -- it can't be overridden (see above). In that case we had better not
16501 -- *require* it to be overridden. This is the case where the parent
16502 -- type inherited the operation privately, so there's no danger of
16503 -- dangling dispatching.
16505 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16506 Set_Requires_Overriding (New_Subp);
16507 end if;
16509 elsif Ada_Version < Ada_2005
16510 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16511 or else (Is_Tagged_Type (Derived_Type)
16512 and then Etype (New_Subp) = Derived_Type
16513 and then No (Actual_Subp)))
16514 then
16515 Set_Is_Abstract_Subprogram (New_Subp);
16517 -- AI05-0097 : an inherited operation that dispatches on result is
16518 -- abstract if the derived type is abstract, even if the parent type
16519 -- is concrete and the derived type is a null extension.
16521 elsif Has_Controlling_Result (Alias (New_Subp))
16522 and then Is_Abstract_Type (Etype (New_Subp))
16523 then
16524 Set_Is_Abstract_Subprogram (New_Subp);
16526 -- Finally, if the parent type is abstract we must verify that all
16527 -- inherited operations are either non-abstract or overridden, or that
16528 -- the derived type itself is abstract (this check is performed at the
16529 -- end of a package declaration, in Check_Abstract_Overriding). A
16530 -- private overriding in the parent type will not be visible in the
16531 -- derivation if we are not in an inner package or in a child unit of
16532 -- the parent type, in which case the abstractness of the inherited
16533 -- operation is carried to the new subprogram.
16535 elsif Is_Abstract_Type (Parent_Type)
16536 and then not In_Open_Scopes (Scope (Parent_Type))
16537 and then Is_Private_Overriding
16538 and then Is_Abstract_Subprogram (Visible_Subp)
16539 then
16540 if No (Actual_Subp) then
16541 Set_Alias (New_Subp, Visible_Subp);
16542 Set_Is_Abstract_Subprogram (New_Subp, True);
16544 else
16545 -- If this is a derivation for an instance of a formal derived
16546 -- type, abstractness comes from the primitive operation of the
16547 -- actual, not from the operation inherited from the ancestor.
16549 Set_Is_Abstract_Subprogram
16550 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16551 end if;
16552 end if;
16554 New_Overloaded_Entity (New_Subp, Derived_Type);
16556 -- RM 6.1.1(15): If a subprogram inherits nonconforming class-wide
16557 -- preconditions and the derived type is abstract, the derived operation
16558 -- is abstract as well if parent subprogram is not abstract or null.
16560 if Is_Abstract_Type (Derived_Type)
16561 and then Has_Non_Trivial_Precondition (Parent_Subp)
16562 and then Present (Interfaces (Derived_Type))
16563 then
16565 -- Add useful attributes of subprogram before the freeze point,
16566 -- in case freezing is delayed or there are previous errors.
16568 Set_Is_Dispatching_Operation (New_Subp);
16570 declare
16571 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16573 begin
16574 if Present (Iface_Prim)
16575 and then Has_Non_Trivial_Precondition (Iface_Prim)
16576 then
16577 Set_Is_Abstract_Subprogram (New_Subp);
16578 end if;
16579 end;
16580 end if;
16582 -- Check for case of a derived subprogram for the instantiation of a
16583 -- formal derived tagged type, if so mark the subprogram as dispatching
16584 -- and inherit the dispatching attributes of the actual subprogram. The
16585 -- derived subprogram is effectively renaming of the actual subprogram,
16586 -- so it needs to have the same attributes as the actual.
16588 if Present (Actual_Subp)
16589 and then Is_Dispatching_Operation (Actual_Subp)
16590 then
16591 Set_Is_Dispatching_Operation (New_Subp);
16593 if Present (DTC_Entity (Actual_Subp)) then
16594 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16595 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16596 end if;
16597 end if;
16599 -- Indicate that a derived subprogram does not require a body and that
16600 -- it does not require processing of default expressions.
16602 Set_Has_Completion (New_Subp);
16603 Set_Default_Expressions_Processed (New_Subp);
16605 if Ekind (New_Subp) = E_Function then
16606 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16607 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16608 end if;
16610 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16611 -- primitive subprogram S of a type T, then the aspect is inherited
16612 -- by the corresponding primitive subprogram of each descendant of T.
16614 if Is_Tagged_Type (Derived_Type)
16615 and then Is_Dispatching_Operation (New_Subp)
16616 and then Has_Yield_Aspect (Alias (New_Subp))
16617 then
16618 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16619 end if;
16621 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16622 end Derive_Subprogram;
16624 ------------------------
16625 -- Derive_Subprograms --
16626 ------------------------
16628 procedure Derive_Subprograms
16629 (Parent_Type : Entity_Id;
16630 Derived_Type : Entity_Id;
16631 Generic_Actual : Entity_Id := Empty)
16633 Op_List : constant Elist_Id :=
16634 Collect_Primitive_Operations (Parent_Type);
16636 function Check_Derived_Type return Boolean;
16637 -- Check that all the entities derived from Parent_Type are found in
16638 -- the list of primitives of Derived_Type exactly in the same order.
16640 procedure Derive_Interface_Subprogram
16641 (New_Subp : out Entity_Id;
16642 Subp : Entity_Id;
16643 Actual_Subp : Entity_Id);
16644 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16645 -- (which is an interface primitive). If Generic_Actual is present then
16646 -- Actual_Subp is the actual subprogram corresponding with the generic
16647 -- subprogram Subp.
16649 ------------------------
16650 -- Check_Derived_Type --
16651 ------------------------
16653 function Check_Derived_Type return Boolean is
16654 E : Entity_Id;
16655 Derived_Elmt : Elmt_Id;
16656 Derived_Op : Entity_Id;
16657 Derived_Ops : Elist_Id;
16658 Parent_Elmt : Elmt_Id;
16659 Parent_Op : Entity_Id;
16661 begin
16662 -- Traverse list of entities in the current scope searching for
16663 -- an incomplete type whose full-view is derived type.
16665 E := First_Entity (Scope (Derived_Type));
16666 while Present (E) and then E /= Derived_Type loop
16667 if Ekind (E) = E_Incomplete_Type
16668 and then Present (Full_View (E))
16669 and then Full_View (E) = Derived_Type
16670 then
16671 -- Disable this test if Derived_Type completes an incomplete
16672 -- type because in such case more primitives can be added
16673 -- later to the list of primitives of Derived_Type by routine
16674 -- Process_Incomplete_Dependents.
16676 return True;
16677 end if;
16679 Next_Entity (E);
16680 end loop;
16682 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16684 Derived_Elmt := First_Elmt (Derived_Ops);
16685 Parent_Elmt := First_Elmt (Op_List);
16686 while Present (Parent_Elmt) loop
16687 Parent_Op := Node (Parent_Elmt);
16688 Derived_Op := Node (Derived_Elmt);
16690 -- At this early stage Derived_Type has no entities with attribute
16691 -- Interface_Alias. In addition, such primitives are always
16692 -- located at the end of the list of primitives of Parent_Type.
16693 -- Therefore, if found we can safely stop processing pending
16694 -- entities.
16696 exit when Present (Interface_Alias (Parent_Op));
16698 -- Handle hidden entities
16700 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16701 and then Is_Hidden (Parent_Op)
16702 then
16703 if Present (Derived_Op)
16704 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16705 then
16706 Next_Elmt (Derived_Elmt);
16707 end if;
16709 else
16710 if No (Derived_Op)
16711 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16712 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16713 then
16714 return False;
16715 end if;
16717 Next_Elmt (Derived_Elmt);
16718 end if;
16720 Next_Elmt (Parent_Elmt);
16721 end loop;
16723 return True;
16724 end Check_Derived_Type;
16726 ---------------------------------
16727 -- Derive_Interface_Subprogram --
16728 ---------------------------------
16730 procedure Derive_Interface_Subprogram
16731 (New_Subp : out Entity_Id;
16732 Subp : Entity_Id;
16733 Actual_Subp : Entity_Id)
16735 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16736 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16738 begin
16739 pragma Assert (Is_Interface (Iface_Type));
16741 Derive_Subprogram
16742 (New_Subp => New_Subp,
16743 Parent_Subp => Iface_Subp,
16744 Derived_Type => Derived_Type,
16745 Parent_Type => Iface_Type,
16746 Actual_Subp => Actual_Subp);
16748 -- Given that this new interface entity corresponds with a primitive
16749 -- of the parent that was not overridden we must leave it associated
16750 -- with its parent primitive to ensure that it will share the same
16751 -- dispatch table slot when overridden. We must set the Alias to Subp
16752 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16753 -- (in case we inherited Subp from Iface_Type via a nonabstract
16754 -- generic formal type).
16756 if No (Actual_Subp) then
16757 Set_Alias (New_Subp, Subp);
16759 declare
16760 T : Entity_Id := Find_Dispatching_Type (Subp);
16761 begin
16762 while Etype (T) /= T loop
16763 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16764 Set_Is_Abstract_Subprogram (New_Subp, False);
16765 exit;
16766 end if;
16768 T := Etype (T);
16769 end loop;
16770 end;
16772 -- For instantiations this is not needed since the previous call to
16773 -- Derive_Subprogram leaves the entity well decorated.
16775 else
16776 pragma Assert (Alias (New_Subp) = Actual_Subp);
16777 null;
16778 end if;
16779 end Derive_Interface_Subprogram;
16781 -- Local variables
16783 Alias_Subp : Entity_Id;
16784 Act_List : Elist_Id;
16785 Act_Elmt : Elmt_Id;
16786 Act_Subp : Entity_Id := Empty;
16787 Elmt : Elmt_Id;
16788 Need_Search : Boolean := False;
16789 New_Subp : Entity_Id;
16790 Parent_Base : Entity_Id;
16791 Subp : Entity_Id;
16793 -- Start of processing for Derive_Subprograms
16795 begin
16796 if Ekind (Parent_Type) = E_Record_Type_With_Private
16797 and then Has_Discriminants (Parent_Type)
16798 and then Present (Full_View (Parent_Type))
16799 then
16800 Parent_Base := Full_View (Parent_Type);
16801 else
16802 Parent_Base := Parent_Type;
16803 end if;
16805 if Present (Generic_Actual) then
16806 Act_List := Collect_Primitive_Operations (Generic_Actual);
16807 Act_Elmt := First_Elmt (Act_List);
16808 else
16809 Act_List := No_Elist;
16810 Act_Elmt := No_Elmt;
16811 end if;
16813 -- Derive primitives inherited from the parent. Note that if the generic
16814 -- actual is present, this is not really a type derivation, it is a
16815 -- completion within an instance.
16817 -- Case 1: Derived_Type does not implement interfaces
16819 if not Is_Tagged_Type (Derived_Type)
16820 or else (not Has_Interfaces (Derived_Type)
16821 and then not (Present (Generic_Actual)
16822 and then Has_Interfaces (Generic_Actual)))
16823 then
16824 Elmt := First_Elmt (Op_List);
16825 while Present (Elmt) loop
16826 Subp := Node (Elmt);
16828 -- Literals are derived earlier in the process of building the
16829 -- derived type, and are skipped here.
16831 if Ekind (Subp) = E_Enumeration_Literal then
16832 null;
16834 -- The actual is a direct descendant and the common primitive
16835 -- operations appear in the same order.
16837 -- If the generic parent type is present, the derived type is an
16838 -- instance of a formal derived type, and within the instance its
16839 -- operations are those of the actual. We derive from the formal
16840 -- type but make the inherited operations aliases of the
16841 -- corresponding operations of the actual.
16843 else
16844 pragma Assert (No (Node (Act_Elmt))
16845 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16846 and then
16847 Type_Conformant
16848 (Subp, Node (Act_Elmt),
16849 Skip_Controlling_Formals => True)));
16851 Derive_Subprogram
16852 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16854 if Present (Act_Elmt) then
16855 Next_Elmt (Act_Elmt);
16856 end if;
16857 end if;
16859 Next_Elmt (Elmt);
16860 end loop;
16862 -- Case 2: Derived_Type implements interfaces
16864 else
16865 -- If the parent type has no predefined primitives we remove
16866 -- predefined primitives from the list of primitives of generic
16867 -- actual to simplify the complexity of this algorithm.
16869 if Present (Generic_Actual) then
16870 declare
16871 Has_Predefined_Primitives : Boolean := False;
16873 begin
16874 -- Check if the parent type has predefined primitives
16876 Elmt := First_Elmt (Op_List);
16877 while Present (Elmt) loop
16878 Subp := Node (Elmt);
16880 if Is_Predefined_Dispatching_Operation (Subp)
16881 and then not Comes_From_Source (Ultimate_Alias (Subp))
16882 then
16883 Has_Predefined_Primitives := True;
16884 exit;
16885 end if;
16887 Next_Elmt (Elmt);
16888 end loop;
16890 -- Remove predefined primitives of Generic_Actual. We must use
16891 -- an auxiliary list because in case of tagged types the value
16892 -- returned by Collect_Primitive_Operations is the value stored
16893 -- in its Primitive_Operations attribute (and we don't want to
16894 -- modify its current contents).
16896 if not Has_Predefined_Primitives then
16897 declare
16898 Aux_List : constant Elist_Id := New_Elmt_List;
16900 begin
16901 Elmt := First_Elmt (Act_List);
16902 while Present (Elmt) loop
16903 Subp := Node (Elmt);
16905 if not Is_Predefined_Dispatching_Operation (Subp)
16906 or else Comes_From_Source (Subp)
16907 then
16908 Append_Elmt (Subp, Aux_List);
16909 end if;
16911 Next_Elmt (Elmt);
16912 end loop;
16914 Act_List := Aux_List;
16915 end;
16916 end if;
16918 Act_Elmt := First_Elmt (Act_List);
16919 Act_Subp := Node (Act_Elmt);
16920 end;
16921 end if;
16923 -- Stage 1: If the generic actual is not present we derive the
16924 -- primitives inherited from the parent type. If the generic parent
16925 -- type is present, the derived type is an instance of a formal
16926 -- derived type, and within the instance its operations are those of
16927 -- the actual. We derive from the formal type but make the inherited
16928 -- operations aliases of the corresponding operations of the actual.
16930 Elmt := First_Elmt (Op_List);
16931 while Present (Elmt) loop
16932 Subp := Node (Elmt);
16933 Alias_Subp := Ultimate_Alias (Subp);
16935 -- Do not derive internal entities of the parent that link
16936 -- interface primitives with their covering primitive. These
16937 -- entities will be added to this type when frozen.
16939 if Present (Interface_Alias (Subp)) then
16940 goto Continue;
16941 end if;
16943 -- If the generic actual is present find the corresponding
16944 -- operation in the generic actual. If the parent type is a
16945 -- direct ancestor of the derived type then, even if it is an
16946 -- interface, the operations are inherited from the primary
16947 -- dispatch table and are in the proper order. If we detect here
16948 -- that primitives are not in the same order we traverse the list
16949 -- of primitive operations of the actual to find the one that
16950 -- implements the interface primitive.
16952 if Need_Search
16953 or else
16954 (Present (Generic_Actual)
16955 and then Present (Act_Subp)
16956 and then not
16957 (Primitive_Names_Match (Subp, Act_Subp)
16958 and then
16959 Type_Conformant (Subp, Act_Subp,
16960 Skip_Controlling_Formals => True)))
16961 then
16962 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16963 Use_Full_View => True));
16965 -- Remember that we need searching for all pending primitives
16967 Need_Search := True;
16969 -- Handle entities associated with interface primitives
16971 if Present (Alias_Subp)
16972 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16973 and then not Is_Predefined_Dispatching_Operation (Subp)
16974 then
16975 -- Search for the primitive in the homonym chain
16977 Act_Subp :=
16978 Find_Primitive_Covering_Interface
16979 (Tagged_Type => Generic_Actual,
16980 Iface_Prim => Alias_Subp);
16982 -- Previous search may not locate primitives covering
16983 -- interfaces defined in generics units or instantiations.
16984 -- (it fails if the covering primitive has formals whose
16985 -- type is also defined in generics or instantiations).
16986 -- In such case we search in the list of primitives of the
16987 -- generic actual for the internal entity that links the
16988 -- interface primitive and the covering primitive.
16990 if No (Act_Subp)
16991 and then Is_Generic_Type (Parent_Type)
16992 then
16993 -- This code has been designed to handle only generic
16994 -- formals that implement interfaces that are defined
16995 -- in a generic unit or instantiation. If this code is
16996 -- needed for other cases we must review it because
16997 -- (given that it relies on Original_Location to locate
16998 -- the primitive of Generic_Actual that covers the
16999 -- interface) it could leave linked through attribute
17000 -- Alias entities of unrelated instantiations).
17002 pragma Assert
17003 (Is_Generic_Unit
17004 (Scope (Find_Dispatching_Type (Alias_Subp)))
17005 or else
17006 Instantiation_Location
17007 (Sloc (Find_Dispatching_Type (Alias_Subp)))
17008 /= No_Location);
17009 declare
17010 Iface_Prim_Loc : constant Source_Ptr :=
17011 Original_Location (Sloc (Alias_Subp));
17013 Elmt : Elmt_Id;
17014 Prim : Entity_Id;
17016 begin
17017 Elmt :=
17018 First_Elmt (Primitive_Operations (Generic_Actual));
17020 Search : while Present (Elmt) loop
17021 Prim := Node (Elmt);
17023 if Present (Interface_Alias (Prim))
17024 and then Original_Location
17025 (Sloc (Interface_Alias (Prim))) =
17026 Iface_Prim_Loc
17027 then
17028 Act_Subp := Alias (Prim);
17029 exit Search;
17030 end if;
17032 Next_Elmt (Elmt);
17033 end loop Search;
17034 end;
17035 end if;
17037 pragma Assert (Present (Act_Subp)
17038 or else Is_Abstract_Type (Generic_Actual)
17039 or else Serious_Errors_Detected > 0);
17041 -- Handle predefined primitives plus the rest of user-defined
17042 -- primitives
17044 else
17045 Act_Elmt := First_Elmt (Act_List);
17046 while Present (Act_Elmt) loop
17047 Act_Subp := Node (Act_Elmt);
17049 exit when Primitive_Names_Match (Subp, Act_Subp)
17050 and then Type_Conformant
17051 (Subp, Act_Subp,
17052 Skip_Controlling_Formals => True)
17053 and then No (Interface_Alias (Act_Subp));
17055 Next_Elmt (Act_Elmt);
17056 end loop;
17058 if No (Act_Elmt) then
17059 Act_Subp := Empty;
17060 end if;
17061 end if;
17062 end if;
17064 -- Case 1: If the parent is a limited interface then it has the
17065 -- predefined primitives of synchronized interfaces. However, the
17066 -- actual type may be a non-limited type and hence it does not
17067 -- have such primitives.
17069 if Present (Generic_Actual)
17070 and then No (Act_Subp)
17071 and then Is_Limited_Interface (Parent_Base)
17072 and then Is_Predefined_Interface_Primitive (Subp)
17073 then
17074 null;
17076 -- Case 2: Inherit entities associated with interfaces that were
17077 -- not covered by the parent type. We exclude here null interface
17078 -- primitives because they do not need special management.
17080 -- We also exclude interface operations that are renamings. If the
17081 -- subprogram is an explicit renaming of an interface primitive,
17082 -- it is a regular primitive operation, and the presence of its
17083 -- alias is not relevant: it has to be derived like any other
17084 -- primitive.
17086 elsif Present (Alias (Subp))
17087 and then Nkind (Unit_Declaration_Node (Subp)) /=
17088 N_Subprogram_Renaming_Declaration
17089 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
17090 and then not
17091 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
17092 and then Null_Present (Parent (Alias_Subp)))
17093 then
17094 -- If this is an abstract private type then we transfer the
17095 -- derivation of the interface primitive from the partial view
17096 -- to the full view. This is safe because all the interfaces
17097 -- must be visible in the partial view. Done to avoid adding
17098 -- a new interface derivation to the private part of the
17099 -- enclosing package; otherwise this new derivation would be
17100 -- decorated as hidden when the analysis of the enclosing
17101 -- package completes.
17103 if Is_Abstract_Type (Derived_Type)
17104 and then In_Private_Part (Current_Scope)
17105 and then Has_Private_Declaration (Derived_Type)
17106 then
17107 declare
17108 Partial_View : Entity_Id;
17109 Elmt : Elmt_Id;
17110 Ent : Entity_Id;
17112 begin
17113 Partial_View := First_Entity (Current_Scope);
17114 loop
17115 exit when No (Partial_View)
17116 or else (Has_Private_Declaration (Partial_View)
17117 and then
17118 Full_View (Partial_View) = Derived_Type);
17120 Next_Entity (Partial_View);
17121 end loop;
17123 -- If the partial view was not found then the source code
17124 -- has errors and the derivation is not needed.
17126 if Present (Partial_View) then
17127 Elmt :=
17128 First_Elmt (Primitive_Operations (Partial_View));
17129 while Present (Elmt) loop
17130 Ent := Node (Elmt);
17132 if Present (Alias (Ent))
17133 and then Ultimate_Alias (Ent) = Alias (Subp)
17134 then
17135 Append_Elmt
17136 (Ent, Primitive_Operations (Derived_Type));
17137 exit;
17138 end if;
17140 Next_Elmt (Elmt);
17141 end loop;
17143 -- If the interface primitive was not found in the
17144 -- partial view then this interface primitive was
17145 -- overridden. We add a derivation to activate in
17146 -- Derive_Progenitor_Subprograms the machinery to
17147 -- search for it.
17149 if No (Elmt) then
17150 Derive_Interface_Subprogram
17151 (New_Subp => New_Subp,
17152 Subp => Subp,
17153 Actual_Subp => Act_Subp);
17154 end if;
17155 end if;
17156 end;
17157 else
17158 Derive_Interface_Subprogram
17159 (New_Subp => New_Subp,
17160 Subp => Subp,
17161 Actual_Subp => Act_Subp);
17162 end if;
17164 -- Case 3: Common derivation
17166 else
17167 Derive_Subprogram
17168 (New_Subp => New_Subp,
17169 Parent_Subp => Subp,
17170 Derived_Type => Derived_Type,
17171 Parent_Type => Parent_Base,
17172 Actual_Subp => Act_Subp);
17173 end if;
17175 -- No need to update Act_Elm if we must search for the
17176 -- corresponding operation in the generic actual
17178 if not Need_Search
17179 and then Present (Act_Elmt)
17180 then
17181 Next_Elmt (Act_Elmt);
17182 Act_Subp := Node (Act_Elmt);
17183 end if;
17185 <<Continue>>
17186 Next_Elmt (Elmt);
17187 end loop;
17189 -- Inherit additional operations from progenitors. If the derived
17190 -- type is a generic actual, there are not new primitive operations
17191 -- for the type because it has those of the actual, and therefore
17192 -- nothing needs to be done. The renamings generated above are not
17193 -- primitive operations, and their purpose is simply to make the
17194 -- proper operations visible within an instantiation.
17196 if No (Generic_Actual) then
17197 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17198 end if;
17199 end if;
17201 -- Final check: Direct descendants must have their primitives in the
17202 -- same order. We exclude from this test untagged types and instances
17203 -- of formal derived types. We skip this test if we have already
17204 -- reported serious errors in the sources.
17206 pragma Assert (not Is_Tagged_Type (Derived_Type)
17207 or else Present (Generic_Actual)
17208 or else Serious_Errors_Detected > 0
17209 or else Check_Derived_Type);
17210 end Derive_Subprograms;
17212 --------------------------------
17213 -- Derived_Standard_Character --
17214 --------------------------------
17216 procedure Derived_Standard_Character
17217 (N : Node_Id;
17218 Parent_Type : Entity_Id;
17219 Derived_Type : Entity_Id)
17221 Loc : constant Source_Ptr := Sloc (N);
17222 Def : constant Node_Id := Type_Definition (N);
17223 Indic : constant Node_Id := Subtype_Indication (Def);
17224 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17225 Implicit_Base : constant Entity_Id :=
17226 Create_Itype
17227 (E_Enumeration_Type, N, Derived_Type, 'B');
17229 Lo : Node_Id;
17230 Hi : Node_Id;
17232 begin
17233 Discard_Node (Process_Subtype (Indic, N));
17235 Set_Etype (Implicit_Base, Parent_Base);
17236 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17237 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17239 Set_Is_Character_Type (Implicit_Base, True);
17240 Set_Has_Delayed_Freeze (Implicit_Base);
17242 -- The bounds of the implicit base are the bounds of the parent base.
17243 -- Note that their type is the parent base.
17245 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17246 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17248 Set_Scalar_Range (Implicit_Base,
17249 Make_Range (Loc,
17250 Low_Bound => Lo,
17251 High_Bound => Hi));
17253 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17254 Set_Etype (Derived_Type, Implicit_Base);
17255 Set_Size_Info (Derived_Type, Parent_Type);
17257 if not Known_RM_Size (Derived_Type) then
17258 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17259 end if;
17261 Set_Is_Character_Type (Derived_Type, True);
17263 if Nkind (Indic) /= N_Subtype_Indication then
17265 -- If no explicit constraint, the bounds are those
17266 -- of the parent type.
17268 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17269 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17270 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17271 end if;
17273 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17274 end Derived_Standard_Character;
17276 ------------------------------
17277 -- Derived_Type_Declaration --
17278 ------------------------------
17280 procedure Derived_Type_Declaration
17281 (T : Entity_Id;
17282 N : Node_Id;
17283 Is_Completion : Boolean)
17285 Parent_Type : Entity_Id;
17287 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17288 -- Check whether the parent type is a generic formal, or derives
17289 -- directly or indirectly from one.
17291 ------------------------
17292 -- Comes_From_Generic --
17293 ------------------------
17295 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17296 begin
17297 if Is_Generic_Type (Typ) then
17298 return True;
17300 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17301 return True;
17303 elsif Is_Private_Type (Typ)
17304 and then Present (Full_View (Typ))
17305 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17306 then
17307 return True;
17309 elsif Is_Generic_Actual_Type (Typ) then
17310 return True;
17312 else
17313 return False;
17314 end if;
17315 end Comes_From_Generic;
17317 -- Local variables
17319 Def : constant Node_Id := Type_Definition (N);
17320 Iface_Def : Node_Id;
17321 Indic : constant Node_Id := Subtype_Indication (Def);
17322 Extension : constant Node_Id := Record_Extension_Part (Def);
17323 Parent_Node : Node_Id;
17324 Taggd : Boolean;
17326 -- Start of processing for Derived_Type_Declaration
17328 begin
17329 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17331 -- Ada 2005 (AI-251): In case of interface derivation check that the
17332 -- parent is also an interface.
17334 if Interface_Present (Def) then
17335 if not Is_Interface (Parent_Type) then
17336 Diagnose_Interface (Indic, Parent_Type);
17338 else
17339 Parent_Node := Parent (Base_Type (Parent_Type));
17340 Iface_Def := Type_Definition (Parent_Node);
17342 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17343 -- other limited interfaces.
17345 if Limited_Present (Def) then
17346 if Limited_Present (Iface_Def) then
17347 null;
17349 elsif Protected_Present (Iface_Def) then
17350 Error_Msg_NE
17351 ("descendant of & must be declared as a protected "
17352 & "interface", N, Parent_Type);
17354 elsif Synchronized_Present (Iface_Def) then
17355 Error_Msg_NE
17356 ("descendant of & must be declared as a synchronized "
17357 & "interface", N, Parent_Type);
17359 elsif Task_Present (Iface_Def) then
17360 Error_Msg_NE
17361 ("descendant of & must be declared as a task interface",
17362 N, Parent_Type);
17364 else
17365 Error_Msg_N
17366 ("(Ada 2005) limited interface cannot inherit from "
17367 & "non-limited interface", Indic);
17368 end if;
17370 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17371 -- from non-limited or limited interfaces.
17373 elsif not Protected_Present (Def)
17374 and then not Synchronized_Present (Def)
17375 and then not Task_Present (Def)
17376 then
17377 if Limited_Present (Iface_Def) then
17378 null;
17380 elsif Protected_Present (Iface_Def) then
17381 Error_Msg_NE
17382 ("descendant of & must be declared as a protected "
17383 & "interface", N, Parent_Type);
17385 elsif Synchronized_Present (Iface_Def) then
17386 Error_Msg_NE
17387 ("descendant of & must be declared as a synchronized "
17388 & "interface", N, Parent_Type);
17390 elsif Task_Present (Iface_Def) then
17391 Error_Msg_NE
17392 ("descendant of & must be declared as a task interface",
17393 N, Parent_Type);
17394 else
17395 null;
17396 end if;
17397 end if;
17398 end if;
17399 end if;
17401 if Is_Tagged_Type (Parent_Type)
17402 and then Is_Concurrent_Type (Parent_Type)
17403 and then not Is_Interface (Parent_Type)
17404 then
17405 Error_Msg_N
17406 ("parent type of a record extension cannot be a synchronized "
17407 & "tagged type (RM 3.9.1 (3/1))", N);
17408 Set_Etype (T, Any_Type);
17409 return;
17410 end if;
17412 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17413 -- interfaces
17415 if Is_Tagged_Type (Parent_Type)
17416 and then Is_Non_Empty_List (Interface_List (Def))
17417 then
17418 declare
17419 Intf : Node_Id;
17420 T : Entity_Id;
17422 begin
17423 Intf := First (Interface_List (Def));
17424 while Present (Intf) loop
17425 T := Find_Type_Of_Subtype_Indic (Intf);
17427 if not Is_Interface (T) then
17428 Diagnose_Interface (Intf, T);
17430 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17431 -- a limited type from having a nonlimited progenitor.
17433 elsif (Limited_Present (Def)
17434 or else (not Is_Interface (Parent_Type)
17435 and then Is_Limited_Type (Parent_Type)))
17436 and then not Is_Limited_Interface (T)
17437 then
17438 Error_Msg_NE
17439 ("progenitor interface& of limited type must be limited",
17440 N, T);
17441 end if;
17443 Next (Intf);
17444 end loop;
17445 end;
17447 -- Check consistency of any nonoverridable aspects that are
17448 -- inherited from multiple sources.
17450 Check_Inherited_Nonoverridable_Aspects
17451 (Inheritor => T,
17452 Interface_List => Interface_List (Def),
17453 Parent_Type => Parent_Type);
17454 end if;
17456 if Parent_Type = Any_Type
17457 or else Etype (Parent_Type) = Any_Type
17458 or else (Is_Class_Wide_Type (Parent_Type)
17459 and then Etype (Parent_Type) = T)
17460 then
17461 -- If Parent_Type is undefined or illegal, make new type into a
17462 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17463 -- errors. If this is a self-definition, emit error now.
17465 if T = Parent_Type or else T = Etype (Parent_Type) then
17466 Error_Msg_N ("type cannot be used in its own definition", Indic);
17467 end if;
17469 Mutate_Ekind (T, Ekind (Parent_Type));
17470 Set_Etype (T, Any_Type);
17471 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17473 -- Initialize the list of primitive operations to an empty list,
17474 -- to cover tagged types as well as untagged types. For untagged
17475 -- types this is used either to analyze the call as legal when
17476 -- Extensions_Allowed is True, or to issue a better error message
17477 -- otherwise.
17479 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17481 return;
17482 end if;
17484 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17485 -- an interface is special because the list of interfaces in the full
17486 -- view can be given in any order. For example:
17488 -- type A is interface;
17489 -- type B is interface and A;
17490 -- type D is new B with private;
17491 -- private
17492 -- type D is new A and B with null record; -- 1 --
17494 -- In this case we perform the following transformation of -1-:
17496 -- type D is new B and A with null record;
17498 -- If the parent of the full-view covers the parent of the partial-view
17499 -- we have two possible cases:
17501 -- 1) They have the same parent
17502 -- 2) The parent of the full-view implements some further interfaces
17504 -- In both cases we do not need to perform the transformation. In the
17505 -- first case the source program is correct and the transformation is
17506 -- not needed; in the second case the source program does not fulfill
17507 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17508 -- later.
17510 -- This transformation not only simplifies the rest of the analysis of
17511 -- this type declaration but also simplifies the correct generation of
17512 -- the object layout to the expander.
17514 if In_Private_Part (Current_Scope)
17515 and then Is_Interface (Parent_Type)
17516 then
17517 declare
17518 Partial_View : Entity_Id;
17519 Partial_View_Parent : Entity_Id;
17521 function Reorder_Interfaces return Boolean;
17522 -- Look for an interface in the full view's interface list that
17523 -- matches the parent type of the partial view, and when found,
17524 -- rewrite the full view's parent with the partial view's parent,
17525 -- append the full view's original parent to the interface list,
17526 -- recursively call Derived_Type_Definition on the full type, and
17527 -- return True. If a match is not found, return False.
17529 ------------------------
17530 -- Reorder_Interfaces --
17531 ------------------------
17533 function Reorder_Interfaces return Boolean is
17534 Iface : Node_Id;
17535 New_Iface : Node_Id;
17537 begin
17538 Iface := First (Interface_List (Def));
17539 while Present (Iface) loop
17540 if Etype (Iface) = Etype (Partial_View) then
17541 Rewrite (Subtype_Indication (Def),
17542 New_Copy (Subtype_Indication (Parent (Partial_View))));
17544 New_Iface :=
17545 Make_Identifier (Sloc (N), Chars (Parent_Type));
17546 Rewrite (Iface, New_Iface);
17548 -- Analyze the transformed code
17550 Derived_Type_Declaration (T, N, Is_Completion);
17551 return True;
17552 end if;
17554 Next (Iface);
17555 end loop;
17556 return False;
17557 end Reorder_Interfaces;
17559 begin
17560 -- Look for the associated private type declaration
17562 Partial_View := Incomplete_Or_Partial_View (T);
17564 -- If the partial view was not found then the source code has
17565 -- errors and the transformation is not needed.
17567 if Present (Partial_View) then
17568 Partial_View_Parent := Etype (Partial_View);
17570 -- If the parent of the full-view covers the parent of the
17571 -- partial-view we have nothing else to do.
17573 if Interface_Present_In_Ancestor
17574 (Parent_Type, Partial_View_Parent)
17575 then
17576 null;
17578 -- Traverse the list of interfaces of the full view to look
17579 -- for the parent of the partial view and reorder the
17580 -- interfaces to match the order in the partial view,
17581 -- if needed.
17583 else
17585 if Reorder_Interfaces then
17586 -- Having the interfaces listed in any order is legal.
17587 -- However, the compiler does not properly handle
17588 -- different orders between partial and full views in
17589 -- generic units. We give a warning about the order
17590 -- mismatch, so the user can work around this problem.
17592 Error_Msg_N ("??full declaration does not respect " &
17593 "partial declaration order", T);
17594 Error_Msg_N ("\??consider reordering", T);
17596 return;
17597 end if;
17598 end if;
17599 end if;
17600 end;
17601 end if;
17603 -- Only composite types other than array types are allowed to have
17604 -- discriminants.
17606 if Present (Discriminant_Specifications (N)) then
17607 if (Is_Elementary_Type (Parent_Type)
17608 or else
17609 Is_Array_Type (Parent_Type))
17610 and then not Error_Posted (N)
17611 then
17612 Error_Msg_N
17613 ("elementary or array type cannot have discriminants",
17614 Defining_Identifier (First (Discriminant_Specifications (N))));
17616 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17617 -- only if we are not already processing a malformed syntax tree.
17619 if Is_Type (T) then
17620 Set_Has_Discriminants (T, False);
17621 end if;
17622 end if;
17623 end if;
17625 -- In Ada 83, a derived type defined in a package specification cannot
17626 -- be used for further derivation until the end of its visible part.
17627 -- Note that derivation in the private part of the package is allowed.
17629 if Ada_Version = Ada_83
17630 and then Is_Derived_Type (Parent_Type)
17631 and then In_Visible_Part (Scope (Parent_Type))
17632 then
17633 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17634 Error_Msg_N
17635 ("(Ada 83) premature use of type for derivation", Indic);
17636 end if;
17637 end if;
17639 -- Check for early use of incomplete or private type
17641 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17642 Error_Msg_N ("premature derivation of incomplete type", Indic);
17643 return;
17645 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17646 and then not Comes_From_Generic (Parent_Type))
17647 or else Has_Private_Component (Parent_Type)
17648 then
17649 -- The ancestor type of a formal type can be incomplete, in which
17650 -- case only the operations of the partial view are available in the
17651 -- generic. Subsequent checks may be required when the full view is
17652 -- analyzed to verify that a derivation from a tagged type has an
17653 -- extension.
17655 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17656 null;
17658 elsif No (Underlying_Type (Parent_Type))
17659 or else Has_Private_Component (Parent_Type)
17660 then
17661 Error_Msg_N
17662 ("premature derivation of derived or private type", Indic);
17664 -- Flag the type itself as being in error, this prevents some
17665 -- nasty problems with subsequent uses of the malformed type.
17667 Set_Error_Posted (T);
17669 -- Check that within the immediate scope of an untagged partial
17670 -- view it's illegal to derive from the partial view if the
17671 -- full view is tagged. (7.3(7))
17673 -- We verify that the Parent_Type is a partial view by checking
17674 -- that it is not a Full_Type_Declaration (i.e. a private type or
17675 -- private extension declaration), to distinguish a partial view
17676 -- from a derivation from a private type which also appears as
17677 -- E_Private_Type. If the parent base type is not declared in an
17678 -- enclosing scope there is no need to check.
17680 elsif Present (Full_View (Parent_Type))
17681 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17682 and then not Is_Tagged_Type (Parent_Type)
17683 and then Is_Tagged_Type (Full_View (Parent_Type))
17684 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17685 then
17686 Error_Msg_N
17687 ("premature derivation from type with tagged full view",
17688 Indic);
17689 end if;
17690 end if;
17692 -- Check that form of derivation is appropriate
17694 Taggd := Is_Tagged_Type (Parent_Type);
17696 -- Set the parent type to the class-wide type's specific type in this
17697 -- case to prevent cascading errors
17699 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17700 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17701 Set_Etype (T, Etype (Parent_Type));
17702 return;
17703 end if;
17705 if Present (Extension) and then not Taggd then
17706 Error_Msg_N
17707 ("type derived from untagged type cannot have extension", Indic);
17709 elsif No (Extension) and then Taggd then
17711 -- If this declaration is within a private part (or body) of a
17712 -- generic instantiation then the derivation is allowed (the parent
17713 -- type can only appear tagged in this case if it's a generic actual
17714 -- type, since it would otherwise have been rejected in the analysis
17715 -- of the generic template).
17717 if not Is_Generic_Actual_Type (Parent_Type)
17718 or else In_Visible_Part (Scope (Parent_Type))
17719 then
17720 if Is_Class_Wide_Type (Parent_Type) then
17721 Error_Msg_N
17722 ("parent type must not be a class-wide type", Indic);
17724 -- Use specific type to prevent cascaded errors.
17726 Parent_Type := Etype (Parent_Type);
17728 else
17729 Error_Msg_N
17730 ("type derived from tagged type must have extension", Indic);
17731 end if;
17732 end if;
17733 end if;
17735 -- AI-443: Synchronized formal derived types require a private
17736 -- extension. There is no point in checking the ancestor type or
17737 -- the progenitors since the construct is wrong to begin with.
17739 if Ada_Version >= Ada_2005
17740 and then Is_Generic_Type (T)
17741 and then Present (Original_Node (N))
17742 then
17743 declare
17744 Decl : constant Node_Id := Original_Node (N);
17746 begin
17747 if Nkind (Decl) = N_Formal_Type_Declaration
17748 and then Nkind (Formal_Type_Definition (Decl)) =
17749 N_Formal_Derived_Type_Definition
17750 and then Synchronized_Present (Formal_Type_Definition (Decl))
17751 and then No (Extension)
17753 -- Avoid emitting a duplicate error message
17755 and then not Error_Posted (Indic)
17756 then
17757 Error_Msg_N
17758 ("synchronized derived type must have extension", N);
17759 end if;
17760 end;
17761 end if;
17763 if Null_Exclusion_Present (Def)
17764 and then not Is_Access_Type (Parent_Type)
17765 then
17766 Error_Msg_N ("null exclusion can only apply to an access type", N);
17767 end if;
17769 Check_Wide_Character_Restriction (Parent_Type, Indic);
17771 -- Avoid deriving parent primitives of underlying record views
17773 Set_Is_Not_Self_Hidden (T);
17775 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17776 Derive_Subps => not Is_Underlying_Record_View (T));
17778 -- AI-419: The parent type of an explicitly limited derived type must
17779 -- be a limited type or a limited interface.
17781 if Limited_Present (Def) then
17782 Set_Is_Limited_Record (T);
17784 if Is_Interface (T) then
17785 Set_Is_Limited_Interface (T);
17786 end if;
17788 if not Is_Limited_Type (Parent_Type)
17789 and then
17790 (not Is_Interface (Parent_Type)
17791 or else not Is_Limited_Interface (Parent_Type))
17792 then
17793 -- AI05-0096: a derivation in the private part of an instance is
17794 -- legal if the generic formal is untagged limited, and the actual
17795 -- is non-limited.
17797 if Is_Generic_Actual_Type (Parent_Type)
17798 and then In_Private_Part (Current_Scope)
17799 and then
17800 not Is_Tagged_Type
17801 (Generic_Parent_Type (Parent (Parent_Type)))
17802 then
17803 null;
17805 else
17806 Error_Msg_NE
17807 ("parent type& of limited type must be limited",
17808 N, Parent_Type);
17809 end if;
17810 end if;
17811 end if;
17812 end Derived_Type_Declaration;
17814 ------------------------
17815 -- Diagnose_Interface --
17816 ------------------------
17818 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17819 begin
17820 if not Is_Interface (E) and then E /= Any_Type then
17821 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17822 end if;
17823 end Diagnose_Interface;
17825 ----------------------------------
17826 -- Enumeration_Type_Declaration --
17827 ----------------------------------
17829 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17830 Ev : Uint;
17831 L : Node_Id;
17832 R_Node : Node_Id;
17833 B_Node : Node_Id;
17835 begin
17836 -- Create identifier node representing lower bound
17838 B_Node := New_Node (N_Identifier, Sloc (Def));
17839 L := First (Literals (Def));
17840 Set_Chars (B_Node, Chars (L));
17841 Set_Entity (B_Node, L);
17842 Set_Etype (B_Node, T);
17843 Set_Is_Static_Expression (B_Node, True);
17845 R_Node := New_Node (N_Range, Sloc (Def));
17846 Set_Low_Bound (R_Node, B_Node);
17848 Mutate_Ekind (T, E_Enumeration_Type);
17849 Set_First_Literal (T, L);
17850 Set_Etype (T, T);
17851 Set_Is_Constrained (T);
17853 Ev := Uint_0;
17855 -- Loop through literals of enumeration type setting pos and rep values
17856 -- except that if the Ekind is already set, then it means the literal
17857 -- was already constructed (case of a derived type declaration and we
17858 -- should not disturb the Pos and Rep values.
17860 while Present (L) loop
17861 if Ekind (L) /= E_Enumeration_Literal then
17862 Mutate_Ekind (L, E_Enumeration_Literal);
17863 Set_Is_Not_Self_Hidden (L);
17864 Set_Enumeration_Pos (L, Ev);
17865 Set_Enumeration_Rep (L, Ev);
17866 Set_Is_Known_Valid (L, True);
17867 end if;
17869 Set_Etype (L, T);
17870 New_Overloaded_Entity (L);
17871 Generate_Definition (L);
17872 Set_Convention (L, Convention_Intrinsic);
17874 -- Case of character literal
17876 if Nkind (L) = N_Defining_Character_Literal then
17877 Set_Is_Character_Type (T, True);
17879 -- Check violation of No_Wide_Characters
17881 if Restriction_Check_Required (No_Wide_Characters) then
17882 Get_Name_String (Chars (L));
17884 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17885 Check_Restriction (No_Wide_Characters, L);
17886 end if;
17887 end if;
17888 end if;
17890 Ev := Ev + 1;
17891 Next (L);
17892 end loop;
17894 -- Now create a node representing upper bound
17896 B_Node := New_Node (N_Identifier, Sloc (Def));
17897 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17898 Set_Entity (B_Node, Last (Literals (Def)));
17899 Set_Etype (B_Node, T);
17900 Set_Is_Static_Expression (B_Node, True);
17902 Set_High_Bound (R_Node, B_Node);
17904 -- Initialize various fields of the type. Some of this information
17905 -- may be overwritten later through rep. clauses.
17907 Set_Scalar_Range (T, R_Node);
17908 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17909 Set_Enum_Esize (T);
17910 Set_Enum_Pos_To_Rep (T, Empty);
17912 -- Set Discard_Names if configuration pragma set, or if there is
17913 -- a parameterless pragma in the current declarative region
17915 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17916 Set_Discard_Names (T);
17917 end if;
17919 -- Process end label if there is one
17921 if Present (Def) then
17922 Process_End_Label (Def, 'e', T);
17923 end if;
17924 end Enumeration_Type_Declaration;
17926 ---------------------------------
17927 -- Expand_To_Stored_Constraint --
17928 ---------------------------------
17930 function Expand_To_Stored_Constraint
17931 (Typ : Entity_Id;
17932 Constraint : Elist_Id) return Elist_Id
17934 Explicitly_Discriminated_Type : Entity_Id;
17935 Expansion : Elist_Id;
17936 Discriminant : Entity_Id;
17938 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17939 -- Find the nearest type that actually specifies discriminants
17941 ---------------------------------
17942 -- Type_With_Explicit_Discrims --
17943 ---------------------------------
17945 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17946 Typ : constant E := Base_Type (Id);
17948 begin
17949 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17950 if Present (Full_View (Typ)) then
17951 return Type_With_Explicit_Discrims (Full_View (Typ));
17952 end if;
17954 else
17955 if Has_Discriminants (Typ) then
17956 return Typ;
17957 end if;
17958 end if;
17960 if Etype (Typ) = Typ then
17961 return Empty;
17962 elsif Has_Discriminants (Typ) then
17963 return Typ;
17964 else
17965 return Type_With_Explicit_Discrims (Etype (Typ));
17966 end if;
17968 end Type_With_Explicit_Discrims;
17970 -- Start of processing for Expand_To_Stored_Constraint
17972 begin
17973 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17974 return No_Elist;
17975 end if;
17977 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17979 if No (Explicitly_Discriminated_Type) then
17980 return No_Elist;
17981 end if;
17983 Expansion := New_Elmt_List;
17985 Discriminant :=
17986 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17987 while Present (Discriminant) loop
17988 Append_Elmt
17989 (Get_Discriminant_Value
17990 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17991 To => Expansion);
17992 Next_Stored_Discriminant (Discriminant);
17993 end loop;
17995 return Expansion;
17996 end Expand_To_Stored_Constraint;
17998 ---------------------------
17999 -- Find_Hidden_Interface --
18000 ---------------------------
18002 function Find_Hidden_Interface
18003 (Src : Elist_Id;
18004 Dest : Elist_Id) return Entity_Id
18006 Iface : Entity_Id;
18007 Iface_Elmt : Elmt_Id;
18009 begin
18010 if Present (Src) and then Present (Dest) then
18011 Iface_Elmt := First_Elmt (Src);
18012 while Present (Iface_Elmt) loop
18013 Iface := Node (Iface_Elmt);
18015 if Is_Interface (Iface)
18016 and then not Contain_Interface (Iface, Dest)
18017 then
18018 return Iface;
18019 end if;
18021 Next_Elmt (Iface_Elmt);
18022 end loop;
18023 end if;
18025 return Empty;
18026 end Find_Hidden_Interface;
18028 --------------------
18029 -- Find_Type_Name --
18030 --------------------
18032 function Find_Type_Name (N : Node_Id) return Entity_Id is
18033 Id : constant Entity_Id := Defining_Identifier (N);
18034 New_Id : Entity_Id;
18035 Prev : Entity_Id;
18036 Prev_Par : Node_Id;
18038 procedure Check_Duplicate_Aspects;
18039 -- Check that aspects specified in a completion have not been specified
18040 -- already in the partial view.
18042 procedure Tag_Mismatch;
18043 -- Diagnose a tagged partial view whose full view is untagged. We post
18044 -- the message on the full view, with a reference to the previous
18045 -- partial view. The partial view can be private or incomplete, and
18046 -- these are handled in a different manner, so we determine the position
18047 -- of the error message from the respective slocs of both.
18049 -----------------------------
18050 -- Check_Duplicate_Aspects --
18051 -----------------------------
18053 procedure Check_Duplicate_Aspects is
18054 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
18055 -- Return the corresponding aspect of the partial view which matches
18056 -- the aspect id of Asp. Return Empty is no such aspect exists.
18058 -----------------------------
18059 -- Get_Partial_View_Aspect --
18060 -----------------------------
18062 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
18063 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
18064 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
18065 Prev_Asp : Node_Id;
18067 begin
18068 Prev_Asp := First (Prev_Asps);
18069 while Present (Prev_Asp) loop
18070 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
18071 return Prev_Asp;
18072 end if;
18074 Next (Prev_Asp);
18075 end loop;
18077 return Empty;
18078 end Get_Partial_View_Aspect;
18080 -- Local variables
18082 Full_Asps : constant List_Id := Aspect_Specifications (N);
18083 Full_Asp : Node_Id;
18084 Part_Asp : Node_Id;
18086 -- Start of processing for Check_Duplicate_Aspects
18088 begin
18089 Full_Asp := First (Full_Asps);
18090 while Present (Full_Asp) loop
18091 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
18093 -- An aspect and its class-wide counterpart are two distinct
18094 -- aspects and may apply to both views of an entity.
18096 if Present (Part_Asp)
18097 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
18098 then
18099 Error_Msg_N
18100 ("aspect already specified in private declaration", Full_Asp);
18102 Remove (Full_Asp);
18103 return;
18104 end if;
18106 if Has_Discriminants (Prev)
18107 and then not Has_Unknown_Discriminants (Prev)
18108 and then Get_Aspect_Id (Full_Asp) =
18109 Aspect_Implicit_Dereference
18110 then
18111 Error_Msg_N
18112 ("cannot specify aspect if partial view has known "
18113 & "discriminants", Full_Asp);
18114 end if;
18116 Next (Full_Asp);
18117 end loop;
18118 end Check_Duplicate_Aspects;
18120 ------------------
18121 -- Tag_Mismatch --
18122 ------------------
18124 procedure Tag_Mismatch is
18125 begin
18126 if Sloc (Prev) < Sloc (Id) then
18127 if Ada_Version >= Ada_2012
18128 and then Nkind (N) = N_Private_Type_Declaration
18129 then
18130 Error_Msg_NE
18131 ("declaration of private } must be a tagged type", Id, Prev);
18132 else
18133 Error_Msg_NE
18134 ("full declaration of } must be a tagged type", Id, Prev);
18135 end if;
18137 else
18138 if Ada_Version >= Ada_2012
18139 and then Nkind (N) = N_Private_Type_Declaration
18140 then
18141 Error_Msg_NE
18142 ("declaration of private } must be a tagged type", Prev, Id);
18143 else
18144 Error_Msg_NE
18145 ("full declaration of } must be a tagged type", Prev, Id);
18146 end if;
18147 end if;
18148 end Tag_Mismatch;
18150 -- Start of processing for Find_Type_Name
18152 begin
18153 -- Find incomplete declaration, if one was given
18155 Prev := Current_Entity_In_Scope (Id);
18157 -- New type declaration
18159 if No (Prev) then
18160 Enter_Name (Id);
18161 return Id;
18163 -- Previous declaration exists
18165 else
18166 Prev_Par := Parent (Prev);
18168 -- Error if not incomplete/private case except if previous
18169 -- declaration is implicit, etc. Enter_Name will emit error if
18170 -- appropriate.
18172 if not Is_Incomplete_Or_Private_Type (Prev) then
18173 Enter_Name (Id);
18174 New_Id := Id;
18176 -- Check invalid completion of private or incomplete type
18178 elsif Nkind (N) not in N_Full_Type_Declaration
18179 | N_Task_Type_Declaration
18180 | N_Protected_Type_Declaration
18181 and then
18182 (Ada_Version < Ada_2012
18183 or else not Is_Incomplete_Type (Prev)
18184 or else Nkind (N) not in N_Private_Type_Declaration
18185 | N_Private_Extension_Declaration)
18186 then
18187 -- Completion must be a full type declarations (RM 7.3(4))
18189 Error_Msg_Sloc := Sloc (Prev);
18190 Error_Msg_NE ("invalid completion of }", Id, Prev);
18192 -- Set scope of Id to avoid cascaded errors. Entity is never
18193 -- examined again, except when saving globals in generics.
18195 Set_Scope (Id, Current_Scope);
18196 New_Id := Id;
18198 -- If this is a repeated incomplete declaration, no further
18199 -- checks are possible.
18201 if Nkind (N) = N_Incomplete_Type_Declaration then
18202 return Prev;
18203 end if;
18205 -- Case of full declaration of incomplete type
18207 elsif Ekind (Prev) = E_Incomplete_Type
18208 and then (Ada_Version < Ada_2012
18209 or else No (Full_View (Prev))
18210 or else not Is_Private_Type (Full_View (Prev)))
18211 then
18212 -- Indicate that the incomplete declaration has a matching full
18213 -- declaration. The defining occurrence of the incomplete
18214 -- declaration remains the visible one, and the procedure
18215 -- Get_Full_View dereferences it whenever the type is used.
18217 if Present (Full_View (Prev)) then
18218 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18219 end if;
18221 Set_Full_View (Prev, Id);
18222 Append_Entity (Id, Current_Scope);
18223 Set_Is_Public (Id, Is_Public (Prev));
18224 Set_Is_Internal (Id);
18225 New_Id := Prev;
18227 -- If the incomplete view is tagged, a class_wide type has been
18228 -- created already. Use it for the private type as well, in order
18229 -- to prevent multiple incompatible class-wide types that may be
18230 -- created for self-referential anonymous access components.
18232 if Is_Tagged_Type (Prev)
18233 and then Present (Class_Wide_Type (Prev))
18234 then
18235 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18236 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18238 -- Type of the class-wide type is the current Id. Previously
18239 -- this was not done for private declarations because of order-
18240 -- of-elaboration issues in the back end, but gigi now handles
18241 -- this properly.
18243 Set_Etype (Class_Wide_Type (Id), Id);
18244 end if;
18246 -- Case of full declaration of private type
18248 else
18249 -- If the private type was a completion of an incomplete type then
18250 -- update Prev to reference the private type
18252 if Ada_Version >= Ada_2012
18253 and then Ekind (Prev) = E_Incomplete_Type
18254 and then Present (Full_View (Prev))
18255 and then Is_Private_Type (Full_View (Prev))
18256 then
18257 Prev := Full_View (Prev);
18258 Prev_Par := Parent (Prev);
18259 end if;
18261 if Nkind (N) = N_Full_Type_Declaration
18262 and then Nkind (Type_Definition (N)) in
18263 N_Record_Definition | N_Derived_Type_Definition
18264 and then Interface_Present (Type_Definition (N))
18265 then
18266 Error_Msg_N
18267 ("completion of private type cannot be an interface", N);
18268 end if;
18270 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18271 if Etype (Prev) /= Prev then
18273 -- Prev is a private subtype or a derived type, and needs
18274 -- no completion.
18276 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18277 New_Id := Id;
18279 elsif Ekind (Prev) = E_Private_Type
18280 and then Nkind (N) in N_Task_Type_Declaration
18281 | N_Protected_Type_Declaration
18282 then
18283 Error_Msg_N
18284 ("completion of nonlimited type cannot be limited", N);
18286 elsif Ekind (Prev) = E_Record_Type_With_Private
18287 and then Nkind (N) in N_Task_Type_Declaration
18288 | N_Protected_Type_Declaration
18289 then
18290 if not Is_Limited_Record (Prev) then
18291 Error_Msg_N
18292 ("completion of nonlimited type cannot be limited", N);
18294 elsif No (Interface_List (N)) then
18295 Error_Msg_N
18296 ("completion of tagged private type must be tagged",
18298 end if;
18299 end if;
18301 -- Ada 2005 (AI-251): Private extension declaration of a task
18302 -- type or a protected type. This case arises when covering
18303 -- interface types.
18305 elsif Nkind (N) in N_Task_Type_Declaration
18306 | N_Protected_Type_Declaration
18307 then
18308 null;
18310 elsif Nkind (N) /= N_Full_Type_Declaration
18311 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18312 then
18313 Error_Msg_N
18314 ("full view of private extension must be an extension", N);
18316 elsif not (Abstract_Present (Parent (Prev)))
18317 and then Abstract_Present (Type_Definition (N))
18318 then
18319 Error_Msg_N
18320 ("full view of non-abstract extension cannot be abstract", N);
18321 end if;
18323 if not In_Private_Part (Current_Scope) then
18324 Error_Msg_N
18325 ("declaration of full view must appear in private part", N);
18326 end if;
18328 if Ada_Version >= Ada_2012 then
18329 Check_Duplicate_Aspects;
18330 end if;
18332 Copy_And_Swap (Prev, Id);
18333 Set_Has_Private_Declaration (Prev);
18334 Set_Has_Private_Declaration (Id);
18336 -- AI12-0133: Indicate whether we have a partial view with
18337 -- unknown discriminants, in which case initialization of objects
18338 -- of the type do not receive an invariant check.
18340 Set_Partial_View_Has_Unknown_Discr
18341 (Prev, Has_Unknown_Discriminants (Id));
18343 -- Preserve aspect and iterator flags that may have been set on
18344 -- the partial view.
18346 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18347 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18349 -- If no error, propagate freeze_node from private to full view.
18350 -- It may have been generated for an early operational item.
18352 if Present (Freeze_Node (Id))
18353 and then Serious_Errors_Detected = 0
18354 and then No (Full_View (Id))
18355 then
18356 Set_Freeze_Node (Prev, Freeze_Node (Id));
18357 Set_Freeze_Node (Id, Empty);
18358 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18359 end if;
18361 Set_Full_View (Id, Prev);
18362 New_Id := Prev;
18363 end if;
18365 -- Verify that full declaration conforms to partial one
18367 if Is_Incomplete_Or_Private_Type (Prev)
18368 and then Present (Discriminant_Specifications (Prev_Par))
18369 then
18370 if Present (Discriminant_Specifications (N)) then
18371 if Ekind (Prev) = E_Incomplete_Type then
18372 Check_Discriminant_Conformance (N, Prev, Prev);
18373 else
18374 Check_Discriminant_Conformance (N, Prev, Id);
18375 end if;
18377 else
18378 Error_Msg_N
18379 ("missing discriminants in full type declaration", N);
18381 -- To avoid cascaded errors on subsequent use, share the
18382 -- discriminants of the partial view.
18384 Set_Discriminant_Specifications (N,
18385 Discriminant_Specifications (Prev_Par));
18386 end if;
18387 end if;
18389 -- A prior untagged partial view can have an associated class-wide
18390 -- type due to use of the class attribute, and in this case the full
18391 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18392 -- of incomplete tagged declarations, but we check for it.
18394 if Is_Type (Prev)
18395 and then (Is_Tagged_Type (Prev)
18396 or else Present (Class_Wide_Type (Prev)))
18397 then
18398 -- Ada 2012 (AI05-0162): A private type may be the completion of
18399 -- an incomplete type.
18401 if Ada_Version >= Ada_2012
18402 and then Is_Incomplete_Type (Prev)
18403 and then Nkind (N) in N_Private_Type_Declaration
18404 | N_Private_Extension_Declaration
18405 then
18406 -- No need to check private extensions since they are tagged
18408 if Nkind (N) = N_Private_Type_Declaration
18409 and then not Tagged_Present (N)
18410 then
18411 Tag_Mismatch;
18412 end if;
18414 -- The full declaration is either a tagged type (including
18415 -- a synchronized type that implements interfaces) or a
18416 -- type extension, otherwise this is an error.
18418 elsif Nkind (N) in N_Task_Type_Declaration
18419 | N_Protected_Type_Declaration
18420 then
18421 if No (Interface_List (N)) and then not Error_Posted (N) then
18422 Tag_Mismatch;
18423 end if;
18425 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18427 -- Indicate that the previous declaration (tagged incomplete
18428 -- or private declaration) requires the same on the full one.
18430 if not Tagged_Present (Type_Definition (N)) then
18431 Tag_Mismatch;
18432 Set_Is_Tagged_Type (Id);
18433 end if;
18435 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18436 if No (Record_Extension_Part (Type_Definition (N))) then
18437 Error_Msg_NE
18438 ("full declaration of } must be a record extension",
18439 Prev, Id);
18441 -- Set some attributes to produce a usable full view
18443 Set_Is_Tagged_Type (Id);
18444 end if;
18446 else
18447 Tag_Mismatch;
18448 end if;
18449 end if;
18451 if Present (Prev)
18452 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18453 and then Present (Premature_Use (Parent (Prev)))
18454 then
18455 Error_Msg_Sloc := Sloc (N);
18456 Error_Msg_N
18457 ("\full declaration #", Premature_Use (Parent (Prev)));
18458 end if;
18460 return New_Id;
18461 end if;
18462 end Find_Type_Name;
18464 -------------------------
18465 -- Find_Type_Of_Object --
18466 -------------------------
18468 function Find_Type_Of_Object
18469 (Obj_Def : Node_Id;
18470 Related_Nod : Node_Id) return Entity_Id
18472 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18473 P : Node_Id := Parent (Obj_Def);
18474 T : Entity_Id;
18475 Nam : Name_Id;
18477 begin
18478 -- If the parent is a component_definition node we climb to the
18479 -- component_declaration node.
18481 if Nkind (P) = N_Component_Definition then
18482 P := Parent (P);
18483 end if;
18485 -- Case of an anonymous array subtype
18487 if Def_Kind in N_Array_Type_Definition then
18488 T := Empty;
18489 Array_Type_Declaration (T, Obj_Def);
18491 -- Create an explicit subtype whenever possible
18493 elsif Nkind (P) /= N_Component_Declaration
18494 and then Def_Kind = N_Subtype_Indication
18495 then
18496 -- Base name of subtype on object name, which will be unique in
18497 -- the current scope.
18499 -- If this is a duplicate declaration, return base type, to avoid
18500 -- generating duplicate anonymous types.
18502 if Error_Posted (P) then
18503 Analyze (Subtype_Mark (Obj_Def));
18504 return Entity (Subtype_Mark (Obj_Def));
18505 end if;
18507 Nam :=
18508 New_External_Name
18509 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18511 T := Make_Defining_Identifier (Sloc (P), Nam);
18513 -- If In_Spec_Expression, for example within a pre/postcondition,
18514 -- provide enough information for use of the subtype without
18515 -- depending on full analysis and freezing, which will happen when
18516 -- building the corresponding subprogram.
18518 if In_Spec_Expression then
18519 Analyze (Subtype_Mark (Obj_Def));
18521 declare
18522 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18523 New_Def : constant Node_Id := New_Copy_Tree (Obj_Def);
18524 Decl : constant Node_Id :=
18525 Make_Subtype_Declaration (Sloc (P),
18526 Defining_Identifier => T,
18527 Subtype_Indication => New_Def);
18529 begin
18530 Set_Etype (T, Base_T);
18531 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18532 Set_Parent (T, Decl);
18533 Set_Scope (T, Current_Scope);
18535 if Ekind (T) = E_Array_Subtype then
18536 Constrain_Array (T, New_Def, Related_Nod, T, 'P');
18538 elsif Ekind (T) = E_Record_Subtype then
18539 Set_First_Entity (T, First_Entity (Base_T));
18540 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18541 Set_Is_Constrained (T);
18542 end if;
18544 Insert_Before (Related_Nod, Decl);
18545 end;
18547 return T;
18548 end if;
18550 -- When generating code, insert subtype declaration ahead of
18551 -- declaration that generated it.
18553 Insert_Action (Obj_Def,
18554 Make_Subtype_Declaration (Sloc (P),
18555 Defining_Identifier => T,
18556 Subtype_Indication => Relocate_Node (Obj_Def)));
18558 -- This subtype may need freezing, and this will not be done
18559 -- automatically if the object declaration is not in declarative
18560 -- part. Since this is an object declaration, the type cannot always
18561 -- be frozen here. Deferred constants do not freeze their type
18562 -- (which often enough will be private).
18564 if Nkind (P) = N_Object_Declaration
18565 and then Constant_Present (P)
18566 and then No (Expression (P))
18567 then
18568 null;
18570 -- Here we freeze the base type of object type to catch premature use
18571 -- of discriminated private type without a full view.
18573 else
18574 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18575 end if;
18577 -- Ada 2005 AI-406: the object definition in an object declaration
18578 -- can be an access definition.
18580 elsif Def_Kind = N_Access_Definition then
18581 T := Access_Definition (Related_Nod, Obj_Def);
18583 Set_Is_Local_Anonymous_Access
18584 (T, Ada_Version < Ada_2012
18585 or else Nkind (P) /= N_Object_Declaration
18586 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18588 -- Otherwise, the object definition is just a subtype_mark
18590 else
18591 T := Process_Subtype (Obj_Def, Related_Nod);
18592 end if;
18594 return T;
18595 end Find_Type_Of_Object;
18597 --------------------------------
18598 -- Find_Type_Of_Subtype_Indic --
18599 --------------------------------
18601 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18602 Typ : Entity_Id;
18604 begin
18605 -- Case of subtype mark with a constraint
18607 if Nkind (S) = N_Subtype_Indication then
18608 Find_Type (Subtype_Mark (S));
18609 Typ := Entity (Subtype_Mark (S));
18611 if not
18612 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18613 then
18614 Error_Msg_N
18615 ("incorrect constraint for this kind of type", Constraint (S));
18616 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18617 end if;
18619 -- Otherwise we have a subtype mark without a constraint
18621 elsif Error_Posted (S) then
18622 -- Don't rewrite if S is Empty or Error
18623 if S > Empty_Or_Error then
18624 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18625 end if;
18626 return Any_Type;
18628 else
18629 Find_Type (S);
18630 Typ := Entity (S);
18631 end if;
18633 return Typ;
18634 end Find_Type_Of_Subtype_Indic;
18636 -------------------------------------
18637 -- Floating_Point_Type_Declaration --
18638 -------------------------------------
18640 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18641 Digs : constant Node_Id := Digits_Expression (Def);
18642 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18643 Digs_Val : Uint;
18644 Base_Typ : Entity_Id;
18645 Implicit_Base : Entity_Id;
18647 function Can_Derive_From (E : Entity_Id) return Boolean;
18648 -- Find if given digits value, and possibly a specified range, allows
18649 -- derivation from specified type
18651 procedure Convert_Bound (B : Node_Id);
18652 -- If specified, the bounds must be static but may be of different
18653 -- types. They must be converted into machine numbers of the base type,
18654 -- in accordance with RM 4.9(38).
18656 function Find_Base_Type return Entity_Id;
18657 -- Find a predefined base type that Def can derive from, or generate
18658 -- an error and substitute Long_Long_Float if none exists.
18660 ---------------------
18661 -- Can_Derive_From --
18662 ---------------------
18664 function Can_Derive_From (E : Entity_Id) return Boolean is
18665 Spec : constant Entity_Id := Real_Range_Specification (Def);
18667 begin
18668 -- Check specified "digits" constraint
18670 if Digs_Val > Digits_Value (E) then
18671 return False;
18672 end if;
18674 -- Check for matching range, if specified
18676 if Present (Spec) then
18677 if Expr_Value_R (Type_Low_Bound (E)) >
18678 Expr_Value_R (Low_Bound (Spec))
18679 then
18680 return False;
18681 end if;
18683 if Expr_Value_R (Type_High_Bound (E)) <
18684 Expr_Value_R (High_Bound (Spec))
18685 then
18686 return False;
18687 end if;
18688 end if;
18690 return True;
18691 end Can_Derive_From;
18693 -------------------
18694 -- Convert_Bound --
18695 --------------------
18697 procedure Convert_Bound (B : Node_Id) is
18698 begin
18699 -- If the bound is not a literal it can only be static if it is
18700 -- a static constant, possibly of a specified type.
18702 if Is_Entity_Name (B)
18703 and then Ekind (Entity (B)) = E_Constant
18704 then
18705 Rewrite (B, Constant_Value (Entity (B)));
18706 end if;
18708 if Nkind (B) = N_Real_Literal then
18709 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18710 Set_Is_Machine_Number (B);
18711 Set_Etype (B, Base_Typ);
18712 end if;
18713 end Convert_Bound;
18715 --------------------
18716 -- Find_Base_Type --
18717 --------------------
18719 function Find_Base_Type return Entity_Id is
18720 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18722 begin
18723 -- Iterate over the predefined types in order, returning the first
18724 -- one that Def can derive from.
18726 while Present (Choice) loop
18727 if Can_Derive_From (Node (Choice)) then
18728 return Node (Choice);
18729 end if;
18731 Next_Elmt (Choice);
18732 end loop;
18734 -- If we can't derive from any existing type, use Long_Long_Float
18735 -- and give appropriate message explaining the problem.
18737 if Digs_Val > Max_Digs_Val then
18738 -- It might be the case that there is a type with the requested
18739 -- range, just not the combination of digits and range.
18741 Error_Msg_N
18742 ("no predefined type has requested range and precision",
18743 Real_Range_Specification (Def));
18745 else
18746 Error_Msg_N
18747 ("range too large for any predefined type",
18748 Real_Range_Specification (Def));
18749 end if;
18751 return Standard_Long_Long_Float;
18752 end Find_Base_Type;
18754 -- Start of processing for Floating_Point_Type_Declaration
18756 begin
18757 Check_Restriction (No_Floating_Point, Def);
18759 -- Create an implicit base type
18761 Implicit_Base :=
18762 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18764 -- Analyze and verify digits value
18766 Analyze_And_Resolve (Digs, Any_Integer);
18767 Check_Digits_Expression (Digs);
18768 Digs_Val := Expr_Value (Digs);
18770 -- Process possible range spec and find correct type to derive from
18772 Process_Real_Range_Specification (Def);
18774 -- Check that requested number of digits is not too high.
18776 if Digs_Val > Max_Digs_Val then
18778 -- The check for Max_Base_Digits may be somewhat expensive, as it
18779 -- requires reading System, so only do it when necessary.
18781 declare
18782 Max_Base_Digits : constant Uint :=
18783 Expr_Value
18784 (Expression
18785 (Parent (RTE (RE_Max_Base_Digits))));
18787 begin
18788 if Digs_Val > Max_Base_Digits then
18789 Error_Msg_Uint_1 := Max_Base_Digits;
18790 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18792 elsif No (Real_Range_Specification (Def)) then
18793 Error_Msg_Uint_1 := Max_Digs_Val;
18794 Error_Msg_N ("types with more than ^ digits need range spec "
18795 & "(RM 3.5.7(6))", Digs);
18796 end if;
18797 end;
18798 end if;
18800 -- Find a suitable type to derive from or complain and use a substitute
18802 Base_Typ := Find_Base_Type;
18804 -- If there are bounds given in the declaration use them as the bounds
18805 -- of the type, otherwise use the bounds of the predefined base type
18806 -- that was chosen based on the Digits value.
18808 if Present (Real_Range_Specification (Def)) then
18809 Set_Scalar_Range (T, Real_Range_Specification (Def));
18810 Set_Is_Constrained (T);
18812 Convert_Bound (Type_Low_Bound (T));
18813 Convert_Bound (Type_High_Bound (T));
18815 else
18816 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18817 end if;
18819 -- Complete definition of implicit base and declared first subtype. The
18820 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18821 -- are not clobbered when the floating point type acts as a full view of
18822 -- a private type.
18824 Set_Etype (Implicit_Base, Base_Typ);
18825 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18826 Set_Size_Info (Implicit_Base, Base_Typ);
18827 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18828 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18829 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18830 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18832 Mutate_Ekind (T, E_Floating_Point_Subtype);
18833 Set_Etype (T, Implicit_Base);
18834 Set_Size_Info (T, Implicit_Base);
18835 Set_RM_Size (T, RM_Size (Implicit_Base));
18836 Inherit_Rep_Item_Chain (T, Implicit_Base);
18838 if Digs_Val >= Uint_1 then
18839 Set_Digits_Value (T, Digs_Val);
18840 else
18841 pragma Assert (Serious_Errors_Detected > 0); null;
18842 end if;
18843 end Floating_Point_Type_Declaration;
18845 ----------------------------
18846 -- Get_Discriminant_Value --
18847 ----------------------------
18849 -- This is the situation:
18851 -- There is a non-derived type
18853 -- type T0 (Dx, Dy, Dz...)
18855 -- There are zero or more levels of derivation, with each derivation
18856 -- either purely inheriting the discriminants, or defining its own.
18858 -- type Ti is new Ti-1
18859 -- or
18860 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18861 -- or
18862 -- subtype Ti is ...
18864 -- The subtype issue is avoided by the use of Original_Record_Component,
18865 -- and the fact that derived subtypes also derive the constraints.
18867 -- This chain leads back from
18869 -- Typ_For_Constraint
18871 -- Typ_For_Constraint has discriminants, and the value for each
18872 -- discriminant is given by its corresponding Elmt of Constraints.
18874 -- Discriminant is some discriminant in this hierarchy
18876 -- We need to return its value
18878 -- We do this by recursively searching each level, and looking for
18879 -- Discriminant. Once we get to the bottom, we start backing up
18880 -- returning the value for it which may in turn be a discriminant
18881 -- further up, so on the backup we continue the substitution.
18883 function Get_Discriminant_Value
18884 (Discriminant : Entity_Id;
18885 Typ_For_Constraint : Entity_Id;
18886 Constraint : Elist_Id) return Node_Id
18888 function Root_Corresponding_Discriminant
18889 (Discr : Entity_Id) return Entity_Id;
18890 -- Given a discriminant, traverse the chain of inherited discriminants
18891 -- and return the topmost discriminant.
18893 function Search_Derivation_Levels
18894 (Ti : Entity_Id;
18895 Discrim_Values : Elist_Id;
18896 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18897 -- This is the routine that performs the recursive search of levels
18898 -- as described above.
18900 -------------------------------------
18901 -- Root_Corresponding_Discriminant --
18902 -------------------------------------
18904 function Root_Corresponding_Discriminant
18905 (Discr : Entity_Id) return Entity_Id
18907 D : Entity_Id;
18909 begin
18910 D := Discr;
18911 while Present (Corresponding_Discriminant (D)) loop
18912 D := Corresponding_Discriminant (D);
18913 end loop;
18915 return D;
18916 end Root_Corresponding_Discriminant;
18918 ------------------------------
18919 -- Search_Derivation_Levels --
18920 ------------------------------
18922 function Search_Derivation_Levels
18923 (Ti : Entity_Id;
18924 Discrim_Values : Elist_Id;
18925 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18927 Assoc : Elmt_Id;
18928 Disc : Entity_Id;
18929 Result : Node_Or_Entity_Id;
18930 Result_Entity : Node_Id;
18932 begin
18933 -- If inappropriate type, return Error, this happens only in
18934 -- cascaded error situations, and we want to avoid a blow up.
18936 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18937 return Error;
18938 end if;
18940 -- Look deeper if possible. Use Stored_Constraints only for
18941 -- untagged types. For tagged types use the given constraint.
18942 -- This asymmetry needs explanation???
18944 if not Stored_Discrim_Values
18945 and then Present (Stored_Constraint (Ti))
18946 and then not Is_Tagged_Type (Ti)
18947 then
18948 Result :=
18949 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18951 else
18952 declare
18953 Td : Entity_Id := Etype (Ti);
18955 begin
18956 -- If the parent type is private, the full view may include
18957 -- renamed discriminants, and it is those stored values that
18958 -- may be needed (the partial view never has more information
18959 -- than the full view).
18961 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18962 Td := Full_View (Td);
18963 end if;
18965 if Td = Ti then
18966 Result := Discriminant;
18968 else
18969 if Present (Stored_Constraint (Ti)) then
18970 Result :=
18971 Search_Derivation_Levels
18972 (Td, Stored_Constraint (Ti), True);
18973 else
18974 Result :=
18975 Search_Derivation_Levels
18976 (Td, Discrim_Values, Stored_Discrim_Values);
18977 end if;
18978 end if;
18979 end;
18980 end if;
18982 -- Extra underlying places to search, if not found above. For
18983 -- concurrent types, the relevant discriminant appears in the
18984 -- corresponding record. For a type derived from a private type
18985 -- without discriminant, the full view inherits the discriminants
18986 -- of the full view of the parent.
18988 if Result = Discriminant then
18989 if Is_Concurrent_Type (Ti)
18990 and then Present (Corresponding_Record_Type (Ti))
18991 then
18992 Result :=
18993 Search_Derivation_Levels (
18994 Corresponding_Record_Type (Ti),
18995 Discrim_Values,
18996 Stored_Discrim_Values);
18998 elsif Is_Private_Type (Ti)
18999 and then not Has_Discriminants (Ti)
19000 and then Present (Full_View (Ti))
19001 and then Etype (Full_View (Ti)) /= Ti
19002 then
19003 Result :=
19004 Search_Derivation_Levels (
19005 Full_View (Ti),
19006 Discrim_Values,
19007 Stored_Discrim_Values);
19008 end if;
19009 end if;
19011 -- If Result is not a (reference to a) discriminant, return it,
19012 -- otherwise set Result_Entity to the discriminant.
19014 if Nkind (Result) = N_Defining_Identifier then
19015 pragma Assert (Result = Discriminant);
19016 Result_Entity := Result;
19018 else
19019 if not Denotes_Discriminant (Result) then
19020 return Result;
19021 end if;
19023 Result_Entity := Entity (Result);
19024 end if;
19026 -- See if this level of derivation actually has discriminants because
19027 -- tagged derivations can add them, hence the lower levels need not
19028 -- have any.
19030 if not Has_Discriminants (Ti) then
19031 return Result;
19032 end if;
19034 -- Scan Ti's discriminants for Result_Entity, and return its
19035 -- corresponding value, if any.
19037 Result_Entity := Original_Record_Component (Result_Entity);
19039 Assoc := First_Elmt (Discrim_Values);
19041 if Stored_Discrim_Values then
19042 Disc := First_Stored_Discriminant (Ti);
19043 else
19044 Disc := First_Discriminant (Ti);
19045 end if;
19047 while Present (Disc) loop
19049 -- If no further associations return the discriminant, value will
19050 -- be found on the second pass.
19052 if No (Assoc) then
19053 return Result;
19054 end if;
19056 if Original_Record_Component (Disc) = Result_Entity then
19057 return Node (Assoc);
19058 end if;
19060 Next_Elmt (Assoc);
19062 if Stored_Discrim_Values then
19063 Next_Stored_Discriminant (Disc);
19064 else
19065 Next_Discriminant (Disc);
19066 end if;
19067 end loop;
19069 -- Could not find it
19071 return Result;
19072 end Search_Derivation_Levels;
19074 -- Local Variables
19076 Result : Node_Or_Entity_Id;
19078 -- Start of processing for Get_Discriminant_Value
19080 begin
19081 -- ??? This routine is a gigantic mess and will be deleted. For the
19082 -- time being just test for the trivial case before calling recurse.
19084 -- We are now celebrating the 20th anniversary of this comment!
19086 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
19087 declare
19088 D : Entity_Id;
19089 E : Elmt_Id;
19091 begin
19092 D := First_Discriminant (Typ_For_Constraint);
19093 E := First_Elmt (Constraint);
19094 while Present (D) loop
19095 if Chars (D) = Chars (Discriminant) then
19096 return Node (E);
19097 end if;
19099 Next_Discriminant (D);
19100 Next_Elmt (E);
19101 end loop;
19102 end;
19103 end if;
19105 Result := Search_Derivation_Levels
19106 (Typ_For_Constraint, Constraint, False);
19108 -- ??? hack to disappear when this routine is gone
19110 if Nkind (Result) = N_Defining_Identifier then
19111 declare
19112 D : Entity_Id;
19113 E : Elmt_Id;
19115 begin
19116 D := First_Discriminant (Typ_For_Constraint);
19117 E := First_Elmt (Constraint);
19118 while Present (D) loop
19119 if Root_Corresponding_Discriminant (D) = Discriminant then
19120 return Node (E);
19121 end if;
19123 Next_Discriminant (D);
19124 Next_Elmt (E);
19125 end loop;
19126 end;
19127 end if;
19129 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19130 return Result;
19131 end Get_Discriminant_Value;
19133 --------------------------
19134 -- Has_Range_Constraint --
19135 --------------------------
19137 function Has_Range_Constraint (N : Node_Id) return Boolean is
19138 C : constant Node_Id := Constraint (N);
19140 begin
19141 if Nkind (C) = N_Range_Constraint then
19142 return True;
19144 elsif Nkind (C) = N_Digits_Constraint then
19145 return
19146 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19147 or else Present (Range_Constraint (C));
19149 elsif Nkind (C) = N_Delta_Constraint then
19150 return Present (Range_Constraint (C));
19152 else
19153 return False;
19154 end if;
19155 end Has_Range_Constraint;
19157 ------------------------
19158 -- Inherit_Components --
19159 ------------------------
19161 function Inherit_Components
19162 (N : Node_Id;
19163 Parent_Base : Entity_Id;
19164 Derived_Base : Entity_Id;
19165 Is_Tagged : Boolean;
19166 Inherit_Discr : Boolean;
19167 Discs : Elist_Id) return Elist_Id
19169 Assoc_List : constant Elist_Id := New_Elmt_List;
19171 procedure Inherit_Component
19172 (Old_C : Entity_Id;
19173 Plain_Discrim : Boolean := False;
19174 Stored_Discrim : Boolean := False);
19175 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19176 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19177 -- True, Old_C is a stored discriminant. If they are both false then
19178 -- Old_C is a regular component.
19180 -----------------------
19181 -- Inherit_Component --
19182 -----------------------
19184 procedure Inherit_Component
19185 (Old_C : Entity_Id;
19186 Plain_Discrim : Boolean := False;
19187 Stored_Discrim : Boolean := False)
19189 procedure Set_Anonymous_Type (Id : Entity_Id);
19190 -- Id denotes the entity of an access discriminant or anonymous
19191 -- access component. Set the type of Id to either the same type of
19192 -- Old_C or create a new one depending on whether the parent and
19193 -- the child types are in the same scope.
19195 ------------------------
19196 -- Set_Anonymous_Type --
19197 ------------------------
19199 procedure Set_Anonymous_Type (Id : Entity_Id) is
19200 Old_Typ : constant Entity_Id := Etype (Old_C);
19202 begin
19203 if Scope (Parent_Base) = Scope (Derived_Base) then
19204 Set_Etype (Id, Old_Typ);
19206 -- The parent and the derived type are in two different scopes.
19207 -- Reuse the type of the original discriminant / component by
19208 -- copying it in order to preserve all attributes.
19210 else
19211 declare
19212 Typ : constant Entity_Id := New_Copy (Old_Typ);
19214 begin
19215 Set_Etype (Id, Typ);
19217 -- Since we do not generate component declarations for
19218 -- inherited components, associate the itype with the
19219 -- derived type.
19221 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19222 Set_Scope (Typ, Derived_Base);
19223 end;
19224 end if;
19225 end Set_Anonymous_Type;
19227 -- Local variables and constants
19229 New_C : constant Entity_Id := New_Copy (Old_C);
19231 Corr_Discrim : Entity_Id;
19232 Discrim : Entity_Id;
19234 -- Start of processing for Inherit_Component
19236 begin
19237 pragma Assert (not Is_Tagged or not Stored_Discrim);
19239 Set_Parent (New_C, Parent (Old_C));
19241 -- Regular discriminants and components must be inserted in the scope
19242 -- of the Derived_Base. Do it here.
19244 if not Stored_Discrim then
19245 Enter_Name (New_C);
19246 end if;
19248 -- For tagged types the Original_Record_Component must point to
19249 -- whatever this field was pointing to in the parent type. This has
19250 -- already been achieved by the call to New_Copy above.
19252 if not Is_Tagged then
19253 Set_Original_Record_Component (New_C, New_C);
19254 Set_Corresponding_Record_Component (New_C, Old_C);
19255 end if;
19257 -- Set the proper type of an access discriminant
19259 if Ekind (New_C) = E_Discriminant
19260 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19261 then
19262 Set_Anonymous_Type (New_C);
19263 end if;
19265 -- If we have inherited a component then see if its Etype contains
19266 -- references to Parent_Base discriminants. In this case, replace
19267 -- these references with the constraints given in Discs. We do not
19268 -- do this for the partial view of private types because this is
19269 -- not needed (only the components of the full view will be used
19270 -- for code generation) and cause problem. We also avoid this
19271 -- transformation in some error situations.
19273 if Ekind (New_C) = E_Component then
19275 -- Set the proper type of an anonymous access component
19277 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19278 Set_Anonymous_Type (New_C);
19280 elsif (Is_Private_Type (Derived_Base)
19281 and then not Is_Generic_Type (Derived_Base))
19282 or else (Is_Empty_Elmt_List (Discs)
19283 and then not Expander_Active)
19284 then
19285 Set_Etype (New_C, Etype (Old_C));
19287 else
19288 -- The current component introduces a circularity of the
19289 -- following kind:
19291 -- limited with Pack_2;
19292 -- package Pack_1 is
19293 -- type T_1 is tagged record
19294 -- Comp : access Pack_2.T_2;
19295 -- ...
19296 -- end record;
19297 -- end Pack_1;
19299 -- with Pack_1;
19300 -- package Pack_2 is
19301 -- type T_2 is new Pack_1.T_1 with ...;
19302 -- end Pack_2;
19304 Set_Etype
19305 (New_C,
19306 Constrain_Component_Type
19307 (Old_C, Derived_Base, N, Parent_Base, Discs));
19308 end if;
19309 end if;
19311 if Plain_Discrim then
19312 Set_Corresponding_Discriminant (New_C, Old_C);
19313 Build_Discriminal (New_C);
19315 -- If we are explicitly inheriting a stored discriminant it will be
19316 -- completely hidden.
19318 elsif Stored_Discrim then
19319 Set_Corresponding_Discriminant (New_C, Empty);
19320 Set_Discriminal (New_C, Empty);
19321 Set_Is_Completely_Hidden (New_C);
19323 -- Set the Original_Record_Component of each discriminant in the
19324 -- derived base to point to the corresponding stored that we just
19325 -- created.
19327 Discrim := First_Discriminant (Derived_Base);
19328 while Present (Discrim) loop
19329 Corr_Discrim := Corresponding_Discriminant (Discrim);
19331 -- Corr_Discrim could be missing in an error situation
19333 if Present (Corr_Discrim)
19334 and then Original_Record_Component (Corr_Discrim) = Old_C
19335 then
19336 Set_Original_Record_Component (Discrim, New_C);
19337 Set_Corresponding_Record_Component (Discrim, Empty);
19338 end if;
19340 Next_Discriminant (Discrim);
19341 end loop;
19343 Append_Entity (New_C, Derived_Base);
19344 end if;
19346 if not Is_Tagged then
19347 Append_Elmt (Old_C, Assoc_List);
19348 Append_Elmt (New_C, Assoc_List);
19349 end if;
19350 end Inherit_Component;
19352 -- Variables local to Inherit_Component
19354 Loc : constant Source_Ptr := Sloc (N);
19356 Parent_Discrim : Entity_Id;
19357 Stored_Discrim : Entity_Id;
19358 D : Entity_Id;
19359 Component : Entity_Id;
19361 -- Start of processing for Inherit_Components
19363 begin
19364 if not Is_Tagged then
19365 Append_Elmt (Parent_Base, Assoc_List);
19366 Append_Elmt (Derived_Base, Assoc_List);
19367 end if;
19369 -- Inherit parent discriminants if needed
19371 if Inherit_Discr then
19372 Parent_Discrim := First_Discriminant (Parent_Base);
19373 while Present (Parent_Discrim) loop
19374 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19375 Next_Discriminant (Parent_Discrim);
19376 end loop;
19377 end if;
19379 -- Create explicit stored discrims for untagged types when necessary
19381 if not Has_Unknown_Discriminants (Derived_Base)
19382 and then Has_Discriminants (Parent_Base)
19383 and then not Is_Tagged
19384 and then
19385 (not Inherit_Discr
19386 or else First_Discriminant (Parent_Base) /=
19387 First_Stored_Discriminant (Parent_Base))
19388 then
19389 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19390 while Present (Stored_Discrim) loop
19391 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19392 Next_Stored_Discriminant (Stored_Discrim);
19393 end loop;
19394 end if;
19396 -- See if we can apply the second transformation for derived types, as
19397 -- explained in point 6. in the comments above Build_Derived_Record_Type
19398 -- This is achieved by appending Derived_Base discriminants into Discs,
19399 -- which has the side effect of returning a non empty Discs list to the
19400 -- caller of Inherit_Components, which is what we want. This must be
19401 -- done for private derived types if there are explicit stored
19402 -- discriminants, to ensure that we can retrieve the values of the
19403 -- constraints provided in the ancestors.
19405 if Inherit_Discr
19406 and then Is_Empty_Elmt_List (Discs)
19407 and then Present (First_Discriminant (Derived_Base))
19408 and then
19409 (not Is_Private_Type (Derived_Base)
19410 or else Is_Completely_Hidden
19411 (First_Stored_Discriminant (Derived_Base))
19412 or else Is_Generic_Type (Derived_Base))
19413 then
19414 D := First_Discriminant (Derived_Base);
19415 while Present (D) loop
19416 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19417 Next_Discriminant (D);
19418 end loop;
19419 end if;
19421 -- Finally, inherit non-discriminant components unless they are not
19422 -- visible because defined or inherited from the full view of the
19423 -- parent. Don't inherit the _parent field of the parent type.
19425 Component := First_Entity (Parent_Base);
19426 while Present (Component) loop
19428 -- Ada 2005 (AI-251): Do not inherit components associated with
19429 -- secondary tags of the parent.
19431 if Ekind (Component) = E_Component
19432 and then Present (Related_Type (Component))
19433 then
19434 null;
19436 elsif Ekind (Component) /= E_Component
19437 or else Chars (Component) = Name_uParent
19438 then
19439 null;
19441 -- If the derived type is within the parent type's declarative
19442 -- region, then the components can still be inherited even though
19443 -- they aren't visible at this point. This can occur for cases
19444 -- such as within public child units where the components must
19445 -- become visible upon entering the child unit's private part.
19447 elsif not Is_Visible_Component (Component)
19448 and then not In_Open_Scopes (Scope (Parent_Base))
19449 then
19450 null;
19452 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19453 then
19454 null;
19456 else
19457 Inherit_Component (Component);
19458 end if;
19460 Next_Entity (Component);
19461 end loop;
19463 -- For tagged derived types, inherited discriminants cannot be used in
19464 -- component declarations of the record extension part. To achieve this
19465 -- we mark the inherited discriminants as not visible.
19467 if Is_Tagged and then Inherit_Discr then
19468 D := First_Discriminant (Derived_Base);
19469 while Present (D) loop
19470 Set_Is_Immediately_Visible (D, False);
19471 Next_Discriminant (D);
19472 end loop;
19473 end if;
19475 return Assoc_List;
19476 end Inherit_Components;
19478 ----------------------
19479 -- Is_EVF_Procedure --
19480 ----------------------
19482 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19483 Formal : Entity_Id;
19485 begin
19486 -- Examine the formals of an Extensions_Visible False procedure looking
19487 -- for a controlling OUT parameter.
19489 if Ekind (Subp) = E_Procedure
19490 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19491 then
19492 Formal := First_Formal (Subp);
19493 while Present (Formal) loop
19494 if Ekind (Formal) = E_Out_Parameter
19495 and then Is_Controlling_Formal (Formal)
19496 then
19497 return True;
19498 end if;
19500 Next_Formal (Formal);
19501 end loop;
19502 end if;
19504 return False;
19505 end Is_EVF_Procedure;
19507 --------------------------
19508 -- Is_Private_Primitive --
19509 --------------------------
19511 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19512 Prim_Scope : constant Entity_Id := Scope (Prim);
19513 Priv_Entity : Entity_Id;
19514 begin
19515 if Is_Package_Or_Generic_Package (Prim_Scope) then
19516 Priv_Entity := First_Private_Entity (Prim_Scope);
19518 while Present (Priv_Entity) loop
19519 if Priv_Entity = Prim then
19520 return True;
19521 end if;
19523 Next_Entity (Priv_Entity);
19524 end loop;
19525 end if;
19527 return False;
19528 end Is_Private_Primitive;
19530 ------------------------------
19531 -- Is_Valid_Constraint_Kind --
19532 ------------------------------
19534 function Is_Valid_Constraint_Kind
19535 (T_Kind : Type_Kind;
19536 Constraint_Kind : Node_Kind) return Boolean
19538 begin
19539 case T_Kind is
19540 when Enumeration_Kind
19541 | Integer_Kind
19543 return Constraint_Kind = N_Range_Constraint;
19545 when Decimal_Fixed_Point_Kind =>
19546 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19548 when Ordinary_Fixed_Point_Kind =>
19549 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19551 when Float_Kind =>
19552 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19554 when Access_Kind
19555 | Array_Kind
19556 | Class_Wide_Kind
19557 | Concurrent_Kind
19558 | Private_Kind
19559 | E_Incomplete_Type
19560 | E_Record_Subtype
19561 | E_Record_Type
19563 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19565 when others =>
19566 return True; -- Error will be detected later
19567 end case;
19568 end Is_Valid_Constraint_Kind;
19570 --------------------------
19571 -- Is_Visible_Component --
19572 --------------------------
19574 function Is_Visible_Component
19575 (C : Entity_Id;
19576 N : Node_Id := Empty) return Boolean
19578 Original_Comp : Entity_Id := Empty;
19579 Original_Type : Entity_Id;
19580 Type_Scope : Entity_Id;
19582 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19583 -- Check whether parent type of inherited component is declared locally,
19584 -- possibly within a nested package or instance. The current scope is
19585 -- the derived record itself.
19587 -------------------
19588 -- Is_Local_Type --
19589 -------------------
19591 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19592 begin
19593 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19594 end Is_Local_Type;
19596 -- Start of processing for Is_Visible_Component
19598 begin
19599 if Ekind (C) in E_Component | E_Discriminant then
19600 Original_Comp := Original_Record_Component (C);
19601 end if;
19603 if No (Original_Comp) then
19605 -- Premature usage, or previous error
19607 return False;
19609 else
19610 Original_Type := Scope (Original_Comp);
19611 Type_Scope := Scope (Base_Type (Scope (C)));
19612 end if;
19614 -- This test only concerns tagged types
19616 if not Is_Tagged_Type (Original_Type) then
19618 -- Check if this is a renamed discriminant (hidden either by the
19619 -- derived type or by some ancestor), unless we are analyzing code
19620 -- generated by the expander since it may reference such components
19621 -- (for example see the expansion of Deep_Adjust).
19623 if Ekind (C) = E_Discriminant and then Present (N) then
19624 return
19625 not Comes_From_Source (N)
19626 or else not Is_Completely_Hidden (C);
19627 else
19628 return True;
19629 end if;
19631 -- If it is _Parent or _Tag, there is no visibility issue
19633 elsif not Comes_From_Source (Original_Comp) then
19634 return True;
19636 -- Discriminants are visible unless the (private) type has unknown
19637 -- discriminants. If the discriminant reference is inserted for a
19638 -- discriminant check on a full view it is also visible.
19640 elsif Ekind (Original_Comp) = E_Discriminant
19641 and then
19642 (not Has_Unknown_Discriminants (Original_Type)
19643 or else (Present (N)
19644 and then Nkind (N) = N_Selected_Component
19645 and then Nkind (Prefix (N)) = N_Type_Conversion
19646 and then not Comes_From_Source (Prefix (N))))
19647 then
19648 return True;
19650 -- If the component has been declared in an ancestor which is currently
19651 -- a private type, then it is not visible. The same applies if the
19652 -- component's containing type is not in an open scope and the original
19653 -- component's enclosing type is a visible full view of a private type
19654 -- (which can occur in cases where an attempt is being made to reference
19655 -- a component in a sibling package that is inherited from a visible
19656 -- component of a type in an ancestor package; the component in the
19657 -- sibling package should not be visible even though the component it
19658 -- inherited from is visible), but instance bodies are not subject to
19659 -- this second case since they have the Has_Private_View mechanism to
19660 -- ensure proper visibility. This does not apply however in the case
19661 -- where the scope of the type is a private child unit, or when the
19662 -- parent comes from a local package in which the ancestor is currently
19663 -- visible. The latter suppression of visibility is needed for cases
19664 -- that are tested in B730006.
19666 elsif Is_Private_Type (Original_Type)
19667 or else
19668 (not Is_Private_Descendant (Type_Scope)
19669 and then not In_Open_Scopes (Type_Scope)
19670 and then Has_Private_Declaration (Original_Type)
19671 and then not In_Instance_Body)
19672 then
19673 -- If the type derives from an entity in a formal package, there
19674 -- are no additional visible components.
19676 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19677 N_Formal_Package_Declaration
19678 then
19679 return False;
19681 -- if we are not in the private part of the current package, there
19682 -- are no additional visible components.
19684 elsif Ekind (Scope (Current_Scope)) = E_Package
19685 and then not In_Private_Part (Scope (Current_Scope))
19686 then
19687 return False;
19688 else
19689 return
19690 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19691 and then In_Open_Scopes (Scope (Original_Type))
19692 and then Is_Local_Type (Type_Scope);
19693 end if;
19695 -- There is another weird way in which a component may be invisible when
19696 -- the private and the full view are not derived from the same ancestor.
19697 -- Here is an example :
19699 -- type A1 is tagged record F1 : integer; end record;
19700 -- type A2 is new A1 with record F2 : integer; end record;
19701 -- type T is new A1 with private;
19702 -- private
19703 -- type T is new A2 with null record;
19705 -- In this case, the full view of T inherits F1 and F2 but the private
19706 -- view inherits only F1
19708 else
19709 declare
19710 Ancestor : Entity_Id := Scope (C);
19712 begin
19713 loop
19714 if Ancestor = Original_Type then
19715 return True;
19717 -- The ancestor may have a partial view of the original type,
19718 -- but if the full view is in scope, as in a child body, the
19719 -- component is visible.
19721 elsif In_Private_Part (Scope (Original_Type))
19722 and then Full_View (Ancestor) = Original_Type
19723 then
19724 return True;
19726 elsif Ancestor = Etype (Ancestor) then
19728 -- No further ancestors to examine
19730 return False;
19731 end if;
19733 Ancestor := Etype (Ancestor);
19734 end loop;
19735 end;
19736 end if;
19737 end Is_Visible_Component;
19739 --------------------------
19740 -- Make_Class_Wide_Type --
19741 --------------------------
19743 procedure Make_Class_Wide_Type (T : Entity_Id) is
19744 CW_Type : Entity_Id;
19745 CW_Name : Name_Id;
19746 Next_E : Entity_Id;
19747 Prev_E : Entity_Id;
19749 begin
19750 if Present (Class_Wide_Type (T)) then
19752 -- The class-wide type is a partially decorated entity created for a
19753 -- unanalyzed tagged type referenced through a limited with clause.
19754 -- When the tagged type is analyzed, its class-wide type needs to be
19755 -- redecorated. Note that we reuse the entity created by Decorate_
19756 -- Tagged_Type in order to preserve all links.
19758 if Materialize_Entity (Class_Wide_Type (T)) then
19759 CW_Type := Class_Wide_Type (T);
19760 Set_Materialize_Entity (CW_Type, False);
19762 -- The class wide type can have been defined by the partial view, in
19763 -- which case everything is already done.
19765 else
19766 return;
19767 end if;
19769 -- Default case, we need to create a new class-wide type
19771 else
19772 CW_Type :=
19773 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19774 end if;
19776 -- Inherit root type characteristics
19778 CW_Name := Chars (CW_Type);
19779 Next_E := Next_Entity (CW_Type);
19780 Prev_E := Prev_Entity (CW_Type);
19781 Copy_Node (T, CW_Type);
19782 Set_Comes_From_Source (CW_Type, False);
19783 Set_Chars (CW_Type, CW_Name);
19784 Set_Parent (CW_Type, Parent (T));
19785 Set_Prev_Entity (CW_Type, Prev_E);
19786 Set_Next_Entity (CW_Type, Next_E);
19788 -- Ensure we have a new freeze node for the class-wide type. The partial
19789 -- view may have freeze action of its own, requiring a proper freeze
19790 -- node, and the same freeze node cannot be shared between the two
19791 -- types.
19793 Set_Has_Delayed_Freeze (CW_Type);
19794 Set_Freeze_Node (CW_Type, Empty);
19796 -- Customize the class-wide type: It has no prim. op., it cannot be
19797 -- abstract, its Etype points back to the specific root type, and it
19798 -- cannot have any invariants.
19800 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19801 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19803 elsif Ekind (CW_Type) in Concurrent_Kind then
19804 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19805 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19807 if Ekind (CW_Type) in Task_Kind then
19808 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19809 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19810 end if;
19812 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19813 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19814 end if;
19816 elsif Ekind (CW_Type) = E_Record_Type then
19817 Reinit_Field_To_Zero (CW_Type, F_Corresponding_Concurrent_Type);
19818 end if;
19820 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19821 Set_Is_Tagged_Type (CW_Type, True);
19822 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19823 Set_Is_Abstract_Type (CW_Type, False);
19824 Set_Is_Constrained (CW_Type, False);
19825 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19826 Set_Default_SSO (CW_Type);
19827 Set_Has_Inheritable_Invariants (CW_Type, False);
19828 Set_Has_Inherited_Invariants (CW_Type, False);
19829 Set_Has_Own_Invariants (CW_Type, False);
19831 if Ekind (T) = E_Class_Wide_Subtype then
19832 Set_Etype (CW_Type, Etype (Base_Type (T)));
19833 else
19834 Set_Etype (CW_Type, T);
19835 end if;
19837 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19839 -- If this is the class_wide type of a constrained subtype, it does
19840 -- not have discriminants.
19842 Set_Has_Discriminants (CW_Type,
19843 Has_Discriminants (T) and then not Is_Constrained (T));
19845 Set_Has_Unknown_Discriminants (CW_Type, True);
19846 Set_Class_Wide_Type (T, CW_Type);
19847 Set_Equivalent_Type (CW_Type, Empty);
19849 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19851 Set_Class_Wide_Type (CW_Type, CW_Type);
19852 end Make_Class_Wide_Type;
19854 ----------------
19855 -- Make_Index --
19856 ----------------
19858 procedure Make_Index
19859 (N : Node_Id;
19860 Related_Nod : Node_Id;
19861 Related_Id : Entity_Id := Empty;
19862 Suffix_Index : Pos := 1)
19864 R : Node_Id;
19865 T : Entity_Id;
19866 Def_Id : Entity_Id := Empty;
19867 Found : Boolean := False;
19869 begin
19870 -- For a discrete range used in a constrained array definition and
19871 -- defined by a range, an implicit conversion to the predefined type
19872 -- INTEGER is assumed if each bound is either a numeric literal, a named
19873 -- number, or an attribute, and the type of both bounds (prior to the
19874 -- implicit conversion) is the type universal_integer. Otherwise, both
19875 -- bounds must be of the same discrete type, other than universal
19876 -- integer; this type must be determinable independently of the
19877 -- context, but using the fact that the type must be discrete and that
19878 -- both bounds must have the same type.
19880 -- Character literals also have a universal type in the absence of
19881 -- of additional context, and are resolved to Standard_Character.
19883 if Nkind (N) = N_Range then
19885 -- The index is given by a range constraint. The bounds are known
19886 -- to be of a consistent type.
19888 if not Is_Overloaded (N) then
19889 T := Etype (N);
19891 -- For universal bounds, choose the specific predefined type
19893 if T = Universal_Integer then
19894 T := Standard_Integer;
19896 elsif T = Any_Character then
19897 Ambiguous_Character (Low_Bound (N));
19899 T := Standard_Character;
19900 end if;
19902 -- The node may be overloaded because some user-defined operators
19903 -- are available, but if a universal interpretation exists it is
19904 -- also the selected one.
19906 elsif Universal_Interpretation (N) = Universal_Integer then
19907 T := Standard_Integer;
19909 else
19910 T := Any_Type;
19912 declare
19913 Ind : Interp_Index;
19914 It : Interp;
19916 begin
19917 Get_First_Interp (N, Ind, It);
19918 while Present (It.Typ) loop
19919 if Is_Discrete_Type (It.Typ) then
19921 if Found
19922 and then not Covers (It.Typ, T)
19923 and then not Covers (T, It.Typ)
19924 then
19925 Error_Msg_N ("ambiguous bounds in discrete range", N);
19926 exit;
19927 else
19928 T := It.Typ;
19929 Found := True;
19930 end if;
19931 end if;
19933 Get_Next_Interp (Ind, It);
19934 end loop;
19936 if T = Any_Type then
19937 Error_Msg_N ("discrete type required for range", N);
19938 Set_Etype (N, Any_Type);
19939 return;
19941 elsif T = Universal_Integer then
19942 T := Standard_Integer;
19943 end if;
19944 end;
19945 end if;
19947 if not Is_Discrete_Type (T) then
19948 Error_Msg_N ("discrete type required for range", N);
19949 Set_Etype (N, Any_Type);
19950 return;
19951 end if;
19953 -- If the range bounds are "T'First .. T'Last" where T is a name of a
19954 -- discrete type, then use T as the type of the index.
19956 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19957 and then Attribute_Name (Low_Bound (N)) = Name_First
19958 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19959 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19961 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19962 and then Attribute_Name (High_Bound (N)) = Name_Last
19963 and then Is_Entity_Name (Prefix (High_Bound (N)))
19964 and then Entity (Prefix (High_Bound (N))) = Def_Id
19965 then
19966 Def_Id := Entity (Prefix (Low_Bound (N)));
19967 end if;
19969 R := N;
19970 Process_Range_Expr_In_Decl (R, T);
19972 elsif Nkind (N) = N_Subtype_Indication then
19974 -- The index is given by a subtype with a range constraint
19976 T := Base_Type (Entity (Subtype_Mark (N)));
19978 if not Is_Discrete_Type (T) then
19979 Error_Msg_N ("discrete type required for range", N);
19980 Set_Etype (N, Any_Type);
19981 return;
19982 end if;
19984 R := Range_Expression (Constraint (N));
19986 Resolve (R, T);
19987 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19989 elsif Nkind (N) = N_Attribute_Reference then
19991 -- Catch beginner's error (use of attribute other than 'Range)
19993 if Attribute_Name (N) /= Name_Range then
19994 Error_Msg_N ("expect attribute ''Range", N);
19995 Set_Etype (N, Any_Type);
19996 return;
19997 end if;
19999 -- If the node denotes the range of a type mark, that is also the
20000 -- resulting type, and we do not need to create an Itype for it.
20002 if Is_Entity_Name (Prefix (N))
20003 and then Comes_From_Source (N)
20004 and then Is_Discrete_Type (Entity (Prefix (N)))
20005 then
20006 Def_Id := Entity (Prefix (N));
20007 end if;
20009 Analyze_And_Resolve (N);
20010 T := Etype (N);
20011 R := N;
20013 -- If none of the above, must be a subtype. We convert this to a
20014 -- range attribute reference because in the case of declared first
20015 -- named subtypes, the types in the range reference can be different
20016 -- from the type of the entity. A range attribute normalizes the
20017 -- reference and obtains the correct types for the bounds.
20019 -- This transformation is in the nature of an expansion, is only
20020 -- done if expansion is active. In particular, it is not done on
20021 -- formal generic types, because we need to retain the name of the
20022 -- original index for instantiation purposes.
20024 else
20025 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
20026 Error_Msg_N ("invalid subtype mark in discrete range", N);
20027 Set_Etype (N, Any_Integer);
20028 return;
20030 else
20031 -- The type mark may be that of an incomplete type. It is only
20032 -- now that we can get the full view, previous analysis does
20033 -- not look specifically for a type mark.
20035 Set_Entity (N, Get_Full_View (Entity (N)));
20036 Set_Etype (N, Entity (N));
20037 Def_Id := Entity (N);
20039 if not Is_Discrete_Type (Def_Id) then
20040 Error_Msg_N ("discrete type required for index", N);
20041 Set_Etype (N, Any_Type);
20042 return;
20043 end if;
20044 end if;
20046 if Expander_Active then
20047 Rewrite (N,
20048 Make_Attribute_Reference (Sloc (N),
20049 Attribute_Name => Name_Range,
20050 Prefix => Relocate_Node (N)));
20052 -- The original was a subtype mark that does not freeze. This
20053 -- means that the rewritten version must not freeze either.
20055 Set_Must_Not_Freeze (N);
20056 Set_Must_Not_Freeze (Prefix (N));
20057 Analyze_And_Resolve (N);
20058 T := Etype (N);
20059 R := N;
20061 -- If expander is inactive, type is legal, nothing else to construct
20063 else
20064 return;
20065 end if;
20066 end if;
20068 if not Is_Discrete_Type (T) then
20069 Error_Msg_N ("discrete type required for range", N);
20070 Set_Etype (N, Any_Type);
20071 return;
20073 elsif T = Any_Type then
20074 Set_Etype (N, Any_Type);
20075 return;
20076 end if;
20078 -- We will now create the appropriate Itype to describe the range, but
20079 -- first a check. If we originally had a subtype, then we just label
20080 -- the range with this subtype. Not only is there no need to construct
20081 -- a new subtype, but it is wrong to do so for two reasons:
20083 -- 1. A legality concern, if we have a subtype, it must not freeze,
20084 -- and the Itype would cause freezing incorrectly
20086 -- 2. An efficiency concern, if we created an Itype, it would not be
20087 -- recognized as the same type for the purposes of eliminating
20088 -- checks in some circumstances.
20090 -- We signal this case by setting the subtype entity in Def_Id
20092 if No (Def_Id) then
20093 Def_Id :=
20094 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
20095 Set_Etype (Def_Id, Base_Type (T));
20097 if Is_Signed_Integer_Type (T) then
20098 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20100 elsif Is_Modular_Integer_Type (T) then
20101 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20103 else
20104 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20105 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20106 Set_First_Literal (Def_Id, First_Literal (T));
20107 end if;
20109 Set_Size_Info (Def_Id, (T));
20110 Set_RM_Size (Def_Id, RM_Size (T));
20111 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20113 Set_Scalar_Range (Def_Id, R);
20114 Conditional_Delay (Def_Id, T);
20116 -- In the subtype indication case inherit properties of the parent
20118 if Nkind (N) = N_Subtype_Indication then
20120 -- It is enough to inherit predicate flags and not the predicate
20121 -- functions, because predicates on an index type are illegal
20122 -- anyway and the flags are enough to detect them.
20124 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20126 -- If the immediate parent of the new subtype is nonstatic, then
20127 -- the subtype we create is nonstatic as well, even if its bounds
20128 -- are static.
20130 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20131 Set_Is_Non_Static_Subtype (Def_Id);
20132 end if;
20133 end if;
20135 Set_Parent (Def_Id, N);
20136 end if;
20138 -- Final step is to label the index with this constructed type
20140 Set_Etype (N, Def_Id);
20141 end Make_Index;
20143 ------------------------------
20144 -- Modular_Type_Declaration --
20145 ------------------------------
20147 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20148 Mod_Expr : constant Node_Id := Expression (Def);
20149 M_Val : Uint;
20151 procedure Set_Modular_Size (Bits : Int);
20152 -- Sets RM_Size to Bits, and Esize to normal word size above this
20154 ----------------------
20155 -- Set_Modular_Size --
20156 ----------------------
20158 procedure Set_Modular_Size (Bits : Int) is
20159 Siz : Int;
20161 begin
20162 Set_RM_Size (T, UI_From_Int (Bits));
20164 if Bits < System_Max_Binary_Modulus_Power then
20165 Siz := 8;
20167 while Siz < 128 loop
20168 exit when Bits <= Siz;
20169 Siz := Siz * 2;
20170 end loop;
20172 Set_Esize (T, UI_From_Int (Siz));
20174 else
20175 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20176 end if;
20178 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20179 Set_Is_Known_Valid (T);
20180 end if;
20181 end Set_Modular_Size;
20183 -- Start of processing for Modular_Type_Declaration
20185 begin
20186 -- If the mod expression is (exactly) 2 * literal, where literal is
20187 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20189 if Warn_On_Suspicious_Modulus_Value
20190 and then Nkind (Mod_Expr) = N_Op_Multiply
20191 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20192 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20193 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20194 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20195 then
20196 Error_Msg_N
20197 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20198 end if;
20200 -- Proceed with analysis of mod expression
20202 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20204 Set_Etype (T, T);
20205 Mutate_Ekind (T, E_Modular_Integer_Type);
20206 Reinit_Alignment (T);
20207 Set_Is_Constrained (T);
20209 if not Is_OK_Static_Expression (Mod_Expr) then
20210 Flag_Non_Static_Expr
20211 ("non-static expression used for modular type bound!", Mod_Expr);
20212 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20213 else
20214 M_Val := Expr_Value (Mod_Expr);
20215 end if;
20217 if M_Val < 1 then
20218 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20219 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20220 end if;
20222 if M_Val > 2 ** Standard_Long_Integer_Size then
20223 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20224 end if;
20226 Set_Modulus (T, M_Val);
20228 -- Create bounds for the modular type based on the modulus given in
20229 -- the type declaration and then analyze and resolve those bounds.
20231 Set_Scalar_Range (T,
20232 Make_Range (Sloc (Mod_Expr),
20233 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20234 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20236 -- Properly analyze the literals for the range. We do this manually
20237 -- because we can't go calling Resolve, since we are resolving these
20238 -- bounds with the type, and this type is certainly not complete yet.
20240 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20241 Set_Etype (High_Bound (Scalar_Range (T)), T);
20242 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20243 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20245 -- Loop through powers of two to find number of bits required
20247 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20249 -- Binary case
20251 if M_Val = 2 ** Bits then
20252 Set_Modular_Size (Bits);
20253 return;
20255 -- Nonbinary case
20257 elsif M_Val < 2 ** Bits then
20258 Set_Non_Binary_Modulus (T);
20260 if Bits > System_Max_Nonbinary_Modulus_Power then
20261 Error_Msg_Uint_1 :=
20262 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20263 Error_Msg_F
20264 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20265 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20266 return;
20268 else
20269 -- In the nonbinary case, set size as per RM 13.3(55)
20271 Set_Modular_Size (Bits);
20272 return;
20273 end if;
20274 end if;
20276 end loop;
20278 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20279 -- so we just signal an error and set the maximum size.
20281 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20282 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20284 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20285 Reinit_Alignment (T);
20287 end Modular_Type_Declaration;
20289 --------------------------
20290 -- New_Concatenation_Op --
20291 --------------------------
20293 procedure New_Concatenation_Op (Typ : Entity_Id) is
20294 Loc : constant Source_Ptr := Sloc (Typ);
20295 Op : Entity_Id;
20297 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20298 -- Create abbreviated declaration for the formal of a predefined
20299 -- Operator 'Op' of type 'Typ'
20301 --------------------
20302 -- Make_Op_Formal --
20303 --------------------
20305 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20306 Formal : Entity_Id;
20307 begin
20308 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20309 Set_Etype (Formal, Typ);
20310 Set_Mechanism (Formal, Default_Mechanism);
20311 return Formal;
20312 end Make_Op_Formal;
20314 -- Start of processing for New_Concatenation_Op
20316 begin
20317 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20319 Mutate_Ekind (Op, E_Operator);
20320 Set_Is_Not_Self_Hidden (Op);
20321 Set_Scope (Op, Current_Scope);
20322 Set_Etype (Op, Typ);
20323 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20324 Set_Is_Immediately_Visible (Op);
20325 Set_Is_Intrinsic_Subprogram (Op);
20326 Set_Has_Completion (Op);
20327 Append_Entity (Op, Current_Scope);
20329 Set_Name_Entity_Id (Name_Op_Concat, Op);
20331 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20332 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20333 end New_Concatenation_Op;
20335 -------------------------
20336 -- OK_For_Limited_Init --
20337 -------------------------
20339 -- ???Check all calls of this, and compare the conditions under which it's
20340 -- called.
20342 function OK_For_Limited_Init
20343 (Typ : Entity_Id;
20344 Exp : Node_Id) return Boolean
20346 begin
20347 return Is_CPP_Constructor_Call (Exp)
20348 or else (Ada_Version >= Ada_2005
20349 and then not Debug_Flag_Dot_L
20350 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20351 end OK_For_Limited_Init;
20353 -------------------------------
20354 -- OK_For_Limited_Init_In_05 --
20355 -------------------------------
20357 function OK_For_Limited_Init_In_05
20358 (Typ : Entity_Id;
20359 Exp : Node_Id) return Boolean
20361 begin
20362 -- An object of a limited interface type can be initialized with any
20363 -- expression of a nonlimited descendant type. However this does not
20364 -- apply if this is a view conversion of some other expression. This
20365 -- is checked below.
20367 if Is_Class_Wide_Type (Typ)
20368 and then Is_Limited_Interface (Typ)
20369 and then not Is_Limited_Type (Etype (Exp))
20370 and then Nkind (Exp) /= N_Type_Conversion
20371 then
20372 return True;
20373 end if;
20375 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20376 -- case of limited aggregates (including extension aggregates), and
20377 -- function calls. The function call may have been given in prefixed
20378 -- notation, in which case the original node is an indexed component.
20379 -- If the function is parameterless, the original node was an explicit
20380 -- dereference. The function may also be parameterless, in which case
20381 -- the source node is just an identifier.
20383 -- A branch of a conditional expression may have been removed if the
20384 -- condition is statically known. This happens during expansion, and
20385 -- thus will not happen if previous errors were encountered. The check
20386 -- will have been performed on the chosen branch, which replaces the
20387 -- original conditional expression.
20389 if No (Exp) then
20390 return True;
20391 end if;
20393 case Nkind (Original_Node (Exp)) is
20394 when N_Aggregate
20395 | N_Delta_Aggregate
20396 | N_Extension_Aggregate
20397 | N_Function_Call
20398 | N_Op
20400 return True;
20402 when N_Identifier =>
20403 return Present (Entity (Original_Node (Exp)))
20404 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20406 when N_Qualified_Expression =>
20407 return
20408 OK_For_Limited_Init_In_05
20409 (Typ, Expression (Original_Node (Exp)));
20411 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20412 -- with a function call, the expander has rewritten the call into an
20413 -- N_Type_Conversion node to force displacement of the pointer to
20414 -- reference the component containing the secondary dispatch table.
20415 -- Otherwise a type conversion is not a legal context.
20416 -- A return statement for a build-in-place function returning a
20417 -- synchronized type also introduces an unchecked conversion.
20419 when N_Type_Conversion
20420 | N_Unchecked_Type_Conversion
20422 return not Comes_From_Source (Exp)
20423 and then
20424 -- If the conversion has been rewritten, check Original_Node;
20425 -- otherwise, check the expression of the compiler-generated
20426 -- conversion (which is a conversion that we want to ignore
20427 -- for purposes of the limited-initialization restrictions).
20429 (if Is_Rewrite_Substitution (Exp)
20430 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20431 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20433 when N_Explicit_Dereference
20434 | N_Indexed_Component
20435 | N_Selected_Component
20437 return Nkind (Exp) = N_Function_Call;
20439 -- A use of 'Input is a function call, hence allowed. Normally the
20440 -- attribute will be changed to a call, but the attribute by itself
20441 -- can occur with -gnatc.
20443 when N_Attribute_Reference =>
20444 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20446 -- "return raise ..." is OK
20448 when N_Raise_Expression =>
20449 return True;
20451 -- For a case expression, all dependent expressions must be legal
20453 when N_Case_Expression =>
20454 declare
20455 Alt : Node_Id;
20457 begin
20458 Alt := First (Alternatives (Original_Node (Exp)));
20459 while Present (Alt) loop
20460 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20461 return False;
20462 end if;
20464 Next (Alt);
20465 end loop;
20467 return True;
20468 end;
20470 -- For an if expression, all dependent expressions must be legal
20472 when N_If_Expression =>
20473 declare
20474 Then_Expr : constant Node_Id :=
20475 Next (First (Expressions (Original_Node (Exp))));
20476 Else_Expr : constant Node_Id := Next (Then_Expr);
20477 begin
20478 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20479 and then
20480 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20481 end;
20483 when others =>
20484 return False;
20485 end case;
20486 end OK_For_Limited_Init_In_05;
20488 -------------------------------------------
20489 -- Ordinary_Fixed_Point_Type_Declaration --
20490 -------------------------------------------
20492 procedure Ordinary_Fixed_Point_Type_Declaration
20493 (T : Entity_Id;
20494 Def : Node_Id)
20496 Loc : constant Source_Ptr := Sloc (Def);
20497 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20498 RRS : constant Node_Id := Real_Range_Specification (Def);
20499 Implicit_Base : Entity_Id;
20500 Delta_Val : Ureal;
20501 Small_Val : Ureal;
20502 Low_Val : Ureal;
20503 High_Val : Ureal;
20505 begin
20506 Check_Restriction (No_Fixed_Point, Def);
20508 -- Create implicit base type
20510 Implicit_Base :=
20511 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20512 Set_Etype (Implicit_Base, Implicit_Base);
20514 -- Analyze and process delta expression
20516 Analyze_And_Resolve (Delta_Expr, Any_Real);
20518 Check_Delta_Expression (Delta_Expr);
20519 Delta_Val := Expr_Value_R (Delta_Expr);
20521 Set_Delta_Value (Implicit_Base, Delta_Val);
20523 -- Compute default small from given delta, which is the largest power
20524 -- of two that does not exceed the given delta value.
20526 declare
20527 Tmp : Ureal;
20528 Scale : Int;
20530 begin
20531 Tmp := Ureal_1;
20532 Scale := 0;
20534 if Delta_Val < Ureal_1 then
20535 while Delta_Val < Tmp loop
20536 Tmp := Tmp / Ureal_2;
20537 Scale := Scale + 1;
20538 end loop;
20540 else
20541 loop
20542 Tmp := Tmp * Ureal_2;
20543 exit when Tmp > Delta_Val;
20544 Scale := Scale - 1;
20545 end loop;
20546 end if;
20548 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20549 end;
20551 Set_Small_Value (Implicit_Base, Small_Val);
20553 -- If no range was given, set a dummy range
20555 if RRS <= Empty_Or_Error then
20556 Low_Val := -Small_Val;
20557 High_Val := Small_Val;
20559 -- Otherwise analyze and process given range
20561 else
20562 declare
20563 Low : constant Node_Id := Low_Bound (RRS);
20564 High : constant Node_Id := High_Bound (RRS);
20566 begin
20567 Analyze_And_Resolve (Low, Any_Real);
20568 Analyze_And_Resolve (High, Any_Real);
20569 Check_Real_Bound (Low);
20570 Check_Real_Bound (High);
20572 -- Obtain and set the range
20574 Low_Val := Expr_Value_R (Low);
20575 High_Val := Expr_Value_R (High);
20577 if Low_Val > High_Val then
20578 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20579 end if;
20580 end;
20581 end if;
20583 -- The range for both the implicit base and the declared first subtype
20584 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20585 -- set a temporary range in place. Note that the bounds of the base
20586 -- type will be widened to be symmetrical and to fill the available
20587 -- bits when the type is frozen.
20589 -- We could do this with all discrete types, and probably should, but
20590 -- we absolutely have to do it for fixed-point, since the end-points
20591 -- of the range and the size are determined by the small value, which
20592 -- could be reset before the freeze point.
20594 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20595 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20597 -- Complete definition of first subtype. The inheritance of the rep item
20598 -- chain ensures that SPARK-related pragmas are not clobbered when the
20599 -- ordinary fixed point type acts as a full view of a private type.
20601 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20602 Set_Etype (T, Implicit_Base);
20603 Reinit_Size_Align (T);
20604 Inherit_Rep_Item_Chain (T, Implicit_Base);
20605 Set_Small_Value (T, Small_Val);
20606 Set_Delta_Value (T, Delta_Val);
20607 Set_Is_Constrained (T);
20608 end Ordinary_Fixed_Point_Type_Declaration;
20610 ----------------------------------
20611 -- Preanalyze_Assert_Expression --
20612 ----------------------------------
20614 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20615 begin
20616 In_Assertion_Expr := In_Assertion_Expr + 1;
20617 Preanalyze_Spec_Expression (N, T);
20618 In_Assertion_Expr := In_Assertion_Expr - 1;
20619 end Preanalyze_Assert_Expression;
20621 -- ??? The variant below explicitly saves and restores all the flags,
20622 -- because it is impossible to compose the existing variety of
20623 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20624 -- to achieve the desired semantics.
20626 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20627 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20628 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
20629 Save_Full_Analysis : constant Boolean := Full_Analysis;
20631 begin
20632 In_Assertion_Expr := In_Assertion_Expr + 1;
20633 In_Spec_Expression := True;
20634 Set_Must_Not_Freeze (N);
20635 Inside_Preanalysis_Without_Freezing :=
20636 Inside_Preanalysis_Without_Freezing + 1;
20637 Full_Analysis := False;
20638 Expander_Mode_Save_And_Set (False);
20640 if GNATprove_Mode then
20641 Analyze_And_Resolve (N);
20642 else
20643 Analyze_And_Resolve (N, Suppress => All_Checks);
20644 end if;
20646 Expander_Mode_Restore;
20647 Full_Analysis := Save_Full_Analysis;
20648 Inside_Preanalysis_Without_Freezing :=
20649 Inside_Preanalysis_Without_Freezing - 1;
20650 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
20651 In_Spec_Expression := Save_In_Spec_Expression;
20652 In_Assertion_Expr := In_Assertion_Expr - 1;
20653 end Preanalyze_Assert_Expression;
20655 -----------------------------------
20656 -- Preanalyze_Default_Expression --
20657 -----------------------------------
20659 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20660 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20661 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20663 begin
20664 In_Default_Expr := True;
20665 In_Spec_Expression := True;
20667 Preanalyze_With_Freezing_And_Resolve (N, T);
20669 In_Default_Expr := Save_In_Default_Expr;
20670 In_Spec_Expression := Save_In_Spec_Expression;
20671 end Preanalyze_Default_Expression;
20673 --------------------------------
20674 -- Preanalyze_Spec_Expression --
20675 --------------------------------
20677 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20678 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20679 begin
20680 In_Spec_Expression := True;
20681 Preanalyze_And_Resolve (N, T);
20682 In_Spec_Expression := Save_In_Spec_Expression;
20683 end Preanalyze_Spec_Expression;
20685 ----------------------------------------
20686 -- Prepare_Private_Subtype_Completion --
20687 ----------------------------------------
20689 procedure Prepare_Private_Subtype_Completion
20690 (Id : Entity_Id;
20691 Related_Nod : Node_Id)
20693 Id_B : constant Entity_Id := Base_Type (Id);
20694 Full_B : constant Entity_Id := Full_View (Id_B);
20695 Full : Entity_Id;
20697 begin
20698 if Present (Full_B) then
20700 -- The Base_Type is already completed, we can complete the subtype
20701 -- now. We have to create a new entity with the same name, Thus we
20702 -- can't use Create_Itype.
20704 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20705 Set_Is_Itype (Full);
20706 Set_Associated_Node_For_Itype (Full, Related_Nod);
20707 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20708 Set_Full_View (Id, Full);
20709 end if;
20711 -- The parent subtype may be private, but the base might not, in some
20712 -- nested instances. In that case, the subtype does not need to be
20713 -- exchanged. It would still be nice to make private subtypes and their
20714 -- bases consistent at all times ???
20716 if Is_Private_Type (Id_B) then
20717 Append_Elmt (Id, Private_Dependents (Id_B));
20718 end if;
20719 end Prepare_Private_Subtype_Completion;
20721 ---------------------------
20722 -- Process_Discriminants --
20723 ---------------------------
20725 procedure Process_Discriminants
20726 (N : Node_Id;
20727 Prev : Entity_Id := Empty)
20729 Elist : constant Elist_Id := New_Elmt_List;
20730 Id : Node_Id;
20731 Discr : Node_Id;
20732 Discr_Number : Uint;
20733 Discr_Type : Entity_Id;
20734 Default_Present : Boolean := False;
20735 Default_Not_Present : Boolean := False;
20737 begin
20738 -- A composite type other than an array type can have discriminants.
20739 -- On entry, the current scope is the composite type.
20741 -- The discriminants are initially entered into the scope of the type
20742 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20743 -- use, as explained at the end of this procedure.
20745 Discr := First (Discriminant_Specifications (N));
20746 while Present (Discr) loop
20747 Enter_Name (Defining_Identifier (Discr));
20749 -- For navigation purposes we add a reference to the discriminant
20750 -- in the entity for the type. If the current declaration is a
20751 -- completion, place references on the partial view. Otherwise the
20752 -- type is the current scope.
20754 if Present (Prev) then
20756 -- The references go on the partial view, if present. If the
20757 -- partial view has discriminants, the references have been
20758 -- generated already.
20760 if not Has_Discriminants (Prev) then
20761 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20762 end if;
20763 else
20764 Generate_Reference
20765 (Current_Scope, Defining_Identifier (Discr), 'd');
20766 end if;
20768 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20769 Check_Anonymous_Access_Component
20770 (Typ_Decl => N,
20771 Typ => Defining_Identifier (N),
20772 Prev => Prev,
20773 Comp_Def => Discr,
20774 Access_Def => Discriminant_Type (Discr));
20776 -- if Check_Anonymous_Access_Component replaced Discr then
20777 -- its Original_Node points to the old Discr and the access type
20778 -- for Discr_Type has already been created.
20780 if Is_Rewrite_Substitution (Discr) then
20781 Discr_Type := Etype (Discriminant_Type (Discr));
20782 else
20783 Discr_Type :=
20784 Access_Definition (Discr, Discriminant_Type (Discr));
20786 -- Ada 2005 (AI-254)
20788 if Present (Access_To_Subprogram_Definition
20789 (Discriminant_Type (Discr)))
20790 and then Protected_Present (Access_To_Subprogram_Definition
20791 (Discriminant_Type (Discr)))
20792 then
20793 Discr_Type :=
20794 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20795 end if;
20796 end if;
20797 else
20798 Find_Type (Discriminant_Type (Discr));
20799 Discr_Type := Etype (Discriminant_Type (Discr));
20801 if Error_Posted (Discriminant_Type (Discr)) then
20802 Discr_Type := Any_Type;
20803 end if;
20804 end if;
20806 -- Handling of discriminants that are access types
20808 if Is_Access_Type (Discr_Type) then
20810 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20811 -- limited record types
20813 if Ada_Version < Ada_2005 then
20814 Check_Access_Discriminant_Requires_Limited
20815 (Discr, Discriminant_Type (Discr));
20816 end if;
20818 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20819 Error_Msg_N
20820 ("(Ada 83) access discriminant not allowed", Discr);
20821 end if;
20823 -- If not access type, must be a discrete type
20825 elsif not Is_Discrete_Type (Discr_Type) then
20826 Error_Msg_N
20827 ("discriminants must have a discrete or access type",
20828 Discriminant_Type (Discr));
20829 end if;
20831 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20833 -- If a discriminant specification includes the assignment compound
20834 -- delimiter followed by an expression, the expression is the default
20835 -- expression of the discriminant; the default expression must be of
20836 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20837 -- a default expression, we do the special preanalysis, since this
20838 -- expression does not freeze (see section "Handling of Default and
20839 -- Per-Object Expressions" in spec of package Sem).
20841 if Present (Expression (Discr)) then
20842 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20844 -- Legaity checks
20846 if Nkind (N) = N_Formal_Type_Declaration then
20847 Error_Msg_N
20848 ("discriminant defaults not allowed for formal type",
20849 Expression (Discr));
20851 -- Flag an error for a tagged type with defaulted discriminants,
20852 -- excluding limited tagged types when compiling for Ada 2012
20853 -- (see AI05-0214).
20855 elsif Is_Tagged_Type (Current_Scope)
20856 and then (not Is_Limited_Type (Current_Scope)
20857 or else Ada_Version < Ada_2012)
20858 and then Comes_From_Source (N)
20859 then
20860 -- Note: see similar test in Check_Or_Process_Discriminants, to
20861 -- handle the (illegal) case of the completion of an untagged
20862 -- view with discriminants with defaults by a tagged full view.
20863 -- We skip the check if Discr does not come from source, to
20864 -- account for the case of an untagged derived type providing
20865 -- defaults for a renamed discriminant from a private untagged
20866 -- ancestor with a tagged full view (ACATS B460006).
20868 if Ada_Version >= Ada_2012 then
20869 Error_Msg_N
20870 ("discriminants of nonlimited tagged type cannot have"
20871 & " defaults",
20872 Expression (Discr));
20873 else
20874 Error_Msg_N
20875 ("discriminants of tagged type cannot have defaults",
20876 Expression (Discr));
20877 end if;
20879 else
20880 Default_Present := True;
20881 Append_Elmt (Expression (Discr), Elist);
20883 -- Tag the defining identifiers for the discriminants with
20884 -- their corresponding default expressions from the tree.
20886 Set_Discriminant_Default_Value
20887 (Defining_Identifier (Discr), Expression (Discr));
20888 end if;
20890 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20891 -- gets set unless we can be sure that no range check is required.
20893 if not Expander_Active
20894 and then not
20895 Is_In_Range
20896 (Expression (Discr), Discr_Type, Assume_Valid => True)
20897 then
20898 Set_Do_Range_Check (Expression (Discr));
20899 end if;
20901 -- No default discriminant value given
20903 else
20904 Default_Not_Present := True;
20905 end if;
20907 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20908 -- Discr_Type but with the null-exclusion attribute
20910 if Ada_Version >= Ada_2005 then
20912 -- Ada 2005 (AI-231): Static checks
20914 if Can_Never_Be_Null (Discr_Type) then
20915 Null_Exclusion_Static_Checks (Discr);
20917 elsif Is_Access_Type (Discr_Type)
20918 and then Null_Exclusion_Present (Discr)
20920 -- No need to check itypes because in their case this check
20921 -- was done at their point of creation
20923 and then not Is_Itype (Discr_Type)
20924 then
20925 if Can_Never_Be_Null (Discr_Type) then
20926 Error_Msg_NE
20927 ("`NOT NULL` not allowed (& already excludes null)",
20928 Discr,
20929 Discr_Type);
20930 end if;
20932 Set_Etype (Defining_Identifier (Discr),
20933 Create_Null_Excluding_Itype
20934 (T => Discr_Type,
20935 Related_Nod => Discr));
20937 -- Check for improper null exclusion if the type is otherwise
20938 -- legal for a discriminant.
20940 elsif Null_Exclusion_Present (Discr)
20941 and then Is_Discrete_Type (Discr_Type)
20942 then
20943 Error_Msg_N
20944 ("null exclusion can only apply to an access type", Discr);
20945 end if;
20947 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20948 -- can't have defaults. Synchronized types, or types that are
20949 -- explicitly limited are fine, but special tests apply to derived
20950 -- types in generics: in a generic body we have to assume the
20951 -- worst, and therefore defaults are not allowed if the parent is
20952 -- a generic formal private type (see ACATS B370001).
20954 if Is_Access_Type (Discr_Type) and then Default_Present then
20955 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20956 or else Is_Limited_Record (Current_Scope)
20957 or else Is_Concurrent_Type (Current_Scope)
20958 or else Is_Concurrent_Record_Type (Current_Scope)
20959 or else Ekind (Current_Scope) = E_Limited_Private_Type
20960 then
20961 if not Is_Derived_Type (Current_Scope)
20962 or else not Is_Generic_Type (Etype (Current_Scope))
20963 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20964 or else Limited_Present
20965 (Type_Definition (Parent (Current_Scope)))
20966 then
20967 null;
20969 else
20970 Error_Msg_N
20971 ("access discriminants of nonlimited types cannot "
20972 & "have defaults", Expression (Discr));
20973 end if;
20975 elsif Present (Expression (Discr)) then
20976 Error_Msg_N
20977 ("(Ada 2005) access discriminants of nonlimited types "
20978 & "cannot have defaults", Expression (Discr));
20979 end if;
20980 end if;
20981 end if;
20983 Next (Discr);
20984 end loop;
20986 -- An element list consisting of the default expressions of the
20987 -- discriminants is constructed in the above loop and used to set
20988 -- the Discriminant_Constraint attribute for the type. If an object
20989 -- is declared of this (record or task) type without any explicit
20990 -- discriminant constraint given, this element list will form the
20991 -- actual parameters for the corresponding initialization procedure
20992 -- for the type.
20994 Set_Discriminant_Constraint (Current_Scope, Elist);
20995 Set_Stored_Constraint (Current_Scope, No_Elist);
20997 -- Default expressions must be provided either for all or for none
20998 -- of the discriminants of a discriminant part. (RM 3.7.1)
21000 if Default_Present and then Default_Not_Present then
21001 Error_Msg_N
21002 ("incomplete specification of defaults for discriminants", N);
21003 end if;
21005 -- The use of the name of a discriminant is not allowed in default
21006 -- expressions of a discriminant part if the specification of the
21007 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
21009 -- To detect this, the discriminant names are entered initially with an
21010 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
21011 -- attempt to use a void entity (for example in an expression that is
21012 -- type-checked) produces the error message: premature usage. Now after
21013 -- completing the semantic analysis of the discriminant part, we can set
21014 -- the Ekind of all the discriminants appropriately.
21016 Discr := First (Discriminant_Specifications (N));
21017 Discr_Number := Uint_1;
21018 while Present (Discr) loop
21019 Id := Defining_Identifier (Discr);
21021 if Ekind (Id) = E_In_Parameter then
21022 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
21023 end if;
21025 Mutate_Ekind (Id, E_Discriminant);
21026 Set_Is_Not_Self_Hidden (Id);
21027 Reinit_Component_Location (Id);
21028 Reinit_Esize (Id);
21029 Set_Discriminant_Number (Id, Discr_Number);
21031 -- Make sure this is always set, even in illegal programs
21033 Set_Corresponding_Discriminant (Id, Empty);
21035 -- Initialize the Original_Record_Component to the entity itself.
21036 -- Inherit_Components will propagate the right value to
21037 -- discriminants in derived record types.
21039 Set_Original_Record_Component (Id, Id);
21041 -- Create the discriminal for the discriminant
21043 Build_Discriminal (Id);
21045 Next (Discr);
21046 Discr_Number := Discr_Number + 1;
21047 end loop;
21049 Set_Has_Discriminants (Current_Scope);
21050 end Process_Discriminants;
21052 -----------------------
21053 -- Process_Full_View --
21054 -----------------------
21056 -- WARNING: This routine manages Ghost regions. Return statements must be
21057 -- replaced by gotos which jump to the end of the routine and restore the
21058 -- Ghost mode.
21060 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
21061 procedure Collect_Implemented_Interfaces
21062 (Typ : Entity_Id;
21063 Ifaces : Elist_Id);
21064 -- Ada 2005: Gather all the interfaces that Typ directly or
21065 -- inherently implements. Duplicate entries are not added to
21066 -- the list Ifaces.
21068 ------------------------------------
21069 -- Collect_Implemented_Interfaces --
21070 ------------------------------------
21072 procedure Collect_Implemented_Interfaces
21073 (Typ : Entity_Id;
21074 Ifaces : Elist_Id)
21076 Iface : Entity_Id;
21077 Iface_Elmt : Elmt_Id;
21079 begin
21080 -- Abstract interfaces are only associated with tagged record types
21082 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
21083 return;
21084 end if;
21086 -- Recursively climb to the ancestors
21088 if Etype (Typ) /= Typ
21090 -- Protect the frontend against wrong cyclic declarations like:
21092 -- type B is new A with private;
21093 -- type C is new A with private;
21094 -- private
21095 -- type B is new C with null record;
21096 -- type C is new B with null record;
21098 and then Etype (Typ) /= Priv_T
21099 and then Etype (Typ) /= Full_T
21100 then
21101 -- Keep separate the management of private type declarations
21103 if Ekind (Typ) = E_Record_Type_With_Private then
21105 -- Handle the following illegal usage:
21106 -- type Private_Type is tagged private;
21107 -- private
21108 -- type Private_Type is new Type_Implementing_Iface;
21110 if Present (Full_View (Typ))
21111 and then Etype (Typ) /= Full_View (Typ)
21112 then
21113 if Is_Interface (Etype (Typ)) then
21114 Append_Unique_Elmt (Etype (Typ), Ifaces);
21115 end if;
21117 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21118 end if;
21120 -- Non-private types
21122 else
21123 if Is_Interface (Etype (Typ)) then
21124 Append_Unique_Elmt (Etype (Typ), Ifaces);
21125 end if;
21127 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21128 end if;
21129 end if;
21131 -- Handle entities in the list of abstract interfaces
21133 if Present (Interfaces (Typ)) then
21134 Iface_Elmt := First_Elmt (Interfaces (Typ));
21135 while Present (Iface_Elmt) loop
21136 Iface := Node (Iface_Elmt);
21138 pragma Assert (Is_Interface (Iface));
21140 if not Contain_Interface (Iface, Ifaces) then
21141 Append_Elmt (Iface, Ifaces);
21142 Collect_Implemented_Interfaces (Iface, Ifaces);
21143 end if;
21145 Next_Elmt (Iface_Elmt);
21146 end loop;
21147 end if;
21148 end Collect_Implemented_Interfaces;
21150 -- Local variables
21152 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21153 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21154 -- Save the Ghost-related attributes to restore on exit
21156 Full_Indic : Node_Id;
21157 Full_Parent : Entity_Id;
21158 Priv_Parent : Entity_Id;
21160 -- Start of processing for Process_Full_View
21162 begin
21163 Mark_And_Set_Ghost_Completion (N, Priv_T);
21165 -- First some sanity checks that must be done after semantic
21166 -- decoration of the full view and thus cannot be placed with other
21167 -- similar checks in Find_Type_Name
21169 if not Is_Limited_Type (Priv_T)
21170 and then (Is_Limited_Type (Full_T)
21171 or else Is_Limited_Composite (Full_T))
21172 then
21173 if In_Instance then
21174 null;
21175 else
21176 Error_Msg_N
21177 ("completion of nonlimited type cannot be limited", Full_T);
21178 Explain_Limited_Type (Full_T, Full_T);
21179 end if;
21181 elsif Is_Abstract_Type (Full_T)
21182 and then not Is_Abstract_Type (Priv_T)
21183 then
21184 Error_Msg_N
21185 ("completion of nonabstract type cannot be abstract", Full_T);
21187 elsif Is_Tagged_Type (Priv_T)
21188 and then Is_Limited_Type (Priv_T)
21189 and then not Is_Limited_Type (Full_T)
21190 then
21191 -- If pragma CPP_Class was applied to the private declaration
21192 -- propagate the limitedness to the full-view
21194 if Is_CPP_Class (Priv_T) then
21195 Set_Is_Limited_Record (Full_T);
21197 -- GNAT allow its own definition of Limited_Controlled to disobey
21198 -- this rule in order in ease the implementation. This test is safe
21199 -- because Root_Controlled is defined in a child of System that
21200 -- normal programs are not supposed to use.
21202 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21203 Set_Is_Limited_Composite (Full_T);
21204 else
21205 Error_Msg_N
21206 ("completion of limited tagged type must be limited", Full_T);
21207 end if;
21209 elsif Is_Generic_Type (Priv_T) then
21210 Error_Msg_N ("generic type cannot have a completion", Full_T);
21211 end if;
21213 -- Check that ancestor interfaces of private and full views are
21214 -- consistent. We omit this check for synchronized types because
21215 -- they are performed on the corresponding record type when frozen.
21217 if Ada_Version >= Ada_2005
21218 and then Is_Tagged_Type (Priv_T)
21219 and then Is_Tagged_Type (Full_T)
21220 and then not Is_Concurrent_Type (Full_T)
21221 then
21222 declare
21223 Iface : Entity_Id;
21224 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21225 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21227 begin
21228 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21229 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21231 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21232 -- an interface type if and only if the full type is descendant
21233 -- of the interface type (AARM 7.3 (7.3/2)).
21235 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21237 if Present (Iface) then
21238 Error_Msg_NE
21239 ("interface in partial view& not implemented by full type "
21240 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21241 end if;
21243 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21245 if Present (Iface) then
21246 Error_Msg_NE
21247 ("interface & not implemented by partial view "
21248 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21249 end if;
21250 end;
21251 end if;
21253 if Is_Tagged_Type (Priv_T)
21254 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21255 and then Is_Derived_Type (Full_T)
21256 then
21257 Priv_Parent := Etype (Priv_T);
21259 -- The full view of a private extension may have been transformed
21260 -- into an unconstrained derived type declaration and a subtype
21261 -- declaration (see build_derived_record_type for details).
21263 if Nkind (N) = N_Subtype_Declaration then
21264 Full_Indic := Subtype_Indication (N);
21265 Full_Parent := Etype (Base_Type (Full_T));
21266 else
21267 Full_Indic := Subtype_Indication (Type_Definition (N));
21268 Full_Parent := Etype (Full_T);
21269 end if;
21271 -- Check that the parent type of the full type is a descendant of
21272 -- the ancestor subtype given in the private extension. If either
21273 -- entity has an Etype equal to Any_Type then we had some previous
21274 -- error situation [7.3(8)].
21276 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21277 goto Leave;
21279 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21280 -- any order. Therefore we don't have to check that its parent must
21281 -- be a descendant of the parent of the private type declaration.
21283 elsif Is_Interface (Priv_Parent)
21284 and then Is_Interface (Full_Parent)
21285 then
21286 null;
21288 -- Ada 2005 (AI-251): If the parent of the private type declaration
21289 -- is an interface there is no need to check that it is an ancestor
21290 -- of the associated full type declaration. The required tests for
21291 -- this case are performed by Build_Derived_Record_Type.
21293 elsif not Is_Interface (Base_Type (Priv_Parent))
21294 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21295 then
21296 Error_Msg_N
21297 ("parent of full type must descend from parent of private "
21298 & "extension", Full_Indic);
21300 -- First check a formal restriction, and then proceed with checking
21301 -- Ada rules. Since the formal restriction is not a serious error, we
21302 -- don't prevent further error detection for this check, hence the
21303 -- ELSE.
21305 else
21306 -- Check the rules of 7.3(10): if the private extension inherits
21307 -- known discriminants, then the full type must also inherit those
21308 -- discriminants from the same (ancestor) type, and the parent
21309 -- subtype of the full type must be constrained if and only if
21310 -- the ancestor subtype of the private extension is constrained.
21312 if No (Discriminant_Specifications (Parent (Priv_T)))
21313 and then not Has_Unknown_Discriminants (Priv_T)
21314 and then Has_Discriminants (Base_Type (Priv_Parent))
21315 then
21316 declare
21317 Priv_Indic : constant Node_Id :=
21318 Subtype_Indication (Parent (Priv_T));
21320 Priv_Constr : constant Boolean :=
21321 Is_Constrained (Priv_Parent)
21322 or else
21323 Nkind (Priv_Indic) = N_Subtype_Indication
21324 or else
21325 Is_Constrained (Entity (Priv_Indic));
21327 Full_Constr : constant Boolean :=
21328 Is_Constrained (Full_Parent)
21329 or else
21330 Nkind (Full_Indic) = N_Subtype_Indication
21331 or else
21332 Is_Constrained (Entity (Full_Indic));
21334 Priv_Discr : Entity_Id;
21335 Full_Discr : Entity_Id;
21337 begin
21338 Priv_Discr := First_Discriminant (Priv_Parent);
21339 Full_Discr := First_Discriminant (Full_Parent);
21340 while Present (Priv_Discr) and then Present (Full_Discr) loop
21341 if Original_Record_Component (Priv_Discr) =
21342 Original_Record_Component (Full_Discr)
21343 or else
21344 Corresponding_Discriminant (Priv_Discr) =
21345 Corresponding_Discriminant (Full_Discr)
21346 then
21347 null;
21348 else
21349 exit;
21350 end if;
21352 Next_Discriminant (Priv_Discr);
21353 Next_Discriminant (Full_Discr);
21354 end loop;
21356 if Present (Priv_Discr) or else Present (Full_Discr) then
21357 Error_Msg_N
21358 ("full view must inherit discriminants of the parent "
21359 & "type used in the private extension", Full_Indic);
21361 elsif Priv_Constr and then not Full_Constr then
21362 Error_Msg_N
21363 ("parent subtype of full type must be constrained",
21364 Full_Indic);
21366 elsif Full_Constr and then not Priv_Constr then
21367 Error_Msg_N
21368 ("parent subtype of full type must be unconstrained",
21369 Full_Indic);
21370 end if;
21371 end;
21373 -- Check the rules of 7.3(12): if a partial view has neither
21374 -- known or unknown discriminants, then the full type
21375 -- declaration shall define a definite subtype.
21377 elsif not Has_Unknown_Discriminants (Priv_T)
21378 and then not Has_Discriminants (Priv_T)
21379 and then not Is_Constrained (Full_T)
21380 then
21381 Error_Msg_N
21382 ("full view must define a constrained type if partial view "
21383 & "has no discriminants", Full_T);
21384 end if;
21386 -- Do we implement the following properly???
21387 -- If the ancestor subtype of a private extension has constrained
21388 -- discriminants, then the parent subtype of the full view shall
21389 -- impose a statically matching constraint on those discriminants
21390 -- [7.3(13)].
21391 end if;
21393 else
21394 -- For untagged types, verify that a type without discriminants is
21395 -- not completed with an unconstrained type. A separate error message
21396 -- is produced if the full type has defaulted discriminants.
21398 if Is_Definite_Subtype (Priv_T)
21399 and then not Is_Definite_Subtype (Full_T)
21400 then
21401 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21402 Error_Msg_NE
21403 ("full view of& not compatible with declaration#",
21404 Full_T, Priv_T);
21406 if not Is_Tagged_Type (Full_T) then
21407 Error_Msg_N
21408 ("\one is constrained, the other unconstrained", Full_T);
21409 end if;
21410 end if;
21411 end if;
21413 -- AI-419: verify that the use of "limited" is consistent
21415 declare
21416 Orig_Decl : constant Node_Id := Original_Node (N);
21418 begin
21419 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21420 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21421 and then Nkind
21422 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21423 then
21424 if not Limited_Present (Parent (Priv_T))
21425 and then not Synchronized_Present (Parent (Priv_T))
21426 and then Limited_Present (Type_Definition (Orig_Decl))
21427 then
21428 Error_Msg_N
21429 ("full view of non-limited extension cannot be limited", N);
21431 -- Conversely, if the partial view carries the limited keyword,
21432 -- the full view must as well, even if it may be redundant.
21434 elsif Limited_Present (Parent (Priv_T))
21435 and then not Limited_Present (Type_Definition (Orig_Decl))
21436 then
21437 Error_Msg_N
21438 ("full view of limited extension must be explicitly limited",
21440 end if;
21441 end if;
21442 end;
21444 -- Ada 2005 (AI-443): A synchronized private extension must be
21445 -- completed by a task or protected type.
21447 if Ada_Version >= Ada_2005
21448 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21449 and then Synchronized_Present (Parent (Priv_T))
21450 and then not Is_Concurrent_Type (Full_T)
21451 then
21452 Error_Msg_N ("full view of synchronized extension must " &
21453 "be synchronized type", N);
21454 end if;
21456 -- Ada 2005 AI-363: if the full view has discriminants with
21457 -- defaults, it is illegal to declare constrained access subtypes
21458 -- whose designated type is the current type. This allows objects
21459 -- of the type that are declared in the heap to be unconstrained.
21461 if not Has_Unknown_Discriminants (Priv_T)
21462 and then not Has_Discriminants (Priv_T)
21463 and then Has_Defaulted_Discriminants (Full_T)
21464 then
21465 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21466 Set_Has_Constrained_Partial_View (Priv_T);
21467 end if;
21469 -- Create a full declaration for all its subtypes recorded in
21470 -- Private_Dependents and swap them similarly to the base type. These
21471 -- are subtypes that have been define before the full declaration of
21472 -- the private type. We also swap the entry in Private_Dependents list
21473 -- so we can properly restore the private view on exit from the scope.
21475 declare
21476 Priv_Elmt : Elmt_Id;
21477 Priv_Scop : Entity_Id;
21478 Priv : Entity_Id;
21479 Full : Entity_Id;
21481 begin
21482 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21483 while Present (Priv_Elmt) loop
21484 Priv := Node (Priv_Elmt);
21485 Priv_Scop := Scope (Priv);
21487 if Ekind (Priv) in E_Private_Subtype
21488 | E_Limited_Private_Subtype
21489 | E_Record_Subtype_With_Private
21490 then
21491 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21492 Set_Is_Itype (Full);
21493 Set_Parent (Full, Parent (Priv));
21494 Set_Associated_Node_For_Itype (Full, N);
21496 -- Now we need to complete the private subtype, but since the
21497 -- base type has already been swapped, we must also swap the
21498 -- subtypes (and thus, reverse the arguments in the call to
21499 -- Complete_Private_Subtype). Also note that we may need to
21500 -- re-establish the scope of the private subtype.
21502 Copy_And_Swap (Priv, Full);
21504 if not In_Open_Scopes (Priv_Scop) then
21505 Push_Scope (Priv_Scop);
21507 else
21508 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21510 Priv_Scop := Empty;
21511 end if;
21513 Complete_Private_Subtype (Full, Priv, Full_T, N);
21514 Set_Full_View (Full, Priv);
21516 if Present (Priv_Scop) then
21517 Pop_Scope;
21518 end if;
21520 Replace_Elmt (Priv_Elmt, Full);
21521 end if;
21523 Next_Elmt (Priv_Elmt);
21524 end loop;
21525 end;
21527 declare
21528 Disp_Typ : Entity_Id;
21529 Full_List : Elist_Id;
21530 Prim : Entity_Id;
21531 Prim_Elmt : Elmt_Id;
21532 Priv_List : Elist_Id;
21534 function Contains
21535 (E : Entity_Id;
21536 L : Elist_Id) return Boolean;
21537 -- Determine whether list L contains element E
21539 --------------
21540 -- Contains --
21541 --------------
21543 function Contains
21544 (E : Entity_Id;
21545 L : Elist_Id) return Boolean
21547 List_Elmt : Elmt_Id;
21549 begin
21550 List_Elmt := First_Elmt (L);
21551 while Present (List_Elmt) loop
21552 if Node (List_Elmt) = E then
21553 return True;
21554 end if;
21556 Next_Elmt (List_Elmt);
21557 end loop;
21559 return False;
21560 end Contains;
21562 -- Start of processing
21564 begin
21565 -- If the private view was tagged, copy the new primitive operations
21566 -- from the private view to the full view.
21568 if Is_Tagged_Type (Full_T) then
21569 if Is_Tagged_Type (Priv_T) then
21570 Priv_List := Primitive_Operations (Priv_T);
21571 Prim_Elmt := First_Elmt (Priv_List);
21573 -- In the case of a concurrent type completing a private tagged
21574 -- type, primitives may have been declared in between the two
21575 -- views. These subprograms need to be wrapped the same way
21576 -- entries and protected procedures are handled because they
21577 -- cannot be directly shared by the two views.
21579 if Is_Concurrent_Type (Full_T) then
21580 declare
21581 Conc_Typ : constant Entity_Id :=
21582 Corresponding_Record_Type (Full_T);
21583 Curr_Nod : Node_Id := Parent (Conc_Typ);
21584 Wrap_Spec : Node_Id;
21586 begin
21587 while Present (Prim_Elmt) loop
21588 Prim := Node (Prim_Elmt);
21590 if Comes_From_Source (Prim)
21591 and then not Is_Abstract_Subprogram (Prim)
21592 then
21593 Wrap_Spec :=
21594 Make_Subprogram_Declaration (Sloc (Prim),
21595 Specification =>
21596 Build_Wrapper_Spec
21597 (Subp_Id => Prim,
21598 Obj_Typ => Conc_Typ,
21599 Formals =>
21600 Parameter_Specifications
21601 (Parent (Prim))));
21603 Insert_After (Curr_Nod, Wrap_Spec);
21604 Curr_Nod := Wrap_Spec;
21606 Analyze (Wrap_Spec);
21608 -- Remove the wrapper from visibility to avoid
21609 -- spurious conflict with the wrapped entity.
21611 Set_Is_Immediately_Visible
21612 (Defining_Entity (Specification (Wrap_Spec)),
21613 False);
21614 end if;
21616 Next_Elmt (Prim_Elmt);
21617 end loop;
21619 goto Leave;
21620 end;
21622 -- For nonconcurrent types, transfer explicit primitives, but
21623 -- omit those inherited from the parent of the private view
21624 -- since they will be re-inherited later on.
21626 else
21627 Full_List := Primitive_Operations (Full_T);
21628 while Present (Prim_Elmt) loop
21629 Prim := Node (Prim_Elmt);
21631 if Comes_From_Source (Prim)
21632 and then not Contains (Prim, Full_List)
21633 then
21634 Append_Elmt (Prim, Full_List);
21635 end if;
21637 Next_Elmt (Prim_Elmt);
21638 end loop;
21639 end if;
21641 -- Untagged private view
21643 else
21644 Full_List := Primitive_Operations (Full_T);
21646 -- In this case the partial view is untagged, so here we locate
21647 -- all of the earlier primitives that need to be treated as
21648 -- dispatching (those that appear between the two views). Note
21649 -- that these additional operations must all be new operations
21650 -- (any earlier operations that override inherited operations
21651 -- of the full view will already have been inserted in the
21652 -- primitives list, marked by Check_Operation_From_Private_View
21653 -- as dispatching. Note that implicit "/=" operators are
21654 -- excluded from being added to the primitives list since they
21655 -- shouldn't be treated as dispatching (tagged "/=" is handled
21656 -- specially).
21658 Prim := Next_Entity (Full_T);
21659 while Present (Prim) and then Prim /= Priv_T loop
21660 if Ekind (Prim) in E_Procedure | E_Function then
21661 Disp_Typ := Find_Dispatching_Type (Prim);
21663 if Disp_Typ = Full_T
21664 and then (Chars (Prim) /= Name_Op_Ne
21665 or else Comes_From_Source (Prim))
21666 then
21667 Check_Controlling_Formals (Full_T, Prim);
21669 if Is_Suitable_Primitive (Prim)
21670 and then not Is_Dispatching_Operation (Prim)
21671 then
21672 Append_Elmt (Prim, Full_List);
21673 Set_Is_Dispatching_Operation (Prim);
21674 Set_DT_Position_Value (Prim, No_Uint);
21675 end if;
21677 elsif Is_Dispatching_Operation (Prim)
21678 and then Disp_Typ /= Full_T
21679 then
21680 -- Verify that it is not otherwise controlled by a
21681 -- formal or a return value of type T.
21683 Check_Controlling_Formals (Disp_Typ, Prim);
21684 end if;
21685 end if;
21687 Next_Entity (Prim);
21688 end loop;
21689 end if;
21691 -- For the tagged case, the two views can share the same primitive
21692 -- operations list and the same class-wide type. Update attributes
21693 -- of the class-wide type which depend on the full declaration.
21695 if Is_Tagged_Type (Priv_T) then
21696 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21697 Set_Class_Wide_Type
21698 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21700 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21701 end if;
21703 -- For untagged types, copy the primitives across from the private
21704 -- view to the full view, for support of prefixed calls when
21705 -- extensions are enabled, and better error messages otherwise.
21707 else
21708 Priv_List := Primitive_Operations (Priv_T);
21709 Prim_Elmt := First_Elmt (Priv_List);
21711 Full_List := Primitive_Operations (Full_T);
21712 while Present (Prim_Elmt) loop
21713 Prim := Node (Prim_Elmt);
21714 Append_Elmt (Prim, Full_List);
21715 Next_Elmt (Prim_Elmt);
21716 end loop;
21717 end if;
21718 end;
21720 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21722 if Known_To_Have_Preelab_Init (Priv_T) then
21724 -- Case where there is a pragma Preelaborable_Initialization. We
21725 -- always allow this in predefined units, which is cheating a bit,
21726 -- but it means we don't have to struggle to meet the requirements in
21727 -- the RM for having Preelaborable Initialization. Otherwise we
21728 -- require that the type meets the RM rules. But we can't check that
21729 -- yet, because of the rule about overriding Initialize, so we simply
21730 -- set a flag that will be checked at freeze time.
21732 if not In_Predefined_Unit (Full_T) then
21733 Set_Must_Have_Preelab_Init (Full_T);
21734 end if;
21735 end if;
21737 -- If pragma CPP_Class was applied to the private type declaration,
21738 -- propagate it now to the full type declaration.
21740 if Is_CPP_Class (Priv_T) then
21741 Set_Is_CPP_Class (Full_T);
21742 Set_Convention (Full_T, Convention_CPP);
21744 -- Check that components of imported CPP types do not have default
21745 -- expressions.
21747 Check_CPP_Type_Has_No_Defaults (Full_T);
21748 end if;
21750 -- If the private view has user specified stream attributes, then so has
21751 -- the full view.
21753 -- Why the test, how could these flags be already set in Full_T ???
21755 if Has_Specified_Stream_Read (Priv_T) then
21756 Set_Has_Specified_Stream_Read (Full_T);
21757 end if;
21759 if Has_Specified_Stream_Write (Priv_T) then
21760 Set_Has_Specified_Stream_Write (Full_T);
21761 end if;
21763 if Has_Specified_Stream_Input (Priv_T) then
21764 Set_Has_Specified_Stream_Input (Full_T);
21765 end if;
21767 if Has_Specified_Stream_Output (Priv_T) then
21768 Set_Has_Specified_Stream_Output (Full_T);
21769 end if;
21771 -- Propagate Default_Initial_Condition-related attributes from the
21772 -- partial view to the full view.
21774 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21776 -- And to the underlying full view, if any
21778 if Is_Private_Type (Full_T)
21779 and then Present (Underlying_Full_View (Full_T))
21780 then
21781 Propagate_DIC_Attributes
21782 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21783 end if;
21785 -- Propagate invariant-related attributes from the partial view to the
21786 -- full view.
21788 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21790 -- And to the underlying full view, if any
21792 if Is_Private_Type (Full_T)
21793 and then Present (Underlying_Full_View (Full_T))
21794 then
21795 Propagate_Invariant_Attributes
21796 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21797 end if;
21799 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21800 -- in the full view without advertising the inheritance in the partial
21801 -- view. This can only occur when the partial view has no parent type
21802 -- and the full view has an interface as a parent. Any other scenarios
21803 -- are illegal because implemented interfaces must match between the
21804 -- two views.
21806 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21807 declare
21808 Full_Par : constant Entity_Id := Etype (Full_T);
21809 Priv_Par : constant Entity_Id := Etype (Priv_T);
21811 begin
21812 if not Is_Interface (Priv_Par)
21813 and then Is_Interface (Full_Par)
21814 and then Has_Inheritable_Invariants (Full_Par)
21815 then
21816 Error_Msg_N
21817 ("hidden inheritance of class-wide type invariants not "
21818 & "allowed", N);
21819 end if;
21820 end;
21821 end if;
21823 -- Propagate predicates to full type, and predicate function if already
21824 -- defined. It is not clear that this can actually happen? the partial
21825 -- view cannot be frozen yet, and the predicate function has not been
21826 -- built. Still it is a cheap check and seems safer to make it.
21828 Propagate_Predicate_Attributes (Full_T, Priv_T);
21830 if Is_Private_Type (Full_T)
21831 and then Present (Underlying_Full_View (Full_T))
21832 then
21833 Propagate_Predicate_Attributes
21834 (Underlying_Full_View (Full_T), Priv_T);
21835 end if;
21837 <<Leave>>
21838 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21839 end Process_Full_View;
21841 -----------------------------------
21842 -- Process_Incomplete_Dependents --
21843 -----------------------------------
21845 procedure Process_Incomplete_Dependents
21846 (N : Node_Id;
21847 Full_T : Entity_Id;
21848 Inc_T : Entity_Id)
21850 Inc_Elmt : Elmt_Id;
21851 Priv_Dep : Entity_Id;
21852 New_Subt : Entity_Id;
21854 Disc_Constraint : Elist_Id;
21856 begin
21857 if No (Private_Dependents (Inc_T)) then
21858 return;
21859 end if;
21861 -- Itypes that may be generated by the completion of an incomplete
21862 -- subtype are not used by the back-end and not attached to the tree.
21863 -- They are created only for constraint-checking purposes.
21865 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21866 while Present (Inc_Elmt) loop
21867 Priv_Dep := Node (Inc_Elmt);
21869 if Ekind (Priv_Dep) = E_Subprogram_Type then
21871 -- An Access_To_Subprogram type may have a return type or a
21872 -- parameter type that is incomplete. Replace with the full view.
21874 if Etype (Priv_Dep) = Inc_T then
21875 Set_Etype (Priv_Dep, Full_T);
21876 end if;
21878 declare
21879 Formal : Entity_Id;
21881 begin
21882 Formal := First_Formal (Priv_Dep);
21883 while Present (Formal) loop
21884 if Etype (Formal) = Inc_T then
21885 Set_Etype (Formal, Full_T);
21886 end if;
21888 Next_Formal (Formal);
21889 end loop;
21890 end;
21892 elsif Is_Overloadable (Priv_Dep) then
21894 -- If a subprogram in the incomplete dependents list is primitive
21895 -- for a tagged full type then mark it as a dispatching operation,
21896 -- check whether it overrides an inherited subprogram, and check
21897 -- restrictions on its controlling formals. Note that a protected
21898 -- operation is never dispatching: only its wrapper operation
21899 -- (which has convention Ada) is.
21901 if Is_Tagged_Type (Full_T)
21902 and then Is_Primitive (Priv_Dep)
21903 and then Convention (Priv_Dep) /= Convention_Protected
21904 then
21905 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21906 Set_Is_Dispatching_Operation (Priv_Dep);
21907 Check_Controlling_Formals (Full_T, Priv_Dep);
21908 end if;
21910 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21912 -- Can happen during processing of a body before the completion
21913 -- of a TA type. Ignore, because spec is also on dependent list.
21915 return;
21917 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21918 -- corresponding subtype of the full view.
21920 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21921 and then Comes_From_Source (Priv_Dep)
21922 then
21923 Set_Subtype_Indication
21924 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21925 Reinit_Field_To_Zero
21926 (Priv_Dep, F_Private_Dependents,
21927 Old_Ekind => E_Incomplete_Subtype);
21928 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21929 Set_Etype (Priv_Dep, Full_T);
21930 Set_Analyzed (Parent (Priv_Dep), False);
21932 -- Reanalyze the declaration, suppressing the call to Enter_Name
21933 -- to avoid duplicate names.
21935 Analyze_Subtype_Declaration
21936 (N => Parent (Priv_Dep),
21937 Skip => True);
21939 -- Dependent is a subtype
21941 else
21942 -- We build a new subtype indication using the full view of the
21943 -- incomplete parent. The discriminant constraints have been
21944 -- elaborated already at the point of the subtype declaration.
21946 New_Subt := Create_Itype (E_Void, N);
21948 if Has_Discriminants (Full_T) then
21949 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21950 else
21951 Disc_Constraint := No_Elist;
21952 end if;
21954 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21955 Set_Full_View (Priv_Dep, New_Subt);
21956 end if;
21958 Next_Elmt (Inc_Elmt);
21959 end loop;
21960 end Process_Incomplete_Dependents;
21962 --------------------------------
21963 -- Process_Range_Expr_In_Decl --
21964 --------------------------------
21966 procedure Process_Range_Expr_In_Decl
21967 (R : Node_Id;
21968 T : Entity_Id;
21969 Subtyp : Entity_Id := Empty;
21970 Check_List : List_Id := No_List)
21972 Lo, Hi : Node_Id;
21973 R_Checks : Check_Result;
21974 Insert_Node : Node_Id;
21975 Def_Id : Entity_Id;
21977 begin
21978 Analyze_And_Resolve (R, Base_Type (T));
21980 if Nkind (R) = N_Range then
21981 Lo := Low_Bound (R);
21982 Hi := High_Bound (R);
21984 -- Validity checks on the range of a quantified expression are
21985 -- delayed until the construct is transformed into a loop.
21987 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21988 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21989 then
21990 null;
21992 -- We need to ensure validity of the bounds here, because if we
21993 -- go ahead and do the expansion, then the expanded code will get
21994 -- analyzed with range checks suppressed and we miss the check.
21996 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21997 -- the temporaries generated by routine Remove_Side_Effects by means
21998 -- of validity checks must use the same names. When a range appears
21999 -- in the parent of a generic, the range is processed with checks
22000 -- disabled as part of the generic context and with checks enabled
22001 -- for code generation purposes. This leads to link issues as the
22002 -- generic contains references to xxx_FIRST/_LAST, but the inlined
22003 -- template sees the temporaries generated by Remove_Side_Effects.
22005 else
22006 Validity_Check_Range (R, Subtyp);
22007 end if;
22009 -- If there were errors in the declaration, try and patch up some
22010 -- common mistakes in the bounds. The cases handled are literals
22011 -- which are Integer where the expected type is Real and vice versa.
22012 -- These corrections allow the compilation process to proceed further
22013 -- along since some basic assumptions of the format of the bounds
22014 -- are guaranteed.
22016 if Etype (R) = Any_Type then
22017 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
22018 Rewrite (Lo,
22019 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
22021 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
22022 Rewrite (Hi,
22023 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
22025 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
22026 Rewrite (Lo,
22027 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
22029 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
22030 Rewrite (Hi,
22031 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
22032 end if;
22034 Set_Etype (Lo, T);
22035 Set_Etype (Hi, T);
22036 end if;
22038 -- If the bounds of the range have been mistakenly given as string
22039 -- literals (perhaps in place of character literals), then an error
22040 -- has already been reported, but we rewrite the string literal as a
22041 -- bound of the range's type to avoid blowups in later processing
22042 -- that looks at static values.
22044 if Nkind (Lo) = N_String_Literal then
22045 Rewrite (Lo,
22046 Make_Attribute_Reference (Sloc (Lo),
22047 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
22048 Attribute_Name => Name_First));
22049 Analyze_And_Resolve (Lo);
22050 end if;
22052 if Nkind (Hi) = N_String_Literal then
22053 Rewrite (Hi,
22054 Make_Attribute_Reference (Sloc (Hi),
22055 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
22056 Attribute_Name => Name_First));
22057 Analyze_And_Resolve (Hi);
22058 end if;
22060 -- If bounds aren't scalar at this point then exit, avoiding
22061 -- problems with further processing of the range in this procedure.
22063 if not Is_Scalar_Type (Etype (Lo)) then
22064 return;
22065 end if;
22067 -- Resolve (actually Sem_Eval) has checked that the bounds are in
22068 -- then range of the base type. Here we check whether the bounds
22069 -- are in the range of the subtype itself. Note that if the bounds
22070 -- represent the null range the Constraint_Error exception should
22071 -- not be raised.
22073 -- Capture values of bounds and generate temporaries for them
22074 -- if needed, before applying checks, since checks may cause
22075 -- duplication of the expression without forcing evaluation.
22077 -- The forced evaluation removes side effects from expressions,
22078 -- which should occur also in GNATprove mode. Otherwise, we end up
22079 -- with unexpected insertions of actions at places where this is
22080 -- not supposed to occur, e.g. on default parameters of a call.
22082 if Expander_Active or GNATprove_Mode then
22084 -- Call Force_Evaluation to create declarations as needed
22085 -- to deal with side effects, and also create typ_FIRST/LAST
22086 -- entities for bounds if we have a subtype name.
22088 -- Note: we do this transformation even if expansion is not
22089 -- active if we are in GNATprove_Mode since the transformation
22090 -- is in general required to ensure that the resulting tree has
22091 -- proper Ada semantics.
22093 Force_Evaluation
22094 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22095 Force_Evaluation
22096 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22097 end if;
22099 -- We use a flag here instead of suppressing checks on the type
22100 -- because the type we check against isn't necessarily the place
22101 -- where we put the check.
22103 R_Checks := Get_Range_Checks (R, T);
22105 -- Look up tree to find an appropriate insertion point. We can't
22106 -- just use insert_actions because later processing depends on
22107 -- the insertion node. Prior to Ada 2012 the insertion point could
22108 -- only be a declaration or a loop, but quantified expressions can
22109 -- appear within any context in an expression, and the insertion
22110 -- point can be any statement, pragma, or declaration.
22112 Insert_Node := Parent (R);
22113 while Present (Insert_Node) loop
22114 exit when
22115 Nkind (Insert_Node) in N_Declaration
22116 and then
22117 Nkind (Insert_Node) not in N_Component_Declaration
22118 | N_Loop_Parameter_Specification
22119 | N_Function_Specification
22120 | N_Procedure_Specification;
22122 exit when Nkind (Insert_Node) in
22123 N_Later_Decl_Item |
22124 N_Statement_Other_Than_Procedure_Call |
22125 N_Procedure_Call_Statement |
22126 N_Pragma;
22128 Insert_Node := Parent (Insert_Node);
22129 end loop;
22131 if Present (Insert_Node) then
22133 -- Case of loop statement. Verify that the range is part of the
22134 -- subtype indication of the iteration scheme.
22136 if Nkind (Insert_Node) = N_Loop_Statement then
22137 declare
22138 Indic : Node_Id;
22140 begin
22141 Indic := Parent (R);
22142 while Present (Indic)
22143 and then Nkind (Indic) /= N_Subtype_Indication
22144 loop
22145 Indic := Parent (Indic);
22146 end loop;
22148 if Present (Indic) then
22149 Def_Id := Etype (Subtype_Mark (Indic));
22151 Insert_Range_Checks
22152 (R_Checks,
22153 Insert_Node,
22154 Def_Id,
22155 Sloc (Insert_Node),
22156 Do_Before => True);
22157 end if;
22158 end;
22160 -- Case of declarations. If the declaration is for a type and
22161 -- involves discriminants, the checks are premature at the
22162 -- declaration point and need to wait for the expansion of the
22163 -- initialization procedure, which will pass in the list to put
22164 -- them on; otherwise, the checks are done at the declaration
22165 -- point and there is no need to do them again in the
22166 -- initialization procedure.
22168 elsif Nkind (Insert_Node) in N_Declaration then
22169 Def_Id := Defining_Identifier (Insert_Node);
22171 if (Ekind (Def_Id) = E_Record_Type
22172 and then Depends_On_Discriminant (R))
22173 or else
22174 (Ekind (Def_Id) = E_Protected_Type
22175 and then Has_Discriminants (Def_Id))
22176 then
22177 if Present (Check_List) then
22178 Append_Range_Checks
22179 (R_Checks,
22180 Check_List, Def_Id, Sloc (Insert_Node));
22181 end if;
22183 else
22184 if No (Check_List) then
22185 Insert_Range_Checks
22186 (R_Checks,
22187 Insert_Node, Def_Id, Sloc (Insert_Node));
22188 end if;
22189 end if;
22191 -- Case of statements. Drop the checks, as the range appears in
22192 -- the context of a quantified expression. Insertion will take
22193 -- place when expression is expanded.
22195 else
22196 null;
22197 end if;
22198 end if;
22200 -- Case of other than an explicit N_Range node
22202 -- The forced evaluation removes side effects from expressions, which
22203 -- should occur also in GNATprove mode. Otherwise, we end up with
22204 -- unexpected insertions of actions at places where this is not
22205 -- supposed to occur, e.g. on default parameters of a call.
22207 elsif Expander_Active or GNATprove_Mode then
22208 Get_Index_Bounds (R, Lo, Hi);
22209 Force_Evaluation (Lo);
22210 Force_Evaluation (Hi);
22211 end if;
22212 end Process_Range_Expr_In_Decl;
22214 --------------------------------------
22215 -- Process_Real_Range_Specification --
22216 --------------------------------------
22218 procedure Process_Real_Range_Specification (Def : Node_Id) is
22219 Spec : constant Node_Id := Real_Range_Specification (Def);
22220 Lo : Node_Id;
22221 Hi : Node_Id;
22222 Err : Boolean := False;
22224 procedure Analyze_Bound (N : Node_Id);
22225 -- Analyze and check one bound
22227 -------------------
22228 -- Analyze_Bound --
22229 -------------------
22231 procedure Analyze_Bound (N : Node_Id) is
22232 begin
22233 Analyze_And_Resolve (N, Any_Real);
22235 if not Is_OK_Static_Expression (N) then
22236 Flag_Non_Static_Expr
22237 ("bound in real type definition is not static!", N);
22238 Err := True;
22239 end if;
22240 end Analyze_Bound;
22242 -- Start of processing for Process_Real_Range_Specification
22244 begin
22245 if Present (Spec) then
22246 Lo := Low_Bound (Spec);
22247 Hi := High_Bound (Spec);
22248 Analyze_Bound (Lo);
22249 Analyze_Bound (Hi);
22251 -- If error, clear away junk range specification
22253 if Err then
22254 Set_Real_Range_Specification (Def, Empty);
22255 end if;
22256 end if;
22257 end Process_Real_Range_Specification;
22259 ---------------------
22260 -- Process_Subtype --
22261 ---------------------
22263 function Process_Subtype
22264 (S : Node_Id;
22265 Related_Nod : Node_Id;
22266 Related_Id : Entity_Id := Empty;
22267 Suffix : Character := ' ') return Entity_Id
22269 procedure Check_Incomplete (T : Node_Id);
22270 -- Called to verify that an incomplete type is not used prematurely
22272 ----------------------
22273 -- Check_Incomplete --
22274 ----------------------
22276 procedure Check_Incomplete (T : Node_Id) is
22277 begin
22278 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22280 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22281 and then
22282 not (Ada_Version >= Ada_2005
22283 and then
22284 (Nkind (Parent (T)) = N_Subtype_Declaration
22285 or else (Nkind (Parent (T)) = N_Subtype_Indication
22286 and then Nkind (Parent (Parent (T))) =
22287 N_Subtype_Declaration)))
22288 then
22289 Error_Msg_N ("invalid use of type before its full declaration", T);
22290 end if;
22291 end Check_Incomplete;
22293 -- Local variables
22295 P : Node_Id;
22296 Def_Id : Entity_Id;
22297 Error_Node : Node_Id;
22298 Full_View_Id : Entity_Id;
22299 Subtype_Mark_Id : Entity_Id;
22301 May_Have_Null_Exclusion : Boolean;
22303 -- Start of processing for Process_Subtype
22305 begin
22306 -- Case of no constraints present
22308 if Nkind (S) /= N_Subtype_Indication then
22309 Find_Type (S);
22311 -- No way to proceed if the subtype indication is malformed. This
22312 -- will happen for example when the subtype indication in an object
22313 -- declaration is missing altogether and the expression is analyzed
22314 -- as if it were that indication.
22316 if not Is_Entity_Name (S) then
22317 return Any_Type;
22318 end if;
22320 Check_Incomplete (S);
22321 P := Parent (S);
22323 -- The following mirroring of assertion in Null_Exclusion_Present is
22324 -- ugly, can't we have a range, a static predicate or even a flag???
22326 May_Have_Null_Exclusion :=
22327 Present (P)
22328 and then
22329 Nkind (P) in N_Access_Definition
22330 | N_Access_Function_Definition
22331 | N_Access_Procedure_Definition
22332 | N_Access_To_Object_Definition
22333 | N_Allocator
22334 | N_Component_Definition
22335 | N_Derived_Type_Definition
22336 | N_Discriminant_Specification
22337 | N_Formal_Object_Declaration
22338 | N_Function_Specification
22339 | N_Object_Declaration
22340 | N_Object_Renaming_Declaration
22341 | N_Parameter_Specification
22342 | N_Subtype_Declaration;
22344 -- Ada 2005 (AI-231): Static check
22346 if Ada_Version >= Ada_2005
22347 and then May_Have_Null_Exclusion
22348 and then Null_Exclusion_Present (P)
22349 and then Nkind (P) /= N_Access_To_Object_Definition
22350 and then not Is_Access_Type (Entity (S))
22351 then
22352 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22353 end if;
22355 -- Create an Itype that is a duplicate of Entity (S) but with the
22356 -- null-exclusion attribute.
22358 if May_Have_Null_Exclusion
22359 and then Is_Access_Type (Entity (S))
22360 and then Null_Exclusion_Present (P)
22362 -- No need to check the case of an access to object definition.
22363 -- It is correct to define double not-null pointers.
22365 -- Example:
22366 -- type Not_Null_Int_Ptr is not null access Integer;
22367 -- type Acc is not null access Not_Null_Int_Ptr;
22369 and then Nkind (P) /= N_Access_To_Object_Definition
22370 then
22371 if Can_Never_Be_Null (Entity (S)) then
22372 case Nkind (Related_Nod) is
22373 when N_Full_Type_Declaration =>
22374 if Nkind (Type_Definition (Related_Nod))
22375 in N_Array_Type_Definition
22376 then
22377 Error_Node :=
22378 Subtype_Indication
22379 (Component_Definition
22380 (Type_Definition (Related_Nod)));
22381 else
22382 Error_Node :=
22383 Subtype_Indication (Type_Definition (Related_Nod));
22384 end if;
22386 when N_Subtype_Declaration =>
22387 Error_Node := Subtype_Indication (Related_Nod);
22389 when N_Object_Declaration =>
22390 Error_Node := Object_Definition (Related_Nod);
22392 when N_Component_Declaration =>
22393 Error_Node :=
22394 Subtype_Indication (Component_Definition (Related_Nod));
22396 when N_Allocator =>
22397 Error_Node := Expression (Related_Nod);
22399 when others =>
22400 pragma Assert (False);
22401 Error_Node := Related_Nod;
22402 end case;
22404 Error_Msg_NE
22405 ("`NOT NULL` not allowed (& already excludes null)",
22406 Error_Node,
22407 Entity (S));
22408 end if;
22410 Set_Etype (S,
22411 Create_Null_Excluding_Itype
22412 (T => Entity (S),
22413 Related_Nod => P));
22414 Set_Entity (S, Etype (S));
22415 end if;
22417 return Entity (S);
22419 -- Case of constraint present, so that we have an N_Subtype_Indication
22420 -- node (this node is created only if constraints are present).
22422 else
22423 Find_Type (Subtype_Mark (S));
22425 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22426 and then not
22427 (Nkind (Parent (S)) = N_Subtype_Declaration
22428 and then Is_Itype (Defining_Identifier (Parent (S))))
22429 then
22430 Check_Incomplete (Subtype_Mark (S));
22431 end if;
22433 P := Parent (S);
22434 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22436 -- Explicit subtype declaration case
22438 if Nkind (P) = N_Subtype_Declaration then
22439 Def_Id := Defining_Identifier (P);
22441 -- Explicit derived type definition case
22443 elsif Nkind (P) = N_Derived_Type_Definition then
22444 Def_Id := Defining_Identifier (Parent (P));
22446 -- Implicit case, the Def_Id must be created as an implicit type.
22447 -- The one exception arises in the case of concurrent types, array
22448 -- and access types, where other subsidiary implicit types may be
22449 -- created and must appear before the main implicit type. In these
22450 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22451 -- has not yet been called to create Def_Id.
22453 else
22454 if Is_Array_Type (Subtype_Mark_Id)
22455 or else Is_Concurrent_Type (Subtype_Mark_Id)
22456 or else Is_Access_Type (Subtype_Mark_Id)
22457 then
22458 Def_Id := Empty;
22460 -- For the other cases, we create a new unattached Itype,
22461 -- and set the indication to ensure it gets attached later.
22463 else
22464 Def_Id :=
22465 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22466 end if;
22467 end if;
22469 -- If the kind of constraint is invalid for this kind of type,
22470 -- then give an error, and then pretend no constraint was given.
22472 if not Is_Valid_Constraint_Kind
22473 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22474 then
22475 Error_Msg_N
22476 ("incorrect constraint for this kind of type", Constraint (S));
22478 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22480 -- Set Ekind of orphan itype, to prevent cascaded errors
22482 if Present (Def_Id) then
22483 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22484 end if;
22486 -- Make recursive call, having got rid of the bogus constraint
22488 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22489 end if;
22491 -- Remaining processing depends on type. Select on Base_Type kind to
22492 -- ensure getting to the concrete type kind in the case of a private
22493 -- subtype (needed when only doing semantic analysis).
22495 case Ekind (Base_Type (Subtype_Mark_Id)) is
22496 when Access_Kind =>
22498 -- If this is a constraint on a class-wide type, discard it.
22499 -- There is currently no way to express a partial discriminant
22500 -- constraint on a type with unknown discriminants. This is
22501 -- a pathology that the ACATS wisely decides not to test.
22503 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22504 if Comes_From_Source (S) then
22505 Error_Msg_N
22506 ("constraint on class-wide type ignored??",
22507 Constraint (S));
22508 end if;
22510 if Nkind (P) = N_Subtype_Declaration then
22511 Set_Subtype_Indication (P,
22512 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22513 end if;
22515 return Subtype_Mark_Id;
22516 end if;
22518 Constrain_Access (Def_Id, S, Related_Nod);
22520 if Expander_Active
22521 and then Is_Itype (Designated_Type (Def_Id))
22522 and then Nkind (Related_Nod) = N_Subtype_Declaration
22523 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22524 then
22525 Build_Itype_Reference
22526 (Designated_Type (Def_Id), Related_Nod);
22527 end if;
22529 when Array_Kind =>
22530 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22532 when Decimal_Fixed_Point_Kind =>
22533 Constrain_Decimal (Def_Id, S);
22535 when Enumeration_Kind =>
22536 Constrain_Enumeration (Def_Id, S);
22538 when Ordinary_Fixed_Point_Kind =>
22539 Constrain_Ordinary_Fixed (Def_Id, S);
22541 when Float_Kind =>
22542 Constrain_Float (Def_Id, S);
22544 when Integer_Kind =>
22545 Constrain_Integer (Def_Id, S);
22547 when Class_Wide_Kind
22548 | E_Incomplete_Type
22549 | E_Record_Subtype
22550 | E_Record_Type
22552 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22554 if Ekind (Def_Id) = E_Incomplete_Type then
22555 Set_Private_Dependents (Def_Id, New_Elmt_List);
22556 end if;
22558 when Private_Kind =>
22560 -- A private type with unknown discriminants may be completed
22561 -- by an unconstrained array type.
22563 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22564 and then Present (Full_View (Subtype_Mark_Id))
22565 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22566 then
22567 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22569 -- ... but more commonly is completed by a discriminated record
22570 -- type.
22572 else
22573 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22574 end if;
22576 -- The base type may be private but Def_Id may be a full view
22577 -- in an instance.
22579 if Is_Private_Type (Def_Id) then
22580 Set_Private_Dependents (Def_Id, New_Elmt_List);
22581 end if;
22583 -- In case of an invalid constraint prevent further processing
22584 -- since the type constructed is missing expected fields.
22586 if Etype (Def_Id) = Any_Type then
22587 return Def_Id;
22588 end if;
22590 -- If the full view is that of a task with discriminants,
22591 -- we must constrain both the concurrent type and its
22592 -- corresponding record type. Otherwise we will just propagate
22593 -- the constraint to the full view, if available.
22595 if Present (Full_View (Subtype_Mark_Id))
22596 and then Has_Discriminants (Subtype_Mark_Id)
22597 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22598 then
22599 Full_View_Id :=
22600 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22602 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22603 Constrain_Concurrent (Full_View_Id, S,
22604 Related_Nod, Related_Id, Suffix);
22605 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22606 Set_Full_View (Def_Id, Full_View_Id);
22608 -- Introduce an explicit reference to the private subtype,
22609 -- to prevent scope anomalies in gigi if first use appears
22610 -- in a nested context, e.g. a later function body.
22611 -- Should this be generated in other contexts than a full
22612 -- type declaration?
22614 if Is_Itype (Def_Id)
22615 and then
22616 Nkind (Parent (P)) = N_Full_Type_Declaration
22617 then
22618 Build_Itype_Reference (Def_Id, Parent (P));
22619 end if;
22621 else
22622 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22623 end if;
22625 when Concurrent_Kind =>
22626 Constrain_Concurrent (Def_Id, S,
22627 Related_Nod, Related_Id, Suffix);
22629 when others =>
22630 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22631 end case;
22633 -- Size, Alignment, Representation aspects and Convention are always
22634 -- inherited from the base type.
22636 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22637 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22638 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22640 -- The anonymous subtype created for the subtype indication
22641 -- inherits the predicates of the parent.
22643 if Has_Predicates (Subtype_Mark_Id) then
22644 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22646 -- Indicate where the predicate function may be found
22648 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22649 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22650 end if;
22651 end if;
22653 return Def_Id;
22654 end if;
22655 end Process_Subtype;
22657 -----------------------------
22658 -- Record_Type_Declaration --
22659 -----------------------------
22661 procedure Record_Type_Declaration
22662 (T : Entity_Id;
22663 N : Node_Id;
22664 Prev : Entity_Id)
22666 Def : constant Node_Id := Type_Definition (N);
22667 Is_Tagged : Boolean;
22668 Tag_Comp : Entity_Id;
22670 begin
22671 -- These flags must be initialized before calling Process_Discriminants
22672 -- because this routine makes use of them.
22674 Mutate_Ekind (T, E_Record_Type);
22675 Set_Etype (T, T);
22676 Reinit_Size_Align (T);
22677 Set_Interfaces (T, No_Elist);
22678 Set_Stored_Constraint (T, No_Elist);
22679 Set_Default_SSO (T);
22680 Set_No_Reordering (T, No_Component_Reordering);
22682 -- Normal case
22684 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22685 -- The flag Is_Tagged_Type might have already been set by
22686 -- Find_Type_Name if it detected an error for declaration T. This
22687 -- arises in the case of private tagged types where the full view
22688 -- omits the word tagged.
22690 Is_Tagged :=
22691 Tagged_Present (Def)
22692 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22694 Set_Is_Limited_Record (T, Limited_Present (Def));
22696 if Is_Tagged then
22697 Set_Is_Tagged_Type (T, True);
22698 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22699 end if;
22701 -- Type is abstract if full declaration carries keyword, or if
22702 -- previous partial view did.
22704 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22705 or else Abstract_Present (Def));
22707 else
22708 Is_Tagged := True;
22709 Analyze_Interface_Declaration (T, Def);
22711 if Present (Discriminant_Specifications (N)) then
22712 Error_Msg_N
22713 ("interface types cannot have discriminants",
22714 Defining_Identifier
22715 (First (Discriminant_Specifications (N))));
22716 end if;
22717 end if;
22719 -- First pass: if there are self-referential access components,
22720 -- create the required anonymous access type declarations, and if
22721 -- need be an incomplete type declaration for T itself.
22723 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22725 if Ada_Version >= Ada_2005
22726 and then Present (Interface_List (Def))
22727 then
22728 Check_Interfaces (N, Def);
22730 declare
22731 Ifaces_List : Elist_Id;
22733 begin
22734 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22735 -- already in the parents.
22737 Collect_Interfaces
22738 (T => T,
22739 Ifaces_List => Ifaces_List,
22740 Exclude_Parents => True);
22742 Set_Interfaces (T, Ifaces_List);
22743 end;
22744 end if;
22746 -- Records constitute a scope for the component declarations within.
22747 -- The scope is created prior to the processing of these declarations.
22748 -- Discriminants are processed first, so that they are visible when
22749 -- processing the other components. The Ekind of the record type itself
22750 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22752 -- Enter record scope
22754 Push_Scope (T);
22756 -- If an incomplete or private type declaration was already given for
22757 -- the type, then this scope already exists, and the discriminants have
22758 -- been declared within. We must verify that the full declaration
22759 -- matches the incomplete one.
22761 Check_Or_Process_Discriminants (N, T, Prev);
22763 Set_Is_Constrained (T, not Has_Discriminants (T));
22764 Set_Has_Delayed_Freeze (T, True);
22766 -- For tagged types add a manually analyzed component corresponding
22767 -- to the component _tag, the corresponding piece of tree will be
22768 -- expanded as part of the freezing actions if it is not a CPP_Class.
22770 if Is_Tagged then
22772 -- Do not add the tag unless we are in expansion mode
22774 if Expander_Active then
22775 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22776 Enter_Name (Tag_Comp);
22778 Mutate_Ekind (Tag_Comp, E_Component);
22779 Set_Is_Tag (Tag_Comp);
22780 Set_Is_Aliased (Tag_Comp);
22781 Set_Is_Independent (Tag_Comp);
22782 Set_Etype (Tag_Comp, RTE (RE_Tag));
22783 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22784 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22785 Reinit_Component_Location (Tag_Comp);
22787 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22788 -- implemented interfaces.
22790 if Has_Interfaces (T) then
22791 Add_Interface_Tag_Components (N, T);
22792 end if;
22793 end if;
22795 Make_Class_Wide_Type (T);
22796 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22797 end if;
22799 -- We must suppress range checks when processing record components in
22800 -- the presence of discriminants, since we don't want spurious checks to
22801 -- be generated during their analysis, but Suppress_Range_Checks flags
22802 -- must be reset the after processing the record definition.
22804 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22805 -- couldn't we just use the normal range check suppression method here.
22806 -- That would seem cleaner ???
22808 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22809 Set_Kill_Range_Checks (T, True);
22810 Record_Type_Definition (Def, Prev);
22811 Set_Kill_Range_Checks (T, False);
22812 else
22813 Record_Type_Definition (Def, Prev);
22814 end if;
22816 -- Exit from record scope
22818 End_Scope;
22820 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22821 -- the implemented interfaces and associate them an aliased entity.
22823 if Is_Tagged
22824 and then not Is_Empty_List (Interface_List (Def))
22825 then
22826 Derive_Progenitor_Subprograms (T, T);
22827 end if;
22829 Check_Function_Writable_Actuals (N);
22830 end Record_Type_Declaration;
22832 ----------------------------
22833 -- Record_Type_Definition --
22834 ----------------------------
22836 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22837 Component : Entity_Id;
22838 Ctrl_Components : Boolean := False;
22839 Final_Storage_Only : Boolean;
22840 T : Entity_Id;
22842 begin
22843 if Ekind (Prev_T) = E_Incomplete_Type then
22844 T := Full_View (Prev_T);
22845 else
22846 T := Prev_T;
22847 end if;
22849 Set_Is_Not_Self_Hidden (T);
22851 Final_Storage_Only := not Is_Controlled (T);
22853 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22854 -- type declaration.
22856 if Parent_Kind (Def) = N_Derived_Type_Definition
22857 and then Limited_Present (Parent (Def))
22858 then
22859 Set_Is_Limited_Record (T);
22860 end if;
22862 -- If the component list of a record type is defined by the reserved
22863 -- word null and there is no discriminant part, then the record type has
22864 -- no components and all records of the type are null records (RM 3.7)
22865 -- This procedure is also called to process the extension part of a
22866 -- record extension, in which case the current scope may have inherited
22867 -- components.
22869 if Present (Def)
22870 and then Present (Component_List (Def))
22871 and then not Null_Present (Component_List (Def))
22872 then
22873 Analyze_Declarations (Component_Items (Component_List (Def)));
22875 if Present (Variant_Part (Component_List (Def))) then
22876 Analyze (Variant_Part (Component_List (Def)));
22877 end if;
22878 end if;
22880 -- After completing the semantic analysis of the record definition,
22881 -- record components, both new and inherited, are accessible. Set their
22882 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22883 -- whose Ekind may be void.
22885 Component := First_Entity (Current_Scope);
22886 while Present (Component) loop
22887 if Ekind (Component) = E_Void
22888 and then not Is_Itype (Component)
22889 then
22890 Mutate_Ekind (Component, E_Component);
22891 Reinit_Component_Location (Component);
22892 Set_Is_Not_Self_Hidden (Component);
22893 end if;
22895 Propagate_Concurrent_Flags (T, Etype (Component));
22897 if Ekind (Component) /= E_Component then
22898 null;
22900 -- Do not set Has_Controlled_Component on a class-wide equivalent
22901 -- type. See Make_CW_Equivalent_Type.
22903 elsif not Is_Class_Wide_Equivalent_Type (T)
22904 and then (Has_Controlled_Component (Etype (Component))
22905 or else (Chars (Component) /= Name_uParent
22906 and then Is_Controlled (Etype (Component))))
22907 then
22908 Set_Has_Controlled_Component (T, True);
22909 Final_Storage_Only :=
22910 Final_Storage_Only
22911 and then Finalize_Storage_Only (Etype (Component));
22912 Ctrl_Components := True;
22913 end if;
22915 Next_Entity (Component);
22916 end loop;
22918 -- A Type is Finalize_Storage_Only only if all its controlled components
22919 -- are also.
22921 if Ctrl_Components then
22922 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22923 end if;
22925 -- Place reference to end record on the proper entity, which may
22926 -- be a partial view.
22928 if Present (Def) then
22929 Process_End_Label (Def, 'e', Prev_T);
22930 end if;
22931 end Record_Type_Definition;
22933 ---------------------------
22934 -- Replace_Discriminants --
22935 ---------------------------
22937 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22938 function Process (N : Node_Id) return Traverse_Result;
22940 -------------
22941 -- Process --
22942 -------------
22944 function Process (N : Node_Id) return Traverse_Result is
22945 Comp : Entity_Id;
22947 begin
22948 if Nkind (N) = N_Discriminant_Specification then
22949 Comp := First_Discriminant (Typ);
22950 while Present (Comp) loop
22951 if Original_Record_Component (Comp) = Defining_Identifier (N)
22952 or else Chars (Comp) = Chars (Defining_Identifier (N))
22953 then
22954 Set_Defining_Identifier (N, Comp);
22955 exit;
22956 end if;
22958 Next_Discriminant (Comp);
22959 end loop;
22961 elsif Nkind (N) = N_Variant_Part then
22962 Comp := First_Discriminant (Typ);
22963 while Present (Comp) loop
22964 if Original_Record_Component (Comp) = Entity (Name (N))
22965 or else Chars (Comp) = Chars (Name (N))
22966 then
22967 -- Make sure to preserve the type coming from the parent on
22968 -- the Name, even if the subtype of the discriminant can be
22969 -- constrained, so that discrete choices inherited from the
22970 -- parent in the variant part are not flagged as violating
22971 -- the constraints of the subtype.
22973 declare
22974 Typ : constant Entity_Id := Etype (Name (N));
22975 begin
22976 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22977 Set_Etype (Name (N), Typ);
22978 end;
22979 exit;
22980 end if;
22982 Next_Discriminant (Comp);
22983 end loop;
22984 end if;
22986 return OK;
22987 end Process;
22989 procedure Replace is new Traverse_Proc (Process);
22991 -- Start of processing for Replace_Discriminants
22993 begin
22994 Replace (Decl);
22995 end Replace_Discriminants;
22997 -------------------------------
22998 -- Set_Completion_Referenced --
22999 -------------------------------
23001 procedure Set_Completion_Referenced (E : Entity_Id) is
23002 begin
23003 -- If in main unit, mark entity that is a completion as referenced,
23004 -- warnings go on the partial view when needed.
23006 if In_Extended_Main_Source_Unit (E) then
23007 Set_Referenced (E);
23008 end if;
23009 end Set_Completion_Referenced;
23011 ---------------------
23012 -- Set_Default_SSO --
23013 ---------------------
23015 procedure Set_Default_SSO (T : Entity_Id) is
23016 begin
23017 case Opt.Default_SSO is
23018 when ' ' =>
23019 null;
23020 when 'L' =>
23021 Set_SSO_Set_Low_By_Default (T, True);
23022 when 'H' =>
23023 Set_SSO_Set_High_By_Default (T, True);
23024 when others =>
23025 raise Program_Error;
23026 end case;
23027 end Set_Default_SSO;
23029 ---------------------
23030 -- Set_Fixed_Range --
23031 ---------------------
23033 -- The range for fixed-point types is complicated by the fact that we
23034 -- do not know the exact end points at the time of the declaration. This
23035 -- is true for three reasons:
23037 -- A size clause may affect the fudging of the end-points.
23038 -- A small clause may affect the values of the end-points.
23039 -- We try to include the end-points if it does not affect the size.
23041 -- This means that the actual end-points must be established at the
23042 -- point when the type is frozen. Meanwhile, we first narrow the range
23043 -- as permitted (so that it will fit if necessary in a small specified
23044 -- size), and then build a range subtree with these narrowed bounds.
23045 -- Set_Fixed_Range constructs the range from real literal values, and
23046 -- sets the range as the Scalar_Range of the given fixed-point type entity.
23048 -- The parent of this range is set to point to the entity so that it is
23049 -- properly hooked into the tree (unlike normal Scalar_Range entries for
23050 -- other scalar types, which are just pointers to the range in the
23051 -- original tree, this would otherwise be an orphan).
23053 -- The tree is left unanalyzed. When the type is frozen, the processing
23054 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
23055 -- analyzed, and uses this as an indication that it should complete
23056 -- work on the range (it will know the final small and size values).
23058 procedure Set_Fixed_Range
23059 (E : Entity_Id;
23060 Loc : Source_Ptr;
23061 Lo : Ureal;
23062 Hi : Ureal)
23064 S : constant Node_Id :=
23065 Make_Range (Loc,
23066 Low_Bound => Make_Real_Literal (Loc, Lo),
23067 High_Bound => Make_Real_Literal (Loc, Hi));
23068 begin
23069 Set_Scalar_Range (E, S);
23070 Set_Parent (S, E);
23072 -- Before the freeze point, the bounds of a fixed point are universal
23073 -- and carry the corresponding type.
23075 Set_Etype (Low_Bound (S), Universal_Real);
23076 Set_Etype (High_Bound (S), Universal_Real);
23077 end Set_Fixed_Range;
23079 ----------------------------------
23080 -- Set_Scalar_Range_For_Subtype --
23081 ----------------------------------
23083 procedure Set_Scalar_Range_For_Subtype
23084 (Def_Id : Entity_Id;
23085 R : Node_Id;
23086 Subt : Entity_Id)
23088 Kind : constant Entity_Kind := Ekind (Def_Id);
23090 begin
23091 -- Defend against previous error
23093 if Nkind (R) = N_Error then
23094 return;
23095 end if;
23097 Set_Scalar_Range (Def_Id, R);
23099 -- We need to link the range into the tree before resolving it so
23100 -- that types that are referenced, including importantly the subtype
23101 -- itself, are properly frozen (Freeze_Expression requires that the
23102 -- expression be properly linked into the tree). Of course if it is
23103 -- already linked in, then we do not disturb the current link.
23105 if No (Parent (R)) then
23106 Set_Parent (R, Def_Id);
23107 end if;
23109 -- Reset the kind of the subtype during analysis of the range, to
23110 -- catch possible premature use in the bounds themselves.
23112 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23113 pragma Assert (Ekind (Def_Id) = Kind);
23114 end Set_Scalar_Range_For_Subtype;
23116 --------------------------------------------------------
23117 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23118 --------------------------------------------------------
23120 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23121 (E : Entity_Id)
23123 begin
23124 -- Make sure set if encountered during Expand_To_Stored_Constraint
23126 Set_Stored_Constraint (E, No_Elist);
23128 -- Give it the right value
23130 if Is_Constrained (E) and then Has_Discriminants (E) then
23131 Set_Stored_Constraint (E,
23132 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23133 end if;
23134 end Set_Stored_Constraint_From_Discriminant_Constraint;
23136 -------------------------------------
23137 -- Signed_Integer_Type_Declaration --
23138 -------------------------------------
23140 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23141 Implicit_Base : Entity_Id;
23142 Base_Typ : Entity_Id;
23143 Lo_Val : Uint;
23144 Hi_Val : Uint;
23145 Errs : Boolean := False;
23146 Lo : Node_Id;
23147 Hi : Node_Id;
23149 function Can_Derive_From (E : Entity_Id) return Boolean;
23150 -- Determine whether given bounds allow derivation from specified type
23152 procedure Check_Bound (Expr : Node_Id);
23153 -- Check bound to make sure it is integral and static. If not, post
23154 -- appropriate error message and set Errs flag
23156 ---------------------
23157 -- Can_Derive_From --
23158 ---------------------
23160 -- Note we check both bounds against both end values, to deal with
23161 -- strange types like ones with a range of 0 .. -12341234.
23163 function Can_Derive_From (E : Entity_Id) return Boolean is
23164 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23165 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23166 begin
23167 return Lo <= Lo_Val and then Lo_Val <= Hi
23168 and then
23169 Lo <= Hi_Val and then Hi_Val <= Hi;
23170 end Can_Derive_From;
23172 -----------------
23173 -- Check_Bound --
23174 -----------------
23176 procedure Check_Bound (Expr : Node_Id) is
23177 begin
23178 -- If a range constraint is used as an integer type definition, each
23179 -- bound of the range must be defined by a static expression of some
23180 -- integer type, but the two bounds need not have the same integer
23181 -- type (Negative bounds are allowed.) (RM 3.5.4)
23183 if not Is_Integer_Type (Etype (Expr)) then
23184 Error_Msg_N
23185 ("integer type definition bounds must be of integer type", Expr);
23186 Errs := True;
23188 elsif not Is_OK_Static_Expression (Expr) then
23189 Flag_Non_Static_Expr
23190 ("non-static expression used for integer type bound!", Expr);
23191 Errs := True;
23193 -- Otherwise the bounds are folded into literals
23195 elsif Is_Entity_Name (Expr) then
23196 Fold_Uint (Expr, Expr_Value (Expr), True);
23197 end if;
23198 end Check_Bound;
23200 -- Start of processing for Signed_Integer_Type_Declaration
23202 begin
23203 -- Create an anonymous base type
23205 Implicit_Base :=
23206 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23208 -- Analyze and check the bounds, they can be of any integer type
23210 Lo := Low_Bound (Def);
23211 Hi := High_Bound (Def);
23213 -- Arbitrarily use Integer as the type if either bound had an error
23215 if Hi = Error or else Lo = Error then
23216 Base_Typ := Any_Integer;
23217 Set_Error_Posted (T, True);
23218 Errs := True;
23220 -- Here both bounds are OK expressions
23222 else
23223 Analyze_And_Resolve (Lo, Any_Integer);
23224 Analyze_And_Resolve (Hi, Any_Integer);
23226 Check_Bound (Lo);
23227 Check_Bound (Hi);
23229 if Errs then
23230 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23231 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23232 end if;
23234 -- Find type to derive from
23236 Lo_Val := Expr_Value (Lo);
23237 Hi_Val := Expr_Value (Hi);
23239 if Can_Derive_From (Standard_Short_Short_Integer) then
23240 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23242 elsif Can_Derive_From (Standard_Short_Integer) then
23243 Base_Typ := Base_Type (Standard_Short_Integer);
23245 elsif Can_Derive_From (Standard_Integer) then
23246 Base_Typ := Base_Type (Standard_Integer);
23248 elsif Can_Derive_From (Standard_Long_Integer) then
23249 Base_Typ := Base_Type (Standard_Long_Integer);
23251 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23252 Check_Restriction (No_Long_Long_Integers, Def);
23253 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23255 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23256 Check_Restriction (No_Long_Long_Integers, Def);
23257 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23259 else
23260 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23261 Error_Msg_N ("integer type definition bounds out of range", Def);
23262 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23263 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23264 end if;
23265 end if;
23267 -- Set the type of the bounds to the implicit base: we cannot set it to
23268 -- the new type, because this would be a forward reference for the code
23269 -- generator and, if the original type is user-defined, this could even
23270 -- lead to spurious semantic errors. Furthermore we do not set it to be
23271 -- universal, because this could make it much larger than needed here.
23273 if not Errs then
23274 Set_Etype (Lo, Implicit_Base);
23275 Set_Etype (Hi, Implicit_Base);
23276 end if;
23278 -- Complete both implicit base and declared first subtype entities. The
23279 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23280 -- are not clobbered when the signed integer type acts as a full view of
23281 -- a private type.
23283 Set_Etype (Implicit_Base, Base_Typ);
23284 Set_Size_Info (Implicit_Base, Base_Typ);
23285 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23286 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23287 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23289 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23290 Set_Etype (T, Implicit_Base);
23291 Set_Size_Info (T, Implicit_Base);
23292 Inherit_Rep_Item_Chain (T, Implicit_Base);
23293 Set_Scalar_Range (T, Def);
23294 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23295 Set_Is_Constrained (T);
23296 end Signed_Integer_Type_Declaration;
23298 end Sem_Ch3;