Skip several gcc.dg/builtin-dynamic-object-size tests on hppa*-*-hpux*
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob70cf772edcc211123d5aa0ef624e912f3008e045
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-2023, 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_Master 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 masters.
1474 if No (Associated_Storage_Pool (T)) then
1475 Set_Finalization_Master (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 Contains_POC (Constr : Node_Id) return Boolean;
1964 -- Determines whether a constraint uses the discriminant of a record
1965 -- type thus becoming a per-object constraint (POC).
1967 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1968 -- Typ is the type of the current component, check whether this type is
1969 -- a limited type. Used to validate declaration against that of
1970 -- enclosing record.
1972 ------------------
1973 -- Contains_POC --
1974 ------------------
1976 function Contains_POC (Constr : Node_Id) return Boolean is
1977 begin
1978 -- Prevent cascaded errors
1980 if Error_Posted (Constr) then
1981 return False;
1982 end if;
1984 case Nkind (Constr) is
1985 when N_Attribute_Reference =>
1986 return Attribute_Name (Constr) = Name_Access
1987 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1989 when N_Discriminant_Association =>
1990 return Denotes_Discriminant (Expression (Constr));
1992 when N_Identifier =>
1993 return Denotes_Discriminant (Constr);
1995 when N_Index_Or_Discriminant_Constraint =>
1996 declare
1997 IDC : Node_Id;
1999 begin
2000 IDC := First (Constraints (Constr));
2001 while Present (IDC) loop
2003 -- One per-object constraint is sufficient
2005 if Contains_POC (IDC) then
2006 return True;
2007 end if;
2009 Next (IDC);
2010 end loop;
2012 return False;
2013 end;
2015 when N_Range =>
2016 return Denotes_Discriminant (Low_Bound (Constr))
2017 or else
2018 Denotes_Discriminant (High_Bound (Constr));
2020 when N_Range_Constraint =>
2021 return Denotes_Discriminant (Range_Expression (Constr));
2023 when others =>
2024 return False;
2025 end case;
2026 end Contains_POC;
2028 ----------------------
2029 -- Is_Known_Limited --
2030 ----------------------
2032 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
2033 P : constant Entity_Id := Etype (Typ);
2034 R : constant Entity_Id := Root_Type (Typ);
2036 begin
2037 if Is_Limited_Record (Typ) then
2038 return True;
2040 -- If the root type is limited (and not a limited interface) so is
2041 -- the current type.
2043 elsif Is_Limited_Record (R)
2044 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
2045 then
2046 return True;
2048 -- Else the type may have a limited interface progenitor, but a
2049 -- limited record parent that is not an interface.
2051 elsif R /= P
2052 and then Is_Limited_Record (P)
2053 and then not Is_Interface (P)
2054 then
2055 return True;
2057 else
2058 return False;
2059 end if;
2060 end Is_Known_Limited;
2062 -- Start of processing for Analyze_Component_Declaration
2064 begin
2065 Generate_Definition (Id);
2066 Enter_Name (Id);
2068 if Present (Typ) then
2069 T := Find_Type_Of_Object
2070 (Subtype_Indication (Component_Definition (N)), N);
2072 -- Ada 2005 (AI-230): Access Definition case
2074 else
2075 pragma Assert (Present
2076 (Access_Definition (Component_Definition (N))));
2078 T := Access_Definition
2079 (Related_Nod => N,
2080 N => Access_Definition (Component_Definition (N)));
2081 Set_Is_Local_Anonymous_Access (T);
2083 -- Ada 2005 (AI-254)
2085 if Present (Access_To_Subprogram_Definition
2086 (Access_Definition (Component_Definition (N))))
2087 and then Protected_Present (Access_To_Subprogram_Definition
2088 (Access_Definition
2089 (Component_Definition (N))))
2090 then
2091 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2092 end if;
2093 end if;
2095 -- If the subtype is a constrained subtype of the enclosing record,
2096 -- (which must have a partial view) the back-end does not properly
2097 -- handle the recursion. Rewrite the component declaration with an
2098 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2099 -- the tree directly because side effects have already been removed from
2100 -- discriminant constraints.
2102 if Ekind (T) = E_Access_Subtype
2103 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2104 and then Comes_From_Source (T)
2105 and then Nkind (Parent (T)) = N_Subtype_Declaration
2106 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2107 then
2108 Rewrite
2109 (Subtype_Indication (Component_Definition (N)),
2110 New_Copy_Tree (Subtype_Indication (Parent (T))));
2111 T := Find_Type_Of_Object
2112 (Subtype_Indication (Component_Definition (N)), N);
2113 end if;
2115 -- If the component declaration includes a default expression, then we
2116 -- check that the component is not of a limited type (RM 3.7(5)),
2117 -- and do the special preanalysis of the expression (see section on
2118 -- "Handling of Default and Per-Object Expressions" in the spec of
2119 -- package Sem).
2121 if Present (E) then
2122 Preanalyze_Default_Expression (E, T);
2123 Check_Initialization (T, E);
2125 if Ada_Version >= Ada_2005
2126 and then Ekind (T) = E_Anonymous_Access_Type
2127 and then Etype (E) /= Any_Type
2128 then
2129 -- Check RM 3.9.2(9): "if the expected type for an expression is
2130 -- an anonymous access-to-specific tagged type, then the object
2131 -- designated by the expression shall not be dynamically tagged
2132 -- unless it is a controlling operand in a call on a dispatching
2133 -- operation"
2135 if Is_Tagged_Type (Directly_Designated_Type (T))
2136 and then
2137 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2138 and then
2139 Ekind (Directly_Designated_Type (Etype (E))) =
2140 E_Class_Wide_Type
2141 then
2142 Error_Msg_N
2143 ("access to specific tagged type required (RM 3.9.2(9))", E);
2144 end if;
2146 -- (Ada 2005: AI-230): Accessibility check for anonymous
2147 -- components
2149 if Type_Access_Level (Etype (E)) >
2150 Deepest_Type_Access_Level (T)
2151 then
2152 Error_Msg_N
2153 ("expression has deeper access level than component " &
2154 "(RM 3.10.2 (12.2))", E);
2155 end if;
2157 -- The initialization expression is a reference to an access
2158 -- discriminant. The type of the discriminant is always deeper
2159 -- than any access type.
2161 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2162 and then Is_Entity_Name (E)
2163 and then Ekind (Entity (E)) = E_In_Parameter
2164 and then Present (Discriminal_Link (Entity (E)))
2165 then
2166 Error_Msg_N
2167 ("discriminant has deeper accessibility level than target",
2169 end if;
2170 end if;
2171 end if;
2173 -- The parent type may be a private view with unknown discriminants,
2174 -- and thus unconstrained. Regular components must be constrained.
2176 if not Is_Definite_Subtype (T)
2177 and then Chars (Id) /= Name_uParent
2178 then
2179 if Is_Class_Wide_Type (T) then
2180 Error_Msg_N
2181 ("class-wide subtype with unknown discriminants" &
2182 " in component declaration",
2183 Subtype_Indication (Component_Definition (N)));
2184 else
2185 Error_Msg_N
2186 ("unconstrained subtype in component declaration",
2187 Subtype_Indication (Component_Definition (N)));
2188 end if;
2190 -- Components cannot be abstract, except for the special case of
2191 -- the _Parent field (case of extending an abstract tagged type)
2193 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2194 Error_Msg_N ("type of a component cannot be abstract", N);
2195 end if;
2197 Set_Etype (Id, T);
2199 if Aliased_Present (Component_Definition (N)) then
2200 Set_Is_Aliased (Id);
2202 -- AI12-001: All aliased objects are considered to be specified as
2203 -- independently addressable (RM C.6(8.1/4)).
2205 Set_Is_Independent (Id);
2206 end if;
2208 -- The component declaration may have a per-object constraint, set
2209 -- the appropriate flag in the defining identifier of the subtype.
2211 if Present (Subtype_Indication (Component_Definition (N))) then
2212 declare
2213 Sindic : constant Node_Id :=
2214 Subtype_Indication (Component_Definition (N));
2215 begin
2216 if Nkind (Sindic) = N_Subtype_Indication
2217 and then Present (Constraint (Sindic))
2218 and then Contains_POC (Constraint (Sindic))
2219 then
2220 Set_Has_Per_Object_Constraint (Id);
2221 end if;
2222 end;
2223 end if;
2225 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2226 -- out some static checks.
2228 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2229 Null_Exclusion_Static_Checks (N);
2230 end if;
2232 -- If this component is private (or depends on a private type), flag the
2233 -- record type to indicate that some operations are not available.
2235 P := Private_Component (T);
2237 if Present (P) then
2239 -- Check for circular definitions
2241 if P = Any_Type then
2242 Set_Etype (Id, Any_Type);
2244 -- There is a gap in the visibility of operations only if the
2245 -- component type is not defined in the scope of the record type.
2247 elsif Scope (P) = Scope (Current_Scope) then
2248 null;
2250 elsif Is_Limited_Type (P) then
2251 Set_Is_Limited_Composite (Current_Scope);
2253 else
2254 Set_Is_Private_Composite (Current_Scope);
2255 end if;
2256 end if;
2258 if P /= Any_Type
2259 and then Is_Limited_Type (T)
2260 and then Chars (Id) /= Name_uParent
2261 and then Is_Tagged_Type (Current_Scope)
2262 then
2263 if Is_Derived_Type (Current_Scope)
2264 and then not Is_Known_Limited (Current_Scope)
2265 then
2266 Error_Msg_N
2267 ("extension of nonlimited type cannot have limited components",
2270 if Is_Interface (Root_Type (Current_Scope)) then
2271 Error_Msg_N
2272 ("\limitedness is not inherited from limited interface", N);
2273 Error_Msg_N ("\add LIMITED to type indication", N);
2274 end if;
2276 Explain_Limited_Type (T, N);
2277 Set_Etype (Id, Any_Type);
2278 Set_Is_Limited_Composite (Current_Scope, False);
2280 elsif not Is_Derived_Type (Current_Scope)
2281 and then not Is_Limited_Record (Current_Scope)
2282 and then not Is_Concurrent_Type (Current_Scope)
2283 then
2284 Error_Msg_N
2285 ("nonlimited tagged type cannot have limited components", N);
2286 Explain_Limited_Type (T, N);
2287 Set_Etype (Id, Any_Type);
2288 Set_Is_Limited_Composite (Current_Scope, False);
2289 end if;
2290 end if;
2292 Set_Original_Record_Component (Id, Id);
2294 Analyze_Aspect_Specifications (N, Id);
2296 Analyze_Dimension (N);
2297 end Analyze_Component_Declaration;
2299 --------------------------
2300 -- Analyze_Declarations --
2301 --------------------------
2303 procedure Analyze_Declarations (L : List_Id) is
2304 Decl : Node_Id;
2306 procedure Adjust_Decl;
2307 -- Adjust Decl not to include implicit label declarations, since these
2308 -- have strange Sloc values that result in elaboration check problems.
2309 -- (They have the sloc of the label as found in the source, and that
2310 -- is ahead of the current declarative part).
2312 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2313 -- Create the subprogram bodies which verify the run-time semantics of
2314 -- the pragmas listed below for each elibigle type found in declarative
2315 -- list Decls. The pragmas are:
2317 -- Default_Initial_Condition
2318 -- Invariant
2319 -- Type_Invariant
2321 -- Context denotes the owner of the declarative list.
2323 procedure Check_Entry_Contracts;
2324 -- Perform a preanalysis of the pre- and postconditions of an entry
2325 -- declaration. This must be done before full resolution and creation
2326 -- of the parameter block, etc. to catch illegal uses within the
2327 -- contract expression. Full analysis of the expression is done when
2328 -- the contract is processed.
2330 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2331 -- Check if a nested package has entities within it that rely on library
2332 -- level private types where the full view has not been completed for
2333 -- the purposes of checking if it is acceptable to freeze an expression
2334 -- function at the point of declaration.
2336 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2337 -- Determine whether Body_Decl denotes the body of a late controlled
2338 -- primitive (either Initialize, Adjust or Finalize). If this is the
2339 -- case, add a proper spec if the body lacks one. The spec is inserted
2340 -- before Body_Decl and immediately analyzed.
2342 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2343 -- Spec_Id is the entity of a package that may define abstract states,
2344 -- and in the case of a child unit, whose ancestors may define abstract
2345 -- states. If the states have partial visible refinement, remove the
2346 -- partial visibility of each constituent at the end of the package
2347 -- spec and body declarations.
2349 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2350 -- Spec_Id is the entity of a package that may define abstract states.
2351 -- If the states have visible refinement, remove the visibility of each
2352 -- constituent at the end of the package body declaration.
2354 procedure Resolve_Aspects;
2355 -- Utility to resolve the expressions of aspects at the end of a list of
2356 -- declarations, or before a declaration that freezes previous entities,
2357 -- such as in a subprogram body.
2359 -----------------
2360 -- Adjust_Decl --
2361 -----------------
2363 procedure Adjust_Decl is
2364 begin
2365 while Present (Prev (Decl))
2366 and then Nkind (Decl) = N_Implicit_Label_Declaration
2367 loop
2368 Prev (Decl);
2369 end loop;
2370 end Adjust_Decl;
2372 ----------------------------
2373 -- Build_Assertion_Bodies --
2374 ----------------------------
2376 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2377 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2378 -- Create the subprogram bodies which verify the run-time semantics
2379 -- of the pragmas listed below for type Typ. The pragmas are:
2381 -- Default_Initial_Condition
2382 -- Invariant
2383 -- Type_Invariant
2385 -------------------------------------
2386 -- Build_Assertion_Bodies_For_Type --
2387 -------------------------------------
2389 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2390 begin
2391 if Nkind (Context) = N_Package_Specification then
2393 -- Preanalyze and resolve the class-wide invariants of an
2394 -- interface at the end of whichever declarative part has the
2395 -- interface type. Note that an interface may be declared in
2396 -- any non-package declarative part, but reaching the end of
2397 -- such a declarative part will always freeze the type and
2398 -- generate the invariant procedure (see Freeze_Type).
2400 if Is_Interface (Typ) then
2402 -- Interfaces are treated as the partial view of a private
2403 -- type, in order to achieve uniformity with the general
2404 -- case. As a result, an interface receives only a "partial"
2405 -- invariant procedure, which is never called.
2407 if Has_Own_Invariants (Typ) then
2408 Build_Invariant_Procedure_Body
2409 (Typ => Typ,
2410 Partial_Invariant => True);
2411 end if;
2413 elsif Decls = Visible_Declarations (Context) then
2414 -- Preanalyze and resolve the invariants of a private type
2415 -- at the end of the visible declarations to catch potential
2416 -- errors. Inherited class-wide invariants are not included
2417 -- because they have already been resolved.
2419 if Ekind (Typ) in E_Limited_Private_Type
2420 | E_Private_Type
2421 | E_Record_Type_With_Private
2422 and then Has_Own_Invariants (Typ)
2423 then
2424 Build_Invariant_Procedure_Body
2425 (Typ => Typ,
2426 Partial_Invariant => True);
2427 end if;
2429 -- Preanalyze and resolve the Default_Initial_Condition
2430 -- assertion expression at the end of the declarations to
2431 -- catch any errors.
2433 if Ekind (Typ) in E_Limited_Private_Type
2434 | E_Private_Type
2435 | E_Record_Type_With_Private
2436 and then Has_Own_DIC (Typ)
2437 then
2438 Build_DIC_Procedure_Body
2439 (Typ => Typ,
2440 Partial_DIC => True);
2441 end if;
2443 elsif Decls = Private_Declarations (Context) then
2445 -- Preanalyze and resolve the invariants of a private type's
2446 -- full view at the end of the private declarations to catch
2447 -- potential errors.
2449 if (not Is_Private_Type (Typ)
2450 or else Present (Underlying_Full_View (Typ)))
2451 and then Has_Private_Declaration (Typ)
2452 and then Has_Invariants (Typ)
2453 then
2454 Build_Invariant_Procedure_Body (Typ);
2455 end if;
2457 if (not Is_Private_Type (Typ)
2458 or else Present (Underlying_Full_View (Typ)))
2459 and then Has_Private_Declaration (Typ)
2460 and then Has_DIC (Typ)
2461 then
2462 Build_DIC_Procedure_Body (Typ);
2463 end if;
2464 end if;
2465 end if;
2466 end Build_Assertion_Bodies_For_Type;
2468 -- Local variables
2470 Decl : Node_Id;
2471 Decl_Id : Entity_Id;
2473 -- Start of processing for Build_Assertion_Bodies
2475 begin
2476 Decl := First (Decls);
2477 while Present (Decl) loop
2478 if Is_Declaration (Decl) then
2479 Decl_Id := Defining_Entity (Decl);
2481 if Is_Type (Decl_Id) then
2482 Build_Assertion_Bodies_For_Type (Decl_Id);
2483 end if;
2484 end if;
2486 Next (Decl);
2487 end loop;
2488 end Build_Assertion_Bodies;
2490 ---------------------------
2491 -- Check_Entry_Contracts --
2492 ---------------------------
2494 procedure Check_Entry_Contracts is
2495 ASN : Node_Id;
2496 Ent : Entity_Id;
2497 Exp : Node_Id;
2499 begin
2500 Ent := First_Entity (Current_Scope);
2501 while Present (Ent) loop
2503 -- This only concerns entries with pre/postconditions
2505 if Ekind (Ent) = E_Entry
2506 and then Present (Contract (Ent))
2507 and then Present (Pre_Post_Conditions (Contract (Ent)))
2508 then
2509 ASN := Pre_Post_Conditions (Contract (Ent));
2510 Push_Scope (Ent);
2511 Install_Formals (Ent);
2513 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2514 -- is performed on a copy of the pragma expression, to prevent
2515 -- modifying the original expression.
2517 while Present (ASN) loop
2518 if Nkind (ASN) = N_Pragma then
2519 Exp :=
2520 New_Copy_Tree
2521 (Expression
2522 (First (Pragma_Argument_Associations (ASN))));
2523 Set_Parent (Exp, ASN);
2525 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2526 end if;
2528 ASN := Next_Pragma (ASN);
2529 end loop;
2531 End_Scope;
2532 end if;
2534 Next_Entity (Ent);
2535 end loop;
2536 end Check_Entry_Contracts;
2538 ----------------------------------
2539 -- Contains_Lib_Incomplete_Type --
2540 ----------------------------------
2542 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2543 Curr : Entity_Id;
2545 begin
2546 -- Avoid looking through scopes that do not meet the precondition of
2547 -- Pkg not being within a library unit spec.
2549 if not Is_Compilation_Unit (Pkg)
2550 and then not Is_Generic_Instance (Pkg)
2551 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2552 then
2553 -- Loop through all entities in the current scope to identify
2554 -- an entity that depends on a private type.
2556 Curr := First_Entity (Pkg);
2557 loop
2558 if Nkind (Curr) in N_Entity
2559 and then Depends_On_Private (Curr)
2560 then
2561 return True;
2562 end if;
2564 exit when Last_Entity (Current_Scope) = Curr;
2565 Next_Entity (Curr);
2566 end loop;
2567 end if;
2569 return False;
2570 end Contains_Lib_Incomplete_Type;
2572 --------------------------------------
2573 -- Handle_Late_Controlled_Primitive --
2574 --------------------------------------
2576 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2577 Body_Spec : constant Node_Id := Specification (Body_Decl);
2578 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2579 Loc : constant Source_Ptr := Sloc (Body_Id);
2580 Params : constant List_Id :=
2581 Parameter_Specifications (Body_Spec);
2582 Spec : Node_Id;
2583 Spec_Id : Entity_Id;
2584 Typ : Node_Id;
2586 begin
2587 -- Consider only procedure bodies whose name matches one of the three
2588 -- controlled primitives.
2590 if Nkind (Body_Spec) /= N_Procedure_Specification
2591 or else Chars (Body_Id) not in Name_Adjust
2592 | Name_Finalize
2593 | Name_Initialize
2594 then
2595 return;
2597 -- A controlled primitive must have exactly one formal which is not
2598 -- an anonymous access type.
2600 elsif List_Length (Params) /= 1 then
2601 return;
2602 end if;
2604 Typ := Parameter_Type (First (Params));
2606 if Nkind (Typ) = N_Access_Definition then
2607 return;
2608 end if;
2610 Find_Type (Typ);
2612 -- The type of the formal must be derived from [Limited_]Controlled
2614 if not Is_Controlled (Entity (Typ)) then
2615 return;
2616 end if;
2618 -- Check whether a specification exists for this body. We do not
2619 -- analyze the spec of the body in full, because it will be analyzed
2620 -- again when the body is properly analyzed, and we cannot create
2621 -- duplicate entries in the formals chain. We look for an explicit
2622 -- specification because the body may be an overriding operation and
2623 -- an inherited spec may be present.
2625 Spec_Id := Current_Entity (Body_Id);
2627 while Present (Spec_Id) loop
2628 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2629 and then Scope (Spec_Id) = Current_Scope
2630 and then Present (First_Formal (Spec_Id))
2631 and then No (Next_Formal (First_Formal (Spec_Id)))
2632 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2633 and then Comes_From_Source (Spec_Id)
2634 then
2635 return;
2636 end if;
2638 Spec_Id := Homonym (Spec_Id);
2639 end loop;
2641 -- At this point the body is known to be a late controlled primitive.
2642 -- Generate a matching spec and insert it before the body. Note the
2643 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2644 -- tree in this case.
2646 Spec := Copy_Separate_Tree (Body_Spec);
2648 -- Ensure that the subprogram declaration does not inherit the null
2649 -- indicator from the body as we now have a proper spec/body pair.
2651 Set_Null_Present (Spec, False);
2653 -- Ensure that the freeze node is inserted after the declaration of
2654 -- the primitive since its expansion will freeze the primitive.
2656 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2658 Insert_Before_And_Analyze (Body_Decl, Decl);
2659 end Handle_Late_Controlled_Primitive;
2661 ----------------------------------------
2662 -- Remove_Partial_Visible_Refinements --
2663 ----------------------------------------
2665 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2666 State_Elmt : Elmt_Id;
2667 begin
2668 if Present (Abstract_States (Spec_Id)) then
2669 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2670 while Present (State_Elmt) loop
2671 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2672 Next_Elmt (State_Elmt);
2673 end loop;
2674 end if;
2676 -- For a child unit, also hide the partial state refinement from
2677 -- ancestor packages.
2679 if Is_Child_Unit (Spec_Id) then
2680 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2681 end if;
2682 end Remove_Partial_Visible_Refinements;
2684 --------------------------------
2685 -- Remove_Visible_Refinements --
2686 --------------------------------
2688 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2689 State_Elmt : Elmt_Id;
2690 begin
2691 if Present (Abstract_States (Spec_Id)) then
2692 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2693 while Present (State_Elmt) loop
2694 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2695 Next_Elmt (State_Elmt);
2696 end loop;
2697 end if;
2698 end Remove_Visible_Refinements;
2700 ---------------------
2701 -- Resolve_Aspects --
2702 ---------------------
2704 procedure Resolve_Aspects is
2705 E : Entity_Id;
2707 begin
2708 E := First_Entity (Current_Scope);
2709 while Present (E) loop
2710 Resolve_Aspect_Expressions (E);
2712 -- Now that the aspect expressions have been resolved, if this is
2713 -- at the end of the visible declarations, we can set the flag
2714 -- Known_To_Have_Preelab_Init properly on types declared in the
2715 -- visible part, which is needed for checking whether full types
2716 -- in the private part satisfy the Preelaborable_Initialization
2717 -- aspect of the partial view. We can't wait for the creation of
2718 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2719 -- freeze point may occur after the end of the package declaration
2720 -- (in the case of nested packages).
2722 if Is_Type (E)
2723 and then L = Visible_Declarations (Parent (L))
2724 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2725 then
2726 declare
2727 ASN : constant Node_Id :=
2728 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2729 Expr : constant Node_Id := Expression (ASN);
2730 begin
2731 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2732 -- expression, or if the expression is True (or was folded
2733 -- to True), or if the expression is a conjunction of one or
2734 -- more Preelaborable_Initialization attributes applied to
2735 -- formal types and wasn't folded to False. (Note that
2736 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2737 -- Original_Node if needed, hence test for Standard_False.)
2739 if No (Expr)
2740 or else (Is_Entity_Name (Expr)
2741 and then Entity (Expr) = Standard_True)
2742 or else
2743 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2744 and then
2745 not (Is_Entity_Name (Expr)
2746 and then Entity (Expr) = Standard_False))
2747 then
2748 Set_Known_To_Have_Preelab_Init (E);
2749 end if;
2750 end;
2751 end if;
2753 Next_Entity (E);
2754 end loop;
2755 end Resolve_Aspects;
2757 -- Local variables
2759 Context : Node_Id := Empty;
2760 Ctrl_Typ : Entity_Id := Empty;
2761 Freeze_From : Entity_Id := Empty;
2762 Next_Decl : Node_Id;
2764 -- Start of processing for Analyze_Declarations
2766 begin
2767 Decl := First (L);
2768 while Present (Decl) loop
2770 -- Complete analysis of declaration
2772 Analyze (Decl);
2773 Next_Decl := Next (Decl);
2775 if No (Freeze_From) then
2776 Freeze_From := First_Entity (Current_Scope);
2777 end if;
2779 -- Remember if the declaration we just processed is the full type
2780 -- declaration of a controlled type (to handle late overriding of
2781 -- initialize, adjust or finalize).
2783 if Nkind (Decl) = N_Full_Type_Declaration
2784 and then Is_Controlled (Defining_Identifier (Decl))
2785 then
2786 Ctrl_Typ := Defining_Identifier (Decl);
2787 end if;
2789 -- At the end of a declarative part, freeze remaining entities
2790 -- declared in it. The end of the visible declarations of package
2791 -- specification is not the end of a declarative part if private
2792 -- declarations are present. The end of a package declaration is a
2793 -- freezing point only if it a library package. A task definition or
2794 -- protected type definition is not a freeze point either. Finally,
2795 -- we do not freeze entities in generic scopes, because there is no
2796 -- code generated for them and freeze nodes will be generated for
2797 -- the instance.
2799 -- The end of a package instantiation is not a freeze point, but
2800 -- for now we make it one, because the generic body is inserted
2801 -- (currently) immediately after. Generic instantiations will not
2802 -- be a freeze point once delayed freezing of bodies is implemented.
2803 -- (This is needed in any case for early instantiations ???).
2805 if No (Next_Decl) then
2806 if Nkind (Parent (L)) = N_Component_List then
2807 null;
2809 elsif Nkind (Parent (L)) in
2810 N_Protected_Definition | N_Task_Definition
2811 then
2812 Check_Entry_Contracts;
2814 elsif Nkind (Parent (L)) /= N_Package_Specification then
2815 if Nkind (Parent (L)) = N_Package_Body then
2816 Freeze_From := First_Entity (Current_Scope);
2817 end if;
2819 -- There may have been several freezing points previously,
2820 -- for example object declarations or subprogram bodies, but
2821 -- at the end of a declarative part we check freezing from
2822 -- the beginning, even though entities may already be frozen,
2823 -- in order to perform visibility checks on delayed aspects.
2825 Adjust_Decl;
2827 -- If the current scope is a generic subprogram body. Skip the
2828 -- generic formal parameters that are not frozen here.
2830 if Is_Subprogram (Current_Scope)
2831 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2832 N_Generic_Subprogram_Declaration
2833 and then Present (First_Entity (Current_Scope))
2834 then
2835 while Is_Generic_Formal (Freeze_From) loop
2836 Next_Entity (Freeze_From);
2837 end loop;
2839 Freeze_All (Freeze_From, Decl);
2840 Freeze_From := Last_Entity (Current_Scope);
2842 else
2843 -- For declarations in a subprogram body there is no issue
2844 -- with name resolution in aspect specifications.
2846 Freeze_All (First_Entity (Current_Scope), Decl);
2847 Freeze_From := Last_Entity (Current_Scope);
2848 end if;
2850 -- Current scope is a package specification
2852 elsif Scope (Current_Scope) /= Standard_Standard
2853 and then not Is_Child_Unit (Current_Scope)
2854 and then No (Generic_Parent (Parent (L)))
2855 then
2856 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2857 -- resolved at the end of the immediately enclosing declaration
2858 -- list (AI05-0183-1).
2860 Resolve_Aspects;
2862 elsif L /= Visible_Declarations (Parent (L))
2863 or else Is_Empty_List (Private_Declarations (Parent (L)))
2864 then
2865 Adjust_Decl;
2867 -- End of a package declaration
2869 -- This is a freeze point because it is the end of a
2870 -- compilation unit.
2872 Freeze_All (First_Entity (Current_Scope), Decl);
2873 Freeze_From := Last_Entity (Current_Scope);
2875 -- At the end of the visible declarations the expressions in
2876 -- aspects of all entities declared so far must be resolved.
2877 -- The entities themselves might be frozen later, and the
2878 -- generated pragmas and attribute definition clauses analyzed
2879 -- in full at that point, but name resolution must take place
2880 -- now.
2881 -- In addition to being the proper semantics, this is mandatory
2882 -- within generic units, because global name capture requires
2883 -- those expressions to be analyzed, given that the generated
2884 -- pragmas do not appear in the original generic tree.
2886 elsif Serious_Errors_Detected = 0 then
2887 Resolve_Aspects;
2888 end if;
2890 -- If next node is a body then freeze all types before the body.
2891 -- An exception occurs for some expander-generated bodies. If these
2892 -- are generated at places where in general language rules would not
2893 -- allow a freeze point, then we assume that the expander has
2894 -- explicitly checked that all required types are properly frozen,
2895 -- and we do not cause general freezing here. This special circuit
2896 -- is used when the encountered body is marked as having already
2897 -- been analyzed.
2899 -- In all other cases (bodies that come from source, and expander
2900 -- generated bodies that have not been analyzed yet), freeze all
2901 -- types now. Note that in the latter case, the expander must take
2902 -- care to attach the bodies at a proper place in the tree so as to
2903 -- not cause unwanted freezing at that point.
2905 -- It is also necessary to check for a case where both an expression
2906 -- function is used and the current scope depends on an incomplete
2907 -- private type from a library unit, otherwise premature freezing of
2908 -- the private type will occur.
2910 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2911 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2912 or else not Was_Expression_Function (Next_Decl))
2913 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2914 and then not Contains_Lib_Incomplete_Type
2915 (Current_Scope)))
2916 then
2917 -- When a controlled type is frozen, the expander generates stream
2918 -- and controlled-type support routines. If the freeze is caused
2919 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2920 -- expander will end up using the wrong version of these routines,
2921 -- as the body has not been processed yet. To remedy this, detect
2922 -- a late controlled primitive and create a proper spec for it.
2923 -- This ensures that the primitive will override its inherited
2924 -- counterpart before the freeze takes place.
2926 -- If the declaration we just processed is a body, do not attempt
2927 -- to examine Next_Decl as the late primitive idiom can only apply
2928 -- to the first encountered body.
2930 -- ??? A cleaner approach may be possible and/or this solution
2931 -- could be extended to general-purpose late primitives.
2933 if Present (Ctrl_Typ) then
2935 -- No need to continue searching for late body overriding if
2936 -- the controlled type is already frozen.
2938 if Is_Frozen (Ctrl_Typ) then
2939 Ctrl_Typ := Empty;
2941 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2942 Handle_Late_Controlled_Primitive (Next_Decl);
2943 end if;
2944 end if;
2946 Adjust_Decl;
2948 -- The generated body of an expression function does not freeze,
2949 -- unless it is a completion, in which case only the expression
2950 -- itself freezes. This is handled when the body itself is
2951 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2953 Freeze_All (Freeze_From, Decl);
2954 Freeze_From := Last_Entity (Current_Scope);
2955 end if;
2957 Decl := Next_Decl;
2958 end loop;
2960 -- Post-freezing actions
2962 if Present (L) then
2963 Context := Parent (L);
2965 -- Certain contract annotations have forward visibility semantics and
2966 -- must be analyzed after all declarative items have been processed.
2967 -- This timing ensures that entities referenced by such contracts are
2968 -- visible.
2970 -- Analyze the contract of an immediately enclosing package spec or
2971 -- body first because other contracts may depend on its information.
2973 if Nkind (Context) = N_Package_Body then
2974 Analyze_Package_Body_Contract (Defining_Entity (Context));
2976 elsif Nkind (Context) = N_Package_Specification then
2977 Analyze_Package_Contract (Defining_Entity (Context));
2978 end if;
2980 -- Analyze the contracts of various constructs in the declarative
2981 -- list.
2983 Analyze_Contracts (L);
2985 if Nkind (Context) = N_Package_Body then
2987 -- Ensure that all abstract states and objects declared in the
2988 -- state space of a package body are utilized as constituents.
2990 Check_Unused_Body_States (Defining_Entity (Context));
2992 -- State refinements are visible up to the end of the package body
2993 -- declarations. Hide the state refinements from visibility to
2994 -- restore the original state conditions.
2996 Remove_Visible_Refinements (Corresponding_Spec (Context));
2997 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2999 elsif Nkind (Context) = N_Package_Specification then
3001 -- Partial state refinements are visible up to the end of the
3002 -- package spec declarations. Hide the partial state refinements
3003 -- from visibility to restore the original state conditions.
3005 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
3006 end if;
3008 -- Verify that all abstract states found in any package declared in
3009 -- the input declarative list have proper refinements. The check is
3010 -- performed only when the context denotes a block, entry, package,
3011 -- protected, subprogram, or task body (SPARK RM 7.1.4(4) and SPARK
3012 -- RM 7.2.2(3)).
3014 Check_State_Refinements (Context);
3016 -- Create the subprogram bodies which verify the run-time semantics
3017 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
3018 -- types within the current declarative list. This ensures that all
3019 -- assertion expressions are preanalyzed and resolved at the end of
3020 -- the declarative part. Note that the resolution happens even when
3021 -- freezing does not take place.
3023 Build_Assertion_Bodies (L, Context);
3024 end if;
3025 end Analyze_Declarations;
3027 -----------------------------------
3028 -- Analyze_Full_Type_Declaration --
3029 -----------------------------------
3031 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
3032 Def : constant Node_Id := Type_Definition (N);
3033 Def_Id : constant Entity_Id := Defining_Identifier (N);
3034 T : Entity_Id;
3035 Prev : Entity_Id;
3037 Is_Remote : constant Boolean :=
3038 (Is_Remote_Types (Current_Scope)
3039 or else Is_Remote_Call_Interface (Current_Scope))
3040 and then not (In_Private_Part (Current_Scope)
3041 or else In_Package_Body (Current_Scope));
3043 procedure Check_Nonoverridable_Aspects;
3044 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
3045 -- be overridden, and can only be confirmed on derivation.
3047 procedure Check_Ops_From_Incomplete_Type;
3048 -- If there is a tagged incomplete partial view of the type, traverse
3049 -- the primitives of the incomplete view and change the type of any
3050 -- controlling formals and result to indicate the full view. The
3051 -- primitives will be added to the full type's primitive operations
3052 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
3053 -- is called from Process_Incomplete_Dependents).
3055 ----------------------------------
3056 -- Check_Nonoverridable_Aspects --
3057 ----------------------------------
3059 procedure Check_Nonoverridable_Aspects is
3060 function Get_Aspect_Spec
3061 (Specs : List_Id;
3062 Aspect_Name : Name_Id) return Node_Id;
3063 -- Check whether a list of aspect specifications includes an entry
3064 -- for a specific aspect. The list is either that of a partial or
3065 -- a full view.
3067 ---------------------
3068 -- Get_Aspect_Spec --
3069 ---------------------
3071 function Get_Aspect_Spec
3072 (Specs : List_Id;
3073 Aspect_Name : Name_Id) return Node_Id
3075 Spec : Node_Id;
3077 begin
3078 Spec := First (Specs);
3079 while Present (Spec) loop
3080 if Chars (Identifier (Spec)) = Aspect_Name then
3081 return Spec;
3082 end if;
3083 Next (Spec);
3084 end loop;
3086 return Empty;
3087 end Get_Aspect_Spec;
3089 -- Local variables
3091 Prev_Aspects : constant List_Id :=
3092 Aspect_Specifications (Parent (Def_Id));
3093 Par_Type : Entity_Id;
3094 Prev_Aspect : Node_Id;
3096 -- Start of processing for Check_Nonoverridable_Aspects
3098 begin
3099 -- Get parent type of derived type. Note that Prev is the entity in
3100 -- the partial declaration, but its contents are now those of full
3101 -- view, while Def_Id reflects the partial view.
3103 if Is_Private_Type (Def_Id) then
3104 Par_Type := Etype (Full_View (Def_Id));
3105 else
3106 Par_Type := Etype (Def_Id);
3107 end if;
3109 -- If there is an inherited Implicit_Dereference, verify that it is
3110 -- made explicit in the partial view.
3112 if Has_Discriminants (Base_Type (Par_Type))
3113 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3114 and then Present (Discriminant_Specifications (Parent (Prev)))
3115 and then Present (Get_Reference_Discriminant (Par_Type))
3116 then
3117 Prev_Aspect :=
3118 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3120 if No (Prev_Aspect)
3121 and then Present
3122 (Discriminant_Specifications
3123 (Original_Node (Parent (Prev))))
3124 then
3125 Error_Msg_N
3126 ("type does not inherit implicit dereference", Prev);
3128 else
3129 -- If one of the views has the aspect specified, verify that it
3130 -- is consistent with that of the parent.
3132 declare
3133 Cur_Discr : constant Entity_Id :=
3134 Get_Reference_Discriminant (Prev);
3135 Par_Discr : constant Entity_Id :=
3136 Get_Reference_Discriminant (Par_Type);
3138 begin
3139 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3140 Error_Msg_N
3141 ("aspect inconsistent with that of parent", N);
3142 end if;
3144 -- Check that specification in partial view matches the
3145 -- inherited aspect. Compare names directly because aspect
3146 -- expression may not be analyzed.
3148 if Present (Prev_Aspect)
3149 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3150 and then Chars (Expression (Prev_Aspect)) /=
3151 Chars (Cur_Discr)
3152 then
3153 Error_Msg_N
3154 ("aspect inconsistent with that of parent", N);
3155 end if;
3156 end;
3157 end if;
3158 end if;
3160 -- What about other nonoverridable aspects???
3161 end Check_Nonoverridable_Aspects;
3163 ------------------------------------
3164 -- Check_Ops_From_Incomplete_Type --
3165 ------------------------------------
3167 procedure Check_Ops_From_Incomplete_Type is
3168 Elmt : Elmt_Id;
3169 Formal : Entity_Id;
3170 Op : Entity_Id;
3172 begin
3173 if Prev /= T
3174 and then Ekind (Prev) = E_Incomplete_Type
3175 and then Is_Tagged_Type (Prev)
3176 and then Is_Tagged_Type (T)
3177 and then Present (Primitive_Operations (Prev))
3178 then
3179 Elmt := First_Elmt (Primitive_Operations (Prev));
3180 while Present (Elmt) loop
3181 Op := Node (Elmt);
3183 Formal := First_Formal (Op);
3184 while Present (Formal) loop
3185 if Etype (Formal) = Prev then
3186 Set_Etype (Formal, T);
3187 end if;
3189 Next_Formal (Formal);
3190 end loop;
3192 if Etype (Op) = Prev then
3193 Set_Etype (Op, T);
3194 end if;
3196 Next_Elmt (Elmt);
3197 end loop;
3198 end if;
3199 end Check_Ops_From_Incomplete_Type;
3201 -- Start of processing for Analyze_Full_Type_Declaration
3203 begin
3204 Prev := Find_Type_Name (N);
3206 -- The full view, if present, now points to the current type. If there
3207 -- is an incomplete partial view, set a link to it, to simplify the
3208 -- retrieval of primitive operations of the type.
3210 -- Ada 2005 (AI-50217): If the type was previously decorated when
3211 -- imported through a LIMITED WITH clause, it appears as incomplete
3212 -- but has no full view.
3214 if Ekind (Prev) = E_Incomplete_Type
3215 and then Present (Full_View (Prev))
3216 then
3217 T := Full_View (Prev);
3218 Set_Incomplete_View (N, Prev);
3219 else
3220 T := Prev;
3221 end if;
3223 Set_Is_Pure (T, Is_Pure (Current_Scope));
3225 -- We set the flag Is_First_Subtype here. It is needed to set the
3226 -- corresponding flag for the Implicit class-wide-type created
3227 -- during tagged types processing.
3229 Set_Is_First_Subtype (T, True);
3231 -- Only composite types other than array types are allowed to have
3232 -- discriminants.
3234 case Nkind (Def) is
3236 -- For derived types, the rule will be checked once we've figured
3237 -- out the parent type.
3239 when N_Derived_Type_Definition =>
3240 null;
3242 -- For record types, discriminants are allowed.
3244 when N_Record_Definition =>
3245 null;
3247 when others =>
3248 if Present (Discriminant_Specifications (N)) then
3249 Error_Msg_N
3250 ("elementary or array type cannot have discriminants",
3251 Defining_Identifier
3252 (First (Discriminant_Specifications (N))));
3253 end if;
3254 end case;
3256 -- Elaborate the type definition according to kind, and generate
3257 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3258 -- already done (this happens during the reanalysis that follows a call
3259 -- to the high level optimizer).
3261 if not Analyzed (T) then
3262 Set_Analyzed (T);
3264 -- Set the SPARK mode from the current context
3266 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3267 Set_SPARK_Pragma_Inherited (T);
3269 case Nkind (Def) is
3270 when N_Access_To_Subprogram_Definition =>
3271 Access_Subprogram_Declaration (T, Def);
3273 -- If this is a remote access to subprogram, we must create the
3274 -- equivalent fat pointer type, and related subprograms.
3276 if Is_Remote then
3277 Process_Remote_AST_Declaration (N);
3278 end if;
3280 -- Validate categorization rule against access type declaration
3281 -- usually a violation in Pure unit, Shared_Passive unit.
3283 Validate_Access_Type_Declaration (T, N);
3285 -- If the type has contracts, we create the corresponding
3286 -- wrapper at once, before analyzing the aspect specifications,
3287 -- so that pre/postconditions can be handled directly on the
3288 -- generated wrapper.
3290 if Ada_Version >= Ada_2022
3291 and then Present (Aspect_Specifications (N))
3292 and then Expander_Active
3293 then
3294 Build_Access_Subprogram_Wrapper (N);
3295 end if;
3297 when N_Access_To_Object_Definition =>
3298 Access_Type_Declaration (T, Def);
3300 -- Validate categorization rule against access type declaration
3301 -- usually a violation in Pure unit, Shared_Passive unit.
3303 Validate_Access_Type_Declaration (T, N);
3305 -- If we are in a Remote_Call_Interface package and define a
3306 -- RACW, then calling stubs and specific stream attributes
3307 -- must be added.
3309 if Is_Remote
3310 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3311 then
3312 Add_RACW_Features (Def_Id);
3313 end if;
3315 when N_Array_Type_Definition =>
3316 Array_Type_Declaration (T, Def);
3318 when N_Derived_Type_Definition =>
3319 Derived_Type_Declaration (T, N, T /= Def_Id);
3321 -- Save the scenario for examination by the ABE Processing
3322 -- phase.
3324 Record_Elaboration_Scenario (N);
3326 when N_Enumeration_Type_Definition =>
3327 Enumeration_Type_Declaration (T, Def);
3329 when N_Floating_Point_Definition =>
3330 Floating_Point_Type_Declaration (T, Def);
3332 when N_Decimal_Fixed_Point_Definition =>
3333 Decimal_Fixed_Point_Type_Declaration (T, Def);
3335 when N_Ordinary_Fixed_Point_Definition =>
3336 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3338 when N_Signed_Integer_Type_Definition =>
3339 Signed_Integer_Type_Declaration (T, Def);
3341 when N_Modular_Type_Definition =>
3342 Modular_Type_Declaration (T, Def);
3344 when N_Record_Definition =>
3345 Record_Type_Declaration (T, N, Prev);
3347 -- If declaration has a parse error, nothing to elaborate.
3349 when N_Error =>
3350 null;
3352 when others =>
3353 raise Program_Error;
3354 end case;
3355 end if;
3357 if Etype (T) = Any_Type then
3358 return;
3359 end if;
3361 -- Set the primitives list of the full type and its base type when
3362 -- needed. T may be E_Void in cases of earlier errors, and in that
3363 -- case we bypass this.
3365 if Ekind (T) /= E_Void then
3366 if No (Direct_Primitive_Operations (T)) then
3367 if Etype (T) = T then
3368 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3370 -- If Etype of T is the base type (as opposed to a parent type)
3371 -- and already has an associated list of primitive operations,
3372 -- then set T's primitive list to the base type's list. Otherwise,
3373 -- create a new empty primitives list and share the list between
3374 -- T and its base type. The lists need to be shared in common.
3376 elsif Etype (T) = Base_Type (T) then
3378 if No (Direct_Primitive_Operations (Base_Type (T))) then
3379 Set_Direct_Primitive_Operations
3380 (Base_Type (T), New_Elmt_List);
3381 end if;
3383 Set_Direct_Primitive_Operations
3384 (T, Direct_Primitive_Operations (Base_Type (T)));
3386 -- Case where the Etype is a parent type, so we need a new
3387 -- primitives list for T.
3389 else
3390 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3391 end if;
3393 -- If T already has a Direct_Primitive_Operations list but its
3394 -- base type doesn't then set the base type's list to T's list.
3396 elsif No (Direct_Primitive_Operations (Base_Type (T))) then
3397 Set_Direct_Primitive_Operations
3398 (Base_Type (T), Direct_Primitive_Operations (T));
3399 end if;
3400 end if;
3402 -- Some common processing for all types
3404 Set_Depends_On_Private (T, Has_Private_Component (T));
3405 Check_Ops_From_Incomplete_Type;
3407 -- Both the declared entity, and its anonymous base type if one was
3408 -- created, need freeze nodes allocated.
3410 declare
3411 B : constant Entity_Id := Base_Type (T);
3413 begin
3414 -- In the case where the base type differs from the first subtype, we
3415 -- pre-allocate a freeze node, and set the proper link to the first
3416 -- subtype. Freeze_Entity will use this preallocated freeze node when
3417 -- it freezes the entity.
3419 -- This does not apply if the base type is a generic type, whose
3420 -- declaration is independent of the current derived definition.
3422 if B /= T and then not Is_Generic_Type (B) then
3423 Ensure_Freeze_Node (B);
3424 Set_First_Subtype_Link (Freeze_Node (B), T);
3425 end if;
3427 -- A type that is imported through a limited_with clause cannot
3428 -- generate any code, and thus need not be frozen. However, an access
3429 -- type with an imported designated type needs a finalization list,
3430 -- which may be referenced in some other package that has non-limited
3431 -- visibility on the designated type. Thus we must create the
3432 -- finalization list at the point the access type is frozen, to
3433 -- prevent unsatisfied references at link time.
3435 if not From_Limited_With (T) or else Is_Access_Type (T) then
3436 Set_Has_Delayed_Freeze (T);
3437 end if;
3438 end;
3440 -- Case where T is the full declaration of some private type which has
3441 -- been swapped in Defining_Identifier (N).
3443 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3444 Process_Full_View (N, T, Def_Id);
3446 -- Record the reference. The form of this is a little strange, since
3447 -- the full declaration has been swapped in. So the first parameter
3448 -- here represents the entity to which a reference is made which is
3449 -- the "real" entity, i.e. the one swapped in, and the second
3450 -- parameter provides the reference location.
3452 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3453 -- since we don't want a complaint about the full type being an
3454 -- unwanted reference to the private type
3456 declare
3457 B : constant Boolean := Has_Pragma_Unreferenced (T);
3458 begin
3459 Set_Has_Pragma_Unreferenced (T, False);
3460 Generate_Reference (T, T, 'c');
3461 Set_Has_Pragma_Unreferenced (T, B);
3462 end;
3464 Set_Completion_Referenced (Def_Id);
3466 -- For completion of incomplete type, process incomplete dependents
3467 -- and always mark the full type as referenced (it is the incomplete
3468 -- type that we get for any real reference).
3470 elsif Ekind (Prev) = E_Incomplete_Type then
3471 Process_Incomplete_Dependents (N, T, Prev);
3472 Generate_Reference (Prev, Def_Id, 'c');
3473 Set_Completion_Referenced (Def_Id);
3475 -- If not private type or incomplete type completion, this is a real
3476 -- definition of a new entity, so record it.
3478 else
3479 Generate_Definition (Def_Id);
3480 end if;
3482 -- Propagate any pending access types whose finalization masters need to
3483 -- be fully initialized from the partial to the full view. Guard against
3484 -- an illegal full view that remains unanalyzed.
3486 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3487 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3488 end if;
3490 if Chars (Scope (Def_Id)) = Name_System
3491 and then Chars (Def_Id) = Name_Address
3492 and then In_Predefined_Unit (N)
3493 then
3494 Set_Is_Descendant_Of_Address (Def_Id);
3495 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3496 Set_Is_Descendant_Of_Address (Prev);
3497 end if;
3499 Set_Optimize_Alignment_Flags (Def_Id);
3500 Check_Eliminated (Def_Id);
3502 -- If the declaration is a completion and aspects are present, apply
3503 -- them to the entity for the type which is currently the partial
3504 -- view, but which is the one that will be frozen.
3506 -- In most cases the partial view is a private type, and both views
3507 -- appear in different declarative parts. In the unusual case where
3508 -- the partial view is incomplete, perform the analysis on the
3509 -- full view, to prevent freezing anomalies with the corresponding
3510 -- class-wide type, which otherwise might be frozen before the
3511 -- dispatch table is built.
3513 if Prev /= Def_Id
3514 and then Ekind (Prev) /= E_Incomplete_Type
3515 then
3516 Analyze_Aspect_Specifications (N, Prev);
3518 -- Normal case
3520 else
3521 Analyze_Aspect_Specifications (N, Def_Id);
3522 end if;
3524 if Is_Derived_Type (Prev)
3525 and then Def_Id /= Prev
3526 then
3527 Check_Nonoverridable_Aspects;
3528 end if;
3530 -- Check for tagged type declaration at library level
3532 if Is_Tagged_Type (T)
3533 and then not Is_Library_Level_Entity (T)
3534 then
3535 Check_Restriction (No_Local_Tagged_Types, T);
3536 end if;
3537 end Analyze_Full_Type_Declaration;
3539 ----------------------------------
3540 -- Analyze_Incomplete_Type_Decl --
3541 ----------------------------------
3543 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3544 F : constant Boolean := Is_Pure (Current_Scope);
3545 T : Entity_Id;
3547 begin
3548 Generate_Definition (Defining_Identifier (N));
3550 -- Process an incomplete declaration. The identifier must not have been
3551 -- declared already in the scope. However, an incomplete declaration may
3552 -- appear in the private part of a package, for a private type that has
3553 -- already been declared.
3555 -- In this case, the discriminants (if any) must match
3557 T := Find_Type_Name (N);
3559 Mutate_Ekind (T, E_Incomplete_Type);
3560 Set_Etype (T, T);
3561 Set_Is_First_Subtype (T);
3562 Reinit_Size_Align (T);
3564 -- Set the SPARK mode from the current context
3566 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3567 Set_SPARK_Pragma_Inherited (T);
3569 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3570 -- incomplete types.
3572 if Tagged_Present (N) then
3573 Set_Is_Tagged_Type (T, True);
3574 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3575 Make_Class_Wide_Type (T);
3576 end if;
3578 -- Initialize the list of primitive operations to an empty list,
3579 -- to cover tagged types as well as untagged types. For untagged
3580 -- types this is used either to analyze the call as legal when
3581 -- Core_Extensions_Allowed is True, or to issue a better error message
3582 -- otherwise.
3584 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3586 Set_Stored_Constraint (T, No_Elist);
3588 if Present (Discriminant_Specifications (N)) then
3589 Push_Scope (T);
3590 Process_Discriminants (N);
3591 End_Scope;
3592 end if;
3594 -- If the type has discriminants, nontrivial subtypes may be declared
3595 -- before the full view of the type. The full views of those subtypes
3596 -- will be built after the full view of the type.
3598 Set_Private_Dependents (T, New_Elmt_List);
3599 Set_Is_Pure (T, F);
3600 end Analyze_Incomplete_Type_Decl;
3602 -----------------------------------
3603 -- Analyze_Interface_Declaration --
3604 -----------------------------------
3606 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3607 CW : constant Entity_Id := Class_Wide_Type (T);
3609 begin
3610 Set_Is_Tagged_Type (T);
3611 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3613 Set_Is_Limited_Record (T, Limited_Present (Def)
3614 or else Task_Present (Def)
3615 or else Protected_Present (Def)
3616 or else Synchronized_Present (Def));
3618 -- Type is abstract if full declaration carries keyword, or if previous
3619 -- partial view did.
3621 Set_Is_Abstract_Type (T);
3622 Set_Is_Interface (T);
3624 -- Type is a limited interface if it includes the keyword limited, task,
3625 -- protected, or synchronized.
3627 Set_Is_Limited_Interface
3628 (T, Limited_Present (Def)
3629 or else Protected_Present (Def)
3630 or else Synchronized_Present (Def)
3631 or else Task_Present (Def));
3633 Set_Interfaces (T, New_Elmt_List);
3634 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3636 -- Complete the decoration of the class-wide entity if it was already
3637 -- built (i.e. during the creation of the limited view)
3639 if Present (CW) then
3640 Set_Is_Interface (CW);
3641 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3642 end if;
3644 -- Check runtime support for synchronized interfaces
3646 if Is_Concurrent_Interface (T)
3647 and then not RTE_Available (RE_Select_Specific_Data)
3648 then
3649 Error_Msg_CRT ("synchronized interfaces", T);
3650 end if;
3651 end Analyze_Interface_Declaration;
3653 -----------------------------
3654 -- Analyze_Itype_Reference --
3655 -----------------------------
3657 -- Nothing to do. This node is placed in the tree only for the benefit of
3658 -- back end processing, and has no effect on the semantic processing.
3660 procedure Analyze_Itype_Reference (N : Node_Id) is
3661 begin
3662 pragma Assert (Is_Itype (Itype (N)));
3663 null;
3664 end Analyze_Itype_Reference;
3666 --------------------------------
3667 -- Analyze_Number_Declaration --
3668 --------------------------------
3670 procedure Analyze_Number_Declaration (N : Node_Id) is
3671 E : Node_Id := Expression (N);
3672 Id : constant Entity_Id := Defining_Identifier (N);
3673 Index : Interp_Index;
3674 It : Interp;
3675 T : Entity_Id;
3677 begin
3678 Generate_Definition (Id);
3679 Enter_Name (Id);
3681 -- This is an optimization of a common case of an integer literal
3683 if Nkind (E) = N_Integer_Literal then
3684 Set_Is_Static_Expression (E, True);
3685 Set_Etype (E, Universal_Integer);
3687 Set_Etype (Id, Universal_Integer);
3688 Mutate_Ekind (Id, E_Named_Integer);
3689 Set_Is_Frozen (Id, True);
3691 Set_Debug_Info_Needed (Id);
3692 return;
3693 end if;
3695 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3697 -- Replace Error by integer zero, which seems least likely to cause
3698 -- cascaded errors.
3700 if E = Error then
3701 pragma Assert (Serious_Errors_Detected > 0);
3702 E := Make_Integer_Literal (Sloc (N), Uint_0);
3703 Set_Expression (N, E);
3704 Set_Error_Posted (E);
3705 end if;
3707 Analyze (E);
3709 -- Verify that the expression is static and numeric. If
3710 -- the expression is overloaded, we apply the preference
3711 -- rule that favors root numeric types.
3713 if not Is_Overloaded (E) then
3714 T := Etype (E);
3715 if Has_Dynamic_Predicate_Aspect (T)
3716 or else Has_Ghost_Predicate_Aspect (T)
3717 then
3718 Error_Msg_N
3719 ("subtype has non-static predicate, "
3720 & "not allowed in number declaration", N);
3721 end if;
3723 else
3724 T := Any_Type;
3726 Get_First_Interp (E, Index, It);
3727 while Present (It.Typ) loop
3728 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3729 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3730 then
3731 if T = Any_Type then
3732 T := It.Typ;
3734 elsif Is_Universal_Numeric_Type (It.Typ) then
3735 -- Choose universal interpretation over any other
3737 T := It.Typ;
3738 exit;
3739 end if;
3740 end if;
3742 Get_Next_Interp (Index, It);
3743 end loop;
3744 end if;
3746 if Is_Integer_Type (T) then
3747 Resolve (E, T);
3748 Set_Etype (Id, Universal_Integer);
3749 Mutate_Ekind (Id, E_Named_Integer);
3751 elsif Is_Real_Type (T) then
3753 -- Because the real value is converted to universal_real, this is a
3754 -- legal context for a universal fixed expression.
3756 if T = Universal_Fixed then
3757 declare
3758 Loc : constant Source_Ptr := Sloc (N);
3759 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3760 Subtype_Mark =>
3761 New_Occurrence_Of (Universal_Real, Loc),
3762 Expression => Relocate_Node (E));
3764 begin
3765 Rewrite (E, Conv);
3766 Analyze (E);
3767 end;
3769 elsif T = Any_Fixed then
3770 Error_Msg_N ("illegal context for mixed mode operation", E);
3772 -- Expression is of the form : universal_fixed * integer. Try to
3773 -- resolve as universal_real.
3775 T := Universal_Real;
3776 Set_Etype (E, T);
3777 end if;
3779 Resolve (E, T);
3780 Set_Etype (Id, Universal_Real);
3781 Mutate_Ekind (Id, E_Named_Real);
3783 else
3784 Wrong_Type (E, Any_Numeric);
3785 Resolve (E, T);
3787 Set_Etype (Id, T);
3788 Mutate_Ekind (Id, E_Constant);
3789 Set_Never_Set_In_Source (Id, True);
3790 Set_Is_True_Constant (Id, True);
3791 return;
3792 end if;
3794 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3795 Set_Etype (E, Etype (Id));
3796 end if;
3798 if not Is_OK_Static_Expression (E) then
3799 Flag_Non_Static_Expr
3800 ("non-static expression used in number declaration!", E);
3801 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3802 Set_Etype (E, Any_Type);
3803 end if;
3805 Analyze_Dimension (N);
3806 end Analyze_Number_Declaration;
3808 --------------------------------
3809 -- Analyze_Object_Declaration --
3810 --------------------------------
3812 -- WARNING: This routine manages Ghost regions. Return statements must be
3813 -- replaced by gotos which jump to the end of the routine and restore the
3814 -- Ghost mode.
3816 procedure Analyze_Object_Declaration (N : Node_Id) is
3817 Loc : constant Source_Ptr := Sloc (N);
3818 Id : constant Entity_Id := Defining_Identifier (N);
3819 Next_Decl : constant Node_Id := Next (N);
3821 Act_T : Entity_Id;
3822 T : Entity_Id;
3824 E : Node_Id := Expression (N);
3825 -- E is set to Expression (N) throughout this routine. When Expression
3826 -- (N) is modified, E is changed accordingly.
3828 procedure Check_Dynamic_Object (Typ : Entity_Id);
3829 -- A library-level object with nonstatic discriminant constraints may
3830 -- require dynamic allocation. The declaration is illegal if the
3831 -- profile includes the restriction No_Implicit_Heap_Allocations.
3833 procedure Check_For_Null_Excluding_Components
3834 (Obj_Typ : Entity_Id;
3835 Obj_Decl : Node_Id);
3836 -- Verify that each null-excluding component of object declaration
3837 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3838 -- a compile-time warning if this is not the case.
3840 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
3841 -- Check that the return subtype indication properly matches the result
3842 -- subtype of the function in an extended return object declaration, as
3843 -- required by RM 6.5(5.1/2-5.3/2).
3845 function Count_Tasks (T : Entity_Id) return Uint;
3846 -- This function is called when a non-generic library level object of a
3847 -- task type is declared. Its function is to count the static number of
3848 -- tasks declared within the type (it is only called if Has_Task is set
3849 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3850 -- or a variant record type is encountered, Check_Restriction is called
3851 -- indicating the count is unknown.
3853 function Delayed_Aspect_Present return Boolean;
3854 -- If the declaration has an expression that is an aggregate, and it
3855 -- has aspects that require delayed analysis, the resolution of the
3856 -- aggregate must be deferred to the freeze point of the object. This
3857 -- special processing was created for address clauses, but it must
3858 -- also apply to address aspects. This must be done before the aspect
3859 -- specifications are analyzed because we must handle the aggregate
3860 -- before the analysis of the object declaration is complete.
3862 -- Any other relevant delayed aspects on object declarations ???
3864 --------------------------
3865 -- Check_Dynamic_Object --
3866 --------------------------
3868 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3869 Comp : Entity_Id;
3870 Obj_Type : Entity_Id;
3872 begin
3873 Obj_Type := Typ;
3875 if Is_Private_Type (Obj_Type)
3876 and then Present (Full_View (Obj_Type))
3877 then
3878 Obj_Type := Full_View (Obj_Type);
3879 end if;
3881 if Known_Static_Esize (Obj_Type) then
3882 return;
3883 end if;
3885 if Restriction_Active (No_Implicit_Heap_Allocations)
3886 and then Expander_Active
3887 and then Has_Discriminants (Obj_Type)
3888 then
3889 Comp := First_Component (Obj_Type);
3890 while Present (Comp) loop
3891 if Known_Static_Esize (Etype (Comp))
3892 or else Size_Known_At_Compile_Time (Etype (Comp))
3893 then
3894 null;
3896 elsif Is_Record_Type (Etype (Comp)) then
3897 Check_Dynamic_Object (Etype (Comp));
3899 elsif not Discriminated_Size (Comp)
3900 and then Comes_From_Source (Comp)
3901 then
3902 Error_Msg_NE
3903 ("component& of non-static size will violate restriction "
3904 & "No_Implicit_Heap_Allocation?", N, Comp);
3906 end if;
3908 Next_Component (Comp);
3909 end loop;
3910 end if;
3911 end Check_Dynamic_Object;
3913 -----------------------------------------
3914 -- Check_For_Null_Excluding_Components --
3915 -----------------------------------------
3917 procedure Check_For_Null_Excluding_Components
3918 (Obj_Typ : Entity_Id;
3919 Obj_Decl : Node_Id)
3921 procedure Check_Component
3922 (Comp_Typ : Entity_Id;
3923 Comp_Decl : Node_Id := Empty;
3924 Array_Comp : Boolean := False);
3925 -- Apply a compile-time null-exclusion check on a component denoted
3926 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3927 -- subcomponents (if any).
3929 ---------------------
3930 -- Check_Component --
3931 ---------------------
3933 procedure Check_Component
3934 (Comp_Typ : Entity_Id;
3935 Comp_Decl : Node_Id := Empty;
3936 Array_Comp : Boolean := False)
3938 Comp : Entity_Id;
3939 T : Entity_Id;
3941 begin
3942 -- Do not consider internally-generated components or those that
3943 -- are already initialized.
3945 if Present (Comp_Decl)
3946 and then (not Comes_From_Source (Comp_Decl)
3947 or else Present (Expression (Comp_Decl)))
3948 then
3949 return;
3950 end if;
3952 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3953 and then Present (Full_View (Comp_Typ))
3954 then
3955 T := Full_View (Comp_Typ);
3956 else
3957 T := Comp_Typ;
3958 end if;
3960 -- Verify a component of a null-excluding access type
3962 if Is_Access_Type (T)
3963 and then Can_Never_Be_Null (T)
3964 then
3965 if Comp_Decl = Obj_Decl then
3966 Null_Exclusion_Static_Checks
3967 (N => Obj_Decl,
3968 Comp => Empty,
3969 Array_Comp => Array_Comp);
3971 else
3972 Null_Exclusion_Static_Checks
3973 (N => Obj_Decl,
3974 Comp => Comp_Decl,
3975 Array_Comp => Array_Comp);
3976 end if;
3978 -- Check array components
3980 elsif Is_Array_Type (T) then
3982 -- There is no suitable component when the object is of an
3983 -- array type. However, a namable component may appear at some
3984 -- point during the recursive inspection, but not at the top
3985 -- level. At the top level just indicate array component case.
3987 if Comp_Decl = Obj_Decl then
3988 Check_Component (Component_Type (T), Array_Comp => True);
3989 else
3990 Check_Component (Component_Type (T), Comp_Decl);
3991 end if;
3993 -- Verify all components of type T
3995 -- Note: No checks are performed on types with discriminants due
3996 -- to complexities involving variants. ???
3998 elsif (Is_Concurrent_Type (T)
3999 or else Is_Incomplete_Or_Private_Type (T)
4000 or else Is_Record_Type (T))
4001 and then not Has_Discriminants (T)
4002 then
4003 Comp := First_Component (T);
4004 while Present (Comp) loop
4005 Check_Component (Etype (Comp), Parent (Comp));
4007 Next_Component (Comp);
4008 end loop;
4009 end if;
4010 end Check_Component;
4012 -- Start processing for Check_For_Null_Excluding_Components
4014 begin
4015 Check_Component (Obj_Typ, Obj_Decl);
4016 end Check_For_Null_Excluding_Components;
4018 -------------------------------------
4019 -- Check_Return_Subtype_Indication --
4020 -------------------------------------
4022 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
4023 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
4024 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4025 Func_Id : constant Entity_Id := Return_Applies_To (Scope (Obj_Id));
4026 R_Typ : constant Entity_Id := Etype (Func_Id);
4027 Indic : constant Node_Id :=
4028 Object_Definition (Original_Node (Obj_Decl));
4030 procedure Error_No_Match (N : Node_Id);
4031 -- Output error messages for case where types do not statically
4032 -- match. N is the location for the messages.
4034 --------------------
4035 -- Error_No_Match --
4036 --------------------
4038 procedure Error_No_Match (N : Node_Id) is
4039 begin
4040 Error_Msg_N
4041 ("subtype must statically match function result subtype", N);
4043 if not Predicates_Match (Obj_Typ, R_Typ) then
4044 Error_Msg_Node_2 := R_Typ;
4045 Error_Msg_NE
4046 ("\predicate of& does not match predicate of&",
4047 N, Obj_Typ);
4048 end if;
4049 end Error_No_Match;
4051 -- Start of processing for Check_Return_Subtype_Indication
4053 begin
4054 -- First, avoid cascaded errors
4056 if Error_Posted (Obj_Decl) or else Error_Posted (Indic) then
4057 return;
4058 end if;
4060 -- "return access T" case; check that the return statement also has
4061 -- "access T", and that the subtypes statically match:
4062 -- if this is an access to subprogram the signatures must match.
4064 if Is_Anonymous_Access_Type (R_Typ) then
4065 if Is_Anonymous_Access_Type (Obj_Typ) then
4066 if Ekind (Designated_Type (Obj_Typ)) /= E_Subprogram_Type
4067 then
4068 if Base_Type (Designated_Type (Obj_Typ)) /=
4069 Base_Type (Designated_Type (R_Typ))
4070 or else not Subtypes_Statically_Match (Obj_Typ, R_Typ)
4071 then
4072 Error_No_Match (Subtype_Mark (Indic));
4073 end if;
4075 else
4076 -- For two anonymous access to subprogram types, the types
4077 -- themselves must be type conformant.
4079 if not Conforming_Types
4080 (Obj_Typ, R_Typ, Fully_Conformant)
4081 then
4082 Error_No_Match (Indic);
4083 end if;
4084 end if;
4086 else
4087 Error_Msg_N ("must use anonymous access type", Indic);
4088 end if;
4090 -- If the return object is of an anonymous access type, then report
4091 -- an error if the function's result type is not also anonymous.
4093 elsif Is_Anonymous_Access_Type (Obj_Typ) then
4094 pragma Assert (not Is_Anonymous_Access_Type (R_Typ));
4095 Error_Msg_N
4096 ("anonymous access not allowed for function with named access "
4097 & "result", Indic);
4099 -- Subtype indication case: check that the return object's type is
4100 -- covered by the result type, and that the subtypes statically match
4101 -- when the result subtype is constrained. Also handle record types
4102 -- with unknown discriminants for which we have built the underlying
4103 -- record view. Coverage is needed to allow specific-type return
4104 -- objects when the result type is class-wide (see AI05-32).
4106 elsif Covers (Base_Type (R_Typ), Base_Type (Obj_Typ))
4107 or else (Is_Underlying_Record_View (Base_Type (Obj_Typ))
4108 and then
4109 Covers
4110 (Base_Type (R_Typ),
4111 Underlying_Record_View (Base_Type (Obj_Typ))))
4112 then
4113 -- A null exclusion may be present on the return type, on the
4114 -- function specification, on the object declaration or on the
4115 -- subtype itself.
4117 if Is_Access_Type (R_Typ)
4118 and then
4119 (Can_Never_Be_Null (R_Typ)
4120 or else Null_Exclusion_Present (Parent (Func_Id))) /=
4121 Can_Never_Be_Null (Obj_Typ)
4122 then
4123 Error_No_Match (Indic);
4124 end if;
4126 -- AI05-103: for elementary types, subtypes must statically match
4128 if Is_Constrained (R_Typ) or else Is_Access_Type (R_Typ) then
4129 if not Subtypes_Statically_Match (Obj_Typ, R_Typ) then
4130 Error_No_Match (Indic);
4131 end if;
4132 end if;
4134 -- All remaining cases are illegal
4136 -- Note: previous versions of this subprogram allowed the return
4137 -- value to be the ancestor of the return type if the return type
4138 -- was a null extension. This was plainly incorrect.
4140 else
4141 Error_Msg_N
4142 ("wrong type for return_subtype_indication", Indic);
4143 end if;
4144 end Check_Return_Subtype_Indication;
4146 -----------------
4147 -- Count_Tasks --
4148 -----------------
4150 function Count_Tasks (T : Entity_Id) return Uint is
4151 C : Entity_Id;
4152 X : Node_Id;
4153 V : Uint;
4155 begin
4156 if Is_Task_Type (T) then
4157 return Uint_1;
4159 elsif Is_Record_Type (T) then
4160 if Has_Discriminants (T) then
4161 Check_Restriction (Max_Tasks, N);
4162 return Uint_0;
4164 else
4165 V := Uint_0;
4166 C := First_Component (T);
4167 while Present (C) loop
4168 V := V + Count_Tasks (Etype (C));
4169 Next_Component (C);
4170 end loop;
4172 return V;
4173 end if;
4175 elsif Is_Array_Type (T) then
4176 X := First_Index (T);
4177 V := Count_Tasks (Component_Type (T));
4178 while Present (X) loop
4179 C := Etype (X);
4181 if not Is_OK_Static_Subtype (C) then
4182 Check_Restriction (Max_Tasks, N);
4183 return Uint_0;
4184 else
4185 V := V * (UI_Max (Uint_0,
4186 Expr_Value (Type_High_Bound (C)) -
4187 Expr_Value (Type_Low_Bound (C)) + Uint_1));
4188 end if;
4190 Next_Index (X);
4191 end loop;
4193 return V;
4195 else
4196 return Uint_0;
4197 end if;
4198 end Count_Tasks;
4200 ----------------------------
4201 -- Delayed_Aspect_Present --
4202 ----------------------------
4204 function Delayed_Aspect_Present return Boolean is
4205 A : Node_Id;
4206 A_Id : Aspect_Id;
4208 begin
4209 if Present (Aspect_Specifications (N)) then
4210 A := First (Aspect_Specifications (N));
4212 while Present (A) loop
4213 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4215 if A_Id = Aspect_Address then
4217 -- Set flag on object entity, for later processing at
4218 -- the freeze point.
4220 Set_Has_Delayed_Aspects (Id);
4221 return True;
4222 end if;
4224 Next (A);
4225 end loop;
4226 end if;
4228 return False;
4229 end Delayed_Aspect_Present;
4231 -- Local variables
4233 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4234 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4235 -- Save the Ghost-related attributes to restore on exit
4237 Prev_Entity : Entity_Id := Empty;
4238 Related_Id : Entity_Id;
4240 -- Start of processing for Analyze_Object_Declaration
4242 begin
4243 -- There are three kinds of implicit types generated by an
4244 -- object declaration:
4246 -- 1. Those generated by the original Object Definition
4248 -- 2. Those generated by the Expression
4250 -- 3. Those used to constrain the Object Definition with the
4251 -- expression constraints when the definition is unconstrained.
4253 -- They must be generated in this order to avoid order of elaboration
4254 -- issues. Thus the first step (after entering the name) is to analyze
4255 -- the object definition.
4257 if Constant_Present (N) then
4258 Prev_Entity := Current_Entity_In_Scope (Id);
4260 if Present (Prev_Entity)
4261 and then
4262 -- If the homograph is an implicit subprogram, it is overridden
4263 -- by the current declaration.
4265 ((Is_Overloadable (Prev_Entity)
4266 and then Is_Inherited_Operation (Prev_Entity))
4268 -- The current object is a discriminal generated for an entry
4269 -- family index. Even though the index is a constant, in this
4270 -- particular context there is no true constant redeclaration.
4271 -- Enter_Name will handle the visibility.
4273 or else
4274 (Is_Discriminal (Id)
4275 and then Ekind (Discriminal_Link (Id)) =
4276 E_Entry_Index_Parameter)
4278 -- The current object is the renaming for a generic declared
4279 -- within the instance.
4281 or else
4282 (Ekind (Prev_Entity) = E_Package
4283 and then Nkind (Parent (Prev_Entity)) =
4284 N_Package_Renaming_Declaration
4285 and then not Comes_From_Source (Prev_Entity)
4286 and then
4287 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4289 -- The entity may be a homonym of a private component of the
4290 -- enclosing protected object, for which we create a local
4291 -- renaming declaration. The declaration is legal, even if
4292 -- useless when it just captures that component.
4294 or else
4295 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4296 and then Nkind (Parent (Prev_Entity)) =
4297 N_Object_Renaming_Declaration))
4298 then
4299 Prev_Entity := Empty;
4300 end if;
4301 end if;
4303 if Present (Prev_Entity) then
4305 -- The object declaration is Ghost when it completes a deferred Ghost
4306 -- constant.
4308 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4310 Constant_Redeclaration (Id, N, T);
4312 Generate_Reference (Prev_Entity, Id, 'c');
4313 Set_Completion_Referenced (Id);
4315 if Error_Posted (N) then
4317 -- Type mismatch or illegal redeclaration; do not analyze
4318 -- expression to avoid cascaded errors.
4320 T := Find_Type_Of_Object (Object_Definition (N), N);
4321 Set_Etype (Id, T);
4322 Mutate_Ekind (Id, E_Variable);
4323 goto Leave;
4324 end if;
4326 -- In the normal case, enter identifier at the start to catch premature
4327 -- usage in the initialization expression.
4329 else
4330 Generate_Definition (Id);
4331 Enter_Name (Id);
4333 Mark_Coextensions (N, Object_Definition (N));
4335 T := Find_Type_Of_Object (Object_Definition (N), N);
4337 if Nkind (Object_Definition (N)) = N_Access_Definition
4338 and then Present
4339 (Access_To_Subprogram_Definition (Object_Definition (N)))
4340 and then Protected_Present
4341 (Access_To_Subprogram_Definition (Object_Definition (N)))
4342 then
4343 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4344 end if;
4346 if Error_Posted (Id) then
4347 Set_Etype (Id, T);
4348 Mutate_Ekind (Id, E_Variable);
4349 goto Leave;
4350 end if;
4351 end if;
4353 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4354 -- out some static checks.
4356 if Ada_Version >= Ada_2005 then
4358 -- In case of aggregates we must also take care of the correct
4359 -- initialization of nested aggregates bug this is done at the
4360 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4362 if Can_Never_Be_Null (T) then
4363 if Present (Expression (N))
4364 and then Nkind (Expression (N)) = N_Aggregate
4365 then
4366 null;
4368 elsif Comes_From_Source (Id) then
4369 declare
4370 Save_Typ : constant Entity_Id := Etype (Id);
4371 begin
4372 Set_Etype (Id, T); -- Temp. decoration for static checks
4373 Null_Exclusion_Static_Checks (N);
4374 Set_Etype (Id, Save_Typ);
4375 end;
4376 end if;
4378 -- We might be dealing with an object of a composite type containing
4379 -- null-excluding components without an aggregate, so we must verify
4380 -- that such components have default initialization.
4382 else
4383 Check_For_Null_Excluding_Components (T, N);
4384 end if;
4385 end if;
4387 -- Object is marked pure if it is in a pure scope
4389 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4391 -- If deferred constant, make sure context is appropriate. We detect
4392 -- a deferred constant as a constant declaration with no expression.
4393 -- A deferred constant can appear in a package body if its completion
4394 -- is by means of an interface pragma.
4396 if Constant_Present (N) and then No (E) then
4398 -- A deferred constant may appear in the declarative part of the
4399 -- following constructs:
4401 -- blocks
4402 -- entry bodies
4403 -- extended return statements
4404 -- package specs
4405 -- package bodies
4406 -- subprogram bodies
4407 -- task bodies
4409 -- When declared inside a package spec, a deferred constant must be
4410 -- completed by a full constant declaration or pragma Import. In all
4411 -- other cases, the only proper completion is pragma Import. Extended
4412 -- return statements are flagged as invalid contexts because they do
4413 -- not have a declarative part and so cannot accommodate the pragma.
4415 if Ekind (Current_Scope) = E_Return_Statement then
4416 Error_Msg_N
4417 ("invalid context for deferred constant declaration (RM 7.4)",
4419 Error_Msg_N
4420 ("\declaration requires an initialization expression",
4422 Set_Constant_Present (N, False);
4424 -- In Ada 83, deferred constant must be of private type
4426 elsif not Is_Private_Type (T) then
4427 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4428 Error_Msg_N
4429 ("(Ada 83) deferred constant must be private type", N);
4430 end if;
4431 end if;
4433 -- If not a deferred constant, then the object declaration freezes
4434 -- its type, unless the object is of an anonymous type and has delayed
4435 -- aspects (in that case the type is frozen when the object itself is)
4436 -- or the context is a spec expression.
4438 else
4439 Check_Fully_Declared (T, N);
4441 if Has_Delayed_Aspects (Id)
4442 and then Is_Array_Type (T)
4443 and then Is_Itype (T)
4444 then
4445 Set_Has_Delayed_Freeze (T);
4446 elsif not In_Spec_Expression then
4447 Freeze_Before (N, T);
4448 end if;
4449 end if;
4451 -- If the object was created by a constrained array definition, then
4452 -- set the link in both the anonymous base type and anonymous subtype
4453 -- that are built to represent the array type to point to the object.
4455 if Nkind (Object_Definition (Declaration_Node (Id))) =
4456 N_Constrained_Array_Definition
4457 then
4458 Set_Related_Array_Object (T, Id);
4459 Set_Related_Array_Object (Base_Type (T), Id);
4460 end if;
4462 -- Check for protected objects not at library level
4464 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4465 Check_Restriction (No_Local_Protected_Objects, Id);
4466 end if;
4468 -- Check for violation of No_Local_Timing_Events
4470 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4471 Check_Restriction (No_Local_Timing_Events, Id);
4472 end if;
4474 -- The actual subtype of the object is the nominal subtype, unless
4475 -- the nominal one is unconstrained and obtained from the expression.
4477 Act_T := T;
4479 if Is_Library_Level_Entity (Id) then
4480 Check_Dynamic_Object (T);
4481 end if;
4483 -- Process initialization expression if present and not in error
4485 if Present (E) and then E /= Error then
4487 -- Generate an error in case of CPP class-wide object initialization.
4488 -- Required because otherwise the expansion of the class-wide
4489 -- assignment would try to use 'size to initialize the object
4490 -- (primitive that is not available in CPP tagged types).
4492 if Is_Class_Wide_Type (Act_T)
4493 and then
4494 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4495 or else
4496 (Present (Full_View (Root_Type (Etype (Act_T))))
4497 and then
4498 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4499 then
4500 Error_Msg_N
4501 ("predefined assignment not available for 'C'P'P tagged types",
4503 end if;
4505 Mark_Coextensions (N, E);
4506 Analyze (E);
4508 -- In case of errors detected in the analysis of the expression,
4509 -- decorate it with the expected type to avoid cascaded errors.
4511 if No (Etype (E)) then
4512 Set_Etype (E, T);
4513 end if;
4515 -- If an initialization expression is present, then we set the
4516 -- Is_True_Constant flag. It will be reset if this is a variable
4517 -- and it is indeed modified.
4519 Set_Is_True_Constant (Id, True);
4521 -- If we are analyzing a constant declaration, set its completion
4522 -- flag after analyzing and resolving the expression.
4524 if Constant_Present (N) then
4525 Set_Has_Completion (Id);
4526 end if;
4528 -- Set type and resolve (type may be overridden later on). Note:
4529 -- Ekind (Id) must still be E_Void at this point so that incorrect
4530 -- early usage within E is properly diagnosed.
4532 Set_Etype (Id, T);
4534 -- If the expression is an aggregate we must look ahead to detect
4535 -- the possible presence of an address clause, and defer resolution
4536 -- and expansion of the aggregate to the freeze point of the entity.
4538 -- This is not always legal because the aggregate may contain other
4539 -- references that need freezing, e.g. references to other entities
4540 -- with address clauses. In any case, when compiling with -gnatI the
4541 -- presence of the address clause must be ignored.
4543 if Comes_From_Source (N)
4544 and then Expander_Active
4545 and then Nkind (E) = N_Aggregate
4546 and then
4547 ((Present (Following_Address_Clause (N))
4548 and then not Ignore_Rep_Clauses)
4549 or else Delayed_Aspect_Present)
4550 then
4551 Set_Etype (E, T);
4553 -- If the aggregate is limited it will be built in place, and its
4554 -- expansion is deferred until the object declaration is expanded.
4556 -- This is also required when generating C code to ensure that an
4557 -- object with an alignment or address clause can be initialized
4558 -- by means of component by component assignments.
4560 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4561 Set_Expansion_Delayed (E);
4562 end if;
4564 else
4565 -- If the expression is a formal that is a "subprogram pointer"
4566 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4567 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4568 -- the corresponding check, as is done for assignments.
4570 if Is_Entity_Name (E)
4571 and then Present (Entity (E))
4572 and then Is_Formal (Entity (E))
4573 and then
4574 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4575 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4576 then
4577 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4578 end if;
4580 Resolve (E, T);
4581 end if;
4583 -- No further action needed if E is a call to an inlined function
4584 -- which returns an unconstrained type and it has been expanded into
4585 -- a procedure call. In that case N has been replaced by an object
4586 -- declaration without initializing expression and it has been
4587 -- analyzed (see Expand_Inlined_Call).
4589 if Back_End_Inlining
4590 and then Expander_Active
4591 and then Nkind (E) = N_Function_Call
4592 and then Nkind (Name (E)) in N_Has_Entity
4593 and then Is_Inlined (Entity (Name (E)))
4594 and then not Is_Constrained (Etype (E))
4595 and then Analyzed (N)
4596 and then No (Expression (N))
4597 then
4598 goto Leave;
4599 end if;
4601 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4602 -- node (which was marked already-analyzed), we need to set the type
4603 -- to something else than Universal_Access to keep gigi happy.
4605 if Etype (E) = Universal_Access then
4606 Set_Etype (E, T);
4607 end if;
4609 -- If the object is an access to variable, the initialization
4610 -- expression cannot be an access to constant.
4612 if Is_Access_Type (T)
4613 and then not Is_Access_Constant (T)
4614 and then Is_Access_Type (Etype (E))
4615 and then Is_Access_Constant (Etype (E))
4616 then
4617 Error_Msg_N
4618 ("access to variable cannot be initialized with an "
4619 & "access-to-constant expression", E);
4620 end if;
4622 if not Assignment_OK (N) then
4623 Check_Initialization (T, E);
4624 end if;
4626 Check_Unset_Reference (E);
4628 -- If this is a variable, then set current value. If this is a
4629 -- declared constant of a scalar type with a static expression,
4630 -- indicate that it is always valid.
4632 if not Constant_Present (N) then
4633 if Compile_Time_Known_Value (E) then
4634 Set_Current_Value (Id, E);
4635 end if;
4637 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4638 Set_Is_Known_Valid (Id);
4640 -- If it is a constant initialized with a valid nonstatic entity,
4641 -- the constant is known valid as well, and can inherit the subtype
4642 -- of the entity if it is a subtype of the given type. This info
4643 -- is preserved on the actual subtype of the constant.
4645 elsif Is_Scalar_Type (T)
4646 and then Is_Entity_Name (E)
4647 and then Is_Known_Valid (Entity (E))
4648 and then In_Subrange_Of (Etype (Entity (E)), T)
4649 then
4650 Set_Is_Known_Valid (Id);
4651 Mutate_Ekind (Id, E_Constant);
4652 Set_Actual_Subtype (Id, Etype (Entity (E)));
4653 end if;
4655 -- Deal with setting of null flags
4657 if Is_Access_Type (T) then
4658 if Known_Non_Null (E) then
4659 Set_Is_Known_Non_Null (Id, True);
4660 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4661 Set_Is_Known_Null (Id, True);
4662 end if;
4663 end if;
4665 -- Check incorrect use of dynamically tagged expressions
4667 if Is_Tagged_Type (T) then
4668 Check_Dynamically_Tagged_Expression
4669 (Expr => E,
4670 Typ => T,
4671 Related_Nod => N);
4672 end if;
4674 Apply_Scalar_Range_Check (E, T);
4675 Apply_Static_Length_Check (E, T);
4677 -- A formal parameter of a specific tagged type whose related
4678 -- subprogram is subject to pragma Extensions_Visible with value
4679 -- "False" cannot be implicitly converted to a class-wide type by
4680 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4681 -- not consider internally generated expressions.
4683 if Is_Class_Wide_Type (T)
4684 and then Comes_From_Source (E)
4685 and then Is_EVF_Expression (E)
4686 then
4687 Error_Msg_N
4688 ("formal parameter cannot be implicitly converted to "
4689 & "class-wide type when Extensions_Visible is False", E);
4690 end if;
4691 end if;
4693 -- If the No_Streams restriction is set, check that the type of the
4694 -- object is not, and does not contain, any subtype derived from
4695 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4696 -- Has_Stream just for efficiency reasons. There is no point in
4697 -- spending time on a Has_Stream check if the restriction is not set.
4699 if Restriction_Check_Required (No_Streams) then
4700 if Has_Stream (T) then
4701 Check_Restriction (No_Streams, N);
4702 end if;
4703 end if;
4705 -- Deal with predicate check before we start to do major rewriting. It
4706 -- is OK to initialize and then check the initialized value, since the
4707 -- object goes out of scope if we get a predicate failure. Note that we
4708 -- do this in the analyzer and not the expander because the analyzer
4709 -- does some substantial rewriting in some cases.
4711 -- We need a predicate check if the type has predicates that are not
4712 -- ignored, and if either there is an initializing expression, or for
4713 -- default initialization when we have at least one case of an explicit
4714 -- default initial value (including via a Default_Value or
4715 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4716 -- an internal declaration whose initialization comes later (as for an
4717 -- aggregate expansion) or a deferred constant.
4718 -- If expression is an aggregate it may be expanded into assignments
4719 -- and the declaration itself is marked with No_Initialization, but
4720 -- the predicate still applies.
4722 if not Suppress_Assignment_Checks (N)
4723 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4724 and then
4725 (not No_Initialization (N)
4726 or else (Present (E) and then Nkind (E) = N_Aggregate))
4727 and then
4728 (Present (E)
4729 or else
4730 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4731 and then not (Constant_Present (N) and then No (E))
4732 then
4733 -- If the type has a static predicate and the expression is known at
4734 -- compile time, see if the expression satisfies the predicate.
4735 -- In the case of a static expression, this must be done even if
4736 -- the predicate is not enabled (as per static expression rules).
4738 if Present (E) then
4739 Check_Expression_Against_Static_Predicate (E, T);
4740 end if;
4742 -- Do not perform further predicate-related checks unless
4743 -- predicates are enabled for the subtype.
4745 if not Predicate_Enabled (T) then
4746 null;
4748 -- If the type is a null record and there is no explicit initial
4749 -- expression, no predicate check applies.
4751 elsif No (E) and then Is_Null_Record_Type (T) then
4752 null;
4754 -- If there is an address clause for this object, do not generate a
4755 -- predicate check here. It will be generated later, at the freezng
4756 -- point. It would be wrong to generate references to the object
4757 -- here, before the address has been determined.
4759 elsif Has_Aspect (Id, Aspect_Address)
4760 or else Present (Following_Address_Clause (N))
4761 then
4762 null;
4764 -- Do not generate a predicate check if the initialization expression
4765 -- is a type conversion whose target subtype statically matches the
4766 -- object's subtype because the conversion has been subjected to the
4767 -- same check. This is a small optimization which avoids redundant
4768 -- checks.
4770 elsif Present (E)
4771 and then Nkind (E) in N_Type_Conversion
4772 and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
4773 then
4774 null;
4776 else
4777 -- The check must be inserted after the expanded aggregate
4778 -- expansion code, if any.
4780 declare
4781 Check : constant Node_Id :=
4782 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4783 begin
4784 if No (Next_Decl) then
4785 Append_To (List_Containing (N), Check);
4786 else
4787 Insert_Before (Next_Decl, Check);
4788 end if;
4789 end;
4790 end if;
4791 end if;
4793 -- Case of unconstrained type
4795 if not Is_Definite_Subtype (T) then
4797 -- Nothing to do in deferred constant case
4799 if Constant_Present (N) and then No (E) then
4800 null;
4802 -- Case of no initialization present
4804 elsif No (E) then
4805 if No_Initialization (N) then
4806 null;
4808 elsif Is_Class_Wide_Type (T) then
4809 Error_Msg_N
4810 ("initialization required in class-wide declaration", N);
4812 else
4813 Error_Msg_N
4814 ("unconstrained subtype not allowed (need initialization)",
4815 Object_Definition (N));
4817 if Is_Record_Type (T) and then Has_Discriminants (T) then
4818 Error_Msg_N
4819 ("\provide initial value or explicit discriminant values",
4820 Object_Definition (N));
4822 Error_Msg_NE
4823 ("\or give default discriminant values for type&",
4824 Object_Definition (N), T);
4826 elsif Is_Array_Type (T) then
4827 Error_Msg_N
4828 ("\provide initial value or explicit array bounds",
4829 Object_Definition (N));
4830 end if;
4831 end if;
4833 -- Case of initialization present but in error. Set initial
4834 -- expression as absent (but do not make above complaints).
4836 elsif E = Error then
4837 Set_Expression (N, Empty);
4838 E := Empty;
4840 -- Case of initialization present
4842 else
4843 -- Unconstrained variables not allowed in Ada 83
4845 if Ada_Version = Ada_83
4846 and then not Constant_Present (N)
4847 and then Comes_From_Source (Object_Definition (N))
4848 then
4849 Error_Msg_N
4850 ("(Ada 83) unconstrained variable not allowed",
4851 Object_Definition (N));
4852 end if;
4854 -- Now we constrain the variable from the initializing expression
4856 -- If the expression is an aggregate, it has been expanded into
4857 -- individual assignments. Retrieve the actual type from the
4858 -- expanded construct.
4860 if Is_Array_Type (T)
4861 and then No_Initialization (N)
4862 and then Nkind (Original_Node (E)) = N_Aggregate
4863 then
4864 Act_T := Etype (E);
4866 -- In case of class-wide interface object declarations we delay
4867 -- the generation of the equivalent record type declarations until
4868 -- its expansion because there are cases in they are not required.
4870 elsif Is_Interface (T) then
4871 null;
4873 -- If the type is an unchecked union, no subtype can be built from
4874 -- the expression. Rewrite declaration as a renaming, which the
4875 -- back-end can handle properly. This is a rather unusual case,
4876 -- because most unchecked_union declarations have default values
4877 -- for discriminants and are thus not indefinite.
4879 elsif Is_Unchecked_Union (T) then
4880 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4881 Mutate_Ekind (Id, E_Constant);
4882 else
4883 Mutate_Ekind (Id, E_Variable);
4884 end if;
4886 -- If the expression is an aggregate it contains the required
4887 -- discriminant values but it has not been resolved yet, so do
4888 -- it now, and treat it as the initial expression of an object
4889 -- declaration, rather than a renaming.
4891 if Nkind (E) = N_Aggregate then
4892 Analyze_And_Resolve (E, T);
4894 else
4895 Rewrite (N,
4896 Make_Object_Renaming_Declaration (Loc,
4897 Defining_Identifier => Id,
4898 Subtype_Mark => New_Occurrence_Of (T, Loc),
4899 Name => E));
4901 Set_Renamed_Object (Id, E);
4902 Freeze_Before (N, T);
4903 Set_Is_Frozen (Id);
4904 goto Leave;
4905 end if;
4907 else
4908 -- Ensure that the generated subtype has a unique external name
4909 -- when the related object is public. This guarantees that the
4910 -- subtype and its bounds will not be affected by switches or
4911 -- pragmas that may offset the internal counter due to extra
4912 -- generated code.
4914 if Is_Public (Id) then
4915 Related_Id := Id;
4916 else
4917 Related_Id := Empty;
4918 end if;
4920 -- If the object has an unconstrained array subtype with fixed
4921 -- lower bound, then sliding to that bound may be needed.
4923 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4924 Expand_Sliding_Conversion (E, T);
4925 end if;
4927 if In_Spec_Expression and then In_Declare_Expr > 0 then
4928 -- It is too early to be doing expansion-ish things,
4929 -- so exit early. But we have to set Ekind (Id) now so
4930 -- that subsequent uses of this entity are not rejected
4931 -- via the same mechanism that (correctly) rejects
4932 -- "X : Integer := X;".
4934 if Constant_Present (N) then
4935 Mutate_Ekind (Id, E_Constant);
4936 Set_Is_True_Constant (Id);
4937 else
4938 Mutate_Ekind (Id, E_Variable);
4939 if Present (E) then
4940 Set_Has_Initial_Value (Id);
4941 end if;
4942 end if;
4944 goto Leave;
4945 end if;
4947 Expand_Subtype_From_Expr
4948 (N => N,
4949 Unc_Type => T,
4950 Subtype_Indic => Object_Definition (N),
4951 Exp => E,
4952 Related_Id => Related_Id);
4954 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4955 end if;
4957 if Act_T /= T then
4958 declare
4959 Full_Act_T : constant Entity_Id :=
4960 (if Is_Private_Type (Act_T)
4961 then Full_View (Act_T)
4962 else Empty);
4963 -- Propagate attributes to full view when needed
4965 begin
4966 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4968 if Present (Full_Act_T) then
4969 Set_Is_Constr_Subt_For_U_Nominal (Full_Act_T);
4970 end if;
4972 -- If the object is aliased, then it may be pointed to by an
4973 -- access-to-unconstrained-array value, which means that it
4974 -- must be allocated with its bounds.
4976 if Aliased_Present (N)
4977 and then (Is_Array_Type (Act_T)
4978 or else (Present (Full_Act_T)
4979 and then Is_Array_Type (Full_Act_T)))
4980 then
4981 Set_Is_Constr_Array_Subt_With_Bounds (Act_T);
4983 if Present (Full_Act_T) then
4984 Set_Is_Constr_Array_Subt_With_Bounds (Full_Act_T);
4985 end if;
4986 end if;
4988 Freeze_Before (N, Act_T);
4989 end;
4990 end if;
4992 Freeze_Before (N, T);
4993 end if;
4995 elsif Is_Array_Type (T)
4996 and then No_Initialization (N)
4997 and then (Nkind (Original_Node (E)) = N_Aggregate
4998 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4999 and then Nkind (Original_Node (Expression
5000 (Original_Node (E)))) = N_Aggregate))
5001 then
5002 if not Is_Entity_Name (Object_Definition (N)) then
5003 Act_T := Etype (E);
5004 Check_Compile_Time_Size (Act_T);
5005 end if;
5007 -- When the given object definition and the aggregate are specified
5008 -- independently, and their lengths might differ do a length check.
5009 -- This cannot happen if the aggregate is of the form (others =>...)
5011 if Nkind (E) = N_Raise_Constraint_Error then
5013 -- Aggregate is statically illegal. Place back in declaration
5015 Set_Expression (N, E);
5016 Set_No_Initialization (N, False);
5018 elsif T = Etype (E) then
5019 null;
5021 elsif Nkind (E) = N_Aggregate
5022 and then Present (Component_Associations (E))
5023 and then Present (Choice_List (First (Component_Associations (E))))
5024 and then
5025 Nkind (First (Choice_List (First (Component_Associations (E))))) =
5026 N_Others_Choice
5027 then
5028 null;
5030 else
5031 Apply_Length_Check (E, T);
5032 end if;
5034 -- When possible, and not a deferred constant, build the default subtype
5036 elsif Build_Default_Subtype_OK (T)
5037 and then (not Constant_Present (N) or else Present (E))
5038 then
5039 if No (E) then
5040 Act_T := Build_Default_Subtype (T, N);
5041 else
5042 -- Ada 2005: A limited object may be initialized by means of an
5043 -- aggregate. If the type has default discriminants it has an
5044 -- unconstrained nominal type, Its actual subtype will be obtained
5045 -- from the aggregate, and not from the default discriminants.
5047 Act_T := Etype (E);
5048 end if;
5050 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
5051 Freeze_Before (N, Act_T);
5053 elsif Nkind (E) = N_Function_Call
5054 and then Constant_Present (N)
5055 and then Has_Unconstrained_Elements (Etype (E))
5056 then
5057 -- The back-end has problems with constants of a discriminated type
5058 -- with defaults, if the initial value is a function call. We
5059 -- generate an intermediate temporary that will receive a reference
5060 -- to the result of the call. The initialization expression then
5061 -- becomes a dereference of that temporary.
5063 Remove_Side_Effects (E);
5065 -- If this is a constant declaration of an unconstrained type and
5066 -- the initialization is an aggregate, we can use the subtype of the
5067 -- aggregate for the declared entity because it is immutable.
5069 elsif not Is_Constrained (T)
5070 and then Has_Discriminants (T)
5071 and then Constant_Present (N)
5072 and then not Has_Unchecked_Union (T)
5073 and then Nkind (E) = N_Aggregate
5074 then
5075 Act_T := Etype (E);
5076 end if;
5078 -- Check No_Wide_Characters restriction
5080 Check_Wide_Character_Restriction (T, Object_Definition (N));
5082 -- Indicate this is not set in source. Certainly true for constants, and
5083 -- true for variables so far (will be reset for a variable if and when
5084 -- we encounter a modification in the source).
5086 Set_Never_Set_In_Source (Id);
5088 -- Now establish the proper kind and type of the object
5090 if Ekind (Id) = E_Void then
5091 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5092 end if;
5094 if Constant_Present (N) then
5095 Mutate_Ekind (Id, E_Constant);
5096 Set_Is_True_Constant (Id);
5098 else
5099 Mutate_Ekind (Id, E_Variable);
5101 -- A variable is set as shared passive if it appears in a shared
5102 -- passive package, and is at the outer level. This is not done for
5103 -- entities generated during expansion, because those are always
5104 -- manipulated locally.
5106 if Is_Shared_Passive (Current_Scope)
5107 and then Is_Library_Level_Entity (Id)
5108 and then Comes_From_Source (Id)
5109 then
5110 Set_Is_Shared_Passive (Id);
5111 Check_Shared_Var (Id, T, N);
5112 end if;
5114 -- Set Has_Initial_Value if initializing expression present. Note
5115 -- that if there is no initializing expression, we leave the state
5116 -- of this flag unchanged (usually it will be False, but notably in
5117 -- the case of exception choice variables, it will already be true).
5119 if Present (E) then
5120 Set_Has_Initial_Value (Id);
5121 end if;
5122 end if;
5124 -- Set the SPARK mode from the current context (may be overwritten later
5125 -- with explicit pragma).
5127 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5128 Set_SPARK_Pragma_Inherited (Id);
5130 -- Preserve relevant elaboration-related attributes of the context which
5131 -- are no longer available or very expensive to recompute once analysis,
5132 -- resolution, and expansion are over.
5134 Mark_Elaboration_Attributes
5135 (N_Id => Id,
5136 Checks => True,
5137 Warnings => True);
5139 -- Initialize alignment and size and capture alignment setting
5141 Reinit_Alignment (Id);
5142 Reinit_Esize (Id);
5143 Set_Optimize_Alignment_Flags (Id);
5145 -- Deal with aliased case
5147 if Aliased_Present (N) then
5148 Set_Is_Aliased (Id);
5150 -- AI12-001: All aliased objects are considered to be specified as
5151 -- independently addressable (RM C.6(8.1/4)).
5153 Set_Is_Independent (Id);
5155 -- If the object is aliased and the type is unconstrained with
5156 -- defaulted discriminants and there is no expression, then the
5157 -- object is constrained by the defaults, so it is worthwhile
5158 -- building the corresponding subtype.
5160 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5161 -- unconstrained, then only establish an actual subtype if the
5162 -- nominal subtype is indefinite. In definite cases the object is
5163 -- unconstrained in Ada 2005.
5165 if No (E)
5166 and then Is_Record_Type (T)
5167 and then not Is_Constrained (T)
5168 and then Has_Discriminants (T)
5169 and then (Ada_Version < Ada_2005
5170 or else not Is_Definite_Subtype (T))
5171 then
5172 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5173 end if;
5174 end if;
5176 -- Now we can set the type of the object
5178 Set_Etype (Id, Act_T);
5180 -- Non-constant object is marked to be treated as volatile if type is
5181 -- volatile and we clear the Current_Value setting that may have been
5182 -- set above. Doing so for constants isn't required and might interfere
5183 -- with possible uses of the object as a static expression in contexts
5184 -- incompatible with volatility (e.g. as a case-statement alternative).
5186 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5187 Set_Treat_As_Volatile (Id);
5188 Set_Current_Value (Id, Empty);
5189 end if;
5191 -- Deal with controlled types
5193 if Has_Controlled_Component (Etype (Id))
5194 or else Is_Controlled (Etype (Id))
5195 then
5196 if not Is_Library_Level_Entity (Id) then
5197 Check_Restriction (No_Nested_Finalization, N);
5198 else
5199 Validate_Controlled_Object (Id);
5200 end if;
5201 end if;
5203 if Has_Task (Etype (Id)) then
5204 Check_Restriction (No_Tasking, N);
5206 -- Deal with counting max tasks
5208 -- Nothing to do if inside a generic
5210 if Inside_A_Generic then
5211 null;
5213 -- If library level entity, then count tasks
5215 elsif Is_Library_Level_Entity (Id) then
5216 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5218 -- If not library level entity, then indicate we don't know max
5219 -- tasks and also check task hierarchy restriction and blocking
5220 -- operation (since starting a task is definitely blocking).
5222 else
5223 Check_Restriction (Max_Tasks, N);
5224 Check_Restriction (No_Task_Hierarchy, N);
5225 Check_Potentially_Blocking_Operation (N);
5226 end if;
5228 -- A rather specialized test. If we see two tasks being declared
5229 -- of the same type in the same object declaration, and the task
5230 -- has an entry with an address clause, we know that program error
5231 -- will be raised at run time since we can't have two tasks with
5232 -- entries at the same address.
5234 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5235 declare
5236 E : Entity_Id;
5238 begin
5239 E := First_Entity (Etype (Id));
5240 while Present (E) loop
5241 if Ekind (E) = E_Entry
5242 and then Present (Get_Attribute_Definition_Clause
5243 (E, Attribute_Address))
5244 then
5245 Error_Msg_Warn := SPARK_Mode /= On;
5246 Error_Msg_N
5247 ("more than one task with same entry address<<", N);
5248 Error_Msg_N ("\Program_Error [<<", N);
5249 Insert_Action (N,
5250 Make_Raise_Program_Error (Loc,
5251 Reason => PE_Duplicated_Entry_Address));
5252 exit;
5253 end if;
5255 Next_Entity (E);
5256 end loop;
5257 end;
5258 end if;
5259 end if;
5261 -- Check specific legality rules for a return object
5263 if Is_Return_Object (Id) then
5264 Check_Return_Subtype_Indication (N);
5265 end if;
5267 -- Some simple constant-propagation: if the expression is a constant
5268 -- string initialized with a literal, share the literal. This avoids
5269 -- a run-time copy.
5271 if Present (E)
5272 and then Is_Entity_Name (E)
5273 and then Ekind (Entity (E)) = E_Constant
5274 and then Base_Type (Etype (E)) = Standard_String
5275 then
5276 declare
5277 Val : constant Node_Id := Constant_Value (Entity (E));
5278 begin
5279 if Present (Val) and then Nkind (Val) = N_String_Literal then
5280 Rewrite (E, New_Copy (Val));
5281 end if;
5282 end;
5283 end if;
5285 if Present (Prev_Entity)
5286 and then Is_Frozen (Prev_Entity)
5287 and then not Error_Posted (Id)
5288 then
5289 Error_Msg_N ("full constant declaration appears too late", N);
5290 end if;
5292 Check_Eliminated (Id);
5294 -- Deal with setting In_Private_Part flag if in private part
5296 if Ekind (Scope (Id)) = E_Package
5297 and then In_Private_Part (Scope (Id))
5298 then
5299 Set_In_Private_Part (Id);
5300 end if;
5302 <<Leave>>
5303 -- Initialize the refined state of a variable here because this is a
5304 -- common destination for legal and illegal object declarations.
5306 if Ekind (Id) = E_Variable then
5307 Set_Encapsulating_State (Id, Empty);
5308 end if;
5310 Analyze_Aspect_Specifications (N, Id);
5312 Analyze_Dimension (N);
5314 -- Verify whether the object declaration introduces an illegal hidden
5315 -- state within a package subject to a null abstract state.
5317 if Ekind (Id) = E_Variable then
5318 Check_No_Hidden_State (Id);
5319 end if;
5321 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5322 end Analyze_Object_Declaration;
5324 ---------------------------
5325 -- Analyze_Others_Choice --
5326 ---------------------------
5328 -- Nothing to do for the others choice node itself, the semantic analysis
5329 -- of the others choice will occur as part of the processing of the parent
5331 procedure Analyze_Others_Choice (N : Node_Id) is
5332 pragma Warnings (Off, N);
5333 begin
5334 null;
5335 end Analyze_Others_Choice;
5337 -------------------------------------------
5338 -- Analyze_Private_Extension_Declaration --
5339 -------------------------------------------
5341 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5342 Indic : constant Node_Id := Subtype_Indication (N);
5343 T : constant Entity_Id := Defining_Identifier (N);
5344 Iface : Entity_Id;
5345 Iface_Elmt : Elmt_Id;
5346 Parent_Base : Entity_Id;
5347 Parent_Type : Entity_Id;
5349 begin
5350 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5352 if Is_Non_Empty_List (Interface_List (N)) then
5353 declare
5354 Intf : Node_Id;
5355 T : Entity_Id;
5357 begin
5358 Intf := First (Interface_List (N));
5359 while Present (Intf) loop
5360 T := Find_Type_Of_Subtype_Indic (Intf);
5362 Diagnose_Interface (Intf, T);
5363 Next (Intf);
5364 end loop;
5365 end;
5366 end if;
5368 Generate_Definition (T);
5370 -- For other than Ada 2012, just enter the name in the current scope
5372 if Ada_Version < Ada_2012 then
5373 Enter_Name (T);
5375 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5376 -- case of private type that completes an incomplete type.
5378 else
5379 declare
5380 Prev : Entity_Id;
5382 begin
5383 Prev := Find_Type_Name (N);
5385 pragma Assert (Prev = T
5386 or else (Ekind (Prev) = E_Incomplete_Type
5387 and then Present (Full_View (Prev))
5388 and then Full_View (Prev) = T));
5389 end;
5390 end if;
5392 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5393 Parent_Base := Base_Type (Parent_Type);
5395 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5396 Mutate_Ekind (T, Ekind (Parent_Type));
5397 Set_Etype (T, Any_Type);
5398 goto Leave;
5400 elsif not Is_Tagged_Type (Parent_Type) then
5401 Error_Msg_N
5402 ("parent of type extension must be a tagged type", Indic);
5403 goto Leave;
5405 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5406 Error_Msg_N ("premature derivation of incomplete type", Indic);
5407 goto Leave;
5409 elsif Is_Concurrent_Type (Parent_Type) then
5410 Error_Msg_N
5411 ("parent type of a private extension cannot be a synchronized "
5412 & "tagged type (RM 3.9.1 (3/1))", N);
5414 Set_Etype (T, Any_Type);
5415 Mutate_Ekind (T, E_Limited_Private_Type);
5416 Set_Private_Dependents (T, New_Elmt_List);
5417 Set_Error_Posted (T);
5418 goto Leave;
5419 end if;
5421 Check_Wide_Character_Restriction (Parent_Type, Indic);
5423 -- Perhaps the parent type should be changed to the class-wide type's
5424 -- specific type in this case to prevent cascading errors ???
5426 if Is_Class_Wide_Type (Parent_Type) then
5427 Error_Msg_N
5428 ("parent of type extension must not be a class-wide type", Indic);
5429 goto Leave;
5430 end if;
5432 if (not Is_Package_Or_Generic_Package (Current_Scope)
5433 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5434 or else In_Private_Part (Current_Scope)
5435 then
5436 Error_Msg_N ("invalid context for private extension", N);
5437 end if;
5439 -- Set common attributes
5441 Set_Is_Pure (T, Is_Pure (Current_Scope));
5442 Set_Scope (T, Current_Scope);
5443 Mutate_Ekind (T, E_Record_Type_With_Private);
5444 Reinit_Size_Align (T);
5445 Set_Default_SSO (T);
5446 Set_No_Reordering (T, No_Component_Reordering);
5448 Set_Etype (T, Parent_Base);
5449 Propagate_Concurrent_Flags (T, Parent_Base);
5451 Set_Convention (T, Convention (Parent_Type));
5452 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5453 Set_Is_First_Subtype (T);
5455 -- Set the SPARK mode from the current context
5457 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5458 Set_SPARK_Pragma_Inherited (T);
5460 if Unknown_Discriminants_Present (N) then
5461 Set_Discriminant_Constraint (T, No_Elist);
5462 end if;
5464 Build_Derived_Record_Type (N, Parent_Type, T);
5466 -- A private extension inherits the Default_Initial_Condition pragma
5467 -- coming from any parent type within the derivation chain.
5469 if Has_DIC (Parent_Type) then
5470 Set_Has_Inherited_DIC (T);
5471 end if;
5473 -- A private extension inherits any class-wide invariants coming from a
5474 -- parent type or an interface. Note that the invariant procedure of the
5475 -- parent type should not be inherited because the private extension may
5476 -- define invariants of its own.
5478 if Has_Inherited_Invariants (Parent_Type)
5479 or else Has_Inheritable_Invariants (Parent_Type)
5480 then
5481 Set_Has_Inherited_Invariants (T);
5483 elsif Present (Interfaces (T)) then
5484 Iface_Elmt := First_Elmt (Interfaces (T));
5485 while Present (Iface_Elmt) loop
5486 Iface := Node (Iface_Elmt);
5488 if Has_Inheritable_Invariants (Iface) then
5489 Set_Has_Inherited_Invariants (T);
5490 exit;
5491 end if;
5493 Next_Elmt (Iface_Elmt);
5494 end loop;
5495 end if;
5497 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5498 -- synchronized formal derived type.
5500 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5501 Set_Is_Limited_Record (T);
5503 -- Formal derived type case
5505 if Is_Generic_Type (T) then
5507 -- The parent must be a tagged limited type or a synchronized
5508 -- interface.
5510 if (not Is_Tagged_Type (Parent_Type)
5511 or else not Is_Limited_Type (Parent_Type))
5512 and then
5513 (not Is_Interface (Parent_Type)
5514 or else not Is_Synchronized_Interface (Parent_Type))
5515 then
5516 Error_Msg_NE
5517 ("parent type of & must be tagged limited or synchronized",
5518 N, T);
5519 end if;
5521 -- The progenitors (if any) must be limited or synchronized
5522 -- interfaces.
5524 if Present (Interfaces (T)) then
5525 Iface_Elmt := First_Elmt (Interfaces (T));
5526 while Present (Iface_Elmt) loop
5527 Iface := Node (Iface_Elmt);
5529 if not Is_Limited_Interface (Iface)
5530 and then not Is_Synchronized_Interface (Iface)
5531 then
5532 Error_Msg_NE
5533 ("progenitor & must be limited or synchronized",
5534 N, Iface);
5535 end if;
5537 Next_Elmt (Iface_Elmt);
5538 end loop;
5539 end if;
5541 -- Regular derived extension, the parent must be a limited or
5542 -- synchronized interface.
5544 else
5545 if not Is_Interface (Parent_Type)
5546 or else (not Is_Limited_Interface (Parent_Type)
5547 and then not Is_Synchronized_Interface (Parent_Type))
5548 then
5549 Error_Msg_NE
5550 ("parent type of & must be limited interface", N, T);
5551 end if;
5552 end if;
5554 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5555 -- extension with a synchronized parent must be explicitly declared
5556 -- synchronized, because the full view will be a synchronized type.
5557 -- This must be checked before the check for limited types below,
5558 -- to ensure that types declared limited are not allowed to extend
5559 -- synchronized interfaces.
5561 elsif Is_Interface (Parent_Type)
5562 and then Is_Synchronized_Interface (Parent_Type)
5563 and then not Synchronized_Present (N)
5564 then
5565 Error_Msg_NE
5566 ("private extension of& must be explicitly synchronized",
5567 N, Parent_Type);
5569 elsif Limited_Present (N) then
5570 Set_Is_Limited_Record (T);
5572 if not Is_Limited_Type (Parent_Type)
5573 and then
5574 (not Is_Interface (Parent_Type)
5575 or else not Is_Limited_Interface (Parent_Type))
5576 then
5577 Error_Msg_NE ("parent type& of limited extension must be limited",
5578 N, Parent_Type);
5579 end if;
5580 end if;
5582 -- Remember that its parent type has a private extension. Used to warn
5583 -- on public primitives of the parent type defined after its private
5584 -- extensions (see Check_Dispatching_Operation).
5586 Set_Has_Private_Extension (Parent_Type);
5588 <<Leave>>
5589 Analyze_Aspect_Specifications (N, T);
5590 end Analyze_Private_Extension_Declaration;
5592 ---------------------------------
5593 -- Analyze_Subtype_Declaration --
5594 ---------------------------------
5596 procedure Analyze_Subtype_Declaration
5597 (N : Node_Id;
5598 Skip : Boolean := False)
5600 Id : constant Entity_Id := Defining_Identifier (N);
5601 T : Entity_Id;
5603 begin
5604 Generate_Definition (Id);
5605 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5606 Reinit_Size_Align (Id);
5608 -- The following guard condition on Enter_Name is to handle cases where
5609 -- the defining identifier has already been entered into the scope but
5610 -- the declaration as a whole needs to be analyzed.
5612 -- This case in particular happens for derived enumeration types. The
5613 -- derived enumeration type is processed as an inserted enumeration type
5614 -- declaration followed by a rewritten subtype declaration. The defining
5615 -- identifier, however, is entered into the name scope very early in the
5616 -- processing of the original type declaration and therefore needs to be
5617 -- avoided here, when the created subtype declaration is analyzed. (See
5618 -- Build_Derived_Types)
5620 -- This also happens when the full view of a private type is a derived
5621 -- type with constraints. In this case the entity has been introduced
5622 -- in the private declaration.
5624 -- Finally this happens in some complex cases when validity checks are
5625 -- enabled, where the same subtype declaration may be analyzed twice.
5626 -- This can happen if the subtype is created by the preanalysis of
5627 -- an attribute that gives the range of a loop statement, and the loop
5628 -- itself appears within an if_statement that will be rewritten during
5629 -- expansion.
5631 if Skip
5632 or else (Present (Etype (Id))
5633 and then (Is_Private_Type (Etype (Id))
5634 or else Is_Task_Type (Etype (Id))
5635 or else Is_Rewrite_Substitution (N)))
5636 then
5637 null;
5639 elsif Current_Entity (Id) = Id then
5640 null;
5642 else
5643 Enter_Name (Id);
5644 end if;
5646 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5648 -- Class-wide equivalent types of records with unknown discriminants
5649 -- involve the generation of an itype which serves as the private view
5650 -- of a constrained record subtype. In such cases the base type of the
5651 -- current subtype we are processing is the private itype. Use the full
5652 -- of the private itype when decorating various attributes.
5654 if Is_Itype (T)
5655 and then Is_Private_Type (T)
5656 and then Present (Full_View (T))
5657 then
5658 T := Full_View (T);
5659 end if;
5661 -- Inherit common attributes
5663 Set_Is_Volatile (Id, Is_Volatile (T));
5664 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5665 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5666 Set_Convention (Id, Convention (T));
5668 -- If ancestor has predicates then so does the subtype, and in addition
5669 -- we must delay the freeze to properly arrange predicate inheritance.
5671 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5672 -- in which T = ID, so the above tests and assignments do nothing???
5674 if Has_Predicates (T)
5675 or else (Present (Ancestor_Subtype (T))
5676 and then Has_Predicates (Ancestor_Subtype (T)))
5677 then
5678 Set_Has_Predicates (Id);
5679 Set_Has_Delayed_Freeze (Id);
5681 -- Generated subtypes inherit the predicate function from the parent
5682 -- (no aspects to examine on the generated declaration).
5684 if not Comes_From_Source (N) then
5685 Mutate_Ekind (Id, Ekind (T));
5687 if Present (Predicate_Function (Id)) then
5688 null;
5690 elsif Present (Predicate_Function (T)) then
5691 Set_Predicate_Function (Id, Predicate_Function (T));
5693 elsif Present (Ancestor_Subtype (T))
5694 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5695 then
5696 Set_Predicate_Function (Id,
5697 Predicate_Function (Ancestor_Subtype (T)));
5698 end if;
5699 end if;
5700 end if;
5702 -- In the case where there is no constraint given in the subtype
5703 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5704 -- semantic attributes must be established here.
5706 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5707 Set_Etype (Id, Base_Type (T));
5709 case Ekind (T) is
5710 when Array_Kind =>
5711 Mutate_Ekind (Id, E_Array_Subtype);
5712 Copy_Array_Subtype_Attributes (Id, T);
5713 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5715 when Decimal_Fixed_Point_Kind =>
5716 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5717 Set_Digits_Value (Id, Digits_Value (T));
5718 Set_Delta_Value (Id, Delta_Value (T));
5719 Set_Scale_Value (Id, Scale_Value (T));
5720 Set_Small_Value (Id, Small_Value (T));
5721 Set_Scalar_Range (Id, Scalar_Range (T));
5722 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5723 Set_Is_Constrained (Id, Is_Constrained (T));
5724 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5725 Copy_RM_Size (To => Id, From => T);
5727 when Enumeration_Kind =>
5728 Mutate_Ekind (Id, E_Enumeration_Subtype);
5729 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5730 Set_Scalar_Range (Id, Scalar_Range (T));
5731 Set_Is_Character_Type (Id, Is_Character_Type (T));
5732 Set_Is_Constrained (Id, Is_Constrained (T));
5733 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5734 Copy_RM_Size (To => Id, From => T);
5736 when Ordinary_Fixed_Point_Kind =>
5737 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5738 Set_Scalar_Range (Id, Scalar_Range (T));
5739 Set_Small_Value (Id, Small_Value (T));
5740 Set_Delta_Value (Id, Delta_Value (T));
5741 Set_Is_Constrained (Id, Is_Constrained (T));
5742 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5743 Copy_RM_Size (To => Id, From => T);
5745 when Float_Kind =>
5746 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5747 Set_Scalar_Range (Id, Scalar_Range (T));
5748 Set_Digits_Value (Id, Digits_Value (T));
5749 Set_Is_Constrained (Id, Is_Constrained (T));
5751 -- If the floating point type has dimensions, these will be
5752 -- inherited subsequently when Analyze_Dimensions is called.
5754 when Signed_Integer_Kind =>
5755 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5756 Set_Scalar_Range (Id, Scalar_Range (T));
5757 Set_Is_Constrained (Id, Is_Constrained (T));
5758 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5759 Copy_RM_Size (To => Id, From => T);
5761 when Modular_Integer_Kind =>
5762 Mutate_Ekind (Id, E_Modular_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 Class_Wide_Kind =>
5769 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5770 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5771 Set_Cloned_Subtype (Id, T);
5772 Set_Is_Tagged_Type (Id, True);
5773 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5774 Set_Has_Unknown_Discriminants
5775 (Id, True);
5776 Set_No_Tagged_Streams_Pragma
5777 (Id, No_Tagged_Streams_Pragma (T));
5779 if Ekind (T) = E_Class_Wide_Subtype then
5780 Set_Equivalent_Type (Id, Equivalent_Type (T));
5781 end if;
5783 when E_Record_Subtype
5784 | E_Record_Type
5786 Mutate_Ekind (Id, E_Record_Subtype);
5788 -- Subtype declarations introduced for formal type parameters
5789 -- in generic instantiations should inherit the Size value of
5790 -- the type they rename.
5792 if Present (Generic_Parent_Type (N)) then
5793 Copy_RM_Size (To => Id, From => T);
5794 end if;
5796 if Ekind (T) = E_Record_Subtype
5797 and then Present (Cloned_Subtype (T))
5798 then
5799 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5800 else
5801 Set_Cloned_Subtype (Id, T);
5802 end if;
5804 Set_First_Entity (Id, First_Entity (T));
5805 Set_Last_Entity (Id, Last_Entity (T));
5806 Set_Has_Discriminants (Id, Has_Discriminants (T));
5807 Set_Is_Constrained (Id, Is_Constrained (T));
5808 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5809 Set_Has_Implicit_Dereference
5810 (Id, Has_Implicit_Dereference (T));
5811 Set_Has_Unknown_Discriminants
5812 (Id, Has_Unknown_Discriminants (T));
5814 if Has_Discriminants (T) then
5815 Set_Discriminant_Constraint
5816 (Id, Discriminant_Constraint (T));
5817 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5819 elsif Has_Unknown_Discriminants (Id) then
5820 Set_Discriminant_Constraint (Id, No_Elist);
5821 end if;
5823 if Is_Tagged_Type (T) then
5824 Set_Is_Tagged_Type (Id, True);
5825 Set_No_Tagged_Streams_Pragma
5826 (Id, No_Tagged_Streams_Pragma (T));
5827 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5828 Set_Direct_Primitive_Operations
5829 (Id, Direct_Primitive_Operations (T));
5830 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5832 if Is_Interface (T) then
5833 Set_Is_Interface (Id);
5834 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5835 end if;
5836 end if;
5838 when Private_Kind =>
5839 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5840 Set_Has_Discriminants (Id, Has_Discriminants (T));
5841 Set_Is_Constrained (Id, Is_Constrained (T));
5842 Set_First_Entity (Id, First_Entity (T));
5843 Set_Last_Entity (Id, Last_Entity (T));
5844 Set_Private_Dependents (Id, New_Elmt_List);
5845 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5846 Set_Has_Implicit_Dereference
5847 (Id, Has_Implicit_Dereference (T));
5848 Set_Has_Unknown_Discriminants
5849 (Id, Has_Unknown_Discriminants (T));
5850 Set_Known_To_Have_Preelab_Init
5851 (Id, Known_To_Have_Preelab_Init (T));
5853 if Is_Tagged_Type (T) then
5854 Set_Is_Tagged_Type (Id);
5855 Set_No_Tagged_Streams_Pragma (Id,
5856 No_Tagged_Streams_Pragma (T));
5857 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5858 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5859 Set_Direct_Primitive_Operations (Id,
5860 Direct_Primitive_Operations (T));
5861 end if;
5863 -- In general the attributes of the subtype of a private type
5864 -- are the attributes of the partial view of parent. However,
5865 -- the full view may be a discriminated type, and the subtype
5866 -- must share the discriminant constraint to generate correct
5867 -- calls to initialization procedures.
5869 if Has_Discriminants (T) then
5870 Set_Discriminant_Constraint
5871 (Id, Discriminant_Constraint (T));
5872 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5874 elsif Present (Full_View (T))
5875 and then Has_Discriminants (Full_View (T))
5876 then
5877 Set_Discriminant_Constraint
5878 (Id, Discriminant_Constraint (Full_View (T)));
5879 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5881 -- This would seem semantically correct, but apparently
5882 -- generates spurious errors about missing components ???
5884 -- Set_Has_Discriminants (Id);
5885 end if;
5887 Prepare_Private_Subtype_Completion (Id, N);
5889 -- If this is the subtype of a constrained private type with
5890 -- discriminants that has got a full view and we also have
5891 -- built a completion just above, show that the completion
5892 -- is a clone of the full view to the back-end.
5894 if Has_Discriminants (T)
5895 and then not Has_Unknown_Discriminants (T)
5896 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5897 and then Present (Full_View (T))
5898 and then Present (Full_View (Id))
5899 then
5900 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5901 end if;
5903 when Access_Kind =>
5904 Mutate_Ekind (Id, E_Access_Subtype);
5905 Set_Is_Constrained (Id, Is_Constrained (T));
5906 Set_Is_Access_Constant
5907 (Id, Is_Access_Constant (T));
5908 Set_Directly_Designated_Type
5909 (Id, Designated_Type (T));
5910 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5912 -- A Pure library_item must not contain the declaration of a
5913 -- named access type, except within a subprogram, generic
5914 -- subprogram, task unit, or protected unit, or if it has
5915 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5917 if Comes_From_Source (Id)
5918 and then In_Pure_Unit
5919 and then not In_Subprogram_Task_Protected_Unit
5920 and then not No_Pool_Assigned (Id)
5921 then
5922 Error_Msg_N
5923 ("named access types not allowed in pure unit", N);
5924 end if;
5926 when Concurrent_Kind =>
5927 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5928 Set_Corresponding_Record_Type (Id,
5929 Corresponding_Record_Type (T));
5930 Set_First_Entity (Id, First_Entity (T));
5931 Set_First_Private_Entity (Id, First_Private_Entity (T));
5932 Set_Has_Discriminants (Id, Has_Discriminants (T));
5933 Set_Is_Constrained (Id, Is_Constrained (T));
5934 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5935 Set_Last_Entity (Id, Last_Entity (T));
5937 if Is_Tagged_Type (T) then
5938 Set_No_Tagged_Streams_Pragma
5939 (Id, No_Tagged_Streams_Pragma (T));
5940 end if;
5942 if Has_Discriminants (T) then
5943 Set_Discriminant_Constraint
5944 (Id, Discriminant_Constraint (T));
5945 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5946 end if;
5948 when Incomplete_Kind =>
5949 if Ada_Version >= Ada_2005 then
5951 -- In Ada 2005 an incomplete type can be explicitly tagged:
5952 -- propagate indication. Note that we also have to include
5953 -- subtypes for Ada 2012 extended use of incomplete types.
5955 Mutate_Ekind (Id, E_Incomplete_Subtype);
5956 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5957 Set_Private_Dependents (Id, New_Elmt_List);
5959 if Is_Tagged_Type (Id) then
5960 Set_No_Tagged_Streams_Pragma
5961 (Id, No_Tagged_Streams_Pragma (T));
5962 end if;
5964 -- For tagged types, or when prefixed-call syntax is allowed
5965 -- for untagged types, initialize the list of primitive
5966 -- operations to an empty list.
5968 if Is_Tagged_Type (Id)
5969 or else Core_Extensions_Allowed
5970 then
5971 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5972 end if;
5974 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5975 -- incomplete type visible through a limited with clause.
5977 if From_Limited_With (T)
5978 and then Present (Non_Limited_View (T))
5979 then
5980 Set_From_Limited_With (Id);
5981 Set_Non_Limited_View (Id, Non_Limited_View (T));
5983 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5984 -- to the private dependents of the original incomplete
5985 -- type for future transformation.
5987 else
5988 Append_Elmt (Id, Private_Dependents (T));
5989 end if;
5991 -- If the subtype name denotes an incomplete type an error
5992 -- was already reported by Process_Subtype.
5994 else
5995 Set_Etype (Id, Any_Type);
5996 end if;
5998 when others =>
5999 raise Program_Error;
6000 end case;
6002 -- If there is no constraint in the subtype indication, the
6003 -- declared entity inherits predicates from the parent.
6005 Inherit_Predicate_Flags (Id, T);
6006 end if;
6008 if Etype (Id) = Any_Type then
6009 goto Leave;
6010 end if;
6012 -- When prefixed calls are enabled for untagged types, the subtype
6013 -- shares the primitive operations of its base type. Do this even
6014 -- when Extensions_Allowed is False to issue better error messages.
6016 Set_Direct_Primitive_Operations
6017 (Id, Direct_Primitive_Operations (Base_Type (T)));
6019 -- Some common processing on all types
6021 Set_Size_Info (Id, T);
6022 Set_First_Rep_Item (Id, First_Rep_Item (T));
6024 -- If the parent type is a generic actual, so is the subtype. This may
6025 -- happen in a nested instance. Why Comes_From_Source test???
6027 if not Comes_From_Source (N) then
6028 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
6029 end if;
6031 -- If this is a subtype declaration for an actual in an instance,
6032 -- inherit static and dynamic predicates if any.
6034 if Has_Predicates (T)
6035 and then Present (Predicate_Function (T))
6036 and then In_Instance
6037 and then not Comes_From_Source (N)
6038 then
6039 -- Inherit Subprograms_For_Type from the full view, if present
6041 if Present (Full_View (T))
6042 and then Present (Subprograms_For_Type (Full_View (T)))
6043 then
6044 Set_Subprograms_For_Type
6045 (Id, Subprograms_For_Type (Full_View (T)));
6046 else
6047 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
6048 end if;
6050 -- If the current declaration created both a private and a full view,
6051 -- then propagate Predicate_Function to the latter as well.
6053 if Present (Full_View (Id))
6054 and then No (Predicate_Function (Full_View (Id)))
6055 then
6056 Set_Subprograms_For_Type
6057 (Full_View (Id), Subprograms_For_Type (Id));
6058 end if;
6060 if Has_Static_Predicate (T) then
6061 Set_Has_Static_Predicate (Id);
6062 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
6063 end if;
6064 end if;
6066 -- If the base type is a scalar type, or else if there is no
6067 -- constraint, the atomic flag is inherited by the subtype.
6068 -- Ditto for the Independent aspect.
6070 if Is_Scalar_Type (Id)
6071 or else Is_Entity_Name (Subtype_Indication (N))
6072 then
6073 Set_Is_Atomic (Id, Is_Atomic (T));
6074 Set_Is_Independent (Id, Is_Independent (T));
6075 end if;
6077 -- Remaining processing depends on characteristics of base type
6079 T := Etype (Id);
6081 Set_Is_Immediately_Visible (Id, True);
6082 Set_Depends_On_Private (Id, Has_Private_Component (T));
6083 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6085 if Is_Interface (T) then
6086 Set_Is_Interface (Id);
6087 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6088 end if;
6090 if Present (Generic_Parent_Type (N))
6091 and then
6092 (Nkind (Parent (Generic_Parent_Type (N))) /=
6093 N_Formal_Type_Declaration
6094 or else Nkind (Formal_Type_Definition
6095 (Parent (Generic_Parent_Type (N)))) /=
6096 N_Formal_Private_Type_Definition)
6097 then
6098 if Is_Tagged_Type (Id) then
6100 -- If this is a generic actual subtype for a synchronized type,
6101 -- the primitive operations are those of the corresponding record
6102 -- for which there is a separate subtype declaration.
6104 if Is_Concurrent_Type (Id) then
6105 null;
6106 elsif Is_Class_Wide_Type (Id) then
6107 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6108 else
6109 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6110 end if;
6112 elsif Scope (Etype (Id)) /= Standard_Standard then
6113 Derive_Subprograms (Generic_Parent_Type (N), Id);
6114 end if;
6115 end if;
6117 if Is_Private_Type (T) and then Present (Full_View (T)) then
6118 Conditional_Delay (Id, Full_View (T));
6120 -- The subtypes of components or subcomponents of protected types
6121 -- do not need freeze nodes, which would otherwise appear in the
6122 -- wrong scope (before the freeze node for the protected type). The
6123 -- proper subtypes are those of the subcomponents of the corresponding
6124 -- record.
6126 elsif Ekind (Scope (Id)) /= E_Protected_Type
6127 and then Present (Scope (Scope (Id))) -- error defense
6128 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6129 then
6130 Conditional_Delay (Id, T);
6131 end if;
6133 -- If we have a subtype of an incomplete type whose full type is a
6134 -- derived numeric type, we need to have a freeze node for the subtype.
6135 -- Otherwise gigi will complain while computing the (static) bounds of
6136 -- the subtype.
6138 if Is_Itype (T)
6139 and then Is_Elementary_Type (Id)
6140 and then Etype (Id) /= Id
6141 then
6142 declare
6143 Partial : constant Entity_Id :=
6144 Incomplete_Or_Partial_View (First_Subtype (Id));
6145 begin
6146 if Present (Partial)
6147 and then Ekind (Partial) = E_Incomplete_Type
6148 then
6149 Set_Has_Delayed_Freeze (Id);
6150 end if;
6151 end;
6152 end if;
6154 -- Check that Constraint_Error is raised for a scalar subtype indication
6155 -- when the lower or upper bound of a non-null range lies outside the
6156 -- range of the type mark. Likewise for an array subtype, but check the
6157 -- compatibility for each index.
6159 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6160 declare
6161 Indic_Typ : constant Entity_Id :=
6162 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6163 Subt_Index : Node_Id;
6164 Target_Index : Node_Id;
6166 begin
6167 if Is_Scalar_Type (Etype (Id))
6168 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6169 then
6170 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6172 elsif Is_Array_Type (Etype (Id))
6173 and then Present (First_Index (Id))
6174 then
6175 Subt_Index := First_Index (Id);
6176 Target_Index := First_Index (Indic_Typ);
6178 while Present (Subt_Index) loop
6179 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6180 and then Is_Scalar_Type (Entity (Subt_Index)))
6181 or else Nkind (Subt_Index) = N_Subtype_Indication)
6182 and then
6183 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6184 then
6185 Apply_Range_Check
6186 (Scalar_Range (Etype (Subt_Index)),
6187 Etype (Target_Index),
6188 Insert_Node => N);
6189 end if;
6191 Next_Index (Subt_Index);
6192 Next_Index (Target_Index);
6193 end loop;
6194 end if;
6195 end;
6196 end if;
6198 Set_Optimize_Alignment_Flags (Id);
6199 Check_Eliminated (Id);
6201 <<Leave>>
6202 Analyze_Aspect_Specifications (N, Id);
6204 Analyze_Dimension (N);
6206 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6207 -- indications on composite types where the constraints are dynamic.
6208 -- Note that object declarations and aggregates generate implicit
6209 -- subtype declarations, which this covers. One special case is that the
6210 -- implicitly generated "=" for discriminated types includes an
6211 -- offending subtype declaration, which is harmless, so we ignore it
6212 -- here.
6214 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6215 declare
6216 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6217 begin
6218 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6219 and then not (Is_Internal (Id)
6220 and then Is_TSS (Scope (Id),
6221 TSS_Composite_Equality))
6222 and then not Within_Init_Proc
6223 and then not All_Composite_Constraints_Static (Cstr)
6224 then
6225 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6226 end if;
6227 end;
6228 end if;
6229 end Analyze_Subtype_Declaration;
6231 --------------------------------
6232 -- Analyze_Subtype_Indication --
6233 --------------------------------
6235 procedure Analyze_Subtype_Indication (N : Node_Id) is
6236 T : constant Entity_Id := Subtype_Mark (N);
6237 R : constant Node_Id := Range_Expression (Constraint (N));
6239 begin
6240 Analyze (T);
6242 if R = Error then
6243 Set_Error_Posted (R);
6244 Set_Error_Posted (T);
6245 else
6246 Analyze (R);
6247 Set_Etype (N, Etype (R));
6248 Resolve (R, Entity (T));
6249 end if;
6250 end Analyze_Subtype_Indication;
6252 --------------------------
6253 -- Analyze_Variant_Part --
6254 --------------------------
6256 procedure Analyze_Variant_Part (N : Node_Id) is
6257 Discr_Name : Node_Id;
6258 Discr_Type : Entity_Id;
6260 procedure Process_Variant (A : Node_Id);
6261 -- Analyze declarations for a single variant
6263 package Analyze_Variant_Choices is
6264 new Generic_Analyze_Choices (Process_Variant);
6265 use Analyze_Variant_Choices;
6267 ---------------------
6268 -- Process_Variant --
6269 ---------------------
6271 procedure Process_Variant (A : Node_Id) is
6272 CL : constant Node_Id := Component_List (A);
6273 begin
6274 if not Null_Present (CL) then
6275 Analyze_Declarations (Component_Items (CL));
6277 if Present (Variant_Part (CL)) then
6278 Analyze (Variant_Part (CL));
6279 end if;
6280 end if;
6281 end Process_Variant;
6283 -- Start of processing for Analyze_Variant_Part
6285 begin
6286 Discr_Name := Name (N);
6287 Analyze (Discr_Name);
6289 -- If Discr_Name bad, get out (prevent cascaded errors)
6291 if Etype (Discr_Name) = Any_Type then
6292 return;
6293 end if;
6295 -- Check invalid discriminant in variant part
6297 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6298 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6299 end if;
6301 Discr_Type := Etype (Entity (Discr_Name));
6303 if not Is_Discrete_Type (Discr_Type) then
6304 Error_Msg_N
6305 ("discriminant in a variant part must be of a discrete type",
6306 Name (N));
6307 return;
6308 end if;
6310 -- Now analyze the choices, which also analyzes the declarations that
6311 -- are associated with each choice.
6313 Analyze_Choices (Variants (N), Discr_Type);
6315 -- Note: we used to instantiate and call Check_Choices here to check
6316 -- that the choices covered the discriminant, but it's too early to do
6317 -- that because of statically predicated subtypes, whose analysis may
6318 -- be deferred to their freeze point which may be as late as the freeze
6319 -- point of the containing record. So this call is now to be found in
6320 -- Freeze_Record_Declaration.
6322 end Analyze_Variant_Part;
6324 ----------------------------
6325 -- Array_Type_Declaration --
6326 ----------------------------
6328 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6329 Component_Def : constant Node_Id := Component_Definition (Def);
6330 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6331 P : constant Node_Id := Parent (Def);
6332 Element_Type : Entity_Id;
6333 Implicit_Base : Entity_Id;
6334 Index : Node_Id;
6335 Nb_Index : Pos;
6336 Priv : Entity_Id;
6337 Related_Id : Entity_Id;
6338 Has_FLB_Index : Boolean := False;
6340 begin
6341 if Nkind (Def) = N_Constrained_Array_Definition then
6342 Index := First (Discrete_Subtype_Definitions (Def));
6343 else
6344 Index := First (Subtype_Marks (Def));
6345 end if;
6347 -- Find proper names for the implicit types which may be public. In case
6348 -- of anonymous arrays we use the name of the first object of that type
6349 -- as prefix.
6351 if No (T) then
6352 Related_Id := Defining_Identifier (P);
6353 else
6354 Related_Id := T;
6355 end if;
6357 Nb_Index := 1;
6358 while Present (Index) loop
6359 Analyze (Index);
6361 -- Test for odd case of trying to index a type by the type itself
6363 if Is_Entity_Name (Index) and then Entity (Index) = T then
6364 Error_Msg_N ("type& cannot be indexed by itself", Index);
6365 Set_Entity (Index, Standard_Boolean);
6366 Set_Etype (Index, Standard_Boolean);
6367 end if;
6369 -- Add a subtype declaration for each index of private array type
6370 -- declaration whose type is also private. For example:
6372 -- package Pkg is
6373 -- type Index is private;
6374 -- private
6375 -- type Table is array (Index) of ...
6376 -- end;
6378 -- This is currently required by the expander for the internally
6379 -- generated equality subprogram of records with variant parts in
6380 -- which the type of some component is such a private type. And it
6381 -- also helps semantic analysis in peculiar cases where the array
6382 -- type is referenced from an instance but not the index directly.
6384 if Is_Package_Or_Generic_Package (Current_Scope)
6385 and then In_Private_Part (Current_Scope)
6386 and then Has_Private_Declaration (Etype (Index))
6387 and then Scope (Etype (Index)) = Current_Scope
6388 then
6389 declare
6390 Loc : constant Source_Ptr := Sloc (Def);
6391 Decl : Node_Id;
6392 New_E : Entity_Id;
6394 begin
6395 New_E := Make_Temporary (Loc, 'T');
6396 Set_Is_Internal (New_E);
6398 Decl :=
6399 Make_Subtype_Declaration (Loc,
6400 Defining_Identifier => New_E,
6401 Subtype_Indication =>
6402 New_Occurrence_Of (Etype (Index), Loc));
6404 Insert_Before (Parent (Def), Decl);
6405 Analyze (Decl);
6406 Set_Etype (Index, New_E);
6408 -- If the index is a range or a subtype indication it carries
6409 -- no entity. Example:
6411 -- package Pkg is
6412 -- type T is private;
6413 -- private
6414 -- type T is new Natural;
6415 -- Table : array (T(1) .. T(10)) of Boolean;
6416 -- end Pkg;
6418 -- Otherwise the type of the reference is its entity.
6420 if Is_Entity_Name (Index) then
6421 Set_Entity (Index, New_E);
6422 end if;
6423 end;
6424 end if;
6426 Make_Index (Index, P, Related_Id, Nb_Index);
6428 -- In the case where we have an unconstrained array with an index
6429 -- given by a subtype_indication, this is necessarily a "fixed lower
6430 -- bound" index. We change the upper bound of that index to the upper
6431 -- bound of the index's subtype (denoted by the subtype_mark), since
6432 -- that upper bound was originally set by the parser to be the same
6433 -- as the lower bound. In truth, that upper bound corresponds to
6434 -- a box ("<>"), and could be set to Empty, but it's convenient to
6435 -- set it to the upper bound to avoid needing to add special tests
6436 -- in various places for an Empty upper bound, and in any case that
6437 -- accurately characterizes the index's range of values.
6439 if Nkind (Def) = N_Unconstrained_Array_Definition
6440 and then Nkind (Index) = N_Subtype_Indication
6441 then
6442 declare
6443 Index_Subtype_High_Bound : constant Entity_Id :=
6444 Type_High_Bound (Entity (Subtype_Mark (Index)));
6445 begin
6446 Set_High_Bound (Range_Expression (Constraint (Index)),
6447 Index_Subtype_High_Bound);
6449 -- Record that the array type has one or more indexes with
6450 -- a fixed lower bound.
6452 Has_FLB_Index := True;
6454 -- Mark the index as belonging to an array type with a fixed
6455 -- lower bound.
6457 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6458 end;
6459 end if;
6461 -- Check error of subtype with predicate for index type
6463 Bad_Predicated_Subtype_Use
6464 ("subtype& has predicate, not allowed as index subtype",
6465 Index, Etype (Index));
6467 -- Move to next index
6469 Next (Index);
6470 Nb_Index := Nb_Index + 1;
6471 end loop;
6473 -- Process subtype indication if one is present
6475 if Present (Component_Typ) then
6476 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6477 Set_Etype (Component_Typ, Element_Type);
6479 -- Ada 2005 (AI-230): Access Definition case
6481 else pragma Assert (Present (Access_Definition (Component_Def)));
6483 -- Indicate that the anonymous access type is created by the
6484 -- array type declaration.
6486 Element_Type := Access_Definition
6487 (Related_Nod => P,
6488 N => Access_Definition (Component_Def));
6489 Set_Is_Local_Anonymous_Access (Element_Type);
6491 -- Propagate the parent. This field is needed if we have to generate
6492 -- the master_id associated with an anonymous access to task type
6493 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6495 Copy_Parent (To => Element_Type, From => T);
6497 -- Ada 2005 (AI-230): In case of components that are anonymous access
6498 -- types the level of accessibility depends on the enclosing type
6499 -- declaration
6501 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6503 -- Ada 2005 (AI-254)
6505 declare
6506 CD : constant Node_Id :=
6507 Access_To_Subprogram_Definition
6508 (Access_Definition (Component_Def));
6509 begin
6510 if Present (CD) and then Protected_Present (CD) then
6511 Element_Type :=
6512 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6513 end if;
6514 end;
6515 end if;
6517 -- Constrained array case
6519 if No (T) then
6520 -- We might be creating more than one itype with the same Related_Id,
6521 -- e.g. for an array object definition and its initial value. Give
6522 -- them unique suffixes, because GNATprove require distinct types to
6523 -- have different names.
6525 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6526 end if;
6528 if Nkind (Def) = N_Constrained_Array_Definition then
6529 -- Establish Implicit_Base as unconstrained base type
6531 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6533 Set_Etype (Implicit_Base, Implicit_Base);
6534 Set_Scope (Implicit_Base, Current_Scope);
6535 Set_Has_Delayed_Freeze (Implicit_Base);
6536 Set_Default_SSO (Implicit_Base);
6538 -- The constrained array type is a subtype of the unconstrained one
6540 Mutate_Ekind (T, E_Array_Subtype);
6541 Reinit_Size_Align (T);
6542 Set_Etype (T, Implicit_Base);
6543 Set_Scope (T, Current_Scope);
6544 Set_Is_Constrained (T);
6545 Set_First_Index (T,
6546 First (Discrete_Subtype_Definitions (Def)));
6547 Set_Has_Delayed_Freeze (T);
6549 -- Complete setup of implicit base type
6551 pragma Assert (not Known_Component_Size (Implicit_Base));
6552 Set_Component_Type (Implicit_Base, Element_Type);
6553 Set_Finalize_Storage_Only
6554 (Implicit_Base,
6555 Finalize_Storage_Only (Element_Type));
6556 Set_First_Index (Implicit_Base, First_Index (T));
6557 Set_Has_Controlled_Component
6558 (Implicit_Base,
6559 Has_Controlled_Component (Element_Type)
6560 or else Is_Controlled (Element_Type));
6561 Set_Packed_Array_Impl_Type
6562 (Implicit_Base, Empty);
6564 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6566 -- Unconstrained array case
6568 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6569 Mutate_Ekind (T, E_Array_Type);
6570 Reinit_Size_Align (T);
6571 Set_Etype (T, T);
6572 Set_Scope (T, Current_Scope);
6573 pragma Assert (not Known_Component_Size (T));
6574 Set_Is_Constrained (T, False);
6575 Set_Is_Fixed_Lower_Bound_Array_Subtype
6576 (T, Has_FLB_Index);
6577 Set_First_Index (T, First (Subtype_Marks (Def)));
6578 Set_Has_Delayed_Freeze (T, True);
6579 Propagate_Concurrent_Flags (T, Element_Type);
6580 Set_Has_Controlled_Component (T, Has_Controlled_Component
6581 (Element_Type)
6582 or else
6583 Is_Controlled (Element_Type));
6584 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6585 (Element_Type));
6586 Set_Default_SSO (T);
6587 end if;
6589 -- Common attributes for both cases
6591 Set_Component_Type (Base_Type (T), Element_Type);
6592 Set_Packed_Array_Impl_Type (T, Empty);
6594 if Aliased_Present (Component_Definition (Def)) then
6595 Set_Has_Aliased_Components (Etype (T));
6597 -- AI12-001: All aliased objects are considered to be specified as
6598 -- independently addressable (RM C.6(8.1/4)).
6600 Set_Has_Independent_Components (Etype (T));
6601 end if;
6603 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6604 -- array type to ensure that objects of this type are initialized.
6606 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6607 Set_Can_Never_Be_Null (T);
6609 if Null_Exclusion_Present (Component_Definition (Def))
6611 -- No need to check itypes because in their case this check was
6612 -- done at their point of creation
6614 and then not Is_Itype (Element_Type)
6615 then
6616 Error_Msg_N
6617 ("`NOT NULL` not allowed (null already excluded)",
6618 Subtype_Indication (Component_Definition (Def)));
6619 end if;
6620 end if;
6622 Priv := Private_Component (Element_Type);
6624 if Present (Priv) then
6626 -- Check for circular definitions
6628 if Priv = Any_Type then
6629 Set_Component_Type (Etype (T), Any_Type);
6631 -- There is a gap in the visibility of operations on the composite
6632 -- type only if the component type is defined in a different scope.
6634 elsif Scope (Priv) = Current_Scope then
6635 null;
6637 elsif Is_Limited_Type (Priv) then
6638 Set_Is_Limited_Composite (Etype (T));
6639 Set_Is_Limited_Composite (T);
6640 else
6641 Set_Is_Private_Composite (Etype (T));
6642 Set_Is_Private_Composite (T);
6643 end if;
6644 end if;
6646 -- A syntax error in the declaration itself may lead to an empty index
6647 -- list, in which case do a minimal patch.
6649 if No (First_Index (T)) then
6650 Error_Msg_N ("missing index definition in array type declaration", T);
6652 declare
6653 Indexes : constant List_Id :=
6654 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6655 begin
6656 Set_Discrete_Subtype_Definitions (Def, Indexes);
6657 Set_First_Index (T, First (Indexes));
6658 return;
6659 end;
6660 end if;
6662 -- Create a concatenation operator for the new type. Internal array
6663 -- types created for packed entities do not need such, they are
6664 -- compatible with the user-defined type.
6666 if Number_Dimensions (T) = 1
6667 and then not Is_Packed_Array_Impl_Type (T)
6668 then
6669 New_Concatenation_Op (T);
6670 end if;
6672 -- In the case of an unconstrained array the parser has already verified
6673 -- that all the indexes are unconstrained but we still need to make sure
6674 -- that the element type is constrained.
6676 if not Is_Definite_Subtype (Element_Type) then
6677 Error_Msg_N
6678 ("unconstrained element type in array declaration",
6679 Subtype_Indication (Component_Def));
6681 elsif Is_Abstract_Type (Element_Type) then
6682 Error_Msg_N
6683 ("the type of a component cannot be abstract",
6684 Subtype_Indication (Component_Def));
6685 end if;
6687 -- There may be an invariant declared for the component type, but
6688 -- the construction of the component invariant checking procedure
6689 -- takes place during expansion.
6690 end Array_Type_Declaration;
6692 ------------------------------------------------------
6693 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6694 ------------------------------------------------------
6696 function Replace_Anonymous_Access_To_Protected_Subprogram
6697 (N : Node_Id) return Entity_Id
6699 Loc : constant Source_Ptr := Sloc (N);
6701 Curr_Scope : constant Scope_Stack_Entry :=
6702 Scope_Stack.Table (Scope_Stack.Last);
6704 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6706 Acc : Node_Id;
6707 -- Access definition in declaration
6709 Comp : Node_Id;
6710 -- Object definition or formal definition with an access definition
6712 Decl : Node_Id;
6713 -- Declaration of anonymous access to subprogram type
6715 Spec : Node_Id;
6716 -- Original specification in access to subprogram
6718 P : Node_Id;
6720 begin
6721 Set_Is_Internal (Anon);
6723 case Nkind (N) is
6724 when N_Constrained_Array_Definition
6725 | N_Component_Declaration
6726 | N_Unconstrained_Array_Definition
6728 Comp := Component_Definition (N);
6729 Acc := Access_Definition (Comp);
6731 when N_Discriminant_Specification =>
6732 Comp := Discriminant_Type (N);
6733 Acc := Comp;
6735 when N_Parameter_Specification =>
6736 Comp := Parameter_Type (N);
6737 Acc := Comp;
6739 when N_Access_Function_Definition =>
6740 Comp := Result_Definition (N);
6741 Acc := Comp;
6743 when N_Object_Declaration =>
6744 Comp := Object_Definition (N);
6745 Acc := Comp;
6747 when N_Function_Specification =>
6748 Comp := Result_Definition (N);
6749 Acc := Comp;
6751 when others =>
6752 raise Program_Error;
6753 end case;
6755 Spec := Access_To_Subprogram_Definition (Acc);
6757 Decl :=
6758 Make_Full_Type_Declaration (Loc,
6759 Defining_Identifier => Anon,
6760 Type_Definition => Copy_Separate_Tree (Spec));
6762 Mark_Rewrite_Insertion (Decl);
6764 -- Insert the new declaration in the nearest enclosing scope. If the
6765 -- parent is a body and N is its return type, the declaration belongs
6766 -- in the enclosing scope. Likewise if N is the type of a parameter.
6768 P := Parent (N);
6770 if Nkind (N) = N_Function_Specification
6771 and then Nkind (P) = N_Subprogram_Body
6772 then
6773 P := Parent (P);
6774 elsif Nkind (N) = N_Parameter_Specification
6775 and then Nkind (P) in N_Subprogram_Specification
6776 and then Nkind (Parent (P)) = N_Subprogram_Body
6777 then
6778 P := Parent (Parent (P));
6779 end if;
6781 while Present (P) and then not Has_Declarations (P) loop
6782 P := Parent (P);
6783 end loop;
6785 pragma Assert (Present (P));
6787 if Nkind (P) = N_Package_Specification then
6788 Prepend (Decl, Visible_Declarations (P));
6789 else
6790 Prepend (Decl, Declarations (P));
6791 end if;
6793 -- Replace the anonymous type with an occurrence of the new declaration.
6794 -- In all cases the rewritten node does not have the null-exclusion
6795 -- attribute because (if present) it was already inherited by the
6796 -- anonymous entity (Anon). Thus, in case of components we do not
6797 -- inherit this attribute.
6799 if Nkind (N) = N_Parameter_Specification then
6800 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6801 Set_Etype (Defining_Identifier (N), Anon);
6802 Set_Null_Exclusion_Present (N, False);
6804 elsif Nkind (N) = N_Object_Declaration then
6805 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6806 Set_Etype (Defining_Identifier (N), Anon);
6808 elsif Nkind (N) = N_Access_Function_Definition then
6809 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6811 elsif Nkind (N) = N_Function_Specification then
6812 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6813 Set_Etype (Defining_Unit_Name (N), Anon);
6815 else
6816 Rewrite (Comp,
6817 Make_Component_Definition (Loc,
6818 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6819 end if;
6821 Mark_Rewrite_Insertion (Comp);
6823 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6824 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6825 and then not Is_Type (Current_Scope))
6826 then
6828 -- Declaration can be analyzed in the current scope.
6830 Analyze (Decl);
6832 else
6833 -- Temporarily remove the current scope (record or subprogram) from
6834 -- the stack to add the new declarations to the enclosing scope.
6835 -- The anonymous entity is an Itype with the proper attributes.
6837 Scope_Stack.Decrement_Last;
6838 Analyze (Decl);
6839 Set_Is_Itype (Anon);
6840 Set_Associated_Node_For_Itype (Anon, N);
6841 Scope_Stack.Append (Curr_Scope);
6842 end if;
6844 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6845 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6846 return Anon;
6847 end Replace_Anonymous_Access_To_Protected_Subprogram;
6849 -------------------------------------
6850 -- Build_Access_Subprogram_Wrapper --
6851 -------------------------------------
6853 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6854 Loc : constant Source_Ptr := Sloc (Decl);
6855 Id : constant Entity_Id := Defining_Identifier (Decl);
6856 Type_Def : constant Node_Id := Type_Definition (Decl);
6857 Specs : constant List_Id :=
6858 Parameter_Specifications (Type_Def);
6859 Profile : constant List_Id := New_List;
6860 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6862 Contracts : constant List_Id := New_List;
6863 Form_P : Node_Id;
6864 New_P : Node_Id;
6865 New_Decl : Node_Id;
6866 Spec : Node_Id;
6868 procedure Replace_Type_Name (Expr : Node_Id);
6869 -- In the expressions for contract aspects, replace occurrences of the
6870 -- access type with the name of the subprogram entity, as needed, e.g.
6871 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6872 -- remain on the original access type declaration. What about expanded
6873 -- names denoting formals, whose prefix in source is the type name ???
6875 -----------------------
6876 -- Replace_Type_Name --
6877 -----------------------
6879 procedure Replace_Type_Name (Expr : Node_Id) is
6880 function Process (N : Node_Id) return Traverse_Result;
6881 function Process (N : Node_Id) return Traverse_Result is
6882 begin
6883 if Nkind (N) = N_Attribute_Reference
6884 and then Is_Entity_Name (Prefix (N))
6885 and then Chars (Prefix (N)) = Chars (Id)
6886 then
6887 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6888 end if;
6890 return OK;
6891 end Process;
6893 procedure Traverse is new Traverse_Proc (Process);
6894 begin
6895 Traverse (Expr);
6896 end Replace_Type_Name;
6898 begin
6899 if Ekind (Id) in E_Access_Subprogram_Type
6900 | E_Access_Protected_Subprogram_Type
6901 | E_Anonymous_Access_Protected_Subprogram_Type
6902 | E_Anonymous_Access_Subprogram_Type
6903 then
6904 null;
6906 else
6907 Error_Msg_N
6908 ("illegal pre/postcondition on access type", Decl);
6909 return;
6910 end if;
6912 declare
6913 Asp : Node_Id;
6914 A_Id : Aspect_Id;
6916 begin
6917 Asp := First (Aspect_Specifications (Decl));
6918 while Present (Asp) loop
6919 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6920 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6921 Append (New_Copy_Tree (Asp), Contracts);
6922 Replace_Type_Name (Expression (Last (Contracts)));
6923 end if;
6924 Next (Asp);
6925 end loop;
6926 end;
6928 -- If there are no contract aspects, no need for a wrapper.
6930 if Is_Empty_List (Contracts) then
6931 return;
6932 end if;
6934 Form_P := First (Specs);
6936 while Present (Form_P) loop
6937 New_P := New_Copy_Tree (Form_P);
6938 Set_Defining_Identifier (New_P,
6939 Make_Defining_Identifier
6940 (Loc, Chars (Defining_Identifier (Form_P))));
6941 Append (New_P, Profile);
6942 Next (Form_P);
6943 end loop;
6945 -- Add to parameter specifications the access parameter that is passed
6946 -- in from an indirect call.
6948 Append (
6949 Make_Parameter_Specification (Loc,
6950 Defining_Identifier => Make_Temporary (Loc, 'P'),
6951 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6952 Profile);
6954 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6955 Spec :=
6956 Make_Procedure_Specification (Loc,
6957 Defining_Unit_Name => Subp,
6958 Parameter_Specifications => Profile);
6959 Mutate_Ekind (Subp, E_Procedure);
6960 else
6961 Spec :=
6962 Make_Function_Specification (Loc,
6963 Defining_Unit_Name => Subp,
6964 Parameter_Specifications => Profile,
6965 Result_Definition =>
6966 New_Copy_Tree
6967 (Result_Definition (Type_Definition (Decl))));
6968 Mutate_Ekind (Subp, E_Function);
6969 end if;
6971 New_Decl :=
6972 Make_Subprogram_Declaration (Loc, Specification => Spec);
6973 Set_Aspect_Specifications (New_Decl, Contracts);
6974 Set_Is_Wrapper (Subp);
6976 -- The wrapper is declared in the freezing actions to facilitate its
6977 -- identification and thus avoid handling it as a primitive operation
6978 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
6979 -- may be handled as a dispatching operation and erroneously registered
6980 -- in a dispatch table.
6982 Append_Freeze_Action (Id, New_Decl);
6984 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6985 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6986 end Build_Access_Subprogram_Wrapper;
6988 -------------------------------
6989 -- Build_Derived_Access_Type --
6990 -------------------------------
6992 procedure Build_Derived_Access_Type
6993 (N : Node_Id;
6994 Parent_Type : Entity_Id;
6995 Derived_Type : Entity_Id)
6997 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6999 Desig_Type : Entity_Id;
7000 Discr : Entity_Id;
7001 Discr_Con_Elist : Elist_Id;
7002 Discr_Con_El : Elmt_Id;
7003 Subt : Entity_Id;
7005 begin
7006 -- Set the designated type so it is available in case this is an access
7007 -- to a self-referential type, e.g. a standard list type with a next
7008 -- pointer. Will be reset after subtype is built.
7010 Set_Directly_Designated_Type
7011 (Derived_Type, Designated_Type (Parent_Type));
7013 Subt := Process_Subtype (S, N);
7015 if Nkind (S) /= N_Subtype_Indication
7016 and then Subt /= Base_Type (Subt)
7017 then
7018 Mutate_Ekind (Derived_Type, E_Access_Subtype);
7019 end if;
7021 if Ekind (Derived_Type) = E_Access_Subtype then
7022 declare
7023 Pbase : constant Entity_Id := Base_Type (Parent_Type);
7024 Ibase : constant Entity_Id :=
7025 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
7026 Svg_Chars : constant Name_Id := Chars (Ibase);
7027 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
7028 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
7030 begin
7031 Copy_Node (Pbase, Ibase);
7033 -- Restore Itype status after Copy_Node
7035 Set_Is_Itype (Ibase);
7036 Set_Associated_Node_For_Itype (Ibase, N);
7038 Set_Chars (Ibase, Svg_Chars);
7039 Set_Prev_Entity (Ibase, Svg_Prev_E);
7040 Set_Next_Entity (Ibase, Svg_Next_E);
7041 Set_Sloc (Ibase, Sloc (Derived_Type));
7042 Set_Scope (Ibase, Scope (Derived_Type));
7043 Set_Freeze_Node (Ibase, Empty);
7044 Set_Is_Frozen (Ibase, False);
7045 Set_Comes_From_Source (Ibase, False);
7046 Set_Is_First_Subtype (Ibase, False);
7048 Set_Etype (Ibase, Pbase);
7049 Set_Etype (Derived_Type, Ibase);
7050 end;
7051 end if;
7053 Set_Directly_Designated_Type
7054 (Derived_Type, Designated_Type (Subt));
7056 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7057 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7058 Set_Size_Info (Derived_Type, Parent_Type);
7059 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7060 Set_Depends_On_Private (Derived_Type,
7061 Has_Private_Component (Derived_Type));
7062 Conditional_Delay (Derived_Type, Subt);
7064 if Is_Access_Subprogram_Type (Derived_Type)
7065 and then Is_Base_Type (Derived_Type)
7066 then
7067 Set_Can_Use_Internal_Rep
7068 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7069 end if;
7071 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7072 -- that it is not redundant.
7074 if Null_Exclusion_Present (Type_Definition (N)) then
7075 Set_Can_Never_Be_Null (Derived_Type);
7077 elsif Can_Never_Be_Null (Parent_Type) then
7078 Set_Can_Never_Be_Null (Derived_Type);
7079 end if;
7081 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7082 -- the root type for this information.
7084 -- Apply range checks to discriminants for derived record case
7085 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7087 Desig_Type := Designated_Type (Derived_Type);
7089 if Is_Composite_Type (Desig_Type)
7090 and then not Is_Array_Type (Desig_Type)
7091 and then Has_Discriminants (Desig_Type)
7092 and then Base_Type (Desig_Type) /= Desig_Type
7093 then
7094 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7095 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7097 Discr := First_Discriminant (Base_Type (Desig_Type));
7098 while Present (Discr_Con_El) loop
7099 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7100 Next_Elmt (Discr_Con_El);
7101 Next_Discriminant (Discr);
7102 end loop;
7103 end if;
7104 end Build_Derived_Access_Type;
7106 ------------------------------
7107 -- Build_Derived_Array_Type --
7108 ------------------------------
7110 procedure Build_Derived_Array_Type
7111 (N : Node_Id;
7112 Parent_Type : Entity_Id;
7113 Derived_Type : Entity_Id)
7115 Loc : constant Source_Ptr := Sloc (N);
7116 Tdef : constant Node_Id := Type_Definition (N);
7117 Indic : constant Node_Id := Subtype_Indication (Tdef);
7118 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7119 Implicit_Base : Entity_Id := Empty;
7120 New_Indic : Node_Id;
7122 procedure Make_Implicit_Base;
7123 -- If the parent subtype is constrained, the derived type is a subtype
7124 -- of an implicit base type derived from the parent base.
7126 ------------------------
7127 -- Make_Implicit_Base --
7128 ------------------------
7130 procedure Make_Implicit_Base is
7131 begin
7132 Implicit_Base :=
7133 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7135 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7136 Set_Etype (Implicit_Base, Parent_Base);
7138 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7139 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7141 Set_Has_Delayed_Freeze (Implicit_Base, True);
7142 end Make_Implicit_Base;
7144 -- Start of processing for Build_Derived_Array_Type
7146 begin
7147 if not Is_Constrained (Parent_Type) then
7148 if Nkind (Indic) /= N_Subtype_Indication then
7149 Mutate_Ekind (Derived_Type, E_Array_Type);
7151 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7152 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7154 Set_Has_Delayed_Freeze (Derived_Type, True);
7156 else
7157 Make_Implicit_Base;
7158 Set_Etype (Derived_Type, Implicit_Base);
7160 New_Indic :=
7161 Make_Subtype_Declaration (Loc,
7162 Defining_Identifier => Derived_Type,
7163 Subtype_Indication =>
7164 Make_Subtype_Indication (Loc,
7165 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7166 Constraint => Constraint (Indic)));
7168 Rewrite (N, New_Indic);
7170 -- Keep the aspects from the original node
7172 Move_Aspects (Original_Node (N), N);
7174 Analyze (N);
7175 end if;
7177 else
7178 if Nkind (Indic) /= N_Subtype_Indication then
7179 Make_Implicit_Base;
7181 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7182 Set_Etype (Derived_Type, Implicit_Base);
7183 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7185 else
7186 Error_Msg_N ("illegal constraint on constrained type", Indic);
7187 end if;
7188 end if;
7190 -- If parent type is not a derived type itself, and is declared in
7191 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7192 -- the new type's concatenation operator since Derive_Subprograms
7193 -- will not inherit the parent's operator. If the parent type is
7194 -- unconstrained, the operator is of the unconstrained base type.
7196 if Number_Dimensions (Parent_Type) = 1
7197 and then not Is_Limited_Type (Parent_Type)
7198 and then not Is_Derived_Type (Parent_Type)
7199 and then not Is_Package_Or_Generic_Package
7200 (Scope (Base_Type (Parent_Type)))
7201 then
7202 if not Is_Constrained (Parent_Type)
7203 and then Is_Constrained (Derived_Type)
7204 then
7205 New_Concatenation_Op (Implicit_Base);
7206 else
7207 New_Concatenation_Op (Derived_Type);
7208 end if;
7209 end if;
7210 end Build_Derived_Array_Type;
7212 -----------------------------------
7213 -- Build_Derived_Concurrent_Type --
7214 -----------------------------------
7216 procedure Build_Derived_Concurrent_Type
7217 (N : Node_Id;
7218 Parent_Type : Entity_Id;
7219 Derived_Type : Entity_Id)
7221 Loc : constant Source_Ptr := Sloc (N);
7222 Def : constant Node_Id := Type_Definition (N);
7223 Indic : constant Node_Id := Subtype_Indication (Def);
7225 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7226 Corr_Decl : Node_Id := Empty;
7227 Corr_Decl_Needed : Boolean;
7228 -- If the derived type has fewer discriminants than its parent, the
7229 -- corresponding record is also a derived type, in order to account for
7230 -- the bound discriminants. We create a full type declaration for it in
7231 -- this case.
7233 Constraint_Present : constant Boolean :=
7234 Nkind (Indic) = N_Subtype_Indication;
7236 D_Constraint : Node_Id;
7237 New_Constraint : Elist_Id := No_Elist;
7238 Old_Disc : Entity_Id;
7239 New_Disc : Entity_Id;
7240 New_N : Node_Id;
7242 begin
7243 Set_Stored_Constraint (Derived_Type, No_Elist);
7244 Corr_Decl_Needed := False;
7245 Old_Disc := Empty;
7247 if Present (Discriminant_Specifications (N))
7248 and then Constraint_Present
7249 then
7250 Old_Disc := First_Discriminant (Parent_Type);
7251 New_Disc := First (Discriminant_Specifications (N));
7252 while Present (New_Disc) and then Present (Old_Disc) loop
7253 Next_Discriminant (Old_Disc);
7254 Next (New_Disc);
7255 end loop;
7256 end if;
7258 if Present (Old_Disc) and then Expander_Active then
7260 -- The new type has fewer discriminants, so we need to create a new
7261 -- corresponding record, which is derived from the corresponding
7262 -- record of the parent, and has a stored constraint that captures
7263 -- the values of the discriminant constraints. The corresponding
7264 -- record is needed only if expander is active and code generation is
7265 -- enabled.
7267 -- The type declaration for the derived corresponding record has the
7268 -- same discriminant part and constraints as the current declaration.
7269 -- Copy the unanalyzed tree to build declaration.
7271 Corr_Decl_Needed := True;
7272 New_N := Copy_Separate_Tree (N);
7274 Corr_Decl :=
7275 Make_Full_Type_Declaration (Loc,
7276 Defining_Identifier => Corr_Record,
7277 Discriminant_Specifications =>
7278 Discriminant_Specifications (New_N),
7279 Type_Definition =>
7280 Make_Derived_Type_Definition (Loc,
7281 Subtype_Indication =>
7282 Make_Subtype_Indication (Loc,
7283 Subtype_Mark =>
7284 New_Occurrence_Of
7285 (Corresponding_Record_Type (Parent_Type), Loc),
7286 Constraint =>
7287 Constraint
7288 (Subtype_Indication (Type_Definition (New_N))))));
7289 end if;
7291 -- Copy Storage_Size and Relative_Deadline variables if task case
7293 if Is_Task_Type (Parent_Type) then
7294 Set_Storage_Size_Variable (Derived_Type,
7295 Storage_Size_Variable (Parent_Type));
7296 Set_Relative_Deadline_Variable (Derived_Type,
7297 Relative_Deadline_Variable (Parent_Type));
7298 end if;
7300 if Present (Discriminant_Specifications (N)) then
7301 Push_Scope (Derived_Type);
7302 Check_Or_Process_Discriminants (N, Derived_Type);
7304 if Constraint_Present then
7305 New_Constraint :=
7306 Expand_To_Stored_Constraint
7307 (Parent_Type,
7308 Build_Discriminant_Constraints
7309 (Parent_Type, Indic, True));
7310 end if;
7312 End_Scope;
7314 elsif Constraint_Present then
7316 -- Build an unconstrained derived type and rewrite the derived type
7317 -- as a subtype of this new base type.
7319 declare
7320 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7321 New_Base : Entity_Id;
7322 New_Decl : Node_Id;
7323 New_Indic : Node_Id;
7325 begin
7326 New_Base :=
7327 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7329 New_Decl :=
7330 Make_Full_Type_Declaration (Loc,
7331 Defining_Identifier => New_Base,
7332 Type_Definition =>
7333 Make_Derived_Type_Definition (Loc,
7334 Abstract_Present => Abstract_Present (Def),
7335 Limited_Present => Limited_Present (Def),
7336 Subtype_Indication =>
7337 New_Occurrence_Of (Parent_Base, Loc)));
7339 Mark_Rewrite_Insertion (New_Decl);
7340 Insert_Before (N, New_Decl);
7341 Analyze (New_Decl);
7343 New_Indic :=
7344 Make_Subtype_Indication (Loc,
7345 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7346 Constraint => Relocate_Node (Constraint (Indic)));
7348 Rewrite (N,
7349 Make_Subtype_Declaration (Loc,
7350 Defining_Identifier => Derived_Type,
7351 Subtype_Indication => New_Indic));
7353 -- Keep the aspects from the original node
7355 Move_Aspects (Original_Node (N), N);
7357 Analyze (N);
7358 return;
7359 end;
7360 end if;
7362 -- By default, operations and private data are inherited from parent.
7363 -- However, in the presence of bound discriminants, a new corresponding
7364 -- record will be created, see below.
7366 Set_Has_Discriminants
7367 (Derived_Type, Has_Discriminants (Parent_Type));
7368 Set_Corresponding_Record_Type
7369 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7371 -- Is_Constrained is set according the parent subtype, but is set to
7372 -- False if the derived type is declared with new discriminants.
7374 Set_Is_Constrained
7375 (Derived_Type,
7376 (Is_Constrained (Parent_Type) or else Constraint_Present)
7377 and then No (Discriminant_Specifications (N)));
7379 if Constraint_Present then
7380 if not Has_Discriminants (Parent_Type) then
7381 Error_Msg_N ("untagged parent must have discriminants", N);
7383 elsif Present (Discriminant_Specifications (N)) then
7385 -- Verify that new discriminants are used to constrain old ones
7387 D_Constraint := First (Constraints (Constraint (Indic)));
7389 Old_Disc := First_Discriminant (Parent_Type);
7391 while Present (D_Constraint) loop
7392 if Nkind (D_Constraint) /= N_Discriminant_Association then
7394 -- Positional constraint. If it is a reference to a new
7395 -- discriminant, it constrains the corresponding old one.
7397 if Nkind (D_Constraint) = N_Identifier then
7398 New_Disc := First_Discriminant (Derived_Type);
7399 while Present (New_Disc) loop
7400 exit when Chars (New_Disc) = Chars (D_Constraint);
7401 Next_Discriminant (New_Disc);
7402 end loop;
7404 if Present (New_Disc) then
7405 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7406 end if;
7407 end if;
7409 Next_Discriminant (Old_Disc);
7411 -- if this is a named constraint, search by name for the old
7412 -- discriminants constrained by the new one.
7414 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7416 -- Find new discriminant with that name
7418 New_Disc := First_Discriminant (Derived_Type);
7419 while Present (New_Disc) loop
7420 exit when
7421 Chars (New_Disc) = Chars (Expression (D_Constraint));
7422 Next_Discriminant (New_Disc);
7423 end loop;
7425 if Present (New_Disc) then
7427 -- Verify that new discriminant renames some discriminant
7428 -- of the parent type, and associate the new discriminant
7429 -- with one or more old ones that it renames.
7431 declare
7432 Selector : Node_Id;
7434 begin
7435 Selector := First (Selector_Names (D_Constraint));
7436 while Present (Selector) loop
7437 Old_Disc := First_Discriminant (Parent_Type);
7438 while Present (Old_Disc) loop
7439 exit when Chars (Old_Disc) = Chars (Selector);
7440 Next_Discriminant (Old_Disc);
7441 end loop;
7443 if Present (Old_Disc) then
7444 Set_Corresponding_Discriminant
7445 (New_Disc, Old_Disc);
7446 end if;
7448 Next (Selector);
7449 end loop;
7450 end;
7451 end if;
7452 end if;
7454 Next (D_Constraint);
7455 end loop;
7457 New_Disc := First_Discriminant (Derived_Type);
7458 while Present (New_Disc) loop
7459 if No (Corresponding_Discriminant (New_Disc)) then
7460 Error_Msg_NE
7461 ("new discriminant& must constrain old one", N, New_Disc);
7463 -- If a new discriminant is used in the constraint, then its
7464 -- subtype must be statically compatible with the subtype of
7465 -- the parent discriminant (RM 3.7(15)).
7467 else
7468 Check_Constraining_Discriminant
7469 (New_Disc, Corresponding_Discriminant (New_Disc));
7470 end if;
7472 Next_Discriminant (New_Disc);
7473 end loop;
7474 end if;
7476 elsif Present (Discriminant_Specifications (N)) then
7477 Error_Msg_N
7478 ("missing discriminant constraint in untagged derivation", N);
7479 end if;
7481 -- The entity chain of the derived type includes the new discriminants
7482 -- but shares operations with the parent.
7484 if Present (Discriminant_Specifications (N)) then
7485 Old_Disc := First_Discriminant (Parent_Type);
7486 while Present (Old_Disc) loop
7487 if No (Next_Entity (Old_Disc))
7488 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7489 then
7490 Link_Entities
7491 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7492 exit;
7493 end if;
7495 Next_Discriminant (Old_Disc);
7496 end loop;
7498 else
7499 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7500 if Has_Discriminants (Parent_Type) then
7501 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7502 Set_Discriminant_Constraint (
7503 Derived_Type, Discriminant_Constraint (Parent_Type));
7504 end if;
7505 end if;
7507 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7509 Set_Has_Completion (Derived_Type);
7511 if Corr_Decl_Needed then
7512 Set_Stored_Constraint (Derived_Type, New_Constraint);
7513 Insert_After (N, Corr_Decl);
7514 Analyze (Corr_Decl);
7515 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7516 end if;
7517 end Build_Derived_Concurrent_Type;
7519 ------------------------------------
7520 -- Build_Derived_Enumeration_Type --
7521 ------------------------------------
7523 procedure Build_Derived_Enumeration_Type
7524 (N : Node_Id;
7525 Parent_Type : Entity_Id;
7526 Derived_Type : Entity_Id)
7528 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7529 -- When the type declaration includes a constraint, we generate
7530 -- a subtype declaration of an anonymous base type, with the constraint
7531 -- given in the original type declaration. Conceptually, the bounds
7532 -- are converted to the new base type, and this conversion freezes
7533 -- (prematurely) that base type, when the bounds are simply literals.
7534 -- As a result, a representation clause for the derived type is then
7535 -- rejected or ignored. This procedure recognizes the simple case of
7536 -- literal bounds, which allows us to indicate that the conversions
7537 -- are not freeze points, and the subsequent representation clause
7538 -- can be accepted.
7539 -- A similar approach might be used to resolve the long-standing
7540 -- problem of premature freezing of derived numeric types ???
7542 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7543 begin
7544 return Nkind (B) = N_Type_Conversion
7545 and then Is_Entity_Name (Expression (B))
7546 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7547 end Bound_Belongs_To_Type;
7549 Loc : constant Source_Ptr := Sloc (N);
7550 Def : constant Node_Id := Type_Definition (N);
7551 Indic : constant Node_Id := Subtype_Indication (Def);
7552 Implicit_Base : Entity_Id;
7553 Literal : Entity_Id;
7554 New_Lit : Entity_Id;
7555 Literals_List : List_Id;
7556 Type_Decl : Node_Id;
7557 Hi, Lo : Node_Id;
7558 Rang_Expr : Node_Id;
7560 begin
7561 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7562 -- not have explicit literals lists we need to process types derived
7563 -- from them specially. This is handled by Derived_Standard_Character.
7564 -- If the parent type is a generic type, there are no literals either,
7565 -- and we construct the same skeletal representation as for the generic
7566 -- parent type.
7568 if Is_Standard_Character_Type (Parent_Type) then
7569 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7571 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7572 declare
7573 Lo : Node_Id;
7574 Hi : Node_Id;
7576 begin
7577 if Nkind (Indic) /= N_Subtype_Indication then
7578 Lo :=
7579 Make_Attribute_Reference (Loc,
7580 Attribute_Name => Name_First,
7581 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7582 Set_Etype (Lo, Derived_Type);
7584 Hi :=
7585 Make_Attribute_Reference (Loc,
7586 Attribute_Name => Name_Last,
7587 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7588 Set_Etype (Hi, Derived_Type);
7590 Set_Scalar_Range (Derived_Type,
7591 Make_Range (Loc,
7592 Low_Bound => Lo,
7593 High_Bound => Hi));
7594 else
7596 -- Analyze subtype indication and verify compatibility
7597 -- with parent type.
7599 if Base_Type (Process_Subtype (Indic, N)) /=
7600 Base_Type (Parent_Type)
7601 then
7602 Error_Msg_N
7603 ("illegal constraint for formal discrete type", N);
7604 end if;
7605 end if;
7606 end;
7608 else
7609 -- If a constraint is present, analyze the bounds to catch
7610 -- premature usage of the derived literals.
7612 if Nkind (Indic) = N_Subtype_Indication
7613 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7614 then
7615 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7616 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7617 end if;
7619 -- Create an implicit base type for the derived type even if there
7620 -- is no constraint attached to it, since this seems closer to the
7621 -- Ada semantics. Use an Itype like for the implicit base type of
7622 -- other kinds of derived type, but build a full type declaration
7623 -- for it so as to analyze the new literals properly. Then build a
7624 -- subtype declaration tree which applies the constraint (if any)
7625 -- and have it replace the derived type declaration.
7627 Literal := First_Literal (Parent_Type);
7628 Literals_List := New_List;
7629 while Present (Literal)
7630 and then Ekind (Literal) = E_Enumeration_Literal
7631 loop
7632 -- Literals of the derived type have the same representation as
7633 -- those of the parent type, but this representation can be
7634 -- overridden by an explicit representation clause. Indicate
7635 -- that there is no explicit representation given yet. These
7636 -- derived literals are implicit operations of the new type,
7637 -- and can be overridden by explicit ones.
7639 if Nkind (Literal) = N_Defining_Character_Literal then
7640 New_Lit :=
7641 Make_Defining_Character_Literal (Loc, Chars (Literal));
7642 else
7643 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7644 end if;
7646 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7647 Set_Is_Not_Self_Hidden (New_Lit);
7648 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7649 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7650 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7651 Set_Alias (New_Lit, Literal);
7652 Set_Is_Known_Valid (New_Lit, True);
7654 Append (New_Lit, Literals_List);
7655 Next_Literal (Literal);
7656 end loop;
7658 Implicit_Base :=
7659 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7661 -- Indicate the proper nature of the derived type. This must be done
7662 -- before analysis of the literals, to recognize cases when a literal
7663 -- may be hidden by a previous explicit function definition (cf.
7664 -- c83031a).
7666 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7667 Set_Etype (Derived_Type, Implicit_Base);
7669 Type_Decl :=
7670 Make_Full_Type_Declaration (Loc,
7671 Defining_Identifier => Implicit_Base,
7672 Type_Definition =>
7673 Make_Enumeration_Type_Definition (Loc, Literals_List));
7675 -- Do not insert the declarationn, just analyze it in the context
7677 Set_Parent (Type_Decl, Parent (N));
7678 Analyze (Type_Decl);
7680 -- The anonymous base now has a full declaration, but this base
7681 -- is not a first subtype.
7683 Set_Is_First_Subtype (Implicit_Base, False);
7685 -- After the implicit base is analyzed its Etype needs to be changed
7686 -- to reflect the fact that it is derived from the parent type which
7687 -- was ignored during analysis. We also set the size at this point.
7689 Set_Etype (Implicit_Base, Parent_Type);
7691 Set_Size_Info (Implicit_Base, Parent_Type);
7692 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7693 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7695 -- Copy other flags from parent type
7697 Set_Has_Non_Standard_Rep
7698 (Implicit_Base, Has_Non_Standard_Rep
7699 (Parent_Type));
7700 Set_Has_Pragma_Ordered
7701 (Implicit_Base, Has_Pragma_Ordered
7702 (Parent_Type));
7703 Set_Has_Delayed_Freeze (Implicit_Base);
7705 -- Process the subtype indication including a validation check on the
7706 -- constraint, if any. If a constraint is given, its bounds must be
7707 -- implicitly converted to the new type.
7709 if Nkind (Indic) = N_Subtype_Indication then
7710 declare
7711 R : constant Node_Id :=
7712 Range_Expression (Constraint (Indic));
7714 begin
7715 if Nkind (R) = N_Range then
7716 Hi := Build_Scalar_Bound
7717 (High_Bound (R), Parent_Type, Implicit_Base);
7718 Lo := Build_Scalar_Bound
7719 (Low_Bound (R), Parent_Type, Implicit_Base);
7721 else
7722 -- Constraint is a Range attribute. Replace with explicit
7723 -- mention of the bounds of the prefix, which must be a
7724 -- subtype.
7726 Analyze (Prefix (R));
7727 Hi :=
7728 Convert_To (Implicit_Base,
7729 Make_Attribute_Reference (Loc,
7730 Attribute_Name => Name_Last,
7731 Prefix =>
7732 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7734 Lo :=
7735 Convert_To (Implicit_Base,
7736 Make_Attribute_Reference (Loc,
7737 Attribute_Name => Name_First,
7738 Prefix =>
7739 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7740 end if;
7741 end;
7743 else
7744 Hi :=
7745 Build_Scalar_Bound
7746 (Type_High_Bound (Parent_Type),
7747 Parent_Type, Implicit_Base);
7748 Lo :=
7749 Build_Scalar_Bound
7750 (Type_Low_Bound (Parent_Type),
7751 Parent_Type, Implicit_Base);
7752 end if;
7754 Rang_Expr :=
7755 Make_Range (Loc,
7756 Low_Bound => Lo,
7757 High_Bound => Hi);
7759 -- If we constructed a default range for the case where no range
7760 -- was given, then the expressions in the range must not freeze
7761 -- since they do not correspond to expressions in the source.
7762 -- However, if the type inherits predicates the expressions will
7763 -- be elaborated earlier and must freeze.
7765 if (Nkind (Indic) /= N_Subtype_Indication
7766 or else
7767 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7768 and then not Has_Predicates (Derived_Type)
7769 then
7770 Set_Must_Not_Freeze (Lo);
7771 Set_Must_Not_Freeze (Hi);
7772 Set_Must_Not_Freeze (Rang_Expr);
7773 end if;
7775 Rewrite (N,
7776 Make_Subtype_Declaration (Loc,
7777 Defining_Identifier => Derived_Type,
7778 Subtype_Indication =>
7779 Make_Subtype_Indication (Loc,
7780 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7781 Constraint =>
7782 Make_Range_Constraint (Loc,
7783 Range_Expression => Rang_Expr))));
7785 -- Keep the aspects from the orignal node
7787 Move_Aspects (Original_Node (N), N);
7789 Analyze (N);
7791 -- Propagate the aspects from the original type declaration to the
7792 -- declaration of the implicit base.
7794 Copy_Aspects (From => N, To => Type_Decl);
7796 -- Apply a range check. Since this range expression doesn't have an
7797 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7798 -- this right???
7800 if Nkind (Indic) = N_Subtype_Indication then
7801 Apply_Range_Check
7802 (Range_Expression (Constraint (Indic)), Parent_Type,
7803 Source_Typ => Entity (Subtype_Mark (Indic)));
7804 end if;
7805 end if;
7806 end Build_Derived_Enumeration_Type;
7808 --------------------------------
7809 -- Build_Derived_Numeric_Type --
7810 --------------------------------
7812 procedure Build_Derived_Numeric_Type
7813 (N : Node_Id;
7814 Parent_Type : Entity_Id;
7815 Derived_Type : Entity_Id)
7817 Loc : constant Source_Ptr := Sloc (N);
7818 Tdef : constant Node_Id := Type_Definition (N);
7819 Indic : constant Node_Id := Subtype_Indication (Tdef);
7820 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7821 No_Constraint : constant Boolean := Nkind (Indic) /=
7822 N_Subtype_Indication;
7823 Implicit_Base : Entity_Id;
7825 Lo : Node_Id;
7826 Hi : Node_Id;
7828 begin
7829 -- Process the subtype indication including a validation check on
7830 -- the constraint if any.
7832 Discard_Node (Process_Subtype (Indic, N));
7834 -- Introduce an implicit base type for the derived type even if there
7835 -- is no constraint attached to it, since this seems closer to the Ada
7836 -- semantics.
7838 Implicit_Base :=
7839 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7841 Set_Etype (Implicit_Base, Parent_Base);
7842 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7843 Set_Size_Info (Implicit_Base, Parent_Base);
7844 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7845 Set_Parent (Implicit_Base, Parent (Derived_Type));
7846 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7847 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7849 -- Set RM Size for discrete type or decimal fixed-point type
7850 -- Ordinary fixed-point is excluded, why???
7852 if Is_Discrete_Type (Parent_Base)
7853 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7854 then
7855 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7856 end if;
7858 Set_Has_Delayed_Freeze (Implicit_Base);
7860 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7861 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7863 Set_Scalar_Range (Implicit_Base,
7864 Make_Range (Loc,
7865 Low_Bound => Lo,
7866 High_Bound => Hi));
7868 if Has_Infinities (Parent_Base) then
7869 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7870 end if;
7872 -- The Derived_Type, which is the entity of the declaration, is a
7873 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7874 -- absence of an explicit constraint.
7876 Set_Etype (Derived_Type, Implicit_Base);
7878 -- If we did not have a constraint, then the Ekind is set from the
7879 -- parent type (otherwise Process_Subtype has set the bounds)
7881 if No_Constraint then
7882 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7883 end if;
7885 -- If we did not have a range constraint, then set the range from the
7886 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7888 if No_Constraint or else not Has_Range_Constraint (Indic) then
7889 Set_Scalar_Range (Derived_Type,
7890 Make_Range (Loc,
7891 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7892 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7893 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7895 if Has_Infinities (Parent_Type) then
7896 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7897 end if;
7899 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7900 end if;
7902 Set_Is_Descendant_Of_Address (Derived_Type,
7903 Is_Descendant_Of_Address (Parent_Type));
7904 Set_Is_Descendant_Of_Address (Implicit_Base,
7905 Is_Descendant_Of_Address (Parent_Type));
7907 -- Set remaining type-specific fields, depending on numeric type
7909 if Is_Modular_Integer_Type (Parent_Type) then
7910 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7912 Set_Non_Binary_Modulus
7913 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7915 Set_Is_Known_Valid
7916 (Implicit_Base, Is_Known_Valid (Parent_Base));
7918 elsif Is_Floating_Point_Type (Parent_Type) then
7920 -- Digits of base type is always copied from the digits value of
7921 -- the parent base type, but the digits of the derived type will
7922 -- already have been set if there was a constraint present.
7924 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7925 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7927 if No_Constraint then
7928 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7929 end if;
7931 elsif Is_Fixed_Point_Type (Parent_Type) then
7933 -- Small of base type and derived type are always copied from the
7934 -- parent base type, since smalls never change. The delta of the
7935 -- base type is also copied from the parent base type. However the
7936 -- delta of the derived type will have been set already if a
7937 -- constraint was present.
7939 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7940 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7941 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7943 if No_Constraint then
7944 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7945 end if;
7947 -- The scale and machine radix in the decimal case are always
7948 -- copied from the parent base type.
7950 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7951 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7952 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7954 Set_Machine_Radix_10
7955 (Derived_Type, Machine_Radix_10 (Parent_Base));
7956 Set_Machine_Radix_10
7957 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7959 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7961 if No_Constraint then
7962 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7964 else
7965 -- the analysis of the subtype_indication sets the
7966 -- digits value of the derived type.
7968 null;
7969 end if;
7970 end if;
7971 end if;
7973 if Is_Integer_Type (Parent_Type) then
7974 Set_Has_Shift_Operator
7975 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7976 end if;
7978 -- The type of the bounds is that of the parent type, and they
7979 -- must be converted to the derived type.
7981 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7982 end Build_Derived_Numeric_Type;
7984 --------------------------------
7985 -- Build_Derived_Private_Type --
7986 --------------------------------
7988 procedure Build_Derived_Private_Type
7989 (N : Node_Id;
7990 Parent_Type : Entity_Id;
7991 Derived_Type : Entity_Id;
7992 Is_Completion : Boolean;
7993 Derive_Subps : Boolean := True)
7995 Loc : constant Source_Ptr := Sloc (N);
7996 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7997 Par_Scope : constant Entity_Id := Scope (Par_Base);
7998 Full_N : constant Node_Id := New_Copy_Tree (N);
7999 Full_Der : Entity_Id := New_Copy (Derived_Type);
8000 Full_P : Entity_Id;
8002 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
8003 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
8004 -- present (they cannot be both present for the same type), or Empty.
8006 procedure Build_Full_Derivation;
8007 -- Build full derivation, i.e. derive from the full view
8009 procedure Copy_And_Build;
8010 -- Copy derived type declaration, replace parent with its full view,
8011 -- and build derivation
8013 -------------------------
8014 -- Available_Full_View --
8015 -------------------------
8017 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
8018 begin
8019 if Present (Full_View (Typ)) then
8020 return Full_View (Typ);
8022 elsif Present (Underlying_Full_View (Typ)) then
8024 -- We should be called on a type with an underlying full view
8025 -- only by means of the recursive call made in Copy_And_Build
8026 -- through the first call to Build_Derived_Type, or else if
8027 -- the parent scope is being analyzed because we are deriving
8028 -- a completion.
8030 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
8032 return Underlying_Full_View (Typ);
8034 else
8035 return Empty;
8036 end if;
8037 end Available_Full_View;
8039 ---------------------------
8040 -- Build_Full_Derivation --
8041 ---------------------------
8043 procedure Build_Full_Derivation is
8044 begin
8045 -- If parent scope is not open, install the declarations
8047 if not In_Open_Scopes (Par_Scope) then
8048 Install_Private_Declarations (Par_Scope);
8049 Install_Visible_Declarations (Par_Scope);
8050 Copy_And_Build;
8051 Uninstall_Declarations (Par_Scope);
8053 -- If parent scope is open and in another unit, and parent has a
8054 -- completion, then the derivation is taking place in the visible
8055 -- part of a child unit. In that case retrieve the full view of
8056 -- the parent momentarily.
8058 elsif not In_Same_Source_Unit (N, Parent_Type)
8059 and then Present (Full_View (Parent_Type))
8060 then
8061 Full_P := Full_View (Parent_Type);
8062 Exchange_Declarations (Parent_Type);
8063 Copy_And_Build;
8064 Exchange_Declarations (Full_P);
8066 -- Otherwise it is a local derivation
8068 else
8069 Copy_And_Build;
8070 end if;
8071 end Build_Full_Derivation;
8073 --------------------
8074 -- Copy_And_Build --
8075 --------------------
8077 procedure Copy_And_Build is
8078 Full_Parent : Entity_Id := Parent_Type;
8080 begin
8081 -- If the parent is itself derived from another private type,
8082 -- installing the private declarations has not affected its
8083 -- privacy status, so use its own full view explicitly.
8085 if Is_Private_Type (Full_Parent)
8086 and then Present (Full_View (Full_Parent))
8087 then
8088 Full_Parent := Full_View (Full_Parent);
8089 end if;
8091 -- If the full view is itself derived from another private type
8092 -- and has got an underlying full view, and this is done for a
8093 -- completion, i.e. to build the underlying full view of the type,
8094 -- then use this underlying full view. We cannot do that if this
8095 -- is not a completion, i.e. to build the full view of the type,
8096 -- because this would break the privacy of the parent type, except
8097 -- if the parent scope is being analyzed because we are deriving a
8098 -- completion.
8100 if Is_Private_Type (Full_Parent)
8101 and then Present (Underlying_Full_View (Full_Parent))
8102 and then (Is_Completion or else In_Private_Part (Par_Scope))
8103 then
8104 Full_Parent := Underlying_Full_View (Full_Parent);
8105 end if;
8107 -- For private, record, concurrent, access and almost all enumeration
8108 -- types, the derivation from the full view requires a fully-fledged
8109 -- declaration. In the other cases, just use an itype.
8111 if Is_Private_Type (Full_Parent)
8112 or else Is_Record_Type (Full_Parent)
8113 or else Is_Concurrent_Type (Full_Parent)
8114 or else Is_Access_Type (Full_Parent)
8115 or else
8116 (Is_Enumeration_Type (Full_Parent)
8117 and then not Is_Standard_Character_Type (Full_Parent)
8118 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8119 then
8120 -- Copy and adjust declaration to provide a completion for what
8121 -- is originally a private declaration. Indicate that full view
8122 -- is internally generated.
8124 Set_Comes_From_Source (Full_N, False);
8125 Set_Comes_From_Source (Full_Der, False);
8126 Set_Parent (Full_Der, Full_N);
8127 Set_Defining_Identifier (Full_N, Full_Der);
8129 -- If there are no constraints, adjust the subtype mark
8131 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8132 N_Subtype_Indication
8133 then
8134 Set_Subtype_Indication
8135 (Type_Definition (Full_N),
8136 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8137 end if;
8139 Insert_After (N, Full_N);
8141 -- Build full view of derived type from full view of parent which
8142 -- is now installed. Subprograms have been derived on the partial
8143 -- view, the completion does not derive them anew.
8145 if Is_Record_Type (Full_Parent) then
8147 -- If parent type is tagged, the completion inherits the proper
8148 -- primitive operations.
8150 if Is_Tagged_Type (Parent_Type) then
8151 Build_Derived_Record_Type
8152 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8153 else
8154 Build_Derived_Record_Type
8155 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8156 end if;
8158 else
8159 -- If the parent type is private, this is not a completion and
8160 -- we build the full derivation recursively as a completion.
8162 Build_Derived_Type
8163 (Full_N, Full_Parent, Full_Der,
8164 Is_Completion => Is_Private_Type (Full_Parent),
8165 Derive_Subps => False);
8166 end if;
8168 -- The full declaration has been introduced into the tree and
8169 -- processed in the step above. It should not be analyzed again
8170 -- (when encountered later in the current list of declarations)
8171 -- to prevent spurious name conflicts. The full entity remains
8172 -- invisible.
8174 Set_Analyzed (Full_N);
8176 else
8177 Full_Der :=
8178 Make_Defining_Identifier (Sloc (Derived_Type),
8179 Chars => Chars (Derived_Type));
8180 Set_Is_Itype (Full_Der);
8181 Set_Associated_Node_For_Itype (Full_Der, N);
8182 Set_Parent (Full_Der, N);
8183 Build_Derived_Type
8184 (N, Full_Parent, Full_Der,
8185 Is_Completion => False, Derive_Subps => False);
8186 Set_Is_Not_Self_Hidden (Full_Der);
8187 end if;
8189 Set_Has_Private_Declaration (Full_Der);
8190 Set_Has_Private_Declaration (Derived_Type);
8192 Set_Scope (Full_Der, Scope (Derived_Type));
8193 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8194 Set_Has_Size_Clause (Full_Der, False);
8195 Set_Has_Alignment_Clause (Full_Der, False);
8196 Set_Has_Delayed_Freeze (Full_Der);
8197 Set_Is_Frozen (Full_Der, False);
8198 Set_Freeze_Node (Full_Der, Empty);
8199 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8200 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8202 -- The convention on the base type may be set in the private part
8203 -- and not propagated to the subtype until later, so we obtain the
8204 -- convention from the base type of the parent.
8206 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8207 end Copy_And_Build;
8209 -- Start of processing for Build_Derived_Private_Type
8211 begin
8212 if Is_Tagged_Type (Parent_Type) then
8213 Full_P := Full_View (Parent_Type);
8215 -- A type extension of a type with unknown discriminants is an
8216 -- indefinite type that the back-end cannot handle directly.
8217 -- We treat it as a private type, and build a completion that is
8218 -- derived from the full view of the parent, and hopefully has
8219 -- known discriminants.
8221 -- If the full view of the parent type has an underlying record view,
8222 -- use it to generate the underlying record view of this derived type
8223 -- (required for chains of derivations with unknown discriminants).
8225 -- Minor optimization: we avoid the generation of useless underlying
8226 -- record view entities if the private type declaration has unknown
8227 -- discriminants but its corresponding full view has no
8228 -- discriminants.
8230 if Has_Unknown_Discriminants (Parent_Type)
8231 and then Present (Full_P)
8232 and then (Has_Discriminants (Full_P)
8233 or else Present (Underlying_Record_View (Full_P)))
8234 and then not In_Open_Scopes (Par_Scope)
8235 and then Expander_Active
8236 then
8237 declare
8238 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8239 New_Ext : constant Node_Id :=
8240 Copy_Separate_Tree
8241 (Record_Extension_Part (Type_Definition (N)));
8242 Decl : Node_Id;
8244 begin
8245 Build_Derived_Record_Type
8246 (N, Parent_Type, Derived_Type, Derive_Subps);
8248 -- Build anonymous completion, as a derivation from the full
8249 -- view of the parent. This is not a completion in the usual
8250 -- sense, because the current type is not private.
8252 Decl :=
8253 Make_Full_Type_Declaration (Loc,
8254 Defining_Identifier => Full_Der,
8255 Type_Definition =>
8256 Make_Derived_Type_Definition (Loc,
8257 Subtype_Indication =>
8258 New_Copy_Tree
8259 (Subtype_Indication (Type_Definition (N))),
8260 Record_Extension_Part => New_Ext));
8262 -- If the parent type has an underlying record view, use it
8263 -- here to build the new underlying record view.
8265 if Present (Underlying_Record_View (Full_P)) then
8266 pragma Assert
8267 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8268 = N_Identifier);
8269 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8270 Underlying_Record_View (Full_P));
8271 end if;
8273 Install_Private_Declarations (Par_Scope);
8274 Install_Visible_Declarations (Par_Scope);
8275 Insert_Before (N, Decl);
8277 -- Mark entity as an underlying record view before analysis,
8278 -- to avoid generating the list of its primitive operations
8279 -- (which is not really required for this entity) and thus
8280 -- prevent spurious errors associated with missing overriding
8281 -- of abstract primitives (overridden only for Derived_Type).
8283 Mutate_Ekind (Full_Der, E_Record_Type);
8284 Set_Is_Underlying_Record_View (Full_Der);
8285 Set_Default_SSO (Full_Der);
8286 Set_No_Reordering (Full_Der, No_Component_Reordering);
8288 Analyze (Decl);
8290 pragma Assert (Has_Discriminants (Full_Der)
8291 and then not Has_Unknown_Discriminants (Full_Der));
8293 Uninstall_Declarations (Par_Scope);
8295 -- Freeze the underlying record view, to prevent generation of
8296 -- useless dispatching information, which is simply shared with
8297 -- the real derived type.
8299 Set_Is_Frozen (Full_Der);
8301 -- If the derived type has access discriminants, create
8302 -- references to their anonymous types now, to prevent
8303 -- back-end problems when their first use is in generated
8304 -- bodies of primitives.
8306 declare
8307 E : Entity_Id;
8309 begin
8310 E := First_Entity (Full_Der);
8312 while Present (E) loop
8313 if Ekind (E) = E_Discriminant
8314 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8315 then
8316 Build_Itype_Reference (Etype (E), Decl);
8317 end if;
8319 Next_Entity (E);
8320 end loop;
8321 end;
8323 -- Set up links between real entity and underlying record view
8325 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8326 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8327 end;
8329 -- If discriminants are known, build derived record
8331 else
8332 Build_Derived_Record_Type
8333 (N, Parent_Type, Derived_Type, Derive_Subps);
8334 end if;
8336 return;
8338 elsif Has_Discriminants (Parent_Type) then
8340 -- Build partial view of derived type from partial view of parent.
8341 -- This must be done before building the full derivation because the
8342 -- second derivation will modify the discriminants of the first and
8343 -- the discriminants are chained with the rest of the components in
8344 -- the full derivation.
8346 Build_Derived_Record_Type
8347 (N, Parent_Type, Derived_Type, Derive_Subps);
8349 -- Build the full derivation if this is not the anonymous derived
8350 -- base type created by Build_Derived_Record_Type in the constrained
8351 -- case (see point 5. of its head comment) since we build it for the
8352 -- derived subtype.
8354 if Present (Available_Full_View (Parent_Type))
8355 and then not Is_Itype (Derived_Type)
8356 then
8357 declare
8358 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8359 Discr : Entity_Id;
8360 Last_Discr : Entity_Id;
8362 begin
8363 -- If this is not a completion, construct the implicit full
8364 -- view by deriving from the full view of the parent type.
8365 -- But if this is a completion, the derived private type
8366 -- being built is a full view and the full derivation can
8367 -- only be its underlying full view.
8369 Build_Full_Derivation;
8371 if not Is_Completion then
8372 Set_Full_View (Derived_Type, Full_Der);
8373 else
8374 Set_Underlying_Full_View (Derived_Type, Full_Der);
8375 Set_Is_Underlying_Full_View (Full_Der);
8376 end if;
8378 if not Is_Base_Type (Derived_Type) then
8379 Set_Full_View (Der_Base, Base_Type (Full_Der));
8380 end if;
8382 -- Copy the discriminant list from full view to the partial
8383 -- view (base type and its subtype). Gigi requires that the
8384 -- partial and full views have the same discriminants.
8386 -- Note that since the partial view points to discriminants
8387 -- in the full view, their scope will be that of the full
8388 -- view. This might cause some front end problems and need
8389 -- adjustment???
8391 Discr := First_Discriminant (Base_Type (Full_Der));
8392 Set_First_Entity (Der_Base, Discr);
8394 loop
8395 Last_Discr := Discr;
8396 Next_Discriminant (Discr);
8397 exit when No (Discr);
8398 end loop;
8400 Set_Last_Entity (Der_Base, Last_Discr);
8401 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8402 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8403 end;
8404 end if;
8406 elsif Present (Available_Full_View (Parent_Type))
8407 and then Has_Discriminants (Available_Full_View (Parent_Type))
8408 then
8409 if Has_Unknown_Discriminants (Parent_Type)
8410 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8411 N_Subtype_Indication
8412 then
8413 Error_Msg_N
8414 ("cannot constrain type with unknown discriminants",
8415 Subtype_Indication (Type_Definition (N)));
8416 return;
8417 end if;
8419 -- If this is not a completion, construct the implicit full view by
8420 -- deriving from the full view of the parent type. But if this is a
8421 -- completion, the derived private type being built is a full view
8422 -- and the full derivation can only be its underlying full view.
8424 Build_Full_Derivation;
8426 if not Is_Completion then
8427 Set_Full_View (Derived_Type, Full_Der);
8428 else
8429 Set_Underlying_Full_View (Derived_Type, Full_Der);
8430 Set_Is_Underlying_Full_View (Full_Der);
8431 end if;
8433 -- In any case, the primitive operations are inherited from the
8434 -- parent type, not from the internal full view.
8436 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8438 if Derive_Subps then
8439 -- Initialize the list of primitive operations to an empty list,
8440 -- to cover tagged types as well as untagged types. For untagged
8441 -- types this is used either to analyze the call as legal when
8442 -- Extensions_Allowed is True, or to issue a better error message
8443 -- otherwise.
8445 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8447 Derive_Subprograms (Parent_Type, Derived_Type);
8448 end if;
8450 Set_Stored_Constraint (Derived_Type, No_Elist);
8451 Set_Is_Constrained
8452 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8454 else
8455 -- Untagged type, No discriminants on either view
8457 if Nkind (Subtype_Indication (Type_Definition (N))) =
8458 N_Subtype_Indication
8459 then
8460 Error_Msg_N
8461 ("illegal constraint on type without discriminants", N);
8462 end if;
8464 if Present (Discriminant_Specifications (N))
8465 and then Present (Available_Full_View (Parent_Type))
8466 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8467 then
8468 Error_Msg_N ("cannot add discriminants to untagged type", N);
8469 end if;
8471 Set_Stored_Constraint (Derived_Type, No_Elist);
8472 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8474 Set_Is_Controlled_Active
8475 (Derived_Type, Is_Controlled_Active (Parent_Type));
8477 Set_Disable_Controlled
8478 (Derived_Type, Disable_Controlled (Parent_Type));
8480 Set_Has_Controlled_Component
8481 (Derived_Type, Has_Controlled_Component (Parent_Type));
8483 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8485 if not Is_Controlled (Parent_Type) then
8486 Set_Finalize_Storage_Only
8487 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8488 end if;
8490 -- If this is not a completion, construct the implicit full view by
8491 -- deriving from the full view of the parent type. But if this is a
8492 -- completion, the derived private type being built is a full view
8493 -- and the full derivation can only be its underlying full view.
8495 -- ??? If the parent type is untagged private and its completion is
8496 -- tagged, this mechanism will not work because we cannot derive from
8497 -- the tagged full view unless we have an extension.
8499 if Present (Available_Full_View (Parent_Type))
8500 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8501 and then not Error_Posted (N)
8502 then
8503 Build_Full_Derivation;
8505 if not Is_Completion then
8506 Set_Full_View (Derived_Type, Full_Der);
8507 else
8508 Set_Underlying_Full_View (Derived_Type, Full_Der);
8509 Set_Is_Underlying_Full_View (Full_Der);
8510 end if;
8511 end if;
8512 end if;
8514 Set_Has_Unknown_Discriminants (Derived_Type,
8515 Has_Unknown_Discriminants (Parent_Type));
8517 if Is_Private_Type (Derived_Type) then
8518 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8519 end if;
8521 -- If the parent base type is in scope, add the derived type to its
8522 -- list of private dependents, because its full view may become
8523 -- visible subsequently (in a nested private part, a body, or in a
8524 -- further child unit).
8526 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8527 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8529 -- Check for unusual case where a type completed by a private
8530 -- derivation occurs within a package nested in a child unit, and
8531 -- the parent is declared in an ancestor.
8533 if Is_Child_Unit (Scope (Current_Scope))
8534 and then Is_Completion
8535 and then In_Private_Part (Current_Scope)
8536 and then Scope (Parent_Type) /= Current_Scope
8538 -- Note that if the parent has a completion in the private part,
8539 -- (which is itself a derivation from some other private type)
8540 -- it is that completion that is visible, there is no full view
8541 -- available, and no special processing is needed.
8543 and then Present (Full_View (Parent_Type))
8544 then
8545 -- In this case, the full view of the parent type will become
8546 -- visible in the body of the enclosing child, and only then will
8547 -- the current type be possibly non-private. Build an underlying
8548 -- full view that will be installed when the enclosing child body
8549 -- is compiled.
8551 if Present (Underlying_Full_View (Derived_Type)) then
8552 Full_Der := Underlying_Full_View (Derived_Type);
8553 else
8554 Build_Full_Derivation;
8555 Set_Underlying_Full_View (Derived_Type, Full_Der);
8556 Set_Is_Underlying_Full_View (Full_Der);
8557 end if;
8559 -- The full view will be used to swap entities on entry/exit to
8560 -- the body, and must appear in the entity list for the package.
8562 Append_Entity (Full_Der, Scope (Derived_Type));
8563 end if;
8564 end if;
8565 end Build_Derived_Private_Type;
8567 -------------------------------
8568 -- Build_Derived_Record_Type --
8569 -------------------------------
8571 -- 1. INTRODUCTION
8573 -- Ideally we would like to use the same model of type derivation for
8574 -- tagged and untagged record types. Unfortunately this is not quite
8575 -- possible because the semantics of representation clauses is different
8576 -- for tagged and untagged records under inheritance. Consider the
8577 -- following:
8579 -- type R (...) is [tagged] record ... end record;
8580 -- type T (...) is new R (...) [with ...];
8582 -- The representation clauses for T can specify a completely different
8583 -- record layout from R's. Hence the same component can be placed in two
8584 -- very different positions in objects of type T and R. If R and T are
8585 -- tagged types, representation clauses for T can only specify the layout
8586 -- of non inherited components, thus components that are common in R and T
8587 -- have the same position in objects of type R and T.
8589 -- This has two implications. The first is that the entire tree for R's
8590 -- declaration needs to be copied for T in the untagged case, so that T
8591 -- can be viewed as a record type of its own with its own representation
8592 -- clauses. The second implication is the way we handle discriminants.
8593 -- Specifically, in the untagged case we need a way to communicate to Gigi
8594 -- what are the real discriminants in the record, while for the semantics
8595 -- we need to consider those introduced by the user to rename the
8596 -- discriminants in the parent type. This is handled by introducing the
8597 -- notion of stored discriminants. See below for more.
8599 -- Fortunately the way regular components are inherited can be handled in
8600 -- the same way in tagged and untagged types.
8602 -- To complicate things a bit more the private view of a private extension
8603 -- cannot be handled in the same way as the full view (for one thing the
8604 -- semantic rules are somewhat different). We will explain what differs
8605 -- below.
8607 -- 2. DISCRIMINANTS UNDER INHERITANCE
8609 -- The semantic rules governing the discriminants of derived types are
8610 -- quite subtle.
8612 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8613 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8615 -- If parent type has discriminants, then the discriminants that are
8616 -- declared in the derived type are [3.4 (11)]:
8618 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8619 -- there is one;
8621 -- o Otherwise, each discriminant of the parent type (implicitly declared
8622 -- in the same order with the same specifications). In this case, the
8623 -- discriminants are said to be "inherited", or if unknown in the parent
8624 -- are also unknown in the derived type.
8626 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8628 -- o The parent subtype must be constrained;
8630 -- o If the parent type is not a tagged type, then each discriminant of
8631 -- the derived type must be used in the constraint defining a parent
8632 -- subtype. [Implementation note: This ensures that the new discriminant
8633 -- can share storage with an existing discriminant.]
8635 -- For the derived type each discriminant of the parent type is either
8636 -- inherited, constrained to equal some new discriminant of the derived
8637 -- type, or constrained to the value of an expression.
8639 -- When inherited or constrained to equal some new discriminant, the
8640 -- parent discriminant and the discriminant of the derived type are said
8641 -- to "correspond".
8643 -- If a discriminant of the parent type is constrained to a specific value
8644 -- in the derived type definition, then the discriminant is said to be
8645 -- "specified" by that derived type definition.
8647 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8649 -- We have spoken about stored discriminants in point 1 (introduction)
8650 -- above. There are two sorts of stored discriminants: implicit and
8651 -- explicit. As long as the derived type inherits the same discriminants as
8652 -- the root record type, stored discriminants are the same as regular
8653 -- discriminants, and are said to be implicit. However, if any discriminant
8654 -- in the root type was renamed in the derived type, then the derived
8655 -- type will contain explicit stored discriminants. Explicit stored
8656 -- discriminants are discriminants in addition to the semantically visible
8657 -- discriminants defined for the derived type. Stored discriminants are
8658 -- used by Gigi to figure out what are the physical discriminants in
8659 -- objects of the derived type (see precise definition in einfo.ads).
8660 -- As an example, consider the following:
8662 -- type R (D1, D2, D3 : Int) is record ... end record;
8663 -- type T1 is new R;
8664 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8665 -- type T3 is new T2;
8666 -- type T4 (Y : Int) is new T3 (Y, 99);
8668 -- The following table summarizes the discriminants and stored
8669 -- discriminants in R and T1 through T4:
8671 -- Type Discrim Stored Discrim Comment
8672 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8673 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8674 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8675 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8676 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8678 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8679 -- find the corresponding discriminant in the parent type, while
8680 -- Original_Record_Component (abbreviated ORC below) the actual physical
8681 -- component that is renamed. Finally the field Is_Completely_Hidden
8682 -- (abbreviated ICH below) is set for all explicit stored discriminants
8683 -- (see einfo.ads for more info). For the above example this gives:
8685 -- Discrim CD ORC ICH
8686 -- ^^^^^^^ ^^ ^^^ ^^^
8687 -- D1 in R empty itself no
8688 -- D2 in R empty itself no
8689 -- D3 in R empty itself no
8691 -- D1 in T1 D1 in R itself no
8692 -- D2 in T1 D2 in R itself no
8693 -- D3 in T1 D3 in R itself no
8695 -- X1 in T2 D3 in T1 D3 in T2 no
8696 -- X2 in T2 D1 in T1 D1 in T2 no
8697 -- D1 in T2 empty itself yes
8698 -- D2 in T2 empty itself yes
8699 -- D3 in T2 empty itself yes
8701 -- X1 in T3 X1 in T2 D3 in T3 no
8702 -- X2 in T3 X2 in T2 D1 in T3 no
8703 -- D1 in T3 empty itself yes
8704 -- D2 in T3 empty itself yes
8705 -- D3 in T3 empty itself yes
8707 -- Y in T4 X1 in T3 D3 in T4 no
8708 -- D1 in T4 empty itself yes
8709 -- D2 in T4 empty itself yes
8710 -- D3 in T4 empty itself yes
8712 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8714 -- Type derivation for tagged types is fairly straightforward. If no
8715 -- discriminants are specified by the derived type, these are inherited
8716 -- from the parent. No explicit stored discriminants are ever necessary.
8717 -- The only manipulation that is done to the tree is that of adding a
8718 -- _parent field with parent type and constrained to the same constraint
8719 -- specified for the parent in the derived type definition. For instance:
8721 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8722 -- type T1 is new R with null record;
8723 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8725 -- are changed into:
8727 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8728 -- _parent : R (D1, D2, D3);
8729 -- end record;
8731 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8732 -- _parent : T1 (X2, 88, X1);
8733 -- end record;
8735 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8736 -- ORC and ICH fields are:
8738 -- Discrim CD ORC ICH
8739 -- ^^^^^^^ ^^ ^^^ ^^^
8740 -- D1 in R empty itself no
8741 -- D2 in R empty itself no
8742 -- D3 in R empty itself no
8744 -- D1 in T1 D1 in R D1 in R no
8745 -- D2 in T1 D2 in R D2 in R no
8746 -- D3 in T1 D3 in R D3 in R no
8748 -- X1 in T2 D3 in T1 D3 in R no
8749 -- X2 in T2 D1 in T1 D1 in R no
8751 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8753 -- Regardless of whether we are dealing with a tagged or untagged type
8754 -- we will transform all derived type declarations of the form
8756 -- type T is new R (...) [with ...];
8757 -- or
8758 -- subtype S is R (...);
8759 -- type T is new S [with ...];
8760 -- into
8761 -- type BT is new R [with ...];
8762 -- subtype T is BT (...);
8764 -- That is, the base derived type is constrained only if it has no
8765 -- discriminants. The reason for doing this is that GNAT's semantic model
8766 -- assumes that a base type with discriminants is unconstrained.
8768 -- Note that, strictly speaking, the above transformation is not always
8769 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8771 -- procedure B34011A is
8772 -- type REC (D : integer := 0) is record
8773 -- I : Integer;
8774 -- end record;
8776 -- package P is
8777 -- type T6 is new Rec;
8778 -- function F return T6;
8779 -- end P;
8781 -- use P;
8782 -- package Q6 is
8783 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8784 -- end Q6;
8786 -- The definition of Q6.U is illegal. However transforming Q6.U into
8788 -- type BaseU is new T6;
8789 -- subtype U is BaseU (Q6.F.I)
8791 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8792 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8793 -- the transformation described above.
8795 -- There is another instance where the above transformation is incorrect.
8796 -- Consider:
8798 -- package Pack is
8799 -- type Base (D : Integer) is tagged null record;
8800 -- procedure P (X : Base);
8802 -- type Der is new Base (2) with null record;
8803 -- procedure P (X : Der);
8804 -- end Pack;
8806 -- Then the above transformation turns this into
8808 -- type Der_Base is new Base with null record;
8809 -- -- procedure P (X : Base) is implicitly inherited here
8810 -- -- as procedure P (X : Der_Base).
8812 -- subtype Der is Der_Base (2);
8813 -- procedure P (X : Der);
8814 -- -- The overriding of P (X : Der_Base) is illegal since we
8815 -- -- have a parameter conformance problem.
8817 -- To get around this problem, after having semantically processed Der_Base
8818 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8819 -- Discriminant_Constraint from Der so that when parameter conformance is
8820 -- checked when P is overridden, no semantic errors are flagged.
8822 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8824 -- Regardless of whether we are dealing with a tagged or untagged type
8825 -- we will transform all derived type declarations of the form
8827 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8828 -- type T is new R [with ...];
8829 -- into
8830 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8832 -- The reason for such transformation is that it allows us to implement a
8833 -- very clean form of component inheritance as explained below.
8835 -- Note that this transformation is not achieved by direct tree rewriting
8836 -- and manipulation, but rather by redoing the semantic actions that the
8837 -- above transformation will entail. This is done directly in routine
8838 -- Inherit_Components.
8840 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8842 -- In both tagged and untagged derived types, regular non discriminant
8843 -- components are inherited in the derived type from the parent type. In
8844 -- the absence of discriminants component, inheritance is straightforward
8845 -- as components can simply be copied from the parent.
8847 -- If the parent has discriminants, inheriting components constrained with
8848 -- these discriminants requires caution. Consider the following example:
8850 -- type R (D1, D2 : Positive) is [tagged] record
8851 -- S : String (D1 .. D2);
8852 -- end record;
8854 -- type T1 is new R [with null record];
8855 -- type T2 (X : positive) is new R (1, X) [with null record];
8857 -- As explained in 6. above, T1 is rewritten as
8858 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8859 -- which makes the treatment for T1 and T2 identical.
8861 -- What we want when inheriting S, is that references to D1 and D2 in R are
8862 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8863 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8864 -- with either discriminant references in the derived type or expressions.
8865 -- This replacement is achieved as follows: before inheriting R's
8866 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8867 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8868 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8869 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8870 -- by String (1 .. X).
8872 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8874 -- We explain here the rules governing private type extensions relevant to
8875 -- type derivation. These rules are explained on the following example:
8877 -- type D [(...)] is new A [(...)] with private; <-- partial view
8878 -- type D [(...)] is new P [(...)] with null record; <-- full view
8880 -- Type A is called the ancestor subtype of the private extension.
8881 -- Type P is the parent type of the full view of the private extension. It
8882 -- must be A or a type derived from A.
8884 -- The rules concerning the discriminants of private type extensions are
8885 -- [7.3(10-13)]:
8887 -- o If a private extension inherits known discriminants from the ancestor
8888 -- subtype, then the full view must also inherit its discriminants from
8889 -- the ancestor subtype and the parent subtype of the full view must be
8890 -- constrained if and only if the ancestor subtype is constrained.
8892 -- o If a partial view has unknown discriminants, then the full view may
8893 -- define a definite or an indefinite subtype, with or without
8894 -- discriminants.
8896 -- o If a partial view has neither known nor unknown discriminants, then
8897 -- the full view must define a definite subtype.
8899 -- o If the ancestor subtype of a private extension has constrained
8900 -- discriminants, then the parent subtype of the full view must impose a
8901 -- statically matching constraint on those discriminants.
8903 -- This means that only the following forms of private extensions are
8904 -- allowed:
8906 -- type D is new A with private; <-- partial view
8907 -- type D is new P with null record; <-- full view
8909 -- If A has no discriminants than P has no discriminants, otherwise P must
8910 -- inherit A's discriminants.
8912 -- type D is new A (...) with private; <-- partial view
8913 -- type D is new P (:::) with null record; <-- full view
8915 -- P must inherit A's discriminants and (...) and (:::) must statically
8916 -- match.
8918 -- subtype A is R (...);
8919 -- type D is new A with private; <-- partial view
8920 -- type D is new P with null record; <-- full view
8922 -- P must have inherited R's discriminants and must be derived from A or
8923 -- any of its subtypes.
8925 -- type D (..) is new A with private; <-- partial view
8926 -- type D (..) is new P [(:::)] with null record; <-- full view
8928 -- No specific constraints on P's discriminants or constraint (:::).
8929 -- Note that A can be unconstrained, but the parent subtype P must either
8930 -- be constrained or (:::) must be present.
8932 -- type D (..) is new A [(...)] with private; <-- partial view
8933 -- type D (..) is new P [(:::)] with null record; <-- full view
8935 -- P's constraints on A's discriminants must statically match those
8936 -- imposed by (...).
8938 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8940 -- The full view of a private extension is handled exactly as described
8941 -- above. The model chose for the private view of a private extension is
8942 -- the same for what concerns discriminants (i.e. they receive the same
8943 -- treatment as in the tagged case). However, the private view of the
8944 -- private extension always inherits the components of the parent base,
8945 -- without replacing any discriminant reference. Strictly speaking this is
8946 -- incorrect. However, Gigi never uses this view to generate code so this
8947 -- is a purely semantic issue. In theory, a set of transformations similar
8948 -- to those given in 5. and 6. above could be applied to private views of
8949 -- private extensions to have the same model of component inheritance as
8950 -- for non private extensions. However, this is not done because it would
8951 -- further complicate private type processing. Semantically speaking, this
8952 -- leaves us in an uncomfortable situation. As an example consider:
8954 -- package Pack is
8955 -- type R (D : integer) is tagged record
8956 -- S : String (1 .. D);
8957 -- end record;
8958 -- procedure P (X : R);
8959 -- type T is new R (1) with private;
8960 -- private
8961 -- type T is new R (1) with null record;
8962 -- end;
8964 -- This is transformed into:
8966 -- package Pack is
8967 -- type R (D : integer) is tagged record
8968 -- S : String (1 .. D);
8969 -- end record;
8970 -- procedure P (X : R);
8971 -- type T is new R (1) with private;
8972 -- private
8973 -- type BaseT is new R with null record;
8974 -- subtype T is BaseT (1);
8975 -- end;
8977 -- (strictly speaking the above is incorrect Ada)
8979 -- From the semantic standpoint the private view of private extension T
8980 -- should be flagged as constrained since one can clearly have
8982 -- Obj : T;
8984 -- in a unit withing Pack. However, when deriving subprograms for the
8985 -- private view of private extension T, T must be seen as unconstrained
8986 -- since T has discriminants (this is a constraint of the current
8987 -- subprogram derivation model). Thus, when processing the private view of
8988 -- a private extension such as T, we first mark T as unconstrained, we
8989 -- process it, we perform program derivation and just before returning from
8990 -- Build_Derived_Record_Type we mark T as constrained.
8992 -- ??? Are there are other uncomfortable cases that we will have to
8993 -- deal with.
8995 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8997 -- Types that are derived from a visible record type and have a private
8998 -- extension present other peculiarities. They behave mostly like private
8999 -- types, but if they have primitive operations defined, these will not
9000 -- have the proper signatures for further inheritance, because other
9001 -- primitive operations will use the implicit base that we define for
9002 -- private derivations below. This affect subprogram inheritance (see
9003 -- Derive_Subprograms for details). We also derive the implicit base from
9004 -- the base type of the full view, so that the implicit base is a record
9005 -- type and not another private type, This avoids infinite loops.
9007 procedure Build_Derived_Record_Type
9008 (N : Node_Id;
9009 Parent_Type : Entity_Id;
9010 Derived_Type : Entity_Id;
9011 Derive_Subps : Boolean := True)
9013 Discriminant_Specs : constant Boolean :=
9014 Present (Discriminant_Specifications (N));
9015 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
9016 Loc : constant Source_Ptr := Sloc (N);
9017 Private_Extension : constant Boolean :=
9018 Nkind (N) = N_Private_Extension_Declaration;
9019 Assoc_List : Elist_Id;
9020 Constraint_Present : Boolean;
9021 Constrs : Elist_Id;
9022 Discrim : Entity_Id;
9023 Indic : Node_Id;
9024 Inherit_Discrims : Boolean := False;
9025 Last_Discrim : Entity_Id;
9026 New_Base : Entity_Id;
9027 New_Decl : Node_Id;
9028 New_Discrs : Elist_Id;
9029 New_Indic : Node_Id;
9030 Parent_Base : Entity_Id;
9031 Save_Etype : Entity_Id;
9032 Save_Discr_Constr : Elist_Id;
9033 Save_Next_Entity : Entity_Id;
9034 Type_Def : Node_Id;
9036 Discs : Elist_Id := New_Elmt_List;
9037 -- An empty Discs list means that there were no constraints in the
9038 -- subtype indication or that there was an error processing it.
9040 procedure Check_Generic_Ancestors;
9041 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
9042 -- cannot be declared at a deeper level than its parent type is
9043 -- removed. The check on derivation within a generic body is also
9044 -- relaxed, but there's a restriction that a derived tagged type
9045 -- cannot be declared in a generic body if it's derived directly
9046 -- or indirectly from a formal type of that generic. This applies
9047 -- to progenitors as well.
9049 -----------------------------
9050 -- Check_Generic_Ancestors --
9051 -----------------------------
9053 procedure Check_Generic_Ancestors is
9054 Ancestor_Type : Entity_Id;
9055 Intf_List : List_Id;
9056 Intf_Name : Node_Id;
9058 procedure Check_Ancestor;
9059 -- For parent and progenitors.
9061 --------------------
9062 -- Check_Ancestor --
9063 --------------------
9065 procedure Check_Ancestor is
9066 begin
9067 -- If the derived type does have a formal type as an ancestor
9068 -- then it's an error if the derived type is declared within
9069 -- the body of the generic unit that declares the formal type
9070 -- in its generic formal part. It's sufficient to check whether
9071 -- the ancestor type is declared inside the same generic body
9072 -- as the derived type (such as within a nested generic spec),
9073 -- in which case the derivation is legal. If the formal type is
9074 -- declared outside of that generic body, then it's certain
9075 -- that the derived type is declared within the generic body
9076 -- of the generic unit declaring the formal type.
9078 if Is_Generic_Type (Ancestor_Type)
9079 and then Enclosing_Generic_Body (Ancestor_Type) /=
9080 Enclosing_Generic_Body (Derived_Type)
9081 then
9082 Error_Msg_NE
9083 ("ancestor type& is formal type of enclosing"
9084 & " generic unit (RM 3.9.1 (4/2))",
9085 Indic, Ancestor_Type);
9086 end if;
9087 end Check_Ancestor;
9089 begin
9090 if Nkind (N) = N_Private_Extension_Declaration then
9091 Intf_List := Interface_List (N);
9092 else
9093 Intf_List := Interface_List (Type_Definition (N));
9094 end if;
9096 if Present (Enclosing_Generic_Body (Derived_Type)) then
9097 Ancestor_Type := Parent_Type;
9099 while not Is_Generic_Type (Ancestor_Type)
9100 and then Etype (Ancestor_Type) /= Ancestor_Type
9101 loop
9102 Ancestor_Type := Etype (Ancestor_Type);
9103 end loop;
9105 Check_Ancestor;
9107 if Present (Intf_List) then
9108 Intf_Name := First (Intf_List);
9109 while Present (Intf_Name) loop
9110 Ancestor_Type := Entity (Intf_Name);
9111 Check_Ancestor;
9112 Next (Intf_Name);
9113 end loop;
9114 end if;
9115 end if;
9116 end Check_Generic_Ancestors;
9118 -- Start of processing for Build_Derived_Record_Type
9120 begin
9121 -- If the parent type is a private extension with discriminants, we
9122 -- need to have an unconstrained type on which to apply the inherited
9123 -- constraint, so we get to the full view. However, this means that the
9124 -- derived type and its implicit base type created below will not point
9125 -- to the same view of their respective parent type and, thus, special
9126 -- glue code like Exp_Ch7.Convert_View is needed to bridge this gap.
9128 if Ekind (Parent_Type) = E_Record_Type_With_Private
9129 and then Has_Discriminants (Parent_Type)
9130 and then Present (Full_View (Parent_Type))
9131 then
9132 Parent_Base := Base_Type (Full_View (Parent_Type));
9133 else
9134 Parent_Base := Base_Type (Parent_Type);
9135 end if;
9137 -- If the parent type is declared as a subtype of another private
9138 -- type with inherited discriminants, its generated base type is
9139 -- itself a record subtype. To further inherit the constraint we
9140 -- need to use its own base to have an unconstrained type on which
9141 -- to apply the inherited constraint.
9143 if Ekind (Parent_Base) = E_Record_Subtype then
9144 Parent_Base := Base_Type (Parent_Base);
9145 end if;
9147 -- If the parent base is a private type and only its full view has
9148 -- discriminants, use the full view's base type.
9150 -- This can happen when we are deriving from a subtype of a derived type
9151 -- of a private type derived from a discriminated type with known
9152 -- discriminant:
9154 -- package Pkg;
9155 -- type Root_Type(I: Positive) is record
9156 -- ...
9157 -- end record;
9158 -- type Bounded_Root_Type is private;
9159 -- private
9160 -- type Bounded_Root_Type is new Root_Type(10);
9161 -- end Pkg;
9163 -- package Pkg2 is
9164 -- type Constrained_Root_Type is new Pkg.Bounded_Root_Type;
9165 -- end Pkg2;
9166 -- subtype Sub_Base is Pkg2.Constrained_Root_Type;
9167 -- type New_Der_Type is new Sub_Base;
9169 if Is_Private_Type (Parent_Base)
9170 and then Present (Full_View (Parent_Base))
9171 and then not Has_Discriminants (Parent_Base)
9172 and then Has_Discriminants (Full_View (Parent_Base))
9173 then
9174 Parent_Base := Base_Type (Full_View (Parent_Base));
9175 end if;
9177 -- AI05-0115: if this is a derivation from a private type in some
9178 -- other scope that may lead to invisible components for the derived
9179 -- type, mark it accordingly.
9181 if Is_Private_Type (Parent_Type) then
9182 if Scope (Parent_Base) = Scope (Derived_Type) then
9183 null;
9185 elsif In_Open_Scopes (Scope (Parent_Base))
9186 and then In_Private_Part (Scope (Parent_Base))
9187 then
9188 null;
9190 else
9191 Set_Has_Private_Ancestor (Derived_Type);
9192 end if;
9194 else
9195 Set_Has_Private_Ancestor
9196 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9197 end if;
9199 -- Before we start the previously documented transformations, here is
9200 -- little fix for size and alignment of tagged types. Normally when we
9201 -- derive type D from type P, we copy the size and alignment of P as the
9202 -- default for D, and in the absence of explicit representation clauses
9203 -- for D, the size and alignment are indeed the same as the parent.
9205 -- But this is wrong for tagged types, since fields may be added, and
9206 -- the default size may need to be larger, and the default alignment may
9207 -- need to be larger.
9209 -- We therefore reset the size and alignment fields in the tagged case.
9210 -- Note that the size and alignment will in any case be at least as
9211 -- large as the parent type (since the derived type has a copy of the
9212 -- parent type in the _parent field)
9214 -- The type is also marked as being tagged here, which is needed when
9215 -- processing components with a self-referential anonymous access type
9216 -- in the call to Check_Anonymous_Access_Components below. Note that
9217 -- this flag is also set later on for completeness.
9219 if Is_Tagged then
9220 Set_Is_Tagged_Type (Derived_Type);
9221 Reinit_Size_Align (Derived_Type);
9222 end if;
9224 -- STEP 0a: figure out what kind of derived type declaration we have
9226 if Private_Extension then
9227 Type_Def := N;
9228 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9229 Set_Default_SSO (Derived_Type);
9230 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9232 else
9233 Type_Def := Type_Definition (N);
9235 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9236 -- Parent_Base can be a private type or private extension. However,
9237 -- for tagged types with an extension the newly added fields are
9238 -- visible and hence the Derived_Type is always an E_Record_Type.
9239 -- (except that the parent may have its own private fields).
9240 -- For untagged types we preserve the Ekind of the Parent_Base.
9242 if Present (Record_Extension_Part (Type_Def)) then
9243 Mutate_Ekind (Derived_Type, E_Record_Type);
9244 Set_Default_SSO (Derived_Type);
9245 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9247 -- Create internal access types for components with anonymous
9248 -- access types.
9250 if Ada_Version >= Ada_2005 then
9251 Check_Anonymous_Access_Components
9252 (N, Derived_Type, Derived_Type,
9253 Component_List (Record_Extension_Part (Type_Def)));
9254 end if;
9256 else
9257 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9258 end if;
9259 end if;
9261 -- Indic can either be an N_Identifier if the subtype indication
9262 -- contains no constraint or an N_Subtype_Indication if the subtype
9263 -- indication has a constraint. In either case it can include an
9264 -- interface list.
9266 Indic := Subtype_Indication (Type_Def);
9267 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9269 -- Check that the type has visible discriminants. The type may be
9270 -- a private type with unknown discriminants whose full view has
9271 -- discriminants which are invisible.
9273 if Constraint_Present then
9274 if not Has_Discriminants (Parent_Base)
9275 or else
9276 (Has_Unknown_Discriminants (Parent_Base)
9277 and then Is_Private_Type (Parent_Base))
9278 then
9279 Error_Msg_N
9280 ("invalid constraint: type has no discriminant",
9281 Constraint (Indic));
9283 Constraint_Present := False;
9284 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9286 elsif Is_Constrained (Parent_Type) then
9287 Error_Msg_N
9288 ("invalid constraint: parent type is already constrained",
9289 Constraint (Indic));
9291 Constraint_Present := False;
9292 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9293 end if;
9294 end if;
9296 -- STEP 0b: If needed, apply transformation given in point 5. above
9298 if not Private_Extension
9299 and then Has_Discriminants (Parent_Type)
9300 and then not Discriminant_Specs
9301 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9302 then
9303 -- First, we must analyze the constraint (see comment in point 5.)
9304 -- The constraint may come from the subtype indication of the full
9305 -- declaration. Temporarily set the state of the Derived_Type to
9306 -- "self-hidden" (see RM-8.3(17)).
9308 if Constraint_Present then
9309 pragma Assert (Is_Not_Self_Hidden (Derived_Type));
9310 Set_Is_Not_Self_Hidden (Derived_Type, False);
9311 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9312 Set_Is_Not_Self_Hidden (Derived_Type);
9314 -- If there is no explicit constraint, there might be one that is
9315 -- inherited from a constrained parent type. In that case verify that
9316 -- it conforms to the constraint in the partial view. In perverse
9317 -- cases the parent subtypes of the partial and full view can have
9318 -- different constraints.
9320 elsif Present (Stored_Constraint (Parent_Type)) then
9321 New_Discrs := Stored_Constraint (Parent_Type);
9323 else
9324 New_Discrs := No_Elist;
9325 end if;
9327 if Has_Discriminants (Derived_Type)
9328 and then Has_Private_Declaration (Derived_Type)
9329 and then Present (Discriminant_Constraint (Derived_Type))
9330 and then Present (New_Discrs)
9331 then
9332 -- Verify that constraints of the full view statically match
9333 -- those given in the partial view.
9335 declare
9336 C1, C2 : Elmt_Id;
9338 begin
9339 C1 := First_Elmt (New_Discrs);
9340 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9341 while Present (C1) and then Present (C2) loop
9342 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9343 or else
9344 (Is_OK_Static_Expression (Node (C1))
9345 and then Is_OK_Static_Expression (Node (C2))
9346 and then
9347 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9348 then
9349 null;
9351 else
9352 if Constraint_Present then
9353 Error_Msg_N
9354 ("constraint not conformant to previous declaration",
9355 Node (C1));
9356 else
9357 Error_Msg_N
9358 ("constraint of full view is incompatible "
9359 & "with partial view", N);
9360 end if;
9361 end if;
9363 Next_Elmt (C1);
9364 Next_Elmt (C2);
9365 end loop;
9366 end;
9367 end if;
9369 -- Insert and analyze the declaration for the unconstrained base type
9371 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9373 New_Decl :=
9374 Make_Full_Type_Declaration (Loc,
9375 Defining_Identifier => New_Base,
9376 Type_Definition =>
9377 Make_Derived_Type_Definition (Loc,
9378 Abstract_Present => Abstract_Present (Type_Def),
9379 Limited_Present => Limited_Present (Type_Def),
9380 Subtype_Indication =>
9381 New_Occurrence_Of (Parent_Base, Loc),
9382 Record_Extension_Part =>
9383 Relocate_Node (Record_Extension_Part (Type_Def)),
9384 Interface_List => Interface_List (Type_Def)));
9386 Set_Parent (New_Decl, Parent (N));
9387 Mark_Rewrite_Insertion (New_Decl);
9388 Insert_Before (N, New_Decl);
9390 -- In the extension case, make sure ancestor is frozen appropriately
9391 -- (see also non-discriminated case below).
9393 if Present (Record_Extension_Part (Type_Def))
9394 or else Is_Interface (Parent_Base)
9395 then
9396 Freeze_Before (New_Decl, Parent_Type);
9397 end if;
9399 -- Note that this call passes False for the Derive_Subps parameter
9400 -- because subprogram derivation is deferred until after creating
9401 -- the subtype (see below).
9403 Build_Derived_Type
9404 (New_Decl, Parent_Base, New_Base,
9405 Is_Completion => False, Derive_Subps => False);
9407 -- ??? This needs re-examination to determine whether the
9408 -- following call can simply be replaced by a call to Analyze.
9410 Set_Analyzed (New_Decl);
9412 -- Insert and analyze the declaration for the constrained subtype
9414 if Constraint_Present then
9415 New_Indic :=
9416 Make_Subtype_Indication (Loc,
9417 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9418 Constraint => Relocate_Node (Constraint (Indic)));
9420 else
9421 declare
9422 Constr_List : constant List_Id := New_List;
9423 C : Elmt_Id;
9424 Expr : Node_Id;
9426 begin
9427 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9428 while Present (C) loop
9429 Expr := Node (C);
9431 -- It is safe here to call New_Copy_Tree since we called
9432 -- Force_Evaluation on each constraint previously
9433 -- in Build_Discriminant_Constraints.
9435 Append (New_Copy_Tree (Expr), To => Constr_List);
9437 Next_Elmt (C);
9438 end loop;
9440 New_Indic :=
9441 Make_Subtype_Indication (Loc,
9442 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9443 Constraint =>
9444 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9445 end;
9446 end if;
9448 Rewrite (N,
9449 Make_Subtype_Declaration (Loc,
9450 Defining_Identifier => Derived_Type,
9451 Subtype_Indication => New_Indic));
9453 -- Keep the aspects from the original node
9455 Move_Aspects (Original_Node (N), N);
9457 Analyze (N);
9459 -- Derivation of subprograms must be delayed until the full subtype
9460 -- has been established, to ensure proper overriding of subprograms
9461 -- inherited by full types. If the derivations occurred as part of
9462 -- the call to Build_Derived_Type above, then the check for type
9463 -- conformance would fail because earlier primitive subprograms
9464 -- could still refer to the full type prior the change to the new
9465 -- subtype and hence would not match the new base type created here.
9466 -- Subprograms are not derived, however, when Derive_Subps is False
9467 -- (since otherwise there could be redundant derivations).
9469 if Derive_Subps then
9470 Derive_Subprograms (Parent_Type, Derived_Type);
9471 end if;
9473 -- For tagged types the Discriminant_Constraint of the new base itype
9474 -- is inherited from the first subtype so that no subtype conformance
9475 -- problem arise when the first subtype overrides primitive
9476 -- operations inherited by the implicit base type.
9478 if Is_Tagged then
9479 Set_Discriminant_Constraint
9480 (New_Base, Discriminant_Constraint (Derived_Type));
9481 end if;
9483 return;
9484 end if;
9486 -- If we get here Derived_Type will have no discriminants or it will be
9487 -- a discriminated unconstrained base type.
9489 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9491 if Is_Tagged then
9493 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9494 -- The declaration of a specific descendant of an interface type
9495 -- freezes the interface type (RM 13.14).
9497 if not Private_Extension or else Is_Interface (Parent_Base) then
9498 Freeze_Before (N, Parent_Type);
9499 end if;
9501 if Ada_Version >= Ada_2005 then
9502 Check_Generic_Ancestors;
9504 elsif Type_Access_Level (Derived_Type) /=
9505 Type_Access_Level (Parent_Type)
9506 and then not Is_Generic_Type (Derived_Type)
9507 then
9508 if Is_Controlled (Parent_Type) then
9509 Error_Msg_N
9510 ("controlled type must be declared at the library level",
9511 Indic);
9512 else
9513 Error_Msg_N
9514 ("type extension at deeper accessibility level than parent",
9515 Indic);
9516 end if;
9518 else
9519 declare
9520 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9521 begin
9522 if Present (GB)
9523 and then GB /= Enclosing_Generic_Body (Parent_Base)
9524 then
9525 Error_Msg_NE
9526 ("parent type of& must not be outside generic body"
9527 & " (RM 3.9.1(4))",
9528 Indic, Derived_Type);
9529 end if;
9530 end;
9531 end if;
9532 end if;
9534 -- Ada 2005 (AI-251)
9536 if Ada_Version >= Ada_2005 and then Is_Tagged then
9538 -- "The declaration of a specific descendant of an interface type
9539 -- freezes the interface type" (RM 13.14).
9541 declare
9542 Iface : Node_Id;
9543 begin
9544 Iface := First (Interface_List (Type_Def));
9545 while Present (Iface) loop
9546 Freeze_Before (N, Etype (Iface));
9547 Next (Iface);
9548 end loop;
9549 end;
9550 end if;
9552 -- STEP 1b : preliminary cleanup of the full view of private types
9554 -- If the type is already marked as having discriminants, then it's the
9555 -- completion of a private type or private extension and we need to
9556 -- retain the discriminants from the partial view if the current
9557 -- declaration has Discriminant_Specifications so that we can verify
9558 -- conformance. However, we must remove any existing components that
9559 -- were inherited from the parent (and attached in Copy_And_Swap)
9560 -- because the full type inherits all appropriate components anyway, and
9561 -- we do not want the partial view's components interfering.
9563 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9564 Discrim := First_Discriminant (Derived_Type);
9565 loop
9566 Last_Discrim := Discrim;
9567 Next_Discriminant (Discrim);
9568 exit when No (Discrim);
9569 end loop;
9571 Set_Last_Entity (Derived_Type, Last_Discrim);
9573 -- In all other cases wipe out the list of inherited components (even
9574 -- inherited discriminants), it will be properly rebuilt here.
9576 else
9577 Set_First_Entity (Derived_Type, Empty);
9578 Set_Last_Entity (Derived_Type, Empty);
9579 end if;
9581 -- STEP 1c: Initialize some flags for the Derived_Type
9583 -- The following flags must be initialized here so that
9584 -- Process_Discriminants can check that discriminants of tagged types do
9585 -- not have a default initial value and that access discriminants are
9586 -- only specified for limited records. For completeness, these flags are
9587 -- also initialized along with all the other flags below.
9589 -- AI-419: Limitedness is not inherited from an interface parent, so to
9590 -- be limited in that case the type must be explicitly declared as
9591 -- limited, or synchronized. While task and protected interfaces are
9592 -- always limited, a synchronized private extension might not inherit
9593 -- from such interfaces, and so we also need to recognize the
9594 -- explicit limitedness implied by a synchronized private extension
9595 -- that does not derive from a synchronized interface (see RM-7.3(6/2)).
9597 if Limited_Present (Type_Def)
9598 or else Synchronized_Present (Type_Def)
9599 then
9600 Set_Is_Limited_Record (Derived_Type);
9602 elsif Is_Limited_Record (Parent_Type)
9603 or else (Present (Full_View (Parent_Type))
9604 and then Is_Limited_Record (Full_View (Parent_Type)))
9605 then
9606 if not Is_Interface (Parent_Type)
9607 or else Is_Concurrent_Interface (Parent_Type)
9608 then
9609 Set_Is_Limited_Record (Derived_Type);
9610 end if;
9611 end if;
9613 -- STEP 2a: process discriminants of derived type if any
9615 Push_Scope (Derived_Type);
9617 if Discriminant_Specs then
9618 Set_Has_Unknown_Discriminants (Derived_Type, False);
9620 -- The following call to Check_Or_Process_Discriminants initializes
9621 -- fields Has_Discriminants and Discriminant_Constraint, unless we
9622 -- are processing the completion of a private type declaration.
9623 -- Temporarily set the state of the Derived_Type to "self-hidden"
9624 -- (see RM-8.3(17)), unless it is already the case.
9626 if Is_Not_Self_Hidden (Derived_Type) then
9627 Set_Is_Not_Self_Hidden (Derived_Type, False);
9628 Check_Or_Process_Discriminants (N, Derived_Type);
9629 Set_Is_Not_Self_Hidden (Derived_Type);
9630 else
9631 Check_Or_Process_Discriminants (N, Derived_Type);
9632 end if;
9634 -- For untagged types, the constraint on the Parent_Type must be
9635 -- present and is used to rename the discriminants.
9637 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9638 Error_Msg_N ("untagged parent must have discriminants", Indic);
9640 elsif not Is_Tagged and then not Constraint_Present then
9641 Error_Msg_N
9642 ("discriminant constraint needed for derived untagged records",
9643 Indic);
9645 -- Otherwise the parent subtype must be constrained unless we have a
9646 -- private extension.
9648 elsif not Constraint_Present
9649 and then not Private_Extension
9650 and then not Is_Constrained (Parent_Type)
9651 then
9652 Error_Msg_N
9653 ("unconstrained type not allowed in this context", Indic);
9655 elsif Constraint_Present then
9656 -- The following call sets the field Corresponding_Discriminant
9657 -- for the discriminants in the Derived_Type.
9659 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9661 -- For untagged types all new discriminants must rename
9662 -- discriminants in the parent. For private extensions new
9663 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9665 Discrim := First_Discriminant (Derived_Type);
9666 while Present (Discrim) loop
9667 if not Is_Tagged
9668 and then No (Corresponding_Discriminant (Discrim))
9669 then
9670 Error_Msg_N
9671 ("new discriminants must constrain old ones", Discrim);
9673 elsif Private_Extension
9674 and then Present (Corresponding_Discriminant (Discrim))
9675 then
9676 Error_Msg_N
9677 ("only static constraints allowed for parent"
9678 & " discriminants in the partial view", Indic);
9679 exit;
9680 end if;
9682 -- If a new discriminant is used in the constraint, then its
9683 -- subtype must be statically compatible with the subtype of
9684 -- the parent discriminant (RM 3.7(15)).
9686 if Present (Corresponding_Discriminant (Discrim)) then
9687 Check_Constraining_Discriminant
9688 (Discrim, Corresponding_Discriminant (Discrim));
9689 end if;
9691 Next_Discriminant (Discrim);
9692 end loop;
9694 -- Check whether the constraints of the full view statically
9695 -- match those imposed by the parent subtype [7.3(13)].
9697 if Present (Stored_Constraint (Derived_Type)) then
9698 declare
9699 C1, C2 : Elmt_Id;
9701 begin
9702 C1 := First_Elmt (Discs);
9703 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9704 while Present (C1) and then Present (C2) loop
9705 if not
9706 Fully_Conformant_Expressions (Node (C1), Node (C2))
9707 then
9708 Error_Msg_N
9709 ("not conformant with previous declaration",
9710 Node (C1));
9711 end if;
9713 Next_Elmt (C1);
9714 Next_Elmt (C2);
9715 end loop;
9716 end;
9717 end if;
9718 end if;
9720 -- STEP 2b: No new discriminants, inherit discriminants if any
9722 else
9723 if Private_Extension then
9724 Set_Has_Unknown_Discriminants
9725 (Derived_Type,
9726 Has_Unknown_Discriminants (Parent_Type)
9727 or else Unknown_Discriminants_Present (N));
9729 -- The partial view of the parent may have unknown discriminants,
9730 -- but if the full view has discriminants and the parent type is
9731 -- in scope they must be inherited.
9733 elsif Has_Unknown_Discriminants (Parent_Type)
9734 and then
9735 (not Has_Discriminants (Parent_Type)
9736 or else not In_Open_Scopes (Scope (Parent_Base)))
9737 then
9738 Set_Has_Unknown_Discriminants (Derived_Type);
9739 end if;
9741 if not Has_Unknown_Discriminants (Derived_Type)
9742 and then not Has_Unknown_Discriminants (Parent_Base)
9743 and then Has_Discriminants (Parent_Type)
9744 then
9745 Inherit_Discrims := True;
9746 Set_Has_Discriminants
9747 (Derived_Type, True);
9748 Set_Discriminant_Constraint
9749 (Derived_Type, Discriminant_Constraint (Parent_Base));
9750 end if;
9752 -- The following test is true for private types (remember
9753 -- transformation 5. is not applied to those) and in an error
9754 -- situation.
9756 if Constraint_Present then
9757 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9758 end if;
9760 -- For now mark a new derived type as constrained only if it has no
9761 -- discriminants. At the end of Build_Derived_Record_Type we properly
9762 -- set this flag in the case of private extensions. See comments in
9763 -- point 9. just before body of Build_Derived_Record_Type.
9765 Set_Is_Constrained
9766 (Derived_Type,
9767 not (Inherit_Discrims
9768 or else Has_Unknown_Discriminants (Derived_Type)));
9769 end if;
9771 -- STEP 3: initialize fields of derived type
9773 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9774 Set_Stored_Constraint (Derived_Type, No_Elist);
9776 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9777 -- but cannot be interfaces
9779 if not Private_Extension
9780 and then Ekind (Derived_Type) /= E_Private_Type
9781 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9782 then
9783 if Interface_Present (Type_Def) then
9784 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9785 end if;
9787 Set_Interfaces (Derived_Type, No_Elist);
9788 end if;
9790 -- Fields inherited from the Parent_Type
9792 Set_Has_Specified_Layout
9793 (Derived_Type, Has_Specified_Layout (Parent_Type));
9794 Set_Is_Limited_Composite
9795 (Derived_Type, Is_Limited_Composite (Parent_Type));
9796 Set_Is_Private_Composite
9797 (Derived_Type, Is_Private_Composite (Parent_Type));
9799 if Is_Tagged_Type (Parent_Type) then
9800 Set_No_Tagged_Streams_Pragma
9801 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9802 end if;
9804 -- Fields inherited from the Parent_Base
9806 Set_Has_Controlled_Component
9807 (Derived_Type, Has_Controlled_Component (Parent_Base));
9808 Set_Has_Non_Standard_Rep
9809 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9810 Set_Has_Primitive_Operations
9811 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9813 -- Set fields for private derived types
9815 if Is_Private_Type (Derived_Type) then
9816 Set_Depends_On_Private (Derived_Type, True);
9817 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9818 end if;
9820 -- Inherit fields for non-private types. If this is the completion of a
9821 -- derivation from a private type, the parent itself is private and the
9822 -- attributes come from its full view, which must be present.
9824 if Is_Record_Type (Derived_Type) then
9825 declare
9826 Parent_Full : Entity_Id;
9828 begin
9829 if Is_Private_Type (Parent_Base)
9830 and then not Is_Record_Type (Parent_Base)
9831 then
9832 Parent_Full := Full_View (Parent_Base);
9833 else
9834 Parent_Full := Parent_Base;
9835 end if;
9837 Set_Component_Alignment
9838 (Derived_Type, Component_Alignment (Parent_Full));
9839 Set_C_Pass_By_Copy
9840 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9841 Set_Has_Complex_Representation
9842 (Derived_Type, Has_Complex_Representation (Parent_Full));
9844 -- For untagged types, inherit the layout by default to avoid
9845 -- costly changes of representation for type conversions.
9847 if not Is_Tagged then
9848 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9849 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9850 end if;
9851 end;
9852 end if;
9854 -- Initialize the list of primitive operations to an empty list,
9855 -- to cover tagged types as well as untagged types. For untagged
9856 -- types this is used either to analyze the call as legal when
9857 -- Extensions_Allowed is True, or to issue a better error message
9858 -- otherwise.
9860 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9862 -- Set fields for tagged types
9864 if Is_Tagged then
9865 -- All tagged types defined in Ada.Finalization are controlled
9867 if Chars (Scope (Derived_Type)) = Name_Finalization
9868 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9869 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9870 then
9871 Set_Is_Controlled_Active (Derived_Type);
9872 else
9873 Set_Is_Controlled_Active
9874 (Derived_Type, Is_Controlled_Active (Parent_Base));
9875 end if;
9877 -- Minor optimization: there is no need to generate the class-wide
9878 -- entity associated with an underlying record view.
9880 if not Is_Underlying_Record_View (Derived_Type) then
9881 Make_Class_Wide_Type (Derived_Type);
9882 end if;
9884 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9886 if Has_Discriminants (Derived_Type)
9887 and then Constraint_Present
9888 then
9889 Set_Stored_Constraint
9890 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9891 end if;
9893 if Ada_Version >= Ada_2005 then
9894 declare
9895 Ifaces_List : Elist_Id;
9897 begin
9898 -- Checks rules 3.9.4 (13/2 and 14/2)
9900 if Comes_From_Source (Derived_Type)
9901 and then not Is_Private_Type (Derived_Type)
9902 and then Is_Interface (Parent_Type)
9903 and then not Is_Interface (Derived_Type)
9904 then
9905 if Is_Task_Interface (Parent_Type) then
9906 Error_Msg_N
9907 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9908 Derived_Type);
9910 elsif Is_Protected_Interface (Parent_Type) then
9911 Error_Msg_N
9912 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9913 Derived_Type);
9914 end if;
9915 end if;
9917 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9919 Check_Interfaces (N, Type_Def);
9921 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9922 -- not already in the parents.
9924 Collect_Interfaces
9925 (T => Derived_Type,
9926 Ifaces_List => Ifaces_List,
9927 Exclude_Parents => True);
9929 Set_Interfaces (Derived_Type, Ifaces_List);
9931 -- If the derived type is the anonymous type created for
9932 -- a declaration whose parent has a constraint, propagate
9933 -- the interface list to the source type. This must be done
9934 -- prior to the completion of the analysis of the source type
9935 -- because the components in the extension may contain current
9936 -- instances whose legality depends on some ancestor.
9938 if Is_Itype (Derived_Type) then
9939 declare
9940 Def : constant Node_Id :=
9941 Associated_Node_For_Itype (Derived_Type);
9942 begin
9943 if Present (Def)
9944 and then Nkind (Def) = N_Full_Type_Declaration
9945 then
9946 Set_Interfaces
9947 (Defining_Identifier (Def), Ifaces_List);
9948 end if;
9949 end;
9950 end if;
9952 -- A type extension is automatically Ghost when one of its
9953 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9954 -- also inherited when the parent type is Ghost, but this is
9955 -- done in Build_Derived_Type as the mechanism also handles
9956 -- untagged derivations.
9958 if Implements_Ghost_Interface (Derived_Type) then
9959 Set_Is_Ghost_Entity (Derived_Type);
9960 end if;
9961 end;
9962 end if;
9963 end if;
9965 -- STEP 4: Inherit components from the parent base and constrain them.
9966 -- Apply the second transformation described in point 6. above.
9968 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9969 or else not Has_Discriminants (Parent_Type)
9970 or else not Is_Constrained (Parent_Type)
9971 then
9972 Constrs := Discs;
9973 else
9974 Constrs := Discriminant_Constraint (Parent_Type);
9975 end if;
9977 Assoc_List :=
9978 Inherit_Components
9979 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9981 -- STEP 5a: Copy the parent record declaration for untagged types
9983 Set_Has_Implicit_Dereference
9984 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9986 if not Is_Tagged then
9988 -- Discriminant_Constraint (Derived_Type) has been properly
9989 -- constructed. Save it and temporarily set it to Empty because we
9990 -- do not want the call to New_Copy_Tree below to mess this list.
9992 if Has_Discriminants (Derived_Type) then
9993 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9994 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9995 else
9996 Save_Discr_Constr := No_Elist;
9997 end if;
9999 -- Save the Etype field of Derived_Type. It is correctly set now,
10000 -- but the call to New_Copy tree may remap it to point to itself,
10001 -- which is not what we want. Ditto for the Next_Entity field.
10003 Save_Etype := Etype (Derived_Type);
10004 Save_Next_Entity := Next_Entity (Derived_Type);
10006 -- Assoc_List maps all stored discriminants in the Parent_Base to
10007 -- stored discriminants in the Derived_Type. It is fundamental that
10008 -- no types or itypes with discriminants other than the stored
10009 -- discriminants appear in the entities declared inside
10010 -- Derived_Type, since the back end cannot deal with it.
10012 New_Decl :=
10013 New_Copy_Tree
10014 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
10015 Copy_Dimensions_Of_Components (Derived_Type);
10017 -- Restore the fields saved prior to the New_Copy_Tree call
10018 -- and compute the stored constraint.
10020 Set_Etype (Derived_Type, Save_Etype);
10021 Link_Entities (Derived_Type, Save_Next_Entity);
10023 if Has_Discriminants (Derived_Type) then
10024 Set_Discriminant_Constraint
10025 (Derived_Type, Save_Discr_Constr);
10026 Set_Stored_Constraint
10027 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
10029 Replace_Discriminants (Derived_Type, New_Decl);
10030 end if;
10032 -- Relocate the aspects from the original type
10034 Remove_Aspects (New_Decl);
10035 Move_Aspects (N, New_Decl);
10037 -- Insert the new derived type declaration
10039 Rewrite (N, New_Decl);
10041 -- STEP 5b: Complete the processing for record extensions in generics
10043 -- There is no completion for record extensions declared in the
10044 -- parameter part of a generic, so we need to complete processing for
10045 -- these generic record extensions here. Record_Type_Definition will
10046 -- set the Is_Not_Self_Hidden flag.
10048 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
10049 Record_Type_Definition (Empty, Derived_Type);
10051 -- STEP 5c: Process the record extension for non private tagged types
10053 elsif not Private_Extension then
10054 Expand_Record_Extension (Derived_Type, Type_Def);
10056 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
10057 -- implemented interfaces if we are in expansion mode
10059 if Expander_Active
10060 and then Has_Interfaces (Derived_Type)
10061 then
10062 Add_Interface_Tag_Components (N, Derived_Type);
10063 end if;
10065 -- Analyze the record extension
10067 Record_Type_Definition
10068 (Record_Extension_Part (Type_Def), Derived_Type);
10069 end if;
10071 End_Scope;
10073 -- Nothing else to do if there is an error in the derivation.
10074 -- An unusual case: the full view may be derived from a type in an
10075 -- instance, when the partial view was used illegally as an actual
10076 -- in that instance, leading to a circular definition.
10078 if Etype (Derived_Type) = Any_Type
10079 or else Etype (Parent_Type) = Derived_Type
10080 then
10081 return;
10082 end if;
10084 -- Set delayed freeze and then derive subprograms, we need to do
10085 -- this in this order so that derived subprograms inherit the
10086 -- derived freeze if necessary.
10088 Set_Has_Delayed_Freeze (Derived_Type);
10090 if Derive_Subps then
10091 Derive_Subprograms (Parent_Type, Derived_Type);
10092 end if;
10094 -- If we have a private extension which defines a constrained derived
10095 -- type mark as constrained here after we have derived subprograms. See
10096 -- comment on point 9. just above the body of Build_Derived_Record_Type.
10098 if Private_Extension and then Inherit_Discrims then
10099 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
10100 Set_Is_Constrained (Derived_Type, True);
10101 Set_Discriminant_Constraint (Derived_Type, Discs);
10103 elsif Is_Constrained (Parent_Type) then
10104 Set_Is_Constrained
10105 (Derived_Type, True);
10106 Set_Discriminant_Constraint
10107 (Derived_Type, Discriminant_Constraint (Parent_Type));
10108 end if;
10109 end if;
10111 -- Update the class-wide type, which shares the now-completed entity
10112 -- list with its specific type. In case of underlying record views,
10113 -- we do not generate the corresponding class wide entity.
10115 if Is_Tagged
10116 and then not Is_Underlying_Record_View (Derived_Type)
10117 then
10118 Set_First_Entity
10119 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
10120 Set_Last_Entity
10121 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
10122 end if;
10124 Check_Function_Writable_Actuals (N);
10125 end Build_Derived_Record_Type;
10127 ------------------------
10128 -- Build_Derived_Type --
10129 ------------------------
10131 procedure Build_Derived_Type
10132 (N : Node_Id;
10133 Parent_Type : Entity_Id;
10134 Derived_Type : Entity_Id;
10135 Is_Completion : Boolean;
10136 Derive_Subps : Boolean := True)
10138 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10140 begin
10141 -- Set common attributes
10143 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10144 and then Ekind (Parent_Base) in Elementary_Kind
10145 then
10146 Reinit_Field_To_Zero (Derived_Type, F_Discriminant_Constraint);
10147 end if;
10149 Set_Scope (Derived_Type, Current_Scope);
10150 Set_Etype (Derived_Type, Parent_Base);
10151 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10152 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10154 Set_Size_Info (Derived_Type, Parent_Type);
10155 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10157 Set_Is_Controlled_Active
10158 (Derived_Type, Is_Controlled_Active (Parent_Type));
10160 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
10161 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10162 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10164 if Is_Tagged_Type (Derived_Type) then
10165 Set_No_Tagged_Streams_Pragma
10166 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10167 end if;
10169 -- If the parent has primitive routines and may have not-seen-yet aspect
10170 -- specifications (e.g., a Pack pragma), then set the derived type link
10171 -- in order to later diagnose "early derivation" issues. If in different
10172 -- compilation units, then "early derivation" cannot be an issue (and we
10173 -- don't like interunit references that go in the opposite direction of
10174 -- semantic dependencies).
10176 if Has_Primitive_Operations (Parent_Type)
10177 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10178 Enclosing_Comp_Unit_Node (Derived_Type)
10179 then
10180 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10181 end if;
10183 -- If the parent type is a private subtype, the convention on the base
10184 -- type may be set in the private part, and not propagated to the
10185 -- subtype until later, so we obtain the convention from the base type.
10187 Set_Convention (Derived_Type, Convention (Parent_Base));
10189 if Is_Tagged_Type (Derived_Type)
10190 and then Present (Class_Wide_Type (Derived_Type))
10191 then
10192 Set_Convention (Class_Wide_Type (Derived_Type),
10193 Convention (Class_Wide_Type (Parent_Base)));
10194 end if;
10196 -- Set SSO default for record or array type
10198 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10199 and then Is_Base_Type (Derived_Type)
10200 then
10201 Set_Default_SSO (Derived_Type);
10202 end if;
10204 -- A derived type inherits the Default_Initial_Condition pragma coming
10205 -- from any parent type within the derivation chain.
10207 if Has_DIC (Parent_Type) then
10208 Set_Has_Inherited_DIC (Derived_Type);
10209 end if;
10211 -- A derived type inherits any class-wide invariants coming from a
10212 -- parent type or an interface. Note that the invariant procedure of
10213 -- the parent type should not be inherited because the derived type may
10214 -- define invariants of its own.
10216 if not Is_Interface (Derived_Type) then
10217 if Has_Inherited_Invariants (Parent_Type)
10218 or else Has_Inheritable_Invariants (Parent_Type)
10219 then
10220 Set_Has_Inherited_Invariants (Derived_Type);
10222 elsif Is_Concurrent_Type (Derived_Type)
10223 or else Is_Tagged_Type (Derived_Type)
10224 then
10225 declare
10226 Iface : Entity_Id;
10227 Ifaces : Elist_Id;
10228 Iface_Elmt : Elmt_Id;
10230 begin
10231 Collect_Interfaces
10232 (T => Derived_Type,
10233 Ifaces_List => Ifaces,
10234 Exclude_Parents => True);
10236 if Present (Ifaces) then
10237 Iface_Elmt := First_Elmt (Ifaces);
10238 while Present (Iface_Elmt) loop
10239 Iface := Node (Iface_Elmt);
10241 if Has_Inheritable_Invariants (Iface) then
10242 Set_Has_Inherited_Invariants (Derived_Type);
10243 exit;
10244 end if;
10246 Next_Elmt (Iface_Elmt);
10247 end loop;
10248 end if;
10249 end;
10250 end if;
10251 end if;
10253 -- We similarly inherit predicates
10255 Inherit_Predicate_Flags (Derived_Type, Parent_Type, Only_Flags => True);
10257 -- The derived type inherits representation clauses from the parent
10258 -- type, and from any interfaces.
10260 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10262 declare
10263 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10264 begin
10265 while Present (Iface) loop
10266 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10267 Next (Iface);
10268 end loop;
10269 end;
10271 -- If the parent type has delayed rep aspects, then mark the derived
10272 -- type as possibly inheriting a delayed rep aspect.
10274 if Has_Delayed_Rep_Aspects (Parent_Type) then
10275 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10276 end if;
10278 -- A derived type becomes Ghost when its parent type is also Ghost
10279 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10280 -- directly inherited because the Ghost policy in effect may differ.
10282 if Is_Ghost_Entity (Parent_Type) then
10283 Set_Is_Ghost_Entity (Derived_Type);
10284 end if;
10286 -- Type dependent processing
10288 case Ekind (Parent_Type) is
10289 when Numeric_Kind =>
10290 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10292 when Array_Kind =>
10293 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10295 when Class_Wide_Kind
10296 | E_Record_Subtype
10297 | E_Record_Type
10299 Build_Derived_Record_Type
10300 (N, Parent_Type, Derived_Type, Derive_Subps);
10301 return;
10303 when Enumeration_Kind =>
10304 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10306 when Access_Kind =>
10307 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10309 when Incomplete_Or_Private_Kind =>
10310 Build_Derived_Private_Type
10311 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10313 -- For discriminated types, the derivation includes deriving
10314 -- primitive operations. For others it is done below.
10316 if Is_Tagged_Type (Parent_Type)
10317 or else Has_Discriminants (Parent_Type)
10318 or else (Present (Full_View (Parent_Type))
10319 and then Has_Discriminants (Full_View (Parent_Type)))
10320 then
10321 return;
10322 end if;
10324 when Concurrent_Kind =>
10325 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10327 when others =>
10328 raise Program_Error;
10329 end case;
10331 -- Nothing more to do if some error occurred
10333 if Etype (Derived_Type) = Any_Type then
10334 return;
10335 end if;
10337 -- If not already set, initialize the derived type's list of primitive
10338 -- operations to an empty element list.
10340 if No (Direct_Primitive_Operations (Derived_Type)) then
10341 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10343 -- If Etype of the derived type is the base type (as opposed to
10344 -- a parent type) and doesn't have an associated list of primitive
10345 -- operations, then set the base type's primitive list to the
10346 -- derived type's list. The lists need to be shared in common
10347 -- between the two.
10349 if Etype (Derived_Type) = Base_Type (Derived_Type)
10350 and then No (Direct_Primitive_Operations (Etype (Derived_Type)))
10351 then
10352 Set_Direct_Primitive_Operations
10353 (Etype (Derived_Type),
10354 Direct_Primitive_Operations (Derived_Type));
10355 end if;
10356 end if;
10358 -- Set delayed freeze and then derive subprograms, we need to do this
10359 -- in this order so that derived subprograms inherit the derived freeze
10360 -- if necessary.
10362 Set_Has_Delayed_Freeze (Derived_Type);
10364 if Derive_Subps then
10365 Derive_Subprograms (Parent_Type, Derived_Type);
10366 end if;
10368 Set_Has_Primitive_Operations
10369 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10370 end Build_Derived_Type;
10372 -----------------------
10373 -- Build_Discriminal --
10374 -----------------------
10376 procedure Build_Discriminal (Discrim : Entity_Id) is
10377 D_Minal : Entity_Id;
10378 CR_Disc : Entity_Id;
10380 begin
10381 -- A discriminal has the same name as the discriminant
10383 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10385 Mutate_Ekind (D_Minal, E_In_Parameter);
10386 Set_Mechanism (D_Minal, Default_Mechanism);
10387 Set_Etype (D_Minal, Etype (Discrim));
10388 Set_Scope (D_Minal, Current_Scope);
10389 Set_Parent (D_Minal, Parent (Discrim));
10391 Set_Discriminal (Discrim, D_Minal);
10392 Set_Discriminal_Link (D_Minal, Discrim);
10394 -- For task types, build at once the discriminants of the corresponding
10395 -- record, which are needed if discriminants are used in entry defaults
10396 -- and in family bounds.
10398 if Is_Concurrent_Type (Current_Scope)
10399 or else
10400 Is_Limited_Type (Current_Scope)
10401 then
10402 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10404 Mutate_Ekind (CR_Disc, E_In_Parameter);
10405 Set_Mechanism (CR_Disc, Default_Mechanism);
10406 Set_Etype (CR_Disc, Etype (Discrim));
10407 Set_Scope (CR_Disc, Current_Scope);
10408 Set_Discriminal_Link (CR_Disc, Discrim);
10409 Set_CR_Discriminant (Discrim, CR_Disc);
10410 end if;
10411 end Build_Discriminal;
10413 ------------------------------------
10414 -- Build_Discriminant_Constraints --
10415 ------------------------------------
10417 function Build_Discriminant_Constraints
10418 (T : Entity_Id;
10419 Def : Node_Id;
10420 Derived_Def : Boolean := False) return Elist_Id
10422 C : constant Node_Id := Constraint (Def);
10423 Nb_Discr : constant Nat := Number_Discriminants (T);
10425 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10426 -- Saves the expression corresponding to a given discriminant in T
10428 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10429 -- Return the Position number within array Discr_Expr of a discriminant
10430 -- D within the discriminant list of the discriminated type T.
10432 procedure Process_Discriminant_Expression
10433 (Expr : Node_Id;
10434 D : Entity_Id);
10435 -- If this is a discriminant constraint on a partial view, do not
10436 -- generate an overflow check on the discriminant expression. The check
10437 -- will be generated when constraining the full view. Otherwise the
10438 -- backend creates duplicate symbols for the temporaries corresponding
10439 -- to the expressions to be checked, causing spurious assembler errors.
10441 ------------------
10442 -- Pos_Of_Discr --
10443 ------------------
10445 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10446 Disc : Entity_Id;
10448 begin
10449 Disc := First_Discriminant (T);
10450 for J in Discr_Expr'Range loop
10451 if Disc = D then
10452 return J;
10453 end if;
10455 Next_Discriminant (Disc);
10456 end loop;
10458 -- Note: Since this function is called on discriminants that are
10459 -- known to belong to the discriminated type, falling through the
10460 -- loop with no match signals an internal compiler error.
10462 raise Program_Error;
10463 end Pos_Of_Discr;
10465 -------------------------------------
10466 -- Process_Discriminant_Expression --
10467 -------------------------------------
10469 procedure Process_Discriminant_Expression
10470 (Expr : Node_Id;
10471 D : Entity_Id)
10473 BDT : constant Entity_Id := Base_Type (Etype (D));
10475 begin
10476 -- If this is a discriminant constraint on a partial view, do
10477 -- not generate an overflow on the discriminant expression. The
10478 -- check will be generated when constraining the full view.
10480 if Is_Private_Type (T)
10481 and then Present (Full_View (T))
10482 then
10483 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10484 else
10485 Analyze_And_Resolve (Expr, BDT);
10486 end if;
10487 end Process_Discriminant_Expression;
10489 -- Declarations local to Build_Discriminant_Constraints
10491 Discr : Entity_Id;
10492 E : Entity_Id;
10493 Elist : constant Elist_Id := New_Elmt_List;
10495 Constr : Node_Id;
10496 Expr : Node_Id;
10497 Id : Node_Id;
10498 Position : Nat;
10499 Found : Boolean;
10501 Discrim_Present : Boolean := False;
10503 -- Start of processing for Build_Discriminant_Constraints
10505 begin
10506 -- The following loop will process positional associations only.
10507 -- For a positional association, the (single) discriminant is
10508 -- implicitly specified by position, in textual order (RM 3.7.2).
10510 Discr := First_Discriminant (T);
10511 Constr := First (Constraints (C));
10512 for D in Discr_Expr'Range loop
10513 exit when Nkind (Constr) = N_Discriminant_Association;
10515 if No (Constr) then
10516 Error_Msg_N ("too few discriminants given in constraint", C);
10517 return New_Elmt_List;
10519 elsif Nkind (Constr) = N_Range
10520 or else (Nkind (Constr) = N_Attribute_Reference
10521 and then Attribute_Name (Constr) = Name_Range)
10522 then
10523 Error_Msg_N
10524 ("a range is not a valid discriminant constraint", Constr);
10525 Discr_Expr (D) := Error;
10527 elsif Nkind (Constr) = N_Subtype_Indication then
10528 Error_Msg_N
10529 ("a subtype indication is not a valid discriminant constraint",
10530 Constr);
10531 Discr_Expr (D) := Error;
10533 else
10534 Process_Discriminant_Expression (Constr, Discr);
10535 Discr_Expr (D) := Constr;
10536 end if;
10538 Next_Discriminant (Discr);
10539 Next (Constr);
10540 end loop;
10542 if No (Discr) and then Present (Constr) then
10543 Error_Msg_N ("too many discriminants given in constraint", Constr);
10544 return New_Elmt_List;
10545 end if;
10547 -- Named associations can be given in any order, but if both positional
10548 -- and named associations are used in the same discriminant constraint,
10549 -- then positional associations must occur first, at their normal
10550 -- position. Hence once a named association is used, the rest of the
10551 -- discriminant constraint must use only named associations.
10553 while Present (Constr) loop
10555 -- Positional association forbidden after a named association
10557 if Nkind (Constr) /= N_Discriminant_Association then
10558 Error_Msg_N ("positional association follows named one", Constr);
10559 return New_Elmt_List;
10561 -- Otherwise it is a named association
10563 else
10564 -- E records the type of the discriminants in the named
10565 -- association. All the discriminants specified in the same name
10566 -- association must have the same type.
10568 E := Empty;
10570 -- Search the list of discriminants in T to see if the simple name
10571 -- given in the constraint matches any of them.
10573 Id := First (Selector_Names (Constr));
10574 while Present (Id) loop
10575 Found := False;
10577 -- If Original_Discriminant is present, we are processing a
10578 -- generic instantiation and this is an instance node. We need
10579 -- to find the name of the corresponding discriminant in the
10580 -- actual record type T and not the name of the discriminant in
10581 -- the generic formal. Example:
10583 -- generic
10584 -- type G (D : int) is private;
10585 -- package P is
10586 -- subtype W is G (D => 1);
10587 -- end package;
10588 -- type Rec (X : int) is record ... end record;
10589 -- package Q is new P (G => Rec);
10591 -- At the point of the instantiation, formal type G is Rec
10592 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10593 -- which really looks like "subtype W is Rec (D => 1);" at
10594 -- the point of instantiation, we want to find the discriminant
10595 -- that corresponds to D in Rec, i.e. X.
10597 if Present (Original_Discriminant (Id))
10598 and then In_Instance
10599 then
10600 Discr := Find_Corresponding_Discriminant (Id, T);
10601 Found := True;
10603 else
10604 Discr := First_Discriminant (T);
10605 while Present (Discr) loop
10606 if Chars (Discr) = Chars (Id) then
10607 Found := True;
10608 exit;
10609 end if;
10611 Next_Discriminant (Discr);
10612 end loop;
10614 if not Found then
10615 Error_Msg_N ("& does not match any discriminant", Id);
10616 return New_Elmt_List;
10618 -- If the parent type is a generic formal, preserve the
10619 -- name of the discriminant for subsequent instances.
10620 -- see comment at the beginning of this if statement.
10622 elsif Is_Generic_Type (Root_Type (T)) then
10623 Set_Original_Discriminant (Id, Discr);
10624 end if;
10625 end if;
10627 Position := Pos_Of_Discr (T, Discr);
10629 if Present (Discr_Expr (Position)) then
10630 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10632 else
10633 -- Each discriminant specified in the same named association
10634 -- must be associated with a separate copy of the
10635 -- corresponding expression.
10637 if Present (Next (Id)) then
10638 Expr := New_Copy_Tree (Expression (Constr));
10639 Set_Parent (Expr, Parent (Expression (Constr)));
10640 else
10641 Expr := Expression (Constr);
10642 end if;
10644 Discr_Expr (Position) := Expr;
10645 Process_Discriminant_Expression (Expr, Discr);
10646 end if;
10648 -- A discriminant association with more than one discriminant
10649 -- name is only allowed if the named discriminants are all of
10650 -- the same type (RM 3.7.1(8)).
10652 if E = Empty then
10653 E := Base_Type (Etype (Discr));
10655 elsif Base_Type (Etype (Discr)) /= E then
10656 Error_Msg_N
10657 ("all discriminants in an association " &
10658 "must have the same type", Id);
10659 end if;
10661 Next (Id);
10662 end loop;
10663 end if;
10665 Next (Constr);
10666 end loop;
10668 -- A discriminant constraint must provide exactly one value for each
10669 -- discriminant of the type (RM 3.7.1(8)).
10671 for J in Discr_Expr'Range loop
10672 if No (Discr_Expr (J)) then
10673 Error_Msg_N ("too few discriminants given in constraint", C);
10674 return New_Elmt_List;
10675 end if;
10676 end loop;
10678 -- Determine if there are discriminant expressions in the constraint
10680 for J in Discr_Expr'Range loop
10681 if Denotes_Discriminant
10682 (Discr_Expr (J), Check_Concurrent => True)
10683 then
10684 Discrim_Present := True;
10685 exit;
10686 end if;
10687 end loop;
10689 -- Build an element list consisting of the expressions given in the
10690 -- discriminant constraint and apply the appropriate checks. The list
10691 -- is constructed after resolving any named discriminant associations
10692 -- and therefore the expressions appear in the textual order of the
10693 -- discriminants.
10695 Discr := First_Discriminant (T);
10696 for J in Discr_Expr'Range loop
10697 if Discr_Expr (J) /= Error then
10698 Append_Elmt (Discr_Expr (J), Elist);
10700 -- If any of the discriminant constraints is given by a
10701 -- discriminant and we are in a derived type declaration we
10702 -- have a discriminant renaming. Establish link between new
10703 -- and old discriminant. The new discriminant has an implicit
10704 -- dereference if the old one does.
10706 if Denotes_Discriminant (Discr_Expr (J)) then
10707 if Derived_Def then
10708 declare
10709 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10711 begin
10712 Set_Corresponding_Discriminant (New_Discr, Discr);
10713 Set_Has_Implicit_Dereference (New_Discr,
10714 Has_Implicit_Dereference (Discr));
10715 end;
10716 end if;
10718 -- Force the evaluation of non-discriminant expressions.
10719 -- If we have found a discriminant in the constraint 3.4(26)
10720 -- and 3.8(18) demand that no range checks are performed are
10721 -- after evaluation. If the constraint is for a component
10722 -- definition that has a per-object constraint, expressions are
10723 -- evaluated but not checked either. In all other cases perform
10724 -- a range check.
10726 else
10727 if Discrim_Present then
10728 null;
10730 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10731 and then Has_Per_Object_Constraint
10732 (Defining_Identifier (Parent (Parent (Def))))
10733 then
10734 null;
10736 elsif Is_Access_Type (Etype (Discr)) then
10737 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10739 else
10740 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10741 end if;
10743 -- If the value of the discriminant may be visible in
10744 -- another unit or child unit, create an external name
10745 -- for it. We use the name of the object or component
10746 -- that carries the discriminated subtype. The code
10747 -- below may generate external symbols for the discriminant
10748 -- expression when not strictly needed, which is harmless.
10750 if Expander_Active
10751 and then Comes_From_Source (Def)
10752 and then not Is_Subprogram (Current_Scope)
10753 then
10754 declare
10755 Id : Entity_Id := Empty;
10756 begin
10757 if Nkind (Parent (Def)) = N_Object_Declaration then
10758 Id := Defining_Identifier (Parent (Def));
10760 elsif Nkind (Parent (Def)) = N_Component_Definition
10761 and then
10762 Nkind (Parent (Parent (Def)))
10763 = N_Component_Declaration
10764 then
10765 Id := Defining_Identifier (Parent (Parent (Def)));
10766 end if;
10768 if Present (Id) then
10769 Force_Evaluation (
10770 Discr_Expr (J),
10771 Related_Id => Id,
10772 Discr_Number => J);
10773 else
10774 Force_Evaluation (Discr_Expr (J));
10775 end if;
10776 end;
10777 else
10778 Force_Evaluation (Discr_Expr (J));
10779 end if;
10780 end if;
10782 -- Check that the designated type of an access discriminant's
10783 -- expression is not a class-wide type unless the discriminant's
10784 -- designated type is also class-wide.
10786 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10787 and then not Is_Class_Wide_Type
10788 (Designated_Type (Etype (Discr)))
10789 and then Etype (Discr_Expr (J)) /= Any_Type
10790 and then Is_Class_Wide_Type
10791 (Designated_Type (Etype (Discr_Expr (J))))
10792 then
10793 Wrong_Type (Discr_Expr (J), Etype (Discr));
10795 elsif Is_Access_Type (Etype (Discr))
10796 and then not Is_Access_Constant (Etype (Discr))
10797 and then Is_Access_Type (Etype (Discr_Expr (J)))
10798 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10799 then
10800 Error_Msg_NE
10801 ("constraint for discriminant& must be access to variable",
10802 Def, Discr);
10803 end if;
10804 end if;
10806 Next_Discriminant (Discr);
10807 end loop;
10809 return Elist;
10810 end Build_Discriminant_Constraints;
10812 ---------------------------------
10813 -- Build_Discriminated_Subtype --
10814 ---------------------------------
10816 procedure Build_Discriminated_Subtype
10817 (T : Entity_Id;
10818 Def_Id : Entity_Id;
10819 Elist : Elist_Id;
10820 Related_Nod : Node_Id;
10821 For_Access : Boolean := False)
10823 Has_Discrs : constant Boolean := Has_Discriminants (T);
10824 Constrained : constant Boolean :=
10825 (Has_Discrs
10826 and then not Is_Empty_Elmt_List (Elist)
10827 and then not Is_Class_Wide_Type (T))
10828 or else Is_Constrained (T);
10830 begin
10831 if Ekind (T) = E_Record_Type then
10832 Mutate_Ekind (Def_Id, E_Record_Subtype);
10834 -- Inherit preelaboration flag from base, for types for which it
10835 -- may have been set: records, private types, protected types.
10837 Set_Known_To_Have_Preelab_Init
10838 (Def_Id, Known_To_Have_Preelab_Init (T));
10840 elsif Ekind (T) = E_Task_Type then
10841 Mutate_Ekind (Def_Id, E_Task_Subtype);
10843 elsif Ekind (T) = E_Protected_Type then
10844 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10845 Set_Known_To_Have_Preelab_Init
10846 (Def_Id, Known_To_Have_Preelab_Init (T));
10848 elsif Is_Private_Type (T) then
10849 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10850 Set_Known_To_Have_Preelab_Init
10851 (Def_Id, Known_To_Have_Preelab_Init (T));
10853 -- Private subtypes may have private dependents
10855 Set_Private_Dependents (Def_Id, New_Elmt_List);
10857 elsif Is_Class_Wide_Type (T) then
10858 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10860 else
10861 -- Incomplete type. Attach subtype to list of dependents, to be
10862 -- completed with full view of parent type, unless is it the
10863 -- designated subtype of a record component within an init_proc.
10864 -- This last case arises for a component of an access type whose
10865 -- designated type is incomplete (e.g. a Taft Amendment type).
10866 -- The designated subtype is within an inner scope, and needs no
10867 -- elaboration, because only the access type is needed in the
10868 -- initialization procedure.
10870 if Ekind (T) = E_Incomplete_Type then
10871 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10872 else
10873 Mutate_Ekind (Def_Id, Ekind (T));
10874 end if;
10876 if For_Access and then Within_Init_Proc then
10877 null;
10878 else
10879 Append_Elmt (Def_Id, Private_Dependents (T));
10880 end if;
10881 end if;
10883 Set_Etype (Def_Id, T);
10884 Reinit_Size_Align (Def_Id);
10885 Set_Has_Discriminants (Def_Id, Has_Discrs);
10886 Set_Is_Constrained (Def_Id, Constrained);
10888 Set_First_Entity (Def_Id, First_Entity (T));
10889 Set_Last_Entity (Def_Id, Last_Entity (T));
10890 Set_Has_Implicit_Dereference
10891 (Def_Id, Has_Implicit_Dereference (T));
10892 Set_Has_Pragma_Unreferenced_Objects
10893 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10895 -- If the subtype is the completion of a private declaration, there may
10896 -- have been representation clauses for the partial view, and they must
10897 -- be preserved. Build_Derived_Type chains the inherited clauses with
10898 -- the ones appearing on the extension. If this comes from a subtype
10899 -- declaration, all clauses are inherited.
10901 if No (First_Rep_Item (Def_Id)) then
10902 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10903 end if;
10905 if Is_Tagged_Type (T) then
10906 Set_Is_Tagged_Type (Def_Id);
10907 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10908 Make_Class_Wide_Type (Def_Id);
10909 end if;
10911 Set_Stored_Constraint (Def_Id, No_Elist);
10913 if Has_Discrs then
10914 Set_Discriminant_Constraint (Def_Id, Elist);
10915 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10916 end if;
10918 if Is_Tagged_Type (T) then
10920 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10921 -- concurrent record type (which has the list of primitive
10922 -- operations).
10924 if Ada_Version >= Ada_2005
10925 and then Is_Concurrent_Type (T)
10926 then
10927 Set_Corresponding_Record_Type (Def_Id,
10928 Corresponding_Record_Type (T));
10929 else
10930 Set_Direct_Primitive_Operations (Def_Id,
10931 Direct_Primitive_Operations (T));
10932 end if;
10934 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10935 end if;
10937 -- Subtypes introduced by component declarations do not need to be
10938 -- marked as delayed, and do not get freeze nodes, because the semantics
10939 -- verifies that the parents of the subtypes are frozen before the
10940 -- enclosing record is frozen.
10942 if not Is_Type (Scope (Def_Id)) then
10943 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10945 if Is_Private_Type (T)
10946 and then Present (Full_View (T))
10947 then
10948 Conditional_Delay (Def_Id, Full_View (T));
10949 else
10950 Conditional_Delay (Def_Id, T);
10951 end if;
10952 end if;
10954 if Is_Record_Type (T) then
10955 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10957 if Has_Discrs
10958 and then not Is_Empty_Elmt_List (Elist)
10959 and then not For_Access
10960 then
10961 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10963 elsif not Is_Private_Type (T) then
10964 Set_Cloned_Subtype (Def_Id, T);
10965 end if;
10966 end if;
10967 end Build_Discriminated_Subtype;
10969 ---------------------------
10970 -- Build_Itype_Reference --
10971 ---------------------------
10973 procedure Build_Itype_Reference
10974 (Ityp : Entity_Id;
10975 Nod : Node_Id)
10977 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10978 begin
10980 -- Itype references are only created for use by the back-end
10982 if Inside_A_Generic then
10983 return;
10984 else
10985 Set_Itype (IR, Ityp);
10987 -- If Nod is a library unit entity, then Insert_After won't work,
10988 -- because Nod is not a member of any list. Therefore, we use
10989 -- Add_Global_Declaration in this case. This can happen if we have a
10990 -- build-in-place library function, child unit or not.
10992 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10993 or else (Nkind (Nod) in
10994 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
10995 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10996 then
10997 Add_Global_Declaration (IR);
10998 else
10999 Insert_After (Nod, IR);
11000 end if;
11001 end if;
11002 end Build_Itype_Reference;
11004 ------------------------
11005 -- Build_Scalar_Bound --
11006 ------------------------
11008 function Build_Scalar_Bound
11009 (Bound : Node_Id;
11010 Par_T : Entity_Id;
11011 Der_T : Entity_Id) return Node_Id
11013 New_Bound : Entity_Id;
11015 begin
11016 -- Note: not clear why this is needed, how can the original bound
11017 -- be unanalyzed at this point? and if it is, what business do we
11018 -- have messing around with it? and why is the base type of the
11019 -- parent type the right type for the resolution. It probably is
11020 -- not. It is OK for the new bound we are creating, but not for
11021 -- the old one??? Still if it never happens, no problem.
11023 Analyze_And_Resolve (Bound, Base_Type (Par_T));
11025 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
11026 New_Bound := New_Copy (Bound);
11027 Set_Etype (New_Bound, Der_T);
11028 Set_Analyzed (New_Bound);
11030 elsif Is_Entity_Name (Bound) then
11031 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
11033 -- The following is almost certainly wrong. What business do we have
11034 -- relocating a node (Bound) that is presumably still attached to
11035 -- the tree elsewhere???
11037 else
11038 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
11039 end if;
11041 Set_Etype (New_Bound, Der_T);
11042 return New_Bound;
11043 end Build_Scalar_Bound;
11045 -------------------------------
11046 -- Check_Abstract_Overriding --
11047 -------------------------------
11049 procedure Check_Abstract_Overriding (T : Entity_Id) is
11050 Alias_Subp : Entity_Id;
11051 Elmt : Elmt_Id;
11052 Op_List : Elist_Id;
11053 Subp : Entity_Id;
11054 Type_Def : Node_Id;
11056 procedure Check_Pragma_Implemented (Subp : Entity_Id);
11057 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
11058 -- which has pragma Implemented already set. Check whether Subp's entity
11059 -- kind conforms to the implementation kind of the overridden routine.
11061 procedure Check_Pragma_Implemented
11062 (Subp : Entity_Id;
11063 Iface_Subp : Entity_Id);
11064 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
11065 -- Iface_Subp and both entities have pragma Implemented already set on
11066 -- them. Check whether the two implementation kinds are conforming.
11068 procedure Inherit_Pragma_Implemented
11069 (Subp : Entity_Id;
11070 Iface_Subp : Entity_Id);
11071 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
11072 -- subprogram Iface_Subp which has been marked by pragma Implemented.
11073 -- Propagate the implementation kind of Iface_Subp to Subp.
11075 ------------------------------
11076 -- Check_Pragma_Implemented --
11077 ------------------------------
11079 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
11080 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
11081 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
11082 Subp_Alias : constant Entity_Id := Alias (Subp);
11083 Contr_Typ : Entity_Id;
11084 Impl_Subp : Entity_Id;
11086 begin
11087 -- Subp must have an alias since it is a hidden entity used to link
11088 -- an interface subprogram to its overriding counterpart.
11090 pragma Assert (Present (Subp_Alias));
11092 -- Handle aliases to synchronized wrappers
11094 Impl_Subp := Subp_Alias;
11096 if Is_Primitive_Wrapper (Impl_Subp) then
11097 Impl_Subp := Wrapped_Entity (Impl_Subp);
11098 end if;
11100 -- Extract the type of the controlling formal
11102 Contr_Typ := Etype (First_Formal (Subp_Alias));
11104 if Is_Concurrent_Record_Type (Contr_Typ) then
11105 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
11106 end if;
11108 -- An interface subprogram whose implementation kind is By_Entry must
11109 -- be implemented by an entry.
11111 if Impl_Kind = Name_By_Entry
11112 and then Ekind (Impl_Subp) /= E_Entry
11113 then
11114 Error_Msg_Node_2 := Iface_Alias;
11115 Error_Msg_NE
11116 ("type & must implement abstract subprogram & with an entry",
11117 Subp_Alias, Contr_Typ);
11119 elsif Impl_Kind = Name_By_Protected_Procedure then
11121 -- An interface subprogram whose implementation kind is By_
11122 -- Protected_Procedure cannot be implemented by a primitive
11123 -- procedure of a task type.
11125 if Ekind (Contr_Typ) /= E_Protected_Type then
11126 Error_Msg_Node_2 := Contr_Typ;
11127 Error_Msg_NE
11128 ("interface subprogram & cannot be implemented by a "
11129 & "primitive procedure of task type &",
11130 Subp_Alias, Iface_Alias);
11132 -- An interface subprogram whose implementation kind is By_
11133 -- Protected_Procedure must be implemented by a procedure.
11135 elsif Ekind (Impl_Subp) /= E_Procedure then
11136 Error_Msg_Node_2 := Iface_Alias;
11137 Error_Msg_NE
11138 ("type & must implement abstract subprogram & with a "
11139 & "procedure", Subp_Alias, Contr_Typ);
11141 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11142 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11143 then
11144 Error_Msg_Name_1 := Impl_Kind;
11145 Error_Msg_N
11146 ("overriding operation& must have synchronization%",
11147 Subp_Alias);
11148 end if;
11150 -- If primitive has Optional synchronization, overriding operation
11151 -- must match if it has an explicit synchronization.
11153 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11154 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11155 then
11156 Error_Msg_Name_1 := Impl_Kind;
11157 Error_Msg_N
11158 ("overriding operation& must have synchronization%", Subp_Alias);
11159 end if;
11160 end Check_Pragma_Implemented;
11162 ------------------------------
11163 -- Check_Pragma_Implemented --
11164 ------------------------------
11166 procedure Check_Pragma_Implemented
11167 (Subp : Entity_Id;
11168 Iface_Subp : Entity_Id)
11170 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11171 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11173 begin
11174 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11175 -- and overriding subprogram are different. In general this is an
11176 -- error except when the implementation kind of the overridden
11177 -- subprograms is By_Any or Optional.
11179 if Iface_Kind /= Subp_Kind
11180 and then Iface_Kind /= Name_By_Any
11181 and then Iface_Kind /= Name_Optional
11182 then
11183 if Iface_Kind = Name_By_Entry then
11184 Error_Msg_N
11185 ("incompatible implementation kind, overridden subprogram " &
11186 "is marked By_Entry", Subp);
11187 else
11188 Error_Msg_N
11189 ("incompatible implementation kind, overridden subprogram " &
11190 "is marked By_Protected_Procedure", Subp);
11191 end if;
11192 end if;
11193 end Check_Pragma_Implemented;
11195 --------------------------------
11196 -- Inherit_Pragma_Implemented --
11197 --------------------------------
11199 procedure Inherit_Pragma_Implemented
11200 (Subp : Entity_Id;
11201 Iface_Subp : Entity_Id)
11203 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11204 Loc : constant Source_Ptr := Sloc (Subp);
11205 Impl_Prag : Node_Id;
11207 begin
11208 -- Since the implementation kind is stored as a representation item
11209 -- rather than a flag, create a pragma node.
11211 Impl_Prag :=
11212 Make_Pragma (Loc,
11213 Chars => Name_Implemented,
11214 Pragma_Argument_Associations => New_List (
11215 Make_Pragma_Argument_Association (Loc,
11216 Expression => New_Occurrence_Of (Subp, Loc)),
11218 Make_Pragma_Argument_Association (Loc,
11219 Expression => Make_Identifier (Loc, Iface_Kind))));
11221 -- The pragma doesn't need to be analyzed because it is internally
11222 -- built. It is safe to directly register it as a rep item since we
11223 -- are only interested in the characters of the implementation kind.
11225 Record_Rep_Item (Subp, Impl_Prag);
11226 end Inherit_Pragma_Implemented;
11228 -- Start of processing for Check_Abstract_Overriding
11230 begin
11231 Op_List := Primitive_Operations (T);
11233 -- Loop to check primitive operations
11235 Elmt := First_Elmt (Op_List);
11236 while Present (Elmt) loop
11237 Subp := Node (Elmt);
11238 Alias_Subp := Alias (Subp);
11240 -- If the parent type is untagged, then no overriding error checks
11241 -- are needed (such as in the case of an implicit full type for
11242 -- a derived type whose parent is an untagged private type with
11243 -- a tagged full type).
11245 if not Is_Tagged_Type (Etype (T)) then
11246 null;
11248 -- Inherited subprograms are identified by the fact that they do not
11249 -- come from source, and the associated source location is the
11250 -- location of the first subtype of the derived type.
11252 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11253 -- subprograms that "require overriding".
11255 -- Special exception, do not complain about failure to override the
11256 -- stream routines _Input and _Output, as well as the primitive
11257 -- operations used in dispatching selects since we always provide
11258 -- automatic overridings for these subprograms.
11260 -- The partial view of T may have been a private extension, for
11261 -- which inherited functions dispatching on result are abstract.
11262 -- If the full view is a null extension, there is no need for
11263 -- overriding in Ada 2005, but wrappers need to be built for them
11264 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11266 elsif Is_Null_Extension (T)
11267 and then Has_Controlling_Result (Subp)
11268 and then Ada_Version >= Ada_2005
11269 and then Present (Alias_Subp)
11270 and then not Comes_From_Source (Subp)
11271 and then not Is_Abstract_Subprogram (Alias_Subp)
11272 and then not Is_Access_Type (Etype (Subp))
11273 then
11274 null;
11276 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11277 -- processing because this check is done with the aliased
11278 -- entity
11280 elsif Present (Interface_Alias (Subp)) then
11281 null;
11283 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11284 -- of a visible private primitive inherited from an ancestor with
11285 -- the aspect Type_Invariant'Class, unless the inherited primitive
11286 -- is abstract.
11288 elsif not Is_Abstract_Subprogram (Subp)
11289 and then not Comes_From_Source (Subp) -- An inherited subprogram
11290 and then Requires_Overriding (Subp)
11291 and then Present (Alias_Subp)
11292 and then Has_Invariants (Etype (T))
11293 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11294 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11295 and then Is_Private_Primitive (Alias_Subp)
11296 then
11297 Error_Msg_NE
11298 ("inherited private primitive & must be overridden", T, Subp);
11299 Error_Msg_N
11300 ("\because ancestor type has 'Type_'Invariant''Class " &
11301 "(RM 7.3.2(6.1))", T);
11303 elsif (Is_Abstract_Subprogram (Subp)
11304 or else Requires_Overriding (Subp)
11305 or else
11306 (Has_Controlling_Result (Subp)
11307 and then Present (Alias_Subp)
11308 and then not Comes_From_Source (Subp)
11309 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11310 and then not Is_TSS (Subp, TSS_Stream_Input)
11311 and then not Is_TSS (Subp, TSS_Stream_Output)
11312 and then not Is_Abstract_Type (T)
11313 and then not Is_Predefined_Interface_Primitive (Subp)
11315 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11316 -- with abstract interface types because the check will be done
11317 -- with the aliased entity (otherwise we generate a duplicated
11318 -- error message).
11320 and then No (Interface_Alias (Subp))
11321 then
11322 if Present (Alias_Subp) then
11324 -- Only perform the check for a derived subprogram when the
11325 -- type has an explicit record extension. This avoids incorrect
11326 -- flagging of abstract subprograms for the case of a type
11327 -- without an extension that is derived from a formal type
11328 -- with a tagged actual (can occur within a private part).
11330 -- Ada 2005 (AI-391): In the case of an inherited function with
11331 -- a controlling result of the type, the rule does not apply if
11332 -- the type is a null extension (unless the parent function
11333 -- itself is abstract, in which case the function must still be
11334 -- be overridden). The expander will generate an overriding
11335 -- wrapper function calling the parent subprogram (see
11336 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11338 Type_Def := Type_Definition (Parent (T));
11340 if Nkind (Type_Def) = N_Derived_Type_Definition
11341 and then Present (Record_Extension_Part (Type_Def))
11342 and then
11343 (Ada_Version < Ada_2005
11344 or else not Is_Null_Extension (T)
11345 or else Ekind (Subp) = E_Procedure
11346 or else not Has_Controlling_Result (Subp)
11347 or else Is_Abstract_Subprogram (Alias_Subp)
11348 or else Requires_Overriding (Subp)
11349 or else Is_Access_Type (Etype (Subp)))
11350 then
11351 -- Avoid reporting error in case of abstract predefined
11352 -- primitive inherited from interface type because the
11353 -- body of internally generated predefined primitives
11354 -- of tagged types are generated later by Freeze_Type
11356 if Is_Interface (Root_Type (T))
11357 and then Is_Abstract_Subprogram (Subp)
11358 and then Is_Predefined_Dispatching_Operation (Subp)
11359 and then not Comes_From_Source (Ultimate_Alias (Subp))
11360 then
11361 null;
11363 -- A null extension is not obliged to override an inherited
11364 -- procedure subject to pragma Extensions_Visible with value
11365 -- False and at least one controlling OUT parameter
11366 -- (SPARK RM 6.1.7(6)).
11368 elsif Is_Null_Extension (T)
11369 and then Is_EVF_Procedure (Subp)
11370 then
11371 null;
11373 -- Subprogram renamings cannot be overridden
11375 elsif Comes_From_Source (Subp)
11376 and then Present (Alias (Subp))
11377 then
11378 null;
11380 -- Skip reporting the error on Ada 2022 only subprograms
11381 -- that require overriding if we are not in Ada 2022 mode.
11383 elsif Ada_Version < Ada_2022
11384 and then Requires_Overriding (Subp)
11385 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11386 then
11387 null;
11389 else
11390 Error_Msg_NE
11391 ("type must be declared abstract or & overridden",
11392 T, Subp);
11394 -- Traverse the whole chain of aliased subprograms to
11395 -- complete the error notification. This is especially
11396 -- useful for traceability of the chain of entities when
11397 -- the subprogram corresponds with an interface
11398 -- subprogram (which may be defined in another package).
11400 if Present (Alias_Subp) then
11401 declare
11402 E : Entity_Id;
11404 begin
11405 E := Subp;
11406 while Present (Alias (E)) loop
11408 -- Avoid reporting redundant errors on entities
11409 -- inherited from interfaces
11411 if Sloc (E) /= Sloc (T) then
11412 Error_Msg_Sloc := Sloc (E);
11413 Error_Msg_NE
11414 ("\& has been inherited #", T, Subp);
11415 end if;
11417 E := Alias (E);
11418 end loop;
11420 Error_Msg_Sloc := Sloc (E);
11422 -- AI05-0068: report if there is an overriding
11423 -- non-abstract subprogram that is invisible.
11425 if Is_Hidden (E)
11426 and then not Is_Abstract_Subprogram (E)
11427 then
11428 Error_Msg_NE
11429 ("\& subprogram# is not visible",
11430 T, Subp);
11432 -- Clarify the case where a non-null extension must
11433 -- override inherited procedure subject to pragma
11434 -- Extensions_Visible with value False and at least
11435 -- one controlling OUT param.
11437 elsif Is_EVF_Procedure (E) then
11438 Error_Msg_NE
11439 ("\& # is subject to Extensions_Visible False",
11440 T, Subp);
11442 else
11443 Error_Msg_NE
11444 ("\& has been inherited from subprogram #",
11445 T, Subp);
11446 end if;
11447 end;
11448 end if;
11449 end if;
11451 -- Ada 2005 (AI-345): Protected or task type implementing
11452 -- abstract interfaces.
11454 elsif Is_Concurrent_Record_Type (T)
11455 and then Present (Interfaces (T))
11456 then
11457 -- There is no need to check here RM 9.4(11.9/3) since we
11458 -- are processing the corresponding record type and the
11459 -- mode of the overriding subprograms was verified by
11460 -- Check_Conformance when the corresponding concurrent
11461 -- type declaration was analyzed.
11463 Error_Msg_NE
11464 ("interface subprogram & must be overridden", T, Subp);
11466 -- Examine primitive operations of synchronized type to find
11467 -- homonyms that have the wrong profile.
11469 declare
11470 Prim : Entity_Id;
11472 begin
11473 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11474 while Present (Prim) loop
11475 if Chars (Prim) = Chars (Subp) then
11476 Error_Msg_NE
11477 ("profile is not type conformant with prefixed "
11478 & "view profile of inherited operation&",
11479 Prim, Subp);
11480 end if;
11482 Next_Entity (Prim);
11483 end loop;
11484 end;
11485 end if;
11487 else
11488 Error_Msg_Node_2 := T;
11489 Error_Msg_N
11490 ("abstract subprogram& not allowed for type&", Subp);
11492 -- Also post unconditional warning on the type (unconditional
11493 -- so that if there are more than one of these cases, we get
11494 -- them all, and not just the first one).
11496 Error_Msg_Node_2 := Subp;
11497 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11498 end if;
11500 -- A subprogram subject to pragma Extensions_Visible with value
11501 -- "True" cannot override a subprogram subject to the same pragma
11502 -- with value "False" (SPARK RM 6.1.7(5)).
11504 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11505 and then Present (Overridden_Operation (Subp))
11506 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11507 Extensions_Visible_False
11508 then
11509 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11510 Error_Msg_N
11511 ("subprogram & with Extensions_Visible True cannot override "
11512 & "subprogram # with Extensions_Visible False", Subp);
11513 end if;
11515 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11517 -- Subp is an expander-generated procedure which maps an interface
11518 -- alias to a protected wrapper. The interface alias is flagged by
11519 -- pragma Implemented. Ensure that Subp is a procedure when the
11520 -- implementation kind is By_Protected_Procedure or an entry when
11521 -- By_Entry.
11523 if Ada_Version >= Ada_2012
11524 and then Is_Hidden (Subp)
11525 and then Present (Interface_Alias (Subp))
11526 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11527 then
11528 Check_Pragma_Implemented (Subp);
11529 end if;
11531 -- Subp is an interface primitive which overrides another interface
11532 -- primitive marked with pragma Implemented.
11534 if Ada_Version >= Ada_2012
11535 and then Present (Overridden_Operation (Subp))
11536 and then Has_Rep_Pragma
11537 (Overridden_Operation (Subp), Name_Implemented)
11538 then
11539 -- If the overriding routine is also marked by Implemented, check
11540 -- that the two implementation kinds are conforming.
11542 if Has_Rep_Pragma (Subp, Name_Implemented) then
11543 Check_Pragma_Implemented
11544 (Subp => Subp,
11545 Iface_Subp => Overridden_Operation (Subp));
11547 -- Otherwise the overriding routine inherits the implementation
11548 -- kind from the overridden subprogram.
11550 else
11551 Inherit_Pragma_Implemented
11552 (Subp => Subp,
11553 Iface_Subp => Overridden_Operation (Subp));
11554 end if;
11555 end if;
11557 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11558 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11559 -- for procedures, since this is our pragma.
11561 if Present (Overridden_Operation (Subp))
11562 and then No_Return (Overridden_Operation (Subp))
11563 then
11565 -- If the subprogram is a renaming, check that the renamed
11566 -- subprogram is No_Return.
11568 if Present (Renamed_Or_Alias (Subp)) then
11569 if not No_Return (Renamed_Or_Alias (Subp)) then
11570 Error_Msg_NE ("subprogram & must be No_Return",
11571 Subp,
11572 Renamed_Or_Alias (Subp));
11573 Error_Msg_N ("\since renaming & overrides No_Return "
11574 & "subprogram (RM 6.5.1(6/2))",
11575 Subp);
11576 end if;
11578 -- Make sure that the subprogram itself is No_Return.
11580 elsif not No_Return (Subp) then
11581 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11582 Error_Msg_N
11583 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11584 Subp);
11585 end if;
11586 end if;
11588 -- If the operation is a wrapper for a synchronized primitive, it
11589 -- may be called indirectly through a dispatching select. We assume
11590 -- that it will be referenced elsewhere indirectly, and suppress
11591 -- warnings about an unused entity.
11593 if Is_Primitive_Wrapper (Subp)
11594 and then Present (Wrapped_Entity (Subp))
11595 then
11596 Set_Referenced (Wrapped_Entity (Subp));
11597 end if;
11599 Next_Elmt (Elmt);
11600 end loop;
11601 end Check_Abstract_Overriding;
11603 ------------------------------------------------
11604 -- Check_Access_Discriminant_Requires_Limited --
11605 ------------------------------------------------
11607 procedure Check_Access_Discriminant_Requires_Limited
11608 (D : Node_Id;
11609 Loc : Node_Id)
11611 begin
11612 -- A discriminant_specification for an access discriminant shall appear
11613 -- only in the declaration for a task or protected type, or for a type
11614 -- with the reserved word 'limited' in its definition or in one of its
11615 -- ancestors (RM 3.7(10)).
11617 -- AI-0063: The proper condition is that type must be immutably limited,
11618 -- or else be a partial view.
11620 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11621 if Is_Inherently_Limited_Type (Current_Scope)
11622 or else
11623 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11624 and then Limited_Present (Parent (Current_Scope)))
11625 then
11626 null;
11628 else
11629 Error_Msg_N
11630 ("access discriminants allowed only for limited types", Loc);
11631 end if;
11632 end if;
11633 end Check_Access_Discriminant_Requires_Limited;
11635 -----------------------------------
11636 -- Check_Aliased_Component_Types --
11637 -----------------------------------
11639 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11640 C : Entity_Id;
11642 begin
11643 -- ??? Also need to check components of record extensions, but not
11644 -- components of protected types (which are always limited).
11646 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11647 -- types to be unconstrained. This is safe because it is illegal to
11648 -- create access subtypes to such types with explicit discriminant
11649 -- constraints.
11651 if not Is_Limited_Type (T) then
11652 if Ekind (T) = E_Record_Type then
11653 C := First_Component (T);
11654 while Present (C) loop
11655 if Is_Aliased (C)
11656 and then Has_Discriminants (Etype (C))
11657 and then not Is_Constrained (Etype (C))
11658 and then not In_Instance_Body
11659 and then Ada_Version < Ada_2005
11660 then
11661 Error_Msg_N
11662 ("aliased component must be constrained (RM 3.6(11))",
11664 end if;
11666 Next_Component (C);
11667 end loop;
11669 elsif Ekind (T) = E_Array_Type then
11670 if Has_Aliased_Components (T)
11671 and then Has_Discriminants (Component_Type (T))
11672 and then not Is_Constrained (Component_Type (T))
11673 and then not In_Instance_Body
11674 and then Ada_Version < Ada_2005
11675 then
11676 Error_Msg_N
11677 ("aliased component type must be constrained (RM 3.6(11))",
11679 end if;
11680 end if;
11681 end if;
11682 end Check_Aliased_Component_Types;
11684 --------------------------------------
11685 -- Check_Anonymous_Access_Component --
11686 --------------------------------------
11688 procedure Check_Anonymous_Access_Component
11689 (Typ_Decl : Node_Id;
11690 Typ : Entity_Id;
11691 Prev : Entity_Id;
11692 Comp_Def : Node_Id;
11693 Access_Def : Node_Id)
11695 Loc : constant Source_Ptr := Sloc (Comp_Def);
11696 Anon_Access : Entity_Id;
11697 Acc_Def : Node_Id;
11698 Decl : Node_Id;
11699 Type_Def : Node_Id;
11701 procedure Build_Incomplete_Type_Declaration;
11702 -- If the record type contains components that include an access to the
11703 -- current record, then create an incomplete type declaration for the
11704 -- record, to be used as the designated type of the anonymous access.
11705 -- This is done only once, and only if there is no previous partial
11706 -- view of the type.
11708 function Designates_T (Subt : Node_Id) return Boolean;
11709 -- Check whether a node designates the enclosing record type, or 'Class
11710 -- of that type
11712 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11713 -- Check whether an access definition includes a reference to
11714 -- the enclosing record type. The reference can be a subtype mark
11715 -- in the access definition itself, a 'Class attribute reference, or
11716 -- recursively a reference appearing in a parameter specification
11717 -- or result definition of an access_to_subprogram definition.
11719 --------------------------------------
11720 -- Build_Incomplete_Type_Declaration --
11721 --------------------------------------
11723 procedure Build_Incomplete_Type_Declaration is
11724 Decl : Node_Id;
11725 Inc_T : Entity_Id;
11726 H : Entity_Id;
11728 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11729 -- it's "is new ... with record" or else "is tagged record ...".
11731 Typ_Def : constant Node_Id :=
11732 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11733 then Type_Definition (Typ_Decl) else Empty);
11734 Is_Tagged : constant Boolean :=
11735 Present (Typ_Def)
11736 and then
11737 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11738 and then
11739 Present (Record_Extension_Part (Typ_Def)))
11740 or else
11741 (Nkind (Typ_Def) = N_Record_Definition
11742 and then Tagged_Present (Typ_Def)));
11744 begin
11745 -- If there is a previous partial view, no need to create a new one
11746 -- If the partial view, given by Prev, is incomplete, If Prev is
11747 -- a private declaration, full declaration is flagged accordingly.
11749 if Prev /= Typ then
11750 if Is_Tagged then
11751 Make_Class_Wide_Type (Prev);
11752 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11753 Set_Etype (Class_Wide_Type (Typ), Typ);
11754 end if;
11756 return;
11758 elsif Has_Private_Declaration (Typ) then
11760 -- If we refer to T'Class inside T, and T is the completion of a
11761 -- private type, then make sure the class-wide type exists.
11763 if Is_Tagged then
11764 Make_Class_Wide_Type (Typ);
11765 end if;
11767 return;
11769 -- If there was a previous anonymous access type, the incomplete
11770 -- type declaration will have been created already.
11772 elsif Present (Current_Entity (Typ))
11773 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11774 and then Full_View (Current_Entity (Typ)) = Typ
11775 then
11776 if Is_Tagged
11777 and then Comes_From_Source (Current_Entity (Typ))
11778 and then not Is_Tagged_Type (Current_Entity (Typ))
11779 then
11780 Make_Class_Wide_Type (Typ);
11781 Error_Msg_N
11782 ("incomplete view of tagged type should be declared tagged??",
11783 Parent (Current_Entity (Typ)));
11784 end if;
11785 return;
11787 else
11788 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11789 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11791 -- Type has already been inserted into the current scope. Remove
11792 -- it, and add incomplete declaration for type, so that subsequent
11793 -- anonymous access types can use it. The entity is unchained from
11794 -- the homonym list and from immediate visibility. After analysis,
11795 -- the entity in the incomplete declaration becomes immediately
11796 -- visible in the record declaration that follows.
11798 H := Current_Entity (Typ);
11800 if H = Typ then
11801 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11803 else
11804 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11805 H := Homonym (Typ);
11806 end loop;
11808 Set_Homonym (H, Homonym (Typ));
11809 end if;
11811 Insert_Before (Typ_Decl, Decl);
11812 Analyze (Decl);
11813 Set_Full_View (Inc_T, Typ);
11814 Set_Incomplete_View (Typ_Decl, Inc_T);
11816 -- If the type is tagged, create a common class-wide type for
11817 -- both views, and set the Etype of the class-wide type to the
11818 -- full view.
11820 if Is_Tagged then
11821 Make_Class_Wide_Type (Inc_T);
11822 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11823 Set_Etype (Class_Wide_Type (Typ), Typ);
11824 end if;
11826 -- If the scope is a package with a limited view, create a shadow
11827 -- entity for the incomplete type like Build_Limited_Views, so as
11828 -- to make it possible for Remove_Limited_With_Unit to reinstall
11829 -- this incomplete type as the visible entity.
11831 if Ekind (Scope (Inc_T)) = E_Package
11832 and then Present (Limited_View (Scope (Inc_T)))
11833 then
11834 declare
11835 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11837 begin
11838 -- This is modeled on Build_Shadow_Entity
11840 Set_Chars (Shadow, Chars (Inc_T));
11841 Set_Parent (Shadow, Decl);
11842 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
11843 Set_Is_Internal (Shadow);
11844 Set_From_Limited_With (Shadow);
11845 Set_Non_Limited_View (Shadow, Inc_T);
11846 Set_Private_Dependents (Shadow, New_Elmt_List);
11848 if Is_Tagged then
11849 Set_Non_Limited_View
11850 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
11851 end if;
11853 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
11854 end;
11855 end if;
11856 end if;
11857 end Build_Incomplete_Type_Declaration;
11859 ------------------
11860 -- Designates_T --
11861 ------------------
11863 function Designates_T (Subt : Node_Id) return Boolean is
11864 Type_Id : constant Name_Id := Chars (Typ);
11866 function Names_T (Nam : Node_Id) return Boolean;
11867 -- The record type has not been introduced in the current scope
11868 -- yet, so we must examine the name of the type itself, either
11869 -- an identifier T, or an expanded name of the form P.T, where
11870 -- P denotes the current scope.
11872 -------------
11873 -- Names_T --
11874 -------------
11876 function Names_T (Nam : Node_Id) return Boolean is
11877 begin
11878 if Nkind (Nam) = N_Identifier then
11879 return Chars (Nam) = Type_Id;
11881 elsif Nkind (Nam) = N_Selected_Component then
11882 if Chars (Selector_Name (Nam)) = Type_Id then
11883 if Nkind (Prefix (Nam)) = N_Identifier then
11884 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11886 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11887 return Chars (Selector_Name (Prefix (Nam))) =
11888 Chars (Current_Scope);
11889 else
11890 return False;
11891 end if;
11893 else
11894 return False;
11895 end if;
11897 else
11898 return False;
11899 end if;
11900 end Names_T;
11902 -- Start of processing for Designates_T
11904 begin
11905 if Nkind (Subt) = N_Identifier then
11906 return Chars (Subt) = Type_Id;
11908 -- Reference can be through an expanded name which has not been
11909 -- analyzed yet, and which designates enclosing scopes.
11911 elsif Nkind (Subt) = N_Selected_Component then
11912 if Names_T (Subt) then
11913 return True;
11915 -- Otherwise it must denote an entity that is already visible.
11916 -- The access definition may name a subtype of the enclosing
11917 -- type, if there is a previous incomplete declaration for it.
11919 else
11920 Find_Selected_Component (Subt);
11921 return
11922 Is_Entity_Name (Subt)
11923 and then Scope (Entity (Subt)) = Current_Scope
11924 and then
11925 (Chars (Base_Type (Entity (Subt))) = Type_Id
11926 or else
11927 (Is_Class_Wide_Type (Entity (Subt))
11928 and then
11929 Chars (Etype (Base_Type (Entity (Subt)))) =
11930 Type_Id));
11931 end if;
11933 -- A reference to the current type may appear as the prefix of
11934 -- a 'Class attribute.
11936 elsif Nkind (Subt) = N_Attribute_Reference
11937 and then Attribute_Name (Subt) = Name_Class
11938 then
11939 return Names_T (Prefix (Subt));
11941 else
11942 return False;
11943 end if;
11944 end Designates_T;
11946 ----------------
11947 -- Mentions_T --
11948 ----------------
11950 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11951 Param_Spec : Node_Id;
11953 Acc_Subprg : constant Node_Id :=
11954 Access_To_Subprogram_Definition (Acc_Def);
11956 begin
11957 if No (Acc_Subprg) then
11958 return Designates_T (Subtype_Mark (Acc_Def));
11959 end if;
11961 -- Component is an access_to_subprogram: examine its formals,
11962 -- and result definition in the case of an access_to_function.
11964 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11965 while Present (Param_Spec) loop
11966 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11967 and then Mentions_T (Parameter_Type (Param_Spec))
11968 then
11969 return True;
11971 elsif Designates_T (Parameter_Type (Param_Spec)) then
11972 return True;
11973 end if;
11975 Next (Param_Spec);
11976 end loop;
11978 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11979 if Nkind (Result_Definition (Acc_Subprg)) =
11980 N_Access_Definition
11981 then
11982 return Mentions_T (Result_Definition (Acc_Subprg));
11983 else
11984 return Designates_T (Result_Definition (Acc_Subprg));
11985 end if;
11986 end if;
11988 return False;
11989 end Mentions_T;
11991 -- Start of processing for Check_Anonymous_Access_Component
11993 begin
11994 if Present (Access_Def) and then Mentions_T (Access_Def) then
11995 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
11997 Build_Incomplete_Type_Declaration;
11998 Anon_Access := Make_Temporary (Loc, 'S');
12000 -- Create a declaration for the anonymous access type: either
12001 -- an access_to_object or an access_to_subprogram.
12003 if Present (Acc_Def) then
12004 if Nkind (Acc_Def) = N_Access_Function_Definition then
12005 Type_Def :=
12006 Make_Access_Function_Definition (Loc,
12007 Parameter_Specifications =>
12008 Parameter_Specifications (Acc_Def),
12009 Result_Definition => Result_Definition (Acc_Def));
12010 else
12011 Type_Def :=
12012 Make_Access_Procedure_Definition (Loc,
12013 Parameter_Specifications =>
12014 Parameter_Specifications (Acc_Def));
12015 end if;
12017 else
12018 Type_Def :=
12019 Make_Access_To_Object_Definition (Loc,
12020 Subtype_Indication =>
12021 Relocate_Node (Subtype_Mark (Access_Def)));
12023 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
12024 Set_All_Present (Type_Def, All_Present (Access_Def));
12025 end if;
12027 Set_Null_Exclusion_Present
12028 (Type_Def, Null_Exclusion_Present (Access_Def));
12030 Decl :=
12031 Make_Full_Type_Declaration (Loc,
12032 Defining_Identifier => Anon_Access,
12033 Type_Definition => Type_Def);
12035 Insert_Before (Typ_Decl, Decl);
12036 Analyze (Decl);
12038 -- At first sight we could add here the extra formals of an access to
12039 -- subprogram; however, it must delayed till the freeze point so that
12040 -- we know the convention.
12042 if Nkind (Comp_Def) = N_Component_Definition then
12043 Rewrite (Comp_Def,
12044 Make_Component_Definition (Loc,
12045 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
12046 else
12047 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
12048 Rewrite (Comp_Def,
12049 Make_Discriminant_Specification (Loc,
12050 Defining_Identifier => Defining_Identifier (Comp_Def),
12051 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
12052 end if;
12054 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
12055 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
12056 else
12057 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
12058 end if;
12060 Set_Is_Local_Anonymous_Access (Anon_Access);
12061 end if;
12062 end Check_Anonymous_Access_Component;
12064 ---------------------------------------
12065 -- Check_Anonymous_Access_Components --
12066 ---------------------------------------
12068 procedure Check_Anonymous_Access_Components
12069 (Typ_Decl : Node_Id;
12070 Typ : Entity_Id;
12071 Prev : Entity_Id;
12072 Comp_List : Node_Id)
12074 Comp : Node_Id;
12075 begin
12076 if No (Comp_List) then
12077 return;
12078 end if;
12080 Set_Is_Not_Self_Hidden (Typ);
12082 Comp := First (Component_Items (Comp_List));
12083 while Present (Comp) loop
12084 if Nkind (Comp) = N_Component_Declaration then
12085 Check_Anonymous_Access_Component
12086 (Typ_Decl, Typ, Prev,
12087 Component_Definition (Comp),
12088 Access_Definition (Component_Definition (Comp)));
12089 end if;
12091 Next (Comp);
12092 end loop;
12094 if Present (Variant_Part (Comp_List)) then
12095 declare
12096 V : Node_Id;
12097 begin
12098 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
12099 while Present (V) loop
12100 Check_Anonymous_Access_Components
12101 (Typ_Decl, Typ, Prev, Component_List (V));
12102 Next_Non_Pragma (V);
12103 end loop;
12104 end;
12105 end if;
12106 end Check_Anonymous_Access_Components;
12108 ----------------------
12109 -- Check_Completion --
12110 ----------------------
12112 procedure Check_Completion (Body_Id : Node_Id := Empty) is
12113 E : Entity_Id;
12115 procedure Post_Error;
12116 -- Post error message for lack of completion for entity E
12118 ----------------
12119 -- Post_Error --
12120 ----------------
12122 procedure Post_Error is
12123 procedure Missing_Body;
12124 -- Output missing body message
12126 ------------------
12127 -- Missing_Body --
12128 ------------------
12130 procedure Missing_Body is
12131 begin
12132 -- Spec is in same unit, so we can post on spec
12134 if In_Same_Source_Unit (Body_Id, E) then
12135 Error_Msg_N ("missing body for &", E);
12137 -- Spec is in a separate unit, so we have to post on the body
12139 else
12140 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12141 end if;
12142 end Missing_Body;
12144 -- Start of processing for Post_Error
12146 begin
12147 if not Comes_From_Source (E) then
12148 if Ekind (E) in E_Task_Type | E_Protected_Type then
12150 -- It may be an anonymous protected type created for a
12151 -- single variable. Post error on variable, if present.
12153 declare
12154 Var : Entity_Id;
12156 begin
12157 Var := First_Entity (Current_Scope);
12158 while Present (Var) loop
12159 exit when Etype (Var) = E
12160 and then Comes_From_Source (Var);
12162 Next_Entity (Var);
12163 end loop;
12165 if Present (Var) then
12166 E := Var;
12167 end if;
12168 end;
12169 end if;
12170 end if;
12172 -- If a generated entity has no completion, then either previous
12173 -- semantic errors have disabled the expansion phase, or else we had
12174 -- missing subunits, or else we are compiling without expansion,
12175 -- or else something is very wrong.
12177 if not Comes_From_Source (E) then
12178 pragma Assert
12179 (Serious_Errors_Detected > 0
12180 or else Configurable_Run_Time_Violations > 0
12181 or else Subunits_Missing
12182 or else not Expander_Active);
12183 return;
12185 -- Here for source entity
12187 else
12188 -- Here if no body to post the error message, so we post the error
12189 -- on the declaration that has no completion. This is not really
12190 -- the right place to post it, think about this later ???
12192 if No (Body_Id) then
12193 if Is_Type (E) then
12194 Error_Msg_NE
12195 ("missing full declaration for }", Parent (E), E);
12196 else
12197 Error_Msg_NE ("missing body for &", Parent (E), E);
12198 end if;
12200 -- Package body has no completion for a declaration that appears
12201 -- in the corresponding spec. Post error on the body, with a
12202 -- reference to the non-completed declaration.
12204 else
12205 Error_Msg_Sloc := Sloc (E);
12207 if Is_Type (E) then
12208 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12210 elsif Is_Overloadable (E)
12211 and then Current_Entity_In_Scope (E) /= E
12212 then
12213 -- It may be that the completion is mistyped and appears as
12214 -- a distinct overloading of the entity.
12216 declare
12217 Candidate : constant Entity_Id :=
12218 Current_Entity_In_Scope (E);
12219 Decl : constant Node_Id :=
12220 Unit_Declaration_Node (Candidate);
12222 begin
12223 if Is_Overloadable (Candidate)
12224 and then Ekind (Candidate) = Ekind (E)
12225 and then Nkind (Decl) = N_Subprogram_Body
12226 and then Acts_As_Spec (Decl)
12227 then
12228 Check_Type_Conformant (Candidate, E);
12230 else
12231 Missing_Body;
12232 end if;
12233 end;
12235 else
12236 Missing_Body;
12237 end if;
12238 end if;
12239 end if;
12240 end Post_Error;
12242 -- Local variables
12244 Pack_Id : constant Entity_Id := Current_Scope;
12246 -- Start of processing for Check_Completion
12248 begin
12249 E := First_Entity (Pack_Id);
12250 while Present (E) loop
12251 if Is_Intrinsic_Subprogram (E) then
12252 null;
12254 -- The following situation requires special handling: a child unit
12255 -- that appears in the context clause of the body of its parent:
12257 -- procedure Parent.Child (...);
12259 -- with Parent.Child;
12260 -- package body Parent is
12262 -- Here Parent.Child appears as a local entity, but should not be
12263 -- flagged as requiring completion, because it is a compilation
12264 -- unit.
12266 -- Ignore missing completion for a subprogram that does not come from
12267 -- source (including the _Call primitive operation of RAS types,
12268 -- which has to have the flag Comes_From_Source for other purposes):
12269 -- we assume that the expander will provide the missing completion.
12270 -- In case of previous errors, other expansion actions that provide
12271 -- bodies for null procedures with not be invoked, so inhibit message
12272 -- in those cases.
12274 -- Note that E_Operator is not in the list that follows, because
12275 -- this kind is reserved for predefined operators, that are
12276 -- intrinsic and do not need completion.
12278 elsif Ekind (E) in E_Function
12279 | E_Procedure
12280 | E_Generic_Function
12281 | E_Generic_Procedure
12282 then
12283 if Has_Completion (E) then
12284 null;
12286 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12287 null;
12289 elsif Is_Subprogram (E)
12290 and then (not Comes_From_Source (E)
12291 or else Chars (E) = Name_uCall)
12292 then
12293 null;
12295 elsif
12296 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12297 then
12298 null;
12300 elsif Nkind (Parent (E)) = N_Procedure_Specification
12301 and then Null_Present (Parent (E))
12302 and then Serious_Errors_Detected > 0
12303 then
12304 null;
12306 else
12307 Post_Error;
12308 end if;
12310 elsif Is_Entry (E) then
12311 if not Has_Completion (E)
12312 and then Ekind (Scope (E)) = E_Protected_Type
12313 then
12314 Post_Error;
12315 end if;
12317 elsif Is_Package_Or_Generic_Package (E) then
12318 if Unit_Requires_Body (E) then
12319 if not Has_Completion (E)
12320 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12321 N_Compilation_Unit
12322 then
12323 Post_Error;
12324 end if;
12326 elsif not Is_Child_Unit (E) then
12327 May_Need_Implicit_Body (E);
12328 end if;
12330 -- A formal incomplete type (Ada 2012) does not require a completion;
12331 -- other incomplete type declarations do.
12333 elsif Ekind (E) = E_Incomplete_Type then
12334 if No (Underlying_Type (E))
12335 and then not Is_Generic_Type (E)
12336 then
12337 Post_Error;
12338 end if;
12340 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12341 if not Has_Completion (E) then
12342 Post_Error;
12343 end if;
12345 -- A single task declared in the current scope is a constant, verify
12346 -- that the body of its anonymous type is in the same scope. If the
12347 -- task is defined elsewhere, this may be a renaming declaration for
12348 -- which no completion is needed.
12350 elsif Ekind (E) = E_Constant then
12351 if Ekind (Etype (E)) = E_Task_Type
12352 and then not Has_Completion (Etype (E))
12353 and then Scope (Etype (E)) = Current_Scope
12354 then
12355 Post_Error;
12356 end if;
12358 elsif Ekind (E) = E_Record_Type then
12359 if Is_Tagged_Type (E) then
12360 Check_Abstract_Overriding (E);
12361 Check_Conventions (E);
12362 end if;
12364 Check_Aliased_Component_Types (E);
12366 elsif Ekind (E) = E_Array_Type then
12367 Check_Aliased_Component_Types (E);
12369 end if;
12371 Next_Entity (E);
12372 end loop;
12373 end Check_Completion;
12375 -------------------------------------
12376 -- Check_Constraining_Discriminant --
12377 -------------------------------------
12379 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12381 New_Type : constant Entity_Id := Etype (New_Disc);
12382 Old_Type : Entity_Id;
12384 begin
12385 -- If the record type contains an array constrained by the discriminant
12386 -- but with some different bound, the compiler tries to create a smaller
12387 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12388 -- In this case, where the discriminant type is a scalar type, the check
12389 -- must use the original discriminant type in the parent declaration.
12391 if Is_Scalar_Type (New_Type) then
12392 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12393 else
12394 Old_Type := Etype (Old_Disc);
12395 end if;
12397 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12398 Error_Msg_N
12399 ("subtype must be statically compatible with parent discriminant",
12400 New_Disc);
12402 if not Predicates_Compatible (New_Type, Old_Type) then
12403 Error_Msg_N
12404 ("\subtype predicate is not compatible with parent discriminant",
12405 New_Disc);
12406 end if;
12407 end if;
12408 end Check_Constraining_Discriminant;
12410 ------------------------------------
12411 -- Check_CPP_Type_Has_No_Defaults --
12412 ------------------------------------
12414 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12415 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12416 Clist : Node_Id;
12417 Comp : Node_Id;
12419 begin
12420 -- Obtain the component list
12422 if Nkind (Tdef) = N_Record_Definition then
12423 Clist := Component_List (Tdef);
12424 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12425 Clist := Component_List (Record_Extension_Part (Tdef));
12426 end if;
12428 -- Check all components to ensure no default expressions
12430 if Present (Clist) then
12431 Comp := First_Non_Pragma (Component_Items (Clist));
12432 while Present (Comp) loop
12433 if Present (Expression (Comp)) then
12434 Error_Msg_N
12435 ("component of imported 'C'P'P type cannot have "
12436 & "default expression", Expression (Comp));
12437 end if;
12439 Next_Non_Pragma (Comp);
12440 end loop;
12441 end if;
12442 end Check_CPP_Type_Has_No_Defaults;
12444 ----------------------------
12445 -- Check_Delta_Expression --
12446 ----------------------------
12448 procedure Check_Delta_Expression (E : Node_Id) is
12449 begin
12450 if not (Is_Real_Type (Etype (E))) then
12451 Wrong_Type (E, Any_Real);
12453 elsif not Is_OK_Static_Expression (E) then
12454 Flag_Non_Static_Expr
12455 ("non-static expression used for delta value!", E);
12457 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12458 Error_Msg_N ("delta expression must be positive", E);
12460 else
12461 return;
12462 end if;
12464 -- If any of above errors occurred, then replace the incorrect
12465 -- expression by the real 0.1, which should prevent further errors.
12467 Rewrite (E,
12468 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12469 Analyze_And_Resolve (E, Standard_Float);
12470 end Check_Delta_Expression;
12472 -----------------------------
12473 -- Check_Digits_Expression --
12474 -----------------------------
12476 procedure Check_Digits_Expression (E : Node_Id) is
12477 begin
12478 if not (Is_Integer_Type (Etype (E))) then
12479 Wrong_Type (E, Any_Integer);
12481 elsif not Is_OK_Static_Expression (E) then
12482 Flag_Non_Static_Expr
12483 ("non-static expression used for digits value!", E);
12485 elsif Expr_Value (E) <= 0 then
12486 Error_Msg_N ("digits value must be greater than zero", E);
12488 else
12489 return;
12490 end if;
12492 -- If any of above errors occurred, then replace the incorrect
12493 -- expression by the integer 1, which should prevent further errors.
12495 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12496 Analyze_And_Resolve (E, Standard_Integer);
12498 end Check_Digits_Expression;
12500 --------------------------
12501 -- Check_Initialization --
12502 --------------------------
12504 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12505 begin
12506 -- Special processing for limited types
12508 if Is_Limited_Type (T)
12509 and then not In_Instance
12510 and then not In_Inlined_Body
12511 then
12512 if not OK_For_Limited_Init (T, Exp) then
12514 -- In GNAT mode, this is just a warning, to allow it to be evilly
12515 -- turned off. Otherwise it is a real error.
12517 if GNAT_Mode then
12518 Error_Msg_N
12519 ("??cannot initialize entities of limited type!", Exp);
12521 elsif Ada_Version < Ada_2005 then
12523 -- The side effect removal machinery may generate illegal Ada
12524 -- code to avoid the usage of access types and 'reference in
12525 -- SPARK mode. Since this is legal code with respect to theorem
12526 -- proving, do not emit the error.
12528 if GNATprove_Mode
12529 and then Nkind (Exp) = N_Function_Call
12530 and then Nkind (Parent (Exp)) = N_Object_Declaration
12531 and then not Comes_From_Source
12532 (Defining_Identifier (Parent (Exp)))
12533 then
12534 null;
12536 else
12537 Error_Msg_N
12538 ("cannot initialize entities of limited type", Exp);
12539 Explain_Limited_Type (T, Exp);
12540 end if;
12542 else
12543 -- Specialize error message according to kind of illegal
12544 -- initial expression. We check the Original_Node to cover
12545 -- cases where the initialization expression of an object
12546 -- declaration generated by the compiler has been rewritten
12547 -- (such as for dispatching calls).
12549 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12550 and then
12551 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12552 then
12553 -- No error for internally-generated object declarations,
12554 -- which can come from build-in-place assignment statements.
12556 if Nkind (Parent (Exp)) = N_Object_Declaration
12557 and then not Comes_From_Source
12558 (Defining_Identifier (Parent (Exp)))
12559 then
12560 null;
12562 else
12563 Error_Msg_N
12564 ("illegal context for call to function with limited "
12565 & "result", Exp);
12566 end if;
12568 else
12569 Error_Msg_N
12570 ("initialization of limited object requires aggregate or "
12571 & "function call", Exp);
12572 end if;
12573 end if;
12574 end if;
12575 end if;
12577 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12578 -- set unless we can be sure that no range check is required.
12580 if not Expander_Active
12581 and then Is_Scalar_Type (T)
12582 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12583 then
12584 Set_Do_Range_Check (Exp);
12585 end if;
12586 end Check_Initialization;
12588 ----------------------
12589 -- Check_Interfaces --
12590 ----------------------
12592 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12593 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12595 Iface : Node_Id;
12596 Iface_Def : Node_Id;
12597 Iface_Typ : Entity_Id;
12598 Parent_Node : Node_Id;
12600 Is_Task : Boolean := False;
12601 -- Set True if parent type or any progenitor is a task interface
12603 Is_Protected : Boolean := False;
12604 -- Set True if parent type or any progenitor is a protected interface
12606 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12607 -- Check that a progenitor is compatible with declaration. If an error
12608 -- message is output, it is posted on Error_Node.
12610 ------------------
12611 -- Check_Ifaces --
12612 ------------------
12614 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12615 Iface_Id : constant Entity_Id :=
12616 Defining_Identifier (Parent (Iface_Def));
12617 Type_Def : Node_Id;
12619 begin
12620 if Nkind (N) = N_Private_Extension_Declaration then
12621 Type_Def := N;
12622 else
12623 Type_Def := Type_Definition (N);
12624 end if;
12626 if Is_Task_Interface (Iface_Id) then
12627 Is_Task := True;
12629 elsif Is_Protected_Interface (Iface_Id) then
12630 Is_Protected := True;
12631 end if;
12633 if Is_Synchronized_Interface (Iface_Id) then
12635 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12636 -- extension derived from a synchronized interface must explicitly
12637 -- be declared synchronized, because the full view will be a
12638 -- synchronized type.
12640 if Nkind (N) = N_Private_Extension_Declaration then
12641 if not Synchronized_Present (N) then
12642 Error_Msg_NE
12643 ("private extension of& must be explicitly synchronized",
12644 N, Iface_Id);
12645 end if;
12647 -- However, by 3.9.4(16/2), a full type that is a record extension
12648 -- is never allowed to derive from a synchronized interface (note
12649 -- that interfaces must be excluded from this check, because those
12650 -- are represented by derived type definitions in some cases).
12652 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12653 and then not Interface_Present (Type_Definition (N))
12654 then
12655 Error_Msg_N ("record extension cannot derive from synchronized "
12656 & "interface", Error_Node);
12657 end if;
12658 end if;
12660 -- Check that the characteristics of the progenitor are compatible
12661 -- with the explicit qualifier in the declaration.
12662 -- The check only applies to qualifiers that come from source.
12663 -- Limited_Present also appears in the declaration of corresponding
12664 -- records, and the check does not apply to them.
12666 if Limited_Present (Type_Def)
12667 and then not
12668 Is_Concurrent_Record_Type (Defining_Identifier (N))
12669 then
12670 if Is_Limited_Interface (Parent_Type)
12671 and then not Is_Limited_Interface (Iface_Id)
12672 then
12673 Error_Msg_NE
12674 ("progenitor & must be limited interface",
12675 Error_Node, Iface_Id);
12677 elsif
12678 (Task_Present (Iface_Def)
12679 or else Protected_Present (Iface_Def)
12680 or else Synchronized_Present (Iface_Def))
12681 and then Nkind (N) /= N_Private_Extension_Declaration
12682 and then not Error_Posted (N)
12683 then
12684 Error_Msg_NE
12685 ("progenitor & must be limited interface",
12686 Error_Node, Iface_Id);
12687 end if;
12689 -- Protected interfaces can only inherit from limited, synchronized
12690 -- or protected interfaces.
12692 elsif Nkind (N) = N_Full_Type_Declaration
12693 and then Protected_Present (Type_Def)
12694 then
12695 if Limited_Present (Iface_Def)
12696 or else Synchronized_Present (Iface_Def)
12697 or else Protected_Present (Iface_Def)
12698 then
12699 null;
12701 elsif Task_Present (Iface_Def) then
12702 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12703 & "from task interface", Error_Node);
12705 else
12706 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12707 & "from non-limited interface", Error_Node);
12708 end if;
12710 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12711 -- limited and synchronized.
12713 elsif Synchronized_Present (Type_Def) then
12714 if Limited_Present (Iface_Def)
12715 or else Synchronized_Present (Iface_Def)
12716 then
12717 null;
12719 elsif Protected_Present (Iface_Def)
12720 and then Nkind (N) /= N_Private_Extension_Declaration
12721 then
12722 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12723 & "from protected interface", Error_Node);
12725 elsif Task_Present (Iface_Def)
12726 and then Nkind (N) /= N_Private_Extension_Declaration
12727 then
12728 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12729 & "from task interface", Error_Node);
12731 elsif not Is_Limited_Interface (Iface_Id) then
12732 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12733 & "from non-limited interface", Error_Node);
12734 end if;
12736 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12737 -- synchronized or task interfaces.
12739 elsif Nkind (N) = N_Full_Type_Declaration
12740 and then Task_Present (Type_Def)
12741 then
12742 if Limited_Present (Iface_Def)
12743 or else Synchronized_Present (Iface_Def)
12744 or else Task_Present (Iface_Def)
12745 then
12746 null;
12748 elsif Protected_Present (Iface_Def) then
12749 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12750 & "protected interface", Error_Node);
12752 else
12753 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12754 & "non-limited interface", Error_Node);
12755 end if;
12756 end if;
12757 end Check_Ifaces;
12759 -- Start of processing for Check_Interfaces
12761 begin
12762 if Is_Interface (Parent_Type) then
12763 if Is_Task_Interface (Parent_Type) then
12764 Is_Task := True;
12766 elsif Is_Protected_Interface (Parent_Type) then
12767 Is_Protected := True;
12768 end if;
12769 end if;
12771 if Nkind (N) = N_Private_Extension_Declaration then
12773 -- Check that progenitors are compatible with declaration
12775 Iface := First (Interface_List (Def));
12776 while Present (Iface) loop
12777 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12779 Parent_Node := Parent (Base_Type (Iface_Typ));
12780 Iface_Def := Type_Definition (Parent_Node);
12782 if not Is_Interface (Iface_Typ) then
12783 Diagnose_Interface (Iface, Iface_Typ);
12784 else
12785 Check_Ifaces (Iface_Def, Iface);
12786 end if;
12788 Next (Iface);
12789 end loop;
12791 if Is_Task and Is_Protected then
12792 Error_Msg_N
12793 ("type cannot derive from task and protected interface", N);
12794 end if;
12796 return;
12797 end if;
12799 -- Full type declaration of derived type.
12800 -- Check compatibility with parent if it is interface type
12802 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12803 and then Is_Interface (Parent_Type)
12804 then
12805 Parent_Node := Parent (Parent_Type);
12807 -- More detailed checks for interface varieties
12809 Check_Ifaces
12810 (Iface_Def => Type_Definition (Parent_Node),
12811 Error_Node => Subtype_Indication (Type_Definition (N)));
12812 end if;
12814 Iface := First (Interface_List (Def));
12815 while Present (Iface) loop
12816 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12818 Parent_Node := Parent (Base_Type (Iface_Typ));
12819 Iface_Def := Type_Definition (Parent_Node);
12821 if not Is_Interface (Iface_Typ) then
12822 Diagnose_Interface (Iface, Iface_Typ);
12824 else
12825 -- "The declaration of a specific descendant of an interface
12826 -- type freezes the interface type" RM 13.14
12828 Freeze_Before (N, Iface_Typ);
12829 Check_Ifaces (Iface_Def, Error_Node => Iface);
12830 end if;
12832 Next (Iface);
12833 end loop;
12835 if Is_Task and Is_Protected then
12836 Error_Msg_N
12837 ("type cannot derive from task and protected interface", N);
12838 end if;
12839 end Check_Interfaces;
12841 ------------------------------------
12842 -- Check_Or_Process_Discriminants --
12843 ------------------------------------
12845 -- If an incomplete or private type declaration was already given for the
12846 -- type, the discriminants may have already been processed if they were
12847 -- present on the incomplete declaration. In this case a full conformance
12848 -- check has been performed in Find_Type_Name, and we then recheck here
12849 -- some properties that can't be checked on the partial view alone.
12850 -- Otherwise we call Process_Discriminants.
12852 procedure Check_Or_Process_Discriminants
12853 (N : Node_Id;
12854 T : Entity_Id;
12855 Prev : Entity_Id := Empty)
12857 begin
12858 if Has_Discriminants (T) then
12860 -- Discriminants are already set on T if they were already present
12861 -- on the partial view. Make them visible to component declarations.
12863 declare
12864 D : Entity_Id;
12865 -- Discriminant on T (full view) referencing expr on partial view
12867 Prev_D : Entity_Id;
12868 -- Entity of corresponding discriminant on partial view
12870 New_D : Node_Id;
12871 -- Discriminant specification for full view, expression is
12872 -- the syntactic copy on full view (which has been checked for
12873 -- conformance with partial view), only used here to post error
12874 -- message.
12876 begin
12877 D := First_Discriminant (T);
12878 New_D := First (Discriminant_Specifications (N));
12879 while Present (D) loop
12880 Prev_D := Current_Entity (D);
12881 Set_Current_Entity (D);
12882 Set_Is_Immediately_Visible (D);
12883 Set_Homonym (D, Prev_D);
12885 -- Handle the case where there is an untagged partial view and
12886 -- the full view is tagged: must disallow discriminants with
12887 -- defaults, unless compiling for Ada 2012, which allows a
12888 -- limited tagged type to have defaulted discriminants (see
12889 -- AI05-0214). However, suppress error here if it was already
12890 -- reported on the default expression of the partial view.
12892 if Is_Tagged_Type (T)
12893 and then Present (Expression (Parent (D)))
12894 and then (not Is_Limited_Type (Current_Scope)
12895 or else Ada_Version < Ada_2012)
12896 and then not Error_Posted (Expression (Parent (D)))
12897 then
12898 if Ada_Version >= Ada_2012 then
12899 Error_Msg_N
12900 ("discriminants of nonlimited tagged type cannot have "
12901 & "defaults",
12902 Expression (New_D));
12903 else
12904 Error_Msg_N
12905 ("discriminants of tagged type cannot have defaults",
12906 Expression (New_D));
12907 end if;
12908 end if;
12910 -- Ada 2005 (AI-230): Access discriminant allowed in
12911 -- non-limited record types.
12913 if Ada_Version < Ada_2005 then
12915 -- This restriction gets applied to the full type here. It
12916 -- has already been applied earlier to the partial view.
12918 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12919 end if;
12921 Next_Discriminant (D);
12922 Next (New_D);
12923 end loop;
12924 end;
12926 elsif Present (Discriminant_Specifications (N)) then
12927 Process_Discriminants (N, Prev);
12928 end if;
12929 end Check_Or_Process_Discriminants;
12931 ----------------------
12932 -- Check_Real_Bound --
12933 ----------------------
12935 procedure Check_Real_Bound (Bound : Node_Id) is
12936 begin
12937 if not Is_Real_Type (Etype (Bound)) then
12938 Error_Msg_N
12939 ("bound in real type definition must be of real type", Bound);
12941 elsif not Is_OK_Static_Expression (Bound) then
12942 Flag_Non_Static_Expr
12943 ("non-static expression used for real type bound!", Bound);
12945 else
12946 return;
12947 end if;
12949 Rewrite
12950 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12951 Analyze (Bound);
12952 Resolve (Bound, Standard_Float);
12953 end Check_Real_Bound;
12955 ------------------------------
12956 -- Complete_Private_Subtype --
12957 ------------------------------
12959 procedure Complete_Private_Subtype
12960 (Priv : Entity_Id;
12961 Full : Entity_Id;
12962 Full_Base : Entity_Id;
12963 Related_Nod : Node_Id)
12965 Save_Next_Entity : Entity_Id;
12966 Save_Homonym : Entity_Id;
12968 begin
12969 -- Set semantic attributes for (implicit) private subtype completion.
12970 -- If the full type has no discriminants, then it is a copy of the
12971 -- full view of the base. Otherwise, it is a subtype of the base with
12972 -- a possible discriminant constraint. Save and restore the original
12973 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12974 -- not corrupt the entity chain.
12976 Save_Next_Entity := Next_Entity (Full);
12977 Save_Homonym := Homonym (Priv);
12979 if Is_Private_Type (Full_Base)
12980 or else Is_Record_Type (Full_Base)
12981 or else Is_Concurrent_Type (Full_Base)
12982 then
12983 Copy_Node (Priv, Full);
12985 -- Note that the Etype of the full view is the same as the Etype of
12986 -- the partial view. In this fashion, the subtype has access to the
12987 -- correct view of the parent.
12989 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12990 Set_Has_Unknown_Discriminants
12991 (Full, Has_Unknown_Discriminants (Full_Base));
12992 Set_First_Entity (Full, First_Entity (Full_Base));
12993 Set_Last_Entity (Full, Last_Entity (Full_Base));
12995 -- If the underlying base type is constrained, we know that the
12996 -- full view of the subtype is constrained as well (the converse
12997 -- is not necessarily true).
12999 if Is_Constrained (Full_Base) then
13000 Set_Is_Constrained (Full);
13001 end if;
13003 else
13004 Copy_Node (Full_Base, Full);
13006 -- The following subtlety with the Etype of the full view needs to be
13007 -- taken into account here. One could think that it must naturally be
13008 -- set to the base type of the full base:
13010 -- Set_Etype (Full, Base_Type (Full_Base));
13012 -- so that the full view becomes a subtype of the full base when the
13013 -- latter is a base type, which must for example happen when the full
13014 -- base is declared as derived type. That's also correct if the full
13015 -- base is declared as an array type, or a floating-point type, or a
13016 -- fixed-point type, or a signed integer type, as these declarations
13017 -- create an implicit base type and a first subtype so the Etype of
13018 -- the full views must be the implicit base type. But that's wrong
13019 -- if the full base is declared as an access type, or an enumeration
13020 -- type, or a modular integer type, as these declarations directly
13021 -- create a base type, i.e. with Etype pointing to itself. Moreover
13022 -- the full base being declared in the private part, i.e. when the
13023 -- views are swapped, the end result is that the Etype of the full
13024 -- base is set to its private view in this case and that we need to
13025 -- propagate this setting to the full view in order for the subtype
13026 -- to be compatible with the base type.
13028 if Is_Base_Type (Full_Base)
13029 and then (Is_Derived_Type (Full_Base)
13030 or else Ekind (Full_Base) in Array_Kind
13031 or else Ekind (Full_Base) in Fixed_Point_Kind
13032 or else Ekind (Full_Base) in Float_Kind
13033 or else Ekind (Full_Base) in Signed_Integer_Kind)
13034 then
13035 Set_Etype (Full, Full_Base);
13036 end if;
13038 Set_Chars (Full, Chars (Priv));
13039 Set_Sloc (Full, Sloc (Priv));
13040 Conditional_Delay (Full, Priv);
13041 end if;
13043 Link_Entities (Full, Save_Next_Entity);
13044 Set_Homonym (Full, Save_Homonym);
13045 Set_Associated_Node_For_Itype (Full, Related_Nod);
13047 if Ekind (Full) in Incomplete_Or_Private_Kind then
13048 Reinit_Field_To_Zero (Full, F_Private_Dependents);
13049 end if;
13051 -- Set common attributes for all subtypes: kind, convention, etc.
13053 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
13054 Set_Is_Not_Self_Hidden (Full);
13055 Set_Convention (Full, Convention (Full_Base));
13056 Set_Is_First_Subtype (Full, False);
13057 Set_Scope (Full, Scope (Priv));
13058 Set_Size_Info (Full, Full_Base);
13059 Copy_RM_Size (To => Full, From => Full_Base);
13060 Set_Is_Itype (Full);
13062 -- A subtype of a private-type-without-discriminants, whose full-view
13063 -- has discriminants with default expressions, is not constrained.
13065 if not Has_Discriminants (Priv) then
13066 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
13068 if Has_Discriminants (Full_Base) then
13069 Set_Discriminant_Constraint
13070 (Full, Discriminant_Constraint (Full_Base));
13072 -- The partial view may have been indefinite, the full view
13073 -- might not be.
13075 Set_Has_Unknown_Discriminants
13076 (Full, Has_Unknown_Discriminants (Full_Base));
13077 end if;
13078 end if;
13080 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
13081 Set_Depends_On_Private (Full, Has_Private_Component (Full));
13083 -- Freeze the private subtype entity if its parent is delayed, and not
13084 -- already frozen. We skip this processing if the type is an anonymous
13085 -- subtype of a record component, or is the corresponding record of a
13086 -- protected type, since these are processed when the enclosing type
13087 -- is frozen. If the parent type is declared in a nested package then
13088 -- the freezing of the private and full views also happens later.
13090 if not Is_Type (Scope (Full)) then
13091 if Is_Itype (Priv)
13092 and then In_Same_Source_Unit (Full, Full_Base)
13093 and then Scope (Full_Base) /= Scope (Full)
13094 then
13095 Set_Has_Delayed_Freeze (Full);
13096 Set_Has_Delayed_Freeze (Priv);
13098 else
13099 Set_Has_Delayed_Freeze (Full,
13100 Has_Delayed_Freeze (Full_Base)
13101 and then not Is_Frozen (Full_Base));
13102 end if;
13103 end if;
13105 Set_Freeze_Node (Full, Empty);
13106 Set_Is_Frozen (Full, False);
13108 if Has_Discriminants (Full) then
13109 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
13110 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
13112 if Has_Unknown_Discriminants (Full) then
13113 Set_Discriminant_Constraint (Full, No_Elist);
13114 end if;
13115 end if;
13117 if Ekind (Full_Base) = E_Record_Type
13118 and then Has_Discriminants (Full_Base)
13119 and then Has_Discriminants (Priv) -- might not, if errors
13120 and then not Has_Unknown_Discriminants (Priv)
13121 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13122 then
13123 Create_Constrained_Components
13124 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13126 -- If the full base is itself derived from private, build a congruent
13127 -- subtype of its underlying full view, for use by the back end.
13129 elsif Is_Private_Type (Full_Base)
13130 and then Present (Underlying_Full_View (Full_Base))
13131 then
13132 declare
13133 Underlying_Full_Base : constant Entity_Id
13134 := Underlying_Full_View (Full_Base);
13135 Underlying_Full : constant Entity_Id
13136 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13137 begin
13138 Set_Is_Itype (Underlying_Full);
13139 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13140 Complete_Private_Subtype
13141 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13142 Set_Underlying_Full_View (Full, Underlying_Full);
13143 Set_Is_Underlying_Full_View (Underlying_Full);
13144 end;
13146 elsif Is_Record_Type (Full_Base) then
13148 -- Show Full is simply a renaming of Full_Base
13150 Set_Cloned_Subtype (Full, Full_Base);
13151 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13153 -- Propagate predicates
13155 Propagate_Predicate_Attributes (Full, Full_Base);
13156 end if;
13158 -- It is unsafe to share the bounds of a scalar type, because the Itype
13159 -- is elaborated on demand, and if a bound is nonstatic, then different
13160 -- orders of elaboration in different units will lead to different
13161 -- external symbols.
13163 if Is_Scalar_Type (Full_Base) then
13164 Set_Scalar_Range (Full,
13165 Make_Range (Sloc (Related_Nod),
13166 Low_Bound =>
13167 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13168 High_Bound =>
13169 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13171 -- This completion inherits the bounds of the full parent, but if
13172 -- the parent is an unconstrained floating point type, so is the
13173 -- completion.
13175 if Is_Floating_Point_Type (Full_Base) then
13176 Set_Includes_Infinities
13177 (Scalar_Range (Full), Has_Infinities (Full_Base));
13178 end if;
13179 end if;
13181 -- ??? It seems that a lot of fields are missing that should be copied
13182 -- from Full_Base to Full. Here are some that are introduced in a
13183 -- non-disruptive way but a cleanup is necessary.
13185 if Is_Tagged_Type (Full_Base) then
13186 Set_Is_Tagged_Type (Full);
13187 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13189 Set_Direct_Primitive_Operations
13190 (Full, Direct_Primitive_Operations (Full_Base));
13191 Set_No_Tagged_Streams_Pragma
13192 (Full, No_Tagged_Streams_Pragma (Full_Base));
13194 if Is_Interface (Full_Base) then
13195 Set_Is_Interface (Full);
13196 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13197 end if;
13199 -- Inherit class_wide type of full_base in case the partial view was
13200 -- not tagged. Otherwise it has already been created when the private
13201 -- subtype was analyzed.
13203 if No (Class_Wide_Type (Full)) then
13204 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13205 end if;
13207 -- If this is a subtype of a protected or task type, constrain its
13208 -- corresponding record, unless this is a subtype without constraints,
13209 -- i.e. a simple renaming as with an actual subtype in an instance.
13211 elsif Is_Concurrent_Type (Full_Base) then
13212 if Has_Discriminants (Full)
13213 and then Present (Corresponding_Record_Type (Full_Base))
13214 and then
13215 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13216 then
13217 Set_Corresponding_Record_Type (Full,
13218 Constrain_Corresponding_Record
13219 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13221 else
13222 Set_Corresponding_Record_Type (Full,
13223 Corresponding_Record_Type (Full_Base));
13224 end if;
13225 end if;
13227 -- Link rep item chain, and also setting of Has_Predicates from private
13228 -- subtype to full subtype, since we will need these on the full subtype
13229 -- to create the predicate function. Note that the full subtype may
13230 -- already have rep items, inherited from the full view of the base
13231 -- type, so we must be sure not to overwrite these entries.
13233 declare
13234 Append : Boolean;
13235 Item : Node_Id;
13236 Next_Item : Node_Id;
13237 Priv_Item : Node_Id;
13239 begin
13240 Item := First_Rep_Item (Full);
13241 Priv_Item := First_Rep_Item (Priv);
13243 -- If no existing rep items on full type, we can just link directly
13244 -- to the list of items on the private type, if any exist.. Same if
13245 -- the rep items are only those inherited from the base
13247 if (No (Item)
13248 or else Nkind (Item) /= N_Aspect_Specification
13249 or else Entity (Item) = Full_Base)
13250 and then Present (First_Rep_Item (Priv))
13251 then
13252 Set_First_Rep_Item (Full, Priv_Item);
13254 -- Otherwise, search to the end of items currently linked to the full
13255 -- subtype and append the private items to the end. However, if Priv
13256 -- and Full already have the same list of rep items, then the append
13257 -- is not done, as that would create a circularity.
13259 -- The partial view may have a predicate and the rep item lists of
13260 -- both views agree when inherited from the same ancestor. In that
13261 -- case, simply propagate the list from one view to the other.
13262 -- A more complex analysis needed here ???
13264 elsif Present (Priv_Item)
13265 and then Item = Next_Rep_Item (Priv_Item)
13266 then
13267 Set_First_Rep_Item (Full, Priv_Item);
13269 elsif Item /= Priv_Item then
13270 Append := True;
13271 loop
13272 Next_Item := Next_Rep_Item (Item);
13273 exit when No (Next_Item);
13274 Item := Next_Item;
13276 -- If the private view has aspect specifications, the full view
13277 -- inherits them. Since these aspects may already have been
13278 -- attached to the full view during derivation, do not append
13279 -- them if already present.
13281 if Item = First_Rep_Item (Priv) then
13282 Append := False;
13283 exit;
13284 end if;
13285 end loop;
13287 -- And link the private type items at the end of the chain
13289 if Append then
13290 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13291 end if;
13292 end if;
13293 end;
13295 -- Make sure Has_Predicates is set on full type if it is set on the
13296 -- private type. Note that it may already be set on the full type and
13297 -- if so, we don't want to unset it. Similarly, propagate information
13298 -- about delayed aspects, because the corresponding pragmas must be
13299 -- analyzed when one of the views is frozen. This last step is needed
13300 -- in particular when the full type is a scalar type for which an
13301 -- anonymous base type is constructed.
13303 -- The predicate functions are generated either at the freeze point
13304 -- of the type or at the end of the visible part, and we must avoid
13305 -- generating them twice.
13307 Propagate_Predicate_Attributes (Full, Priv);
13309 if Has_Delayed_Aspects (Priv) then
13310 Set_Has_Delayed_Aspects (Full);
13311 end if;
13312 end Complete_Private_Subtype;
13314 ----------------------------
13315 -- Constant_Redeclaration --
13316 ----------------------------
13318 procedure Constant_Redeclaration
13319 (Id : Entity_Id;
13320 N : Node_Id;
13321 T : out Entity_Id)
13323 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13324 Obj_Def : constant Node_Id := Object_Definition (N);
13325 New_T : Entity_Id;
13327 procedure Check_Possible_Deferred_Completion
13328 (Prev_Id : Entity_Id;
13329 Curr_Obj_Def : Node_Id);
13330 -- Determine whether the two object definitions describe the partial
13331 -- and the full view of a constrained deferred constant. Generate
13332 -- a subtype for the full view and verify that it statically matches
13333 -- the subtype of the partial view.
13335 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13336 -- If deferred constant is an access type initialized with an allocator,
13337 -- check whether there is an illegal recursion in the definition,
13338 -- through a default value of some record subcomponent. This is normally
13339 -- detected when generating init procs, but requires this additional
13340 -- mechanism when expansion is disabled.
13342 ----------------------------------------
13343 -- Check_Possible_Deferred_Completion --
13344 ----------------------------------------
13346 procedure Check_Possible_Deferred_Completion
13347 (Prev_Id : Entity_Id;
13348 Curr_Obj_Def : Node_Id)
13350 Curr_Typ : Entity_Id;
13351 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13352 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13353 Mismatch : Boolean := False;
13354 begin
13355 if Anon_Acc then
13356 null;
13357 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13358 declare
13359 Loc : constant Source_Ptr := Sloc (N);
13360 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13361 Decl : constant Node_Id :=
13362 Make_Subtype_Declaration (Loc,
13363 Defining_Identifier => Def_Id,
13364 Subtype_Indication =>
13365 Relocate_Node (Curr_Obj_Def));
13367 begin
13368 Insert_Before_And_Analyze (N, Decl);
13369 Set_Etype (Id, Def_Id);
13370 Curr_Typ := Def_Id;
13371 end;
13372 else
13373 Curr_Typ := Etype (Curr_Obj_Def);
13374 end if;
13376 if Anon_Acc then
13377 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13378 Mismatch := True;
13379 elsif Has_Null_Exclusion (Prev_Typ)
13380 and then not Null_Exclusion_Present (Curr_Obj_Def)
13381 then
13382 Mismatch := True;
13383 end if;
13384 -- ??? Another check needed: mismatch if disagreement
13385 -- between designated types/profiles .
13386 else
13387 Mismatch :=
13388 Is_Constrained (Prev_Typ)
13389 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13390 end if;
13392 if Mismatch then
13393 Error_Msg_Sloc := Sloc (Prev_Id);
13394 Error_Msg_N ("subtype does not statically match deferred "
13395 & "declaration #", N);
13396 end if;
13397 end Check_Possible_Deferred_Completion;
13399 ---------------------------------
13400 -- Check_Recursive_Declaration --
13401 ---------------------------------
13403 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13404 Comp : Entity_Id;
13406 begin
13407 if Is_Record_Type (Typ) then
13408 Comp := First_Component (Typ);
13409 while Present (Comp) loop
13410 if Comes_From_Source (Comp) then
13411 if Present (Expression (Parent (Comp)))
13412 and then Is_Entity_Name (Expression (Parent (Comp)))
13413 and then Entity (Expression (Parent (Comp))) = Prev
13414 then
13415 Error_Msg_Sloc := Sloc (Parent (Comp));
13416 Error_Msg_NE
13417 ("illegal circularity with declaration for & #",
13418 N, Comp);
13419 return;
13421 elsif Is_Record_Type (Etype (Comp)) then
13422 Check_Recursive_Declaration (Etype (Comp));
13423 end if;
13424 end if;
13426 Next_Component (Comp);
13427 end loop;
13428 end if;
13429 end Check_Recursive_Declaration;
13431 -- Start of processing for Constant_Redeclaration
13433 begin
13434 if Nkind (Parent (Prev)) = N_Object_Declaration then
13435 if Nkind (Object_Definition
13436 (Parent (Prev))) = N_Subtype_Indication
13437 then
13438 -- Find type of new declaration. The constraints of the two
13439 -- views must match statically, but there is no point in
13440 -- creating an itype for the full view.
13442 if Nkind (Obj_Def) = N_Subtype_Indication then
13443 Find_Type (Subtype_Mark (Obj_Def));
13444 New_T := Entity (Subtype_Mark (Obj_Def));
13446 else
13447 Find_Type (Obj_Def);
13448 New_T := Entity (Obj_Def);
13449 end if;
13451 T := Etype (Prev);
13453 else
13454 -- The full view may impose a constraint, even if the partial
13455 -- view does not, so construct the subtype.
13457 New_T := Find_Type_Of_Object (Obj_Def, N);
13458 T := New_T;
13459 end if;
13461 else
13462 -- Current declaration is illegal, diagnosed below in Enter_Name
13464 T := Empty;
13465 New_T := Any_Type;
13466 end if;
13468 -- If previous full declaration or a renaming declaration exists, or if
13469 -- a homograph is present, let Enter_Name handle it, either with an
13470 -- error or with the removal of an overridden implicit subprogram.
13471 -- The previous one is a full declaration if it has an expression
13472 -- (which in the case of an aggregate is indicated by the Init flag).
13474 if Ekind (Prev) /= E_Constant
13475 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13476 or else Present (Expression (Parent (Prev)))
13477 or else Has_Init_Expression (Parent (Prev))
13478 or else Present (Full_View (Prev))
13479 then
13480 Enter_Name (Id);
13482 -- Verify that types of both declarations match, or else that both types
13483 -- are anonymous access types whose designated subtypes statically match
13484 -- (as allowed in Ada 2005 by AI-385).
13486 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13487 and then
13488 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13489 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13490 or else Is_Access_Constant (Etype (New_T)) /=
13491 Is_Access_Constant (Etype (Prev))
13492 or else Can_Never_Be_Null (Etype (New_T)) /=
13493 Can_Never_Be_Null (Etype (Prev))
13494 or else Null_Exclusion_Present (Parent (Prev)) /=
13495 Null_Exclusion_Present (Parent (Id))
13496 or else not Subtypes_Statically_Match
13497 (Designated_Type (Etype (Prev)),
13498 Designated_Type (Etype (New_T))))
13499 then
13500 Error_Msg_Sloc := Sloc (Prev);
13501 Error_Msg_N ("type does not match declaration#", N);
13502 Set_Full_View (Prev, Id);
13503 Set_Etype (Id, Any_Type);
13505 -- A deferred constant whose type is an anonymous array is always
13506 -- illegal (unless imported). A detailed error message might be
13507 -- helpful for Ada beginners.
13509 if Nkind (Object_Definition (Parent (Prev)))
13510 = N_Constrained_Array_Definition
13511 and then Nkind (Object_Definition (N))
13512 = N_Constrained_Array_Definition
13513 then
13514 Error_Msg_N ("\each anonymous array is a distinct type", N);
13515 Error_Msg_N ("a deferred constant must have a named type",
13516 Object_Definition (Parent (Prev)));
13517 end if;
13519 elsif
13520 Null_Exclusion_Present (Parent (Prev))
13521 and then not Null_Exclusion_Present (N)
13522 then
13523 Error_Msg_Sloc := Sloc (Prev);
13524 Error_Msg_N ("null-exclusion does not match declaration#", N);
13525 Set_Full_View (Prev, Id);
13526 Set_Etype (Id, Any_Type);
13528 -- If so, process the full constant declaration
13530 else
13531 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13532 -- the deferred declaration is constrained, then the subtype defined
13533 -- by the subtype_indication in the full declaration shall match it
13534 -- statically.
13536 Check_Possible_Deferred_Completion
13537 (Prev_Id => Prev,
13538 Curr_Obj_Def => Obj_Def);
13540 Set_Full_View (Prev, Id);
13541 Set_Is_Public (Id, Is_Public (Prev));
13542 Set_Is_Internal (Id);
13543 Append_Entity (Id, Current_Scope);
13545 -- Check ALIASED present if present before (RM 7.4(7))
13547 if Is_Aliased (Prev)
13548 and then not Aliased_Present (N)
13549 then
13550 Error_Msg_Sloc := Sloc (Prev);
13551 Error_Msg_N ("ALIASED required (see declaration #)", N);
13552 end if;
13554 -- Check that placement is in private part and that the incomplete
13555 -- declaration appeared in the visible part.
13557 if Ekind (Current_Scope) = E_Package
13558 and then not In_Private_Part (Current_Scope)
13559 then
13560 Error_Msg_Sloc := Sloc (Prev);
13561 Error_Msg_N
13562 ("full constant for declaration # must be in private part", N);
13564 elsif Ekind (Current_Scope) = E_Package
13565 and then
13566 List_Containing (Parent (Prev)) /=
13567 Visible_Declarations (Package_Specification (Current_Scope))
13568 then
13569 Error_Msg_N
13570 ("deferred constant must be declared in visible part",
13571 Parent (Prev));
13572 end if;
13574 if Is_Access_Type (T)
13575 and then Nkind (Expression (N)) = N_Allocator
13576 then
13577 Check_Recursive_Declaration (Designated_Type (T));
13578 end if;
13580 -- A deferred constant is a visible entity. If type has invariants,
13581 -- verify that the initial value satisfies them. This is not done in
13582 -- GNATprove mode, as GNATprove handles invariant checks itself.
13584 if Has_Invariants (T)
13585 and then Present (Invariant_Procedure (T))
13586 and then not GNATprove_Mode
13587 then
13588 Insert_After (N,
13589 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13590 end if;
13591 end if;
13592 end Constant_Redeclaration;
13594 ----------------------
13595 -- Constrain_Access --
13596 ----------------------
13598 procedure Constrain_Access
13599 (Def_Id : in out Entity_Id;
13600 S : Node_Id;
13601 Related_Nod : Node_Id)
13603 T : constant Entity_Id := Entity (Subtype_Mark (S));
13604 Desig_Type : constant Entity_Id := Designated_Type (T);
13605 Desig_Subtype : Entity_Id;
13606 Constraint_OK : Boolean := True;
13608 begin
13609 if Is_Array_Type (Desig_Type) then
13610 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13611 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13613 elsif (Is_Record_Type (Desig_Type)
13614 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13615 and then not Is_Constrained (Desig_Type)
13616 then
13617 -- If this is a constrained access definition for a record
13618 -- component, we leave the type as an unconstrained access,
13619 -- and mark the component so that its actual type is built
13620 -- at a point of use (e.g., an assignment statement). This
13621 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13623 if Desig_Type = Current_Scope
13624 and then No (Def_Id)
13625 then
13626 Desig_Subtype :=
13627 Create_Itype
13628 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13629 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13630 Def_Id := Entity (Subtype_Mark (S));
13632 -- We indicate that the component has a per-object constraint
13633 -- for treatment at a point of use, even though the constraint
13634 -- may be independent of discriminants of the enclosing type.
13636 if Nkind (Related_Nod) = N_Component_Declaration then
13637 Set_Has_Per_Object_Constraint
13638 (Defining_Identifier (Related_Nod));
13639 end if;
13641 -- This call added to ensure that the constraint is analyzed
13642 -- (needed for a B test). Note that we still return early from
13643 -- this procedure to avoid recursive processing.
13645 Constrain_Discriminated_Type
13646 (Desig_Subtype, S, Related_Nod, For_Access => True);
13647 return;
13648 end if;
13650 -- Enforce rule that the constraint is illegal if there is an
13651 -- unconstrained view of the designated type. This means that the
13652 -- partial view (either a private type declaration or a derivation
13653 -- from a private type) has no discriminants. (Defect Report
13654 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13656 -- Rule updated for Ada 2005: The private type is said to have
13657 -- a constrained partial view, given that objects of the type
13658 -- can be declared. Furthermore, the rule applies to all access
13659 -- types, unlike the rule concerning default discriminants (see
13660 -- RM 3.7.1(7/3))
13662 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13663 and then Has_Private_Declaration (Desig_Type)
13664 and then In_Open_Scopes (Scope (Desig_Type))
13665 and then Has_Discriminants (Desig_Type)
13666 then
13667 declare
13668 Pack : constant Node_Id :=
13669 Unit_Declaration_Node (Scope (Desig_Type));
13670 Decls : List_Id;
13671 Decl : Node_Id;
13673 begin
13674 if Nkind (Pack) = N_Package_Declaration then
13675 Decls := Visible_Declarations (Specification (Pack));
13676 Decl := First (Decls);
13677 while Present (Decl) loop
13678 if (Nkind (Decl) = N_Private_Type_Declaration
13679 and then Chars (Defining_Identifier (Decl)) =
13680 Chars (Desig_Type))
13682 or else
13683 (Nkind (Decl) = N_Full_Type_Declaration
13684 and then
13685 Chars (Defining_Identifier (Decl)) =
13686 Chars (Desig_Type)
13687 and then Is_Derived_Type (Desig_Type)
13688 and then
13689 Has_Private_Declaration (Etype (Desig_Type)))
13690 then
13691 if No (Discriminant_Specifications (Decl)) then
13692 Error_Msg_N
13693 ("cannot constrain access type if designated "
13694 & "type has constrained partial view", S);
13695 end if;
13697 exit;
13698 end if;
13700 Next (Decl);
13701 end loop;
13702 end if;
13703 end;
13704 end if;
13706 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13707 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13708 For_Access => True);
13710 elsif Is_Concurrent_Type (Desig_Type)
13711 and then not Is_Constrained (Desig_Type)
13712 then
13713 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13714 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13716 else
13717 Error_Msg_N ("invalid constraint on access type", S);
13719 -- We simply ignore an invalid constraint
13721 Desig_Subtype := Desig_Type;
13722 Constraint_OK := False;
13723 end if;
13725 if No (Def_Id) then
13726 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13727 else
13728 Mutate_Ekind (Def_Id, E_Access_Subtype);
13729 end if;
13731 if Constraint_OK then
13732 Set_Etype (Def_Id, Base_Type (T));
13734 if Is_Private_Type (Desig_Type) then
13735 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13736 end if;
13737 else
13738 Set_Etype (Def_Id, Any_Type);
13739 end if;
13741 Set_Size_Info (Def_Id, T);
13742 Set_Is_Constrained (Def_Id, Constraint_OK);
13743 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13744 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13745 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13746 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13748 Conditional_Delay (Def_Id, T);
13750 -- AI-363 : Subtypes of general access types whose designated types have
13751 -- default discriminants are disallowed. In instances, the rule has to
13752 -- be checked against the actual, of which T is the subtype. In a
13753 -- generic body, the rule is checked assuming that the actual type has
13754 -- defaulted discriminants.
13756 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13757 if Ekind (Base_Type (T)) = E_General_Access_Type
13758 and then Has_Defaulted_Discriminants (Desig_Type)
13759 then
13760 if Ada_Version < Ada_2005 then
13761 Error_Msg_N
13762 ("access subtype of general access type would not " &
13763 "be allowed in Ada 2005?y?", S);
13764 else
13765 Error_Msg_N
13766 ("access subtype of general access type not allowed", S);
13767 end if;
13769 Error_Msg_N ("\discriminants have defaults", S);
13771 elsif Is_Access_Type (T)
13772 and then Is_Generic_Type (Desig_Type)
13773 and then Has_Discriminants (Desig_Type)
13774 and then In_Package_Body (Current_Scope)
13775 then
13776 if Ada_Version < Ada_2005 then
13777 Error_Msg_N
13778 ("access subtype would not be allowed in generic body "
13779 & "in Ada 2005?y?", S);
13780 else
13781 Error_Msg_N
13782 ("access subtype not allowed in generic body", S);
13783 end if;
13785 Error_Msg_N
13786 ("\designated type is a discriminated formal", S);
13787 end if;
13788 end if;
13789 end Constrain_Access;
13791 ---------------------
13792 -- Constrain_Array --
13793 ---------------------
13795 procedure Constrain_Array
13796 (Def_Id : in out Entity_Id;
13797 SI : Node_Id;
13798 Related_Nod : Node_Id;
13799 Related_Id : Entity_Id;
13800 Suffix : Character)
13802 C : constant Node_Id := Constraint (SI);
13803 Number_Of_Constraints : constant Nat := List_Length (Constraints (C));
13804 Index : Node_Id;
13805 S, T : Entity_Id;
13806 Constraint_OK : Boolean := True;
13807 Is_FLB_Array_Subtype : Boolean := False;
13809 begin
13810 T := Entity (Subtype_Mark (SI));
13812 if Is_Access_Type (T) then
13813 T := Designated_Type (T);
13814 end if;
13816 T := Underlying_Type (T);
13818 -- If an index constraint follows a subtype mark in a subtype indication
13819 -- then the type or subtype denoted by the subtype mark must not already
13820 -- impose an index constraint. The subtype mark must denote either an
13821 -- unconstrained array type or an access type whose designated type
13822 -- is such an array type... (RM 3.6.1)
13824 if Is_Constrained (T) then
13825 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13826 Constraint_OK := False;
13828 else
13829 -- In either case, the index constraint must provide a discrete
13830 -- range for each index of the array type and the type of each
13831 -- discrete range must be the same as that of the corresponding
13832 -- index. (RM 3.6.1)
13834 if Number_Of_Constraints /= Number_Dimensions (T) then
13835 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13836 Constraint_OK := False;
13838 else
13839 S := First (Constraints (C));
13840 Index := First_Index (T);
13841 Analyze (Index);
13843 -- Apply constraints to each index type
13845 for J in 1 .. Number_Of_Constraints loop
13846 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13848 -- If the subtype of the index has been set to indicate that
13849 -- it has a fixed lower bound, then record that the subtype's
13850 -- entity will need to be marked as being a fixed-lower-bound
13851 -- array subtype.
13853 if S = First (Constraints (C)) then
13854 Is_FLB_Array_Subtype :=
13855 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13857 -- If the parent subtype (or should this be Etype of that?)
13858 -- is an FLB array subtype, we flag an error, because we
13859 -- don't currently allow subtypes of such subtypes to
13860 -- specify a fixed lower bound for any of their indexes,
13861 -- even if the index of the parent subtype is a "range <>"
13862 -- index.
13864 if Is_FLB_Array_Subtype
13865 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13866 then
13867 Error_Msg_NE
13868 ("index with fixed lower bound not allowed for subtype "
13869 & "of fixed-lower-bound }", S, T);
13871 Is_FLB_Array_Subtype := False;
13872 end if;
13874 elsif Is_FLB_Array_Subtype
13875 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13876 then
13877 Error_Msg_NE
13878 ("constrained index not allowed for fixed-lower-bound "
13879 & "subtype of}", S, T);
13881 elsif not Is_FLB_Array_Subtype
13882 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13883 then
13884 Error_Msg_NE
13885 ("index with fixed lower bound not allowed for "
13886 & "constrained subtype of}", S, T);
13887 end if;
13889 Next (Index);
13890 Next (S);
13891 end loop;
13893 end if;
13894 end if;
13896 if No (Def_Id) then
13897 Def_Id :=
13898 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13899 Set_Parent (Def_Id, Related_Nod);
13901 else
13902 Mutate_Ekind (Def_Id, E_Array_Subtype);
13903 end if;
13905 Set_Size_Info (Def_Id, (T));
13906 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13907 Set_Etype (Def_Id, Base_Type (T));
13909 if Constraint_OK then
13910 Set_First_Index (Def_Id, First (Constraints (C)));
13911 else
13912 Set_First_Index (Def_Id, First_Index (T));
13913 end if;
13915 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13916 Set_Is_Fixed_Lower_Bound_Array_Subtype
13917 (Def_Id, Is_FLB_Array_Subtype);
13918 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13919 Set_Is_Independent (Def_Id, Is_Independent (T));
13920 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13922 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13923 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13925 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13926 -- We need to initialize the attribute because if Def_Id is previously
13927 -- analyzed through a limited_with clause, it will have the attributes
13928 -- of an incomplete type, one of which is an Elist that overlaps the
13929 -- Packed_Array_Impl_Type field.
13931 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13933 -- Build a freeze node if parent still needs one. Also make sure that
13934 -- the Depends_On_Private status is set because the subtype will need
13935 -- reprocessing at the time the base type does, and also we must set a
13936 -- conditional delay.
13938 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13939 Conditional_Delay (Def_Id, T);
13940 end Constrain_Array;
13942 ------------------------------
13943 -- Constrain_Component_Type --
13944 ------------------------------
13946 function Constrain_Component_Type
13947 (Comp : Entity_Id;
13948 Constrained_Typ : Entity_Id;
13949 Related_Node : Node_Id;
13950 Typ : Entity_Id;
13951 Constraints : Elist_Id) return Entity_Id
13953 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13954 Compon_Type : constant Entity_Id := Etype (Comp);
13956 function Build_Constrained_Array_Type
13957 (Old_Type : Entity_Id) return Entity_Id;
13958 -- If Old_Type is an array type, one of whose indexes is constrained
13959 -- by a discriminant, build an Itype whose constraint replaces the
13960 -- discriminant with its value in the constraint.
13962 function Build_Constrained_Discriminated_Type
13963 (Old_Type : Entity_Id) return Entity_Id;
13964 -- Ditto for record components. Handle the case where the constraint
13965 -- is a conversion of the discriminant value, introduced during
13966 -- expansion.
13968 function Build_Constrained_Access_Type
13969 (Old_Type : Entity_Id) return Entity_Id;
13970 -- Ditto for access types. Makes use of previous two functions, to
13971 -- constrain designated type.
13973 function Is_Discriminant (Expr : Node_Id) return Boolean;
13974 -- Returns True if Expr is a discriminant
13976 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13977 -- Find the value of a discriminant named by Discr_Expr in Constraints
13979 -----------------------------------
13980 -- Build_Constrained_Access_Type --
13981 -----------------------------------
13983 function Build_Constrained_Access_Type
13984 (Old_Type : Entity_Id) return Entity_Id
13986 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13987 Itype : Entity_Id;
13988 Desig_Subtype : Entity_Id;
13989 Scop : Entity_Id;
13991 begin
13992 -- If the original access type was not embedded in the enclosing
13993 -- type definition, there is no need to produce a new access
13994 -- subtype. In fact every access type with an explicit constraint
13995 -- generates an itype whose scope is the enclosing record.
13997 if not Is_Type (Scope (Old_Type)) then
13998 return Old_Type;
14000 elsif Is_Array_Type (Desig_Type) then
14001 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
14003 elsif Has_Discriminants (Desig_Type) then
14005 -- This may be an access type to an enclosing record type for
14006 -- which we are constructing the constrained components. Return
14007 -- the enclosing record subtype. This is not always correct,
14008 -- but avoids infinite recursion. ???
14010 Desig_Subtype := Any_Type;
14012 for J in reverse 0 .. Scope_Stack.Last loop
14013 Scop := Scope_Stack.Table (J).Entity;
14015 if Is_Type (Scop)
14016 and then Base_Type (Scop) = Base_Type (Desig_Type)
14017 then
14018 Desig_Subtype := Scop;
14019 end if;
14021 exit when not Is_Type (Scop);
14022 end loop;
14024 if Desig_Subtype = Any_Type then
14025 Desig_Subtype :=
14026 Build_Constrained_Discriminated_Type (Desig_Type);
14027 end if;
14029 else
14030 return Old_Type;
14031 end if;
14033 if Desig_Subtype /= Desig_Type then
14035 -- The Related_Node better be here or else we won't be able
14036 -- to attach new itypes to a node in the tree.
14038 pragma Assert (Present (Related_Node));
14040 Itype := Create_Itype (E_Access_Subtype, Related_Node);
14042 Set_Etype (Itype, Base_Type (Old_Type));
14043 Set_Size_Info (Itype, (Old_Type));
14044 Set_Directly_Designated_Type (Itype, Desig_Subtype);
14045 Set_Depends_On_Private (Itype, Has_Private_Component
14046 (Old_Type));
14047 Set_Is_Access_Constant (Itype, Is_Access_Constant
14048 (Old_Type));
14050 -- The new itype needs freezing when it depends on a not frozen
14051 -- type and the enclosing subtype needs freezing.
14053 if Has_Delayed_Freeze (Constrained_Typ)
14054 and then not Is_Frozen (Constrained_Typ)
14055 then
14056 Conditional_Delay (Itype, Base_Type (Old_Type));
14057 end if;
14059 return Itype;
14061 else
14062 return Old_Type;
14063 end if;
14064 end Build_Constrained_Access_Type;
14066 ----------------------------------
14067 -- Build_Constrained_Array_Type --
14068 ----------------------------------
14070 function Build_Constrained_Array_Type
14071 (Old_Type : Entity_Id) return Entity_Id
14073 Lo_Expr : Node_Id;
14074 Hi_Expr : Node_Id;
14075 Old_Index : Node_Id;
14076 Range_Node : Node_Id;
14077 Constr_List : List_Id;
14079 Need_To_Create_Itype : Boolean := False;
14081 begin
14082 Old_Index := First_Index (Old_Type);
14083 while Present (Old_Index) loop
14084 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14086 if Is_Discriminant (Lo_Expr)
14087 or else
14088 Is_Discriminant (Hi_Expr)
14089 then
14090 Need_To_Create_Itype := True;
14091 exit;
14092 end if;
14094 Next_Index (Old_Index);
14095 end loop;
14097 if Need_To_Create_Itype then
14098 Constr_List := New_List;
14100 Old_Index := First_Index (Old_Type);
14101 while Present (Old_Index) loop
14102 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14104 if Is_Discriminant (Lo_Expr) then
14105 Lo_Expr := Get_Discr_Value (Lo_Expr);
14106 end if;
14108 if Is_Discriminant (Hi_Expr) then
14109 Hi_Expr := Get_Discr_Value (Hi_Expr);
14110 end if;
14112 Range_Node :=
14113 Make_Range
14114 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14116 Append (Range_Node, To => Constr_List);
14118 Next_Index (Old_Index);
14119 end loop;
14121 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14123 else
14124 return Old_Type;
14125 end if;
14126 end Build_Constrained_Array_Type;
14128 ------------------------------------------
14129 -- Build_Constrained_Discriminated_Type --
14130 ------------------------------------------
14132 function Build_Constrained_Discriminated_Type
14133 (Old_Type : Entity_Id) return Entity_Id
14135 Expr : Node_Id;
14136 Constr_List : List_Id;
14137 Old_Constraint : Elmt_Id;
14139 Need_To_Create_Itype : Boolean := False;
14141 begin
14142 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14143 while Present (Old_Constraint) loop
14144 Expr := Node (Old_Constraint);
14146 if Is_Discriminant (Expr) then
14147 Need_To_Create_Itype := True;
14148 exit;
14150 -- After expansion of discriminated task types, the value
14151 -- of the discriminant may be converted to a run-time type
14152 -- for restricted run-times. Propagate the value of the
14153 -- discriminant as well, so that e.g. the secondary stack
14154 -- component has a static constraint. Necessary for LLVM.
14156 elsif Nkind (Expr) = N_Type_Conversion
14157 and then Is_Discriminant (Expression (Expr))
14158 then
14159 Need_To_Create_Itype := True;
14160 exit;
14161 end if;
14163 Next_Elmt (Old_Constraint);
14164 end loop;
14166 if Need_To_Create_Itype then
14167 Constr_List := New_List;
14169 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14170 while Present (Old_Constraint) loop
14171 Expr := Node (Old_Constraint);
14173 if Is_Discriminant (Expr) then
14174 Expr := Get_Discr_Value (Expr);
14176 elsif Nkind (Expr) = N_Type_Conversion
14177 and then Is_Discriminant (Expression (Expr))
14178 then
14179 Expr := New_Copy_Tree (Expr);
14180 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14181 end if;
14183 Append (New_Copy_Tree (Expr), To => Constr_List);
14185 Next_Elmt (Old_Constraint);
14186 end loop;
14188 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14190 else
14191 return Old_Type;
14192 end if;
14193 end Build_Constrained_Discriminated_Type;
14195 ---------------------
14196 -- Get_Discr_Value --
14197 ---------------------
14199 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14200 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14201 -- Entity of a discriminant that appear as a standalone expression in
14202 -- the constraint of a component.
14204 D : Entity_Id;
14205 E : Elmt_Id;
14207 begin
14208 -- The discriminant may be declared for the type, in which case we
14209 -- find it by iterating over the list of discriminants. If the
14210 -- discriminant is inherited from a parent type, it appears as the
14211 -- corresponding discriminant of the current type. This will be the
14212 -- case when constraining an inherited component whose constraint is
14213 -- given by a discriminant of the parent.
14215 D := First_Discriminant (Typ);
14216 E := First_Elmt (Constraints);
14218 while Present (D) loop
14219 if D = Discr_Id
14220 or else D = CR_Discriminant (Discr_Id)
14221 or else Corresponding_Discriminant (D) = Discr_Id
14222 then
14223 return New_Copy_Tree (Node (E));
14224 end if;
14226 Next_Discriminant (D);
14227 Next_Elmt (E);
14228 end loop;
14230 -- The Corresponding_Discriminant mechanism is incomplete, because
14231 -- the correspondence between new and old discriminants is not one
14232 -- to one: one new discriminant can constrain several old ones. In
14233 -- that case, scan sequentially the stored_constraint, the list of
14234 -- discriminants of the parents, and the constraints.
14236 -- Previous code checked for the present of the Stored_Constraint
14237 -- list for the derived type, but did not use it at all. Should it
14238 -- be present when the component is a discriminated task type?
14240 if Is_Derived_Type (Typ)
14241 and then Scope (Discr_Id) = Etype (Typ)
14242 then
14243 D := First_Discriminant (Etype (Typ));
14244 E := First_Elmt (Constraints);
14245 while Present (D) loop
14246 if D = Discr_Id then
14247 return New_Copy_Tree (Node (E));
14248 end if;
14250 Next_Discriminant (D);
14251 Next_Elmt (E);
14252 end loop;
14253 end if;
14255 -- Something is wrong if we did not find the value
14257 raise Program_Error;
14258 end Get_Discr_Value;
14260 ---------------------
14261 -- Is_Discriminant --
14262 ---------------------
14264 function Is_Discriminant (Expr : Node_Id) return Boolean is
14265 Discrim_Scope : Entity_Id;
14267 begin
14268 if Denotes_Discriminant (Expr) then
14269 Discrim_Scope := Scope (Entity (Expr));
14271 -- Either we have a reference to one of Typ's discriminants,
14273 pragma Assert (Discrim_Scope = Typ
14275 -- or to the discriminants of the parent type, in the case
14276 -- of a derivation of a tagged type with variants.
14278 or else Discrim_Scope = Etype (Typ)
14279 or else Full_View (Discrim_Scope) = Etype (Typ)
14281 -- or same as above for the case where the discriminants
14282 -- were declared in Typ's private view.
14284 or else (Is_Private_Type (Discrim_Scope)
14285 and then Chars (Discrim_Scope) = Chars (Typ))
14287 -- or else we are deriving from the full view and the
14288 -- discriminant is declared in the private entity.
14290 or else (Is_Private_Type (Typ)
14291 and then Chars (Discrim_Scope) = Chars (Typ))
14293 -- Or we are constrained the corresponding record of a
14294 -- synchronized type that completes a private declaration.
14296 or else (Is_Concurrent_Record_Type (Typ)
14297 and then
14298 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14300 -- or we have a class-wide type, in which case make sure the
14301 -- discriminant found belongs to the root type.
14303 or else (Is_Class_Wide_Type (Typ)
14304 and then Etype (Typ) = Discrim_Scope));
14306 return True;
14307 end if;
14309 -- In all other cases we have something wrong
14311 return False;
14312 end Is_Discriminant;
14314 -- Start of processing for Constrain_Component_Type
14316 begin
14317 if Nkind (Parent (Comp)) = N_Component_Declaration
14318 and then Comes_From_Source (Parent (Comp))
14319 and then Comes_From_Source
14320 (Subtype_Indication (Component_Definition (Parent (Comp))))
14321 and then
14322 Is_Entity_Name
14323 (Subtype_Indication (Component_Definition (Parent (Comp))))
14324 then
14325 return Compon_Type;
14327 elsif Is_Array_Type (Compon_Type) then
14328 return Build_Constrained_Array_Type (Compon_Type);
14330 elsif Has_Discriminants (Compon_Type) then
14331 return Build_Constrained_Discriminated_Type (Compon_Type);
14333 elsif Is_Access_Type (Compon_Type) then
14334 return Build_Constrained_Access_Type (Compon_Type);
14336 else
14337 return Compon_Type;
14338 end if;
14339 end Constrain_Component_Type;
14341 --------------------------
14342 -- Constrain_Concurrent --
14343 --------------------------
14345 -- For concurrent types, the associated record value type carries the same
14346 -- discriminants, so when we constrain a concurrent type, we must constrain
14347 -- the corresponding record type as well.
14349 procedure Constrain_Concurrent
14350 (Def_Id : in out Entity_Id;
14351 SI : Node_Id;
14352 Related_Nod : Node_Id;
14353 Related_Id : Entity_Id;
14354 Suffix : Character)
14356 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14357 -- case of a private subtype (needed when only doing semantic analysis).
14359 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14360 T_Val : Entity_Id;
14362 begin
14363 if Is_Access_Type (T_Ent) then
14364 T_Ent := Designated_Type (T_Ent);
14365 end if;
14367 T_Val := Corresponding_Record_Type (T_Ent);
14369 if Present (T_Val) then
14371 if No (Def_Id) then
14372 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14374 -- Elaborate itype now, as it may be used in a subsequent
14375 -- synchronized operation in another scope.
14377 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14378 Build_Itype_Reference (Def_Id, Related_Nod);
14379 end if;
14380 end if;
14382 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14383 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14385 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14386 Set_Corresponding_Record_Type (Def_Id,
14387 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14389 else
14390 -- If there is no associated record, expansion is disabled and this
14391 -- is a generic context. Create a subtype in any case, so that
14392 -- semantic analysis can proceed.
14394 if No (Def_Id) then
14395 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14396 end if;
14398 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14399 end if;
14400 end Constrain_Concurrent;
14402 ------------------------------------
14403 -- Constrain_Corresponding_Record --
14404 ------------------------------------
14406 function Constrain_Corresponding_Record
14407 (Prot_Subt : Entity_Id;
14408 Corr_Rec : Entity_Id;
14409 Related_Nod : Node_Id) return Entity_Id
14411 T_Sub : constant Entity_Id :=
14412 Create_Itype
14413 (Ekind => E_Record_Subtype,
14414 Related_Nod => Related_Nod,
14415 Related_Id => Corr_Rec,
14416 Suffix => 'C',
14417 Suffix_Index => -1);
14419 begin
14420 Set_Etype (T_Sub, Corr_Rec);
14421 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14422 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14423 Set_Is_Constrained (T_Sub, True);
14424 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14425 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14426 Set_Direct_Primitive_Operations
14427 (T_Sub, Direct_Primitive_Operations (Corr_Rec));
14429 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14430 Set_Discriminant_Constraint
14431 (T_Sub, Discriminant_Constraint (Prot_Subt));
14432 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14433 Create_Constrained_Components
14434 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14435 end if;
14437 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14439 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14440 Conditional_Delay (T_Sub, Corr_Rec);
14442 else
14443 -- This is a component subtype: it will be frozen in the context of
14444 -- the enclosing record's init_proc, so that discriminant references
14445 -- are resolved to discriminals. (Note: we used to skip freezing
14446 -- altogether in that case, which caused errors downstream for
14447 -- components of a bit packed array type).
14449 Set_Has_Delayed_Freeze (T_Sub);
14450 end if;
14452 return T_Sub;
14453 end Constrain_Corresponding_Record;
14455 -----------------------
14456 -- Constrain_Decimal --
14457 -----------------------
14459 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14460 T : constant Entity_Id := Entity (Subtype_Mark (S));
14461 C : constant Node_Id := Constraint (S);
14462 Loc : constant Source_Ptr := Sloc (C);
14463 Range_Expr : Node_Id;
14464 Digits_Expr : Node_Id;
14465 Digits_Val : Uint;
14466 Bound_Val : Ureal;
14468 begin
14469 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14471 if Nkind (C) = N_Range_Constraint then
14472 Range_Expr := Range_Expression (C);
14473 Digits_Val := Digits_Value (T);
14475 else
14476 pragma Assert (Nkind (C) = N_Digits_Constraint);
14478 Digits_Expr := Digits_Expression (C);
14479 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14481 Check_Digits_Expression (Digits_Expr);
14482 Digits_Val := Expr_Value (Digits_Expr);
14484 if Digits_Val > Digits_Value (T) then
14485 Error_Msg_N
14486 ("digits expression is incompatible with subtype", C);
14487 Digits_Val := Digits_Value (T);
14488 end if;
14490 if Present (Range_Constraint (C)) then
14491 Range_Expr := Range_Expression (Range_Constraint (C));
14492 else
14493 Range_Expr := Empty;
14494 end if;
14495 end if;
14497 Set_Etype (Def_Id, Base_Type (T));
14498 Set_Size_Info (Def_Id, (T));
14499 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14500 Set_Delta_Value (Def_Id, Delta_Value (T));
14501 Set_Scale_Value (Def_Id, Scale_Value (T));
14502 Set_Small_Value (Def_Id, Small_Value (T));
14503 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14504 Set_Digits_Value (Def_Id, Digits_Val);
14506 -- Manufacture range from given digits value if no range present
14508 if No (Range_Expr) then
14509 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14510 Range_Expr :=
14511 Make_Range (Loc,
14512 Low_Bound =>
14513 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14514 High_Bound =>
14515 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14516 end if;
14518 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14519 Set_Discrete_RM_Size (Def_Id);
14521 -- Unconditionally delay the freeze, since we cannot set size
14522 -- information in all cases correctly until the freeze point.
14524 Set_Has_Delayed_Freeze (Def_Id);
14525 end Constrain_Decimal;
14527 ----------------------------------
14528 -- Constrain_Discriminated_Type --
14529 ----------------------------------
14531 procedure Constrain_Discriminated_Type
14532 (Def_Id : Entity_Id;
14533 S : Node_Id;
14534 Related_Nod : Node_Id;
14535 For_Access : Boolean := False)
14537 E : Entity_Id := Entity (Subtype_Mark (S));
14538 T : Entity_Id;
14540 procedure Fixup_Bad_Constraint;
14541 -- Called after finding a bad constraint, and after having posted an
14542 -- appropriate error message. The goal is to leave type Def_Id in as
14543 -- reasonable state as possible.
14545 --------------------------
14546 -- Fixup_Bad_Constraint --
14547 --------------------------
14549 procedure Fixup_Bad_Constraint is
14550 begin
14551 -- Set a reasonable Ekind for the entity, including incomplete types.
14553 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14555 -- Set Etype to the known type, to reduce chances of cascaded errors
14557 Set_Etype (Def_Id, E);
14558 Set_Error_Posted (Def_Id);
14559 end Fixup_Bad_Constraint;
14561 -- Local variables
14563 C : Node_Id;
14564 Constr : Elist_Id := New_Elmt_List;
14566 -- Start of processing for Constrain_Discriminated_Type
14568 begin
14569 C := Constraint (S);
14571 -- A discriminant constraint is only allowed in a subtype indication,
14572 -- after a subtype mark. This subtype mark must denote either a type
14573 -- with discriminants, or an access type whose designated type is a
14574 -- type with discriminants. A discriminant constraint specifies the
14575 -- values of these discriminants (RM 3.7.2(5)).
14577 T := Base_Type (Entity (Subtype_Mark (S)));
14579 if Is_Access_Type (T) then
14580 T := Designated_Type (T);
14581 end if;
14583 -- In an instance it may be necessary to retrieve the full view of a
14584 -- type with unknown discriminants, or a full view with defaulted
14585 -- discriminants. In other contexts the constraint is illegal.
14587 if In_Instance
14588 and then Is_Private_Type (T)
14589 and then Present (Full_View (T))
14590 and then
14591 (Has_Unknown_Discriminants (T)
14592 or else
14593 (not Has_Discriminants (T)
14594 and then Has_Defaulted_Discriminants (Full_View (T))))
14595 then
14596 T := Full_View (T);
14597 E := Full_View (E);
14598 end if;
14600 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14601 -- generating an error for access-to-incomplete subtypes.
14603 if Ada_Version >= Ada_2005
14604 and then Ekind (T) = E_Incomplete_Type
14605 and then Nkind (Parent (S)) = N_Subtype_Declaration
14606 and then not Is_Itype (Def_Id)
14607 then
14608 -- A little sanity check: emit an error message if the type has
14609 -- discriminants to begin with. Type T may be a regular incomplete
14610 -- type or imported via a limited with clause.
14612 if Has_Discriminants (T)
14613 or else (From_Limited_With (T)
14614 and then Present (Non_Limited_View (T))
14615 and then Nkind (Parent (Non_Limited_View (T))) =
14616 N_Full_Type_Declaration
14617 and then Present (Discriminant_Specifications
14618 (Parent (Non_Limited_View (T)))))
14619 then
14620 Error_Msg_N
14621 ("(Ada 2005) incomplete subtype may not be constrained", C);
14622 else
14623 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14624 end if;
14626 Fixup_Bad_Constraint;
14627 return;
14629 -- Check that the type has visible discriminants. The type may be
14630 -- a private type with unknown discriminants whose full view has
14631 -- discriminants which are invisible.
14633 elsif not Has_Discriminants (T)
14634 or else
14635 (Has_Unknown_Discriminants (T)
14636 and then Is_Private_Type (T))
14637 then
14638 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14639 Fixup_Bad_Constraint;
14640 return;
14642 elsif Is_Constrained (E)
14643 or else (Ekind (E) = E_Class_Wide_Subtype
14644 and then Present (Discriminant_Constraint (E)))
14645 then
14646 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14647 Fixup_Bad_Constraint;
14648 return;
14649 end if;
14651 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14652 -- applies to the base type.
14654 T := Base_Type (T);
14656 Constr := Build_Discriminant_Constraints (T, S);
14658 -- If the list returned was empty we had an error in building the
14659 -- discriminant constraint. We have also already signalled an error
14660 -- in the incomplete type case
14662 if Is_Empty_Elmt_List (Constr) then
14663 Fixup_Bad_Constraint;
14664 return;
14665 end if;
14667 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14668 end Constrain_Discriminated_Type;
14670 ---------------------------
14671 -- Constrain_Enumeration --
14672 ---------------------------
14674 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14675 T : constant Entity_Id := Entity (Subtype_Mark (S));
14676 C : constant Node_Id := Constraint (S);
14678 begin
14679 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14681 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14682 Set_Etype (Def_Id, Base_Type (T));
14683 Set_Size_Info (Def_Id, (T));
14684 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14685 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14687 -- Inherit the chain of representation items instead of replacing it
14688 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14689 -- the derived type as a subtype declaration and the former needs to
14690 -- preserve existing representation items (see Build_Derived_Type).
14692 Inherit_Rep_Item_Chain (Def_Id, T);
14694 Set_Discrete_RM_Size (Def_Id);
14695 end Constrain_Enumeration;
14697 ----------------------
14698 -- Constrain_Float --
14699 ----------------------
14701 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14702 T : constant Entity_Id := Entity (Subtype_Mark (S));
14703 C : Node_Id;
14704 D : Node_Id;
14705 Rais : Node_Id;
14707 begin
14708 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14710 Set_Etype (Def_Id, Base_Type (T));
14711 Set_Size_Info (Def_Id, (T));
14712 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14714 -- Process the constraint
14716 C := Constraint (S);
14718 -- Digits constraint present
14720 if Nkind (C) = N_Digits_Constraint then
14721 Check_Restriction (No_Obsolescent_Features, C);
14723 if Warn_On_Obsolescent_Feature then
14724 Error_Msg_N
14725 ("subtype digits constraint is an " &
14726 "obsolescent feature (RM J.3(8))?j?", C);
14727 end if;
14729 D := Digits_Expression (C);
14730 Analyze_And_Resolve (D, Any_Integer);
14731 Check_Digits_Expression (D);
14732 Set_Digits_Value (Def_Id, Expr_Value (D));
14734 -- Check that digits value is in range. Obviously we can do this
14735 -- at compile time, but it is strictly a runtime check, and of
14736 -- course there is an ACVC test that checks this.
14738 if Digits_Value (Def_Id) > Digits_Value (T) then
14739 Error_Msg_Uint_1 := Digits_Value (T);
14740 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14741 Rais :=
14742 Make_Raise_Constraint_Error (Sloc (D),
14743 Reason => CE_Range_Check_Failed);
14744 Insert_Action (Declaration_Node (Def_Id), Rais);
14745 end if;
14747 C := Range_Constraint (C);
14749 -- No digits constraint present
14751 else
14752 Set_Digits_Value (Def_Id, Digits_Value (T));
14753 end if;
14755 -- Range constraint present
14757 if Nkind (C) = N_Range_Constraint then
14758 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14760 -- No range constraint present
14762 else
14763 pragma Assert (No (C));
14764 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14765 end if;
14767 Set_Is_Constrained (Def_Id);
14768 end Constrain_Float;
14770 ---------------------
14771 -- Constrain_Index --
14772 ---------------------
14774 procedure Constrain_Index
14775 (Index : Node_Id;
14776 S : Node_Id;
14777 Related_Nod : Node_Id;
14778 Related_Id : Entity_Id;
14779 Suffix : Character;
14780 Suffix_Index : Pos)
14782 Def_Id : Entity_Id;
14783 R : Node_Id := Empty;
14784 T : constant Entity_Id := Etype (Index);
14785 Is_FLB_Index : Boolean := False;
14787 begin
14788 Def_Id :=
14789 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14790 Set_Etype (Def_Id, Base_Type (T));
14792 if Nkind (S) = N_Range
14793 or else
14794 (Nkind (S) = N_Attribute_Reference
14795 and then Attribute_Name (S) = Name_Range)
14796 then
14797 -- A Range attribute will be transformed into N_Range by Resolve
14799 -- If a range has an Empty upper bound, then remember that for later
14800 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14801 -- flag, and also set the upper bound of the range to the index
14802 -- subtype's upper bound rather than leaving it Empty. In truth,
14803 -- that upper bound corresponds to a box ("<>"), but it's convenient
14804 -- to set it to the upper bound to avoid needing to add special tests
14805 -- in various places for an Empty upper bound, and in any case it
14806 -- accurately characterizes the index's range of values.
14808 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14809 Is_FLB_Index := True;
14810 Set_High_Bound (S, Type_High_Bound (T));
14811 end if;
14813 R := S;
14815 Process_Range_Expr_In_Decl (R, T);
14817 if not Error_Posted (S)
14818 and then
14819 (Nkind (S) /= N_Range
14820 or else not Covers (T, (Etype (Low_Bound (S))))
14821 or else not Covers (T, (Etype (High_Bound (S)))))
14822 then
14823 if Base_Type (T) /= Any_Type
14824 and then Etype (Low_Bound (S)) /= Any_Type
14825 and then Etype (High_Bound (S)) /= Any_Type
14826 then
14827 Error_Msg_N ("range expected", S);
14828 end if;
14829 end if;
14831 elsif Nkind (S) = N_Subtype_Indication then
14833 -- The parser has verified that this is a discrete indication
14835 Resolve_Discrete_Subtype_Indication (S, T);
14836 Bad_Predicated_Subtype_Use
14837 ("subtype& has predicate, not allowed in index constraint",
14838 S, Entity (Subtype_Mark (S)));
14840 R := Range_Expression (Constraint (S));
14842 -- Capture values of bounds and generate temporaries for them if
14843 -- needed, since checks may cause duplication of the expressions
14844 -- which must not be reevaluated.
14846 -- The forced evaluation removes side effects from expressions, which
14847 -- should occur also in GNATprove mode. Otherwise, we end up with
14848 -- unexpected insertions of actions at places where this is not
14849 -- supposed to occur, e.g. on default parameters of a call.
14851 if Expander_Active or GNATprove_Mode then
14852 Force_Evaluation
14853 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14854 Force_Evaluation
14855 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14856 end if;
14858 elsif Nkind (S) = N_Discriminant_Association then
14860 -- Syntactically valid in subtype indication
14862 Error_Msg_N ("invalid index constraint", S);
14863 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14864 return;
14866 -- Subtype_Mark case, no anonymous subtypes to construct
14868 else
14869 Analyze (S);
14871 if Is_Entity_Name (S) then
14872 if not Is_Type (Entity (S)) then
14873 Error_Msg_N ("expect subtype mark for index constraint", S);
14875 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14876 Wrong_Type (S, Base_Type (T));
14878 -- Check error of subtype with predicate in index constraint
14880 else
14881 Bad_Predicated_Subtype_Use
14882 ("subtype& has predicate, not allowed in index constraint",
14883 S, Entity (S));
14884 end if;
14886 return;
14888 else
14889 Error_Msg_N ("invalid index constraint", S);
14890 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14891 return;
14892 end if;
14893 end if;
14895 -- Complete construction of the Itype
14897 if Is_Modular_Integer_Type (T) then
14898 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14900 elsif Is_Integer_Type (T) then
14901 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14903 else
14904 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14905 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14906 Set_First_Literal (Def_Id, First_Literal (T));
14907 end if;
14909 Set_Size_Info (Def_Id, (T));
14910 Copy_RM_Size (To => Def_Id, From => T);
14911 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14913 -- If this is a range for a fixed-lower-bound subtype, then set the
14914 -- index itype's low bound to the FLB and the index itype's upper bound
14915 -- to the high bound of the parent array type's index subtype. Also,
14916 -- mark the itype as an FLB index subtype.
14918 if Nkind (S) = N_Range and then Is_FLB_Index then
14919 Set_Scalar_Range
14920 (Def_Id,
14921 Make_Range (Sloc (S),
14922 Low_Bound => Low_Bound (S),
14923 High_Bound => Type_High_Bound (T)));
14924 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14926 else
14927 Set_Scalar_Range (Def_Id, R);
14928 end if;
14930 Set_Etype (S, Def_Id);
14931 Set_Discrete_RM_Size (Def_Id);
14932 end Constrain_Index;
14934 -----------------------
14935 -- Constrain_Integer --
14936 -----------------------
14938 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14939 T : constant Entity_Id := Entity (Subtype_Mark (S));
14940 C : constant Node_Id := Constraint (S);
14942 begin
14943 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14945 if Is_Modular_Integer_Type (T) then
14946 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14947 else
14948 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14949 end if;
14951 Set_Etype (Def_Id, Base_Type (T));
14952 Set_Size_Info (Def_Id, (T));
14953 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14954 Set_Discrete_RM_Size (Def_Id);
14955 end Constrain_Integer;
14957 ------------------------------
14958 -- Constrain_Ordinary_Fixed --
14959 ------------------------------
14961 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14962 T : constant Entity_Id := Entity (Subtype_Mark (S));
14963 C : Node_Id;
14964 D : Node_Id;
14965 Rais : Node_Id;
14967 begin
14968 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14969 Set_Etype (Def_Id, Base_Type (T));
14970 Set_Size_Info (Def_Id, (T));
14971 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14972 Set_Small_Value (Def_Id, Small_Value (T));
14974 -- Process the constraint
14976 C := Constraint (S);
14978 -- Delta constraint present
14980 if Nkind (C) = N_Delta_Constraint then
14981 Check_Restriction (No_Obsolescent_Features, C);
14983 if Warn_On_Obsolescent_Feature then
14984 Error_Msg_S
14985 ("subtype delta constraint is an " &
14986 "obsolescent feature (RM J.3(7))?j?");
14987 end if;
14989 D := Delta_Expression (C);
14990 Analyze_And_Resolve (D, Any_Real);
14991 Check_Delta_Expression (D);
14992 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14994 -- Check that delta value is in range. Obviously we can do this
14995 -- at compile time, but it is strictly a runtime check, and of
14996 -- course there is an ACVC test that checks this.
14998 if Delta_Value (Def_Id) < Delta_Value (T) then
14999 Error_Msg_N ("??delta value is too small", D);
15000 Rais :=
15001 Make_Raise_Constraint_Error (Sloc (D),
15002 Reason => CE_Range_Check_Failed);
15003 Insert_Action (Declaration_Node (Def_Id), Rais);
15004 end if;
15006 C := Range_Constraint (C);
15008 -- No delta constraint present
15010 else
15011 Set_Delta_Value (Def_Id, Delta_Value (T));
15012 end if;
15014 -- Range constraint present
15016 if Nkind (C) = N_Range_Constraint then
15017 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
15019 -- No range constraint present
15021 else
15022 pragma Assert (No (C));
15023 Set_Scalar_Range (Def_Id, Scalar_Range (T));
15024 end if;
15026 Set_Discrete_RM_Size (Def_Id);
15028 -- Unconditionally delay the freeze, since we cannot set size
15029 -- information in all cases correctly until the freeze point.
15031 Set_Has_Delayed_Freeze (Def_Id);
15032 end Constrain_Ordinary_Fixed;
15034 -----------------------
15035 -- Contain_Interface --
15036 -----------------------
15038 function Contain_Interface
15039 (Iface : Entity_Id;
15040 Ifaces : Elist_Id) return Boolean
15042 Iface_Elmt : Elmt_Id;
15044 begin
15045 if Present (Ifaces) then
15046 Iface_Elmt := First_Elmt (Ifaces);
15047 while Present (Iface_Elmt) loop
15048 if Node (Iface_Elmt) = Iface then
15049 return True;
15050 end if;
15052 Next_Elmt (Iface_Elmt);
15053 end loop;
15054 end if;
15056 return False;
15057 end Contain_Interface;
15059 ---------------------------
15060 -- Convert_Scalar_Bounds --
15061 ---------------------------
15063 procedure Convert_Scalar_Bounds
15064 (N : Node_Id;
15065 Parent_Type : Entity_Id;
15066 Derived_Type : Entity_Id;
15067 Loc : Source_Ptr)
15069 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
15071 Lo : Node_Id;
15072 Hi : Node_Id;
15073 Rng : Node_Id;
15075 begin
15076 -- Defend against previous errors
15078 if No (Scalar_Range (Derived_Type)) then
15079 Check_Error_Detected;
15080 return;
15081 end if;
15083 Lo := Build_Scalar_Bound
15084 (Type_Low_Bound (Derived_Type),
15085 Parent_Type, Implicit_Base);
15087 Hi := Build_Scalar_Bound
15088 (Type_High_Bound (Derived_Type),
15089 Parent_Type, Implicit_Base);
15091 Rng :=
15092 Make_Range (Loc,
15093 Low_Bound => Lo,
15094 High_Bound => Hi);
15096 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
15098 Set_Parent (Rng, N);
15099 Set_Scalar_Range (Derived_Type, Rng);
15101 -- Analyze the bounds
15103 Analyze_And_Resolve (Lo, Implicit_Base);
15104 Analyze_And_Resolve (Hi, Implicit_Base);
15106 -- Analyze the range itself, except that we do not analyze it if
15107 -- the bounds are real literals, and we have a fixed-point type.
15108 -- The reason for this is that we delay setting the bounds in this
15109 -- case till we know the final Small and Size values (see circuit
15110 -- in Freeze.Freeze_Fixed_Point_Type for further details).
15112 if Is_Fixed_Point_Type (Parent_Type)
15113 and then Nkind (Lo) = N_Real_Literal
15114 and then Nkind (Hi) = N_Real_Literal
15115 then
15116 return;
15118 -- Here we do the analysis of the range
15120 -- Note: we do this manually, since if we do a normal Analyze and
15121 -- Resolve call, there are problems with the conversions used for
15122 -- the derived type range.
15124 else
15125 Set_Etype (Rng, Implicit_Base);
15126 Set_Analyzed (Rng, True);
15127 end if;
15128 end Convert_Scalar_Bounds;
15130 -------------------
15131 -- Copy_And_Swap --
15132 -------------------
15134 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15135 begin
15136 -- Initialize new full declaration entity by copying the pertinent
15137 -- fields of the corresponding private declaration entity.
15139 -- We temporarily set Ekind to a value appropriate for a type to
15140 -- avoid assert failures in Einfo from checking for setting type
15141 -- attributes on something that is not a type. Ekind (Priv) is an
15142 -- appropriate choice, since it allowed the attributes to be set
15143 -- in the first place. This Ekind value will be modified later.
15145 Mutate_Ekind (Full, Ekind (Priv));
15147 -- Also set Etype temporarily to Any_Type, again, in the absence
15148 -- of errors, it will be properly reset, and if there are errors,
15149 -- then we want a value of Any_Type to remain.
15151 Set_Etype (Full, Any_Type);
15153 -- Now start copying attributes
15155 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15157 if Has_Discriminants (Full) then
15158 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15159 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15160 end if;
15162 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15163 Set_Homonym (Full, Homonym (Priv));
15164 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15165 Set_Is_Public (Full, Is_Public (Priv));
15166 Set_Is_Pure (Full, Is_Pure (Priv));
15167 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15168 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15169 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15170 Set_Has_Pragma_Unreferenced_Objects
15171 (Full, Has_Pragma_Unreferenced_Objects
15172 (Priv));
15174 Conditional_Delay (Full, Priv);
15176 if Is_Tagged_Type (Full) then
15177 Set_Direct_Primitive_Operations
15178 (Full, Direct_Primitive_Operations (Priv));
15179 Set_No_Tagged_Streams_Pragma
15180 (Full, No_Tagged_Streams_Pragma (Priv));
15182 if Is_Base_Type (Priv) then
15183 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15184 end if;
15185 end if;
15187 Set_Is_Volatile (Full, Is_Volatile (Priv));
15188 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15189 Set_Scope (Full, Scope (Priv));
15190 Set_Prev_Entity (Full, Prev_Entity (Priv));
15191 Set_Next_Entity (Full, Next_Entity (Priv));
15192 Set_First_Entity (Full, First_Entity (Priv));
15193 Set_Last_Entity (Full, Last_Entity (Priv));
15195 -- If access types have been recorded for later handling, keep them in
15196 -- the full view so that they get handled when the full view freeze
15197 -- node is expanded.
15199 if Present (Freeze_Node (Priv))
15200 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15201 then
15202 Ensure_Freeze_Node (Full);
15203 Set_Access_Types_To_Process
15204 (Freeze_Node (Full),
15205 Access_Types_To_Process (Freeze_Node (Priv)));
15206 end if;
15208 -- Swap the two entities. Now Private is the full type entity and Full
15209 -- is the private one. They will be swapped back at the end of the
15210 -- private part. This swapping ensures that the entity that is visible
15211 -- in the private part is the full declaration.
15213 Exchange_Entities (Priv, Full);
15214 Set_Is_Not_Self_Hidden (Priv);
15215 Append_Entity (Full, Scope (Full));
15216 end Copy_And_Swap;
15218 -------------------------------------
15219 -- Copy_Array_Base_Type_Attributes --
15220 -------------------------------------
15222 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15223 begin
15224 Set_Component_Alignment (T1, Component_Alignment (T2));
15225 Set_Component_Type (T1, Component_Type (T2));
15226 Set_Component_Size (T1, Component_Size (T2));
15227 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
15228 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15229 Propagate_Concurrent_Flags (T1, T2);
15230 Set_Is_Packed (T1, Is_Packed (T2));
15231 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15232 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15233 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15234 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15235 end Copy_Array_Base_Type_Attributes;
15237 -----------------------------------
15238 -- Copy_Array_Subtype_Attributes --
15239 -----------------------------------
15241 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15242 -- let it be recreated during freezing for the sake of better debug info.
15244 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15245 begin
15246 Set_Size_Info (T1, T2);
15248 Set_First_Index (T1, First_Index (T2));
15249 Set_Is_Aliased (T1, Is_Aliased (T2));
15250 Set_Is_Atomic (T1, Is_Atomic (T2));
15251 Set_Is_Independent (T1, Is_Independent (T2));
15252 Set_Is_Volatile (T1, Is_Volatile (T2));
15253 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15254 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15255 Set_Is_Constrained (T1, Is_Constrained (T2));
15256 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15257 Inherit_Rep_Item_Chain (T1, T2);
15258 Set_Convention (T1, Convention (T2));
15259 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15260 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15261 end Copy_Array_Subtype_Attributes;
15263 -----------------------------------
15264 -- Create_Constrained_Components --
15265 -----------------------------------
15267 procedure Create_Constrained_Components
15268 (Subt : Entity_Id;
15269 Decl_Node : Node_Id;
15270 Typ : Entity_Id;
15271 Constraints : Elist_Id)
15273 Loc : constant Source_Ptr := Sloc (Subt);
15274 Comp_List : constant Elist_Id := New_Elmt_List;
15275 Parent_Type : constant Entity_Id := Etype (Typ);
15277 Assoc_List : List_Id;
15278 Discr_Val : Elmt_Id;
15279 Errors : Boolean;
15280 New_C : Entity_Id;
15281 Old_C : Entity_Id;
15282 Is_Static : Boolean := True;
15283 Is_Compile_Time_Known : Boolean := True;
15285 procedure Collect_Fixed_Components (Typ : Entity_Id);
15286 -- Collect parent type components that do not appear in a variant part
15288 procedure Create_All_Components;
15289 -- Iterate over Comp_List to create the components of the subtype
15291 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15292 -- Creates a new component from Old_Compon, copying all the fields from
15293 -- it, including its Etype, inserts the new component in the Subt entity
15294 -- chain and returns the new component.
15296 function Is_Variant_Record (T : Entity_Id) return Boolean;
15297 -- If true, and discriminants are static, collect only components from
15298 -- variants selected by discriminant values.
15300 ------------------------------
15301 -- Collect_Fixed_Components --
15302 ------------------------------
15304 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15305 begin
15306 -- Build association list for discriminants, and find components of
15307 -- the variant part selected by the values of the discriminants.
15309 Assoc_List := New_List;
15311 Old_C := First_Discriminant (Typ);
15312 Discr_Val := First_Elmt (Constraints);
15313 while Present (Old_C) loop
15314 Append_To (Assoc_List,
15315 Make_Component_Association (Loc,
15316 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15317 Expression => New_Copy (Node (Discr_Val))));
15319 Next_Elmt (Discr_Val);
15320 Next_Discriminant (Old_C);
15321 end loop;
15323 -- The tag and the possible parent component are unconditionally in
15324 -- the subtype.
15326 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15327 Old_C := First_Component (Typ);
15328 while Present (Old_C) loop
15329 if Chars (Old_C) in Name_uTag | Name_uParent then
15330 Append_Elmt (Old_C, Comp_List);
15331 end if;
15333 Next_Component (Old_C);
15334 end loop;
15335 end if;
15336 end Collect_Fixed_Components;
15338 ---------------------------
15339 -- Create_All_Components --
15340 ---------------------------
15342 procedure Create_All_Components is
15343 Comp : Elmt_Id;
15345 begin
15346 Comp := First_Elmt (Comp_List);
15347 while Present (Comp) loop
15348 Old_C := Node (Comp);
15349 New_C := Create_Component (Old_C);
15351 Set_Etype
15352 (New_C,
15353 Constrain_Component_Type
15354 (Old_C, Subt, Decl_Node, Typ, Constraints));
15355 Set_Is_Public (New_C, Is_Public (Subt));
15357 Next_Elmt (Comp);
15358 end loop;
15359 end Create_All_Components;
15361 ----------------------
15362 -- Create_Component --
15363 ----------------------
15365 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15366 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15368 begin
15369 if Ekind (Old_Compon) = E_Discriminant
15370 and then Is_Completely_Hidden (Old_Compon)
15371 then
15372 -- This is a shadow discriminant created for a discriminant of
15373 -- the parent type, which needs to be present in the subtype.
15374 -- Give the shadow discriminant an internal name that cannot
15375 -- conflict with that of visible components.
15377 Set_Chars (New_Compon, New_Internal_Name ('C'));
15378 end if;
15380 -- Set the parent so we have a proper link for freezing etc. This is
15381 -- not a real parent pointer, since of course our parent does not own
15382 -- up to us and reference us, we are an illegitimate child of the
15383 -- original parent.
15385 Set_Parent (New_Compon, Parent (Old_Compon));
15387 -- We do not want this node marked as Comes_From_Source, since
15388 -- otherwise it would get first class status and a separate cross-
15389 -- reference line would be generated. Illegitimate children do not
15390 -- rate such recognition.
15392 Set_Comes_From_Source (New_Compon, False);
15394 -- But it is a real entity, and a birth certificate must be properly
15395 -- registered by entering it into the entity list, and setting its
15396 -- scope to the given subtype. This turns out to be useful for the
15397 -- LLVM code generator, but that scope is not used otherwise.
15399 Enter_Name (New_Compon);
15400 Set_Scope (New_Compon, Subt);
15402 return New_Compon;
15403 end Create_Component;
15405 -----------------------
15406 -- Is_Variant_Record --
15407 -----------------------
15409 function Is_Variant_Record (T : Entity_Id) return Boolean is
15410 Decl : constant Node_Id := Parent (T);
15411 begin
15412 return Nkind (Decl) = N_Full_Type_Declaration
15413 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15414 and then Present (Component_List (Type_Definition (Decl)))
15415 and then
15416 Present (Variant_Part (Component_List (Type_Definition (Decl))));
15417 end Is_Variant_Record;
15419 -- Start of processing for Create_Constrained_Components
15421 begin
15422 pragma Assert (Subt /= Base_Type (Subt));
15423 pragma Assert (Typ = Base_Type (Typ));
15425 Set_First_Entity (Subt, Empty);
15426 Set_Last_Entity (Subt, Empty);
15428 -- Check whether constraint is fully static, in which case we can
15429 -- optimize the list of components.
15431 Discr_Val := First_Elmt (Constraints);
15432 while Present (Discr_Val) loop
15433 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15434 Is_Static := False;
15436 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15437 Is_Compile_Time_Known := False;
15438 exit;
15439 end if;
15440 end if;
15442 Next_Elmt (Discr_Val);
15443 end loop;
15445 Set_Has_Static_Discriminants (Subt, Is_Static);
15447 Push_Scope (Subt);
15449 -- Inherit the discriminants of the parent type
15451 Add_Discriminants : declare
15452 Num_Disc : Nat;
15453 Num_Stor : Nat;
15455 begin
15456 Num_Disc := 0;
15457 Old_C := First_Discriminant (Typ);
15459 while Present (Old_C) loop
15460 Num_Disc := Num_Disc + 1;
15461 New_C := Create_Component (Old_C);
15462 Set_Is_Public (New_C, Is_Public (Subt));
15463 Next_Discriminant (Old_C);
15464 end loop;
15466 -- For an untagged derived subtype, the number of discriminants may
15467 -- be smaller than the number of inherited discriminants, because
15468 -- several of them may be renamed by a single new discriminant or
15469 -- constrained. In this case, add the hidden discriminants back into
15470 -- the subtype, because they need to be present if the optimizer of
15471 -- the GCC 4.x back-end decides to break apart assignments between
15472 -- objects using the parent view into member-wise assignments.
15474 Num_Stor := 0;
15476 if Is_Derived_Type (Typ)
15477 and then not Is_Tagged_Type (Typ)
15478 then
15479 Old_C := First_Stored_Discriminant (Typ);
15481 while Present (Old_C) loop
15482 Num_Stor := Num_Stor + 1;
15483 Next_Stored_Discriminant (Old_C);
15484 end loop;
15485 end if;
15487 if Num_Stor > Num_Disc then
15489 -- Find out multiple uses of new discriminants, and add hidden
15490 -- components for the extra renamed discriminants. We recognize
15491 -- multiple uses through the Corresponding_Discriminant of a
15492 -- new discriminant: if it constrains several old discriminants,
15493 -- this field points to the last one in the parent type. The
15494 -- stored discriminants of the derived type have the same name
15495 -- as those of the parent.
15497 declare
15498 Constr : Elmt_Id;
15499 New_Discr : Entity_Id;
15500 Old_Discr : Entity_Id;
15502 begin
15503 Constr := First_Elmt (Stored_Constraint (Typ));
15504 Old_Discr := First_Stored_Discriminant (Typ);
15505 while Present (Constr) loop
15506 if Is_Entity_Name (Node (Constr))
15507 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15508 then
15509 New_Discr := Entity (Node (Constr));
15511 if Chars (Corresponding_Discriminant (New_Discr)) /=
15512 Chars (Old_Discr)
15513 then
15514 -- The new discriminant has been used to rename a
15515 -- subsequent old discriminant. Introduce a shadow
15516 -- component for the current old discriminant.
15518 New_C := Create_Component (Old_Discr);
15519 Set_Original_Record_Component (New_C, Old_Discr);
15520 end if;
15522 else
15523 -- The constraint has eliminated the old discriminant.
15524 -- Introduce a shadow component.
15526 New_C := Create_Component (Old_Discr);
15527 Set_Original_Record_Component (New_C, Old_Discr);
15528 end if;
15530 Next_Elmt (Constr);
15531 Next_Stored_Discriminant (Old_Discr);
15532 end loop;
15533 end;
15534 end if;
15535 end Add_Discriminants;
15537 if Is_Compile_Time_Known
15538 and then Is_Variant_Record (Typ)
15539 then
15540 Collect_Fixed_Components (Typ);
15541 Gather_Components
15542 (Typ,
15543 Component_List (Type_Definition (Parent (Typ))),
15544 Governed_By => Assoc_List,
15545 Into => Comp_List,
15546 Report_Errors => Errors,
15547 Allow_Compile_Time => True);
15548 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15550 Create_All_Components;
15552 -- If the subtype declaration is created for a tagged type derivation
15553 -- with constraints, we retrieve the record definition of the parent
15554 -- type to select the components of the proper variant.
15556 elsif Is_Compile_Time_Known
15557 and then Is_Tagged_Type (Typ)
15558 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15559 and then
15560 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15561 and then Is_Variant_Record (Parent_Type)
15562 then
15563 Collect_Fixed_Components (Typ);
15564 Gather_Components
15565 (Typ,
15566 Component_List (Type_Definition (Parent (Parent_Type))),
15567 Governed_By => Assoc_List,
15568 Into => Comp_List,
15569 Report_Errors => Errors,
15570 Allow_Compile_Time => True);
15572 -- Note: previously there was a check at this point that no errors
15573 -- were detected. As a consequence of AI05-220 there may be an error
15574 -- if an inherited discriminant that controls a variant has a non-
15575 -- static constraint.
15577 -- If the tagged derivation has a type extension, collect all the
15578 -- new relevant components therein via Gather_Components.
15580 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15581 then
15582 Gather_Components
15583 (Typ,
15584 Component_List
15585 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15586 Governed_By => Assoc_List,
15587 Into => Comp_List,
15588 Report_Errors => Errors,
15589 Allow_Compile_Time => True,
15590 Include_Interface_Tag => True);
15591 end if;
15593 Create_All_Components;
15595 else
15596 -- If discriminants are not static, or if this is a multi-level type
15597 -- extension, we have to include all components of the parent type.
15599 Old_C := First_Component (Typ);
15600 while Present (Old_C) loop
15601 New_C := Create_Component (Old_C);
15603 Set_Etype
15604 (New_C,
15605 Constrain_Component_Type
15606 (Old_C, Subt, Decl_Node, Typ, Constraints));
15607 Set_Is_Public (New_C, Is_Public (Subt));
15609 Next_Component (Old_C);
15610 end loop;
15611 end if;
15613 End_Scope;
15614 end Create_Constrained_Components;
15616 ------------------------------------------
15617 -- Decimal_Fixed_Point_Type_Declaration --
15618 ------------------------------------------
15620 procedure Decimal_Fixed_Point_Type_Declaration
15621 (T : Entity_Id;
15622 Def : Node_Id)
15624 Loc : constant Source_Ptr := Sloc (Def);
15625 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15626 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15627 Max_Digits : constant Nat :=
15628 (if System_Max_Integer_Size = 128 then 38 else 18);
15629 -- Maximum number of digits that can be represented in an integer
15631 Implicit_Base : Entity_Id;
15632 Digs_Val : Uint;
15633 Delta_Val : Ureal;
15634 Scale_Val : Uint;
15635 Bound_Val : Ureal;
15637 begin
15638 Check_Restriction (No_Fixed_Point, Def);
15640 -- Create implicit base type
15642 Implicit_Base :=
15643 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15644 Set_Etype (Implicit_Base, Implicit_Base);
15646 -- Analyze and process delta expression
15648 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15650 Check_Delta_Expression (Delta_Expr);
15651 Delta_Val := Expr_Value_R (Delta_Expr);
15653 -- Check delta is power of 10, and determine scale value from it
15655 declare
15656 Val : Ureal;
15658 begin
15659 Scale_Val := Uint_0;
15660 Val := Delta_Val;
15662 if Val < Ureal_1 then
15663 while Val < Ureal_1 loop
15664 Val := Val * Ureal_10;
15665 Scale_Val := Scale_Val + 1;
15666 end loop;
15668 if Scale_Val > Max_Digits then
15669 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15670 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15671 Scale_Val := UI_From_Int (Max_Digits);
15672 end if;
15674 else
15675 while Val > Ureal_1 loop
15676 Val := Val / Ureal_10;
15677 Scale_Val := Scale_Val - 1;
15678 end loop;
15680 if Scale_Val < -Max_Digits then
15681 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15682 Error_Msg_N ("scale is less than minimum value of ^", Def);
15683 Scale_Val := UI_From_Int (-Max_Digits);
15684 end if;
15685 end if;
15687 if Val /= Ureal_1 then
15688 Error_Msg_N ("delta expression must be a power of 10", Def);
15689 Delta_Val := Ureal_10 ** (-Scale_Val);
15690 end if;
15691 end;
15693 -- Set delta, scale and small (small = delta for decimal type)
15695 Set_Delta_Value (Implicit_Base, Delta_Val);
15696 Set_Scale_Value (Implicit_Base, Scale_Val);
15697 Set_Small_Value (Implicit_Base, Delta_Val);
15699 -- Analyze and process digits expression
15701 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15702 Check_Digits_Expression (Digs_Expr);
15703 Digs_Val := Expr_Value (Digs_Expr);
15705 if Digs_Val > Max_Digits then
15706 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15707 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15708 Digs_Val := UI_From_Int (Max_Digits);
15709 end if;
15711 Set_Digits_Value (Implicit_Base, Digs_Val);
15712 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15714 -- Set range of base type from digits value for now. This will be
15715 -- expanded to represent the true underlying base range by Freeze.
15717 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15719 -- Note: We leave Esize unset for now, size will be set at freeze
15720 -- time. We have to do this for ordinary fixed-point, because the size
15721 -- depends on the specified small, and we might as well do the same for
15722 -- decimal fixed-point.
15724 pragma Assert (not Known_Esize (Implicit_Base));
15726 -- If there are bounds given in the declaration use them as the
15727 -- bounds of the first named subtype.
15729 if Present (Real_Range_Specification (Def)) then
15730 declare
15731 RRS : constant Node_Id := Real_Range_Specification (Def);
15732 Low : constant Node_Id := Low_Bound (RRS);
15733 High : constant Node_Id := High_Bound (RRS);
15734 Low_Val : Ureal;
15735 High_Val : Ureal;
15737 begin
15738 Analyze_And_Resolve (Low, Any_Real);
15739 Analyze_And_Resolve (High, Any_Real);
15740 Check_Real_Bound (Low);
15741 Check_Real_Bound (High);
15742 Low_Val := Expr_Value_R (Low);
15743 High_Val := Expr_Value_R (High);
15745 if Low_Val < (-Bound_Val) then
15746 Error_Msg_N
15747 ("range low bound too small for digits value", Low);
15748 Low_Val := -Bound_Val;
15749 end if;
15751 if High_Val > Bound_Val then
15752 Error_Msg_N
15753 ("range high bound too large for digits value", High);
15754 High_Val := Bound_Val;
15755 end if;
15757 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15758 end;
15760 -- If no explicit range, use range that corresponds to given
15761 -- digits value. This will end up as the final range for the
15762 -- first subtype.
15764 else
15765 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15766 end if;
15768 -- Complete entity for first subtype. The inheritance of the rep item
15769 -- chain ensures that SPARK-related pragmas are not clobbered when the
15770 -- decimal fixed point type acts as a full view of a private type.
15772 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15773 Set_Etype (T, Implicit_Base);
15774 Set_Size_Info (T, Implicit_Base);
15775 Inherit_Rep_Item_Chain (T, Implicit_Base);
15776 Set_Digits_Value (T, Digs_Val);
15777 Set_Delta_Value (T, Delta_Val);
15778 Set_Small_Value (T, Delta_Val);
15779 Set_Scale_Value (T, Scale_Val);
15780 Set_Is_Constrained (T);
15781 end Decimal_Fixed_Point_Type_Declaration;
15783 -----------------------------------
15784 -- Derive_Progenitor_Subprograms --
15785 -----------------------------------
15787 procedure Derive_Progenitor_Subprograms
15788 (Parent_Type : Entity_Id;
15789 Tagged_Type : Entity_Id)
15791 E : Entity_Id;
15792 Elmt : Elmt_Id;
15793 Iface : Entity_Id;
15794 Iface_Alias : Entity_Id;
15795 Iface_Elmt : Elmt_Id;
15796 Iface_Subp : Entity_Id;
15797 New_Subp : Entity_Id := Empty;
15798 Prim_Elmt : Elmt_Id;
15799 Subp : Entity_Id;
15800 Typ : Entity_Id;
15802 begin
15803 pragma Assert (Ada_Version >= Ada_2005
15804 and then Is_Record_Type (Tagged_Type)
15805 and then Is_Tagged_Type (Tagged_Type)
15806 and then Has_Interfaces (Tagged_Type));
15808 -- Step 1: Transfer to the full-view primitives associated with the
15809 -- partial-view that cover interface primitives. Conceptually this
15810 -- work should be done later by Process_Full_View; done here to
15811 -- simplify its implementation at later stages. It can be safely
15812 -- done here because interfaces must be visible in the partial and
15813 -- private view (RM 7.3(7.3/2)).
15815 -- Small optimization: This work is only required if the parent may
15816 -- have entities whose Alias attribute reference an interface primitive.
15817 -- Such a situation may occur if the parent is an abstract type and the
15818 -- primitive has not been yet overridden or if the parent is a generic
15819 -- formal type covering interfaces.
15821 -- If the tagged type is not abstract, it cannot have abstract
15822 -- primitives (the only entities in the list of primitives of
15823 -- non-abstract tagged types that can reference abstract primitives
15824 -- through its Alias attribute are the internal entities that have
15825 -- attribute Interface_Alias, and these entities are generated later
15826 -- by Add_Internal_Interface_Entities).
15828 if In_Private_Part (Current_Scope)
15829 and then (Is_Abstract_Type (Parent_Type)
15830 or else
15831 Is_Generic_Type (Parent_Type))
15832 then
15833 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15834 while Present (Elmt) loop
15835 Subp := Node (Elmt);
15837 -- At this stage it is not possible to have entities in the list
15838 -- of primitives that have attribute Interface_Alias.
15840 pragma Assert (No (Interface_Alias (Subp)));
15842 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15844 if Is_Interface (Typ) then
15845 E := Find_Primitive_Covering_Interface
15846 (Tagged_Type => Tagged_Type,
15847 Iface_Prim => Subp);
15849 if Present (E)
15850 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15851 then
15852 Replace_Elmt (Elmt, E);
15853 Remove_Homonym (Subp);
15854 end if;
15855 end if;
15857 Next_Elmt (Elmt);
15858 end loop;
15859 end if;
15861 -- Step 2: Add primitives of progenitors that are not implemented by
15862 -- parents of Tagged_Type.
15864 if Present (Interfaces (Base_Type (Tagged_Type))) then
15865 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15866 while Present (Iface_Elmt) loop
15867 Iface := Node (Iface_Elmt);
15869 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15870 while Present (Prim_Elmt) loop
15871 Iface_Subp := Node (Prim_Elmt);
15872 Iface_Alias := Ultimate_Alias (Iface_Subp);
15874 -- Exclude derivation of predefined primitives except those
15875 -- that come from source, or are inherited from one that comes
15876 -- from source. Required to catch declarations of equality
15877 -- operators of interfaces. For example:
15879 -- type Iface is interface;
15880 -- function "=" (Left, Right : Iface) return Boolean;
15882 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15883 or else Comes_From_Source (Iface_Alias)
15884 then
15885 E :=
15886 Find_Primitive_Covering_Interface
15887 (Tagged_Type => Tagged_Type,
15888 Iface_Prim => Iface_Subp);
15890 -- If not found we derive a new primitive leaving its alias
15891 -- attribute referencing the interface primitive.
15893 if No (E) then
15894 Derive_Subprogram
15895 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15897 -- Ada 2012 (AI05-0197): If the covering primitive's name
15898 -- differs from the name of the interface primitive then it
15899 -- is a private primitive inherited from a parent type. In
15900 -- such case, given that Tagged_Type covers the interface,
15901 -- the inherited private primitive becomes visible. For such
15902 -- purpose we add a new entity that renames the inherited
15903 -- private primitive.
15905 elsif Chars (E) /= Chars (Iface_Subp) then
15906 pragma Assert (Has_Suffix (E, 'P'));
15907 Derive_Subprogram
15908 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15909 Set_Alias (New_Subp, E);
15910 Set_Is_Abstract_Subprogram (New_Subp,
15911 Is_Abstract_Subprogram (E));
15913 -- Propagate to the full view interface entities associated
15914 -- with the partial view.
15916 elsif In_Private_Part (Current_Scope)
15917 and then Present (Alias (E))
15918 and then Alias (E) = Iface_Subp
15919 and then
15920 List_Containing (Parent (E)) /=
15921 Private_Declarations
15922 (Specification
15923 (Unit_Declaration_Node (Current_Scope)))
15924 then
15925 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15926 end if;
15927 end if;
15929 Next_Elmt (Prim_Elmt);
15930 end loop;
15932 Next_Elmt (Iface_Elmt);
15933 end loop;
15934 end if;
15935 end Derive_Progenitor_Subprograms;
15937 -----------------------
15938 -- Derive_Subprogram --
15939 -----------------------
15941 procedure Derive_Subprogram
15942 (New_Subp : out Entity_Id;
15943 Parent_Subp : Entity_Id;
15944 Derived_Type : Entity_Id;
15945 Parent_Type : Entity_Id;
15946 Actual_Subp : Entity_Id := Empty)
15948 Formal : Entity_Id;
15949 -- Formal parameter of parent primitive operation
15951 Formal_Of_Actual : Entity_Id;
15952 -- Formal parameter of actual operation, when the derivation is to
15953 -- create a renaming for a primitive operation of an actual in an
15954 -- instantiation.
15956 New_Formal : Entity_Id;
15957 -- Formal of inherited operation
15959 Visible_Subp : Entity_Id := Parent_Subp;
15961 function Is_Private_Overriding return Boolean;
15962 -- If Subp is a private overriding of a visible operation, the inherited
15963 -- operation derives from the overridden op (even though its body is the
15964 -- overriding one) and the inherited operation is visible now. See
15965 -- sem_disp to see the full details of the handling of the overridden
15966 -- subprogram, which is removed from the list of primitive operations of
15967 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15968 -- and used to diagnose abstract operations that need overriding in the
15969 -- derived type.
15971 procedure Replace_Type (Id, New_Id : Entity_Id);
15972 -- Set the Etype of New_Id to the appropriate subtype determined from
15973 -- the Etype of Id, following (RM 3.4 (18, 19, 20, 21)). Id is either
15974 -- the parent type's primitive subprogram or one of its formals, and
15975 -- New_Id is the corresponding entity for the derived type. When the
15976 -- Etype of Id is an anonymous access type, create a new access type
15977 -- designating the derived type.
15979 procedure Set_Derived_Name;
15980 -- This procedure sets the appropriate Chars name for New_Subp. This
15981 -- is normally just a copy of the parent name. An exception arises for
15982 -- type support subprograms, where the name is changed to reflect the
15983 -- name of the derived type, e.g. if type foo is derived from type bar,
15984 -- then a procedure barDA is derived with a name fooDA.
15986 ---------------------------
15987 -- Is_Private_Overriding --
15988 ---------------------------
15990 function Is_Private_Overriding return Boolean is
15991 Prev : Entity_Id;
15993 begin
15994 -- If the parent is not a dispatching operation there is no
15995 -- need to investigate overridings
15997 if not Is_Dispatching_Operation (Parent_Subp) then
15998 return False;
15999 end if;
16001 -- The visible operation that is overridden is a homonym of the
16002 -- parent subprogram. We scan the homonym chain to find the one
16003 -- whose alias is the subprogram we are deriving.
16005 Prev := Current_Entity (Parent_Subp);
16006 while Present (Prev) loop
16007 if Ekind (Prev) = Ekind (Parent_Subp)
16008 and then Alias (Prev) = Parent_Subp
16009 and then Scope (Parent_Subp) = Scope (Prev)
16010 and then not Is_Hidden (Prev)
16011 then
16012 Visible_Subp := Prev;
16013 return True;
16014 end if;
16016 Prev := Homonym (Prev);
16017 end loop;
16019 return False;
16020 end Is_Private_Overriding;
16022 ------------------
16023 -- Replace_Type --
16024 ------------------
16026 procedure Replace_Type (Id, New_Id : Entity_Id) is
16027 Id_Type : constant Entity_Id := Etype (Id);
16028 Par : constant Node_Id := Parent (Derived_Type);
16030 begin
16031 -- When the type is an anonymous access type, create a new access
16032 -- type designating the derived type. This itype must be elaborated
16033 -- at the point of the derivation, not on subsequent calls that may
16034 -- be out of the proper scope for Gigi, so we insert a reference to
16035 -- it after the derivation.
16037 if Ekind (Id_Type) = E_Anonymous_Access_Type then
16038 declare
16039 Acc_Type : Entity_Id;
16040 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
16042 begin
16043 if Ekind (Desig_Typ) = E_Record_Type_With_Private
16044 and then Present (Full_View (Desig_Typ))
16045 and then not Is_Private_Type (Parent_Type)
16046 then
16047 Desig_Typ := Full_View (Desig_Typ);
16048 end if;
16050 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
16052 -- Ada 2005 (AI-251): Handle also derivations of abstract
16053 -- interface primitives.
16055 or else (Is_Interface (Desig_Typ)
16056 and then not Is_Class_Wide_Type (Desig_Typ))
16057 then
16058 Acc_Type := New_Copy (Id_Type);
16059 Set_Etype (Acc_Type, Acc_Type);
16060 Set_Scope (Acc_Type, New_Subp);
16062 -- Set size of anonymous access type. If we have an access
16063 -- to an unconstrained array, this is a fat pointer, so it
16064 -- is sizes at twice addtress size.
16066 if Is_Array_Type (Desig_Typ)
16067 and then not Is_Constrained (Desig_Typ)
16068 then
16069 Init_Size (Acc_Type, 2 * System_Address_Size);
16071 -- Other cases use a thin pointer
16073 else
16074 Init_Size (Acc_Type, System_Address_Size);
16075 end if;
16077 -- Set remaining characterstics of anonymous access type
16079 Reinit_Alignment (Acc_Type);
16080 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
16082 Set_Etype (New_Id, Acc_Type);
16083 Set_Scope (New_Id, New_Subp);
16085 -- Create a reference to it
16087 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
16089 else
16090 Set_Etype (New_Id, Id_Type);
16091 end if;
16092 end;
16094 -- In Ada2012, a formal may have an incomplete type but the type
16095 -- derivation that inherits the primitive follows the full view.
16097 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
16098 or else
16099 (Ekind (Id_Type) = E_Record_Type_With_Private
16100 and then Present (Full_View (Id_Type))
16101 and then
16102 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
16103 or else
16104 (Ada_Version >= Ada_2012
16105 and then Ekind (Id_Type) = E_Incomplete_Type
16106 and then Full_View (Id_Type) = Parent_Type)
16107 then
16108 -- Constraint checks on formals are generated during expansion,
16109 -- based on the signature of the original subprogram. The bounds
16110 -- of the derived type are not relevant, and thus we can use
16111 -- the base type for the formals. However, the return type may be
16112 -- used in a context that requires that the proper static bounds
16113 -- be used (a case statement, for example) and for those cases
16114 -- we must use the derived type (first subtype), not its base.
16116 -- If the derived_type_definition has no constraints, we know that
16117 -- the derived type has the same constraints as the first subtype
16118 -- of the parent, and we can also use it rather than its base,
16119 -- which can lead to more efficient code.
16121 if Id_Type = Parent_Type then
16122 if Is_Scalar_Type (Parent_Type)
16123 and then
16124 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16125 then
16126 Set_Etype (New_Id, Derived_Type);
16128 elsif Nkind (Par) = N_Full_Type_Declaration
16129 and then
16130 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16131 and then
16132 Is_Entity_Name
16133 (Subtype_Indication (Type_Definition (Par)))
16134 then
16135 Set_Etype (New_Id, Derived_Type);
16137 else
16138 Set_Etype (New_Id, Base_Type (Derived_Type));
16139 end if;
16141 else
16142 Set_Etype (New_Id, Base_Type (Derived_Type));
16143 end if;
16145 else
16146 Set_Etype (New_Id, Id_Type);
16147 end if;
16148 end Replace_Type;
16150 ----------------------
16151 -- Set_Derived_Name --
16152 ----------------------
16154 procedure Set_Derived_Name is
16155 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16156 begin
16157 if Nm = TSS_Null then
16158 Set_Chars (New_Subp, Chars (Parent_Subp));
16159 else
16160 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16161 end if;
16162 end Set_Derived_Name;
16164 -- Start of processing for Derive_Subprogram
16166 begin
16167 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16168 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16169 Set_Is_Not_Self_Hidden (New_Subp);
16171 -- Check whether the inherited subprogram is a private operation that
16172 -- should be inherited but not yet made visible. Such subprograms can
16173 -- become visible at a later point (e.g., the private part of a public
16174 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16175 -- following predicate is true, then this is not such a private
16176 -- operation and the subprogram simply inherits the name of the parent
16177 -- subprogram. Note the special check for the names of controlled
16178 -- operations, which are currently exempted from being inherited with
16179 -- a hidden name because they must be findable for generation of
16180 -- implicit run-time calls.
16182 if not Is_Hidden (Parent_Subp)
16183 or else Is_Internal (Parent_Subp)
16184 or else Is_Private_Overriding
16185 or else Is_Internal_Name (Chars (Parent_Subp))
16186 or else (Is_Controlled (Parent_Type)
16187 and then Chars (Parent_Subp) in Name_Adjust
16188 | Name_Finalize
16189 | Name_Initialize)
16190 then
16191 Set_Derived_Name;
16193 -- An inherited dispatching equality will be overridden by an internally
16194 -- generated one, or by an explicit one, so preserve its name and thus
16195 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16196 -- private operation it may become invisible if the full view has
16197 -- progenitors, and the dispatch table will be malformed.
16198 -- We check that the type is limited to handle the anomalous declaration
16199 -- of Limited_Controlled, which is derived from a non-limited type, and
16200 -- which is handled specially elsewhere as well.
16202 elsif Chars (Parent_Subp) = Name_Op_Eq
16203 and then Is_Dispatching_Operation (Parent_Subp)
16204 and then Etype (Parent_Subp) = Standard_Boolean
16205 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16206 and then
16207 Etype (First_Formal (Parent_Subp)) =
16208 Etype (Next_Formal (First_Formal (Parent_Subp)))
16209 then
16210 Set_Derived_Name;
16212 -- If parent is hidden, this can be a regular derivation if the
16213 -- parent is immediately visible in a non-instantiating context,
16214 -- or if we are in the private part of an instance. This test
16215 -- should still be refined ???
16217 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16218 -- operation as a non-visible operation in cases where the parent
16219 -- subprogram might not be visible now, but was visible within the
16220 -- original generic, so it would be wrong to make the inherited
16221 -- subprogram non-visible now. (Not clear if this test is fully
16222 -- correct; are there any cases where we should declare the inherited
16223 -- operation as not visible to avoid it being overridden, e.g., when
16224 -- the parent type is a generic actual with private primitives ???)
16226 -- (they should be treated the same as other private inherited
16227 -- subprograms, but it's not clear how to do this cleanly). ???
16229 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16230 and then Is_Immediately_Visible (Parent_Subp)
16231 and then not In_Instance)
16232 or else In_Instance_Not_Visible
16233 then
16234 Set_Derived_Name;
16236 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16237 -- overrides an interface primitive because interface primitives
16238 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16240 elsif Ada_Version >= Ada_2005
16241 and then Is_Dispatching_Operation (Parent_Subp)
16242 and then Present (Covered_Interface_Op (Parent_Subp))
16243 then
16244 Set_Derived_Name;
16246 -- Otherwise, the type is inheriting a private operation, so enter it
16247 -- with a special name so it can't be overridden. See also below, where
16248 -- we check for this case, and if so avoid setting Requires_Overriding.
16250 else
16251 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16252 end if;
16254 Set_Parent (New_Subp, Parent (Derived_Type));
16256 if Present (Actual_Subp) then
16257 Replace_Type (Actual_Subp, New_Subp);
16258 else
16259 Replace_Type (Parent_Subp, New_Subp);
16260 end if;
16262 Conditional_Delay (New_Subp, Parent_Subp);
16264 -- If we are creating a renaming for a primitive operation of an
16265 -- actual of a generic derived type, we must examine the signature
16266 -- of the actual primitive, not that of the generic formal, which for
16267 -- example may be an interface. However the name and initial value
16268 -- of the inherited operation are those of the formal primitive.
16270 Formal := First_Formal (Parent_Subp);
16272 if Present (Actual_Subp) then
16273 Formal_Of_Actual := First_Formal (Actual_Subp);
16274 else
16275 Formal_Of_Actual := Empty;
16276 end if;
16278 while Present (Formal) loop
16279 New_Formal := New_Copy (Formal);
16281 -- Extra formals are not inherited from a limited interface parent
16282 -- since limitedness is not inherited in such case (AI-419) and this
16283 -- affects the extra formals.
16285 if Is_Limited_Interface (Parent_Type) then
16286 Set_Extra_Formal (New_Formal, Empty);
16287 Set_Extra_Accessibility (New_Formal, Empty);
16288 end if;
16290 -- Normally we do not go copying parents, but in the case of
16291 -- formals, we need to link up to the declaration (which is the
16292 -- parameter specification), and it is fine to link up to the
16293 -- original formal's parameter specification in this case.
16295 Set_Parent (New_Formal, Parent (Formal));
16296 Append_Entity (New_Formal, New_Subp);
16298 if Present (Formal_Of_Actual) then
16299 Replace_Type (Formal_Of_Actual, New_Formal);
16300 Next_Formal (Formal_Of_Actual);
16301 else
16302 Replace_Type (Formal, New_Formal);
16303 end if;
16305 Next_Formal (Formal);
16306 end loop;
16308 -- Extra formals are shared between the parent subprogram and this
16309 -- internal entity built by Derive_Subprogram (implicit in the above
16310 -- copy of formals), unless the parent type is a limited interface type;
16311 -- hence we must inherit also the reference to the first extra formal.
16312 -- When the parent type is an interface, the extra formals will be added
16313 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16315 if not Is_Limited_Interface (Parent_Type) then
16316 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16318 if Ekind (New_Subp) = E_Function then
16319 Set_Extra_Accessibility_Of_Result (New_Subp,
16320 Extra_Accessibility_Of_Result (Parent_Subp));
16321 end if;
16322 end if;
16324 -- If this derivation corresponds to a tagged generic actual, then
16325 -- primitive operations rename those of the actual. Otherwise the
16326 -- primitive operations rename those of the parent type, If the parent
16327 -- renames an intrinsic operator, so does the new subprogram. We except
16328 -- concatenation, which is always properly typed, and does not get
16329 -- expanded as other intrinsic operations.
16331 if No (Actual_Subp) then
16332 if Is_Intrinsic_Subprogram (Parent_Subp) then
16333 Set_Convention (New_Subp, Convention_Intrinsic);
16334 Set_Is_Intrinsic_Subprogram (New_Subp);
16336 if Present (Alias (Parent_Subp))
16337 and then Chars (Parent_Subp) /= Name_Op_Concat
16338 then
16339 Set_Alias (New_Subp, Alias (Parent_Subp));
16340 else
16341 Set_Alias (New_Subp, Parent_Subp);
16342 end if;
16344 else
16345 Set_Alias (New_Subp, Parent_Subp);
16346 end if;
16348 else
16349 Set_Alias (New_Subp, Actual_Subp);
16350 end if;
16352 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16354 -- Derived subprograms of a tagged type must inherit the convention
16355 -- of the parent subprogram (a requirement of AI95-117). Derived
16356 -- subprograms of untagged types simply get convention Ada by default.
16358 -- If the derived type is a tagged generic formal type with unknown
16359 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16361 -- However, if the type is derived from a generic formal, the further
16362 -- inherited subprogram has the convention of the non-generic ancestor.
16363 -- Otherwise there would be no way to override the operation.
16364 -- (This is subject to forthcoming ARG discussions).
16366 if Is_Tagged_Type (Derived_Type) then
16367 if Is_Generic_Type (Derived_Type)
16368 and then Has_Unknown_Discriminants (Derived_Type)
16369 then
16370 Set_Convention (New_Subp, Convention_Intrinsic);
16372 else
16373 if Is_Generic_Type (Parent_Type)
16374 and then Has_Unknown_Discriminants (Parent_Type)
16375 then
16376 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16377 else
16378 Set_Convention (New_Subp, Convention (Parent_Subp));
16379 end if;
16380 end if;
16381 end if;
16383 -- Predefined controlled operations retain their name even if the parent
16384 -- is hidden (see above), but they are not primitive operations if the
16385 -- ancestor is not visible, for example if the parent is a private
16386 -- extension completed with a controlled extension. Note that a full
16387 -- type that is controlled can break privacy: the flag Is_Controlled is
16388 -- set on both views of the type.
16390 if Is_Controlled (Parent_Type)
16391 and then Chars (Parent_Subp) in Name_Initialize
16392 | Name_Adjust
16393 | Name_Finalize
16394 and then Is_Hidden (Parent_Subp)
16395 and then not Is_Visibly_Controlled (Parent_Type)
16396 then
16397 Set_Is_Hidden (New_Subp);
16398 end if;
16400 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16401 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16403 if Ekind (Parent_Subp) = E_Procedure then
16404 Set_Is_Valued_Procedure
16405 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16406 else
16407 Set_Has_Controlling_Result
16408 (New_Subp, Has_Controlling_Result (Parent_Subp));
16409 end if;
16411 -- No_Return must be inherited properly. If this is overridden in the
16412 -- case of a dispatching operation, then the check is made later in
16413 -- Check_Abstract_Overriding that the overriding operation is also
16414 -- No_Return (no such check is required for the nondispatching case).
16416 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16418 -- If the parent subprogram is marked as Ghost, then so is the derived
16419 -- subprogram. The ghost policy for the derived subprogram is set from
16420 -- the effective ghost policy at the point of derived type declaration.
16422 if Is_Ghost_Entity (Parent_Subp) then
16423 Set_Is_Ghost_Entity (New_Subp);
16424 end if;
16426 -- A derived function with a controlling result is abstract. If the
16427 -- Derived_Type is a nonabstract formal generic derived type, then
16428 -- inherited operations are not abstract: the required check is done at
16429 -- instantiation time. If the derivation is for a generic actual, the
16430 -- function is not abstract unless the actual is.
16432 if Is_Generic_Type (Derived_Type)
16433 and then not Is_Abstract_Type (Derived_Type)
16434 then
16435 null;
16437 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16438 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16439 -- that functions with controlling access results of record extensions
16440 -- with a null extension part require overriding (AI95-00391/06).
16442 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16443 -- implementing the rule of RM 7.3.2(6.1/4).
16445 -- A subprogram subject to pragma Extensions_Visible with value False
16446 -- requires overriding if the subprogram has at least one controlling
16447 -- OUT parameter (SPARK RM 6.1.7(6)).
16449 elsif Ada_Version >= Ada_2005
16450 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16451 or else (Is_Tagged_Type (Derived_Type)
16452 and then Etype (New_Subp) = Derived_Type
16453 and then not Is_Null_Extension (Derived_Type))
16454 or else (Is_Tagged_Type (Derived_Type)
16455 and then Ekind (Etype (New_Subp)) =
16456 E_Anonymous_Access_Type
16457 and then Designated_Type (Etype (New_Subp)) =
16458 Derived_Type)
16459 or else (Comes_From_Source (Alias (New_Subp))
16460 and then Is_EVF_Procedure (Alias (New_Subp)))
16462 -- AI12-0042: Set Requires_Overriding when a type extension
16463 -- inherits a private operation that is visible at the
16464 -- point of extension (Has_Private_Ancestor is False) from
16465 -- an ancestor that has Type_Invariant'Class, and when the
16466 -- type extension is in a visible part (the latter as
16467 -- clarified by AI12-0382).
16469 or else
16470 (not Has_Private_Ancestor (Derived_Type)
16471 and then Has_Invariants (Parent_Type)
16472 and then
16473 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16474 and then
16475 Class_Present
16476 (Get_Pragma (Parent_Type, Pragma_Invariant))
16477 and then Is_Private_Primitive (Parent_Subp)
16478 and then In_Visible_Part (Scope (Derived_Type))))
16480 and then No (Actual_Subp)
16481 then
16482 if not Is_Tagged_Type (Derived_Type)
16483 or else Is_Abstract_Type (Derived_Type)
16484 or else Is_Abstract_Subprogram (Alias (New_Subp))
16485 then
16486 Set_Is_Abstract_Subprogram (New_Subp);
16488 -- If the Chars of the new subprogram is different from that of the
16489 -- parent's one, it means that we entered it with a special name so
16490 -- it can't be overridden (see above). In that case we had better not
16491 -- *require* it to be overridden. This is the case where the parent
16492 -- type inherited the operation privately, so there's no danger of
16493 -- dangling dispatching.
16495 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16496 Set_Requires_Overriding (New_Subp);
16497 end if;
16499 elsif Ada_Version < Ada_2005
16500 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16501 or else (Is_Tagged_Type (Derived_Type)
16502 and then Etype (New_Subp) = Derived_Type
16503 and then No (Actual_Subp)))
16504 then
16505 Set_Is_Abstract_Subprogram (New_Subp);
16507 -- AI05-0097 : an inherited operation that dispatches on result is
16508 -- abstract if the derived type is abstract, even if the parent type
16509 -- is concrete and the derived type is a null extension.
16511 elsif Has_Controlling_Result (Alias (New_Subp))
16512 and then Is_Abstract_Type (Etype (New_Subp))
16513 then
16514 Set_Is_Abstract_Subprogram (New_Subp);
16516 -- Finally, if the parent type is abstract we must verify that all
16517 -- inherited operations are either non-abstract or overridden, or that
16518 -- the derived type itself is abstract (this check is performed at the
16519 -- end of a package declaration, in Check_Abstract_Overriding). A
16520 -- private overriding in the parent type will not be visible in the
16521 -- derivation if we are not in an inner package or in a child unit of
16522 -- the parent type, in which case the abstractness of the inherited
16523 -- operation is carried to the new subprogram.
16525 elsif Is_Abstract_Type (Parent_Type)
16526 and then not In_Open_Scopes (Scope (Parent_Type))
16527 and then Is_Private_Overriding
16528 and then Is_Abstract_Subprogram (Visible_Subp)
16529 then
16530 if No (Actual_Subp) then
16531 Set_Alias (New_Subp, Visible_Subp);
16532 Set_Is_Abstract_Subprogram (New_Subp, True);
16534 else
16535 -- If this is a derivation for an instance of a formal derived
16536 -- type, abstractness comes from the primitive operation of the
16537 -- actual, not from the operation inherited from the ancestor.
16539 Set_Is_Abstract_Subprogram
16540 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16541 end if;
16542 end if;
16544 New_Overloaded_Entity (New_Subp, Derived_Type);
16546 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16547 -- preconditions and the derived type is abstract, the derived operation
16548 -- is abstract as well if parent subprogram is not abstract or null.
16550 if Is_Abstract_Type (Derived_Type)
16551 and then Has_Non_Trivial_Precondition (Parent_Subp)
16552 and then Present (Interfaces (Derived_Type))
16553 then
16555 -- Add useful attributes of subprogram before the freeze point,
16556 -- in case freezing is delayed or there are previous errors.
16558 Set_Is_Dispatching_Operation (New_Subp);
16560 declare
16561 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16563 begin
16564 if Present (Iface_Prim)
16565 and then Has_Non_Trivial_Precondition (Iface_Prim)
16566 then
16567 Set_Is_Abstract_Subprogram (New_Subp);
16568 end if;
16569 end;
16570 end if;
16572 -- Check for case of a derived subprogram for the instantiation of a
16573 -- formal derived tagged type, if so mark the subprogram as dispatching
16574 -- and inherit the dispatching attributes of the actual subprogram. The
16575 -- derived subprogram is effectively renaming of the actual subprogram,
16576 -- so it needs to have the same attributes as the actual.
16578 if Present (Actual_Subp)
16579 and then Is_Dispatching_Operation (Actual_Subp)
16580 then
16581 Set_Is_Dispatching_Operation (New_Subp);
16583 if Present (DTC_Entity (Actual_Subp)) then
16584 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16585 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16586 end if;
16587 end if;
16589 -- Indicate that a derived subprogram does not require a body and that
16590 -- it does not require processing of default expressions.
16592 Set_Has_Completion (New_Subp);
16593 Set_Default_Expressions_Processed (New_Subp);
16595 if Ekind (New_Subp) = E_Function then
16596 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16597 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16598 end if;
16600 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16601 -- primitive subprogram S of a type T, then the aspect is inherited
16602 -- by the corresponding primitive subprogram of each descendant of T.
16604 if Is_Tagged_Type (Derived_Type)
16605 and then Is_Dispatching_Operation (New_Subp)
16606 and then Has_Yield_Aspect (Alias (New_Subp))
16607 then
16608 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16609 end if;
16611 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16612 end Derive_Subprogram;
16614 ------------------------
16615 -- Derive_Subprograms --
16616 ------------------------
16618 procedure Derive_Subprograms
16619 (Parent_Type : Entity_Id;
16620 Derived_Type : Entity_Id;
16621 Generic_Actual : Entity_Id := Empty)
16623 Op_List : constant Elist_Id :=
16624 Collect_Primitive_Operations (Parent_Type);
16626 function Check_Derived_Type return Boolean;
16627 -- Check that all the entities derived from Parent_Type are found in
16628 -- the list of primitives of Derived_Type exactly in the same order.
16630 procedure Derive_Interface_Subprogram
16631 (New_Subp : out Entity_Id;
16632 Subp : Entity_Id;
16633 Actual_Subp : Entity_Id);
16634 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16635 -- (which is an interface primitive). If Generic_Actual is present then
16636 -- Actual_Subp is the actual subprogram corresponding with the generic
16637 -- subprogram Subp.
16639 ------------------------
16640 -- Check_Derived_Type --
16641 ------------------------
16643 function Check_Derived_Type return Boolean is
16644 E : Entity_Id;
16645 Derived_Elmt : Elmt_Id;
16646 Derived_Op : Entity_Id;
16647 Derived_Ops : Elist_Id;
16648 Parent_Elmt : Elmt_Id;
16649 Parent_Op : Entity_Id;
16651 begin
16652 -- Traverse list of entities in the current scope searching for
16653 -- an incomplete type whose full-view is derived type.
16655 E := First_Entity (Scope (Derived_Type));
16656 while Present (E) and then E /= Derived_Type loop
16657 if Ekind (E) = E_Incomplete_Type
16658 and then Present (Full_View (E))
16659 and then Full_View (E) = Derived_Type
16660 then
16661 -- Disable this test if Derived_Type completes an incomplete
16662 -- type because in such case more primitives can be added
16663 -- later to the list of primitives of Derived_Type by routine
16664 -- Process_Incomplete_Dependents.
16666 return True;
16667 end if;
16669 Next_Entity (E);
16670 end loop;
16672 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16674 Derived_Elmt := First_Elmt (Derived_Ops);
16675 Parent_Elmt := First_Elmt (Op_List);
16676 while Present (Parent_Elmt) loop
16677 Parent_Op := Node (Parent_Elmt);
16678 Derived_Op := Node (Derived_Elmt);
16680 -- At this early stage Derived_Type has no entities with attribute
16681 -- Interface_Alias. In addition, such primitives are always
16682 -- located at the end of the list of primitives of Parent_Type.
16683 -- Therefore, if found we can safely stop processing pending
16684 -- entities.
16686 exit when Present (Interface_Alias (Parent_Op));
16688 -- Handle hidden entities
16690 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16691 and then Is_Hidden (Parent_Op)
16692 then
16693 if Present (Derived_Op)
16694 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16695 then
16696 Next_Elmt (Derived_Elmt);
16697 end if;
16699 else
16700 if No (Derived_Op)
16701 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16702 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16703 then
16704 return False;
16705 end if;
16707 Next_Elmt (Derived_Elmt);
16708 end if;
16710 Next_Elmt (Parent_Elmt);
16711 end loop;
16713 return True;
16714 end Check_Derived_Type;
16716 ---------------------------------
16717 -- Derive_Interface_Subprogram --
16718 ---------------------------------
16720 procedure Derive_Interface_Subprogram
16721 (New_Subp : out Entity_Id;
16722 Subp : Entity_Id;
16723 Actual_Subp : Entity_Id)
16725 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16726 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16728 begin
16729 pragma Assert (Is_Interface (Iface_Type));
16731 Derive_Subprogram
16732 (New_Subp => New_Subp,
16733 Parent_Subp => Iface_Subp,
16734 Derived_Type => Derived_Type,
16735 Parent_Type => Iface_Type,
16736 Actual_Subp => Actual_Subp);
16738 -- Given that this new interface entity corresponds with a primitive
16739 -- of the parent that was not overridden we must leave it associated
16740 -- with its parent primitive to ensure that it will share the same
16741 -- dispatch table slot when overridden. We must set the Alias to Subp
16742 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16743 -- (in case we inherited Subp from Iface_Type via a nonabstract
16744 -- generic formal type).
16746 if No (Actual_Subp) then
16747 Set_Alias (New_Subp, Subp);
16749 declare
16750 T : Entity_Id := Find_Dispatching_Type (Subp);
16751 begin
16752 while Etype (T) /= T loop
16753 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16754 Set_Is_Abstract_Subprogram (New_Subp, False);
16755 exit;
16756 end if;
16758 T := Etype (T);
16759 end loop;
16760 end;
16762 -- For instantiations this is not needed since the previous call to
16763 -- Derive_Subprogram leaves the entity well decorated.
16765 else
16766 pragma Assert (Alias (New_Subp) = Actual_Subp);
16767 null;
16768 end if;
16769 end Derive_Interface_Subprogram;
16771 -- Local variables
16773 Alias_Subp : Entity_Id;
16774 Act_List : Elist_Id;
16775 Act_Elmt : Elmt_Id;
16776 Act_Subp : Entity_Id := Empty;
16777 Elmt : Elmt_Id;
16778 Need_Search : Boolean := False;
16779 New_Subp : Entity_Id;
16780 Parent_Base : Entity_Id;
16781 Subp : Entity_Id;
16783 -- Start of processing for Derive_Subprograms
16785 begin
16786 if Ekind (Parent_Type) = E_Record_Type_With_Private
16787 and then Has_Discriminants (Parent_Type)
16788 and then Present (Full_View (Parent_Type))
16789 then
16790 Parent_Base := Full_View (Parent_Type);
16791 else
16792 Parent_Base := Parent_Type;
16793 end if;
16795 if Present (Generic_Actual) then
16796 Act_List := Collect_Primitive_Operations (Generic_Actual);
16797 Act_Elmt := First_Elmt (Act_List);
16798 else
16799 Act_List := No_Elist;
16800 Act_Elmt := No_Elmt;
16801 end if;
16803 -- Derive primitives inherited from the parent. Note that if the generic
16804 -- actual is present, this is not really a type derivation, it is a
16805 -- completion within an instance.
16807 -- Case 1: Derived_Type does not implement interfaces
16809 if not Is_Tagged_Type (Derived_Type)
16810 or else (not Has_Interfaces (Derived_Type)
16811 and then not (Present (Generic_Actual)
16812 and then Has_Interfaces (Generic_Actual)))
16813 then
16814 Elmt := First_Elmt (Op_List);
16815 while Present (Elmt) loop
16816 Subp := Node (Elmt);
16818 -- Literals are derived earlier in the process of building the
16819 -- derived type, and are skipped here.
16821 if Ekind (Subp) = E_Enumeration_Literal then
16822 null;
16824 -- The actual is a direct descendant and the common primitive
16825 -- operations appear in the same order.
16827 -- If the generic parent type is present, the derived type is an
16828 -- instance of a formal derived type, and within the instance its
16829 -- operations are those of the actual. We derive from the formal
16830 -- type but make the inherited operations aliases of the
16831 -- corresponding operations of the actual.
16833 else
16834 pragma Assert (No (Node (Act_Elmt))
16835 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16836 and then
16837 Type_Conformant
16838 (Subp, Node (Act_Elmt),
16839 Skip_Controlling_Formals => True)));
16841 Derive_Subprogram
16842 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16844 if Present (Act_Elmt) then
16845 Next_Elmt (Act_Elmt);
16846 end if;
16847 end if;
16849 Next_Elmt (Elmt);
16850 end loop;
16852 -- Case 2: Derived_Type implements interfaces
16854 else
16855 -- If the parent type has no predefined primitives we remove
16856 -- predefined primitives from the list of primitives of generic
16857 -- actual to simplify the complexity of this algorithm.
16859 if Present (Generic_Actual) then
16860 declare
16861 Has_Predefined_Primitives : Boolean := False;
16863 begin
16864 -- Check if the parent type has predefined primitives
16866 Elmt := First_Elmt (Op_List);
16867 while Present (Elmt) loop
16868 Subp := Node (Elmt);
16870 if Is_Predefined_Dispatching_Operation (Subp)
16871 and then not Comes_From_Source (Ultimate_Alias (Subp))
16872 then
16873 Has_Predefined_Primitives := True;
16874 exit;
16875 end if;
16877 Next_Elmt (Elmt);
16878 end loop;
16880 -- Remove predefined primitives of Generic_Actual. We must use
16881 -- an auxiliary list because in case of tagged types the value
16882 -- returned by Collect_Primitive_Operations is the value stored
16883 -- in its Primitive_Operations attribute (and we don't want to
16884 -- modify its current contents).
16886 if not Has_Predefined_Primitives then
16887 declare
16888 Aux_List : constant Elist_Id := New_Elmt_List;
16890 begin
16891 Elmt := First_Elmt (Act_List);
16892 while Present (Elmt) loop
16893 Subp := Node (Elmt);
16895 if not Is_Predefined_Dispatching_Operation (Subp)
16896 or else Comes_From_Source (Subp)
16897 then
16898 Append_Elmt (Subp, Aux_List);
16899 end if;
16901 Next_Elmt (Elmt);
16902 end loop;
16904 Act_List := Aux_List;
16905 end;
16906 end if;
16908 Act_Elmt := First_Elmt (Act_List);
16909 Act_Subp := Node (Act_Elmt);
16910 end;
16911 end if;
16913 -- Stage 1: If the generic actual is not present we derive the
16914 -- primitives inherited from the parent type. If the generic parent
16915 -- type is present, the derived type is an instance of a formal
16916 -- derived type, and within the instance its operations are those of
16917 -- the actual. We derive from the formal type but make the inherited
16918 -- operations aliases of the corresponding operations of the actual.
16920 Elmt := First_Elmt (Op_List);
16921 while Present (Elmt) loop
16922 Subp := Node (Elmt);
16923 Alias_Subp := Ultimate_Alias (Subp);
16925 -- Do not derive internal entities of the parent that link
16926 -- interface primitives with their covering primitive. These
16927 -- entities will be added to this type when frozen.
16929 if Present (Interface_Alias (Subp)) then
16930 goto Continue;
16931 end if;
16933 -- If the generic actual is present find the corresponding
16934 -- operation in the generic actual. If the parent type is a
16935 -- direct ancestor of the derived type then, even if it is an
16936 -- interface, the operations are inherited from the primary
16937 -- dispatch table and are in the proper order. If we detect here
16938 -- that primitives are not in the same order we traverse the list
16939 -- of primitive operations of the actual to find the one that
16940 -- implements the interface primitive.
16942 if Need_Search
16943 or else
16944 (Present (Generic_Actual)
16945 and then Present (Act_Subp)
16946 and then not
16947 (Primitive_Names_Match (Subp, Act_Subp)
16948 and then
16949 Type_Conformant (Subp, Act_Subp,
16950 Skip_Controlling_Formals => True)))
16951 then
16952 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16953 Use_Full_View => True));
16955 -- Remember that we need searching for all pending primitives
16957 Need_Search := True;
16959 -- Handle entities associated with interface primitives
16961 if Present (Alias_Subp)
16962 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16963 and then not Is_Predefined_Dispatching_Operation (Subp)
16964 then
16965 -- Search for the primitive in the homonym chain
16967 Act_Subp :=
16968 Find_Primitive_Covering_Interface
16969 (Tagged_Type => Generic_Actual,
16970 Iface_Prim => Alias_Subp);
16972 -- Previous search may not locate primitives covering
16973 -- interfaces defined in generics units or instantiations.
16974 -- (it fails if the covering primitive has formals whose
16975 -- type is also defined in generics or instantiations).
16976 -- In such case we search in the list of primitives of the
16977 -- generic actual for the internal entity that links the
16978 -- interface primitive and the covering primitive.
16980 if No (Act_Subp)
16981 and then Is_Generic_Type (Parent_Type)
16982 then
16983 -- This code has been designed to handle only generic
16984 -- formals that implement interfaces that are defined
16985 -- in a generic unit or instantiation. If this code is
16986 -- needed for other cases we must review it because
16987 -- (given that it relies on Original_Location to locate
16988 -- the primitive of Generic_Actual that covers the
16989 -- interface) it could leave linked through attribute
16990 -- Alias entities of unrelated instantiations).
16992 pragma Assert
16993 (Is_Generic_Unit
16994 (Scope (Find_Dispatching_Type (Alias_Subp)))
16995 or else
16996 Instantiation_Location
16997 (Sloc (Find_Dispatching_Type (Alias_Subp)))
16998 /= No_Location);
16999 declare
17000 Iface_Prim_Loc : constant Source_Ptr :=
17001 Original_Location (Sloc (Alias_Subp));
17003 Elmt : Elmt_Id;
17004 Prim : Entity_Id;
17006 begin
17007 Elmt :=
17008 First_Elmt (Primitive_Operations (Generic_Actual));
17010 Search : while Present (Elmt) loop
17011 Prim := Node (Elmt);
17013 if Present (Interface_Alias (Prim))
17014 and then Original_Location
17015 (Sloc (Interface_Alias (Prim))) =
17016 Iface_Prim_Loc
17017 then
17018 Act_Subp := Alias (Prim);
17019 exit Search;
17020 end if;
17022 Next_Elmt (Elmt);
17023 end loop Search;
17024 end;
17025 end if;
17027 pragma Assert (Present (Act_Subp)
17028 or else Is_Abstract_Type (Generic_Actual)
17029 or else Serious_Errors_Detected > 0);
17031 -- Handle predefined primitives plus the rest of user-defined
17032 -- primitives
17034 else
17035 Act_Elmt := First_Elmt (Act_List);
17036 while Present (Act_Elmt) loop
17037 Act_Subp := Node (Act_Elmt);
17039 exit when Primitive_Names_Match (Subp, Act_Subp)
17040 and then Type_Conformant
17041 (Subp, Act_Subp,
17042 Skip_Controlling_Formals => True)
17043 and then No (Interface_Alias (Act_Subp));
17045 Next_Elmt (Act_Elmt);
17046 end loop;
17048 if No (Act_Elmt) then
17049 Act_Subp := Empty;
17050 end if;
17051 end if;
17052 end if;
17054 -- Case 1: If the parent is a limited interface then it has the
17055 -- predefined primitives of synchronized interfaces. However, the
17056 -- actual type may be a non-limited type and hence it does not
17057 -- have such primitives.
17059 if Present (Generic_Actual)
17060 and then No (Act_Subp)
17061 and then Is_Limited_Interface (Parent_Base)
17062 and then Is_Predefined_Interface_Primitive (Subp)
17063 then
17064 null;
17066 -- Case 2: Inherit entities associated with interfaces that were
17067 -- not covered by the parent type. We exclude here null interface
17068 -- primitives because they do not need special management.
17070 -- We also exclude interface operations that are renamings. If the
17071 -- subprogram is an explicit renaming of an interface primitive,
17072 -- it is a regular primitive operation, and the presence of its
17073 -- alias is not relevant: it has to be derived like any other
17074 -- primitive.
17076 elsif Present (Alias (Subp))
17077 and then Nkind (Unit_Declaration_Node (Subp)) /=
17078 N_Subprogram_Renaming_Declaration
17079 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
17080 and then not
17081 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
17082 and then Null_Present (Parent (Alias_Subp)))
17083 then
17084 -- If this is an abstract private type then we transfer the
17085 -- derivation of the interface primitive from the partial view
17086 -- to the full view. This is safe because all the interfaces
17087 -- must be visible in the partial view. Done to avoid adding
17088 -- a new interface derivation to the private part of the
17089 -- enclosing package; otherwise this new derivation would be
17090 -- decorated as hidden when the analysis of the enclosing
17091 -- package completes.
17093 if Is_Abstract_Type (Derived_Type)
17094 and then In_Private_Part (Current_Scope)
17095 and then Has_Private_Declaration (Derived_Type)
17096 then
17097 declare
17098 Partial_View : Entity_Id;
17099 Elmt : Elmt_Id;
17100 Ent : Entity_Id;
17102 begin
17103 Partial_View := First_Entity (Current_Scope);
17104 loop
17105 exit when No (Partial_View)
17106 or else (Has_Private_Declaration (Partial_View)
17107 and then
17108 Full_View (Partial_View) = Derived_Type);
17110 Next_Entity (Partial_View);
17111 end loop;
17113 -- If the partial view was not found then the source code
17114 -- has errors and the derivation is not needed.
17116 if Present (Partial_View) then
17117 Elmt :=
17118 First_Elmt (Primitive_Operations (Partial_View));
17119 while Present (Elmt) loop
17120 Ent := Node (Elmt);
17122 if Present (Alias (Ent))
17123 and then Ultimate_Alias (Ent) = Alias (Subp)
17124 then
17125 Append_Elmt
17126 (Ent, Primitive_Operations (Derived_Type));
17127 exit;
17128 end if;
17130 Next_Elmt (Elmt);
17131 end loop;
17133 -- If the interface primitive was not found in the
17134 -- partial view then this interface primitive was
17135 -- overridden. We add a derivation to activate in
17136 -- Derive_Progenitor_Subprograms the machinery to
17137 -- search for it.
17139 if No (Elmt) then
17140 Derive_Interface_Subprogram
17141 (New_Subp => New_Subp,
17142 Subp => Subp,
17143 Actual_Subp => Act_Subp);
17144 end if;
17145 end if;
17146 end;
17147 else
17148 Derive_Interface_Subprogram
17149 (New_Subp => New_Subp,
17150 Subp => Subp,
17151 Actual_Subp => Act_Subp);
17152 end if;
17154 -- Case 3: Common derivation
17156 else
17157 Derive_Subprogram
17158 (New_Subp => New_Subp,
17159 Parent_Subp => Subp,
17160 Derived_Type => Derived_Type,
17161 Parent_Type => Parent_Base,
17162 Actual_Subp => Act_Subp);
17163 end if;
17165 -- No need to update Act_Elm if we must search for the
17166 -- corresponding operation in the generic actual
17168 if not Need_Search
17169 and then Present (Act_Elmt)
17170 then
17171 Next_Elmt (Act_Elmt);
17172 Act_Subp := Node (Act_Elmt);
17173 end if;
17175 <<Continue>>
17176 Next_Elmt (Elmt);
17177 end loop;
17179 -- Inherit additional operations from progenitors. If the derived
17180 -- type is a generic actual, there are not new primitive operations
17181 -- for the type because it has those of the actual, and therefore
17182 -- nothing needs to be done. The renamings generated above are not
17183 -- primitive operations, and their purpose is simply to make the
17184 -- proper operations visible within an instantiation.
17186 if No (Generic_Actual) then
17187 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17188 end if;
17189 end if;
17191 -- Final check: Direct descendants must have their primitives in the
17192 -- same order. We exclude from this test untagged types and instances
17193 -- of formal derived types. We skip this test if we have already
17194 -- reported serious errors in the sources.
17196 pragma Assert (not Is_Tagged_Type (Derived_Type)
17197 or else Present (Generic_Actual)
17198 or else Serious_Errors_Detected > 0
17199 or else Check_Derived_Type);
17200 end Derive_Subprograms;
17202 --------------------------------
17203 -- Derived_Standard_Character --
17204 --------------------------------
17206 procedure Derived_Standard_Character
17207 (N : Node_Id;
17208 Parent_Type : Entity_Id;
17209 Derived_Type : Entity_Id)
17211 Loc : constant Source_Ptr := Sloc (N);
17212 Def : constant Node_Id := Type_Definition (N);
17213 Indic : constant Node_Id := Subtype_Indication (Def);
17214 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17215 Implicit_Base : constant Entity_Id :=
17216 Create_Itype
17217 (E_Enumeration_Type, N, Derived_Type, 'B');
17219 Lo : Node_Id;
17220 Hi : Node_Id;
17222 begin
17223 Discard_Node (Process_Subtype (Indic, N));
17225 Set_Etype (Implicit_Base, Parent_Base);
17226 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17227 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17229 Set_Is_Character_Type (Implicit_Base, True);
17230 Set_Has_Delayed_Freeze (Implicit_Base);
17232 -- The bounds of the implicit base are the bounds of the parent base.
17233 -- Note that their type is the parent base.
17235 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17236 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17238 Set_Scalar_Range (Implicit_Base,
17239 Make_Range (Loc,
17240 Low_Bound => Lo,
17241 High_Bound => Hi));
17243 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17244 Set_Etype (Derived_Type, Implicit_Base);
17245 Set_Size_Info (Derived_Type, Parent_Type);
17247 if not Known_RM_Size (Derived_Type) then
17248 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17249 end if;
17251 Set_Is_Character_Type (Derived_Type, True);
17253 if Nkind (Indic) /= N_Subtype_Indication then
17255 -- If no explicit constraint, the bounds are those
17256 -- of the parent type.
17258 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17259 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17260 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17261 end if;
17263 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17264 end Derived_Standard_Character;
17266 ------------------------------
17267 -- Derived_Type_Declaration --
17268 ------------------------------
17270 procedure Derived_Type_Declaration
17271 (T : Entity_Id;
17272 N : Node_Id;
17273 Is_Completion : Boolean)
17275 Parent_Type : Entity_Id;
17277 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17278 -- Check whether the parent type is a generic formal, or derives
17279 -- directly or indirectly from one.
17281 ------------------------
17282 -- Comes_From_Generic --
17283 ------------------------
17285 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17286 begin
17287 if Is_Generic_Type (Typ) then
17288 return True;
17290 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17291 return True;
17293 elsif Is_Private_Type (Typ)
17294 and then Present (Full_View (Typ))
17295 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17296 then
17297 return True;
17299 elsif Is_Generic_Actual_Type (Typ) then
17300 return True;
17302 else
17303 return False;
17304 end if;
17305 end Comes_From_Generic;
17307 -- Local variables
17309 Def : constant Node_Id := Type_Definition (N);
17310 Iface_Def : Node_Id;
17311 Indic : constant Node_Id := Subtype_Indication (Def);
17312 Extension : constant Node_Id := Record_Extension_Part (Def);
17313 Parent_Node : Node_Id;
17314 Taggd : Boolean;
17316 -- Start of processing for Derived_Type_Declaration
17318 begin
17319 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17321 -- Ada 2005 (AI-251): In case of interface derivation check that the
17322 -- parent is also an interface.
17324 if Interface_Present (Def) then
17325 if not Is_Interface (Parent_Type) then
17326 Diagnose_Interface (Indic, Parent_Type);
17328 else
17329 Parent_Node := Parent (Base_Type (Parent_Type));
17330 Iface_Def := Type_Definition (Parent_Node);
17332 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17333 -- other limited interfaces.
17335 if Limited_Present (Def) then
17336 if Limited_Present (Iface_Def) then
17337 null;
17339 elsif Protected_Present (Iface_Def) then
17340 Error_Msg_NE
17341 ("descendant of & must be declared as a protected "
17342 & "interface", N, Parent_Type);
17344 elsif Synchronized_Present (Iface_Def) then
17345 Error_Msg_NE
17346 ("descendant of & must be declared as a synchronized "
17347 & "interface", N, Parent_Type);
17349 elsif Task_Present (Iface_Def) then
17350 Error_Msg_NE
17351 ("descendant of & must be declared as a task interface",
17352 N, Parent_Type);
17354 else
17355 Error_Msg_N
17356 ("(Ada 2005) limited interface cannot inherit from "
17357 & "non-limited interface", Indic);
17358 end if;
17360 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17361 -- from non-limited or limited interfaces.
17363 elsif not Protected_Present (Def)
17364 and then not Synchronized_Present (Def)
17365 and then not Task_Present (Def)
17366 then
17367 if Limited_Present (Iface_Def) then
17368 null;
17370 elsif Protected_Present (Iface_Def) then
17371 Error_Msg_NE
17372 ("descendant of & must be declared as a protected "
17373 & "interface", N, Parent_Type);
17375 elsif Synchronized_Present (Iface_Def) then
17376 Error_Msg_NE
17377 ("descendant of & must be declared as a synchronized "
17378 & "interface", N, Parent_Type);
17380 elsif Task_Present (Iface_Def) then
17381 Error_Msg_NE
17382 ("descendant of & must be declared as a task interface",
17383 N, Parent_Type);
17384 else
17385 null;
17386 end if;
17387 end if;
17388 end if;
17389 end if;
17391 if Is_Tagged_Type (Parent_Type)
17392 and then Is_Concurrent_Type (Parent_Type)
17393 and then not Is_Interface (Parent_Type)
17394 then
17395 Error_Msg_N
17396 ("parent type of a record extension cannot be a synchronized "
17397 & "tagged type (RM 3.9.1 (3/1))", N);
17398 Set_Etype (T, Any_Type);
17399 return;
17400 end if;
17402 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17403 -- interfaces
17405 if Is_Tagged_Type (Parent_Type)
17406 and then Is_Non_Empty_List (Interface_List (Def))
17407 then
17408 declare
17409 Intf : Node_Id;
17410 T : Entity_Id;
17412 begin
17413 Intf := First (Interface_List (Def));
17414 while Present (Intf) loop
17415 T := Find_Type_Of_Subtype_Indic (Intf);
17417 if not Is_Interface (T) then
17418 Diagnose_Interface (Intf, T);
17420 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17421 -- a limited type from having a nonlimited progenitor.
17423 elsif (Limited_Present (Def)
17424 or else (not Is_Interface (Parent_Type)
17425 and then Is_Limited_Type (Parent_Type)))
17426 and then not Is_Limited_Interface (T)
17427 then
17428 Error_Msg_NE
17429 ("progenitor interface& of limited type must be limited",
17430 N, T);
17431 end if;
17433 Next (Intf);
17434 end loop;
17435 end;
17437 -- Check consistency of any nonoverridable aspects that are
17438 -- inherited from multiple sources.
17440 Check_Inherited_Nonoverridable_Aspects
17441 (Inheritor => T,
17442 Interface_List => Interface_List (Def),
17443 Parent_Type => Parent_Type);
17444 end if;
17446 if Parent_Type = Any_Type
17447 or else Etype (Parent_Type) = Any_Type
17448 or else (Is_Class_Wide_Type (Parent_Type)
17449 and then Etype (Parent_Type) = T)
17450 then
17451 -- If Parent_Type is undefined or illegal, make new type into a
17452 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17453 -- errors. If this is a self-definition, emit error now.
17455 if T = Parent_Type or else T = Etype (Parent_Type) then
17456 Error_Msg_N ("type cannot be used in its own definition", Indic);
17457 end if;
17459 Mutate_Ekind (T, Ekind (Parent_Type));
17460 Set_Etype (T, Any_Type);
17461 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17463 -- Initialize the list of primitive operations to an empty list,
17464 -- to cover tagged types as well as untagged types. For untagged
17465 -- types this is used either to analyze the call as legal when
17466 -- Extensions_Allowed is True, or to issue a better error message
17467 -- otherwise.
17469 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17471 return;
17472 end if;
17474 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17475 -- an interface is special because the list of interfaces in the full
17476 -- view can be given in any order. For example:
17478 -- type A is interface;
17479 -- type B is interface and A;
17480 -- type D is new B with private;
17481 -- private
17482 -- type D is new A and B with null record; -- 1 --
17484 -- In this case we perform the following transformation of -1-:
17486 -- type D is new B and A with null record;
17488 -- If the parent of the full-view covers the parent of the partial-view
17489 -- we have two possible cases:
17491 -- 1) They have the same parent
17492 -- 2) The parent of the full-view implements some further interfaces
17494 -- In both cases we do not need to perform the transformation. In the
17495 -- first case the source program is correct and the transformation is
17496 -- not needed; in the second case the source program does not fulfill
17497 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17498 -- later.
17500 -- This transformation not only simplifies the rest of the analysis of
17501 -- this type declaration but also simplifies the correct generation of
17502 -- the object layout to the expander.
17504 if In_Private_Part (Current_Scope)
17505 and then Is_Interface (Parent_Type)
17506 then
17507 declare
17508 Partial_View : Entity_Id;
17509 Partial_View_Parent : Entity_Id;
17511 function Reorder_Interfaces return Boolean;
17512 -- Look for an interface in the full view's interface list that
17513 -- matches the parent type of the partial view, and when found,
17514 -- rewrite the full view's parent with the partial view's parent,
17515 -- append the full view's original parent to the interface list,
17516 -- recursively call Derived_Type_Definition on the full type, and
17517 -- return True. If a match is not found, return False.
17519 ------------------------
17520 -- Reorder_Interfaces --
17521 ------------------------
17523 function Reorder_Interfaces return Boolean is
17524 Iface : Node_Id;
17525 New_Iface : Node_Id;
17527 begin
17528 Iface := First (Interface_List (Def));
17529 while Present (Iface) loop
17530 if Etype (Iface) = Etype (Partial_View) then
17531 Rewrite (Subtype_Indication (Def),
17532 New_Copy (Subtype_Indication (Parent (Partial_View))));
17534 New_Iface :=
17535 Make_Identifier (Sloc (N), Chars (Parent_Type));
17536 Rewrite (Iface, New_Iface);
17538 -- Analyze the transformed code
17540 Derived_Type_Declaration (T, N, Is_Completion);
17541 return True;
17542 end if;
17544 Next (Iface);
17545 end loop;
17546 return False;
17547 end Reorder_Interfaces;
17549 begin
17550 -- Look for the associated private type declaration
17552 Partial_View := Incomplete_Or_Partial_View (T);
17554 -- If the partial view was not found then the source code has
17555 -- errors and the transformation is not needed.
17557 if Present (Partial_View) then
17558 Partial_View_Parent := Etype (Partial_View);
17560 -- If the parent of the full-view covers the parent of the
17561 -- partial-view we have nothing else to do.
17563 if Interface_Present_In_Ancestor
17564 (Parent_Type, Partial_View_Parent)
17565 then
17566 null;
17568 -- Traverse the list of interfaces of the full view to look
17569 -- for the parent of the partial view and reorder the
17570 -- interfaces to match the order in the partial view,
17571 -- if needed.
17573 else
17575 if Reorder_Interfaces then
17576 -- Having the interfaces listed in any order is legal.
17577 -- However, the compiler does not properly handle
17578 -- different orders between partial and full views in
17579 -- generic units. We give a warning about the order
17580 -- mismatch, so the user can work around this problem.
17582 Error_Msg_N ("??full declaration does not respect " &
17583 "partial declaration order", T);
17584 Error_Msg_N ("\??consider reordering", T);
17586 return;
17587 end if;
17588 end if;
17589 end if;
17590 end;
17591 end if;
17593 -- Only composite types other than array types are allowed to have
17594 -- discriminants.
17596 if Present (Discriminant_Specifications (N)) then
17597 if (Is_Elementary_Type (Parent_Type)
17598 or else
17599 Is_Array_Type (Parent_Type))
17600 and then not Error_Posted (N)
17601 then
17602 Error_Msg_N
17603 ("elementary or array type cannot have discriminants",
17604 Defining_Identifier (First (Discriminant_Specifications (N))));
17606 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17607 -- only if we are not already processing a malformed syntax tree.
17609 if Is_Type (T) then
17610 Set_Has_Discriminants (T, False);
17611 end if;
17612 end if;
17613 end if;
17615 -- In Ada 83, a derived type defined in a package specification cannot
17616 -- be used for further derivation until the end of its visible part.
17617 -- Note that derivation in the private part of the package is allowed.
17619 if Ada_Version = Ada_83
17620 and then Is_Derived_Type (Parent_Type)
17621 and then In_Visible_Part (Scope (Parent_Type))
17622 then
17623 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17624 Error_Msg_N
17625 ("(Ada 83) premature use of type for derivation", Indic);
17626 end if;
17627 end if;
17629 -- Check for early use of incomplete or private type
17631 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17632 Error_Msg_N ("premature derivation of incomplete type", Indic);
17633 return;
17635 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17636 and then not Comes_From_Generic (Parent_Type))
17637 or else Has_Private_Component (Parent_Type)
17638 then
17639 -- The ancestor type of a formal type can be incomplete, in which
17640 -- case only the operations of the partial view are available in the
17641 -- generic. Subsequent checks may be required when the full view is
17642 -- analyzed to verify that a derivation from a tagged type has an
17643 -- extension.
17645 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17646 null;
17648 elsif No (Underlying_Type (Parent_Type))
17649 or else Has_Private_Component (Parent_Type)
17650 then
17651 Error_Msg_N
17652 ("premature derivation of derived or private type", Indic);
17654 -- Flag the type itself as being in error, this prevents some
17655 -- nasty problems with subsequent uses of the malformed type.
17657 Set_Error_Posted (T);
17659 -- Check that within the immediate scope of an untagged partial
17660 -- view it's illegal to derive from the partial view if the
17661 -- full view is tagged. (7.3(7))
17663 -- We verify that the Parent_Type is a partial view by checking
17664 -- that it is not a Full_Type_Declaration (i.e. a private type or
17665 -- private extension declaration), to distinguish a partial view
17666 -- from a derivation from a private type which also appears as
17667 -- E_Private_Type. If the parent base type is not declared in an
17668 -- enclosing scope there is no need to check.
17670 elsif Present (Full_View (Parent_Type))
17671 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17672 and then not Is_Tagged_Type (Parent_Type)
17673 and then Is_Tagged_Type (Full_View (Parent_Type))
17674 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17675 then
17676 Error_Msg_N
17677 ("premature derivation from type with tagged full view",
17678 Indic);
17679 end if;
17680 end if;
17682 -- Check that form of derivation is appropriate
17684 Taggd := Is_Tagged_Type (Parent_Type);
17686 -- Set the parent type to the class-wide type's specific type in this
17687 -- case to prevent cascading errors
17689 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17690 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17691 Set_Etype (T, Etype (Parent_Type));
17692 return;
17693 end if;
17695 if Present (Extension) and then not Taggd then
17696 Error_Msg_N
17697 ("type derived from untagged type cannot have extension", Indic);
17699 elsif No (Extension) and then Taggd then
17701 -- If this declaration is within a private part (or body) of a
17702 -- generic instantiation then the derivation is allowed (the parent
17703 -- type can only appear tagged in this case if it's a generic actual
17704 -- type, since it would otherwise have been rejected in the analysis
17705 -- of the generic template).
17707 if not Is_Generic_Actual_Type (Parent_Type)
17708 or else In_Visible_Part (Scope (Parent_Type))
17709 then
17710 if Is_Class_Wide_Type (Parent_Type) then
17711 Error_Msg_N
17712 ("parent type must not be a class-wide type", Indic);
17714 -- Use specific type to prevent cascaded errors.
17716 Parent_Type := Etype (Parent_Type);
17718 else
17719 Error_Msg_N
17720 ("type derived from tagged type must have extension", Indic);
17721 end if;
17722 end if;
17723 end if;
17725 -- AI-443: Synchronized formal derived types require a private
17726 -- extension. There is no point in checking the ancestor type or
17727 -- the progenitors since the construct is wrong to begin with.
17729 if Ada_Version >= Ada_2005
17730 and then Is_Generic_Type (T)
17731 and then Present (Original_Node (N))
17732 then
17733 declare
17734 Decl : constant Node_Id := Original_Node (N);
17736 begin
17737 if Nkind (Decl) = N_Formal_Type_Declaration
17738 and then Nkind (Formal_Type_Definition (Decl)) =
17739 N_Formal_Derived_Type_Definition
17740 and then Synchronized_Present (Formal_Type_Definition (Decl))
17741 and then No (Extension)
17743 -- Avoid emitting a duplicate error message
17745 and then not Error_Posted (Indic)
17746 then
17747 Error_Msg_N
17748 ("synchronized derived type must have extension", N);
17749 end if;
17750 end;
17751 end if;
17753 if Null_Exclusion_Present (Def)
17754 and then not Is_Access_Type (Parent_Type)
17755 then
17756 Error_Msg_N ("null exclusion can only apply to an access type", N);
17757 end if;
17759 Check_Wide_Character_Restriction (Parent_Type, Indic);
17761 -- Avoid deriving parent primitives of underlying record views
17763 Set_Is_Not_Self_Hidden (T);
17765 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17766 Derive_Subps => not Is_Underlying_Record_View (T));
17768 -- AI-419: The parent type of an explicitly limited derived type must
17769 -- be a limited type or a limited interface.
17771 if Limited_Present (Def) then
17772 Set_Is_Limited_Record (T);
17774 if Is_Interface (T) then
17775 Set_Is_Limited_Interface (T);
17776 end if;
17778 if not Is_Limited_Type (Parent_Type)
17779 and then
17780 (not Is_Interface (Parent_Type)
17781 or else not Is_Limited_Interface (Parent_Type))
17782 then
17783 -- AI05-0096: a derivation in the private part of an instance is
17784 -- legal if the generic formal is untagged limited, and the actual
17785 -- is non-limited.
17787 if Is_Generic_Actual_Type (Parent_Type)
17788 and then In_Private_Part (Current_Scope)
17789 and then
17790 not Is_Tagged_Type
17791 (Generic_Parent_Type (Parent (Parent_Type)))
17792 then
17793 null;
17795 else
17796 Error_Msg_NE
17797 ("parent type& of limited type must be limited",
17798 N, Parent_Type);
17799 end if;
17800 end if;
17801 end if;
17802 end Derived_Type_Declaration;
17804 ------------------------
17805 -- Diagnose_Interface --
17806 ------------------------
17808 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17809 begin
17810 if not Is_Interface (E) and then E /= Any_Type then
17811 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17812 end if;
17813 end Diagnose_Interface;
17815 ----------------------------------
17816 -- Enumeration_Type_Declaration --
17817 ----------------------------------
17819 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17820 Ev : Uint;
17821 L : Node_Id;
17822 R_Node : Node_Id;
17823 B_Node : Node_Id;
17825 begin
17826 -- Create identifier node representing lower bound
17828 B_Node := New_Node (N_Identifier, Sloc (Def));
17829 L := First (Literals (Def));
17830 Set_Chars (B_Node, Chars (L));
17831 Set_Entity (B_Node, L);
17832 Set_Etype (B_Node, T);
17833 Set_Is_Static_Expression (B_Node, True);
17835 R_Node := New_Node (N_Range, Sloc (Def));
17836 Set_Low_Bound (R_Node, B_Node);
17838 Mutate_Ekind (T, E_Enumeration_Type);
17839 Set_First_Literal (T, L);
17840 Set_Etype (T, T);
17841 Set_Is_Constrained (T);
17843 Ev := Uint_0;
17845 -- Loop through literals of enumeration type setting pos and rep values
17846 -- except that if the Ekind is already set, then it means the literal
17847 -- was already constructed (case of a derived type declaration and we
17848 -- should not disturb the Pos and Rep values.
17850 while Present (L) loop
17851 if Ekind (L) /= E_Enumeration_Literal then
17852 Mutate_Ekind (L, E_Enumeration_Literal);
17853 Set_Is_Not_Self_Hidden (L);
17854 Set_Enumeration_Pos (L, Ev);
17855 Set_Enumeration_Rep (L, Ev);
17856 Set_Is_Known_Valid (L, True);
17857 end if;
17859 Set_Etype (L, T);
17860 New_Overloaded_Entity (L);
17861 Generate_Definition (L);
17862 Set_Convention (L, Convention_Intrinsic);
17864 -- Case of character literal
17866 if Nkind (L) = N_Defining_Character_Literal then
17867 Set_Is_Character_Type (T, True);
17869 -- Check violation of No_Wide_Characters
17871 if Restriction_Check_Required (No_Wide_Characters) then
17872 Get_Name_String (Chars (L));
17874 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17875 Check_Restriction (No_Wide_Characters, L);
17876 end if;
17877 end if;
17878 end if;
17880 Ev := Ev + 1;
17881 Next (L);
17882 end loop;
17884 -- Now create a node representing upper bound
17886 B_Node := New_Node (N_Identifier, Sloc (Def));
17887 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17888 Set_Entity (B_Node, Last (Literals (Def)));
17889 Set_Etype (B_Node, T);
17890 Set_Is_Static_Expression (B_Node, True);
17892 Set_High_Bound (R_Node, B_Node);
17894 -- Initialize various fields of the type. Some of this information
17895 -- may be overwritten later through rep. clauses.
17897 Set_Scalar_Range (T, R_Node);
17898 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17899 Set_Enum_Esize (T);
17900 Set_Enum_Pos_To_Rep (T, Empty);
17902 -- Set Discard_Names if configuration pragma set, or if there is
17903 -- a parameterless pragma in the current declarative region
17905 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17906 Set_Discard_Names (T);
17907 end if;
17909 -- Process end label if there is one
17911 if Present (Def) then
17912 Process_End_Label (Def, 'e', T);
17913 end if;
17914 end Enumeration_Type_Declaration;
17916 ---------------------------------
17917 -- Expand_To_Stored_Constraint --
17918 ---------------------------------
17920 function Expand_To_Stored_Constraint
17921 (Typ : Entity_Id;
17922 Constraint : Elist_Id) return Elist_Id
17924 Explicitly_Discriminated_Type : Entity_Id;
17925 Expansion : Elist_Id;
17926 Discriminant : Entity_Id;
17928 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17929 -- Find the nearest type that actually specifies discriminants
17931 ---------------------------------
17932 -- Type_With_Explicit_Discrims --
17933 ---------------------------------
17935 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17936 Typ : constant E := Base_Type (Id);
17938 begin
17939 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17940 if Present (Full_View (Typ)) then
17941 return Type_With_Explicit_Discrims (Full_View (Typ));
17942 end if;
17944 else
17945 if Has_Discriminants (Typ) then
17946 return Typ;
17947 end if;
17948 end if;
17950 if Etype (Typ) = Typ then
17951 return Empty;
17952 elsif Has_Discriminants (Typ) then
17953 return Typ;
17954 else
17955 return Type_With_Explicit_Discrims (Etype (Typ));
17956 end if;
17958 end Type_With_Explicit_Discrims;
17960 -- Start of processing for Expand_To_Stored_Constraint
17962 begin
17963 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17964 return No_Elist;
17965 end if;
17967 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17969 if No (Explicitly_Discriminated_Type) then
17970 return No_Elist;
17971 end if;
17973 Expansion := New_Elmt_List;
17975 Discriminant :=
17976 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17977 while Present (Discriminant) loop
17978 Append_Elmt
17979 (Get_Discriminant_Value
17980 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17981 To => Expansion);
17982 Next_Stored_Discriminant (Discriminant);
17983 end loop;
17985 return Expansion;
17986 end Expand_To_Stored_Constraint;
17988 ---------------------------
17989 -- Find_Hidden_Interface --
17990 ---------------------------
17992 function Find_Hidden_Interface
17993 (Src : Elist_Id;
17994 Dest : Elist_Id) return Entity_Id
17996 Iface : Entity_Id;
17997 Iface_Elmt : Elmt_Id;
17999 begin
18000 if Present (Src) and then Present (Dest) then
18001 Iface_Elmt := First_Elmt (Src);
18002 while Present (Iface_Elmt) loop
18003 Iface := Node (Iface_Elmt);
18005 if Is_Interface (Iface)
18006 and then not Contain_Interface (Iface, Dest)
18007 then
18008 return Iface;
18009 end if;
18011 Next_Elmt (Iface_Elmt);
18012 end loop;
18013 end if;
18015 return Empty;
18016 end Find_Hidden_Interface;
18018 --------------------
18019 -- Find_Type_Name --
18020 --------------------
18022 function Find_Type_Name (N : Node_Id) return Entity_Id is
18023 Id : constant Entity_Id := Defining_Identifier (N);
18024 New_Id : Entity_Id;
18025 Prev : Entity_Id;
18026 Prev_Par : Node_Id;
18028 procedure Check_Duplicate_Aspects;
18029 -- Check that aspects specified in a completion have not been specified
18030 -- already in the partial view.
18032 procedure Tag_Mismatch;
18033 -- Diagnose a tagged partial view whose full view is untagged. We post
18034 -- the message on the full view, with a reference to the previous
18035 -- partial view. The partial view can be private or incomplete, and
18036 -- these are handled in a different manner, so we determine the position
18037 -- of the error message from the respective slocs of both.
18039 -----------------------------
18040 -- Check_Duplicate_Aspects --
18041 -----------------------------
18043 procedure Check_Duplicate_Aspects is
18044 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
18045 -- Return the corresponding aspect of the partial view which matches
18046 -- the aspect id of Asp. Return Empty is no such aspect exists.
18048 -----------------------------
18049 -- Get_Partial_View_Aspect --
18050 -----------------------------
18052 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
18053 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
18054 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
18055 Prev_Asp : Node_Id;
18057 begin
18058 if Present (Prev_Asps) then
18059 Prev_Asp := First (Prev_Asps);
18060 while Present (Prev_Asp) loop
18061 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
18062 return Prev_Asp;
18063 end if;
18065 Next (Prev_Asp);
18066 end loop;
18067 end if;
18069 return Empty;
18070 end Get_Partial_View_Aspect;
18072 -- Local variables
18074 Full_Asps : constant List_Id := Aspect_Specifications (N);
18075 Full_Asp : Node_Id;
18076 Part_Asp : Node_Id;
18078 -- Start of processing for Check_Duplicate_Aspects
18080 begin
18081 if Present (Full_Asps) then
18082 Full_Asp := First (Full_Asps);
18083 while Present (Full_Asp) loop
18084 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
18086 -- An aspect and its class-wide counterpart are two distinct
18087 -- aspects and may apply to both views of an entity.
18089 if Present (Part_Asp)
18090 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
18091 then
18092 Error_Msg_N
18093 ("aspect already specified in private declaration",
18094 Full_Asp);
18096 Remove (Full_Asp);
18097 return;
18098 end if;
18100 if Has_Discriminants (Prev)
18101 and then not Has_Unknown_Discriminants (Prev)
18102 and then Get_Aspect_Id (Full_Asp) =
18103 Aspect_Implicit_Dereference
18104 then
18105 Error_Msg_N
18106 ("cannot specify aspect if partial view has known "
18107 & "discriminants", Full_Asp);
18108 end if;
18110 Next (Full_Asp);
18111 end loop;
18112 end if;
18113 end Check_Duplicate_Aspects;
18115 ------------------
18116 -- Tag_Mismatch --
18117 ------------------
18119 procedure Tag_Mismatch is
18120 begin
18121 if Sloc (Prev) < Sloc (Id) then
18122 if Ada_Version >= Ada_2012
18123 and then Nkind (N) = N_Private_Type_Declaration
18124 then
18125 Error_Msg_NE
18126 ("declaration of private } must be a tagged type", Id, Prev);
18127 else
18128 Error_Msg_NE
18129 ("full declaration of } must be a tagged type", Id, Prev);
18130 end if;
18132 else
18133 if Ada_Version >= Ada_2012
18134 and then Nkind (N) = N_Private_Type_Declaration
18135 then
18136 Error_Msg_NE
18137 ("declaration of private } must be a tagged type", Prev, Id);
18138 else
18139 Error_Msg_NE
18140 ("full declaration of } must be a tagged type", Prev, Id);
18141 end if;
18142 end if;
18143 end Tag_Mismatch;
18145 -- Start of processing for Find_Type_Name
18147 begin
18148 -- Find incomplete declaration, if one was given
18150 Prev := Current_Entity_In_Scope (Id);
18152 -- New type declaration
18154 if No (Prev) then
18155 Enter_Name (Id);
18156 return Id;
18158 -- Previous declaration exists
18160 else
18161 Prev_Par := Parent (Prev);
18163 -- Error if not incomplete/private case except if previous
18164 -- declaration is implicit, etc. Enter_Name will emit error if
18165 -- appropriate.
18167 if not Is_Incomplete_Or_Private_Type (Prev) then
18168 Enter_Name (Id);
18169 New_Id := Id;
18171 -- Check invalid completion of private or incomplete type
18173 elsif Nkind (N) not in N_Full_Type_Declaration
18174 | N_Task_Type_Declaration
18175 | N_Protected_Type_Declaration
18176 and then
18177 (Ada_Version < Ada_2012
18178 or else not Is_Incomplete_Type (Prev)
18179 or else Nkind (N) not in N_Private_Type_Declaration
18180 | N_Private_Extension_Declaration)
18181 then
18182 -- Completion must be a full type declarations (RM 7.3(4))
18184 Error_Msg_Sloc := Sloc (Prev);
18185 Error_Msg_NE ("invalid completion of }", Id, Prev);
18187 -- Set scope of Id to avoid cascaded errors. Entity is never
18188 -- examined again, except when saving globals in generics.
18190 Set_Scope (Id, Current_Scope);
18191 New_Id := Id;
18193 -- If this is a repeated incomplete declaration, no further
18194 -- checks are possible.
18196 if Nkind (N) = N_Incomplete_Type_Declaration then
18197 return Prev;
18198 end if;
18200 -- Case of full declaration of incomplete type
18202 elsif Ekind (Prev) = E_Incomplete_Type
18203 and then (Ada_Version < Ada_2012
18204 or else No (Full_View (Prev))
18205 or else not Is_Private_Type (Full_View (Prev)))
18206 then
18207 -- Indicate that the incomplete declaration has a matching full
18208 -- declaration. The defining occurrence of the incomplete
18209 -- declaration remains the visible one, and the procedure
18210 -- Get_Full_View dereferences it whenever the type is used.
18212 if Present (Full_View (Prev)) then
18213 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18214 end if;
18216 Set_Full_View (Prev, Id);
18217 Append_Entity (Id, Current_Scope);
18218 Set_Is_Public (Id, Is_Public (Prev));
18219 Set_Is_Internal (Id);
18220 New_Id := Prev;
18222 -- If the incomplete view is tagged, a class_wide type has been
18223 -- created already. Use it for the private type as well, in order
18224 -- to prevent multiple incompatible class-wide types that may be
18225 -- created for self-referential anonymous access components.
18227 if Is_Tagged_Type (Prev)
18228 and then Present (Class_Wide_Type (Prev))
18229 then
18230 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18231 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18233 -- Type of the class-wide type is the current Id. Previously
18234 -- this was not done for private declarations because of order-
18235 -- of-elaboration issues in the back end, but gigi now handles
18236 -- this properly.
18238 Set_Etype (Class_Wide_Type (Id), Id);
18239 end if;
18241 -- Case of full declaration of private type
18243 else
18244 -- If the private type was a completion of an incomplete type then
18245 -- update Prev to reference the private type
18247 if Ada_Version >= Ada_2012
18248 and then Ekind (Prev) = E_Incomplete_Type
18249 and then Present (Full_View (Prev))
18250 and then Is_Private_Type (Full_View (Prev))
18251 then
18252 Prev := Full_View (Prev);
18253 Prev_Par := Parent (Prev);
18254 end if;
18256 if Nkind (N) = N_Full_Type_Declaration
18257 and then Nkind (Type_Definition (N)) in
18258 N_Record_Definition | N_Derived_Type_Definition
18259 and then Interface_Present (Type_Definition (N))
18260 then
18261 Error_Msg_N
18262 ("completion of private type cannot be an interface", N);
18263 end if;
18265 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18266 if Etype (Prev) /= Prev then
18268 -- Prev is a private subtype or a derived type, and needs
18269 -- no completion.
18271 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18272 New_Id := Id;
18274 elsif Ekind (Prev) = E_Private_Type
18275 and then Nkind (N) in N_Task_Type_Declaration
18276 | N_Protected_Type_Declaration
18277 then
18278 Error_Msg_N
18279 ("completion of nonlimited type cannot be limited", N);
18281 elsif Ekind (Prev) = E_Record_Type_With_Private
18282 and then Nkind (N) in N_Task_Type_Declaration
18283 | N_Protected_Type_Declaration
18284 then
18285 if not Is_Limited_Record (Prev) then
18286 Error_Msg_N
18287 ("completion of nonlimited type cannot be limited", N);
18289 elsif No (Interface_List (N)) then
18290 Error_Msg_N
18291 ("completion of tagged private type must be tagged",
18293 end if;
18294 end if;
18296 -- Ada 2005 (AI-251): Private extension declaration of a task
18297 -- type or a protected type. This case arises when covering
18298 -- interface types.
18300 elsif Nkind (N) in N_Task_Type_Declaration
18301 | N_Protected_Type_Declaration
18302 then
18303 null;
18305 elsif Nkind (N) /= N_Full_Type_Declaration
18306 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18307 then
18308 Error_Msg_N
18309 ("full view of private extension must be an extension", N);
18311 elsif not (Abstract_Present (Parent (Prev)))
18312 and then Abstract_Present (Type_Definition (N))
18313 then
18314 Error_Msg_N
18315 ("full view of non-abstract extension cannot be abstract", N);
18316 end if;
18318 if not In_Private_Part (Current_Scope) then
18319 Error_Msg_N
18320 ("declaration of full view must appear in private part", N);
18321 end if;
18323 if Ada_Version >= Ada_2012 then
18324 Check_Duplicate_Aspects;
18325 end if;
18327 Copy_And_Swap (Prev, Id);
18328 Set_Has_Private_Declaration (Prev);
18329 Set_Has_Private_Declaration (Id);
18331 -- AI12-0133: Indicate whether we have a partial view with
18332 -- unknown discriminants, in which case initialization of objects
18333 -- of the type do not receive an invariant check.
18335 Set_Partial_View_Has_Unknown_Discr
18336 (Prev, Has_Unknown_Discriminants (Id));
18338 -- Preserve aspect and iterator flags that may have been set on
18339 -- the partial view.
18341 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18342 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18344 -- If no error, propagate freeze_node from private to full view.
18345 -- It may have been generated for an early operational item.
18347 if Present (Freeze_Node (Id))
18348 and then Serious_Errors_Detected = 0
18349 and then No (Full_View (Id))
18350 then
18351 Set_Freeze_Node (Prev, Freeze_Node (Id));
18352 Set_Freeze_Node (Id, Empty);
18353 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18354 end if;
18356 Set_Full_View (Id, Prev);
18357 New_Id := Prev;
18358 end if;
18360 -- Verify that full declaration conforms to partial one
18362 if Is_Incomplete_Or_Private_Type (Prev)
18363 and then Present (Discriminant_Specifications (Prev_Par))
18364 then
18365 if Present (Discriminant_Specifications (N)) then
18366 if Ekind (Prev) = E_Incomplete_Type then
18367 Check_Discriminant_Conformance (N, Prev, Prev);
18368 else
18369 Check_Discriminant_Conformance (N, Prev, Id);
18370 end if;
18372 else
18373 Error_Msg_N
18374 ("missing discriminants in full type declaration", N);
18376 -- To avoid cascaded errors on subsequent use, share the
18377 -- discriminants of the partial view.
18379 Set_Discriminant_Specifications (N,
18380 Discriminant_Specifications (Prev_Par));
18381 end if;
18382 end if;
18384 -- A prior untagged partial view can have an associated class-wide
18385 -- type due to use of the class attribute, and in this case the full
18386 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18387 -- of incomplete tagged declarations, but we check for it.
18389 if Is_Type (Prev)
18390 and then (Is_Tagged_Type (Prev)
18391 or else Present (Class_Wide_Type (Prev)))
18392 then
18393 -- Ada 2012 (AI05-0162): A private type may be the completion of
18394 -- an incomplete type.
18396 if Ada_Version >= Ada_2012
18397 and then Is_Incomplete_Type (Prev)
18398 and then Nkind (N) in N_Private_Type_Declaration
18399 | N_Private_Extension_Declaration
18400 then
18401 -- No need to check private extensions since they are tagged
18403 if Nkind (N) = N_Private_Type_Declaration
18404 and then not Tagged_Present (N)
18405 then
18406 Tag_Mismatch;
18407 end if;
18409 -- The full declaration is either a tagged type (including
18410 -- a synchronized type that implements interfaces) or a
18411 -- type extension, otherwise this is an error.
18413 elsif Nkind (N) in N_Task_Type_Declaration
18414 | N_Protected_Type_Declaration
18415 then
18416 if No (Interface_List (N)) and then not Error_Posted (N) then
18417 Tag_Mismatch;
18418 end if;
18420 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18422 -- Indicate that the previous declaration (tagged incomplete
18423 -- or private declaration) requires the same on the full one.
18425 if not Tagged_Present (Type_Definition (N)) then
18426 Tag_Mismatch;
18427 Set_Is_Tagged_Type (Id);
18428 end if;
18430 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18431 if No (Record_Extension_Part (Type_Definition (N))) then
18432 Error_Msg_NE
18433 ("full declaration of } must be a record extension",
18434 Prev, Id);
18436 -- Set some attributes to produce a usable full view
18438 Set_Is_Tagged_Type (Id);
18439 end if;
18441 else
18442 Tag_Mismatch;
18443 end if;
18444 end if;
18446 if Present (Prev)
18447 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18448 and then Present (Premature_Use (Parent (Prev)))
18449 then
18450 Error_Msg_Sloc := Sloc (N);
18451 Error_Msg_N
18452 ("\full declaration #", Premature_Use (Parent (Prev)));
18453 end if;
18455 return New_Id;
18456 end if;
18457 end Find_Type_Name;
18459 -------------------------
18460 -- Find_Type_Of_Object --
18461 -------------------------
18463 function Find_Type_Of_Object
18464 (Obj_Def : Node_Id;
18465 Related_Nod : Node_Id) return Entity_Id
18467 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18468 P : Node_Id := Parent (Obj_Def);
18469 T : Entity_Id;
18470 Nam : Name_Id;
18472 begin
18473 -- If the parent is a component_definition node we climb to the
18474 -- component_declaration node.
18476 if Nkind (P) = N_Component_Definition then
18477 P := Parent (P);
18478 end if;
18480 -- Case of an anonymous array subtype
18482 if Def_Kind in N_Array_Type_Definition then
18483 T := Empty;
18484 Array_Type_Declaration (T, Obj_Def);
18486 -- Create an explicit subtype whenever possible
18488 elsif Nkind (P) /= N_Component_Declaration
18489 and then Def_Kind = N_Subtype_Indication
18490 then
18491 -- Base name of subtype on object name, which will be unique in
18492 -- the current scope.
18494 -- If this is a duplicate declaration, return base type, to avoid
18495 -- generating duplicate anonymous types.
18497 if Error_Posted (P) then
18498 Analyze (Subtype_Mark (Obj_Def));
18499 return Entity (Subtype_Mark (Obj_Def));
18500 end if;
18502 Nam :=
18503 New_External_Name
18504 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18506 T := Make_Defining_Identifier (Sloc (P), Nam);
18508 -- If In_Spec_Expression, for example within a pre/postcondition,
18509 -- provide enough information for use of the subtype without
18510 -- depending on full analysis and freezing, which will happen when
18511 -- building the corresponding subprogram.
18513 if In_Spec_Expression then
18514 Analyze (Subtype_Mark (Obj_Def));
18516 declare
18517 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18518 New_Def : constant Node_Id := New_Copy_Tree (Obj_Def);
18519 Decl : constant Node_Id :=
18520 Make_Subtype_Declaration (Sloc (P),
18521 Defining_Identifier => T,
18522 Subtype_Indication => New_Def);
18524 begin
18525 Set_Etype (T, Base_T);
18526 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18527 Set_Parent (T, Decl);
18528 Set_Scope (T, Current_Scope);
18530 if Ekind (T) = E_Array_Subtype then
18531 Constrain_Array (T, New_Def, Related_Nod, T, 'P');
18533 elsif Ekind (T) = E_Record_Subtype then
18534 Set_First_Entity (T, First_Entity (Base_T));
18535 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18536 Set_Is_Constrained (T);
18537 end if;
18539 Insert_Before (Related_Nod, Decl);
18540 end;
18542 return T;
18543 end if;
18545 -- When generating code, insert subtype declaration ahead of
18546 -- declaration that generated it.
18548 Insert_Action (Obj_Def,
18549 Make_Subtype_Declaration (Sloc (P),
18550 Defining_Identifier => T,
18551 Subtype_Indication => Relocate_Node (Obj_Def)));
18553 -- This subtype may need freezing, and this will not be done
18554 -- automatically if the object declaration is not in declarative
18555 -- part. Since this is an object declaration, the type cannot always
18556 -- be frozen here. Deferred constants do not freeze their type
18557 -- (which often enough will be private).
18559 if Nkind (P) = N_Object_Declaration
18560 and then Constant_Present (P)
18561 and then No (Expression (P))
18562 then
18563 null;
18565 -- Here we freeze the base type of object type to catch premature use
18566 -- of discriminated private type without a full view.
18568 else
18569 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18570 end if;
18572 -- Ada 2005 AI-406: the object definition in an object declaration
18573 -- can be an access definition.
18575 elsif Def_Kind = N_Access_Definition then
18576 T := Access_Definition (Related_Nod, Obj_Def);
18578 Set_Is_Local_Anonymous_Access
18579 (T, Ada_Version < Ada_2012
18580 or else Nkind (P) /= N_Object_Declaration
18581 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18583 -- Otherwise, the object definition is just a subtype_mark
18585 else
18586 T := Process_Subtype (Obj_Def, Related_Nod);
18587 end if;
18589 return T;
18590 end Find_Type_Of_Object;
18592 --------------------------------
18593 -- Find_Type_Of_Subtype_Indic --
18594 --------------------------------
18596 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18597 Typ : Entity_Id;
18599 begin
18600 -- Case of subtype mark with a constraint
18602 if Nkind (S) = N_Subtype_Indication then
18603 Find_Type (Subtype_Mark (S));
18604 Typ := Entity (Subtype_Mark (S));
18606 if not
18607 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18608 then
18609 Error_Msg_N
18610 ("incorrect constraint for this kind of type", Constraint (S));
18611 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18612 end if;
18614 -- Otherwise we have a subtype mark without a constraint
18616 elsif Error_Posted (S) then
18617 -- Don't rewrite if S is Empty or Error
18618 if S > Empty_Or_Error then
18619 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18620 end if;
18621 return Any_Type;
18623 else
18624 Find_Type (S);
18625 Typ := Entity (S);
18626 end if;
18628 return Typ;
18629 end Find_Type_Of_Subtype_Indic;
18631 -------------------------------------
18632 -- Floating_Point_Type_Declaration --
18633 -------------------------------------
18635 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18636 Digs : constant Node_Id := Digits_Expression (Def);
18637 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18638 Digs_Val : Uint;
18639 Base_Typ : Entity_Id;
18640 Implicit_Base : Entity_Id;
18642 function Can_Derive_From (E : Entity_Id) return Boolean;
18643 -- Find if given digits value, and possibly a specified range, allows
18644 -- derivation from specified type
18646 procedure Convert_Bound (B : Node_Id);
18647 -- If specified, the bounds must be static but may be of different
18648 -- types. They must be converted into machine numbers of the base type,
18649 -- in accordance with RM 4.9(38).
18651 function Find_Base_Type return Entity_Id;
18652 -- Find a predefined base type that Def can derive from, or generate
18653 -- an error and substitute Long_Long_Float if none exists.
18655 ---------------------
18656 -- Can_Derive_From --
18657 ---------------------
18659 function Can_Derive_From (E : Entity_Id) return Boolean is
18660 Spec : constant Entity_Id := Real_Range_Specification (Def);
18662 begin
18663 -- Check specified "digits" constraint
18665 if Digs_Val > Digits_Value (E) then
18666 return False;
18667 end if;
18669 -- Check for matching range, if specified
18671 if Present (Spec) then
18672 if Expr_Value_R (Type_Low_Bound (E)) >
18673 Expr_Value_R (Low_Bound (Spec))
18674 then
18675 return False;
18676 end if;
18678 if Expr_Value_R (Type_High_Bound (E)) <
18679 Expr_Value_R (High_Bound (Spec))
18680 then
18681 return False;
18682 end if;
18683 end if;
18685 return True;
18686 end Can_Derive_From;
18688 -------------------
18689 -- Convert_Bound --
18690 --------------------
18692 procedure Convert_Bound (B : Node_Id) is
18693 begin
18694 -- If the bound is not a literal it can only be static if it is
18695 -- a static constant, possibly of a specified type.
18697 if Is_Entity_Name (B)
18698 and then Ekind (Entity (B)) = E_Constant
18699 then
18700 Rewrite (B, Constant_Value (Entity (B)));
18701 end if;
18703 if Nkind (B) = N_Real_Literal then
18704 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18705 Set_Is_Machine_Number (B);
18706 Set_Etype (B, Base_Typ);
18707 end if;
18708 end Convert_Bound;
18710 --------------------
18711 -- Find_Base_Type --
18712 --------------------
18714 function Find_Base_Type return Entity_Id is
18715 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18717 begin
18718 -- Iterate over the predefined types in order, returning the first
18719 -- one that Def can derive from.
18721 while Present (Choice) loop
18722 if Can_Derive_From (Node (Choice)) then
18723 return Node (Choice);
18724 end if;
18726 Next_Elmt (Choice);
18727 end loop;
18729 -- If we can't derive from any existing type, use Long_Long_Float
18730 -- and give appropriate message explaining the problem.
18732 if Digs_Val > Max_Digs_Val then
18733 -- It might be the case that there is a type with the requested
18734 -- range, just not the combination of digits and range.
18736 Error_Msg_N
18737 ("no predefined type has requested range and precision",
18738 Real_Range_Specification (Def));
18740 else
18741 Error_Msg_N
18742 ("range too large for any predefined type",
18743 Real_Range_Specification (Def));
18744 end if;
18746 return Standard_Long_Long_Float;
18747 end Find_Base_Type;
18749 -- Start of processing for Floating_Point_Type_Declaration
18751 begin
18752 Check_Restriction (No_Floating_Point, Def);
18754 -- Create an implicit base type
18756 Implicit_Base :=
18757 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18759 -- Analyze and verify digits value
18761 Analyze_And_Resolve (Digs, Any_Integer);
18762 Check_Digits_Expression (Digs);
18763 Digs_Val := Expr_Value (Digs);
18765 -- Process possible range spec and find correct type to derive from
18767 Process_Real_Range_Specification (Def);
18769 -- Check that requested number of digits is not too high.
18771 if Digs_Val > Max_Digs_Val then
18773 -- The check for Max_Base_Digits may be somewhat expensive, as it
18774 -- requires reading System, so only do it when necessary.
18776 declare
18777 Max_Base_Digits : constant Uint :=
18778 Expr_Value
18779 (Expression
18780 (Parent (RTE (RE_Max_Base_Digits))));
18782 begin
18783 if Digs_Val > Max_Base_Digits then
18784 Error_Msg_Uint_1 := Max_Base_Digits;
18785 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18787 elsif No (Real_Range_Specification (Def)) then
18788 Error_Msg_Uint_1 := Max_Digs_Val;
18789 Error_Msg_N ("types with more than ^ digits need range spec "
18790 & "(RM 3.5.7(6))", Digs);
18791 end if;
18792 end;
18793 end if;
18795 -- Find a suitable type to derive from or complain and use a substitute
18797 Base_Typ := Find_Base_Type;
18799 -- If there are bounds given in the declaration use them as the bounds
18800 -- of the type, otherwise use the bounds of the predefined base type
18801 -- that was chosen based on the Digits value.
18803 if Present (Real_Range_Specification (Def)) then
18804 Set_Scalar_Range (T, Real_Range_Specification (Def));
18805 Set_Is_Constrained (T);
18807 Convert_Bound (Type_Low_Bound (T));
18808 Convert_Bound (Type_High_Bound (T));
18810 else
18811 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18812 end if;
18814 -- Complete definition of implicit base and declared first subtype. The
18815 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18816 -- are not clobbered when the floating point type acts as a full view of
18817 -- a private type.
18819 Set_Etype (Implicit_Base, Base_Typ);
18820 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18821 Set_Size_Info (Implicit_Base, Base_Typ);
18822 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18823 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18824 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18825 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18827 Mutate_Ekind (T, E_Floating_Point_Subtype);
18828 Set_Etype (T, Implicit_Base);
18829 Set_Size_Info (T, Implicit_Base);
18830 Set_RM_Size (T, RM_Size (Implicit_Base));
18831 Inherit_Rep_Item_Chain (T, Implicit_Base);
18833 if Digs_Val >= Uint_1 then
18834 Set_Digits_Value (T, Digs_Val);
18835 else
18836 pragma Assert (Serious_Errors_Detected > 0); null;
18837 end if;
18838 end Floating_Point_Type_Declaration;
18840 ----------------------------
18841 -- Get_Discriminant_Value --
18842 ----------------------------
18844 -- This is the situation:
18846 -- There is a non-derived type
18848 -- type T0 (Dx, Dy, Dz...)
18850 -- There are zero or more levels of derivation, with each derivation
18851 -- either purely inheriting the discriminants, or defining its own.
18853 -- type Ti is new Ti-1
18854 -- or
18855 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18856 -- or
18857 -- subtype Ti is ...
18859 -- The subtype issue is avoided by the use of Original_Record_Component,
18860 -- and the fact that derived subtypes also derive the constraints.
18862 -- This chain leads back from
18864 -- Typ_For_Constraint
18866 -- Typ_For_Constraint has discriminants, and the value for each
18867 -- discriminant is given by its corresponding Elmt of Constraints.
18869 -- Discriminant is some discriminant in this hierarchy
18871 -- We need to return its value
18873 -- We do this by recursively searching each level, and looking for
18874 -- Discriminant. Once we get to the bottom, we start backing up
18875 -- returning the value for it which may in turn be a discriminant
18876 -- further up, so on the backup we continue the substitution.
18878 function Get_Discriminant_Value
18879 (Discriminant : Entity_Id;
18880 Typ_For_Constraint : Entity_Id;
18881 Constraint : Elist_Id) return Node_Id
18883 function Root_Corresponding_Discriminant
18884 (Discr : Entity_Id) return Entity_Id;
18885 -- Given a discriminant, traverse the chain of inherited discriminants
18886 -- and return the topmost discriminant.
18888 function Search_Derivation_Levels
18889 (Ti : Entity_Id;
18890 Discrim_Values : Elist_Id;
18891 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18892 -- This is the routine that performs the recursive search of levels
18893 -- as described above.
18895 -------------------------------------
18896 -- Root_Corresponding_Discriminant --
18897 -------------------------------------
18899 function Root_Corresponding_Discriminant
18900 (Discr : Entity_Id) return Entity_Id
18902 D : Entity_Id;
18904 begin
18905 D := Discr;
18906 while Present (Corresponding_Discriminant (D)) loop
18907 D := Corresponding_Discriminant (D);
18908 end loop;
18910 return D;
18911 end Root_Corresponding_Discriminant;
18913 ------------------------------
18914 -- Search_Derivation_Levels --
18915 ------------------------------
18917 function Search_Derivation_Levels
18918 (Ti : Entity_Id;
18919 Discrim_Values : Elist_Id;
18920 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18922 Assoc : Elmt_Id;
18923 Disc : Entity_Id;
18924 Result : Node_Or_Entity_Id;
18925 Result_Entity : Node_Id;
18927 begin
18928 -- If inappropriate type, return Error, this happens only in
18929 -- cascaded error situations, and we want to avoid a blow up.
18931 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18932 return Error;
18933 end if;
18935 -- Look deeper if possible. Use Stored_Constraints only for
18936 -- untagged types. For tagged types use the given constraint.
18937 -- This asymmetry needs explanation???
18939 if not Stored_Discrim_Values
18940 and then Present (Stored_Constraint (Ti))
18941 and then not Is_Tagged_Type (Ti)
18942 then
18943 Result :=
18944 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18946 else
18947 declare
18948 Td : Entity_Id := Etype (Ti);
18950 begin
18951 -- If the parent type is private, the full view may include
18952 -- renamed discriminants, and it is those stored values that
18953 -- may be needed (the partial view never has more information
18954 -- than the full view).
18956 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18957 Td := Full_View (Td);
18958 end if;
18960 if Td = Ti then
18961 Result := Discriminant;
18963 else
18964 if Present (Stored_Constraint (Ti)) then
18965 Result :=
18966 Search_Derivation_Levels
18967 (Td, Stored_Constraint (Ti), True);
18968 else
18969 Result :=
18970 Search_Derivation_Levels
18971 (Td, Discrim_Values, Stored_Discrim_Values);
18972 end if;
18973 end if;
18974 end;
18975 end if;
18977 -- Extra underlying places to search, if not found above. For
18978 -- concurrent types, the relevant discriminant appears in the
18979 -- corresponding record. For a type derived from a private type
18980 -- without discriminant, the full view inherits the discriminants
18981 -- of the full view of the parent.
18983 if Result = Discriminant then
18984 if Is_Concurrent_Type (Ti)
18985 and then Present (Corresponding_Record_Type (Ti))
18986 then
18987 Result :=
18988 Search_Derivation_Levels (
18989 Corresponding_Record_Type (Ti),
18990 Discrim_Values,
18991 Stored_Discrim_Values);
18993 elsif Is_Private_Type (Ti)
18994 and then not Has_Discriminants (Ti)
18995 and then Present (Full_View (Ti))
18996 and then Etype (Full_View (Ti)) /= Ti
18997 then
18998 Result :=
18999 Search_Derivation_Levels (
19000 Full_View (Ti),
19001 Discrim_Values,
19002 Stored_Discrim_Values);
19003 end if;
19004 end if;
19006 -- If Result is not a (reference to a) discriminant, return it,
19007 -- otherwise set Result_Entity to the discriminant.
19009 if Nkind (Result) = N_Defining_Identifier then
19010 pragma Assert (Result = Discriminant);
19011 Result_Entity := Result;
19013 else
19014 if not Denotes_Discriminant (Result) then
19015 return Result;
19016 end if;
19018 Result_Entity := Entity (Result);
19019 end if;
19021 -- See if this level of derivation actually has discriminants because
19022 -- tagged derivations can add them, hence the lower levels need not
19023 -- have any.
19025 if not Has_Discriminants (Ti) then
19026 return Result;
19027 end if;
19029 -- Scan Ti's discriminants for Result_Entity, and return its
19030 -- corresponding value, if any.
19032 Result_Entity := Original_Record_Component (Result_Entity);
19034 Assoc := First_Elmt (Discrim_Values);
19036 if Stored_Discrim_Values then
19037 Disc := First_Stored_Discriminant (Ti);
19038 else
19039 Disc := First_Discriminant (Ti);
19040 end if;
19042 while Present (Disc) loop
19044 -- If no further associations return the discriminant, value will
19045 -- be found on the second pass.
19047 if No (Assoc) then
19048 return Result;
19049 end if;
19051 if Original_Record_Component (Disc) = Result_Entity then
19052 return Node (Assoc);
19053 end if;
19055 Next_Elmt (Assoc);
19057 if Stored_Discrim_Values then
19058 Next_Stored_Discriminant (Disc);
19059 else
19060 Next_Discriminant (Disc);
19061 end if;
19062 end loop;
19064 -- Could not find it
19066 return Result;
19067 end Search_Derivation_Levels;
19069 -- Local Variables
19071 Result : Node_Or_Entity_Id;
19073 -- Start of processing for Get_Discriminant_Value
19075 begin
19076 -- ??? This routine is a gigantic mess and will be deleted. For the
19077 -- time being just test for the trivial case before calling recurse.
19079 -- We are now celebrating the 20th anniversary of this comment!
19081 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
19082 declare
19083 D : Entity_Id;
19084 E : Elmt_Id;
19086 begin
19087 D := First_Discriminant (Typ_For_Constraint);
19088 E := First_Elmt (Constraint);
19089 while Present (D) loop
19090 if Chars (D) = Chars (Discriminant) then
19091 return Node (E);
19092 end if;
19094 Next_Discriminant (D);
19095 Next_Elmt (E);
19096 end loop;
19097 end;
19098 end if;
19100 Result := Search_Derivation_Levels
19101 (Typ_For_Constraint, Constraint, False);
19103 -- ??? hack to disappear when this routine is gone
19105 if Nkind (Result) = N_Defining_Identifier then
19106 declare
19107 D : Entity_Id;
19108 E : Elmt_Id;
19110 begin
19111 D := First_Discriminant (Typ_For_Constraint);
19112 E := First_Elmt (Constraint);
19113 while Present (D) loop
19114 if Root_Corresponding_Discriminant (D) = Discriminant then
19115 return Node (E);
19116 end if;
19118 Next_Discriminant (D);
19119 Next_Elmt (E);
19120 end loop;
19121 end;
19122 end if;
19124 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19125 return Result;
19126 end Get_Discriminant_Value;
19128 --------------------------
19129 -- Has_Range_Constraint --
19130 --------------------------
19132 function Has_Range_Constraint (N : Node_Id) return Boolean is
19133 C : constant Node_Id := Constraint (N);
19135 begin
19136 if Nkind (C) = N_Range_Constraint then
19137 return True;
19139 elsif Nkind (C) = N_Digits_Constraint then
19140 return
19141 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19142 or else Present (Range_Constraint (C));
19144 elsif Nkind (C) = N_Delta_Constraint then
19145 return Present (Range_Constraint (C));
19147 else
19148 return False;
19149 end if;
19150 end Has_Range_Constraint;
19152 ------------------------
19153 -- Inherit_Components --
19154 ------------------------
19156 function Inherit_Components
19157 (N : Node_Id;
19158 Parent_Base : Entity_Id;
19159 Derived_Base : Entity_Id;
19160 Is_Tagged : Boolean;
19161 Inherit_Discr : Boolean;
19162 Discs : Elist_Id) return Elist_Id
19164 Assoc_List : constant Elist_Id := New_Elmt_List;
19166 procedure Inherit_Component
19167 (Old_C : Entity_Id;
19168 Plain_Discrim : Boolean := False;
19169 Stored_Discrim : Boolean := False);
19170 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19171 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19172 -- True, Old_C is a stored discriminant. If they are both false then
19173 -- Old_C is a regular component.
19175 -----------------------
19176 -- Inherit_Component --
19177 -----------------------
19179 procedure Inherit_Component
19180 (Old_C : Entity_Id;
19181 Plain_Discrim : Boolean := False;
19182 Stored_Discrim : Boolean := False)
19184 procedure Set_Anonymous_Type (Id : Entity_Id);
19185 -- Id denotes the entity of an access discriminant or anonymous
19186 -- access component. Set the type of Id to either the same type of
19187 -- Old_C or create a new one depending on whether the parent and
19188 -- the child types are in the same scope.
19190 ------------------------
19191 -- Set_Anonymous_Type --
19192 ------------------------
19194 procedure Set_Anonymous_Type (Id : Entity_Id) is
19195 Old_Typ : constant Entity_Id := Etype (Old_C);
19197 begin
19198 if Scope (Parent_Base) = Scope (Derived_Base) then
19199 Set_Etype (Id, Old_Typ);
19201 -- The parent and the derived type are in two different scopes.
19202 -- Reuse the type of the original discriminant / component by
19203 -- copying it in order to preserve all attributes.
19205 else
19206 declare
19207 Typ : constant Entity_Id := New_Copy (Old_Typ);
19209 begin
19210 Set_Etype (Id, Typ);
19212 -- Since we do not generate component declarations for
19213 -- inherited components, associate the itype with the
19214 -- derived type.
19216 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19217 Set_Scope (Typ, Derived_Base);
19218 end;
19219 end if;
19220 end Set_Anonymous_Type;
19222 -- Local variables and constants
19224 New_C : constant Entity_Id := New_Copy (Old_C);
19226 Corr_Discrim : Entity_Id;
19227 Discrim : Entity_Id;
19229 -- Start of processing for Inherit_Component
19231 begin
19232 pragma Assert (not Is_Tagged or not Stored_Discrim);
19234 Set_Parent (New_C, Parent (Old_C));
19236 -- Regular discriminants and components must be inserted in the scope
19237 -- of the Derived_Base. Do it here.
19239 if not Stored_Discrim then
19240 Enter_Name (New_C);
19241 end if;
19243 -- For tagged types the Original_Record_Component must point to
19244 -- whatever this field was pointing to in the parent type. This has
19245 -- already been achieved by the call to New_Copy above.
19247 if not Is_Tagged then
19248 Set_Original_Record_Component (New_C, New_C);
19249 Set_Corresponding_Record_Component (New_C, Old_C);
19250 end if;
19252 -- Set the proper type of an access discriminant
19254 if Ekind (New_C) = E_Discriminant
19255 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19256 then
19257 Set_Anonymous_Type (New_C);
19258 end if;
19260 -- If we have inherited a component then see if its Etype contains
19261 -- references to Parent_Base discriminants. In this case, replace
19262 -- these references with the constraints given in Discs. We do not
19263 -- do this for the partial view of private types because this is
19264 -- not needed (only the components of the full view will be used
19265 -- for code generation) and cause problem. We also avoid this
19266 -- transformation in some error situations.
19268 if Ekind (New_C) = E_Component then
19270 -- Set the proper type of an anonymous access component
19272 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19273 Set_Anonymous_Type (New_C);
19275 elsif (Is_Private_Type (Derived_Base)
19276 and then not Is_Generic_Type (Derived_Base))
19277 or else (Is_Empty_Elmt_List (Discs)
19278 and then not Expander_Active)
19279 then
19280 Set_Etype (New_C, Etype (Old_C));
19282 else
19283 -- The current component introduces a circularity of the
19284 -- following kind:
19286 -- limited with Pack_2;
19287 -- package Pack_1 is
19288 -- type T_1 is tagged record
19289 -- Comp : access Pack_2.T_2;
19290 -- ...
19291 -- end record;
19292 -- end Pack_1;
19294 -- with Pack_1;
19295 -- package Pack_2 is
19296 -- type T_2 is new Pack_1.T_1 with ...;
19297 -- end Pack_2;
19299 Set_Etype
19300 (New_C,
19301 Constrain_Component_Type
19302 (Old_C, Derived_Base, N, Parent_Base, Discs));
19303 end if;
19304 end if;
19306 if Plain_Discrim then
19307 Set_Corresponding_Discriminant (New_C, Old_C);
19308 Build_Discriminal (New_C);
19310 -- If we are explicitly inheriting a stored discriminant it will be
19311 -- completely hidden.
19313 elsif Stored_Discrim then
19314 Set_Corresponding_Discriminant (New_C, Empty);
19315 Set_Discriminal (New_C, Empty);
19316 Set_Is_Completely_Hidden (New_C);
19318 -- Set the Original_Record_Component of each discriminant in the
19319 -- derived base to point to the corresponding stored that we just
19320 -- created.
19322 Discrim := First_Discriminant (Derived_Base);
19323 while Present (Discrim) loop
19324 Corr_Discrim := Corresponding_Discriminant (Discrim);
19326 -- Corr_Discrim could be missing in an error situation
19328 if Present (Corr_Discrim)
19329 and then Original_Record_Component (Corr_Discrim) = Old_C
19330 then
19331 Set_Original_Record_Component (Discrim, New_C);
19332 Set_Corresponding_Record_Component (Discrim, Empty);
19333 end if;
19335 Next_Discriminant (Discrim);
19336 end loop;
19338 Append_Entity (New_C, Derived_Base);
19339 end if;
19341 if not Is_Tagged then
19342 Append_Elmt (Old_C, Assoc_List);
19343 Append_Elmt (New_C, Assoc_List);
19344 end if;
19345 end Inherit_Component;
19347 -- Variables local to Inherit_Component
19349 Loc : constant Source_Ptr := Sloc (N);
19351 Parent_Discrim : Entity_Id;
19352 Stored_Discrim : Entity_Id;
19353 D : Entity_Id;
19354 Component : Entity_Id;
19356 -- Start of processing for Inherit_Components
19358 begin
19359 if not Is_Tagged then
19360 Append_Elmt (Parent_Base, Assoc_List);
19361 Append_Elmt (Derived_Base, Assoc_List);
19362 end if;
19364 -- Inherit parent discriminants if needed
19366 if Inherit_Discr then
19367 Parent_Discrim := First_Discriminant (Parent_Base);
19368 while Present (Parent_Discrim) loop
19369 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19370 Next_Discriminant (Parent_Discrim);
19371 end loop;
19372 end if;
19374 -- Create explicit stored discrims for untagged types when necessary
19376 if not Has_Unknown_Discriminants (Derived_Base)
19377 and then Has_Discriminants (Parent_Base)
19378 and then not Is_Tagged
19379 and then
19380 (not Inherit_Discr
19381 or else First_Discriminant (Parent_Base) /=
19382 First_Stored_Discriminant (Parent_Base))
19383 then
19384 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19385 while Present (Stored_Discrim) loop
19386 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19387 Next_Stored_Discriminant (Stored_Discrim);
19388 end loop;
19389 end if;
19391 -- See if we can apply the second transformation for derived types, as
19392 -- explained in point 6. in the comments above Build_Derived_Record_Type
19393 -- This is achieved by appending Derived_Base discriminants into Discs,
19394 -- which has the side effect of returning a non empty Discs list to the
19395 -- caller of Inherit_Components, which is what we want. This must be
19396 -- done for private derived types if there are explicit stored
19397 -- discriminants, to ensure that we can retrieve the values of the
19398 -- constraints provided in the ancestors.
19400 if Inherit_Discr
19401 and then Is_Empty_Elmt_List (Discs)
19402 and then Present (First_Discriminant (Derived_Base))
19403 and then
19404 (not Is_Private_Type (Derived_Base)
19405 or else Is_Completely_Hidden
19406 (First_Stored_Discriminant (Derived_Base))
19407 or else Is_Generic_Type (Derived_Base))
19408 then
19409 D := First_Discriminant (Derived_Base);
19410 while Present (D) loop
19411 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19412 Next_Discriminant (D);
19413 end loop;
19414 end if;
19416 -- Finally, inherit non-discriminant components unless they are not
19417 -- visible because defined or inherited from the full view of the
19418 -- parent. Don't inherit the _parent field of the parent type.
19420 Component := First_Entity (Parent_Base);
19421 while Present (Component) loop
19423 -- Ada 2005 (AI-251): Do not inherit components associated with
19424 -- secondary tags of the parent.
19426 if Ekind (Component) = E_Component
19427 and then Present (Related_Type (Component))
19428 then
19429 null;
19431 elsif Ekind (Component) /= E_Component
19432 or else Chars (Component) = Name_uParent
19433 then
19434 null;
19436 -- If the derived type is within the parent type's declarative
19437 -- region, then the components can still be inherited even though
19438 -- they aren't visible at this point. This can occur for cases
19439 -- such as within public child units where the components must
19440 -- become visible upon entering the child unit's private part.
19442 elsif not Is_Visible_Component (Component)
19443 and then not In_Open_Scopes (Scope (Parent_Base))
19444 then
19445 null;
19447 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19448 then
19449 null;
19451 else
19452 Inherit_Component (Component);
19453 end if;
19455 Next_Entity (Component);
19456 end loop;
19458 -- For tagged derived types, inherited discriminants cannot be used in
19459 -- component declarations of the record extension part. To achieve this
19460 -- we mark the inherited discriminants as not visible.
19462 if Is_Tagged and then Inherit_Discr then
19463 D := First_Discriminant (Derived_Base);
19464 while Present (D) loop
19465 Set_Is_Immediately_Visible (D, False);
19466 Next_Discriminant (D);
19467 end loop;
19468 end if;
19470 return Assoc_List;
19471 end Inherit_Components;
19473 ----------------------
19474 -- Is_EVF_Procedure --
19475 ----------------------
19477 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19478 Formal : Entity_Id;
19480 begin
19481 -- Examine the formals of an Extensions_Visible False procedure looking
19482 -- for a controlling OUT parameter.
19484 if Ekind (Subp) = E_Procedure
19485 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19486 then
19487 Formal := First_Formal (Subp);
19488 while Present (Formal) loop
19489 if Ekind (Formal) = E_Out_Parameter
19490 and then Is_Controlling_Formal (Formal)
19491 then
19492 return True;
19493 end if;
19495 Next_Formal (Formal);
19496 end loop;
19497 end if;
19499 return False;
19500 end Is_EVF_Procedure;
19502 --------------------------
19503 -- Is_Private_Primitive --
19504 --------------------------
19506 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19507 Prim_Scope : constant Entity_Id := Scope (Prim);
19508 Priv_Entity : Entity_Id;
19509 begin
19510 if Is_Package_Or_Generic_Package (Prim_Scope) then
19511 Priv_Entity := First_Private_Entity (Prim_Scope);
19513 while Present (Priv_Entity) loop
19514 if Priv_Entity = Prim then
19515 return True;
19516 end if;
19518 Next_Entity (Priv_Entity);
19519 end loop;
19520 end if;
19522 return False;
19523 end Is_Private_Primitive;
19525 ------------------------------
19526 -- Is_Valid_Constraint_Kind --
19527 ------------------------------
19529 function Is_Valid_Constraint_Kind
19530 (T_Kind : Type_Kind;
19531 Constraint_Kind : Node_Kind) return Boolean
19533 begin
19534 case T_Kind is
19535 when Enumeration_Kind
19536 | Integer_Kind
19538 return Constraint_Kind = N_Range_Constraint;
19540 when Decimal_Fixed_Point_Kind =>
19541 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19543 when Ordinary_Fixed_Point_Kind =>
19544 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19546 when Float_Kind =>
19547 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19549 when Access_Kind
19550 | Array_Kind
19551 | Class_Wide_Kind
19552 | Concurrent_Kind
19553 | Private_Kind
19554 | E_Incomplete_Type
19555 | E_Record_Subtype
19556 | E_Record_Type
19558 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19560 when others =>
19561 return True; -- Error will be detected later
19562 end case;
19563 end Is_Valid_Constraint_Kind;
19565 --------------------------
19566 -- Is_Visible_Component --
19567 --------------------------
19569 function Is_Visible_Component
19570 (C : Entity_Id;
19571 N : Node_Id := Empty) return Boolean
19573 Original_Comp : Entity_Id := Empty;
19574 Original_Type : Entity_Id;
19575 Type_Scope : Entity_Id;
19577 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19578 -- Check whether parent type of inherited component is declared locally,
19579 -- possibly within a nested package or instance. The current scope is
19580 -- the derived record itself.
19582 -------------------
19583 -- Is_Local_Type --
19584 -------------------
19586 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19587 begin
19588 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19589 end Is_Local_Type;
19591 -- Start of processing for Is_Visible_Component
19593 begin
19594 if Ekind (C) in E_Component | E_Discriminant then
19595 Original_Comp := Original_Record_Component (C);
19596 end if;
19598 if No (Original_Comp) then
19600 -- Premature usage, or previous error
19602 return False;
19604 else
19605 Original_Type := Scope (Original_Comp);
19606 Type_Scope := Scope (Base_Type (Scope (C)));
19607 end if;
19609 -- This test only concerns tagged types
19611 if not Is_Tagged_Type (Original_Type) then
19613 -- Check if this is a renamed discriminant (hidden either by the
19614 -- derived type or by some ancestor), unless we are analyzing code
19615 -- generated by the expander since it may reference such components
19616 -- (for example see the expansion of Deep_Adjust).
19618 if Ekind (C) = E_Discriminant and then Present (N) then
19619 return
19620 not Comes_From_Source (N)
19621 or else not Is_Completely_Hidden (C);
19622 else
19623 return True;
19624 end if;
19626 -- If it is _Parent or _Tag, there is no visibility issue
19628 elsif not Comes_From_Source (Original_Comp) then
19629 return True;
19631 -- Discriminants are visible unless the (private) type has unknown
19632 -- discriminants. If the discriminant reference is inserted for a
19633 -- discriminant check on a full view it is also visible.
19635 elsif Ekind (Original_Comp) = E_Discriminant
19636 and then
19637 (not Has_Unknown_Discriminants (Original_Type)
19638 or else (Present (N)
19639 and then Nkind (N) = N_Selected_Component
19640 and then Nkind (Prefix (N)) = N_Type_Conversion
19641 and then not Comes_From_Source (Prefix (N))))
19642 then
19643 return True;
19645 -- If the component has been declared in an ancestor which is currently
19646 -- a private type, then it is not visible. The same applies if the
19647 -- component's containing type is not in an open scope and the original
19648 -- component's enclosing type is a visible full view of a private type
19649 -- (which can occur in cases where an attempt is being made to reference
19650 -- a component in a sibling package that is inherited from a visible
19651 -- component of a type in an ancestor package; the component in the
19652 -- sibling package should not be visible even though the component it
19653 -- inherited from is visible), but instance bodies are not subject to
19654 -- this second case since they have the Has_Private_View mechanism to
19655 -- ensure proper visibility. This does not apply however in the case
19656 -- where the scope of the type is a private child unit, or when the
19657 -- parent comes from a local package in which the ancestor is currently
19658 -- visible. The latter suppression of visibility is needed for cases
19659 -- that are tested in B730006.
19661 elsif Is_Private_Type (Original_Type)
19662 or else
19663 (not Is_Private_Descendant (Type_Scope)
19664 and then not In_Open_Scopes (Type_Scope)
19665 and then Has_Private_Declaration (Original_Type)
19666 and then not In_Instance_Body)
19667 then
19668 -- If the type derives from an entity in a formal package, there
19669 -- are no additional visible components.
19671 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19672 N_Formal_Package_Declaration
19673 then
19674 return False;
19676 -- if we are not in the private part of the current package, there
19677 -- are no additional visible components.
19679 elsif Ekind (Scope (Current_Scope)) = E_Package
19680 and then not In_Private_Part (Scope (Current_Scope))
19681 then
19682 return False;
19683 else
19684 return
19685 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19686 and then In_Open_Scopes (Scope (Original_Type))
19687 and then Is_Local_Type (Type_Scope);
19688 end if;
19690 -- There is another weird way in which a component may be invisible when
19691 -- the private and the full view are not derived from the same ancestor.
19692 -- Here is an example :
19694 -- type A1 is tagged record F1 : integer; end record;
19695 -- type A2 is new A1 with record F2 : integer; end record;
19696 -- type T is new A1 with private;
19697 -- private
19698 -- type T is new A2 with null record;
19700 -- In this case, the full view of T inherits F1 and F2 but the private
19701 -- view inherits only F1
19703 else
19704 declare
19705 Ancestor : Entity_Id := Scope (C);
19707 begin
19708 loop
19709 if Ancestor = Original_Type then
19710 return True;
19712 -- The ancestor may have a partial view of the original type,
19713 -- but if the full view is in scope, as in a child body, the
19714 -- component is visible.
19716 elsif In_Private_Part (Scope (Original_Type))
19717 and then Full_View (Ancestor) = Original_Type
19718 then
19719 return True;
19721 elsif Ancestor = Etype (Ancestor) then
19723 -- No further ancestors to examine
19725 return False;
19726 end if;
19728 Ancestor := Etype (Ancestor);
19729 end loop;
19730 end;
19731 end if;
19732 end Is_Visible_Component;
19734 --------------------------
19735 -- Make_Class_Wide_Type --
19736 --------------------------
19738 procedure Make_Class_Wide_Type (T : Entity_Id) is
19739 CW_Type : Entity_Id;
19740 CW_Name : Name_Id;
19741 Next_E : Entity_Id;
19742 Prev_E : Entity_Id;
19744 begin
19745 if Present (Class_Wide_Type (T)) then
19747 -- The class-wide type is a partially decorated entity created for a
19748 -- unanalyzed tagged type referenced through a limited with clause.
19749 -- When the tagged type is analyzed, its class-wide type needs to be
19750 -- redecorated. Note that we reuse the entity created by Decorate_
19751 -- Tagged_Type in order to preserve all links.
19753 if Materialize_Entity (Class_Wide_Type (T)) then
19754 CW_Type := Class_Wide_Type (T);
19755 Set_Materialize_Entity (CW_Type, False);
19757 -- The class wide type can have been defined by the partial view, in
19758 -- which case everything is already done.
19760 else
19761 return;
19762 end if;
19764 -- Default case, we need to create a new class-wide type
19766 else
19767 CW_Type :=
19768 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19769 end if;
19771 -- Inherit root type characteristics
19773 CW_Name := Chars (CW_Type);
19774 Next_E := Next_Entity (CW_Type);
19775 Prev_E := Prev_Entity (CW_Type);
19776 Copy_Node (T, CW_Type);
19777 Set_Comes_From_Source (CW_Type, False);
19778 Set_Chars (CW_Type, CW_Name);
19779 Set_Parent (CW_Type, Parent (T));
19780 Set_Prev_Entity (CW_Type, Prev_E);
19781 Set_Next_Entity (CW_Type, Next_E);
19783 -- Ensure we have a new freeze node for the class-wide type. The partial
19784 -- view may have freeze action of its own, requiring a proper freeze
19785 -- node, and the same freeze node cannot be shared between the two
19786 -- types.
19788 Set_Has_Delayed_Freeze (CW_Type);
19789 Set_Freeze_Node (CW_Type, Empty);
19791 -- Customize the class-wide type: It has no prim. op., it cannot be
19792 -- abstract, its Etype points back to the specific root type, and it
19793 -- cannot have any invariants.
19795 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19796 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19798 elsif Ekind (CW_Type) in Concurrent_Kind then
19799 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19800 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19802 if Ekind (CW_Type) in Task_Kind then
19803 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19804 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19805 end if;
19807 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19808 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19809 end if;
19811 elsif Ekind (CW_Type) = E_Record_Type then
19812 Reinit_Field_To_Zero (CW_Type, F_Corresponding_Concurrent_Type);
19813 end if;
19815 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19816 Set_Is_Tagged_Type (CW_Type, True);
19817 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19818 Set_Is_Abstract_Type (CW_Type, False);
19819 Set_Is_Constrained (CW_Type, False);
19820 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19821 Set_Default_SSO (CW_Type);
19822 Set_Has_Inheritable_Invariants (CW_Type, False);
19823 Set_Has_Inherited_Invariants (CW_Type, False);
19824 Set_Has_Own_Invariants (CW_Type, False);
19826 if Ekind (T) = E_Class_Wide_Subtype then
19827 Set_Etype (CW_Type, Etype (Base_Type (T)));
19828 else
19829 Set_Etype (CW_Type, T);
19830 end if;
19832 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19834 -- If this is the class_wide type of a constrained subtype, it does
19835 -- not have discriminants.
19837 Set_Has_Discriminants (CW_Type,
19838 Has_Discriminants (T) and then not Is_Constrained (T));
19840 Set_Has_Unknown_Discriminants (CW_Type, True);
19841 Set_Class_Wide_Type (T, CW_Type);
19842 Set_Equivalent_Type (CW_Type, Empty);
19844 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19846 Set_Class_Wide_Type (CW_Type, CW_Type);
19847 end Make_Class_Wide_Type;
19849 ----------------
19850 -- Make_Index --
19851 ----------------
19853 procedure Make_Index
19854 (N : Node_Id;
19855 Related_Nod : Node_Id;
19856 Related_Id : Entity_Id := Empty;
19857 Suffix_Index : Pos := 1)
19859 R : Node_Id;
19860 T : Entity_Id;
19861 Def_Id : Entity_Id := Empty;
19862 Found : Boolean := False;
19864 begin
19865 -- For a discrete range used in a constrained array definition and
19866 -- defined by a range, an implicit conversion to the predefined type
19867 -- INTEGER is assumed if each bound is either a numeric literal, a named
19868 -- number, or an attribute, and the type of both bounds (prior to the
19869 -- implicit conversion) is the type universal_integer. Otherwise, both
19870 -- bounds must be of the same discrete type, other than universal
19871 -- integer; this type must be determinable independently of the
19872 -- context, but using the fact that the type must be discrete and that
19873 -- both bounds must have the same type.
19875 -- Character literals also have a universal type in the absence of
19876 -- of additional context, and are resolved to Standard_Character.
19878 if Nkind (N) = N_Range then
19880 -- The index is given by a range constraint. The bounds are known
19881 -- to be of a consistent type.
19883 if not Is_Overloaded (N) then
19884 T := Etype (N);
19886 -- For universal bounds, choose the specific predefined type
19888 if T = Universal_Integer then
19889 T := Standard_Integer;
19891 elsif T = Any_Character then
19892 Ambiguous_Character (Low_Bound (N));
19894 T := Standard_Character;
19895 end if;
19897 -- The node may be overloaded because some user-defined operators
19898 -- are available, but if a universal interpretation exists it is
19899 -- also the selected one.
19901 elsif Universal_Interpretation (N) = Universal_Integer then
19902 T := Standard_Integer;
19904 else
19905 T := Any_Type;
19907 declare
19908 Ind : Interp_Index;
19909 It : Interp;
19911 begin
19912 Get_First_Interp (N, Ind, It);
19913 while Present (It.Typ) loop
19914 if Is_Discrete_Type (It.Typ) then
19916 if Found
19917 and then not Covers (It.Typ, T)
19918 and then not Covers (T, It.Typ)
19919 then
19920 Error_Msg_N ("ambiguous bounds in discrete range", N);
19921 exit;
19922 else
19923 T := It.Typ;
19924 Found := True;
19925 end if;
19926 end if;
19928 Get_Next_Interp (Ind, It);
19929 end loop;
19931 if T = Any_Type then
19932 Error_Msg_N ("discrete type required for range", N);
19933 Set_Etype (N, Any_Type);
19934 return;
19936 elsif T = Universal_Integer then
19937 T := Standard_Integer;
19938 end if;
19939 end;
19940 end if;
19942 if not Is_Discrete_Type (T) then
19943 Error_Msg_N ("discrete type required for range", N);
19944 Set_Etype (N, Any_Type);
19945 return;
19946 end if;
19948 -- If the range bounds are "T'First .. T'Last" where T is a name of a
19949 -- discrete type, then use T as the type of the index.
19951 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19952 and then Attribute_Name (Low_Bound (N)) = Name_First
19953 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19954 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19956 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19957 and then Attribute_Name (High_Bound (N)) = Name_Last
19958 and then Is_Entity_Name (Prefix (High_Bound (N)))
19959 and then Entity (Prefix (High_Bound (N))) = Def_Id
19960 then
19961 Def_Id := Entity (Prefix (Low_Bound (N)));
19962 end if;
19964 R := N;
19965 Process_Range_Expr_In_Decl (R, T);
19967 elsif Nkind (N) = N_Subtype_Indication then
19969 -- The index is given by a subtype with a range constraint
19971 T := Base_Type (Entity (Subtype_Mark (N)));
19973 if not Is_Discrete_Type (T) then
19974 Error_Msg_N ("discrete type required for range", N);
19975 Set_Etype (N, Any_Type);
19976 return;
19977 end if;
19979 R := Range_Expression (Constraint (N));
19981 Resolve (R, T);
19982 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19984 elsif Nkind (N) = N_Attribute_Reference then
19986 -- Catch beginner's error (use of attribute other than 'Range)
19988 if Attribute_Name (N) /= Name_Range then
19989 Error_Msg_N ("expect attribute ''Range", N);
19990 Set_Etype (N, Any_Type);
19991 return;
19992 end if;
19994 -- If the node denotes the range of a type mark, that is also the
19995 -- resulting type, and we do not need to create an Itype for it.
19997 if Is_Entity_Name (Prefix (N))
19998 and then Comes_From_Source (N)
19999 and then Is_Discrete_Type (Entity (Prefix (N)))
20000 then
20001 Def_Id := Entity (Prefix (N));
20002 end if;
20004 Analyze_And_Resolve (N);
20005 T := Etype (N);
20006 R := N;
20008 -- If none of the above, must be a subtype. We convert this to a
20009 -- range attribute reference because in the case of declared first
20010 -- named subtypes, the types in the range reference can be different
20011 -- from the type of the entity. A range attribute normalizes the
20012 -- reference and obtains the correct types for the bounds.
20014 -- This transformation is in the nature of an expansion, is only
20015 -- done if expansion is active. In particular, it is not done on
20016 -- formal generic types, because we need to retain the name of the
20017 -- original index for instantiation purposes.
20019 else
20020 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
20021 Error_Msg_N ("invalid subtype mark in discrete range", N);
20022 Set_Etype (N, Any_Integer);
20023 return;
20025 else
20026 -- The type mark may be that of an incomplete type. It is only
20027 -- now that we can get the full view, previous analysis does
20028 -- not look specifically for a type mark.
20030 Set_Entity (N, Get_Full_View (Entity (N)));
20031 Set_Etype (N, Entity (N));
20032 Def_Id := Entity (N);
20034 if not Is_Discrete_Type (Def_Id) then
20035 Error_Msg_N ("discrete type required for index", N);
20036 Set_Etype (N, Any_Type);
20037 return;
20038 end if;
20039 end if;
20041 if Expander_Active then
20042 Rewrite (N,
20043 Make_Attribute_Reference (Sloc (N),
20044 Attribute_Name => Name_Range,
20045 Prefix => Relocate_Node (N)));
20047 -- The original was a subtype mark that does not freeze. This
20048 -- means that the rewritten version must not freeze either.
20050 Set_Must_Not_Freeze (N);
20051 Set_Must_Not_Freeze (Prefix (N));
20052 Analyze_And_Resolve (N);
20053 T := Etype (N);
20054 R := N;
20056 -- If expander is inactive, type is legal, nothing else to construct
20058 else
20059 return;
20060 end if;
20061 end if;
20063 if not Is_Discrete_Type (T) then
20064 Error_Msg_N ("discrete type required for range", N);
20065 Set_Etype (N, Any_Type);
20066 return;
20068 elsif T = Any_Type then
20069 Set_Etype (N, Any_Type);
20070 return;
20071 end if;
20073 -- We will now create the appropriate Itype to describe the range, but
20074 -- first a check. If we originally had a subtype, then we just label
20075 -- the range with this subtype. Not only is there no need to construct
20076 -- a new subtype, but it is wrong to do so for two reasons:
20078 -- 1. A legality concern, if we have a subtype, it must not freeze,
20079 -- and the Itype would cause freezing incorrectly
20081 -- 2. An efficiency concern, if we created an Itype, it would not be
20082 -- recognized as the same type for the purposes of eliminating
20083 -- checks in some circumstances.
20085 -- We signal this case by setting the subtype entity in Def_Id
20087 if No (Def_Id) then
20088 Def_Id :=
20089 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
20090 Set_Etype (Def_Id, Base_Type (T));
20092 if Is_Signed_Integer_Type (T) then
20093 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20095 elsif Is_Modular_Integer_Type (T) then
20096 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20098 else
20099 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20100 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20101 Set_First_Literal (Def_Id, First_Literal (T));
20102 end if;
20104 Set_Size_Info (Def_Id, (T));
20105 Set_RM_Size (Def_Id, RM_Size (T));
20106 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20108 Set_Scalar_Range (Def_Id, R);
20109 Conditional_Delay (Def_Id, T);
20111 -- In the subtype indication case inherit properties of the parent
20113 if Nkind (N) = N_Subtype_Indication then
20115 -- It is enough to inherit predicate flags and not the predicate
20116 -- functions, because predicates on an index type are illegal
20117 -- anyway and the flags are enough to detect them.
20119 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20121 -- If the immediate parent of the new subtype is nonstatic, then
20122 -- the subtype we create is nonstatic as well, even if its bounds
20123 -- are static.
20125 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20126 Set_Is_Non_Static_Subtype (Def_Id);
20127 end if;
20128 end if;
20130 Set_Parent (Def_Id, N);
20131 end if;
20133 -- Final step is to label the index with this constructed type
20135 Set_Etype (N, Def_Id);
20136 end Make_Index;
20138 ------------------------------
20139 -- Modular_Type_Declaration --
20140 ------------------------------
20142 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20143 Mod_Expr : constant Node_Id := Expression (Def);
20144 M_Val : Uint;
20146 procedure Set_Modular_Size (Bits : Int);
20147 -- Sets RM_Size to Bits, and Esize to normal word size above this
20149 ----------------------
20150 -- Set_Modular_Size --
20151 ----------------------
20153 procedure Set_Modular_Size (Bits : Int) is
20154 Siz : Int;
20156 begin
20157 Set_RM_Size (T, UI_From_Int (Bits));
20159 if Bits < System_Max_Binary_Modulus_Power then
20160 Siz := 8;
20162 while Siz < 128 loop
20163 exit when Bits <= Siz;
20164 Siz := Siz * 2;
20165 end loop;
20167 Set_Esize (T, UI_From_Int (Siz));
20169 else
20170 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20171 end if;
20173 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20174 Set_Is_Known_Valid (T);
20175 end if;
20176 end Set_Modular_Size;
20178 -- Start of processing for Modular_Type_Declaration
20180 begin
20181 -- If the mod expression is (exactly) 2 * literal, where literal is
20182 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20184 if Warn_On_Suspicious_Modulus_Value
20185 and then Nkind (Mod_Expr) = N_Op_Multiply
20186 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20187 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20188 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20189 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20190 then
20191 Error_Msg_N
20192 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20193 end if;
20195 -- Proceed with analysis of mod expression
20197 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20199 Set_Etype (T, T);
20200 Mutate_Ekind (T, E_Modular_Integer_Type);
20201 Reinit_Alignment (T);
20202 Set_Is_Constrained (T);
20204 if not Is_OK_Static_Expression (Mod_Expr) then
20205 Flag_Non_Static_Expr
20206 ("non-static expression used for modular type bound!", Mod_Expr);
20207 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20208 else
20209 M_Val := Expr_Value (Mod_Expr);
20210 end if;
20212 if M_Val < 1 then
20213 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20214 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20215 end if;
20217 if M_Val > 2 ** Standard_Long_Integer_Size then
20218 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20219 end if;
20221 Set_Modulus (T, M_Val);
20223 -- Create bounds for the modular type based on the modulus given in
20224 -- the type declaration and then analyze and resolve those bounds.
20226 Set_Scalar_Range (T,
20227 Make_Range (Sloc (Mod_Expr),
20228 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20229 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20231 -- Properly analyze the literals for the range. We do this manually
20232 -- because we can't go calling Resolve, since we are resolving these
20233 -- bounds with the type, and this type is certainly not complete yet.
20235 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20236 Set_Etype (High_Bound (Scalar_Range (T)), T);
20237 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20238 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20240 -- Loop through powers of two to find number of bits required
20242 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20244 -- Binary case
20246 if M_Val = 2 ** Bits then
20247 Set_Modular_Size (Bits);
20248 return;
20250 -- Nonbinary case
20252 elsif M_Val < 2 ** Bits then
20253 Set_Non_Binary_Modulus (T);
20255 if Bits > System_Max_Nonbinary_Modulus_Power then
20256 Error_Msg_Uint_1 :=
20257 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20258 Error_Msg_F
20259 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20260 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20261 return;
20263 else
20264 -- In the nonbinary case, set size as per RM 13.3(55)
20266 Set_Modular_Size (Bits);
20267 return;
20268 end if;
20269 end if;
20271 end loop;
20273 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20274 -- so we just signal an error and set the maximum size.
20276 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20277 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20279 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20280 Reinit_Alignment (T);
20282 end Modular_Type_Declaration;
20284 --------------------------
20285 -- New_Concatenation_Op --
20286 --------------------------
20288 procedure New_Concatenation_Op (Typ : Entity_Id) is
20289 Loc : constant Source_Ptr := Sloc (Typ);
20290 Op : Entity_Id;
20292 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20293 -- Create abbreviated declaration for the formal of a predefined
20294 -- Operator 'Op' of type 'Typ'
20296 --------------------
20297 -- Make_Op_Formal --
20298 --------------------
20300 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20301 Formal : Entity_Id;
20302 begin
20303 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20304 Set_Etype (Formal, Typ);
20305 Set_Mechanism (Formal, Default_Mechanism);
20306 return Formal;
20307 end Make_Op_Formal;
20309 -- Start of processing for New_Concatenation_Op
20311 begin
20312 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20314 Mutate_Ekind (Op, E_Operator);
20315 Set_Is_Not_Self_Hidden (Op);
20316 Set_Scope (Op, Current_Scope);
20317 Set_Etype (Op, Typ);
20318 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20319 Set_Is_Immediately_Visible (Op);
20320 Set_Is_Intrinsic_Subprogram (Op);
20321 Set_Has_Completion (Op);
20322 Append_Entity (Op, Current_Scope);
20324 Set_Name_Entity_Id (Name_Op_Concat, Op);
20326 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20327 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20328 end New_Concatenation_Op;
20330 -------------------------
20331 -- OK_For_Limited_Init --
20332 -------------------------
20334 -- ???Check all calls of this, and compare the conditions under which it's
20335 -- called.
20337 function OK_For_Limited_Init
20338 (Typ : Entity_Id;
20339 Exp : Node_Id) return Boolean
20341 begin
20342 return Is_CPP_Constructor_Call (Exp)
20343 or else (Ada_Version >= Ada_2005
20344 and then not Debug_Flag_Dot_L
20345 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20346 end OK_For_Limited_Init;
20348 -------------------------------
20349 -- OK_For_Limited_Init_In_05 --
20350 -------------------------------
20352 function OK_For_Limited_Init_In_05
20353 (Typ : Entity_Id;
20354 Exp : Node_Id) return Boolean
20356 begin
20357 -- An object of a limited interface type can be initialized with any
20358 -- expression of a nonlimited descendant type. However this does not
20359 -- apply if this is a view conversion of some other expression. This
20360 -- is checked below.
20362 if Is_Class_Wide_Type (Typ)
20363 and then Is_Limited_Interface (Typ)
20364 and then not Is_Limited_Type (Etype (Exp))
20365 and then Nkind (Exp) /= N_Type_Conversion
20366 then
20367 return True;
20368 end if;
20370 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20371 -- case of limited aggregates (including extension aggregates), and
20372 -- function calls. The function call may have been given in prefixed
20373 -- notation, in which case the original node is an indexed component.
20374 -- If the function is parameterless, the original node was an explicit
20375 -- dereference. The function may also be parameterless, in which case
20376 -- the source node is just an identifier.
20378 -- A branch of a conditional expression may have been removed if the
20379 -- condition is statically known. This happens during expansion, and
20380 -- thus will not happen if previous errors were encountered. The check
20381 -- will have been performed on the chosen branch, which replaces the
20382 -- original conditional expression.
20384 if No (Exp) then
20385 return True;
20386 end if;
20388 case Nkind (Original_Node (Exp)) is
20389 when N_Aggregate
20390 | N_Delta_Aggregate
20391 | N_Extension_Aggregate
20392 | N_Function_Call
20393 | N_Op
20395 return True;
20397 when N_Identifier =>
20398 return Present (Entity (Original_Node (Exp)))
20399 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20401 when N_Qualified_Expression =>
20402 return
20403 OK_For_Limited_Init_In_05
20404 (Typ, Expression (Original_Node (Exp)));
20406 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20407 -- with a function call, the expander has rewritten the call into an
20408 -- N_Type_Conversion node to force displacement of the pointer to
20409 -- reference the component containing the secondary dispatch table.
20410 -- Otherwise a type conversion is not a legal context.
20411 -- A return statement for a build-in-place function returning a
20412 -- synchronized type also introduces an unchecked conversion.
20414 when N_Type_Conversion
20415 | N_Unchecked_Type_Conversion
20417 return not Comes_From_Source (Exp)
20418 and then
20419 -- If the conversion has been rewritten, check Original_Node;
20420 -- otherwise, check the expression of the compiler-generated
20421 -- conversion (which is a conversion that we want to ignore
20422 -- for purposes of the limited-initialization restrictions).
20424 (if Is_Rewrite_Substitution (Exp)
20425 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20426 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20428 when N_Explicit_Dereference
20429 | N_Indexed_Component
20430 | N_Selected_Component
20432 return Nkind (Exp) = N_Function_Call;
20434 -- A use of 'Input is a function call, hence allowed. Normally the
20435 -- attribute will be changed to a call, but the attribute by itself
20436 -- can occur with -gnatc.
20438 when N_Attribute_Reference =>
20439 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20441 -- "return raise ..." is OK
20443 when N_Raise_Expression =>
20444 return True;
20446 -- For a case expression, all dependent expressions must be legal
20448 when N_Case_Expression =>
20449 declare
20450 Alt : Node_Id;
20452 begin
20453 Alt := First (Alternatives (Original_Node (Exp)));
20454 while Present (Alt) loop
20455 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20456 return False;
20457 end if;
20459 Next (Alt);
20460 end loop;
20462 return True;
20463 end;
20465 -- For an if expression, all dependent expressions must be legal
20467 when N_If_Expression =>
20468 declare
20469 Then_Expr : constant Node_Id :=
20470 Next (First (Expressions (Original_Node (Exp))));
20471 Else_Expr : constant Node_Id := Next (Then_Expr);
20472 begin
20473 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20474 and then
20475 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20476 end;
20478 when others =>
20479 return False;
20480 end case;
20481 end OK_For_Limited_Init_In_05;
20483 -------------------------------------------
20484 -- Ordinary_Fixed_Point_Type_Declaration --
20485 -------------------------------------------
20487 procedure Ordinary_Fixed_Point_Type_Declaration
20488 (T : Entity_Id;
20489 Def : Node_Id)
20491 Loc : constant Source_Ptr := Sloc (Def);
20492 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20493 RRS : constant Node_Id := Real_Range_Specification (Def);
20494 Implicit_Base : Entity_Id;
20495 Delta_Val : Ureal;
20496 Small_Val : Ureal;
20497 Low_Val : Ureal;
20498 High_Val : Ureal;
20500 begin
20501 Check_Restriction (No_Fixed_Point, Def);
20503 -- Create implicit base type
20505 Implicit_Base :=
20506 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20507 Set_Etype (Implicit_Base, Implicit_Base);
20509 -- Analyze and process delta expression
20511 Analyze_And_Resolve (Delta_Expr, Any_Real);
20513 Check_Delta_Expression (Delta_Expr);
20514 Delta_Val := Expr_Value_R (Delta_Expr);
20516 Set_Delta_Value (Implicit_Base, Delta_Val);
20518 -- Compute default small from given delta, which is the largest power
20519 -- of two that does not exceed the given delta value.
20521 declare
20522 Tmp : Ureal;
20523 Scale : Int;
20525 begin
20526 Tmp := Ureal_1;
20527 Scale := 0;
20529 if Delta_Val < Ureal_1 then
20530 while Delta_Val < Tmp loop
20531 Tmp := Tmp / Ureal_2;
20532 Scale := Scale + 1;
20533 end loop;
20535 else
20536 loop
20537 Tmp := Tmp * Ureal_2;
20538 exit when Tmp > Delta_Val;
20539 Scale := Scale - 1;
20540 end loop;
20541 end if;
20543 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20544 end;
20546 Set_Small_Value (Implicit_Base, Small_Val);
20548 -- If no range was given, set a dummy range
20550 if RRS <= Empty_Or_Error then
20551 Low_Val := -Small_Val;
20552 High_Val := Small_Val;
20554 -- Otherwise analyze and process given range
20556 else
20557 declare
20558 Low : constant Node_Id := Low_Bound (RRS);
20559 High : constant Node_Id := High_Bound (RRS);
20561 begin
20562 Analyze_And_Resolve (Low, Any_Real);
20563 Analyze_And_Resolve (High, Any_Real);
20564 Check_Real_Bound (Low);
20565 Check_Real_Bound (High);
20567 -- Obtain and set the range
20569 Low_Val := Expr_Value_R (Low);
20570 High_Val := Expr_Value_R (High);
20572 if Low_Val > High_Val then
20573 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20574 end if;
20575 end;
20576 end if;
20578 -- The range for both the implicit base and the declared first subtype
20579 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20580 -- set a temporary range in place. Note that the bounds of the base
20581 -- type will be widened to be symmetrical and to fill the available
20582 -- bits when the type is frozen.
20584 -- We could do this with all discrete types, and probably should, but
20585 -- we absolutely have to do it for fixed-point, since the end-points
20586 -- of the range and the size are determined by the small value, which
20587 -- could be reset before the freeze point.
20589 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20590 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20592 -- Complete definition of first subtype. The inheritance of the rep item
20593 -- chain ensures that SPARK-related pragmas are not clobbered when the
20594 -- ordinary fixed point type acts as a full view of a private type.
20596 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20597 Set_Etype (T, Implicit_Base);
20598 Reinit_Size_Align (T);
20599 Inherit_Rep_Item_Chain (T, Implicit_Base);
20600 Set_Small_Value (T, Small_Val);
20601 Set_Delta_Value (T, Delta_Val);
20602 Set_Is_Constrained (T);
20603 end Ordinary_Fixed_Point_Type_Declaration;
20605 ----------------------------------
20606 -- Preanalyze_Assert_Expression --
20607 ----------------------------------
20609 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20610 begin
20611 In_Assertion_Expr := In_Assertion_Expr + 1;
20612 Preanalyze_Spec_Expression (N, T);
20613 In_Assertion_Expr := In_Assertion_Expr - 1;
20614 end Preanalyze_Assert_Expression;
20616 -- ??? The variant below explicitly saves and restores all the flags,
20617 -- because it is impossible to compose the existing variety of
20618 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20619 -- to achieve the desired semantics.
20621 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20622 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20623 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
20624 Save_Full_Analysis : constant Boolean := Full_Analysis;
20626 begin
20627 In_Assertion_Expr := In_Assertion_Expr + 1;
20628 In_Spec_Expression := True;
20629 Set_Must_Not_Freeze (N);
20630 Inside_Preanalysis_Without_Freezing :=
20631 Inside_Preanalysis_Without_Freezing + 1;
20632 Full_Analysis := False;
20633 Expander_Mode_Save_And_Set (False);
20635 if GNATprove_Mode then
20636 Analyze_And_Resolve (N);
20637 else
20638 Analyze_And_Resolve (N, Suppress => All_Checks);
20639 end if;
20641 Expander_Mode_Restore;
20642 Full_Analysis := Save_Full_Analysis;
20643 Inside_Preanalysis_Without_Freezing :=
20644 Inside_Preanalysis_Without_Freezing - 1;
20645 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
20646 In_Spec_Expression := Save_In_Spec_Expression;
20647 In_Assertion_Expr := In_Assertion_Expr - 1;
20648 end Preanalyze_Assert_Expression;
20650 -----------------------------------
20651 -- Preanalyze_Default_Expression --
20652 -----------------------------------
20654 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20655 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20656 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20658 begin
20659 In_Default_Expr := True;
20660 In_Spec_Expression := True;
20662 Preanalyze_With_Freezing_And_Resolve (N, T);
20664 In_Default_Expr := Save_In_Default_Expr;
20665 In_Spec_Expression := Save_In_Spec_Expression;
20666 end Preanalyze_Default_Expression;
20668 --------------------------------
20669 -- Preanalyze_Spec_Expression --
20670 --------------------------------
20672 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20673 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20674 begin
20675 In_Spec_Expression := True;
20676 Preanalyze_And_Resolve (N, T);
20677 In_Spec_Expression := Save_In_Spec_Expression;
20678 end Preanalyze_Spec_Expression;
20680 ----------------------------------------
20681 -- Prepare_Private_Subtype_Completion --
20682 ----------------------------------------
20684 procedure Prepare_Private_Subtype_Completion
20685 (Id : Entity_Id;
20686 Related_Nod : Node_Id)
20688 Id_B : constant Entity_Id := Base_Type (Id);
20689 Full_B : constant Entity_Id := Full_View (Id_B);
20690 Full : Entity_Id;
20692 begin
20693 if Present (Full_B) then
20695 -- The Base_Type is already completed, we can complete the subtype
20696 -- now. We have to create a new entity with the same name, Thus we
20697 -- can't use Create_Itype.
20699 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20700 Set_Is_Itype (Full);
20701 Set_Associated_Node_For_Itype (Full, Related_Nod);
20702 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20703 Set_Full_View (Id, Full);
20704 end if;
20706 -- The parent subtype may be private, but the base might not, in some
20707 -- nested instances. In that case, the subtype does not need to be
20708 -- exchanged. It would still be nice to make private subtypes and their
20709 -- bases consistent at all times ???
20711 if Is_Private_Type (Id_B) then
20712 Append_Elmt (Id, Private_Dependents (Id_B));
20713 end if;
20714 end Prepare_Private_Subtype_Completion;
20716 ---------------------------
20717 -- Process_Discriminants --
20718 ---------------------------
20720 procedure Process_Discriminants
20721 (N : Node_Id;
20722 Prev : Entity_Id := Empty)
20724 Elist : constant Elist_Id := New_Elmt_List;
20725 Id : Node_Id;
20726 Discr : Node_Id;
20727 Discr_Number : Uint;
20728 Discr_Type : Entity_Id;
20729 Default_Present : Boolean := False;
20730 Default_Not_Present : Boolean := False;
20732 begin
20733 -- A composite type other than an array type can have discriminants.
20734 -- On entry, the current scope is the composite type.
20736 -- The discriminants are initially entered into the scope of the type
20737 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20738 -- use, as explained at the end of this procedure.
20740 Discr := First (Discriminant_Specifications (N));
20741 while Present (Discr) loop
20742 Enter_Name (Defining_Identifier (Discr));
20744 -- For navigation purposes we add a reference to the discriminant
20745 -- in the entity for the type. If the current declaration is a
20746 -- completion, place references on the partial view. Otherwise the
20747 -- type is the current scope.
20749 if Present (Prev) then
20751 -- The references go on the partial view, if present. If the
20752 -- partial view has discriminants, the references have been
20753 -- generated already.
20755 if not Has_Discriminants (Prev) then
20756 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20757 end if;
20758 else
20759 Generate_Reference
20760 (Current_Scope, Defining_Identifier (Discr), 'd');
20761 end if;
20763 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20764 Check_Anonymous_Access_Component
20765 (Typ_Decl => N,
20766 Typ => Defining_Identifier (N),
20767 Prev => Prev,
20768 Comp_Def => Discr,
20769 Access_Def => Discriminant_Type (Discr));
20771 -- if Check_Anonymous_Access_Component replaced Discr then
20772 -- its Original_Node points to the old Discr and the access type
20773 -- for Discr_Type has already been created.
20775 if Is_Rewrite_Substitution (Discr) then
20776 Discr_Type := Etype (Discriminant_Type (Discr));
20777 else
20778 Discr_Type :=
20779 Access_Definition (Discr, Discriminant_Type (Discr));
20781 -- Ada 2005 (AI-254)
20783 if Present (Access_To_Subprogram_Definition
20784 (Discriminant_Type (Discr)))
20785 and then Protected_Present (Access_To_Subprogram_Definition
20786 (Discriminant_Type (Discr)))
20787 then
20788 Discr_Type :=
20789 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20790 end if;
20791 end if;
20792 else
20793 Find_Type (Discriminant_Type (Discr));
20794 Discr_Type := Etype (Discriminant_Type (Discr));
20796 if Error_Posted (Discriminant_Type (Discr)) then
20797 Discr_Type := Any_Type;
20798 end if;
20799 end if;
20801 -- Handling of discriminants that are access types
20803 if Is_Access_Type (Discr_Type) then
20805 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20806 -- limited record types
20808 if Ada_Version < Ada_2005 then
20809 Check_Access_Discriminant_Requires_Limited
20810 (Discr, Discriminant_Type (Discr));
20811 end if;
20813 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20814 Error_Msg_N
20815 ("(Ada 83) access discriminant not allowed", Discr);
20816 end if;
20818 -- If not access type, must be a discrete type
20820 elsif not Is_Discrete_Type (Discr_Type) then
20821 Error_Msg_N
20822 ("discriminants must have a discrete or access type",
20823 Discriminant_Type (Discr));
20824 end if;
20826 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20828 -- If a discriminant specification includes the assignment compound
20829 -- delimiter followed by an expression, the expression is the default
20830 -- expression of the discriminant; the default expression must be of
20831 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20832 -- a default expression, we do the special preanalysis, since this
20833 -- expression does not freeze (see section "Handling of Default and
20834 -- Per-Object Expressions" in spec of package Sem).
20836 if Present (Expression (Discr)) then
20837 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20839 -- Legaity checks
20841 if Nkind (N) = N_Formal_Type_Declaration then
20842 Error_Msg_N
20843 ("discriminant defaults not allowed for formal type",
20844 Expression (Discr));
20846 -- Flag an error for a tagged type with defaulted discriminants,
20847 -- excluding limited tagged types when compiling for Ada 2012
20848 -- (see AI05-0214).
20850 elsif Is_Tagged_Type (Current_Scope)
20851 and then (not Is_Limited_Type (Current_Scope)
20852 or else Ada_Version < Ada_2012)
20853 and then Comes_From_Source (N)
20854 then
20855 -- Note: see similar test in Check_Or_Process_Discriminants, to
20856 -- handle the (illegal) case of the completion of an untagged
20857 -- view with discriminants with defaults by a tagged full view.
20858 -- We skip the check if Discr does not come from source, to
20859 -- account for the case of an untagged derived type providing
20860 -- defaults for a renamed discriminant from a private untagged
20861 -- ancestor with a tagged full view (ACATS B460006).
20863 if Ada_Version >= Ada_2012 then
20864 Error_Msg_N
20865 ("discriminants of nonlimited tagged type cannot have"
20866 & " defaults",
20867 Expression (Discr));
20868 else
20869 Error_Msg_N
20870 ("discriminants of tagged type cannot have defaults",
20871 Expression (Discr));
20872 end if;
20874 else
20875 Default_Present := True;
20876 Append_Elmt (Expression (Discr), Elist);
20878 -- Tag the defining identifiers for the discriminants with
20879 -- their corresponding default expressions from the tree.
20881 Set_Discriminant_Default_Value
20882 (Defining_Identifier (Discr), Expression (Discr));
20883 end if;
20885 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20886 -- gets set unless we can be sure that no range check is required.
20888 if not Expander_Active
20889 and then not
20890 Is_In_Range
20891 (Expression (Discr), Discr_Type, Assume_Valid => True)
20892 then
20893 Set_Do_Range_Check (Expression (Discr));
20894 end if;
20896 -- No default discriminant value given
20898 else
20899 Default_Not_Present := True;
20900 end if;
20902 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20903 -- Discr_Type but with the null-exclusion attribute
20905 if Ada_Version >= Ada_2005 then
20907 -- Ada 2005 (AI-231): Static checks
20909 if Can_Never_Be_Null (Discr_Type) then
20910 Null_Exclusion_Static_Checks (Discr);
20912 elsif Is_Access_Type (Discr_Type)
20913 and then Null_Exclusion_Present (Discr)
20915 -- No need to check itypes because in their case this check
20916 -- was done at their point of creation
20918 and then not Is_Itype (Discr_Type)
20919 then
20920 if Can_Never_Be_Null (Discr_Type) then
20921 Error_Msg_NE
20922 ("`NOT NULL` not allowed (& already excludes null)",
20923 Discr,
20924 Discr_Type);
20925 end if;
20927 Set_Etype (Defining_Identifier (Discr),
20928 Create_Null_Excluding_Itype
20929 (T => Discr_Type,
20930 Related_Nod => Discr));
20932 -- Check for improper null exclusion if the type is otherwise
20933 -- legal for a discriminant.
20935 elsif Null_Exclusion_Present (Discr)
20936 and then Is_Discrete_Type (Discr_Type)
20937 then
20938 Error_Msg_N
20939 ("null exclusion can only apply to an access type", Discr);
20940 end if;
20942 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20943 -- can't have defaults. Synchronized types, or types that are
20944 -- explicitly limited are fine, but special tests apply to derived
20945 -- types in generics: in a generic body we have to assume the
20946 -- worst, and therefore defaults are not allowed if the parent is
20947 -- a generic formal private type (see ACATS B370001).
20949 if Is_Access_Type (Discr_Type) and then Default_Present then
20950 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20951 or else Is_Limited_Record (Current_Scope)
20952 or else Is_Concurrent_Type (Current_Scope)
20953 or else Is_Concurrent_Record_Type (Current_Scope)
20954 or else Ekind (Current_Scope) = E_Limited_Private_Type
20955 then
20956 if not Is_Derived_Type (Current_Scope)
20957 or else not Is_Generic_Type (Etype (Current_Scope))
20958 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20959 or else Limited_Present
20960 (Type_Definition (Parent (Current_Scope)))
20961 then
20962 null;
20964 else
20965 Error_Msg_N
20966 ("access discriminants of nonlimited types cannot "
20967 & "have defaults", Expression (Discr));
20968 end if;
20970 elsif Present (Expression (Discr)) then
20971 Error_Msg_N
20972 ("(Ada 2005) access discriminants of nonlimited types "
20973 & "cannot have defaults", Expression (Discr));
20974 end if;
20975 end if;
20976 end if;
20978 Next (Discr);
20979 end loop;
20981 -- An element list consisting of the default expressions of the
20982 -- discriminants is constructed in the above loop and used to set
20983 -- the Discriminant_Constraint attribute for the type. If an object
20984 -- is declared of this (record or task) type without any explicit
20985 -- discriminant constraint given, this element list will form the
20986 -- actual parameters for the corresponding initialization procedure
20987 -- for the type.
20989 Set_Discriminant_Constraint (Current_Scope, Elist);
20990 Set_Stored_Constraint (Current_Scope, No_Elist);
20992 -- Default expressions must be provided either for all or for none
20993 -- of the discriminants of a discriminant part. (RM 3.7.1)
20995 if Default_Present and then Default_Not_Present then
20996 Error_Msg_N
20997 ("incomplete specification of defaults for discriminants", N);
20998 end if;
21000 -- The use of the name of a discriminant is not allowed in default
21001 -- expressions of a discriminant part if the specification of the
21002 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
21004 -- To detect this, the discriminant names are entered initially with an
21005 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
21006 -- attempt to use a void entity (for example in an expression that is
21007 -- type-checked) produces the error message: premature usage. Now after
21008 -- completing the semantic analysis of the discriminant part, we can set
21009 -- the Ekind of all the discriminants appropriately.
21011 Discr := First (Discriminant_Specifications (N));
21012 Discr_Number := Uint_1;
21013 while Present (Discr) loop
21014 Id := Defining_Identifier (Discr);
21016 if Ekind (Id) = E_In_Parameter then
21017 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
21018 end if;
21020 Mutate_Ekind (Id, E_Discriminant);
21021 Set_Is_Not_Self_Hidden (Id);
21022 Reinit_Component_Location (Id);
21023 Reinit_Esize (Id);
21024 Set_Discriminant_Number (Id, Discr_Number);
21026 -- Make sure this is always set, even in illegal programs
21028 Set_Corresponding_Discriminant (Id, Empty);
21030 -- Initialize the Original_Record_Component to the entity itself.
21031 -- Inherit_Components will propagate the right value to
21032 -- discriminants in derived record types.
21034 Set_Original_Record_Component (Id, Id);
21036 -- Create the discriminal for the discriminant
21038 Build_Discriminal (Id);
21040 Next (Discr);
21041 Discr_Number := Discr_Number + 1;
21042 end loop;
21044 Set_Has_Discriminants (Current_Scope);
21045 end Process_Discriminants;
21047 -----------------------
21048 -- Process_Full_View --
21049 -----------------------
21051 -- WARNING: This routine manages Ghost regions. Return statements must be
21052 -- replaced by gotos which jump to the end of the routine and restore the
21053 -- Ghost mode.
21055 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
21056 procedure Collect_Implemented_Interfaces
21057 (Typ : Entity_Id;
21058 Ifaces : Elist_Id);
21059 -- Ada 2005: Gather all the interfaces that Typ directly or
21060 -- inherently implements. Duplicate entries are not added to
21061 -- the list Ifaces.
21063 ------------------------------------
21064 -- Collect_Implemented_Interfaces --
21065 ------------------------------------
21067 procedure Collect_Implemented_Interfaces
21068 (Typ : Entity_Id;
21069 Ifaces : Elist_Id)
21071 Iface : Entity_Id;
21072 Iface_Elmt : Elmt_Id;
21074 begin
21075 -- Abstract interfaces are only associated with tagged record types
21077 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
21078 return;
21079 end if;
21081 -- Recursively climb to the ancestors
21083 if Etype (Typ) /= Typ
21085 -- Protect the frontend against wrong cyclic declarations like:
21087 -- type B is new A with private;
21088 -- type C is new A with private;
21089 -- private
21090 -- type B is new C with null record;
21091 -- type C is new B with null record;
21093 and then Etype (Typ) /= Priv_T
21094 and then Etype (Typ) /= Full_T
21095 then
21096 -- Keep separate the management of private type declarations
21098 if Ekind (Typ) = E_Record_Type_With_Private then
21100 -- Handle the following illegal usage:
21101 -- type Private_Type is tagged private;
21102 -- private
21103 -- type Private_Type is new Type_Implementing_Iface;
21105 if Present (Full_View (Typ))
21106 and then Etype (Typ) /= Full_View (Typ)
21107 then
21108 if Is_Interface (Etype (Typ)) then
21109 Append_Unique_Elmt (Etype (Typ), Ifaces);
21110 end if;
21112 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21113 end if;
21115 -- Non-private types
21117 else
21118 if Is_Interface (Etype (Typ)) then
21119 Append_Unique_Elmt (Etype (Typ), Ifaces);
21120 end if;
21122 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21123 end if;
21124 end if;
21126 -- Handle entities in the list of abstract interfaces
21128 if Present (Interfaces (Typ)) then
21129 Iface_Elmt := First_Elmt (Interfaces (Typ));
21130 while Present (Iface_Elmt) loop
21131 Iface := Node (Iface_Elmt);
21133 pragma Assert (Is_Interface (Iface));
21135 if not Contain_Interface (Iface, Ifaces) then
21136 Append_Elmt (Iface, Ifaces);
21137 Collect_Implemented_Interfaces (Iface, Ifaces);
21138 end if;
21140 Next_Elmt (Iface_Elmt);
21141 end loop;
21142 end if;
21143 end Collect_Implemented_Interfaces;
21145 -- Local variables
21147 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21148 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21149 -- Save the Ghost-related attributes to restore on exit
21151 Full_Indic : Node_Id;
21152 Full_Parent : Entity_Id;
21153 Priv_Parent : Entity_Id;
21155 -- Start of processing for Process_Full_View
21157 begin
21158 Mark_And_Set_Ghost_Completion (N, Priv_T);
21160 -- First some sanity checks that must be done after semantic
21161 -- decoration of the full view and thus cannot be placed with other
21162 -- similar checks in Find_Type_Name
21164 if not Is_Limited_Type (Priv_T)
21165 and then (Is_Limited_Type (Full_T)
21166 or else Is_Limited_Composite (Full_T))
21167 then
21168 if In_Instance then
21169 null;
21170 else
21171 Error_Msg_N
21172 ("completion of nonlimited type cannot be limited", Full_T);
21173 Explain_Limited_Type (Full_T, Full_T);
21174 end if;
21176 elsif Is_Abstract_Type (Full_T)
21177 and then not Is_Abstract_Type (Priv_T)
21178 then
21179 Error_Msg_N
21180 ("completion of nonabstract type cannot be abstract", Full_T);
21182 elsif Is_Tagged_Type (Priv_T)
21183 and then Is_Limited_Type (Priv_T)
21184 and then not Is_Limited_Type (Full_T)
21185 then
21186 -- If pragma CPP_Class was applied to the private declaration
21187 -- propagate the limitedness to the full-view
21189 if Is_CPP_Class (Priv_T) then
21190 Set_Is_Limited_Record (Full_T);
21192 -- GNAT allow its own definition of Limited_Controlled to disobey
21193 -- this rule in order in ease the implementation. This test is safe
21194 -- because Root_Controlled is defined in a child of System that
21195 -- normal programs are not supposed to use.
21197 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21198 Set_Is_Limited_Composite (Full_T);
21199 else
21200 Error_Msg_N
21201 ("completion of limited tagged type must be limited", Full_T);
21202 end if;
21204 elsif Is_Generic_Type (Priv_T) then
21205 Error_Msg_N ("generic type cannot have a completion", Full_T);
21206 end if;
21208 -- Check that ancestor interfaces of private and full views are
21209 -- consistent. We omit this check for synchronized types because
21210 -- they are performed on the corresponding record type when frozen.
21212 if Ada_Version >= Ada_2005
21213 and then Is_Tagged_Type (Priv_T)
21214 and then Is_Tagged_Type (Full_T)
21215 and then not Is_Concurrent_Type (Full_T)
21216 then
21217 declare
21218 Iface : Entity_Id;
21219 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21220 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21222 begin
21223 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21224 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21226 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21227 -- an interface type if and only if the full type is descendant
21228 -- of the interface type (AARM 7.3 (7.3/2)).
21230 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21232 if Present (Iface) then
21233 Error_Msg_NE
21234 ("interface in partial view& not implemented by full type "
21235 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21236 end if;
21238 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21240 if Present (Iface) then
21241 Error_Msg_NE
21242 ("interface & not implemented by partial view "
21243 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21244 end if;
21245 end;
21246 end if;
21248 if Is_Tagged_Type (Priv_T)
21249 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21250 and then Is_Derived_Type (Full_T)
21251 then
21252 Priv_Parent := Etype (Priv_T);
21254 -- The full view of a private extension may have been transformed
21255 -- into an unconstrained derived type declaration and a subtype
21256 -- declaration (see build_derived_record_type for details).
21258 if Nkind (N) = N_Subtype_Declaration then
21259 Full_Indic := Subtype_Indication (N);
21260 Full_Parent := Etype (Base_Type (Full_T));
21261 else
21262 Full_Indic := Subtype_Indication (Type_Definition (N));
21263 Full_Parent := Etype (Full_T);
21264 end if;
21266 -- Check that the parent type of the full type is a descendant of
21267 -- the ancestor subtype given in the private extension. If either
21268 -- entity has an Etype equal to Any_Type then we had some previous
21269 -- error situation [7.3(8)].
21271 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21272 goto Leave;
21274 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21275 -- any order. Therefore we don't have to check that its parent must
21276 -- be a descendant of the parent of the private type declaration.
21278 elsif Is_Interface (Priv_Parent)
21279 and then Is_Interface (Full_Parent)
21280 then
21281 null;
21283 -- Ada 2005 (AI-251): If the parent of the private type declaration
21284 -- is an interface there is no need to check that it is an ancestor
21285 -- of the associated full type declaration. The required tests for
21286 -- this case are performed by Build_Derived_Record_Type.
21288 elsif not Is_Interface (Base_Type (Priv_Parent))
21289 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21290 then
21291 Error_Msg_N
21292 ("parent of full type must descend from parent of private "
21293 & "extension", Full_Indic);
21295 -- First check a formal restriction, and then proceed with checking
21296 -- Ada rules. Since the formal restriction is not a serious error, we
21297 -- don't prevent further error detection for this check, hence the
21298 -- ELSE.
21300 else
21301 -- Check the rules of 7.3(10): if the private extension inherits
21302 -- known discriminants, then the full type must also inherit those
21303 -- discriminants from the same (ancestor) type, and the parent
21304 -- subtype of the full type must be constrained if and only if
21305 -- the ancestor subtype of the private extension is constrained.
21307 if No (Discriminant_Specifications (Parent (Priv_T)))
21308 and then not Has_Unknown_Discriminants (Priv_T)
21309 and then Has_Discriminants (Base_Type (Priv_Parent))
21310 then
21311 declare
21312 Priv_Indic : constant Node_Id :=
21313 Subtype_Indication (Parent (Priv_T));
21315 Priv_Constr : constant Boolean :=
21316 Is_Constrained (Priv_Parent)
21317 or else
21318 Nkind (Priv_Indic) = N_Subtype_Indication
21319 or else
21320 Is_Constrained (Entity (Priv_Indic));
21322 Full_Constr : constant Boolean :=
21323 Is_Constrained (Full_Parent)
21324 or else
21325 Nkind (Full_Indic) = N_Subtype_Indication
21326 or else
21327 Is_Constrained (Entity (Full_Indic));
21329 Priv_Discr : Entity_Id;
21330 Full_Discr : Entity_Id;
21332 begin
21333 Priv_Discr := First_Discriminant (Priv_Parent);
21334 Full_Discr := First_Discriminant (Full_Parent);
21335 while Present (Priv_Discr) and then Present (Full_Discr) loop
21336 if Original_Record_Component (Priv_Discr) =
21337 Original_Record_Component (Full_Discr)
21338 or else
21339 Corresponding_Discriminant (Priv_Discr) =
21340 Corresponding_Discriminant (Full_Discr)
21341 then
21342 null;
21343 else
21344 exit;
21345 end if;
21347 Next_Discriminant (Priv_Discr);
21348 Next_Discriminant (Full_Discr);
21349 end loop;
21351 if Present (Priv_Discr) or else Present (Full_Discr) then
21352 Error_Msg_N
21353 ("full view must inherit discriminants of the parent "
21354 & "type used in the private extension", Full_Indic);
21356 elsif Priv_Constr and then not Full_Constr then
21357 Error_Msg_N
21358 ("parent subtype of full type must be constrained",
21359 Full_Indic);
21361 elsif Full_Constr and then not Priv_Constr then
21362 Error_Msg_N
21363 ("parent subtype of full type must be unconstrained",
21364 Full_Indic);
21365 end if;
21366 end;
21368 -- Check the rules of 7.3(12): if a partial view has neither
21369 -- known or unknown discriminants, then the full type
21370 -- declaration shall define a definite subtype.
21372 elsif not Has_Unknown_Discriminants (Priv_T)
21373 and then not Has_Discriminants (Priv_T)
21374 and then not Is_Constrained (Full_T)
21375 then
21376 Error_Msg_N
21377 ("full view must define a constrained type if partial view "
21378 & "has no discriminants", Full_T);
21379 end if;
21381 -- Do we implement the following properly???
21382 -- If the ancestor subtype of a private extension has constrained
21383 -- discriminants, then the parent subtype of the full view shall
21384 -- impose a statically matching constraint on those discriminants
21385 -- [7.3(13)].
21386 end if;
21388 else
21389 -- For untagged types, verify that a type without discriminants is
21390 -- not completed with an unconstrained type. A separate error message
21391 -- is produced if the full type has defaulted discriminants.
21393 if Is_Definite_Subtype (Priv_T)
21394 and then not Is_Definite_Subtype (Full_T)
21395 then
21396 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21397 Error_Msg_NE
21398 ("full view of& not compatible with declaration#",
21399 Full_T, Priv_T);
21401 if not Is_Tagged_Type (Full_T) then
21402 Error_Msg_N
21403 ("\one is constrained, the other unconstrained", Full_T);
21404 end if;
21405 end if;
21406 end if;
21408 -- AI-419: verify that the use of "limited" is consistent
21410 declare
21411 Orig_Decl : constant Node_Id := Original_Node (N);
21413 begin
21414 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21415 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21416 and then Nkind
21417 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21418 then
21419 if not Limited_Present (Parent (Priv_T))
21420 and then not Synchronized_Present (Parent (Priv_T))
21421 and then Limited_Present (Type_Definition (Orig_Decl))
21422 then
21423 Error_Msg_N
21424 ("full view of non-limited extension cannot be limited", N);
21426 -- Conversely, if the partial view carries the limited keyword,
21427 -- the full view must as well, even if it may be redundant.
21429 elsif Limited_Present (Parent (Priv_T))
21430 and then not Limited_Present (Type_Definition (Orig_Decl))
21431 then
21432 Error_Msg_N
21433 ("full view of limited extension must be explicitly limited",
21435 end if;
21436 end if;
21437 end;
21439 -- Ada 2005 (AI-443): A synchronized private extension must be
21440 -- completed by a task or protected type.
21442 if Ada_Version >= Ada_2005
21443 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21444 and then Synchronized_Present (Parent (Priv_T))
21445 and then not Is_Concurrent_Type (Full_T)
21446 then
21447 Error_Msg_N ("full view of synchronized extension must " &
21448 "be synchronized type", N);
21449 end if;
21451 -- Ada 2005 AI-363: if the full view has discriminants with
21452 -- defaults, it is illegal to declare constrained access subtypes
21453 -- whose designated type is the current type. This allows objects
21454 -- of the type that are declared in the heap to be unconstrained.
21456 if not Has_Unknown_Discriminants (Priv_T)
21457 and then not Has_Discriminants (Priv_T)
21458 and then Has_Defaulted_Discriminants (Full_T)
21459 then
21460 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21461 Set_Has_Constrained_Partial_View (Priv_T);
21462 end if;
21464 -- Create a full declaration for all its subtypes recorded in
21465 -- Private_Dependents and swap them similarly to the base type. These
21466 -- are subtypes that have been define before the full declaration of
21467 -- the private type. We also swap the entry in Private_Dependents list
21468 -- so we can properly restore the private view on exit from the scope.
21470 declare
21471 Priv_Elmt : Elmt_Id;
21472 Priv_Scop : Entity_Id;
21473 Priv : Entity_Id;
21474 Full : Entity_Id;
21476 begin
21477 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21478 while Present (Priv_Elmt) loop
21479 Priv := Node (Priv_Elmt);
21480 Priv_Scop := Scope (Priv);
21482 if Ekind (Priv) in E_Private_Subtype
21483 | E_Limited_Private_Subtype
21484 | E_Record_Subtype_With_Private
21485 then
21486 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21487 Set_Is_Itype (Full);
21488 Set_Parent (Full, Parent (Priv));
21489 Set_Associated_Node_For_Itype (Full, N);
21491 -- Now we need to complete the private subtype, but since the
21492 -- base type has already been swapped, we must also swap the
21493 -- subtypes (and thus, reverse the arguments in the call to
21494 -- Complete_Private_Subtype). Also note that we may need to
21495 -- re-establish the scope of the private subtype.
21497 Copy_And_Swap (Priv, Full);
21499 if not In_Open_Scopes (Priv_Scop) then
21500 Push_Scope (Priv_Scop);
21502 else
21503 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21505 Priv_Scop := Empty;
21506 end if;
21508 Complete_Private_Subtype (Full, Priv, Full_T, N);
21509 Set_Full_View (Full, Priv);
21511 if Present (Priv_Scop) then
21512 Pop_Scope;
21513 end if;
21515 Replace_Elmt (Priv_Elmt, Full);
21516 end if;
21518 Next_Elmt (Priv_Elmt);
21519 end loop;
21520 end;
21522 declare
21523 Disp_Typ : Entity_Id;
21524 Full_List : Elist_Id;
21525 Prim : Entity_Id;
21526 Prim_Elmt : Elmt_Id;
21527 Priv_List : Elist_Id;
21529 function Contains
21530 (E : Entity_Id;
21531 L : Elist_Id) return Boolean;
21532 -- Determine whether list L contains element E
21534 --------------
21535 -- Contains --
21536 --------------
21538 function Contains
21539 (E : Entity_Id;
21540 L : Elist_Id) return Boolean
21542 List_Elmt : Elmt_Id;
21544 begin
21545 List_Elmt := First_Elmt (L);
21546 while Present (List_Elmt) loop
21547 if Node (List_Elmt) = E then
21548 return True;
21549 end if;
21551 Next_Elmt (List_Elmt);
21552 end loop;
21554 return False;
21555 end Contains;
21557 -- Start of processing
21559 begin
21560 -- If the private view was tagged, copy the new primitive operations
21561 -- from the private view to the full view.
21563 if Is_Tagged_Type (Full_T) then
21564 if Is_Tagged_Type (Priv_T) then
21565 Priv_List := Primitive_Operations (Priv_T);
21566 Prim_Elmt := First_Elmt (Priv_List);
21568 -- In the case of a concurrent type completing a private tagged
21569 -- type, primitives may have been declared in between the two
21570 -- views. These subprograms need to be wrapped the same way
21571 -- entries and protected procedures are handled because they
21572 -- cannot be directly shared by the two views.
21574 if Is_Concurrent_Type (Full_T) then
21575 declare
21576 Conc_Typ : constant Entity_Id :=
21577 Corresponding_Record_Type (Full_T);
21578 Curr_Nod : Node_Id := Parent (Conc_Typ);
21579 Wrap_Spec : Node_Id;
21581 begin
21582 while Present (Prim_Elmt) loop
21583 Prim := Node (Prim_Elmt);
21585 if Comes_From_Source (Prim)
21586 and then not Is_Abstract_Subprogram (Prim)
21587 then
21588 Wrap_Spec :=
21589 Make_Subprogram_Declaration (Sloc (Prim),
21590 Specification =>
21591 Build_Wrapper_Spec
21592 (Subp_Id => Prim,
21593 Obj_Typ => Conc_Typ,
21594 Formals =>
21595 Parameter_Specifications
21596 (Parent (Prim))));
21598 Insert_After (Curr_Nod, Wrap_Spec);
21599 Curr_Nod := Wrap_Spec;
21601 Analyze (Wrap_Spec);
21603 -- Remove the wrapper from visibility to avoid
21604 -- spurious conflict with the wrapped entity.
21606 Set_Is_Immediately_Visible
21607 (Defining_Entity (Specification (Wrap_Spec)),
21608 False);
21609 end if;
21611 Next_Elmt (Prim_Elmt);
21612 end loop;
21614 goto Leave;
21615 end;
21617 -- For nonconcurrent types, transfer explicit primitives, but
21618 -- omit those inherited from the parent of the private view
21619 -- since they will be re-inherited later on.
21621 else
21622 Full_List := Primitive_Operations (Full_T);
21623 while Present (Prim_Elmt) loop
21624 Prim := Node (Prim_Elmt);
21626 if Comes_From_Source (Prim)
21627 and then not Contains (Prim, Full_List)
21628 then
21629 Append_Elmt (Prim, Full_List);
21630 end if;
21632 Next_Elmt (Prim_Elmt);
21633 end loop;
21634 end if;
21636 -- Untagged private view
21638 else
21639 Full_List := Primitive_Operations (Full_T);
21641 -- In this case the partial view is untagged, so here we locate
21642 -- all of the earlier primitives that need to be treated as
21643 -- dispatching (those that appear between the two views). Note
21644 -- that these additional operations must all be new operations
21645 -- (any earlier operations that override inherited operations
21646 -- of the full view will already have been inserted in the
21647 -- primitives list, marked by Check_Operation_From_Private_View
21648 -- as dispatching. Note that implicit "/=" operators are
21649 -- excluded from being added to the primitives list since they
21650 -- shouldn't be treated as dispatching (tagged "/=" is handled
21651 -- specially).
21653 Prim := Next_Entity (Full_T);
21654 while Present (Prim) and then Prim /= Priv_T loop
21655 if Ekind (Prim) in E_Procedure | E_Function then
21656 Disp_Typ := Find_Dispatching_Type (Prim);
21658 if Disp_Typ = Full_T
21659 and then (Chars (Prim) /= Name_Op_Ne
21660 or else Comes_From_Source (Prim))
21661 then
21662 Check_Controlling_Formals (Full_T, Prim);
21664 if Is_Suitable_Primitive (Prim)
21665 and then not Is_Dispatching_Operation (Prim)
21666 then
21667 Append_Elmt (Prim, Full_List);
21668 Set_Is_Dispatching_Operation (Prim);
21669 Set_DT_Position_Value (Prim, No_Uint);
21670 end if;
21672 elsif Is_Dispatching_Operation (Prim)
21673 and then Disp_Typ /= Full_T
21674 then
21675 -- Verify that it is not otherwise controlled by a
21676 -- formal or a return value of type T.
21678 Check_Controlling_Formals (Disp_Typ, Prim);
21679 end if;
21680 end if;
21682 Next_Entity (Prim);
21683 end loop;
21684 end if;
21686 -- For the tagged case, the two views can share the same primitive
21687 -- operations list and the same class-wide type. Update attributes
21688 -- of the class-wide type which depend on the full declaration.
21690 if Is_Tagged_Type (Priv_T) then
21691 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21692 Set_Class_Wide_Type
21693 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21695 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21696 end if;
21698 -- For untagged types, copy the primitives across from the private
21699 -- view to the full view, for support of prefixed calls when
21700 -- extensions are enabled, and better error messages otherwise.
21702 else
21703 Priv_List := Primitive_Operations (Priv_T);
21704 Prim_Elmt := First_Elmt (Priv_List);
21706 Full_List := Primitive_Operations (Full_T);
21707 while Present (Prim_Elmt) loop
21708 Prim := Node (Prim_Elmt);
21709 Append_Elmt (Prim, Full_List);
21710 Next_Elmt (Prim_Elmt);
21711 end loop;
21712 end if;
21713 end;
21715 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21717 if Known_To_Have_Preelab_Init (Priv_T) then
21719 -- Case where there is a pragma Preelaborable_Initialization. We
21720 -- always allow this in predefined units, which is cheating a bit,
21721 -- but it means we don't have to struggle to meet the requirements in
21722 -- the RM for having Preelaborable Initialization. Otherwise we
21723 -- require that the type meets the RM rules. But we can't check that
21724 -- yet, because of the rule about overriding Initialize, so we simply
21725 -- set a flag that will be checked at freeze time.
21727 if not In_Predefined_Unit (Full_T) then
21728 Set_Must_Have_Preelab_Init (Full_T);
21729 end if;
21730 end if;
21732 -- If pragma CPP_Class was applied to the private type declaration,
21733 -- propagate it now to the full type declaration.
21735 if Is_CPP_Class (Priv_T) then
21736 Set_Is_CPP_Class (Full_T);
21737 Set_Convention (Full_T, Convention_CPP);
21739 -- Check that components of imported CPP types do not have default
21740 -- expressions.
21742 Check_CPP_Type_Has_No_Defaults (Full_T);
21743 end if;
21745 -- If the private view has user specified stream attributes, then so has
21746 -- the full view.
21748 -- Why the test, how could these flags be already set in Full_T ???
21750 if Has_Specified_Stream_Read (Priv_T) then
21751 Set_Has_Specified_Stream_Read (Full_T);
21752 end if;
21754 if Has_Specified_Stream_Write (Priv_T) then
21755 Set_Has_Specified_Stream_Write (Full_T);
21756 end if;
21758 if Has_Specified_Stream_Input (Priv_T) then
21759 Set_Has_Specified_Stream_Input (Full_T);
21760 end if;
21762 if Has_Specified_Stream_Output (Priv_T) then
21763 Set_Has_Specified_Stream_Output (Full_T);
21764 end if;
21766 -- Propagate Default_Initial_Condition-related attributes from the
21767 -- partial view to the full view.
21769 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21771 -- And to the underlying full view, if any
21773 if Is_Private_Type (Full_T)
21774 and then Present (Underlying_Full_View (Full_T))
21775 then
21776 Propagate_DIC_Attributes
21777 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21778 end if;
21780 -- Propagate invariant-related attributes from the partial view to the
21781 -- full view.
21783 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21785 -- And to the underlying full view, if any
21787 if Is_Private_Type (Full_T)
21788 and then Present (Underlying_Full_View (Full_T))
21789 then
21790 Propagate_Invariant_Attributes
21791 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21792 end if;
21794 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21795 -- in the full view without advertising the inheritance in the partial
21796 -- view. This can only occur when the partial view has no parent type
21797 -- and the full view has an interface as a parent. Any other scenarios
21798 -- are illegal because implemented interfaces must match between the
21799 -- two views.
21801 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21802 declare
21803 Full_Par : constant Entity_Id := Etype (Full_T);
21804 Priv_Par : constant Entity_Id := Etype (Priv_T);
21806 begin
21807 if not Is_Interface (Priv_Par)
21808 and then Is_Interface (Full_Par)
21809 and then Has_Inheritable_Invariants (Full_Par)
21810 then
21811 Error_Msg_N
21812 ("hidden inheritance of class-wide type invariants not "
21813 & "allowed", N);
21814 end if;
21815 end;
21816 end if;
21818 -- Propagate predicates to full type, and predicate function if already
21819 -- defined. It is not clear that this can actually happen? the partial
21820 -- view cannot be frozen yet, and the predicate function has not been
21821 -- built. Still it is a cheap check and seems safer to make it.
21823 Propagate_Predicate_Attributes (Full_T, Priv_T);
21825 if Is_Private_Type (Full_T)
21826 and then Present (Underlying_Full_View (Full_T))
21827 then
21828 Propagate_Predicate_Attributes
21829 (Underlying_Full_View (Full_T), Priv_T);
21830 end if;
21832 <<Leave>>
21833 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21834 end Process_Full_View;
21836 -----------------------------------
21837 -- Process_Incomplete_Dependents --
21838 -----------------------------------
21840 procedure Process_Incomplete_Dependents
21841 (N : Node_Id;
21842 Full_T : Entity_Id;
21843 Inc_T : Entity_Id)
21845 Inc_Elmt : Elmt_Id;
21846 Priv_Dep : Entity_Id;
21847 New_Subt : Entity_Id;
21849 Disc_Constraint : Elist_Id;
21851 begin
21852 if No (Private_Dependents (Inc_T)) then
21853 return;
21854 end if;
21856 -- Itypes that may be generated by the completion of an incomplete
21857 -- subtype are not used by the back-end and not attached to the tree.
21858 -- They are created only for constraint-checking purposes.
21860 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21861 while Present (Inc_Elmt) loop
21862 Priv_Dep := Node (Inc_Elmt);
21864 if Ekind (Priv_Dep) = E_Subprogram_Type then
21866 -- An Access_To_Subprogram type may have a return type or a
21867 -- parameter type that is incomplete. Replace with the full view.
21869 if Etype (Priv_Dep) = Inc_T then
21870 Set_Etype (Priv_Dep, Full_T);
21871 end if;
21873 declare
21874 Formal : Entity_Id;
21876 begin
21877 Formal := First_Formal (Priv_Dep);
21878 while Present (Formal) loop
21879 if Etype (Formal) = Inc_T then
21880 Set_Etype (Formal, Full_T);
21881 end if;
21883 Next_Formal (Formal);
21884 end loop;
21885 end;
21887 elsif Is_Overloadable (Priv_Dep) then
21889 -- If a subprogram in the incomplete dependents list is primitive
21890 -- for a tagged full type then mark it as a dispatching operation,
21891 -- check whether it overrides an inherited subprogram, and check
21892 -- restrictions on its controlling formals. Note that a protected
21893 -- operation is never dispatching: only its wrapper operation
21894 -- (which has convention Ada) is.
21896 if Is_Tagged_Type (Full_T)
21897 and then Is_Primitive (Priv_Dep)
21898 and then Convention (Priv_Dep) /= Convention_Protected
21899 then
21900 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21901 Set_Is_Dispatching_Operation (Priv_Dep);
21902 Check_Controlling_Formals (Full_T, Priv_Dep);
21903 end if;
21905 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21907 -- Can happen during processing of a body before the completion
21908 -- of a TA type. Ignore, because spec is also on dependent list.
21910 return;
21912 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21913 -- corresponding subtype of the full view.
21915 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21916 and then Comes_From_Source (Priv_Dep)
21917 then
21918 Set_Subtype_Indication
21919 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21920 Reinit_Field_To_Zero
21921 (Priv_Dep, F_Private_Dependents,
21922 Old_Ekind => E_Incomplete_Subtype);
21923 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21924 Set_Etype (Priv_Dep, Full_T);
21925 Set_Analyzed (Parent (Priv_Dep), False);
21927 -- Reanalyze the declaration, suppressing the call to Enter_Name
21928 -- to avoid duplicate names.
21930 Analyze_Subtype_Declaration
21931 (N => Parent (Priv_Dep),
21932 Skip => True);
21934 -- Dependent is a subtype
21936 else
21937 -- We build a new subtype indication using the full view of the
21938 -- incomplete parent. The discriminant constraints have been
21939 -- elaborated already at the point of the subtype declaration.
21941 New_Subt := Create_Itype (E_Void, N);
21943 if Has_Discriminants (Full_T) then
21944 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21945 else
21946 Disc_Constraint := No_Elist;
21947 end if;
21949 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21950 Set_Full_View (Priv_Dep, New_Subt);
21951 end if;
21953 Next_Elmt (Inc_Elmt);
21954 end loop;
21955 end Process_Incomplete_Dependents;
21957 --------------------------------
21958 -- Process_Range_Expr_In_Decl --
21959 --------------------------------
21961 procedure Process_Range_Expr_In_Decl
21962 (R : Node_Id;
21963 T : Entity_Id;
21964 Subtyp : Entity_Id := Empty;
21965 Check_List : List_Id := No_List)
21967 Lo, Hi : Node_Id;
21968 R_Checks : Check_Result;
21969 Insert_Node : Node_Id;
21970 Def_Id : Entity_Id;
21972 begin
21973 Analyze_And_Resolve (R, Base_Type (T));
21975 if Nkind (R) = N_Range then
21976 Lo := Low_Bound (R);
21977 Hi := High_Bound (R);
21979 -- Validity checks on the range of a quantified expression are
21980 -- delayed until the construct is transformed into a loop.
21982 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21983 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21984 then
21985 null;
21987 -- We need to ensure validity of the bounds here, because if we
21988 -- go ahead and do the expansion, then the expanded code will get
21989 -- analyzed with range checks suppressed and we miss the check.
21991 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21992 -- the temporaries generated by routine Remove_Side_Effects by means
21993 -- of validity checks must use the same names. When a range appears
21994 -- in the parent of a generic, the range is processed with checks
21995 -- disabled as part of the generic context and with checks enabled
21996 -- for code generation purposes. This leads to link issues as the
21997 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21998 -- template sees the temporaries generated by Remove_Side_Effects.
22000 else
22001 Validity_Check_Range (R, Subtyp);
22002 end if;
22004 -- If there were errors in the declaration, try and patch up some
22005 -- common mistakes in the bounds. The cases handled are literals
22006 -- which are Integer where the expected type is Real and vice versa.
22007 -- These corrections allow the compilation process to proceed further
22008 -- along since some basic assumptions of the format of the bounds
22009 -- are guaranteed.
22011 if Etype (R) = Any_Type then
22012 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
22013 Rewrite (Lo,
22014 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
22016 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
22017 Rewrite (Hi,
22018 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
22020 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
22021 Rewrite (Lo,
22022 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
22024 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
22025 Rewrite (Hi,
22026 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
22027 end if;
22029 Set_Etype (Lo, T);
22030 Set_Etype (Hi, T);
22031 end if;
22033 -- If the bounds of the range have been mistakenly given as string
22034 -- literals (perhaps in place of character literals), then an error
22035 -- has already been reported, but we rewrite the string literal as a
22036 -- bound of the range's type to avoid blowups in later processing
22037 -- that looks at static values.
22039 if Nkind (Lo) = N_String_Literal then
22040 Rewrite (Lo,
22041 Make_Attribute_Reference (Sloc (Lo),
22042 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
22043 Attribute_Name => Name_First));
22044 Analyze_And_Resolve (Lo);
22045 end if;
22047 if Nkind (Hi) = N_String_Literal then
22048 Rewrite (Hi,
22049 Make_Attribute_Reference (Sloc (Hi),
22050 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
22051 Attribute_Name => Name_First));
22052 Analyze_And_Resolve (Hi);
22053 end if;
22055 -- If bounds aren't scalar at this point then exit, avoiding
22056 -- problems with further processing of the range in this procedure.
22058 if not Is_Scalar_Type (Etype (Lo)) then
22059 return;
22060 end if;
22062 -- Resolve (actually Sem_Eval) has checked that the bounds are in
22063 -- then range of the base type. Here we check whether the bounds
22064 -- are in the range of the subtype itself. Note that if the bounds
22065 -- represent the null range the Constraint_Error exception should
22066 -- not be raised.
22068 -- Capture values of bounds and generate temporaries for them
22069 -- if needed, before applying checks, since checks may cause
22070 -- duplication of the expression without forcing evaluation.
22072 -- The forced evaluation removes side effects from expressions,
22073 -- which should occur also in GNATprove mode. Otherwise, we end up
22074 -- with unexpected insertions of actions at places where this is
22075 -- not supposed to occur, e.g. on default parameters of a call.
22077 if Expander_Active or GNATprove_Mode then
22079 -- Call Force_Evaluation to create declarations as needed
22080 -- to deal with side effects, and also create typ_FIRST/LAST
22081 -- entities for bounds if we have a subtype name.
22083 -- Note: we do this transformation even if expansion is not
22084 -- active if we are in GNATprove_Mode since the transformation
22085 -- is in general required to ensure that the resulting tree has
22086 -- proper Ada semantics.
22088 Force_Evaluation
22089 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22090 Force_Evaluation
22091 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22092 end if;
22094 -- We use a flag here instead of suppressing checks on the type
22095 -- because the type we check against isn't necessarily the place
22096 -- where we put the check.
22098 R_Checks := Get_Range_Checks (R, T);
22100 -- Look up tree to find an appropriate insertion point. We can't
22101 -- just use insert_actions because later processing depends on
22102 -- the insertion node. Prior to Ada 2012 the insertion point could
22103 -- only be a declaration or a loop, but quantified expressions can
22104 -- appear within any context in an expression, and the insertion
22105 -- point can be any statement, pragma, or declaration.
22107 Insert_Node := Parent (R);
22108 while Present (Insert_Node) loop
22109 exit when
22110 Nkind (Insert_Node) in N_Declaration
22111 and then
22112 Nkind (Insert_Node) not in N_Component_Declaration
22113 | N_Loop_Parameter_Specification
22114 | N_Function_Specification
22115 | N_Procedure_Specification;
22117 exit when Nkind (Insert_Node) in
22118 N_Later_Decl_Item |
22119 N_Statement_Other_Than_Procedure_Call |
22120 N_Procedure_Call_Statement |
22121 N_Pragma;
22123 Insert_Node := Parent (Insert_Node);
22124 end loop;
22126 if Present (Insert_Node) then
22128 -- Case of loop statement. Verify that the range is part of the
22129 -- subtype indication of the iteration scheme.
22131 if Nkind (Insert_Node) = N_Loop_Statement then
22132 declare
22133 Indic : Node_Id;
22135 begin
22136 Indic := Parent (R);
22137 while Present (Indic)
22138 and then Nkind (Indic) /= N_Subtype_Indication
22139 loop
22140 Indic := Parent (Indic);
22141 end loop;
22143 if Present (Indic) then
22144 Def_Id := Etype (Subtype_Mark (Indic));
22146 Insert_Range_Checks
22147 (R_Checks,
22148 Insert_Node,
22149 Def_Id,
22150 Sloc (Insert_Node),
22151 Do_Before => True);
22152 end if;
22153 end;
22155 -- Case of declarations. If the declaration is for a type and
22156 -- involves discriminants, the checks are premature at the
22157 -- declaration point and need to wait for the expansion of the
22158 -- initialization procedure, which will pass in the list to put
22159 -- them on; otherwise, the checks are done at the declaration
22160 -- point and there is no need to do them again in the
22161 -- initialization procedure.
22163 elsif Nkind (Insert_Node) in N_Declaration then
22164 Def_Id := Defining_Identifier (Insert_Node);
22166 if (Ekind (Def_Id) = E_Record_Type
22167 and then Depends_On_Discriminant (R))
22168 or else
22169 (Ekind (Def_Id) = E_Protected_Type
22170 and then Has_Discriminants (Def_Id))
22171 then
22172 if Present (Check_List) then
22173 Append_Range_Checks
22174 (R_Checks,
22175 Check_List, Def_Id, Sloc (Insert_Node));
22176 end if;
22178 else
22179 if No (Check_List) then
22180 Insert_Range_Checks
22181 (R_Checks,
22182 Insert_Node, Def_Id, Sloc (Insert_Node));
22183 end if;
22184 end if;
22186 -- Case of statements. Drop the checks, as the range appears in
22187 -- the context of a quantified expression. Insertion will take
22188 -- place when expression is expanded.
22190 else
22191 null;
22192 end if;
22193 end if;
22195 -- Case of other than an explicit N_Range node
22197 -- The forced evaluation removes side effects from expressions, which
22198 -- should occur also in GNATprove mode. Otherwise, we end up with
22199 -- unexpected insertions of actions at places where this is not
22200 -- supposed to occur, e.g. on default parameters of a call.
22202 elsif Expander_Active or GNATprove_Mode then
22203 Get_Index_Bounds (R, Lo, Hi);
22204 Force_Evaluation (Lo);
22205 Force_Evaluation (Hi);
22206 end if;
22207 end Process_Range_Expr_In_Decl;
22209 --------------------------------------
22210 -- Process_Real_Range_Specification --
22211 --------------------------------------
22213 procedure Process_Real_Range_Specification (Def : Node_Id) is
22214 Spec : constant Node_Id := Real_Range_Specification (Def);
22215 Lo : Node_Id;
22216 Hi : Node_Id;
22217 Err : Boolean := False;
22219 procedure Analyze_Bound (N : Node_Id);
22220 -- Analyze and check one bound
22222 -------------------
22223 -- Analyze_Bound --
22224 -------------------
22226 procedure Analyze_Bound (N : Node_Id) is
22227 begin
22228 Analyze_And_Resolve (N, Any_Real);
22230 if not Is_OK_Static_Expression (N) then
22231 Flag_Non_Static_Expr
22232 ("bound in real type definition is not static!", N);
22233 Err := True;
22234 end if;
22235 end Analyze_Bound;
22237 -- Start of processing for Process_Real_Range_Specification
22239 begin
22240 if Present (Spec) then
22241 Lo := Low_Bound (Spec);
22242 Hi := High_Bound (Spec);
22243 Analyze_Bound (Lo);
22244 Analyze_Bound (Hi);
22246 -- If error, clear away junk range specification
22248 if Err then
22249 Set_Real_Range_Specification (Def, Empty);
22250 end if;
22251 end if;
22252 end Process_Real_Range_Specification;
22254 ---------------------
22255 -- Process_Subtype --
22256 ---------------------
22258 function Process_Subtype
22259 (S : Node_Id;
22260 Related_Nod : Node_Id;
22261 Related_Id : Entity_Id := Empty;
22262 Suffix : Character := ' ') return Entity_Id
22264 procedure Check_Incomplete (T : Node_Id);
22265 -- Called to verify that an incomplete type is not used prematurely
22267 ----------------------
22268 -- Check_Incomplete --
22269 ----------------------
22271 procedure Check_Incomplete (T : Node_Id) is
22272 begin
22273 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22275 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22276 and then
22277 not (Ada_Version >= Ada_2005
22278 and then
22279 (Nkind (Parent (T)) = N_Subtype_Declaration
22280 or else (Nkind (Parent (T)) = N_Subtype_Indication
22281 and then Nkind (Parent (Parent (T))) =
22282 N_Subtype_Declaration)))
22283 then
22284 Error_Msg_N ("invalid use of type before its full declaration", T);
22285 end if;
22286 end Check_Incomplete;
22288 -- Local variables
22290 P : Node_Id;
22291 Def_Id : Entity_Id;
22292 Error_Node : Node_Id;
22293 Full_View_Id : Entity_Id;
22294 Subtype_Mark_Id : Entity_Id;
22296 May_Have_Null_Exclusion : Boolean;
22298 -- Start of processing for Process_Subtype
22300 begin
22301 -- Case of no constraints present
22303 if Nkind (S) /= N_Subtype_Indication then
22304 Find_Type (S);
22306 -- No way to proceed if the subtype indication is malformed. This
22307 -- will happen for example when the subtype indication in an object
22308 -- declaration is missing altogether and the expression is analyzed
22309 -- as if it were that indication.
22311 if not Is_Entity_Name (S) then
22312 return Any_Type;
22313 end if;
22315 Check_Incomplete (S);
22316 P := Parent (S);
22318 -- The following mirroring of assertion in Null_Exclusion_Present is
22319 -- ugly, can't we have a range, a static predicate or even a flag???
22321 May_Have_Null_Exclusion :=
22322 Present (P)
22323 and then
22324 Nkind (P) in N_Access_Definition
22325 | N_Access_Function_Definition
22326 | N_Access_Procedure_Definition
22327 | N_Access_To_Object_Definition
22328 | N_Allocator
22329 | N_Component_Definition
22330 | N_Derived_Type_Definition
22331 | N_Discriminant_Specification
22332 | N_Formal_Object_Declaration
22333 | N_Function_Specification
22334 | N_Object_Declaration
22335 | N_Object_Renaming_Declaration
22336 | N_Parameter_Specification
22337 | N_Subtype_Declaration;
22339 -- Ada 2005 (AI-231): Static check
22341 if Ada_Version >= Ada_2005
22342 and then May_Have_Null_Exclusion
22343 and then Null_Exclusion_Present (P)
22344 and then Nkind (P) /= N_Access_To_Object_Definition
22345 and then not Is_Access_Type (Entity (S))
22346 then
22347 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22348 end if;
22350 -- Create an Itype that is a duplicate of Entity (S) but with the
22351 -- null-exclusion attribute.
22353 if May_Have_Null_Exclusion
22354 and then Is_Access_Type (Entity (S))
22355 and then Null_Exclusion_Present (P)
22357 -- No need to check the case of an access to object definition.
22358 -- It is correct to define double not-null pointers.
22360 -- Example:
22361 -- type Not_Null_Int_Ptr is not null access Integer;
22362 -- type Acc is not null access Not_Null_Int_Ptr;
22364 and then Nkind (P) /= N_Access_To_Object_Definition
22365 then
22366 if Can_Never_Be_Null (Entity (S)) then
22367 case Nkind (Related_Nod) is
22368 when N_Full_Type_Declaration =>
22369 if Nkind (Type_Definition (Related_Nod))
22370 in N_Array_Type_Definition
22371 then
22372 Error_Node :=
22373 Subtype_Indication
22374 (Component_Definition
22375 (Type_Definition (Related_Nod)));
22376 else
22377 Error_Node :=
22378 Subtype_Indication (Type_Definition (Related_Nod));
22379 end if;
22381 when N_Subtype_Declaration =>
22382 Error_Node := Subtype_Indication (Related_Nod);
22384 when N_Object_Declaration =>
22385 Error_Node := Object_Definition (Related_Nod);
22387 when N_Component_Declaration =>
22388 Error_Node :=
22389 Subtype_Indication (Component_Definition (Related_Nod));
22391 when N_Allocator =>
22392 Error_Node := Expression (Related_Nod);
22394 when others =>
22395 pragma Assert (False);
22396 Error_Node := Related_Nod;
22397 end case;
22399 Error_Msg_NE
22400 ("`NOT NULL` not allowed (& already excludes null)",
22401 Error_Node,
22402 Entity (S));
22403 end if;
22405 Set_Etype (S,
22406 Create_Null_Excluding_Itype
22407 (T => Entity (S),
22408 Related_Nod => P));
22409 Set_Entity (S, Etype (S));
22410 end if;
22412 return Entity (S);
22414 -- Case of constraint present, so that we have an N_Subtype_Indication
22415 -- node (this node is created only if constraints are present).
22417 else
22418 Find_Type (Subtype_Mark (S));
22420 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22421 and then not
22422 (Nkind (Parent (S)) = N_Subtype_Declaration
22423 and then Is_Itype (Defining_Identifier (Parent (S))))
22424 then
22425 Check_Incomplete (Subtype_Mark (S));
22426 end if;
22428 P := Parent (S);
22429 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22431 -- Explicit subtype declaration case
22433 if Nkind (P) = N_Subtype_Declaration then
22434 Def_Id := Defining_Identifier (P);
22436 -- Explicit derived type definition case
22438 elsif Nkind (P) = N_Derived_Type_Definition then
22439 Def_Id := Defining_Identifier (Parent (P));
22441 -- Implicit case, the Def_Id must be created as an implicit type.
22442 -- The one exception arises in the case of concurrent types, array
22443 -- and access types, where other subsidiary implicit types may be
22444 -- created and must appear before the main implicit type. In these
22445 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22446 -- has not yet been called to create Def_Id.
22448 else
22449 if Is_Array_Type (Subtype_Mark_Id)
22450 or else Is_Concurrent_Type (Subtype_Mark_Id)
22451 or else Is_Access_Type (Subtype_Mark_Id)
22452 then
22453 Def_Id := Empty;
22455 -- For the other cases, we create a new unattached Itype,
22456 -- and set the indication to ensure it gets attached later.
22458 else
22459 Def_Id :=
22460 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22461 end if;
22462 end if;
22464 -- If the kind of constraint is invalid for this kind of type,
22465 -- then give an error, and then pretend no constraint was given.
22467 if not Is_Valid_Constraint_Kind
22468 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22469 then
22470 Error_Msg_N
22471 ("incorrect constraint for this kind of type", Constraint (S));
22473 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22475 -- Set Ekind of orphan itype, to prevent cascaded errors
22477 if Present (Def_Id) then
22478 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22479 end if;
22481 -- Make recursive call, having got rid of the bogus constraint
22483 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22484 end if;
22486 -- Remaining processing depends on type. Select on Base_Type kind to
22487 -- ensure getting to the concrete type kind in the case of a private
22488 -- subtype (needed when only doing semantic analysis).
22490 case Ekind (Base_Type (Subtype_Mark_Id)) is
22491 when Access_Kind =>
22493 -- If this is a constraint on a class-wide type, discard it.
22494 -- There is currently no way to express a partial discriminant
22495 -- constraint on a type with unknown discriminants. This is
22496 -- a pathology that the ACATS wisely decides not to test.
22498 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22499 if Comes_From_Source (S) then
22500 Error_Msg_N
22501 ("constraint on class-wide type ignored??",
22502 Constraint (S));
22503 end if;
22505 if Nkind (P) = N_Subtype_Declaration then
22506 Set_Subtype_Indication (P,
22507 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22508 end if;
22510 return Subtype_Mark_Id;
22511 end if;
22513 Constrain_Access (Def_Id, S, Related_Nod);
22515 if Expander_Active
22516 and then Is_Itype (Designated_Type (Def_Id))
22517 and then Nkind (Related_Nod) = N_Subtype_Declaration
22518 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22519 then
22520 Build_Itype_Reference
22521 (Designated_Type (Def_Id), Related_Nod);
22522 end if;
22524 when Array_Kind =>
22525 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22527 when Decimal_Fixed_Point_Kind =>
22528 Constrain_Decimal (Def_Id, S);
22530 when Enumeration_Kind =>
22531 Constrain_Enumeration (Def_Id, S);
22533 when Ordinary_Fixed_Point_Kind =>
22534 Constrain_Ordinary_Fixed (Def_Id, S);
22536 when Float_Kind =>
22537 Constrain_Float (Def_Id, S);
22539 when Integer_Kind =>
22540 Constrain_Integer (Def_Id, S);
22542 when Class_Wide_Kind
22543 | E_Incomplete_Type
22544 | E_Record_Subtype
22545 | E_Record_Type
22547 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22549 if Ekind (Def_Id) = E_Incomplete_Type then
22550 Set_Private_Dependents (Def_Id, New_Elmt_List);
22551 end if;
22553 when Private_Kind =>
22555 -- A private type with unknown discriminants may be completed
22556 -- by an unconstrained array type.
22558 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22559 and then Present (Full_View (Subtype_Mark_Id))
22560 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22561 then
22562 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22564 -- ... but more commonly is completed by a discriminated record
22565 -- type.
22567 else
22568 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22569 end if;
22571 -- The base type may be private but Def_Id may be a full view
22572 -- in an instance.
22574 if Is_Private_Type (Def_Id) then
22575 Set_Private_Dependents (Def_Id, New_Elmt_List);
22576 end if;
22578 -- In case of an invalid constraint prevent further processing
22579 -- since the type constructed is missing expected fields.
22581 if Etype (Def_Id) = Any_Type then
22582 return Def_Id;
22583 end if;
22585 -- If the full view is that of a task with discriminants,
22586 -- we must constrain both the concurrent type and its
22587 -- corresponding record type. Otherwise we will just propagate
22588 -- the constraint to the full view, if available.
22590 if Present (Full_View (Subtype_Mark_Id))
22591 and then Has_Discriminants (Subtype_Mark_Id)
22592 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22593 then
22594 Full_View_Id :=
22595 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22597 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22598 Constrain_Concurrent (Full_View_Id, S,
22599 Related_Nod, Related_Id, Suffix);
22600 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22601 Set_Full_View (Def_Id, Full_View_Id);
22603 -- Introduce an explicit reference to the private subtype,
22604 -- to prevent scope anomalies in gigi if first use appears
22605 -- in a nested context, e.g. a later function body.
22606 -- Should this be generated in other contexts than a full
22607 -- type declaration?
22609 if Is_Itype (Def_Id)
22610 and then
22611 Nkind (Parent (P)) = N_Full_Type_Declaration
22612 then
22613 Build_Itype_Reference (Def_Id, Parent (P));
22614 end if;
22616 else
22617 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22618 end if;
22620 when Concurrent_Kind =>
22621 Constrain_Concurrent (Def_Id, S,
22622 Related_Nod, Related_Id, Suffix);
22624 when others =>
22625 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22626 end case;
22628 -- Size, Alignment, Representation aspects and Convention are always
22629 -- inherited from the base type.
22631 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22632 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22633 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22635 -- The anonymous subtype created for the subtype indication
22636 -- inherits the predicates of the parent.
22638 if Has_Predicates (Subtype_Mark_Id) then
22639 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22641 -- Indicate where the predicate function may be found
22643 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22644 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22645 end if;
22646 end if;
22648 return Def_Id;
22649 end if;
22650 end Process_Subtype;
22652 -----------------------------
22653 -- Record_Type_Declaration --
22654 -----------------------------
22656 procedure Record_Type_Declaration
22657 (T : Entity_Id;
22658 N : Node_Id;
22659 Prev : Entity_Id)
22661 Def : constant Node_Id := Type_Definition (N);
22662 Is_Tagged : Boolean;
22663 Tag_Comp : Entity_Id;
22665 begin
22666 -- These flags must be initialized before calling Process_Discriminants
22667 -- because this routine makes use of them.
22669 Mutate_Ekind (T, E_Record_Type);
22670 Set_Etype (T, T);
22671 Reinit_Size_Align (T);
22672 Set_Interfaces (T, No_Elist);
22673 Set_Stored_Constraint (T, No_Elist);
22674 Set_Default_SSO (T);
22675 Set_No_Reordering (T, No_Component_Reordering);
22677 -- Normal case
22679 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22680 -- The flag Is_Tagged_Type might have already been set by
22681 -- Find_Type_Name if it detected an error for declaration T. This
22682 -- arises in the case of private tagged types where the full view
22683 -- omits the word tagged.
22685 Is_Tagged :=
22686 Tagged_Present (Def)
22687 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22689 Set_Is_Limited_Record (T, Limited_Present (Def));
22691 if Is_Tagged then
22692 Set_Is_Tagged_Type (T, True);
22693 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22694 end if;
22696 -- Type is abstract if full declaration carries keyword, or if
22697 -- previous partial view did.
22699 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22700 or else Abstract_Present (Def));
22702 else
22703 Is_Tagged := True;
22704 Analyze_Interface_Declaration (T, Def);
22706 if Present (Discriminant_Specifications (N)) then
22707 Error_Msg_N
22708 ("interface types cannot have discriminants",
22709 Defining_Identifier
22710 (First (Discriminant_Specifications (N))));
22711 end if;
22712 end if;
22714 -- First pass: if there are self-referential access components,
22715 -- create the required anonymous access type declarations, and if
22716 -- need be an incomplete type declaration for T itself.
22718 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22720 if Ada_Version >= Ada_2005
22721 and then Present (Interface_List (Def))
22722 then
22723 Check_Interfaces (N, Def);
22725 declare
22726 Ifaces_List : Elist_Id;
22728 begin
22729 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22730 -- already in the parents.
22732 Collect_Interfaces
22733 (T => T,
22734 Ifaces_List => Ifaces_List,
22735 Exclude_Parents => True);
22737 Set_Interfaces (T, Ifaces_List);
22738 end;
22739 end if;
22741 -- Records constitute a scope for the component declarations within.
22742 -- The scope is created prior to the processing of these declarations.
22743 -- Discriminants are processed first, so that they are visible when
22744 -- processing the other components. The Ekind of the record type itself
22745 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22747 -- Enter record scope
22749 Push_Scope (T);
22751 -- If an incomplete or private type declaration was already given for
22752 -- the type, then this scope already exists, and the discriminants have
22753 -- been declared within. We must verify that the full declaration
22754 -- matches the incomplete one.
22756 Check_Or_Process_Discriminants (N, T, Prev);
22758 Set_Is_Constrained (T, not Has_Discriminants (T));
22759 Set_Has_Delayed_Freeze (T, True);
22761 -- For tagged types add a manually analyzed component corresponding
22762 -- to the component _tag, the corresponding piece of tree will be
22763 -- expanded as part of the freezing actions if it is not a CPP_Class.
22765 if Is_Tagged then
22767 -- Do not add the tag unless we are in expansion mode
22769 if Expander_Active then
22770 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22771 Enter_Name (Tag_Comp);
22773 Mutate_Ekind (Tag_Comp, E_Component);
22774 Set_Is_Tag (Tag_Comp);
22775 Set_Is_Aliased (Tag_Comp);
22776 Set_Is_Independent (Tag_Comp);
22777 Set_Etype (Tag_Comp, RTE (RE_Tag));
22778 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22779 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22780 Reinit_Component_Location (Tag_Comp);
22782 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22783 -- implemented interfaces.
22785 if Has_Interfaces (T) then
22786 Add_Interface_Tag_Components (N, T);
22787 end if;
22788 end if;
22790 Make_Class_Wide_Type (T);
22791 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22792 end if;
22794 -- We must suppress range checks when processing record components in
22795 -- the presence of discriminants, since we don't want spurious checks to
22796 -- be generated during their analysis, but Suppress_Range_Checks flags
22797 -- must be reset the after processing the record definition.
22799 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22800 -- couldn't we just use the normal range check suppression method here.
22801 -- That would seem cleaner ???
22803 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22804 Set_Kill_Range_Checks (T, True);
22805 Record_Type_Definition (Def, Prev);
22806 Set_Kill_Range_Checks (T, False);
22807 else
22808 Record_Type_Definition (Def, Prev);
22809 end if;
22811 -- Exit from record scope
22813 End_Scope;
22815 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22816 -- the implemented interfaces and associate them an aliased entity.
22818 if Is_Tagged
22819 and then not Is_Empty_List (Interface_List (Def))
22820 then
22821 Derive_Progenitor_Subprograms (T, T);
22822 end if;
22824 Check_Function_Writable_Actuals (N);
22825 end Record_Type_Declaration;
22827 ----------------------------
22828 -- Record_Type_Definition --
22829 ----------------------------
22831 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22832 Component : Entity_Id;
22833 Ctrl_Components : Boolean := False;
22834 Final_Storage_Only : Boolean;
22835 T : Entity_Id;
22837 begin
22838 if Ekind (Prev_T) = E_Incomplete_Type then
22839 T := Full_View (Prev_T);
22840 else
22841 T := Prev_T;
22842 end if;
22844 Set_Is_Not_Self_Hidden (T);
22846 Final_Storage_Only := not Is_Controlled (T);
22848 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22849 -- type declaration.
22851 if Parent_Kind (Def) = N_Derived_Type_Definition
22852 and then Limited_Present (Parent (Def))
22853 then
22854 Set_Is_Limited_Record (T);
22855 end if;
22857 -- If the component list of a record type is defined by the reserved
22858 -- word null and there is no discriminant part, then the record type has
22859 -- no components and all records of the type are null records (RM 3.7)
22860 -- This procedure is also called to process the extension part of a
22861 -- record extension, in which case the current scope may have inherited
22862 -- components.
22864 if Present (Def)
22865 and then Present (Component_List (Def))
22866 and then not Null_Present (Component_List (Def))
22867 then
22868 Analyze_Declarations (Component_Items (Component_List (Def)));
22870 if Present (Variant_Part (Component_List (Def))) then
22871 Analyze (Variant_Part (Component_List (Def)));
22872 end if;
22873 end if;
22875 -- After completing the semantic analysis of the record definition,
22876 -- record components, both new and inherited, are accessible. Set their
22877 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22878 -- whose Ekind may be void.
22880 Component := First_Entity (Current_Scope);
22881 while Present (Component) loop
22882 if Ekind (Component) = E_Void
22883 and then not Is_Itype (Component)
22884 then
22885 Mutate_Ekind (Component, E_Component);
22886 Reinit_Component_Location (Component);
22887 Set_Is_Not_Self_Hidden (Component);
22888 end if;
22890 Propagate_Concurrent_Flags (T, Etype (Component));
22892 if Ekind (Component) /= E_Component then
22893 null;
22895 -- Do not set Has_Controlled_Component on a class-wide equivalent
22896 -- type. See Make_CW_Equivalent_Type.
22898 elsif not Is_Class_Wide_Equivalent_Type (T)
22899 and then (Has_Controlled_Component (Etype (Component))
22900 or else (Chars (Component) /= Name_uParent
22901 and then Is_Controlled (Etype (Component))))
22902 then
22903 Set_Has_Controlled_Component (T, True);
22904 Final_Storage_Only :=
22905 Final_Storage_Only
22906 and then Finalize_Storage_Only (Etype (Component));
22907 Ctrl_Components := True;
22908 end if;
22910 Next_Entity (Component);
22911 end loop;
22913 -- A Type is Finalize_Storage_Only only if all its controlled components
22914 -- are also.
22916 if Ctrl_Components then
22917 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22918 end if;
22920 -- Place reference to end record on the proper entity, which may
22921 -- be a partial view.
22923 if Present (Def) then
22924 Process_End_Label (Def, 'e', Prev_T);
22925 end if;
22926 end Record_Type_Definition;
22928 ---------------------------
22929 -- Replace_Discriminants --
22930 ---------------------------
22932 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22933 function Process (N : Node_Id) return Traverse_Result;
22935 -------------
22936 -- Process --
22937 -------------
22939 function Process (N : Node_Id) return Traverse_Result is
22940 Comp : Entity_Id;
22942 begin
22943 if Nkind (N) = N_Discriminant_Specification then
22944 Comp := First_Discriminant (Typ);
22945 while Present (Comp) loop
22946 if Original_Record_Component (Comp) = Defining_Identifier (N)
22947 or else Chars (Comp) = Chars (Defining_Identifier (N))
22948 then
22949 Set_Defining_Identifier (N, Comp);
22950 exit;
22951 end if;
22953 Next_Discriminant (Comp);
22954 end loop;
22956 elsif Nkind (N) = N_Variant_Part then
22957 Comp := First_Discriminant (Typ);
22958 while Present (Comp) loop
22959 if Original_Record_Component (Comp) = Entity (Name (N))
22960 or else Chars (Comp) = Chars (Name (N))
22961 then
22962 -- Make sure to preserve the type coming from the parent on
22963 -- the Name, even if the subtype of the discriminant can be
22964 -- constrained, so that discrete choices inherited from the
22965 -- parent in the variant part are not flagged as violating
22966 -- the constraints of the subtype.
22968 declare
22969 Typ : constant Entity_Id := Etype (Name (N));
22970 begin
22971 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22972 Set_Etype (Name (N), Typ);
22973 end;
22974 exit;
22975 end if;
22977 Next_Discriminant (Comp);
22978 end loop;
22979 end if;
22981 return OK;
22982 end Process;
22984 procedure Replace is new Traverse_Proc (Process);
22986 -- Start of processing for Replace_Discriminants
22988 begin
22989 Replace (Decl);
22990 end Replace_Discriminants;
22992 -------------------------------
22993 -- Set_Completion_Referenced --
22994 -------------------------------
22996 procedure Set_Completion_Referenced (E : Entity_Id) is
22997 begin
22998 -- If in main unit, mark entity that is a completion as referenced,
22999 -- warnings go on the partial view when needed.
23001 if In_Extended_Main_Source_Unit (E) then
23002 Set_Referenced (E);
23003 end if;
23004 end Set_Completion_Referenced;
23006 ---------------------
23007 -- Set_Default_SSO --
23008 ---------------------
23010 procedure Set_Default_SSO (T : Entity_Id) is
23011 begin
23012 case Opt.Default_SSO is
23013 when ' ' =>
23014 null;
23015 when 'L' =>
23016 Set_SSO_Set_Low_By_Default (T, True);
23017 when 'H' =>
23018 Set_SSO_Set_High_By_Default (T, True);
23019 when others =>
23020 raise Program_Error;
23021 end case;
23022 end Set_Default_SSO;
23024 ---------------------
23025 -- Set_Fixed_Range --
23026 ---------------------
23028 -- The range for fixed-point types is complicated by the fact that we
23029 -- do not know the exact end points at the time of the declaration. This
23030 -- is true for three reasons:
23032 -- A size clause may affect the fudging of the end-points.
23033 -- A small clause may affect the values of the end-points.
23034 -- We try to include the end-points if it does not affect the size.
23036 -- This means that the actual end-points must be established at the
23037 -- point when the type is frozen. Meanwhile, we first narrow the range
23038 -- as permitted (so that it will fit if necessary in a small specified
23039 -- size), and then build a range subtree with these narrowed bounds.
23040 -- Set_Fixed_Range constructs the range from real literal values, and
23041 -- sets the range as the Scalar_Range of the given fixed-point type entity.
23043 -- The parent of this range is set to point to the entity so that it is
23044 -- properly hooked into the tree (unlike normal Scalar_Range entries for
23045 -- other scalar types, which are just pointers to the range in the
23046 -- original tree, this would otherwise be an orphan).
23048 -- The tree is left unanalyzed. When the type is frozen, the processing
23049 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
23050 -- analyzed, and uses this as an indication that it should complete
23051 -- work on the range (it will know the final small and size values).
23053 procedure Set_Fixed_Range
23054 (E : Entity_Id;
23055 Loc : Source_Ptr;
23056 Lo : Ureal;
23057 Hi : Ureal)
23059 S : constant Node_Id :=
23060 Make_Range (Loc,
23061 Low_Bound => Make_Real_Literal (Loc, Lo),
23062 High_Bound => Make_Real_Literal (Loc, Hi));
23063 begin
23064 Set_Scalar_Range (E, S);
23065 Set_Parent (S, E);
23067 -- Before the freeze point, the bounds of a fixed point are universal
23068 -- and carry the corresponding type.
23070 Set_Etype (Low_Bound (S), Universal_Real);
23071 Set_Etype (High_Bound (S), Universal_Real);
23072 end Set_Fixed_Range;
23074 ----------------------------------
23075 -- Set_Scalar_Range_For_Subtype --
23076 ----------------------------------
23078 procedure Set_Scalar_Range_For_Subtype
23079 (Def_Id : Entity_Id;
23080 R : Node_Id;
23081 Subt : Entity_Id)
23083 Kind : constant Entity_Kind := Ekind (Def_Id);
23085 begin
23086 -- Defend against previous error
23088 if Nkind (R) = N_Error then
23089 return;
23090 end if;
23092 Set_Scalar_Range (Def_Id, R);
23094 -- We need to link the range into the tree before resolving it so
23095 -- that types that are referenced, including importantly the subtype
23096 -- itself, are properly frozen (Freeze_Expression requires that the
23097 -- expression be properly linked into the tree). Of course if it is
23098 -- already linked in, then we do not disturb the current link.
23100 if No (Parent (R)) then
23101 Set_Parent (R, Def_Id);
23102 end if;
23104 -- Reset the kind of the subtype during analysis of the range, to
23105 -- catch possible premature use in the bounds themselves.
23107 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23108 pragma Assert (Ekind (Def_Id) = Kind);
23109 end Set_Scalar_Range_For_Subtype;
23111 --------------------------------------------------------
23112 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23113 --------------------------------------------------------
23115 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23116 (E : Entity_Id)
23118 begin
23119 -- Make sure set if encountered during Expand_To_Stored_Constraint
23121 Set_Stored_Constraint (E, No_Elist);
23123 -- Give it the right value
23125 if Is_Constrained (E) and then Has_Discriminants (E) then
23126 Set_Stored_Constraint (E,
23127 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23128 end if;
23129 end Set_Stored_Constraint_From_Discriminant_Constraint;
23131 -------------------------------------
23132 -- Signed_Integer_Type_Declaration --
23133 -------------------------------------
23135 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23136 Implicit_Base : Entity_Id;
23137 Base_Typ : Entity_Id;
23138 Lo_Val : Uint;
23139 Hi_Val : Uint;
23140 Errs : Boolean := False;
23141 Lo : Node_Id;
23142 Hi : Node_Id;
23144 function Can_Derive_From (E : Entity_Id) return Boolean;
23145 -- Determine whether given bounds allow derivation from specified type
23147 procedure Check_Bound (Expr : Node_Id);
23148 -- Check bound to make sure it is integral and static. If not, post
23149 -- appropriate error message and set Errs flag
23151 ---------------------
23152 -- Can_Derive_From --
23153 ---------------------
23155 -- Note we check both bounds against both end values, to deal with
23156 -- strange types like ones with a range of 0 .. -12341234.
23158 function Can_Derive_From (E : Entity_Id) return Boolean is
23159 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23160 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23161 begin
23162 return Lo <= Lo_Val and then Lo_Val <= Hi
23163 and then
23164 Lo <= Hi_Val and then Hi_Val <= Hi;
23165 end Can_Derive_From;
23167 -----------------
23168 -- Check_Bound --
23169 -----------------
23171 procedure Check_Bound (Expr : Node_Id) is
23172 begin
23173 -- If a range constraint is used as an integer type definition, each
23174 -- bound of the range must be defined by a static expression of some
23175 -- integer type, but the two bounds need not have the same integer
23176 -- type (Negative bounds are allowed.) (RM 3.5.4)
23178 if not Is_Integer_Type (Etype (Expr)) then
23179 Error_Msg_N
23180 ("integer type definition bounds must be of integer type", Expr);
23181 Errs := True;
23183 elsif not Is_OK_Static_Expression (Expr) then
23184 Flag_Non_Static_Expr
23185 ("non-static expression used for integer type bound!", Expr);
23186 Errs := True;
23188 -- Otherwise the bounds are folded into literals
23190 elsif Is_Entity_Name (Expr) then
23191 Fold_Uint (Expr, Expr_Value (Expr), True);
23192 end if;
23193 end Check_Bound;
23195 -- Start of processing for Signed_Integer_Type_Declaration
23197 begin
23198 -- Create an anonymous base type
23200 Implicit_Base :=
23201 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23203 -- Analyze and check the bounds, they can be of any integer type
23205 Lo := Low_Bound (Def);
23206 Hi := High_Bound (Def);
23208 -- Arbitrarily use Integer as the type if either bound had an error
23210 if Hi = Error or else Lo = Error then
23211 Base_Typ := Any_Integer;
23212 Set_Error_Posted (T, True);
23213 Errs := True;
23215 -- Here both bounds are OK expressions
23217 else
23218 Analyze_And_Resolve (Lo, Any_Integer);
23219 Analyze_And_Resolve (Hi, Any_Integer);
23221 Check_Bound (Lo);
23222 Check_Bound (Hi);
23224 if Errs then
23225 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23226 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23227 end if;
23229 -- Find type to derive from
23231 Lo_Val := Expr_Value (Lo);
23232 Hi_Val := Expr_Value (Hi);
23234 if Can_Derive_From (Standard_Short_Short_Integer) then
23235 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23237 elsif Can_Derive_From (Standard_Short_Integer) then
23238 Base_Typ := Base_Type (Standard_Short_Integer);
23240 elsif Can_Derive_From (Standard_Integer) then
23241 Base_Typ := Base_Type (Standard_Integer);
23243 elsif Can_Derive_From (Standard_Long_Integer) then
23244 Base_Typ := Base_Type (Standard_Long_Integer);
23246 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23247 Check_Restriction (No_Long_Long_Integers, Def);
23248 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23250 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23251 Check_Restriction (No_Long_Long_Integers, Def);
23252 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23254 else
23255 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23256 Error_Msg_N ("integer type definition bounds out of range", Def);
23257 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23258 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23259 end if;
23260 end if;
23262 -- Set the type of the bounds to the implicit base: we cannot set it to
23263 -- the new type, because this would be a forward reference for the code
23264 -- generator and, if the original type is user-defined, this could even
23265 -- lead to spurious semantic errors. Furthermore we do not set it to be
23266 -- universal, because this could make it much larger than needed here.
23268 if not Errs then
23269 Set_Etype (Lo, Implicit_Base);
23270 Set_Etype (Hi, Implicit_Base);
23271 end if;
23273 -- Complete both implicit base and declared first subtype entities. The
23274 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23275 -- are not clobbered when the signed integer type acts as a full view of
23276 -- a private type.
23278 Set_Etype (Implicit_Base, Base_Typ);
23279 Set_Size_Info (Implicit_Base, Base_Typ);
23280 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23281 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23282 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23284 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23285 Set_Etype (T, Implicit_Base);
23286 Set_Size_Info (T, Implicit_Base);
23287 Inherit_Rep_Item_Chain (T, Implicit_Base);
23288 Set_Scalar_Range (T, Def);
23289 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23290 Set_Is_Constrained (T);
23291 end Signed_Integer_Type_Declaration;
23293 end Sem_Ch3;