Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob299ea6e989f7ccfdd8669ac09f1b9c7326bb4d38
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);
1446 -- For SPARK, check that the designated type is compatible with
1447 -- respect to volatility with the access type.
1449 if SPARK_Mode /= Off
1450 and then Comes_From_Source (T)
1451 then
1452 -- ??? UNIMPLEMENTED
1453 -- In the case where the designated type is incomplete at this
1454 -- point, performing this check here is harmless but the check
1455 -- will need to be repeated when the designated type is complete.
1457 -- The preceding call to Comes_From_Source is needed because the
1458 -- FE sometimes introduces implicitly declared access types. See,
1459 -- for example, the expansion of nested_po.ads in OA28-015.
1461 Check_Volatility_Compatibility
1462 (Full_Desig, T, "designated type", "access type",
1463 Srcpos_Bearer => T);
1464 end if;
1465 end if;
1467 -- If the type has appeared already in a with_type clause, it is frozen
1468 -- and the pointer size is already set. Else, initialize.
1470 if not From_Limited_With (T) then
1471 Reinit_Size_Align (T);
1472 end if;
1474 -- Note that Has_Task is always false, since the access type itself
1475 -- is not a task type. See Einfo for more description on this point.
1476 -- Exactly the same consideration applies to Has_Controlled_Component
1477 -- and to Has_Protected.
1479 Set_Has_Task (T, False);
1480 Set_Has_Protected (T, False);
1481 Set_Has_Timing_Event (T, False);
1482 Set_Has_Controlled_Component (T, False);
1484 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1485 -- problems where an incomplete view of this entity has been previously
1486 -- established by a limited with and an overlaid version of this field
1487 -- (Stored_Constraint) was initialized for the incomplete view.
1489 -- This reset is performed in most cases except where the access type
1490 -- has been created for the purposes of allocating or deallocating a
1491 -- build-in-place object. Such access types have explicitly set pools
1492 -- and finalization masters.
1494 if No (Associated_Storage_Pool (T)) then
1495 Set_Finalization_Master (T, Empty);
1496 end if;
1498 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1499 -- attributes
1501 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1502 Set_Is_Access_Constant (T, Constant_Present (Def));
1503 end Access_Type_Declaration;
1505 ----------------------------------
1506 -- Add_Interface_Tag_Components --
1507 ----------------------------------
1509 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1510 Loc : constant Source_Ptr := Sloc (N);
1511 L : List_Id;
1512 Last_Tag : Node_Id;
1514 procedure Add_Tag (Iface : Entity_Id);
1515 -- Add tag for one of the progenitor interfaces
1517 -------------
1518 -- Add_Tag --
1519 -------------
1521 procedure Add_Tag (Iface : Entity_Id) is
1522 Decl : Node_Id;
1523 Def : Node_Id;
1524 Tag : Entity_Id;
1525 Offset : Entity_Id;
1527 begin
1528 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1530 -- This is a reasonable place to propagate predicates
1532 if Has_Predicates (Iface) then
1533 Set_Has_Predicates (Typ);
1534 end if;
1536 Def :=
1537 Make_Component_Definition (Loc,
1538 Aliased_Present => True,
1539 Subtype_Indication =>
1540 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1542 Tag := Make_Temporary (Loc, 'V');
1544 Decl :=
1545 Make_Component_Declaration (Loc,
1546 Defining_Identifier => Tag,
1547 Component_Definition => Def);
1549 Analyze_Component_Declaration (Decl);
1551 Set_Analyzed (Decl);
1552 Mutate_Ekind (Tag, E_Component);
1553 Set_Is_Tag (Tag);
1554 Set_Is_Aliased (Tag);
1555 Set_Is_Independent (Tag);
1556 Set_Related_Type (Tag, Iface);
1557 Reinit_Component_Location (Tag);
1559 pragma Assert (Is_Frozen (Iface));
1561 Set_DT_Entry_Count (Tag,
1562 DT_Entry_Count (First_Entity (Iface)));
1564 if No (Last_Tag) then
1565 Prepend (Decl, L);
1566 else
1567 Insert_After (Last_Tag, Decl);
1568 end if;
1570 Last_Tag := Decl;
1572 -- If the ancestor has discriminants we need to give special support
1573 -- to store the offset_to_top value of the secondary dispatch tables.
1574 -- For this purpose we add a supplementary component just after the
1575 -- field that contains the tag associated with each secondary DT.
1577 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1578 Def :=
1579 Make_Component_Definition (Loc,
1580 Subtype_Indication =>
1581 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1583 Offset := Make_Temporary (Loc, 'V');
1585 Decl :=
1586 Make_Component_Declaration (Loc,
1587 Defining_Identifier => Offset,
1588 Component_Definition => Def);
1590 Analyze_Component_Declaration (Decl);
1592 Set_Analyzed (Decl);
1593 Mutate_Ekind (Offset, E_Component);
1594 Set_Is_Aliased (Offset);
1595 Set_Is_Independent (Offset);
1596 Set_Related_Type (Offset, Iface);
1597 Reinit_Component_Location (Offset);
1598 Insert_After (Last_Tag, Decl);
1599 Last_Tag := Decl;
1600 end if;
1601 end Add_Tag;
1603 -- Local variables
1605 Elmt : Elmt_Id;
1606 Ext : Node_Id;
1607 Comp : Node_Id;
1609 -- Start of processing for Add_Interface_Tag_Components
1611 begin
1612 if not RTE_Available (RE_Interface_Tag) then
1613 Error_Msg_N
1614 ("(Ada 2005) interface types not supported by this run-time!", N);
1615 return;
1616 end if;
1618 if Ekind (Typ) /= E_Record_Type
1619 or else (Is_Concurrent_Record_Type (Typ)
1620 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1621 or else (not Is_Concurrent_Record_Type (Typ)
1622 and then No (Interfaces (Typ))
1623 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1624 then
1625 return;
1626 end if;
1628 -- Find the current last tag
1630 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1631 Ext := Record_Extension_Part (Type_Definition (N));
1632 else
1633 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1634 Ext := Type_Definition (N);
1635 end if;
1637 Last_Tag := Empty;
1639 if not (Present (Component_List (Ext))) then
1640 Set_Null_Present (Ext, False);
1641 L := New_List;
1642 Set_Component_List (Ext,
1643 Make_Component_List (Loc,
1644 Component_Items => L,
1645 Null_Present => False));
1646 else
1647 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1648 L := Component_Items
1649 (Component_List
1650 (Record_Extension_Part
1651 (Type_Definition (N))));
1652 else
1653 L := Component_Items
1654 (Component_List
1655 (Type_Definition (N)));
1656 end if;
1658 -- Find the last tag component
1660 Comp := First (L);
1661 while Present (Comp) loop
1662 if Nkind (Comp) = N_Component_Declaration
1663 and then Is_Tag (Defining_Identifier (Comp))
1664 then
1665 Last_Tag := Comp;
1666 end if;
1668 Next (Comp);
1669 end loop;
1670 end if;
1672 -- At this point L references the list of components and Last_Tag
1673 -- references the current last tag (if any). Now we add the tag
1674 -- corresponding with all the interfaces that are not implemented
1675 -- by the parent.
1677 if Present (Interfaces (Typ)) then
1678 Elmt := First_Elmt (Interfaces (Typ));
1679 while Present (Elmt) loop
1680 Add_Tag (Node (Elmt));
1681 Next_Elmt (Elmt);
1682 end loop;
1683 end if;
1684 end Add_Interface_Tag_Components;
1686 -------------------------------------
1687 -- Add_Internal_Interface_Entities --
1688 -------------------------------------
1690 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1691 Elmt : Elmt_Id;
1692 Iface : Entity_Id;
1693 Iface_Elmt : Elmt_Id;
1694 Iface_Prim : Entity_Id;
1695 Ifaces_List : Elist_Id;
1696 New_Subp : Entity_Id := Empty;
1697 Prim : Entity_Id;
1698 Restore_Scope : Boolean := False;
1700 begin
1701 pragma Assert (Ada_Version >= Ada_2005
1702 and then Is_Record_Type (Tagged_Type)
1703 and then Is_Tagged_Type (Tagged_Type)
1704 and then Has_Interfaces (Tagged_Type)
1705 and then not Is_Interface (Tagged_Type));
1707 -- Ensure that the internal entities are added to the scope of the type
1709 if Scope (Tagged_Type) /= Current_Scope then
1710 Push_Scope (Scope (Tagged_Type));
1711 Restore_Scope := True;
1712 end if;
1714 Collect_Interfaces (Tagged_Type, Ifaces_List);
1716 Iface_Elmt := First_Elmt (Ifaces_List);
1717 while Present (Iface_Elmt) loop
1718 Iface := Node (Iface_Elmt);
1720 -- Originally we excluded here from this processing interfaces that
1721 -- are parents of Tagged_Type because their primitives are located
1722 -- in the primary dispatch table (and hence no auxiliary internal
1723 -- entities are required to handle secondary dispatch tables in such
1724 -- case). However, these auxiliary entities are also required to
1725 -- handle derivations of interfaces in formals of generics (see
1726 -- Derive_Subprograms).
1728 Elmt := First_Elmt (Primitive_Operations (Iface));
1729 while Present (Elmt) loop
1730 Iface_Prim := Node (Elmt);
1732 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1733 Prim :=
1734 Find_Primitive_Covering_Interface
1735 (Tagged_Type => Tagged_Type,
1736 Iface_Prim => Iface_Prim);
1738 if No (Prim) and then Serious_Errors_Detected > 0 then
1739 goto Continue;
1740 end if;
1742 pragma Assert (Present (Prim));
1744 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1745 -- differs from the name of the interface primitive then it is
1746 -- a private primitive inherited from a parent type. In such
1747 -- case, given that Tagged_Type covers the interface, the
1748 -- inherited private primitive becomes visible. For such
1749 -- purpose we add a new entity that renames the inherited
1750 -- private primitive.
1752 if Chars (Prim) /= Chars (Iface_Prim) then
1753 pragma Assert (Has_Suffix (Prim, 'P'));
1754 Derive_Subprogram
1755 (New_Subp => New_Subp,
1756 Parent_Subp => Iface_Prim,
1757 Derived_Type => Tagged_Type,
1758 Parent_Type => Iface);
1759 Set_Alias (New_Subp, Prim);
1760 Set_Is_Abstract_Subprogram
1761 (New_Subp, Is_Abstract_Subprogram (Prim));
1762 end if;
1764 Derive_Subprogram
1765 (New_Subp => New_Subp,
1766 Parent_Subp => Iface_Prim,
1767 Derived_Type => Tagged_Type,
1768 Parent_Type => Iface);
1770 declare
1771 Anc : Entity_Id;
1772 begin
1773 if Is_Inherited_Operation (Prim)
1774 and then Present (Alias (Prim))
1775 then
1776 Anc := Alias (Prim);
1777 else
1778 Anc := Overridden_Operation (Prim);
1779 end if;
1781 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1782 -- nonconforming preconditions in both an ancestor and
1783 -- a progenitor operation.
1785 -- If the operation is a primitive wrapper it is an explicit
1786 -- (overriding) operqtion and all is fine.
1788 if Present (Anc)
1789 and then Has_Non_Trivial_Precondition (Anc)
1790 and then Has_Non_Trivial_Precondition (Iface_Prim)
1791 then
1792 if Is_Abstract_Subprogram (Prim)
1793 or else
1794 (Ekind (Prim) = E_Procedure
1795 and then Nkind (Parent (Prim)) =
1796 N_Procedure_Specification
1797 and then Null_Present (Parent (Prim)))
1798 or else Is_Primitive_Wrapper (Prim)
1799 then
1800 null;
1802 -- The operation is inherited and must be overridden
1804 elsif not Comes_From_Source (Prim) then
1805 Error_Msg_NE
1806 ("&inherits non-conforming preconditions and must "
1807 & "be overridden (RM 6.1.1 (10-16))",
1808 Parent (Tagged_Type), Prim);
1809 end if;
1810 end if;
1811 end;
1813 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1814 -- associated with interface types. These entities are
1815 -- only registered in the list of primitives of its
1816 -- corresponding tagged type because they are only used
1817 -- to fill the contents of the secondary dispatch tables.
1818 -- Therefore they are removed from the homonym chains.
1820 Set_Is_Hidden (New_Subp);
1821 Set_Is_Internal (New_Subp);
1822 Set_Alias (New_Subp, Prim);
1823 Set_Is_Abstract_Subprogram
1824 (New_Subp, Is_Abstract_Subprogram (Prim));
1825 Set_Interface_Alias (New_Subp, Iface_Prim);
1827 -- If the returned type is an interface then propagate it to
1828 -- the returned type. Needed by the thunk to generate the code
1829 -- which displaces "this" to reference the corresponding
1830 -- secondary dispatch table in the returned object.
1832 if Is_Interface (Etype (Iface_Prim)) then
1833 Set_Etype (New_Subp, Etype (Iface_Prim));
1834 end if;
1836 -- Internal entities associated with interface types are only
1837 -- registered in the list of primitives of the tagged type.
1838 -- They are only used to fill the contents of the secondary
1839 -- dispatch tables. Therefore they are not needed in the
1840 -- homonym chains.
1842 Remove_Homonym (New_Subp);
1844 -- Hidden entities associated with interfaces must have set
1845 -- the Has_Delay_Freeze attribute to ensure that, in case
1846 -- of locally defined tagged types (or compiling with static
1847 -- dispatch tables generation disabled) the corresponding
1848 -- entry of the secondary dispatch table is filled when such
1849 -- an entity is frozen.
1851 Set_Has_Delayed_Freeze (New_Subp);
1852 end if;
1854 <<Continue>>
1855 Next_Elmt (Elmt);
1856 end loop;
1858 Next_Elmt (Iface_Elmt);
1859 end loop;
1861 if Restore_Scope then
1862 Pop_Scope;
1863 end if;
1864 end Add_Internal_Interface_Entities;
1866 -----------------------------------
1867 -- Analyze_Component_Declaration --
1868 -----------------------------------
1870 procedure Analyze_Component_Declaration (N : Node_Id) is
1871 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1872 Id : constant Entity_Id := Defining_Identifier (N);
1873 E : constant Node_Id := Expression (N);
1874 Typ : constant Node_Id :=
1875 Subtype_Indication (Component_Definition (N));
1876 T : Entity_Id;
1877 P : Entity_Id;
1879 function Contains_POC (Constr : Node_Id) return Boolean;
1880 -- Determines whether a constraint uses the discriminant of a record
1881 -- type thus becoming a per-object constraint (POC).
1883 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1884 -- Typ is the type of the current component, check whether this type is
1885 -- a limited type. Used to validate declaration against that of
1886 -- enclosing record.
1888 ------------------
1889 -- Contains_POC --
1890 ------------------
1892 function Contains_POC (Constr : Node_Id) return Boolean is
1893 begin
1894 -- Prevent cascaded errors
1896 if Error_Posted (Constr) then
1897 return False;
1898 end if;
1900 case Nkind (Constr) is
1901 when N_Attribute_Reference =>
1902 return Attribute_Name (Constr) = Name_Access
1903 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1905 when N_Discriminant_Association =>
1906 return Denotes_Discriminant (Expression (Constr));
1908 when N_Identifier =>
1909 return Denotes_Discriminant (Constr);
1911 when N_Index_Or_Discriminant_Constraint =>
1912 declare
1913 IDC : Node_Id;
1915 begin
1916 IDC := First (Constraints (Constr));
1917 while Present (IDC) loop
1919 -- One per-object constraint is sufficient
1921 if Contains_POC (IDC) then
1922 return True;
1923 end if;
1925 Next (IDC);
1926 end loop;
1928 return False;
1929 end;
1931 when N_Range =>
1932 return Denotes_Discriminant (Low_Bound (Constr))
1933 or else
1934 Denotes_Discriminant (High_Bound (Constr));
1936 when N_Range_Constraint =>
1937 return Denotes_Discriminant (Range_Expression (Constr));
1939 when others =>
1940 return False;
1941 end case;
1942 end Contains_POC;
1944 ----------------------
1945 -- Is_Known_Limited --
1946 ----------------------
1948 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1949 P : constant Entity_Id := Etype (Typ);
1950 R : constant Entity_Id := Root_Type (Typ);
1952 begin
1953 if Is_Limited_Record (Typ) then
1954 return True;
1956 -- If the root type is limited (and not a limited interface) so is
1957 -- the current type.
1959 elsif Is_Limited_Record (R)
1960 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1961 then
1962 return True;
1964 -- Else the type may have a limited interface progenitor, but a
1965 -- limited record parent that is not an interface.
1967 elsif R /= P
1968 and then Is_Limited_Record (P)
1969 and then not Is_Interface (P)
1970 then
1971 return True;
1973 else
1974 return False;
1975 end if;
1976 end Is_Known_Limited;
1978 -- Start of processing for Analyze_Component_Declaration
1980 begin
1981 Generate_Definition (Id);
1982 Enter_Name (Id);
1984 if Present (Typ) then
1985 T := Find_Type_Of_Object
1986 (Subtype_Indication (Component_Definition (N)), N);
1988 -- Ada 2005 (AI-230): Access Definition case
1990 else
1991 pragma Assert (Present
1992 (Access_Definition (Component_Definition (N))));
1994 T := Access_Definition
1995 (Related_Nod => N,
1996 N => Access_Definition (Component_Definition (N)));
1997 Set_Is_Local_Anonymous_Access (T);
1999 -- Ada 2005 (AI-254)
2001 if Present (Access_To_Subprogram_Definition
2002 (Access_Definition (Component_Definition (N))))
2003 and then Protected_Present (Access_To_Subprogram_Definition
2004 (Access_Definition
2005 (Component_Definition (N))))
2006 then
2007 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2008 end if;
2009 end if;
2011 -- If the subtype is a constrained subtype of the enclosing record,
2012 -- (which must have a partial view) the back-end does not properly
2013 -- handle the recursion. Rewrite the component declaration with an
2014 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2015 -- the tree directly because side effects have already been removed from
2016 -- discriminant constraints.
2018 if Ekind (T) = E_Access_Subtype
2019 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2020 and then Comes_From_Source (T)
2021 and then Nkind (Parent (T)) = N_Subtype_Declaration
2022 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2023 then
2024 Rewrite
2025 (Subtype_Indication (Component_Definition (N)),
2026 New_Copy_Tree (Subtype_Indication (Parent (T))));
2027 T := Find_Type_Of_Object
2028 (Subtype_Indication (Component_Definition (N)), N);
2029 end if;
2031 -- If the component declaration includes a default expression, then we
2032 -- check that the component is not of a limited type (RM 3.7(5)),
2033 -- and do the special preanalysis of the expression (see section on
2034 -- "Handling of Default and Per-Object Expressions" in the spec of
2035 -- package Sem).
2037 if Present (E) then
2038 Preanalyze_Default_Expression (E, T);
2039 Check_Initialization (T, E);
2041 if Ada_Version >= Ada_2005
2042 and then Ekind (T) = E_Anonymous_Access_Type
2043 and then Etype (E) /= Any_Type
2044 then
2045 -- Check RM 3.9.2(9): "if the expected type for an expression is
2046 -- an anonymous access-to-specific tagged type, then the object
2047 -- designated by the expression shall not be dynamically tagged
2048 -- unless it is a controlling operand in a call on a dispatching
2049 -- operation"
2051 if Is_Tagged_Type (Directly_Designated_Type (T))
2052 and then
2053 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2054 and then
2055 Ekind (Directly_Designated_Type (Etype (E))) =
2056 E_Class_Wide_Type
2057 then
2058 Error_Msg_N
2059 ("access to specific tagged type required (RM 3.9.2(9))", E);
2060 end if;
2062 -- (Ada 2005: AI-230): Accessibility check for anonymous
2063 -- components
2065 if Type_Access_Level (Etype (E)) >
2066 Deepest_Type_Access_Level (T)
2067 then
2068 Error_Msg_N
2069 ("expression has deeper access level than component " &
2070 "(RM 3.10.2 (12.2))", E);
2071 end if;
2073 -- The initialization expression is a reference to an access
2074 -- discriminant. The type of the discriminant is always deeper
2075 -- than any access type.
2077 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2078 and then Is_Entity_Name (E)
2079 and then Ekind (Entity (E)) = E_In_Parameter
2080 and then Present (Discriminal_Link (Entity (E)))
2081 then
2082 Error_Msg_N
2083 ("discriminant has deeper accessibility level than target",
2085 end if;
2086 end if;
2087 end if;
2089 -- The parent type may be a private view with unknown discriminants,
2090 -- and thus unconstrained. Regular components must be constrained.
2092 if not Is_Definite_Subtype (T)
2093 and then Chars (Id) /= Name_uParent
2094 then
2095 if Is_Class_Wide_Type (T) then
2096 Error_Msg_N
2097 ("class-wide subtype with unknown discriminants" &
2098 " in component declaration",
2099 Subtype_Indication (Component_Definition (N)));
2100 else
2101 Error_Msg_N
2102 ("unconstrained subtype in component declaration",
2103 Subtype_Indication (Component_Definition (N)));
2104 end if;
2106 -- Components cannot be abstract, except for the special case of
2107 -- the _Parent field (case of extending an abstract tagged type)
2109 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2110 Error_Msg_N ("type of a component cannot be abstract", N);
2111 end if;
2113 Set_Etype (Id, T);
2115 if Aliased_Present (Component_Definition (N)) then
2116 Set_Is_Aliased (Id);
2118 -- AI12-001: All aliased objects are considered to be specified as
2119 -- independently addressable (RM C.6(8.1/4)).
2121 Set_Is_Independent (Id);
2122 end if;
2124 -- The component declaration may have a per-object constraint, set
2125 -- the appropriate flag in the defining identifier of the subtype.
2127 if Present (Subtype_Indication (Component_Definition (N))) then
2128 declare
2129 Sindic : constant Node_Id :=
2130 Subtype_Indication (Component_Definition (N));
2131 begin
2132 if Nkind (Sindic) = N_Subtype_Indication
2133 and then Present (Constraint (Sindic))
2134 and then Contains_POC (Constraint (Sindic))
2135 then
2136 Set_Has_Per_Object_Constraint (Id);
2137 end if;
2138 end;
2139 end if;
2141 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2142 -- out some static checks.
2144 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2145 Null_Exclusion_Static_Checks (N);
2146 end if;
2148 -- If this component is private (or depends on a private type), flag the
2149 -- record type to indicate that some operations are not available.
2151 P := Private_Component (T);
2153 if Present (P) then
2155 -- Check for circular definitions
2157 if P = Any_Type then
2158 Set_Etype (Id, Any_Type);
2160 -- There is a gap in the visibility of operations only if the
2161 -- component type is not defined in the scope of the record type.
2163 elsif Scope (P) = Scope (Current_Scope) then
2164 null;
2166 elsif Is_Limited_Type (P) then
2167 Set_Is_Limited_Composite (Current_Scope);
2169 else
2170 Set_Is_Private_Composite (Current_Scope);
2171 end if;
2172 end if;
2174 if P /= Any_Type
2175 and then Is_Limited_Type (T)
2176 and then Chars (Id) /= Name_uParent
2177 and then Is_Tagged_Type (Current_Scope)
2178 then
2179 if Is_Derived_Type (Current_Scope)
2180 and then not Is_Known_Limited (Current_Scope)
2181 then
2182 Error_Msg_N
2183 ("extension of nonlimited type cannot have limited components",
2186 if Is_Interface (Root_Type (Current_Scope)) then
2187 Error_Msg_N
2188 ("\limitedness is not inherited from limited interface", N);
2189 Error_Msg_N ("\add LIMITED to type indication", N);
2190 end if;
2192 Explain_Limited_Type (T, N);
2193 Set_Etype (Id, Any_Type);
2194 Set_Is_Limited_Composite (Current_Scope, False);
2196 elsif not Is_Derived_Type (Current_Scope)
2197 and then not Is_Limited_Record (Current_Scope)
2198 and then not Is_Concurrent_Type (Current_Scope)
2199 then
2200 Error_Msg_N
2201 ("nonlimited tagged type cannot have limited components", N);
2202 Explain_Limited_Type (T, N);
2203 Set_Etype (Id, Any_Type);
2204 Set_Is_Limited_Composite (Current_Scope, False);
2205 end if;
2206 end if;
2208 -- When possible, build the default subtype
2210 if Build_Default_Subtype_OK (T) then
2211 declare
2212 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2214 begin
2215 Set_Etype (Id, Act_T);
2217 -- Rewrite component definition to use the constrained subtype
2219 Rewrite (Component_Definition (N),
2220 Make_Component_Definition (Loc,
2221 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2222 end;
2223 end if;
2225 Set_Original_Record_Component (Id, Id);
2227 if Has_Aspects (N) then
2228 Analyze_Aspect_Specifications (N, Id);
2229 end if;
2231 Analyze_Dimension (N);
2232 end Analyze_Component_Declaration;
2234 --------------------------
2235 -- Analyze_Declarations --
2236 --------------------------
2238 procedure Analyze_Declarations (L : List_Id) is
2239 Decl : Node_Id;
2241 procedure Adjust_Decl;
2242 -- Adjust Decl not to include implicit label declarations, since these
2243 -- have strange Sloc values that result in elaboration check problems.
2244 -- (They have the sloc of the label as found in the source, and that
2245 -- is ahead of the current declarative part).
2247 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2248 -- Create the subprogram bodies which verify the run-time semantics of
2249 -- the pragmas listed below for each elibigle type found in declarative
2250 -- list Decls. The pragmas are:
2252 -- Default_Initial_Condition
2253 -- Invariant
2254 -- Type_Invariant
2256 -- Context denotes the owner of the declarative list.
2258 procedure Check_Entry_Contracts;
2259 -- Perform a preanalysis of the pre- and postconditions of an entry
2260 -- declaration. This must be done before full resolution and creation
2261 -- of the parameter block, etc. to catch illegal uses within the
2262 -- contract expression. Full analysis of the expression is done when
2263 -- the contract is processed.
2265 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2266 -- Check if a nested package has entities within it that rely on library
2267 -- level private types where the full view has not been completed for
2268 -- the purposes of checking if it is acceptable to freeze an expression
2269 -- function at the point of declaration.
2271 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2272 -- Determine whether Body_Decl denotes the body of a late controlled
2273 -- primitive (either Initialize, Adjust or Finalize). If this is the
2274 -- case, add a proper spec if the body lacks one. The spec is inserted
2275 -- before Body_Decl and immediately analyzed.
2277 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2278 -- Spec_Id is the entity of a package that may define abstract states,
2279 -- and in the case of a child unit, whose ancestors may define abstract
2280 -- states. If the states have partial visible refinement, remove the
2281 -- partial visibility of each constituent at the end of the package
2282 -- spec and body declarations.
2284 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2285 -- Spec_Id is the entity of a package that may define abstract states.
2286 -- If the states have visible refinement, remove the visibility of each
2287 -- constituent at the end of the package body declaration.
2289 procedure Resolve_Aspects;
2290 -- Utility to resolve the expressions of aspects at the end of a list of
2291 -- declarations, or before a declaration that freezes previous entities,
2292 -- such as in a subprogram body.
2294 -----------------
2295 -- Adjust_Decl --
2296 -----------------
2298 procedure Adjust_Decl is
2299 begin
2300 while Present (Prev (Decl))
2301 and then Nkind (Decl) = N_Implicit_Label_Declaration
2302 loop
2303 Prev (Decl);
2304 end loop;
2305 end Adjust_Decl;
2307 ----------------------------
2308 -- Build_Assertion_Bodies --
2309 ----------------------------
2311 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2312 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2313 -- Create the subprogram bodies which verify the run-time semantics
2314 -- of the pragmas listed below for type Typ. The pragmas are:
2316 -- Default_Initial_Condition
2317 -- Invariant
2318 -- Type_Invariant
2320 -------------------------------------
2321 -- Build_Assertion_Bodies_For_Type --
2322 -------------------------------------
2324 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2325 begin
2326 if Nkind (Context) = N_Package_Specification then
2328 -- Preanalyze and resolve the class-wide invariants of an
2329 -- interface at the end of whichever declarative part has the
2330 -- interface type. Note that an interface may be declared in
2331 -- any non-package declarative part, but reaching the end of
2332 -- such a declarative part will always freeze the type and
2333 -- generate the invariant procedure (see Freeze_Type).
2335 if Is_Interface (Typ) then
2337 -- Interfaces are treated as the partial view of a private
2338 -- type, in order to achieve uniformity with the general
2339 -- case. As a result, an interface receives only a "partial"
2340 -- invariant procedure, which is never called.
2342 if Has_Own_Invariants (Typ) then
2343 Build_Invariant_Procedure_Body
2344 (Typ => Typ,
2345 Partial_Invariant => True);
2346 end if;
2348 elsif Decls = Visible_Declarations (Context) then
2349 -- Preanalyze and resolve the invariants of a private type
2350 -- at the end of the visible declarations to catch potential
2351 -- errors. Inherited class-wide invariants are not included
2352 -- because they have already been resolved.
2354 if Ekind (Typ) in E_Limited_Private_Type
2355 | E_Private_Type
2356 | E_Record_Type_With_Private
2357 and then Has_Own_Invariants (Typ)
2358 then
2359 Build_Invariant_Procedure_Body
2360 (Typ => Typ,
2361 Partial_Invariant => True);
2362 end if;
2364 -- Preanalyze and resolve the Default_Initial_Condition
2365 -- assertion expression at the end of the declarations to
2366 -- catch any errors.
2368 if Ekind (Typ) in E_Limited_Private_Type
2369 | E_Private_Type
2370 | E_Record_Type_With_Private
2371 and then Has_Own_DIC (Typ)
2372 then
2373 Build_DIC_Procedure_Body
2374 (Typ => Typ,
2375 Partial_DIC => True);
2376 end if;
2378 elsif Decls = Private_Declarations (Context) then
2380 -- Preanalyze and resolve the invariants of a private type's
2381 -- full view at the end of the private declarations to catch
2382 -- potential errors.
2384 if (not Is_Private_Type (Typ)
2385 or else Present (Underlying_Full_View (Typ)))
2386 and then Has_Private_Declaration (Typ)
2387 and then Has_Invariants (Typ)
2388 then
2389 Build_Invariant_Procedure_Body (Typ);
2390 end if;
2392 if (not Is_Private_Type (Typ)
2393 or else Present (Underlying_Full_View (Typ)))
2394 and then Has_Private_Declaration (Typ)
2395 and then Has_DIC (Typ)
2396 then
2397 Build_DIC_Procedure_Body (Typ);
2398 end if;
2399 end if;
2400 end if;
2401 end Build_Assertion_Bodies_For_Type;
2403 -- Local variables
2405 Decl : Node_Id;
2406 Decl_Id : Entity_Id;
2408 -- Start of processing for Build_Assertion_Bodies
2410 begin
2411 Decl := First (Decls);
2412 while Present (Decl) loop
2413 if Is_Declaration (Decl) then
2414 Decl_Id := Defining_Entity (Decl);
2416 if Is_Type (Decl_Id) then
2417 Build_Assertion_Bodies_For_Type (Decl_Id);
2418 end if;
2419 end if;
2421 Next (Decl);
2422 end loop;
2423 end Build_Assertion_Bodies;
2425 ---------------------------
2426 -- Check_Entry_Contracts --
2427 ---------------------------
2429 procedure Check_Entry_Contracts is
2430 ASN : Node_Id;
2431 Ent : Entity_Id;
2432 Exp : Node_Id;
2434 begin
2435 Ent := First_Entity (Current_Scope);
2436 while Present (Ent) loop
2438 -- This only concerns entries with pre/postconditions
2440 if Ekind (Ent) = E_Entry
2441 and then Present (Contract (Ent))
2442 and then Present (Pre_Post_Conditions (Contract (Ent)))
2443 then
2444 ASN := Pre_Post_Conditions (Contract (Ent));
2445 Push_Scope (Ent);
2446 Install_Formals (Ent);
2448 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2449 -- is performed on a copy of the pragma expression, to prevent
2450 -- modifying the original expression.
2452 while Present (ASN) loop
2453 if Nkind (ASN) = N_Pragma then
2454 Exp :=
2455 New_Copy_Tree
2456 (Expression
2457 (First (Pragma_Argument_Associations (ASN))));
2458 Set_Parent (Exp, ASN);
2460 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2461 end if;
2463 ASN := Next_Pragma (ASN);
2464 end loop;
2466 End_Scope;
2467 end if;
2469 Next_Entity (Ent);
2470 end loop;
2471 end Check_Entry_Contracts;
2473 ----------------------------------
2474 -- Contains_Lib_Incomplete_Type --
2475 ----------------------------------
2477 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2478 Curr : Entity_Id;
2480 begin
2481 -- Avoid looking through scopes that do not meet the precondition of
2482 -- Pkg not being within a library unit spec.
2484 if not Is_Compilation_Unit (Pkg)
2485 and then not Is_Generic_Instance (Pkg)
2486 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2487 then
2488 -- Loop through all entities in the current scope to identify
2489 -- an entity that depends on a private type.
2491 Curr := First_Entity (Pkg);
2492 loop
2493 if Nkind (Curr) in N_Entity
2494 and then Depends_On_Private (Curr)
2495 then
2496 return True;
2497 end if;
2499 exit when Last_Entity (Current_Scope) = Curr;
2500 Next_Entity (Curr);
2501 end loop;
2502 end if;
2504 return False;
2505 end Contains_Lib_Incomplete_Type;
2507 --------------------------------------
2508 -- Handle_Late_Controlled_Primitive --
2509 --------------------------------------
2511 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2512 Body_Spec : constant Node_Id := Specification (Body_Decl);
2513 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2514 Loc : constant Source_Ptr := Sloc (Body_Id);
2515 Params : constant List_Id :=
2516 Parameter_Specifications (Body_Spec);
2517 Spec : Node_Id;
2518 Spec_Id : Entity_Id;
2519 Typ : Node_Id;
2521 begin
2522 -- Consider only procedure bodies whose name matches one of the three
2523 -- controlled primitives.
2525 if Nkind (Body_Spec) /= N_Procedure_Specification
2526 or else Chars (Body_Id) not in Name_Adjust
2527 | Name_Finalize
2528 | Name_Initialize
2529 then
2530 return;
2532 -- A controlled primitive must have exactly one formal which is not
2533 -- an anonymous access type.
2535 elsif List_Length (Params) /= 1 then
2536 return;
2537 end if;
2539 Typ := Parameter_Type (First (Params));
2541 if Nkind (Typ) = N_Access_Definition then
2542 return;
2543 end if;
2545 Find_Type (Typ);
2547 -- The type of the formal must be derived from [Limited_]Controlled
2549 if not Is_Controlled (Entity (Typ)) then
2550 return;
2551 end if;
2553 -- Check whether a specification exists for this body. We do not
2554 -- analyze the spec of the body in full, because it will be analyzed
2555 -- again when the body is properly analyzed, and we cannot create
2556 -- duplicate entries in the formals chain. We look for an explicit
2557 -- specification because the body may be an overriding operation and
2558 -- an inherited spec may be present.
2560 Spec_Id := Current_Entity (Body_Id);
2562 while Present (Spec_Id) loop
2563 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2564 and then Scope (Spec_Id) = Current_Scope
2565 and then Present (First_Formal (Spec_Id))
2566 and then No (Next_Formal (First_Formal (Spec_Id)))
2567 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2568 and then Comes_From_Source (Spec_Id)
2569 then
2570 return;
2571 end if;
2573 Spec_Id := Homonym (Spec_Id);
2574 end loop;
2576 -- At this point the body is known to be a late controlled primitive.
2577 -- Generate a matching spec and insert it before the body. Note the
2578 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2579 -- tree in this case.
2581 Spec := Copy_Separate_Tree (Body_Spec);
2583 -- Ensure that the subprogram declaration does not inherit the null
2584 -- indicator from the body as we now have a proper spec/body pair.
2586 Set_Null_Present (Spec, False);
2588 -- Ensure that the freeze node is inserted after the declaration of
2589 -- the primitive since its expansion will freeze the primitive.
2591 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2593 Insert_Before_And_Analyze (Body_Decl, Decl);
2594 end Handle_Late_Controlled_Primitive;
2596 ----------------------------------------
2597 -- Remove_Partial_Visible_Refinements --
2598 ----------------------------------------
2600 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2601 State_Elmt : Elmt_Id;
2602 begin
2603 if Present (Abstract_States (Spec_Id)) then
2604 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2605 while Present (State_Elmt) loop
2606 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2607 Next_Elmt (State_Elmt);
2608 end loop;
2609 end if;
2611 -- For a child unit, also hide the partial state refinement from
2612 -- ancestor packages.
2614 if Is_Child_Unit (Spec_Id) then
2615 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2616 end if;
2617 end Remove_Partial_Visible_Refinements;
2619 --------------------------------
2620 -- Remove_Visible_Refinements --
2621 --------------------------------
2623 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2624 State_Elmt : Elmt_Id;
2625 begin
2626 if Present (Abstract_States (Spec_Id)) then
2627 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2628 while Present (State_Elmt) loop
2629 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2630 Next_Elmt (State_Elmt);
2631 end loop;
2632 end if;
2633 end Remove_Visible_Refinements;
2635 ---------------------
2636 -- Resolve_Aspects --
2637 ---------------------
2639 procedure Resolve_Aspects is
2640 E : Entity_Id;
2642 begin
2643 E := First_Entity (Current_Scope);
2644 while Present (E) loop
2645 Resolve_Aspect_Expressions (E);
2647 -- Now that the aspect expressions have been resolved, if this is
2648 -- at the end of the visible declarations, we can set the flag
2649 -- Known_To_Have_Preelab_Init properly on types declared in the
2650 -- visible part, which is needed for checking whether full types
2651 -- in the private part satisfy the Preelaborable_Initialization
2652 -- aspect of the partial view. We can't wait for the creation of
2653 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2654 -- freeze point may occur after the end of the package declaration
2655 -- (in the case of nested packages).
2657 if Is_Type (E)
2658 and then L = Visible_Declarations (Parent (L))
2659 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2660 then
2661 declare
2662 ASN : constant Node_Id :=
2663 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2664 Expr : constant Node_Id := Expression (ASN);
2665 begin
2666 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2667 -- expression, or if the expression is True (or was folded
2668 -- to True), or if the expression is a conjunction of one or
2669 -- more Preelaborable_Initialization attributes applied to
2670 -- formal types and wasn't folded to False. (Note that
2671 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2672 -- Original_Node if needed, hence test for Standard_False.)
2674 if No (Expr)
2675 or else (Is_Entity_Name (Expr)
2676 and then Entity (Expr) = Standard_True)
2677 or else
2678 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2679 and then
2680 not (Is_Entity_Name (Expr)
2681 and then Entity (Expr) = Standard_False))
2682 then
2683 Set_Known_To_Have_Preelab_Init (E);
2684 end if;
2685 end;
2686 end if;
2688 Next_Entity (E);
2689 end loop;
2690 end Resolve_Aspects;
2692 -- Local variables
2694 Context : Node_Id := Empty;
2695 Ctrl_Typ : Entity_Id := Empty;
2696 Freeze_From : Entity_Id := Empty;
2697 Next_Decl : Node_Id;
2699 -- Start of processing for Analyze_Declarations
2701 begin
2702 Decl := First (L);
2703 while Present (Decl) loop
2705 -- Complete analysis of declaration
2707 Analyze (Decl);
2708 Next_Decl := Next (Decl);
2710 if No (Freeze_From) then
2711 Freeze_From := First_Entity (Current_Scope);
2712 end if;
2714 -- Remember if the declaration we just processed is the full type
2715 -- declaration of a controlled type (to handle late overriding of
2716 -- initialize, adjust or finalize).
2718 if Nkind (Decl) = N_Full_Type_Declaration
2719 and then Is_Controlled (Defining_Identifier (Decl))
2720 then
2721 Ctrl_Typ := Defining_Identifier (Decl);
2722 end if;
2724 -- At the end of a declarative part, freeze remaining entities
2725 -- declared in it. The end of the visible declarations of package
2726 -- specification is not the end of a declarative part if private
2727 -- declarations are present. The end of a package declaration is a
2728 -- freezing point only if it a library package. A task definition or
2729 -- protected type definition is not a freeze point either. Finally,
2730 -- we do not freeze entities in generic scopes, because there is no
2731 -- code generated for them and freeze nodes will be generated for
2732 -- the instance.
2734 -- The end of a package instantiation is not a freeze point, but
2735 -- for now we make it one, because the generic body is inserted
2736 -- (currently) immediately after. Generic instantiations will not
2737 -- be a freeze point once delayed freezing of bodies is implemented.
2738 -- (This is needed in any case for early instantiations ???).
2740 if No (Next_Decl) then
2741 if Nkind (Parent (L)) = N_Component_List then
2742 null;
2744 elsif Nkind (Parent (L)) in
2745 N_Protected_Definition | N_Task_Definition
2746 then
2747 Check_Entry_Contracts;
2749 elsif Nkind (Parent (L)) /= N_Package_Specification then
2750 if Nkind (Parent (L)) = N_Package_Body then
2751 Freeze_From := First_Entity (Current_Scope);
2752 end if;
2754 -- There may have been several freezing points previously,
2755 -- for example object declarations or subprogram bodies, but
2756 -- at the end of a declarative part we check freezing from
2757 -- the beginning, even though entities may already be frozen,
2758 -- in order to perform visibility checks on delayed aspects.
2760 Adjust_Decl;
2762 -- If the current scope is a generic subprogram body. Skip the
2763 -- generic formal parameters that are not frozen here.
2765 if Is_Subprogram (Current_Scope)
2766 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2767 N_Generic_Subprogram_Declaration
2768 and then Present (First_Entity (Current_Scope))
2769 then
2770 while Is_Generic_Formal (Freeze_From) loop
2771 Next_Entity (Freeze_From);
2772 end loop;
2774 Freeze_All (Freeze_From, Decl);
2775 Freeze_From := Last_Entity (Current_Scope);
2777 else
2778 -- For declarations in a subprogram body there is no issue
2779 -- with name resolution in aspect specifications.
2781 Freeze_All (First_Entity (Current_Scope), Decl);
2782 Freeze_From := Last_Entity (Current_Scope);
2783 end if;
2785 -- Current scope is a package specification
2787 elsif Scope (Current_Scope) /= Standard_Standard
2788 and then not Is_Child_Unit (Current_Scope)
2789 and then No (Generic_Parent (Parent (L)))
2790 then
2791 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2792 -- resolved at the end of the immediately enclosing declaration
2793 -- list (AI05-0183-1).
2795 Resolve_Aspects;
2797 elsif L /= Visible_Declarations (Parent (L))
2798 or else Is_Empty_List (Private_Declarations (Parent (L)))
2799 then
2800 Adjust_Decl;
2802 -- End of a package declaration
2804 -- This is a freeze point because it is the end of a
2805 -- compilation unit.
2807 Freeze_All (First_Entity (Current_Scope), Decl);
2808 Freeze_From := Last_Entity (Current_Scope);
2810 -- At the end of the visible declarations the expressions in
2811 -- aspects of all entities declared so far must be resolved.
2812 -- The entities themselves might be frozen later, and the
2813 -- generated pragmas and attribute definition clauses analyzed
2814 -- in full at that point, but name resolution must take place
2815 -- now.
2816 -- In addition to being the proper semantics, this is mandatory
2817 -- within generic units, because global name capture requires
2818 -- those expressions to be analyzed, given that the generated
2819 -- pragmas do not appear in the original generic tree.
2821 elsif Serious_Errors_Detected = 0 then
2822 Resolve_Aspects;
2823 end if;
2825 -- If next node is a body then freeze all types before the body.
2826 -- An exception occurs for some expander-generated bodies. If these
2827 -- are generated at places where in general language rules would not
2828 -- allow a freeze point, then we assume that the expander has
2829 -- explicitly checked that all required types are properly frozen,
2830 -- and we do not cause general freezing here. This special circuit
2831 -- is used when the encountered body is marked as having already
2832 -- been analyzed.
2834 -- In all other cases (bodies that come from source, and expander
2835 -- generated bodies that have not been analyzed yet), freeze all
2836 -- types now. Note that in the latter case, the expander must take
2837 -- care to attach the bodies at a proper place in the tree so as to
2838 -- not cause unwanted freezing at that point.
2840 -- It is also necessary to check for a case where both an expression
2841 -- function is used and the current scope depends on an incomplete
2842 -- private type from a library unit, otherwise premature freezing of
2843 -- the private type will occur.
2845 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2846 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2847 or else not Was_Expression_Function (Next_Decl))
2848 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2849 and then not Contains_Lib_Incomplete_Type
2850 (Current_Scope)))
2851 then
2852 -- When a controlled type is frozen, the expander generates stream
2853 -- and controlled-type support routines. If the freeze is caused
2854 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2855 -- expander will end up using the wrong version of these routines,
2856 -- as the body has not been processed yet. To remedy this, detect
2857 -- a late controlled primitive and create a proper spec for it.
2858 -- This ensures that the primitive will override its inherited
2859 -- counterpart before the freeze takes place.
2861 -- If the declaration we just processed is a body, do not attempt
2862 -- to examine Next_Decl as the late primitive idiom can only apply
2863 -- to the first encountered body.
2865 -- ??? A cleaner approach may be possible and/or this solution
2866 -- could be extended to general-purpose late primitives.
2868 if Present (Ctrl_Typ) then
2870 -- No need to continue searching for late body overriding if
2871 -- the controlled type is already frozen.
2873 if Is_Frozen (Ctrl_Typ) then
2874 Ctrl_Typ := Empty;
2876 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2877 Handle_Late_Controlled_Primitive (Next_Decl);
2878 end if;
2879 end if;
2881 Adjust_Decl;
2883 -- The generated body of an expression function does not freeze,
2884 -- unless it is a completion, in which case only the expression
2885 -- itself freezes. This is handled when the body itself is
2886 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2888 Freeze_All (Freeze_From, Decl);
2889 Freeze_From := Last_Entity (Current_Scope);
2890 end if;
2892 Decl := Next_Decl;
2893 end loop;
2895 -- Post-freezing actions
2897 if Present (L) then
2898 Context := Parent (L);
2900 -- Certain contract annotations have forward visibility semantics and
2901 -- must be analyzed after all declarative items have been processed.
2902 -- This timing ensures that entities referenced by such contracts are
2903 -- visible.
2905 -- Analyze the contract of an immediately enclosing package spec or
2906 -- body first because other contracts may depend on its information.
2908 if Nkind (Context) = N_Package_Body then
2909 Analyze_Package_Body_Contract (Defining_Entity (Context));
2911 elsif Nkind (Context) = N_Package_Specification then
2912 Analyze_Package_Contract (Defining_Entity (Context));
2913 end if;
2915 -- Analyze the contracts of various constructs in the declarative
2916 -- list.
2918 Analyze_Contracts (L);
2920 if Nkind (Context) = N_Package_Body then
2922 -- Ensure that all abstract states and objects declared in the
2923 -- state space of a package body are utilized as constituents.
2925 Check_Unused_Body_States (Defining_Entity (Context));
2927 -- State refinements are visible up to the end of the package body
2928 -- declarations. Hide the state refinements from visibility to
2929 -- restore the original state conditions.
2931 Remove_Visible_Refinements (Corresponding_Spec (Context));
2932 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2934 elsif Nkind (Context) = N_Package_Specification then
2936 -- Partial state refinements are visible up to the end of the
2937 -- package spec declarations. Hide the partial state refinements
2938 -- from visibility to restore the original state conditions.
2940 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2941 end if;
2943 -- Verify that all abstract states found in any package declared in
2944 -- the input declarative list have proper refinements. The check is
2945 -- performed only when the context denotes a block, entry, package,
2946 -- protected, subprogram, or task body (SPARK RM 7.1.4(4) and SPARK
2947 -- RM 7.2.2(3)).
2949 Check_State_Refinements (Context);
2951 -- Create the subprogram bodies which verify the run-time semantics
2952 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2953 -- types within the current declarative list. This ensures that all
2954 -- assertion expressions are preanalyzed and resolved at the end of
2955 -- the declarative part. Note that the resolution happens even when
2956 -- freezing does not take place.
2958 Build_Assertion_Bodies (L, Context);
2959 end if;
2960 end Analyze_Declarations;
2962 -----------------------------------
2963 -- Analyze_Full_Type_Declaration --
2964 -----------------------------------
2966 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2967 Def : constant Node_Id := Type_Definition (N);
2968 Def_Id : constant Entity_Id := Defining_Identifier (N);
2969 T : Entity_Id;
2970 Prev : Entity_Id;
2972 Is_Remote : constant Boolean :=
2973 (Is_Remote_Types (Current_Scope)
2974 or else Is_Remote_Call_Interface (Current_Scope))
2975 and then not (In_Private_Part (Current_Scope)
2976 or else In_Package_Body (Current_Scope));
2978 procedure Check_Nonoverridable_Aspects;
2979 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2980 -- be overridden, and can only be confirmed on derivation.
2982 procedure Check_Ops_From_Incomplete_Type;
2983 -- If there is a tagged incomplete partial view of the type, traverse
2984 -- the primitives of the incomplete view and change the type of any
2985 -- controlling formals and result to indicate the full view. The
2986 -- primitives will be added to the full type's primitive operations
2987 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2988 -- is called from Process_Incomplete_Dependents).
2990 ----------------------------------
2991 -- Check_Nonoverridable_Aspects --
2992 ----------------------------------
2994 procedure Check_Nonoverridable_Aspects is
2995 function Get_Aspect_Spec
2996 (Specs : List_Id;
2997 Aspect_Name : Name_Id) return Node_Id;
2998 -- Check whether a list of aspect specifications includes an entry
2999 -- for a specific aspect. The list is either that of a partial or
3000 -- a full view.
3002 ---------------------
3003 -- Get_Aspect_Spec --
3004 ---------------------
3006 function Get_Aspect_Spec
3007 (Specs : List_Id;
3008 Aspect_Name : Name_Id) return Node_Id
3010 Spec : Node_Id;
3012 begin
3013 Spec := First (Specs);
3014 while Present (Spec) loop
3015 if Chars (Identifier (Spec)) = Aspect_Name then
3016 return Spec;
3017 end if;
3018 Next (Spec);
3019 end loop;
3021 return Empty;
3022 end Get_Aspect_Spec;
3024 -- Local variables
3026 Prev_Aspects : constant List_Id :=
3027 Aspect_Specifications (Parent (Def_Id));
3028 Par_Type : Entity_Id;
3029 Prev_Aspect : Node_Id;
3031 -- Start of processing for Check_Nonoverridable_Aspects
3033 begin
3034 -- Get parent type of derived type. Note that Prev is the entity in
3035 -- the partial declaration, but its contents are now those of full
3036 -- view, while Def_Id reflects the partial view.
3038 if Is_Private_Type (Def_Id) then
3039 Par_Type := Etype (Full_View (Def_Id));
3040 else
3041 Par_Type := Etype (Def_Id);
3042 end if;
3044 -- If there is an inherited Implicit_Dereference, verify that it is
3045 -- made explicit in the partial view.
3047 if Has_Discriminants (Base_Type (Par_Type))
3048 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3049 and then Present (Discriminant_Specifications (Parent (Prev)))
3050 and then Present (Get_Reference_Discriminant (Par_Type))
3051 then
3052 Prev_Aspect :=
3053 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3055 if No (Prev_Aspect)
3056 and then Present
3057 (Discriminant_Specifications
3058 (Original_Node (Parent (Prev))))
3059 then
3060 Error_Msg_N
3061 ("type does not inherit implicit dereference", Prev);
3063 else
3064 -- If one of the views has the aspect specified, verify that it
3065 -- is consistent with that of the parent.
3067 declare
3068 Cur_Discr : constant Entity_Id :=
3069 Get_Reference_Discriminant (Prev);
3070 Par_Discr : constant Entity_Id :=
3071 Get_Reference_Discriminant (Par_Type);
3073 begin
3074 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3075 Error_Msg_N
3076 ("aspect inconsistent with that of parent", N);
3077 end if;
3079 -- Check that specification in partial view matches the
3080 -- inherited aspect. Compare names directly because aspect
3081 -- expression may not be analyzed.
3083 if Present (Prev_Aspect)
3084 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3085 and then Chars (Expression (Prev_Aspect)) /=
3086 Chars (Cur_Discr)
3087 then
3088 Error_Msg_N
3089 ("aspect inconsistent with that of parent", N);
3090 end if;
3091 end;
3092 end if;
3093 end if;
3095 -- What about other nonoverridable aspects???
3096 end Check_Nonoverridable_Aspects;
3098 ------------------------------------
3099 -- Check_Ops_From_Incomplete_Type --
3100 ------------------------------------
3102 procedure Check_Ops_From_Incomplete_Type is
3103 Elmt : Elmt_Id;
3104 Formal : Entity_Id;
3105 Op : Entity_Id;
3107 begin
3108 if Prev /= T
3109 and then Ekind (Prev) = E_Incomplete_Type
3110 and then Is_Tagged_Type (Prev)
3111 and then Is_Tagged_Type (T)
3112 and then Present (Primitive_Operations (Prev))
3113 then
3114 Elmt := First_Elmt (Primitive_Operations (Prev));
3115 while Present (Elmt) loop
3116 Op := Node (Elmt);
3118 Formal := First_Formal (Op);
3119 while Present (Formal) loop
3120 if Etype (Formal) = Prev then
3121 Set_Etype (Formal, T);
3122 end if;
3124 Next_Formal (Formal);
3125 end loop;
3127 if Etype (Op) = Prev then
3128 Set_Etype (Op, T);
3129 end if;
3131 Next_Elmt (Elmt);
3132 end loop;
3133 end if;
3134 end Check_Ops_From_Incomplete_Type;
3136 -- Start of processing for Analyze_Full_Type_Declaration
3138 begin
3139 Prev := Find_Type_Name (N);
3141 -- The full view, if present, now points to the current type. If there
3142 -- is an incomplete partial view, set a link to it, to simplify the
3143 -- retrieval of primitive operations of the type.
3145 -- Ada 2005 (AI-50217): If the type was previously decorated when
3146 -- imported through a LIMITED WITH clause, it appears as incomplete
3147 -- but has no full view.
3149 if Ekind (Prev) = E_Incomplete_Type
3150 and then Present (Full_View (Prev))
3151 then
3152 T := Full_View (Prev);
3153 Set_Incomplete_View (N, Prev);
3154 else
3155 T := Prev;
3156 end if;
3158 Set_Is_Pure (T, Is_Pure (Current_Scope));
3160 -- We set the flag Is_First_Subtype here. It is needed to set the
3161 -- corresponding flag for the Implicit class-wide-type created
3162 -- during tagged types processing.
3164 Set_Is_First_Subtype (T, True);
3166 -- Only composite types other than array types are allowed to have
3167 -- discriminants.
3169 case Nkind (Def) is
3171 -- For derived types, the rule will be checked once we've figured
3172 -- out the parent type.
3174 when N_Derived_Type_Definition =>
3175 null;
3177 -- For record types, discriminants are allowed.
3179 when N_Record_Definition =>
3180 null;
3182 when others =>
3183 if Present (Discriminant_Specifications (N)) then
3184 Error_Msg_N
3185 ("elementary or array type cannot have discriminants",
3186 Defining_Identifier
3187 (First (Discriminant_Specifications (N))));
3188 end if;
3189 end case;
3191 -- Elaborate the type definition according to kind, and generate
3192 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3193 -- already done (this happens during the reanalysis that follows a call
3194 -- to the high level optimizer).
3196 if not Analyzed (T) then
3197 Set_Analyzed (T);
3199 -- Set the SPARK mode from the current context
3201 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3202 Set_SPARK_Pragma_Inherited (T);
3204 case Nkind (Def) is
3205 when N_Access_To_Subprogram_Definition =>
3206 Access_Subprogram_Declaration (T, Def);
3208 -- If this is a remote access to subprogram, we must create the
3209 -- equivalent fat pointer type, and related subprograms.
3211 if Is_Remote then
3212 Process_Remote_AST_Declaration (N);
3213 end if;
3215 -- Validate categorization rule against access type declaration
3216 -- usually a violation in Pure unit, Shared_Passive unit.
3218 Validate_Access_Type_Declaration (T, N);
3220 -- If the type has contracts, we create the corresponding
3221 -- wrapper at once, before analyzing the aspect specifications,
3222 -- so that pre/postconditions can be handled directly on the
3223 -- generated wrapper.
3225 if Ada_Version >= Ada_2022
3226 and then Present (Aspect_Specifications (N))
3227 then
3228 Build_Access_Subprogram_Wrapper (N);
3229 end if;
3231 when N_Access_To_Object_Definition =>
3232 Access_Type_Declaration (T, Def);
3234 -- Validate categorization rule against access type declaration
3235 -- usually a violation in Pure unit, Shared_Passive unit.
3237 Validate_Access_Type_Declaration (T, N);
3239 -- If we are in a Remote_Call_Interface package and define a
3240 -- RACW, then calling stubs and specific stream attributes
3241 -- must be added.
3243 if Is_Remote
3244 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3245 then
3246 Add_RACW_Features (Def_Id);
3247 end if;
3249 when N_Array_Type_Definition =>
3250 Array_Type_Declaration (T, Def);
3252 when N_Derived_Type_Definition =>
3253 Derived_Type_Declaration (T, N, T /= Def_Id);
3255 -- Inherit predicates from parent, and protect against illegal
3256 -- derivations.
3258 if Is_Type (T) and then Has_Predicates (T) then
3259 Set_Has_Predicates (Def_Id);
3260 end if;
3262 -- Save the scenario for examination by the ABE Processing
3263 -- phase.
3265 Record_Elaboration_Scenario (N);
3267 when N_Enumeration_Type_Definition =>
3268 Enumeration_Type_Declaration (T, Def);
3270 when N_Floating_Point_Definition =>
3271 Floating_Point_Type_Declaration (T, Def);
3273 when N_Decimal_Fixed_Point_Definition =>
3274 Decimal_Fixed_Point_Type_Declaration (T, Def);
3276 when N_Ordinary_Fixed_Point_Definition =>
3277 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3279 when N_Signed_Integer_Type_Definition =>
3280 Signed_Integer_Type_Declaration (T, Def);
3282 when N_Modular_Type_Definition =>
3283 Modular_Type_Declaration (T, Def);
3285 when N_Record_Definition =>
3286 Record_Type_Declaration (T, N, Prev);
3288 -- If declaration has a parse error, nothing to elaborate.
3290 when N_Error =>
3291 null;
3293 when others =>
3294 raise Program_Error;
3295 end case;
3296 end if;
3298 if Etype (T) = Any_Type then
3299 return;
3300 end if;
3302 -- Set the primitives list of the full type and its base type when
3303 -- needed. T may be E_Void in cases of earlier errors, and in that
3304 -- case we bypass this.
3306 if Ekind (T) /= E_Void then
3307 if not Present (Direct_Primitive_Operations (T)) then
3308 if Etype (T) = T then
3309 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3311 -- If Etype of T is the base type (as opposed to a parent type)
3312 -- and already has an associated list of primitive operations,
3313 -- then set T's primitive list to the base type's list. Otherwise,
3314 -- create a new empty primitives list and share the list between
3315 -- T and its base type. The lists need to be shared in common.
3317 elsif Etype (T) = Base_Type (T) then
3319 if not Present (Direct_Primitive_Operations (Base_Type (T)))
3320 then
3321 Set_Direct_Primitive_Operations
3322 (Base_Type (T), New_Elmt_List);
3323 end if;
3325 Set_Direct_Primitive_Operations
3326 (T, Direct_Primitive_Operations (Base_Type (T)));
3328 -- Case where the Etype is a parent type, so we need a new
3329 -- primitives list for T.
3331 else
3332 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3333 end if;
3335 -- If T already has a Direct_Primitive_Operations list but its
3336 -- base type doesn't then set the base type's list to T's list.
3338 elsif not Present (Direct_Primitive_Operations (Base_Type (T))) then
3339 Set_Direct_Primitive_Operations
3340 (Base_Type (T), Direct_Primitive_Operations (T));
3341 end if;
3342 end if;
3344 -- Some common processing for all types
3346 Set_Depends_On_Private (T, Has_Private_Component (T));
3347 Check_Ops_From_Incomplete_Type;
3349 -- Both the declared entity, and its anonymous base type if one was
3350 -- created, need freeze nodes allocated.
3352 declare
3353 B : constant Entity_Id := Base_Type (T);
3355 begin
3356 -- In the case where the base type differs from the first subtype, we
3357 -- pre-allocate a freeze node, and set the proper link to the first
3358 -- subtype. Freeze_Entity will use this preallocated freeze node when
3359 -- it freezes the entity.
3361 -- This does not apply if the base type is a generic type, whose
3362 -- declaration is independent of the current derived definition.
3364 if B /= T and then not Is_Generic_Type (B) then
3365 Ensure_Freeze_Node (B);
3366 Set_First_Subtype_Link (Freeze_Node (B), T);
3367 end if;
3369 -- A type that is imported through a limited_with clause cannot
3370 -- generate any code, and thus need not be frozen. However, an access
3371 -- type with an imported designated type needs a finalization list,
3372 -- which may be referenced in some other package that has non-limited
3373 -- visibility on the designated type. Thus we must create the
3374 -- finalization list at the point the access type is frozen, to
3375 -- prevent unsatisfied references at link time.
3377 if not From_Limited_With (T) or else Is_Access_Type (T) then
3378 Set_Has_Delayed_Freeze (T);
3379 end if;
3380 end;
3382 -- Case where T is the full declaration of some private type which has
3383 -- been swapped in Defining_Identifier (N).
3385 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3386 Process_Full_View (N, T, Def_Id);
3388 -- Record the reference. The form of this is a little strange, since
3389 -- the full declaration has been swapped in. So the first parameter
3390 -- here represents the entity to which a reference is made which is
3391 -- the "real" entity, i.e. the one swapped in, and the second
3392 -- parameter provides the reference location.
3394 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3395 -- since we don't want a complaint about the full type being an
3396 -- unwanted reference to the private type
3398 declare
3399 B : constant Boolean := Has_Pragma_Unreferenced (T);
3400 begin
3401 Set_Has_Pragma_Unreferenced (T, False);
3402 Generate_Reference (T, T, 'c');
3403 Set_Has_Pragma_Unreferenced (T, B);
3404 end;
3406 Set_Completion_Referenced (Def_Id);
3408 -- For completion of incomplete type, process incomplete dependents
3409 -- and always mark the full type as referenced (it is the incomplete
3410 -- type that we get for any real reference).
3412 elsif Ekind (Prev) = E_Incomplete_Type then
3413 Process_Incomplete_Dependents (N, T, Prev);
3414 Generate_Reference (Prev, Def_Id, 'c');
3415 Set_Completion_Referenced (Def_Id);
3417 -- If not private type or incomplete type completion, this is a real
3418 -- definition of a new entity, so record it.
3420 else
3421 Generate_Definition (Def_Id);
3422 end if;
3424 -- Propagate any pending access types whose finalization masters need to
3425 -- be fully initialized from the partial to the full view. Guard against
3426 -- an illegal full view that remains unanalyzed.
3428 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3429 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3430 end if;
3432 if Chars (Scope (Def_Id)) = Name_System
3433 and then Chars (Def_Id) = Name_Address
3434 and then In_Predefined_Unit (N)
3435 then
3436 Set_Is_Descendant_Of_Address (Def_Id);
3437 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3438 Set_Is_Descendant_Of_Address (Prev);
3439 end if;
3441 Set_Optimize_Alignment_Flags (Def_Id);
3442 Check_Eliminated (Def_Id);
3444 -- If the declaration is a completion and aspects are present, apply
3445 -- them to the entity for the type which is currently the partial
3446 -- view, but which is the one that will be frozen.
3448 if Has_Aspects (N) then
3450 -- In most cases the partial view is a private type, and both views
3451 -- appear in different declarative parts. In the unusual case where
3452 -- the partial view is incomplete, perform the analysis on the
3453 -- full view, to prevent freezing anomalies with the corresponding
3454 -- class-wide type, which otherwise might be frozen before the
3455 -- dispatch table is built.
3457 if Prev /= Def_Id
3458 and then Ekind (Prev) /= E_Incomplete_Type
3459 then
3460 Analyze_Aspect_Specifications (N, Prev);
3462 -- Normal case
3464 else
3465 Analyze_Aspect_Specifications (N, Def_Id);
3466 end if;
3467 end if;
3469 if Is_Derived_Type (Prev)
3470 and then Def_Id /= Prev
3471 then
3472 Check_Nonoverridable_Aspects;
3473 end if;
3475 -- Check for tagged type declaration at library level
3477 if Is_Tagged_Type (T)
3478 and then not Is_Library_Level_Entity (T)
3479 then
3480 Check_Restriction (No_Local_Tagged_Types, T);
3481 end if;
3482 end Analyze_Full_Type_Declaration;
3484 ----------------------------------
3485 -- Analyze_Incomplete_Type_Decl --
3486 ----------------------------------
3488 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3489 F : constant Boolean := Is_Pure (Current_Scope);
3490 T : Entity_Id;
3492 begin
3493 Generate_Definition (Defining_Identifier (N));
3495 -- Process an incomplete declaration. The identifier must not have been
3496 -- declared already in the scope. However, an incomplete declaration may
3497 -- appear in the private part of a package, for a private type that has
3498 -- already been declared.
3500 -- In this case, the discriminants (if any) must match
3502 T := Find_Type_Name (N);
3504 Mutate_Ekind (T, E_Incomplete_Type);
3505 Set_Etype (T, T);
3506 Set_Is_First_Subtype (T);
3507 Reinit_Size_Align (T);
3509 -- Set the SPARK mode from the current context
3511 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3512 Set_SPARK_Pragma_Inherited (T);
3514 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3515 -- incomplete types.
3517 if Tagged_Present (N) then
3518 Set_Is_Tagged_Type (T, True);
3519 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3520 Make_Class_Wide_Type (T);
3521 end if;
3523 -- Initialize the list of primitive operations to an empty list,
3524 -- to cover tagged types as well as untagged types. For untagged
3525 -- types this is used either to analyze the call as legal when
3526 -- Core_Extensions_Allowed is True, or to issue a better error message
3527 -- otherwise.
3529 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3531 Set_Stored_Constraint (T, No_Elist);
3533 if Present (Discriminant_Specifications (N)) then
3534 Push_Scope (T);
3535 Process_Discriminants (N);
3536 End_Scope;
3537 end if;
3539 -- If the type has discriminants, nontrivial subtypes may be declared
3540 -- before the full view of the type. The full views of those subtypes
3541 -- will be built after the full view of the type.
3543 Set_Private_Dependents (T, New_Elmt_List);
3544 Set_Is_Pure (T, F);
3545 end Analyze_Incomplete_Type_Decl;
3547 -----------------------------------
3548 -- Analyze_Interface_Declaration --
3549 -----------------------------------
3551 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3552 CW : constant Entity_Id := Class_Wide_Type (T);
3554 begin
3555 Set_Is_Tagged_Type (T);
3556 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3558 Set_Is_Limited_Record (T, Limited_Present (Def)
3559 or else Task_Present (Def)
3560 or else Protected_Present (Def)
3561 or else Synchronized_Present (Def));
3563 -- Type is abstract if full declaration carries keyword, or if previous
3564 -- partial view did.
3566 Set_Is_Abstract_Type (T);
3567 Set_Is_Interface (T);
3569 -- Type is a limited interface if it includes the keyword limited, task,
3570 -- protected, or synchronized.
3572 Set_Is_Limited_Interface
3573 (T, Limited_Present (Def)
3574 or else Protected_Present (Def)
3575 or else Synchronized_Present (Def)
3576 or else Task_Present (Def));
3578 Set_Interfaces (T, New_Elmt_List);
3579 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3581 -- Complete the decoration of the class-wide entity if it was already
3582 -- built (i.e. during the creation of the limited view)
3584 if Present (CW) then
3585 Set_Is_Interface (CW);
3586 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3587 end if;
3589 -- Check runtime support for synchronized interfaces
3591 if Is_Concurrent_Interface (T)
3592 and then not RTE_Available (RE_Select_Specific_Data)
3593 then
3594 Error_Msg_CRT ("synchronized interfaces", T);
3595 end if;
3596 end Analyze_Interface_Declaration;
3598 -----------------------------
3599 -- Analyze_Itype_Reference --
3600 -----------------------------
3602 -- Nothing to do. This node is placed in the tree only for the benefit of
3603 -- back end processing, and has no effect on the semantic processing.
3605 procedure Analyze_Itype_Reference (N : Node_Id) is
3606 begin
3607 pragma Assert (Is_Itype (Itype (N)));
3608 null;
3609 end Analyze_Itype_Reference;
3611 --------------------------------
3612 -- Analyze_Number_Declaration --
3613 --------------------------------
3615 procedure Analyze_Number_Declaration (N : Node_Id) is
3616 E : constant Node_Id := Expression (N);
3617 Id : constant Entity_Id := Defining_Identifier (N);
3618 Index : Interp_Index;
3619 It : Interp;
3620 T : Entity_Id;
3622 begin
3623 Generate_Definition (Id);
3624 Enter_Name (Id);
3626 -- This is an optimization of a common case of an integer literal
3628 if Nkind (E) = N_Integer_Literal then
3629 Set_Is_Static_Expression (E, True);
3630 Set_Etype (E, Universal_Integer);
3632 Set_Etype (Id, Universal_Integer);
3633 Mutate_Ekind (Id, E_Named_Integer);
3634 Set_Is_Frozen (Id, True);
3636 Set_Debug_Info_Needed (Id);
3637 return;
3638 end if;
3640 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3642 -- Process expression, replacing error by integer zero, to avoid
3643 -- cascaded errors or aborts further along in the processing
3645 -- Replace Error by integer zero, which seems least likely to cause
3646 -- cascaded errors.
3648 if E = Error then
3649 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3650 Set_Error_Posted (E);
3651 end if;
3653 Analyze (E);
3655 -- Verify that the expression is static and numeric. If
3656 -- the expression is overloaded, we apply the preference
3657 -- rule that favors root numeric types.
3659 if not Is_Overloaded (E) then
3660 T := Etype (E);
3661 if Has_Dynamic_Predicate_Aspect (T) then
3662 Error_Msg_N
3663 ("subtype has dynamic predicate, "
3664 & "not allowed in number declaration", N);
3665 end if;
3667 else
3668 T := Any_Type;
3670 Get_First_Interp (E, Index, It);
3671 while Present (It.Typ) loop
3672 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3673 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3674 then
3675 if T = Any_Type then
3676 T := It.Typ;
3678 elsif Is_Universal_Numeric_Type (It.Typ) then
3679 -- Choose universal interpretation over any other
3681 T := It.Typ;
3682 exit;
3683 end if;
3684 end if;
3686 Get_Next_Interp (Index, It);
3687 end loop;
3688 end if;
3690 if Is_Integer_Type (T) then
3691 Resolve (E, T);
3692 Set_Etype (Id, Universal_Integer);
3693 Mutate_Ekind (Id, E_Named_Integer);
3695 elsif Is_Real_Type (T) then
3697 -- Because the real value is converted to universal_real, this is a
3698 -- legal context for a universal fixed expression.
3700 if T = Universal_Fixed then
3701 declare
3702 Loc : constant Source_Ptr := Sloc (N);
3703 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3704 Subtype_Mark =>
3705 New_Occurrence_Of (Universal_Real, Loc),
3706 Expression => Relocate_Node (E));
3708 begin
3709 Rewrite (E, Conv);
3710 Analyze (E);
3711 end;
3713 elsif T = Any_Fixed then
3714 Error_Msg_N ("illegal context for mixed mode operation", E);
3716 -- Expression is of the form : universal_fixed * integer. Try to
3717 -- resolve as universal_real.
3719 T := Universal_Real;
3720 Set_Etype (E, T);
3721 end if;
3723 Resolve (E, T);
3724 Set_Etype (Id, Universal_Real);
3725 Mutate_Ekind (Id, E_Named_Real);
3727 else
3728 Wrong_Type (E, Any_Numeric);
3729 Resolve (E, T);
3731 Set_Etype (Id, T);
3732 Mutate_Ekind (Id, E_Constant);
3733 Set_Never_Set_In_Source (Id, True);
3734 Set_Is_True_Constant (Id, True);
3735 return;
3736 end if;
3738 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3739 Set_Etype (E, Etype (Id));
3740 end if;
3742 if not Is_OK_Static_Expression (E) then
3743 Flag_Non_Static_Expr
3744 ("non-static expression used in number declaration!", E);
3745 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3746 Set_Etype (E, Any_Type);
3747 end if;
3749 Analyze_Dimension (N);
3750 end Analyze_Number_Declaration;
3752 --------------------------------
3753 -- Analyze_Object_Declaration --
3754 --------------------------------
3756 -- WARNING: This routine manages Ghost regions. Return statements must be
3757 -- replaced by gotos which jump to the end of the routine and restore the
3758 -- Ghost mode.
3760 procedure Analyze_Object_Declaration (N : Node_Id) is
3761 Loc : constant Source_Ptr := Sloc (N);
3762 Id : constant Entity_Id := Defining_Identifier (N);
3763 Next_Decl : constant Node_Id := Next (N);
3765 Act_T : Entity_Id;
3766 T : Entity_Id;
3768 E : Node_Id := Expression (N);
3769 -- E is set to Expression (N) throughout this routine. When Expression
3770 -- (N) is modified, E is changed accordingly.
3772 procedure Check_Dynamic_Object (Typ : Entity_Id);
3773 -- A library-level object with nonstatic discriminant constraints may
3774 -- require dynamic allocation. The declaration is illegal if the
3775 -- profile includes the restriction No_Implicit_Heap_Allocations.
3777 procedure Check_For_Null_Excluding_Components
3778 (Obj_Typ : Entity_Id;
3779 Obj_Decl : Node_Id);
3780 -- Verify that each null-excluding component of object declaration
3781 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3782 -- a compile-time warning if this is not the case.
3784 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
3785 -- Check that the return subtype indication properly matches the result
3786 -- subtype of the function in an extended return object declaration, as
3787 -- required by RM 6.5(5.1/2-5.3/2).
3789 function Count_Tasks (T : Entity_Id) return Uint;
3790 -- This function is called when a non-generic library level object of a
3791 -- task type is declared. Its function is to count the static number of
3792 -- tasks declared within the type (it is only called if Has_Task is set
3793 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3794 -- or a variant record type is encountered, Check_Restriction is called
3795 -- indicating the count is unknown.
3797 function Delayed_Aspect_Present return Boolean;
3798 -- If the declaration has an expression that is an aggregate, and it
3799 -- has aspects that require delayed analysis, the resolution of the
3800 -- aggregate must be deferred to the freeze point of the object. This
3801 -- special processing was created for address clauses, but it must
3802 -- also apply to address aspects. This must be done before the aspect
3803 -- specifications are analyzed because we must handle the aggregate
3804 -- before the analysis of the object declaration is complete.
3806 -- Any other relevant delayed aspects on object declarations ???
3808 --------------------------
3809 -- Check_Dynamic_Object --
3810 --------------------------
3812 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3813 Comp : Entity_Id;
3814 Obj_Type : Entity_Id;
3816 begin
3817 Obj_Type := Typ;
3819 if Is_Private_Type (Obj_Type)
3820 and then Present (Full_View (Obj_Type))
3821 then
3822 Obj_Type := Full_View (Obj_Type);
3823 end if;
3825 if Known_Static_Esize (Obj_Type) then
3826 return;
3827 end if;
3829 if Restriction_Active (No_Implicit_Heap_Allocations)
3830 and then Expander_Active
3831 and then Has_Discriminants (Obj_Type)
3832 then
3833 Comp := First_Component (Obj_Type);
3834 while Present (Comp) loop
3835 if Known_Static_Esize (Etype (Comp))
3836 or else Size_Known_At_Compile_Time (Etype (Comp))
3837 then
3838 null;
3840 elsif Is_Record_Type (Etype (Comp)) then
3841 Check_Dynamic_Object (Etype (Comp));
3843 elsif not Discriminated_Size (Comp)
3844 and then Comes_From_Source (Comp)
3845 then
3846 Error_Msg_NE
3847 ("component& of non-static size will violate restriction "
3848 & "No_Implicit_Heap_Allocation?", N, Comp);
3850 end if;
3852 Next_Component (Comp);
3853 end loop;
3854 end if;
3855 end Check_Dynamic_Object;
3857 -----------------------------------------
3858 -- Check_For_Null_Excluding_Components --
3859 -----------------------------------------
3861 procedure Check_For_Null_Excluding_Components
3862 (Obj_Typ : Entity_Id;
3863 Obj_Decl : Node_Id)
3865 procedure Check_Component
3866 (Comp_Typ : Entity_Id;
3867 Comp_Decl : Node_Id := Empty;
3868 Array_Comp : Boolean := False);
3869 -- Apply a compile-time null-exclusion check on a component denoted
3870 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3871 -- subcomponents (if any).
3873 ---------------------
3874 -- Check_Component --
3875 ---------------------
3877 procedure Check_Component
3878 (Comp_Typ : Entity_Id;
3879 Comp_Decl : Node_Id := Empty;
3880 Array_Comp : Boolean := False)
3882 Comp : Entity_Id;
3883 T : Entity_Id;
3885 begin
3886 -- Do not consider internally-generated components or those that
3887 -- are already initialized.
3889 if Present (Comp_Decl)
3890 and then (not Comes_From_Source (Comp_Decl)
3891 or else Present (Expression (Comp_Decl)))
3892 then
3893 return;
3894 end if;
3896 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3897 and then Present (Full_View (Comp_Typ))
3898 then
3899 T := Full_View (Comp_Typ);
3900 else
3901 T := Comp_Typ;
3902 end if;
3904 -- Verify a component of a null-excluding access type
3906 if Is_Access_Type (T)
3907 and then Can_Never_Be_Null (T)
3908 then
3909 if Comp_Decl = Obj_Decl then
3910 Null_Exclusion_Static_Checks
3911 (N => Obj_Decl,
3912 Comp => Empty,
3913 Array_Comp => Array_Comp);
3915 else
3916 Null_Exclusion_Static_Checks
3917 (N => Obj_Decl,
3918 Comp => Comp_Decl,
3919 Array_Comp => Array_Comp);
3920 end if;
3922 -- Check array components
3924 elsif Is_Array_Type (T) then
3926 -- There is no suitable component when the object is of an
3927 -- array type. However, a namable component may appear at some
3928 -- point during the recursive inspection, but not at the top
3929 -- level. At the top level just indicate array component case.
3931 if Comp_Decl = Obj_Decl then
3932 Check_Component (Component_Type (T), Array_Comp => True);
3933 else
3934 Check_Component (Component_Type (T), Comp_Decl);
3935 end if;
3937 -- Verify all components of type T
3939 -- Note: No checks are performed on types with discriminants due
3940 -- to complexities involving variants. ???
3942 elsif (Is_Concurrent_Type (T)
3943 or else Is_Incomplete_Or_Private_Type (T)
3944 or else Is_Record_Type (T))
3945 and then not Has_Discriminants (T)
3946 then
3947 Comp := First_Component (T);
3948 while Present (Comp) loop
3949 Check_Component (Etype (Comp), Parent (Comp));
3951 Next_Component (Comp);
3952 end loop;
3953 end if;
3954 end Check_Component;
3956 -- Start processing for Check_For_Null_Excluding_Components
3958 begin
3959 Check_Component (Obj_Typ, Obj_Decl);
3960 end Check_For_Null_Excluding_Components;
3962 -------------------------------------
3963 -- Check_Return_Subtype_Indication --
3964 -------------------------------------
3966 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
3967 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
3968 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3969 Func_Id : constant Entity_Id := Return_Applies_To (Scope (Obj_Id));
3970 R_Typ : constant Entity_Id := Etype (Func_Id);
3971 Indic : constant Node_Id :=
3972 Object_Definition (Original_Node (Obj_Decl));
3974 procedure Error_No_Match (N : Node_Id);
3975 -- Output error messages for case where types do not statically
3976 -- match. N is the location for the messages.
3978 --------------------
3979 -- Error_No_Match --
3980 --------------------
3982 procedure Error_No_Match (N : Node_Id) is
3983 begin
3984 Error_Msg_N
3985 ("subtype must statically match function result subtype", N);
3987 if not Predicates_Match (Obj_Typ, R_Typ) then
3988 Error_Msg_Node_2 := R_Typ;
3989 Error_Msg_NE
3990 ("\predicate of& does not match predicate of&",
3991 N, Obj_Typ);
3992 end if;
3993 end Error_No_Match;
3995 -- Start of processing for Check_Return_Subtype_Indication
3997 begin
3998 -- First, avoid cascaded errors
4000 if Error_Posted (Obj_Decl) or else Error_Posted (Indic) then
4001 return;
4002 end if;
4004 -- "return access T" case; check that the return statement also has
4005 -- "access T", and that the subtypes statically match:
4006 -- if this is an access to subprogram the signatures must match.
4008 if Is_Anonymous_Access_Type (R_Typ) then
4009 if Is_Anonymous_Access_Type (Obj_Typ) then
4010 if Ekind (Designated_Type (Obj_Typ)) /= E_Subprogram_Type
4011 then
4012 if Base_Type (Designated_Type (Obj_Typ)) /=
4013 Base_Type (Designated_Type (R_Typ))
4014 or else not Subtypes_Statically_Match (Obj_Typ, R_Typ)
4015 then
4016 Error_No_Match (Subtype_Mark (Indic));
4017 end if;
4019 else
4020 -- For two anonymous access to subprogram types, the types
4021 -- themselves must be type conformant.
4023 if not Conforming_Types
4024 (Obj_Typ, R_Typ, Fully_Conformant)
4025 then
4026 Error_No_Match (Indic);
4027 end if;
4028 end if;
4030 else
4031 Error_Msg_N ("must use anonymous access type", Indic);
4032 end if;
4034 -- If the return object is of an anonymous access type, then report
4035 -- an error if the function's result type is not also anonymous.
4037 elsif Is_Anonymous_Access_Type (Obj_Typ) then
4038 pragma Assert (not Is_Anonymous_Access_Type (R_Typ));
4039 Error_Msg_N
4040 ("anonymous access not allowed for function with named access "
4041 & "result", Indic);
4043 -- Subtype indication case: check that the return object's type is
4044 -- covered by the result type, and that the subtypes statically match
4045 -- when the result subtype is constrained. Also handle record types
4046 -- with unknown discriminants for which we have built the underlying
4047 -- record view. Coverage is needed to allow specific-type return
4048 -- objects when the result type is class-wide (see AI05-32).
4050 elsif Covers (Base_Type (R_Typ), Base_Type (Obj_Typ))
4051 or else (Is_Underlying_Record_View (Base_Type (Obj_Typ))
4052 and then
4053 Covers
4054 (Base_Type (R_Typ),
4055 Underlying_Record_View (Base_Type (Obj_Typ))))
4056 then
4057 -- A null exclusion may be present on the return type, on the
4058 -- function specification, on the object declaration or on the
4059 -- subtype itself.
4061 if Is_Access_Type (R_Typ)
4062 and then
4063 (Can_Never_Be_Null (R_Typ)
4064 or else Null_Exclusion_Present (Parent (Func_Id))) /=
4065 Can_Never_Be_Null (Obj_Typ)
4066 then
4067 Error_No_Match (Indic);
4068 end if;
4070 -- AI05-103: for elementary types, subtypes must statically match
4072 if Is_Constrained (R_Typ) or else Is_Access_Type (R_Typ) then
4073 if not Subtypes_Statically_Match (Obj_Typ, R_Typ) then
4074 Error_No_Match (Indic);
4075 end if;
4076 end if;
4078 -- All remaining cases are illegal
4080 -- Note: previous versions of this subprogram allowed the return
4081 -- value to be the ancestor of the return type if the return type
4082 -- was a null extension. This was plainly incorrect.
4084 else
4085 Error_Msg_N
4086 ("wrong type for return_subtype_indication", Indic);
4087 end if;
4088 end Check_Return_Subtype_Indication;
4090 -----------------
4091 -- Count_Tasks --
4092 -----------------
4094 function Count_Tasks (T : Entity_Id) return Uint is
4095 C : Entity_Id;
4096 X : Node_Id;
4097 V : Uint;
4099 begin
4100 if Is_Task_Type (T) then
4101 return Uint_1;
4103 elsif Is_Record_Type (T) then
4104 if Has_Discriminants (T) then
4105 Check_Restriction (Max_Tasks, N);
4106 return Uint_0;
4108 else
4109 V := Uint_0;
4110 C := First_Component (T);
4111 while Present (C) loop
4112 V := V + Count_Tasks (Etype (C));
4113 Next_Component (C);
4114 end loop;
4116 return V;
4117 end if;
4119 elsif Is_Array_Type (T) then
4120 X := First_Index (T);
4121 V := Count_Tasks (Component_Type (T));
4122 while Present (X) loop
4123 C := Etype (X);
4125 if not Is_OK_Static_Subtype (C) then
4126 Check_Restriction (Max_Tasks, N);
4127 return Uint_0;
4128 else
4129 V := V * (UI_Max (Uint_0,
4130 Expr_Value (Type_High_Bound (C)) -
4131 Expr_Value (Type_Low_Bound (C)) + Uint_1));
4132 end if;
4134 Next_Index (X);
4135 end loop;
4137 return V;
4139 else
4140 return Uint_0;
4141 end if;
4142 end Count_Tasks;
4144 ----------------------------
4145 -- Delayed_Aspect_Present --
4146 ----------------------------
4148 function Delayed_Aspect_Present return Boolean is
4149 A : Node_Id;
4150 A_Id : Aspect_Id;
4152 begin
4153 if Present (Aspect_Specifications (N)) then
4154 A := First (Aspect_Specifications (N));
4156 while Present (A) loop
4157 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4159 if A_Id = Aspect_Address then
4161 -- Set flag on object entity, for later processing at
4162 -- the freeze point.
4164 Set_Has_Delayed_Aspects (Id);
4165 return True;
4166 end if;
4168 Next (A);
4169 end loop;
4170 end if;
4172 return False;
4173 end Delayed_Aspect_Present;
4175 -- Local variables
4177 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4178 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4179 -- Save the Ghost-related attributes to restore on exit
4181 Prev_Entity : Entity_Id := Empty;
4182 Related_Id : Entity_Id;
4184 -- Start of processing for Analyze_Object_Declaration
4186 begin
4187 -- There are three kinds of implicit types generated by an
4188 -- object declaration:
4190 -- 1. Those generated by the original Object Definition
4192 -- 2. Those generated by the Expression
4194 -- 3. Those used to constrain the Object Definition with the
4195 -- expression constraints when the definition is unconstrained.
4197 -- They must be generated in this order to avoid order of elaboration
4198 -- issues. Thus the first step (after entering the name) is to analyze
4199 -- the object definition.
4201 if Constant_Present (N) then
4202 Prev_Entity := Current_Entity_In_Scope (Id);
4204 if Present (Prev_Entity)
4205 and then
4206 -- If the homograph is an implicit subprogram, it is overridden
4207 -- by the current declaration.
4209 ((Is_Overloadable (Prev_Entity)
4210 and then Is_Inherited_Operation (Prev_Entity))
4212 -- The current object is a discriminal generated for an entry
4213 -- family index. Even though the index is a constant, in this
4214 -- particular context there is no true constant redeclaration.
4215 -- Enter_Name will handle the visibility.
4217 or else
4218 (Is_Discriminal (Id)
4219 and then Ekind (Discriminal_Link (Id)) =
4220 E_Entry_Index_Parameter)
4222 -- The current object is the renaming for a generic declared
4223 -- within the instance.
4225 or else
4226 (Ekind (Prev_Entity) = E_Package
4227 and then Nkind (Parent (Prev_Entity)) =
4228 N_Package_Renaming_Declaration
4229 and then not Comes_From_Source (Prev_Entity)
4230 and then
4231 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4233 -- The entity may be a homonym of a private component of the
4234 -- enclosing protected object, for which we create a local
4235 -- renaming declaration. The declaration is legal, even if
4236 -- useless when it just captures that component.
4238 or else
4239 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4240 and then Nkind (Parent (Prev_Entity)) =
4241 N_Object_Renaming_Declaration))
4242 then
4243 Prev_Entity := Empty;
4244 end if;
4245 end if;
4247 if Present (Prev_Entity) then
4249 -- The object declaration is Ghost when it completes a deferred Ghost
4250 -- constant.
4252 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4254 Constant_Redeclaration (Id, N, T);
4256 Generate_Reference (Prev_Entity, Id, 'c');
4257 Set_Completion_Referenced (Id);
4259 if Error_Posted (N) then
4261 -- Type mismatch or illegal redeclaration; do not analyze
4262 -- expression to avoid cascaded errors.
4264 T := Find_Type_Of_Object (Object_Definition (N), N);
4265 Set_Etype (Id, T);
4266 Mutate_Ekind (Id, E_Variable);
4267 goto Leave;
4268 end if;
4270 -- In the normal case, enter identifier at the start to catch premature
4271 -- usage in the initialization expression.
4273 else
4274 Generate_Definition (Id);
4275 Enter_Name (Id);
4277 Mark_Coextensions (N, Object_Definition (N));
4279 T := Find_Type_Of_Object (Object_Definition (N), N);
4281 if Nkind (Object_Definition (N)) = N_Access_Definition
4282 and then Present
4283 (Access_To_Subprogram_Definition (Object_Definition (N)))
4284 and then Protected_Present
4285 (Access_To_Subprogram_Definition (Object_Definition (N)))
4286 then
4287 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4288 end if;
4290 if Error_Posted (Id) then
4291 Set_Etype (Id, T);
4292 Mutate_Ekind (Id, E_Variable);
4293 goto Leave;
4294 end if;
4295 end if;
4297 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4298 -- out some static checks.
4300 if Ada_Version >= Ada_2005 then
4302 -- In case of aggregates we must also take care of the correct
4303 -- initialization of nested aggregates bug this is done at the
4304 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4306 if Can_Never_Be_Null (T) then
4307 if Present (Expression (N))
4308 and then Nkind (Expression (N)) = N_Aggregate
4309 then
4310 null;
4312 elsif Comes_From_Source (Id) then
4313 declare
4314 Save_Typ : constant Entity_Id := Etype (Id);
4315 begin
4316 Set_Etype (Id, T); -- Temp. decoration for static checks
4317 Null_Exclusion_Static_Checks (N);
4318 Set_Etype (Id, Save_Typ);
4319 end;
4320 end if;
4322 -- We might be dealing with an object of a composite type containing
4323 -- null-excluding components without an aggregate, so we must verify
4324 -- that such components have default initialization.
4326 else
4327 Check_For_Null_Excluding_Components (T, N);
4328 end if;
4329 end if;
4331 -- Object is marked pure if it is in a pure scope
4333 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4335 -- If deferred constant, make sure context is appropriate. We detect
4336 -- a deferred constant as a constant declaration with no expression.
4337 -- A deferred constant can appear in a package body if its completion
4338 -- is by means of an interface pragma.
4340 if Constant_Present (N) and then No (E) then
4342 -- A deferred constant may appear in the declarative part of the
4343 -- following constructs:
4345 -- blocks
4346 -- entry bodies
4347 -- extended return statements
4348 -- package specs
4349 -- package bodies
4350 -- subprogram bodies
4351 -- task bodies
4353 -- When declared inside a package spec, a deferred constant must be
4354 -- completed by a full constant declaration or pragma Import. In all
4355 -- other cases, the only proper completion is pragma Import. Extended
4356 -- return statements are flagged as invalid contexts because they do
4357 -- not have a declarative part and so cannot accommodate the pragma.
4359 if Ekind (Current_Scope) = E_Return_Statement then
4360 Error_Msg_N
4361 ("invalid context for deferred constant declaration (RM 7.4)",
4363 Error_Msg_N
4364 ("\declaration requires an initialization expression",
4366 Set_Constant_Present (N, False);
4368 -- In Ada 83, deferred constant must be of private type
4370 elsif not Is_Private_Type (T) then
4371 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4372 Error_Msg_N
4373 ("(Ada 83) deferred constant must be private type", N);
4374 end if;
4375 end if;
4377 -- If not a deferred constant, then the object declaration freezes
4378 -- its type, unless the object is of an anonymous type and has delayed
4379 -- aspects. In that case the type is frozen when the object itself is.
4381 else
4382 Check_Fully_Declared (T, N);
4384 if Has_Delayed_Aspects (Id)
4385 and then Is_Array_Type (T)
4386 and then Is_Itype (T)
4387 then
4388 Set_Has_Delayed_Freeze (T);
4389 else
4390 Freeze_Before (N, T);
4391 end if;
4392 end if;
4394 -- If the object was created by a constrained array definition, then
4395 -- set the link in both the anonymous base type and anonymous subtype
4396 -- that are built to represent the array type to point to the object.
4398 if Nkind (Object_Definition (Declaration_Node (Id))) =
4399 N_Constrained_Array_Definition
4400 then
4401 Set_Related_Array_Object (T, Id);
4402 Set_Related_Array_Object (Base_Type (T), Id);
4403 end if;
4405 -- Check for protected objects not at library level
4407 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4408 Check_Restriction (No_Local_Protected_Objects, Id);
4409 end if;
4411 -- Check for violation of No_Local_Timing_Events
4413 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4414 Check_Restriction (No_Local_Timing_Events, Id);
4415 end if;
4417 -- The actual subtype of the object is the nominal subtype, unless
4418 -- the nominal one is unconstrained and obtained from the expression.
4420 Act_T := T;
4422 if Is_Library_Level_Entity (Id) then
4423 Check_Dynamic_Object (T);
4424 end if;
4426 -- Process initialization expression if present and not in error
4428 if Present (E) and then E /= Error then
4430 -- Generate an error in case of CPP class-wide object initialization.
4431 -- Required because otherwise the expansion of the class-wide
4432 -- assignment would try to use 'size to initialize the object
4433 -- (primitive that is not available in CPP tagged types).
4435 if Is_Class_Wide_Type (Act_T)
4436 and then
4437 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4438 or else
4439 (Present (Full_View (Root_Type (Etype (Act_T))))
4440 and then
4441 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4442 then
4443 Error_Msg_N
4444 ("predefined assignment not available for 'C'P'P tagged types",
4446 end if;
4448 Mark_Coextensions (N, E);
4449 Analyze (E);
4451 -- In case of errors detected in the analysis of the expression,
4452 -- decorate it with the expected type to avoid cascaded errors.
4454 if No (Etype (E)) then
4455 Set_Etype (E, T);
4456 end if;
4458 -- If an initialization expression is present, then we set the
4459 -- Is_True_Constant flag. It will be reset if this is a variable
4460 -- and it is indeed modified.
4462 Set_Is_True_Constant (Id, True);
4464 -- If we are analyzing a constant declaration, set its completion
4465 -- flag after analyzing and resolving the expression.
4467 if Constant_Present (N) then
4468 Set_Has_Completion (Id);
4469 end if;
4471 -- Set type and resolve (type may be overridden later on). Note:
4472 -- Ekind (Id) must still be E_Void at this point so that incorrect
4473 -- early usage within E is properly diagnosed.
4475 Set_Etype (Id, T);
4477 -- If the expression is an aggregate we must look ahead to detect
4478 -- the possible presence of an address clause, and defer resolution
4479 -- and expansion of the aggregate to the freeze point of the entity.
4481 -- This is not always legal because the aggregate may contain other
4482 -- references that need freezing, e.g. references to other entities
4483 -- with address clauses. In any case, when compiling with -gnatI the
4484 -- presence of the address clause must be ignored.
4486 if Comes_From_Source (N)
4487 and then Expander_Active
4488 and then Nkind (E) = N_Aggregate
4489 and then
4490 ((Present (Following_Address_Clause (N))
4491 and then not Ignore_Rep_Clauses)
4492 or else Delayed_Aspect_Present)
4493 then
4494 Set_Etype (E, T);
4496 -- If the aggregate is limited it will be built in place, and its
4497 -- expansion is deferred until the object declaration is expanded.
4499 -- This is also required when generating C code to ensure that an
4500 -- object with an alignment or address clause can be initialized
4501 -- by means of component by component assignments.
4503 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4504 Set_Expansion_Delayed (E);
4505 end if;
4507 else
4508 -- If the expression is a formal that is a "subprogram pointer"
4509 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4510 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4511 -- the corresponding check, as is done for assignments.
4513 if Is_Entity_Name (E)
4514 and then Present (Entity (E))
4515 and then Is_Formal (Entity (E))
4516 and then
4517 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4518 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4519 then
4520 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4521 end if;
4523 Resolve (E, T);
4524 end if;
4526 -- No further action needed if E is a call to an inlined function
4527 -- which returns an unconstrained type and it has been expanded into
4528 -- a procedure call. In that case N has been replaced by an object
4529 -- declaration without initializing expression and it has been
4530 -- analyzed (see Expand_Inlined_Call).
4532 if Back_End_Inlining
4533 and then Expander_Active
4534 and then Nkind (E) = N_Function_Call
4535 and then Nkind (Name (E)) in N_Has_Entity
4536 and then Is_Inlined (Entity (Name (E)))
4537 and then not Is_Constrained (Etype (E))
4538 and then Analyzed (N)
4539 and then No (Expression (N))
4540 then
4541 goto Leave;
4542 end if;
4544 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4545 -- node (which was marked already-analyzed), we need to set the type
4546 -- to something else than Universal_Access to keep gigi happy.
4548 if Etype (E) = Universal_Access then
4549 Set_Etype (E, T);
4550 end if;
4552 -- If the object is an access to variable, the initialization
4553 -- expression cannot be an access to constant.
4555 if Is_Access_Type (T)
4556 and then not Is_Access_Constant (T)
4557 and then Is_Access_Type (Etype (E))
4558 and then Is_Access_Constant (Etype (E))
4559 then
4560 Error_Msg_N
4561 ("access to variable cannot be initialized with an "
4562 & "access-to-constant expression", E);
4563 end if;
4565 if not Assignment_OK (N) then
4566 Check_Initialization (T, E);
4567 end if;
4569 Check_Unset_Reference (E);
4571 -- If this is a variable, then set current value. If this is a
4572 -- declared constant of a scalar type with a static expression,
4573 -- indicate that it is always valid.
4575 if not Constant_Present (N) then
4576 if Compile_Time_Known_Value (E) then
4577 Set_Current_Value (Id, E);
4578 end if;
4580 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4581 Set_Is_Known_Valid (Id);
4583 -- If it is a constant initialized with a valid nonstatic entity,
4584 -- the constant is known valid as well, and can inherit the subtype
4585 -- of the entity if it is a subtype of the given type. This info
4586 -- is preserved on the actual subtype of the constant.
4588 elsif Is_Scalar_Type (T)
4589 and then Is_Entity_Name (E)
4590 and then Is_Known_Valid (Entity (E))
4591 and then In_Subrange_Of (Etype (Entity (E)), T)
4592 then
4593 Set_Is_Known_Valid (Id);
4594 Mutate_Ekind (Id, E_Constant);
4595 Set_Actual_Subtype (Id, Etype (Entity (E)));
4596 end if;
4598 -- Deal with setting of null flags
4600 if Is_Access_Type (T) then
4601 if Known_Non_Null (E) then
4602 Set_Is_Known_Non_Null (Id, True);
4603 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4604 Set_Is_Known_Null (Id, True);
4605 end if;
4606 end if;
4608 -- Check incorrect use of dynamically tagged expressions
4610 if Is_Tagged_Type (T) then
4611 Check_Dynamically_Tagged_Expression
4612 (Expr => E,
4613 Typ => T,
4614 Related_Nod => N);
4615 end if;
4617 Apply_Scalar_Range_Check (E, T);
4618 Apply_Static_Length_Check (E, T);
4620 -- A formal parameter of a specific tagged type whose related
4621 -- subprogram is subject to pragma Extensions_Visible with value
4622 -- "False" cannot be implicitly converted to a class-wide type by
4623 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4624 -- not consider internally generated expressions.
4626 if Is_Class_Wide_Type (T)
4627 and then Comes_From_Source (E)
4628 and then Is_EVF_Expression (E)
4629 then
4630 Error_Msg_N
4631 ("formal parameter cannot be implicitly converted to "
4632 & "class-wide type when Extensions_Visible is False", E);
4633 end if;
4634 end if;
4636 -- If the No_Streams restriction is set, check that the type of the
4637 -- object is not, and does not contain, any subtype derived from
4638 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4639 -- Has_Stream just for efficiency reasons. There is no point in
4640 -- spending time on a Has_Stream check if the restriction is not set.
4642 if Restriction_Check_Required (No_Streams) then
4643 if Has_Stream (T) then
4644 Check_Restriction (No_Streams, N);
4645 end if;
4646 end if;
4648 -- Deal with predicate check before we start to do major rewriting. It
4649 -- is OK to initialize and then check the initialized value, since the
4650 -- object goes out of scope if we get a predicate failure. Note that we
4651 -- do this in the analyzer and not the expander because the analyzer
4652 -- does some substantial rewriting in some cases.
4654 -- We need a predicate check if the type has predicates that are not
4655 -- ignored, and if either there is an initializing expression, or for
4656 -- default initialization when we have at least one case of an explicit
4657 -- default initial value (including via a Default_Value or
4658 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4659 -- an internal declaration whose initialization comes later (as for an
4660 -- aggregate expansion) or a deferred constant.
4661 -- If expression is an aggregate it may be expanded into assignments
4662 -- and the declaration itself is marked with No_Initialization, but
4663 -- the predicate still applies.
4665 if not Suppress_Assignment_Checks (N)
4666 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4667 and then
4668 (not No_Initialization (N)
4669 or else (Present (E) and then Nkind (E) = N_Aggregate))
4670 and then
4671 (Present (E)
4672 or else
4673 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4674 and then not (Constant_Present (N) and then No (E))
4675 then
4676 -- If the type has a static predicate and the expression is known at
4677 -- compile time, see if the expression satisfies the predicate.
4678 -- In the case of a static expression, this must be done even if
4679 -- the predicate is not enabled (as per static expression rules).
4681 if Present (E) then
4682 Check_Expression_Against_Static_Predicate (E, T);
4683 end if;
4685 -- Do not perform further predicate-related checks unless
4686 -- predicates are enabled for the subtype.
4688 if not Predicate_Enabled (T) then
4689 null;
4691 -- If the type is a null record and there is no explicit initial
4692 -- expression, no predicate check applies.
4694 elsif No (E) and then Is_Null_Record_Type (T) then
4695 null;
4697 -- Do not generate a predicate check if the initialization expression
4698 -- is a type conversion whose target subtype statically matches the
4699 -- object's subtype because the conversion has been subjected to the
4700 -- same check. This is a small optimization which avoids redundant
4701 -- checks.
4703 elsif Present (E)
4704 and then Nkind (E) in N_Type_Conversion
4705 and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
4706 then
4707 null;
4709 else
4710 -- The check must be inserted after the expanded aggregate
4711 -- expansion code, if any.
4713 declare
4714 Check : constant Node_Id :=
4715 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4717 begin
4718 if No (Next_Decl) then
4719 Append_To (List_Containing (N), Check);
4720 else
4721 Insert_Before (Next_Decl, Check);
4722 end if;
4723 end;
4724 end if;
4725 end if;
4727 -- Case of unconstrained type
4729 if not Is_Definite_Subtype (T) then
4731 -- Nothing to do in deferred constant case
4733 if Constant_Present (N) and then No (E) then
4734 null;
4736 -- Case of no initialization present
4738 elsif No (E) then
4739 if No_Initialization (N) then
4740 null;
4742 elsif Is_Class_Wide_Type (T) then
4743 Error_Msg_N
4744 ("initialization required in class-wide declaration", N);
4746 else
4747 Error_Msg_N
4748 ("unconstrained subtype not allowed (need initialization)",
4749 Object_Definition (N));
4751 if Is_Record_Type (T) and then Has_Discriminants (T) then
4752 Error_Msg_N
4753 ("\provide initial value or explicit discriminant values",
4754 Object_Definition (N));
4756 Error_Msg_NE
4757 ("\or give default discriminant values for type&",
4758 Object_Definition (N), T);
4760 elsif Is_Array_Type (T) then
4761 Error_Msg_N
4762 ("\provide initial value or explicit array bounds",
4763 Object_Definition (N));
4764 end if;
4765 end if;
4767 -- Case of initialization present but in error. Set initial
4768 -- expression as absent (but do not make above complaints).
4770 elsif E = Error then
4771 Set_Expression (N, Empty);
4772 E := Empty;
4774 -- Case of initialization present
4776 else
4777 -- Unconstrained variables not allowed in Ada 83
4779 if Ada_Version = Ada_83
4780 and then not Constant_Present (N)
4781 and then Comes_From_Source (Object_Definition (N))
4782 then
4783 Error_Msg_N
4784 ("(Ada 83) unconstrained variable not allowed",
4785 Object_Definition (N));
4786 end if;
4788 -- Now we constrain the variable from the initializing expression
4790 -- If the expression is an aggregate, it has been expanded into
4791 -- individual assignments. Retrieve the actual type from the
4792 -- expanded construct.
4794 if Is_Array_Type (T)
4795 and then No_Initialization (N)
4796 and then Nkind (Original_Node (E)) = N_Aggregate
4797 then
4798 Act_T := Etype (E);
4800 -- In case of class-wide interface object declarations we delay
4801 -- the generation of the equivalent record type declarations until
4802 -- its expansion because there are cases in they are not required.
4804 elsif Is_Interface (T) then
4805 null;
4807 -- If the type is an unchecked union, no subtype can be built from
4808 -- the expression. Rewrite declaration as a renaming, which the
4809 -- back-end can handle properly. This is a rather unusual case,
4810 -- because most unchecked_union declarations have default values
4811 -- for discriminants and are thus not indefinite.
4813 elsif Is_Unchecked_Union (T) then
4814 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4815 Mutate_Ekind (Id, E_Constant);
4816 else
4817 Mutate_Ekind (Id, E_Variable);
4818 end if;
4820 -- If the expression is an aggregate it contains the required
4821 -- discriminant values but it has not been resolved yet, so do
4822 -- it now, and treat it as the initial expression of an object
4823 -- declaration, rather than a renaming.
4825 if Nkind (E) = N_Aggregate then
4826 Analyze_And_Resolve (E, T);
4828 else
4829 Rewrite (N,
4830 Make_Object_Renaming_Declaration (Loc,
4831 Defining_Identifier => Id,
4832 Subtype_Mark => New_Occurrence_Of (T, Loc),
4833 Name => E));
4835 Set_Renamed_Object (Id, E);
4836 Freeze_Before (N, T);
4837 Set_Is_Frozen (Id);
4838 goto Leave;
4839 end if;
4841 else
4842 -- Ensure that the generated subtype has a unique external name
4843 -- when the related object is public. This guarantees that the
4844 -- subtype and its bounds will not be affected by switches or
4845 -- pragmas that may offset the internal counter due to extra
4846 -- generated code.
4848 if Is_Public (Id) then
4849 Related_Id := Id;
4850 else
4851 Related_Id := Empty;
4852 end if;
4854 -- If the object has an unconstrained array subtype with fixed
4855 -- lower bound, then sliding to that bound may be needed.
4857 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4858 Expand_Sliding_Conversion (E, T);
4859 end if;
4861 if In_Spec_Expression and then In_Declare_Expr > 0 then
4862 -- It is too early to be doing expansion-ish things,
4863 -- so exit early. But we have to set Ekind (Id) now so
4864 -- that subsequent uses of this entity are not rejected
4865 -- via the same mechanism that (correctly) rejects
4866 -- "X : Integer := X;".
4868 if Constant_Present (N) then
4869 Mutate_Ekind (Id, E_Constant);
4870 Set_Is_True_Constant (Id);
4871 else
4872 Mutate_Ekind (Id, E_Variable);
4873 if Present (E) then
4874 Set_Has_Initial_Value (Id);
4875 end if;
4876 end if;
4878 goto Leave;
4879 end if;
4881 Expand_Subtype_From_Expr
4882 (N => N,
4883 Unc_Type => T,
4884 Subtype_Indic => Object_Definition (N),
4885 Exp => E,
4886 Related_Id => Related_Id);
4888 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4889 end if;
4891 if Act_T /= T then
4892 declare
4893 Full_View_Present : constant Boolean :=
4894 Is_Private_Type (Act_T)
4895 and then Present (Full_View (Act_T));
4896 -- Propagate attributes to full view when needed
4898 begin
4899 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4901 if Full_View_Present then
4902 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4903 end if;
4905 if Aliased_Present (N) then
4906 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4908 if Full_View_Present then
4909 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4910 end if;
4911 end if;
4913 Freeze_Before (N, Act_T);
4914 end;
4915 end if;
4917 Freeze_Before (N, T);
4918 end if;
4920 elsif Is_Array_Type (T)
4921 and then No_Initialization (N)
4922 and then (Nkind (Original_Node (E)) = N_Aggregate
4923 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4924 and then Nkind (Original_Node (Expression
4925 (Original_Node (E)))) = N_Aggregate))
4926 then
4927 if not Is_Entity_Name (Object_Definition (N)) then
4928 Act_T := Etype (E);
4929 Check_Compile_Time_Size (Act_T);
4930 end if;
4932 -- When the given object definition and the aggregate are specified
4933 -- independently, and their lengths might differ do a length check.
4934 -- This cannot happen if the aggregate is of the form (others =>...)
4936 if Nkind (E) = N_Raise_Constraint_Error then
4938 -- Aggregate is statically illegal. Place back in declaration
4940 Set_Expression (N, E);
4941 Set_No_Initialization (N, False);
4943 elsif T = Etype (E) then
4944 null;
4946 elsif Nkind (E) = N_Aggregate
4947 and then Present (Component_Associations (E))
4948 and then Present (Choice_List (First (Component_Associations (E))))
4949 and then
4950 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4951 N_Others_Choice
4952 then
4953 null;
4955 else
4956 Apply_Length_Check (E, T);
4957 end if;
4959 -- When possible, build the default subtype
4961 elsif Build_Default_Subtype_OK (T) then
4962 if No (E) then
4963 Act_T := Build_Default_Subtype (T, N);
4964 else
4965 -- Ada 2005: A limited object may be initialized by means of an
4966 -- aggregate. If the type has default discriminants it has an
4967 -- unconstrained nominal type, Its actual subtype will be obtained
4968 -- from the aggregate, and not from the default discriminants.
4970 Act_T := Etype (E);
4971 end if;
4973 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4975 elsif Nkind (E) = N_Function_Call
4976 and then Constant_Present (N)
4977 and then Has_Unconstrained_Elements (Etype (E))
4978 then
4979 -- The back-end has problems with constants of a discriminated type
4980 -- with defaults, if the initial value is a function call. We
4981 -- generate an intermediate temporary that will receive a reference
4982 -- to the result of the call. The initialization expression then
4983 -- becomes a dereference of that temporary.
4985 Remove_Side_Effects (E);
4987 -- If this is a constant declaration of an unconstrained type and
4988 -- the initialization is an aggregate, we can use the subtype of the
4989 -- aggregate for the declared entity because it is immutable.
4991 elsif not Is_Constrained (T)
4992 and then Has_Discriminants (T)
4993 and then Constant_Present (N)
4994 and then not Has_Unchecked_Union (T)
4995 and then Nkind (E) = N_Aggregate
4996 then
4997 Act_T := Etype (E);
4998 end if;
5000 -- Check No_Wide_Characters restriction
5002 Check_Wide_Character_Restriction (T, Object_Definition (N));
5004 -- Indicate this is not set in source. Certainly true for constants, and
5005 -- true for variables so far (will be reset for a variable if and when
5006 -- we encounter a modification in the source).
5008 Set_Never_Set_In_Source (Id);
5010 -- Now establish the proper kind and type of the object
5012 if Ekind (Id) = E_Void then
5013 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5014 end if;
5016 if Constant_Present (N) then
5017 Mutate_Ekind (Id, E_Constant);
5018 Set_Is_True_Constant (Id);
5020 else
5021 Mutate_Ekind (Id, E_Variable);
5023 -- A variable is set as shared passive if it appears in a shared
5024 -- passive package, and is at the outer level. This is not done for
5025 -- entities generated during expansion, because those are always
5026 -- manipulated locally.
5028 if Is_Shared_Passive (Current_Scope)
5029 and then Is_Library_Level_Entity (Id)
5030 and then Comes_From_Source (Id)
5031 then
5032 Set_Is_Shared_Passive (Id);
5033 Check_Shared_Var (Id, T, N);
5034 end if;
5036 -- Set Has_Initial_Value if initializing expression present. Note
5037 -- that if there is no initializing expression, we leave the state
5038 -- of this flag unchanged (usually it will be False, but notably in
5039 -- the case of exception choice variables, it will already be true).
5041 if Present (E) then
5042 Set_Has_Initial_Value (Id);
5043 end if;
5044 end if;
5046 -- Set the SPARK mode from the current context (may be overwritten later
5047 -- with explicit pragma).
5049 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5050 Set_SPARK_Pragma_Inherited (Id);
5052 -- Preserve relevant elaboration-related attributes of the context which
5053 -- are no longer available or very expensive to recompute once analysis,
5054 -- resolution, and expansion are over.
5056 Mark_Elaboration_Attributes
5057 (N_Id => Id,
5058 Checks => True,
5059 Warnings => True);
5061 -- Initialize alignment and size and capture alignment setting
5063 Reinit_Alignment (Id);
5064 Reinit_Esize (Id);
5065 Set_Optimize_Alignment_Flags (Id);
5067 -- Deal with aliased case
5069 if Aliased_Present (N) then
5070 Set_Is_Aliased (Id);
5072 -- AI12-001: All aliased objects are considered to be specified as
5073 -- independently addressable (RM C.6(8.1/4)).
5075 Set_Is_Independent (Id);
5077 -- If the object is aliased and the type is unconstrained with
5078 -- defaulted discriminants and there is no expression, then the
5079 -- object is constrained by the defaults, so it is worthwhile
5080 -- building the corresponding subtype.
5082 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5083 -- unconstrained, then only establish an actual subtype if the
5084 -- nominal subtype is indefinite. In definite cases the object is
5085 -- unconstrained in Ada 2005.
5087 if No (E)
5088 and then Is_Record_Type (T)
5089 and then not Is_Constrained (T)
5090 and then Has_Discriminants (T)
5091 and then (Ada_Version < Ada_2005
5092 or else not Is_Definite_Subtype (T))
5093 then
5094 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5095 end if;
5096 end if;
5098 -- Now we can set the type of the object
5100 Set_Etype (Id, Act_T);
5102 -- Non-constant object is marked to be treated as volatile if type is
5103 -- volatile and we clear the Current_Value setting that may have been
5104 -- set above. Doing so for constants isn't required and might interfere
5105 -- with possible uses of the object as a static expression in contexts
5106 -- incompatible with volatility (e.g. as a case-statement alternative).
5108 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5109 Set_Treat_As_Volatile (Id);
5110 Set_Current_Value (Id, Empty);
5111 end if;
5113 -- Deal with controlled types
5115 if Has_Controlled_Component (Etype (Id))
5116 or else Is_Controlled (Etype (Id))
5117 then
5118 if not Is_Library_Level_Entity (Id) then
5119 Check_Restriction (No_Nested_Finalization, N);
5120 else
5121 Validate_Controlled_Object (Id);
5122 end if;
5123 end if;
5125 if Has_Task (Etype (Id)) then
5126 Check_Restriction (No_Tasking, N);
5128 -- Deal with counting max tasks
5130 -- Nothing to do if inside a generic
5132 if Inside_A_Generic then
5133 null;
5135 -- If library level entity, then count tasks
5137 elsif Is_Library_Level_Entity (Id) then
5138 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5140 -- If not library level entity, then indicate we don't know max
5141 -- tasks and also check task hierarchy restriction and blocking
5142 -- operation (since starting a task is definitely blocking).
5144 else
5145 Check_Restriction (Max_Tasks, N);
5146 Check_Restriction (No_Task_Hierarchy, N);
5147 Check_Potentially_Blocking_Operation (N);
5148 end if;
5150 -- A rather specialized test. If we see two tasks being declared
5151 -- of the same type in the same object declaration, and the task
5152 -- has an entry with an address clause, we know that program error
5153 -- will be raised at run time since we can't have two tasks with
5154 -- entries at the same address.
5156 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5157 declare
5158 E : Entity_Id;
5160 begin
5161 E := First_Entity (Etype (Id));
5162 while Present (E) loop
5163 if Ekind (E) = E_Entry
5164 and then Present (Get_Attribute_Definition_Clause
5165 (E, Attribute_Address))
5166 then
5167 Error_Msg_Warn := SPARK_Mode /= On;
5168 Error_Msg_N
5169 ("more than one task with same entry address<<", N);
5170 Error_Msg_N ("\Program_Error [<<", N);
5171 Insert_Action (N,
5172 Make_Raise_Program_Error (Loc,
5173 Reason => PE_Duplicated_Entry_Address));
5174 exit;
5175 end if;
5177 Next_Entity (E);
5178 end loop;
5179 end;
5180 end if;
5181 end if;
5183 -- Check specific legality rules for a return object
5185 if Is_Return_Object (Id) then
5186 Check_Return_Subtype_Indication (N);
5187 end if;
5189 -- Some simple constant-propagation: if the expression is a constant
5190 -- string initialized with a literal, share the literal. This avoids
5191 -- a run-time copy.
5193 if Present (E)
5194 and then Is_Entity_Name (E)
5195 and then Ekind (Entity (E)) = E_Constant
5196 and then Base_Type (Etype (E)) = Standard_String
5197 then
5198 declare
5199 Val : constant Node_Id := Constant_Value (Entity (E));
5200 begin
5201 if Present (Val) and then Nkind (Val) = N_String_Literal then
5202 Rewrite (E, New_Copy (Val));
5203 end if;
5204 end;
5205 end if;
5207 if Present (Prev_Entity)
5208 and then Is_Frozen (Prev_Entity)
5209 and then not Error_Posted (Id)
5210 then
5211 Error_Msg_N ("full constant declaration appears too late", N);
5212 end if;
5214 Check_Eliminated (Id);
5216 -- Deal with setting In_Private_Part flag if in private part
5218 if Ekind (Scope (Id)) = E_Package
5219 and then In_Private_Part (Scope (Id))
5220 then
5221 Set_In_Private_Part (Id);
5222 end if;
5224 <<Leave>>
5225 -- Initialize the refined state of a variable here because this is a
5226 -- common destination for legal and illegal object declarations.
5228 if Ekind (Id) = E_Variable then
5229 Set_Encapsulating_State (Id, Empty);
5230 end if;
5232 if Has_Aspects (N) then
5233 Analyze_Aspect_Specifications (N, Id);
5234 end if;
5236 Analyze_Dimension (N);
5238 -- Verify whether the object declaration introduces an illegal hidden
5239 -- state within a package subject to a null abstract state.
5241 if Ekind (Id) = E_Variable then
5242 Check_No_Hidden_State (Id);
5243 end if;
5245 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5246 end Analyze_Object_Declaration;
5248 ---------------------------
5249 -- Analyze_Others_Choice --
5250 ---------------------------
5252 -- Nothing to do for the others choice node itself, the semantic analysis
5253 -- of the others choice will occur as part of the processing of the parent
5255 procedure Analyze_Others_Choice (N : Node_Id) is
5256 pragma Warnings (Off, N);
5257 begin
5258 null;
5259 end Analyze_Others_Choice;
5261 -------------------------------------------
5262 -- Analyze_Private_Extension_Declaration --
5263 -------------------------------------------
5265 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5266 Indic : constant Node_Id := Subtype_Indication (N);
5267 T : constant Entity_Id := Defining_Identifier (N);
5268 Iface : Entity_Id;
5269 Iface_Elmt : Elmt_Id;
5270 Parent_Base : Entity_Id;
5271 Parent_Type : Entity_Id;
5273 begin
5274 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5276 if Is_Non_Empty_List (Interface_List (N)) then
5277 declare
5278 Intf : Node_Id;
5279 T : Entity_Id;
5281 begin
5282 Intf := First (Interface_List (N));
5283 while Present (Intf) loop
5284 T := Find_Type_Of_Subtype_Indic (Intf);
5286 Diagnose_Interface (Intf, T);
5287 Next (Intf);
5288 end loop;
5289 end;
5290 end if;
5292 Generate_Definition (T);
5294 -- For other than Ada 2012, just enter the name in the current scope
5296 if Ada_Version < Ada_2012 then
5297 Enter_Name (T);
5299 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5300 -- case of private type that completes an incomplete type.
5302 else
5303 declare
5304 Prev : Entity_Id;
5306 begin
5307 Prev := Find_Type_Name (N);
5309 pragma Assert (Prev = T
5310 or else (Ekind (Prev) = E_Incomplete_Type
5311 and then Present (Full_View (Prev))
5312 and then Full_View (Prev) = T));
5313 end;
5314 end if;
5316 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5317 Parent_Base := Base_Type (Parent_Type);
5319 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5320 Mutate_Ekind (T, Ekind (Parent_Type));
5321 Set_Etype (T, Any_Type);
5322 goto Leave;
5324 elsif not Is_Tagged_Type (Parent_Type) then
5325 Error_Msg_N
5326 ("parent of type extension must be a tagged type", Indic);
5327 goto Leave;
5329 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5330 Error_Msg_N ("premature derivation of incomplete type", Indic);
5331 goto Leave;
5333 elsif Is_Concurrent_Type (Parent_Type) then
5334 Error_Msg_N
5335 ("parent type of a private extension cannot be a synchronized "
5336 & "tagged type (RM 3.9.1 (3/1))", N);
5338 Set_Etype (T, Any_Type);
5339 Mutate_Ekind (T, E_Limited_Private_Type);
5340 Set_Private_Dependents (T, New_Elmt_List);
5341 Set_Error_Posted (T);
5342 goto Leave;
5343 end if;
5345 Check_Wide_Character_Restriction (Parent_Type, Indic);
5347 -- Perhaps the parent type should be changed to the class-wide type's
5348 -- specific type in this case to prevent cascading errors ???
5350 if Is_Class_Wide_Type (Parent_Type) then
5351 Error_Msg_N
5352 ("parent of type extension must not be a class-wide type", Indic);
5353 goto Leave;
5354 end if;
5356 if (not Is_Package_Or_Generic_Package (Current_Scope)
5357 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5358 or else In_Private_Part (Current_Scope)
5359 then
5360 Error_Msg_N ("invalid context for private extension", N);
5361 end if;
5363 -- Set common attributes
5365 Set_Is_Pure (T, Is_Pure (Current_Scope));
5366 Set_Scope (T, Current_Scope);
5367 Mutate_Ekind (T, E_Record_Type_With_Private);
5368 Reinit_Size_Align (T);
5369 Set_Default_SSO (T);
5370 Set_No_Reordering (T, No_Component_Reordering);
5372 Set_Etype (T, Parent_Base);
5373 Propagate_Concurrent_Flags (T, Parent_Base);
5375 Set_Convention (T, Convention (Parent_Type));
5376 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5377 Set_Is_First_Subtype (T);
5378 Make_Class_Wide_Type (T);
5380 -- Set the SPARK mode from the current context
5382 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5383 Set_SPARK_Pragma_Inherited (T);
5385 if Unknown_Discriminants_Present (N) then
5386 Set_Discriminant_Constraint (T, No_Elist);
5387 end if;
5389 Build_Derived_Record_Type (N, Parent_Type, T);
5391 -- A private extension inherits the Default_Initial_Condition pragma
5392 -- coming from any parent type within the derivation chain.
5394 if Has_DIC (Parent_Type) then
5395 Set_Has_Inherited_DIC (T);
5396 end if;
5398 -- A private extension inherits any class-wide invariants coming from a
5399 -- parent type or an interface. Note that the invariant procedure of the
5400 -- parent type should not be inherited because the private extension may
5401 -- define invariants of its own.
5403 if Has_Inherited_Invariants (Parent_Type)
5404 or else Has_Inheritable_Invariants (Parent_Type)
5405 then
5406 Set_Has_Inherited_Invariants (T);
5408 elsif Present (Interfaces (T)) then
5409 Iface_Elmt := First_Elmt (Interfaces (T));
5410 while Present (Iface_Elmt) loop
5411 Iface := Node (Iface_Elmt);
5413 if Has_Inheritable_Invariants (Iface) then
5414 Set_Has_Inherited_Invariants (T);
5415 exit;
5416 end if;
5418 Next_Elmt (Iface_Elmt);
5419 end loop;
5420 end if;
5422 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5423 -- synchronized formal derived type.
5425 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5426 Set_Is_Limited_Record (T);
5428 -- Formal derived type case
5430 if Is_Generic_Type (T) then
5432 -- The parent must be a tagged limited type or a synchronized
5433 -- interface.
5435 if (not Is_Tagged_Type (Parent_Type)
5436 or else not Is_Limited_Type (Parent_Type))
5437 and then
5438 (not Is_Interface (Parent_Type)
5439 or else not Is_Synchronized_Interface (Parent_Type))
5440 then
5441 Error_Msg_NE
5442 ("parent type of & must be tagged limited or synchronized",
5443 N, T);
5444 end if;
5446 -- The progenitors (if any) must be limited or synchronized
5447 -- interfaces.
5449 if Present (Interfaces (T)) then
5450 Iface_Elmt := First_Elmt (Interfaces (T));
5451 while Present (Iface_Elmt) loop
5452 Iface := Node (Iface_Elmt);
5454 if not Is_Limited_Interface (Iface)
5455 and then not Is_Synchronized_Interface (Iface)
5456 then
5457 Error_Msg_NE
5458 ("progenitor & must be limited or synchronized",
5459 N, Iface);
5460 end if;
5462 Next_Elmt (Iface_Elmt);
5463 end loop;
5464 end if;
5466 -- Regular derived extension, the parent must be a limited or
5467 -- synchronized interface.
5469 else
5470 if not Is_Interface (Parent_Type)
5471 or else (not Is_Limited_Interface (Parent_Type)
5472 and then not Is_Synchronized_Interface (Parent_Type))
5473 then
5474 Error_Msg_NE
5475 ("parent type of & must be limited interface", N, T);
5476 end if;
5477 end if;
5479 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5480 -- extension with a synchronized parent must be explicitly declared
5481 -- synchronized, because the full view will be a synchronized type.
5482 -- This must be checked before the check for limited types below,
5483 -- to ensure that types declared limited are not allowed to extend
5484 -- synchronized interfaces.
5486 elsif Is_Interface (Parent_Type)
5487 and then Is_Synchronized_Interface (Parent_Type)
5488 and then not Synchronized_Present (N)
5489 then
5490 Error_Msg_NE
5491 ("private extension of& must be explicitly synchronized",
5492 N, Parent_Type);
5494 elsif Limited_Present (N) then
5495 Set_Is_Limited_Record (T);
5497 if not Is_Limited_Type (Parent_Type)
5498 and then
5499 (not Is_Interface (Parent_Type)
5500 or else not Is_Limited_Interface (Parent_Type))
5501 then
5502 Error_Msg_NE ("parent type& of limited extension must be limited",
5503 N, Parent_Type);
5504 end if;
5505 end if;
5507 -- Remember that its parent type has a private extension. Used to warn
5508 -- on public primitives of the parent type defined after its private
5509 -- extensions (see Check_Dispatching_Operation).
5511 Set_Has_Private_Extension (Parent_Type);
5513 <<Leave>>
5514 if Has_Aspects (N) then
5515 Analyze_Aspect_Specifications (N, T);
5516 end if;
5517 end Analyze_Private_Extension_Declaration;
5519 ---------------------------------
5520 -- Analyze_Subtype_Declaration --
5521 ---------------------------------
5523 procedure Analyze_Subtype_Declaration
5524 (N : Node_Id;
5525 Skip : Boolean := False)
5527 Id : constant Entity_Id := Defining_Identifier (N);
5528 T : Entity_Id;
5530 begin
5531 Generate_Definition (Id);
5532 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5533 Reinit_Size_Align (Id);
5535 -- The following guard condition on Enter_Name is to handle cases where
5536 -- the defining identifier has already been entered into the scope but
5537 -- the declaration as a whole needs to be analyzed.
5539 -- This case in particular happens for derived enumeration types. The
5540 -- derived enumeration type is processed as an inserted enumeration type
5541 -- declaration followed by a rewritten subtype declaration. The defining
5542 -- identifier, however, is entered into the name scope very early in the
5543 -- processing of the original type declaration and therefore needs to be
5544 -- avoided here, when the created subtype declaration is analyzed. (See
5545 -- Build_Derived_Types)
5547 -- This also happens when the full view of a private type is derived
5548 -- type with constraints. In this case the entity has been introduced
5549 -- in the private declaration.
5551 -- Finally this happens in some complex cases when validity checks are
5552 -- enabled, where the same subtype declaration may be analyzed twice.
5553 -- This can happen if the subtype is created by the preanalysis of
5554 -- an attribute that gives the range of a loop statement, and the loop
5555 -- itself appears within an if_statement that will be rewritten during
5556 -- expansion.
5558 if Skip
5559 or else (Present (Etype (Id))
5560 and then (Is_Private_Type (Etype (Id))
5561 or else Is_Task_Type (Etype (Id))
5562 or else Is_Rewrite_Substitution (N)))
5563 then
5564 null;
5566 elsif Current_Entity (Id) = Id then
5567 null;
5569 else
5570 Enter_Name (Id);
5571 end if;
5573 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5575 -- Class-wide equivalent types of records with unknown discriminants
5576 -- involve the generation of an itype which serves as the private view
5577 -- of a constrained record subtype. In such cases the base type of the
5578 -- current subtype we are processing is the private itype. Use the full
5579 -- of the private itype when decorating various attributes.
5581 if Is_Itype (T)
5582 and then Is_Private_Type (T)
5583 and then Present (Full_View (T))
5584 then
5585 T := Full_View (T);
5586 end if;
5588 -- Inherit common attributes
5590 Set_Is_Volatile (Id, Is_Volatile (T));
5591 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5592 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5593 Set_Convention (Id, Convention (T));
5595 -- If ancestor has predicates then so does the subtype, and in addition
5596 -- we must delay the freeze to properly arrange predicate inheritance.
5598 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5599 -- in which T = ID, so the above tests and assignments do nothing???
5601 if Has_Predicates (T)
5602 or else (Present (Ancestor_Subtype (T))
5603 and then Has_Predicates (Ancestor_Subtype (T)))
5604 then
5605 Set_Has_Predicates (Id);
5606 Set_Has_Delayed_Freeze (Id);
5608 -- Generated subtypes inherit the predicate function from the parent
5609 -- (no aspects to examine on the generated declaration).
5611 if not Comes_From_Source (N) then
5612 Mutate_Ekind (Id, Ekind (T));
5614 if Present (Predicate_Function (Id)) then
5615 null;
5617 elsif Present (Predicate_Function (T)) then
5618 Set_Predicate_Function (Id, Predicate_Function (T));
5620 elsif Present (Ancestor_Subtype (T))
5621 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5622 then
5623 Set_Predicate_Function (Id,
5624 Predicate_Function (Ancestor_Subtype (T)));
5625 end if;
5626 end if;
5627 end if;
5629 -- In the case where there is no constraint given in the subtype
5630 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5631 -- semantic attributes must be established here.
5633 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5634 Set_Etype (Id, Base_Type (T));
5636 case Ekind (T) is
5637 when Array_Kind =>
5638 Mutate_Ekind (Id, E_Array_Subtype);
5639 Copy_Array_Subtype_Attributes (Id, T);
5640 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5642 when Decimal_Fixed_Point_Kind =>
5643 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5644 Set_Digits_Value (Id, Digits_Value (T));
5645 Set_Delta_Value (Id, Delta_Value (T));
5646 Set_Scale_Value (Id, Scale_Value (T));
5647 Set_Small_Value (Id, Small_Value (T));
5648 Set_Scalar_Range (Id, Scalar_Range (T));
5649 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5650 Set_Is_Constrained (Id, Is_Constrained (T));
5651 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5652 Copy_RM_Size (To => Id, From => T);
5654 when Enumeration_Kind =>
5655 Mutate_Ekind (Id, E_Enumeration_Subtype);
5656 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5657 Set_Scalar_Range (Id, Scalar_Range (T));
5658 Set_Is_Character_Type (Id, Is_Character_Type (T));
5659 Set_Is_Constrained (Id, Is_Constrained (T));
5660 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5661 Copy_RM_Size (To => Id, From => T);
5663 when Ordinary_Fixed_Point_Kind =>
5664 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5665 Set_Scalar_Range (Id, Scalar_Range (T));
5666 Set_Small_Value (Id, Small_Value (T));
5667 Set_Delta_Value (Id, Delta_Value (T));
5668 Set_Is_Constrained (Id, Is_Constrained (T));
5669 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5670 Copy_RM_Size (To => Id, From => T);
5672 when Float_Kind =>
5673 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5674 Set_Scalar_Range (Id, Scalar_Range (T));
5675 Set_Digits_Value (Id, Digits_Value (T));
5676 Set_Is_Constrained (Id, Is_Constrained (T));
5678 -- If the floating point type has dimensions, these will be
5679 -- inherited subsequently when Analyze_Dimensions is called.
5681 when Signed_Integer_Kind =>
5682 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5683 Set_Scalar_Range (Id, Scalar_Range (T));
5684 Set_Is_Constrained (Id, Is_Constrained (T));
5685 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5686 Copy_RM_Size (To => Id, From => T);
5688 when Modular_Integer_Kind =>
5689 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5690 Set_Scalar_Range (Id, Scalar_Range (T));
5691 Set_Is_Constrained (Id, Is_Constrained (T));
5692 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5693 Copy_RM_Size (To => Id, From => T);
5695 when Class_Wide_Kind =>
5696 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5697 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5698 Set_Cloned_Subtype (Id, T);
5699 Set_Is_Tagged_Type (Id, True);
5700 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5701 Set_Has_Unknown_Discriminants
5702 (Id, True);
5703 Set_No_Tagged_Streams_Pragma
5704 (Id, No_Tagged_Streams_Pragma (T));
5706 if Ekind (T) = E_Class_Wide_Subtype then
5707 Set_Equivalent_Type (Id, Equivalent_Type (T));
5708 end if;
5710 when E_Record_Subtype
5711 | E_Record_Type
5713 Mutate_Ekind (Id, E_Record_Subtype);
5715 -- Subtype declarations introduced for formal type parameters
5716 -- in generic instantiations should inherit the Size value of
5717 -- the type they rename.
5719 if Present (Generic_Parent_Type (N)) then
5720 Copy_RM_Size (To => Id, From => T);
5721 end if;
5723 if Ekind (T) = E_Record_Subtype
5724 and then Present (Cloned_Subtype (T))
5725 then
5726 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5727 else
5728 Set_Cloned_Subtype (Id, T);
5729 end if;
5731 Set_First_Entity (Id, First_Entity (T));
5732 Set_Last_Entity (Id, Last_Entity (T));
5733 Set_Has_Discriminants (Id, Has_Discriminants (T));
5734 Set_Is_Constrained (Id, Is_Constrained (T));
5735 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5736 Set_Has_Implicit_Dereference
5737 (Id, Has_Implicit_Dereference (T));
5738 Set_Has_Unknown_Discriminants
5739 (Id, Has_Unknown_Discriminants (T));
5741 if Has_Discriminants (T) then
5742 Set_Discriminant_Constraint
5743 (Id, Discriminant_Constraint (T));
5744 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5746 elsif Has_Unknown_Discriminants (Id) then
5747 Set_Discriminant_Constraint (Id, No_Elist);
5748 end if;
5750 if Is_Tagged_Type (T) then
5751 Set_Is_Tagged_Type (Id, True);
5752 Set_No_Tagged_Streams_Pragma
5753 (Id, No_Tagged_Streams_Pragma (T));
5754 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5755 Set_Direct_Primitive_Operations
5756 (Id, Direct_Primitive_Operations (T));
5757 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5759 if Is_Interface (T) then
5760 Set_Is_Interface (Id);
5761 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5762 end if;
5763 end if;
5765 when Private_Kind =>
5766 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5767 Set_Has_Discriminants (Id, Has_Discriminants (T));
5768 Set_Is_Constrained (Id, Is_Constrained (T));
5769 Set_First_Entity (Id, First_Entity (T));
5770 Set_Last_Entity (Id, Last_Entity (T));
5771 Set_Private_Dependents (Id, New_Elmt_List);
5772 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5773 Set_Has_Implicit_Dereference
5774 (Id, Has_Implicit_Dereference (T));
5775 Set_Has_Unknown_Discriminants
5776 (Id, Has_Unknown_Discriminants (T));
5777 Set_Known_To_Have_Preelab_Init
5778 (Id, Known_To_Have_Preelab_Init (T));
5780 if Is_Tagged_Type (T) then
5781 Set_Is_Tagged_Type (Id);
5782 Set_No_Tagged_Streams_Pragma (Id,
5783 No_Tagged_Streams_Pragma (T));
5784 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5785 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5786 Set_Direct_Primitive_Operations (Id,
5787 Direct_Primitive_Operations (T));
5788 end if;
5790 -- In general the attributes of the subtype of a private type
5791 -- are the attributes of the partial view of parent. However,
5792 -- the full view may be a discriminated type, and the subtype
5793 -- must share the discriminant constraint to generate correct
5794 -- calls to initialization procedures.
5796 if Has_Discriminants (T) then
5797 Set_Discriminant_Constraint
5798 (Id, Discriminant_Constraint (T));
5799 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5801 elsif Present (Full_View (T))
5802 and then Has_Discriminants (Full_View (T))
5803 then
5804 Set_Discriminant_Constraint
5805 (Id, Discriminant_Constraint (Full_View (T)));
5806 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5808 -- This would seem semantically correct, but apparently
5809 -- generates spurious errors about missing components ???
5811 -- Set_Has_Discriminants (Id);
5812 end if;
5814 Prepare_Private_Subtype_Completion (Id, N);
5816 -- If this is the subtype of a constrained private type with
5817 -- discriminants that has got a full view and we also have
5818 -- built a completion just above, show that the completion
5819 -- is a clone of the full view to the back-end.
5821 if Has_Discriminants (T)
5822 and then not Has_Unknown_Discriminants (T)
5823 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5824 and then Present (Full_View (T))
5825 and then Present (Full_View (Id))
5826 then
5827 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5828 end if;
5830 when Access_Kind =>
5831 Mutate_Ekind (Id, E_Access_Subtype);
5832 Set_Is_Constrained (Id, Is_Constrained (T));
5833 Set_Is_Access_Constant
5834 (Id, Is_Access_Constant (T));
5835 Set_Directly_Designated_Type
5836 (Id, Designated_Type (T));
5837 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5839 -- A Pure library_item must not contain the declaration of a
5840 -- named access type, except within a subprogram, generic
5841 -- subprogram, task unit, or protected unit, or if it has
5842 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5844 if Comes_From_Source (Id)
5845 and then In_Pure_Unit
5846 and then not In_Subprogram_Task_Protected_Unit
5847 and then not No_Pool_Assigned (Id)
5848 then
5849 Error_Msg_N
5850 ("named access types not allowed in pure unit", N);
5851 end if;
5853 when Concurrent_Kind =>
5854 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5855 Set_Corresponding_Record_Type (Id,
5856 Corresponding_Record_Type (T));
5857 Set_First_Entity (Id, First_Entity (T));
5858 Set_First_Private_Entity (Id, First_Private_Entity (T));
5859 Set_Has_Discriminants (Id, Has_Discriminants (T));
5860 Set_Is_Constrained (Id, Is_Constrained (T));
5861 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5862 Set_Last_Entity (Id, Last_Entity (T));
5864 if Is_Tagged_Type (T) then
5865 Set_No_Tagged_Streams_Pragma
5866 (Id, No_Tagged_Streams_Pragma (T));
5867 end if;
5869 if Has_Discriminants (T) then
5870 Set_Discriminant_Constraint
5871 (Id, Discriminant_Constraint (T));
5872 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5873 end if;
5875 when Incomplete_Kind =>
5876 if Ada_Version >= Ada_2005 then
5878 -- In Ada 2005 an incomplete type can be explicitly tagged:
5879 -- propagate indication. Note that we also have to include
5880 -- subtypes for Ada 2012 extended use of incomplete types.
5882 Mutate_Ekind (Id, E_Incomplete_Subtype);
5883 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5884 Set_Private_Dependents (Id, New_Elmt_List);
5886 if Is_Tagged_Type (Id) then
5887 Set_No_Tagged_Streams_Pragma
5888 (Id, No_Tagged_Streams_Pragma (T));
5889 end if;
5891 -- For tagged types, or when prefixed-call syntax is allowed
5892 -- for untagged types, initialize the list of primitive
5893 -- operations to an empty list.
5895 if Is_Tagged_Type (Id)
5896 or else Core_Extensions_Allowed
5897 then
5898 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5899 end if;
5901 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5902 -- incomplete type visible through a limited with clause.
5904 if From_Limited_With (T)
5905 and then Present (Non_Limited_View (T))
5906 then
5907 Set_From_Limited_With (Id);
5908 Set_Non_Limited_View (Id, Non_Limited_View (T));
5910 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5911 -- to the private dependents of the original incomplete
5912 -- type for future transformation.
5914 else
5915 Append_Elmt (Id, Private_Dependents (T));
5916 end if;
5918 -- If the subtype name denotes an incomplete type an error
5919 -- was already reported by Process_Subtype.
5921 else
5922 Set_Etype (Id, Any_Type);
5923 end if;
5925 when others =>
5926 raise Program_Error;
5927 end case;
5929 -- If there is no constraint in the subtype indication, the
5930 -- declared entity inherits predicates from the parent.
5932 Inherit_Predicate_Flags (Id, T);
5933 end if;
5935 if Etype (Id) = Any_Type then
5936 goto Leave;
5937 end if;
5939 -- When prefixed calls are enabled for untagged types, the subtype
5940 -- shares the primitive operations of its base type. Do this even
5941 -- when Extensions_Allowed is False to issue better error messages.
5943 Set_Direct_Primitive_Operations
5944 (Id, Direct_Primitive_Operations (Base_Type (T)));
5946 -- Some common processing on all types
5948 Set_Size_Info (Id, T);
5949 Set_First_Rep_Item (Id, First_Rep_Item (T));
5951 -- If the parent type is a generic actual, so is the subtype. This may
5952 -- happen in a nested instance. Why Comes_From_Source test???
5954 if not Comes_From_Source (N) then
5955 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5956 end if;
5958 -- If this is a subtype declaration for an actual in an instance,
5959 -- inherit static and dynamic predicates if any.
5961 -- If declaration has no aspect specifications, inherit predicate
5962 -- info as well. Unclear how to handle the case of both specified
5963 -- and inherited predicates ??? Other inherited aspects, such as
5964 -- invariants, should be OK, but the combination with later pragmas
5965 -- may also require special merging.
5967 if Has_Predicates (T)
5968 and then Present (Predicate_Function (T))
5969 and then
5970 ((In_Instance and then not Comes_From_Source (N))
5971 or else No (Aspect_Specifications (N)))
5972 then
5973 -- Inherit Subprograms_For_Type from the full view, if present
5975 if Present (Full_View (T))
5976 and then Present (Subprograms_For_Type (Full_View (T)))
5977 then
5978 Set_Subprograms_For_Type
5979 (Id, Subprograms_For_Type (Full_View (T)));
5980 else
5981 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5982 end if;
5984 -- If the current declaration created both a private and a full view,
5985 -- then propagate Predicate_Function to the latter as well.
5987 if Present (Full_View (Id))
5988 and then No (Predicate_Function (Full_View (Id)))
5989 then
5990 Set_Subprograms_For_Type
5991 (Full_View (Id), Subprograms_For_Type (Id));
5992 end if;
5994 if Has_Static_Predicate (T) then
5995 Set_Has_Static_Predicate (Id);
5996 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5997 end if;
5998 end if;
6000 -- If the base type is a scalar type, or else if there is no
6001 -- constraint, the atomic flag is inherited by the subtype.
6002 -- Ditto for the Independent aspect.
6004 if Is_Scalar_Type (Id)
6005 or else Is_Entity_Name (Subtype_Indication (N))
6006 then
6007 Set_Is_Atomic (Id, Is_Atomic (T));
6008 Set_Is_Independent (Id, Is_Independent (T));
6009 end if;
6011 -- Remaining processing depends on characteristics of base type
6013 T := Etype (Id);
6015 Set_Is_Immediately_Visible (Id, True);
6016 Set_Depends_On_Private (Id, Has_Private_Component (T));
6017 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6019 if Is_Interface (T) then
6020 Set_Is_Interface (Id);
6021 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6022 end if;
6024 if Present (Generic_Parent_Type (N))
6025 and then
6026 (Nkind (Parent (Generic_Parent_Type (N))) /=
6027 N_Formal_Type_Declaration
6028 or else Nkind (Formal_Type_Definition
6029 (Parent (Generic_Parent_Type (N)))) /=
6030 N_Formal_Private_Type_Definition)
6031 then
6032 if Is_Tagged_Type (Id) then
6034 -- If this is a generic actual subtype for a synchronized type,
6035 -- the primitive operations are those of the corresponding record
6036 -- for which there is a separate subtype declaration.
6038 if Is_Concurrent_Type (Id) then
6039 null;
6040 elsif Is_Class_Wide_Type (Id) then
6041 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6042 else
6043 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6044 end if;
6046 elsif Scope (Etype (Id)) /= Standard_Standard then
6047 Derive_Subprograms (Generic_Parent_Type (N), Id);
6048 end if;
6049 end if;
6051 if Is_Private_Type (T) and then Present (Full_View (T)) then
6052 Conditional_Delay (Id, Full_View (T));
6054 -- The subtypes of components or subcomponents of protected types
6055 -- do not need freeze nodes, which would otherwise appear in the
6056 -- wrong scope (before the freeze node for the protected type). The
6057 -- proper subtypes are those of the subcomponents of the corresponding
6058 -- record.
6060 elsif Ekind (Scope (Id)) /= E_Protected_Type
6061 and then Present (Scope (Scope (Id))) -- error defense
6062 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6063 then
6064 Conditional_Delay (Id, T);
6065 end if;
6067 -- If we have a subtype of an incomplete type whose full type is a
6068 -- derived numeric type, we need to have a freeze node for the subtype.
6069 -- Otherwise gigi will complain while computing the (static) bounds of
6070 -- the subtype.
6072 if Is_Itype (T)
6073 and then Is_Elementary_Type (Id)
6074 and then Etype (Id) /= Id
6075 then
6076 declare
6077 Partial : constant Entity_Id :=
6078 Incomplete_Or_Partial_View (First_Subtype (Id));
6079 begin
6080 if Present (Partial)
6081 and then Ekind (Partial) = E_Incomplete_Type
6082 then
6083 Set_Has_Delayed_Freeze (Id);
6084 end if;
6085 end;
6086 end if;
6088 -- Check that Constraint_Error is raised for a scalar subtype indication
6089 -- when the lower or upper bound of a non-null range lies outside the
6090 -- range of the type mark. Likewise for an array subtype, but check the
6091 -- compatibility for each index.
6093 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6094 declare
6095 Indic_Typ : constant Entity_Id :=
6096 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6097 Subt_Index : Node_Id;
6098 Target_Index : Node_Id;
6100 begin
6101 if Is_Scalar_Type (Etype (Id))
6102 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6103 then
6104 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6106 elsif Is_Array_Type (Etype (Id))
6107 and then Present (First_Index (Id))
6108 then
6109 Subt_Index := First_Index (Id);
6110 Target_Index := First_Index (Indic_Typ);
6112 while Present (Subt_Index) loop
6113 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6114 and then Is_Scalar_Type (Entity (Subt_Index)))
6115 or else Nkind (Subt_Index) = N_Subtype_Indication)
6116 and then
6117 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6118 then
6119 Apply_Range_Check
6120 (Scalar_Range (Etype (Subt_Index)),
6121 Etype (Target_Index),
6122 Insert_Node => N);
6123 end if;
6125 Next_Index (Subt_Index);
6126 Next_Index (Target_Index);
6127 end loop;
6128 end if;
6129 end;
6130 end if;
6132 Set_Optimize_Alignment_Flags (Id);
6133 Check_Eliminated (Id);
6135 <<Leave>>
6136 if Has_Aspects (N) then
6137 Analyze_Aspect_Specifications (N, Id);
6138 end if;
6140 Analyze_Dimension (N);
6142 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6143 -- indications on composite types where the constraints are dynamic.
6144 -- Note that object declarations and aggregates generate implicit
6145 -- subtype declarations, which this covers. One special case is that the
6146 -- implicitly generated "=" for discriminated types includes an
6147 -- offending subtype declaration, which is harmless, so we ignore it
6148 -- here.
6150 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6151 declare
6152 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6153 begin
6154 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6155 and then not (Is_Internal (Id)
6156 and then Is_TSS (Scope (Id),
6157 TSS_Composite_Equality))
6158 and then not Within_Init_Proc
6159 and then not All_Composite_Constraints_Static (Cstr)
6160 then
6161 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6162 end if;
6163 end;
6164 end if;
6165 end Analyze_Subtype_Declaration;
6167 --------------------------------
6168 -- Analyze_Subtype_Indication --
6169 --------------------------------
6171 procedure Analyze_Subtype_Indication (N : Node_Id) is
6172 T : constant Entity_Id := Subtype_Mark (N);
6173 R : constant Node_Id := Range_Expression (Constraint (N));
6175 begin
6176 Analyze (T);
6178 if R = Error then
6179 Set_Error_Posted (R);
6180 Set_Error_Posted (T);
6181 else
6182 Analyze (R);
6183 Set_Etype (N, Etype (R));
6184 Resolve (R, Entity (T));
6185 end if;
6186 end Analyze_Subtype_Indication;
6188 --------------------------
6189 -- Analyze_Variant_Part --
6190 --------------------------
6192 procedure Analyze_Variant_Part (N : Node_Id) is
6193 Discr_Name : Node_Id;
6194 Discr_Type : Entity_Id;
6196 procedure Process_Variant (A : Node_Id);
6197 -- Analyze declarations for a single variant
6199 package Analyze_Variant_Choices is
6200 new Generic_Analyze_Choices (Process_Variant);
6201 use Analyze_Variant_Choices;
6203 ---------------------
6204 -- Process_Variant --
6205 ---------------------
6207 procedure Process_Variant (A : Node_Id) is
6208 CL : constant Node_Id := Component_List (A);
6209 begin
6210 if not Null_Present (CL) then
6211 Analyze_Declarations (Component_Items (CL));
6213 if Present (Variant_Part (CL)) then
6214 Analyze (Variant_Part (CL));
6215 end if;
6216 end if;
6217 end Process_Variant;
6219 -- Start of processing for Analyze_Variant_Part
6221 begin
6222 Discr_Name := Name (N);
6223 Analyze (Discr_Name);
6225 -- If Discr_Name bad, get out (prevent cascaded errors)
6227 if Etype (Discr_Name) = Any_Type then
6228 return;
6229 end if;
6231 -- Check invalid discriminant in variant part
6233 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6234 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6235 end if;
6237 Discr_Type := Etype (Entity (Discr_Name));
6239 if not Is_Discrete_Type (Discr_Type) then
6240 Error_Msg_N
6241 ("discriminant in a variant part must be of a discrete type",
6242 Name (N));
6243 return;
6244 end if;
6246 -- Now analyze the choices, which also analyzes the declarations that
6247 -- are associated with each choice.
6249 Analyze_Choices (Variants (N), Discr_Type);
6251 -- Note: we used to instantiate and call Check_Choices here to check
6252 -- that the choices covered the discriminant, but it's too early to do
6253 -- that because of statically predicated subtypes, whose analysis may
6254 -- be deferred to their freeze point which may be as late as the freeze
6255 -- point of the containing record. So this call is now to be found in
6256 -- Freeze_Record_Declaration.
6258 end Analyze_Variant_Part;
6260 ----------------------------
6261 -- Array_Type_Declaration --
6262 ----------------------------
6264 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6265 Component_Def : constant Node_Id := Component_Definition (Def);
6266 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6267 P : constant Node_Id := Parent (Def);
6268 Element_Type : Entity_Id;
6269 Implicit_Base : Entity_Id;
6270 Index : Node_Id;
6271 Nb_Index : Pos;
6272 Priv : Entity_Id;
6273 Related_Id : Entity_Id;
6274 Has_FLB_Index : Boolean := False;
6276 begin
6277 if Nkind (Def) = N_Constrained_Array_Definition then
6278 Index := First (Discrete_Subtype_Definitions (Def));
6279 else
6280 Index := First (Subtype_Marks (Def));
6281 end if;
6283 -- Find proper names for the implicit types which may be public. In case
6284 -- of anonymous arrays we use the name of the first object of that type
6285 -- as prefix.
6287 if No (T) then
6288 Related_Id := Defining_Identifier (P);
6289 else
6290 Related_Id := T;
6291 end if;
6293 Nb_Index := 1;
6294 while Present (Index) loop
6295 Analyze (Index);
6297 -- Test for odd case of trying to index a type by the type itself
6299 if Is_Entity_Name (Index) and then Entity (Index) = T then
6300 Error_Msg_N ("type& cannot be indexed by itself", Index);
6301 Set_Entity (Index, Standard_Boolean);
6302 Set_Etype (Index, Standard_Boolean);
6303 end if;
6305 -- Add a subtype declaration for each index of private array type
6306 -- declaration whose type is also private. For example:
6308 -- package Pkg is
6309 -- type Index is private;
6310 -- private
6311 -- type Table is array (Index) of ...
6312 -- end;
6314 -- This is currently required by the expander for the internally
6315 -- generated equality subprogram of records with variant parts in
6316 -- which the type of some component is such a private type. And it
6317 -- also helps semantic analysis in peculiar cases where the array
6318 -- type is referenced from an instance but not the index directly.
6320 if Is_Package_Or_Generic_Package (Current_Scope)
6321 and then In_Private_Part (Current_Scope)
6322 and then Has_Private_Declaration (Etype (Index))
6323 and then Scope (Etype (Index)) = Current_Scope
6324 then
6325 declare
6326 Loc : constant Source_Ptr := Sloc (Def);
6327 Decl : Node_Id;
6328 New_E : Entity_Id;
6330 begin
6331 New_E := Make_Temporary (Loc, 'T');
6332 Set_Is_Internal (New_E);
6334 Decl :=
6335 Make_Subtype_Declaration (Loc,
6336 Defining_Identifier => New_E,
6337 Subtype_Indication =>
6338 New_Occurrence_Of (Etype (Index), Loc));
6340 Insert_Before (Parent (Def), Decl);
6341 Analyze (Decl);
6342 Set_Etype (Index, New_E);
6344 -- If the index is a range or a subtype indication it carries
6345 -- no entity. Example:
6347 -- package Pkg is
6348 -- type T is private;
6349 -- private
6350 -- type T is new Natural;
6351 -- Table : array (T(1) .. T(10)) of Boolean;
6352 -- end Pkg;
6354 -- Otherwise the type of the reference is its entity.
6356 if Is_Entity_Name (Index) then
6357 Set_Entity (Index, New_E);
6358 end if;
6359 end;
6360 end if;
6362 Make_Index (Index, P, Related_Id, Nb_Index);
6364 -- In the case where we have an unconstrained array with an index
6365 -- given by a subtype_indication, this is necessarily a "fixed lower
6366 -- bound" index. We change the upper bound of that index to the upper
6367 -- bound of the index's subtype (denoted by the subtype_mark), since
6368 -- that upper bound was originally set by the parser to be the same
6369 -- as the lower bound. In truth, that upper bound corresponds to
6370 -- a box ("<>"), and could be set to Empty, but it's convenient to
6371 -- set it to the upper bound to avoid needing to add special tests
6372 -- in various places for an Empty upper bound, and in any case that
6373 -- accurately characterizes the index's range of values.
6375 if Nkind (Def) = N_Unconstrained_Array_Definition
6376 and then Nkind (Index) = N_Subtype_Indication
6377 then
6378 declare
6379 Index_Subtype_High_Bound : constant Entity_Id :=
6380 Type_High_Bound (Entity (Subtype_Mark (Index)));
6381 begin
6382 Set_High_Bound (Range_Expression (Constraint (Index)),
6383 Index_Subtype_High_Bound);
6385 -- Record that the array type has one or more indexes with
6386 -- a fixed lower bound.
6388 Has_FLB_Index := True;
6390 -- Mark the index as belonging to an array type with a fixed
6391 -- lower bound.
6393 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6394 end;
6395 end if;
6397 -- Check error of subtype with predicate for index type
6399 Bad_Predicated_Subtype_Use
6400 ("subtype& has predicate, not allowed as index subtype",
6401 Index, Etype (Index));
6403 -- Move to next index
6405 Next (Index);
6406 Nb_Index := Nb_Index + 1;
6407 end loop;
6409 -- Process subtype indication if one is present
6411 if Present (Component_Typ) then
6412 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6413 Set_Etype (Component_Typ, Element_Type);
6415 -- Ada 2005 (AI-230): Access Definition case
6417 else pragma Assert (Present (Access_Definition (Component_Def)));
6419 -- Indicate that the anonymous access type is created by the
6420 -- array type declaration.
6422 Element_Type := Access_Definition
6423 (Related_Nod => P,
6424 N => Access_Definition (Component_Def));
6425 Set_Is_Local_Anonymous_Access (Element_Type);
6427 -- Propagate the parent. This field is needed if we have to generate
6428 -- the master_id associated with an anonymous access to task type
6429 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6431 Copy_Parent (To => Element_Type, From => T);
6433 -- Ada 2005 (AI-230): In case of components that are anonymous access
6434 -- types the level of accessibility depends on the enclosing type
6435 -- declaration
6437 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6439 -- Ada 2005 (AI-254)
6441 declare
6442 CD : constant Node_Id :=
6443 Access_To_Subprogram_Definition
6444 (Access_Definition (Component_Def));
6445 begin
6446 if Present (CD) and then Protected_Present (CD) then
6447 Element_Type :=
6448 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6449 end if;
6450 end;
6451 end if;
6453 -- Constrained array case
6455 if No (T) then
6456 -- We might be creating more than one itype with the same Related_Id,
6457 -- e.g. for an array object definition and its initial value. Give
6458 -- them unique suffixes, because GNATprove require distinct types to
6459 -- have different names.
6461 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6462 end if;
6464 if Nkind (Def) = N_Constrained_Array_Definition then
6466 if Ekind (T) in Incomplete_Or_Private_Kind then
6467 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6468 else
6469 pragma Assert (Ekind (T) = E_Void);
6470 end if;
6472 -- Establish Implicit_Base as unconstrained base type
6474 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6476 Set_Etype (Implicit_Base, Implicit_Base);
6477 Set_Scope (Implicit_Base, Current_Scope);
6478 Set_Has_Delayed_Freeze (Implicit_Base);
6479 Set_Default_SSO (Implicit_Base);
6481 -- The constrained array type is a subtype of the unconstrained one
6483 Mutate_Ekind (T, E_Array_Subtype);
6484 Reinit_Size_Align (T);
6485 Set_Etype (T, Implicit_Base);
6486 Set_Scope (T, Current_Scope);
6487 Set_Is_Constrained (T);
6488 Set_First_Index (T,
6489 First (Discrete_Subtype_Definitions (Def)));
6490 Set_Has_Delayed_Freeze (T);
6492 -- Complete setup of implicit base type
6494 pragma Assert (not Known_Component_Size (Implicit_Base));
6495 Set_Component_Type (Implicit_Base, Element_Type);
6496 Set_Finalize_Storage_Only
6497 (Implicit_Base,
6498 Finalize_Storage_Only (Element_Type));
6499 Set_First_Index (Implicit_Base, First_Index (T));
6500 Set_Has_Controlled_Component
6501 (Implicit_Base,
6502 Has_Controlled_Component (Element_Type)
6503 or else Is_Controlled (Element_Type));
6504 Set_Packed_Array_Impl_Type
6505 (Implicit_Base, Empty);
6507 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6509 -- Unconstrained array case
6511 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6513 if Ekind (T) in Incomplete_Or_Private_Kind then
6514 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6515 else
6516 pragma Assert (Ekind (T) = E_Void);
6517 end if;
6519 Mutate_Ekind (T, E_Array_Type);
6520 Reinit_Size_Align (T);
6521 Set_Etype (T, T);
6522 Set_Scope (T, Current_Scope);
6523 pragma Assert (not Known_Component_Size (T));
6524 Set_Is_Constrained (T, False);
6525 Set_Is_Fixed_Lower_Bound_Array_Subtype
6526 (T, Has_FLB_Index);
6527 Set_First_Index (T, First (Subtype_Marks (Def)));
6528 Set_Has_Delayed_Freeze (T, True);
6529 Propagate_Concurrent_Flags (T, Element_Type);
6530 Set_Has_Controlled_Component (T, Has_Controlled_Component
6531 (Element_Type)
6532 or else
6533 Is_Controlled (Element_Type));
6534 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6535 (Element_Type));
6536 Set_Default_SSO (T);
6537 end if;
6539 -- Common attributes for both cases
6541 Set_Component_Type (Base_Type (T), Element_Type);
6542 Set_Packed_Array_Impl_Type (T, Empty);
6544 if Aliased_Present (Component_Definition (Def)) then
6545 Set_Has_Aliased_Components (Etype (T));
6547 -- AI12-001: All aliased objects are considered to be specified as
6548 -- independently addressable (RM C.6(8.1/4)).
6550 Set_Has_Independent_Components (Etype (T));
6551 end if;
6553 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6554 -- array type to ensure that objects of this type are initialized.
6556 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6557 Set_Can_Never_Be_Null (T);
6559 if Null_Exclusion_Present (Component_Definition (Def))
6561 -- No need to check itypes because in their case this check was
6562 -- done at their point of creation
6564 and then not Is_Itype (Element_Type)
6565 then
6566 Error_Msg_N
6567 ("`NOT NULL` not allowed (null already excluded)",
6568 Subtype_Indication (Component_Definition (Def)));
6569 end if;
6570 end if;
6572 Priv := Private_Component (Element_Type);
6574 if Present (Priv) then
6576 -- Check for circular definitions
6578 if Priv = Any_Type then
6579 Set_Component_Type (Etype (T), Any_Type);
6581 -- There is a gap in the visibility of operations on the composite
6582 -- type only if the component type is defined in a different scope.
6584 elsif Scope (Priv) = Current_Scope then
6585 null;
6587 elsif Is_Limited_Type (Priv) then
6588 Set_Is_Limited_Composite (Etype (T));
6589 Set_Is_Limited_Composite (T);
6590 else
6591 Set_Is_Private_Composite (Etype (T));
6592 Set_Is_Private_Composite (T);
6593 end if;
6594 end if;
6596 -- A syntax error in the declaration itself may lead to an empty index
6597 -- list, in which case do a minimal patch.
6599 if No (First_Index (T)) then
6600 Error_Msg_N ("missing index definition in array type declaration", T);
6602 declare
6603 Indexes : constant List_Id :=
6604 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6605 begin
6606 Set_Discrete_Subtype_Definitions (Def, Indexes);
6607 Set_First_Index (T, First (Indexes));
6608 return;
6609 end;
6610 end if;
6612 -- Create a concatenation operator for the new type. Internal array
6613 -- types created for packed entities do not need such, they are
6614 -- compatible with the user-defined type.
6616 if Number_Dimensions (T) = 1
6617 and then not Is_Packed_Array_Impl_Type (T)
6618 then
6619 New_Concatenation_Op (T);
6620 end if;
6622 -- In the case of an unconstrained array the parser has already verified
6623 -- that all the indexes are unconstrained but we still need to make sure
6624 -- that the element type is constrained.
6626 if not Is_Definite_Subtype (Element_Type) then
6627 Error_Msg_N
6628 ("unconstrained element type in array declaration",
6629 Subtype_Indication (Component_Def));
6631 elsif Is_Abstract_Type (Element_Type) then
6632 Error_Msg_N
6633 ("the type of a component cannot be abstract",
6634 Subtype_Indication (Component_Def));
6635 end if;
6637 -- There may be an invariant declared for the component type, but
6638 -- the construction of the component invariant checking procedure
6639 -- takes place during expansion.
6640 end Array_Type_Declaration;
6642 ------------------------------------------------------
6643 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6644 ------------------------------------------------------
6646 function Replace_Anonymous_Access_To_Protected_Subprogram
6647 (N : Node_Id) return Entity_Id
6649 Loc : constant Source_Ptr := Sloc (N);
6651 Curr_Scope : constant Scope_Stack_Entry :=
6652 Scope_Stack.Table (Scope_Stack.Last);
6654 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6656 Acc : Node_Id;
6657 -- Access definition in declaration
6659 Comp : Node_Id;
6660 -- Object definition or formal definition with an access definition
6662 Decl : Node_Id;
6663 -- Declaration of anonymous access to subprogram type
6665 Spec : Node_Id;
6666 -- Original specification in access to subprogram
6668 P : Node_Id;
6670 begin
6671 Set_Is_Internal (Anon);
6673 case Nkind (N) is
6674 when N_Constrained_Array_Definition
6675 | N_Component_Declaration
6676 | N_Unconstrained_Array_Definition
6678 Comp := Component_Definition (N);
6679 Acc := Access_Definition (Comp);
6681 when N_Discriminant_Specification =>
6682 Comp := Discriminant_Type (N);
6683 Acc := Comp;
6685 when N_Parameter_Specification =>
6686 Comp := Parameter_Type (N);
6687 Acc := Comp;
6689 when N_Access_Function_Definition =>
6690 Comp := Result_Definition (N);
6691 Acc := Comp;
6693 when N_Object_Declaration =>
6694 Comp := Object_Definition (N);
6695 Acc := Comp;
6697 when N_Function_Specification =>
6698 Comp := Result_Definition (N);
6699 Acc := Comp;
6701 when others =>
6702 raise Program_Error;
6703 end case;
6705 Spec := Access_To_Subprogram_Definition (Acc);
6707 Decl :=
6708 Make_Full_Type_Declaration (Loc,
6709 Defining_Identifier => Anon,
6710 Type_Definition => Copy_Separate_Tree (Spec));
6712 Mark_Rewrite_Insertion (Decl);
6714 -- Insert the new declaration in the nearest enclosing scope. If the
6715 -- parent is a body and N is its return type, the declaration belongs
6716 -- in the enclosing scope. Likewise if N is the type of a parameter.
6718 P := Parent (N);
6720 if Nkind (N) = N_Function_Specification
6721 and then Nkind (P) = N_Subprogram_Body
6722 then
6723 P := Parent (P);
6724 elsif Nkind (N) = N_Parameter_Specification
6725 and then Nkind (P) in N_Subprogram_Specification
6726 and then Nkind (Parent (P)) = N_Subprogram_Body
6727 then
6728 P := Parent (Parent (P));
6729 end if;
6731 while Present (P) and then not Has_Declarations (P) loop
6732 P := Parent (P);
6733 end loop;
6735 pragma Assert (Present (P));
6737 if Nkind (P) = N_Package_Specification then
6738 Prepend (Decl, Visible_Declarations (P));
6739 else
6740 Prepend (Decl, Declarations (P));
6741 end if;
6743 -- Replace the anonymous type with an occurrence of the new declaration.
6744 -- In all cases the rewritten node does not have the null-exclusion
6745 -- attribute because (if present) it was already inherited by the
6746 -- anonymous entity (Anon). Thus, in case of components we do not
6747 -- inherit this attribute.
6749 if Nkind (N) = N_Parameter_Specification then
6750 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6751 Set_Etype (Defining_Identifier (N), Anon);
6752 Set_Null_Exclusion_Present (N, False);
6754 elsif Nkind (N) = N_Object_Declaration then
6755 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6756 Set_Etype (Defining_Identifier (N), Anon);
6758 elsif Nkind (N) = N_Access_Function_Definition then
6759 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6761 elsif Nkind (N) = N_Function_Specification then
6762 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6763 Set_Etype (Defining_Unit_Name (N), Anon);
6765 else
6766 Rewrite (Comp,
6767 Make_Component_Definition (Loc,
6768 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6769 end if;
6771 Mark_Rewrite_Insertion (Comp);
6773 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6774 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6775 and then not Is_Type (Current_Scope))
6776 then
6778 -- Declaration can be analyzed in the current scope.
6780 Analyze (Decl);
6782 else
6783 -- Temporarily remove the current scope (record or subprogram) from
6784 -- the stack to add the new declarations to the enclosing scope.
6785 -- The anonymous entity is an Itype with the proper attributes.
6787 Scope_Stack.Decrement_Last;
6788 Analyze (Decl);
6789 Set_Is_Itype (Anon);
6790 Set_Associated_Node_For_Itype (Anon, N);
6791 Scope_Stack.Append (Curr_Scope);
6792 end if;
6794 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6795 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6796 return Anon;
6797 end Replace_Anonymous_Access_To_Protected_Subprogram;
6799 -------------------------------------
6800 -- Build_Access_Subprogram_Wrapper --
6801 -------------------------------------
6803 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6804 Loc : constant Source_Ptr := Sloc (Decl);
6805 Id : constant Entity_Id := Defining_Identifier (Decl);
6806 Type_Def : constant Node_Id := Type_Definition (Decl);
6807 Specs : constant List_Id :=
6808 Parameter_Specifications (Type_Def);
6809 Profile : constant List_Id := New_List;
6810 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6812 Contracts : constant List_Id := New_List;
6813 Form_P : Node_Id;
6814 New_P : Node_Id;
6815 New_Decl : Node_Id;
6816 Spec : Node_Id;
6818 procedure Replace_Type_Name (Expr : Node_Id);
6819 -- In the expressions for contract aspects, replace occurrences of the
6820 -- access type with the name of the subprogram entity, as needed, e.g.
6821 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6822 -- remain on the original access type declaration. What about expanded
6823 -- names denoting formals, whose prefix in source is the type name ???
6825 -----------------------
6826 -- Replace_Type_Name --
6827 -----------------------
6829 procedure Replace_Type_Name (Expr : Node_Id) is
6830 function Process (N : Node_Id) return Traverse_Result;
6831 function Process (N : Node_Id) return Traverse_Result is
6832 begin
6833 if Nkind (N) = N_Attribute_Reference
6834 and then Is_Entity_Name (Prefix (N))
6835 and then Chars (Prefix (N)) = Chars (Id)
6836 then
6837 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6838 end if;
6840 return OK;
6841 end Process;
6843 procedure Traverse is new Traverse_Proc (Process);
6844 begin
6845 Traverse (Expr);
6846 end Replace_Type_Name;
6848 begin
6849 if Ekind (Id) in E_Access_Subprogram_Type
6850 | E_Access_Protected_Subprogram_Type
6851 | E_Anonymous_Access_Protected_Subprogram_Type
6852 | E_Anonymous_Access_Subprogram_Type
6853 then
6854 null;
6856 else
6857 Error_Msg_N
6858 ("illegal pre/postcondition on access type", Decl);
6859 return;
6860 end if;
6862 declare
6863 Asp : Node_Id;
6864 A_Id : Aspect_Id;
6865 Cond : Node_Id;
6866 Expr : Node_Id;
6868 begin
6869 Asp := First (Aspect_Specifications (Decl));
6870 while Present (Asp) loop
6871 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6872 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6873 Cond := Asp;
6874 Expr := Expression (Cond);
6875 Replace_Type_Name (Expr);
6876 Next (Asp);
6878 Remove (Cond);
6879 Append (Cond, Contracts);
6881 else
6882 Next (Asp);
6883 end if;
6884 end loop;
6885 end;
6887 -- If there are no contract aspects, no need for a wrapper.
6889 if Is_Empty_List (Contracts) then
6890 return;
6891 end if;
6893 Form_P := First (Specs);
6895 while Present (Form_P) loop
6896 New_P := New_Copy_Tree (Form_P);
6897 Set_Defining_Identifier (New_P,
6898 Make_Defining_Identifier
6899 (Loc, Chars (Defining_Identifier (Form_P))));
6900 Append (New_P, Profile);
6901 Next (Form_P);
6902 end loop;
6904 -- Add to parameter specifications the access parameter that is passed
6905 -- in from an indirect call.
6907 Append (
6908 Make_Parameter_Specification (Loc,
6909 Defining_Identifier => Make_Temporary (Loc, 'P'),
6910 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6911 Profile);
6913 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6914 Spec :=
6915 Make_Procedure_Specification (Loc,
6916 Defining_Unit_Name => Subp,
6917 Parameter_Specifications => Profile);
6918 Mutate_Ekind (Subp, E_Procedure);
6919 else
6920 Spec :=
6921 Make_Function_Specification (Loc,
6922 Defining_Unit_Name => Subp,
6923 Parameter_Specifications => Profile,
6924 Result_Definition =>
6925 New_Copy_Tree
6926 (Result_Definition (Type_Definition (Decl))));
6927 Mutate_Ekind (Subp, E_Function);
6928 end if;
6930 New_Decl :=
6931 Make_Subprogram_Declaration (Loc, Specification => Spec);
6932 Set_Aspect_Specifications (New_Decl, Contracts);
6933 Set_Is_Wrapper (Subp);
6935 -- The wrapper is declared in the freezing actions to facilitate its
6936 -- identification and thus avoid handling it as a primitive operation
6937 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
6938 -- may be handled as a dispatching operation and erroneously registered
6939 -- in a dispatch table.
6941 if not GNATprove_Mode then
6942 Append_Freeze_Action (Id, New_Decl);
6944 -- Under GNATprove mode there is no such problem but we do not declare
6945 -- it in the freezing actions since they are not analyzed under this
6946 -- mode.
6948 else
6949 Insert_After (Decl, New_Decl);
6950 end if;
6952 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6953 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6954 end Build_Access_Subprogram_Wrapper;
6956 -------------------------------
6957 -- Build_Derived_Access_Type --
6958 -------------------------------
6960 procedure Build_Derived_Access_Type
6961 (N : Node_Id;
6962 Parent_Type : Entity_Id;
6963 Derived_Type : Entity_Id)
6965 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6967 Desig_Type : Entity_Id;
6968 Discr : Entity_Id;
6969 Discr_Con_Elist : Elist_Id;
6970 Discr_Con_El : Elmt_Id;
6971 Subt : Entity_Id;
6973 begin
6974 -- Set the designated type so it is available in case this is an access
6975 -- to a self-referential type, e.g. a standard list type with a next
6976 -- pointer. Will be reset after subtype is built.
6978 Set_Directly_Designated_Type
6979 (Derived_Type, Designated_Type (Parent_Type));
6981 Subt := Process_Subtype (S, N);
6983 if Nkind (S) /= N_Subtype_Indication
6984 and then Subt /= Base_Type (Subt)
6985 then
6986 Mutate_Ekind (Derived_Type, E_Access_Subtype);
6987 end if;
6989 if Ekind (Derived_Type) = E_Access_Subtype then
6990 declare
6991 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6992 Ibase : constant Entity_Id :=
6993 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6994 Svg_Chars : constant Name_Id := Chars (Ibase);
6995 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6996 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6998 begin
6999 Copy_Node (Pbase, Ibase);
7001 -- Restore Itype status after Copy_Node
7003 Set_Is_Itype (Ibase);
7004 Set_Associated_Node_For_Itype (Ibase, N);
7006 Set_Chars (Ibase, Svg_Chars);
7007 Set_Prev_Entity (Ibase, Svg_Prev_E);
7008 Set_Next_Entity (Ibase, Svg_Next_E);
7009 Set_Sloc (Ibase, Sloc (Derived_Type));
7010 Set_Scope (Ibase, Scope (Derived_Type));
7011 Set_Freeze_Node (Ibase, Empty);
7012 Set_Is_Frozen (Ibase, False);
7013 Set_Comes_From_Source (Ibase, False);
7014 Set_Is_First_Subtype (Ibase, False);
7016 Set_Etype (Ibase, Pbase);
7017 Set_Etype (Derived_Type, Ibase);
7018 end;
7019 end if;
7021 Set_Directly_Designated_Type
7022 (Derived_Type, Designated_Type (Subt));
7024 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7025 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7026 Set_Size_Info (Derived_Type, Parent_Type);
7027 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7028 Set_Depends_On_Private (Derived_Type,
7029 Has_Private_Component (Derived_Type));
7030 Conditional_Delay (Derived_Type, Subt);
7032 if Is_Access_Subprogram_Type (Derived_Type)
7033 and then Is_Base_Type (Derived_Type)
7034 then
7035 Set_Can_Use_Internal_Rep
7036 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7037 end if;
7039 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7040 -- that it is not redundant.
7042 if Null_Exclusion_Present (Type_Definition (N)) then
7043 Set_Can_Never_Be_Null (Derived_Type);
7045 elsif Can_Never_Be_Null (Parent_Type) then
7046 Set_Can_Never_Be_Null (Derived_Type);
7047 end if;
7049 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7050 -- the root type for this information.
7052 -- Apply range checks to discriminants for derived record case
7053 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7055 Desig_Type := Designated_Type (Derived_Type);
7057 if Is_Composite_Type (Desig_Type)
7058 and then (not Is_Array_Type (Desig_Type))
7059 and then Has_Discriminants (Desig_Type)
7060 and then Base_Type (Desig_Type) /= Desig_Type
7061 then
7062 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7063 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7065 Discr := First_Discriminant (Base_Type (Desig_Type));
7066 while Present (Discr_Con_El) loop
7067 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7068 Next_Elmt (Discr_Con_El);
7069 Next_Discriminant (Discr);
7070 end loop;
7071 end if;
7072 end Build_Derived_Access_Type;
7074 ------------------------------
7075 -- Build_Derived_Array_Type --
7076 ------------------------------
7078 procedure Build_Derived_Array_Type
7079 (N : Node_Id;
7080 Parent_Type : Entity_Id;
7081 Derived_Type : Entity_Id)
7083 Loc : constant Source_Ptr := Sloc (N);
7084 Tdef : constant Node_Id := Type_Definition (N);
7085 Indic : constant Node_Id := Subtype_Indication (Tdef);
7086 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7087 Implicit_Base : Entity_Id := Empty;
7088 New_Indic : Node_Id;
7090 procedure Make_Implicit_Base;
7091 -- If the parent subtype is constrained, the derived type is a subtype
7092 -- of an implicit base type derived from the parent base.
7094 ------------------------
7095 -- Make_Implicit_Base --
7096 ------------------------
7098 procedure Make_Implicit_Base is
7099 begin
7100 Implicit_Base :=
7101 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7103 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7104 Set_Etype (Implicit_Base, Parent_Base);
7106 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7107 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7109 Set_Has_Delayed_Freeze (Implicit_Base, True);
7110 end Make_Implicit_Base;
7112 -- Start of processing for Build_Derived_Array_Type
7114 begin
7115 if not Is_Constrained (Parent_Type) then
7116 if Nkind (Indic) /= N_Subtype_Indication then
7117 Mutate_Ekind (Derived_Type, E_Array_Type);
7119 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7120 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7122 Set_Has_Delayed_Freeze (Derived_Type, True);
7124 else
7125 Make_Implicit_Base;
7126 Set_Etype (Derived_Type, Implicit_Base);
7128 New_Indic :=
7129 Make_Subtype_Declaration (Loc,
7130 Defining_Identifier => Derived_Type,
7131 Subtype_Indication =>
7132 Make_Subtype_Indication (Loc,
7133 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7134 Constraint => Constraint (Indic)));
7136 Rewrite (N, New_Indic);
7137 Analyze (N);
7138 end if;
7140 else
7141 if Nkind (Indic) /= N_Subtype_Indication then
7142 Make_Implicit_Base;
7144 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7145 Set_Etype (Derived_Type, Implicit_Base);
7146 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7148 else
7149 Error_Msg_N ("illegal constraint on constrained type", Indic);
7150 end if;
7151 end if;
7153 -- If parent type is not a derived type itself, and is declared in
7154 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7155 -- the new type's concatenation operator since Derive_Subprograms
7156 -- will not inherit the parent's operator. If the parent type is
7157 -- unconstrained, the operator is of the unconstrained base type.
7159 if Number_Dimensions (Parent_Type) = 1
7160 and then not Is_Limited_Type (Parent_Type)
7161 and then not Is_Derived_Type (Parent_Type)
7162 and then not Is_Package_Or_Generic_Package
7163 (Scope (Base_Type (Parent_Type)))
7164 then
7165 if not Is_Constrained (Parent_Type)
7166 and then Is_Constrained (Derived_Type)
7167 then
7168 New_Concatenation_Op (Implicit_Base);
7169 else
7170 New_Concatenation_Op (Derived_Type);
7171 end if;
7172 end if;
7173 end Build_Derived_Array_Type;
7175 -----------------------------------
7176 -- Build_Derived_Concurrent_Type --
7177 -----------------------------------
7179 procedure Build_Derived_Concurrent_Type
7180 (N : Node_Id;
7181 Parent_Type : Entity_Id;
7182 Derived_Type : Entity_Id)
7184 Loc : constant Source_Ptr := Sloc (N);
7185 Def : constant Node_Id := Type_Definition (N);
7186 Indic : constant Node_Id := Subtype_Indication (Def);
7188 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7189 Corr_Decl : Node_Id := Empty;
7190 Corr_Decl_Needed : Boolean;
7191 -- If the derived type has fewer discriminants than its parent, the
7192 -- corresponding record is also a derived type, in order to account for
7193 -- the bound discriminants. We create a full type declaration for it in
7194 -- this case.
7196 Constraint_Present : constant Boolean :=
7197 Nkind (Indic) = N_Subtype_Indication;
7199 D_Constraint : Node_Id;
7200 New_Constraint : Elist_Id := No_Elist;
7201 Old_Disc : Entity_Id;
7202 New_Disc : Entity_Id;
7203 New_N : Node_Id;
7205 begin
7206 Set_Stored_Constraint (Derived_Type, No_Elist);
7207 Corr_Decl_Needed := False;
7208 Old_Disc := Empty;
7210 if Present (Discriminant_Specifications (N))
7211 and then Constraint_Present
7212 then
7213 Old_Disc := First_Discriminant (Parent_Type);
7214 New_Disc := First (Discriminant_Specifications (N));
7215 while Present (New_Disc) and then Present (Old_Disc) loop
7216 Next_Discriminant (Old_Disc);
7217 Next (New_Disc);
7218 end loop;
7219 end if;
7221 if Present (Old_Disc) and then Expander_Active then
7223 -- The new type has fewer discriminants, so we need to create a new
7224 -- corresponding record, which is derived from the corresponding
7225 -- record of the parent, and has a stored constraint that captures
7226 -- the values of the discriminant constraints. The corresponding
7227 -- record is needed only if expander is active and code generation is
7228 -- enabled.
7230 -- The type declaration for the derived corresponding record has the
7231 -- same discriminant part and constraints as the current declaration.
7232 -- Copy the unanalyzed tree to build declaration.
7234 Corr_Decl_Needed := True;
7235 New_N := Copy_Separate_Tree (N);
7237 Corr_Decl :=
7238 Make_Full_Type_Declaration (Loc,
7239 Defining_Identifier => Corr_Record,
7240 Discriminant_Specifications =>
7241 Discriminant_Specifications (New_N),
7242 Type_Definition =>
7243 Make_Derived_Type_Definition (Loc,
7244 Subtype_Indication =>
7245 Make_Subtype_Indication (Loc,
7246 Subtype_Mark =>
7247 New_Occurrence_Of
7248 (Corresponding_Record_Type (Parent_Type), Loc),
7249 Constraint =>
7250 Constraint
7251 (Subtype_Indication (Type_Definition (New_N))))));
7252 end if;
7254 -- Copy Storage_Size and Relative_Deadline variables if task case
7256 if Is_Task_Type (Parent_Type) then
7257 Set_Storage_Size_Variable (Derived_Type,
7258 Storage_Size_Variable (Parent_Type));
7259 Set_Relative_Deadline_Variable (Derived_Type,
7260 Relative_Deadline_Variable (Parent_Type));
7261 end if;
7263 if Present (Discriminant_Specifications (N)) then
7264 Push_Scope (Derived_Type);
7265 Check_Or_Process_Discriminants (N, Derived_Type);
7267 if Constraint_Present then
7268 New_Constraint :=
7269 Expand_To_Stored_Constraint
7270 (Parent_Type,
7271 Build_Discriminant_Constraints
7272 (Parent_Type, Indic, True));
7273 end if;
7275 End_Scope;
7277 elsif Constraint_Present then
7279 -- Build an unconstrained derived type and rewrite the derived type
7280 -- as a subtype of this new base type.
7282 declare
7283 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7284 New_Base : Entity_Id;
7285 New_Decl : Node_Id;
7286 New_Indic : Node_Id;
7288 begin
7289 New_Base :=
7290 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7292 New_Decl :=
7293 Make_Full_Type_Declaration (Loc,
7294 Defining_Identifier => New_Base,
7295 Type_Definition =>
7296 Make_Derived_Type_Definition (Loc,
7297 Abstract_Present => Abstract_Present (Def),
7298 Limited_Present => Limited_Present (Def),
7299 Subtype_Indication =>
7300 New_Occurrence_Of (Parent_Base, Loc)));
7302 Mark_Rewrite_Insertion (New_Decl);
7303 Insert_Before (N, New_Decl);
7304 Analyze (New_Decl);
7306 New_Indic :=
7307 Make_Subtype_Indication (Loc,
7308 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7309 Constraint => Relocate_Node (Constraint (Indic)));
7311 Rewrite (N,
7312 Make_Subtype_Declaration (Loc,
7313 Defining_Identifier => Derived_Type,
7314 Subtype_Indication => New_Indic));
7316 Analyze (N);
7317 return;
7318 end;
7319 end if;
7321 -- By default, operations and private data are inherited from parent.
7322 -- However, in the presence of bound discriminants, a new corresponding
7323 -- record will be created, see below.
7325 Set_Has_Discriminants
7326 (Derived_Type, Has_Discriminants (Parent_Type));
7327 Set_Corresponding_Record_Type
7328 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7330 -- Is_Constrained is set according the parent subtype, but is set to
7331 -- False if the derived type is declared with new discriminants.
7333 Set_Is_Constrained
7334 (Derived_Type,
7335 (Is_Constrained (Parent_Type) or else Constraint_Present)
7336 and then not Present (Discriminant_Specifications (N)));
7338 if Constraint_Present then
7339 if not Has_Discriminants (Parent_Type) then
7340 Error_Msg_N ("untagged parent must have discriminants", N);
7342 elsif Present (Discriminant_Specifications (N)) then
7344 -- Verify that new discriminants are used to constrain old ones
7346 D_Constraint := First (Constraints (Constraint (Indic)));
7348 Old_Disc := First_Discriminant (Parent_Type);
7350 while Present (D_Constraint) loop
7351 if Nkind (D_Constraint) /= N_Discriminant_Association then
7353 -- Positional constraint. If it is a reference to a new
7354 -- discriminant, it constrains the corresponding old one.
7356 if Nkind (D_Constraint) = N_Identifier then
7357 New_Disc := First_Discriminant (Derived_Type);
7358 while Present (New_Disc) loop
7359 exit when Chars (New_Disc) = Chars (D_Constraint);
7360 Next_Discriminant (New_Disc);
7361 end loop;
7363 if Present (New_Disc) then
7364 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7365 end if;
7366 end if;
7368 Next_Discriminant (Old_Disc);
7370 -- if this is a named constraint, search by name for the old
7371 -- discriminants constrained by the new one.
7373 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7375 -- Find new discriminant with that name
7377 New_Disc := First_Discriminant (Derived_Type);
7378 while Present (New_Disc) loop
7379 exit when
7380 Chars (New_Disc) = Chars (Expression (D_Constraint));
7381 Next_Discriminant (New_Disc);
7382 end loop;
7384 if Present (New_Disc) then
7386 -- Verify that new discriminant renames some discriminant
7387 -- of the parent type, and associate the new discriminant
7388 -- with one or more old ones that it renames.
7390 declare
7391 Selector : Node_Id;
7393 begin
7394 Selector := First (Selector_Names (D_Constraint));
7395 while Present (Selector) loop
7396 Old_Disc := First_Discriminant (Parent_Type);
7397 while Present (Old_Disc) loop
7398 exit when Chars (Old_Disc) = Chars (Selector);
7399 Next_Discriminant (Old_Disc);
7400 end loop;
7402 if Present (Old_Disc) then
7403 Set_Corresponding_Discriminant
7404 (New_Disc, Old_Disc);
7405 end if;
7407 Next (Selector);
7408 end loop;
7409 end;
7410 end if;
7411 end if;
7413 Next (D_Constraint);
7414 end loop;
7416 New_Disc := First_Discriminant (Derived_Type);
7417 while Present (New_Disc) loop
7418 if No (Corresponding_Discriminant (New_Disc)) then
7419 Error_Msg_NE
7420 ("new discriminant& must constrain old one", N, New_Disc);
7422 -- If a new discriminant is used in the constraint, then its
7423 -- subtype must be statically compatible with the subtype of
7424 -- the parent discriminant (RM 3.7(15)).
7426 else
7427 Check_Constraining_Discriminant
7428 (New_Disc, Corresponding_Discriminant (New_Disc));
7429 end if;
7431 Next_Discriminant (New_Disc);
7432 end loop;
7433 end if;
7435 elsif Present (Discriminant_Specifications (N)) then
7436 Error_Msg_N
7437 ("missing discriminant constraint in untagged derivation", N);
7438 end if;
7440 -- The entity chain of the derived type includes the new discriminants
7441 -- but shares operations with the parent.
7443 if Present (Discriminant_Specifications (N)) then
7444 Old_Disc := First_Discriminant (Parent_Type);
7445 while Present (Old_Disc) loop
7446 if No (Next_Entity (Old_Disc))
7447 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7448 then
7449 Link_Entities
7450 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7451 exit;
7452 end if;
7454 Next_Discriminant (Old_Disc);
7455 end loop;
7457 else
7458 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7459 if Has_Discriminants (Parent_Type) then
7460 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7461 Set_Discriminant_Constraint (
7462 Derived_Type, Discriminant_Constraint (Parent_Type));
7463 end if;
7464 end if;
7466 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7468 Set_Has_Completion (Derived_Type);
7470 if Corr_Decl_Needed then
7471 Set_Stored_Constraint (Derived_Type, New_Constraint);
7472 Insert_After (N, Corr_Decl);
7473 Analyze (Corr_Decl);
7474 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7475 end if;
7476 end Build_Derived_Concurrent_Type;
7478 ------------------------------------
7479 -- Build_Derived_Enumeration_Type --
7480 ------------------------------------
7482 procedure Build_Derived_Enumeration_Type
7483 (N : Node_Id;
7484 Parent_Type : Entity_Id;
7485 Derived_Type : Entity_Id)
7487 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7488 -- When the type declaration includes a constraint, we generate
7489 -- a subtype declaration of an anonymous base type, with the constraint
7490 -- given in the original type declaration. Conceptually, the bounds
7491 -- are converted to the new base type, and this conversion freezes
7492 -- (prematurely) that base type, when the bounds are simply literals.
7493 -- As a result, a representation clause for the derived type is then
7494 -- rejected or ignored. This procedure recognizes the simple case of
7495 -- literal bounds, which allows us to indicate that the conversions
7496 -- are not freeze points, and the subsequent representation clause
7497 -- can be accepted.
7498 -- A similar approach might be used to resolve the long-standing
7499 -- problem of premature freezing of derived numeric types ???
7501 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7502 begin
7503 return Nkind (B) = N_Type_Conversion
7504 and then Is_Entity_Name (Expression (B))
7505 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7506 end Bound_Belongs_To_Type;
7508 Loc : constant Source_Ptr := Sloc (N);
7509 Def : constant Node_Id := Type_Definition (N);
7510 Indic : constant Node_Id := Subtype_Indication (Def);
7511 Implicit_Base : Entity_Id;
7512 Literal : Entity_Id;
7513 New_Lit : Entity_Id;
7514 Literals_List : List_Id;
7515 Type_Decl : Node_Id;
7516 Hi, Lo : Node_Id;
7517 Rang_Expr : Node_Id;
7519 begin
7520 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7521 -- not have explicit literals lists we need to process types derived
7522 -- from them specially. This is handled by Derived_Standard_Character.
7523 -- If the parent type is a generic type, there are no literals either,
7524 -- and we construct the same skeletal representation as for the generic
7525 -- parent type.
7527 if Is_Standard_Character_Type (Parent_Type) then
7528 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7530 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7531 declare
7532 Lo : Node_Id;
7533 Hi : Node_Id;
7535 begin
7536 if Nkind (Indic) /= N_Subtype_Indication then
7537 Lo :=
7538 Make_Attribute_Reference (Loc,
7539 Attribute_Name => Name_First,
7540 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7541 Set_Etype (Lo, Derived_Type);
7543 Hi :=
7544 Make_Attribute_Reference (Loc,
7545 Attribute_Name => Name_Last,
7546 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7547 Set_Etype (Hi, Derived_Type);
7549 Set_Scalar_Range (Derived_Type,
7550 Make_Range (Loc,
7551 Low_Bound => Lo,
7552 High_Bound => Hi));
7553 else
7555 -- Analyze subtype indication and verify compatibility
7556 -- with parent type.
7558 if Base_Type (Process_Subtype (Indic, N)) /=
7559 Base_Type (Parent_Type)
7560 then
7561 Error_Msg_N
7562 ("illegal constraint for formal discrete type", N);
7563 end if;
7564 end if;
7565 end;
7567 else
7568 -- If a constraint is present, analyze the bounds to catch
7569 -- premature usage of the derived literals.
7571 if Nkind (Indic) = N_Subtype_Indication
7572 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7573 then
7574 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7575 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7576 end if;
7578 -- Create an implicit base type for the derived type even if there
7579 -- is no constraint attached to it, since this seems closer to the
7580 -- Ada semantics. Use an Itype like for the implicit base type of
7581 -- other kinds of derived type, but build a full type declaration
7582 -- for it so as to analyze the new literals properly. Then build a
7583 -- subtype declaration tree which applies the constraint (if any)
7584 -- and have it replace the derived type declaration.
7586 Literal := First_Literal (Parent_Type);
7587 Literals_List := New_List;
7588 while Present (Literal)
7589 and then Ekind (Literal) = E_Enumeration_Literal
7590 loop
7591 -- Literals of the derived type have the same representation as
7592 -- those of the parent type, but this representation can be
7593 -- overridden by an explicit representation clause. Indicate
7594 -- that there is no explicit representation given yet. These
7595 -- derived literals are implicit operations of the new type,
7596 -- and can be overridden by explicit ones.
7598 if Nkind (Literal) = N_Defining_Character_Literal then
7599 New_Lit :=
7600 Make_Defining_Character_Literal (Loc, Chars (Literal));
7601 else
7602 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7603 end if;
7605 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7606 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7607 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7608 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7609 Set_Alias (New_Lit, Literal);
7610 Set_Is_Known_Valid (New_Lit, True);
7612 Append (New_Lit, Literals_List);
7613 Next_Literal (Literal);
7614 end loop;
7616 Implicit_Base :=
7617 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7619 -- Indicate the proper nature of the derived type. This must be done
7620 -- before analysis of the literals, to recognize cases when a literal
7621 -- may be hidden by a previous explicit function definition (cf.
7622 -- c83031a).
7624 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7625 Set_Etype (Derived_Type, Implicit_Base);
7627 Type_Decl :=
7628 Make_Full_Type_Declaration (Loc,
7629 Defining_Identifier => Implicit_Base,
7630 Type_Definition =>
7631 Make_Enumeration_Type_Definition (Loc, Literals_List));
7633 -- Do not insert the declarationn, just analyze it in the context
7635 Set_Parent (Type_Decl, Parent (N));
7636 Analyze (Type_Decl);
7638 -- The anonymous base now has a full declaration, but this base
7639 -- is not a first subtype.
7641 Set_Is_First_Subtype (Implicit_Base, False);
7643 -- After the implicit base is analyzed its Etype needs to be changed
7644 -- to reflect the fact that it is derived from the parent type which
7645 -- was ignored during analysis. We also set the size at this point.
7647 Set_Etype (Implicit_Base, Parent_Type);
7649 Set_Size_Info (Implicit_Base, Parent_Type);
7650 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7651 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7653 -- Copy other flags from parent type
7655 Set_Has_Non_Standard_Rep
7656 (Implicit_Base, Has_Non_Standard_Rep
7657 (Parent_Type));
7658 Set_Has_Pragma_Ordered
7659 (Implicit_Base, Has_Pragma_Ordered
7660 (Parent_Type));
7661 Set_Has_Delayed_Freeze (Implicit_Base);
7663 -- Process the subtype indication including a validation check on the
7664 -- constraint, if any. If a constraint is given, its bounds must be
7665 -- implicitly converted to the new type.
7667 if Nkind (Indic) = N_Subtype_Indication then
7668 declare
7669 R : constant Node_Id :=
7670 Range_Expression (Constraint (Indic));
7672 begin
7673 if Nkind (R) = N_Range then
7674 Hi := Build_Scalar_Bound
7675 (High_Bound (R), Parent_Type, Implicit_Base);
7676 Lo := Build_Scalar_Bound
7677 (Low_Bound (R), Parent_Type, Implicit_Base);
7679 else
7680 -- Constraint is a Range attribute. Replace with explicit
7681 -- mention of the bounds of the prefix, which must be a
7682 -- subtype.
7684 Analyze (Prefix (R));
7685 Hi :=
7686 Convert_To (Implicit_Base,
7687 Make_Attribute_Reference (Loc,
7688 Attribute_Name => Name_Last,
7689 Prefix =>
7690 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7692 Lo :=
7693 Convert_To (Implicit_Base,
7694 Make_Attribute_Reference (Loc,
7695 Attribute_Name => Name_First,
7696 Prefix =>
7697 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7698 end if;
7699 end;
7701 else
7702 Hi :=
7703 Build_Scalar_Bound
7704 (Type_High_Bound (Parent_Type),
7705 Parent_Type, Implicit_Base);
7706 Lo :=
7707 Build_Scalar_Bound
7708 (Type_Low_Bound (Parent_Type),
7709 Parent_Type, Implicit_Base);
7710 end if;
7712 Rang_Expr :=
7713 Make_Range (Loc,
7714 Low_Bound => Lo,
7715 High_Bound => Hi);
7717 -- If we constructed a default range for the case where no range
7718 -- was given, then the expressions in the range must not freeze
7719 -- since they do not correspond to expressions in the source.
7720 -- However, if the type inherits predicates the expressions will
7721 -- be elaborated earlier and must freeze.
7723 if (Nkind (Indic) /= N_Subtype_Indication
7724 or else
7725 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7726 and then not Has_Predicates (Derived_Type)
7727 then
7728 Set_Must_Not_Freeze (Lo);
7729 Set_Must_Not_Freeze (Hi);
7730 Set_Must_Not_Freeze (Rang_Expr);
7731 end if;
7733 Rewrite (N,
7734 Make_Subtype_Declaration (Loc,
7735 Defining_Identifier => Derived_Type,
7736 Subtype_Indication =>
7737 Make_Subtype_Indication (Loc,
7738 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7739 Constraint =>
7740 Make_Range_Constraint (Loc,
7741 Range_Expression => Rang_Expr))));
7743 Analyze (N);
7745 -- Propagate the aspects from the original type declaration to the
7746 -- declaration of the implicit base.
7748 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7750 -- Apply a range check. Since this range expression doesn't have an
7751 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7752 -- this right???
7754 if Nkind (Indic) = N_Subtype_Indication then
7755 Apply_Range_Check
7756 (Range_Expression (Constraint (Indic)), Parent_Type,
7757 Source_Typ => Entity (Subtype_Mark (Indic)));
7758 end if;
7759 end if;
7760 end Build_Derived_Enumeration_Type;
7762 --------------------------------
7763 -- Build_Derived_Numeric_Type --
7764 --------------------------------
7766 procedure Build_Derived_Numeric_Type
7767 (N : Node_Id;
7768 Parent_Type : Entity_Id;
7769 Derived_Type : Entity_Id)
7771 Loc : constant Source_Ptr := Sloc (N);
7772 Tdef : constant Node_Id := Type_Definition (N);
7773 Indic : constant Node_Id := Subtype_Indication (Tdef);
7774 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7775 No_Constraint : constant Boolean := Nkind (Indic) /=
7776 N_Subtype_Indication;
7777 Implicit_Base : Entity_Id;
7779 Lo : Node_Id;
7780 Hi : Node_Id;
7782 begin
7783 -- Process the subtype indication including a validation check on
7784 -- the constraint if any.
7786 Discard_Node (Process_Subtype (Indic, N));
7788 -- Introduce an implicit base type for the derived type even if there
7789 -- is no constraint attached to it, since this seems closer to the Ada
7790 -- semantics.
7792 Implicit_Base :=
7793 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7795 Set_Etype (Implicit_Base, Parent_Base);
7796 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7797 Set_Size_Info (Implicit_Base, Parent_Base);
7798 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7799 Set_Parent (Implicit_Base, Parent (Derived_Type));
7800 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7801 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7803 -- Set RM Size for discrete type or decimal fixed-point type
7804 -- Ordinary fixed-point is excluded, why???
7806 if Is_Discrete_Type (Parent_Base)
7807 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7808 then
7809 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7810 end if;
7812 Set_Has_Delayed_Freeze (Implicit_Base);
7814 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7815 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7817 Set_Scalar_Range (Implicit_Base,
7818 Make_Range (Loc,
7819 Low_Bound => Lo,
7820 High_Bound => Hi));
7822 if Has_Infinities (Parent_Base) then
7823 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7824 end if;
7826 -- The Derived_Type, which is the entity of the declaration, is a
7827 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7828 -- absence of an explicit constraint.
7830 Set_Etype (Derived_Type, Implicit_Base);
7832 -- If we did not have a constraint, then the Ekind is set from the
7833 -- parent type (otherwise Process_Subtype has set the bounds)
7835 if No_Constraint then
7836 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7837 end if;
7839 -- If we did not have a range constraint, then set the range from the
7840 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7842 if No_Constraint or else not Has_Range_Constraint (Indic) then
7843 Set_Scalar_Range (Derived_Type,
7844 Make_Range (Loc,
7845 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7846 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7847 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7849 if Has_Infinities (Parent_Type) then
7850 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7851 end if;
7853 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7854 end if;
7856 Set_Is_Descendant_Of_Address (Derived_Type,
7857 Is_Descendant_Of_Address (Parent_Type));
7858 Set_Is_Descendant_Of_Address (Implicit_Base,
7859 Is_Descendant_Of_Address (Parent_Type));
7861 -- Set remaining type-specific fields, depending on numeric type
7863 if Is_Modular_Integer_Type (Parent_Type) then
7864 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7866 Set_Non_Binary_Modulus
7867 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7869 Set_Is_Known_Valid
7870 (Implicit_Base, Is_Known_Valid (Parent_Base));
7872 elsif Is_Floating_Point_Type (Parent_Type) then
7874 -- Digits of base type is always copied from the digits value of
7875 -- the parent base type, but the digits of the derived type will
7876 -- already have been set if there was a constraint present.
7878 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7879 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7881 if No_Constraint then
7882 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7883 end if;
7885 elsif Is_Fixed_Point_Type (Parent_Type) then
7887 -- Small of base type and derived type are always copied from the
7888 -- parent base type, since smalls never change. The delta of the
7889 -- base type is also copied from the parent base type. However the
7890 -- delta of the derived type will have been set already if a
7891 -- constraint was present.
7893 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7894 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7895 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7897 if No_Constraint then
7898 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7899 end if;
7901 -- The scale and machine radix in the decimal case are always
7902 -- copied from the parent base type.
7904 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7905 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7906 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7908 Set_Machine_Radix_10
7909 (Derived_Type, Machine_Radix_10 (Parent_Base));
7910 Set_Machine_Radix_10
7911 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7913 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7915 if No_Constraint then
7916 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7918 else
7919 -- the analysis of the subtype_indication sets the
7920 -- digits value of the derived type.
7922 null;
7923 end if;
7924 end if;
7925 end if;
7927 if Is_Integer_Type (Parent_Type) then
7928 Set_Has_Shift_Operator
7929 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7930 end if;
7932 -- The type of the bounds is that of the parent type, and they
7933 -- must be converted to the derived type.
7935 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7936 end Build_Derived_Numeric_Type;
7938 --------------------------------
7939 -- Build_Derived_Private_Type --
7940 --------------------------------
7942 procedure Build_Derived_Private_Type
7943 (N : Node_Id;
7944 Parent_Type : Entity_Id;
7945 Derived_Type : Entity_Id;
7946 Is_Completion : Boolean;
7947 Derive_Subps : Boolean := True)
7949 Loc : constant Source_Ptr := Sloc (N);
7950 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7951 Par_Scope : constant Entity_Id := Scope (Par_Base);
7952 Full_N : constant Node_Id := New_Copy_Tree (N);
7953 Full_Der : Entity_Id := New_Copy (Derived_Type);
7954 Full_P : Entity_Id;
7956 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7957 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7958 -- present (they cannot be both present for the same type), or Empty.
7960 procedure Build_Full_Derivation;
7961 -- Build full derivation, i.e. derive from the full view
7963 procedure Copy_And_Build;
7964 -- Copy derived type declaration, replace parent with its full view,
7965 -- and build derivation
7967 -------------------------
7968 -- Available_Full_View --
7969 -------------------------
7971 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7972 begin
7973 if Present (Full_View (Typ)) then
7974 return Full_View (Typ);
7976 elsif Present (Underlying_Full_View (Typ)) then
7978 -- We should be called on a type with an underlying full view
7979 -- only by means of the recursive call made in Copy_And_Build
7980 -- through the first call to Build_Derived_Type, or else if
7981 -- the parent scope is being analyzed because we are deriving
7982 -- a completion.
7984 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7986 return Underlying_Full_View (Typ);
7988 else
7989 return Empty;
7990 end if;
7991 end Available_Full_View;
7993 ---------------------------
7994 -- Build_Full_Derivation --
7995 ---------------------------
7997 procedure Build_Full_Derivation is
7998 begin
7999 -- If parent scope is not open, install the declarations
8001 if not In_Open_Scopes (Par_Scope) then
8002 Install_Private_Declarations (Par_Scope);
8003 Install_Visible_Declarations (Par_Scope);
8004 Copy_And_Build;
8005 Uninstall_Declarations (Par_Scope);
8007 -- If parent scope is open and in another unit, and parent has a
8008 -- completion, then the derivation is taking place in the visible
8009 -- part of a child unit. In that case retrieve the full view of
8010 -- the parent momentarily.
8012 elsif not In_Same_Source_Unit (N, Parent_Type)
8013 and then Present (Full_View (Parent_Type))
8014 then
8015 Full_P := Full_View (Parent_Type);
8016 Exchange_Declarations (Parent_Type);
8017 Copy_And_Build;
8018 Exchange_Declarations (Full_P);
8020 -- Otherwise it is a local derivation
8022 else
8023 Copy_And_Build;
8024 end if;
8025 end Build_Full_Derivation;
8027 --------------------
8028 -- Copy_And_Build --
8029 --------------------
8031 procedure Copy_And_Build is
8032 Full_Parent : Entity_Id := Parent_Type;
8034 begin
8035 -- If the parent is itself derived from another private type,
8036 -- installing the private declarations has not affected its
8037 -- privacy status, so use its own full view explicitly.
8039 if Is_Private_Type (Full_Parent)
8040 and then Present (Full_View (Full_Parent))
8041 then
8042 Full_Parent := Full_View (Full_Parent);
8043 end if;
8045 -- If the full view is itself derived from another private type
8046 -- and has got an underlying full view, and this is done for a
8047 -- completion, i.e. to build the underlying full view of the type,
8048 -- then use this underlying full view. We cannot do that if this
8049 -- is not a completion, i.e. to build the full view of the type,
8050 -- because this would break the privacy of the parent type, except
8051 -- if the parent scope is being analyzed because we are deriving a
8052 -- completion.
8054 if Is_Private_Type (Full_Parent)
8055 and then Present (Underlying_Full_View (Full_Parent))
8056 and then (Is_Completion or else In_Private_Part (Par_Scope))
8057 then
8058 Full_Parent := Underlying_Full_View (Full_Parent);
8059 end if;
8061 -- For private, record, concurrent, access and almost all enumeration
8062 -- types, the derivation from the full view requires a fully-fledged
8063 -- declaration. In the other cases, just use an itype.
8065 if Is_Private_Type (Full_Parent)
8066 or else Is_Record_Type (Full_Parent)
8067 or else Is_Concurrent_Type (Full_Parent)
8068 or else Is_Access_Type (Full_Parent)
8069 or else
8070 (Is_Enumeration_Type (Full_Parent)
8071 and then not Is_Standard_Character_Type (Full_Parent)
8072 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8073 then
8074 -- Copy and adjust declaration to provide a completion for what
8075 -- is originally a private declaration. Indicate that full view
8076 -- is internally generated.
8078 Set_Comes_From_Source (Full_N, False);
8079 Set_Comes_From_Source (Full_Der, False);
8080 Set_Parent (Full_Der, Full_N);
8081 Set_Defining_Identifier (Full_N, Full_Der);
8083 -- If there are no constraints, adjust the subtype mark
8085 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8086 N_Subtype_Indication
8087 then
8088 Set_Subtype_Indication
8089 (Type_Definition (Full_N),
8090 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8091 end if;
8093 Insert_After (N, Full_N);
8095 -- Build full view of derived type from full view of parent which
8096 -- is now installed. Subprograms have been derived on the partial
8097 -- view, the completion does not derive them anew.
8099 if Is_Record_Type (Full_Parent) then
8101 -- If parent type is tagged, the completion inherits the proper
8102 -- primitive operations.
8104 if Is_Tagged_Type (Parent_Type) then
8105 Build_Derived_Record_Type
8106 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8107 else
8108 Build_Derived_Record_Type
8109 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8110 end if;
8112 else
8113 -- If the parent type is private, this is not a completion and
8114 -- we build the full derivation recursively as a completion.
8116 Build_Derived_Type
8117 (Full_N, Full_Parent, Full_Der,
8118 Is_Completion => Is_Private_Type (Full_Parent),
8119 Derive_Subps => False);
8120 end if;
8122 -- The full declaration has been introduced into the tree and
8123 -- processed in the step above. It should not be analyzed again
8124 -- (when encountered later in the current list of declarations)
8125 -- to prevent spurious name conflicts. The full entity remains
8126 -- invisible.
8128 Set_Analyzed (Full_N);
8130 else
8131 Full_Der :=
8132 Make_Defining_Identifier (Sloc (Derived_Type),
8133 Chars => Chars (Derived_Type));
8134 Set_Is_Itype (Full_Der);
8135 Set_Associated_Node_For_Itype (Full_Der, N);
8136 Set_Parent (Full_Der, N);
8137 Build_Derived_Type
8138 (N, Full_Parent, Full_Der,
8139 Is_Completion => False, Derive_Subps => False);
8140 end if;
8142 Set_Has_Private_Declaration (Full_Der);
8143 Set_Has_Private_Declaration (Derived_Type);
8145 Set_Scope (Full_Der, Scope (Derived_Type));
8146 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8147 Set_Has_Size_Clause (Full_Der, False);
8148 Set_Has_Alignment_Clause (Full_Der, False);
8149 Set_Has_Delayed_Freeze (Full_Der);
8150 Set_Is_Frozen (Full_Der, False);
8151 Set_Freeze_Node (Full_Der, Empty);
8152 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8153 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8155 -- The convention on the base type may be set in the private part
8156 -- and not propagated to the subtype until later, so we obtain the
8157 -- convention from the base type of the parent.
8159 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8160 end Copy_And_Build;
8162 -- Start of processing for Build_Derived_Private_Type
8164 begin
8165 if Is_Tagged_Type (Parent_Type) then
8166 Full_P := Full_View (Parent_Type);
8168 -- A type extension of a type with unknown discriminants is an
8169 -- indefinite type that the back-end cannot handle directly.
8170 -- We treat it as a private type, and build a completion that is
8171 -- derived from the full view of the parent, and hopefully has
8172 -- known discriminants.
8174 -- If the full view of the parent type has an underlying record view,
8175 -- use it to generate the underlying record view of this derived type
8176 -- (required for chains of derivations with unknown discriminants).
8178 -- Minor optimization: we avoid the generation of useless underlying
8179 -- record view entities if the private type declaration has unknown
8180 -- discriminants but its corresponding full view has no
8181 -- discriminants.
8183 if Has_Unknown_Discriminants (Parent_Type)
8184 and then Present (Full_P)
8185 and then (Has_Discriminants (Full_P)
8186 or else Present (Underlying_Record_View (Full_P)))
8187 and then not In_Open_Scopes (Par_Scope)
8188 and then Expander_Active
8189 then
8190 declare
8191 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8192 New_Ext : constant Node_Id :=
8193 Copy_Separate_Tree
8194 (Record_Extension_Part (Type_Definition (N)));
8195 Decl : Node_Id;
8197 begin
8198 Build_Derived_Record_Type
8199 (N, Parent_Type, Derived_Type, Derive_Subps);
8201 -- Build anonymous completion, as a derivation from the full
8202 -- view of the parent. This is not a completion in the usual
8203 -- sense, because the current type is not private.
8205 Decl :=
8206 Make_Full_Type_Declaration (Loc,
8207 Defining_Identifier => Full_Der,
8208 Type_Definition =>
8209 Make_Derived_Type_Definition (Loc,
8210 Subtype_Indication =>
8211 New_Copy_Tree
8212 (Subtype_Indication (Type_Definition (N))),
8213 Record_Extension_Part => New_Ext));
8215 -- If the parent type has an underlying record view, use it
8216 -- here to build the new underlying record view.
8218 if Present (Underlying_Record_View (Full_P)) then
8219 pragma Assert
8220 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8221 = N_Identifier);
8222 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8223 Underlying_Record_View (Full_P));
8224 end if;
8226 Install_Private_Declarations (Par_Scope);
8227 Install_Visible_Declarations (Par_Scope);
8228 Insert_Before (N, Decl);
8230 -- Mark entity as an underlying record view before analysis,
8231 -- to avoid generating the list of its primitive operations
8232 -- (which is not really required for this entity) and thus
8233 -- prevent spurious errors associated with missing overriding
8234 -- of abstract primitives (overridden only for Derived_Type).
8236 Mutate_Ekind (Full_Der, E_Record_Type);
8237 Set_Is_Underlying_Record_View (Full_Der);
8238 Set_Default_SSO (Full_Der);
8239 Set_No_Reordering (Full_Der, No_Component_Reordering);
8241 Analyze (Decl);
8243 pragma Assert (Has_Discriminants (Full_Der)
8244 and then not Has_Unknown_Discriminants (Full_Der));
8246 Uninstall_Declarations (Par_Scope);
8248 -- Freeze the underlying record view, to prevent generation of
8249 -- useless dispatching information, which is simply shared with
8250 -- the real derived type.
8252 Set_Is_Frozen (Full_Der);
8254 -- If the derived type has access discriminants, create
8255 -- references to their anonymous types now, to prevent
8256 -- back-end problems when their first use is in generated
8257 -- bodies of primitives.
8259 declare
8260 E : Entity_Id;
8262 begin
8263 E := First_Entity (Full_Der);
8265 while Present (E) loop
8266 if Ekind (E) = E_Discriminant
8267 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8268 then
8269 Build_Itype_Reference (Etype (E), Decl);
8270 end if;
8272 Next_Entity (E);
8273 end loop;
8274 end;
8276 -- Set up links between real entity and underlying record view
8278 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8279 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8280 end;
8282 -- If discriminants are known, build derived record
8284 else
8285 Build_Derived_Record_Type
8286 (N, Parent_Type, Derived_Type, Derive_Subps);
8287 end if;
8289 return;
8291 elsif Has_Discriminants (Parent_Type) then
8293 -- Build partial view of derived type from partial view of parent.
8294 -- This must be done before building the full derivation because the
8295 -- second derivation will modify the discriminants of the first and
8296 -- the discriminants are chained with the rest of the components in
8297 -- the full derivation.
8299 Build_Derived_Record_Type
8300 (N, Parent_Type, Derived_Type, Derive_Subps);
8302 -- Build the full derivation if this is not the anonymous derived
8303 -- base type created by Build_Derived_Record_Type in the constrained
8304 -- case (see point 5. of its head comment) since we build it for the
8305 -- derived subtype.
8307 if Present (Available_Full_View (Parent_Type))
8308 and then not Is_Itype (Derived_Type)
8309 then
8310 declare
8311 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8312 Discr : Entity_Id;
8313 Last_Discr : Entity_Id;
8315 begin
8316 -- If this is not a completion, construct the implicit full
8317 -- view by deriving from the full view of the parent type.
8318 -- But if this is a completion, the derived private type
8319 -- being built is a full view and the full derivation can
8320 -- only be its underlying full view.
8322 Build_Full_Derivation;
8324 if not Is_Completion then
8325 Set_Full_View (Derived_Type, Full_Der);
8326 else
8327 Set_Underlying_Full_View (Derived_Type, Full_Der);
8328 Set_Is_Underlying_Full_View (Full_Der);
8329 end if;
8331 if not Is_Base_Type (Derived_Type) then
8332 Set_Full_View (Der_Base, Base_Type (Full_Der));
8333 end if;
8335 -- Copy the discriminant list from full view to the partial
8336 -- view (base type and its subtype). Gigi requires that the
8337 -- partial and full views have the same discriminants.
8339 -- Note that since the partial view points to discriminants
8340 -- in the full view, their scope will be that of the full
8341 -- view. This might cause some front end problems and need
8342 -- adjustment???
8344 Discr := First_Discriminant (Base_Type (Full_Der));
8345 Set_First_Entity (Der_Base, Discr);
8347 loop
8348 Last_Discr := Discr;
8349 Next_Discriminant (Discr);
8350 exit when No (Discr);
8351 end loop;
8353 Set_Last_Entity (Der_Base, Last_Discr);
8354 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8355 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8356 end;
8357 end if;
8359 elsif Present (Available_Full_View (Parent_Type))
8360 and then Has_Discriminants (Available_Full_View (Parent_Type))
8361 then
8362 if Has_Unknown_Discriminants (Parent_Type)
8363 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8364 N_Subtype_Indication
8365 then
8366 Error_Msg_N
8367 ("cannot constrain type with unknown discriminants",
8368 Subtype_Indication (Type_Definition (N)));
8369 return;
8370 end if;
8372 -- If this is not a completion, construct the implicit full view by
8373 -- deriving from the full view of the parent type. But if this is a
8374 -- completion, the derived private type being built is a full view
8375 -- and the full derivation can only be its underlying full view.
8377 Build_Full_Derivation;
8379 if not Is_Completion then
8380 Set_Full_View (Derived_Type, Full_Der);
8381 else
8382 Set_Underlying_Full_View (Derived_Type, Full_Der);
8383 Set_Is_Underlying_Full_View (Full_Der);
8384 end if;
8386 -- In any case, the primitive operations are inherited from the
8387 -- parent type, not from the internal full view.
8389 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8391 if Derive_Subps then
8392 -- Initialize the list of primitive operations to an empty list,
8393 -- to cover tagged types as well as untagged types. For untagged
8394 -- types this is used either to analyze the call as legal when
8395 -- Extensions_Allowed is True, or to issue a better error message
8396 -- otherwise.
8398 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8400 Derive_Subprograms (Parent_Type, Derived_Type);
8401 end if;
8403 Set_Stored_Constraint (Derived_Type, No_Elist);
8404 Set_Is_Constrained
8405 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8407 else
8408 -- Untagged type, No discriminants on either view
8410 if Nkind (Subtype_Indication (Type_Definition (N))) =
8411 N_Subtype_Indication
8412 then
8413 Error_Msg_N
8414 ("illegal constraint on type without discriminants", N);
8415 end if;
8417 if Present (Discriminant_Specifications (N))
8418 and then Present (Available_Full_View (Parent_Type))
8419 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8420 then
8421 Error_Msg_N ("cannot add discriminants to untagged type", N);
8422 end if;
8424 Set_Stored_Constraint (Derived_Type, No_Elist);
8425 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8427 Set_Is_Controlled_Active
8428 (Derived_Type, Is_Controlled_Active (Parent_Type));
8430 Set_Disable_Controlled
8431 (Derived_Type, Disable_Controlled (Parent_Type));
8433 Set_Has_Controlled_Component
8434 (Derived_Type, Has_Controlled_Component (Parent_Type));
8436 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8438 if not Is_Controlled (Parent_Type) then
8439 Set_Finalize_Storage_Only
8440 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8441 end if;
8443 -- If this is not a completion, construct the implicit full view by
8444 -- deriving from the full view of the parent type. But if this is a
8445 -- completion, the derived private type being built is a full view
8446 -- and the full derivation can only be its underlying full view.
8448 -- ??? If the parent type is untagged private and its completion is
8449 -- tagged, this mechanism will not work because we cannot derive from
8450 -- the tagged full view unless we have an extension.
8452 if Present (Available_Full_View (Parent_Type))
8453 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8454 and then not Error_Posted (N)
8455 then
8456 Build_Full_Derivation;
8458 if not Is_Completion then
8459 Set_Full_View (Derived_Type, Full_Der);
8460 else
8461 Set_Underlying_Full_View (Derived_Type, Full_Der);
8462 Set_Is_Underlying_Full_View (Full_Der);
8463 end if;
8464 end if;
8465 end if;
8467 Set_Has_Unknown_Discriminants (Derived_Type,
8468 Has_Unknown_Discriminants (Parent_Type));
8470 if Is_Private_Type (Derived_Type) then
8471 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8472 end if;
8474 -- If the parent base type is in scope, add the derived type to its
8475 -- list of private dependents, because its full view may become
8476 -- visible subsequently (in a nested private part, a body, or in a
8477 -- further child unit).
8479 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8480 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8482 -- Check for unusual case where a type completed by a private
8483 -- derivation occurs within a package nested in a child unit, and
8484 -- the parent is declared in an ancestor.
8486 if Is_Child_Unit (Scope (Current_Scope))
8487 and then Is_Completion
8488 and then In_Private_Part (Current_Scope)
8489 and then Scope (Parent_Type) /= Current_Scope
8491 -- Note that if the parent has a completion in the private part,
8492 -- (which is itself a derivation from some other private type)
8493 -- it is that completion that is visible, there is no full view
8494 -- available, and no special processing is needed.
8496 and then Present (Full_View (Parent_Type))
8497 then
8498 -- In this case, the full view of the parent type will become
8499 -- visible in the body of the enclosing child, and only then will
8500 -- the current type be possibly non-private. Build an underlying
8501 -- full view that will be installed when the enclosing child body
8502 -- is compiled.
8504 if Present (Underlying_Full_View (Derived_Type)) then
8505 Full_Der := Underlying_Full_View (Derived_Type);
8506 else
8507 Build_Full_Derivation;
8508 Set_Underlying_Full_View (Derived_Type, Full_Der);
8509 Set_Is_Underlying_Full_View (Full_Der);
8510 end if;
8512 -- The full view will be used to swap entities on entry/exit to
8513 -- the body, and must appear in the entity list for the package.
8515 Append_Entity (Full_Der, Scope (Derived_Type));
8516 end if;
8517 end if;
8518 end Build_Derived_Private_Type;
8520 -------------------------------
8521 -- Build_Derived_Record_Type --
8522 -------------------------------
8524 -- 1. INTRODUCTION
8526 -- Ideally we would like to use the same model of type derivation for
8527 -- tagged and untagged record types. Unfortunately this is not quite
8528 -- possible because the semantics of representation clauses is different
8529 -- for tagged and untagged records under inheritance. Consider the
8530 -- following:
8532 -- type R (...) is [tagged] record ... end record;
8533 -- type T (...) is new R (...) [with ...];
8535 -- The representation clauses for T can specify a completely different
8536 -- record layout from R's. Hence the same component can be placed in two
8537 -- very different positions in objects of type T and R. If R and T are
8538 -- tagged types, representation clauses for T can only specify the layout
8539 -- of non inherited components, thus components that are common in R and T
8540 -- have the same position in objects of type R and T.
8542 -- This has two implications. The first is that the entire tree for R's
8543 -- declaration needs to be copied for T in the untagged case, so that T
8544 -- can be viewed as a record type of its own with its own representation
8545 -- clauses. The second implication is the way we handle discriminants.
8546 -- Specifically, in the untagged case we need a way to communicate to Gigi
8547 -- what are the real discriminants in the record, while for the semantics
8548 -- we need to consider those introduced by the user to rename the
8549 -- discriminants in the parent type. This is handled by introducing the
8550 -- notion of stored discriminants. See below for more.
8552 -- Fortunately the way regular components are inherited can be handled in
8553 -- the same way in tagged and untagged types.
8555 -- To complicate things a bit more the private view of a private extension
8556 -- cannot be handled in the same way as the full view (for one thing the
8557 -- semantic rules are somewhat different). We will explain what differs
8558 -- below.
8560 -- 2. DISCRIMINANTS UNDER INHERITANCE
8562 -- The semantic rules governing the discriminants of derived types are
8563 -- quite subtle.
8565 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8566 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8568 -- If parent type has discriminants, then the discriminants that are
8569 -- declared in the derived type are [3.4 (11)]:
8571 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8572 -- there is one;
8574 -- o Otherwise, each discriminant of the parent type (implicitly declared
8575 -- in the same order with the same specifications). In this case, the
8576 -- discriminants are said to be "inherited", or if unknown in the parent
8577 -- are also unknown in the derived type.
8579 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8581 -- o The parent subtype must be constrained;
8583 -- o If the parent type is not a tagged type, then each discriminant of
8584 -- the derived type must be used in the constraint defining a parent
8585 -- subtype. [Implementation note: This ensures that the new discriminant
8586 -- can share storage with an existing discriminant.]
8588 -- For the derived type each discriminant of the parent type is either
8589 -- inherited, constrained to equal some new discriminant of the derived
8590 -- type, or constrained to the value of an expression.
8592 -- When inherited or constrained to equal some new discriminant, the
8593 -- parent discriminant and the discriminant of the derived type are said
8594 -- to "correspond".
8596 -- If a discriminant of the parent type is constrained to a specific value
8597 -- in the derived type definition, then the discriminant is said to be
8598 -- "specified" by that derived type definition.
8600 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8602 -- We have spoken about stored discriminants in point 1 (introduction)
8603 -- above. There are two sorts of stored discriminants: implicit and
8604 -- explicit. As long as the derived type inherits the same discriminants as
8605 -- the root record type, stored discriminants are the same as regular
8606 -- discriminants, and are said to be implicit. However, if any discriminant
8607 -- in the root type was renamed in the derived type, then the derived
8608 -- type will contain explicit stored discriminants. Explicit stored
8609 -- discriminants are discriminants in addition to the semantically visible
8610 -- discriminants defined for the derived type. Stored discriminants are
8611 -- used by Gigi to figure out what are the physical discriminants in
8612 -- objects of the derived type (see precise definition in einfo.ads).
8613 -- As an example, consider the following:
8615 -- type R (D1, D2, D3 : Int) is record ... end record;
8616 -- type T1 is new R;
8617 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8618 -- type T3 is new T2;
8619 -- type T4 (Y : Int) is new T3 (Y, 99);
8621 -- The following table summarizes the discriminants and stored
8622 -- discriminants in R and T1 through T4:
8624 -- Type Discrim Stored Discrim Comment
8625 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8626 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8627 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8628 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8629 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8631 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8632 -- find the corresponding discriminant in the parent type, while
8633 -- Original_Record_Component (abbreviated ORC below) the actual physical
8634 -- component that is renamed. Finally the field Is_Completely_Hidden
8635 -- (abbreviated ICH below) is set for all explicit stored discriminants
8636 -- (see einfo.ads for more info). For the above example this gives:
8638 -- Discrim CD ORC ICH
8639 -- ^^^^^^^ ^^ ^^^ ^^^
8640 -- D1 in R empty itself no
8641 -- D2 in R empty itself no
8642 -- D3 in R empty itself no
8644 -- D1 in T1 D1 in R itself no
8645 -- D2 in T1 D2 in R itself no
8646 -- D3 in T1 D3 in R itself no
8648 -- X1 in T2 D3 in T1 D3 in T2 no
8649 -- X2 in T2 D1 in T1 D1 in T2 no
8650 -- D1 in T2 empty itself yes
8651 -- D2 in T2 empty itself yes
8652 -- D3 in T2 empty itself yes
8654 -- X1 in T3 X1 in T2 D3 in T3 no
8655 -- X2 in T3 X2 in T2 D1 in T3 no
8656 -- D1 in T3 empty itself yes
8657 -- D2 in T3 empty itself yes
8658 -- D3 in T3 empty itself yes
8660 -- Y in T4 X1 in T3 D3 in T4 no
8661 -- D1 in T4 empty itself yes
8662 -- D2 in T4 empty itself yes
8663 -- D3 in T4 empty itself yes
8665 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8667 -- Type derivation for tagged types is fairly straightforward. If no
8668 -- discriminants are specified by the derived type, these are inherited
8669 -- from the parent. No explicit stored discriminants are ever necessary.
8670 -- The only manipulation that is done to the tree is that of adding a
8671 -- _parent field with parent type and constrained to the same constraint
8672 -- specified for the parent in the derived type definition. For instance:
8674 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8675 -- type T1 is new R with null record;
8676 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8678 -- are changed into:
8680 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8681 -- _parent : R (D1, D2, D3);
8682 -- end record;
8684 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8685 -- _parent : T1 (X2, 88, X1);
8686 -- end record;
8688 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8689 -- ORC and ICH fields are:
8691 -- Discrim CD ORC ICH
8692 -- ^^^^^^^ ^^ ^^^ ^^^
8693 -- D1 in R empty itself no
8694 -- D2 in R empty itself no
8695 -- D3 in R empty itself no
8697 -- D1 in T1 D1 in R D1 in R no
8698 -- D2 in T1 D2 in R D2 in R no
8699 -- D3 in T1 D3 in R D3 in R no
8701 -- X1 in T2 D3 in T1 D3 in R no
8702 -- X2 in T2 D1 in T1 D1 in R no
8704 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8706 -- Regardless of whether we dealing with a tagged or untagged type
8707 -- we will transform all derived type declarations of the form
8709 -- type T is new R (...) [with ...];
8710 -- or
8711 -- subtype S is R (...);
8712 -- type T is new S [with ...];
8713 -- into
8714 -- type BT is new R [with ...];
8715 -- subtype T is BT (...);
8717 -- That is, the base derived type is constrained only if it has no
8718 -- discriminants. The reason for doing this is that GNAT's semantic model
8719 -- assumes that a base type with discriminants is unconstrained.
8721 -- Note that, strictly speaking, the above transformation is not always
8722 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8724 -- procedure B34011A is
8725 -- type REC (D : integer := 0) is record
8726 -- I : Integer;
8727 -- end record;
8729 -- package P is
8730 -- type T6 is new Rec;
8731 -- function F return T6;
8732 -- end P;
8734 -- use P;
8735 -- package Q6 is
8736 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8737 -- end Q6;
8739 -- The definition of Q6.U is illegal. However transforming Q6.U into
8741 -- type BaseU is new T6;
8742 -- subtype U is BaseU (Q6.F.I)
8744 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8745 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8746 -- the transformation described above.
8748 -- There is another instance where the above transformation is incorrect.
8749 -- Consider:
8751 -- package Pack is
8752 -- type Base (D : Integer) is tagged null record;
8753 -- procedure P (X : Base);
8755 -- type Der is new Base (2) with null record;
8756 -- procedure P (X : Der);
8757 -- end Pack;
8759 -- Then the above transformation turns this into
8761 -- type Der_Base is new Base with null record;
8762 -- -- procedure P (X : Base) is implicitly inherited here
8763 -- -- as procedure P (X : Der_Base).
8765 -- subtype Der is Der_Base (2);
8766 -- procedure P (X : Der);
8767 -- -- The overriding of P (X : Der_Base) is illegal since we
8768 -- -- have a parameter conformance problem.
8770 -- To get around this problem, after having semantically processed Der_Base
8771 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8772 -- Discriminant_Constraint from Der so that when parameter conformance is
8773 -- checked when P is overridden, no semantic errors are flagged.
8775 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8777 -- Regardless of whether we are dealing with a tagged or untagged type
8778 -- we will transform all derived type declarations of the form
8780 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8781 -- type T is new R [with ...];
8782 -- into
8783 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8785 -- The reason for such transformation is that it allows us to implement a
8786 -- very clean form of component inheritance as explained below.
8788 -- Note that this transformation is not achieved by direct tree rewriting
8789 -- and manipulation, but rather by redoing the semantic actions that the
8790 -- above transformation will entail. This is done directly in routine
8791 -- Inherit_Components.
8793 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8795 -- In both tagged and untagged derived types, regular non discriminant
8796 -- components are inherited in the derived type from the parent type. In
8797 -- the absence of discriminants component, inheritance is straightforward
8798 -- as components can simply be copied from the parent.
8800 -- If the parent has discriminants, inheriting components constrained with
8801 -- these discriminants requires caution. Consider the following example:
8803 -- type R (D1, D2 : Positive) is [tagged] record
8804 -- S : String (D1 .. D2);
8805 -- end record;
8807 -- type T1 is new R [with null record];
8808 -- type T2 (X : positive) is new R (1, X) [with null record];
8810 -- As explained in 6. above, T1 is rewritten as
8811 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8812 -- which makes the treatment for T1 and T2 identical.
8814 -- What we want when inheriting S, is that references to D1 and D2 in R are
8815 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8816 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8817 -- with either discriminant references in the derived type or expressions.
8818 -- This replacement is achieved as follows: before inheriting R's
8819 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8820 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8821 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8822 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8823 -- by String (1 .. X).
8825 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8827 -- We explain here the rules governing private type extensions relevant to
8828 -- type derivation. These rules are explained on the following example:
8830 -- type D [(...)] is new A [(...)] with private; <-- partial view
8831 -- type D [(...)] is new P [(...)] with null record; <-- full view
8833 -- Type A is called the ancestor subtype of the private extension.
8834 -- Type P is the parent type of the full view of the private extension. It
8835 -- must be A or a type derived from A.
8837 -- The rules concerning the discriminants of private type extensions are
8838 -- [7.3(10-13)]:
8840 -- o If a private extension inherits known discriminants from the ancestor
8841 -- subtype, then the full view must also inherit its discriminants from
8842 -- the ancestor subtype and the parent subtype of the full view must be
8843 -- constrained if and only if the ancestor subtype is constrained.
8845 -- o If a partial view has unknown discriminants, then the full view may
8846 -- define a definite or an indefinite subtype, with or without
8847 -- discriminants.
8849 -- o If a partial view has neither known nor unknown discriminants, then
8850 -- the full view must define a definite subtype.
8852 -- o If the ancestor subtype of a private extension has constrained
8853 -- discriminants, then the parent subtype of the full view must impose a
8854 -- statically matching constraint on those discriminants.
8856 -- This means that only the following forms of private extensions are
8857 -- allowed:
8859 -- type D is new A with private; <-- partial view
8860 -- type D is new P with null record; <-- full view
8862 -- If A has no discriminants than P has no discriminants, otherwise P must
8863 -- inherit A's discriminants.
8865 -- type D is new A (...) with private; <-- partial view
8866 -- type D is new P (:::) with null record; <-- full view
8868 -- P must inherit A's discriminants and (...) and (:::) must statically
8869 -- match.
8871 -- subtype A is R (...);
8872 -- type D is new A with private; <-- partial view
8873 -- type D is new P with null record; <-- full view
8875 -- P must have inherited R's discriminants and must be derived from A or
8876 -- any of its subtypes.
8878 -- type D (..) is new A with private; <-- partial view
8879 -- type D (..) is new P [(:::)] with null record; <-- full view
8881 -- No specific constraints on P's discriminants or constraint (:::).
8882 -- Note that A can be unconstrained, but the parent subtype P must either
8883 -- be constrained or (:::) must be present.
8885 -- type D (..) is new A [(...)] with private; <-- partial view
8886 -- type D (..) is new P [(:::)] with null record; <-- full view
8888 -- P's constraints on A's discriminants must statically match those
8889 -- imposed by (...).
8891 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8893 -- The full view of a private extension is handled exactly as described
8894 -- above. The model chose for the private view of a private extension is
8895 -- the same for what concerns discriminants (i.e. they receive the same
8896 -- treatment as in the tagged case). However, the private view of the
8897 -- private extension always inherits the components of the parent base,
8898 -- without replacing any discriminant reference. Strictly speaking this is
8899 -- incorrect. However, Gigi never uses this view to generate code so this
8900 -- is a purely semantic issue. In theory, a set of transformations similar
8901 -- to those given in 5. and 6. above could be applied to private views of
8902 -- private extensions to have the same model of component inheritance as
8903 -- for non private extensions. However, this is not done because it would
8904 -- further complicate private type processing. Semantically speaking, this
8905 -- leaves us in an uncomfortable situation. As an example consider:
8907 -- package Pack is
8908 -- type R (D : integer) is tagged record
8909 -- S : String (1 .. D);
8910 -- end record;
8911 -- procedure P (X : R);
8912 -- type T is new R (1) with private;
8913 -- private
8914 -- type T is new R (1) with null record;
8915 -- end;
8917 -- This is transformed into:
8919 -- package Pack is
8920 -- type R (D : integer) is tagged record
8921 -- S : String (1 .. D);
8922 -- end record;
8923 -- procedure P (X : R);
8924 -- type T is new R (1) with private;
8925 -- private
8926 -- type BaseT is new R with null record;
8927 -- subtype T is BaseT (1);
8928 -- end;
8930 -- (strictly speaking the above is incorrect Ada)
8932 -- From the semantic standpoint the private view of private extension T
8933 -- should be flagged as constrained since one can clearly have
8935 -- Obj : T;
8937 -- in a unit withing Pack. However, when deriving subprograms for the
8938 -- private view of private extension T, T must be seen as unconstrained
8939 -- since T has discriminants (this is a constraint of the current
8940 -- subprogram derivation model). Thus, when processing the private view of
8941 -- a private extension such as T, we first mark T as unconstrained, we
8942 -- process it, we perform program derivation and just before returning from
8943 -- Build_Derived_Record_Type we mark T as constrained.
8945 -- ??? Are there are other uncomfortable cases that we will have to
8946 -- deal with.
8948 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8950 -- Types that are derived from a visible record type and have a private
8951 -- extension present other peculiarities. They behave mostly like private
8952 -- types, but if they have primitive operations defined, these will not
8953 -- have the proper signatures for further inheritance, because other
8954 -- primitive operations will use the implicit base that we define for
8955 -- private derivations below. This affect subprogram inheritance (see
8956 -- Derive_Subprograms for details). We also derive the implicit base from
8957 -- the base type of the full view, so that the implicit base is a record
8958 -- type and not another private type, This avoids infinite loops.
8960 procedure Build_Derived_Record_Type
8961 (N : Node_Id;
8962 Parent_Type : Entity_Id;
8963 Derived_Type : Entity_Id;
8964 Derive_Subps : Boolean := True)
8966 Discriminant_Specs : constant Boolean :=
8967 Present (Discriminant_Specifications (N));
8968 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8969 Loc : constant Source_Ptr := Sloc (N);
8970 Private_Extension : constant Boolean :=
8971 Nkind (N) = N_Private_Extension_Declaration;
8972 Assoc_List : Elist_Id;
8973 Constraint_Present : Boolean;
8974 Constrs : Elist_Id;
8975 Discrim : Entity_Id;
8976 Indic : Node_Id;
8977 Inherit_Discrims : Boolean := False;
8978 Last_Discrim : Entity_Id;
8979 New_Base : Entity_Id;
8980 New_Decl : Node_Id;
8981 New_Discrs : Elist_Id;
8982 New_Indic : Node_Id;
8983 Parent_Base : Entity_Id;
8984 Save_Etype : Entity_Id;
8985 Save_Discr_Constr : Elist_Id;
8986 Save_Next_Entity : Entity_Id;
8987 Type_Def : Node_Id;
8989 Discs : Elist_Id := New_Elmt_List;
8990 -- An empty Discs list means that there were no constraints in the
8991 -- subtype indication or that there was an error processing it.
8993 procedure Check_Generic_Ancestors;
8994 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8995 -- cannot be declared at a deeper level than its parent type is
8996 -- removed. The check on derivation within a generic body is also
8997 -- relaxed, but there's a restriction that a derived tagged type
8998 -- cannot be declared in a generic body if it's derived directly
8999 -- or indirectly from a formal type of that generic. This applies
9000 -- to progenitors as well.
9002 -----------------------------
9003 -- Check_Generic_Ancestors --
9004 -----------------------------
9006 procedure Check_Generic_Ancestors is
9007 Ancestor_Type : Entity_Id;
9008 Intf_List : List_Id;
9009 Intf_Name : Node_Id;
9011 procedure Check_Ancestor;
9012 -- For parent and progenitors.
9014 --------------------
9015 -- Check_Ancestor --
9016 --------------------
9018 procedure Check_Ancestor is
9019 begin
9020 -- If the derived type does have a formal type as an ancestor
9021 -- then it's an error if the derived type is declared within
9022 -- the body of the generic unit that declares the formal type
9023 -- in its generic formal part. It's sufficient to check whether
9024 -- the ancestor type is declared inside the same generic body
9025 -- as the derived type (such as within a nested generic spec),
9026 -- in which case the derivation is legal. If the formal type is
9027 -- declared outside of that generic body, then it's certain
9028 -- that the derived type is declared within the generic body
9029 -- of the generic unit declaring the formal type.
9031 if Is_Generic_Type (Ancestor_Type)
9032 and then Enclosing_Generic_Body (Ancestor_Type) /=
9033 Enclosing_Generic_Body (Derived_Type)
9034 then
9035 Error_Msg_NE
9036 ("ancestor type& is formal type of enclosing"
9037 & " generic unit (RM 3.9.1 (4/2))",
9038 Indic, Ancestor_Type);
9039 end if;
9040 end Check_Ancestor;
9042 begin
9043 if Nkind (N) = N_Private_Extension_Declaration then
9044 Intf_List := Interface_List (N);
9045 else
9046 Intf_List := Interface_List (Type_Definition (N));
9047 end if;
9049 if Present (Enclosing_Generic_Body (Derived_Type)) then
9050 Ancestor_Type := Parent_Type;
9052 while not Is_Generic_Type (Ancestor_Type)
9053 and then Etype (Ancestor_Type) /= Ancestor_Type
9054 loop
9055 Ancestor_Type := Etype (Ancestor_Type);
9056 end loop;
9058 Check_Ancestor;
9060 if Present (Intf_List) then
9061 Intf_Name := First (Intf_List);
9062 while Present (Intf_Name) loop
9063 Ancestor_Type := Entity (Intf_Name);
9064 Check_Ancestor;
9065 Next (Intf_Name);
9066 end loop;
9067 end if;
9068 end if;
9069 end Check_Generic_Ancestors;
9071 -- Start of processing for Build_Derived_Record_Type
9073 begin
9074 if Ekind (Parent_Type) = E_Record_Type_With_Private
9075 and then Present (Full_View (Parent_Type))
9076 and then Has_Discriminants (Parent_Type)
9077 then
9078 Parent_Base := Base_Type (Full_View (Parent_Type));
9079 else
9080 Parent_Base := Base_Type (Parent_Type);
9081 end if;
9083 -- If the parent type is declared as a subtype of another private
9084 -- type with inherited discriminants, its generated base type is
9085 -- itself a record subtype. To further inherit the constraint we
9086 -- need to use its own base to have an unconstrained type on which
9087 -- to apply the inherited constraint.
9089 if Ekind (Parent_Base) = E_Record_Subtype then
9090 Parent_Base := Base_Type (Parent_Base);
9091 end if;
9093 -- AI05-0115: if this is a derivation from a private type in some
9094 -- other scope that may lead to invisible components for the derived
9095 -- type, mark it accordingly.
9097 if Is_Private_Type (Parent_Type) then
9098 if Scope (Parent_Base) = Scope (Derived_Type) then
9099 null;
9101 elsif In_Open_Scopes (Scope (Parent_Base))
9102 and then In_Private_Part (Scope (Parent_Base))
9103 then
9104 null;
9106 else
9107 Set_Has_Private_Ancestor (Derived_Type);
9108 end if;
9110 else
9111 Set_Has_Private_Ancestor
9112 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9113 end if;
9115 -- Before we start the previously documented transformations, here is
9116 -- little fix for size and alignment of tagged types. Normally when we
9117 -- derive type D from type P, we copy the size and alignment of P as the
9118 -- default for D, and in the absence of explicit representation clauses
9119 -- for D, the size and alignment are indeed the same as the parent.
9121 -- But this is wrong for tagged types, since fields may be added, and
9122 -- the default size may need to be larger, and the default alignment may
9123 -- need to be larger.
9125 -- We therefore reset the size and alignment fields in the tagged case.
9126 -- Note that the size and alignment will in any case be at least as
9127 -- large as the parent type (since the derived type has a copy of the
9128 -- parent type in the _parent field)
9130 -- The type is also marked as being tagged here, which is needed when
9131 -- processing components with a self-referential anonymous access type
9132 -- in the call to Check_Anonymous_Access_Components below. Note that
9133 -- this flag is also set later on for completeness.
9135 if Is_Tagged then
9136 Set_Is_Tagged_Type (Derived_Type);
9137 Reinit_Size_Align (Derived_Type);
9138 end if;
9140 -- STEP 0a: figure out what kind of derived type declaration we have
9142 if Private_Extension then
9143 Type_Def := N;
9144 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9145 Set_Default_SSO (Derived_Type);
9146 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9148 else
9149 Type_Def := Type_Definition (N);
9151 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9152 -- Parent_Base can be a private type or private extension. However,
9153 -- for tagged types with an extension the newly added fields are
9154 -- visible and hence the Derived_Type is always an E_Record_Type.
9155 -- (except that the parent may have its own private fields).
9156 -- For untagged types we preserve the Ekind of the Parent_Base.
9158 if Present (Record_Extension_Part (Type_Def)) then
9159 Mutate_Ekind (Derived_Type, E_Record_Type);
9160 Set_Default_SSO (Derived_Type);
9161 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9163 -- Create internal access types for components with anonymous
9164 -- access types.
9166 if Ada_Version >= Ada_2005 then
9167 Check_Anonymous_Access_Components
9168 (N, Derived_Type, Derived_Type,
9169 Component_List (Record_Extension_Part (Type_Def)));
9170 end if;
9172 else
9173 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9174 end if;
9175 end if;
9177 -- Indic can either be an N_Identifier if the subtype indication
9178 -- contains no constraint or an N_Subtype_Indication if the subtype
9179 -- indication has a constraint. In either case it can include an
9180 -- interface list.
9182 Indic := Subtype_Indication (Type_Def);
9183 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9185 -- Check that the type has visible discriminants. The type may be
9186 -- a private type with unknown discriminants whose full view has
9187 -- discriminants which are invisible.
9189 if Constraint_Present then
9190 if not Has_Discriminants (Parent_Base)
9191 or else
9192 (Has_Unknown_Discriminants (Parent_Base)
9193 and then Is_Private_Type (Parent_Base))
9194 then
9195 Error_Msg_N
9196 ("invalid constraint: type has no discriminant",
9197 Constraint (Indic));
9199 Constraint_Present := False;
9200 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9202 elsif Is_Constrained (Parent_Type) then
9203 Error_Msg_N
9204 ("invalid constraint: parent type is already constrained",
9205 Constraint (Indic));
9207 Constraint_Present := False;
9208 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9209 end if;
9210 end if;
9212 -- STEP 0b: If needed, apply transformation given in point 5. above
9214 if not Private_Extension
9215 and then Has_Discriminants (Parent_Type)
9216 and then not Discriminant_Specs
9217 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9218 then
9219 -- First, we must analyze the constraint (see comment in point 5.)
9220 -- The constraint may come from the subtype indication of the full
9221 -- declaration.
9223 if Constraint_Present then
9224 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9226 -- If there is no explicit constraint, there might be one that is
9227 -- inherited from a constrained parent type. In that case verify that
9228 -- it conforms to the constraint in the partial view. In perverse
9229 -- cases the parent subtypes of the partial and full view can have
9230 -- different constraints.
9232 elsif Present (Stored_Constraint (Parent_Type)) then
9233 New_Discrs := Stored_Constraint (Parent_Type);
9235 else
9236 New_Discrs := No_Elist;
9237 end if;
9239 if Has_Discriminants (Derived_Type)
9240 and then Has_Private_Declaration (Derived_Type)
9241 and then Present (Discriminant_Constraint (Derived_Type))
9242 and then Present (New_Discrs)
9243 then
9244 -- Verify that constraints of the full view statically match
9245 -- those given in the partial view.
9247 declare
9248 C1, C2 : Elmt_Id;
9250 begin
9251 C1 := First_Elmt (New_Discrs);
9252 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9253 while Present (C1) and then Present (C2) loop
9254 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9255 or else
9256 (Is_OK_Static_Expression (Node (C1))
9257 and then Is_OK_Static_Expression (Node (C2))
9258 and then
9259 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9260 then
9261 null;
9263 else
9264 if Constraint_Present then
9265 Error_Msg_N
9266 ("constraint not conformant to previous declaration",
9267 Node (C1));
9268 else
9269 Error_Msg_N
9270 ("constraint of full view is incompatible "
9271 & "with partial view", N);
9272 end if;
9273 end if;
9275 Next_Elmt (C1);
9276 Next_Elmt (C2);
9277 end loop;
9278 end;
9279 end if;
9281 -- Insert and analyze the declaration for the unconstrained base type
9283 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9285 New_Decl :=
9286 Make_Full_Type_Declaration (Loc,
9287 Defining_Identifier => New_Base,
9288 Type_Definition =>
9289 Make_Derived_Type_Definition (Loc,
9290 Abstract_Present => Abstract_Present (Type_Def),
9291 Limited_Present => Limited_Present (Type_Def),
9292 Subtype_Indication =>
9293 New_Occurrence_Of (Parent_Base, Loc),
9294 Record_Extension_Part =>
9295 Relocate_Node (Record_Extension_Part (Type_Def)),
9296 Interface_List => Interface_List (Type_Def)));
9298 Set_Parent (New_Decl, Parent (N));
9299 Mark_Rewrite_Insertion (New_Decl);
9300 Insert_Before (N, New_Decl);
9302 -- In the extension case, make sure ancestor is frozen appropriately
9303 -- (see also non-discriminated case below).
9305 if Present (Record_Extension_Part (Type_Def))
9306 or else Is_Interface (Parent_Base)
9307 then
9308 Freeze_Before (New_Decl, Parent_Type);
9309 end if;
9311 -- Note that this call passes False for the Derive_Subps parameter
9312 -- because subprogram derivation is deferred until after creating
9313 -- the subtype (see below).
9315 Build_Derived_Type
9316 (New_Decl, Parent_Base, New_Base,
9317 Is_Completion => False, Derive_Subps => False);
9319 -- ??? This needs re-examination to determine whether the
9320 -- above call can simply be replaced by a call to Analyze.
9322 Set_Analyzed (New_Decl);
9324 -- Insert and analyze the declaration for the constrained subtype
9326 if Constraint_Present then
9327 New_Indic :=
9328 Make_Subtype_Indication (Loc,
9329 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9330 Constraint => Relocate_Node (Constraint (Indic)));
9332 else
9333 declare
9334 Constr_List : constant List_Id := New_List;
9335 C : Elmt_Id;
9336 Expr : Node_Id;
9338 begin
9339 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9340 while Present (C) loop
9341 Expr := Node (C);
9343 -- It is safe here to call New_Copy_Tree since we called
9344 -- Force_Evaluation on each constraint previously
9345 -- in Build_Discriminant_Constraints.
9347 Append (New_Copy_Tree (Expr), To => Constr_List);
9349 Next_Elmt (C);
9350 end loop;
9352 New_Indic :=
9353 Make_Subtype_Indication (Loc,
9354 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9355 Constraint =>
9356 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9357 end;
9358 end if;
9360 Rewrite (N,
9361 Make_Subtype_Declaration (Loc,
9362 Defining_Identifier => Derived_Type,
9363 Subtype_Indication => New_Indic));
9365 Analyze (N);
9367 -- Derivation of subprograms must be delayed until the full subtype
9368 -- has been established, to ensure proper overriding of subprograms
9369 -- inherited by full types. If the derivations occurred as part of
9370 -- the call to Build_Derived_Type above, then the check for type
9371 -- conformance would fail because earlier primitive subprograms
9372 -- could still refer to the full type prior the change to the new
9373 -- subtype and hence would not match the new base type created here.
9374 -- Subprograms are not derived, however, when Derive_Subps is False
9375 -- (since otherwise there could be redundant derivations).
9377 if Derive_Subps then
9378 Derive_Subprograms (Parent_Type, Derived_Type);
9379 end if;
9381 -- For tagged types the Discriminant_Constraint of the new base itype
9382 -- is inherited from the first subtype so that no subtype conformance
9383 -- problem arise when the first subtype overrides primitive
9384 -- operations inherited by the implicit base type.
9386 if Is_Tagged then
9387 Set_Discriminant_Constraint
9388 (New_Base, Discriminant_Constraint (Derived_Type));
9389 end if;
9391 return;
9392 end if;
9394 -- If we get here Derived_Type will have no discriminants or it will be
9395 -- a discriminated unconstrained base type.
9397 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9399 if Is_Tagged then
9401 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9402 -- The declaration of a specific descendant of an interface type
9403 -- freezes the interface type (RM 13.14).
9405 if not Private_Extension or else Is_Interface (Parent_Base) then
9406 Freeze_Before (N, Parent_Type);
9407 end if;
9409 if Ada_Version >= Ada_2005 then
9410 Check_Generic_Ancestors;
9412 elsif Type_Access_Level (Derived_Type) /=
9413 Type_Access_Level (Parent_Type)
9414 and then not Is_Generic_Type (Derived_Type)
9415 then
9416 if Is_Controlled (Parent_Type) then
9417 Error_Msg_N
9418 ("controlled type must be declared at the library level",
9419 Indic);
9420 else
9421 Error_Msg_N
9422 ("type extension at deeper accessibility level than parent",
9423 Indic);
9424 end if;
9426 else
9427 declare
9428 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9429 begin
9430 if Present (GB)
9431 and then GB /= Enclosing_Generic_Body (Parent_Base)
9432 then
9433 Error_Msg_NE
9434 ("parent type of& must not be outside generic body"
9435 & " (RM 3.9.1(4))",
9436 Indic, Derived_Type);
9437 end if;
9438 end;
9439 end if;
9440 end if;
9442 -- Ada 2005 (AI-251)
9444 if Ada_Version >= Ada_2005 and then Is_Tagged then
9446 -- "The declaration of a specific descendant of an interface type
9447 -- freezes the interface type" (RM 13.14).
9449 declare
9450 Iface : Node_Id;
9451 begin
9452 Iface := First (Interface_List (Type_Def));
9453 while Present (Iface) loop
9454 Freeze_Before (N, Etype (Iface));
9455 Next (Iface);
9456 end loop;
9457 end;
9458 end if;
9460 -- STEP 1b : preliminary cleanup of the full view of private types
9462 -- If the type is already marked as having discriminants, then it's the
9463 -- completion of a private type or private extension and we need to
9464 -- retain the discriminants from the partial view if the current
9465 -- declaration has Discriminant_Specifications so that we can verify
9466 -- conformance. However, we must remove any existing components that
9467 -- were inherited from the parent (and attached in Copy_And_Swap)
9468 -- because the full type inherits all appropriate components anyway, and
9469 -- we do not want the partial view's components interfering.
9471 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9472 Discrim := First_Discriminant (Derived_Type);
9473 loop
9474 Last_Discrim := Discrim;
9475 Next_Discriminant (Discrim);
9476 exit when No (Discrim);
9477 end loop;
9479 Set_Last_Entity (Derived_Type, Last_Discrim);
9481 -- In all other cases wipe out the list of inherited components (even
9482 -- inherited discriminants), it will be properly rebuilt here.
9484 else
9485 Set_First_Entity (Derived_Type, Empty);
9486 Set_Last_Entity (Derived_Type, Empty);
9487 end if;
9489 -- STEP 1c: Initialize some flags for the Derived_Type
9491 -- The following flags must be initialized here so that
9492 -- Process_Discriminants can check that discriminants of tagged types do
9493 -- not have a default initial value and that access discriminants are
9494 -- only specified for limited records. For completeness, these flags are
9495 -- also initialized along with all the other flags below.
9497 -- AI-419: Limitedness is not inherited from an interface parent, so to
9498 -- be limited in that case the type must be explicitly declared as
9499 -- limited. However, task and protected interfaces are always limited.
9501 if Limited_Present (Type_Def) then
9502 Set_Is_Limited_Record (Derived_Type);
9504 elsif Is_Limited_Record (Parent_Type)
9505 or else (Present (Full_View (Parent_Type))
9506 and then Is_Limited_Record (Full_View (Parent_Type)))
9507 then
9508 if not Is_Interface (Parent_Type)
9509 or else Is_Concurrent_Interface (Parent_Type)
9510 then
9511 Set_Is_Limited_Record (Derived_Type);
9512 end if;
9513 end if;
9515 -- STEP 2a: process discriminants of derived type if any
9517 Push_Scope (Derived_Type);
9519 if Discriminant_Specs then
9520 Set_Has_Unknown_Discriminants (Derived_Type, False);
9522 -- The following call initializes fields Has_Discriminants and
9523 -- Discriminant_Constraint, unless we are processing the completion
9524 -- of a private type declaration.
9526 Check_Or_Process_Discriminants (N, Derived_Type);
9528 -- For untagged types, the constraint on the Parent_Type must be
9529 -- present and is used to rename the discriminants.
9531 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9532 Error_Msg_N ("untagged parent must have discriminants", Indic);
9534 elsif not Is_Tagged and then not Constraint_Present then
9535 Error_Msg_N
9536 ("discriminant constraint needed for derived untagged records",
9537 Indic);
9539 -- Otherwise the parent subtype must be constrained unless we have a
9540 -- private extension.
9542 elsif not Constraint_Present
9543 and then not Private_Extension
9544 and then not Is_Constrained (Parent_Type)
9545 then
9546 Error_Msg_N
9547 ("unconstrained type not allowed in this context", Indic);
9549 elsif Constraint_Present then
9550 -- The following call sets the field Corresponding_Discriminant
9551 -- for the discriminants in the Derived_Type.
9553 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9555 -- For untagged types all new discriminants must rename
9556 -- discriminants in the parent. For private extensions new
9557 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9559 Discrim := First_Discriminant (Derived_Type);
9560 while Present (Discrim) loop
9561 if not Is_Tagged
9562 and then No (Corresponding_Discriminant (Discrim))
9563 then
9564 Error_Msg_N
9565 ("new discriminants must constrain old ones", Discrim);
9567 elsif Private_Extension
9568 and then Present (Corresponding_Discriminant (Discrim))
9569 then
9570 Error_Msg_N
9571 ("only static constraints allowed for parent"
9572 & " discriminants in the partial view", Indic);
9573 exit;
9574 end if;
9576 -- If a new discriminant is used in the constraint, then its
9577 -- subtype must be statically compatible with the subtype of
9578 -- the parent discriminant (RM 3.7(15)).
9580 if Present (Corresponding_Discriminant (Discrim)) then
9581 Check_Constraining_Discriminant
9582 (Discrim, Corresponding_Discriminant (Discrim));
9583 end if;
9585 Next_Discriminant (Discrim);
9586 end loop;
9588 -- Check whether the constraints of the full view statically
9589 -- match those imposed by the parent subtype [7.3(13)].
9591 if Present (Stored_Constraint (Derived_Type)) then
9592 declare
9593 C1, C2 : Elmt_Id;
9595 begin
9596 C1 := First_Elmt (Discs);
9597 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9598 while Present (C1) and then Present (C2) loop
9599 if not
9600 Fully_Conformant_Expressions (Node (C1), Node (C2))
9601 then
9602 Error_Msg_N
9603 ("not conformant with previous declaration",
9604 Node (C1));
9605 end if;
9607 Next_Elmt (C1);
9608 Next_Elmt (C2);
9609 end loop;
9610 end;
9611 end if;
9612 end if;
9614 -- STEP 2b: No new discriminants, inherit discriminants if any
9616 else
9617 if Private_Extension then
9618 Set_Has_Unknown_Discriminants
9619 (Derived_Type,
9620 Has_Unknown_Discriminants (Parent_Type)
9621 or else Unknown_Discriminants_Present (N));
9623 -- The partial view of the parent may have unknown discriminants,
9624 -- but if the full view has discriminants and the parent type is
9625 -- in scope they must be inherited.
9627 elsif Has_Unknown_Discriminants (Parent_Type)
9628 and then
9629 (not Has_Discriminants (Parent_Type)
9630 or else not In_Open_Scopes (Scope (Parent_Base)))
9631 then
9632 Set_Has_Unknown_Discriminants (Derived_Type);
9633 end if;
9635 if not Has_Unknown_Discriminants (Derived_Type)
9636 and then not Has_Unknown_Discriminants (Parent_Base)
9637 and then Has_Discriminants (Parent_Type)
9638 then
9639 Inherit_Discrims := True;
9640 Set_Has_Discriminants
9641 (Derived_Type, True);
9642 Set_Discriminant_Constraint
9643 (Derived_Type, Discriminant_Constraint (Parent_Base));
9644 end if;
9646 -- The following test is true for private types (remember
9647 -- transformation 5. is not applied to those) and in an error
9648 -- situation.
9650 if Constraint_Present then
9651 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9652 end if;
9654 -- For now mark a new derived type as constrained only if it has no
9655 -- discriminants. At the end of Build_Derived_Record_Type we properly
9656 -- set this flag in the case of private extensions. See comments in
9657 -- point 9. just before body of Build_Derived_Record_Type.
9659 Set_Is_Constrained
9660 (Derived_Type,
9661 not (Inherit_Discrims
9662 or else Has_Unknown_Discriminants (Derived_Type)));
9663 end if;
9665 -- STEP 3: initialize fields of derived type
9667 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9668 Set_Stored_Constraint (Derived_Type, No_Elist);
9670 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9671 -- but cannot be interfaces
9673 if not Private_Extension
9674 and then Ekind (Derived_Type) /= E_Private_Type
9675 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9676 then
9677 if Interface_Present (Type_Def) then
9678 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9679 end if;
9681 Set_Interfaces (Derived_Type, No_Elist);
9682 end if;
9684 -- Fields inherited from the Parent_Type
9686 Set_Has_Specified_Layout
9687 (Derived_Type, Has_Specified_Layout (Parent_Type));
9688 Set_Is_Limited_Composite
9689 (Derived_Type, Is_Limited_Composite (Parent_Type));
9690 Set_Is_Private_Composite
9691 (Derived_Type, Is_Private_Composite (Parent_Type));
9693 if Is_Tagged_Type (Parent_Type) then
9694 Set_No_Tagged_Streams_Pragma
9695 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9696 end if;
9698 -- Fields inherited from the Parent_Base
9700 Set_Has_Controlled_Component
9701 (Derived_Type, Has_Controlled_Component (Parent_Base));
9702 Set_Has_Non_Standard_Rep
9703 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9704 Set_Has_Primitive_Operations
9705 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9707 -- Set fields for private derived types
9709 if Is_Private_Type (Derived_Type) then
9710 Set_Depends_On_Private (Derived_Type, True);
9711 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9712 end if;
9714 -- Inherit fields for non-private types. If this is the completion of a
9715 -- derivation from a private type, the parent itself is private and the
9716 -- attributes come from its full view, which must be present.
9718 if Is_Record_Type (Derived_Type) then
9719 declare
9720 Parent_Full : Entity_Id;
9722 begin
9723 if Is_Private_Type (Parent_Base)
9724 and then not Is_Record_Type (Parent_Base)
9725 then
9726 Parent_Full := Full_View (Parent_Base);
9727 else
9728 Parent_Full := Parent_Base;
9729 end if;
9731 Set_Component_Alignment
9732 (Derived_Type, Component_Alignment (Parent_Full));
9733 Set_C_Pass_By_Copy
9734 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9735 Set_Has_Complex_Representation
9736 (Derived_Type, Has_Complex_Representation (Parent_Full));
9738 -- For untagged types, inherit the layout by default to avoid
9739 -- costly changes of representation for type conversions.
9741 if not Is_Tagged then
9742 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9743 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9744 end if;
9745 end;
9746 end if;
9748 -- Initialize the list of primitive operations to an empty list,
9749 -- to cover tagged types as well as untagged types. For untagged
9750 -- types this is used either to analyze the call as legal when
9751 -- Extensions_Allowed is True, or to issue a better error message
9752 -- otherwise.
9754 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9756 -- Set fields for tagged types
9758 if Is_Tagged then
9759 -- All tagged types defined in Ada.Finalization are controlled
9761 if Chars (Scope (Derived_Type)) = Name_Finalization
9762 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9763 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9764 then
9765 Set_Is_Controlled_Active (Derived_Type);
9766 else
9767 Set_Is_Controlled_Active
9768 (Derived_Type, Is_Controlled_Active (Parent_Base));
9769 end if;
9771 -- Minor optimization: there is no need to generate the class-wide
9772 -- entity associated with an underlying record view.
9774 if not Is_Underlying_Record_View (Derived_Type) then
9775 Make_Class_Wide_Type (Derived_Type);
9776 end if;
9778 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9780 if Has_Discriminants (Derived_Type)
9781 and then Constraint_Present
9782 then
9783 Set_Stored_Constraint
9784 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9785 end if;
9787 if Ada_Version >= Ada_2005 then
9788 declare
9789 Ifaces_List : Elist_Id;
9791 begin
9792 -- Checks rules 3.9.4 (13/2 and 14/2)
9794 if Comes_From_Source (Derived_Type)
9795 and then not Is_Private_Type (Derived_Type)
9796 and then Is_Interface (Parent_Type)
9797 and then not Is_Interface (Derived_Type)
9798 then
9799 if Is_Task_Interface (Parent_Type) then
9800 Error_Msg_N
9801 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9802 Derived_Type);
9804 elsif Is_Protected_Interface (Parent_Type) then
9805 Error_Msg_N
9806 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9807 Derived_Type);
9808 end if;
9809 end if;
9811 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9813 Check_Interfaces (N, Type_Def);
9815 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9816 -- not already in the parents.
9818 Collect_Interfaces
9819 (T => Derived_Type,
9820 Ifaces_List => Ifaces_List,
9821 Exclude_Parents => True);
9823 Set_Interfaces (Derived_Type, Ifaces_List);
9825 -- If the derived type is the anonymous type created for
9826 -- a declaration whose parent has a constraint, propagate
9827 -- the interface list to the source type. This must be done
9828 -- prior to the completion of the analysis of the source type
9829 -- because the components in the extension may contain current
9830 -- instances whose legality depends on some ancestor.
9832 if Is_Itype (Derived_Type) then
9833 declare
9834 Def : constant Node_Id :=
9835 Associated_Node_For_Itype (Derived_Type);
9836 begin
9837 if Present (Def)
9838 and then Nkind (Def) = N_Full_Type_Declaration
9839 then
9840 Set_Interfaces
9841 (Defining_Identifier (Def), Ifaces_List);
9842 end if;
9843 end;
9844 end if;
9846 -- A type extension is automatically Ghost when one of its
9847 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9848 -- also inherited when the parent type is Ghost, but this is
9849 -- done in Build_Derived_Type as the mechanism also handles
9850 -- untagged derivations.
9852 if Implements_Ghost_Interface (Derived_Type) then
9853 Set_Is_Ghost_Entity (Derived_Type);
9854 end if;
9855 end;
9856 end if;
9857 end if;
9859 -- STEP 4: Inherit components from the parent base and constrain them.
9860 -- Apply the second transformation described in point 6. above.
9862 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9863 or else not Has_Discriminants (Parent_Type)
9864 or else not Is_Constrained (Parent_Type)
9865 then
9866 Constrs := Discs;
9867 else
9868 Constrs := Discriminant_Constraint (Parent_Type);
9869 end if;
9871 Assoc_List :=
9872 Inherit_Components
9873 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9875 -- STEP 5a: Copy the parent record declaration for untagged types
9877 Set_Has_Implicit_Dereference
9878 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9880 if not Is_Tagged then
9882 -- Discriminant_Constraint (Derived_Type) has been properly
9883 -- constructed. Save it and temporarily set it to Empty because we
9884 -- do not want the call to New_Copy_Tree below to mess this list.
9886 if Has_Discriminants (Derived_Type) then
9887 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9888 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9889 else
9890 Save_Discr_Constr := No_Elist;
9891 end if;
9893 -- Save the Etype field of Derived_Type. It is correctly set now,
9894 -- but the call to New_Copy tree may remap it to point to itself,
9895 -- which is not what we want. Ditto for the Next_Entity field.
9897 Save_Etype := Etype (Derived_Type);
9898 Save_Next_Entity := Next_Entity (Derived_Type);
9900 -- Assoc_List maps all stored discriminants in the Parent_Base to
9901 -- stored discriminants in the Derived_Type. It is fundamental that
9902 -- no types or itypes with discriminants other than the stored
9903 -- discriminants appear in the entities declared inside
9904 -- Derived_Type, since the back end cannot deal with it.
9906 New_Decl :=
9907 New_Copy_Tree
9908 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9909 Copy_Dimensions_Of_Components (Derived_Type);
9911 -- Restore the fields saved prior to the New_Copy_Tree call
9912 -- and compute the stored constraint.
9914 Set_Etype (Derived_Type, Save_Etype);
9915 Link_Entities (Derived_Type, Save_Next_Entity);
9917 if Has_Discriminants (Derived_Type) then
9918 Set_Discriminant_Constraint
9919 (Derived_Type, Save_Discr_Constr);
9920 Set_Stored_Constraint
9921 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9923 Replace_Discriminants (Derived_Type, New_Decl);
9924 end if;
9926 -- Insert the new derived type declaration
9928 Rewrite (N, New_Decl);
9930 -- STEP 5b: Complete the processing for record extensions in generics
9932 -- There is no completion for record extensions declared in the
9933 -- parameter part of a generic, so we need to complete processing for
9934 -- these generic record extensions here. The Record_Type_Definition call
9935 -- will change the Ekind of the components from E_Void to E_Component.
9937 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9938 Record_Type_Definition (Empty, Derived_Type);
9940 -- STEP 5c: Process the record extension for non private tagged types
9942 elsif not Private_Extension then
9943 Expand_Record_Extension (Derived_Type, Type_Def);
9945 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9946 -- implemented interfaces if we are in expansion mode
9948 if Expander_Active
9949 and then Has_Interfaces (Derived_Type)
9950 then
9951 Add_Interface_Tag_Components (N, Derived_Type);
9952 end if;
9954 -- Analyze the record extension
9956 Record_Type_Definition
9957 (Record_Extension_Part (Type_Def), Derived_Type);
9958 end if;
9960 End_Scope;
9962 -- Nothing else to do if there is an error in the derivation.
9963 -- An unusual case: the full view may be derived from a type in an
9964 -- instance, when the partial view was used illegally as an actual
9965 -- in that instance, leading to a circular definition.
9967 if Etype (Derived_Type) = Any_Type
9968 or else Etype (Parent_Type) = Derived_Type
9969 then
9970 return;
9971 end if;
9973 -- Set delayed freeze and then derive subprograms, we need to do
9974 -- this in this order so that derived subprograms inherit the
9975 -- derived freeze if necessary.
9977 Set_Has_Delayed_Freeze (Derived_Type);
9979 if Derive_Subps then
9980 Derive_Subprograms (Parent_Type, Derived_Type);
9981 end if;
9983 -- If we have a private extension which defines a constrained derived
9984 -- type mark as constrained here after we have derived subprograms. See
9985 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9987 if Private_Extension and then Inherit_Discrims then
9988 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9989 Set_Is_Constrained (Derived_Type, True);
9990 Set_Discriminant_Constraint (Derived_Type, Discs);
9992 elsif Is_Constrained (Parent_Type) then
9993 Set_Is_Constrained
9994 (Derived_Type, True);
9995 Set_Discriminant_Constraint
9996 (Derived_Type, Discriminant_Constraint (Parent_Type));
9997 end if;
9998 end if;
10000 -- Update the class-wide type, which shares the now-completed entity
10001 -- list with its specific type. In case of underlying record views,
10002 -- we do not generate the corresponding class wide entity.
10004 if Is_Tagged
10005 and then not Is_Underlying_Record_View (Derived_Type)
10006 then
10007 Set_First_Entity
10008 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
10009 Set_Last_Entity
10010 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
10011 end if;
10013 Check_Function_Writable_Actuals (N);
10014 end Build_Derived_Record_Type;
10016 ------------------------
10017 -- Build_Derived_Type --
10018 ------------------------
10020 procedure Build_Derived_Type
10021 (N : Node_Id;
10022 Parent_Type : Entity_Id;
10023 Derived_Type : Entity_Id;
10024 Is_Completion : Boolean;
10025 Derive_Subps : Boolean := True)
10027 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10029 begin
10030 -- Set common attributes
10032 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10033 and then Ekind (Parent_Base) in Modular_Integer_Kind | Array_Kind
10034 then
10035 Reinit_Field_To_Zero (Derived_Type, F_Stored_Constraint);
10036 end if;
10038 Set_Scope (Derived_Type, Current_Scope);
10039 Set_Etype (Derived_Type, Parent_Base);
10040 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10041 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10043 Set_Size_Info (Derived_Type, Parent_Type);
10044 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10046 Set_Is_Controlled_Active
10047 (Derived_Type, Is_Controlled_Active (Parent_Type));
10049 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
10050 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10051 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10053 if Is_Tagged_Type (Derived_Type) then
10054 Set_No_Tagged_Streams_Pragma
10055 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10056 end if;
10058 -- If the parent has primitive routines and may have not-seen-yet aspect
10059 -- specifications (e.g., a Pack pragma), then set the derived type link
10060 -- in order to later diagnose "early derivation" issues. If in different
10061 -- compilation units, then "early derivation" cannot be an issue (and we
10062 -- don't like interunit references that go in the opposite direction of
10063 -- semantic dependencies).
10065 if Has_Primitive_Operations (Parent_Type)
10066 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10067 Enclosing_Comp_Unit_Node (Derived_Type)
10068 then
10069 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10070 end if;
10072 -- If the parent type is a private subtype, the convention on the base
10073 -- type may be set in the private part, and not propagated to the
10074 -- subtype until later, so we obtain the convention from the base type.
10076 Set_Convention (Derived_Type, Convention (Parent_Base));
10078 if Is_Tagged_Type (Derived_Type)
10079 and then Present (Class_Wide_Type (Derived_Type))
10080 then
10081 Set_Convention (Class_Wide_Type (Derived_Type),
10082 Convention (Class_Wide_Type (Parent_Base)));
10083 end if;
10085 -- Set SSO default for record or array type
10087 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10088 and then Is_Base_Type (Derived_Type)
10089 then
10090 Set_Default_SSO (Derived_Type);
10091 end if;
10093 -- A derived type inherits the Default_Initial_Condition pragma coming
10094 -- from any parent type within the derivation chain.
10096 if Has_DIC (Parent_Type) then
10097 Set_Has_Inherited_DIC (Derived_Type);
10098 end if;
10100 -- A derived type inherits any class-wide invariants coming from a
10101 -- parent type or an interface. Note that the invariant procedure of
10102 -- the parent type should not be inherited because the derived type may
10103 -- define invariants of its own.
10105 if not Is_Interface (Derived_Type) then
10106 if Has_Inherited_Invariants (Parent_Type)
10107 or else Has_Inheritable_Invariants (Parent_Type)
10108 then
10109 Set_Has_Inherited_Invariants (Derived_Type);
10111 elsif Is_Concurrent_Type (Derived_Type)
10112 or else Is_Tagged_Type (Derived_Type)
10113 then
10114 declare
10115 Iface : Entity_Id;
10116 Ifaces : Elist_Id;
10117 Iface_Elmt : Elmt_Id;
10119 begin
10120 Collect_Interfaces
10121 (T => Derived_Type,
10122 Ifaces_List => Ifaces,
10123 Exclude_Parents => True);
10125 if Present (Ifaces) then
10126 Iface_Elmt := First_Elmt (Ifaces);
10127 while Present (Iface_Elmt) loop
10128 Iface := Node (Iface_Elmt);
10130 if Has_Inheritable_Invariants (Iface) then
10131 Set_Has_Inherited_Invariants (Derived_Type);
10132 exit;
10133 end if;
10135 Next_Elmt (Iface_Elmt);
10136 end loop;
10137 end if;
10138 end;
10139 end if;
10140 end if;
10142 -- We similarly inherit predicates. Note that for scalar derived types
10143 -- the predicate is inherited from the first subtype, and not from its
10144 -- (anonymous) base type.
10146 if Has_Predicates (Parent_Type)
10147 or else Has_Predicates (First_Subtype (Parent_Type))
10148 then
10149 Set_Has_Predicates (Derived_Type);
10150 end if;
10152 -- The derived type inherits representation clauses from the parent
10153 -- type, and from any interfaces.
10155 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10157 declare
10158 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10159 begin
10160 while Present (Iface) loop
10161 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10162 Next (Iface);
10163 end loop;
10164 end;
10166 -- If the parent type has delayed rep aspects, then mark the derived
10167 -- type as possibly inheriting a delayed rep aspect.
10169 if Has_Delayed_Rep_Aspects (Parent_Type) then
10170 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10171 end if;
10173 -- A derived type becomes Ghost when its parent type is also Ghost
10174 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10175 -- directly inherited because the Ghost policy in effect may differ.
10177 if Is_Ghost_Entity (Parent_Type) then
10178 Set_Is_Ghost_Entity (Derived_Type);
10179 end if;
10181 -- Type dependent processing
10183 case Ekind (Parent_Type) is
10184 when Numeric_Kind =>
10185 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10187 when Array_Kind =>
10188 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10190 when Class_Wide_Kind
10191 | E_Record_Subtype
10192 | E_Record_Type
10194 Build_Derived_Record_Type
10195 (N, Parent_Type, Derived_Type, Derive_Subps);
10196 return;
10198 when Enumeration_Kind =>
10199 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10201 when Access_Kind =>
10202 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10204 when Incomplete_Or_Private_Kind =>
10205 Build_Derived_Private_Type
10206 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10208 -- For discriminated types, the derivation includes deriving
10209 -- primitive operations. For others it is done below.
10211 if Is_Tagged_Type (Parent_Type)
10212 or else Has_Discriminants (Parent_Type)
10213 or else (Present (Full_View (Parent_Type))
10214 and then Has_Discriminants (Full_View (Parent_Type)))
10215 then
10216 return;
10217 end if;
10219 when Concurrent_Kind =>
10220 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10222 when others =>
10223 raise Program_Error;
10224 end case;
10226 -- Nothing more to do if some error occurred
10228 if Etype (Derived_Type) = Any_Type then
10229 return;
10230 end if;
10232 -- If not already set, initialize the derived type's list of primitive
10233 -- operations to an empty element list.
10235 if not Present (Direct_Primitive_Operations (Derived_Type)) then
10236 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10238 -- If Etype of the derived type is the base type (as opposed to
10239 -- a parent type) and doesn't have an associated list of primitive
10240 -- operations, then set the base type's primitive list to the
10241 -- derived type's list. The lists need to be shared in common
10242 -- between the two.
10244 if Etype (Derived_Type) = Base_Type (Derived_Type)
10245 and then
10246 not Present (Direct_Primitive_Operations (Etype (Derived_Type)))
10247 then
10248 Set_Direct_Primitive_Operations
10249 (Etype (Derived_Type),
10250 Direct_Primitive_Operations (Derived_Type));
10251 end if;
10252 end if;
10254 -- Set delayed freeze and then derive subprograms, we need to do this
10255 -- in this order so that derived subprograms inherit the derived freeze
10256 -- if necessary.
10258 Set_Has_Delayed_Freeze (Derived_Type);
10260 if Derive_Subps then
10261 Derive_Subprograms (Parent_Type, Derived_Type);
10262 end if;
10264 Set_Has_Primitive_Operations
10265 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10266 end Build_Derived_Type;
10268 -----------------------
10269 -- Build_Discriminal --
10270 -----------------------
10272 procedure Build_Discriminal (Discrim : Entity_Id) is
10273 D_Minal : Entity_Id;
10274 CR_Disc : Entity_Id;
10276 begin
10277 -- A discriminal has the same name as the discriminant
10279 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10281 Mutate_Ekind (D_Minal, E_In_Parameter);
10282 Set_Mechanism (D_Minal, Default_Mechanism);
10283 Set_Etype (D_Minal, Etype (Discrim));
10284 Set_Scope (D_Minal, Current_Scope);
10285 Set_Parent (D_Minal, Parent (Discrim));
10287 Set_Discriminal (Discrim, D_Minal);
10288 Set_Discriminal_Link (D_Minal, Discrim);
10290 -- For task types, build at once the discriminants of the corresponding
10291 -- record, which are needed if discriminants are used in entry defaults
10292 -- and in family bounds.
10294 if Is_Concurrent_Type (Current_Scope)
10295 or else
10296 Is_Limited_Type (Current_Scope)
10297 then
10298 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10300 Mutate_Ekind (CR_Disc, E_In_Parameter);
10301 Set_Mechanism (CR_Disc, Default_Mechanism);
10302 Set_Etype (CR_Disc, Etype (Discrim));
10303 Set_Scope (CR_Disc, Current_Scope);
10304 Set_Discriminal_Link (CR_Disc, Discrim);
10305 Set_CR_Discriminant (Discrim, CR_Disc);
10306 end if;
10307 end Build_Discriminal;
10309 ------------------------------------
10310 -- Build_Discriminant_Constraints --
10311 ------------------------------------
10313 function Build_Discriminant_Constraints
10314 (T : Entity_Id;
10315 Def : Node_Id;
10316 Derived_Def : Boolean := False) return Elist_Id
10318 C : constant Node_Id := Constraint (Def);
10319 Nb_Discr : constant Nat := Number_Discriminants (T);
10321 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10322 -- Saves the expression corresponding to a given discriminant in T
10324 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10325 -- Return the Position number within array Discr_Expr of a discriminant
10326 -- D within the discriminant list of the discriminated type T.
10328 procedure Process_Discriminant_Expression
10329 (Expr : Node_Id;
10330 D : Entity_Id);
10331 -- If this is a discriminant constraint on a partial view, do not
10332 -- generate an overflow check on the discriminant expression. The check
10333 -- will be generated when constraining the full view. Otherwise the
10334 -- backend creates duplicate symbols for the temporaries corresponding
10335 -- to the expressions to be checked, causing spurious assembler errors.
10337 ------------------
10338 -- Pos_Of_Discr --
10339 ------------------
10341 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10342 Disc : Entity_Id;
10344 begin
10345 Disc := First_Discriminant (T);
10346 for J in Discr_Expr'Range loop
10347 if Disc = D then
10348 return J;
10349 end if;
10351 Next_Discriminant (Disc);
10352 end loop;
10354 -- Note: Since this function is called on discriminants that are
10355 -- known to belong to the discriminated type, falling through the
10356 -- loop with no match signals an internal compiler error.
10358 raise Program_Error;
10359 end Pos_Of_Discr;
10361 -------------------------------------
10362 -- Process_Discriminant_Expression --
10363 -------------------------------------
10365 procedure Process_Discriminant_Expression
10366 (Expr : Node_Id;
10367 D : Entity_Id)
10369 BDT : constant Entity_Id := Base_Type (Etype (D));
10371 begin
10372 -- If this is a discriminant constraint on a partial view, do
10373 -- not generate an overflow on the discriminant expression. The
10374 -- check will be generated when constraining the full view.
10376 if Is_Private_Type (T)
10377 and then Present (Full_View (T))
10378 then
10379 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10380 else
10381 Analyze_And_Resolve (Expr, BDT);
10382 end if;
10383 end Process_Discriminant_Expression;
10385 -- Declarations local to Build_Discriminant_Constraints
10387 Discr : Entity_Id;
10388 E : Entity_Id;
10389 Elist : constant Elist_Id := New_Elmt_List;
10391 Constr : Node_Id;
10392 Expr : Node_Id;
10393 Id : Node_Id;
10394 Position : Nat;
10395 Found : Boolean;
10397 Discrim_Present : Boolean := False;
10399 -- Start of processing for Build_Discriminant_Constraints
10401 begin
10402 -- The following loop will process positional associations only.
10403 -- For a positional association, the (single) discriminant is
10404 -- implicitly specified by position, in textual order (RM 3.7.2).
10406 Discr := First_Discriminant (T);
10407 Constr := First (Constraints (C));
10408 for D in Discr_Expr'Range loop
10409 exit when Nkind (Constr) = N_Discriminant_Association;
10411 if No (Constr) then
10412 Error_Msg_N ("too few discriminants given in constraint", C);
10413 return New_Elmt_List;
10415 elsif Nkind (Constr) = N_Range
10416 or else (Nkind (Constr) = N_Attribute_Reference
10417 and then Attribute_Name (Constr) = Name_Range)
10418 then
10419 Error_Msg_N
10420 ("a range is not a valid discriminant constraint", Constr);
10421 Discr_Expr (D) := Error;
10423 elsif Nkind (Constr) = N_Subtype_Indication then
10424 Error_Msg_N
10425 ("a subtype indication is not a valid discriminant constraint",
10426 Constr);
10427 Discr_Expr (D) := Error;
10429 else
10430 Process_Discriminant_Expression (Constr, Discr);
10431 Discr_Expr (D) := Constr;
10432 end if;
10434 Next_Discriminant (Discr);
10435 Next (Constr);
10436 end loop;
10438 if No (Discr) and then Present (Constr) then
10439 Error_Msg_N ("too many discriminants given in constraint", Constr);
10440 return New_Elmt_List;
10441 end if;
10443 -- Named associations can be given in any order, but if both positional
10444 -- and named associations are used in the same discriminant constraint,
10445 -- then positional associations must occur first, at their normal
10446 -- position. Hence once a named association is used, the rest of the
10447 -- discriminant constraint must use only named associations.
10449 while Present (Constr) loop
10451 -- Positional association forbidden after a named association
10453 if Nkind (Constr) /= N_Discriminant_Association then
10454 Error_Msg_N ("positional association follows named one", Constr);
10455 return New_Elmt_List;
10457 -- Otherwise it is a named association
10459 else
10460 -- E records the type of the discriminants in the named
10461 -- association. All the discriminants specified in the same name
10462 -- association must have the same type.
10464 E := Empty;
10466 -- Search the list of discriminants in T to see if the simple name
10467 -- given in the constraint matches any of them.
10469 Id := First (Selector_Names (Constr));
10470 while Present (Id) loop
10471 Found := False;
10473 -- If Original_Discriminant is present, we are processing a
10474 -- generic instantiation and this is an instance node. We need
10475 -- to find the name of the corresponding discriminant in the
10476 -- actual record type T and not the name of the discriminant in
10477 -- the generic formal. Example:
10479 -- generic
10480 -- type G (D : int) is private;
10481 -- package P is
10482 -- subtype W is G (D => 1);
10483 -- end package;
10484 -- type Rec (X : int) is record ... end record;
10485 -- package Q is new P (G => Rec);
10487 -- At the point of the instantiation, formal type G is Rec
10488 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10489 -- which really looks like "subtype W is Rec (D => 1);" at
10490 -- the point of instantiation, we want to find the discriminant
10491 -- that corresponds to D in Rec, i.e. X.
10493 if Present (Original_Discriminant (Id))
10494 and then In_Instance
10495 then
10496 Discr := Find_Corresponding_Discriminant (Id, T);
10497 Found := True;
10499 else
10500 Discr := First_Discriminant (T);
10501 while Present (Discr) loop
10502 if Chars (Discr) = Chars (Id) then
10503 Found := True;
10504 exit;
10505 end if;
10507 Next_Discriminant (Discr);
10508 end loop;
10510 if not Found then
10511 Error_Msg_N ("& does not match any discriminant", Id);
10512 return New_Elmt_List;
10514 -- If the parent type is a generic formal, preserve the
10515 -- name of the discriminant for subsequent instances.
10516 -- see comment at the beginning of this if statement.
10518 elsif Is_Generic_Type (Root_Type (T)) then
10519 Set_Original_Discriminant (Id, Discr);
10520 end if;
10521 end if;
10523 Position := Pos_Of_Discr (T, Discr);
10525 if Present (Discr_Expr (Position)) then
10526 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10528 else
10529 -- Each discriminant specified in the same named association
10530 -- must be associated with a separate copy of the
10531 -- corresponding expression.
10533 if Present (Next (Id)) then
10534 Expr := New_Copy_Tree (Expression (Constr));
10535 Set_Parent (Expr, Parent (Expression (Constr)));
10536 else
10537 Expr := Expression (Constr);
10538 end if;
10540 Discr_Expr (Position) := Expr;
10541 Process_Discriminant_Expression (Expr, Discr);
10542 end if;
10544 -- A discriminant association with more than one discriminant
10545 -- name is only allowed if the named discriminants are all of
10546 -- the same type (RM 3.7.1(8)).
10548 if E = Empty then
10549 E := Base_Type (Etype (Discr));
10551 elsif Base_Type (Etype (Discr)) /= E then
10552 Error_Msg_N
10553 ("all discriminants in an association " &
10554 "must have the same type", Id);
10555 end if;
10557 Next (Id);
10558 end loop;
10559 end if;
10561 Next (Constr);
10562 end loop;
10564 -- A discriminant constraint must provide exactly one value for each
10565 -- discriminant of the type (RM 3.7.1(8)).
10567 for J in Discr_Expr'Range loop
10568 if No (Discr_Expr (J)) then
10569 Error_Msg_N ("too few discriminants given in constraint", C);
10570 return New_Elmt_List;
10571 end if;
10572 end loop;
10574 -- Determine if there are discriminant expressions in the constraint
10576 for J in Discr_Expr'Range loop
10577 if Denotes_Discriminant
10578 (Discr_Expr (J), Check_Concurrent => True)
10579 then
10580 Discrim_Present := True;
10581 exit;
10582 end if;
10583 end loop;
10585 -- Build an element list consisting of the expressions given in the
10586 -- discriminant constraint and apply the appropriate checks. The list
10587 -- is constructed after resolving any named discriminant associations
10588 -- and therefore the expressions appear in the textual order of the
10589 -- discriminants.
10591 Discr := First_Discriminant (T);
10592 for J in Discr_Expr'Range loop
10593 if Discr_Expr (J) /= Error then
10594 Append_Elmt (Discr_Expr (J), Elist);
10596 -- If any of the discriminant constraints is given by a
10597 -- discriminant and we are in a derived type declaration we
10598 -- have a discriminant renaming. Establish link between new
10599 -- and old discriminant. The new discriminant has an implicit
10600 -- dereference if the old one does.
10602 if Denotes_Discriminant (Discr_Expr (J)) then
10603 if Derived_Def then
10604 declare
10605 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10607 begin
10608 Set_Corresponding_Discriminant (New_Discr, Discr);
10609 Set_Has_Implicit_Dereference (New_Discr,
10610 Has_Implicit_Dereference (Discr));
10611 end;
10612 end if;
10614 -- Force the evaluation of non-discriminant expressions.
10615 -- If we have found a discriminant in the constraint 3.4(26)
10616 -- and 3.8(18) demand that no range checks are performed are
10617 -- after evaluation. If the constraint is for a component
10618 -- definition that has a per-object constraint, expressions are
10619 -- evaluated but not checked either. In all other cases perform
10620 -- a range check.
10622 else
10623 if Discrim_Present then
10624 null;
10626 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10627 and then Has_Per_Object_Constraint
10628 (Defining_Identifier (Parent (Parent (Def))))
10629 then
10630 null;
10632 elsif Is_Access_Type (Etype (Discr)) then
10633 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10635 else
10636 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10637 end if;
10639 -- If the value of the discriminant may be visible in
10640 -- another unit or child unit, create an external name
10641 -- for it. We use the name of the object or component
10642 -- that carries the discriminated subtype. The code
10643 -- below may generate external symbols for the discriminant
10644 -- expression when not strictly needed, which is harmless.
10646 if Expander_Active
10647 and then Comes_From_Source (Def)
10648 and then not Is_Subprogram (Current_Scope)
10649 then
10650 declare
10651 Id : Entity_Id := Empty;
10652 begin
10653 if Nkind (Parent (Def)) = N_Object_Declaration then
10654 Id := Defining_Identifier (Parent (Def));
10656 elsif Nkind (Parent (Def)) = N_Component_Definition
10657 and then
10658 Nkind (Parent (Parent (Def)))
10659 = N_Component_Declaration
10660 then
10661 Id := Defining_Identifier (Parent (Parent (Def)));
10662 end if;
10664 if Present (Id) then
10665 Force_Evaluation (
10666 Discr_Expr (J),
10667 Related_Id => Id,
10668 Discr_Number => J);
10669 else
10670 Force_Evaluation (Discr_Expr (J));
10671 end if;
10672 end;
10673 else
10674 Force_Evaluation (Discr_Expr (J));
10675 end if;
10676 end if;
10678 -- Check that the designated type of an access discriminant's
10679 -- expression is not a class-wide type unless the discriminant's
10680 -- designated type is also class-wide.
10682 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10683 and then not Is_Class_Wide_Type
10684 (Designated_Type (Etype (Discr)))
10685 and then Etype (Discr_Expr (J)) /= Any_Type
10686 and then Is_Class_Wide_Type
10687 (Designated_Type (Etype (Discr_Expr (J))))
10688 then
10689 Wrong_Type (Discr_Expr (J), Etype (Discr));
10691 elsif Is_Access_Type (Etype (Discr))
10692 and then not Is_Access_Constant (Etype (Discr))
10693 and then Is_Access_Type (Etype (Discr_Expr (J)))
10694 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10695 then
10696 Error_Msg_NE
10697 ("constraint for discriminant& must be access to variable",
10698 Def, Discr);
10699 end if;
10700 end if;
10702 Next_Discriminant (Discr);
10703 end loop;
10705 return Elist;
10706 end Build_Discriminant_Constraints;
10708 ---------------------------------
10709 -- Build_Discriminated_Subtype --
10710 ---------------------------------
10712 procedure Build_Discriminated_Subtype
10713 (T : Entity_Id;
10714 Def_Id : Entity_Id;
10715 Elist : Elist_Id;
10716 Related_Nod : Node_Id;
10717 For_Access : Boolean := False)
10719 Has_Discrs : constant Boolean := Has_Discriminants (T);
10720 Constrained : constant Boolean :=
10721 (Has_Discrs
10722 and then not Is_Empty_Elmt_List (Elist)
10723 and then not Is_Class_Wide_Type (T))
10724 or else Is_Constrained (T);
10726 begin
10727 if Ekind (T) = E_Record_Type then
10728 Mutate_Ekind (Def_Id, E_Record_Subtype);
10730 -- Inherit preelaboration flag from base, for types for which it
10731 -- may have been set: records, private types, protected types.
10733 Set_Known_To_Have_Preelab_Init
10734 (Def_Id, Known_To_Have_Preelab_Init (T));
10736 elsif Ekind (T) = E_Task_Type then
10737 Mutate_Ekind (Def_Id, E_Task_Subtype);
10739 elsif Ekind (T) = E_Protected_Type then
10740 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10741 Set_Known_To_Have_Preelab_Init
10742 (Def_Id, Known_To_Have_Preelab_Init (T));
10744 elsif Is_Private_Type (T) then
10745 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10746 Set_Known_To_Have_Preelab_Init
10747 (Def_Id, Known_To_Have_Preelab_Init (T));
10749 -- Private subtypes may have private dependents
10751 Set_Private_Dependents (Def_Id, New_Elmt_List);
10753 elsif Is_Class_Wide_Type (T) then
10754 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10756 else
10757 -- Incomplete type. Attach subtype to list of dependents, to be
10758 -- completed with full view of parent type, unless is it the
10759 -- designated subtype of a record component within an init_proc.
10760 -- This last case arises for a component of an access type whose
10761 -- designated type is incomplete (e.g. a Taft Amendment type).
10762 -- The designated subtype is within an inner scope, and needs no
10763 -- elaboration, because only the access type is needed in the
10764 -- initialization procedure.
10766 if Ekind (T) = E_Incomplete_Type then
10767 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10768 else
10769 Mutate_Ekind (Def_Id, Ekind (T));
10770 end if;
10772 if For_Access and then Within_Init_Proc then
10773 null;
10774 else
10775 Append_Elmt (Def_Id, Private_Dependents (T));
10776 end if;
10777 end if;
10779 Set_Etype (Def_Id, T);
10780 Reinit_Size_Align (Def_Id);
10781 Set_Has_Discriminants (Def_Id, Has_Discrs);
10782 Set_Is_Constrained (Def_Id, Constrained);
10784 Set_First_Entity (Def_Id, First_Entity (T));
10785 Set_Last_Entity (Def_Id, Last_Entity (T));
10786 Set_Has_Implicit_Dereference
10787 (Def_Id, Has_Implicit_Dereference (T));
10788 Set_Has_Pragma_Unreferenced_Objects
10789 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10791 -- If the subtype is the completion of a private declaration, there may
10792 -- have been representation clauses for the partial view, and they must
10793 -- be preserved. Build_Derived_Type chains the inherited clauses with
10794 -- the ones appearing on the extension. If this comes from a subtype
10795 -- declaration, all clauses are inherited.
10797 if No (First_Rep_Item (Def_Id)) then
10798 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10799 end if;
10801 if Is_Tagged_Type (T) then
10802 Set_Is_Tagged_Type (Def_Id);
10803 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10804 Make_Class_Wide_Type (Def_Id);
10805 end if;
10807 Set_Stored_Constraint (Def_Id, No_Elist);
10809 if Has_Discrs then
10810 Set_Discriminant_Constraint (Def_Id, Elist);
10811 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10812 end if;
10814 if Is_Tagged_Type (T) then
10816 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10817 -- concurrent record type (which has the list of primitive
10818 -- operations).
10820 if Ada_Version >= Ada_2005
10821 and then Is_Concurrent_Type (T)
10822 then
10823 Set_Corresponding_Record_Type (Def_Id,
10824 Corresponding_Record_Type (T));
10825 else
10826 Set_Direct_Primitive_Operations (Def_Id,
10827 Direct_Primitive_Operations (T));
10828 end if;
10830 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10831 end if;
10833 -- Subtypes introduced by component declarations do not need to be
10834 -- marked as delayed, and do not get freeze nodes, because the semantics
10835 -- verifies that the parents of the subtypes are frozen before the
10836 -- enclosing record is frozen.
10838 if not Is_Type (Scope (Def_Id)) then
10839 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10841 if Is_Private_Type (T)
10842 and then Present (Full_View (T))
10843 then
10844 Conditional_Delay (Def_Id, Full_View (T));
10845 else
10846 Conditional_Delay (Def_Id, T);
10847 end if;
10848 end if;
10850 if Is_Record_Type (T) then
10851 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10853 if Has_Discrs
10854 and then not Is_Empty_Elmt_List (Elist)
10855 and then not For_Access
10856 then
10857 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10859 elsif not Is_Private_Type (T) then
10860 Set_Cloned_Subtype (Def_Id, T);
10861 end if;
10862 end if;
10863 end Build_Discriminated_Subtype;
10865 ---------------------------
10866 -- Build_Itype_Reference --
10867 ---------------------------
10869 procedure Build_Itype_Reference
10870 (Ityp : Entity_Id;
10871 Nod : Node_Id)
10873 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10874 begin
10876 -- Itype references are only created for use by the back-end
10878 if Inside_A_Generic then
10879 return;
10880 else
10881 Set_Itype (IR, Ityp);
10883 -- If Nod is a library unit entity, then Insert_After won't work,
10884 -- because Nod is not a member of any list. Therefore, we use
10885 -- Add_Global_Declaration in this case. This can happen if we have a
10886 -- build-in-place library function, child unit or not.
10888 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10889 or else (Nkind (Nod) in
10890 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
10891 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10892 then
10893 Add_Global_Declaration (IR);
10894 else
10895 Insert_After (Nod, IR);
10896 end if;
10897 end if;
10898 end Build_Itype_Reference;
10900 ------------------------
10901 -- Build_Scalar_Bound --
10902 ------------------------
10904 function Build_Scalar_Bound
10905 (Bound : Node_Id;
10906 Par_T : Entity_Id;
10907 Der_T : Entity_Id) return Node_Id
10909 New_Bound : Entity_Id;
10911 begin
10912 -- Note: not clear why this is needed, how can the original bound
10913 -- be unanalyzed at this point? and if it is, what business do we
10914 -- have messing around with it? and why is the base type of the
10915 -- parent type the right type for the resolution. It probably is
10916 -- not. It is OK for the new bound we are creating, but not for
10917 -- the old one??? Still if it never happens, no problem.
10919 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10921 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
10922 New_Bound := New_Copy (Bound);
10923 Set_Etype (New_Bound, Der_T);
10924 Set_Analyzed (New_Bound);
10926 elsif Is_Entity_Name (Bound) then
10927 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10929 -- The following is almost certainly wrong. What business do we have
10930 -- relocating a node (Bound) that is presumably still attached to
10931 -- the tree elsewhere???
10933 else
10934 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10935 end if;
10937 Set_Etype (New_Bound, Der_T);
10938 return New_Bound;
10939 end Build_Scalar_Bound;
10941 -------------------------------
10942 -- Check_Abstract_Overriding --
10943 -------------------------------
10945 procedure Check_Abstract_Overriding (T : Entity_Id) is
10946 Alias_Subp : Entity_Id;
10947 Elmt : Elmt_Id;
10948 Op_List : Elist_Id;
10949 Subp : Entity_Id;
10950 Type_Def : Node_Id;
10952 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10953 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10954 -- which has pragma Implemented already set. Check whether Subp's entity
10955 -- kind conforms to the implementation kind of the overridden routine.
10957 procedure Check_Pragma_Implemented
10958 (Subp : Entity_Id;
10959 Iface_Subp : Entity_Id);
10960 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10961 -- Iface_Subp and both entities have pragma Implemented already set on
10962 -- them. Check whether the two implementation kinds are conforming.
10964 procedure Inherit_Pragma_Implemented
10965 (Subp : Entity_Id;
10966 Iface_Subp : Entity_Id);
10967 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10968 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10969 -- Propagate the implementation kind of Iface_Subp to Subp.
10971 ------------------------------
10972 -- Check_Pragma_Implemented --
10973 ------------------------------
10975 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10976 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10977 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10978 Subp_Alias : constant Entity_Id := Alias (Subp);
10979 Contr_Typ : Entity_Id;
10980 Impl_Subp : Entity_Id;
10982 begin
10983 -- Subp must have an alias since it is a hidden entity used to link
10984 -- an interface subprogram to its overriding counterpart.
10986 pragma Assert (Present (Subp_Alias));
10988 -- Handle aliases to synchronized wrappers
10990 Impl_Subp := Subp_Alias;
10992 if Is_Primitive_Wrapper (Impl_Subp) then
10993 Impl_Subp := Wrapped_Entity (Impl_Subp);
10994 end if;
10996 -- Extract the type of the controlling formal
10998 Contr_Typ := Etype (First_Formal (Subp_Alias));
11000 if Is_Concurrent_Record_Type (Contr_Typ) then
11001 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
11002 end if;
11004 -- An interface subprogram whose implementation kind is By_Entry must
11005 -- be implemented by an entry.
11007 if Impl_Kind = Name_By_Entry
11008 and then Ekind (Impl_Subp) /= E_Entry
11009 then
11010 Error_Msg_Node_2 := Iface_Alias;
11011 Error_Msg_NE
11012 ("type & must implement abstract subprogram & with an entry",
11013 Subp_Alias, Contr_Typ);
11015 elsif Impl_Kind = Name_By_Protected_Procedure then
11017 -- An interface subprogram whose implementation kind is By_
11018 -- Protected_Procedure cannot be implemented by a primitive
11019 -- procedure of a task type.
11021 if Ekind (Contr_Typ) /= E_Protected_Type then
11022 Error_Msg_Node_2 := Contr_Typ;
11023 Error_Msg_NE
11024 ("interface subprogram & cannot be implemented by a "
11025 & "primitive procedure of task type &",
11026 Subp_Alias, Iface_Alias);
11028 -- An interface subprogram whose implementation kind is By_
11029 -- Protected_Procedure must be implemented by a procedure.
11031 elsif Ekind (Impl_Subp) /= E_Procedure then
11032 Error_Msg_Node_2 := Iface_Alias;
11033 Error_Msg_NE
11034 ("type & must implement abstract subprogram & with a "
11035 & "procedure", Subp_Alias, Contr_Typ);
11037 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11038 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11039 then
11040 Error_Msg_Name_1 := Impl_Kind;
11041 Error_Msg_N
11042 ("overriding operation& must have synchronization%",
11043 Subp_Alias);
11044 end if;
11046 -- If primitive has Optional synchronization, overriding operation
11047 -- must match if it has an explicit synchronization.
11049 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11050 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11051 then
11052 Error_Msg_Name_1 := Impl_Kind;
11053 Error_Msg_N
11054 ("overriding operation& must have synchronization%", Subp_Alias);
11055 end if;
11056 end Check_Pragma_Implemented;
11058 ------------------------------
11059 -- Check_Pragma_Implemented --
11060 ------------------------------
11062 procedure Check_Pragma_Implemented
11063 (Subp : Entity_Id;
11064 Iface_Subp : Entity_Id)
11066 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11067 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11069 begin
11070 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11071 -- and overriding subprogram are different. In general this is an
11072 -- error except when the implementation kind of the overridden
11073 -- subprograms is By_Any or Optional.
11075 if Iface_Kind /= Subp_Kind
11076 and then Iface_Kind /= Name_By_Any
11077 and then Iface_Kind /= Name_Optional
11078 then
11079 if Iface_Kind = Name_By_Entry then
11080 Error_Msg_N
11081 ("incompatible implementation kind, overridden subprogram " &
11082 "is marked By_Entry", Subp);
11083 else
11084 Error_Msg_N
11085 ("incompatible implementation kind, overridden subprogram " &
11086 "is marked By_Protected_Procedure", Subp);
11087 end if;
11088 end if;
11089 end Check_Pragma_Implemented;
11091 --------------------------------
11092 -- Inherit_Pragma_Implemented --
11093 --------------------------------
11095 procedure Inherit_Pragma_Implemented
11096 (Subp : Entity_Id;
11097 Iface_Subp : Entity_Id)
11099 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11100 Loc : constant Source_Ptr := Sloc (Subp);
11101 Impl_Prag : Node_Id;
11103 begin
11104 -- Since the implementation kind is stored as a representation item
11105 -- rather than a flag, create a pragma node.
11107 Impl_Prag :=
11108 Make_Pragma (Loc,
11109 Chars => Name_Implemented,
11110 Pragma_Argument_Associations => New_List (
11111 Make_Pragma_Argument_Association (Loc,
11112 Expression => New_Occurrence_Of (Subp, Loc)),
11114 Make_Pragma_Argument_Association (Loc,
11115 Expression => Make_Identifier (Loc, Iface_Kind))));
11117 -- The pragma doesn't need to be analyzed because it is internally
11118 -- built. It is safe to directly register it as a rep item since we
11119 -- are only interested in the characters of the implementation kind.
11121 Record_Rep_Item (Subp, Impl_Prag);
11122 end Inherit_Pragma_Implemented;
11124 -- Start of processing for Check_Abstract_Overriding
11126 begin
11127 Op_List := Primitive_Operations (T);
11129 -- Loop to check primitive operations
11131 Elmt := First_Elmt (Op_List);
11132 while Present (Elmt) loop
11133 Subp := Node (Elmt);
11134 Alias_Subp := Alias (Subp);
11136 -- If the parent type is untagged, then no overriding error checks
11137 -- are needed (such as in the case of an implicit full type for
11138 -- a derived type whose parent is an untagged private type with
11139 -- a tagged full type).
11141 if not Is_Tagged_Type (Etype (T)) then
11142 null;
11144 -- Inherited subprograms are identified by the fact that they do not
11145 -- come from source, and the associated source location is the
11146 -- location of the first subtype of the derived type.
11148 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11149 -- subprograms that "require overriding".
11151 -- Special exception, do not complain about failure to override the
11152 -- stream routines _Input and _Output, as well as the primitive
11153 -- operations used in dispatching selects since we always provide
11154 -- automatic overridings for these subprograms.
11156 -- The partial view of T may have been a private extension, for
11157 -- which inherited functions dispatching on result are abstract.
11158 -- If the full view is a null extension, there is no need for
11159 -- overriding in Ada 2005, but wrappers need to be built for them
11160 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11162 elsif Is_Null_Extension (T)
11163 and then Has_Controlling_Result (Subp)
11164 and then Ada_Version >= Ada_2005
11165 and then Present (Alias_Subp)
11166 and then not Comes_From_Source (Subp)
11167 and then not Is_Abstract_Subprogram (Alias_Subp)
11168 and then not Is_Access_Type (Etype (Subp))
11169 then
11170 null;
11172 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11173 -- processing because this check is done with the aliased
11174 -- entity
11176 elsif Present (Interface_Alias (Subp)) then
11177 null;
11179 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11180 -- of a visible private primitive inherited from an ancestor with
11181 -- the aspect Type_Invariant'Class, unless the inherited primitive
11182 -- is abstract.
11184 elsif not Is_Abstract_Subprogram (Subp)
11185 and then not Comes_From_Source (Subp) -- An inherited subprogram
11186 and then Requires_Overriding (Subp)
11187 and then Present (Alias_Subp)
11188 and then Has_Invariants (Etype (T))
11189 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11190 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11191 and then Is_Private_Primitive (Alias_Subp)
11192 then
11193 Error_Msg_NE
11194 ("inherited private primitive & must be overridden", T, Subp);
11195 Error_Msg_N
11196 ("\because ancestor type has 'Type_'Invariant''Class " &
11197 "(RM 7.3.2(6.1))", T);
11199 elsif (Is_Abstract_Subprogram (Subp)
11200 or else Requires_Overriding (Subp)
11201 or else
11202 (Has_Controlling_Result (Subp)
11203 and then Present (Alias_Subp)
11204 and then not Comes_From_Source (Subp)
11205 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11206 and then not Is_TSS (Subp, TSS_Stream_Input)
11207 and then not Is_TSS (Subp, TSS_Stream_Output)
11208 and then not Is_Abstract_Type (T)
11209 and then not Is_Predefined_Interface_Primitive (Subp)
11211 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11212 -- with abstract interface types because the check will be done
11213 -- with the aliased entity (otherwise we generate a duplicated
11214 -- error message).
11216 and then No (Interface_Alias (Subp))
11217 then
11218 if Present (Alias_Subp) then
11220 -- Only perform the check for a derived subprogram when the
11221 -- type has an explicit record extension. This avoids incorrect
11222 -- flagging of abstract subprograms for the case of a type
11223 -- without an extension that is derived from a formal type
11224 -- with a tagged actual (can occur within a private part).
11226 -- Ada 2005 (AI-391): In the case of an inherited function with
11227 -- a controlling result of the type, the rule does not apply if
11228 -- the type is a null extension (unless the parent function
11229 -- itself is abstract, in which case the function must still be
11230 -- be overridden). The expander will generate an overriding
11231 -- wrapper function calling the parent subprogram (see
11232 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11234 Type_Def := Type_Definition (Parent (T));
11236 if Nkind (Type_Def) = N_Derived_Type_Definition
11237 and then Present (Record_Extension_Part (Type_Def))
11238 and then
11239 (Ada_Version < Ada_2005
11240 or else not Is_Null_Extension (T)
11241 or else Ekind (Subp) = E_Procedure
11242 or else not Has_Controlling_Result (Subp)
11243 or else Is_Abstract_Subprogram (Alias_Subp)
11244 or else Requires_Overriding (Subp)
11245 or else Is_Access_Type (Etype (Subp)))
11246 then
11247 -- Avoid reporting error in case of abstract predefined
11248 -- primitive inherited from interface type because the
11249 -- body of internally generated predefined primitives
11250 -- of tagged types are generated later by Freeze_Type
11252 if Is_Interface (Root_Type (T))
11253 and then Is_Abstract_Subprogram (Subp)
11254 and then Is_Predefined_Dispatching_Operation (Subp)
11255 and then not Comes_From_Source (Ultimate_Alias (Subp))
11256 then
11257 null;
11259 -- A null extension is not obliged to override an inherited
11260 -- procedure subject to pragma Extensions_Visible with value
11261 -- False and at least one controlling OUT parameter
11262 -- (SPARK RM 6.1.7(6)).
11264 elsif Is_Null_Extension (T)
11265 and then Is_EVF_Procedure (Subp)
11266 then
11267 null;
11269 -- Subprogram renamings cannot be overridden
11271 elsif Comes_From_Source (Subp)
11272 and then Present (Alias (Subp))
11273 then
11274 null;
11276 -- Skip reporting the error on Ada 2022 only subprograms
11277 -- that require overriding if we are not in Ada 2022 mode.
11279 elsif Ada_Version < Ada_2022
11280 and then Requires_Overriding (Subp)
11281 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11282 then
11283 null;
11285 else
11286 Error_Msg_NE
11287 ("type must be declared abstract or & overridden",
11288 T, Subp);
11290 -- Traverse the whole chain of aliased subprograms to
11291 -- complete the error notification. This is especially
11292 -- useful for traceability of the chain of entities when
11293 -- the subprogram corresponds with an interface
11294 -- subprogram (which may be defined in another package).
11296 if Present (Alias_Subp) then
11297 declare
11298 E : Entity_Id;
11300 begin
11301 E := Subp;
11302 while Present (Alias (E)) loop
11304 -- Avoid reporting redundant errors on entities
11305 -- inherited from interfaces
11307 if Sloc (E) /= Sloc (T) then
11308 Error_Msg_Sloc := Sloc (E);
11309 Error_Msg_NE
11310 ("\& has been inherited #", T, Subp);
11311 end if;
11313 E := Alias (E);
11314 end loop;
11316 Error_Msg_Sloc := Sloc (E);
11318 -- AI05-0068: report if there is an overriding
11319 -- non-abstract subprogram that is invisible.
11321 if Is_Hidden (E)
11322 and then not Is_Abstract_Subprogram (E)
11323 then
11324 Error_Msg_NE
11325 ("\& subprogram# is not visible",
11326 T, Subp);
11328 -- Clarify the case where a non-null extension must
11329 -- override inherited procedure subject to pragma
11330 -- Extensions_Visible with value False and at least
11331 -- one controlling OUT param.
11333 elsif Is_EVF_Procedure (E) then
11334 Error_Msg_NE
11335 ("\& # is subject to Extensions_Visible False",
11336 T, Subp);
11338 else
11339 Error_Msg_NE
11340 ("\& has been inherited from subprogram #",
11341 T, Subp);
11342 end if;
11343 end;
11344 end if;
11345 end if;
11347 -- Ada 2005 (AI-345): Protected or task type implementing
11348 -- abstract interfaces.
11350 elsif Is_Concurrent_Record_Type (T)
11351 and then Present (Interfaces (T))
11352 then
11353 -- There is no need to check here RM 9.4(11.9/3) since we
11354 -- are processing the corresponding record type and the
11355 -- mode of the overriding subprograms was verified by
11356 -- Check_Conformance when the corresponding concurrent
11357 -- type declaration was analyzed.
11359 Error_Msg_NE
11360 ("interface subprogram & must be overridden", T, Subp);
11362 -- Examine primitive operations of synchronized type to find
11363 -- homonyms that have the wrong profile.
11365 declare
11366 Prim : Entity_Id;
11368 begin
11369 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11370 while Present (Prim) loop
11371 if Chars (Prim) = Chars (Subp) then
11372 Error_Msg_NE
11373 ("profile is not type conformant with prefixed "
11374 & "view profile of inherited operation&",
11375 Prim, Subp);
11376 end if;
11378 Next_Entity (Prim);
11379 end loop;
11380 end;
11381 end if;
11383 else
11384 Error_Msg_Node_2 := T;
11385 Error_Msg_N
11386 ("abstract subprogram& not allowed for type&", Subp);
11388 -- Also post unconditional warning on the type (unconditional
11389 -- so that if there are more than one of these cases, we get
11390 -- them all, and not just the first one).
11392 Error_Msg_Node_2 := Subp;
11393 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11394 end if;
11396 -- A subprogram subject to pragma Extensions_Visible with value
11397 -- "True" cannot override a subprogram subject to the same pragma
11398 -- with value "False" (SPARK RM 6.1.7(5)).
11400 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11401 and then Present (Overridden_Operation (Subp))
11402 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11403 Extensions_Visible_False
11404 then
11405 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11406 Error_Msg_N
11407 ("subprogram & with Extensions_Visible True cannot override "
11408 & "subprogram # with Extensions_Visible False", Subp);
11409 end if;
11411 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11413 -- Subp is an expander-generated procedure which maps an interface
11414 -- alias to a protected wrapper. The interface alias is flagged by
11415 -- pragma Implemented. Ensure that Subp is a procedure when the
11416 -- implementation kind is By_Protected_Procedure or an entry when
11417 -- By_Entry.
11419 if Ada_Version >= Ada_2012
11420 and then Is_Hidden (Subp)
11421 and then Present (Interface_Alias (Subp))
11422 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11423 then
11424 Check_Pragma_Implemented (Subp);
11425 end if;
11427 -- Subp is an interface primitive which overrides another interface
11428 -- primitive marked with pragma Implemented.
11430 if Ada_Version >= Ada_2012
11431 and then Present (Overridden_Operation (Subp))
11432 and then Has_Rep_Pragma
11433 (Overridden_Operation (Subp), Name_Implemented)
11434 then
11435 -- If the overriding routine is also marked by Implemented, check
11436 -- that the two implementation kinds are conforming.
11438 if Has_Rep_Pragma (Subp, Name_Implemented) then
11439 Check_Pragma_Implemented
11440 (Subp => Subp,
11441 Iface_Subp => Overridden_Operation (Subp));
11443 -- Otherwise the overriding routine inherits the implementation
11444 -- kind from the overridden subprogram.
11446 else
11447 Inherit_Pragma_Implemented
11448 (Subp => Subp,
11449 Iface_Subp => Overridden_Operation (Subp));
11450 end if;
11451 end if;
11453 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11454 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11455 -- for procedures, since this is our pragma.
11457 if Present (Overridden_Operation (Subp))
11458 and then No_Return (Overridden_Operation (Subp))
11459 then
11461 -- If the subprogram is a renaming, check that the renamed
11462 -- subprogram is No_Return.
11464 if Present (Renamed_Or_Alias (Subp)) then
11465 if not No_Return (Renamed_Or_Alias (Subp)) then
11466 Error_Msg_NE ("subprogram & must be No_Return",
11467 Subp,
11468 Renamed_Or_Alias (Subp));
11469 Error_Msg_N ("\since renaming & overrides No_Return "
11470 & "subprogram (RM 6.5.1(6/2))",
11471 Subp);
11472 end if;
11474 -- Make sure that the subprogram itself is No_Return.
11476 elsif not No_Return (Subp) then
11477 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11478 Error_Msg_N
11479 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11480 Subp);
11481 end if;
11482 end if;
11484 -- If the operation is a wrapper for a synchronized primitive, it
11485 -- may be called indirectly through a dispatching select. We assume
11486 -- that it will be referenced elsewhere indirectly, and suppress
11487 -- warnings about an unused entity.
11489 if Is_Primitive_Wrapper (Subp)
11490 and then Present (Wrapped_Entity (Subp))
11491 then
11492 Set_Referenced (Wrapped_Entity (Subp));
11493 end if;
11495 Next_Elmt (Elmt);
11496 end loop;
11497 end Check_Abstract_Overriding;
11499 ------------------------------------------------
11500 -- Check_Access_Discriminant_Requires_Limited --
11501 ------------------------------------------------
11503 procedure Check_Access_Discriminant_Requires_Limited
11504 (D : Node_Id;
11505 Loc : Node_Id)
11507 begin
11508 -- A discriminant_specification for an access discriminant shall appear
11509 -- only in the declaration for a task or protected type, or for a type
11510 -- with the reserved word 'limited' in its definition or in one of its
11511 -- ancestors (RM 3.7(10)).
11513 -- AI-0063: The proper condition is that type must be immutably limited,
11514 -- or else be a partial view.
11516 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11517 if Is_Limited_View (Current_Scope)
11518 or else
11519 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11520 and then Limited_Present (Parent (Current_Scope)))
11521 then
11522 null;
11524 else
11525 Error_Msg_N
11526 ("access discriminants allowed only for limited types", Loc);
11527 end if;
11528 end if;
11529 end Check_Access_Discriminant_Requires_Limited;
11531 -----------------------------------
11532 -- Check_Aliased_Component_Types --
11533 -----------------------------------
11535 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11536 C : Entity_Id;
11538 begin
11539 -- ??? Also need to check components of record extensions, but not
11540 -- components of protected types (which are always limited).
11542 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11543 -- types to be unconstrained. This is safe because it is illegal to
11544 -- create access subtypes to such types with explicit discriminant
11545 -- constraints.
11547 if not Is_Limited_Type (T) then
11548 if Ekind (T) = E_Record_Type then
11549 C := First_Component (T);
11550 while Present (C) loop
11551 if Is_Aliased (C)
11552 and then Has_Discriminants (Etype (C))
11553 and then not Is_Constrained (Etype (C))
11554 and then not In_Instance_Body
11555 and then Ada_Version < Ada_2005
11556 then
11557 Error_Msg_N
11558 ("aliased component must be constrained (RM 3.6(11))",
11560 end if;
11562 Next_Component (C);
11563 end loop;
11565 elsif Ekind (T) = E_Array_Type then
11566 if Has_Aliased_Components (T)
11567 and then Has_Discriminants (Component_Type (T))
11568 and then not Is_Constrained (Component_Type (T))
11569 and then not In_Instance_Body
11570 and then Ada_Version < Ada_2005
11571 then
11572 Error_Msg_N
11573 ("aliased component type must be constrained (RM 3.6(11))",
11575 end if;
11576 end if;
11577 end if;
11578 end Check_Aliased_Component_Types;
11580 --------------------------------------
11581 -- Check_Anonymous_Access_Component --
11582 --------------------------------------
11584 procedure Check_Anonymous_Access_Component
11585 (Typ_Decl : Node_Id;
11586 Typ : Entity_Id;
11587 Prev : Entity_Id;
11588 Comp_Def : Node_Id;
11589 Access_Def : Node_Id)
11591 Loc : constant Source_Ptr := Sloc (Comp_Def);
11592 Anon_Access : Entity_Id;
11593 Acc_Def : Node_Id;
11594 Decl : Node_Id;
11595 Type_Def : Node_Id;
11597 procedure Build_Incomplete_Type_Declaration;
11598 -- If the record type contains components that include an access to the
11599 -- current record, then create an incomplete type declaration for the
11600 -- record, to be used as the designated type of the anonymous access.
11601 -- This is done only once, and only if there is no previous partial
11602 -- view of the type.
11604 function Designates_T (Subt : Node_Id) return Boolean;
11605 -- Check whether a node designates the enclosing record type, or 'Class
11606 -- of that type
11608 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11609 -- Check whether an access definition includes a reference to
11610 -- the enclosing record type. The reference can be a subtype mark
11611 -- in the access definition itself, a 'Class attribute reference, or
11612 -- recursively a reference appearing in a parameter specification
11613 -- or result definition of an access_to_subprogram definition.
11615 --------------------------------------
11616 -- Build_Incomplete_Type_Declaration --
11617 --------------------------------------
11619 procedure Build_Incomplete_Type_Declaration is
11620 Decl : Node_Id;
11621 Inc_T : Entity_Id;
11622 H : Entity_Id;
11624 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11625 -- it's "is new ... with record" or else "is tagged record ...".
11627 Typ_Def : constant Node_Id :=
11628 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11629 then Type_Definition (Typ_Decl) else Empty);
11630 Is_Tagged : constant Boolean :=
11631 Present (Typ_Def)
11632 and then
11633 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11634 and then
11635 Present (Record_Extension_Part (Typ_Def)))
11636 or else
11637 (Nkind (Typ_Def) = N_Record_Definition
11638 and then Tagged_Present (Typ_Def)));
11640 begin
11641 -- If there is a previous partial view, no need to create a new one
11642 -- If the partial view, given by Prev, is incomplete, If Prev is
11643 -- a private declaration, full declaration is flagged accordingly.
11645 if Prev /= Typ then
11646 if Is_Tagged then
11647 Make_Class_Wide_Type (Prev);
11648 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11649 Set_Etype (Class_Wide_Type (Typ), Typ);
11650 end if;
11652 return;
11654 elsif Has_Private_Declaration (Typ) then
11656 -- If we refer to T'Class inside T, and T is the completion of a
11657 -- private type, then make sure the class-wide type exists.
11659 if Is_Tagged then
11660 Make_Class_Wide_Type (Typ);
11661 end if;
11663 return;
11665 -- If there was a previous anonymous access type, the incomplete
11666 -- type declaration will have been created already.
11668 elsif Present (Current_Entity (Typ))
11669 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11670 and then Full_View (Current_Entity (Typ)) = Typ
11671 then
11672 if Is_Tagged
11673 and then Comes_From_Source (Current_Entity (Typ))
11674 and then not Is_Tagged_Type (Current_Entity (Typ))
11675 then
11676 Make_Class_Wide_Type (Typ);
11677 Error_Msg_N
11678 ("incomplete view of tagged type should be declared tagged??",
11679 Parent (Current_Entity (Typ)));
11680 end if;
11681 return;
11683 else
11684 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11685 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11687 -- Type has already been inserted into the current scope. Remove
11688 -- it, and add incomplete declaration for type, so that subsequent
11689 -- anonymous access types can use it. The entity is unchained from
11690 -- the homonym list and from immediate visibility. After analysis,
11691 -- the entity in the incomplete declaration becomes immediately
11692 -- visible in the record declaration that follows.
11694 H := Current_Entity (Typ);
11696 if H = Typ then
11697 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11699 else
11700 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11701 H := Homonym (Typ);
11702 end loop;
11704 Set_Homonym (H, Homonym (Typ));
11705 end if;
11707 Insert_Before (Typ_Decl, Decl);
11708 Analyze (Decl);
11709 Set_Full_View (Inc_T, Typ);
11710 Set_Incomplete_View (Typ_Decl, Inc_T);
11712 -- If the type is tagged, create a common class-wide type for
11713 -- both views, and set the Etype of the class-wide type to the
11714 -- full view.
11716 if Is_Tagged then
11717 Make_Class_Wide_Type (Inc_T);
11718 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11719 Set_Etype (Class_Wide_Type (Typ), Typ);
11720 end if;
11722 -- If the scope is a package with a limited view, create a shadow
11723 -- entity for the incomplete type like Build_Limited_Views, so as
11724 -- to make it possible for Remove_Limited_With_Unit to reinstall
11725 -- this incomplete type as the visible entity.
11727 if Ekind (Scope (Inc_T)) = E_Package
11728 and then Present (Limited_View (Scope (Inc_T)))
11729 then
11730 declare
11731 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11733 begin
11734 -- This is modeled on Build_Shadow_Entity
11736 Set_Chars (Shadow, Chars (Inc_T));
11737 Set_Parent (Shadow, Decl);
11738 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
11739 Set_Is_Internal (Shadow);
11740 Set_From_Limited_With (Shadow);
11741 Set_Non_Limited_View (Shadow, Inc_T);
11742 Set_Private_Dependents (Shadow, New_Elmt_List);
11744 if Is_Tagged then
11745 Set_Non_Limited_View
11746 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
11747 end if;
11749 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
11750 end;
11751 end if;
11752 end if;
11753 end Build_Incomplete_Type_Declaration;
11755 ------------------
11756 -- Designates_T --
11757 ------------------
11759 function Designates_T (Subt : Node_Id) return Boolean is
11760 Type_Id : constant Name_Id := Chars (Typ);
11762 function Names_T (Nam : Node_Id) return Boolean;
11763 -- The record type has not been introduced in the current scope
11764 -- yet, so we must examine the name of the type itself, either
11765 -- an identifier T, or an expanded name of the form P.T, where
11766 -- P denotes the current scope.
11768 -------------
11769 -- Names_T --
11770 -------------
11772 function Names_T (Nam : Node_Id) return Boolean is
11773 begin
11774 if Nkind (Nam) = N_Identifier then
11775 return Chars (Nam) = Type_Id;
11777 elsif Nkind (Nam) = N_Selected_Component then
11778 if Chars (Selector_Name (Nam)) = Type_Id then
11779 if Nkind (Prefix (Nam)) = N_Identifier then
11780 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11782 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11783 return Chars (Selector_Name (Prefix (Nam))) =
11784 Chars (Current_Scope);
11785 else
11786 return False;
11787 end if;
11789 else
11790 return False;
11791 end if;
11793 else
11794 return False;
11795 end if;
11796 end Names_T;
11798 -- Start of processing for Designates_T
11800 begin
11801 if Nkind (Subt) = N_Identifier then
11802 return Chars (Subt) = Type_Id;
11804 -- Reference can be through an expanded name which has not been
11805 -- analyzed yet, and which designates enclosing scopes.
11807 elsif Nkind (Subt) = N_Selected_Component then
11808 if Names_T (Subt) then
11809 return True;
11811 -- Otherwise it must denote an entity that is already visible.
11812 -- The access definition may name a subtype of the enclosing
11813 -- type, if there is a previous incomplete declaration for it.
11815 else
11816 Find_Selected_Component (Subt);
11817 return
11818 Is_Entity_Name (Subt)
11819 and then Scope (Entity (Subt)) = Current_Scope
11820 and then
11821 (Chars (Base_Type (Entity (Subt))) = Type_Id
11822 or else
11823 (Is_Class_Wide_Type (Entity (Subt))
11824 and then
11825 Chars (Etype (Base_Type (Entity (Subt)))) =
11826 Type_Id));
11827 end if;
11829 -- A reference to the current type may appear as the prefix of
11830 -- a 'Class attribute.
11832 elsif Nkind (Subt) = N_Attribute_Reference
11833 and then Attribute_Name (Subt) = Name_Class
11834 then
11835 return Names_T (Prefix (Subt));
11837 else
11838 return False;
11839 end if;
11840 end Designates_T;
11842 ----------------
11843 -- Mentions_T --
11844 ----------------
11846 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11847 Param_Spec : Node_Id;
11849 Acc_Subprg : constant Node_Id :=
11850 Access_To_Subprogram_Definition (Acc_Def);
11852 begin
11853 if No (Acc_Subprg) then
11854 return Designates_T (Subtype_Mark (Acc_Def));
11855 end if;
11857 -- Component is an access_to_subprogram: examine its formals,
11858 -- and result definition in the case of an access_to_function.
11860 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11861 while Present (Param_Spec) loop
11862 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11863 and then Mentions_T (Parameter_Type (Param_Spec))
11864 then
11865 return True;
11867 elsif Designates_T (Parameter_Type (Param_Spec)) then
11868 return True;
11869 end if;
11871 Next (Param_Spec);
11872 end loop;
11874 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11875 if Nkind (Result_Definition (Acc_Subprg)) =
11876 N_Access_Definition
11877 then
11878 return Mentions_T (Result_Definition (Acc_Subprg));
11879 else
11880 return Designates_T (Result_Definition (Acc_Subprg));
11881 end if;
11882 end if;
11884 return False;
11885 end Mentions_T;
11887 -- Start of processing for Check_Anonymous_Access_Component
11889 begin
11890 if Present (Access_Def) and then Mentions_T (Access_Def) then
11891 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
11893 Build_Incomplete_Type_Declaration;
11894 Anon_Access := Make_Temporary (Loc, 'S');
11896 -- Create a declaration for the anonymous access type: either
11897 -- an access_to_object or an access_to_subprogram.
11899 if Present (Acc_Def) then
11900 if Nkind (Acc_Def) = N_Access_Function_Definition then
11901 Type_Def :=
11902 Make_Access_Function_Definition (Loc,
11903 Parameter_Specifications =>
11904 Parameter_Specifications (Acc_Def),
11905 Result_Definition => Result_Definition (Acc_Def));
11906 else
11907 Type_Def :=
11908 Make_Access_Procedure_Definition (Loc,
11909 Parameter_Specifications =>
11910 Parameter_Specifications (Acc_Def));
11911 end if;
11913 else
11914 Type_Def :=
11915 Make_Access_To_Object_Definition (Loc,
11916 Subtype_Indication =>
11917 Relocate_Node (Subtype_Mark (Access_Def)));
11919 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
11920 Set_All_Present (Type_Def, All_Present (Access_Def));
11921 end if;
11923 Set_Null_Exclusion_Present
11924 (Type_Def, Null_Exclusion_Present (Access_Def));
11926 Decl :=
11927 Make_Full_Type_Declaration (Loc,
11928 Defining_Identifier => Anon_Access,
11929 Type_Definition => Type_Def);
11931 Insert_Before (Typ_Decl, Decl);
11932 Analyze (Decl);
11934 -- At first sight we could add here the extra formals of an access to
11935 -- subprogram; however, it must delayed till the freeze point so that
11936 -- we know the convention.
11938 if Nkind (Comp_Def) = N_Component_Definition then
11939 Rewrite (Comp_Def,
11940 Make_Component_Definition (Loc,
11941 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
11942 else
11943 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
11944 Rewrite (Comp_Def,
11945 Make_Discriminant_Specification (Loc,
11946 Defining_Identifier => Defining_Identifier (Comp_Def),
11947 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
11948 end if;
11950 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11951 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11952 else
11953 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
11954 end if;
11956 Set_Is_Local_Anonymous_Access (Anon_Access);
11957 end if;
11958 end Check_Anonymous_Access_Component;
11960 ---------------------------------------
11961 -- Check_Anonymous_Access_Components --
11962 ---------------------------------------
11964 procedure Check_Anonymous_Access_Components
11965 (Typ_Decl : Node_Id;
11966 Typ : Entity_Id;
11967 Prev : Entity_Id;
11968 Comp_List : Node_Id)
11970 Comp : Node_Id;
11971 begin
11972 if No (Comp_List) then
11973 return;
11974 end if;
11976 Comp := First (Component_Items (Comp_List));
11977 while Present (Comp) loop
11978 if Nkind (Comp) = N_Component_Declaration then
11979 Check_Anonymous_Access_Component
11980 (Typ_Decl, Typ, Prev,
11981 Component_Definition (Comp),
11982 Access_Definition (Component_Definition (Comp)));
11983 end if;
11985 Next (Comp);
11986 end loop;
11988 if Present (Variant_Part (Comp_List)) then
11989 declare
11990 V : Node_Id;
11991 begin
11992 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11993 while Present (V) loop
11994 Check_Anonymous_Access_Components
11995 (Typ_Decl, Typ, Prev, Component_List (V));
11996 Next_Non_Pragma (V);
11997 end loop;
11998 end;
11999 end if;
12000 end Check_Anonymous_Access_Components;
12002 ----------------------
12003 -- Check_Completion --
12004 ----------------------
12006 procedure Check_Completion (Body_Id : Node_Id := Empty) is
12007 E : Entity_Id;
12009 procedure Post_Error;
12010 -- Post error message for lack of completion for entity E
12012 ----------------
12013 -- Post_Error --
12014 ----------------
12016 procedure Post_Error is
12017 procedure Missing_Body;
12018 -- Output missing body message
12020 ------------------
12021 -- Missing_Body --
12022 ------------------
12024 procedure Missing_Body is
12025 begin
12026 -- Spec is in same unit, so we can post on spec
12028 if In_Same_Source_Unit (Body_Id, E) then
12029 Error_Msg_N ("missing body for &", E);
12031 -- Spec is in a separate unit, so we have to post on the body
12033 else
12034 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12035 end if;
12036 end Missing_Body;
12038 -- Start of processing for Post_Error
12040 begin
12041 if not Comes_From_Source (E) then
12042 if Ekind (E) in E_Task_Type | E_Protected_Type then
12044 -- It may be an anonymous protected type created for a
12045 -- single variable. Post error on variable, if present.
12047 declare
12048 Var : Entity_Id;
12050 begin
12051 Var := First_Entity (Current_Scope);
12052 while Present (Var) loop
12053 exit when Etype (Var) = E
12054 and then Comes_From_Source (Var);
12056 Next_Entity (Var);
12057 end loop;
12059 if Present (Var) then
12060 E := Var;
12061 end if;
12062 end;
12063 end if;
12064 end if;
12066 -- If a generated entity has no completion, then either previous
12067 -- semantic errors have disabled the expansion phase, or else we had
12068 -- missing subunits, or else we are compiling without expansion,
12069 -- or else something is very wrong.
12071 if not Comes_From_Source (E) then
12072 pragma Assert
12073 (Serious_Errors_Detected > 0
12074 or else Configurable_Run_Time_Violations > 0
12075 or else Subunits_Missing
12076 or else not Expander_Active);
12077 return;
12079 -- Here for source entity
12081 else
12082 -- Here if no body to post the error message, so we post the error
12083 -- on the declaration that has no completion. This is not really
12084 -- the right place to post it, think about this later ???
12086 if No (Body_Id) then
12087 if Is_Type (E) then
12088 Error_Msg_NE
12089 ("missing full declaration for }", Parent (E), E);
12090 else
12091 Error_Msg_NE ("missing body for &", Parent (E), E);
12092 end if;
12094 -- Package body has no completion for a declaration that appears
12095 -- in the corresponding spec. Post error on the body, with a
12096 -- reference to the non-completed declaration.
12098 else
12099 Error_Msg_Sloc := Sloc (E);
12101 if Is_Type (E) then
12102 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12104 elsif Is_Overloadable (E)
12105 and then Current_Entity_In_Scope (E) /= E
12106 then
12107 -- It may be that the completion is mistyped and appears as
12108 -- a distinct overloading of the entity.
12110 declare
12111 Candidate : constant Entity_Id :=
12112 Current_Entity_In_Scope (E);
12113 Decl : constant Node_Id :=
12114 Unit_Declaration_Node (Candidate);
12116 begin
12117 if Is_Overloadable (Candidate)
12118 and then Ekind (Candidate) = Ekind (E)
12119 and then Nkind (Decl) = N_Subprogram_Body
12120 and then Acts_As_Spec (Decl)
12121 then
12122 Check_Type_Conformant (Candidate, E);
12124 else
12125 Missing_Body;
12126 end if;
12127 end;
12129 else
12130 Missing_Body;
12131 end if;
12132 end if;
12133 end if;
12134 end Post_Error;
12136 -- Local variables
12138 Pack_Id : constant Entity_Id := Current_Scope;
12140 -- Start of processing for Check_Completion
12142 begin
12143 E := First_Entity (Pack_Id);
12144 while Present (E) loop
12145 if Is_Intrinsic_Subprogram (E) then
12146 null;
12148 -- The following situation requires special handling: a child unit
12149 -- that appears in the context clause of the body of its parent:
12151 -- procedure Parent.Child (...);
12153 -- with Parent.Child;
12154 -- package body Parent is
12156 -- Here Parent.Child appears as a local entity, but should not be
12157 -- flagged as requiring completion, because it is a compilation
12158 -- unit.
12160 -- Ignore missing completion for a subprogram that does not come from
12161 -- source (including the _Call primitive operation of RAS types,
12162 -- which has to have the flag Comes_From_Source for other purposes):
12163 -- we assume that the expander will provide the missing completion.
12164 -- In case of previous errors, other expansion actions that provide
12165 -- bodies for null procedures with not be invoked, so inhibit message
12166 -- in those cases.
12168 -- Note that E_Operator is not in the list that follows, because
12169 -- this kind is reserved for predefined operators, that are
12170 -- intrinsic and do not need completion.
12172 elsif Ekind (E) in E_Function
12173 | E_Procedure
12174 | E_Generic_Function
12175 | E_Generic_Procedure
12176 then
12177 if Has_Completion (E) then
12178 null;
12180 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12181 null;
12183 elsif Is_Subprogram (E)
12184 and then (not Comes_From_Source (E)
12185 or else Chars (E) = Name_uCall)
12186 then
12187 null;
12189 elsif
12190 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12191 then
12192 null;
12194 elsif Nkind (Parent (E)) = N_Procedure_Specification
12195 and then Null_Present (Parent (E))
12196 and then Serious_Errors_Detected > 0
12197 then
12198 null;
12200 else
12201 Post_Error;
12202 end if;
12204 elsif Is_Entry (E) then
12205 if not Has_Completion (E)
12206 and then Ekind (Scope (E)) = E_Protected_Type
12207 then
12208 Post_Error;
12209 end if;
12211 elsif Is_Package_Or_Generic_Package (E) then
12212 if Unit_Requires_Body (E) then
12213 if not Has_Completion (E)
12214 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12215 N_Compilation_Unit
12216 then
12217 Post_Error;
12218 end if;
12220 elsif not Is_Child_Unit (E) then
12221 May_Need_Implicit_Body (E);
12222 end if;
12224 -- A formal incomplete type (Ada 2012) does not require a completion;
12225 -- other incomplete type declarations do.
12227 elsif Ekind (E) = E_Incomplete_Type then
12228 if No (Underlying_Type (E))
12229 and then not Is_Generic_Type (E)
12230 then
12231 Post_Error;
12232 end if;
12234 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12235 if not Has_Completion (E) then
12236 Post_Error;
12237 end if;
12239 -- A single task declared in the current scope is a constant, verify
12240 -- that the body of its anonymous type is in the same scope. If the
12241 -- task is defined elsewhere, this may be a renaming declaration for
12242 -- which no completion is needed.
12244 elsif Ekind (E) = E_Constant then
12245 if Ekind (Etype (E)) = E_Task_Type
12246 and then not Has_Completion (Etype (E))
12247 and then Scope (Etype (E)) = Current_Scope
12248 then
12249 Post_Error;
12250 end if;
12252 elsif Ekind (E) = E_Record_Type then
12253 if Is_Tagged_Type (E) then
12254 Check_Abstract_Overriding (E);
12255 Check_Conventions (E);
12256 end if;
12258 Check_Aliased_Component_Types (E);
12260 elsif Ekind (E) = E_Array_Type then
12261 Check_Aliased_Component_Types (E);
12263 end if;
12265 Next_Entity (E);
12266 end loop;
12267 end Check_Completion;
12269 -------------------------------------
12270 -- Check_Constraining_Discriminant --
12271 -------------------------------------
12273 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12275 New_Type : constant Entity_Id := Etype (New_Disc);
12276 Old_Type : Entity_Id;
12278 begin
12279 -- If the record type contains an array constrained by the discriminant
12280 -- but with some different bound, the compiler tries to create a smaller
12281 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12282 -- In this case, where the discriminant type is a scalar type, the check
12283 -- must use the original discriminant type in the parent declaration.
12285 if Is_Scalar_Type (New_Type) then
12286 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12287 else
12288 Old_Type := Etype (Old_Disc);
12289 end if;
12291 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12292 Error_Msg_N
12293 ("subtype must be statically compatible with parent discriminant",
12294 New_Disc);
12296 if not Predicates_Compatible (New_Type, Old_Type) then
12297 Error_Msg_N
12298 ("\subtype predicate is not compatible with parent discriminant",
12299 New_Disc);
12300 end if;
12301 end if;
12302 end Check_Constraining_Discriminant;
12304 ------------------------------------
12305 -- Check_CPP_Type_Has_No_Defaults --
12306 ------------------------------------
12308 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12309 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12310 Clist : Node_Id;
12311 Comp : Node_Id;
12313 begin
12314 -- Obtain the component list
12316 if Nkind (Tdef) = N_Record_Definition then
12317 Clist := Component_List (Tdef);
12318 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12319 Clist := Component_List (Record_Extension_Part (Tdef));
12320 end if;
12322 -- Check all components to ensure no default expressions
12324 if Present (Clist) then
12325 Comp := First (Component_Items (Clist));
12326 while Present (Comp) loop
12327 if Present (Expression (Comp)) then
12328 Error_Msg_N
12329 ("component of imported 'C'P'P type cannot have "
12330 & "default expression", Expression (Comp));
12331 end if;
12333 Next (Comp);
12334 end loop;
12335 end if;
12336 end Check_CPP_Type_Has_No_Defaults;
12338 ----------------------------
12339 -- Check_Delta_Expression --
12340 ----------------------------
12342 procedure Check_Delta_Expression (E : Node_Id) is
12343 begin
12344 if not (Is_Real_Type (Etype (E))) then
12345 Wrong_Type (E, Any_Real);
12347 elsif not Is_OK_Static_Expression (E) then
12348 Flag_Non_Static_Expr
12349 ("non-static expression used for delta value!", E);
12351 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12352 Error_Msg_N ("delta expression must be positive", E);
12354 else
12355 return;
12356 end if;
12358 -- If any of above errors occurred, then replace the incorrect
12359 -- expression by the real 0.1, which should prevent further errors.
12361 Rewrite (E,
12362 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12363 Analyze_And_Resolve (E, Standard_Float);
12364 end Check_Delta_Expression;
12366 -----------------------------
12367 -- Check_Digits_Expression --
12368 -----------------------------
12370 procedure Check_Digits_Expression (E : Node_Id) is
12371 begin
12372 if not (Is_Integer_Type (Etype (E))) then
12373 Wrong_Type (E, Any_Integer);
12375 elsif not Is_OK_Static_Expression (E) then
12376 Flag_Non_Static_Expr
12377 ("non-static expression used for digits value!", E);
12379 elsif Expr_Value (E) <= 0 then
12380 Error_Msg_N ("digits value must be greater than zero", E);
12382 else
12383 return;
12384 end if;
12386 -- If any of above errors occurred, then replace the incorrect
12387 -- expression by the integer 1, which should prevent further errors.
12389 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12390 Analyze_And_Resolve (E, Standard_Integer);
12392 end Check_Digits_Expression;
12394 --------------------------
12395 -- Check_Initialization --
12396 --------------------------
12398 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12399 begin
12400 -- Special processing for limited types
12402 if Is_Limited_Type (T)
12403 and then not In_Instance
12404 and then not In_Inlined_Body
12405 then
12406 if not OK_For_Limited_Init (T, Exp) then
12408 -- In GNAT mode, this is just a warning, to allow it to be evilly
12409 -- turned off. Otherwise it is a real error.
12411 if GNAT_Mode then
12412 Error_Msg_N
12413 ("??cannot initialize entities of limited type!", Exp);
12415 elsif Ada_Version < Ada_2005 then
12417 -- The side effect removal machinery may generate illegal Ada
12418 -- code to avoid the usage of access types and 'reference in
12419 -- SPARK mode. Since this is legal code with respect to theorem
12420 -- proving, do not emit the error.
12422 if GNATprove_Mode
12423 and then Nkind (Exp) = N_Function_Call
12424 and then Nkind (Parent (Exp)) = N_Object_Declaration
12425 and then not Comes_From_Source
12426 (Defining_Identifier (Parent (Exp)))
12427 then
12428 null;
12430 else
12431 Error_Msg_N
12432 ("cannot initialize entities of limited type", Exp);
12433 Explain_Limited_Type (T, Exp);
12434 end if;
12436 else
12437 -- Specialize error message according to kind of illegal
12438 -- initial expression. We check the Original_Node to cover
12439 -- cases where the initialization expression of an object
12440 -- declaration generated by the compiler has been rewritten
12441 -- (such as for dispatching calls).
12443 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12444 and then
12445 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12446 then
12447 -- No error for internally-generated object declarations,
12448 -- which can come from build-in-place assignment statements.
12450 if Nkind (Parent (Exp)) = N_Object_Declaration
12451 and then not Comes_From_Source
12452 (Defining_Identifier (Parent (Exp)))
12453 then
12454 null;
12456 else
12457 Error_Msg_N
12458 ("illegal context for call to function with limited "
12459 & "result", Exp);
12460 end if;
12462 else
12463 Error_Msg_N
12464 ("initialization of limited object requires aggregate or "
12465 & "function call", Exp);
12466 end if;
12467 end if;
12468 end if;
12469 end if;
12471 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12472 -- set unless we can be sure that no range check is required.
12474 if not Expander_Active
12475 and then Is_Scalar_Type (T)
12476 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12477 then
12478 Set_Do_Range_Check (Exp);
12479 end if;
12480 end Check_Initialization;
12482 ----------------------
12483 -- Check_Interfaces --
12484 ----------------------
12486 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12487 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12489 Iface : Node_Id;
12490 Iface_Def : Node_Id;
12491 Iface_Typ : Entity_Id;
12492 Parent_Node : Node_Id;
12494 Is_Task : Boolean := False;
12495 -- Set True if parent type or any progenitor is a task interface
12497 Is_Protected : Boolean := False;
12498 -- Set True if parent type or any progenitor is a protected interface
12500 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12501 -- Check that a progenitor is compatible with declaration. If an error
12502 -- message is output, it is posted on Error_Node.
12504 ------------------
12505 -- Check_Ifaces --
12506 ------------------
12508 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12509 Iface_Id : constant Entity_Id :=
12510 Defining_Identifier (Parent (Iface_Def));
12511 Type_Def : Node_Id;
12513 begin
12514 if Nkind (N) = N_Private_Extension_Declaration then
12515 Type_Def := N;
12516 else
12517 Type_Def := Type_Definition (N);
12518 end if;
12520 if Is_Task_Interface (Iface_Id) then
12521 Is_Task := True;
12523 elsif Is_Protected_Interface (Iface_Id) then
12524 Is_Protected := True;
12525 end if;
12527 if Is_Synchronized_Interface (Iface_Id) then
12529 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12530 -- extension derived from a synchronized interface must explicitly
12531 -- be declared synchronized, because the full view will be a
12532 -- synchronized type.
12534 if Nkind (N) = N_Private_Extension_Declaration then
12535 if not Synchronized_Present (N) then
12536 Error_Msg_NE
12537 ("private extension of& must be explicitly synchronized",
12538 N, Iface_Id);
12539 end if;
12541 -- However, by 3.9.4(16/2), a full type that is a record extension
12542 -- is never allowed to derive from a synchronized interface (note
12543 -- that interfaces must be excluded from this check, because those
12544 -- are represented by derived type definitions in some cases).
12546 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12547 and then not Interface_Present (Type_Definition (N))
12548 then
12549 Error_Msg_N ("record extension cannot derive from synchronized "
12550 & "interface", Error_Node);
12551 end if;
12552 end if;
12554 -- Check that the characteristics of the progenitor are compatible
12555 -- with the explicit qualifier in the declaration.
12556 -- The check only applies to qualifiers that come from source.
12557 -- Limited_Present also appears in the declaration of corresponding
12558 -- records, and the check does not apply to them.
12560 if Limited_Present (Type_Def)
12561 and then not
12562 Is_Concurrent_Record_Type (Defining_Identifier (N))
12563 then
12564 if Is_Limited_Interface (Parent_Type)
12565 and then not Is_Limited_Interface (Iface_Id)
12566 then
12567 Error_Msg_NE
12568 ("progenitor & must be limited interface",
12569 Error_Node, Iface_Id);
12571 elsif
12572 (Task_Present (Iface_Def)
12573 or else Protected_Present (Iface_Def)
12574 or else Synchronized_Present (Iface_Def))
12575 and then Nkind (N) /= N_Private_Extension_Declaration
12576 and then not Error_Posted (N)
12577 then
12578 Error_Msg_NE
12579 ("progenitor & must be limited interface",
12580 Error_Node, Iface_Id);
12581 end if;
12583 -- Protected interfaces can only inherit from limited, synchronized
12584 -- or protected interfaces.
12586 elsif Nkind (N) = N_Full_Type_Declaration
12587 and then Protected_Present (Type_Def)
12588 then
12589 if Limited_Present (Iface_Def)
12590 or else Synchronized_Present (Iface_Def)
12591 or else Protected_Present (Iface_Def)
12592 then
12593 null;
12595 elsif Task_Present (Iface_Def) then
12596 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12597 & "from task interface", Error_Node);
12599 else
12600 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12601 & "from non-limited interface", Error_Node);
12602 end if;
12604 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12605 -- limited and synchronized.
12607 elsif Synchronized_Present (Type_Def) then
12608 if Limited_Present (Iface_Def)
12609 or else Synchronized_Present (Iface_Def)
12610 then
12611 null;
12613 elsif Protected_Present (Iface_Def)
12614 and then Nkind (N) /= N_Private_Extension_Declaration
12615 then
12616 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12617 & "from protected interface", Error_Node);
12619 elsif Task_Present (Iface_Def)
12620 and then Nkind (N) /= N_Private_Extension_Declaration
12621 then
12622 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12623 & "from task interface", Error_Node);
12625 elsif not Is_Limited_Interface (Iface_Id) then
12626 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12627 & "from non-limited interface", Error_Node);
12628 end if;
12630 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12631 -- synchronized or task interfaces.
12633 elsif Nkind (N) = N_Full_Type_Declaration
12634 and then Task_Present (Type_Def)
12635 then
12636 if Limited_Present (Iface_Def)
12637 or else Synchronized_Present (Iface_Def)
12638 or else Task_Present (Iface_Def)
12639 then
12640 null;
12642 elsif Protected_Present (Iface_Def) then
12643 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12644 & "protected interface", Error_Node);
12646 else
12647 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12648 & "non-limited interface", Error_Node);
12649 end if;
12650 end if;
12651 end Check_Ifaces;
12653 -- Start of processing for Check_Interfaces
12655 begin
12656 if Is_Interface (Parent_Type) then
12657 if Is_Task_Interface (Parent_Type) then
12658 Is_Task := True;
12660 elsif Is_Protected_Interface (Parent_Type) then
12661 Is_Protected := True;
12662 end if;
12663 end if;
12665 if Nkind (N) = N_Private_Extension_Declaration then
12667 -- Check that progenitors are compatible with declaration
12669 Iface := First (Interface_List (Def));
12670 while Present (Iface) loop
12671 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12673 Parent_Node := Parent (Base_Type (Iface_Typ));
12674 Iface_Def := Type_Definition (Parent_Node);
12676 if not Is_Interface (Iface_Typ) then
12677 Diagnose_Interface (Iface, Iface_Typ);
12678 else
12679 Check_Ifaces (Iface_Def, Iface);
12680 end if;
12682 Next (Iface);
12683 end loop;
12685 if Is_Task and Is_Protected then
12686 Error_Msg_N
12687 ("type cannot derive from task and protected interface", N);
12688 end if;
12690 return;
12691 end if;
12693 -- Full type declaration of derived type.
12694 -- Check compatibility with parent if it is interface type
12696 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12697 and then Is_Interface (Parent_Type)
12698 then
12699 Parent_Node := Parent (Parent_Type);
12701 -- More detailed checks for interface varieties
12703 Check_Ifaces
12704 (Iface_Def => Type_Definition (Parent_Node),
12705 Error_Node => Subtype_Indication (Type_Definition (N)));
12706 end if;
12708 Iface := First (Interface_List (Def));
12709 while Present (Iface) loop
12710 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12712 Parent_Node := Parent (Base_Type (Iface_Typ));
12713 Iface_Def := Type_Definition (Parent_Node);
12715 if not Is_Interface (Iface_Typ) then
12716 Diagnose_Interface (Iface, Iface_Typ);
12718 else
12719 -- "The declaration of a specific descendant of an interface
12720 -- type freezes the interface type" RM 13.14
12722 Freeze_Before (N, Iface_Typ);
12723 Check_Ifaces (Iface_Def, Error_Node => Iface);
12724 end if;
12726 Next (Iface);
12727 end loop;
12729 if Is_Task and Is_Protected then
12730 Error_Msg_N
12731 ("type cannot derive from task and protected interface", N);
12732 end if;
12733 end Check_Interfaces;
12735 ------------------------------------
12736 -- Check_Or_Process_Discriminants --
12737 ------------------------------------
12739 -- If an incomplete or private type declaration was already given for the
12740 -- type, the discriminants may have already been processed if they were
12741 -- present on the incomplete declaration. In this case a full conformance
12742 -- check has been performed in Find_Type_Name, and we then recheck here
12743 -- some properties that can't be checked on the partial view alone.
12744 -- Otherwise we call Process_Discriminants.
12746 procedure Check_Or_Process_Discriminants
12747 (N : Node_Id;
12748 T : Entity_Id;
12749 Prev : Entity_Id := Empty)
12751 begin
12752 if Has_Discriminants (T) then
12754 -- Discriminants are already set on T if they were already present
12755 -- on the partial view. Make them visible to component declarations.
12757 declare
12758 D : Entity_Id;
12759 -- Discriminant on T (full view) referencing expr on partial view
12761 Prev_D : Entity_Id;
12762 -- Entity of corresponding discriminant on partial view
12764 New_D : Node_Id;
12765 -- Discriminant specification for full view, expression is
12766 -- the syntactic copy on full view (which has been checked for
12767 -- conformance with partial view), only used here to post error
12768 -- message.
12770 begin
12771 D := First_Discriminant (T);
12772 New_D := First (Discriminant_Specifications (N));
12773 while Present (D) loop
12774 Prev_D := Current_Entity (D);
12775 Set_Current_Entity (D);
12776 Set_Is_Immediately_Visible (D);
12777 Set_Homonym (D, Prev_D);
12779 -- Handle the case where there is an untagged partial view and
12780 -- the full view is tagged: must disallow discriminants with
12781 -- defaults, unless compiling for Ada 2012, which allows a
12782 -- limited tagged type to have defaulted discriminants (see
12783 -- AI05-0214). However, suppress error here if it was already
12784 -- reported on the default expression of the partial view.
12786 if Is_Tagged_Type (T)
12787 and then Present (Expression (Parent (D)))
12788 and then (not Is_Limited_Type (Current_Scope)
12789 or else Ada_Version < Ada_2012)
12790 and then not Error_Posted (Expression (Parent (D)))
12791 then
12792 if Ada_Version >= Ada_2012 then
12793 Error_Msg_N
12794 ("discriminants of nonlimited tagged type cannot have "
12795 & "defaults",
12796 Expression (New_D));
12797 else
12798 Error_Msg_N
12799 ("discriminants of tagged type cannot have defaults",
12800 Expression (New_D));
12801 end if;
12802 end if;
12804 -- Ada 2005 (AI-230): Access discriminant allowed in
12805 -- non-limited record types.
12807 if Ada_Version < Ada_2005 then
12809 -- This restriction gets applied to the full type here. It
12810 -- has already been applied earlier to the partial view.
12812 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12813 end if;
12815 Next_Discriminant (D);
12816 Next (New_D);
12817 end loop;
12818 end;
12820 elsif Present (Discriminant_Specifications (N)) then
12821 Process_Discriminants (N, Prev);
12822 end if;
12823 end Check_Or_Process_Discriminants;
12825 ----------------------
12826 -- Check_Real_Bound --
12827 ----------------------
12829 procedure Check_Real_Bound (Bound : Node_Id) is
12830 begin
12831 if not Is_Real_Type (Etype (Bound)) then
12832 Error_Msg_N
12833 ("bound in real type definition must be of real type", Bound);
12835 elsif not Is_OK_Static_Expression (Bound) then
12836 Flag_Non_Static_Expr
12837 ("non-static expression used for real type bound!", Bound);
12839 else
12840 return;
12841 end if;
12843 Rewrite
12844 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12845 Analyze (Bound);
12846 Resolve (Bound, Standard_Float);
12847 end Check_Real_Bound;
12849 ------------------------------
12850 -- Complete_Private_Subtype --
12851 ------------------------------
12853 procedure Complete_Private_Subtype
12854 (Priv : Entity_Id;
12855 Full : Entity_Id;
12856 Full_Base : Entity_Id;
12857 Related_Nod : Node_Id)
12859 Save_Next_Entity : Entity_Id;
12860 Save_Homonym : Entity_Id;
12862 begin
12863 -- Set semantic attributes for (implicit) private subtype completion.
12864 -- If the full type has no discriminants, then it is a copy of the
12865 -- full view of the base. Otherwise, it is a subtype of the base with
12866 -- a possible discriminant constraint. Save and restore the original
12867 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12868 -- not corrupt the entity chain.
12870 Save_Next_Entity := Next_Entity (Full);
12871 Save_Homonym := Homonym (Priv);
12873 if Is_Private_Type (Full_Base)
12874 or else Is_Record_Type (Full_Base)
12875 or else Is_Concurrent_Type (Full_Base)
12876 then
12877 Copy_Node (Priv, Full);
12879 -- Note that the Etype of the full view is the same as the Etype of
12880 -- the partial view. In this fashion, the subtype has access to the
12881 -- correct view of the parent.
12883 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12884 Set_Has_Unknown_Discriminants
12885 (Full, Has_Unknown_Discriminants (Full_Base));
12886 Set_First_Entity (Full, First_Entity (Full_Base));
12887 Set_Last_Entity (Full, Last_Entity (Full_Base));
12889 -- If the underlying base type is constrained, we know that the
12890 -- full view of the subtype is constrained as well (the converse
12891 -- is not necessarily true).
12893 if Is_Constrained (Full_Base) then
12894 Set_Is_Constrained (Full);
12895 end if;
12897 else
12898 Copy_Node (Full_Base, Full);
12900 -- The following subtlety with the Etype of the full view needs to be
12901 -- taken into account here. One could think that it must naturally be
12902 -- set to the base type of the full base:
12904 -- Set_Etype (Full, Base_Type (Full_Base));
12906 -- so that the full view becomes a subtype of the full base when the
12907 -- latter is a base type, which must for example happen when the full
12908 -- base is declared as derived type. That's also correct if the full
12909 -- base is declared as an array type, or a floating-point type, or a
12910 -- fixed-point type, or a signed integer type, as these declarations
12911 -- create an implicit base type and a first subtype so the Etype of
12912 -- the full views must be the implicit base type. But that's wrong
12913 -- if the full base is declared as an access type, or an enumeration
12914 -- type, or a modular integer type, as these declarations directly
12915 -- create a base type, i.e. with Etype pointing to itself. Moreover
12916 -- the full base being declared in the private part, i.e. when the
12917 -- views are swapped, the end result is that the Etype of the full
12918 -- base is set to its private view in this case and that we need to
12919 -- propagate this setting to the full view in order for the subtype
12920 -- to be compatible with the base type.
12922 if Is_Base_Type (Full_Base)
12923 and then (Is_Derived_Type (Full_Base)
12924 or else Ekind (Full_Base) in Array_Kind
12925 or else Ekind (Full_Base) in Fixed_Point_Kind
12926 or else Ekind (Full_Base) in Float_Kind
12927 or else Ekind (Full_Base) in Signed_Integer_Kind)
12928 then
12929 Set_Etype (Full, Full_Base);
12930 end if;
12932 Set_Chars (Full, Chars (Priv));
12933 Set_Sloc (Full, Sloc (Priv));
12934 Conditional_Delay (Full, Priv);
12935 end if;
12937 Link_Entities (Full, Save_Next_Entity);
12938 Set_Homonym (Full, Save_Homonym);
12939 Set_Associated_Node_For_Itype (Full, Related_Nod);
12941 if Ekind (Full) in Incomplete_Or_Private_Kind then
12942 Reinit_Field_To_Zero (Full, F_Private_Dependents);
12943 end if;
12945 -- Set common attributes for all subtypes: kind, convention, etc.
12947 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12948 Set_Convention (Full, Convention (Full_Base));
12949 Set_Is_First_Subtype (Full, False);
12950 Set_Scope (Full, Scope (Priv));
12951 Set_Size_Info (Full, Full_Base);
12952 Copy_RM_Size (To => Full, From => Full_Base);
12953 Set_Is_Itype (Full);
12955 -- A subtype of a private-type-without-discriminants, whose full-view
12956 -- has discriminants with default expressions, is not constrained.
12958 if not Has_Discriminants (Priv) then
12959 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12961 if Has_Discriminants (Full_Base) then
12962 Set_Discriminant_Constraint
12963 (Full, Discriminant_Constraint (Full_Base));
12965 -- The partial view may have been indefinite, the full view
12966 -- might not be.
12968 Set_Has_Unknown_Discriminants
12969 (Full, Has_Unknown_Discriminants (Full_Base));
12970 end if;
12971 end if;
12973 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12974 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12976 -- Freeze the private subtype entity if its parent is delayed, and not
12977 -- already frozen. We skip this processing if the type is an anonymous
12978 -- subtype of a record component, or is the corresponding record of a
12979 -- protected type, since these are processed when the enclosing type
12980 -- is frozen. If the parent type is declared in a nested package then
12981 -- the freezing of the private and full views also happens later.
12983 if not Is_Type (Scope (Full)) then
12984 if Is_Itype (Priv)
12985 and then In_Same_Source_Unit (Full, Full_Base)
12986 and then Scope (Full_Base) /= Scope (Full)
12987 then
12988 Set_Has_Delayed_Freeze (Full);
12989 Set_Has_Delayed_Freeze (Priv);
12991 else
12992 Set_Has_Delayed_Freeze (Full,
12993 Has_Delayed_Freeze (Full_Base)
12994 and then not Is_Frozen (Full_Base));
12995 end if;
12996 end if;
12998 Set_Freeze_Node (Full, Empty);
12999 Set_Is_Frozen (Full, False);
13001 if Has_Discriminants (Full) then
13002 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
13003 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
13005 if Has_Unknown_Discriminants (Full) then
13006 Set_Discriminant_Constraint (Full, No_Elist);
13007 end if;
13008 end if;
13010 if Ekind (Full_Base) = E_Record_Type
13011 and then Has_Discriminants (Full_Base)
13012 and then Has_Discriminants (Priv) -- might not, if errors
13013 and then not Has_Unknown_Discriminants (Priv)
13014 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13015 then
13016 Create_Constrained_Components
13017 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13019 -- If the full base is itself derived from private, build a congruent
13020 -- subtype of its underlying full view, for use by the back end.
13022 elsif Is_Private_Type (Full_Base)
13023 and then Present (Underlying_Full_View (Full_Base))
13024 then
13025 declare
13026 Underlying_Full_Base : constant Entity_Id
13027 := Underlying_Full_View (Full_Base);
13028 Underlying_Full : constant Entity_Id
13029 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13030 begin
13031 Set_Is_Itype (Underlying_Full);
13032 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13033 Complete_Private_Subtype
13034 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13035 Set_Underlying_Full_View (Full, Underlying_Full);
13036 Set_Is_Underlying_Full_View (Underlying_Full);
13037 end;
13039 elsif Is_Record_Type (Full_Base) then
13041 -- Show Full is simply a renaming of Full_Base
13043 Set_Cloned_Subtype (Full, Full_Base);
13044 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13046 -- Propagate predicates
13048 Propagate_Predicate_Attributes (Full, Full_Base);
13049 end if;
13051 -- It is unsafe to share the bounds of a scalar type, because the Itype
13052 -- is elaborated on demand, and if a bound is nonstatic, then different
13053 -- orders of elaboration in different units will lead to different
13054 -- external symbols.
13056 if Is_Scalar_Type (Full_Base) then
13057 Set_Scalar_Range (Full,
13058 Make_Range (Sloc (Related_Nod),
13059 Low_Bound =>
13060 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13061 High_Bound =>
13062 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13064 -- This completion inherits the bounds of the full parent, but if
13065 -- the parent is an unconstrained floating point type, so is the
13066 -- completion.
13068 if Is_Floating_Point_Type (Full_Base) then
13069 Set_Includes_Infinities
13070 (Scalar_Range (Full), Has_Infinities (Full_Base));
13071 end if;
13072 end if;
13074 -- ??? It seems that a lot of fields are missing that should be copied
13075 -- from Full_Base to Full. Here are some that are introduced in a
13076 -- non-disruptive way but a cleanup is necessary.
13078 if Is_Tagged_Type (Full_Base) then
13079 Set_Is_Tagged_Type (Full);
13080 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13082 Set_Direct_Primitive_Operations
13083 (Full, Direct_Primitive_Operations (Full_Base));
13084 Set_No_Tagged_Streams_Pragma
13085 (Full, No_Tagged_Streams_Pragma (Full_Base));
13087 if Is_Interface (Full_Base) then
13088 Set_Is_Interface (Full);
13089 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13090 end if;
13092 -- Inherit class_wide type of full_base in case the partial view was
13093 -- not tagged. Otherwise it has already been created when the private
13094 -- subtype was analyzed.
13096 if No (Class_Wide_Type (Full)) then
13097 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13098 end if;
13100 -- If this is a subtype of a protected or task type, constrain its
13101 -- corresponding record, unless this is a subtype without constraints,
13102 -- i.e. a simple renaming as with an actual subtype in an instance.
13104 elsif Is_Concurrent_Type (Full_Base) then
13105 if Has_Discriminants (Full)
13106 and then Present (Corresponding_Record_Type (Full_Base))
13107 and then
13108 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13109 then
13110 Set_Corresponding_Record_Type (Full,
13111 Constrain_Corresponding_Record
13112 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13114 else
13115 Set_Corresponding_Record_Type (Full,
13116 Corresponding_Record_Type (Full_Base));
13117 end if;
13118 end if;
13120 -- Link rep item chain, and also setting of Has_Predicates from private
13121 -- subtype to full subtype, since we will need these on the full subtype
13122 -- to create the predicate function. Note that the full subtype may
13123 -- already have rep items, inherited from the full view of the base
13124 -- type, so we must be sure not to overwrite these entries.
13126 declare
13127 Append : Boolean;
13128 Item : Node_Id;
13129 Next_Item : Node_Id;
13130 Priv_Item : Node_Id;
13132 begin
13133 Item := First_Rep_Item (Full);
13134 Priv_Item := First_Rep_Item (Priv);
13136 -- If no existing rep items on full type, we can just link directly
13137 -- to the list of items on the private type, if any exist.. Same if
13138 -- the rep items are only those inherited from the base
13140 if (No (Item)
13141 or else Nkind (Item) /= N_Aspect_Specification
13142 or else Entity (Item) = Full_Base)
13143 and then Present (First_Rep_Item (Priv))
13144 then
13145 Set_First_Rep_Item (Full, Priv_Item);
13147 -- Otherwise, search to the end of items currently linked to the full
13148 -- subtype and append the private items to the end. However, if Priv
13149 -- and Full already have the same list of rep items, then the append
13150 -- is not done, as that would create a circularity.
13152 -- The partial view may have a predicate and the rep item lists of
13153 -- both views agree when inherited from the same ancestor. In that
13154 -- case, simply propagate the list from one view to the other.
13155 -- A more complex analysis needed here ???
13157 elsif Present (Priv_Item)
13158 and then Item = Next_Rep_Item (Priv_Item)
13159 then
13160 Set_First_Rep_Item (Full, Priv_Item);
13162 elsif Item /= Priv_Item then
13163 Append := True;
13164 loop
13165 Next_Item := Next_Rep_Item (Item);
13166 exit when No (Next_Item);
13167 Item := Next_Item;
13169 -- If the private view has aspect specifications, the full view
13170 -- inherits them. Since these aspects may already have been
13171 -- attached to the full view during derivation, do not append
13172 -- them if already present.
13174 if Item = First_Rep_Item (Priv) then
13175 Append := False;
13176 exit;
13177 end if;
13178 end loop;
13180 -- And link the private type items at the end of the chain
13182 if Append then
13183 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13184 end if;
13185 end if;
13186 end;
13188 -- Make sure Has_Predicates is set on full type if it is set on the
13189 -- private type. Note that it may already be set on the full type and
13190 -- if so, we don't want to unset it. Similarly, propagate information
13191 -- about delayed aspects, because the corresponding pragmas must be
13192 -- analyzed when one of the views is frozen. This last step is needed
13193 -- in particular when the full type is a scalar type for which an
13194 -- anonymous base type is constructed.
13196 -- The predicate functions are generated either at the freeze point
13197 -- of the type or at the end of the visible part, and we must avoid
13198 -- generating them twice.
13200 Propagate_Predicate_Attributes (Full, Priv);
13202 if Has_Delayed_Aspects (Priv) then
13203 Set_Has_Delayed_Aspects (Full);
13204 end if;
13205 end Complete_Private_Subtype;
13207 ----------------------------
13208 -- Constant_Redeclaration --
13209 ----------------------------
13211 procedure Constant_Redeclaration
13212 (Id : Entity_Id;
13213 N : Node_Id;
13214 T : out Entity_Id)
13216 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13217 Obj_Def : constant Node_Id := Object_Definition (N);
13218 New_T : Entity_Id;
13220 procedure Check_Possible_Deferred_Completion
13221 (Prev_Id : Entity_Id;
13222 Curr_Obj_Def : Node_Id);
13223 -- Determine whether the two object definitions describe the partial
13224 -- and the full view of a constrained deferred constant. Generate
13225 -- a subtype for the full view and verify that it statically matches
13226 -- the subtype of the partial view.
13228 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13229 -- If deferred constant is an access type initialized with an allocator,
13230 -- check whether there is an illegal recursion in the definition,
13231 -- through a default value of some record subcomponent. This is normally
13232 -- detected when generating init procs, but requires this additional
13233 -- mechanism when expansion is disabled.
13235 ----------------------------------------
13236 -- Check_Possible_Deferred_Completion --
13237 ----------------------------------------
13239 procedure Check_Possible_Deferred_Completion
13240 (Prev_Id : Entity_Id;
13241 Curr_Obj_Def : Node_Id)
13243 Curr_Typ : Entity_Id;
13244 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13245 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13246 Mismatch : Boolean := False;
13247 begin
13248 if Anon_Acc then
13249 null;
13250 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13251 declare
13252 Loc : constant Source_Ptr := Sloc (N);
13253 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13254 Decl : constant Node_Id :=
13255 Make_Subtype_Declaration (Loc,
13256 Defining_Identifier => Def_Id,
13257 Subtype_Indication =>
13258 Relocate_Node (Curr_Obj_Def));
13260 begin
13261 Insert_Before_And_Analyze (N, Decl);
13262 Set_Etype (Id, Def_Id);
13263 Curr_Typ := Def_Id;
13264 end;
13265 else
13266 Curr_Typ := Etype (Curr_Obj_Def);
13267 end if;
13269 if Anon_Acc then
13270 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13271 Mismatch := True;
13272 elsif Has_Null_Exclusion (Prev_Typ)
13273 and then not Null_Exclusion_Present (Curr_Obj_Def)
13274 then
13275 Mismatch := True;
13276 end if;
13277 -- ??? Another check needed: mismatch if disagreement
13278 -- between designated types/profiles .
13279 else
13280 Mismatch :=
13281 Is_Constrained (Prev_Typ)
13282 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13283 end if;
13285 if Mismatch then
13286 Error_Msg_Sloc := Sloc (Prev_Id);
13287 Error_Msg_N ("subtype does not statically match deferred "
13288 & "declaration #", N);
13289 end if;
13290 end Check_Possible_Deferred_Completion;
13292 ---------------------------------
13293 -- Check_Recursive_Declaration --
13294 ---------------------------------
13296 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13297 Comp : Entity_Id;
13299 begin
13300 if Is_Record_Type (Typ) then
13301 Comp := First_Component (Typ);
13302 while Present (Comp) loop
13303 if Comes_From_Source (Comp) then
13304 if Present (Expression (Parent (Comp)))
13305 and then Is_Entity_Name (Expression (Parent (Comp)))
13306 and then Entity (Expression (Parent (Comp))) = Prev
13307 then
13308 Error_Msg_Sloc := Sloc (Parent (Comp));
13309 Error_Msg_NE
13310 ("illegal circularity with declaration for & #",
13311 N, Comp);
13312 return;
13314 elsif Is_Record_Type (Etype (Comp)) then
13315 Check_Recursive_Declaration (Etype (Comp));
13316 end if;
13317 end if;
13319 Next_Component (Comp);
13320 end loop;
13321 end if;
13322 end Check_Recursive_Declaration;
13324 -- Start of processing for Constant_Redeclaration
13326 begin
13327 if Nkind (Parent (Prev)) = N_Object_Declaration then
13328 if Nkind (Object_Definition
13329 (Parent (Prev))) = N_Subtype_Indication
13330 then
13331 -- Find type of new declaration. The constraints of the two
13332 -- views must match statically, but there is no point in
13333 -- creating an itype for the full view.
13335 if Nkind (Obj_Def) = N_Subtype_Indication then
13336 Find_Type (Subtype_Mark (Obj_Def));
13337 New_T := Entity (Subtype_Mark (Obj_Def));
13339 else
13340 Find_Type (Obj_Def);
13341 New_T := Entity (Obj_Def);
13342 end if;
13344 T := Etype (Prev);
13346 else
13347 -- The full view may impose a constraint, even if the partial
13348 -- view does not, so construct the subtype.
13350 New_T := Find_Type_Of_Object (Obj_Def, N);
13351 T := New_T;
13352 end if;
13354 else
13355 -- Current declaration is illegal, diagnosed below in Enter_Name
13357 T := Empty;
13358 New_T := Any_Type;
13359 end if;
13361 -- If previous full declaration or a renaming declaration exists, or if
13362 -- a homograph is present, let Enter_Name handle it, either with an
13363 -- error or with the removal of an overridden implicit subprogram.
13364 -- The previous one is a full declaration if it has an expression
13365 -- (which in the case of an aggregate is indicated by the Init flag).
13367 if Ekind (Prev) /= E_Constant
13368 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13369 or else Present (Expression (Parent (Prev)))
13370 or else Has_Init_Expression (Parent (Prev))
13371 or else Present (Full_View (Prev))
13372 then
13373 Enter_Name (Id);
13375 -- Verify that types of both declarations match, or else that both types
13376 -- are anonymous access types whose designated subtypes statically match
13377 -- (as allowed in Ada 2005 by AI-385).
13379 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13380 and then
13381 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13382 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13383 or else Is_Access_Constant (Etype (New_T)) /=
13384 Is_Access_Constant (Etype (Prev))
13385 or else Can_Never_Be_Null (Etype (New_T)) /=
13386 Can_Never_Be_Null (Etype (Prev))
13387 or else Null_Exclusion_Present (Parent (Prev)) /=
13388 Null_Exclusion_Present (Parent (Id))
13389 or else not Subtypes_Statically_Match
13390 (Designated_Type (Etype (Prev)),
13391 Designated_Type (Etype (New_T))))
13392 then
13393 Error_Msg_Sloc := Sloc (Prev);
13394 Error_Msg_N ("type does not match declaration#", N);
13395 Set_Full_View (Prev, Id);
13396 Set_Etype (Id, Any_Type);
13398 -- A deferred constant whose type is an anonymous array is always
13399 -- illegal (unless imported). A detailed error message might be
13400 -- helpful for Ada beginners.
13402 if Nkind (Object_Definition (Parent (Prev)))
13403 = N_Constrained_Array_Definition
13404 and then Nkind (Object_Definition (N))
13405 = N_Constrained_Array_Definition
13406 then
13407 Error_Msg_N ("\each anonymous array is a distinct type", N);
13408 Error_Msg_N ("a deferred constant must have a named type",
13409 Object_Definition (Parent (Prev)));
13410 end if;
13412 elsif
13413 Null_Exclusion_Present (Parent (Prev))
13414 and then not Null_Exclusion_Present (N)
13415 then
13416 Error_Msg_Sloc := Sloc (Prev);
13417 Error_Msg_N ("null-exclusion does not match declaration#", N);
13418 Set_Full_View (Prev, Id);
13419 Set_Etype (Id, Any_Type);
13421 -- If so, process the full constant declaration
13423 else
13424 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13425 -- the deferred declaration is constrained, then the subtype defined
13426 -- by the subtype_indication in the full declaration shall match it
13427 -- statically.
13429 Check_Possible_Deferred_Completion
13430 (Prev_Id => Prev,
13431 Curr_Obj_Def => Obj_Def);
13433 Set_Full_View (Prev, Id);
13434 Set_Is_Public (Id, Is_Public (Prev));
13435 Set_Is_Internal (Id);
13436 Append_Entity (Id, Current_Scope);
13438 -- Check ALIASED present if present before (RM 7.4(7))
13440 if Is_Aliased (Prev)
13441 and then not Aliased_Present (N)
13442 then
13443 Error_Msg_Sloc := Sloc (Prev);
13444 Error_Msg_N ("ALIASED required (see declaration #)", N);
13445 end if;
13447 -- Check that placement is in private part and that the incomplete
13448 -- declaration appeared in the visible part.
13450 if Ekind (Current_Scope) = E_Package
13451 and then not In_Private_Part (Current_Scope)
13452 then
13453 Error_Msg_Sloc := Sloc (Prev);
13454 Error_Msg_N
13455 ("full constant for declaration # must be in private part", N);
13457 elsif Ekind (Current_Scope) = E_Package
13458 and then
13459 List_Containing (Parent (Prev)) /=
13460 Visible_Declarations (Package_Specification (Current_Scope))
13461 then
13462 Error_Msg_N
13463 ("deferred constant must be declared in visible part",
13464 Parent (Prev));
13465 end if;
13467 if Is_Access_Type (T)
13468 and then Nkind (Expression (N)) = N_Allocator
13469 then
13470 Check_Recursive_Declaration (Designated_Type (T));
13471 end if;
13473 -- A deferred constant is a visible entity. If type has invariants,
13474 -- verify that the initial value satisfies them. This is not done in
13475 -- GNATprove mode, as GNATprove handles invariant checks itself.
13477 if Has_Invariants (T)
13478 and then Present (Invariant_Procedure (T))
13479 and then not GNATprove_Mode
13480 then
13481 Insert_After (N,
13482 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13483 end if;
13484 end if;
13485 end Constant_Redeclaration;
13487 ----------------------
13488 -- Constrain_Access --
13489 ----------------------
13491 procedure Constrain_Access
13492 (Def_Id : in out Entity_Id;
13493 S : Node_Id;
13494 Related_Nod : Node_Id)
13496 T : constant Entity_Id := Entity (Subtype_Mark (S));
13497 Desig_Type : constant Entity_Id := Designated_Type (T);
13498 Desig_Subtype : Entity_Id;
13499 Constraint_OK : Boolean := True;
13501 begin
13502 if Is_Array_Type (Desig_Type) then
13503 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13504 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13506 elsif (Is_Record_Type (Desig_Type)
13507 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13508 and then not Is_Constrained (Desig_Type)
13509 then
13510 -- If this is a constrained access definition for a record
13511 -- component, we leave the type as an unconstrained access,
13512 -- and mark the component so that its actual type is built
13513 -- at a point of use (e.g., an assignment statement). This
13514 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13516 if Desig_Type = Current_Scope
13517 and then No (Def_Id)
13518 then
13519 Desig_Subtype :=
13520 Create_Itype
13521 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13522 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13523 Def_Id := Entity (Subtype_Mark (S));
13525 -- We indicate that the component has a per-object constraint
13526 -- for treatment at a point of use, even though the constraint
13527 -- may be independent of discriminants of the enclosing type.
13529 if Nkind (Related_Nod) = N_Component_Declaration then
13530 Set_Has_Per_Object_Constraint
13531 (Defining_Identifier (Related_Nod));
13532 end if;
13534 -- This call added to ensure that the constraint is analyzed
13535 -- (needed for a B test). Note that we still return early from
13536 -- this procedure to avoid recursive processing.
13538 Constrain_Discriminated_Type
13539 (Desig_Subtype, S, Related_Nod, For_Access => True);
13540 return;
13541 end if;
13543 -- Enforce rule that the constraint is illegal if there is an
13544 -- unconstrained view of the designated type. This means that the
13545 -- partial view (either a private type declaration or a derivation
13546 -- from a private type) has no discriminants. (Defect Report
13547 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13549 -- Rule updated for Ada 2005: The private type is said to have
13550 -- a constrained partial view, given that objects of the type
13551 -- can be declared. Furthermore, the rule applies to all access
13552 -- types, unlike the rule concerning default discriminants (see
13553 -- RM 3.7.1(7/3))
13555 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13556 and then Has_Private_Declaration (Desig_Type)
13557 and then In_Open_Scopes (Scope (Desig_Type))
13558 and then Has_Discriminants (Desig_Type)
13559 then
13560 declare
13561 Pack : constant Node_Id :=
13562 Unit_Declaration_Node (Scope (Desig_Type));
13563 Decls : List_Id;
13564 Decl : Node_Id;
13566 begin
13567 if Nkind (Pack) = N_Package_Declaration then
13568 Decls := Visible_Declarations (Specification (Pack));
13569 Decl := First (Decls);
13570 while Present (Decl) loop
13571 if (Nkind (Decl) = N_Private_Type_Declaration
13572 and then Chars (Defining_Identifier (Decl)) =
13573 Chars (Desig_Type))
13575 or else
13576 (Nkind (Decl) = N_Full_Type_Declaration
13577 and then
13578 Chars (Defining_Identifier (Decl)) =
13579 Chars (Desig_Type)
13580 and then Is_Derived_Type (Desig_Type)
13581 and then
13582 Has_Private_Declaration (Etype (Desig_Type)))
13583 then
13584 if No (Discriminant_Specifications (Decl)) then
13585 Error_Msg_N
13586 ("cannot constrain access type if designated "
13587 & "type has constrained partial view", S);
13588 end if;
13590 exit;
13591 end if;
13593 Next (Decl);
13594 end loop;
13595 end if;
13596 end;
13597 end if;
13599 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13600 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13601 For_Access => True);
13603 elsif Is_Concurrent_Type (Desig_Type)
13604 and then not Is_Constrained (Desig_Type)
13605 then
13606 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13607 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13609 else
13610 Error_Msg_N ("invalid constraint on access type", S);
13612 -- We simply ignore an invalid constraint
13614 Desig_Subtype := Desig_Type;
13615 Constraint_OK := False;
13616 end if;
13618 if No (Def_Id) then
13619 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13620 else
13621 Mutate_Ekind (Def_Id, E_Access_Subtype);
13622 end if;
13624 if Constraint_OK then
13625 Set_Etype (Def_Id, Base_Type (T));
13627 if Is_Private_Type (Desig_Type) then
13628 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13629 end if;
13630 else
13631 Set_Etype (Def_Id, Any_Type);
13632 end if;
13634 Set_Size_Info (Def_Id, T);
13635 Set_Is_Constrained (Def_Id, Constraint_OK);
13636 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13637 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13638 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13639 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13641 Conditional_Delay (Def_Id, T);
13643 -- AI-363 : Subtypes of general access types whose designated types have
13644 -- default discriminants are disallowed. In instances, the rule has to
13645 -- be checked against the actual, of which T is the subtype. In a
13646 -- generic body, the rule is checked assuming that the actual type has
13647 -- defaulted discriminants.
13649 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13650 if Ekind (Base_Type (T)) = E_General_Access_Type
13651 and then Has_Defaulted_Discriminants (Desig_Type)
13652 then
13653 if Ada_Version < Ada_2005 then
13654 Error_Msg_N
13655 ("access subtype of general access type would not " &
13656 "be allowed in Ada 2005?y?", S);
13657 else
13658 Error_Msg_N
13659 ("access subtype of general access type not allowed", S);
13660 end if;
13662 Error_Msg_N ("\discriminants have defaults", S);
13664 elsif Is_Access_Type (T)
13665 and then Is_Generic_Type (Desig_Type)
13666 and then Has_Discriminants (Desig_Type)
13667 and then In_Package_Body (Current_Scope)
13668 then
13669 if Ada_Version < Ada_2005 then
13670 Error_Msg_N
13671 ("access subtype would not be allowed in generic body "
13672 & "in Ada 2005?y?", S);
13673 else
13674 Error_Msg_N
13675 ("access subtype not allowed in generic body", S);
13676 end if;
13678 Error_Msg_N
13679 ("\designated type is a discriminated formal", S);
13680 end if;
13681 end if;
13682 end Constrain_Access;
13684 ---------------------
13685 -- Constrain_Array --
13686 ---------------------
13688 procedure Constrain_Array
13689 (Def_Id : in out Entity_Id;
13690 SI : Node_Id;
13691 Related_Nod : Node_Id;
13692 Related_Id : Entity_Id;
13693 Suffix : Character)
13695 C : constant Node_Id := Constraint (SI);
13696 Number_Of_Constraints : Nat := 0;
13697 Index : Node_Id;
13698 S, T : Entity_Id;
13699 Constraint_OK : Boolean := True;
13700 Is_FLB_Array_Subtype : Boolean := False;
13702 begin
13703 T := Entity (Subtype_Mark (SI));
13705 if Is_Access_Type (T) then
13706 T := Designated_Type (T);
13707 end if;
13709 T := Underlying_Type (T);
13711 -- If an index constraint follows a subtype mark in a subtype indication
13712 -- then the type or subtype denoted by the subtype mark must not already
13713 -- impose an index constraint. The subtype mark must denote either an
13714 -- unconstrained array type or an access type whose designated type
13715 -- is such an array type... (RM 3.6.1)
13717 if Is_Constrained (T) then
13718 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13719 Constraint_OK := False;
13721 else
13722 S := First (Constraints (C));
13723 while Present (S) loop
13724 Number_Of_Constraints := Number_Of_Constraints + 1;
13725 Next (S);
13726 end loop;
13728 -- In either case, the index constraint must provide a discrete
13729 -- range for each index of the array type and the type of each
13730 -- discrete range must be the same as that of the corresponding
13731 -- index. (RM 3.6.1)
13733 if Number_Of_Constraints /= Number_Dimensions (T) then
13734 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13735 Constraint_OK := False;
13737 else
13738 S := First (Constraints (C));
13739 Index := First_Index (T);
13740 Analyze (Index);
13742 -- Apply constraints to each index type
13744 for J in 1 .. Number_Of_Constraints loop
13745 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13747 -- If the subtype of the index has been set to indicate that
13748 -- it has a fixed lower bound, then record that the subtype's
13749 -- entity will need to be marked as being a fixed-lower-bound
13750 -- array subtype.
13752 if S = First (Constraints (C)) then
13753 Is_FLB_Array_Subtype :=
13754 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13756 -- If the parent subtype (or should this be Etype of that?)
13757 -- is an FLB array subtype, we flag an error, because we
13758 -- don't currently allow subtypes of such subtypes to
13759 -- specify a fixed lower bound for any of their indexes,
13760 -- even if the index of the parent subtype is a "range <>"
13761 -- index.
13763 if Is_FLB_Array_Subtype
13764 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13765 then
13766 Error_Msg_NE
13767 ("index with fixed lower bound not allowed for subtype "
13768 & "of fixed-lower-bound }", S, T);
13770 Is_FLB_Array_Subtype := False;
13771 end if;
13773 elsif Is_FLB_Array_Subtype
13774 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13775 then
13776 Error_Msg_NE
13777 ("constrained index not allowed for fixed-lower-bound "
13778 & "subtype of}", S, T);
13780 elsif not Is_FLB_Array_Subtype
13781 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13782 then
13783 Error_Msg_NE
13784 ("index with fixed lower bound not allowed for "
13785 & "constrained subtype of}", S, T);
13786 end if;
13788 Next (Index);
13789 Next (S);
13790 end loop;
13792 end if;
13793 end if;
13795 if No (Def_Id) then
13796 Def_Id :=
13797 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13798 Set_Parent (Def_Id, Related_Nod);
13800 else
13801 Mutate_Ekind (Def_Id, E_Array_Subtype);
13802 end if;
13804 Set_Size_Info (Def_Id, (T));
13805 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13806 Set_Etype (Def_Id, Base_Type (T));
13808 if Constraint_OK then
13809 Set_First_Index (Def_Id, First (Constraints (C)));
13810 else
13811 Set_First_Index (Def_Id, First_Index (T));
13812 end if;
13814 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13815 Set_Is_Fixed_Lower_Bound_Array_Subtype
13816 (Def_Id, Is_FLB_Array_Subtype);
13817 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13818 Set_Is_Independent (Def_Id, Is_Independent (T));
13819 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13821 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13822 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13824 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13825 -- We need to initialize the attribute because if Def_Id is previously
13826 -- analyzed through a limited_with clause, it will have the attributes
13827 -- of an incomplete type, one of which is an Elist that overlaps the
13828 -- Packed_Array_Impl_Type field.
13830 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13832 -- Build a freeze node if parent still needs one. Also make sure that
13833 -- the Depends_On_Private status is set because the subtype will need
13834 -- reprocessing at the time the base type does, and also we must set a
13835 -- conditional delay.
13837 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13838 Conditional_Delay (Def_Id, T);
13839 end Constrain_Array;
13841 ------------------------------
13842 -- Constrain_Component_Type --
13843 ------------------------------
13845 function Constrain_Component_Type
13846 (Comp : Entity_Id;
13847 Constrained_Typ : Entity_Id;
13848 Related_Node : Node_Id;
13849 Typ : Entity_Id;
13850 Constraints : Elist_Id) return Entity_Id
13852 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13853 Compon_Type : constant Entity_Id := Etype (Comp);
13855 function Build_Constrained_Array_Type
13856 (Old_Type : Entity_Id) return Entity_Id;
13857 -- If Old_Type is an array type, one of whose indexes is constrained
13858 -- by a discriminant, build an Itype whose constraint replaces the
13859 -- discriminant with its value in the constraint.
13861 function Build_Constrained_Discriminated_Type
13862 (Old_Type : Entity_Id) return Entity_Id;
13863 -- Ditto for record components. Handle the case where the constraint
13864 -- is a conversion of the discriminant value, introduced during
13865 -- expansion.
13867 function Build_Constrained_Access_Type
13868 (Old_Type : Entity_Id) return Entity_Id;
13869 -- Ditto for access types. Makes use of previous two functions, to
13870 -- constrain designated type.
13872 function Is_Discriminant (Expr : Node_Id) return Boolean;
13873 -- Returns True if Expr is a discriminant
13875 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13876 -- Find the value of a discriminant named by Discr_Expr in Constraints
13878 -----------------------------------
13879 -- Build_Constrained_Access_Type --
13880 -----------------------------------
13882 function Build_Constrained_Access_Type
13883 (Old_Type : Entity_Id) return Entity_Id
13885 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13886 Itype : Entity_Id;
13887 Desig_Subtype : Entity_Id;
13888 Scop : Entity_Id;
13890 begin
13891 -- If the original access type was not embedded in the enclosing
13892 -- type definition, there is no need to produce a new access
13893 -- subtype. In fact every access type with an explicit constraint
13894 -- generates an itype whose scope is the enclosing record.
13896 if not Is_Type (Scope (Old_Type)) then
13897 return Old_Type;
13899 elsif Is_Array_Type (Desig_Type) then
13900 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13902 elsif Has_Discriminants (Desig_Type) then
13904 -- This may be an access type to an enclosing record type for
13905 -- which we are constructing the constrained components. Return
13906 -- the enclosing record subtype. This is not always correct,
13907 -- but avoids infinite recursion. ???
13909 Desig_Subtype := Any_Type;
13911 for J in reverse 0 .. Scope_Stack.Last loop
13912 Scop := Scope_Stack.Table (J).Entity;
13914 if Is_Type (Scop)
13915 and then Base_Type (Scop) = Base_Type (Desig_Type)
13916 then
13917 Desig_Subtype := Scop;
13918 end if;
13920 exit when not Is_Type (Scop);
13921 end loop;
13923 if Desig_Subtype = Any_Type then
13924 Desig_Subtype :=
13925 Build_Constrained_Discriminated_Type (Desig_Type);
13926 end if;
13928 else
13929 return Old_Type;
13930 end if;
13932 if Desig_Subtype /= Desig_Type then
13934 -- The Related_Node better be here or else we won't be able
13935 -- to attach new itypes to a node in the tree.
13937 pragma Assert (Present (Related_Node));
13939 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13941 Set_Etype (Itype, Base_Type (Old_Type));
13942 Set_Size_Info (Itype, (Old_Type));
13943 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13944 Set_Depends_On_Private (Itype, Has_Private_Component
13945 (Old_Type));
13946 Set_Is_Access_Constant (Itype, Is_Access_Constant
13947 (Old_Type));
13949 -- The new itype needs freezing when it depends on a not frozen
13950 -- type and the enclosing subtype needs freezing.
13952 if Has_Delayed_Freeze (Constrained_Typ)
13953 and then not Is_Frozen (Constrained_Typ)
13954 then
13955 Conditional_Delay (Itype, Base_Type (Old_Type));
13956 end if;
13958 return Itype;
13960 else
13961 return Old_Type;
13962 end if;
13963 end Build_Constrained_Access_Type;
13965 ----------------------------------
13966 -- Build_Constrained_Array_Type --
13967 ----------------------------------
13969 function Build_Constrained_Array_Type
13970 (Old_Type : Entity_Id) return Entity_Id
13972 Lo_Expr : Node_Id;
13973 Hi_Expr : Node_Id;
13974 Old_Index : Node_Id;
13975 Range_Node : Node_Id;
13976 Constr_List : List_Id;
13978 Need_To_Create_Itype : Boolean := False;
13980 begin
13981 Old_Index := First_Index (Old_Type);
13982 while Present (Old_Index) loop
13983 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13985 if Is_Discriminant (Lo_Expr)
13986 or else
13987 Is_Discriminant (Hi_Expr)
13988 then
13989 Need_To_Create_Itype := True;
13990 exit;
13991 end if;
13993 Next_Index (Old_Index);
13994 end loop;
13996 if Need_To_Create_Itype then
13997 Constr_List := New_List;
13999 Old_Index := First_Index (Old_Type);
14000 while Present (Old_Index) loop
14001 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14003 if Is_Discriminant (Lo_Expr) then
14004 Lo_Expr := Get_Discr_Value (Lo_Expr);
14005 end if;
14007 if Is_Discriminant (Hi_Expr) then
14008 Hi_Expr := Get_Discr_Value (Hi_Expr);
14009 end if;
14011 Range_Node :=
14012 Make_Range
14013 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14015 Append (Range_Node, To => Constr_List);
14017 Next_Index (Old_Index);
14018 end loop;
14020 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14022 else
14023 return Old_Type;
14024 end if;
14025 end Build_Constrained_Array_Type;
14027 ------------------------------------------
14028 -- Build_Constrained_Discriminated_Type --
14029 ------------------------------------------
14031 function Build_Constrained_Discriminated_Type
14032 (Old_Type : Entity_Id) return Entity_Id
14034 Expr : Node_Id;
14035 Constr_List : List_Id;
14036 Old_Constraint : Elmt_Id;
14038 Need_To_Create_Itype : Boolean := False;
14040 begin
14041 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14042 while Present (Old_Constraint) loop
14043 Expr := Node (Old_Constraint);
14045 if Is_Discriminant (Expr) then
14046 Need_To_Create_Itype := True;
14047 exit;
14049 -- After expansion of discriminated task types, the value
14050 -- of the discriminant may be converted to a run-time type
14051 -- for restricted run-times. Propagate the value of the
14052 -- discriminant as well, so that e.g. the secondary stack
14053 -- component has a static constraint. Necessary for LLVM.
14055 elsif Nkind (Expr) = N_Type_Conversion
14056 and then Is_Discriminant (Expression (Expr))
14057 then
14058 Need_To_Create_Itype := True;
14059 exit;
14060 end if;
14062 Next_Elmt (Old_Constraint);
14063 end loop;
14065 if Need_To_Create_Itype then
14066 Constr_List := New_List;
14068 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14069 while Present (Old_Constraint) loop
14070 Expr := Node (Old_Constraint);
14072 if Is_Discriminant (Expr) then
14073 Expr := Get_Discr_Value (Expr);
14075 elsif Nkind (Expr) = N_Type_Conversion
14076 and then Is_Discriminant (Expression (Expr))
14077 then
14078 Expr := New_Copy_Tree (Expr);
14079 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14080 end if;
14082 Append (New_Copy_Tree (Expr), To => Constr_List);
14084 Next_Elmt (Old_Constraint);
14085 end loop;
14087 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14089 else
14090 return Old_Type;
14091 end if;
14092 end Build_Constrained_Discriminated_Type;
14094 ---------------------
14095 -- Get_Discr_Value --
14096 ---------------------
14098 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14099 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14100 -- Entity of a discriminant that appear as a standalone expression in
14101 -- the constraint of a component.
14103 D : Entity_Id;
14104 E : Elmt_Id;
14106 begin
14107 -- The discriminant may be declared for the type, in which case we
14108 -- find it by iterating over the list of discriminants. If the
14109 -- discriminant is inherited from a parent type, it appears as the
14110 -- corresponding discriminant of the current type. This will be the
14111 -- case when constraining an inherited component whose constraint is
14112 -- given by a discriminant of the parent.
14114 D := First_Discriminant (Typ);
14115 E := First_Elmt (Constraints);
14117 while Present (D) loop
14118 if D = Discr_Id
14119 or else D = CR_Discriminant (Discr_Id)
14120 or else Corresponding_Discriminant (D) = Discr_Id
14121 then
14122 return New_Copy_Tree (Node (E));
14123 end if;
14125 Next_Discriminant (D);
14126 Next_Elmt (E);
14127 end loop;
14129 -- The Corresponding_Discriminant mechanism is incomplete, because
14130 -- the correspondence between new and old discriminants is not one
14131 -- to one: one new discriminant can constrain several old ones. In
14132 -- that case, scan sequentially the stored_constraint, the list of
14133 -- discriminants of the parents, and the constraints.
14135 -- Previous code checked for the present of the Stored_Constraint
14136 -- list for the derived type, but did not use it at all. Should it
14137 -- be present when the component is a discriminated task type?
14139 if Is_Derived_Type (Typ)
14140 and then Scope (Discr_Id) = Etype (Typ)
14141 then
14142 D := First_Discriminant (Etype (Typ));
14143 E := First_Elmt (Constraints);
14144 while Present (D) loop
14145 if D = Discr_Id then
14146 return New_Copy_Tree (Node (E));
14147 end if;
14149 Next_Discriminant (D);
14150 Next_Elmt (E);
14151 end loop;
14152 end if;
14154 -- Something is wrong if we did not find the value
14156 raise Program_Error;
14157 end Get_Discr_Value;
14159 ---------------------
14160 -- Is_Discriminant --
14161 ---------------------
14163 function Is_Discriminant (Expr : Node_Id) return Boolean is
14164 Discrim_Scope : Entity_Id;
14166 begin
14167 if Denotes_Discriminant (Expr) then
14168 Discrim_Scope := Scope (Entity (Expr));
14170 -- Either we have a reference to one of Typ's discriminants,
14172 pragma Assert (Discrim_Scope = Typ
14174 -- or to the discriminants of the parent type, in the case
14175 -- of a derivation of a tagged type with variants.
14177 or else Discrim_Scope = Etype (Typ)
14178 or else Full_View (Discrim_Scope) = Etype (Typ)
14180 -- or same as above for the case where the discriminants
14181 -- were declared in Typ's private view.
14183 or else (Is_Private_Type (Discrim_Scope)
14184 and then Chars (Discrim_Scope) = Chars (Typ))
14186 -- or else we are deriving from the full view and the
14187 -- discriminant is declared in the private entity.
14189 or else (Is_Private_Type (Typ)
14190 and then Chars (Discrim_Scope) = Chars (Typ))
14192 -- Or we are constrained the corresponding record of a
14193 -- synchronized type that completes a private declaration.
14195 or else (Is_Concurrent_Record_Type (Typ)
14196 and then
14197 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14199 -- or we have a class-wide type, in which case make sure the
14200 -- discriminant found belongs to the root type.
14202 or else (Is_Class_Wide_Type (Typ)
14203 and then Etype (Typ) = Discrim_Scope));
14205 return True;
14206 end if;
14208 -- In all other cases we have something wrong
14210 return False;
14211 end Is_Discriminant;
14213 -- Start of processing for Constrain_Component_Type
14215 begin
14216 if Nkind (Parent (Comp)) = N_Component_Declaration
14217 and then Comes_From_Source (Parent (Comp))
14218 and then Comes_From_Source
14219 (Subtype_Indication (Component_Definition (Parent (Comp))))
14220 and then
14221 Is_Entity_Name
14222 (Subtype_Indication (Component_Definition (Parent (Comp))))
14223 then
14224 return Compon_Type;
14226 elsif Is_Array_Type (Compon_Type) then
14227 return Build_Constrained_Array_Type (Compon_Type);
14229 elsif Has_Discriminants (Compon_Type) then
14230 return Build_Constrained_Discriminated_Type (Compon_Type);
14232 elsif Is_Access_Type (Compon_Type) then
14233 return Build_Constrained_Access_Type (Compon_Type);
14235 else
14236 return Compon_Type;
14237 end if;
14238 end Constrain_Component_Type;
14240 --------------------------
14241 -- Constrain_Concurrent --
14242 --------------------------
14244 -- For concurrent types, the associated record value type carries the same
14245 -- discriminants, so when we constrain a concurrent type, we must constrain
14246 -- the corresponding record type as well.
14248 procedure Constrain_Concurrent
14249 (Def_Id : in out Entity_Id;
14250 SI : Node_Id;
14251 Related_Nod : Node_Id;
14252 Related_Id : Entity_Id;
14253 Suffix : Character)
14255 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14256 -- case of a private subtype (needed when only doing semantic analysis).
14258 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14259 T_Val : Entity_Id;
14261 begin
14262 if Is_Access_Type (T_Ent) then
14263 T_Ent := Designated_Type (T_Ent);
14264 end if;
14266 T_Val := Corresponding_Record_Type (T_Ent);
14268 if Present (T_Val) then
14270 if No (Def_Id) then
14271 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14273 -- Elaborate itype now, as it may be used in a subsequent
14274 -- synchronized operation in another scope.
14276 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14277 Build_Itype_Reference (Def_Id, Related_Nod);
14278 end if;
14279 end if;
14281 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14282 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14284 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14285 Set_Corresponding_Record_Type (Def_Id,
14286 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14288 else
14289 -- If there is no associated record, expansion is disabled and this
14290 -- is a generic context. Create a subtype in any case, so that
14291 -- semantic analysis can proceed.
14293 if No (Def_Id) then
14294 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14295 end if;
14297 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14298 end if;
14299 end Constrain_Concurrent;
14301 ------------------------------------
14302 -- Constrain_Corresponding_Record --
14303 ------------------------------------
14305 function Constrain_Corresponding_Record
14306 (Prot_Subt : Entity_Id;
14307 Corr_Rec : Entity_Id;
14308 Related_Nod : Node_Id) return Entity_Id
14310 T_Sub : constant Entity_Id :=
14311 Create_Itype
14312 (Ekind => E_Record_Subtype,
14313 Related_Nod => Related_Nod,
14314 Related_Id => Corr_Rec,
14315 Suffix => 'C',
14316 Suffix_Index => -1);
14318 begin
14319 Set_Etype (T_Sub, Corr_Rec);
14320 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14321 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14322 Set_Is_Constrained (T_Sub, True);
14323 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14324 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14326 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14327 Set_Discriminant_Constraint
14328 (T_Sub, Discriminant_Constraint (Prot_Subt));
14329 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14330 Create_Constrained_Components
14331 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14332 end if;
14334 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14336 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14337 Conditional_Delay (T_Sub, Corr_Rec);
14339 else
14340 -- This is a component subtype: it will be frozen in the context of
14341 -- the enclosing record's init_proc, so that discriminant references
14342 -- are resolved to discriminals. (Note: we used to skip freezing
14343 -- altogether in that case, which caused errors downstream for
14344 -- components of a bit packed array type).
14346 Set_Has_Delayed_Freeze (T_Sub);
14347 end if;
14349 return T_Sub;
14350 end Constrain_Corresponding_Record;
14352 -----------------------
14353 -- Constrain_Decimal --
14354 -----------------------
14356 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14357 T : constant Entity_Id := Entity (Subtype_Mark (S));
14358 C : constant Node_Id := Constraint (S);
14359 Loc : constant Source_Ptr := Sloc (C);
14360 Range_Expr : Node_Id;
14361 Digits_Expr : Node_Id;
14362 Digits_Val : Uint;
14363 Bound_Val : Ureal;
14365 begin
14366 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14368 if Nkind (C) = N_Range_Constraint then
14369 Range_Expr := Range_Expression (C);
14370 Digits_Val := Digits_Value (T);
14372 else
14373 pragma Assert (Nkind (C) = N_Digits_Constraint);
14375 Digits_Expr := Digits_Expression (C);
14376 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14378 Check_Digits_Expression (Digits_Expr);
14379 Digits_Val := Expr_Value (Digits_Expr);
14381 if Digits_Val > Digits_Value (T) then
14382 Error_Msg_N
14383 ("digits expression is incompatible with subtype", C);
14384 Digits_Val := Digits_Value (T);
14385 end if;
14387 if Present (Range_Constraint (C)) then
14388 Range_Expr := Range_Expression (Range_Constraint (C));
14389 else
14390 Range_Expr := Empty;
14391 end if;
14392 end if;
14394 Set_Etype (Def_Id, Base_Type (T));
14395 Set_Size_Info (Def_Id, (T));
14396 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14397 Set_Delta_Value (Def_Id, Delta_Value (T));
14398 Set_Scale_Value (Def_Id, Scale_Value (T));
14399 Set_Small_Value (Def_Id, Small_Value (T));
14400 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14401 Set_Digits_Value (Def_Id, Digits_Val);
14403 -- Manufacture range from given digits value if no range present
14405 if No (Range_Expr) then
14406 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14407 Range_Expr :=
14408 Make_Range (Loc,
14409 Low_Bound =>
14410 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14411 High_Bound =>
14412 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14413 end if;
14415 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14416 Set_Discrete_RM_Size (Def_Id);
14418 -- Unconditionally delay the freeze, since we cannot set size
14419 -- information in all cases correctly until the freeze point.
14421 Set_Has_Delayed_Freeze (Def_Id);
14422 end Constrain_Decimal;
14424 ----------------------------------
14425 -- Constrain_Discriminated_Type --
14426 ----------------------------------
14428 procedure Constrain_Discriminated_Type
14429 (Def_Id : Entity_Id;
14430 S : Node_Id;
14431 Related_Nod : Node_Id;
14432 For_Access : Boolean := False)
14434 E : Entity_Id := Entity (Subtype_Mark (S));
14435 T : Entity_Id;
14437 procedure Fixup_Bad_Constraint;
14438 -- Called after finding a bad constraint, and after having posted an
14439 -- appropriate error message. The goal is to leave type Def_Id in as
14440 -- reasonable state as possible.
14442 --------------------------
14443 -- Fixup_Bad_Constraint --
14444 --------------------------
14446 procedure Fixup_Bad_Constraint is
14447 begin
14448 -- Set a reasonable Ekind for the entity, including incomplete types.
14450 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14452 -- Set Etype to the known type, to reduce chances of cascaded errors
14454 Set_Etype (Def_Id, E);
14455 Set_Error_Posted (Def_Id);
14456 end Fixup_Bad_Constraint;
14458 -- Local variables
14460 C : Node_Id;
14461 Constr : Elist_Id := New_Elmt_List;
14463 -- Start of processing for Constrain_Discriminated_Type
14465 begin
14466 C := Constraint (S);
14468 -- A discriminant constraint is only allowed in a subtype indication,
14469 -- after a subtype mark. This subtype mark must denote either a type
14470 -- with discriminants, or an access type whose designated type is a
14471 -- type with discriminants. A discriminant constraint specifies the
14472 -- values of these discriminants (RM 3.7.2(5)).
14474 T := Base_Type (Entity (Subtype_Mark (S)));
14476 if Is_Access_Type (T) then
14477 T := Designated_Type (T);
14478 end if;
14480 -- In an instance it may be necessary to retrieve the full view of a
14481 -- type with unknown discriminants, or a full view with defaulted
14482 -- discriminants. In other contexts the constraint is illegal.
14484 if In_Instance
14485 and then Is_Private_Type (T)
14486 and then Present (Full_View (T))
14487 and then
14488 (Has_Unknown_Discriminants (T)
14489 or else
14490 (not Has_Discriminants (T)
14491 and then Has_Defaulted_Discriminants (Full_View (T))))
14492 then
14493 T := Full_View (T);
14494 E := Full_View (E);
14495 end if;
14497 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14498 -- generating an error for access-to-incomplete subtypes.
14500 if Ada_Version >= Ada_2005
14501 and then Ekind (T) = E_Incomplete_Type
14502 and then Nkind (Parent (S)) = N_Subtype_Declaration
14503 and then not Is_Itype (Def_Id)
14504 then
14505 -- A little sanity check: emit an error message if the type has
14506 -- discriminants to begin with. Type T may be a regular incomplete
14507 -- type or imported via a limited with clause.
14509 if Has_Discriminants (T)
14510 or else (From_Limited_With (T)
14511 and then Present (Non_Limited_View (T))
14512 and then Nkind (Parent (Non_Limited_View (T))) =
14513 N_Full_Type_Declaration
14514 and then Present (Discriminant_Specifications
14515 (Parent (Non_Limited_View (T)))))
14516 then
14517 Error_Msg_N
14518 ("(Ada 2005) incomplete subtype may not be constrained", C);
14519 else
14520 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14521 end if;
14523 Fixup_Bad_Constraint;
14524 return;
14526 -- Check that the type has visible discriminants. The type may be
14527 -- a private type with unknown discriminants whose full view has
14528 -- discriminants which are invisible.
14530 elsif not Has_Discriminants (T)
14531 or else
14532 (Has_Unknown_Discriminants (T)
14533 and then Is_Private_Type (T))
14534 then
14535 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14536 Fixup_Bad_Constraint;
14537 return;
14539 elsif Is_Constrained (E)
14540 or else (Ekind (E) = E_Class_Wide_Subtype
14541 and then Present (Discriminant_Constraint (E)))
14542 then
14543 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14544 Fixup_Bad_Constraint;
14545 return;
14546 end if;
14548 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14549 -- applies to the base type.
14551 T := Base_Type (T);
14553 Constr := Build_Discriminant_Constraints (T, S);
14555 -- If the list returned was empty we had an error in building the
14556 -- discriminant constraint. We have also already signalled an error
14557 -- in the incomplete type case
14559 if Is_Empty_Elmt_List (Constr) then
14560 Fixup_Bad_Constraint;
14561 return;
14562 end if;
14564 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14565 end Constrain_Discriminated_Type;
14567 ---------------------------
14568 -- Constrain_Enumeration --
14569 ---------------------------
14571 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14572 T : constant Entity_Id := Entity (Subtype_Mark (S));
14573 C : constant Node_Id := Constraint (S);
14575 begin
14576 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14578 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14579 Set_Etype (Def_Id, Base_Type (T));
14580 Set_Size_Info (Def_Id, (T));
14581 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14582 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14584 -- Inherit the chain of representation items instead of replacing it
14585 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14586 -- the derived type as a subtype declaration and the former needs to
14587 -- preserve existing representation items (see Build_Derived_Type).
14589 Inherit_Rep_Item_Chain (Def_Id, T);
14591 Set_Discrete_RM_Size (Def_Id);
14592 end Constrain_Enumeration;
14594 ----------------------
14595 -- Constrain_Float --
14596 ----------------------
14598 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14599 T : constant Entity_Id := Entity (Subtype_Mark (S));
14600 C : Node_Id;
14601 D : Node_Id;
14602 Rais : Node_Id;
14604 begin
14605 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14607 Set_Etype (Def_Id, Base_Type (T));
14608 Set_Size_Info (Def_Id, (T));
14609 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14611 -- Process the constraint
14613 C := Constraint (S);
14615 -- Digits constraint present
14617 if Nkind (C) = N_Digits_Constraint then
14618 Check_Restriction (No_Obsolescent_Features, C);
14620 if Warn_On_Obsolescent_Feature then
14621 Error_Msg_N
14622 ("subtype digits constraint is an " &
14623 "obsolescent feature (RM J.3(8))?j?", C);
14624 end if;
14626 D := Digits_Expression (C);
14627 Analyze_And_Resolve (D, Any_Integer);
14628 Check_Digits_Expression (D);
14629 Set_Digits_Value (Def_Id, Expr_Value (D));
14631 -- Check that digits value is in range. Obviously we can do this
14632 -- at compile time, but it is strictly a runtime check, and of
14633 -- course there is an ACVC test that checks this.
14635 if Digits_Value (Def_Id) > Digits_Value (T) then
14636 Error_Msg_Uint_1 := Digits_Value (T);
14637 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14638 Rais :=
14639 Make_Raise_Constraint_Error (Sloc (D),
14640 Reason => CE_Range_Check_Failed);
14641 Insert_Action (Declaration_Node (Def_Id), Rais);
14642 end if;
14644 C := Range_Constraint (C);
14646 -- No digits constraint present
14648 else
14649 Set_Digits_Value (Def_Id, Digits_Value (T));
14650 end if;
14652 -- Range constraint present
14654 if Nkind (C) = N_Range_Constraint then
14655 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14657 -- No range constraint present
14659 else
14660 pragma Assert (No (C));
14661 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14662 end if;
14664 Set_Is_Constrained (Def_Id);
14665 end Constrain_Float;
14667 ---------------------
14668 -- Constrain_Index --
14669 ---------------------
14671 procedure Constrain_Index
14672 (Index : Node_Id;
14673 S : Node_Id;
14674 Related_Nod : Node_Id;
14675 Related_Id : Entity_Id;
14676 Suffix : Character;
14677 Suffix_Index : Pos)
14679 Def_Id : Entity_Id;
14680 R : Node_Id := Empty;
14681 T : constant Entity_Id := Etype (Index);
14682 Is_FLB_Index : Boolean := False;
14684 begin
14685 Def_Id :=
14686 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14687 Set_Etype (Def_Id, Base_Type (T));
14689 if Nkind (S) = N_Range
14690 or else
14691 (Nkind (S) = N_Attribute_Reference
14692 and then Attribute_Name (S) = Name_Range)
14693 then
14694 -- A Range attribute will be transformed into N_Range by Resolve
14696 -- If a range has an Empty upper bound, then remember that for later
14697 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14698 -- flag, and also set the upper bound of the range to the index
14699 -- subtype's upper bound rather than leaving it Empty. In truth,
14700 -- that upper bound corresponds to a box ("<>"), but it's convenient
14701 -- to set it to the upper bound to avoid needing to add special tests
14702 -- in various places for an Empty upper bound, and in any case it
14703 -- accurately characterizes the index's range of values.
14705 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14706 Is_FLB_Index := True;
14707 Set_High_Bound (S, Type_High_Bound (T));
14708 end if;
14710 R := S;
14712 Process_Range_Expr_In_Decl (R, T);
14714 if not Error_Posted (S)
14715 and then
14716 (Nkind (S) /= N_Range
14717 or else not Covers (T, (Etype (Low_Bound (S))))
14718 or else not Covers (T, (Etype (High_Bound (S)))))
14719 then
14720 if Base_Type (T) /= Any_Type
14721 and then Etype (Low_Bound (S)) /= Any_Type
14722 and then Etype (High_Bound (S)) /= Any_Type
14723 then
14724 Error_Msg_N ("range expected", S);
14725 end if;
14726 end if;
14728 elsif Nkind (S) = N_Subtype_Indication then
14730 -- The parser has verified that this is a discrete indication
14732 Resolve_Discrete_Subtype_Indication (S, T);
14733 Bad_Predicated_Subtype_Use
14734 ("subtype& has predicate, not allowed in index constraint",
14735 S, Entity (Subtype_Mark (S)));
14737 R := Range_Expression (Constraint (S));
14739 -- Capture values of bounds and generate temporaries for them if
14740 -- needed, since checks may cause duplication of the expressions
14741 -- which must not be reevaluated.
14743 -- The forced evaluation removes side effects from expressions, which
14744 -- should occur also in GNATprove mode. Otherwise, we end up with
14745 -- unexpected insertions of actions at places where this is not
14746 -- supposed to occur, e.g. on default parameters of a call.
14748 if Expander_Active or GNATprove_Mode then
14749 Force_Evaluation
14750 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14751 Force_Evaluation
14752 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14753 end if;
14755 elsif Nkind (S) = N_Discriminant_Association then
14757 -- Syntactically valid in subtype indication
14759 Error_Msg_N ("invalid index constraint", S);
14760 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14761 return;
14763 -- Subtype_Mark case, no anonymous subtypes to construct
14765 else
14766 Analyze (S);
14768 if Is_Entity_Name (S) then
14769 if not Is_Type (Entity (S)) then
14770 Error_Msg_N ("expect subtype mark for index constraint", S);
14772 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14773 Wrong_Type (S, Base_Type (T));
14775 -- Check error of subtype with predicate in index constraint
14777 else
14778 Bad_Predicated_Subtype_Use
14779 ("subtype& has predicate, not allowed in index constraint",
14780 S, Entity (S));
14781 end if;
14783 return;
14785 else
14786 Error_Msg_N ("invalid index constraint", S);
14787 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14788 return;
14789 end if;
14790 end if;
14792 -- Complete construction of the Itype
14794 if Is_Modular_Integer_Type (T) then
14795 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14797 elsif Is_Integer_Type (T) then
14798 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14800 else
14801 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14802 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14803 Set_First_Literal (Def_Id, First_Literal (T));
14804 end if;
14806 Set_Size_Info (Def_Id, (T));
14807 Copy_RM_Size (To => Def_Id, From => T);
14808 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14810 -- If this is a range for a fixed-lower-bound subtype, then set the
14811 -- index itype's low bound to the FLB and the index itype's upper bound
14812 -- to the high bound of the parent array type's index subtype. Also,
14813 -- mark the itype as an FLB index subtype.
14815 if Nkind (S) = N_Range and then Is_FLB_Index then
14816 Set_Scalar_Range
14817 (Def_Id,
14818 Make_Range (Sloc (S),
14819 Low_Bound => Low_Bound (S),
14820 High_Bound => Type_High_Bound (T)));
14821 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14823 else
14824 Set_Scalar_Range (Def_Id, R);
14825 end if;
14827 Set_Etype (S, Def_Id);
14828 Set_Discrete_RM_Size (Def_Id);
14829 end Constrain_Index;
14831 -----------------------
14832 -- Constrain_Integer --
14833 -----------------------
14835 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14836 T : constant Entity_Id := Entity (Subtype_Mark (S));
14837 C : constant Node_Id := Constraint (S);
14839 begin
14840 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14842 if Is_Modular_Integer_Type (T) then
14843 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14844 else
14845 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14846 end if;
14848 Set_Etype (Def_Id, Base_Type (T));
14849 Set_Size_Info (Def_Id, (T));
14850 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14851 Set_Discrete_RM_Size (Def_Id);
14852 end Constrain_Integer;
14854 ------------------------------
14855 -- Constrain_Ordinary_Fixed --
14856 ------------------------------
14858 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14859 T : constant Entity_Id := Entity (Subtype_Mark (S));
14860 C : Node_Id;
14861 D : Node_Id;
14862 Rais : Node_Id;
14864 begin
14865 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14866 Set_Etype (Def_Id, Base_Type (T));
14867 Set_Size_Info (Def_Id, (T));
14868 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14869 Set_Small_Value (Def_Id, Small_Value (T));
14871 -- Process the constraint
14873 C := Constraint (S);
14875 -- Delta constraint present
14877 if Nkind (C) = N_Delta_Constraint then
14878 Check_Restriction (No_Obsolescent_Features, C);
14880 if Warn_On_Obsolescent_Feature then
14881 Error_Msg_S
14882 ("subtype delta constraint is an " &
14883 "obsolescent feature (RM J.3(7))?j?");
14884 end if;
14886 D := Delta_Expression (C);
14887 Analyze_And_Resolve (D, Any_Real);
14888 Check_Delta_Expression (D);
14889 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14891 -- Check that delta value is in range. Obviously we can do this
14892 -- at compile time, but it is strictly a runtime check, and of
14893 -- course there is an ACVC test that checks this.
14895 if Delta_Value (Def_Id) < Delta_Value (T) then
14896 Error_Msg_N ("??delta value is too small", D);
14897 Rais :=
14898 Make_Raise_Constraint_Error (Sloc (D),
14899 Reason => CE_Range_Check_Failed);
14900 Insert_Action (Declaration_Node (Def_Id), Rais);
14901 end if;
14903 C := Range_Constraint (C);
14905 -- No delta constraint present
14907 else
14908 Set_Delta_Value (Def_Id, Delta_Value (T));
14909 end if;
14911 -- Range constraint present
14913 if Nkind (C) = N_Range_Constraint then
14914 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14916 -- No range constraint present
14918 else
14919 pragma Assert (No (C));
14920 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14921 end if;
14923 Set_Discrete_RM_Size (Def_Id);
14925 -- Unconditionally delay the freeze, since we cannot set size
14926 -- information in all cases correctly until the freeze point.
14928 Set_Has_Delayed_Freeze (Def_Id);
14929 end Constrain_Ordinary_Fixed;
14931 -----------------------
14932 -- Contain_Interface --
14933 -----------------------
14935 function Contain_Interface
14936 (Iface : Entity_Id;
14937 Ifaces : Elist_Id) return Boolean
14939 Iface_Elmt : Elmt_Id;
14941 begin
14942 if Present (Ifaces) then
14943 Iface_Elmt := First_Elmt (Ifaces);
14944 while Present (Iface_Elmt) loop
14945 if Node (Iface_Elmt) = Iface then
14946 return True;
14947 end if;
14949 Next_Elmt (Iface_Elmt);
14950 end loop;
14951 end if;
14953 return False;
14954 end Contain_Interface;
14956 ---------------------------
14957 -- Convert_Scalar_Bounds --
14958 ---------------------------
14960 procedure Convert_Scalar_Bounds
14961 (N : Node_Id;
14962 Parent_Type : Entity_Id;
14963 Derived_Type : Entity_Id;
14964 Loc : Source_Ptr)
14966 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14968 Lo : Node_Id;
14969 Hi : Node_Id;
14970 Rng : Node_Id;
14972 begin
14973 -- Defend against previous errors
14975 if No (Scalar_Range (Derived_Type)) then
14976 Check_Error_Detected;
14977 return;
14978 end if;
14980 Lo := Build_Scalar_Bound
14981 (Type_Low_Bound (Derived_Type),
14982 Parent_Type, Implicit_Base);
14984 Hi := Build_Scalar_Bound
14985 (Type_High_Bound (Derived_Type),
14986 Parent_Type, Implicit_Base);
14988 Rng :=
14989 Make_Range (Loc,
14990 Low_Bound => Lo,
14991 High_Bound => Hi);
14993 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14995 Set_Parent (Rng, N);
14996 Set_Scalar_Range (Derived_Type, Rng);
14998 -- Analyze the bounds
15000 Analyze_And_Resolve (Lo, Implicit_Base);
15001 Analyze_And_Resolve (Hi, Implicit_Base);
15003 -- Analyze the range itself, except that we do not analyze it if
15004 -- the bounds are real literals, and we have a fixed-point type.
15005 -- The reason for this is that we delay setting the bounds in this
15006 -- case till we know the final Small and Size values (see circuit
15007 -- in Freeze.Freeze_Fixed_Point_Type for further details).
15009 if Is_Fixed_Point_Type (Parent_Type)
15010 and then Nkind (Lo) = N_Real_Literal
15011 and then Nkind (Hi) = N_Real_Literal
15012 then
15013 return;
15015 -- Here we do the analysis of the range
15017 -- Note: we do this manually, since if we do a normal Analyze and
15018 -- Resolve call, there are problems with the conversions used for
15019 -- the derived type range.
15021 else
15022 Set_Etype (Rng, Implicit_Base);
15023 Set_Analyzed (Rng, True);
15024 end if;
15025 end Convert_Scalar_Bounds;
15027 -------------------
15028 -- Copy_And_Swap --
15029 -------------------
15031 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15032 begin
15033 -- Initialize new full declaration entity by copying the pertinent
15034 -- fields of the corresponding private declaration entity.
15036 -- We temporarily set Ekind to a value appropriate for a type to
15037 -- avoid assert failures in Einfo from checking for setting type
15038 -- attributes on something that is not a type. Ekind (Priv) is an
15039 -- appropriate choice, since it allowed the attributes to be set
15040 -- in the first place. This Ekind value will be modified later.
15042 Mutate_Ekind (Full, Ekind (Priv));
15044 -- Also set Etype temporarily to Any_Type, again, in the absence
15045 -- of errors, it will be properly reset, and if there are errors,
15046 -- then we want a value of Any_Type to remain.
15048 Set_Etype (Full, Any_Type);
15050 -- Now start copying attributes
15052 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15054 if Has_Discriminants (Full) then
15055 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15056 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15057 end if;
15059 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15060 Set_Homonym (Full, Homonym (Priv));
15061 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15062 Set_Is_Public (Full, Is_Public (Priv));
15063 Set_Is_Pure (Full, Is_Pure (Priv));
15064 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15065 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15066 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15067 Set_Has_Pragma_Unreferenced_Objects
15068 (Full, Has_Pragma_Unreferenced_Objects
15069 (Priv));
15071 Conditional_Delay (Full, Priv);
15073 if Is_Tagged_Type (Full) then
15074 Set_Direct_Primitive_Operations
15075 (Full, Direct_Primitive_Operations (Priv));
15076 Set_No_Tagged_Streams_Pragma
15077 (Full, No_Tagged_Streams_Pragma (Priv));
15079 if Is_Base_Type (Priv) then
15080 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15081 end if;
15082 end if;
15084 Set_Is_Volatile (Full, Is_Volatile (Priv));
15085 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15086 Set_Scope (Full, Scope (Priv));
15087 Set_Prev_Entity (Full, Prev_Entity (Priv));
15088 Set_Next_Entity (Full, Next_Entity (Priv));
15089 Set_First_Entity (Full, First_Entity (Priv));
15090 Set_Last_Entity (Full, Last_Entity (Priv));
15092 -- If access types have been recorded for later handling, keep them in
15093 -- the full view so that they get handled when the full view freeze
15094 -- node is expanded.
15096 if Present (Freeze_Node (Priv))
15097 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15098 then
15099 Ensure_Freeze_Node (Full);
15100 Set_Access_Types_To_Process
15101 (Freeze_Node (Full),
15102 Access_Types_To_Process (Freeze_Node (Priv)));
15103 end if;
15105 -- Swap the two entities. Now Private is the full type entity and Full
15106 -- is the private one. They will be swapped back at the end of the
15107 -- private part. This swapping ensures that the entity that is visible
15108 -- in the private part is the full declaration.
15110 Exchange_Entities (Priv, Full);
15111 Append_Entity (Full, Scope (Full));
15112 end Copy_And_Swap;
15114 -------------------------------------
15115 -- Copy_Array_Base_Type_Attributes --
15116 -------------------------------------
15118 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15119 begin
15120 Set_Component_Alignment (T1, Component_Alignment (T2));
15121 Set_Component_Type (T1, Component_Type (T2));
15122 Set_Component_Size (T1, Component_Size (T2));
15123 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
15124 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15125 Propagate_Concurrent_Flags (T1, T2);
15126 Set_Is_Packed (T1, Is_Packed (T2));
15127 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15128 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15129 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15130 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15131 end Copy_Array_Base_Type_Attributes;
15133 -----------------------------------
15134 -- Copy_Array_Subtype_Attributes --
15135 -----------------------------------
15137 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15138 -- let it be recreated during freezing for the sake of better debug info.
15140 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15141 begin
15142 Set_Size_Info (T1, T2);
15144 Set_First_Index (T1, First_Index (T2));
15145 Set_Is_Aliased (T1, Is_Aliased (T2));
15146 Set_Is_Atomic (T1, Is_Atomic (T2));
15147 Set_Is_Independent (T1, Is_Independent (T2));
15148 Set_Is_Volatile (T1, Is_Volatile (T2));
15149 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15150 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15151 Set_Is_Constrained (T1, Is_Constrained (T2));
15152 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15153 Inherit_Rep_Item_Chain (T1, T2);
15154 Set_Convention (T1, Convention (T2));
15155 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15156 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15157 end Copy_Array_Subtype_Attributes;
15159 -----------------------------------
15160 -- Create_Constrained_Components --
15161 -----------------------------------
15163 procedure Create_Constrained_Components
15164 (Subt : Entity_Id;
15165 Decl_Node : Node_Id;
15166 Typ : Entity_Id;
15167 Constraints : Elist_Id)
15169 Loc : constant Source_Ptr := Sloc (Subt);
15170 Comp_List : constant Elist_Id := New_Elmt_List;
15171 Parent_Type : constant Entity_Id := Etype (Typ);
15172 Assoc_List : constant List_Id := New_List;
15174 Discr_Val : Elmt_Id;
15175 Errors : Boolean;
15176 New_C : Entity_Id;
15177 Old_C : Entity_Id;
15178 Is_Static : Boolean := True;
15179 Is_Compile_Time_Known : Boolean := True;
15181 procedure Collect_Fixed_Components (Typ : Entity_Id);
15182 -- Collect parent type components that do not appear in a variant part
15184 procedure Create_All_Components;
15185 -- Iterate over Comp_List to create the components of the subtype
15187 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15188 -- Creates a new component from Old_Compon, copying all the fields from
15189 -- it, including its Etype, inserts the new component in the Subt entity
15190 -- chain and returns the new component.
15192 function Is_Variant_Record (T : Entity_Id) return Boolean;
15193 -- If true, and discriminants are static, collect only components from
15194 -- variants selected by discriminant values.
15196 ------------------------------
15197 -- Collect_Fixed_Components --
15198 ------------------------------
15200 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15201 begin
15202 -- Build association list for discriminants, and find components of the
15203 -- variant part selected by the values of the discriminants.
15205 Old_C := First_Discriminant (Typ);
15206 Discr_Val := First_Elmt (Constraints);
15207 while Present (Old_C) loop
15208 Append_To (Assoc_List,
15209 Make_Component_Association (Loc,
15210 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15211 Expression => New_Copy (Node (Discr_Val))));
15213 Next_Elmt (Discr_Val);
15214 Next_Discriminant (Old_C);
15215 end loop;
15217 -- The tag and the possible parent component are unconditionally in
15218 -- the subtype.
15220 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15221 Old_C := First_Component (Typ);
15222 while Present (Old_C) loop
15223 if Chars (Old_C) in Name_uTag | Name_uParent then
15224 Append_Elmt (Old_C, Comp_List);
15225 end if;
15227 Next_Component (Old_C);
15228 end loop;
15229 end if;
15230 end Collect_Fixed_Components;
15232 ---------------------------
15233 -- Create_All_Components --
15234 ---------------------------
15236 procedure Create_All_Components is
15237 Comp : Elmt_Id;
15239 begin
15240 Comp := First_Elmt (Comp_List);
15241 while Present (Comp) loop
15242 Old_C := Node (Comp);
15243 New_C := Create_Component (Old_C);
15245 Set_Etype
15246 (New_C,
15247 Constrain_Component_Type
15248 (Old_C, Subt, Decl_Node, Typ, Constraints));
15249 Set_Is_Public (New_C, Is_Public (Subt));
15251 Next_Elmt (Comp);
15252 end loop;
15253 end Create_All_Components;
15255 ----------------------
15256 -- Create_Component --
15257 ----------------------
15259 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15260 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15262 begin
15263 if Ekind (Old_Compon) = E_Discriminant
15264 and then Is_Completely_Hidden (Old_Compon)
15265 then
15266 -- This is a shadow discriminant created for a discriminant of
15267 -- the parent type, which needs to be present in the subtype.
15268 -- Give the shadow discriminant an internal name that cannot
15269 -- conflict with that of visible components.
15271 Set_Chars (New_Compon, New_Internal_Name ('C'));
15272 end if;
15274 -- Set the parent so we have a proper link for freezing etc. This is
15275 -- not a real parent pointer, since of course our parent does not own
15276 -- up to us and reference us, we are an illegitimate child of the
15277 -- original parent.
15279 Set_Parent (New_Compon, Parent (Old_Compon));
15281 -- We do not want this node marked as Comes_From_Source, since
15282 -- otherwise it would get first class status and a separate cross-
15283 -- reference line would be generated. Illegitimate children do not
15284 -- rate such recognition.
15286 Set_Comes_From_Source (New_Compon, False);
15288 -- But it is a real entity, and a birth certificate must be properly
15289 -- registered by entering it into the entity list, and setting its
15290 -- scope to the given subtype. This turns out to be useful for the
15291 -- LLVM code generator, but that scope is not used otherwise.
15293 Enter_Name (New_Compon);
15294 Set_Scope (New_Compon, Subt);
15296 return New_Compon;
15297 end Create_Component;
15299 -----------------------
15300 -- Is_Variant_Record --
15301 -----------------------
15303 function Is_Variant_Record (T : Entity_Id) return Boolean is
15304 begin
15305 return Nkind (Parent (T)) = N_Full_Type_Declaration
15306 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
15307 and then Present (Component_List (Type_Definition (Parent (T))))
15308 and then
15309 Present
15310 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
15311 end Is_Variant_Record;
15313 -- Start of processing for Create_Constrained_Components
15315 begin
15316 pragma Assert (Subt /= Base_Type (Subt));
15317 pragma Assert (Typ = Base_Type (Typ));
15319 Set_First_Entity (Subt, Empty);
15320 Set_Last_Entity (Subt, Empty);
15322 -- Check whether constraint is fully static, in which case we can
15323 -- optimize the list of components.
15325 Discr_Val := First_Elmt (Constraints);
15326 while Present (Discr_Val) loop
15327 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15328 Is_Static := False;
15330 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15331 Is_Compile_Time_Known := False;
15332 exit;
15333 end if;
15334 end if;
15336 Next_Elmt (Discr_Val);
15337 end loop;
15339 Set_Has_Static_Discriminants (Subt, Is_Static);
15341 Push_Scope (Subt);
15343 -- Inherit the discriminants of the parent type
15345 Add_Discriminants : declare
15346 Num_Disc : Nat;
15347 Num_Stor : Nat;
15349 begin
15350 Num_Disc := 0;
15351 Old_C := First_Discriminant (Typ);
15353 while Present (Old_C) loop
15354 Num_Disc := Num_Disc + 1;
15355 New_C := Create_Component (Old_C);
15356 Set_Is_Public (New_C, Is_Public (Subt));
15357 Next_Discriminant (Old_C);
15358 end loop;
15360 -- For an untagged derived subtype, the number of discriminants may
15361 -- be smaller than the number of inherited discriminants, because
15362 -- several of them may be renamed by a single new discriminant or
15363 -- constrained. In this case, add the hidden discriminants back into
15364 -- the subtype, because they need to be present if the optimizer of
15365 -- the GCC 4.x back-end decides to break apart assignments between
15366 -- objects using the parent view into member-wise assignments.
15368 Num_Stor := 0;
15370 if Is_Derived_Type (Typ)
15371 and then not Is_Tagged_Type (Typ)
15372 then
15373 Old_C := First_Stored_Discriminant (Typ);
15375 while Present (Old_C) loop
15376 Num_Stor := Num_Stor + 1;
15377 Next_Stored_Discriminant (Old_C);
15378 end loop;
15379 end if;
15381 if Num_Stor > Num_Disc then
15383 -- Find out multiple uses of new discriminants, and add hidden
15384 -- components for the extra renamed discriminants. We recognize
15385 -- multiple uses through the Corresponding_Discriminant of a
15386 -- new discriminant: if it constrains several old discriminants,
15387 -- this field points to the last one in the parent type. The
15388 -- stored discriminants of the derived type have the same name
15389 -- as those of the parent.
15391 declare
15392 Constr : Elmt_Id;
15393 New_Discr : Entity_Id;
15394 Old_Discr : Entity_Id;
15396 begin
15397 Constr := First_Elmt (Stored_Constraint (Typ));
15398 Old_Discr := First_Stored_Discriminant (Typ);
15399 while Present (Constr) loop
15400 if Is_Entity_Name (Node (Constr))
15401 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15402 then
15403 New_Discr := Entity (Node (Constr));
15405 if Chars (Corresponding_Discriminant (New_Discr)) /=
15406 Chars (Old_Discr)
15407 then
15408 -- The new discriminant has been used to rename a
15409 -- subsequent old discriminant. Introduce a shadow
15410 -- component for the current old discriminant.
15412 New_C := Create_Component (Old_Discr);
15413 Set_Original_Record_Component (New_C, Old_Discr);
15414 end if;
15416 else
15417 -- The constraint has eliminated the old discriminant.
15418 -- Introduce a shadow component.
15420 New_C := Create_Component (Old_Discr);
15421 Set_Original_Record_Component (New_C, Old_Discr);
15422 end if;
15424 Next_Elmt (Constr);
15425 Next_Stored_Discriminant (Old_Discr);
15426 end loop;
15427 end;
15428 end if;
15429 end Add_Discriminants;
15431 if Is_Compile_Time_Known
15432 and then Is_Variant_Record (Typ)
15433 then
15434 Collect_Fixed_Components (Typ);
15435 Gather_Components
15436 (Typ,
15437 Component_List (Type_Definition (Parent (Typ))),
15438 Governed_By => Assoc_List,
15439 Into => Comp_List,
15440 Report_Errors => Errors,
15441 Allow_Compile_Time => True);
15442 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15444 Create_All_Components;
15446 -- If the subtype declaration is created for a tagged type derivation
15447 -- with constraints, we retrieve the record definition of the parent
15448 -- type to select the components of the proper variant.
15450 elsif Is_Compile_Time_Known
15451 and then Is_Tagged_Type (Typ)
15452 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15453 and then
15454 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15455 and then Is_Variant_Record (Parent_Type)
15456 then
15457 Collect_Fixed_Components (Typ);
15458 Gather_Components
15459 (Typ,
15460 Component_List (Type_Definition (Parent (Parent_Type))),
15461 Governed_By => Assoc_List,
15462 Into => Comp_List,
15463 Report_Errors => Errors,
15464 Allow_Compile_Time => True);
15466 -- Note: previously there was a check at this point that no errors
15467 -- were detected. As a consequence of AI05-220 there may be an error
15468 -- if an inherited discriminant that controls a variant has a non-
15469 -- static constraint.
15471 -- If the tagged derivation has a type extension, collect all the
15472 -- new relevant components therein via Gather_Components.
15474 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15475 then
15476 Gather_Components
15477 (Typ,
15478 Component_List
15479 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15480 Governed_By => Assoc_List,
15481 Into => Comp_List,
15482 Report_Errors => Errors,
15483 Allow_Compile_Time => True,
15484 Include_Interface_Tag => True);
15485 end if;
15487 Create_All_Components;
15489 else
15490 -- If discriminants are not static, or if this is a multi-level type
15491 -- extension, we have to include all components of the parent type.
15493 Old_C := First_Component (Typ);
15494 while Present (Old_C) loop
15495 New_C := Create_Component (Old_C);
15497 Set_Etype
15498 (New_C,
15499 Constrain_Component_Type
15500 (Old_C, Subt, Decl_Node, Typ, Constraints));
15501 Set_Is_Public (New_C, Is_Public (Subt));
15503 Next_Component (Old_C);
15504 end loop;
15505 end if;
15507 End_Scope;
15508 end Create_Constrained_Components;
15510 ------------------------------------------
15511 -- Decimal_Fixed_Point_Type_Declaration --
15512 ------------------------------------------
15514 procedure Decimal_Fixed_Point_Type_Declaration
15515 (T : Entity_Id;
15516 Def : Node_Id)
15518 Loc : constant Source_Ptr := Sloc (Def);
15519 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15520 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15521 Max_Digits : constant Nat :=
15522 (if System_Max_Integer_Size = 128 then 38 else 18);
15523 -- Maximum number of digits that can be represented in an integer
15525 Implicit_Base : Entity_Id;
15526 Digs_Val : Uint;
15527 Delta_Val : Ureal;
15528 Scale_Val : Uint;
15529 Bound_Val : Ureal;
15531 begin
15532 Check_Restriction (No_Fixed_Point, Def);
15534 -- Create implicit base type
15536 Implicit_Base :=
15537 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15538 Set_Etype (Implicit_Base, Implicit_Base);
15540 -- Analyze and process delta expression
15542 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15544 Check_Delta_Expression (Delta_Expr);
15545 Delta_Val := Expr_Value_R (Delta_Expr);
15547 -- Check delta is power of 10, and determine scale value from it
15549 declare
15550 Val : Ureal;
15552 begin
15553 Scale_Val := Uint_0;
15554 Val := Delta_Val;
15556 if Val < Ureal_1 then
15557 while Val < Ureal_1 loop
15558 Val := Val * Ureal_10;
15559 Scale_Val := Scale_Val + 1;
15560 end loop;
15562 if Scale_Val > Max_Digits then
15563 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15564 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15565 Scale_Val := UI_From_Int (Max_Digits);
15566 end if;
15568 else
15569 while Val > Ureal_1 loop
15570 Val := Val / Ureal_10;
15571 Scale_Val := Scale_Val - 1;
15572 end loop;
15574 if Scale_Val < -Max_Digits then
15575 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15576 Error_Msg_N ("scale is less than minimum value of ^", Def);
15577 Scale_Val := UI_From_Int (-Max_Digits);
15578 end if;
15579 end if;
15581 if Val /= Ureal_1 then
15582 Error_Msg_N ("delta expression must be a power of 10", Def);
15583 Delta_Val := Ureal_10 ** (-Scale_Val);
15584 end if;
15585 end;
15587 -- Set delta, scale and small (small = delta for decimal type)
15589 Set_Delta_Value (Implicit_Base, Delta_Val);
15590 Set_Scale_Value (Implicit_Base, Scale_Val);
15591 Set_Small_Value (Implicit_Base, Delta_Val);
15593 -- Analyze and process digits expression
15595 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15596 Check_Digits_Expression (Digs_Expr);
15597 Digs_Val := Expr_Value (Digs_Expr);
15599 if Digs_Val > Max_Digits then
15600 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15601 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15602 Digs_Val := UI_From_Int (Max_Digits);
15603 end if;
15605 Set_Digits_Value (Implicit_Base, Digs_Val);
15606 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15608 -- Set range of base type from digits value for now. This will be
15609 -- expanded to represent the true underlying base range by Freeze.
15611 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15613 -- Note: We leave Esize unset for now, size will be set at freeze
15614 -- time. We have to do this for ordinary fixed-point, because the size
15615 -- depends on the specified small, and we might as well do the same for
15616 -- decimal fixed-point.
15618 pragma Assert (not Known_Esize (Implicit_Base));
15620 -- If there are bounds given in the declaration use them as the
15621 -- bounds of the first named subtype.
15623 if Present (Real_Range_Specification (Def)) then
15624 declare
15625 RRS : constant Node_Id := Real_Range_Specification (Def);
15626 Low : constant Node_Id := Low_Bound (RRS);
15627 High : constant Node_Id := High_Bound (RRS);
15628 Low_Val : Ureal;
15629 High_Val : Ureal;
15631 begin
15632 Analyze_And_Resolve (Low, Any_Real);
15633 Analyze_And_Resolve (High, Any_Real);
15634 Check_Real_Bound (Low);
15635 Check_Real_Bound (High);
15636 Low_Val := Expr_Value_R (Low);
15637 High_Val := Expr_Value_R (High);
15639 if Low_Val < (-Bound_Val) then
15640 Error_Msg_N
15641 ("range low bound too small for digits value", Low);
15642 Low_Val := -Bound_Val;
15643 end if;
15645 if High_Val > Bound_Val then
15646 Error_Msg_N
15647 ("range high bound too large for digits value", High);
15648 High_Val := Bound_Val;
15649 end if;
15651 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15652 end;
15654 -- If no explicit range, use range that corresponds to given
15655 -- digits value. This will end up as the final range for the
15656 -- first subtype.
15658 else
15659 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15660 end if;
15662 -- Complete entity for first subtype. The inheritance of the rep item
15663 -- chain ensures that SPARK-related pragmas are not clobbered when the
15664 -- decimal fixed point type acts as a full view of a private type.
15666 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15667 Set_Etype (T, Implicit_Base);
15668 Set_Size_Info (T, Implicit_Base);
15669 Inherit_Rep_Item_Chain (T, Implicit_Base);
15670 Set_Digits_Value (T, Digs_Val);
15671 Set_Delta_Value (T, Delta_Val);
15672 Set_Small_Value (T, Delta_Val);
15673 Set_Scale_Value (T, Scale_Val);
15674 Set_Is_Constrained (T);
15675 end Decimal_Fixed_Point_Type_Declaration;
15677 -----------------------------------
15678 -- Derive_Progenitor_Subprograms --
15679 -----------------------------------
15681 procedure Derive_Progenitor_Subprograms
15682 (Parent_Type : Entity_Id;
15683 Tagged_Type : Entity_Id)
15685 E : Entity_Id;
15686 Elmt : Elmt_Id;
15687 Iface : Entity_Id;
15688 Iface_Alias : Entity_Id;
15689 Iface_Elmt : Elmt_Id;
15690 Iface_Subp : Entity_Id;
15691 New_Subp : Entity_Id := Empty;
15692 Prim_Elmt : Elmt_Id;
15693 Subp : Entity_Id;
15694 Typ : Entity_Id;
15696 begin
15697 pragma Assert (Ada_Version >= Ada_2005
15698 and then Is_Record_Type (Tagged_Type)
15699 and then Is_Tagged_Type (Tagged_Type)
15700 and then Has_Interfaces (Tagged_Type));
15702 -- Step 1: Transfer to the full-view primitives associated with the
15703 -- partial-view that cover interface primitives. Conceptually this
15704 -- work should be done later by Process_Full_View; done here to
15705 -- simplify its implementation at later stages. It can be safely
15706 -- done here because interfaces must be visible in the partial and
15707 -- private view (RM 7.3(7.3/2)).
15709 -- Small optimization: This work is only required if the parent may
15710 -- have entities whose Alias attribute reference an interface primitive.
15711 -- Such a situation may occur if the parent is an abstract type and the
15712 -- primitive has not been yet overridden or if the parent is a generic
15713 -- formal type covering interfaces.
15715 -- If the tagged type is not abstract, it cannot have abstract
15716 -- primitives (the only entities in the list of primitives of
15717 -- non-abstract tagged types that can reference abstract primitives
15718 -- through its Alias attribute are the internal entities that have
15719 -- attribute Interface_Alias, and these entities are generated later
15720 -- by Add_Internal_Interface_Entities).
15722 if In_Private_Part (Current_Scope)
15723 and then (Is_Abstract_Type (Parent_Type)
15724 or else
15725 Is_Generic_Type (Parent_Type))
15726 then
15727 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15728 while Present (Elmt) loop
15729 Subp := Node (Elmt);
15731 -- At this stage it is not possible to have entities in the list
15732 -- of primitives that have attribute Interface_Alias.
15734 pragma Assert (No (Interface_Alias (Subp)));
15736 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15738 if Is_Interface (Typ) then
15739 E := Find_Primitive_Covering_Interface
15740 (Tagged_Type => Tagged_Type,
15741 Iface_Prim => Subp);
15743 if Present (E)
15744 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15745 then
15746 Replace_Elmt (Elmt, E);
15747 Remove_Homonym (Subp);
15748 end if;
15749 end if;
15751 Next_Elmt (Elmt);
15752 end loop;
15753 end if;
15755 -- Step 2: Add primitives of progenitors that are not implemented by
15756 -- parents of Tagged_Type.
15758 if Present (Interfaces (Base_Type (Tagged_Type))) then
15759 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15760 while Present (Iface_Elmt) loop
15761 Iface := Node (Iface_Elmt);
15763 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15764 while Present (Prim_Elmt) loop
15765 Iface_Subp := Node (Prim_Elmt);
15766 Iface_Alias := Ultimate_Alias (Iface_Subp);
15768 -- Exclude derivation of predefined primitives except those
15769 -- that come from source, or are inherited from one that comes
15770 -- from source. Required to catch declarations of equality
15771 -- operators of interfaces. For example:
15773 -- type Iface is interface;
15774 -- function "=" (Left, Right : Iface) return Boolean;
15776 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15777 or else Comes_From_Source (Iface_Alias)
15778 then
15779 E :=
15780 Find_Primitive_Covering_Interface
15781 (Tagged_Type => Tagged_Type,
15782 Iface_Prim => Iface_Subp);
15784 -- If not found we derive a new primitive leaving its alias
15785 -- attribute referencing the interface primitive.
15787 if No (E) then
15788 Derive_Subprogram
15789 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15791 -- Ada 2012 (AI05-0197): If the covering primitive's name
15792 -- differs from the name of the interface primitive then it
15793 -- is a private primitive inherited from a parent type. In
15794 -- such case, given that Tagged_Type covers the interface,
15795 -- the inherited private primitive becomes visible. For such
15796 -- purpose we add a new entity that renames the inherited
15797 -- private primitive.
15799 elsif Chars (E) /= Chars (Iface_Subp) then
15800 pragma Assert (Has_Suffix (E, 'P'));
15801 Derive_Subprogram
15802 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15803 Set_Alias (New_Subp, E);
15804 Set_Is_Abstract_Subprogram (New_Subp,
15805 Is_Abstract_Subprogram (E));
15807 -- Propagate to the full view interface entities associated
15808 -- with the partial view.
15810 elsif In_Private_Part (Current_Scope)
15811 and then Present (Alias (E))
15812 and then Alias (E) = Iface_Subp
15813 and then
15814 List_Containing (Parent (E)) /=
15815 Private_Declarations
15816 (Specification
15817 (Unit_Declaration_Node (Current_Scope)))
15818 then
15819 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15820 end if;
15821 end if;
15823 Next_Elmt (Prim_Elmt);
15824 end loop;
15826 Next_Elmt (Iface_Elmt);
15827 end loop;
15828 end if;
15829 end Derive_Progenitor_Subprograms;
15831 -----------------------
15832 -- Derive_Subprogram --
15833 -----------------------
15835 procedure Derive_Subprogram
15836 (New_Subp : out Entity_Id;
15837 Parent_Subp : Entity_Id;
15838 Derived_Type : Entity_Id;
15839 Parent_Type : Entity_Id;
15840 Actual_Subp : Entity_Id := Empty)
15842 Formal : Entity_Id;
15843 -- Formal parameter of parent primitive operation
15845 Formal_Of_Actual : Entity_Id;
15846 -- Formal parameter of actual operation, when the derivation is to
15847 -- create a renaming for a primitive operation of an actual in an
15848 -- instantiation.
15850 New_Formal : Entity_Id;
15851 -- Formal of inherited operation
15853 Visible_Subp : Entity_Id := Parent_Subp;
15855 function Is_Private_Overriding return Boolean;
15856 -- If Subp is a private overriding of a visible operation, the inherited
15857 -- operation derives from the overridden op (even though its body is the
15858 -- overriding one) and the inherited operation is visible now. See
15859 -- sem_disp to see the full details of the handling of the overridden
15860 -- subprogram, which is removed from the list of primitive operations of
15861 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15862 -- and used to diagnose abstract operations that need overriding in the
15863 -- derived type.
15865 procedure Replace_Type (Id, New_Id : Entity_Id);
15866 -- When the type is an anonymous access type, create a new access type
15867 -- designating the derived type.
15869 procedure Set_Derived_Name;
15870 -- This procedure sets the appropriate Chars name for New_Subp. This
15871 -- is normally just a copy of the parent name. An exception arises for
15872 -- type support subprograms, where the name is changed to reflect the
15873 -- name of the derived type, e.g. if type foo is derived from type bar,
15874 -- then a procedure barDA is derived with a name fooDA.
15876 ---------------------------
15877 -- Is_Private_Overriding --
15878 ---------------------------
15880 function Is_Private_Overriding return Boolean is
15881 Prev : Entity_Id;
15883 begin
15884 -- If the parent is not a dispatching operation there is no
15885 -- need to investigate overridings
15887 if not Is_Dispatching_Operation (Parent_Subp) then
15888 return False;
15889 end if;
15891 -- The visible operation that is overridden is a homonym of the
15892 -- parent subprogram. We scan the homonym chain to find the one
15893 -- whose alias is the subprogram we are deriving.
15895 Prev := Current_Entity (Parent_Subp);
15896 while Present (Prev) loop
15897 if Ekind (Prev) = Ekind (Parent_Subp)
15898 and then Alias (Prev) = Parent_Subp
15899 and then Scope (Parent_Subp) = Scope (Prev)
15900 and then not Is_Hidden (Prev)
15901 then
15902 Visible_Subp := Prev;
15903 return True;
15904 end if;
15906 Prev := Homonym (Prev);
15907 end loop;
15909 return False;
15910 end Is_Private_Overriding;
15912 ------------------
15913 -- Replace_Type --
15914 ------------------
15916 procedure Replace_Type (Id, New_Id : Entity_Id) is
15917 Id_Type : constant Entity_Id := Etype (Id);
15918 Acc_Type : Entity_Id;
15919 Par : constant Node_Id := Parent (Derived_Type);
15921 begin
15922 -- When the type is an anonymous access type, create a new access
15923 -- type designating the derived type. This itype must be elaborated
15924 -- at the point of the derivation, not on subsequent calls that may
15925 -- be out of the proper scope for Gigi, so we insert a reference to
15926 -- it after the derivation.
15928 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15929 declare
15930 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15932 begin
15933 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15934 and then Present (Full_View (Desig_Typ))
15935 and then not Is_Private_Type (Parent_Type)
15936 then
15937 Desig_Typ := Full_View (Desig_Typ);
15938 end if;
15940 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15942 -- Ada 2005 (AI-251): Handle also derivations of abstract
15943 -- interface primitives.
15945 or else (Is_Interface (Desig_Typ)
15946 and then not Is_Class_Wide_Type (Desig_Typ))
15947 then
15948 Acc_Type := New_Copy (Id_Type);
15949 Set_Etype (Acc_Type, Acc_Type);
15950 Set_Scope (Acc_Type, New_Subp);
15952 -- Set size of anonymous access type. If we have an access
15953 -- to an unconstrained array, this is a fat pointer, so it
15954 -- is sizes at twice addtress size.
15956 if Is_Array_Type (Desig_Typ)
15957 and then not Is_Constrained (Desig_Typ)
15958 then
15959 Init_Size (Acc_Type, 2 * System_Address_Size);
15961 -- Other cases use a thin pointer
15963 else
15964 Init_Size (Acc_Type, System_Address_Size);
15965 end if;
15967 -- Set remaining characterstics of anonymous access type
15969 Reinit_Alignment (Acc_Type);
15970 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15972 Set_Etype (New_Id, Acc_Type);
15973 Set_Scope (New_Id, New_Subp);
15975 -- Create a reference to it
15977 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15979 else
15980 Set_Etype (New_Id, Id_Type);
15981 end if;
15982 end;
15984 -- In Ada2012, a formal may have an incomplete type but the type
15985 -- derivation that inherits the primitive follows the full view.
15987 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15988 or else
15989 (Ekind (Id_Type) = E_Record_Type_With_Private
15990 and then Present (Full_View (Id_Type))
15991 and then
15992 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15993 or else
15994 (Ada_Version >= Ada_2012
15995 and then Ekind (Id_Type) = E_Incomplete_Type
15996 and then Full_View (Id_Type) = Parent_Type)
15997 then
15998 -- Constraint checks on formals are generated during expansion,
15999 -- based on the signature of the original subprogram. The bounds
16000 -- of the derived type are not relevant, and thus we can use
16001 -- the base type for the formals. However, the return type may be
16002 -- used in a context that requires that the proper static bounds
16003 -- be used (a case statement, for example) and for those cases
16004 -- we must use the derived type (first subtype), not its base.
16006 -- If the derived_type_definition has no constraints, we know that
16007 -- the derived type has the same constraints as the first subtype
16008 -- of the parent, and we can also use it rather than its base,
16009 -- which can lead to more efficient code.
16011 if Etype (Id) = Parent_Type then
16012 if Is_Scalar_Type (Parent_Type)
16013 and then
16014 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16015 then
16016 Set_Etype (New_Id, Derived_Type);
16018 elsif Nkind (Par) = N_Full_Type_Declaration
16019 and then
16020 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16021 and then
16022 Is_Entity_Name
16023 (Subtype_Indication (Type_Definition (Par)))
16024 then
16025 Set_Etype (New_Id, Derived_Type);
16027 else
16028 Set_Etype (New_Id, Base_Type (Derived_Type));
16029 end if;
16031 else
16032 Set_Etype (New_Id, Base_Type (Derived_Type));
16033 end if;
16035 else
16036 Set_Etype (New_Id, Etype (Id));
16037 end if;
16038 end Replace_Type;
16040 ----------------------
16041 -- Set_Derived_Name --
16042 ----------------------
16044 procedure Set_Derived_Name is
16045 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16046 begin
16047 if Nm = TSS_Null then
16048 Set_Chars (New_Subp, Chars (Parent_Subp));
16049 else
16050 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16051 end if;
16052 end Set_Derived_Name;
16054 -- Start of processing for Derive_Subprogram
16056 begin
16057 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16058 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16060 -- Check whether the inherited subprogram is a private operation that
16061 -- should be inherited but not yet made visible. Such subprograms can
16062 -- become visible at a later point (e.g., the private part of a public
16063 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16064 -- following predicate is true, then this is not such a private
16065 -- operation and the subprogram simply inherits the name of the parent
16066 -- subprogram. Note the special check for the names of controlled
16067 -- operations, which are currently exempted from being inherited with
16068 -- a hidden name because they must be findable for generation of
16069 -- implicit run-time calls.
16071 if not Is_Hidden (Parent_Subp)
16072 or else Is_Internal (Parent_Subp)
16073 or else Is_Private_Overriding
16074 or else Is_Internal_Name (Chars (Parent_Subp))
16075 or else (Is_Controlled (Parent_Type)
16076 and then Chars (Parent_Subp) in Name_Adjust
16077 | Name_Finalize
16078 | Name_Initialize)
16079 then
16080 Set_Derived_Name;
16082 -- An inherited dispatching equality will be overridden by an internally
16083 -- generated one, or by an explicit one, so preserve its name and thus
16084 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16085 -- private operation it may become invisible if the full view has
16086 -- progenitors, and the dispatch table will be malformed.
16087 -- We check that the type is limited to handle the anomalous declaration
16088 -- of Limited_Controlled, which is derived from a non-limited type, and
16089 -- which is handled specially elsewhere as well.
16091 elsif Chars (Parent_Subp) = Name_Op_Eq
16092 and then Is_Dispatching_Operation (Parent_Subp)
16093 and then Etype (Parent_Subp) = Standard_Boolean
16094 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16095 and then
16096 Etype (First_Formal (Parent_Subp)) =
16097 Etype (Next_Formal (First_Formal (Parent_Subp)))
16098 then
16099 Set_Derived_Name;
16101 -- If parent is hidden, this can be a regular derivation if the
16102 -- parent is immediately visible in a non-instantiating context,
16103 -- or if we are in the private part of an instance. This test
16104 -- should still be refined ???
16106 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16107 -- operation as a non-visible operation in cases where the parent
16108 -- subprogram might not be visible now, but was visible within the
16109 -- original generic, so it would be wrong to make the inherited
16110 -- subprogram non-visible now. (Not clear if this test is fully
16111 -- correct; are there any cases where we should declare the inherited
16112 -- operation as not visible to avoid it being overridden, e.g., when
16113 -- the parent type is a generic actual with private primitives ???)
16115 -- (they should be treated the same as other private inherited
16116 -- subprograms, but it's not clear how to do this cleanly). ???
16118 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16119 and then Is_Immediately_Visible (Parent_Subp)
16120 and then not In_Instance)
16121 or else In_Instance_Not_Visible
16122 then
16123 Set_Derived_Name;
16125 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16126 -- overrides an interface primitive because interface primitives
16127 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16129 elsif Ada_Version >= Ada_2005
16130 and then Is_Dispatching_Operation (Parent_Subp)
16131 and then Present (Covered_Interface_Op (Parent_Subp))
16132 then
16133 Set_Derived_Name;
16135 -- Otherwise, the type is inheriting a private operation, so enter it
16136 -- with a special name so it can't be overridden. See also below, where
16137 -- we check for this case, and if so avoid setting Requires_Overriding.
16139 else
16140 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16141 end if;
16143 Set_Parent (New_Subp, Parent (Derived_Type));
16145 if Present (Actual_Subp) then
16146 Replace_Type (Actual_Subp, New_Subp);
16147 else
16148 Replace_Type (Parent_Subp, New_Subp);
16149 end if;
16151 Conditional_Delay (New_Subp, Parent_Subp);
16153 -- If we are creating a renaming for a primitive operation of an
16154 -- actual of a generic derived type, we must examine the signature
16155 -- of the actual primitive, not that of the generic formal, which for
16156 -- example may be an interface. However the name and initial value
16157 -- of the inherited operation are those of the formal primitive.
16159 Formal := First_Formal (Parent_Subp);
16161 if Present (Actual_Subp) then
16162 Formal_Of_Actual := First_Formal (Actual_Subp);
16163 else
16164 Formal_Of_Actual := Empty;
16165 end if;
16167 while Present (Formal) loop
16168 New_Formal := New_Copy (Formal);
16170 -- Extra formals are not inherited from a limited interface parent
16171 -- since limitedness is not inherited in such case (AI-419) and this
16172 -- affects the extra formals.
16174 if Is_Limited_Interface (Parent_Type) then
16175 Set_Extra_Formal (New_Formal, Empty);
16176 Set_Extra_Accessibility (New_Formal, Empty);
16177 end if;
16179 -- Normally we do not go copying parents, but in the case of
16180 -- formals, we need to link up to the declaration (which is the
16181 -- parameter specification), and it is fine to link up to the
16182 -- original formal's parameter specification in this case.
16184 Set_Parent (New_Formal, Parent (Formal));
16185 Append_Entity (New_Formal, New_Subp);
16187 if Present (Formal_Of_Actual) then
16188 Replace_Type (Formal_Of_Actual, New_Formal);
16189 Next_Formal (Formal_Of_Actual);
16190 else
16191 Replace_Type (Formal, New_Formal);
16192 end if;
16194 Next_Formal (Formal);
16195 end loop;
16197 -- Extra formals are shared between the parent subprogram and this
16198 -- internal entity built by Derive_Subprogram (implicit in the above
16199 -- copy of formals), unless the parent type is a limited interface type;
16200 -- hence we must inherit also the reference to the first extra formal.
16201 -- When the parent type is an interface, the extra formals will be added
16202 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16204 if not Is_Limited_Interface (Parent_Type) then
16205 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16207 if Ekind (New_Subp) = E_Function then
16208 Set_Extra_Accessibility_Of_Result (New_Subp,
16209 Extra_Accessibility_Of_Result (Parent_Subp));
16210 end if;
16211 end if;
16213 -- If this derivation corresponds to a tagged generic actual, then
16214 -- primitive operations rename those of the actual. Otherwise the
16215 -- primitive operations rename those of the parent type, If the parent
16216 -- renames an intrinsic operator, so does the new subprogram. We except
16217 -- concatenation, which is always properly typed, and does not get
16218 -- expanded as other intrinsic operations.
16220 if No (Actual_Subp) then
16221 if Is_Intrinsic_Subprogram (Parent_Subp) then
16222 Set_Is_Intrinsic_Subprogram (New_Subp);
16224 if Present (Alias (Parent_Subp))
16225 and then Chars (Parent_Subp) /= Name_Op_Concat
16226 then
16227 Set_Alias (New_Subp, Alias (Parent_Subp));
16228 else
16229 Set_Alias (New_Subp, Parent_Subp);
16230 end if;
16232 else
16233 Set_Alias (New_Subp, Parent_Subp);
16234 end if;
16236 else
16237 Set_Alias (New_Subp, Actual_Subp);
16238 end if;
16240 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16242 -- Derived subprograms of a tagged type must inherit the convention
16243 -- of the parent subprogram (a requirement of AI95-117). Derived
16244 -- subprograms of untagged types simply get convention Ada by default.
16246 -- If the derived type is a tagged generic formal type with unknown
16247 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16249 -- However, if the type is derived from a generic formal, the further
16250 -- inherited subprogram has the convention of the non-generic ancestor.
16251 -- Otherwise there would be no way to override the operation.
16252 -- (This is subject to forthcoming ARG discussions).
16254 if Is_Tagged_Type (Derived_Type) then
16255 if Is_Generic_Type (Derived_Type)
16256 and then Has_Unknown_Discriminants (Derived_Type)
16257 then
16258 Set_Convention (New_Subp, Convention_Intrinsic);
16260 else
16261 if Is_Generic_Type (Parent_Type)
16262 and then Has_Unknown_Discriminants (Parent_Type)
16263 then
16264 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16265 else
16266 Set_Convention (New_Subp, Convention (Parent_Subp));
16267 end if;
16268 end if;
16269 end if;
16271 -- Predefined controlled operations retain their name even if the parent
16272 -- is hidden (see above), but they are not primitive operations if the
16273 -- ancestor is not visible, for example if the parent is a private
16274 -- extension completed with a controlled extension. Note that a full
16275 -- type that is controlled can break privacy: the flag Is_Controlled is
16276 -- set on both views of the type.
16278 if Is_Controlled (Parent_Type)
16279 and then Chars (Parent_Subp) in Name_Initialize
16280 | Name_Adjust
16281 | Name_Finalize
16282 and then Is_Hidden (Parent_Subp)
16283 and then not Is_Visibly_Controlled (Parent_Type)
16284 then
16285 Set_Is_Hidden (New_Subp);
16286 end if;
16288 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16289 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16291 if Ekind (Parent_Subp) = E_Procedure then
16292 Set_Is_Valued_Procedure
16293 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16294 else
16295 Set_Has_Controlling_Result
16296 (New_Subp, Has_Controlling_Result (Parent_Subp));
16297 end if;
16299 -- No_Return must be inherited properly. If this is overridden in the
16300 -- case of a dispatching operation, then the check is made later in
16301 -- Check_Abstract_Overriding that the overriding operation is also
16302 -- No_Return (no such check is required for the nondispatching case).
16304 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16306 -- If the parent subprogram is marked as Ghost, then so is the derived
16307 -- subprogram. The ghost policy for the derived subprogram is set from
16308 -- the effective ghost policy at the point of derived type declaration.
16310 if Is_Ghost_Entity (Parent_Subp) then
16311 Set_Is_Ghost_Entity (New_Subp);
16312 end if;
16314 -- A derived function with a controlling result is abstract. If the
16315 -- Derived_Type is a nonabstract formal generic derived type, then
16316 -- inherited operations are not abstract: the required check is done at
16317 -- instantiation time. If the derivation is for a generic actual, the
16318 -- function is not abstract unless the actual is.
16320 if Is_Generic_Type (Derived_Type)
16321 and then not Is_Abstract_Type (Derived_Type)
16322 then
16323 null;
16325 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16326 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16327 -- that functions with controlling access results of record extensions
16328 -- with a null extension part require overriding (AI95-00391/06).
16330 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16331 -- implementing the rule of RM 7.3.2(6.1/4).
16333 -- A subprogram subject to pragma Extensions_Visible with value False
16334 -- requires overriding if the subprogram has at least one controlling
16335 -- OUT parameter (SPARK RM 6.1.7(6)).
16337 elsif Ada_Version >= Ada_2005
16338 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16339 or else (Is_Tagged_Type (Derived_Type)
16340 and then Etype (New_Subp) = Derived_Type
16341 and then not Is_Null_Extension (Derived_Type))
16342 or else (Is_Tagged_Type (Derived_Type)
16343 and then Ekind (Etype (New_Subp)) =
16344 E_Anonymous_Access_Type
16345 and then Designated_Type (Etype (New_Subp)) =
16346 Derived_Type)
16347 or else (Comes_From_Source (Alias (New_Subp))
16348 and then Is_EVF_Procedure (Alias (New_Subp)))
16350 -- AI12-0042: Set Requires_Overriding when a type extension
16351 -- inherits a private operation that is visible at the
16352 -- point of extension (Has_Private_Ancestor is False) from
16353 -- an ancestor that has Type_Invariant'Class, and when the
16354 -- type extension is in a visible part (the latter as
16355 -- clarified by AI12-0382).
16357 or else
16358 (not Has_Private_Ancestor (Derived_Type)
16359 and then Has_Invariants (Parent_Type)
16360 and then
16361 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16362 and then
16363 Class_Present
16364 (Get_Pragma (Parent_Type, Pragma_Invariant))
16365 and then Is_Private_Primitive (Parent_Subp)
16366 and then In_Visible_Part (Scope (Derived_Type))))
16368 and then No (Actual_Subp)
16369 then
16370 if not Is_Tagged_Type (Derived_Type)
16371 or else Is_Abstract_Type (Derived_Type)
16372 or else Is_Abstract_Subprogram (Alias (New_Subp))
16373 then
16374 Set_Is_Abstract_Subprogram (New_Subp);
16376 -- If the Chars of the new subprogram is different from that of the
16377 -- parent's one, it means that we entered it with a special name so
16378 -- it can't be overridden (see above). In that case we had better not
16379 -- *require* it to be overridden. This is the case where the parent
16380 -- type inherited the operation privately, so there's no danger of
16381 -- dangling dispatching.
16383 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16384 Set_Requires_Overriding (New_Subp);
16385 end if;
16387 elsif Ada_Version < Ada_2005
16388 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16389 or else (Is_Tagged_Type (Derived_Type)
16390 and then Etype (New_Subp) = Derived_Type
16391 and then No (Actual_Subp)))
16392 then
16393 Set_Is_Abstract_Subprogram (New_Subp);
16395 -- AI05-0097 : an inherited operation that dispatches on result is
16396 -- abstract if the derived type is abstract, even if the parent type
16397 -- is concrete and the derived type is a null extension.
16399 elsif Has_Controlling_Result (Alias (New_Subp))
16400 and then Is_Abstract_Type (Etype (New_Subp))
16401 then
16402 Set_Is_Abstract_Subprogram (New_Subp);
16404 -- Finally, if the parent type is abstract we must verify that all
16405 -- inherited operations are either non-abstract or overridden, or that
16406 -- the derived type itself is abstract (this check is performed at the
16407 -- end of a package declaration, in Check_Abstract_Overriding). A
16408 -- private overriding in the parent type will not be visible in the
16409 -- derivation if we are not in an inner package or in a child unit of
16410 -- the parent type, in which case the abstractness of the inherited
16411 -- operation is carried to the new subprogram.
16413 elsif Is_Abstract_Type (Parent_Type)
16414 and then not In_Open_Scopes (Scope (Parent_Type))
16415 and then Is_Private_Overriding
16416 and then Is_Abstract_Subprogram (Visible_Subp)
16417 then
16418 if No (Actual_Subp) then
16419 Set_Alias (New_Subp, Visible_Subp);
16420 Set_Is_Abstract_Subprogram (New_Subp, True);
16422 else
16423 -- If this is a derivation for an instance of a formal derived
16424 -- type, abstractness comes from the primitive operation of the
16425 -- actual, not from the operation inherited from the ancestor.
16427 Set_Is_Abstract_Subprogram
16428 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16429 end if;
16430 end if;
16432 New_Overloaded_Entity (New_Subp, Derived_Type);
16434 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16435 -- preconditions and the derived type is abstract, the derived operation
16436 -- is abstract as well if parent subprogram is not abstract or null.
16438 if Is_Abstract_Type (Derived_Type)
16439 and then Has_Non_Trivial_Precondition (Parent_Subp)
16440 and then Present (Interfaces (Derived_Type))
16441 then
16443 -- Add useful attributes of subprogram before the freeze point,
16444 -- in case freezing is delayed or there are previous errors.
16446 Set_Is_Dispatching_Operation (New_Subp);
16448 declare
16449 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16451 begin
16452 if Present (Iface_Prim)
16453 and then Has_Non_Trivial_Precondition (Iface_Prim)
16454 then
16455 Set_Is_Abstract_Subprogram (New_Subp);
16456 end if;
16457 end;
16458 end if;
16460 -- Check for case of a derived subprogram for the instantiation of a
16461 -- formal derived tagged type, if so mark the subprogram as dispatching
16462 -- and inherit the dispatching attributes of the actual subprogram. The
16463 -- derived subprogram is effectively renaming of the actual subprogram,
16464 -- so it needs to have the same attributes as the actual.
16466 if Present (Actual_Subp)
16467 and then Is_Dispatching_Operation (Actual_Subp)
16468 then
16469 Set_Is_Dispatching_Operation (New_Subp);
16471 if Present (DTC_Entity (Actual_Subp)) then
16472 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16473 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16474 end if;
16475 end if;
16477 -- Indicate that a derived subprogram does not require a body and that
16478 -- it does not require processing of default expressions.
16480 Set_Has_Completion (New_Subp);
16481 Set_Default_Expressions_Processed (New_Subp);
16483 if Ekind (New_Subp) = E_Function then
16484 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16485 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16486 end if;
16488 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16489 -- primitive subprogram S of a type T, then the aspect is inherited
16490 -- by the corresponding primitive subprogram of each descendant of T.
16492 if Is_Tagged_Type (Derived_Type)
16493 and then Is_Dispatching_Operation (New_Subp)
16494 and then Has_Yield_Aspect (Alias (New_Subp))
16495 then
16496 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16497 end if;
16499 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16500 end Derive_Subprogram;
16502 ------------------------
16503 -- Derive_Subprograms --
16504 ------------------------
16506 procedure Derive_Subprograms
16507 (Parent_Type : Entity_Id;
16508 Derived_Type : Entity_Id;
16509 Generic_Actual : Entity_Id := Empty)
16511 Op_List : constant Elist_Id :=
16512 Collect_Primitive_Operations (Parent_Type);
16514 function Check_Derived_Type return Boolean;
16515 -- Check that all the entities derived from Parent_Type are found in
16516 -- the list of primitives of Derived_Type exactly in the same order.
16518 procedure Derive_Interface_Subprogram
16519 (New_Subp : out Entity_Id;
16520 Subp : Entity_Id;
16521 Actual_Subp : Entity_Id);
16522 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16523 -- (which is an interface primitive). If Generic_Actual is present then
16524 -- Actual_Subp is the actual subprogram corresponding with the generic
16525 -- subprogram Subp.
16527 ------------------------
16528 -- Check_Derived_Type --
16529 ------------------------
16531 function Check_Derived_Type return Boolean is
16532 E : Entity_Id;
16533 Derived_Elmt : Elmt_Id;
16534 Derived_Op : Entity_Id;
16535 Derived_Ops : Elist_Id;
16536 Parent_Elmt : Elmt_Id;
16537 Parent_Op : Entity_Id;
16539 begin
16540 -- Traverse list of entities in the current scope searching for
16541 -- an incomplete type whose full-view is derived type.
16543 E := First_Entity (Scope (Derived_Type));
16544 while Present (E) and then E /= Derived_Type loop
16545 if Ekind (E) = E_Incomplete_Type
16546 and then Present (Full_View (E))
16547 and then Full_View (E) = Derived_Type
16548 then
16549 -- Disable this test if Derived_Type completes an incomplete
16550 -- type because in such case more primitives can be added
16551 -- later to the list of primitives of Derived_Type by routine
16552 -- Process_Incomplete_Dependents.
16554 return True;
16555 end if;
16557 Next_Entity (E);
16558 end loop;
16560 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16562 Derived_Elmt := First_Elmt (Derived_Ops);
16563 Parent_Elmt := First_Elmt (Op_List);
16564 while Present (Parent_Elmt) loop
16565 Parent_Op := Node (Parent_Elmt);
16566 Derived_Op := Node (Derived_Elmt);
16568 -- At this early stage Derived_Type has no entities with attribute
16569 -- Interface_Alias. In addition, such primitives are always
16570 -- located at the end of the list of primitives of Parent_Type.
16571 -- Therefore, if found we can safely stop processing pending
16572 -- entities.
16574 exit when Present (Interface_Alias (Parent_Op));
16576 -- Handle hidden entities
16578 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16579 and then Is_Hidden (Parent_Op)
16580 then
16581 if Present (Derived_Op)
16582 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16583 then
16584 Next_Elmt (Derived_Elmt);
16585 end if;
16587 else
16588 if No (Derived_Op)
16589 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16590 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16591 then
16592 return False;
16593 end if;
16595 Next_Elmt (Derived_Elmt);
16596 end if;
16598 Next_Elmt (Parent_Elmt);
16599 end loop;
16601 return True;
16602 end Check_Derived_Type;
16604 ---------------------------------
16605 -- Derive_Interface_Subprogram --
16606 ---------------------------------
16608 procedure Derive_Interface_Subprogram
16609 (New_Subp : out Entity_Id;
16610 Subp : Entity_Id;
16611 Actual_Subp : Entity_Id)
16613 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16614 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16616 begin
16617 pragma Assert (Is_Interface (Iface_Type));
16619 Derive_Subprogram
16620 (New_Subp => New_Subp,
16621 Parent_Subp => Iface_Subp,
16622 Derived_Type => Derived_Type,
16623 Parent_Type => Iface_Type,
16624 Actual_Subp => Actual_Subp);
16626 -- Given that this new interface entity corresponds with a primitive
16627 -- of the parent that was not overridden we must leave it associated
16628 -- with its parent primitive to ensure that it will share the same
16629 -- dispatch table slot when overridden. We must set the Alias to Subp
16630 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16631 -- (in case we inherited Subp from Iface_Type via a nonabstract
16632 -- generic formal type).
16634 if No (Actual_Subp) then
16635 Set_Alias (New_Subp, Subp);
16637 declare
16638 T : Entity_Id := Find_Dispatching_Type (Subp);
16639 begin
16640 while Etype (T) /= T loop
16641 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16642 Set_Is_Abstract_Subprogram (New_Subp, False);
16643 exit;
16644 end if;
16646 T := Etype (T);
16647 end loop;
16648 end;
16650 -- For instantiations this is not needed since the previous call to
16651 -- Derive_Subprogram leaves the entity well decorated.
16653 else
16654 pragma Assert (Alias (New_Subp) = Actual_Subp);
16655 null;
16656 end if;
16657 end Derive_Interface_Subprogram;
16659 -- Local variables
16661 Alias_Subp : Entity_Id;
16662 Act_List : Elist_Id;
16663 Act_Elmt : Elmt_Id;
16664 Act_Subp : Entity_Id := Empty;
16665 Elmt : Elmt_Id;
16666 Need_Search : Boolean := False;
16667 New_Subp : Entity_Id;
16668 Parent_Base : Entity_Id;
16669 Subp : Entity_Id;
16671 -- Start of processing for Derive_Subprograms
16673 begin
16674 if Ekind (Parent_Type) = E_Record_Type_With_Private
16675 and then Has_Discriminants (Parent_Type)
16676 and then Present (Full_View (Parent_Type))
16677 then
16678 Parent_Base := Full_View (Parent_Type);
16679 else
16680 Parent_Base := Parent_Type;
16681 end if;
16683 if Present (Generic_Actual) then
16684 Act_List := Collect_Primitive_Operations (Generic_Actual);
16685 Act_Elmt := First_Elmt (Act_List);
16686 else
16687 Act_List := No_Elist;
16688 Act_Elmt := No_Elmt;
16689 end if;
16691 -- Derive primitives inherited from the parent. Note that if the generic
16692 -- actual is present, this is not really a type derivation, it is a
16693 -- completion within an instance.
16695 -- Case 1: Derived_Type does not implement interfaces
16697 if not Is_Tagged_Type (Derived_Type)
16698 or else (not Has_Interfaces (Derived_Type)
16699 and then not (Present (Generic_Actual)
16700 and then Has_Interfaces (Generic_Actual)))
16701 then
16702 Elmt := First_Elmt (Op_List);
16703 while Present (Elmt) loop
16704 Subp := Node (Elmt);
16706 -- Literals are derived earlier in the process of building the
16707 -- derived type, and are skipped here.
16709 if Ekind (Subp) = E_Enumeration_Literal then
16710 null;
16712 -- The actual is a direct descendant and the common primitive
16713 -- operations appear in the same order.
16715 -- If the generic parent type is present, the derived type is an
16716 -- instance of a formal derived type, and within the instance its
16717 -- operations are those of the actual. We derive from the formal
16718 -- type but make the inherited operations aliases of the
16719 -- corresponding operations of the actual.
16721 else
16722 pragma Assert (No (Node (Act_Elmt))
16723 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16724 and then
16725 Type_Conformant
16726 (Subp, Node (Act_Elmt),
16727 Skip_Controlling_Formals => True)));
16729 Derive_Subprogram
16730 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16732 if Present (Act_Elmt) then
16733 Next_Elmt (Act_Elmt);
16734 end if;
16735 end if;
16737 Next_Elmt (Elmt);
16738 end loop;
16740 -- Case 2: Derived_Type implements interfaces
16742 else
16743 -- If the parent type has no predefined primitives we remove
16744 -- predefined primitives from the list of primitives of generic
16745 -- actual to simplify the complexity of this algorithm.
16747 if Present (Generic_Actual) then
16748 declare
16749 Has_Predefined_Primitives : Boolean := False;
16751 begin
16752 -- Check if the parent type has predefined primitives
16754 Elmt := First_Elmt (Op_List);
16755 while Present (Elmt) loop
16756 Subp := Node (Elmt);
16758 if Is_Predefined_Dispatching_Operation (Subp)
16759 and then not Comes_From_Source (Ultimate_Alias (Subp))
16760 then
16761 Has_Predefined_Primitives := True;
16762 exit;
16763 end if;
16765 Next_Elmt (Elmt);
16766 end loop;
16768 -- Remove predefined primitives of Generic_Actual. We must use
16769 -- an auxiliary list because in case of tagged types the value
16770 -- returned by Collect_Primitive_Operations is the value stored
16771 -- in its Primitive_Operations attribute (and we don't want to
16772 -- modify its current contents).
16774 if not Has_Predefined_Primitives then
16775 declare
16776 Aux_List : constant Elist_Id := New_Elmt_List;
16778 begin
16779 Elmt := First_Elmt (Act_List);
16780 while Present (Elmt) loop
16781 Subp := Node (Elmt);
16783 if not Is_Predefined_Dispatching_Operation (Subp)
16784 or else Comes_From_Source (Subp)
16785 then
16786 Append_Elmt (Subp, Aux_List);
16787 end if;
16789 Next_Elmt (Elmt);
16790 end loop;
16792 Act_List := Aux_List;
16793 end;
16794 end if;
16796 Act_Elmt := First_Elmt (Act_List);
16797 Act_Subp := Node (Act_Elmt);
16798 end;
16799 end if;
16801 -- Stage 1: If the generic actual is not present we derive the
16802 -- primitives inherited from the parent type. If the generic parent
16803 -- type is present, the derived type is an instance of a formal
16804 -- derived type, and within the instance its operations are those of
16805 -- the actual. We derive from the formal type but make the inherited
16806 -- operations aliases of the corresponding operations of the actual.
16808 Elmt := First_Elmt (Op_List);
16809 while Present (Elmt) loop
16810 Subp := Node (Elmt);
16811 Alias_Subp := Ultimate_Alias (Subp);
16813 -- Do not derive internal entities of the parent that link
16814 -- interface primitives with their covering primitive. These
16815 -- entities will be added to this type when frozen.
16817 if Present (Interface_Alias (Subp)) then
16818 goto Continue;
16819 end if;
16821 -- If the generic actual is present find the corresponding
16822 -- operation in the generic actual. If the parent type is a
16823 -- direct ancestor of the derived type then, even if it is an
16824 -- interface, the operations are inherited from the primary
16825 -- dispatch table and are in the proper order. If we detect here
16826 -- that primitives are not in the same order we traverse the list
16827 -- of primitive operations of the actual to find the one that
16828 -- implements the interface primitive.
16830 if Need_Search
16831 or else
16832 (Present (Generic_Actual)
16833 and then Present (Act_Subp)
16834 and then not
16835 (Primitive_Names_Match (Subp, Act_Subp)
16836 and then
16837 Type_Conformant (Subp, Act_Subp,
16838 Skip_Controlling_Formals => True)))
16839 then
16840 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16841 Use_Full_View => True));
16843 -- Remember that we need searching for all pending primitives
16845 Need_Search := True;
16847 -- Handle entities associated with interface primitives
16849 if Present (Alias_Subp)
16850 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16851 and then not Is_Predefined_Dispatching_Operation (Subp)
16852 then
16853 -- Search for the primitive in the homonym chain
16855 Act_Subp :=
16856 Find_Primitive_Covering_Interface
16857 (Tagged_Type => Generic_Actual,
16858 Iface_Prim => Alias_Subp);
16860 -- Previous search may not locate primitives covering
16861 -- interfaces defined in generics units or instantiations.
16862 -- (it fails if the covering primitive has formals whose
16863 -- type is also defined in generics or instantiations).
16864 -- In such case we search in the list of primitives of the
16865 -- generic actual for the internal entity that links the
16866 -- interface primitive and the covering primitive.
16868 if No (Act_Subp)
16869 and then Is_Generic_Type (Parent_Type)
16870 then
16871 -- This code has been designed to handle only generic
16872 -- formals that implement interfaces that are defined
16873 -- in a generic unit or instantiation. If this code is
16874 -- needed for other cases we must review it because
16875 -- (given that it relies on Original_Location to locate
16876 -- the primitive of Generic_Actual that covers the
16877 -- interface) it could leave linked through attribute
16878 -- Alias entities of unrelated instantiations).
16880 pragma Assert
16881 (Is_Generic_Unit
16882 (Scope (Find_Dispatching_Type (Alias_Subp)))
16883 or else
16884 Instantiation_Location
16885 (Sloc (Find_Dispatching_Type (Alias_Subp)))
16886 /= No_Location);
16887 declare
16888 Iface_Prim_Loc : constant Source_Ptr :=
16889 Original_Location (Sloc (Alias_Subp));
16891 Elmt : Elmt_Id;
16892 Prim : Entity_Id;
16894 begin
16895 Elmt :=
16896 First_Elmt (Primitive_Operations (Generic_Actual));
16898 Search : while Present (Elmt) loop
16899 Prim := Node (Elmt);
16901 if Present (Interface_Alias (Prim))
16902 and then Original_Location
16903 (Sloc (Interface_Alias (Prim))) =
16904 Iface_Prim_Loc
16905 then
16906 Act_Subp := Alias (Prim);
16907 exit Search;
16908 end if;
16910 Next_Elmt (Elmt);
16911 end loop Search;
16912 end;
16913 end if;
16915 pragma Assert (Present (Act_Subp)
16916 or else Is_Abstract_Type (Generic_Actual)
16917 or else Serious_Errors_Detected > 0);
16919 -- Handle predefined primitives plus the rest of user-defined
16920 -- primitives
16922 else
16923 Act_Elmt := First_Elmt (Act_List);
16924 while Present (Act_Elmt) loop
16925 Act_Subp := Node (Act_Elmt);
16927 exit when Primitive_Names_Match (Subp, Act_Subp)
16928 and then Type_Conformant
16929 (Subp, Act_Subp,
16930 Skip_Controlling_Formals => True)
16931 and then No (Interface_Alias (Act_Subp));
16933 Next_Elmt (Act_Elmt);
16934 end loop;
16936 if No (Act_Elmt) then
16937 Act_Subp := Empty;
16938 end if;
16939 end if;
16940 end if;
16942 -- Case 1: If the parent is a limited interface then it has the
16943 -- predefined primitives of synchronized interfaces. However, the
16944 -- actual type may be a non-limited type and hence it does not
16945 -- have such primitives.
16947 if Present (Generic_Actual)
16948 and then No (Act_Subp)
16949 and then Is_Limited_Interface (Parent_Base)
16950 and then Is_Predefined_Interface_Primitive (Subp)
16951 then
16952 null;
16954 -- Case 2: Inherit entities associated with interfaces that were
16955 -- not covered by the parent type. We exclude here null interface
16956 -- primitives because they do not need special management.
16958 -- We also exclude interface operations that are renamings. If the
16959 -- subprogram is an explicit renaming of an interface primitive,
16960 -- it is a regular primitive operation, and the presence of its
16961 -- alias is not relevant: it has to be derived like any other
16962 -- primitive.
16964 elsif Present (Alias (Subp))
16965 and then Nkind (Unit_Declaration_Node (Subp)) /=
16966 N_Subprogram_Renaming_Declaration
16967 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16968 and then not
16969 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16970 and then Null_Present (Parent (Alias_Subp)))
16971 then
16972 -- If this is an abstract private type then we transfer the
16973 -- derivation of the interface primitive from the partial view
16974 -- to the full view. This is safe because all the interfaces
16975 -- must be visible in the partial view. Done to avoid adding
16976 -- a new interface derivation to the private part of the
16977 -- enclosing package; otherwise this new derivation would be
16978 -- decorated as hidden when the analysis of the enclosing
16979 -- package completes.
16981 if Is_Abstract_Type (Derived_Type)
16982 and then In_Private_Part (Current_Scope)
16983 and then Has_Private_Declaration (Derived_Type)
16984 then
16985 declare
16986 Partial_View : Entity_Id;
16987 Elmt : Elmt_Id;
16988 Ent : Entity_Id;
16990 begin
16991 Partial_View := First_Entity (Current_Scope);
16992 loop
16993 exit when No (Partial_View)
16994 or else (Has_Private_Declaration (Partial_View)
16995 and then
16996 Full_View (Partial_View) = Derived_Type);
16998 Next_Entity (Partial_View);
16999 end loop;
17001 -- If the partial view was not found then the source code
17002 -- has errors and the derivation is not needed.
17004 if Present (Partial_View) then
17005 Elmt :=
17006 First_Elmt (Primitive_Operations (Partial_View));
17007 while Present (Elmt) loop
17008 Ent := Node (Elmt);
17010 if Present (Alias (Ent))
17011 and then Ultimate_Alias (Ent) = Alias (Subp)
17012 then
17013 Append_Elmt
17014 (Ent, Primitive_Operations (Derived_Type));
17015 exit;
17016 end if;
17018 Next_Elmt (Elmt);
17019 end loop;
17021 -- If the interface primitive was not found in the
17022 -- partial view then this interface primitive was
17023 -- overridden. We add a derivation to activate in
17024 -- Derive_Progenitor_Subprograms the machinery to
17025 -- search for it.
17027 if No (Elmt) then
17028 Derive_Interface_Subprogram
17029 (New_Subp => New_Subp,
17030 Subp => Subp,
17031 Actual_Subp => Act_Subp);
17032 end if;
17033 end if;
17034 end;
17035 else
17036 Derive_Interface_Subprogram
17037 (New_Subp => New_Subp,
17038 Subp => Subp,
17039 Actual_Subp => Act_Subp);
17040 end if;
17042 -- Case 3: Common derivation
17044 else
17045 Derive_Subprogram
17046 (New_Subp => New_Subp,
17047 Parent_Subp => Subp,
17048 Derived_Type => Derived_Type,
17049 Parent_Type => Parent_Base,
17050 Actual_Subp => Act_Subp);
17051 end if;
17053 -- No need to update Act_Elm if we must search for the
17054 -- corresponding operation in the generic actual
17056 if not Need_Search
17057 and then Present (Act_Elmt)
17058 then
17059 Next_Elmt (Act_Elmt);
17060 Act_Subp := Node (Act_Elmt);
17061 end if;
17063 <<Continue>>
17064 Next_Elmt (Elmt);
17065 end loop;
17067 -- Inherit additional operations from progenitors. If the derived
17068 -- type is a generic actual, there are not new primitive operations
17069 -- for the type because it has those of the actual, and therefore
17070 -- nothing needs to be done. The renamings generated above are not
17071 -- primitive operations, and their purpose is simply to make the
17072 -- proper operations visible within an instantiation.
17074 if No (Generic_Actual) then
17075 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17076 end if;
17077 end if;
17079 -- Final check: Direct descendants must have their primitives in the
17080 -- same order. We exclude from this test untagged types and instances
17081 -- of formal derived types. We skip this test if we have already
17082 -- reported serious errors in the sources.
17084 pragma Assert (not Is_Tagged_Type (Derived_Type)
17085 or else Present (Generic_Actual)
17086 or else Serious_Errors_Detected > 0
17087 or else Check_Derived_Type);
17088 end Derive_Subprograms;
17090 --------------------------------
17091 -- Derived_Standard_Character --
17092 --------------------------------
17094 procedure Derived_Standard_Character
17095 (N : Node_Id;
17096 Parent_Type : Entity_Id;
17097 Derived_Type : Entity_Id)
17099 Loc : constant Source_Ptr := Sloc (N);
17100 Def : constant Node_Id := Type_Definition (N);
17101 Indic : constant Node_Id := Subtype_Indication (Def);
17102 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17103 Implicit_Base : constant Entity_Id :=
17104 Create_Itype
17105 (E_Enumeration_Type, N, Derived_Type, 'B');
17107 Lo : Node_Id;
17108 Hi : Node_Id;
17110 begin
17111 Discard_Node (Process_Subtype (Indic, N));
17113 Set_Etype (Implicit_Base, Parent_Base);
17114 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17115 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17117 Set_Is_Character_Type (Implicit_Base, True);
17118 Set_Has_Delayed_Freeze (Implicit_Base);
17120 -- The bounds of the implicit base are the bounds of the parent base.
17121 -- Note that their type is the parent base.
17123 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17124 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17126 Set_Scalar_Range (Implicit_Base,
17127 Make_Range (Loc,
17128 Low_Bound => Lo,
17129 High_Bound => Hi));
17131 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17132 Set_Etype (Derived_Type, Implicit_Base);
17133 Set_Size_Info (Derived_Type, Parent_Type);
17135 if not Known_RM_Size (Derived_Type) then
17136 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17137 end if;
17139 Set_Is_Character_Type (Derived_Type, True);
17141 if Nkind (Indic) /= N_Subtype_Indication then
17143 -- If no explicit constraint, the bounds are those
17144 -- of the parent type.
17146 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17147 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17148 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17149 end if;
17151 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17152 end Derived_Standard_Character;
17154 ------------------------------
17155 -- Derived_Type_Declaration --
17156 ------------------------------
17158 procedure Derived_Type_Declaration
17159 (T : Entity_Id;
17160 N : Node_Id;
17161 Is_Completion : Boolean)
17163 Parent_Type : Entity_Id;
17165 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17166 -- Check whether the parent type is a generic formal, or derives
17167 -- directly or indirectly from one.
17169 ------------------------
17170 -- Comes_From_Generic --
17171 ------------------------
17173 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17174 begin
17175 if Is_Generic_Type (Typ) then
17176 return True;
17178 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17179 return True;
17181 elsif Is_Private_Type (Typ)
17182 and then Present (Full_View (Typ))
17183 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17184 then
17185 return True;
17187 elsif Is_Generic_Actual_Type (Typ) then
17188 return True;
17190 else
17191 return False;
17192 end if;
17193 end Comes_From_Generic;
17195 -- Local variables
17197 Def : constant Node_Id := Type_Definition (N);
17198 Iface_Def : Node_Id;
17199 Indic : constant Node_Id := Subtype_Indication (Def);
17200 Extension : constant Node_Id := Record_Extension_Part (Def);
17201 Parent_Node : Node_Id;
17202 Taggd : Boolean;
17204 -- Start of processing for Derived_Type_Declaration
17206 begin
17207 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17209 if SPARK_Mode = On
17210 and then Is_Tagged_Type (Parent_Type)
17211 then
17212 declare
17213 Partial_View : constant Entity_Id :=
17214 Incomplete_Or_Partial_View (Parent_Type);
17216 begin
17217 -- If the partial view was not found then the parent type is not
17218 -- a private type. Otherwise check if the partial view is a tagged
17219 -- private type.
17221 if Present (Partial_View)
17222 and then Is_Private_Type (Partial_View)
17223 and then not Is_Tagged_Type (Partial_View)
17224 then
17225 Error_Msg_NE
17226 ("cannot derive from & declared as untagged private "
17227 & "(SPARK RM 3.4(1))", N, Partial_View);
17228 end if;
17229 end;
17230 end if;
17232 -- Ada 2005 (AI-251): In case of interface derivation check that the
17233 -- parent is also an interface.
17235 if Interface_Present (Def) then
17236 if not Is_Interface (Parent_Type) then
17237 Diagnose_Interface (Indic, Parent_Type);
17239 else
17240 Parent_Node := Parent (Base_Type (Parent_Type));
17241 Iface_Def := Type_Definition (Parent_Node);
17243 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17244 -- other limited interfaces.
17246 if Limited_Present (Def) then
17247 if Limited_Present (Iface_Def) then
17248 null;
17250 elsif Protected_Present (Iface_Def) then
17251 Error_Msg_NE
17252 ("descendant of & must be declared as a protected "
17253 & "interface", N, Parent_Type);
17255 elsif Synchronized_Present (Iface_Def) then
17256 Error_Msg_NE
17257 ("descendant of & must be declared as a synchronized "
17258 & "interface", N, Parent_Type);
17260 elsif Task_Present (Iface_Def) then
17261 Error_Msg_NE
17262 ("descendant of & must be declared as a task interface",
17263 N, Parent_Type);
17265 else
17266 Error_Msg_N
17267 ("(Ada 2005) limited interface cannot inherit from "
17268 & "non-limited interface", Indic);
17269 end if;
17271 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17272 -- from non-limited or limited interfaces.
17274 elsif not Protected_Present (Def)
17275 and then not Synchronized_Present (Def)
17276 and then not Task_Present (Def)
17277 then
17278 if Limited_Present (Iface_Def) then
17279 null;
17281 elsif Protected_Present (Iface_Def) then
17282 Error_Msg_NE
17283 ("descendant of & must be declared as a protected "
17284 & "interface", N, Parent_Type);
17286 elsif Synchronized_Present (Iface_Def) then
17287 Error_Msg_NE
17288 ("descendant of & must be declared as a synchronized "
17289 & "interface", N, Parent_Type);
17291 elsif Task_Present (Iface_Def) then
17292 Error_Msg_NE
17293 ("descendant of & must be declared as a task interface",
17294 N, Parent_Type);
17295 else
17296 null;
17297 end if;
17298 end if;
17299 end if;
17300 end if;
17302 if Is_Tagged_Type (Parent_Type)
17303 and then Is_Concurrent_Type (Parent_Type)
17304 and then not Is_Interface (Parent_Type)
17305 then
17306 Error_Msg_N
17307 ("parent type of a record extension cannot be a synchronized "
17308 & "tagged type (RM 3.9.1 (3/1))", N);
17309 Set_Etype (T, Any_Type);
17310 return;
17311 end if;
17313 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17314 -- interfaces
17316 if Is_Tagged_Type (Parent_Type)
17317 and then Is_Non_Empty_List (Interface_List (Def))
17318 then
17319 declare
17320 Intf : Node_Id;
17321 T : Entity_Id;
17323 begin
17324 Intf := First (Interface_List (Def));
17325 while Present (Intf) loop
17326 T := Find_Type_Of_Subtype_Indic (Intf);
17328 if not Is_Interface (T) then
17329 Diagnose_Interface (Intf, T);
17331 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17332 -- a limited type from having a nonlimited progenitor.
17334 elsif (Limited_Present (Def)
17335 or else (not Is_Interface (Parent_Type)
17336 and then Is_Limited_Type (Parent_Type)))
17337 and then not Is_Limited_Interface (T)
17338 then
17339 Error_Msg_NE
17340 ("progenitor interface& of limited type must be limited",
17341 N, T);
17342 end if;
17344 Next (Intf);
17345 end loop;
17346 end;
17348 -- Check consistency of any nonoverridable aspects that are
17349 -- inherited from multiple sources.
17351 Check_Inherited_Nonoverridable_Aspects
17352 (Inheritor => T,
17353 Interface_List => Interface_List (Def),
17354 Parent_Type => Parent_Type);
17355 end if;
17357 if Parent_Type = Any_Type
17358 or else Etype (Parent_Type) = Any_Type
17359 or else (Is_Class_Wide_Type (Parent_Type)
17360 and then Etype (Parent_Type) = T)
17361 then
17362 -- If Parent_Type is undefined or illegal, make new type into a
17363 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17364 -- errors. If this is a self-definition, emit error now.
17366 if T = Parent_Type or else T = Etype (Parent_Type) then
17367 Error_Msg_N ("type cannot be used in its own definition", Indic);
17368 end if;
17370 Mutate_Ekind (T, Ekind (Parent_Type));
17371 Set_Etype (T, Any_Type);
17372 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17374 -- Initialize the list of primitive operations to an empty list,
17375 -- to cover tagged types as well as untagged types. For untagged
17376 -- types this is used either to analyze the call as legal when
17377 -- Extensions_Allowed is True, or to issue a better error message
17378 -- otherwise.
17380 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17382 return;
17383 end if;
17385 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17386 -- an interface is special because the list of interfaces in the full
17387 -- view can be given in any order. For example:
17389 -- type A is interface;
17390 -- type B is interface and A;
17391 -- type D is new B with private;
17392 -- private
17393 -- type D is new A and B with null record; -- 1 --
17395 -- In this case we perform the following transformation of -1-:
17397 -- type D is new B and A with null record;
17399 -- If the parent of the full-view covers the parent of the partial-view
17400 -- we have two possible cases:
17402 -- 1) They have the same parent
17403 -- 2) The parent of the full-view implements some further interfaces
17405 -- In both cases we do not need to perform the transformation. In the
17406 -- first case the source program is correct and the transformation is
17407 -- not needed; in the second case the source program does not fulfill
17408 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17409 -- later.
17411 -- This transformation not only simplifies the rest of the analysis of
17412 -- this type declaration but also simplifies the correct generation of
17413 -- the object layout to the expander.
17415 if In_Private_Part (Current_Scope)
17416 and then Is_Interface (Parent_Type)
17417 then
17418 declare
17419 Partial_View : Entity_Id;
17420 Partial_View_Parent : Entity_Id;
17422 function Reorder_Interfaces return Boolean;
17423 -- Look for an interface in the full view's interface list that
17424 -- matches the parent type of the partial view, and when found,
17425 -- rewrite the full view's parent with the partial view's parent,
17426 -- append the full view's original parent to the interface list,
17427 -- recursively call Derived_Type_Definition on the full type, and
17428 -- return True. If a match is not found, return False.
17430 ------------------------
17431 -- Reorder_Interfaces --
17432 ------------------------
17434 function Reorder_Interfaces return Boolean is
17435 Iface : Node_Id;
17436 New_Iface : Node_Id;
17438 begin
17439 Iface := First (Interface_List (Def));
17440 while Present (Iface) loop
17441 if Etype (Iface) = Etype (Partial_View) then
17442 Rewrite (Subtype_Indication (Def),
17443 New_Copy (Subtype_Indication (Parent (Partial_View))));
17445 New_Iface :=
17446 Make_Identifier (Sloc (N), Chars (Parent_Type));
17447 Rewrite (Iface, New_Iface);
17449 -- Analyze the transformed code
17451 Derived_Type_Declaration (T, N, Is_Completion);
17452 return True;
17453 end if;
17455 Next (Iface);
17456 end loop;
17457 return False;
17458 end Reorder_Interfaces;
17460 begin
17461 -- Look for the associated private type declaration
17463 Partial_View := Incomplete_Or_Partial_View (T);
17465 -- If the partial view was not found then the source code has
17466 -- errors and the transformation is not needed.
17468 if Present (Partial_View) then
17469 Partial_View_Parent := Etype (Partial_View);
17471 -- If the parent of the full-view covers the parent of the
17472 -- partial-view we have nothing else to do.
17474 if Interface_Present_In_Ancestor
17475 (Parent_Type, Partial_View_Parent)
17476 then
17477 null;
17479 -- Traverse the list of interfaces of the full view to look
17480 -- for the parent of the partial view and reorder the
17481 -- interfaces to match the order in the partial view,
17482 -- if needed.
17484 else
17486 if Reorder_Interfaces then
17487 -- Having the interfaces listed in any order is legal.
17488 -- However, the compiler does not properly handle
17489 -- different orders between partial and full views in
17490 -- generic units. We give a warning about the order
17491 -- mismatch, so the user can work around this problem.
17493 Error_Msg_N ("??full declaration does not respect " &
17494 "partial declaration order", T);
17495 Error_Msg_N ("\??consider reordering", T);
17497 return;
17498 end if;
17499 end if;
17500 end if;
17501 end;
17502 end if;
17504 -- Only composite types other than array types are allowed to have
17505 -- discriminants.
17507 if Present (Discriminant_Specifications (N)) then
17508 if (Is_Elementary_Type (Parent_Type)
17509 or else
17510 Is_Array_Type (Parent_Type))
17511 and then not Error_Posted (N)
17512 then
17513 Error_Msg_N
17514 ("elementary or array type cannot have discriminants",
17515 Defining_Identifier (First (Discriminant_Specifications (N))));
17517 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17518 -- only if we are not already processing a malformed syntax tree.
17520 if Is_Type (T) then
17521 Set_Has_Discriminants (T, False);
17522 end if;
17523 end if;
17524 end if;
17526 -- In Ada 83, a derived type defined in a package specification cannot
17527 -- be used for further derivation until the end of its visible part.
17528 -- Note that derivation in the private part of the package is allowed.
17530 if Ada_Version = Ada_83
17531 and then Is_Derived_Type (Parent_Type)
17532 and then In_Visible_Part (Scope (Parent_Type))
17533 then
17534 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17535 Error_Msg_N
17536 ("(Ada 83) premature use of type for derivation", Indic);
17537 end if;
17538 end if;
17540 -- Check for early use of incomplete or private type
17542 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17543 Error_Msg_N ("premature derivation of incomplete type", Indic);
17544 return;
17546 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17547 and then not Comes_From_Generic (Parent_Type))
17548 or else Has_Private_Component (Parent_Type)
17549 then
17550 -- The ancestor type of a formal type can be incomplete, in which
17551 -- case only the operations of the partial view are available in the
17552 -- generic. Subsequent checks may be required when the full view is
17553 -- analyzed to verify that a derivation from a tagged type has an
17554 -- extension.
17556 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17557 null;
17559 elsif No (Underlying_Type (Parent_Type))
17560 or else Has_Private_Component (Parent_Type)
17561 then
17562 Error_Msg_N
17563 ("premature derivation of derived or private type", Indic);
17565 -- Flag the type itself as being in error, this prevents some
17566 -- nasty problems with subsequent uses of the malformed type.
17568 Set_Error_Posted (T);
17570 -- Check that within the immediate scope of an untagged partial
17571 -- view it's illegal to derive from the partial view if the
17572 -- full view is tagged. (7.3(7))
17574 -- We verify that the Parent_Type is a partial view by checking
17575 -- that it is not a Full_Type_Declaration (i.e. a private type or
17576 -- private extension declaration), to distinguish a partial view
17577 -- from a derivation from a private type which also appears as
17578 -- E_Private_Type. If the parent base type is not declared in an
17579 -- enclosing scope there is no need to check.
17581 elsif Present (Full_View (Parent_Type))
17582 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17583 and then not Is_Tagged_Type (Parent_Type)
17584 and then Is_Tagged_Type (Full_View (Parent_Type))
17585 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17586 then
17587 Error_Msg_N
17588 ("premature derivation from type with tagged full view",
17589 Indic);
17590 end if;
17591 end if;
17593 -- Check that form of derivation is appropriate
17595 Taggd := Is_Tagged_Type (Parent_Type);
17597 -- Set the parent type to the class-wide type's specific type in this
17598 -- case to prevent cascading errors
17600 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17601 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17602 Set_Etype (T, Etype (Parent_Type));
17603 return;
17604 end if;
17606 if Present (Extension) and then not Taggd then
17607 Error_Msg_N
17608 ("type derived from untagged type cannot have extension", Indic);
17610 elsif No (Extension) and then Taggd then
17612 -- If this declaration is within a private part (or body) of a
17613 -- generic instantiation then the derivation is allowed (the parent
17614 -- type can only appear tagged in this case if it's a generic actual
17615 -- type, since it would otherwise have been rejected in the analysis
17616 -- of the generic template).
17618 if not Is_Generic_Actual_Type (Parent_Type)
17619 or else In_Visible_Part (Scope (Parent_Type))
17620 then
17621 if Is_Class_Wide_Type (Parent_Type) then
17622 Error_Msg_N
17623 ("parent type must not be a class-wide type", Indic);
17625 -- Use specific type to prevent cascaded errors.
17627 Parent_Type := Etype (Parent_Type);
17629 else
17630 Error_Msg_N
17631 ("type derived from tagged type must have extension", Indic);
17632 end if;
17633 end if;
17634 end if;
17636 -- AI-443: Synchronized formal derived types require a private
17637 -- extension. There is no point in checking the ancestor type or
17638 -- the progenitors since the construct is wrong to begin with.
17640 if Ada_Version >= Ada_2005
17641 and then Is_Generic_Type (T)
17642 and then Present (Original_Node (N))
17643 then
17644 declare
17645 Decl : constant Node_Id := Original_Node (N);
17647 begin
17648 if Nkind (Decl) = N_Formal_Type_Declaration
17649 and then Nkind (Formal_Type_Definition (Decl)) =
17650 N_Formal_Derived_Type_Definition
17651 and then Synchronized_Present (Formal_Type_Definition (Decl))
17652 and then No (Extension)
17654 -- Avoid emitting a duplicate error message
17656 and then not Error_Posted (Indic)
17657 then
17658 Error_Msg_N
17659 ("synchronized derived type must have extension", N);
17660 end if;
17661 end;
17662 end if;
17664 if Null_Exclusion_Present (Def)
17665 and then not Is_Access_Type (Parent_Type)
17666 then
17667 Error_Msg_N ("null exclusion can only apply to an access type", N);
17668 end if;
17670 Check_Wide_Character_Restriction (Parent_Type, Indic);
17672 -- Avoid deriving parent primitives of underlying record views
17674 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17675 Derive_Subps => not Is_Underlying_Record_View (T));
17677 -- AI-419: The parent type of an explicitly limited derived type must
17678 -- be a limited type or a limited interface.
17680 if Limited_Present (Def) then
17681 Set_Is_Limited_Record (T);
17683 if Is_Interface (T) then
17684 Set_Is_Limited_Interface (T);
17685 end if;
17687 if not Is_Limited_Type (Parent_Type)
17688 and then
17689 (not Is_Interface (Parent_Type)
17690 or else not Is_Limited_Interface (Parent_Type))
17691 then
17692 -- AI05-0096: a derivation in the private part of an instance is
17693 -- legal if the generic formal is untagged limited, and the actual
17694 -- is non-limited.
17696 if Is_Generic_Actual_Type (Parent_Type)
17697 and then In_Private_Part (Current_Scope)
17698 and then
17699 not Is_Tagged_Type
17700 (Generic_Parent_Type (Parent (Parent_Type)))
17701 then
17702 null;
17704 else
17705 Error_Msg_NE
17706 ("parent type& of limited type must be limited",
17707 N, Parent_Type);
17708 end if;
17709 end if;
17710 end if;
17711 end Derived_Type_Declaration;
17713 ------------------------
17714 -- Diagnose_Interface --
17715 ------------------------
17717 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17718 begin
17719 if not Is_Interface (E) and then E /= Any_Type then
17720 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17721 end if;
17722 end Diagnose_Interface;
17724 ----------------------------------
17725 -- Enumeration_Type_Declaration --
17726 ----------------------------------
17728 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17729 Ev : Uint;
17730 L : Node_Id;
17731 R_Node : Node_Id;
17732 B_Node : Node_Id;
17734 begin
17735 -- Create identifier node representing lower bound
17737 B_Node := New_Node (N_Identifier, Sloc (Def));
17738 L := First (Literals (Def));
17739 Set_Chars (B_Node, Chars (L));
17740 Set_Entity (B_Node, L);
17741 Set_Etype (B_Node, T);
17742 Set_Is_Static_Expression (B_Node, True);
17744 R_Node := New_Node (N_Range, Sloc (Def));
17745 Set_Low_Bound (R_Node, B_Node);
17747 Mutate_Ekind (T, E_Enumeration_Type);
17748 Set_First_Literal (T, L);
17749 Set_Etype (T, T);
17750 Set_Is_Constrained (T);
17752 Ev := Uint_0;
17754 -- Loop through literals of enumeration type setting pos and rep values
17755 -- except that if the Ekind is already set, then it means the literal
17756 -- was already constructed (case of a derived type declaration and we
17757 -- should not disturb the Pos and Rep values.
17759 while Present (L) loop
17760 if Ekind (L) /= E_Enumeration_Literal then
17761 Mutate_Ekind (L, E_Enumeration_Literal);
17762 Set_Enumeration_Pos (L, Ev);
17763 Set_Enumeration_Rep (L, Ev);
17764 Set_Is_Known_Valid (L, True);
17765 end if;
17767 Set_Etype (L, T);
17768 New_Overloaded_Entity (L);
17769 Generate_Definition (L);
17770 Set_Convention (L, Convention_Intrinsic);
17772 -- Case of character literal
17774 if Nkind (L) = N_Defining_Character_Literal then
17775 Set_Is_Character_Type (T, True);
17777 -- Check violation of No_Wide_Characters
17779 if Restriction_Check_Required (No_Wide_Characters) then
17780 Get_Name_String (Chars (L));
17782 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17783 Check_Restriction (No_Wide_Characters, L);
17784 end if;
17785 end if;
17786 end if;
17788 Ev := Ev + 1;
17789 Next (L);
17790 end loop;
17792 -- Now create a node representing upper bound
17794 B_Node := New_Node (N_Identifier, Sloc (Def));
17795 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17796 Set_Entity (B_Node, Last (Literals (Def)));
17797 Set_Etype (B_Node, T);
17798 Set_Is_Static_Expression (B_Node, True);
17800 Set_High_Bound (R_Node, B_Node);
17802 -- Initialize various fields of the type. Some of this information
17803 -- may be overwritten later through rep. clauses.
17805 Set_Scalar_Range (T, R_Node);
17806 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17807 Set_Enum_Esize (T);
17808 Set_Enum_Pos_To_Rep (T, Empty);
17810 -- Set Discard_Names if configuration pragma set, or if there is
17811 -- a parameterless pragma in the current declarative region
17813 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17814 Set_Discard_Names (T);
17815 end if;
17817 -- Process end label if there is one
17819 if Present (Def) then
17820 Process_End_Label (Def, 'e', T);
17821 end if;
17822 end Enumeration_Type_Declaration;
17824 ---------------------------------
17825 -- Expand_To_Stored_Constraint --
17826 ---------------------------------
17828 function Expand_To_Stored_Constraint
17829 (Typ : Entity_Id;
17830 Constraint : Elist_Id) return Elist_Id
17832 Explicitly_Discriminated_Type : Entity_Id;
17833 Expansion : Elist_Id;
17834 Discriminant : Entity_Id;
17836 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17837 -- Find the nearest type that actually specifies discriminants
17839 ---------------------------------
17840 -- Type_With_Explicit_Discrims --
17841 ---------------------------------
17843 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17844 Typ : constant E := Base_Type (Id);
17846 begin
17847 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17848 if Present (Full_View (Typ)) then
17849 return Type_With_Explicit_Discrims (Full_View (Typ));
17850 end if;
17852 else
17853 if Has_Discriminants (Typ) then
17854 return Typ;
17855 end if;
17856 end if;
17858 if Etype (Typ) = Typ then
17859 return Empty;
17860 elsif Has_Discriminants (Typ) then
17861 return Typ;
17862 else
17863 return Type_With_Explicit_Discrims (Etype (Typ));
17864 end if;
17866 end Type_With_Explicit_Discrims;
17868 -- Start of processing for Expand_To_Stored_Constraint
17870 begin
17871 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17872 return No_Elist;
17873 end if;
17875 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17877 if No (Explicitly_Discriminated_Type) then
17878 return No_Elist;
17879 end if;
17881 Expansion := New_Elmt_List;
17883 Discriminant :=
17884 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17885 while Present (Discriminant) loop
17886 Append_Elmt
17887 (Get_Discriminant_Value
17888 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17889 To => Expansion);
17890 Next_Stored_Discriminant (Discriminant);
17891 end loop;
17893 return Expansion;
17894 end Expand_To_Stored_Constraint;
17896 ---------------------------
17897 -- Find_Hidden_Interface --
17898 ---------------------------
17900 function Find_Hidden_Interface
17901 (Src : Elist_Id;
17902 Dest : Elist_Id) return Entity_Id
17904 Iface : Entity_Id;
17905 Iface_Elmt : Elmt_Id;
17907 begin
17908 if Present (Src) and then Present (Dest) then
17909 Iface_Elmt := First_Elmt (Src);
17910 while Present (Iface_Elmt) loop
17911 Iface := Node (Iface_Elmt);
17913 if Is_Interface (Iface)
17914 and then not Contain_Interface (Iface, Dest)
17915 then
17916 return Iface;
17917 end if;
17919 Next_Elmt (Iface_Elmt);
17920 end loop;
17921 end if;
17923 return Empty;
17924 end Find_Hidden_Interface;
17926 --------------------
17927 -- Find_Type_Name --
17928 --------------------
17930 function Find_Type_Name (N : Node_Id) return Entity_Id is
17931 Id : constant Entity_Id := Defining_Identifier (N);
17932 New_Id : Entity_Id;
17933 Prev : Entity_Id;
17934 Prev_Par : Node_Id;
17936 procedure Check_Duplicate_Aspects;
17937 -- Check that aspects specified in a completion have not been specified
17938 -- already in the partial view.
17940 procedure Tag_Mismatch;
17941 -- Diagnose a tagged partial view whose full view is untagged. We post
17942 -- the message on the full view, with a reference to the previous
17943 -- partial view. The partial view can be private or incomplete, and
17944 -- these are handled in a different manner, so we determine the position
17945 -- of the error message from the respective slocs of both.
17947 -----------------------------
17948 -- Check_Duplicate_Aspects --
17949 -----------------------------
17951 procedure Check_Duplicate_Aspects is
17952 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17953 -- Return the corresponding aspect of the partial view which matches
17954 -- the aspect id of Asp. Return Empty is no such aspect exists.
17956 -----------------------------
17957 -- Get_Partial_View_Aspect --
17958 -----------------------------
17960 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17961 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17962 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17963 Prev_Asp : Node_Id;
17965 begin
17966 if Present (Prev_Asps) then
17967 Prev_Asp := First (Prev_Asps);
17968 while Present (Prev_Asp) loop
17969 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17970 return Prev_Asp;
17971 end if;
17973 Next (Prev_Asp);
17974 end loop;
17975 end if;
17977 return Empty;
17978 end Get_Partial_View_Aspect;
17980 -- Local variables
17982 Full_Asps : constant List_Id := Aspect_Specifications (N);
17983 Full_Asp : Node_Id;
17984 Part_Asp : Node_Id;
17986 -- Start of processing for Check_Duplicate_Aspects
17988 begin
17989 if Present (Full_Asps) then
17990 Full_Asp := First (Full_Asps);
17991 while Present (Full_Asp) loop
17992 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17994 -- An aspect and its class-wide counterpart are two distinct
17995 -- aspects and may apply to both views of an entity.
17997 if Present (Part_Asp)
17998 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17999 then
18000 Error_Msg_N
18001 ("aspect already specified in private declaration",
18002 Full_Asp);
18004 Remove (Full_Asp);
18005 return;
18006 end if;
18008 if Has_Discriminants (Prev)
18009 and then not Has_Unknown_Discriminants (Prev)
18010 and then Get_Aspect_Id (Full_Asp) =
18011 Aspect_Implicit_Dereference
18012 then
18013 Error_Msg_N
18014 ("cannot specify aspect if partial view has known "
18015 & "discriminants", Full_Asp);
18016 end if;
18018 Next (Full_Asp);
18019 end loop;
18020 end if;
18021 end Check_Duplicate_Aspects;
18023 ------------------
18024 -- Tag_Mismatch --
18025 ------------------
18027 procedure Tag_Mismatch is
18028 begin
18029 if Sloc (Prev) < Sloc (Id) then
18030 if Ada_Version >= Ada_2012
18031 and then Nkind (N) = N_Private_Type_Declaration
18032 then
18033 Error_Msg_NE
18034 ("declaration of private } must be a tagged type", Id, Prev);
18035 else
18036 Error_Msg_NE
18037 ("full declaration of } must be a tagged type", Id, Prev);
18038 end if;
18040 else
18041 if Ada_Version >= Ada_2012
18042 and then Nkind (N) = N_Private_Type_Declaration
18043 then
18044 Error_Msg_NE
18045 ("declaration of private } must be a tagged type", Prev, Id);
18046 else
18047 Error_Msg_NE
18048 ("full declaration of } must be a tagged type", Prev, Id);
18049 end if;
18050 end if;
18051 end Tag_Mismatch;
18053 -- Start of processing for Find_Type_Name
18055 begin
18056 -- Find incomplete declaration, if one was given
18058 Prev := Current_Entity_In_Scope (Id);
18060 -- New type declaration
18062 if No (Prev) then
18063 Enter_Name (Id);
18064 return Id;
18066 -- Previous declaration exists
18068 else
18069 Prev_Par := Parent (Prev);
18071 -- Error if not incomplete/private case except if previous
18072 -- declaration is implicit, etc. Enter_Name will emit error if
18073 -- appropriate.
18075 if not Is_Incomplete_Or_Private_Type (Prev) then
18076 Enter_Name (Id);
18077 New_Id := Id;
18079 -- Check invalid completion of private or incomplete type
18081 elsif Nkind (N) not in N_Full_Type_Declaration
18082 | N_Task_Type_Declaration
18083 | N_Protected_Type_Declaration
18084 and then
18085 (Ada_Version < Ada_2012
18086 or else not Is_Incomplete_Type (Prev)
18087 or else Nkind (N) not in N_Private_Type_Declaration
18088 | N_Private_Extension_Declaration)
18089 then
18090 -- Completion must be a full type declarations (RM 7.3(4))
18092 Error_Msg_Sloc := Sloc (Prev);
18093 Error_Msg_NE ("invalid completion of }", Id, Prev);
18095 -- Set scope of Id to avoid cascaded errors. Entity is never
18096 -- examined again, except when saving globals in generics.
18098 Set_Scope (Id, Current_Scope);
18099 New_Id := Id;
18101 -- If this is a repeated incomplete declaration, no further
18102 -- checks are possible.
18104 if Nkind (N) = N_Incomplete_Type_Declaration then
18105 return Prev;
18106 end if;
18108 -- Case of full declaration of incomplete type
18110 elsif Ekind (Prev) = E_Incomplete_Type
18111 and then (Ada_Version < Ada_2012
18112 or else No (Full_View (Prev))
18113 or else not Is_Private_Type (Full_View (Prev)))
18114 then
18115 -- Indicate that the incomplete declaration has a matching full
18116 -- declaration. The defining occurrence of the incomplete
18117 -- declaration remains the visible one, and the procedure
18118 -- Get_Full_View dereferences it whenever the type is used.
18120 if Present (Full_View (Prev)) then
18121 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18122 end if;
18124 Set_Full_View (Prev, Id);
18125 Append_Entity (Id, Current_Scope);
18126 Set_Is_Public (Id, Is_Public (Prev));
18127 Set_Is_Internal (Id);
18128 New_Id := Prev;
18130 -- If the incomplete view is tagged, a class_wide type has been
18131 -- created already. Use it for the private type as well, in order
18132 -- to prevent multiple incompatible class-wide types that may be
18133 -- created for self-referential anonymous access components.
18135 if Is_Tagged_Type (Prev)
18136 and then Present (Class_Wide_Type (Prev))
18137 then
18138 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18139 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18141 -- Type of the class-wide type is the current Id. Previously
18142 -- this was not done for private declarations because of order-
18143 -- of-elaboration issues in the back end, but gigi now handles
18144 -- this properly.
18146 Set_Etype (Class_Wide_Type (Id), Id);
18147 end if;
18149 -- Case of full declaration of private type
18151 else
18152 -- If the private type was a completion of an incomplete type then
18153 -- update Prev to reference the private type
18155 if Ada_Version >= Ada_2012
18156 and then Ekind (Prev) = E_Incomplete_Type
18157 and then Present (Full_View (Prev))
18158 and then Is_Private_Type (Full_View (Prev))
18159 then
18160 Prev := Full_View (Prev);
18161 Prev_Par := Parent (Prev);
18162 end if;
18164 if Nkind (N) = N_Full_Type_Declaration
18165 and then Nkind (Type_Definition (N)) in
18166 N_Record_Definition | N_Derived_Type_Definition
18167 and then Interface_Present (Type_Definition (N))
18168 then
18169 Error_Msg_N
18170 ("completion of private type cannot be an interface", N);
18171 end if;
18173 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18174 if Etype (Prev) /= Prev then
18176 -- Prev is a private subtype or a derived type, and needs
18177 -- no completion.
18179 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18180 New_Id := Id;
18182 elsif Ekind (Prev) = E_Private_Type
18183 and then Nkind (N) in N_Task_Type_Declaration
18184 | N_Protected_Type_Declaration
18185 then
18186 Error_Msg_N
18187 ("completion of nonlimited type cannot be limited", N);
18189 elsif Ekind (Prev) = E_Record_Type_With_Private
18190 and then Nkind (N) in N_Task_Type_Declaration
18191 | N_Protected_Type_Declaration
18192 then
18193 if not Is_Limited_Record (Prev) then
18194 Error_Msg_N
18195 ("completion of nonlimited type cannot be limited", N);
18197 elsif No (Interface_List (N)) then
18198 Error_Msg_N
18199 ("completion of tagged private type must be tagged",
18201 end if;
18202 end if;
18204 -- Ada 2005 (AI-251): Private extension declaration of a task
18205 -- type or a protected type. This case arises when covering
18206 -- interface types.
18208 elsif Nkind (N) in N_Task_Type_Declaration
18209 | N_Protected_Type_Declaration
18210 then
18211 null;
18213 elsif Nkind (N) /= N_Full_Type_Declaration
18214 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18215 then
18216 Error_Msg_N
18217 ("full view of private extension must be an extension", N);
18219 elsif not (Abstract_Present (Parent (Prev)))
18220 and then Abstract_Present (Type_Definition (N))
18221 then
18222 Error_Msg_N
18223 ("full view of non-abstract extension cannot be abstract", N);
18224 end if;
18226 if not In_Private_Part (Current_Scope) then
18227 Error_Msg_N
18228 ("declaration of full view must appear in private part", N);
18229 end if;
18231 if Ada_Version >= Ada_2012 then
18232 Check_Duplicate_Aspects;
18233 end if;
18235 Copy_And_Swap (Prev, Id);
18236 Set_Has_Private_Declaration (Prev);
18237 Set_Has_Private_Declaration (Id);
18239 -- AI12-0133: Indicate whether we have a partial view with
18240 -- unknown discriminants, in which case initialization of objects
18241 -- of the type do not receive an invariant check.
18243 Set_Partial_View_Has_Unknown_Discr
18244 (Prev, Has_Unknown_Discriminants (Id));
18246 -- Preserve aspect and iterator flags that may have been set on
18247 -- the partial view.
18249 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18250 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18252 -- If no error, propagate freeze_node from private to full view.
18253 -- It may have been generated for an early operational item.
18255 if Present (Freeze_Node (Id))
18256 and then Serious_Errors_Detected = 0
18257 and then No (Full_View (Id))
18258 then
18259 Set_Freeze_Node (Prev, Freeze_Node (Id));
18260 Set_Freeze_Node (Id, Empty);
18261 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18262 end if;
18264 Set_Full_View (Id, Prev);
18265 New_Id := Prev;
18266 end if;
18268 -- Verify that full declaration conforms to partial one
18270 if Is_Incomplete_Or_Private_Type (Prev)
18271 and then Present (Discriminant_Specifications (Prev_Par))
18272 then
18273 if Present (Discriminant_Specifications (N)) then
18274 if Ekind (Prev) = E_Incomplete_Type then
18275 Check_Discriminant_Conformance (N, Prev, Prev);
18276 else
18277 Check_Discriminant_Conformance (N, Prev, Id);
18278 end if;
18280 else
18281 Error_Msg_N
18282 ("missing discriminants in full type declaration", N);
18284 -- To avoid cascaded errors on subsequent use, share the
18285 -- discriminants of the partial view.
18287 Set_Discriminant_Specifications (N,
18288 Discriminant_Specifications (Prev_Par));
18289 end if;
18290 end if;
18292 -- A prior untagged partial view can have an associated class-wide
18293 -- type due to use of the class attribute, and in this case the full
18294 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18295 -- of incomplete tagged declarations, but we check for it.
18297 if Is_Type (Prev)
18298 and then (Is_Tagged_Type (Prev)
18299 or else Present (Class_Wide_Type (Prev)))
18300 then
18301 -- Ada 2012 (AI05-0162): A private type may be the completion of
18302 -- an incomplete type.
18304 if Ada_Version >= Ada_2012
18305 and then Is_Incomplete_Type (Prev)
18306 and then Nkind (N) in N_Private_Type_Declaration
18307 | N_Private_Extension_Declaration
18308 then
18309 -- No need to check private extensions since they are tagged
18311 if Nkind (N) = N_Private_Type_Declaration
18312 and then not Tagged_Present (N)
18313 then
18314 Tag_Mismatch;
18315 end if;
18317 -- The full declaration is either a tagged type (including
18318 -- a synchronized type that implements interfaces) or a
18319 -- type extension, otherwise this is an error.
18321 elsif Nkind (N) in N_Task_Type_Declaration
18322 | N_Protected_Type_Declaration
18323 then
18324 if No (Interface_List (N)) and then not Error_Posted (N) then
18325 Tag_Mismatch;
18326 end if;
18328 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18330 -- Indicate that the previous declaration (tagged incomplete
18331 -- or private declaration) requires the same on the full one.
18333 if not Tagged_Present (Type_Definition (N)) then
18334 Tag_Mismatch;
18335 Set_Is_Tagged_Type (Id);
18336 end if;
18338 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18339 if No (Record_Extension_Part (Type_Definition (N))) then
18340 Error_Msg_NE
18341 ("full declaration of } must be a record extension",
18342 Prev, Id);
18344 -- Set some attributes to produce a usable full view
18346 Set_Is_Tagged_Type (Id);
18347 end if;
18349 else
18350 Tag_Mismatch;
18351 end if;
18352 end if;
18354 if Present (Prev)
18355 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18356 and then Present (Premature_Use (Parent (Prev)))
18357 then
18358 Error_Msg_Sloc := Sloc (N);
18359 Error_Msg_N
18360 ("\full declaration #", Premature_Use (Parent (Prev)));
18361 end if;
18363 return New_Id;
18364 end if;
18365 end Find_Type_Name;
18367 -------------------------
18368 -- Find_Type_Of_Object --
18369 -------------------------
18371 function Find_Type_Of_Object
18372 (Obj_Def : Node_Id;
18373 Related_Nod : Node_Id) return Entity_Id
18375 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18376 P : Node_Id := Parent (Obj_Def);
18377 T : Entity_Id;
18378 Nam : Name_Id;
18380 begin
18381 -- If the parent is a component_definition node we climb to the
18382 -- component_declaration node.
18384 if Nkind (P) = N_Component_Definition then
18385 P := Parent (P);
18386 end if;
18388 -- Case of an anonymous array subtype
18390 if Def_Kind in N_Array_Type_Definition then
18391 T := Empty;
18392 Array_Type_Declaration (T, Obj_Def);
18394 -- Create an explicit subtype whenever possible
18396 elsif Nkind (P) /= N_Component_Declaration
18397 and then Def_Kind = N_Subtype_Indication
18398 then
18399 -- Base name of subtype on object name, which will be unique in
18400 -- the current scope.
18402 -- If this is a duplicate declaration, return base type, to avoid
18403 -- generating duplicate anonymous types.
18405 if Error_Posted (P) then
18406 Analyze (Subtype_Mark (Obj_Def));
18407 return Entity (Subtype_Mark (Obj_Def));
18408 end if;
18410 Nam :=
18411 New_External_Name
18412 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18414 T := Make_Defining_Identifier (Sloc (P), Nam);
18416 -- If In_Spec_Expression, for example within a pre/postcondition,
18417 -- provide enough information for use of the subtype without
18418 -- depending on full analysis and freezing, which will happen when
18419 -- building the corresponding subprogram.
18421 if In_Spec_Expression then
18422 Analyze (Subtype_Mark (Obj_Def));
18424 declare
18425 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18426 Decl : constant Node_Id :=
18427 Make_Subtype_Declaration (Sloc (P),
18428 Defining_Identifier => T,
18429 Subtype_Indication => Relocate_Node (Obj_Def));
18430 begin
18431 Set_Etype (T, Base_T);
18432 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18433 Set_Parent (T, Obj_Def);
18435 if Ekind (T) = E_Array_Subtype then
18436 Set_First_Index (T, First_Index (Base_T));
18437 Set_Is_Constrained (T);
18439 elsif Ekind (T) = E_Record_Subtype then
18440 Set_First_Entity (T, First_Entity (Base_T));
18441 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18442 Set_Is_Constrained (T);
18443 end if;
18445 Insert_Before (Related_Nod, Decl);
18446 end;
18448 return T;
18449 end if;
18451 -- When generating code, insert subtype declaration ahead of
18452 -- declaration that generated it.
18454 Insert_Action (Obj_Def,
18455 Make_Subtype_Declaration (Sloc (P),
18456 Defining_Identifier => T,
18457 Subtype_Indication => Relocate_Node (Obj_Def)));
18459 -- This subtype may need freezing, and this will not be done
18460 -- automatically if the object declaration is not in declarative
18461 -- part. Since this is an object declaration, the type cannot always
18462 -- be frozen here. Deferred constants do not freeze their type
18463 -- (which often enough will be private).
18465 if Nkind (P) = N_Object_Declaration
18466 and then Constant_Present (P)
18467 and then No (Expression (P))
18468 then
18469 null;
18471 -- Here we freeze the base type of object type to catch premature use
18472 -- of discriminated private type without a full view.
18474 else
18475 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18476 end if;
18478 -- Ada 2005 AI-406: the object definition in an object declaration
18479 -- can be an access definition.
18481 elsif Def_Kind = N_Access_Definition then
18482 T := Access_Definition (Related_Nod, Obj_Def);
18484 Set_Is_Local_Anonymous_Access
18485 (T, Ada_Version < Ada_2012
18486 or else Nkind (P) /= N_Object_Declaration
18487 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18489 -- Otherwise, the object definition is just a subtype_mark
18491 else
18492 T := Process_Subtype (Obj_Def, Related_Nod);
18493 end if;
18495 return T;
18496 end Find_Type_Of_Object;
18498 --------------------------------
18499 -- Find_Type_Of_Subtype_Indic --
18500 --------------------------------
18502 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18503 Typ : Entity_Id;
18505 begin
18506 -- Case of subtype mark with a constraint
18508 if Nkind (S) = N_Subtype_Indication then
18509 Find_Type (Subtype_Mark (S));
18510 Typ := Entity (Subtype_Mark (S));
18512 if not
18513 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18514 then
18515 Error_Msg_N
18516 ("incorrect constraint for this kind of type", Constraint (S));
18517 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18518 end if;
18520 -- Otherwise we have a subtype mark without a constraint
18522 elsif Error_Posted (S) then
18523 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18524 return Any_Type;
18526 else
18527 Find_Type (S);
18528 Typ := Entity (S);
18529 end if;
18531 return Typ;
18532 end Find_Type_Of_Subtype_Indic;
18534 -------------------------------------
18535 -- Floating_Point_Type_Declaration --
18536 -------------------------------------
18538 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18539 Digs : constant Node_Id := Digits_Expression (Def);
18540 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18541 Digs_Val : Uint;
18542 Base_Typ : Entity_Id;
18543 Implicit_Base : Entity_Id;
18545 function Can_Derive_From (E : Entity_Id) return Boolean;
18546 -- Find if given digits value, and possibly a specified range, allows
18547 -- derivation from specified type
18549 procedure Convert_Bound (B : Node_Id);
18550 -- If specified, the bounds must be static but may be of different
18551 -- types. They must be converted into machine numbers of the base type,
18552 -- in accordance with RM 4.9(38).
18554 function Find_Base_Type return Entity_Id;
18555 -- Find a predefined base type that Def can derive from, or generate
18556 -- an error and substitute Long_Long_Float if none exists.
18558 ---------------------
18559 -- Can_Derive_From --
18560 ---------------------
18562 function Can_Derive_From (E : Entity_Id) return Boolean is
18563 Spec : constant Entity_Id := Real_Range_Specification (Def);
18565 begin
18566 -- Check specified "digits" constraint
18568 if Digs_Val > Digits_Value (E) then
18569 return False;
18570 end if;
18572 -- Check for matching range, if specified
18574 if Present (Spec) then
18575 if Expr_Value_R (Type_Low_Bound (E)) >
18576 Expr_Value_R (Low_Bound (Spec))
18577 then
18578 return False;
18579 end if;
18581 if Expr_Value_R (Type_High_Bound (E)) <
18582 Expr_Value_R (High_Bound (Spec))
18583 then
18584 return False;
18585 end if;
18586 end if;
18588 return True;
18589 end Can_Derive_From;
18591 -------------------
18592 -- Convert_Bound --
18593 --------------------
18595 procedure Convert_Bound (B : Node_Id) is
18596 begin
18597 -- If the bound is not a literal it can only be static if it is
18598 -- a static constant, possibly of a specified type.
18600 if Is_Entity_Name (B)
18601 and then Ekind (Entity (B)) = E_Constant
18602 then
18603 Rewrite (B, Constant_Value (Entity (B)));
18604 end if;
18606 if Nkind (B) = N_Real_Literal then
18607 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18608 Set_Is_Machine_Number (B);
18609 Set_Etype (B, Base_Typ);
18610 end if;
18611 end Convert_Bound;
18613 --------------------
18614 -- Find_Base_Type --
18615 --------------------
18617 function Find_Base_Type return Entity_Id is
18618 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18620 begin
18621 -- Iterate over the predefined types in order, returning the first
18622 -- one that Def can derive from.
18624 while Present (Choice) loop
18625 if Can_Derive_From (Node (Choice)) then
18626 return Node (Choice);
18627 end if;
18629 Next_Elmt (Choice);
18630 end loop;
18632 -- If we can't derive from any existing type, use Long_Long_Float
18633 -- and give appropriate message explaining the problem.
18635 if Digs_Val > Max_Digs_Val then
18636 -- It might be the case that there is a type with the requested
18637 -- range, just not the combination of digits and range.
18639 Error_Msg_N
18640 ("no predefined type has requested range and precision",
18641 Real_Range_Specification (Def));
18643 else
18644 Error_Msg_N
18645 ("range too large for any predefined type",
18646 Real_Range_Specification (Def));
18647 end if;
18649 return Standard_Long_Long_Float;
18650 end Find_Base_Type;
18652 -- Start of processing for Floating_Point_Type_Declaration
18654 begin
18655 Check_Restriction (No_Floating_Point, Def);
18657 -- Create an implicit base type
18659 Implicit_Base :=
18660 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18662 -- Analyze and verify digits value
18664 Analyze_And_Resolve (Digs, Any_Integer);
18665 Check_Digits_Expression (Digs);
18666 Digs_Val := Expr_Value (Digs);
18668 -- Process possible range spec and find correct type to derive from
18670 Process_Real_Range_Specification (Def);
18672 -- Check that requested number of digits is not too high.
18674 if Digs_Val > Max_Digs_Val then
18676 -- The check for Max_Base_Digits may be somewhat expensive, as it
18677 -- requires reading System, so only do it when necessary.
18679 declare
18680 Max_Base_Digits : constant Uint :=
18681 Expr_Value
18682 (Expression
18683 (Parent (RTE (RE_Max_Base_Digits))));
18685 begin
18686 if Digs_Val > Max_Base_Digits then
18687 Error_Msg_Uint_1 := Max_Base_Digits;
18688 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18690 elsif No (Real_Range_Specification (Def)) then
18691 Error_Msg_Uint_1 := Max_Digs_Val;
18692 Error_Msg_N ("types with more than ^ digits need range spec "
18693 & "(RM 3.5.7(6))", Digs);
18694 end if;
18695 end;
18696 end if;
18698 -- Find a suitable type to derive from or complain and use a substitute
18700 Base_Typ := Find_Base_Type;
18702 -- If there are bounds given in the declaration use them as the bounds
18703 -- of the type, otherwise use the bounds of the predefined base type
18704 -- that was chosen based on the Digits value.
18706 if Present (Real_Range_Specification (Def)) then
18707 Set_Scalar_Range (T, Real_Range_Specification (Def));
18708 Set_Is_Constrained (T);
18710 Convert_Bound (Type_Low_Bound (T));
18711 Convert_Bound (Type_High_Bound (T));
18713 else
18714 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18715 end if;
18717 -- Complete definition of implicit base and declared first subtype. The
18718 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18719 -- are not clobbered when the floating point type acts as a full view of
18720 -- a private type.
18722 Set_Etype (Implicit_Base, Base_Typ);
18723 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18724 Set_Size_Info (Implicit_Base, Base_Typ);
18725 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18726 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18727 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18728 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18730 Mutate_Ekind (T, E_Floating_Point_Subtype);
18731 Set_Etype (T, Implicit_Base);
18732 Set_Size_Info (T, Implicit_Base);
18733 Set_RM_Size (T, RM_Size (Implicit_Base));
18734 Inherit_Rep_Item_Chain (T, Implicit_Base);
18736 if Digs_Val >= Uint_1 then
18737 Set_Digits_Value (T, Digs_Val);
18738 else
18739 pragma Assert (Serious_Errors_Detected > 0); null;
18740 end if;
18741 end Floating_Point_Type_Declaration;
18743 ----------------------------
18744 -- Get_Discriminant_Value --
18745 ----------------------------
18747 -- This is the situation:
18749 -- There is a non-derived type
18751 -- type T0 (Dx, Dy, Dz...)
18753 -- There are zero or more levels of derivation, with each derivation
18754 -- either purely inheriting the discriminants, or defining its own.
18756 -- type Ti is new Ti-1
18757 -- or
18758 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18759 -- or
18760 -- subtype Ti is ...
18762 -- The subtype issue is avoided by the use of Original_Record_Component,
18763 -- and the fact that derived subtypes also derive the constraints.
18765 -- This chain leads back from
18767 -- Typ_For_Constraint
18769 -- Typ_For_Constraint has discriminants, and the value for each
18770 -- discriminant is given by its corresponding Elmt of Constraints.
18772 -- Discriminant is some discriminant in this hierarchy
18774 -- We need to return its value
18776 -- We do this by recursively searching each level, and looking for
18777 -- Discriminant. Once we get to the bottom, we start backing up
18778 -- returning the value for it which may in turn be a discriminant
18779 -- further up, so on the backup we continue the substitution.
18781 function Get_Discriminant_Value
18782 (Discriminant : Entity_Id;
18783 Typ_For_Constraint : Entity_Id;
18784 Constraint : Elist_Id) return Node_Id
18786 function Root_Corresponding_Discriminant
18787 (Discr : Entity_Id) return Entity_Id;
18788 -- Given a discriminant, traverse the chain of inherited discriminants
18789 -- and return the topmost discriminant.
18791 function Search_Derivation_Levels
18792 (Ti : Entity_Id;
18793 Discrim_Values : Elist_Id;
18794 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18795 -- This is the routine that performs the recursive search of levels
18796 -- as described above.
18798 -------------------------------------
18799 -- Root_Corresponding_Discriminant --
18800 -------------------------------------
18802 function Root_Corresponding_Discriminant
18803 (Discr : Entity_Id) return Entity_Id
18805 D : Entity_Id;
18807 begin
18808 D := Discr;
18809 while Present (Corresponding_Discriminant (D)) loop
18810 D := Corresponding_Discriminant (D);
18811 end loop;
18813 return D;
18814 end Root_Corresponding_Discriminant;
18816 ------------------------------
18817 -- Search_Derivation_Levels --
18818 ------------------------------
18820 function Search_Derivation_Levels
18821 (Ti : Entity_Id;
18822 Discrim_Values : Elist_Id;
18823 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18825 Assoc : Elmt_Id;
18826 Disc : Entity_Id;
18827 Result : Node_Or_Entity_Id;
18828 Result_Entity : Node_Id;
18830 begin
18831 -- If inappropriate type, return Error, this happens only in
18832 -- cascaded error situations, and we want to avoid a blow up.
18834 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18835 return Error;
18836 end if;
18838 -- Look deeper if possible. Use Stored_Constraints only for
18839 -- untagged types. For tagged types use the given constraint.
18840 -- This asymmetry needs explanation???
18842 if not Stored_Discrim_Values
18843 and then Present (Stored_Constraint (Ti))
18844 and then not Is_Tagged_Type (Ti)
18845 then
18846 Result :=
18847 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18849 else
18850 declare
18851 Td : Entity_Id := Etype (Ti);
18853 begin
18854 -- If the parent type is private, the full view may include
18855 -- renamed discriminants, and it is those stored values that
18856 -- may be needed (the partial view never has more information
18857 -- than the full view).
18859 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18860 Td := Full_View (Td);
18861 end if;
18863 if Td = Ti then
18864 Result := Discriminant;
18866 else
18867 if Present (Stored_Constraint (Ti)) then
18868 Result :=
18869 Search_Derivation_Levels
18870 (Td, Stored_Constraint (Ti), True);
18871 else
18872 Result :=
18873 Search_Derivation_Levels
18874 (Td, Discrim_Values, Stored_Discrim_Values);
18875 end if;
18876 end if;
18877 end;
18878 end if;
18880 -- Extra underlying places to search, if not found above. For
18881 -- concurrent types, the relevant discriminant appears in the
18882 -- corresponding record. For a type derived from a private type
18883 -- without discriminant, the full view inherits the discriminants
18884 -- of the full view of the parent.
18886 if Result = Discriminant then
18887 if Is_Concurrent_Type (Ti)
18888 and then Present (Corresponding_Record_Type (Ti))
18889 then
18890 Result :=
18891 Search_Derivation_Levels (
18892 Corresponding_Record_Type (Ti),
18893 Discrim_Values,
18894 Stored_Discrim_Values);
18896 elsif Is_Private_Type (Ti)
18897 and then not Has_Discriminants (Ti)
18898 and then Present (Full_View (Ti))
18899 and then Etype (Full_View (Ti)) /= Ti
18900 then
18901 Result :=
18902 Search_Derivation_Levels (
18903 Full_View (Ti),
18904 Discrim_Values,
18905 Stored_Discrim_Values);
18906 end if;
18907 end if;
18909 -- If Result is not a (reference to a) discriminant, return it,
18910 -- otherwise set Result_Entity to the discriminant.
18912 if Nkind (Result) = N_Defining_Identifier then
18913 pragma Assert (Result = Discriminant);
18914 Result_Entity := Result;
18916 else
18917 if not Denotes_Discriminant (Result) then
18918 return Result;
18919 end if;
18921 Result_Entity := Entity (Result);
18922 end if;
18924 -- See if this level of derivation actually has discriminants because
18925 -- tagged derivations can add them, hence the lower levels need not
18926 -- have any.
18928 if not Has_Discriminants (Ti) then
18929 return Result;
18930 end if;
18932 -- Scan Ti's discriminants for Result_Entity, and return its
18933 -- corresponding value, if any.
18935 Result_Entity := Original_Record_Component (Result_Entity);
18937 Assoc := First_Elmt (Discrim_Values);
18939 if Stored_Discrim_Values then
18940 Disc := First_Stored_Discriminant (Ti);
18941 else
18942 Disc := First_Discriminant (Ti);
18943 end if;
18945 while Present (Disc) loop
18947 -- If no further associations return the discriminant, value will
18948 -- be found on the second pass.
18950 if No (Assoc) then
18951 return Result;
18952 end if;
18954 if Original_Record_Component (Disc) = Result_Entity then
18955 return Node (Assoc);
18956 end if;
18958 Next_Elmt (Assoc);
18960 if Stored_Discrim_Values then
18961 Next_Stored_Discriminant (Disc);
18962 else
18963 Next_Discriminant (Disc);
18964 end if;
18965 end loop;
18967 -- Could not find it
18969 return Result;
18970 end Search_Derivation_Levels;
18972 -- Local Variables
18974 Result : Node_Or_Entity_Id;
18976 -- Start of processing for Get_Discriminant_Value
18978 begin
18979 -- ??? This routine is a gigantic mess and will be deleted. For the
18980 -- time being just test for the trivial case before calling recurse.
18982 -- We are now celebrating the 20th anniversary of this comment!
18984 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18985 declare
18986 D : Entity_Id;
18987 E : Elmt_Id;
18989 begin
18990 D := First_Discriminant (Typ_For_Constraint);
18991 E := First_Elmt (Constraint);
18992 while Present (D) loop
18993 if Chars (D) = Chars (Discriminant) then
18994 return Node (E);
18995 end if;
18997 Next_Discriminant (D);
18998 Next_Elmt (E);
18999 end loop;
19000 end;
19001 end if;
19003 Result := Search_Derivation_Levels
19004 (Typ_For_Constraint, Constraint, False);
19006 -- ??? hack to disappear when this routine is gone
19008 if Nkind (Result) = N_Defining_Identifier then
19009 declare
19010 D : Entity_Id;
19011 E : Elmt_Id;
19013 begin
19014 D := First_Discriminant (Typ_For_Constraint);
19015 E := First_Elmt (Constraint);
19016 while Present (D) loop
19017 if Root_Corresponding_Discriminant (D) = Discriminant then
19018 return Node (E);
19019 end if;
19021 Next_Discriminant (D);
19022 Next_Elmt (E);
19023 end loop;
19024 end;
19025 end if;
19027 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19028 return Result;
19029 end Get_Discriminant_Value;
19031 --------------------------
19032 -- Has_Range_Constraint --
19033 --------------------------
19035 function Has_Range_Constraint (N : Node_Id) return Boolean is
19036 C : constant Node_Id := Constraint (N);
19038 begin
19039 if Nkind (C) = N_Range_Constraint then
19040 return True;
19042 elsif Nkind (C) = N_Digits_Constraint then
19043 return
19044 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19045 or else Present (Range_Constraint (C));
19047 elsif Nkind (C) = N_Delta_Constraint then
19048 return Present (Range_Constraint (C));
19050 else
19051 return False;
19052 end if;
19053 end Has_Range_Constraint;
19055 ------------------------
19056 -- Inherit_Components --
19057 ------------------------
19059 function Inherit_Components
19060 (N : Node_Id;
19061 Parent_Base : Entity_Id;
19062 Derived_Base : Entity_Id;
19063 Is_Tagged : Boolean;
19064 Inherit_Discr : Boolean;
19065 Discs : Elist_Id) return Elist_Id
19067 Assoc_List : constant Elist_Id := New_Elmt_List;
19069 procedure Inherit_Component
19070 (Old_C : Entity_Id;
19071 Plain_Discrim : Boolean := False;
19072 Stored_Discrim : Boolean := False);
19073 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19074 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19075 -- True, Old_C is a stored discriminant. If they are both false then
19076 -- Old_C is a regular component.
19078 -----------------------
19079 -- Inherit_Component --
19080 -----------------------
19082 procedure Inherit_Component
19083 (Old_C : Entity_Id;
19084 Plain_Discrim : Boolean := False;
19085 Stored_Discrim : Boolean := False)
19087 procedure Set_Anonymous_Type (Id : Entity_Id);
19088 -- Id denotes the entity of an access discriminant or anonymous
19089 -- access component. Set the type of Id to either the same type of
19090 -- Old_C or create a new one depending on whether the parent and
19091 -- the child types are in the same scope.
19093 ------------------------
19094 -- Set_Anonymous_Type --
19095 ------------------------
19097 procedure Set_Anonymous_Type (Id : Entity_Id) is
19098 Old_Typ : constant Entity_Id := Etype (Old_C);
19100 begin
19101 if Scope (Parent_Base) = Scope (Derived_Base) then
19102 Set_Etype (Id, Old_Typ);
19104 -- The parent and the derived type are in two different scopes.
19105 -- Reuse the type of the original discriminant / component by
19106 -- copying it in order to preserve all attributes.
19108 else
19109 declare
19110 Typ : constant Entity_Id := New_Copy (Old_Typ);
19112 begin
19113 Set_Etype (Id, Typ);
19115 -- Since we do not generate component declarations for
19116 -- inherited components, associate the itype with the
19117 -- derived type.
19119 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19120 Set_Scope (Typ, Derived_Base);
19121 end;
19122 end if;
19123 end Set_Anonymous_Type;
19125 -- Local variables and constants
19127 New_C : constant Entity_Id := New_Copy (Old_C);
19129 Corr_Discrim : Entity_Id;
19130 Discrim : Entity_Id;
19132 -- Start of processing for Inherit_Component
19134 begin
19135 pragma Assert (not Is_Tagged or not Stored_Discrim);
19137 Set_Parent (New_C, Parent (Old_C));
19139 -- Regular discriminants and components must be inserted in the scope
19140 -- of the Derived_Base. Do it here.
19142 if not Stored_Discrim then
19143 Enter_Name (New_C);
19144 end if;
19146 -- For tagged types the Original_Record_Component must point to
19147 -- whatever this field was pointing to in the parent type. This has
19148 -- already been achieved by the call to New_Copy above.
19150 if not Is_Tagged then
19151 Set_Original_Record_Component (New_C, New_C);
19152 Set_Corresponding_Record_Component (New_C, Old_C);
19153 end if;
19155 -- Set the proper type of an access discriminant
19157 if Ekind (New_C) = E_Discriminant
19158 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19159 then
19160 Set_Anonymous_Type (New_C);
19161 end if;
19163 -- If we have inherited a component then see if its Etype contains
19164 -- references to Parent_Base discriminants. In this case, replace
19165 -- these references with the constraints given in Discs. We do not
19166 -- do this for the partial view of private types because this is
19167 -- not needed (only the components of the full view will be used
19168 -- for code generation) and cause problem. We also avoid this
19169 -- transformation in some error situations.
19171 if Ekind (New_C) = E_Component then
19173 -- Set the proper type of an anonymous access component
19175 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19176 Set_Anonymous_Type (New_C);
19178 elsif (Is_Private_Type (Derived_Base)
19179 and then not Is_Generic_Type (Derived_Base))
19180 or else (Is_Empty_Elmt_List (Discs)
19181 and then not Expander_Active)
19182 then
19183 Set_Etype (New_C, Etype (Old_C));
19185 else
19186 -- The current component introduces a circularity of the
19187 -- following kind:
19189 -- limited with Pack_2;
19190 -- package Pack_1 is
19191 -- type T_1 is tagged record
19192 -- Comp : access Pack_2.T_2;
19193 -- ...
19194 -- end record;
19195 -- end Pack_1;
19197 -- with Pack_1;
19198 -- package Pack_2 is
19199 -- type T_2 is new Pack_1.T_1 with ...;
19200 -- end Pack_2;
19202 Set_Etype
19203 (New_C,
19204 Constrain_Component_Type
19205 (Old_C, Derived_Base, N, Parent_Base, Discs));
19206 end if;
19207 end if;
19209 -- In derived tagged types it is illegal to reference a non
19210 -- discriminant component in the parent type. To catch this, mark
19211 -- these components with an Ekind of E_Void. This will be reset in
19212 -- Record_Type_Definition after processing the record extension of
19213 -- the derived type.
19215 -- If the declaration is a private extension, there is no further
19216 -- record extension to process, and the components retain their
19217 -- current kind, because they are visible at this point.
19219 if Is_Tagged and then Ekind (New_C) = E_Component
19220 and then Nkind (N) /= N_Private_Extension_Declaration
19221 then
19222 Mutate_Ekind (New_C, E_Void);
19223 end if;
19225 if Plain_Discrim then
19226 Set_Corresponding_Discriminant (New_C, Old_C);
19227 Build_Discriminal (New_C);
19229 -- If we are explicitly inheriting a stored discriminant it will be
19230 -- completely hidden.
19232 elsif Stored_Discrim then
19233 Set_Corresponding_Discriminant (New_C, Empty);
19234 Set_Discriminal (New_C, Empty);
19235 Set_Is_Completely_Hidden (New_C);
19237 -- Set the Original_Record_Component of each discriminant in the
19238 -- derived base to point to the corresponding stored that we just
19239 -- created.
19241 Discrim := First_Discriminant (Derived_Base);
19242 while Present (Discrim) loop
19243 Corr_Discrim := Corresponding_Discriminant (Discrim);
19245 -- Corr_Discrim could be missing in an error situation
19247 if Present (Corr_Discrim)
19248 and then Original_Record_Component (Corr_Discrim) = Old_C
19249 then
19250 Set_Original_Record_Component (Discrim, New_C);
19251 Set_Corresponding_Record_Component (Discrim, Empty);
19252 end if;
19254 Next_Discriminant (Discrim);
19255 end loop;
19257 Append_Entity (New_C, Derived_Base);
19258 end if;
19260 if not Is_Tagged then
19261 Append_Elmt (Old_C, Assoc_List);
19262 Append_Elmt (New_C, Assoc_List);
19263 end if;
19264 end Inherit_Component;
19266 -- Variables local to Inherit_Component
19268 Loc : constant Source_Ptr := Sloc (N);
19270 Parent_Discrim : Entity_Id;
19271 Stored_Discrim : Entity_Id;
19272 D : Entity_Id;
19273 Component : Entity_Id;
19275 -- Start of processing for Inherit_Components
19277 begin
19278 if not Is_Tagged then
19279 Append_Elmt (Parent_Base, Assoc_List);
19280 Append_Elmt (Derived_Base, Assoc_List);
19281 end if;
19283 -- Inherit parent discriminants if needed
19285 if Inherit_Discr then
19286 Parent_Discrim := First_Discriminant (Parent_Base);
19287 while Present (Parent_Discrim) loop
19288 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19289 Next_Discriminant (Parent_Discrim);
19290 end loop;
19291 end if;
19293 -- Create explicit stored discrims for untagged types when necessary
19295 if not Has_Unknown_Discriminants (Derived_Base)
19296 and then Has_Discriminants (Parent_Base)
19297 and then not Is_Tagged
19298 and then
19299 (not Inherit_Discr
19300 or else First_Discriminant (Parent_Base) /=
19301 First_Stored_Discriminant (Parent_Base))
19302 then
19303 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19304 while Present (Stored_Discrim) loop
19305 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19306 Next_Stored_Discriminant (Stored_Discrim);
19307 end loop;
19308 end if;
19310 -- See if we can apply the second transformation for derived types, as
19311 -- explained in point 6. in the comments above Build_Derived_Record_Type
19312 -- This is achieved by appending Derived_Base discriminants into Discs,
19313 -- which has the side effect of returning a non empty Discs list to the
19314 -- caller of Inherit_Components, which is what we want. This must be
19315 -- done for private derived types if there are explicit stored
19316 -- discriminants, to ensure that we can retrieve the values of the
19317 -- constraints provided in the ancestors.
19319 if Inherit_Discr
19320 and then Is_Empty_Elmt_List (Discs)
19321 and then Present (First_Discriminant (Derived_Base))
19322 and then
19323 (not Is_Private_Type (Derived_Base)
19324 or else Is_Completely_Hidden
19325 (First_Stored_Discriminant (Derived_Base))
19326 or else Is_Generic_Type (Derived_Base))
19327 then
19328 D := First_Discriminant (Derived_Base);
19329 while Present (D) loop
19330 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19331 Next_Discriminant (D);
19332 end loop;
19333 end if;
19335 -- Finally, inherit non-discriminant components unless they are not
19336 -- visible because defined or inherited from the full view of the
19337 -- parent. Don't inherit the _parent field of the parent type.
19339 Component := First_Entity (Parent_Base);
19340 while Present (Component) loop
19342 -- Ada 2005 (AI-251): Do not inherit components associated with
19343 -- secondary tags of the parent.
19345 if Ekind (Component) = E_Component
19346 and then Present (Related_Type (Component))
19347 then
19348 null;
19350 elsif Ekind (Component) /= E_Component
19351 or else Chars (Component) = Name_uParent
19352 then
19353 null;
19355 -- If the derived type is within the parent type's declarative
19356 -- region, then the components can still be inherited even though
19357 -- they aren't visible at this point. This can occur for cases
19358 -- such as within public child units where the components must
19359 -- become visible upon entering the child unit's private part.
19361 elsif not Is_Visible_Component (Component)
19362 and then not In_Open_Scopes (Scope (Parent_Base))
19363 then
19364 null;
19366 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19367 then
19368 null;
19370 else
19371 Inherit_Component (Component);
19372 end if;
19374 Next_Entity (Component);
19375 end loop;
19377 -- For tagged derived types, inherited discriminants cannot be used in
19378 -- component declarations of the record extension part. To achieve this
19379 -- we mark the inherited discriminants as not visible.
19381 if Is_Tagged and then Inherit_Discr then
19382 D := First_Discriminant (Derived_Base);
19383 while Present (D) loop
19384 Set_Is_Immediately_Visible (D, False);
19385 Next_Discriminant (D);
19386 end loop;
19387 end if;
19389 return Assoc_List;
19390 end Inherit_Components;
19392 ----------------------
19393 -- Is_EVF_Procedure --
19394 ----------------------
19396 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19397 Formal : Entity_Id;
19399 begin
19400 -- Examine the formals of an Extensions_Visible False procedure looking
19401 -- for a controlling OUT parameter.
19403 if Ekind (Subp) = E_Procedure
19404 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19405 then
19406 Formal := First_Formal (Subp);
19407 while Present (Formal) loop
19408 if Ekind (Formal) = E_Out_Parameter
19409 and then Is_Controlling_Formal (Formal)
19410 then
19411 return True;
19412 end if;
19414 Next_Formal (Formal);
19415 end loop;
19416 end if;
19418 return False;
19419 end Is_EVF_Procedure;
19421 --------------------------
19422 -- Is_Private_Primitive --
19423 --------------------------
19425 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19426 Prim_Scope : constant Entity_Id := Scope (Prim);
19427 Priv_Entity : Entity_Id;
19428 begin
19429 if Is_Package_Or_Generic_Package (Prim_Scope) then
19430 Priv_Entity := First_Private_Entity (Prim_Scope);
19432 while Present (Priv_Entity) loop
19433 if Priv_Entity = Prim then
19434 return True;
19435 end if;
19437 Next_Entity (Priv_Entity);
19438 end loop;
19439 end if;
19441 return False;
19442 end Is_Private_Primitive;
19444 ------------------------------
19445 -- Is_Valid_Constraint_Kind --
19446 ------------------------------
19448 function Is_Valid_Constraint_Kind
19449 (T_Kind : Type_Kind;
19450 Constraint_Kind : Node_Kind) return Boolean
19452 begin
19453 case T_Kind is
19454 when Enumeration_Kind
19455 | Integer_Kind
19457 return Constraint_Kind = N_Range_Constraint;
19459 when Decimal_Fixed_Point_Kind =>
19460 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19462 when Ordinary_Fixed_Point_Kind =>
19463 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19465 when Float_Kind =>
19466 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19468 when Access_Kind
19469 | Array_Kind
19470 | Class_Wide_Kind
19471 | Concurrent_Kind
19472 | Private_Kind
19473 | E_Incomplete_Type
19474 | E_Record_Subtype
19475 | E_Record_Type
19477 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19479 when others =>
19480 return True; -- Error will be detected later
19481 end case;
19482 end Is_Valid_Constraint_Kind;
19484 --------------------------
19485 -- Is_Visible_Component --
19486 --------------------------
19488 function Is_Visible_Component
19489 (C : Entity_Id;
19490 N : Node_Id := Empty) return Boolean
19492 Original_Comp : Entity_Id := Empty;
19493 Original_Type : Entity_Id;
19494 Type_Scope : Entity_Id;
19496 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19497 -- Check whether parent type of inherited component is declared locally,
19498 -- possibly within a nested package or instance. The current scope is
19499 -- the derived record itself.
19501 -------------------
19502 -- Is_Local_Type --
19503 -------------------
19505 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19506 begin
19507 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19508 end Is_Local_Type;
19510 -- Start of processing for Is_Visible_Component
19512 begin
19513 if Ekind (C) in E_Component | E_Discriminant then
19514 Original_Comp := Original_Record_Component (C);
19515 end if;
19517 if No (Original_Comp) then
19519 -- Premature usage, or previous error
19521 return False;
19523 else
19524 Original_Type := Scope (Original_Comp);
19525 Type_Scope := Scope (Base_Type (Scope (C)));
19526 end if;
19528 -- This test only concerns tagged types
19530 if not Is_Tagged_Type (Original_Type) then
19532 -- Check if this is a renamed discriminant (hidden either by the
19533 -- derived type or by some ancestor), unless we are analyzing code
19534 -- generated by the expander since it may reference such components
19535 -- (for example see the expansion of Deep_Adjust).
19537 if Ekind (C) = E_Discriminant and then Present (N) then
19538 return
19539 not Comes_From_Source (N)
19540 or else not Is_Completely_Hidden (C);
19541 else
19542 return True;
19543 end if;
19545 -- If it is _Parent or _Tag, there is no visibility issue
19547 elsif not Comes_From_Source (Original_Comp) then
19548 return True;
19550 -- Discriminants are visible unless the (private) type has unknown
19551 -- discriminants. If the discriminant reference is inserted for a
19552 -- discriminant check on a full view it is also visible.
19554 elsif Ekind (Original_Comp) = E_Discriminant
19555 and then
19556 (not Has_Unknown_Discriminants (Original_Type)
19557 or else (Present (N)
19558 and then Nkind (N) = N_Selected_Component
19559 and then Nkind (Prefix (N)) = N_Type_Conversion
19560 and then not Comes_From_Source (Prefix (N))))
19561 then
19562 return True;
19564 -- If the component has been declared in an ancestor which is currently
19565 -- a private type, then it is not visible. The same applies if the
19566 -- component's containing type is not in an open scope and the original
19567 -- component's enclosing type is a visible full view of a private type
19568 -- (which can occur in cases where an attempt is being made to reference
19569 -- a component in a sibling package that is inherited from a visible
19570 -- component of a type in an ancestor package; the component in the
19571 -- sibling package should not be visible even though the component it
19572 -- inherited from is visible), but instance bodies are not subject to
19573 -- this second case since they have the Has_Private_View mechanism to
19574 -- ensure proper visibility. This does not apply however in the case
19575 -- where the scope of the type is a private child unit, or when the
19576 -- parent comes from a local package in which the ancestor is currently
19577 -- visible. The latter suppression of visibility is needed for cases
19578 -- that are tested in B730006.
19580 elsif Is_Private_Type (Original_Type)
19581 or else
19582 (not Is_Private_Descendant (Type_Scope)
19583 and then not In_Open_Scopes (Type_Scope)
19584 and then Has_Private_Declaration (Original_Type)
19585 and then not In_Instance_Body)
19586 then
19587 -- If the type derives from an entity in a formal package, there
19588 -- are no additional visible components.
19590 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19591 N_Formal_Package_Declaration
19592 then
19593 return False;
19595 -- if we are not in the private part of the current package, there
19596 -- are no additional visible components.
19598 elsif Ekind (Scope (Current_Scope)) = E_Package
19599 and then not In_Private_Part (Scope (Current_Scope))
19600 then
19601 return False;
19602 else
19603 return
19604 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19605 and then In_Open_Scopes (Scope (Original_Type))
19606 and then Is_Local_Type (Type_Scope);
19607 end if;
19609 -- There is another weird way in which a component may be invisible when
19610 -- the private and the full view are not derived from the same ancestor.
19611 -- Here is an example :
19613 -- type A1 is tagged record F1 : integer; end record;
19614 -- type A2 is new A1 with record F2 : integer; end record;
19615 -- type T is new A1 with private;
19616 -- private
19617 -- type T is new A2 with null record;
19619 -- In this case, the full view of T inherits F1 and F2 but the private
19620 -- view inherits only F1
19622 else
19623 declare
19624 Ancestor : Entity_Id := Scope (C);
19626 begin
19627 loop
19628 if Ancestor = Original_Type then
19629 return True;
19631 -- The ancestor may have a partial view of the original type,
19632 -- but if the full view is in scope, as in a child body, the
19633 -- component is visible.
19635 elsif In_Private_Part (Scope (Original_Type))
19636 and then Full_View (Ancestor) = Original_Type
19637 then
19638 return True;
19640 elsif Ancestor = Etype (Ancestor) then
19642 -- No further ancestors to examine
19644 return False;
19645 end if;
19647 Ancestor := Etype (Ancestor);
19648 end loop;
19649 end;
19650 end if;
19651 end Is_Visible_Component;
19653 --------------------------
19654 -- Make_Class_Wide_Type --
19655 --------------------------
19657 procedure Make_Class_Wide_Type (T : Entity_Id) is
19658 CW_Type : Entity_Id;
19659 CW_Name : Name_Id;
19660 Next_E : Entity_Id;
19661 Prev_E : Entity_Id;
19663 begin
19664 if Present (Class_Wide_Type (T)) then
19666 -- The class-wide type is a partially decorated entity created for a
19667 -- unanalyzed tagged type referenced through a limited with clause.
19668 -- When the tagged type is analyzed, its class-wide type needs to be
19669 -- redecorated. Note that we reuse the entity created by Decorate_
19670 -- Tagged_Type in order to preserve all links.
19672 if Materialize_Entity (Class_Wide_Type (T)) then
19673 CW_Type := Class_Wide_Type (T);
19674 Set_Materialize_Entity (CW_Type, False);
19676 -- The class wide type can have been defined by the partial view, in
19677 -- which case everything is already done.
19679 else
19680 return;
19681 end if;
19683 -- Default case, we need to create a new class-wide type
19685 else
19686 CW_Type :=
19687 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19688 end if;
19690 -- Inherit root type characteristics
19692 CW_Name := Chars (CW_Type);
19693 Next_E := Next_Entity (CW_Type);
19694 Prev_E := Prev_Entity (CW_Type);
19695 Copy_Node (T, CW_Type);
19696 Set_Comes_From_Source (CW_Type, False);
19697 Set_Chars (CW_Type, CW_Name);
19698 Set_Parent (CW_Type, Parent (T));
19699 Set_Prev_Entity (CW_Type, Prev_E);
19700 Set_Next_Entity (CW_Type, Next_E);
19702 -- Ensure we have a new freeze node for the class-wide type. The partial
19703 -- view may have freeze action of its own, requiring a proper freeze
19704 -- node, and the same freeze node cannot be shared between the two
19705 -- types.
19707 Set_Has_Delayed_Freeze (CW_Type);
19708 Set_Freeze_Node (CW_Type, Empty);
19710 -- Customize the class-wide type: It has no prim. op., it cannot be
19711 -- abstract, its Etype points back to the specific root type, and it
19712 -- cannot have any invariants.
19714 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19715 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19717 elsif Ekind (CW_Type) in Concurrent_Kind then
19718 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19719 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19721 if Ekind (CW_Type) in Task_Kind then
19722 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19723 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19724 end if;
19726 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19727 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19728 end if;
19729 end if;
19731 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19732 Set_Is_Tagged_Type (CW_Type, True);
19733 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19734 Set_Is_Abstract_Type (CW_Type, False);
19735 Set_Is_Constrained (CW_Type, False);
19736 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19737 Set_Default_SSO (CW_Type);
19738 Set_Has_Inheritable_Invariants (CW_Type, False);
19739 Set_Has_Inherited_Invariants (CW_Type, False);
19740 Set_Has_Own_Invariants (CW_Type, False);
19742 if Ekind (T) = E_Class_Wide_Subtype then
19743 Set_Etype (CW_Type, Etype (Base_Type (T)));
19744 else
19745 Set_Etype (CW_Type, T);
19746 end if;
19748 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19750 -- If this is the class_wide type of a constrained subtype, it does
19751 -- not have discriminants.
19753 Set_Has_Discriminants (CW_Type,
19754 Has_Discriminants (T) and then not Is_Constrained (T));
19756 Set_Has_Unknown_Discriminants (CW_Type, True);
19757 Set_Class_Wide_Type (T, CW_Type);
19758 Set_Equivalent_Type (CW_Type, Empty);
19760 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19762 Set_Class_Wide_Type (CW_Type, CW_Type);
19763 end Make_Class_Wide_Type;
19765 ----------------
19766 -- Make_Index --
19767 ----------------
19769 procedure Make_Index
19770 (N : Node_Id;
19771 Related_Nod : Node_Id;
19772 Related_Id : Entity_Id := Empty;
19773 Suffix_Index : Pos := 1)
19775 R : Node_Id;
19776 T : Entity_Id;
19777 Def_Id : Entity_Id := Empty;
19778 Found : Boolean := False;
19780 begin
19781 -- For a discrete range used in a constrained array definition and
19782 -- defined by a range, an implicit conversion to the predefined type
19783 -- INTEGER is assumed if each bound is either a numeric literal, a named
19784 -- number, or an attribute, and the type of both bounds (prior to the
19785 -- implicit conversion) is the type universal_integer. Otherwise, both
19786 -- bounds must be of the same discrete type, other than universal
19787 -- integer; this type must be determinable independently of the
19788 -- context, but using the fact that the type must be discrete and that
19789 -- both bounds must have the same type.
19791 -- Character literals also have a universal type in the absence of
19792 -- of additional context, and are resolved to Standard_Character.
19794 if Nkind (N) = N_Range then
19796 -- The index is given by a range constraint. The bounds are known
19797 -- to be of a consistent type.
19799 if not Is_Overloaded (N) then
19800 T := Etype (N);
19802 -- For universal bounds, choose the specific predefined type
19804 if T = Universal_Integer then
19805 T := Standard_Integer;
19807 elsif T = Any_Character then
19808 Ambiguous_Character (Low_Bound (N));
19810 T := Standard_Character;
19811 end if;
19813 -- The node may be overloaded because some user-defined operators
19814 -- are available, but if a universal interpretation exists it is
19815 -- also the selected one.
19817 elsif Universal_Interpretation (N) = Universal_Integer then
19818 T := Standard_Integer;
19820 else
19821 T := Any_Type;
19823 declare
19824 Ind : Interp_Index;
19825 It : Interp;
19827 begin
19828 Get_First_Interp (N, Ind, It);
19829 while Present (It.Typ) loop
19830 if Is_Discrete_Type (It.Typ) then
19832 if Found
19833 and then not Covers (It.Typ, T)
19834 and then not Covers (T, It.Typ)
19835 then
19836 Error_Msg_N ("ambiguous bounds in discrete range", N);
19837 exit;
19838 else
19839 T := It.Typ;
19840 Found := True;
19841 end if;
19842 end if;
19844 Get_Next_Interp (Ind, It);
19845 end loop;
19847 if T = Any_Type then
19848 Error_Msg_N ("discrete type required for range", N);
19849 Set_Etype (N, Any_Type);
19850 return;
19852 elsif T = Universal_Integer then
19853 T := Standard_Integer;
19854 end if;
19855 end;
19856 end if;
19858 if not Is_Discrete_Type (T) then
19859 Error_Msg_N ("discrete type required for range", N);
19860 Set_Etype (N, Any_Type);
19861 return;
19862 end if;
19864 -- If the range bounds are "T'First .. T'Last" where T is a name of a
19865 -- discrete type, then use T as the type of the index.
19867 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19868 and then Attribute_Name (Low_Bound (N)) = Name_First
19869 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19870 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19872 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19873 and then Attribute_Name (High_Bound (N)) = Name_Last
19874 and then Is_Entity_Name (Prefix (High_Bound (N)))
19875 and then Entity (Prefix (High_Bound (N))) = Def_Id
19876 then
19877 Def_Id := Entity (Prefix (Low_Bound (N)));
19878 end if;
19880 R := N;
19881 Process_Range_Expr_In_Decl (R, T);
19883 elsif Nkind (N) = N_Subtype_Indication then
19885 -- The index is given by a subtype with a range constraint
19887 T := Base_Type (Entity (Subtype_Mark (N)));
19889 if not Is_Discrete_Type (T) then
19890 Error_Msg_N ("discrete type required for range", N);
19891 Set_Etype (N, Any_Type);
19892 return;
19893 end if;
19895 R := Range_Expression (Constraint (N));
19897 Resolve (R, T);
19898 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19900 elsif Nkind (N) = N_Attribute_Reference then
19902 -- Catch beginner's error (use of attribute other than 'Range)
19904 if Attribute_Name (N) /= Name_Range then
19905 Error_Msg_N ("expect attribute ''Range", N);
19906 Set_Etype (N, Any_Type);
19907 return;
19908 end if;
19910 -- If the node denotes the range of a type mark, that is also the
19911 -- resulting type, and we do not need to create an Itype for it.
19913 if Is_Entity_Name (Prefix (N))
19914 and then Comes_From_Source (N)
19915 and then Is_Discrete_Type (Entity (Prefix (N)))
19916 then
19917 Def_Id := Entity (Prefix (N));
19918 end if;
19920 Analyze_And_Resolve (N);
19921 T := Etype (N);
19922 R := N;
19924 -- If none of the above, must be a subtype. We convert this to a
19925 -- range attribute reference because in the case of declared first
19926 -- named subtypes, the types in the range reference can be different
19927 -- from the type of the entity. A range attribute normalizes the
19928 -- reference and obtains the correct types for the bounds.
19930 -- This transformation is in the nature of an expansion, is only
19931 -- done if expansion is active. In particular, it is not done on
19932 -- formal generic types, because we need to retain the name of the
19933 -- original index for instantiation purposes.
19935 else
19936 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19937 Error_Msg_N ("invalid subtype mark in discrete range", N);
19938 Set_Etype (N, Any_Integer);
19939 return;
19941 else
19942 -- The type mark may be that of an incomplete type. It is only
19943 -- now that we can get the full view, previous analysis does
19944 -- not look specifically for a type mark.
19946 Set_Entity (N, Get_Full_View (Entity (N)));
19947 Set_Etype (N, Entity (N));
19948 Def_Id := Entity (N);
19950 if not Is_Discrete_Type (Def_Id) then
19951 Error_Msg_N ("discrete type required for index", N);
19952 Set_Etype (N, Any_Type);
19953 return;
19954 end if;
19955 end if;
19957 if Expander_Active then
19958 Rewrite (N,
19959 Make_Attribute_Reference (Sloc (N),
19960 Attribute_Name => Name_Range,
19961 Prefix => Relocate_Node (N)));
19963 -- The original was a subtype mark that does not freeze. This
19964 -- means that the rewritten version must not freeze either.
19966 Set_Must_Not_Freeze (N);
19967 Set_Must_Not_Freeze (Prefix (N));
19968 Analyze_And_Resolve (N);
19969 T := Etype (N);
19970 R := N;
19972 -- If expander is inactive, type is legal, nothing else to construct
19974 else
19975 return;
19976 end if;
19977 end if;
19979 if not Is_Discrete_Type (T) then
19980 Error_Msg_N ("discrete type required for range", N);
19981 Set_Etype (N, Any_Type);
19982 return;
19984 elsif T = Any_Type then
19985 Set_Etype (N, Any_Type);
19986 return;
19987 end if;
19989 -- We will now create the appropriate Itype to describe the range, but
19990 -- first a check. If we originally had a subtype, then we just label
19991 -- the range with this subtype. Not only is there no need to construct
19992 -- a new subtype, but it is wrong to do so for two reasons:
19994 -- 1. A legality concern, if we have a subtype, it must not freeze,
19995 -- and the Itype would cause freezing incorrectly
19997 -- 2. An efficiency concern, if we created an Itype, it would not be
19998 -- recognized as the same type for the purposes of eliminating
19999 -- checks in some circumstances.
20001 -- We signal this case by setting the subtype entity in Def_Id
20003 if No (Def_Id) then
20004 Def_Id :=
20005 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
20006 Set_Etype (Def_Id, Base_Type (T));
20008 if Is_Signed_Integer_Type (T) then
20009 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20011 elsif Is_Modular_Integer_Type (T) then
20012 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20014 else
20015 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20016 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20017 Set_First_Literal (Def_Id, First_Literal (T));
20018 end if;
20020 Set_Size_Info (Def_Id, (T));
20021 Set_RM_Size (Def_Id, RM_Size (T));
20022 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20024 Set_Scalar_Range (Def_Id, R);
20025 Conditional_Delay (Def_Id, T);
20027 -- In the subtype indication case inherit properties of the parent
20029 if Nkind (N) = N_Subtype_Indication then
20031 -- It is enough to inherit predicate flags and not the predicate
20032 -- functions, because predicates on an index type are illegal
20033 -- anyway and the flags are enough to detect them.
20035 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20037 -- If the immediate parent of the new subtype is nonstatic, then
20038 -- the subtype we create is nonstatic as well, even if its bounds
20039 -- are static.
20041 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20042 Set_Is_Non_Static_Subtype (Def_Id);
20043 end if;
20044 end if;
20046 Set_Parent (Def_Id, N);
20047 end if;
20049 -- Final step is to label the index with this constructed type
20051 Set_Etype (N, Def_Id);
20052 end Make_Index;
20054 ------------------------------
20055 -- Modular_Type_Declaration --
20056 ------------------------------
20058 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20059 Mod_Expr : constant Node_Id := Expression (Def);
20060 M_Val : Uint;
20062 procedure Set_Modular_Size (Bits : Int);
20063 -- Sets RM_Size to Bits, and Esize to normal word size above this
20065 ----------------------
20066 -- Set_Modular_Size --
20067 ----------------------
20069 procedure Set_Modular_Size (Bits : Int) is
20070 Siz : Int;
20072 begin
20073 Set_RM_Size (T, UI_From_Int (Bits));
20075 if Bits < System_Max_Binary_Modulus_Power then
20076 Siz := 8;
20078 while Siz < 128 loop
20079 exit when Bits <= Siz;
20080 Siz := Siz * 2;
20081 end loop;
20083 Set_Esize (T, UI_From_Int (Siz));
20085 else
20086 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20087 end if;
20089 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20090 Set_Is_Known_Valid (T);
20091 end if;
20092 end Set_Modular_Size;
20094 -- Start of processing for Modular_Type_Declaration
20096 begin
20097 -- If the mod expression is (exactly) 2 * literal, where literal is
20098 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20100 if Warn_On_Suspicious_Modulus_Value
20101 and then Nkind (Mod_Expr) = N_Op_Multiply
20102 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20103 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20104 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20105 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20106 then
20107 Error_Msg_N
20108 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20109 end if;
20111 -- Proceed with analysis of mod expression
20113 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20115 if Ekind (T) in Incomplete_Or_Private_Kind then
20116 Reinit_Field_To_Zero (T, F_Stored_Constraint);
20117 end if;
20119 Set_Etype (T, T);
20120 Mutate_Ekind (T, E_Modular_Integer_Type);
20121 Reinit_Alignment (T);
20122 Set_Is_Constrained (T);
20124 if not Is_OK_Static_Expression (Mod_Expr) then
20125 Flag_Non_Static_Expr
20126 ("non-static expression used for modular type bound!", Mod_Expr);
20127 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20128 else
20129 M_Val := Expr_Value (Mod_Expr);
20130 end if;
20132 if M_Val < 1 then
20133 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20134 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20135 end if;
20137 if M_Val > 2 ** Standard_Long_Integer_Size then
20138 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20139 end if;
20141 Set_Modulus (T, M_Val);
20143 -- Create bounds for the modular type based on the modulus given in
20144 -- the type declaration and then analyze and resolve those bounds.
20146 Set_Scalar_Range (T,
20147 Make_Range (Sloc (Mod_Expr),
20148 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20149 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20151 -- Properly analyze the literals for the range. We do this manually
20152 -- because we can't go calling Resolve, since we are resolving these
20153 -- bounds with the type, and this type is certainly not complete yet.
20155 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20156 Set_Etype (High_Bound (Scalar_Range (T)), T);
20157 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20158 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20160 -- Loop through powers of two to find number of bits required
20162 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20164 -- Binary case
20166 if M_Val = 2 ** Bits then
20167 Set_Modular_Size (Bits);
20168 return;
20170 -- Nonbinary case
20172 elsif M_Val < 2 ** Bits then
20173 Set_Non_Binary_Modulus (T);
20175 if Bits > System_Max_Nonbinary_Modulus_Power then
20176 Error_Msg_Uint_1 :=
20177 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20178 Error_Msg_F
20179 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20180 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20181 return;
20183 else
20184 -- In the nonbinary case, set size as per RM 13.3(55)
20186 Set_Modular_Size (Bits);
20187 return;
20188 end if;
20189 end if;
20191 end loop;
20193 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20194 -- so we just signal an error and set the maximum size.
20196 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20197 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20199 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20200 Reinit_Alignment (T);
20202 end Modular_Type_Declaration;
20204 --------------------------
20205 -- New_Concatenation_Op --
20206 --------------------------
20208 procedure New_Concatenation_Op (Typ : Entity_Id) is
20209 Loc : constant Source_Ptr := Sloc (Typ);
20210 Op : Entity_Id;
20212 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20213 -- Create abbreviated declaration for the formal of a predefined
20214 -- Operator 'Op' of type 'Typ'
20216 --------------------
20217 -- Make_Op_Formal --
20218 --------------------
20220 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20221 Formal : Entity_Id;
20222 begin
20223 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20224 Set_Etype (Formal, Typ);
20225 Set_Mechanism (Formal, Default_Mechanism);
20226 return Formal;
20227 end Make_Op_Formal;
20229 -- Start of processing for New_Concatenation_Op
20231 begin
20232 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20234 Mutate_Ekind (Op, E_Operator);
20235 Set_Scope (Op, Current_Scope);
20236 Set_Etype (Op, Typ);
20237 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20238 Set_Is_Immediately_Visible (Op);
20239 Set_Is_Intrinsic_Subprogram (Op);
20240 Set_Has_Completion (Op);
20241 Append_Entity (Op, Current_Scope);
20243 Set_Name_Entity_Id (Name_Op_Concat, Op);
20245 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20246 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20247 end New_Concatenation_Op;
20249 -------------------------
20250 -- OK_For_Limited_Init --
20251 -------------------------
20253 -- ???Check all calls of this, and compare the conditions under which it's
20254 -- called.
20256 function OK_For_Limited_Init
20257 (Typ : Entity_Id;
20258 Exp : Node_Id) return Boolean
20260 begin
20261 return Is_CPP_Constructor_Call (Exp)
20262 or else (Ada_Version >= Ada_2005
20263 and then not Debug_Flag_Dot_L
20264 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20265 end OK_For_Limited_Init;
20267 -------------------------------
20268 -- OK_For_Limited_Init_In_05 --
20269 -------------------------------
20271 function OK_For_Limited_Init_In_05
20272 (Typ : Entity_Id;
20273 Exp : Node_Id) return Boolean
20275 begin
20276 -- An object of a limited interface type can be initialized with any
20277 -- expression of a nonlimited descendant type. However this does not
20278 -- apply if this is a view conversion of some other expression. This
20279 -- is checked below.
20281 if Is_Class_Wide_Type (Typ)
20282 and then Is_Limited_Interface (Typ)
20283 and then not Is_Limited_Type (Etype (Exp))
20284 and then Nkind (Exp) /= N_Type_Conversion
20285 then
20286 return True;
20287 end if;
20289 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20290 -- case of limited aggregates (including extension aggregates), and
20291 -- function calls. The function call may have been given in prefixed
20292 -- notation, in which case the original node is an indexed component.
20293 -- If the function is parameterless, the original node was an explicit
20294 -- dereference. The function may also be parameterless, in which case
20295 -- the source node is just an identifier.
20297 -- A branch of a conditional expression may have been removed if the
20298 -- condition is statically known. This happens during expansion, and
20299 -- thus will not happen if previous errors were encountered. The check
20300 -- will have been performed on the chosen branch, which replaces the
20301 -- original conditional expression.
20303 if No (Exp) then
20304 return True;
20305 end if;
20307 case Nkind (Original_Node (Exp)) is
20308 when N_Aggregate
20309 | N_Delta_Aggregate
20310 | N_Extension_Aggregate
20311 | N_Function_Call
20312 | N_Op
20314 return True;
20316 when N_Identifier =>
20317 return Present (Entity (Original_Node (Exp)))
20318 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20320 when N_Qualified_Expression =>
20321 return
20322 OK_For_Limited_Init_In_05
20323 (Typ, Expression (Original_Node (Exp)));
20325 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20326 -- with a function call, the expander has rewritten the call into an
20327 -- N_Type_Conversion node to force displacement of the pointer to
20328 -- reference the component containing the secondary dispatch table.
20329 -- Otherwise a type conversion is not a legal context.
20330 -- A return statement for a build-in-place function returning a
20331 -- synchronized type also introduces an unchecked conversion.
20333 when N_Type_Conversion
20334 | N_Unchecked_Type_Conversion
20336 return not Comes_From_Source (Exp)
20337 and then
20338 -- If the conversion has been rewritten, check Original_Node;
20339 -- otherwise, check the expression of the compiler-generated
20340 -- conversion (which is a conversion that we want to ignore
20341 -- for purposes of the limited-initialization restrictions).
20343 (if Is_Rewrite_Substitution (Exp)
20344 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20345 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20347 when N_Explicit_Dereference
20348 | N_Indexed_Component
20349 | N_Selected_Component
20351 return Nkind (Exp) = N_Function_Call;
20353 -- A use of 'Input is a function call, hence allowed. Normally the
20354 -- attribute will be changed to a call, but the attribute by itself
20355 -- can occur with -gnatc.
20357 when N_Attribute_Reference =>
20358 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20360 -- "return raise ..." is OK
20362 when N_Raise_Expression =>
20363 return True;
20365 -- For a case expression, all dependent expressions must be legal
20367 when N_Case_Expression =>
20368 declare
20369 Alt : Node_Id;
20371 begin
20372 Alt := First (Alternatives (Original_Node (Exp)));
20373 while Present (Alt) loop
20374 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20375 return False;
20376 end if;
20378 Next (Alt);
20379 end loop;
20381 return True;
20382 end;
20384 -- For an if expression, all dependent expressions must be legal
20386 when N_If_Expression =>
20387 declare
20388 Then_Expr : constant Node_Id :=
20389 Next (First (Expressions (Original_Node (Exp))));
20390 Else_Expr : constant Node_Id := Next (Then_Expr);
20391 begin
20392 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20393 and then
20394 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20395 end;
20397 when others =>
20398 return False;
20399 end case;
20400 end OK_For_Limited_Init_In_05;
20402 -------------------------------------------
20403 -- Ordinary_Fixed_Point_Type_Declaration --
20404 -------------------------------------------
20406 procedure Ordinary_Fixed_Point_Type_Declaration
20407 (T : Entity_Id;
20408 Def : Node_Id)
20410 Loc : constant Source_Ptr := Sloc (Def);
20411 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20412 RRS : constant Node_Id := Real_Range_Specification (Def);
20413 Implicit_Base : Entity_Id;
20414 Delta_Val : Ureal;
20415 Small_Val : Ureal;
20416 Low_Val : Ureal;
20417 High_Val : Ureal;
20419 begin
20420 Check_Restriction (No_Fixed_Point, Def);
20422 -- Create implicit base type
20424 Implicit_Base :=
20425 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20426 Set_Etype (Implicit_Base, Implicit_Base);
20428 -- Analyze and process delta expression
20430 Analyze_And_Resolve (Delta_Expr, Any_Real);
20432 Check_Delta_Expression (Delta_Expr);
20433 Delta_Val := Expr_Value_R (Delta_Expr);
20435 Set_Delta_Value (Implicit_Base, Delta_Val);
20437 -- Compute default small from given delta, which is the largest power
20438 -- of two that does not exceed the given delta value.
20440 declare
20441 Tmp : Ureal;
20442 Scale : Int;
20444 begin
20445 Tmp := Ureal_1;
20446 Scale := 0;
20448 if Delta_Val < Ureal_1 then
20449 while Delta_Val < Tmp loop
20450 Tmp := Tmp / Ureal_2;
20451 Scale := Scale + 1;
20452 end loop;
20454 else
20455 loop
20456 Tmp := Tmp * Ureal_2;
20457 exit when Tmp > Delta_Val;
20458 Scale := Scale - 1;
20459 end loop;
20460 end if;
20462 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20463 end;
20465 Set_Small_Value (Implicit_Base, Small_Val);
20467 -- If no range was given, set a dummy range
20469 if RRS <= Empty_Or_Error then
20470 Low_Val := -Small_Val;
20471 High_Val := Small_Val;
20473 -- Otherwise analyze and process given range
20475 else
20476 declare
20477 Low : constant Node_Id := Low_Bound (RRS);
20478 High : constant Node_Id := High_Bound (RRS);
20480 begin
20481 Analyze_And_Resolve (Low, Any_Real);
20482 Analyze_And_Resolve (High, Any_Real);
20483 Check_Real_Bound (Low);
20484 Check_Real_Bound (High);
20486 -- Obtain and set the range
20488 Low_Val := Expr_Value_R (Low);
20489 High_Val := Expr_Value_R (High);
20491 if Low_Val > High_Val then
20492 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20493 end if;
20494 end;
20495 end if;
20497 -- The range for both the implicit base and the declared first subtype
20498 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20499 -- set a temporary range in place. Note that the bounds of the base
20500 -- type will be widened to be symmetrical and to fill the available
20501 -- bits when the type is frozen.
20503 -- We could do this with all discrete types, and probably should, but
20504 -- we absolutely have to do it for fixed-point, since the end-points
20505 -- of the range and the size are determined by the small value, which
20506 -- could be reset before the freeze point.
20508 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20509 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20511 -- Complete definition of first subtype. The inheritance of the rep item
20512 -- chain ensures that SPARK-related pragmas are not clobbered when the
20513 -- ordinary fixed point type acts as a full view of a private type.
20515 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20516 Set_Etype (T, Implicit_Base);
20517 Reinit_Size_Align (T);
20518 Inherit_Rep_Item_Chain (T, Implicit_Base);
20519 Set_Small_Value (T, Small_Val);
20520 Set_Delta_Value (T, Delta_Val);
20521 Set_Is_Constrained (T);
20522 end Ordinary_Fixed_Point_Type_Declaration;
20524 ----------------------------------
20525 -- Preanalyze_Assert_Expression --
20526 ----------------------------------
20528 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20529 begin
20530 In_Assertion_Expr := In_Assertion_Expr + 1;
20531 Preanalyze_Spec_Expression (N, T);
20532 In_Assertion_Expr := In_Assertion_Expr - 1;
20533 end Preanalyze_Assert_Expression;
20535 -- ??? The variant below explicitly saves and restores all the flags,
20536 -- because it is impossible to compose the existing variety of
20537 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20538 -- to achieve the desired semantics.
20540 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20541 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20542 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
20543 Save_Full_Analysis : constant Boolean := Full_Analysis;
20545 begin
20546 In_Assertion_Expr := In_Assertion_Expr + 1;
20547 In_Spec_Expression := True;
20548 Set_Must_Not_Freeze (N);
20549 Inside_Preanalysis_Without_Freezing :=
20550 Inside_Preanalysis_Without_Freezing + 1;
20551 Full_Analysis := False;
20552 Expander_Mode_Save_And_Set (False);
20554 if GNATprove_Mode then
20555 Analyze_And_Resolve (N);
20556 else
20557 Analyze_And_Resolve (N, Suppress => All_Checks);
20558 end if;
20560 Expander_Mode_Restore;
20561 Full_Analysis := Save_Full_Analysis;
20562 Inside_Preanalysis_Without_Freezing :=
20563 Inside_Preanalysis_Without_Freezing - 1;
20564 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
20565 In_Spec_Expression := Save_In_Spec_Expression;
20566 In_Assertion_Expr := In_Assertion_Expr - 1;
20567 end Preanalyze_Assert_Expression;
20569 -----------------------------------
20570 -- Preanalyze_Default_Expression --
20571 -----------------------------------
20573 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20574 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20575 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20577 begin
20578 In_Default_Expr := True;
20579 In_Spec_Expression := True;
20581 Preanalyze_With_Freezing_And_Resolve (N, T);
20583 In_Default_Expr := Save_In_Default_Expr;
20584 In_Spec_Expression := Save_In_Spec_Expression;
20585 end Preanalyze_Default_Expression;
20587 --------------------------------
20588 -- Preanalyze_Spec_Expression --
20589 --------------------------------
20591 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20592 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20593 begin
20594 In_Spec_Expression := True;
20595 Preanalyze_And_Resolve (N, T);
20596 In_Spec_Expression := Save_In_Spec_Expression;
20597 end Preanalyze_Spec_Expression;
20599 ----------------------------------------
20600 -- Prepare_Private_Subtype_Completion --
20601 ----------------------------------------
20603 procedure Prepare_Private_Subtype_Completion
20604 (Id : Entity_Id;
20605 Related_Nod : Node_Id)
20607 Id_B : constant Entity_Id := Base_Type (Id);
20608 Full_B : constant Entity_Id := Full_View (Id_B);
20609 Full : Entity_Id;
20611 begin
20612 if Present (Full_B) then
20614 -- The Base_Type is already completed, we can complete the subtype
20615 -- now. We have to create a new entity with the same name, Thus we
20616 -- can't use Create_Itype.
20618 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20619 Set_Is_Itype (Full);
20620 Set_Associated_Node_For_Itype (Full, Related_Nod);
20621 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20622 Set_Full_View (Id, Full);
20623 end if;
20625 -- The parent subtype may be private, but the base might not, in some
20626 -- nested instances. In that case, the subtype does not need to be
20627 -- exchanged. It would still be nice to make private subtypes and their
20628 -- bases consistent at all times ???
20630 if Is_Private_Type (Id_B) then
20631 Append_Elmt (Id, Private_Dependents (Id_B));
20632 end if;
20633 end Prepare_Private_Subtype_Completion;
20635 ---------------------------
20636 -- Process_Discriminants --
20637 ---------------------------
20639 procedure Process_Discriminants
20640 (N : Node_Id;
20641 Prev : Entity_Id := Empty)
20643 Elist : constant Elist_Id := New_Elmt_List;
20644 Id : Node_Id;
20645 Discr : Node_Id;
20646 Discr_Number : Uint;
20647 Discr_Type : Entity_Id;
20648 Default_Present : Boolean := False;
20649 Default_Not_Present : Boolean := False;
20651 begin
20652 -- A composite type other than an array type can have discriminants.
20653 -- On entry, the current scope is the composite type.
20655 -- The discriminants are initially entered into the scope of the type
20656 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20657 -- use, as explained at the end of this procedure.
20659 Discr := First (Discriminant_Specifications (N));
20660 while Present (Discr) loop
20661 Enter_Name (Defining_Identifier (Discr));
20663 -- For navigation purposes we add a reference to the discriminant
20664 -- in the entity for the type. If the current declaration is a
20665 -- completion, place references on the partial view. Otherwise the
20666 -- type is the current scope.
20668 if Present (Prev) then
20670 -- The references go on the partial view, if present. If the
20671 -- partial view has discriminants, the references have been
20672 -- generated already.
20674 if not Has_Discriminants (Prev) then
20675 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20676 end if;
20677 else
20678 Generate_Reference
20679 (Current_Scope, Defining_Identifier (Discr), 'd');
20680 end if;
20682 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20683 Check_Anonymous_Access_Component
20684 (Typ_Decl => N,
20685 Typ => Defining_Identifier (N),
20686 Prev => Prev,
20687 Comp_Def => Discr,
20688 Access_Def => Discriminant_Type (Discr));
20690 -- if Check_Anonymous_Access_Component replaced Discr then
20691 -- its Original_Node points to the old Discr and the access type
20692 -- for Discr_Type has already been created.
20694 if Is_Rewrite_Substitution (Discr) then
20695 Discr_Type := Etype (Discriminant_Type (Discr));
20696 else
20697 Discr_Type :=
20698 Access_Definition (Discr, Discriminant_Type (Discr));
20700 -- Ada 2005 (AI-254)
20702 if Present (Access_To_Subprogram_Definition
20703 (Discriminant_Type (Discr)))
20704 and then Protected_Present (Access_To_Subprogram_Definition
20705 (Discriminant_Type (Discr)))
20706 then
20707 Discr_Type :=
20708 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20709 end if;
20710 end if;
20711 else
20712 Find_Type (Discriminant_Type (Discr));
20713 Discr_Type := Etype (Discriminant_Type (Discr));
20715 if Error_Posted (Discriminant_Type (Discr)) then
20716 Discr_Type := Any_Type;
20717 end if;
20718 end if;
20720 -- Handling of discriminants that are access types
20722 if Is_Access_Type (Discr_Type) then
20724 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20725 -- limited record types
20727 if Ada_Version < Ada_2005 then
20728 Check_Access_Discriminant_Requires_Limited
20729 (Discr, Discriminant_Type (Discr));
20730 end if;
20732 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20733 Error_Msg_N
20734 ("(Ada 83) access discriminant not allowed", Discr);
20735 end if;
20737 -- If not access type, must be a discrete type
20739 elsif not Is_Discrete_Type (Discr_Type) then
20740 Error_Msg_N
20741 ("discriminants must have a discrete or access type",
20742 Discriminant_Type (Discr));
20743 end if;
20745 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20747 -- If a discriminant specification includes the assignment compound
20748 -- delimiter followed by an expression, the expression is the default
20749 -- expression of the discriminant; the default expression must be of
20750 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20751 -- a default expression, we do the special preanalysis, since this
20752 -- expression does not freeze (see section "Handling of Default and
20753 -- Per-Object Expressions" in spec of package Sem).
20755 if Present (Expression (Discr)) then
20756 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20758 -- Legaity checks
20760 if Nkind (N) = N_Formal_Type_Declaration then
20761 Error_Msg_N
20762 ("discriminant defaults not allowed for formal type",
20763 Expression (Discr));
20765 -- Flag an error for a tagged type with defaulted discriminants,
20766 -- excluding limited tagged types when compiling for Ada 2012
20767 -- (see AI05-0214).
20769 elsif Is_Tagged_Type (Current_Scope)
20770 and then (not Is_Limited_Type (Current_Scope)
20771 or else Ada_Version < Ada_2012)
20772 and then Comes_From_Source (N)
20773 then
20774 -- Note: see similar test in Check_Or_Process_Discriminants, to
20775 -- handle the (illegal) case of the completion of an untagged
20776 -- view with discriminants with defaults by a tagged full view.
20777 -- We skip the check if Discr does not come from source, to
20778 -- account for the case of an untagged derived type providing
20779 -- defaults for a renamed discriminant from a private untagged
20780 -- ancestor with a tagged full view (ACATS B460006).
20782 if Ada_Version >= Ada_2012 then
20783 Error_Msg_N
20784 ("discriminants of nonlimited tagged type cannot have"
20785 & " defaults",
20786 Expression (Discr));
20787 else
20788 Error_Msg_N
20789 ("discriminants of tagged type cannot have defaults",
20790 Expression (Discr));
20791 end if;
20793 else
20794 Default_Present := True;
20795 Append_Elmt (Expression (Discr), Elist);
20797 -- Tag the defining identifiers for the discriminants with
20798 -- their corresponding default expressions from the tree.
20800 Set_Discriminant_Default_Value
20801 (Defining_Identifier (Discr), Expression (Discr));
20802 end if;
20804 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20805 -- gets set unless we can be sure that no range check is required.
20807 if not Expander_Active
20808 and then not
20809 Is_In_Range
20810 (Expression (Discr), Discr_Type, Assume_Valid => True)
20811 then
20812 Set_Do_Range_Check (Expression (Discr));
20813 end if;
20815 -- No default discriminant value given
20817 else
20818 Default_Not_Present := True;
20819 end if;
20821 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20822 -- Discr_Type but with the null-exclusion attribute
20824 if Ada_Version >= Ada_2005 then
20826 -- Ada 2005 (AI-231): Static checks
20828 if Can_Never_Be_Null (Discr_Type) then
20829 Null_Exclusion_Static_Checks (Discr);
20831 elsif Is_Access_Type (Discr_Type)
20832 and then Null_Exclusion_Present (Discr)
20834 -- No need to check itypes because in their case this check
20835 -- was done at their point of creation
20837 and then not Is_Itype (Discr_Type)
20838 then
20839 if Can_Never_Be_Null (Discr_Type) then
20840 Error_Msg_NE
20841 ("`NOT NULL` not allowed (& already excludes null)",
20842 Discr,
20843 Discr_Type);
20844 end if;
20846 Set_Etype (Defining_Identifier (Discr),
20847 Create_Null_Excluding_Itype
20848 (T => Discr_Type,
20849 Related_Nod => Discr));
20851 -- Check for improper null exclusion if the type is otherwise
20852 -- legal for a discriminant.
20854 elsif Null_Exclusion_Present (Discr)
20855 and then Is_Discrete_Type (Discr_Type)
20856 then
20857 Error_Msg_N
20858 ("null exclusion can only apply to an access type", Discr);
20859 end if;
20861 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20862 -- can't have defaults. Synchronized types, or types that are
20863 -- explicitly limited are fine, but special tests apply to derived
20864 -- types in generics: in a generic body we have to assume the
20865 -- worst, and therefore defaults are not allowed if the parent is
20866 -- a generic formal private type (see ACATS B370001).
20868 if Is_Access_Type (Discr_Type) and then Default_Present then
20869 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20870 or else Is_Limited_Record (Current_Scope)
20871 or else Is_Concurrent_Type (Current_Scope)
20872 or else Is_Concurrent_Record_Type (Current_Scope)
20873 or else Ekind (Current_Scope) = E_Limited_Private_Type
20874 then
20875 if not Is_Derived_Type (Current_Scope)
20876 or else not Is_Generic_Type (Etype (Current_Scope))
20877 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20878 or else Limited_Present
20879 (Type_Definition (Parent (Current_Scope)))
20880 then
20881 null;
20883 else
20884 Error_Msg_N
20885 ("access discriminants of nonlimited types cannot "
20886 & "have defaults", Expression (Discr));
20887 end if;
20889 elsif Present (Expression (Discr)) then
20890 Error_Msg_N
20891 ("(Ada 2005) access discriminants of nonlimited types "
20892 & "cannot have defaults", Expression (Discr));
20893 end if;
20894 end if;
20895 end if;
20897 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20898 -- This check is relevant only when SPARK_Mode is on as it is not a
20899 -- standard Ada legality rule. The only way for a discriminant to be
20900 -- effectively volatile is to have an effectively volatile type, so
20901 -- we check this directly, because the Ekind of Discr might not be
20902 -- set yet (to help preventing cascaded errors on derived types).
20904 if SPARK_Mode = On
20905 and then Is_Effectively_Volatile (Discr_Type)
20906 then
20907 Error_Msg_N ("discriminant cannot be volatile", Discr);
20908 end if;
20910 Next (Discr);
20911 end loop;
20913 -- An element list consisting of the default expressions of the
20914 -- discriminants is constructed in the above loop and used to set
20915 -- the Discriminant_Constraint attribute for the type. If an object
20916 -- is declared of this (record or task) type without any explicit
20917 -- discriminant constraint given, this element list will form the
20918 -- actual parameters for the corresponding initialization procedure
20919 -- for the type.
20921 Set_Discriminant_Constraint (Current_Scope, Elist);
20922 Set_Stored_Constraint (Current_Scope, No_Elist);
20924 -- Default expressions must be provided either for all or for none
20925 -- of the discriminants of a discriminant part. (RM 3.7.1)
20927 if Default_Present and then Default_Not_Present then
20928 Error_Msg_N
20929 ("incomplete specification of defaults for discriminants", N);
20930 end if;
20932 -- The use of the name of a discriminant is not allowed in default
20933 -- expressions of a discriminant part if the specification of the
20934 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20936 -- To detect this, the discriminant names are entered initially with an
20937 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20938 -- attempt to use a void entity (for example in an expression that is
20939 -- type-checked) produces the error message: premature usage. Now after
20940 -- completing the semantic analysis of the discriminant part, we can set
20941 -- the Ekind of all the discriminants appropriately.
20943 Discr := First (Discriminant_Specifications (N));
20944 Discr_Number := Uint_1;
20945 while Present (Discr) loop
20946 Id := Defining_Identifier (Discr);
20948 if Ekind (Id) = E_In_Parameter then
20949 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
20950 end if;
20952 Mutate_Ekind (Id, E_Discriminant);
20953 Reinit_Component_Location (Id);
20954 Reinit_Esize (Id);
20955 Set_Discriminant_Number (Id, Discr_Number);
20957 -- Make sure this is always set, even in illegal programs
20959 Set_Corresponding_Discriminant (Id, Empty);
20961 -- Initialize the Original_Record_Component to the entity itself.
20962 -- Inherit_Components will propagate the right value to
20963 -- discriminants in derived record types.
20965 Set_Original_Record_Component (Id, Id);
20967 -- Create the discriminal for the discriminant
20969 Build_Discriminal (Id);
20971 Next (Discr);
20972 Discr_Number := Discr_Number + 1;
20973 end loop;
20975 Set_Has_Discriminants (Current_Scope);
20976 end Process_Discriminants;
20978 -----------------------
20979 -- Process_Full_View --
20980 -----------------------
20982 -- WARNING: This routine manages Ghost regions. Return statements must be
20983 -- replaced by gotos which jump to the end of the routine and restore the
20984 -- Ghost mode.
20986 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20987 procedure Collect_Implemented_Interfaces
20988 (Typ : Entity_Id;
20989 Ifaces : Elist_Id);
20990 -- Ada 2005: Gather all the interfaces that Typ directly or
20991 -- inherently implements. Duplicate entries are not added to
20992 -- the list Ifaces.
20994 ------------------------------------
20995 -- Collect_Implemented_Interfaces --
20996 ------------------------------------
20998 procedure Collect_Implemented_Interfaces
20999 (Typ : Entity_Id;
21000 Ifaces : Elist_Id)
21002 Iface : Entity_Id;
21003 Iface_Elmt : Elmt_Id;
21005 begin
21006 -- Abstract interfaces are only associated with tagged record types
21008 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
21009 return;
21010 end if;
21012 -- Recursively climb to the ancestors
21014 if Etype (Typ) /= Typ
21016 -- Protect the frontend against wrong cyclic declarations like:
21018 -- type B is new A with private;
21019 -- type C is new A with private;
21020 -- private
21021 -- type B is new C with null record;
21022 -- type C is new B with null record;
21024 and then Etype (Typ) /= Priv_T
21025 and then Etype (Typ) /= Full_T
21026 then
21027 -- Keep separate the management of private type declarations
21029 if Ekind (Typ) = E_Record_Type_With_Private then
21031 -- Handle the following illegal usage:
21032 -- type Private_Type is tagged private;
21033 -- private
21034 -- type Private_Type is new Type_Implementing_Iface;
21036 if Present (Full_View (Typ))
21037 and then Etype (Typ) /= Full_View (Typ)
21038 then
21039 if Is_Interface (Etype (Typ)) then
21040 Append_Unique_Elmt (Etype (Typ), Ifaces);
21041 end if;
21043 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21044 end if;
21046 -- Non-private types
21048 else
21049 if Is_Interface (Etype (Typ)) then
21050 Append_Unique_Elmt (Etype (Typ), Ifaces);
21051 end if;
21053 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21054 end if;
21055 end if;
21057 -- Handle entities in the list of abstract interfaces
21059 if Present (Interfaces (Typ)) then
21060 Iface_Elmt := First_Elmt (Interfaces (Typ));
21061 while Present (Iface_Elmt) loop
21062 Iface := Node (Iface_Elmt);
21064 pragma Assert (Is_Interface (Iface));
21066 if not Contain_Interface (Iface, Ifaces) then
21067 Append_Elmt (Iface, Ifaces);
21068 Collect_Implemented_Interfaces (Iface, Ifaces);
21069 end if;
21071 Next_Elmt (Iface_Elmt);
21072 end loop;
21073 end if;
21074 end Collect_Implemented_Interfaces;
21076 -- Local variables
21078 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21079 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21080 -- Save the Ghost-related attributes to restore on exit
21082 Full_Indic : Node_Id;
21083 Full_Parent : Entity_Id;
21084 Priv_Parent : Entity_Id;
21086 -- Start of processing for Process_Full_View
21088 begin
21089 Mark_And_Set_Ghost_Completion (N, Priv_T);
21091 -- First some sanity checks that must be done after semantic
21092 -- decoration of the full view and thus cannot be placed with other
21093 -- similar checks in Find_Type_Name
21095 if not Is_Limited_Type (Priv_T)
21096 and then (Is_Limited_Type (Full_T)
21097 or else Is_Limited_Composite (Full_T))
21098 then
21099 if In_Instance then
21100 null;
21101 else
21102 Error_Msg_N
21103 ("completion of nonlimited type cannot be limited", Full_T);
21104 Explain_Limited_Type (Full_T, Full_T);
21105 end if;
21107 elsif Is_Abstract_Type (Full_T)
21108 and then not Is_Abstract_Type (Priv_T)
21109 then
21110 Error_Msg_N
21111 ("completion of nonabstract type cannot be abstract", Full_T);
21113 elsif Is_Tagged_Type (Priv_T)
21114 and then Is_Limited_Type (Priv_T)
21115 and then not Is_Limited_Type (Full_T)
21116 then
21117 -- If pragma CPP_Class was applied to the private declaration
21118 -- propagate the limitedness to the full-view
21120 if Is_CPP_Class (Priv_T) then
21121 Set_Is_Limited_Record (Full_T);
21123 -- GNAT allow its own definition of Limited_Controlled to disobey
21124 -- this rule in order in ease the implementation. This test is safe
21125 -- because Root_Controlled is defined in a child of System that
21126 -- normal programs are not supposed to use.
21128 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21129 Set_Is_Limited_Composite (Full_T);
21130 else
21131 Error_Msg_N
21132 ("completion of limited tagged type must be limited", Full_T);
21133 end if;
21135 elsif Is_Generic_Type (Priv_T) then
21136 Error_Msg_N ("generic type cannot have a completion", Full_T);
21137 end if;
21139 -- Check that ancestor interfaces of private and full views are
21140 -- consistent. We omit this check for synchronized types because
21141 -- they are performed on the corresponding record type when frozen.
21143 if Ada_Version >= Ada_2005
21144 and then Is_Tagged_Type (Priv_T)
21145 and then Is_Tagged_Type (Full_T)
21146 and then not Is_Concurrent_Type (Full_T)
21147 then
21148 declare
21149 Iface : Entity_Id;
21150 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21151 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21153 begin
21154 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21155 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21157 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21158 -- an interface type if and only if the full type is descendant
21159 -- of the interface type (AARM 7.3 (7.3/2)).
21161 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21163 if Present (Iface) then
21164 Error_Msg_NE
21165 ("interface in partial view& not implemented by full type "
21166 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21167 end if;
21169 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21171 if Present (Iface) then
21172 Error_Msg_NE
21173 ("interface & not implemented by partial view "
21174 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21175 end if;
21176 end;
21177 end if;
21179 if Is_Tagged_Type (Priv_T)
21180 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21181 and then Is_Derived_Type (Full_T)
21182 then
21183 Priv_Parent := Etype (Priv_T);
21185 -- The full view of a private extension may have been transformed
21186 -- into an unconstrained derived type declaration and a subtype
21187 -- declaration (see build_derived_record_type for details).
21189 if Nkind (N) = N_Subtype_Declaration then
21190 Full_Indic := Subtype_Indication (N);
21191 Full_Parent := Etype (Base_Type (Full_T));
21192 else
21193 Full_Indic := Subtype_Indication (Type_Definition (N));
21194 Full_Parent := Etype (Full_T);
21195 end if;
21197 -- Check that the parent type of the full type is a descendant of
21198 -- the ancestor subtype given in the private extension. If either
21199 -- entity has an Etype equal to Any_Type then we had some previous
21200 -- error situation [7.3(8)].
21202 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21203 goto Leave;
21205 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21206 -- any order. Therefore we don't have to check that its parent must
21207 -- be a descendant of the parent of the private type declaration.
21209 elsif Is_Interface (Priv_Parent)
21210 and then Is_Interface (Full_Parent)
21211 then
21212 null;
21214 -- Ada 2005 (AI-251): If the parent of the private type declaration
21215 -- is an interface there is no need to check that it is an ancestor
21216 -- of the associated full type declaration. The required tests for
21217 -- this case are performed by Build_Derived_Record_Type.
21219 elsif not Is_Interface (Base_Type (Priv_Parent))
21220 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21221 then
21222 Error_Msg_N
21223 ("parent of full type must descend from parent of private "
21224 & "extension", Full_Indic);
21226 -- First check a formal restriction, and then proceed with checking
21227 -- Ada rules. Since the formal restriction is not a serious error, we
21228 -- don't prevent further error detection for this check, hence the
21229 -- ELSE.
21231 else
21232 -- Check the rules of 7.3(10): if the private extension inherits
21233 -- known discriminants, then the full type must also inherit those
21234 -- discriminants from the same (ancestor) type, and the parent
21235 -- subtype of the full type must be constrained if and only if
21236 -- the ancestor subtype of the private extension is constrained.
21238 if No (Discriminant_Specifications (Parent (Priv_T)))
21239 and then not Has_Unknown_Discriminants (Priv_T)
21240 and then Has_Discriminants (Base_Type (Priv_Parent))
21241 then
21242 declare
21243 Priv_Indic : constant Node_Id :=
21244 Subtype_Indication (Parent (Priv_T));
21246 Priv_Constr : constant Boolean :=
21247 Is_Constrained (Priv_Parent)
21248 or else
21249 Nkind (Priv_Indic) = N_Subtype_Indication
21250 or else
21251 Is_Constrained (Entity (Priv_Indic));
21253 Full_Constr : constant Boolean :=
21254 Is_Constrained (Full_Parent)
21255 or else
21256 Nkind (Full_Indic) = N_Subtype_Indication
21257 or else
21258 Is_Constrained (Entity (Full_Indic));
21260 Priv_Discr : Entity_Id;
21261 Full_Discr : Entity_Id;
21263 begin
21264 Priv_Discr := First_Discriminant (Priv_Parent);
21265 Full_Discr := First_Discriminant (Full_Parent);
21266 while Present (Priv_Discr) and then Present (Full_Discr) loop
21267 if Original_Record_Component (Priv_Discr) =
21268 Original_Record_Component (Full_Discr)
21269 or else
21270 Corresponding_Discriminant (Priv_Discr) =
21271 Corresponding_Discriminant (Full_Discr)
21272 then
21273 null;
21274 else
21275 exit;
21276 end if;
21278 Next_Discriminant (Priv_Discr);
21279 Next_Discriminant (Full_Discr);
21280 end loop;
21282 if Present (Priv_Discr) or else Present (Full_Discr) then
21283 Error_Msg_N
21284 ("full view must inherit discriminants of the parent "
21285 & "type used in the private extension", Full_Indic);
21287 elsif Priv_Constr and then not Full_Constr then
21288 Error_Msg_N
21289 ("parent subtype of full type must be constrained",
21290 Full_Indic);
21292 elsif Full_Constr and then not Priv_Constr then
21293 Error_Msg_N
21294 ("parent subtype of full type must be unconstrained",
21295 Full_Indic);
21296 end if;
21297 end;
21299 -- Check the rules of 7.3(12): if a partial view has neither
21300 -- known or unknown discriminants, then the full type
21301 -- declaration shall define a definite subtype.
21303 elsif not Has_Unknown_Discriminants (Priv_T)
21304 and then not Has_Discriminants (Priv_T)
21305 and then not Is_Constrained (Full_T)
21306 then
21307 Error_Msg_N
21308 ("full view must define a constrained type if partial view "
21309 & "has no discriminants", Full_T);
21310 end if;
21312 -- Do we implement the following properly???
21313 -- If the ancestor subtype of a private extension has constrained
21314 -- discriminants, then the parent subtype of the full view shall
21315 -- impose a statically matching constraint on those discriminants
21316 -- [7.3(13)].
21317 end if;
21319 else
21320 -- For untagged types, verify that a type without discriminants is
21321 -- not completed with an unconstrained type. A separate error message
21322 -- is produced if the full type has defaulted discriminants.
21324 if Is_Definite_Subtype (Priv_T)
21325 and then not Is_Definite_Subtype (Full_T)
21326 then
21327 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21328 Error_Msg_NE
21329 ("full view of& not compatible with declaration#",
21330 Full_T, Priv_T);
21332 if not Is_Tagged_Type (Full_T) then
21333 Error_Msg_N
21334 ("\one is constrained, the other unconstrained", Full_T);
21335 end if;
21336 end if;
21337 end if;
21339 -- AI-419: verify that the use of "limited" is consistent
21341 declare
21342 Orig_Decl : constant Node_Id := Original_Node (N);
21344 begin
21345 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21346 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21347 and then Nkind
21348 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21349 then
21350 if not Limited_Present (Parent (Priv_T))
21351 and then not Synchronized_Present (Parent (Priv_T))
21352 and then Limited_Present (Type_Definition (Orig_Decl))
21353 then
21354 Error_Msg_N
21355 ("full view of non-limited extension cannot be limited", N);
21357 -- Conversely, if the partial view carries the limited keyword,
21358 -- the full view must as well, even if it may be redundant.
21360 elsif Limited_Present (Parent (Priv_T))
21361 and then not Limited_Present (Type_Definition (Orig_Decl))
21362 then
21363 Error_Msg_N
21364 ("full view of limited extension must be explicitly limited",
21366 end if;
21367 end if;
21368 end;
21370 -- Ada 2005 (AI-443): A synchronized private extension must be
21371 -- completed by a task or protected type.
21373 if Ada_Version >= Ada_2005
21374 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21375 and then Synchronized_Present (Parent (Priv_T))
21376 and then not Is_Concurrent_Type (Full_T)
21377 then
21378 Error_Msg_N ("full view of synchronized extension must " &
21379 "be synchronized type", N);
21380 end if;
21382 -- Ada 2005 AI-363: if the full view has discriminants with
21383 -- defaults, it is illegal to declare constrained access subtypes
21384 -- whose designated type is the current type. This allows objects
21385 -- of the type that are declared in the heap to be unconstrained.
21387 if not Has_Unknown_Discriminants (Priv_T)
21388 and then not Has_Discriminants (Priv_T)
21389 and then Has_Defaulted_Discriminants (Full_T)
21390 then
21391 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21392 Set_Has_Constrained_Partial_View (Priv_T);
21393 end if;
21395 -- Create a full declaration for all its subtypes recorded in
21396 -- Private_Dependents and swap them similarly to the base type. These
21397 -- are subtypes that have been define before the full declaration of
21398 -- the private type. We also swap the entry in Private_Dependents list
21399 -- so we can properly restore the private view on exit from the scope.
21401 declare
21402 Priv_Elmt : Elmt_Id;
21403 Priv_Scop : Entity_Id;
21404 Priv : Entity_Id;
21405 Full : Entity_Id;
21407 begin
21408 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21409 while Present (Priv_Elmt) loop
21410 Priv := Node (Priv_Elmt);
21411 Priv_Scop := Scope (Priv);
21413 if Ekind (Priv) in E_Private_Subtype
21414 | E_Limited_Private_Subtype
21415 | E_Record_Subtype_With_Private
21416 then
21417 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21418 Set_Is_Itype (Full);
21419 Set_Parent (Full, Parent (Priv));
21420 Set_Associated_Node_For_Itype (Full, N);
21422 -- Now we need to complete the private subtype, but since the
21423 -- base type has already been swapped, we must also swap the
21424 -- subtypes (and thus, reverse the arguments in the call to
21425 -- Complete_Private_Subtype). Also note that we may need to
21426 -- re-establish the scope of the private subtype.
21428 Copy_And_Swap (Priv, Full);
21430 if not In_Open_Scopes (Priv_Scop) then
21431 Push_Scope (Priv_Scop);
21433 else
21434 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21436 Priv_Scop := Empty;
21437 end if;
21439 Complete_Private_Subtype (Full, Priv, Full_T, N);
21440 Set_Full_View (Full, Priv);
21442 if Present (Priv_Scop) then
21443 Pop_Scope;
21444 end if;
21446 Replace_Elmt (Priv_Elmt, Full);
21447 end if;
21449 Next_Elmt (Priv_Elmt);
21450 end loop;
21451 end;
21453 declare
21454 Disp_Typ : Entity_Id;
21455 Full_List : Elist_Id;
21456 Prim : Entity_Id;
21457 Prim_Elmt : Elmt_Id;
21458 Priv_List : Elist_Id;
21460 function Contains
21461 (E : Entity_Id;
21462 L : Elist_Id) return Boolean;
21463 -- Determine whether list L contains element E
21465 --------------
21466 -- Contains --
21467 --------------
21469 function Contains
21470 (E : Entity_Id;
21471 L : Elist_Id) return Boolean
21473 List_Elmt : Elmt_Id;
21475 begin
21476 List_Elmt := First_Elmt (L);
21477 while Present (List_Elmt) loop
21478 if Node (List_Elmt) = E then
21479 return True;
21480 end if;
21482 Next_Elmt (List_Elmt);
21483 end loop;
21485 return False;
21486 end Contains;
21488 -- Start of processing
21490 begin
21491 -- If the private view was tagged, copy the new primitive operations
21492 -- from the private view to the full view.
21494 if Is_Tagged_Type (Full_T) then
21495 if Is_Tagged_Type (Priv_T) then
21496 Priv_List := Primitive_Operations (Priv_T);
21497 Prim_Elmt := First_Elmt (Priv_List);
21499 -- In the case of a concurrent type completing a private tagged
21500 -- type, primitives may have been declared in between the two
21501 -- views. These subprograms need to be wrapped the same way
21502 -- entries and protected procedures are handled because they
21503 -- cannot be directly shared by the two views.
21505 if Is_Concurrent_Type (Full_T) then
21506 declare
21507 Conc_Typ : constant Entity_Id :=
21508 Corresponding_Record_Type (Full_T);
21509 Curr_Nod : Node_Id := Parent (Conc_Typ);
21510 Wrap_Spec : Node_Id;
21512 begin
21513 while Present (Prim_Elmt) loop
21514 Prim := Node (Prim_Elmt);
21516 if Comes_From_Source (Prim)
21517 and then not Is_Abstract_Subprogram (Prim)
21518 then
21519 Wrap_Spec :=
21520 Make_Subprogram_Declaration (Sloc (Prim),
21521 Specification =>
21522 Build_Wrapper_Spec
21523 (Subp_Id => Prim,
21524 Obj_Typ => Conc_Typ,
21525 Formals =>
21526 Parameter_Specifications
21527 (Parent (Prim))));
21529 Insert_After (Curr_Nod, Wrap_Spec);
21530 Curr_Nod := Wrap_Spec;
21532 Analyze (Wrap_Spec);
21534 -- Remove the wrapper from visibility to avoid
21535 -- spurious conflict with the wrapped entity.
21537 Set_Is_Immediately_Visible
21538 (Defining_Entity (Specification (Wrap_Spec)),
21539 False);
21540 end if;
21542 Next_Elmt (Prim_Elmt);
21543 end loop;
21545 goto Leave;
21546 end;
21548 -- For nonconcurrent types, transfer explicit primitives, but
21549 -- omit those inherited from the parent of the private view
21550 -- since they will be re-inherited later on.
21552 else
21553 Full_List := Primitive_Operations (Full_T);
21554 while Present (Prim_Elmt) loop
21555 Prim := Node (Prim_Elmt);
21557 if Comes_From_Source (Prim)
21558 and then not Contains (Prim, Full_List)
21559 then
21560 Append_Elmt (Prim, Full_List);
21561 end if;
21563 Next_Elmt (Prim_Elmt);
21564 end loop;
21565 end if;
21567 -- Untagged private view
21569 else
21570 Full_List := Primitive_Operations (Full_T);
21572 -- In this case the partial view is untagged, so here we locate
21573 -- all of the earlier primitives that need to be treated as
21574 -- dispatching (those that appear between the two views). Note
21575 -- that these additional operations must all be new operations
21576 -- (any earlier operations that override inherited operations
21577 -- of the full view will already have been inserted in the
21578 -- primitives list, marked by Check_Operation_From_Private_View
21579 -- as dispatching. Note that implicit "/=" operators are
21580 -- excluded from being added to the primitives list since they
21581 -- shouldn't be treated as dispatching (tagged "/=" is handled
21582 -- specially).
21584 Prim := Next_Entity (Full_T);
21585 while Present (Prim) and then Prim /= Priv_T loop
21586 if Ekind (Prim) in E_Procedure | E_Function then
21587 Disp_Typ := Find_Dispatching_Type (Prim);
21589 if Disp_Typ = Full_T
21590 and then (Chars (Prim) /= Name_Op_Ne
21591 or else Comes_From_Source (Prim))
21592 then
21593 Check_Controlling_Formals (Full_T, Prim);
21595 if Is_Suitable_Primitive (Prim)
21596 and then not Is_Dispatching_Operation (Prim)
21597 then
21598 Append_Elmt (Prim, Full_List);
21599 Set_Is_Dispatching_Operation (Prim);
21600 Set_DT_Position_Value (Prim, No_Uint);
21601 end if;
21603 elsif Is_Dispatching_Operation (Prim)
21604 and then Disp_Typ /= Full_T
21605 then
21606 -- Verify that it is not otherwise controlled by a
21607 -- formal or a return value of type T.
21609 Check_Controlling_Formals (Disp_Typ, Prim);
21610 end if;
21611 end if;
21613 Next_Entity (Prim);
21614 end loop;
21615 end if;
21617 -- For the tagged case, the two views can share the same primitive
21618 -- operations list and the same class-wide type. Update attributes
21619 -- of the class-wide type which depend on the full declaration.
21621 if Is_Tagged_Type (Priv_T) then
21622 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21623 Set_Class_Wide_Type
21624 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21626 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21627 end if;
21629 -- For untagged types, copy the primitives across from the private
21630 -- view to the full view, for support of prefixed calls when
21631 -- extensions are enabled, and better error messages otherwise.
21633 else
21634 Priv_List := Primitive_Operations (Priv_T);
21635 Prim_Elmt := First_Elmt (Priv_List);
21637 Full_List := Primitive_Operations (Full_T);
21638 while Present (Prim_Elmt) loop
21639 Prim := Node (Prim_Elmt);
21640 Append_Elmt (Prim, Full_List);
21641 Next_Elmt (Prim_Elmt);
21642 end loop;
21643 end if;
21644 end;
21646 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21648 if Known_To_Have_Preelab_Init (Priv_T) then
21650 -- Case where there is a pragma Preelaborable_Initialization. We
21651 -- always allow this in predefined units, which is cheating a bit,
21652 -- but it means we don't have to struggle to meet the requirements in
21653 -- the RM for having Preelaborable Initialization. Otherwise we
21654 -- require that the type meets the RM rules. But we can't check that
21655 -- yet, because of the rule about overriding Initialize, so we simply
21656 -- set a flag that will be checked at freeze time.
21658 if not In_Predefined_Unit (Full_T) then
21659 Set_Must_Have_Preelab_Init (Full_T);
21660 end if;
21661 end if;
21663 -- If pragma CPP_Class was applied to the private type declaration,
21664 -- propagate it now to the full type declaration.
21666 if Is_CPP_Class (Priv_T) then
21667 Set_Is_CPP_Class (Full_T);
21668 Set_Convention (Full_T, Convention_CPP);
21670 -- Check that components of imported CPP types do not have default
21671 -- expressions.
21673 Check_CPP_Type_Has_No_Defaults (Full_T);
21674 end if;
21676 -- If the private view has user specified stream attributes, then so has
21677 -- the full view.
21679 -- Why the test, how could these flags be already set in Full_T ???
21681 if Has_Specified_Stream_Read (Priv_T) then
21682 Set_Has_Specified_Stream_Read (Full_T);
21683 end if;
21685 if Has_Specified_Stream_Write (Priv_T) then
21686 Set_Has_Specified_Stream_Write (Full_T);
21687 end if;
21689 if Has_Specified_Stream_Input (Priv_T) then
21690 Set_Has_Specified_Stream_Input (Full_T);
21691 end if;
21693 if Has_Specified_Stream_Output (Priv_T) then
21694 Set_Has_Specified_Stream_Output (Full_T);
21695 end if;
21697 -- Propagate Default_Initial_Condition-related attributes from the
21698 -- partial view to the full view.
21700 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21702 -- And to the underlying full view, if any
21704 if Is_Private_Type (Full_T)
21705 and then Present (Underlying_Full_View (Full_T))
21706 then
21707 Propagate_DIC_Attributes
21708 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21709 end if;
21711 -- Propagate invariant-related attributes from the partial view to the
21712 -- full view.
21714 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21716 -- And to the underlying full view, if any
21718 if Is_Private_Type (Full_T)
21719 and then Present (Underlying_Full_View (Full_T))
21720 then
21721 Propagate_Invariant_Attributes
21722 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21723 end if;
21725 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21726 -- in the full view without advertising the inheritance in the partial
21727 -- view. This can only occur when the partial view has no parent type
21728 -- and the full view has an interface as a parent. Any other scenarios
21729 -- are illegal because implemented interfaces must match between the
21730 -- two views.
21732 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21733 declare
21734 Full_Par : constant Entity_Id := Etype (Full_T);
21735 Priv_Par : constant Entity_Id := Etype (Priv_T);
21737 begin
21738 if not Is_Interface (Priv_Par)
21739 and then Is_Interface (Full_Par)
21740 and then Has_Inheritable_Invariants (Full_Par)
21741 then
21742 Error_Msg_N
21743 ("hidden inheritance of class-wide type invariants not "
21744 & "allowed", N);
21745 end if;
21746 end;
21747 end if;
21749 -- Propagate predicates to full type, and predicate function if already
21750 -- defined. It is not clear that this can actually happen? the partial
21751 -- view cannot be frozen yet, and the predicate function has not been
21752 -- built. Still it is a cheap check and seems safer to make it.
21754 Propagate_Predicate_Attributes (Full_T, Priv_T);
21756 if Is_Private_Type (Full_T)
21757 and then Present (Underlying_Full_View (Full_T))
21758 then
21759 Propagate_Predicate_Attributes
21760 (Underlying_Full_View (Full_T), Priv_T);
21761 end if;
21763 <<Leave>>
21764 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21765 end Process_Full_View;
21767 -----------------------------------
21768 -- Process_Incomplete_Dependents --
21769 -----------------------------------
21771 procedure Process_Incomplete_Dependents
21772 (N : Node_Id;
21773 Full_T : Entity_Id;
21774 Inc_T : Entity_Id)
21776 Inc_Elmt : Elmt_Id;
21777 Priv_Dep : Entity_Id;
21778 New_Subt : Entity_Id;
21780 Disc_Constraint : Elist_Id;
21782 begin
21783 if No (Private_Dependents (Inc_T)) then
21784 return;
21785 end if;
21787 -- Itypes that may be generated by the completion of an incomplete
21788 -- subtype are not used by the back-end and not attached to the tree.
21789 -- They are created only for constraint-checking purposes.
21791 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21792 while Present (Inc_Elmt) loop
21793 Priv_Dep := Node (Inc_Elmt);
21795 if Ekind (Priv_Dep) = E_Subprogram_Type then
21797 -- An Access_To_Subprogram type may have a return type or a
21798 -- parameter type that is incomplete. Replace with the full view.
21800 if Etype (Priv_Dep) = Inc_T then
21801 Set_Etype (Priv_Dep, Full_T);
21802 end if;
21804 declare
21805 Formal : Entity_Id;
21807 begin
21808 Formal := First_Formal (Priv_Dep);
21809 while Present (Formal) loop
21810 if Etype (Formal) = Inc_T then
21811 Set_Etype (Formal, Full_T);
21812 end if;
21814 Next_Formal (Formal);
21815 end loop;
21816 end;
21818 elsif Is_Overloadable (Priv_Dep) then
21820 -- If a subprogram in the incomplete dependents list is primitive
21821 -- for a tagged full type then mark it as a dispatching operation,
21822 -- check whether it overrides an inherited subprogram, and check
21823 -- restrictions on its controlling formals. Note that a protected
21824 -- operation is never dispatching: only its wrapper operation
21825 -- (which has convention Ada) is.
21827 if Is_Tagged_Type (Full_T)
21828 and then Is_Primitive (Priv_Dep)
21829 and then Convention (Priv_Dep) /= Convention_Protected
21830 then
21831 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21832 Set_Is_Dispatching_Operation (Priv_Dep);
21833 Check_Controlling_Formals (Full_T, Priv_Dep);
21834 end if;
21836 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21838 -- Can happen during processing of a body before the completion
21839 -- of a TA type. Ignore, because spec is also on dependent list.
21841 return;
21843 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21844 -- corresponding subtype of the full view.
21846 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21847 and then Comes_From_Source (Priv_Dep)
21848 then
21849 Set_Subtype_Indication
21850 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21851 Reinit_Field_To_Zero
21852 (Priv_Dep, F_Private_Dependents,
21853 Old_Ekind => E_Incomplete_Subtype);
21854 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21855 Set_Etype (Priv_Dep, Full_T);
21856 Set_Analyzed (Parent (Priv_Dep), False);
21858 -- Reanalyze the declaration, suppressing the call to Enter_Name
21859 -- to avoid duplicate names.
21861 Analyze_Subtype_Declaration
21862 (N => Parent (Priv_Dep),
21863 Skip => True);
21865 -- Dependent is a subtype
21867 else
21868 -- We build a new subtype indication using the full view of the
21869 -- incomplete parent. The discriminant constraints have been
21870 -- elaborated already at the point of the subtype declaration.
21872 New_Subt := Create_Itype (E_Void, N);
21874 if Has_Discriminants (Full_T) then
21875 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21876 else
21877 Disc_Constraint := No_Elist;
21878 end if;
21880 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21881 Set_Full_View (Priv_Dep, New_Subt);
21882 end if;
21884 Next_Elmt (Inc_Elmt);
21885 end loop;
21886 end Process_Incomplete_Dependents;
21888 --------------------------------
21889 -- Process_Range_Expr_In_Decl --
21890 --------------------------------
21892 procedure Process_Range_Expr_In_Decl
21893 (R : Node_Id;
21894 T : Entity_Id;
21895 Subtyp : Entity_Id := Empty;
21896 Check_List : List_Id := No_List)
21898 Lo, Hi : Node_Id;
21899 R_Checks : Check_Result;
21900 Insert_Node : Node_Id;
21901 Def_Id : Entity_Id;
21903 begin
21904 Analyze_And_Resolve (R, Base_Type (T));
21906 if Nkind (R) = N_Range then
21907 Lo := Low_Bound (R);
21908 Hi := High_Bound (R);
21910 -- Validity checks on the range of a quantified expression are
21911 -- delayed until the construct is transformed into a loop.
21913 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21914 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21915 then
21916 null;
21918 -- We need to ensure validity of the bounds here, because if we
21919 -- go ahead and do the expansion, then the expanded code will get
21920 -- analyzed with range checks suppressed and we miss the check.
21922 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21923 -- the temporaries generated by routine Remove_Side_Effects by means
21924 -- of validity checks must use the same names. When a range appears
21925 -- in the parent of a generic, the range is processed with checks
21926 -- disabled as part of the generic context and with checks enabled
21927 -- for code generation purposes. This leads to link issues as the
21928 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21929 -- template sees the temporaries generated by Remove_Side_Effects.
21931 else
21932 Validity_Check_Range (R, Subtyp);
21933 end if;
21935 -- If there were errors in the declaration, try and patch up some
21936 -- common mistakes in the bounds. The cases handled are literals
21937 -- which are Integer where the expected type is Real and vice versa.
21938 -- These corrections allow the compilation process to proceed further
21939 -- along since some basic assumptions of the format of the bounds
21940 -- are guaranteed.
21942 if Etype (R) = Any_Type then
21943 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21944 Rewrite (Lo,
21945 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21947 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21948 Rewrite (Hi,
21949 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21951 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21952 Rewrite (Lo,
21953 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21955 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21956 Rewrite (Hi,
21957 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21958 end if;
21960 Set_Etype (Lo, T);
21961 Set_Etype (Hi, T);
21962 end if;
21964 -- If the bounds of the range have been mistakenly given as string
21965 -- literals (perhaps in place of character literals), then an error
21966 -- has already been reported, but we rewrite the string literal as a
21967 -- bound of the range's type to avoid blowups in later processing
21968 -- that looks at static values.
21970 if Nkind (Lo) = N_String_Literal then
21971 Rewrite (Lo,
21972 Make_Attribute_Reference (Sloc (Lo),
21973 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21974 Attribute_Name => Name_First));
21975 Analyze_And_Resolve (Lo);
21976 end if;
21978 if Nkind (Hi) = N_String_Literal then
21979 Rewrite (Hi,
21980 Make_Attribute_Reference (Sloc (Hi),
21981 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21982 Attribute_Name => Name_First));
21983 Analyze_And_Resolve (Hi);
21984 end if;
21986 -- If bounds aren't scalar at this point then exit, avoiding
21987 -- problems with further processing of the range in this procedure.
21989 if not Is_Scalar_Type (Etype (Lo)) then
21990 return;
21991 end if;
21993 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21994 -- then range of the base type. Here we check whether the bounds
21995 -- are in the range of the subtype itself. Note that if the bounds
21996 -- represent the null range the Constraint_Error exception should
21997 -- not be raised.
21999 -- Capture values of bounds and generate temporaries for them
22000 -- if needed, before applying checks, since checks may cause
22001 -- duplication of the expression without forcing evaluation.
22003 -- The forced evaluation removes side effects from expressions,
22004 -- which should occur also in GNATprove mode. Otherwise, we end up
22005 -- with unexpected insertions of actions at places where this is
22006 -- not supposed to occur, e.g. on default parameters of a call.
22008 if Expander_Active or GNATprove_Mode then
22010 -- Call Force_Evaluation to create declarations as needed
22011 -- to deal with side effects, and also create typ_FIRST/LAST
22012 -- entities for bounds if we have a subtype name.
22014 -- Note: we do this transformation even if expansion is not
22015 -- active if we are in GNATprove_Mode since the transformation
22016 -- is in general required to ensure that the resulting tree has
22017 -- proper Ada semantics.
22019 Force_Evaluation
22020 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22021 Force_Evaluation
22022 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22023 end if;
22025 -- We use a flag here instead of suppressing checks on the type
22026 -- because the type we check against isn't necessarily the place
22027 -- where we put the check.
22029 R_Checks := Get_Range_Checks (R, T);
22031 -- Look up tree to find an appropriate insertion point. We can't
22032 -- just use insert_actions because later processing depends on
22033 -- the insertion node. Prior to Ada 2012 the insertion point could
22034 -- only be a declaration or a loop, but quantified expressions can
22035 -- appear within any context in an expression, and the insertion
22036 -- point can be any statement, pragma, or declaration.
22038 Insert_Node := Parent (R);
22039 while Present (Insert_Node) loop
22040 exit when
22041 Nkind (Insert_Node) in N_Declaration
22042 and then
22043 Nkind (Insert_Node) not in N_Component_Declaration
22044 | N_Loop_Parameter_Specification
22045 | N_Function_Specification
22046 | N_Procedure_Specification;
22048 exit when Nkind (Insert_Node) in
22049 N_Later_Decl_Item |
22050 N_Statement_Other_Than_Procedure_Call |
22051 N_Procedure_Call_Statement |
22052 N_Pragma;
22054 Insert_Node := Parent (Insert_Node);
22055 end loop;
22057 if Present (Insert_Node) then
22059 -- Case of loop statement. Verify that the range is part of the
22060 -- subtype indication of the iteration scheme.
22062 if Nkind (Insert_Node) = N_Loop_Statement then
22063 declare
22064 Indic : Node_Id;
22066 begin
22067 Indic := Parent (R);
22068 while Present (Indic)
22069 and then Nkind (Indic) /= N_Subtype_Indication
22070 loop
22071 Indic := Parent (Indic);
22072 end loop;
22074 if Present (Indic) then
22075 Def_Id := Etype (Subtype_Mark (Indic));
22077 Insert_Range_Checks
22078 (R_Checks,
22079 Insert_Node,
22080 Def_Id,
22081 Sloc (Insert_Node),
22082 Do_Before => True);
22083 end if;
22084 end;
22086 -- Case of declarations. If the declaration is for a type and
22087 -- involves discriminants, the checks are premature at the
22088 -- declaration point and need to wait for the expansion of the
22089 -- initialization procedure, which will pass in the list to put
22090 -- them on; otherwise, the checks are done at the declaration
22091 -- point and there is no need to do them again in the
22092 -- initialization procedure.
22094 elsif Nkind (Insert_Node) in N_Declaration then
22095 Def_Id := Defining_Identifier (Insert_Node);
22097 if (Ekind (Def_Id) = E_Record_Type
22098 and then Depends_On_Discriminant (R))
22099 or else
22100 (Ekind (Def_Id) = E_Protected_Type
22101 and then Has_Discriminants (Def_Id))
22102 then
22103 if Present (Check_List) then
22104 Append_Range_Checks
22105 (R_Checks,
22106 Check_List, Def_Id, Sloc (Insert_Node));
22107 end if;
22109 else
22110 if No (Check_List) then
22111 Insert_Range_Checks
22112 (R_Checks,
22113 Insert_Node, Def_Id, Sloc (Insert_Node));
22114 end if;
22115 end if;
22117 -- Case of statements. Drop the checks, as the range appears in
22118 -- the context of a quantified expression. Insertion will take
22119 -- place when expression is expanded.
22121 else
22122 null;
22123 end if;
22124 end if;
22126 -- Case of other than an explicit N_Range node
22128 -- The forced evaluation removes side effects from expressions, which
22129 -- should occur also in GNATprove mode. Otherwise, we end up with
22130 -- unexpected insertions of actions at places where this is not
22131 -- supposed to occur, e.g. on default parameters of a call.
22133 elsif Expander_Active or GNATprove_Mode then
22134 Get_Index_Bounds (R, Lo, Hi);
22135 Force_Evaluation (Lo);
22136 Force_Evaluation (Hi);
22137 end if;
22138 end Process_Range_Expr_In_Decl;
22140 --------------------------------------
22141 -- Process_Real_Range_Specification --
22142 --------------------------------------
22144 procedure Process_Real_Range_Specification (Def : Node_Id) is
22145 Spec : constant Node_Id := Real_Range_Specification (Def);
22146 Lo : Node_Id;
22147 Hi : Node_Id;
22148 Err : Boolean := False;
22150 procedure Analyze_Bound (N : Node_Id);
22151 -- Analyze and check one bound
22153 -------------------
22154 -- Analyze_Bound --
22155 -------------------
22157 procedure Analyze_Bound (N : Node_Id) is
22158 begin
22159 Analyze_And_Resolve (N, Any_Real);
22161 if not Is_OK_Static_Expression (N) then
22162 Flag_Non_Static_Expr
22163 ("bound in real type definition is not static!", N);
22164 Err := True;
22165 end if;
22166 end Analyze_Bound;
22168 -- Start of processing for Process_Real_Range_Specification
22170 begin
22171 if Present (Spec) then
22172 Lo := Low_Bound (Spec);
22173 Hi := High_Bound (Spec);
22174 Analyze_Bound (Lo);
22175 Analyze_Bound (Hi);
22177 -- If error, clear away junk range specification
22179 if Err then
22180 Set_Real_Range_Specification (Def, Empty);
22181 end if;
22182 end if;
22183 end Process_Real_Range_Specification;
22185 ---------------------
22186 -- Process_Subtype --
22187 ---------------------
22189 function Process_Subtype
22190 (S : Node_Id;
22191 Related_Nod : Node_Id;
22192 Related_Id : Entity_Id := Empty;
22193 Suffix : Character := ' ') return Entity_Id
22195 procedure Check_Incomplete (T : Node_Id);
22196 -- Called to verify that an incomplete type is not used prematurely
22198 ----------------------
22199 -- Check_Incomplete --
22200 ----------------------
22202 procedure Check_Incomplete (T : Node_Id) is
22203 begin
22204 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22206 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22207 and then
22208 not (Ada_Version >= Ada_2005
22209 and then
22210 (Nkind (Parent (T)) = N_Subtype_Declaration
22211 or else (Nkind (Parent (T)) = N_Subtype_Indication
22212 and then Nkind (Parent (Parent (T))) =
22213 N_Subtype_Declaration)))
22214 then
22215 Error_Msg_N ("invalid use of type before its full declaration", T);
22216 end if;
22217 end Check_Incomplete;
22219 -- Local variables
22221 P : Node_Id;
22222 Def_Id : Entity_Id;
22223 Error_Node : Node_Id;
22224 Full_View_Id : Entity_Id;
22225 Subtype_Mark_Id : Entity_Id;
22227 May_Have_Null_Exclusion : Boolean;
22229 -- Start of processing for Process_Subtype
22231 begin
22232 -- Case of no constraints present
22234 if Nkind (S) /= N_Subtype_Indication then
22235 Find_Type (S);
22237 -- No way to proceed if the subtype indication is malformed. This
22238 -- will happen for example when the subtype indication in an object
22239 -- declaration is missing altogether and the expression is analyzed
22240 -- as if it were that indication.
22242 if not Is_Entity_Name (S) then
22243 return Any_Type;
22244 end if;
22246 Check_Incomplete (S);
22247 P := Parent (S);
22249 -- The following mirroring of assertion in Null_Exclusion_Present is
22250 -- ugly, can't we have a range, a static predicate or even a flag???
22252 May_Have_Null_Exclusion :=
22253 Present (P)
22254 and then
22255 Nkind (P) in N_Access_Definition
22256 | N_Access_Function_Definition
22257 | N_Access_Procedure_Definition
22258 | N_Access_To_Object_Definition
22259 | N_Allocator
22260 | N_Component_Definition
22261 | N_Derived_Type_Definition
22262 | N_Discriminant_Specification
22263 | N_Formal_Object_Declaration
22264 | N_Function_Specification
22265 | N_Object_Declaration
22266 | N_Object_Renaming_Declaration
22267 | N_Parameter_Specification
22268 | N_Subtype_Declaration;
22270 -- Ada 2005 (AI-231): Static check
22272 if Ada_Version >= Ada_2005
22273 and then May_Have_Null_Exclusion
22274 and then Null_Exclusion_Present (P)
22275 and then Nkind (P) /= N_Access_To_Object_Definition
22276 and then not Is_Access_Type (Entity (S))
22277 then
22278 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22279 end if;
22281 -- Create an Itype that is a duplicate of Entity (S) but with the
22282 -- null-exclusion attribute.
22284 if May_Have_Null_Exclusion
22285 and then Is_Access_Type (Entity (S))
22286 and then Null_Exclusion_Present (P)
22288 -- No need to check the case of an access to object definition.
22289 -- It is correct to define double not-null pointers.
22291 -- Example:
22292 -- type Not_Null_Int_Ptr is not null access Integer;
22293 -- type Acc is not null access Not_Null_Int_Ptr;
22295 and then Nkind (P) /= N_Access_To_Object_Definition
22296 then
22297 if Can_Never_Be_Null (Entity (S)) then
22298 case Nkind (Related_Nod) is
22299 when N_Full_Type_Declaration =>
22300 if Nkind (Type_Definition (Related_Nod))
22301 in N_Array_Type_Definition
22302 then
22303 Error_Node :=
22304 Subtype_Indication
22305 (Component_Definition
22306 (Type_Definition (Related_Nod)));
22307 else
22308 Error_Node :=
22309 Subtype_Indication (Type_Definition (Related_Nod));
22310 end if;
22312 when N_Subtype_Declaration =>
22313 Error_Node := Subtype_Indication (Related_Nod);
22315 when N_Object_Declaration =>
22316 Error_Node := Object_Definition (Related_Nod);
22318 when N_Component_Declaration =>
22319 Error_Node :=
22320 Subtype_Indication (Component_Definition (Related_Nod));
22322 when N_Allocator =>
22323 Error_Node := Expression (Related_Nod);
22325 when others =>
22326 pragma Assert (False);
22327 Error_Node := Related_Nod;
22328 end case;
22330 Error_Msg_NE
22331 ("`NOT NULL` not allowed (& already excludes null)",
22332 Error_Node,
22333 Entity (S));
22334 end if;
22336 Set_Etype (S,
22337 Create_Null_Excluding_Itype
22338 (T => Entity (S),
22339 Related_Nod => P));
22340 Set_Entity (S, Etype (S));
22341 end if;
22343 return Entity (S);
22345 -- Case of constraint present, so that we have an N_Subtype_Indication
22346 -- node (this node is created only if constraints are present).
22348 else
22349 Find_Type (Subtype_Mark (S));
22351 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22352 and then not
22353 (Nkind (Parent (S)) = N_Subtype_Declaration
22354 and then Is_Itype (Defining_Identifier (Parent (S))))
22355 then
22356 Check_Incomplete (Subtype_Mark (S));
22357 end if;
22359 P := Parent (S);
22360 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22362 -- Explicit subtype declaration case
22364 if Nkind (P) = N_Subtype_Declaration then
22365 Def_Id := Defining_Identifier (P);
22367 -- Explicit derived type definition case
22369 elsif Nkind (P) = N_Derived_Type_Definition then
22370 Def_Id := Defining_Identifier (Parent (P));
22372 -- Implicit case, the Def_Id must be created as an implicit type.
22373 -- The one exception arises in the case of concurrent types, array
22374 -- and access types, where other subsidiary implicit types may be
22375 -- created and must appear before the main implicit type. In these
22376 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22377 -- has not yet been called to create Def_Id.
22379 else
22380 if Is_Array_Type (Subtype_Mark_Id)
22381 or else Is_Concurrent_Type (Subtype_Mark_Id)
22382 or else Is_Access_Type (Subtype_Mark_Id)
22383 then
22384 Def_Id := Empty;
22386 -- For the other cases, we create a new unattached Itype,
22387 -- and set the indication to ensure it gets attached later.
22389 else
22390 Def_Id :=
22391 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22392 end if;
22393 end if;
22395 -- If the kind of constraint is invalid for this kind of type,
22396 -- then give an error, and then pretend no constraint was given.
22398 if not Is_Valid_Constraint_Kind
22399 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22400 then
22401 Error_Msg_N
22402 ("incorrect constraint for this kind of type", Constraint (S));
22404 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22406 -- Set Ekind of orphan itype, to prevent cascaded errors
22408 if Present (Def_Id) then
22409 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22410 end if;
22412 -- Make recursive call, having got rid of the bogus constraint
22414 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22415 end if;
22417 -- Remaining processing depends on type. Select on Base_Type kind to
22418 -- ensure getting to the concrete type kind in the case of a private
22419 -- subtype (needed when only doing semantic analysis).
22421 case Ekind (Base_Type (Subtype_Mark_Id)) is
22422 when Access_Kind =>
22424 -- If this is a constraint on a class-wide type, discard it.
22425 -- There is currently no way to express a partial discriminant
22426 -- constraint on a type with unknown discriminants. This is
22427 -- a pathology that the ACATS wisely decides not to test.
22429 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22430 if Comes_From_Source (S) then
22431 Error_Msg_N
22432 ("constraint on class-wide type ignored??",
22433 Constraint (S));
22434 end if;
22436 if Nkind (P) = N_Subtype_Declaration then
22437 Set_Subtype_Indication (P,
22438 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22439 end if;
22441 return Subtype_Mark_Id;
22442 end if;
22444 Constrain_Access (Def_Id, S, Related_Nod);
22446 if Expander_Active
22447 and then Is_Itype (Designated_Type (Def_Id))
22448 and then Nkind (Related_Nod) = N_Subtype_Declaration
22449 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22450 then
22451 Build_Itype_Reference
22452 (Designated_Type (Def_Id), Related_Nod);
22453 end if;
22455 when Array_Kind =>
22456 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22458 when Decimal_Fixed_Point_Kind =>
22459 Constrain_Decimal (Def_Id, S);
22461 when Enumeration_Kind =>
22462 Constrain_Enumeration (Def_Id, S);
22464 when Ordinary_Fixed_Point_Kind =>
22465 Constrain_Ordinary_Fixed (Def_Id, S);
22467 when Float_Kind =>
22468 Constrain_Float (Def_Id, S);
22470 when Integer_Kind =>
22471 Constrain_Integer (Def_Id, S);
22473 when Class_Wide_Kind
22474 | E_Incomplete_Type
22475 | E_Record_Subtype
22476 | E_Record_Type
22478 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22480 if Ekind (Def_Id) = E_Incomplete_Type then
22481 Set_Private_Dependents (Def_Id, New_Elmt_List);
22482 end if;
22484 when Private_Kind =>
22486 -- A private type with unknown discriminants may be completed
22487 -- by an unconstrained array type.
22489 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22490 and then Present (Full_View (Subtype_Mark_Id))
22491 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22492 then
22493 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22495 -- ... but more commonly is completed by a discriminated record
22496 -- type.
22498 else
22499 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22500 end if;
22502 -- The base type may be private but Def_Id may be a full view
22503 -- in an instance.
22505 if Is_Private_Type (Def_Id) then
22506 Set_Private_Dependents (Def_Id, New_Elmt_List);
22507 end if;
22509 -- In case of an invalid constraint prevent further processing
22510 -- since the type constructed is missing expected fields.
22512 if Etype (Def_Id) = Any_Type then
22513 return Def_Id;
22514 end if;
22516 -- If the full view is that of a task with discriminants,
22517 -- we must constrain both the concurrent type and its
22518 -- corresponding record type. Otherwise we will just propagate
22519 -- the constraint to the full view, if available.
22521 if Present (Full_View (Subtype_Mark_Id))
22522 and then Has_Discriminants (Subtype_Mark_Id)
22523 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22524 then
22525 Full_View_Id :=
22526 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22528 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22529 Constrain_Concurrent (Full_View_Id, S,
22530 Related_Nod, Related_Id, Suffix);
22531 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22532 Set_Full_View (Def_Id, Full_View_Id);
22534 -- Introduce an explicit reference to the private subtype,
22535 -- to prevent scope anomalies in gigi if first use appears
22536 -- in a nested context, e.g. a later function body.
22537 -- Should this be generated in other contexts than a full
22538 -- type declaration?
22540 if Is_Itype (Def_Id)
22541 and then
22542 Nkind (Parent (P)) = N_Full_Type_Declaration
22543 then
22544 Build_Itype_Reference (Def_Id, Parent (P));
22545 end if;
22547 else
22548 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22549 end if;
22551 when Concurrent_Kind =>
22552 Constrain_Concurrent (Def_Id, S,
22553 Related_Nod, Related_Id, Suffix);
22555 when others =>
22556 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22557 end case;
22559 -- Size, Alignment, Representation aspects and Convention are always
22560 -- inherited from the base type.
22562 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22563 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22564 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22566 -- The anonymous subtype created for the subtype indication
22567 -- inherits the predicates of the parent.
22569 if Has_Predicates (Subtype_Mark_Id) then
22570 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22572 -- Indicate where the predicate function may be found
22574 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22575 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22576 end if;
22577 end if;
22579 return Def_Id;
22580 end if;
22581 end Process_Subtype;
22583 -----------------------------
22584 -- Record_Type_Declaration --
22585 -----------------------------
22587 procedure Record_Type_Declaration
22588 (T : Entity_Id;
22589 N : Node_Id;
22590 Prev : Entity_Id)
22592 Def : constant Node_Id := Type_Definition (N);
22593 Is_Tagged : Boolean;
22594 Tag_Comp : Entity_Id;
22596 begin
22597 -- These flags must be initialized before calling Process_Discriminants
22598 -- because this routine makes use of them.
22600 Mutate_Ekind (T, E_Record_Type);
22601 Set_Etype (T, T);
22602 Reinit_Size_Align (T);
22603 Set_Interfaces (T, No_Elist);
22604 Set_Stored_Constraint (T, No_Elist);
22605 Set_Default_SSO (T);
22606 Set_No_Reordering (T, No_Component_Reordering);
22608 -- Normal case
22610 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22611 -- The flag Is_Tagged_Type might have already been set by
22612 -- Find_Type_Name if it detected an error for declaration T. This
22613 -- arises in the case of private tagged types where the full view
22614 -- omits the word tagged.
22616 Is_Tagged :=
22617 Tagged_Present (Def)
22618 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22620 Set_Is_Limited_Record (T, Limited_Present (Def));
22622 if Is_Tagged then
22623 Set_Is_Tagged_Type (T, True);
22624 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22625 end if;
22627 -- Type is abstract if full declaration carries keyword, or if
22628 -- previous partial view did.
22630 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22631 or else Abstract_Present (Def));
22633 else
22634 Is_Tagged := True;
22635 Analyze_Interface_Declaration (T, Def);
22637 if Present (Discriminant_Specifications (N)) then
22638 Error_Msg_N
22639 ("interface types cannot have discriminants",
22640 Defining_Identifier
22641 (First (Discriminant_Specifications (N))));
22642 end if;
22643 end if;
22645 -- First pass: if there are self-referential access components,
22646 -- create the required anonymous access type declarations, and if
22647 -- need be an incomplete type declaration for T itself.
22649 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22651 if Ada_Version >= Ada_2005
22652 and then Present (Interface_List (Def))
22653 then
22654 Check_Interfaces (N, Def);
22656 declare
22657 Ifaces_List : Elist_Id;
22659 begin
22660 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22661 -- already in the parents.
22663 Collect_Interfaces
22664 (T => T,
22665 Ifaces_List => Ifaces_List,
22666 Exclude_Parents => True);
22668 Set_Interfaces (T, Ifaces_List);
22669 end;
22670 end if;
22672 -- Records constitute a scope for the component declarations within.
22673 -- The scope is created prior to the processing of these declarations.
22674 -- Discriminants are processed first, so that they are visible when
22675 -- processing the other components. The Ekind of the record type itself
22676 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22678 -- Enter record scope
22680 Push_Scope (T);
22682 -- If an incomplete or private type declaration was already given for
22683 -- the type, then this scope already exists, and the discriminants have
22684 -- been declared within. We must verify that the full declaration
22685 -- matches the incomplete one.
22687 Check_Or_Process_Discriminants (N, T, Prev);
22689 Set_Is_Constrained (T, not Has_Discriminants (T));
22690 Set_Has_Delayed_Freeze (T, True);
22692 -- For tagged types add a manually analyzed component corresponding
22693 -- to the component _tag, the corresponding piece of tree will be
22694 -- expanded as part of the freezing actions if it is not a CPP_Class.
22696 if Is_Tagged then
22698 -- Do not add the tag unless we are in expansion mode
22700 if Expander_Active then
22701 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22702 Enter_Name (Tag_Comp);
22704 Mutate_Ekind (Tag_Comp, E_Component);
22705 Set_Is_Tag (Tag_Comp);
22706 Set_Is_Aliased (Tag_Comp);
22707 Set_Is_Independent (Tag_Comp);
22708 Set_Etype (Tag_Comp, RTE (RE_Tag));
22709 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22710 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22711 Reinit_Component_Location (Tag_Comp);
22713 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22714 -- implemented interfaces.
22716 if Has_Interfaces (T) then
22717 Add_Interface_Tag_Components (N, T);
22718 end if;
22719 end if;
22721 Make_Class_Wide_Type (T);
22722 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22723 end if;
22725 -- We must suppress range checks when processing record components in
22726 -- the presence of discriminants, since we don't want spurious checks to
22727 -- be generated during their analysis, but Suppress_Range_Checks flags
22728 -- must be reset the after processing the record definition.
22730 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22731 -- couldn't we just use the normal range check suppression method here.
22732 -- That would seem cleaner ???
22734 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22735 Set_Kill_Range_Checks (T, True);
22736 Record_Type_Definition (Def, Prev);
22737 Set_Kill_Range_Checks (T, False);
22738 else
22739 Record_Type_Definition (Def, Prev);
22740 end if;
22742 -- Exit from record scope
22744 End_Scope;
22746 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22747 -- the implemented interfaces and associate them an aliased entity.
22749 if Is_Tagged
22750 and then not Is_Empty_List (Interface_List (Def))
22751 then
22752 Derive_Progenitor_Subprograms (T, T);
22753 end if;
22755 Check_Function_Writable_Actuals (N);
22756 end Record_Type_Declaration;
22758 ----------------------------
22759 -- Record_Type_Definition --
22760 ----------------------------
22762 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22763 Component : Entity_Id;
22764 Ctrl_Components : Boolean := False;
22765 Final_Storage_Only : Boolean;
22766 T : Entity_Id;
22768 begin
22769 if Ekind (Prev_T) = E_Incomplete_Type then
22770 T := Full_View (Prev_T);
22771 else
22772 T := Prev_T;
22773 end if;
22775 Final_Storage_Only := not Is_Controlled (T);
22777 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22778 -- type declaration.
22780 if Parent_Kind (Def) = N_Derived_Type_Definition
22781 and then Limited_Present (Parent (Def))
22782 then
22783 Set_Is_Limited_Record (T);
22784 end if;
22786 -- If the component list of a record type is defined by the reserved
22787 -- word null and there is no discriminant part, then the record type has
22788 -- no components and all records of the type are null records (RM 3.7)
22789 -- This procedure is also called to process the extension part of a
22790 -- record extension, in which case the current scope may have inherited
22791 -- components.
22793 if Present (Def)
22794 and then Present (Component_List (Def))
22795 and then not Null_Present (Component_List (Def))
22796 then
22797 Analyze_Declarations (Component_Items (Component_List (Def)));
22799 if Present (Variant_Part (Component_List (Def))) then
22800 Analyze (Variant_Part (Component_List (Def)));
22801 end if;
22802 end if;
22804 -- After completing the semantic analysis of the record definition,
22805 -- record components, both new and inherited, are accessible. Set their
22806 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22807 -- whose Ekind may be void.
22809 Component := First_Entity (Current_Scope);
22810 while Present (Component) loop
22811 if Ekind (Component) = E_Void
22812 and then not Is_Itype (Component)
22813 then
22814 Mutate_Ekind (Component, E_Component);
22815 Reinit_Component_Location (Component);
22816 end if;
22818 Propagate_Concurrent_Flags (T, Etype (Component));
22820 if Ekind (Component) /= E_Component then
22821 null;
22823 -- Do not set Has_Controlled_Component on a class-wide equivalent
22824 -- type. See Make_CW_Equivalent_Type.
22826 elsif not Is_Class_Wide_Equivalent_Type (T)
22827 and then (Has_Controlled_Component (Etype (Component))
22828 or else (Chars (Component) /= Name_uParent
22829 and then Is_Controlled (Etype (Component))))
22830 then
22831 Set_Has_Controlled_Component (T, True);
22832 Final_Storage_Only :=
22833 Final_Storage_Only
22834 and then Finalize_Storage_Only (Etype (Component));
22835 Ctrl_Components := True;
22836 end if;
22838 Next_Entity (Component);
22839 end loop;
22841 -- A Type is Finalize_Storage_Only only if all its controlled components
22842 -- are also.
22844 if Ctrl_Components then
22845 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22846 end if;
22848 -- Place reference to end record on the proper entity, which may
22849 -- be a partial view.
22851 if Present (Def) then
22852 Process_End_Label (Def, 'e', Prev_T);
22853 end if;
22854 end Record_Type_Definition;
22856 ---------------------------
22857 -- Replace_Discriminants --
22858 ---------------------------
22860 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22861 function Process (N : Node_Id) return Traverse_Result;
22863 -------------
22864 -- Process --
22865 -------------
22867 function Process (N : Node_Id) return Traverse_Result is
22868 Comp : Entity_Id;
22870 begin
22871 if Nkind (N) = N_Discriminant_Specification then
22872 Comp := First_Discriminant (Typ);
22873 while Present (Comp) loop
22874 if Original_Record_Component (Comp) = Defining_Identifier (N)
22875 or else Chars (Comp) = Chars (Defining_Identifier (N))
22876 then
22877 Set_Defining_Identifier (N, Comp);
22878 exit;
22879 end if;
22881 Next_Discriminant (Comp);
22882 end loop;
22884 elsif Nkind (N) = N_Variant_Part then
22885 Comp := First_Discriminant (Typ);
22886 while Present (Comp) loop
22887 if Original_Record_Component (Comp) = Entity (Name (N))
22888 or else Chars (Comp) = Chars (Name (N))
22889 then
22890 -- Make sure to preserve the type coming from the parent on
22891 -- the Name, even if the subtype of the discriminant can be
22892 -- constrained, so that discrete choices inherited from the
22893 -- parent in the variant part are not flagged as violating
22894 -- the constraints of the subtype.
22896 declare
22897 Typ : constant Entity_Id := Etype (Name (N));
22898 begin
22899 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22900 Set_Etype (Name (N), Typ);
22901 end;
22902 exit;
22903 end if;
22905 Next_Discriminant (Comp);
22906 end loop;
22907 end if;
22909 return OK;
22910 end Process;
22912 procedure Replace is new Traverse_Proc (Process);
22914 -- Start of processing for Replace_Discriminants
22916 begin
22917 Replace (Decl);
22918 end Replace_Discriminants;
22920 -------------------------------
22921 -- Set_Completion_Referenced --
22922 -------------------------------
22924 procedure Set_Completion_Referenced (E : Entity_Id) is
22925 begin
22926 -- If in main unit, mark entity that is a completion as referenced,
22927 -- warnings go on the partial view when needed.
22929 if In_Extended_Main_Source_Unit (E) then
22930 Set_Referenced (E);
22931 end if;
22932 end Set_Completion_Referenced;
22934 ---------------------
22935 -- Set_Default_SSO --
22936 ---------------------
22938 procedure Set_Default_SSO (T : Entity_Id) is
22939 begin
22940 case Opt.Default_SSO is
22941 when ' ' =>
22942 null;
22943 when 'L' =>
22944 Set_SSO_Set_Low_By_Default (T, True);
22945 when 'H' =>
22946 Set_SSO_Set_High_By_Default (T, True);
22947 when others =>
22948 raise Program_Error;
22949 end case;
22950 end Set_Default_SSO;
22952 ---------------------
22953 -- Set_Fixed_Range --
22954 ---------------------
22956 -- The range for fixed-point types is complicated by the fact that we
22957 -- do not know the exact end points at the time of the declaration. This
22958 -- is true for three reasons:
22960 -- A size clause may affect the fudging of the end-points.
22961 -- A small clause may affect the values of the end-points.
22962 -- We try to include the end-points if it does not affect the size.
22964 -- This means that the actual end-points must be established at the
22965 -- point when the type is frozen. Meanwhile, we first narrow the range
22966 -- as permitted (so that it will fit if necessary in a small specified
22967 -- size), and then build a range subtree with these narrowed bounds.
22968 -- Set_Fixed_Range constructs the range from real literal values, and
22969 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22971 -- The parent of this range is set to point to the entity so that it is
22972 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22973 -- other scalar types, which are just pointers to the range in the
22974 -- original tree, this would otherwise be an orphan).
22976 -- The tree is left unanalyzed. When the type is frozen, the processing
22977 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22978 -- analyzed, and uses this as an indication that it should complete
22979 -- work on the range (it will know the final small and size values).
22981 procedure Set_Fixed_Range
22982 (E : Entity_Id;
22983 Loc : Source_Ptr;
22984 Lo : Ureal;
22985 Hi : Ureal)
22987 S : constant Node_Id :=
22988 Make_Range (Loc,
22989 Low_Bound => Make_Real_Literal (Loc, Lo),
22990 High_Bound => Make_Real_Literal (Loc, Hi));
22991 begin
22992 Set_Scalar_Range (E, S);
22993 Set_Parent (S, E);
22995 -- Before the freeze point, the bounds of a fixed point are universal
22996 -- and carry the corresponding type.
22998 Set_Etype (Low_Bound (S), Universal_Real);
22999 Set_Etype (High_Bound (S), Universal_Real);
23000 end Set_Fixed_Range;
23002 ----------------------------------
23003 -- Set_Scalar_Range_For_Subtype --
23004 ----------------------------------
23006 procedure Set_Scalar_Range_For_Subtype
23007 (Def_Id : Entity_Id;
23008 R : Node_Id;
23009 Subt : Entity_Id)
23011 Kind : constant Entity_Kind := Ekind (Def_Id);
23013 begin
23014 -- Defend against previous error
23016 if Nkind (R) = N_Error then
23017 return;
23018 end if;
23020 Set_Scalar_Range (Def_Id, R);
23022 -- We need to link the range into the tree before resolving it so
23023 -- that types that are referenced, including importantly the subtype
23024 -- itself, are properly frozen (Freeze_Expression requires that the
23025 -- expression be properly linked into the tree). Of course if it is
23026 -- already linked in, then we do not disturb the current link.
23028 if No (Parent (R)) then
23029 Set_Parent (R, Def_Id);
23030 end if;
23032 -- Reset the kind of the subtype during analysis of the range, to
23033 -- catch possible premature use in the bounds themselves.
23035 Mutate_Ekind (Def_Id, E_Void);
23036 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23037 Mutate_Ekind (Def_Id, Kind);
23038 end Set_Scalar_Range_For_Subtype;
23040 --------------------------------------------------------
23041 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23042 --------------------------------------------------------
23044 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23045 (E : Entity_Id)
23047 begin
23048 -- Make sure set if encountered during Expand_To_Stored_Constraint
23050 Set_Stored_Constraint (E, No_Elist);
23052 -- Give it the right value
23054 if Is_Constrained (E) and then Has_Discriminants (E) then
23055 Set_Stored_Constraint (E,
23056 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23057 end if;
23058 end Set_Stored_Constraint_From_Discriminant_Constraint;
23060 -------------------------------------
23061 -- Signed_Integer_Type_Declaration --
23062 -------------------------------------
23064 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23065 Implicit_Base : Entity_Id;
23066 Base_Typ : Entity_Id;
23067 Lo_Val : Uint;
23068 Hi_Val : Uint;
23069 Errs : Boolean := False;
23070 Lo : Node_Id;
23071 Hi : Node_Id;
23073 function Can_Derive_From (E : Entity_Id) return Boolean;
23074 -- Determine whether given bounds allow derivation from specified type
23076 procedure Check_Bound (Expr : Node_Id);
23077 -- Check bound to make sure it is integral and static. If not, post
23078 -- appropriate error message and set Errs flag
23080 ---------------------
23081 -- Can_Derive_From --
23082 ---------------------
23084 -- Note we check both bounds against both end values, to deal with
23085 -- strange types like ones with a range of 0 .. -12341234.
23087 function Can_Derive_From (E : Entity_Id) return Boolean is
23088 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23089 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23090 begin
23091 return Lo <= Lo_Val and then Lo_Val <= Hi
23092 and then
23093 Lo <= Hi_Val and then Hi_Val <= Hi;
23094 end Can_Derive_From;
23096 -----------------
23097 -- Check_Bound --
23098 -----------------
23100 procedure Check_Bound (Expr : Node_Id) is
23101 begin
23102 -- If a range constraint is used as an integer type definition, each
23103 -- bound of the range must be defined by a static expression of some
23104 -- integer type, but the two bounds need not have the same integer
23105 -- type (Negative bounds are allowed.) (RM 3.5.4)
23107 if not Is_Integer_Type (Etype (Expr)) then
23108 Error_Msg_N
23109 ("integer type definition bounds must be of integer type", Expr);
23110 Errs := True;
23112 elsif not Is_OK_Static_Expression (Expr) then
23113 Flag_Non_Static_Expr
23114 ("non-static expression used for integer type bound!", Expr);
23115 Errs := True;
23117 -- Otherwise the bounds are folded into literals
23119 elsif Is_Entity_Name (Expr) then
23120 Fold_Uint (Expr, Expr_Value (Expr), True);
23121 end if;
23122 end Check_Bound;
23124 -- Start of processing for Signed_Integer_Type_Declaration
23126 begin
23127 -- Create an anonymous base type
23129 Implicit_Base :=
23130 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23132 -- Analyze and check the bounds, they can be of any integer type
23134 Lo := Low_Bound (Def);
23135 Hi := High_Bound (Def);
23137 -- Arbitrarily use Integer as the type if either bound had an error
23139 if Hi = Error or else Lo = Error then
23140 Base_Typ := Any_Integer;
23141 Set_Error_Posted (T, True);
23142 Errs := True;
23144 -- Here both bounds are OK expressions
23146 else
23147 Analyze_And_Resolve (Lo, Any_Integer);
23148 Analyze_And_Resolve (Hi, Any_Integer);
23150 Check_Bound (Lo);
23151 Check_Bound (Hi);
23153 if Errs then
23154 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23155 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23156 end if;
23158 -- Find type to derive from
23160 Lo_Val := Expr_Value (Lo);
23161 Hi_Val := Expr_Value (Hi);
23163 if Can_Derive_From (Standard_Short_Short_Integer) then
23164 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23166 elsif Can_Derive_From (Standard_Short_Integer) then
23167 Base_Typ := Base_Type (Standard_Short_Integer);
23169 elsif Can_Derive_From (Standard_Integer) then
23170 Base_Typ := Base_Type (Standard_Integer);
23172 elsif Can_Derive_From (Standard_Long_Integer) then
23173 Base_Typ := Base_Type (Standard_Long_Integer);
23175 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23176 Check_Restriction (No_Long_Long_Integers, Def);
23177 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23179 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23180 Check_Restriction (No_Long_Long_Integers, Def);
23181 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23183 else
23184 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23185 Error_Msg_N ("integer type definition bounds out of range", Def);
23186 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23187 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23188 end if;
23189 end if;
23191 -- Set the type of the bounds to the implicit base: we cannot set it to
23192 -- the new type, because this would be a forward reference for the code
23193 -- generator and, if the original type is user-defined, this could even
23194 -- lead to spurious semantic errors. Furthermore we do not set it to be
23195 -- universal, because this could make it much larger than needed here.
23197 if not Errs then
23198 Set_Etype (Lo, Implicit_Base);
23199 Set_Etype (Hi, Implicit_Base);
23200 end if;
23202 -- Complete both implicit base and declared first subtype entities. The
23203 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23204 -- are not clobbered when the signed integer type acts as a full view of
23205 -- a private type.
23207 Set_Etype (Implicit_Base, Base_Typ);
23208 Set_Size_Info (Implicit_Base, Base_Typ);
23209 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23210 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23211 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23213 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23214 Set_Etype (T, Implicit_Base);
23215 Set_Size_Info (T, Implicit_Base);
23216 Inherit_Rep_Item_Chain (T, Implicit_Base);
23217 Set_Scalar_Range (T, Def);
23218 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23219 Set_Is_Constrained (T);
23220 end Signed_Integer_Type_Declaration;
23222 end Sem_Ch3;