ada: Remove the body of System.Storage_Elements
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob50ccb3903633c976de782b3e602316defae72b4d
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));
4974 Freeze_Before (N, Act_T);
4976 elsif Nkind (E) = N_Function_Call
4977 and then Constant_Present (N)
4978 and then Has_Unconstrained_Elements (Etype (E))
4979 then
4980 -- The back-end has problems with constants of a discriminated type
4981 -- with defaults, if the initial value is a function call. We
4982 -- generate an intermediate temporary that will receive a reference
4983 -- to the result of the call. The initialization expression then
4984 -- becomes a dereference of that temporary.
4986 Remove_Side_Effects (E);
4988 -- If this is a constant declaration of an unconstrained type and
4989 -- the initialization is an aggregate, we can use the subtype of the
4990 -- aggregate for the declared entity because it is immutable.
4992 elsif not Is_Constrained (T)
4993 and then Has_Discriminants (T)
4994 and then Constant_Present (N)
4995 and then not Has_Unchecked_Union (T)
4996 and then Nkind (E) = N_Aggregate
4997 then
4998 Act_T := Etype (E);
4999 end if;
5001 -- Check No_Wide_Characters restriction
5003 Check_Wide_Character_Restriction (T, Object_Definition (N));
5005 -- Indicate this is not set in source. Certainly true for constants, and
5006 -- true for variables so far (will be reset for a variable if and when
5007 -- we encounter a modification in the source).
5009 Set_Never_Set_In_Source (Id);
5011 -- Now establish the proper kind and type of the object
5013 if Ekind (Id) = E_Void then
5014 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5015 end if;
5017 if Constant_Present (N) then
5018 Mutate_Ekind (Id, E_Constant);
5019 Set_Is_True_Constant (Id);
5021 else
5022 Mutate_Ekind (Id, E_Variable);
5024 -- A variable is set as shared passive if it appears in a shared
5025 -- passive package, and is at the outer level. This is not done for
5026 -- entities generated during expansion, because those are always
5027 -- manipulated locally.
5029 if Is_Shared_Passive (Current_Scope)
5030 and then Is_Library_Level_Entity (Id)
5031 and then Comes_From_Source (Id)
5032 then
5033 Set_Is_Shared_Passive (Id);
5034 Check_Shared_Var (Id, T, N);
5035 end if;
5037 -- Set Has_Initial_Value if initializing expression present. Note
5038 -- that if there is no initializing expression, we leave the state
5039 -- of this flag unchanged (usually it will be False, but notably in
5040 -- the case of exception choice variables, it will already be true).
5042 if Present (E) then
5043 Set_Has_Initial_Value (Id);
5044 end if;
5045 end if;
5047 -- Set the SPARK mode from the current context (may be overwritten later
5048 -- with explicit pragma).
5050 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5051 Set_SPARK_Pragma_Inherited (Id);
5053 -- Preserve relevant elaboration-related attributes of the context which
5054 -- are no longer available or very expensive to recompute once analysis,
5055 -- resolution, and expansion are over.
5057 Mark_Elaboration_Attributes
5058 (N_Id => Id,
5059 Checks => True,
5060 Warnings => True);
5062 -- Initialize alignment and size and capture alignment setting
5064 Reinit_Alignment (Id);
5065 Reinit_Esize (Id);
5066 Set_Optimize_Alignment_Flags (Id);
5068 -- Deal with aliased case
5070 if Aliased_Present (N) then
5071 Set_Is_Aliased (Id);
5073 -- AI12-001: All aliased objects are considered to be specified as
5074 -- independently addressable (RM C.6(8.1/4)).
5076 Set_Is_Independent (Id);
5078 -- If the object is aliased and the type is unconstrained with
5079 -- defaulted discriminants and there is no expression, then the
5080 -- object is constrained by the defaults, so it is worthwhile
5081 -- building the corresponding subtype.
5083 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5084 -- unconstrained, then only establish an actual subtype if the
5085 -- nominal subtype is indefinite. In definite cases the object is
5086 -- unconstrained in Ada 2005.
5088 if No (E)
5089 and then Is_Record_Type (T)
5090 and then not Is_Constrained (T)
5091 and then Has_Discriminants (T)
5092 and then (Ada_Version < Ada_2005
5093 or else not Is_Definite_Subtype (T))
5094 then
5095 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5096 end if;
5097 end if;
5099 -- Now we can set the type of the object
5101 Set_Etype (Id, Act_T);
5103 -- Non-constant object is marked to be treated as volatile if type is
5104 -- volatile and we clear the Current_Value setting that may have been
5105 -- set above. Doing so for constants isn't required and might interfere
5106 -- with possible uses of the object as a static expression in contexts
5107 -- incompatible with volatility (e.g. as a case-statement alternative).
5109 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5110 Set_Treat_As_Volatile (Id);
5111 Set_Current_Value (Id, Empty);
5112 end if;
5114 -- Deal with controlled types
5116 if Has_Controlled_Component (Etype (Id))
5117 or else Is_Controlled (Etype (Id))
5118 then
5119 if not Is_Library_Level_Entity (Id) then
5120 Check_Restriction (No_Nested_Finalization, N);
5121 else
5122 Validate_Controlled_Object (Id);
5123 end if;
5124 end if;
5126 if Has_Task (Etype (Id)) then
5127 Check_Restriction (No_Tasking, N);
5129 -- Deal with counting max tasks
5131 -- Nothing to do if inside a generic
5133 if Inside_A_Generic then
5134 null;
5136 -- If library level entity, then count tasks
5138 elsif Is_Library_Level_Entity (Id) then
5139 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5141 -- If not library level entity, then indicate we don't know max
5142 -- tasks and also check task hierarchy restriction and blocking
5143 -- operation (since starting a task is definitely blocking).
5145 else
5146 Check_Restriction (Max_Tasks, N);
5147 Check_Restriction (No_Task_Hierarchy, N);
5148 Check_Potentially_Blocking_Operation (N);
5149 end if;
5151 -- A rather specialized test. If we see two tasks being declared
5152 -- of the same type in the same object declaration, and the task
5153 -- has an entry with an address clause, we know that program error
5154 -- will be raised at run time since we can't have two tasks with
5155 -- entries at the same address.
5157 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5158 declare
5159 E : Entity_Id;
5161 begin
5162 E := First_Entity (Etype (Id));
5163 while Present (E) loop
5164 if Ekind (E) = E_Entry
5165 and then Present (Get_Attribute_Definition_Clause
5166 (E, Attribute_Address))
5167 then
5168 Error_Msg_Warn := SPARK_Mode /= On;
5169 Error_Msg_N
5170 ("more than one task with same entry address<<", N);
5171 Error_Msg_N ("\Program_Error [<<", N);
5172 Insert_Action (N,
5173 Make_Raise_Program_Error (Loc,
5174 Reason => PE_Duplicated_Entry_Address));
5175 exit;
5176 end if;
5178 Next_Entity (E);
5179 end loop;
5180 end;
5181 end if;
5182 end if;
5184 -- Check specific legality rules for a return object
5186 if Is_Return_Object (Id) then
5187 Check_Return_Subtype_Indication (N);
5188 end if;
5190 -- Some simple constant-propagation: if the expression is a constant
5191 -- string initialized with a literal, share the literal. This avoids
5192 -- a run-time copy.
5194 if Present (E)
5195 and then Is_Entity_Name (E)
5196 and then Ekind (Entity (E)) = E_Constant
5197 and then Base_Type (Etype (E)) = Standard_String
5198 then
5199 declare
5200 Val : constant Node_Id := Constant_Value (Entity (E));
5201 begin
5202 if Present (Val) and then Nkind (Val) = N_String_Literal then
5203 Rewrite (E, New_Copy (Val));
5204 end if;
5205 end;
5206 end if;
5208 if Present (Prev_Entity)
5209 and then Is_Frozen (Prev_Entity)
5210 and then not Error_Posted (Id)
5211 then
5212 Error_Msg_N ("full constant declaration appears too late", N);
5213 end if;
5215 Check_Eliminated (Id);
5217 -- Deal with setting In_Private_Part flag if in private part
5219 if Ekind (Scope (Id)) = E_Package
5220 and then In_Private_Part (Scope (Id))
5221 then
5222 Set_In_Private_Part (Id);
5223 end if;
5225 <<Leave>>
5226 -- Initialize the refined state of a variable here because this is a
5227 -- common destination for legal and illegal object declarations.
5229 if Ekind (Id) = E_Variable then
5230 Set_Encapsulating_State (Id, Empty);
5231 end if;
5233 if Has_Aspects (N) then
5234 Analyze_Aspect_Specifications (N, Id);
5235 end if;
5237 Analyze_Dimension (N);
5239 -- Verify whether the object declaration introduces an illegal hidden
5240 -- state within a package subject to a null abstract state.
5242 if Ekind (Id) = E_Variable then
5243 Check_No_Hidden_State (Id);
5244 end if;
5246 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5247 end Analyze_Object_Declaration;
5249 ---------------------------
5250 -- Analyze_Others_Choice --
5251 ---------------------------
5253 -- Nothing to do for the others choice node itself, the semantic analysis
5254 -- of the others choice will occur as part of the processing of the parent
5256 procedure Analyze_Others_Choice (N : Node_Id) is
5257 pragma Warnings (Off, N);
5258 begin
5259 null;
5260 end Analyze_Others_Choice;
5262 -------------------------------------------
5263 -- Analyze_Private_Extension_Declaration --
5264 -------------------------------------------
5266 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5267 Indic : constant Node_Id := Subtype_Indication (N);
5268 T : constant Entity_Id := Defining_Identifier (N);
5269 Iface : Entity_Id;
5270 Iface_Elmt : Elmt_Id;
5271 Parent_Base : Entity_Id;
5272 Parent_Type : Entity_Id;
5274 begin
5275 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5277 if Is_Non_Empty_List (Interface_List (N)) then
5278 declare
5279 Intf : Node_Id;
5280 T : Entity_Id;
5282 begin
5283 Intf := First (Interface_List (N));
5284 while Present (Intf) loop
5285 T := Find_Type_Of_Subtype_Indic (Intf);
5287 Diagnose_Interface (Intf, T);
5288 Next (Intf);
5289 end loop;
5290 end;
5291 end if;
5293 Generate_Definition (T);
5295 -- For other than Ada 2012, just enter the name in the current scope
5297 if Ada_Version < Ada_2012 then
5298 Enter_Name (T);
5300 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5301 -- case of private type that completes an incomplete type.
5303 else
5304 declare
5305 Prev : Entity_Id;
5307 begin
5308 Prev := Find_Type_Name (N);
5310 pragma Assert (Prev = T
5311 or else (Ekind (Prev) = E_Incomplete_Type
5312 and then Present (Full_View (Prev))
5313 and then Full_View (Prev) = T));
5314 end;
5315 end if;
5317 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5318 Parent_Base := Base_Type (Parent_Type);
5320 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5321 Mutate_Ekind (T, Ekind (Parent_Type));
5322 Set_Etype (T, Any_Type);
5323 goto Leave;
5325 elsif not Is_Tagged_Type (Parent_Type) then
5326 Error_Msg_N
5327 ("parent of type extension must be a tagged type", Indic);
5328 goto Leave;
5330 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5331 Error_Msg_N ("premature derivation of incomplete type", Indic);
5332 goto Leave;
5334 elsif Is_Concurrent_Type (Parent_Type) then
5335 Error_Msg_N
5336 ("parent type of a private extension cannot be a synchronized "
5337 & "tagged type (RM 3.9.1 (3/1))", N);
5339 Set_Etype (T, Any_Type);
5340 Mutate_Ekind (T, E_Limited_Private_Type);
5341 Set_Private_Dependents (T, New_Elmt_List);
5342 Set_Error_Posted (T);
5343 goto Leave;
5344 end if;
5346 Check_Wide_Character_Restriction (Parent_Type, Indic);
5348 -- Perhaps the parent type should be changed to the class-wide type's
5349 -- specific type in this case to prevent cascading errors ???
5351 if Is_Class_Wide_Type (Parent_Type) then
5352 Error_Msg_N
5353 ("parent of type extension must not be a class-wide type", Indic);
5354 goto Leave;
5355 end if;
5357 if (not Is_Package_Or_Generic_Package (Current_Scope)
5358 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5359 or else In_Private_Part (Current_Scope)
5360 then
5361 Error_Msg_N ("invalid context for private extension", N);
5362 end if;
5364 -- Set common attributes
5366 Set_Is_Pure (T, Is_Pure (Current_Scope));
5367 Set_Scope (T, Current_Scope);
5368 Mutate_Ekind (T, E_Record_Type_With_Private);
5369 Reinit_Size_Align (T);
5370 Set_Default_SSO (T);
5371 Set_No_Reordering (T, No_Component_Reordering);
5373 Set_Etype (T, Parent_Base);
5374 Propagate_Concurrent_Flags (T, Parent_Base);
5376 Set_Convention (T, Convention (Parent_Type));
5377 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5378 Set_Is_First_Subtype (T);
5379 Make_Class_Wide_Type (T);
5381 -- Set the SPARK mode from the current context
5383 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5384 Set_SPARK_Pragma_Inherited (T);
5386 if Unknown_Discriminants_Present (N) then
5387 Set_Discriminant_Constraint (T, No_Elist);
5388 end if;
5390 Build_Derived_Record_Type (N, Parent_Type, T);
5392 -- A private extension inherits the Default_Initial_Condition pragma
5393 -- coming from any parent type within the derivation chain.
5395 if Has_DIC (Parent_Type) then
5396 Set_Has_Inherited_DIC (T);
5397 end if;
5399 -- A private extension inherits any class-wide invariants coming from a
5400 -- parent type or an interface. Note that the invariant procedure of the
5401 -- parent type should not be inherited because the private extension may
5402 -- define invariants of its own.
5404 if Has_Inherited_Invariants (Parent_Type)
5405 or else Has_Inheritable_Invariants (Parent_Type)
5406 then
5407 Set_Has_Inherited_Invariants (T);
5409 elsif Present (Interfaces (T)) then
5410 Iface_Elmt := First_Elmt (Interfaces (T));
5411 while Present (Iface_Elmt) loop
5412 Iface := Node (Iface_Elmt);
5414 if Has_Inheritable_Invariants (Iface) then
5415 Set_Has_Inherited_Invariants (T);
5416 exit;
5417 end if;
5419 Next_Elmt (Iface_Elmt);
5420 end loop;
5421 end if;
5423 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5424 -- synchronized formal derived type.
5426 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5427 Set_Is_Limited_Record (T);
5429 -- Formal derived type case
5431 if Is_Generic_Type (T) then
5433 -- The parent must be a tagged limited type or a synchronized
5434 -- interface.
5436 if (not Is_Tagged_Type (Parent_Type)
5437 or else not Is_Limited_Type (Parent_Type))
5438 and then
5439 (not Is_Interface (Parent_Type)
5440 or else not Is_Synchronized_Interface (Parent_Type))
5441 then
5442 Error_Msg_NE
5443 ("parent type of & must be tagged limited or synchronized",
5444 N, T);
5445 end if;
5447 -- The progenitors (if any) must be limited or synchronized
5448 -- interfaces.
5450 if Present (Interfaces (T)) then
5451 Iface_Elmt := First_Elmt (Interfaces (T));
5452 while Present (Iface_Elmt) loop
5453 Iface := Node (Iface_Elmt);
5455 if not Is_Limited_Interface (Iface)
5456 and then not Is_Synchronized_Interface (Iface)
5457 then
5458 Error_Msg_NE
5459 ("progenitor & must be limited or synchronized",
5460 N, Iface);
5461 end if;
5463 Next_Elmt (Iface_Elmt);
5464 end loop;
5465 end if;
5467 -- Regular derived extension, the parent must be a limited or
5468 -- synchronized interface.
5470 else
5471 if not Is_Interface (Parent_Type)
5472 or else (not Is_Limited_Interface (Parent_Type)
5473 and then not Is_Synchronized_Interface (Parent_Type))
5474 then
5475 Error_Msg_NE
5476 ("parent type of & must be limited interface", N, T);
5477 end if;
5478 end if;
5480 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5481 -- extension with a synchronized parent must be explicitly declared
5482 -- synchronized, because the full view will be a synchronized type.
5483 -- This must be checked before the check for limited types below,
5484 -- to ensure that types declared limited are not allowed to extend
5485 -- synchronized interfaces.
5487 elsif Is_Interface (Parent_Type)
5488 and then Is_Synchronized_Interface (Parent_Type)
5489 and then not Synchronized_Present (N)
5490 then
5491 Error_Msg_NE
5492 ("private extension of& must be explicitly synchronized",
5493 N, Parent_Type);
5495 elsif Limited_Present (N) then
5496 Set_Is_Limited_Record (T);
5498 if not Is_Limited_Type (Parent_Type)
5499 and then
5500 (not Is_Interface (Parent_Type)
5501 or else not Is_Limited_Interface (Parent_Type))
5502 then
5503 Error_Msg_NE ("parent type& of limited extension must be limited",
5504 N, Parent_Type);
5505 end if;
5506 end if;
5508 -- Remember that its parent type has a private extension. Used to warn
5509 -- on public primitives of the parent type defined after its private
5510 -- extensions (see Check_Dispatching_Operation).
5512 Set_Has_Private_Extension (Parent_Type);
5514 <<Leave>>
5515 if Has_Aspects (N) then
5516 Analyze_Aspect_Specifications (N, T);
5517 end if;
5518 end Analyze_Private_Extension_Declaration;
5520 ---------------------------------
5521 -- Analyze_Subtype_Declaration --
5522 ---------------------------------
5524 procedure Analyze_Subtype_Declaration
5525 (N : Node_Id;
5526 Skip : Boolean := False)
5528 Id : constant Entity_Id := Defining_Identifier (N);
5529 T : Entity_Id;
5531 begin
5532 Generate_Definition (Id);
5533 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5534 Reinit_Size_Align (Id);
5536 -- The following guard condition on Enter_Name is to handle cases where
5537 -- the defining identifier has already been entered into the scope but
5538 -- the declaration as a whole needs to be analyzed.
5540 -- This case in particular happens for derived enumeration types. The
5541 -- derived enumeration type is processed as an inserted enumeration type
5542 -- declaration followed by a rewritten subtype declaration. The defining
5543 -- identifier, however, is entered into the name scope very early in the
5544 -- processing of the original type declaration and therefore needs to be
5545 -- avoided here, when the created subtype declaration is analyzed. (See
5546 -- Build_Derived_Types)
5548 -- This also happens when the full view of a private type is derived
5549 -- type with constraints. In this case the entity has been introduced
5550 -- in the private declaration.
5552 -- Finally this happens in some complex cases when validity checks are
5553 -- enabled, where the same subtype declaration may be analyzed twice.
5554 -- This can happen if the subtype is created by the preanalysis of
5555 -- an attribute that gives the range of a loop statement, and the loop
5556 -- itself appears within an if_statement that will be rewritten during
5557 -- expansion.
5559 if Skip
5560 or else (Present (Etype (Id))
5561 and then (Is_Private_Type (Etype (Id))
5562 or else Is_Task_Type (Etype (Id))
5563 or else Is_Rewrite_Substitution (N)))
5564 then
5565 null;
5567 elsif Current_Entity (Id) = Id then
5568 null;
5570 else
5571 Enter_Name (Id);
5572 end if;
5574 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5576 -- Class-wide equivalent types of records with unknown discriminants
5577 -- involve the generation of an itype which serves as the private view
5578 -- of a constrained record subtype. In such cases the base type of the
5579 -- current subtype we are processing is the private itype. Use the full
5580 -- of the private itype when decorating various attributes.
5582 if Is_Itype (T)
5583 and then Is_Private_Type (T)
5584 and then Present (Full_View (T))
5585 then
5586 T := Full_View (T);
5587 end if;
5589 -- Inherit common attributes
5591 Set_Is_Volatile (Id, Is_Volatile (T));
5592 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5593 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5594 Set_Convention (Id, Convention (T));
5596 -- If ancestor has predicates then so does the subtype, and in addition
5597 -- we must delay the freeze to properly arrange predicate inheritance.
5599 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5600 -- in which T = ID, so the above tests and assignments do nothing???
5602 if Has_Predicates (T)
5603 or else (Present (Ancestor_Subtype (T))
5604 and then Has_Predicates (Ancestor_Subtype (T)))
5605 then
5606 Set_Has_Predicates (Id);
5607 Set_Has_Delayed_Freeze (Id);
5609 -- Generated subtypes inherit the predicate function from the parent
5610 -- (no aspects to examine on the generated declaration).
5612 if not Comes_From_Source (N) then
5613 Mutate_Ekind (Id, Ekind (T));
5615 if Present (Predicate_Function (Id)) then
5616 null;
5618 elsif Present (Predicate_Function (T)) then
5619 Set_Predicate_Function (Id, Predicate_Function (T));
5621 elsif Present (Ancestor_Subtype (T))
5622 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5623 then
5624 Set_Predicate_Function (Id,
5625 Predicate_Function (Ancestor_Subtype (T)));
5626 end if;
5627 end if;
5628 end if;
5630 -- In the case where there is no constraint given in the subtype
5631 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5632 -- semantic attributes must be established here.
5634 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5635 Set_Etype (Id, Base_Type (T));
5637 case Ekind (T) is
5638 when Array_Kind =>
5639 Mutate_Ekind (Id, E_Array_Subtype);
5640 Copy_Array_Subtype_Attributes (Id, T);
5641 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5643 when Decimal_Fixed_Point_Kind =>
5644 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5645 Set_Digits_Value (Id, Digits_Value (T));
5646 Set_Delta_Value (Id, Delta_Value (T));
5647 Set_Scale_Value (Id, Scale_Value (T));
5648 Set_Small_Value (Id, Small_Value (T));
5649 Set_Scalar_Range (Id, Scalar_Range (T));
5650 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5651 Set_Is_Constrained (Id, Is_Constrained (T));
5652 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5653 Copy_RM_Size (To => Id, From => T);
5655 when Enumeration_Kind =>
5656 Mutate_Ekind (Id, E_Enumeration_Subtype);
5657 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5658 Set_Scalar_Range (Id, Scalar_Range (T));
5659 Set_Is_Character_Type (Id, Is_Character_Type (T));
5660 Set_Is_Constrained (Id, Is_Constrained (T));
5661 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5662 Copy_RM_Size (To => Id, From => T);
5664 when Ordinary_Fixed_Point_Kind =>
5665 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5666 Set_Scalar_Range (Id, Scalar_Range (T));
5667 Set_Small_Value (Id, Small_Value (T));
5668 Set_Delta_Value (Id, Delta_Value (T));
5669 Set_Is_Constrained (Id, Is_Constrained (T));
5670 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5671 Copy_RM_Size (To => Id, From => T);
5673 when Float_Kind =>
5674 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5675 Set_Scalar_Range (Id, Scalar_Range (T));
5676 Set_Digits_Value (Id, Digits_Value (T));
5677 Set_Is_Constrained (Id, Is_Constrained (T));
5679 -- If the floating point type has dimensions, these will be
5680 -- inherited subsequently when Analyze_Dimensions is called.
5682 when Signed_Integer_Kind =>
5683 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5684 Set_Scalar_Range (Id, Scalar_Range (T));
5685 Set_Is_Constrained (Id, Is_Constrained (T));
5686 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5687 Copy_RM_Size (To => Id, From => T);
5689 when Modular_Integer_Kind =>
5690 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5691 Set_Scalar_Range (Id, Scalar_Range (T));
5692 Set_Is_Constrained (Id, Is_Constrained (T));
5693 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5694 Copy_RM_Size (To => Id, From => T);
5696 when Class_Wide_Kind =>
5697 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5698 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5699 Set_Cloned_Subtype (Id, T);
5700 Set_Is_Tagged_Type (Id, True);
5701 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5702 Set_Has_Unknown_Discriminants
5703 (Id, True);
5704 Set_No_Tagged_Streams_Pragma
5705 (Id, No_Tagged_Streams_Pragma (T));
5707 if Ekind (T) = E_Class_Wide_Subtype then
5708 Set_Equivalent_Type (Id, Equivalent_Type (T));
5709 end if;
5711 when E_Record_Subtype
5712 | E_Record_Type
5714 Mutate_Ekind (Id, E_Record_Subtype);
5716 -- Subtype declarations introduced for formal type parameters
5717 -- in generic instantiations should inherit the Size value of
5718 -- the type they rename.
5720 if Present (Generic_Parent_Type (N)) then
5721 Copy_RM_Size (To => Id, From => T);
5722 end if;
5724 if Ekind (T) = E_Record_Subtype
5725 and then Present (Cloned_Subtype (T))
5726 then
5727 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5728 else
5729 Set_Cloned_Subtype (Id, T);
5730 end if;
5732 Set_First_Entity (Id, First_Entity (T));
5733 Set_Last_Entity (Id, Last_Entity (T));
5734 Set_Has_Discriminants (Id, Has_Discriminants (T));
5735 Set_Is_Constrained (Id, Is_Constrained (T));
5736 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5737 Set_Has_Implicit_Dereference
5738 (Id, Has_Implicit_Dereference (T));
5739 Set_Has_Unknown_Discriminants
5740 (Id, Has_Unknown_Discriminants (T));
5742 if Has_Discriminants (T) then
5743 Set_Discriminant_Constraint
5744 (Id, Discriminant_Constraint (T));
5745 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5747 elsif Has_Unknown_Discriminants (Id) then
5748 Set_Discriminant_Constraint (Id, No_Elist);
5749 end if;
5751 if Is_Tagged_Type (T) then
5752 Set_Is_Tagged_Type (Id, True);
5753 Set_No_Tagged_Streams_Pragma
5754 (Id, No_Tagged_Streams_Pragma (T));
5755 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5756 Set_Direct_Primitive_Operations
5757 (Id, Direct_Primitive_Operations (T));
5758 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5760 if Is_Interface (T) then
5761 Set_Is_Interface (Id);
5762 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5763 end if;
5764 end if;
5766 when Private_Kind =>
5767 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5768 Set_Has_Discriminants (Id, Has_Discriminants (T));
5769 Set_Is_Constrained (Id, Is_Constrained (T));
5770 Set_First_Entity (Id, First_Entity (T));
5771 Set_Last_Entity (Id, Last_Entity (T));
5772 Set_Private_Dependents (Id, New_Elmt_List);
5773 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5774 Set_Has_Implicit_Dereference
5775 (Id, Has_Implicit_Dereference (T));
5776 Set_Has_Unknown_Discriminants
5777 (Id, Has_Unknown_Discriminants (T));
5778 Set_Known_To_Have_Preelab_Init
5779 (Id, Known_To_Have_Preelab_Init (T));
5781 if Is_Tagged_Type (T) then
5782 Set_Is_Tagged_Type (Id);
5783 Set_No_Tagged_Streams_Pragma (Id,
5784 No_Tagged_Streams_Pragma (T));
5785 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5786 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5787 Set_Direct_Primitive_Operations (Id,
5788 Direct_Primitive_Operations (T));
5789 end if;
5791 -- In general the attributes of the subtype of a private type
5792 -- are the attributes of the partial view of parent. However,
5793 -- the full view may be a discriminated type, and the subtype
5794 -- must share the discriminant constraint to generate correct
5795 -- calls to initialization procedures.
5797 if Has_Discriminants (T) then
5798 Set_Discriminant_Constraint
5799 (Id, Discriminant_Constraint (T));
5800 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5802 elsif Present (Full_View (T))
5803 and then Has_Discriminants (Full_View (T))
5804 then
5805 Set_Discriminant_Constraint
5806 (Id, Discriminant_Constraint (Full_View (T)));
5807 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5809 -- This would seem semantically correct, but apparently
5810 -- generates spurious errors about missing components ???
5812 -- Set_Has_Discriminants (Id);
5813 end if;
5815 Prepare_Private_Subtype_Completion (Id, N);
5817 -- If this is the subtype of a constrained private type with
5818 -- discriminants that has got a full view and we also have
5819 -- built a completion just above, show that the completion
5820 -- is a clone of the full view to the back-end.
5822 if Has_Discriminants (T)
5823 and then not Has_Unknown_Discriminants (T)
5824 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5825 and then Present (Full_View (T))
5826 and then Present (Full_View (Id))
5827 then
5828 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5829 end if;
5831 when Access_Kind =>
5832 Mutate_Ekind (Id, E_Access_Subtype);
5833 Set_Is_Constrained (Id, Is_Constrained (T));
5834 Set_Is_Access_Constant
5835 (Id, Is_Access_Constant (T));
5836 Set_Directly_Designated_Type
5837 (Id, Designated_Type (T));
5838 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5840 -- A Pure library_item must not contain the declaration of a
5841 -- named access type, except within a subprogram, generic
5842 -- subprogram, task unit, or protected unit, or if it has
5843 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5845 if Comes_From_Source (Id)
5846 and then In_Pure_Unit
5847 and then not In_Subprogram_Task_Protected_Unit
5848 and then not No_Pool_Assigned (Id)
5849 then
5850 Error_Msg_N
5851 ("named access types not allowed in pure unit", N);
5852 end if;
5854 when Concurrent_Kind =>
5855 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5856 Set_Corresponding_Record_Type (Id,
5857 Corresponding_Record_Type (T));
5858 Set_First_Entity (Id, First_Entity (T));
5859 Set_First_Private_Entity (Id, First_Private_Entity (T));
5860 Set_Has_Discriminants (Id, Has_Discriminants (T));
5861 Set_Is_Constrained (Id, Is_Constrained (T));
5862 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5863 Set_Last_Entity (Id, Last_Entity (T));
5865 if Is_Tagged_Type (T) then
5866 Set_No_Tagged_Streams_Pragma
5867 (Id, No_Tagged_Streams_Pragma (T));
5868 end if;
5870 if Has_Discriminants (T) then
5871 Set_Discriminant_Constraint
5872 (Id, Discriminant_Constraint (T));
5873 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5874 end if;
5876 when Incomplete_Kind =>
5877 if Ada_Version >= Ada_2005 then
5879 -- In Ada 2005 an incomplete type can be explicitly tagged:
5880 -- propagate indication. Note that we also have to include
5881 -- subtypes for Ada 2012 extended use of incomplete types.
5883 Mutate_Ekind (Id, E_Incomplete_Subtype);
5884 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5885 Set_Private_Dependents (Id, New_Elmt_List);
5887 if Is_Tagged_Type (Id) then
5888 Set_No_Tagged_Streams_Pragma
5889 (Id, No_Tagged_Streams_Pragma (T));
5890 end if;
5892 -- For tagged types, or when prefixed-call syntax is allowed
5893 -- for untagged types, initialize the list of primitive
5894 -- operations to an empty list.
5896 if Is_Tagged_Type (Id)
5897 or else Core_Extensions_Allowed
5898 then
5899 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5900 end if;
5902 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5903 -- incomplete type visible through a limited with clause.
5905 if From_Limited_With (T)
5906 and then Present (Non_Limited_View (T))
5907 then
5908 Set_From_Limited_With (Id);
5909 Set_Non_Limited_View (Id, Non_Limited_View (T));
5911 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5912 -- to the private dependents of the original incomplete
5913 -- type for future transformation.
5915 else
5916 Append_Elmt (Id, Private_Dependents (T));
5917 end if;
5919 -- If the subtype name denotes an incomplete type an error
5920 -- was already reported by Process_Subtype.
5922 else
5923 Set_Etype (Id, Any_Type);
5924 end if;
5926 when others =>
5927 raise Program_Error;
5928 end case;
5930 -- If there is no constraint in the subtype indication, the
5931 -- declared entity inherits predicates from the parent.
5933 Inherit_Predicate_Flags (Id, T);
5934 end if;
5936 if Etype (Id) = Any_Type then
5937 goto Leave;
5938 end if;
5940 -- When prefixed calls are enabled for untagged types, the subtype
5941 -- shares the primitive operations of its base type. Do this even
5942 -- when Extensions_Allowed is False to issue better error messages.
5944 Set_Direct_Primitive_Operations
5945 (Id, Direct_Primitive_Operations (Base_Type (T)));
5947 -- Some common processing on all types
5949 Set_Size_Info (Id, T);
5950 Set_First_Rep_Item (Id, First_Rep_Item (T));
5952 -- If the parent type is a generic actual, so is the subtype. This may
5953 -- happen in a nested instance. Why Comes_From_Source test???
5955 if not Comes_From_Source (N) then
5956 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5957 end if;
5959 -- If this is a subtype declaration for an actual in an instance,
5960 -- inherit static and dynamic predicates if any.
5962 -- If declaration has no aspect specifications, inherit predicate
5963 -- info as well. Unclear how to handle the case of both specified
5964 -- and inherited predicates ??? Other inherited aspects, such as
5965 -- invariants, should be OK, but the combination with later pragmas
5966 -- may also require special merging.
5968 if Has_Predicates (T)
5969 and then Present (Predicate_Function (T))
5970 and then
5971 ((In_Instance and then not Comes_From_Source (N))
5972 or else No (Aspect_Specifications (N)))
5973 then
5974 -- Inherit Subprograms_For_Type from the full view, if present
5976 if Present (Full_View (T))
5977 and then Present (Subprograms_For_Type (Full_View (T)))
5978 then
5979 Set_Subprograms_For_Type
5980 (Id, Subprograms_For_Type (Full_View (T)));
5981 else
5982 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5983 end if;
5985 -- If the current declaration created both a private and a full view,
5986 -- then propagate Predicate_Function to the latter as well.
5988 if Present (Full_View (Id))
5989 and then No (Predicate_Function (Full_View (Id)))
5990 then
5991 Set_Subprograms_For_Type
5992 (Full_View (Id), Subprograms_For_Type (Id));
5993 end if;
5995 if Has_Static_Predicate (T) then
5996 Set_Has_Static_Predicate (Id);
5997 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5998 end if;
5999 end if;
6001 -- If the base type is a scalar type, or else if there is no
6002 -- constraint, the atomic flag is inherited by the subtype.
6003 -- Ditto for the Independent aspect.
6005 if Is_Scalar_Type (Id)
6006 or else Is_Entity_Name (Subtype_Indication (N))
6007 then
6008 Set_Is_Atomic (Id, Is_Atomic (T));
6009 Set_Is_Independent (Id, Is_Independent (T));
6010 end if;
6012 -- Remaining processing depends on characteristics of base type
6014 T := Etype (Id);
6016 Set_Is_Immediately_Visible (Id, True);
6017 Set_Depends_On_Private (Id, Has_Private_Component (T));
6018 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6020 if Is_Interface (T) then
6021 Set_Is_Interface (Id);
6022 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6023 end if;
6025 if Present (Generic_Parent_Type (N))
6026 and then
6027 (Nkind (Parent (Generic_Parent_Type (N))) /=
6028 N_Formal_Type_Declaration
6029 or else Nkind (Formal_Type_Definition
6030 (Parent (Generic_Parent_Type (N)))) /=
6031 N_Formal_Private_Type_Definition)
6032 then
6033 if Is_Tagged_Type (Id) then
6035 -- If this is a generic actual subtype for a synchronized type,
6036 -- the primitive operations are those of the corresponding record
6037 -- for which there is a separate subtype declaration.
6039 if Is_Concurrent_Type (Id) then
6040 null;
6041 elsif Is_Class_Wide_Type (Id) then
6042 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6043 else
6044 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6045 end if;
6047 elsif Scope (Etype (Id)) /= Standard_Standard then
6048 Derive_Subprograms (Generic_Parent_Type (N), Id);
6049 end if;
6050 end if;
6052 if Is_Private_Type (T) and then Present (Full_View (T)) then
6053 Conditional_Delay (Id, Full_View (T));
6055 -- The subtypes of components or subcomponents of protected types
6056 -- do not need freeze nodes, which would otherwise appear in the
6057 -- wrong scope (before the freeze node for the protected type). The
6058 -- proper subtypes are those of the subcomponents of the corresponding
6059 -- record.
6061 elsif Ekind (Scope (Id)) /= E_Protected_Type
6062 and then Present (Scope (Scope (Id))) -- error defense
6063 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6064 then
6065 Conditional_Delay (Id, T);
6066 end if;
6068 -- If we have a subtype of an incomplete type whose full type is a
6069 -- derived numeric type, we need to have a freeze node for the subtype.
6070 -- Otherwise gigi will complain while computing the (static) bounds of
6071 -- the subtype.
6073 if Is_Itype (T)
6074 and then Is_Elementary_Type (Id)
6075 and then Etype (Id) /= Id
6076 then
6077 declare
6078 Partial : constant Entity_Id :=
6079 Incomplete_Or_Partial_View (First_Subtype (Id));
6080 begin
6081 if Present (Partial)
6082 and then Ekind (Partial) = E_Incomplete_Type
6083 then
6084 Set_Has_Delayed_Freeze (Id);
6085 end if;
6086 end;
6087 end if;
6089 -- Check that Constraint_Error is raised for a scalar subtype indication
6090 -- when the lower or upper bound of a non-null range lies outside the
6091 -- range of the type mark. Likewise for an array subtype, but check the
6092 -- compatibility for each index.
6094 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6095 declare
6096 Indic_Typ : constant Entity_Id :=
6097 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6098 Subt_Index : Node_Id;
6099 Target_Index : Node_Id;
6101 begin
6102 if Is_Scalar_Type (Etype (Id))
6103 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6104 then
6105 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6107 elsif Is_Array_Type (Etype (Id))
6108 and then Present (First_Index (Id))
6109 then
6110 Subt_Index := First_Index (Id);
6111 Target_Index := First_Index (Indic_Typ);
6113 while Present (Subt_Index) loop
6114 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6115 and then Is_Scalar_Type (Entity (Subt_Index)))
6116 or else Nkind (Subt_Index) = N_Subtype_Indication)
6117 and then
6118 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6119 then
6120 Apply_Range_Check
6121 (Scalar_Range (Etype (Subt_Index)),
6122 Etype (Target_Index),
6123 Insert_Node => N);
6124 end if;
6126 Next_Index (Subt_Index);
6127 Next_Index (Target_Index);
6128 end loop;
6129 end if;
6130 end;
6131 end if;
6133 Set_Optimize_Alignment_Flags (Id);
6134 Check_Eliminated (Id);
6136 <<Leave>>
6137 if Has_Aspects (N) then
6138 Analyze_Aspect_Specifications (N, Id);
6139 end if;
6141 Analyze_Dimension (N);
6143 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6144 -- indications on composite types where the constraints are dynamic.
6145 -- Note that object declarations and aggregates generate implicit
6146 -- subtype declarations, which this covers. One special case is that the
6147 -- implicitly generated "=" for discriminated types includes an
6148 -- offending subtype declaration, which is harmless, so we ignore it
6149 -- here.
6151 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6152 declare
6153 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6154 begin
6155 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6156 and then not (Is_Internal (Id)
6157 and then Is_TSS (Scope (Id),
6158 TSS_Composite_Equality))
6159 and then not Within_Init_Proc
6160 and then not All_Composite_Constraints_Static (Cstr)
6161 then
6162 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6163 end if;
6164 end;
6165 end if;
6166 end Analyze_Subtype_Declaration;
6168 --------------------------------
6169 -- Analyze_Subtype_Indication --
6170 --------------------------------
6172 procedure Analyze_Subtype_Indication (N : Node_Id) is
6173 T : constant Entity_Id := Subtype_Mark (N);
6174 R : constant Node_Id := Range_Expression (Constraint (N));
6176 begin
6177 Analyze (T);
6179 if R = Error then
6180 Set_Error_Posted (R);
6181 Set_Error_Posted (T);
6182 else
6183 Analyze (R);
6184 Set_Etype (N, Etype (R));
6185 Resolve (R, Entity (T));
6186 end if;
6187 end Analyze_Subtype_Indication;
6189 --------------------------
6190 -- Analyze_Variant_Part --
6191 --------------------------
6193 procedure Analyze_Variant_Part (N : Node_Id) is
6194 Discr_Name : Node_Id;
6195 Discr_Type : Entity_Id;
6197 procedure Process_Variant (A : Node_Id);
6198 -- Analyze declarations for a single variant
6200 package Analyze_Variant_Choices is
6201 new Generic_Analyze_Choices (Process_Variant);
6202 use Analyze_Variant_Choices;
6204 ---------------------
6205 -- Process_Variant --
6206 ---------------------
6208 procedure Process_Variant (A : Node_Id) is
6209 CL : constant Node_Id := Component_List (A);
6210 begin
6211 if not Null_Present (CL) then
6212 Analyze_Declarations (Component_Items (CL));
6214 if Present (Variant_Part (CL)) then
6215 Analyze (Variant_Part (CL));
6216 end if;
6217 end if;
6218 end Process_Variant;
6220 -- Start of processing for Analyze_Variant_Part
6222 begin
6223 Discr_Name := Name (N);
6224 Analyze (Discr_Name);
6226 -- If Discr_Name bad, get out (prevent cascaded errors)
6228 if Etype (Discr_Name) = Any_Type then
6229 return;
6230 end if;
6232 -- Check invalid discriminant in variant part
6234 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6235 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6236 end if;
6238 Discr_Type := Etype (Entity (Discr_Name));
6240 if not Is_Discrete_Type (Discr_Type) then
6241 Error_Msg_N
6242 ("discriminant in a variant part must be of a discrete type",
6243 Name (N));
6244 return;
6245 end if;
6247 -- Now analyze the choices, which also analyzes the declarations that
6248 -- are associated with each choice.
6250 Analyze_Choices (Variants (N), Discr_Type);
6252 -- Note: we used to instantiate and call Check_Choices here to check
6253 -- that the choices covered the discriminant, but it's too early to do
6254 -- that because of statically predicated subtypes, whose analysis may
6255 -- be deferred to their freeze point which may be as late as the freeze
6256 -- point of the containing record. So this call is now to be found in
6257 -- Freeze_Record_Declaration.
6259 end Analyze_Variant_Part;
6261 ----------------------------
6262 -- Array_Type_Declaration --
6263 ----------------------------
6265 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6266 Component_Def : constant Node_Id := Component_Definition (Def);
6267 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6268 P : constant Node_Id := Parent (Def);
6269 Element_Type : Entity_Id;
6270 Implicit_Base : Entity_Id;
6271 Index : Node_Id;
6272 Nb_Index : Pos;
6273 Priv : Entity_Id;
6274 Related_Id : Entity_Id;
6275 Has_FLB_Index : Boolean := False;
6277 begin
6278 if Nkind (Def) = N_Constrained_Array_Definition then
6279 Index := First (Discrete_Subtype_Definitions (Def));
6280 else
6281 Index := First (Subtype_Marks (Def));
6282 end if;
6284 -- Find proper names for the implicit types which may be public. In case
6285 -- of anonymous arrays we use the name of the first object of that type
6286 -- as prefix.
6288 if No (T) then
6289 Related_Id := Defining_Identifier (P);
6290 else
6291 Related_Id := T;
6292 end if;
6294 Nb_Index := 1;
6295 while Present (Index) loop
6296 Analyze (Index);
6298 -- Test for odd case of trying to index a type by the type itself
6300 if Is_Entity_Name (Index) and then Entity (Index) = T then
6301 Error_Msg_N ("type& cannot be indexed by itself", Index);
6302 Set_Entity (Index, Standard_Boolean);
6303 Set_Etype (Index, Standard_Boolean);
6304 end if;
6306 -- Add a subtype declaration for each index of private array type
6307 -- declaration whose type is also private. For example:
6309 -- package Pkg is
6310 -- type Index is private;
6311 -- private
6312 -- type Table is array (Index) of ...
6313 -- end;
6315 -- This is currently required by the expander for the internally
6316 -- generated equality subprogram of records with variant parts in
6317 -- which the type of some component is such a private type. And it
6318 -- also helps semantic analysis in peculiar cases where the array
6319 -- type is referenced from an instance but not the index directly.
6321 if Is_Package_Or_Generic_Package (Current_Scope)
6322 and then In_Private_Part (Current_Scope)
6323 and then Has_Private_Declaration (Etype (Index))
6324 and then Scope (Etype (Index)) = Current_Scope
6325 then
6326 declare
6327 Loc : constant Source_Ptr := Sloc (Def);
6328 Decl : Node_Id;
6329 New_E : Entity_Id;
6331 begin
6332 New_E := Make_Temporary (Loc, 'T');
6333 Set_Is_Internal (New_E);
6335 Decl :=
6336 Make_Subtype_Declaration (Loc,
6337 Defining_Identifier => New_E,
6338 Subtype_Indication =>
6339 New_Occurrence_Of (Etype (Index), Loc));
6341 Insert_Before (Parent (Def), Decl);
6342 Analyze (Decl);
6343 Set_Etype (Index, New_E);
6345 -- If the index is a range or a subtype indication it carries
6346 -- no entity. Example:
6348 -- package Pkg is
6349 -- type T is private;
6350 -- private
6351 -- type T is new Natural;
6352 -- Table : array (T(1) .. T(10)) of Boolean;
6353 -- end Pkg;
6355 -- Otherwise the type of the reference is its entity.
6357 if Is_Entity_Name (Index) then
6358 Set_Entity (Index, New_E);
6359 end if;
6360 end;
6361 end if;
6363 Make_Index (Index, P, Related_Id, Nb_Index);
6365 -- In the case where we have an unconstrained array with an index
6366 -- given by a subtype_indication, this is necessarily a "fixed lower
6367 -- bound" index. We change the upper bound of that index to the upper
6368 -- bound of the index's subtype (denoted by the subtype_mark), since
6369 -- that upper bound was originally set by the parser to be the same
6370 -- as the lower bound. In truth, that upper bound corresponds to
6371 -- a box ("<>"), and could be set to Empty, but it's convenient to
6372 -- set it to the upper bound to avoid needing to add special tests
6373 -- in various places for an Empty upper bound, and in any case that
6374 -- accurately characterizes the index's range of values.
6376 if Nkind (Def) = N_Unconstrained_Array_Definition
6377 and then Nkind (Index) = N_Subtype_Indication
6378 then
6379 declare
6380 Index_Subtype_High_Bound : constant Entity_Id :=
6381 Type_High_Bound (Entity (Subtype_Mark (Index)));
6382 begin
6383 Set_High_Bound (Range_Expression (Constraint (Index)),
6384 Index_Subtype_High_Bound);
6386 -- Record that the array type has one or more indexes with
6387 -- a fixed lower bound.
6389 Has_FLB_Index := True;
6391 -- Mark the index as belonging to an array type with a fixed
6392 -- lower bound.
6394 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6395 end;
6396 end if;
6398 -- Check error of subtype with predicate for index type
6400 Bad_Predicated_Subtype_Use
6401 ("subtype& has predicate, not allowed as index subtype",
6402 Index, Etype (Index));
6404 -- Move to next index
6406 Next (Index);
6407 Nb_Index := Nb_Index + 1;
6408 end loop;
6410 -- Process subtype indication if one is present
6412 if Present (Component_Typ) then
6413 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6414 Set_Etype (Component_Typ, Element_Type);
6416 -- Ada 2005 (AI-230): Access Definition case
6418 else pragma Assert (Present (Access_Definition (Component_Def)));
6420 -- Indicate that the anonymous access type is created by the
6421 -- array type declaration.
6423 Element_Type := Access_Definition
6424 (Related_Nod => P,
6425 N => Access_Definition (Component_Def));
6426 Set_Is_Local_Anonymous_Access (Element_Type);
6428 -- Propagate the parent. This field is needed if we have to generate
6429 -- the master_id associated with an anonymous access to task type
6430 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6432 Copy_Parent (To => Element_Type, From => T);
6434 -- Ada 2005 (AI-230): In case of components that are anonymous access
6435 -- types the level of accessibility depends on the enclosing type
6436 -- declaration
6438 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6440 -- Ada 2005 (AI-254)
6442 declare
6443 CD : constant Node_Id :=
6444 Access_To_Subprogram_Definition
6445 (Access_Definition (Component_Def));
6446 begin
6447 if Present (CD) and then Protected_Present (CD) then
6448 Element_Type :=
6449 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6450 end if;
6451 end;
6452 end if;
6454 -- Constrained array case
6456 if No (T) then
6457 -- We might be creating more than one itype with the same Related_Id,
6458 -- e.g. for an array object definition and its initial value. Give
6459 -- them unique suffixes, because GNATprove require distinct types to
6460 -- have different names.
6462 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6463 end if;
6465 if Nkind (Def) = N_Constrained_Array_Definition then
6466 -- Establish Implicit_Base as unconstrained base type
6468 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6470 Set_Etype (Implicit_Base, Implicit_Base);
6471 Set_Scope (Implicit_Base, Current_Scope);
6472 Set_Has_Delayed_Freeze (Implicit_Base);
6473 Set_Default_SSO (Implicit_Base);
6475 -- The constrained array type is a subtype of the unconstrained one
6477 Mutate_Ekind (T, E_Array_Subtype);
6478 Reinit_Size_Align (T);
6479 Set_Etype (T, Implicit_Base);
6480 Set_Scope (T, Current_Scope);
6481 Set_Is_Constrained (T);
6482 Set_First_Index (T,
6483 First (Discrete_Subtype_Definitions (Def)));
6484 Set_Has_Delayed_Freeze (T);
6486 -- Complete setup of implicit base type
6488 pragma Assert (not Known_Component_Size (Implicit_Base));
6489 Set_Component_Type (Implicit_Base, Element_Type);
6490 Set_Finalize_Storage_Only
6491 (Implicit_Base,
6492 Finalize_Storage_Only (Element_Type));
6493 Set_First_Index (Implicit_Base, First_Index (T));
6494 Set_Has_Controlled_Component
6495 (Implicit_Base,
6496 Has_Controlled_Component (Element_Type)
6497 or else Is_Controlled (Element_Type));
6498 Set_Packed_Array_Impl_Type
6499 (Implicit_Base, Empty);
6501 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6503 -- Unconstrained array case
6505 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6506 Mutate_Ekind (T, E_Array_Type);
6507 Reinit_Size_Align (T);
6508 Set_Etype (T, T);
6509 Set_Scope (T, Current_Scope);
6510 pragma Assert (not Known_Component_Size (T));
6511 Set_Is_Constrained (T, False);
6512 Set_Is_Fixed_Lower_Bound_Array_Subtype
6513 (T, Has_FLB_Index);
6514 Set_First_Index (T, First (Subtype_Marks (Def)));
6515 Set_Has_Delayed_Freeze (T, True);
6516 Propagate_Concurrent_Flags (T, Element_Type);
6517 Set_Has_Controlled_Component (T, Has_Controlled_Component
6518 (Element_Type)
6519 or else
6520 Is_Controlled (Element_Type));
6521 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6522 (Element_Type));
6523 Set_Default_SSO (T);
6524 end if;
6526 -- Common attributes for both cases
6528 Set_Component_Type (Base_Type (T), Element_Type);
6529 Set_Packed_Array_Impl_Type (T, Empty);
6531 if Aliased_Present (Component_Definition (Def)) then
6532 Set_Has_Aliased_Components (Etype (T));
6534 -- AI12-001: All aliased objects are considered to be specified as
6535 -- independently addressable (RM C.6(8.1/4)).
6537 Set_Has_Independent_Components (Etype (T));
6538 end if;
6540 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6541 -- array type to ensure that objects of this type are initialized.
6543 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6544 Set_Can_Never_Be_Null (T);
6546 if Null_Exclusion_Present (Component_Definition (Def))
6548 -- No need to check itypes because in their case this check was
6549 -- done at their point of creation
6551 and then not Is_Itype (Element_Type)
6552 then
6553 Error_Msg_N
6554 ("`NOT NULL` not allowed (null already excluded)",
6555 Subtype_Indication (Component_Definition (Def)));
6556 end if;
6557 end if;
6559 Priv := Private_Component (Element_Type);
6561 if Present (Priv) then
6563 -- Check for circular definitions
6565 if Priv = Any_Type then
6566 Set_Component_Type (Etype (T), Any_Type);
6568 -- There is a gap in the visibility of operations on the composite
6569 -- type only if the component type is defined in a different scope.
6571 elsif Scope (Priv) = Current_Scope then
6572 null;
6574 elsif Is_Limited_Type (Priv) then
6575 Set_Is_Limited_Composite (Etype (T));
6576 Set_Is_Limited_Composite (T);
6577 else
6578 Set_Is_Private_Composite (Etype (T));
6579 Set_Is_Private_Composite (T);
6580 end if;
6581 end if;
6583 -- A syntax error in the declaration itself may lead to an empty index
6584 -- list, in which case do a minimal patch.
6586 if No (First_Index (T)) then
6587 Error_Msg_N ("missing index definition in array type declaration", T);
6589 declare
6590 Indexes : constant List_Id :=
6591 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6592 begin
6593 Set_Discrete_Subtype_Definitions (Def, Indexes);
6594 Set_First_Index (T, First (Indexes));
6595 return;
6596 end;
6597 end if;
6599 -- Create a concatenation operator for the new type. Internal array
6600 -- types created for packed entities do not need such, they are
6601 -- compatible with the user-defined type.
6603 if Number_Dimensions (T) = 1
6604 and then not Is_Packed_Array_Impl_Type (T)
6605 then
6606 New_Concatenation_Op (T);
6607 end if;
6609 -- In the case of an unconstrained array the parser has already verified
6610 -- that all the indexes are unconstrained but we still need to make sure
6611 -- that the element type is constrained.
6613 if not Is_Definite_Subtype (Element_Type) then
6614 Error_Msg_N
6615 ("unconstrained element type in array declaration",
6616 Subtype_Indication (Component_Def));
6618 elsif Is_Abstract_Type (Element_Type) then
6619 Error_Msg_N
6620 ("the type of a component cannot be abstract",
6621 Subtype_Indication (Component_Def));
6622 end if;
6624 -- There may be an invariant declared for the component type, but
6625 -- the construction of the component invariant checking procedure
6626 -- takes place during expansion.
6627 end Array_Type_Declaration;
6629 ------------------------------------------------------
6630 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6631 ------------------------------------------------------
6633 function Replace_Anonymous_Access_To_Protected_Subprogram
6634 (N : Node_Id) return Entity_Id
6636 Loc : constant Source_Ptr := Sloc (N);
6638 Curr_Scope : constant Scope_Stack_Entry :=
6639 Scope_Stack.Table (Scope_Stack.Last);
6641 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6643 Acc : Node_Id;
6644 -- Access definition in declaration
6646 Comp : Node_Id;
6647 -- Object definition or formal definition with an access definition
6649 Decl : Node_Id;
6650 -- Declaration of anonymous access to subprogram type
6652 Spec : Node_Id;
6653 -- Original specification in access to subprogram
6655 P : Node_Id;
6657 begin
6658 Set_Is_Internal (Anon);
6660 case Nkind (N) is
6661 when N_Constrained_Array_Definition
6662 | N_Component_Declaration
6663 | N_Unconstrained_Array_Definition
6665 Comp := Component_Definition (N);
6666 Acc := Access_Definition (Comp);
6668 when N_Discriminant_Specification =>
6669 Comp := Discriminant_Type (N);
6670 Acc := Comp;
6672 when N_Parameter_Specification =>
6673 Comp := Parameter_Type (N);
6674 Acc := Comp;
6676 when N_Access_Function_Definition =>
6677 Comp := Result_Definition (N);
6678 Acc := Comp;
6680 when N_Object_Declaration =>
6681 Comp := Object_Definition (N);
6682 Acc := Comp;
6684 when N_Function_Specification =>
6685 Comp := Result_Definition (N);
6686 Acc := Comp;
6688 when others =>
6689 raise Program_Error;
6690 end case;
6692 Spec := Access_To_Subprogram_Definition (Acc);
6694 Decl :=
6695 Make_Full_Type_Declaration (Loc,
6696 Defining_Identifier => Anon,
6697 Type_Definition => Copy_Separate_Tree (Spec));
6699 Mark_Rewrite_Insertion (Decl);
6701 -- Insert the new declaration in the nearest enclosing scope. If the
6702 -- parent is a body and N is its return type, the declaration belongs
6703 -- in the enclosing scope. Likewise if N is the type of a parameter.
6705 P := Parent (N);
6707 if Nkind (N) = N_Function_Specification
6708 and then Nkind (P) = N_Subprogram_Body
6709 then
6710 P := Parent (P);
6711 elsif Nkind (N) = N_Parameter_Specification
6712 and then Nkind (P) in N_Subprogram_Specification
6713 and then Nkind (Parent (P)) = N_Subprogram_Body
6714 then
6715 P := Parent (Parent (P));
6716 end if;
6718 while Present (P) and then not Has_Declarations (P) loop
6719 P := Parent (P);
6720 end loop;
6722 pragma Assert (Present (P));
6724 if Nkind (P) = N_Package_Specification then
6725 Prepend (Decl, Visible_Declarations (P));
6726 else
6727 Prepend (Decl, Declarations (P));
6728 end if;
6730 -- Replace the anonymous type with an occurrence of the new declaration.
6731 -- In all cases the rewritten node does not have the null-exclusion
6732 -- attribute because (if present) it was already inherited by the
6733 -- anonymous entity (Anon). Thus, in case of components we do not
6734 -- inherit this attribute.
6736 if Nkind (N) = N_Parameter_Specification then
6737 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6738 Set_Etype (Defining_Identifier (N), Anon);
6739 Set_Null_Exclusion_Present (N, False);
6741 elsif Nkind (N) = N_Object_Declaration then
6742 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6743 Set_Etype (Defining_Identifier (N), Anon);
6745 elsif Nkind (N) = N_Access_Function_Definition then
6746 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6748 elsif Nkind (N) = N_Function_Specification then
6749 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6750 Set_Etype (Defining_Unit_Name (N), Anon);
6752 else
6753 Rewrite (Comp,
6754 Make_Component_Definition (Loc,
6755 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6756 end if;
6758 Mark_Rewrite_Insertion (Comp);
6760 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6761 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6762 and then not Is_Type (Current_Scope))
6763 then
6765 -- Declaration can be analyzed in the current scope.
6767 Analyze (Decl);
6769 else
6770 -- Temporarily remove the current scope (record or subprogram) from
6771 -- the stack to add the new declarations to the enclosing scope.
6772 -- The anonymous entity is an Itype with the proper attributes.
6774 Scope_Stack.Decrement_Last;
6775 Analyze (Decl);
6776 Set_Is_Itype (Anon);
6777 Set_Associated_Node_For_Itype (Anon, N);
6778 Scope_Stack.Append (Curr_Scope);
6779 end if;
6781 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6782 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6783 return Anon;
6784 end Replace_Anonymous_Access_To_Protected_Subprogram;
6786 -------------------------------------
6787 -- Build_Access_Subprogram_Wrapper --
6788 -------------------------------------
6790 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6791 Loc : constant Source_Ptr := Sloc (Decl);
6792 Id : constant Entity_Id := Defining_Identifier (Decl);
6793 Type_Def : constant Node_Id := Type_Definition (Decl);
6794 Specs : constant List_Id :=
6795 Parameter_Specifications (Type_Def);
6796 Profile : constant List_Id := New_List;
6797 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6799 Contracts : constant List_Id := New_List;
6800 Form_P : Node_Id;
6801 New_P : Node_Id;
6802 New_Decl : Node_Id;
6803 Spec : Node_Id;
6805 procedure Replace_Type_Name (Expr : Node_Id);
6806 -- In the expressions for contract aspects, replace occurrences of the
6807 -- access type with the name of the subprogram entity, as needed, e.g.
6808 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6809 -- remain on the original access type declaration. What about expanded
6810 -- names denoting formals, whose prefix in source is the type name ???
6812 -----------------------
6813 -- Replace_Type_Name --
6814 -----------------------
6816 procedure Replace_Type_Name (Expr : Node_Id) is
6817 function Process (N : Node_Id) return Traverse_Result;
6818 function Process (N : Node_Id) return Traverse_Result is
6819 begin
6820 if Nkind (N) = N_Attribute_Reference
6821 and then Is_Entity_Name (Prefix (N))
6822 and then Chars (Prefix (N)) = Chars (Id)
6823 then
6824 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6825 end if;
6827 return OK;
6828 end Process;
6830 procedure Traverse is new Traverse_Proc (Process);
6831 begin
6832 Traverse (Expr);
6833 end Replace_Type_Name;
6835 begin
6836 if Ekind (Id) in E_Access_Subprogram_Type
6837 | E_Access_Protected_Subprogram_Type
6838 | E_Anonymous_Access_Protected_Subprogram_Type
6839 | E_Anonymous_Access_Subprogram_Type
6840 then
6841 null;
6843 else
6844 Error_Msg_N
6845 ("illegal pre/postcondition on access type", Decl);
6846 return;
6847 end if;
6849 declare
6850 Asp : Node_Id;
6851 A_Id : Aspect_Id;
6852 Cond : Node_Id;
6853 Expr : Node_Id;
6855 begin
6856 Asp := First (Aspect_Specifications (Decl));
6857 while Present (Asp) loop
6858 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6859 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6860 Cond := Asp;
6861 Expr := Expression (Cond);
6862 Replace_Type_Name (Expr);
6863 Next (Asp);
6865 Remove (Cond);
6866 Append (Cond, Contracts);
6868 else
6869 Next (Asp);
6870 end if;
6871 end loop;
6872 end;
6874 -- If there are no contract aspects, no need for a wrapper.
6876 if Is_Empty_List (Contracts) then
6877 return;
6878 end if;
6880 Form_P := First (Specs);
6882 while Present (Form_P) loop
6883 New_P := New_Copy_Tree (Form_P);
6884 Set_Defining_Identifier (New_P,
6885 Make_Defining_Identifier
6886 (Loc, Chars (Defining_Identifier (Form_P))));
6887 Append (New_P, Profile);
6888 Next (Form_P);
6889 end loop;
6891 -- Add to parameter specifications the access parameter that is passed
6892 -- in from an indirect call.
6894 Append (
6895 Make_Parameter_Specification (Loc,
6896 Defining_Identifier => Make_Temporary (Loc, 'P'),
6897 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6898 Profile);
6900 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6901 Spec :=
6902 Make_Procedure_Specification (Loc,
6903 Defining_Unit_Name => Subp,
6904 Parameter_Specifications => Profile);
6905 Mutate_Ekind (Subp, E_Procedure);
6906 else
6907 Spec :=
6908 Make_Function_Specification (Loc,
6909 Defining_Unit_Name => Subp,
6910 Parameter_Specifications => Profile,
6911 Result_Definition =>
6912 New_Copy_Tree
6913 (Result_Definition (Type_Definition (Decl))));
6914 Mutate_Ekind (Subp, E_Function);
6915 end if;
6917 New_Decl :=
6918 Make_Subprogram_Declaration (Loc, Specification => Spec);
6919 Set_Aspect_Specifications (New_Decl, Contracts);
6920 Set_Is_Wrapper (Subp);
6922 -- The wrapper is declared in the freezing actions to facilitate its
6923 -- identification and thus avoid handling it as a primitive operation
6924 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
6925 -- may be handled as a dispatching operation and erroneously registered
6926 -- in a dispatch table.
6928 if not GNATprove_Mode then
6929 Append_Freeze_Action (Id, New_Decl);
6931 -- Under GNATprove mode there is no such problem but we do not declare
6932 -- it in the freezing actions since they are not analyzed under this
6933 -- mode.
6935 else
6936 Insert_After (Decl, New_Decl);
6937 end if;
6939 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6940 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6941 end Build_Access_Subprogram_Wrapper;
6943 -------------------------------
6944 -- Build_Derived_Access_Type --
6945 -------------------------------
6947 procedure Build_Derived_Access_Type
6948 (N : Node_Id;
6949 Parent_Type : Entity_Id;
6950 Derived_Type : Entity_Id)
6952 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6954 Desig_Type : Entity_Id;
6955 Discr : Entity_Id;
6956 Discr_Con_Elist : Elist_Id;
6957 Discr_Con_El : Elmt_Id;
6958 Subt : Entity_Id;
6960 begin
6961 -- Set the designated type so it is available in case this is an access
6962 -- to a self-referential type, e.g. a standard list type with a next
6963 -- pointer. Will be reset after subtype is built.
6965 Set_Directly_Designated_Type
6966 (Derived_Type, Designated_Type (Parent_Type));
6968 Subt := Process_Subtype (S, N);
6970 if Nkind (S) /= N_Subtype_Indication
6971 and then Subt /= Base_Type (Subt)
6972 then
6973 Mutate_Ekind (Derived_Type, E_Access_Subtype);
6974 end if;
6976 if Ekind (Derived_Type) = E_Access_Subtype then
6977 declare
6978 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6979 Ibase : constant Entity_Id :=
6980 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6981 Svg_Chars : constant Name_Id := Chars (Ibase);
6982 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6983 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6985 begin
6986 Copy_Node (Pbase, Ibase);
6988 -- Restore Itype status after Copy_Node
6990 Set_Is_Itype (Ibase);
6991 Set_Associated_Node_For_Itype (Ibase, N);
6993 Set_Chars (Ibase, Svg_Chars);
6994 Set_Prev_Entity (Ibase, Svg_Prev_E);
6995 Set_Next_Entity (Ibase, Svg_Next_E);
6996 Set_Sloc (Ibase, Sloc (Derived_Type));
6997 Set_Scope (Ibase, Scope (Derived_Type));
6998 Set_Freeze_Node (Ibase, Empty);
6999 Set_Is_Frozen (Ibase, False);
7000 Set_Comes_From_Source (Ibase, False);
7001 Set_Is_First_Subtype (Ibase, False);
7003 Set_Etype (Ibase, Pbase);
7004 Set_Etype (Derived_Type, Ibase);
7005 end;
7006 end if;
7008 Set_Directly_Designated_Type
7009 (Derived_Type, Designated_Type (Subt));
7011 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7012 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7013 Set_Size_Info (Derived_Type, Parent_Type);
7014 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7015 Set_Depends_On_Private (Derived_Type,
7016 Has_Private_Component (Derived_Type));
7017 Conditional_Delay (Derived_Type, Subt);
7019 if Is_Access_Subprogram_Type (Derived_Type)
7020 and then Is_Base_Type (Derived_Type)
7021 then
7022 Set_Can_Use_Internal_Rep
7023 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7024 end if;
7026 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7027 -- that it is not redundant.
7029 if Null_Exclusion_Present (Type_Definition (N)) then
7030 Set_Can_Never_Be_Null (Derived_Type);
7032 elsif Can_Never_Be_Null (Parent_Type) then
7033 Set_Can_Never_Be_Null (Derived_Type);
7034 end if;
7036 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7037 -- the root type for this information.
7039 -- Apply range checks to discriminants for derived record case
7040 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7042 Desig_Type := Designated_Type (Derived_Type);
7044 if Is_Composite_Type (Desig_Type)
7045 and then not Is_Array_Type (Desig_Type)
7046 and then Has_Discriminants (Desig_Type)
7047 and then Base_Type (Desig_Type) /= Desig_Type
7048 then
7049 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7050 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7052 Discr := First_Discriminant (Base_Type (Desig_Type));
7053 while Present (Discr_Con_El) loop
7054 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7055 Next_Elmt (Discr_Con_El);
7056 Next_Discriminant (Discr);
7057 end loop;
7058 end if;
7059 end Build_Derived_Access_Type;
7061 ------------------------------
7062 -- Build_Derived_Array_Type --
7063 ------------------------------
7065 procedure Build_Derived_Array_Type
7066 (N : Node_Id;
7067 Parent_Type : Entity_Id;
7068 Derived_Type : Entity_Id)
7070 Loc : constant Source_Ptr := Sloc (N);
7071 Tdef : constant Node_Id := Type_Definition (N);
7072 Indic : constant Node_Id := Subtype_Indication (Tdef);
7073 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7074 Implicit_Base : Entity_Id := Empty;
7075 New_Indic : Node_Id;
7077 procedure Make_Implicit_Base;
7078 -- If the parent subtype is constrained, the derived type is a subtype
7079 -- of an implicit base type derived from the parent base.
7081 ------------------------
7082 -- Make_Implicit_Base --
7083 ------------------------
7085 procedure Make_Implicit_Base is
7086 begin
7087 Implicit_Base :=
7088 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7090 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7091 Set_Etype (Implicit_Base, Parent_Base);
7093 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7094 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7096 Set_Has_Delayed_Freeze (Implicit_Base, True);
7097 end Make_Implicit_Base;
7099 -- Start of processing for Build_Derived_Array_Type
7101 begin
7102 if not Is_Constrained (Parent_Type) then
7103 if Nkind (Indic) /= N_Subtype_Indication then
7104 Mutate_Ekind (Derived_Type, E_Array_Type);
7106 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7107 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7109 Set_Has_Delayed_Freeze (Derived_Type, True);
7111 else
7112 Make_Implicit_Base;
7113 Set_Etype (Derived_Type, Implicit_Base);
7115 New_Indic :=
7116 Make_Subtype_Declaration (Loc,
7117 Defining_Identifier => Derived_Type,
7118 Subtype_Indication =>
7119 Make_Subtype_Indication (Loc,
7120 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7121 Constraint => Constraint (Indic)));
7123 Rewrite (N, New_Indic);
7124 Analyze (N);
7125 end if;
7127 else
7128 if Nkind (Indic) /= N_Subtype_Indication then
7129 Make_Implicit_Base;
7131 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7132 Set_Etype (Derived_Type, Implicit_Base);
7133 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7135 else
7136 Error_Msg_N ("illegal constraint on constrained type", Indic);
7137 end if;
7138 end if;
7140 -- If parent type is not a derived type itself, and is declared in
7141 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7142 -- the new type's concatenation operator since Derive_Subprograms
7143 -- will not inherit the parent's operator. If the parent type is
7144 -- unconstrained, the operator is of the unconstrained base type.
7146 if Number_Dimensions (Parent_Type) = 1
7147 and then not Is_Limited_Type (Parent_Type)
7148 and then not Is_Derived_Type (Parent_Type)
7149 and then not Is_Package_Or_Generic_Package
7150 (Scope (Base_Type (Parent_Type)))
7151 then
7152 if not Is_Constrained (Parent_Type)
7153 and then Is_Constrained (Derived_Type)
7154 then
7155 New_Concatenation_Op (Implicit_Base);
7156 else
7157 New_Concatenation_Op (Derived_Type);
7158 end if;
7159 end if;
7160 end Build_Derived_Array_Type;
7162 -----------------------------------
7163 -- Build_Derived_Concurrent_Type --
7164 -----------------------------------
7166 procedure Build_Derived_Concurrent_Type
7167 (N : Node_Id;
7168 Parent_Type : Entity_Id;
7169 Derived_Type : Entity_Id)
7171 Loc : constant Source_Ptr := Sloc (N);
7172 Def : constant Node_Id := Type_Definition (N);
7173 Indic : constant Node_Id := Subtype_Indication (Def);
7175 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7176 Corr_Decl : Node_Id := Empty;
7177 Corr_Decl_Needed : Boolean;
7178 -- If the derived type has fewer discriminants than its parent, the
7179 -- corresponding record is also a derived type, in order to account for
7180 -- the bound discriminants. We create a full type declaration for it in
7181 -- this case.
7183 Constraint_Present : constant Boolean :=
7184 Nkind (Indic) = N_Subtype_Indication;
7186 D_Constraint : Node_Id;
7187 New_Constraint : Elist_Id := No_Elist;
7188 Old_Disc : Entity_Id;
7189 New_Disc : Entity_Id;
7190 New_N : Node_Id;
7192 begin
7193 Set_Stored_Constraint (Derived_Type, No_Elist);
7194 Corr_Decl_Needed := False;
7195 Old_Disc := Empty;
7197 if Present (Discriminant_Specifications (N))
7198 and then Constraint_Present
7199 then
7200 Old_Disc := First_Discriminant (Parent_Type);
7201 New_Disc := First (Discriminant_Specifications (N));
7202 while Present (New_Disc) and then Present (Old_Disc) loop
7203 Next_Discriminant (Old_Disc);
7204 Next (New_Disc);
7205 end loop;
7206 end if;
7208 if Present (Old_Disc) and then Expander_Active then
7210 -- The new type has fewer discriminants, so we need to create a new
7211 -- corresponding record, which is derived from the corresponding
7212 -- record of the parent, and has a stored constraint that captures
7213 -- the values of the discriminant constraints. The corresponding
7214 -- record is needed only if expander is active and code generation is
7215 -- enabled.
7217 -- The type declaration for the derived corresponding record has the
7218 -- same discriminant part and constraints as the current declaration.
7219 -- Copy the unanalyzed tree to build declaration.
7221 Corr_Decl_Needed := True;
7222 New_N := Copy_Separate_Tree (N);
7224 Corr_Decl :=
7225 Make_Full_Type_Declaration (Loc,
7226 Defining_Identifier => Corr_Record,
7227 Discriminant_Specifications =>
7228 Discriminant_Specifications (New_N),
7229 Type_Definition =>
7230 Make_Derived_Type_Definition (Loc,
7231 Subtype_Indication =>
7232 Make_Subtype_Indication (Loc,
7233 Subtype_Mark =>
7234 New_Occurrence_Of
7235 (Corresponding_Record_Type (Parent_Type), Loc),
7236 Constraint =>
7237 Constraint
7238 (Subtype_Indication (Type_Definition (New_N))))));
7239 end if;
7241 -- Copy Storage_Size and Relative_Deadline variables if task case
7243 if Is_Task_Type (Parent_Type) then
7244 Set_Storage_Size_Variable (Derived_Type,
7245 Storage_Size_Variable (Parent_Type));
7246 Set_Relative_Deadline_Variable (Derived_Type,
7247 Relative_Deadline_Variable (Parent_Type));
7248 end if;
7250 if Present (Discriminant_Specifications (N)) then
7251 Push_Scope (Derived_Type);
7252 Check_Or_Process_Discriminants (N, Derived_Type);
7254 if Constraint_Present then
7255 New_Constraint :=
7256 Expand_To_Stored_Constraint
7257 (Parent_Type,
7258 Build_Discriminant_Constraints
7259 (Parent_Type, Indic, True));
7260 end if;
7262 End_Scope;
7264 elsif Constraint_Present then
7266 -- Build an unconstrained derived type and rewrite the derived type
7267 -- as a subtype of this new base type.
7269 declare
7270 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7271 New_Base : Entity_Id;
7272 New_Decl : Node_Id;
7273 New_Indic : Node_Id;
7275 begin
7276 New_Base :=
7277 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7279 New_Decl :=
7280 Make_Full_Type_Declaration (Loc,
7281 Defining_Identifier => New_Base,
7282 Type_Definition =>
7283 Make_Derived_Type_Definition (Loc,
7284 Abstract_Present => Abstract_Present (Def),
7285 Limited_Present => Limited_Present (Def),
7286 Subtype_Indication =>
7287 New_Occurrence_Of (Parent_Base, Loc)));
7289 Mark_Rewrite_Insertion (New_Decl);
7290 Insert_Before (N, New_Decl);
7291 Analyze (New_Decl);
7293 New_Indic :=
7294 Make_Subtype_Indication (Loc,
7295 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7296 Constraint => Relocate_Node (Constraint (Indic)));
7298 Rewrite (N,
7299 Make_Subtype_Declaration (Loc,
7300 Defining_Identifier => Derived_Type,
7301 Subtype_Indication => New_Indic));
7303 Analyze (N);
7304 return;
7305 end;
7306 end if;
7308 -- By default, operations and private data are inherited from parent.
7309 -- However, in the presence of bound discriminants, a new corresponding
7310 -- record will be created, see below.
7312 Set_Has_Discriminants
7313 (Derived_Type, Has_Discriminants (Parent_Type));
7314 Set_Corresponding_Record_Type
7315 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7317 -- Is_Constrained is set according the parent subtype, but is set to
7318 -- False if the derived type is declared with new discriminants.
7320 Set_Is_Constrained
7321 (Derived_Type,
7322 (Is_Constrained (Parent_Type) or else Constraint_Present)
7323 and then not Present (Discriminant_Specifications (N)));
7325 if Constraint_Present then
7326 if not Has_Discriminants (Parent_Type) then
7327 Error_Msg_N ("untagged parent must have discriminants", N);
7329 elsif Present (Discriminant_Specifications (N)) then
7331 -- Verify that new discriminants are used to constrain old ones
7333 D_Constraint := First (Constraints (Constraint (Indic)));
7335 Old_Disc := First_Discriminant (Parent_Type);
7337 while Present (D_Constraint) loop
7338 if Nkind (D_Constraint) /= N_Discriminant_Association then
7340 -- Positional constraint. If it is a reference to a new
7341 -- discriminant, it constrains the corresponding old one.
7343 if Nkind (D_Constraint) = N_Identifier then
7344 New_Disc := First_Discriminant (Derived_Type);
7345 while Present (New_Disc) loop
7346 exit when Chars (New_Disc) = Chars (D_Constraint);
7347 Next_Discriminant (New_Disc);
7348 end loop;
7350 if Present (New_Disc) then
7351 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7352 end if;
7353 end if;
7355 Next_Discriminant (Old_Disc);
7357 -- if this is a named constraint, search by name for the old
7358 -- discriminants constrained by the new one.
7360 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7362 -- Find new discriminant with that name
7364 New_Disc := First_Discriminant (Derived_Type);
7365 while Present (New_Disc) loop
7366 exit when
7367 Chars (New_Disc) = Chars (Expression (D_Constraint));
7368 Next_Discriminant (New_Disc);
7369 end loop;
7371 if Present (New_Disc) then
7373 -- Verify that new discriminant renames some discriminant
7374 -- of the parent type, and associate the new discriminant
7375 -- with one or more old ones that it renames.
7377 declare
7378 Selector : Node_Id;
7380 begin
7381 Selector := First (Selector_Names (D_Constraint));
7382 while Present (Selector) loop
7383 Old_Disc := First_Discriminant (Parent_Type);
7384 while Present (Old_Disc) loop
7385 exit when Chars (Old_Disc) = Chars (Selector);
7386 Next_Discriminant (Old_Disc);
7387 end loop;
7389 if Present (Old_Disc) then
7390 Set_Corresponding_Discriminant
7391 (New_Disc, Old_Disc);
7392 end if;
7394 Next (Selector);
7395 end loop;
7396 end;
7397 end if;
7398 end if;
7400 Next (D_Constraint);
7401 end loop;
7403 New_Disc := First_Discriminant (Derived_Type);
7404 while Present (New_Disc) loop
7405 if No (Corresponding_Discriminant (New_Disc)) then
7406 Error_Msg_NE
7407 ("new discriminant& must constrain old one", N, New_Disc);
7409 -- If a new discriminant is used in the constraint, then its
7410 -- subtype must be statically compatible with the subtype of
7411 -- the parent discriminant (RM 3.7(15)).
7413 else
7414 Check_Constraining_Discriminant
7415 (New_Disc, Corresponding_Discriminant (New_Disc));
7416 end if;
7418 Next_Discriminant (New_Disc);
7419 end loop;
7420 end if;
7422 elsif Present (Discriminant_Specifications (N)) then
7423 Error_Msg_N
7424 ("missing discriminant constraint in untagged derivation", N);
7425 end if;
7427 -- The entity chain of the derived type includes the new discriminants
7428 -- but shares operations with the parent.
7430 if Present (Discriminant_Specifications (N)) then
7431 Old_Disc := First_Discriminant (Parent_Type);
7432 while Present (Old_Disc) loop
7433 if No (Next_Entity (Old_Disc))
7434 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7435 then
7436 Link_Entities
7437 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7438 exit;
7439 end if;
7441 Next_Discriminant (Old_Disc);
7442 end loop;
7444 else
7445 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7446 if Has_Discriminants (Parent_Type) then
7447 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7448 Set_Discriminant_Constraint (
7449 Derived_Type, Discriminant_Constraint (Parent_Type));
7450 end if;
7451 end if;
7453 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7455 Set_Has_Completion (Derived_Type);
7457 if Corr_Decl_Needed then
7458 Set_Stored_Constraint (Derived_Type, New_Constraint);
7459 Insert_After (N, Corr_Decl);
7460 Analyze (Corr_Decl);
7461 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7462 end if;
7463 end Build_Derived_Concurrent_Type;
7465 ------------------------------------
7466 -- Build_Derived_Enumeration_Type --
7467 ------------------------------------
7469 procedure Build_Derived_Enumeration_Type
7470 (N : Node_Id;
7471 Parent_Type : Entity_Id;
7472 Derived_Type : Entity_Id)
7474 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7475 -- When the type declaration includes a constraint, we generate
7476 -- a subtype declaration of an anonymous base type, with the constraint
7477 -- given in the original type declaration. Conceptually, the bounds
7478 -- are converted to the new base type, and this conversion freezes
7479 -- (prematurely) that base type, when the bounds are simply literals.
7480 -- As a result, a representation clause for the derived type is then
7481 -- rejected or ignored. This procedure recognizes the simple case of
7482 -- literal bounds, which allows us to indicate that the conversions
7483 -- are not freeze points, and the subsequent representation clause
7484 -- can be accepted.
7485 -- A similar approach might be used to resolve the long-standing
7486 -- problem of premature freezing of derived numeric types ???
7488 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7489 begin
7490 return Nkind (B) = N_Type_Conversion
7491 and then Is_Entity_Name (Expression (B))
7492 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7493 end Bound_Belongs_To_Type;
7495 Loc : constant Source_Ptr := Sloc (N);
7496 Def : constant Node_Id := Type_Definition (N);
7497 Indic : constant Node_Id := Subtype_Indication (Def);
7498 Implicit_Base : Entity_Id;
7499 Literal : Entity_Id;
7500 New_Lit : Entity_Id;
7501 Literals_List : List_Id;
7502 Type_Decl : Node_Id;
7503 Hi, Lo : Node_Id;
7504 Rang_Expr : Node_Id;
7506 begin
7507 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7508 -- not have explicit literals lists we need to process types derived
7509 -- from them specially. This is handled by Derived_Standard_Character.
7510 -- If the parent type is a generic type, there are no literals either,
7511 -- and we construct the same skeletal representation as for the generic
7512 -- parent type.
7514 if Is_Standard_Character_Type (Parent_Type) then
7515 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7517 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7518 declare
7519 Lo : Node_Id;
7520 Hi : Node_Id;
7522 begin
7523 if Nkind (Indic) /= N_Subtype_Indication then
7524 Lo :=
7525 Make_Attribute_Reference (Loc,
7526 Attribute_Name => Name_First,
7527 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7528 Set_Etype (Lo, Derived_Type);
7530 Hi :=
7531 Make_Attribute_Reference (Loc,
7532 Attribute_Name => Name_Last,
7533 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7534 Set_Etype (Hi, Derived_Type);
7536 Set_Scalar_Range (Derived_Type,
7537 Make_Range (Loc,
7538 Low_Bound => Lo,
7539 High_Bound => Hi));
7540 else
7542 -- Analyze subtype indication and verify compatibility
7543 -- with parent type.
7545 if Base_Type (Process_Subtype (Indic, N)) /=
7546 Base_Type (Parent_Type)
7547 then
7548 Error_Msg_N
7549 ("illegal constraint for formal discrete type", N);
7550 end if;
7551 end if;
7552 end;
7554 else
7555 -- If a constraint is present, analyze the bounds to catch
7556 -- premature usage of the derived literals.
7558 if Nkind (Indic) = N_Subtype_Indication
7559 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7560 then
7561 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7562 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7563 end if;
7565 -- Create an implicit base type for the derived type even if there
7566 -- is no constraint attached to it, since this seems closer to the
7567 -- Ada semantics. Use an Itype like for the implicit base type of
7568 -- other kinds of derived type, but build a full type declaration
7569 -- for it so as to analyze the new literals properly. Then build a
7570 -- subtype declaration tree which applies the constraint (if any)
7571 -- and have it replace the derived type declaration.
7573 Literal := First_Literal (Parent_Type);
7574 Literals_List := New_List;
7575 while Present (Literal)
7576 and then Ekind (Literal) = E_Enumeration_Literal
7577 loop
7578 -- Literals of the derived type have the same representation as
7579 -- those of the parent type, but this representation can be
7580 -- overridden by an explicit representation clause. Indicate
7581 -- that there is no explicit representation given yet. These
7582 -- derived literals are implicit operations of the new type,
7583 -- and can be overridden by explicit ones.
7585 if Nkind (Literal) = N_Defining_Character_Literal then
7586 New_Lit :=
7587 Make_Defining_Character_Literal (Loc, Chars (Literal));
7588 else
7589 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7590 end if;
7592 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7593 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7594 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7595 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7596 Set_Alias (New_Lit, Literal);
7597 Set_Is_Known_Valid (New_Lit, True);
7599 Append (New_Lit, Literals_List);
7600 Next_Literal (Literal);
7601 end loop;
7603 Implicit_Base :=
7604 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7606 -- Indicate the proper nature of the derived type. This must be done
7607 -- before analysis of the literals, to recognize cases when a literal
7608 -- may be hidden by a previous explicit function definition (cf.
7609 -- c83031a).
7611 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7612 Set_Etype (Derived_Type, Implicit_Base);
7614 Type_Decl :=
7615 Make_Full_Type_Declaration (Loc,
7616 Defining_Identifier => Implicit_Base,
7617 Type_Definition =>
7618 Make_Enumeration_Type_Definition (Loc, Literals_List));
7620 -- Do not insert the declarationn, just analyze it in the context
7622 Set_Parent (Type_Decl, Parent (N));
7623 Analyze (Type_Decl);
7625 -- The anonymous base now has a full declaration, but this base
7626 -- is not a first subtype.
7628 Set_Is_First_Subtype (Implicit_Base, False);
7630 -- After the implicit base is analyzed its Etype needs to be changed
7631 -- to reflect the fact that it is derived from the parent type which
7632 -- was ignored during analysis. We also set the size at this point.
7634 Set_Etype (Implicit_Base, Parent_Type);
7636 Set_Size_Info (Implicit_Base, Parent_Type);
7637 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7638 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7640 -- Copy other flags from parent type
7642 Set_Has_Non_Standard_Rep
7643 (Implicit_Base, Has_Non_Standard_Rep
7644 (Parent_Type));
7645 Set_Has_Pragma_Ordered
7646 (Implicit_Base, Has_Pragma_Ordered
7647 (Parent_Type));
7648 Set_Has_Delayed_Freeze (Implicit_Base);
7650 -- Process the subtype indication including a validation check on the
7651 -- constraint, if any. If a constraint is given, its bounds must be
7652 -- implicitly converted to the new type.
7654 if Nkind (Indic) = N_Subtype_Indication then
7655 declare
7656 R : constant Node_Id :=
7657 Range_Expression (Constraint (Indic));
7659 begin
7660 if Nkind (R) = N_Range then
7661 Hi := Build_Scalar_Bound
7662 (High_Bound (R), Parent_Type, Implicit_Base);
7663 Lo := Build_Scalar_Bound
7664 (Low_Bound (R), Parent_Type, Implicit_Base);
7666 else
7667 -- Constraint is a Range attribute. Replace with explicit
7668 -- mention of the bounds of the prefix, which must be a
7669 -- subtype.
7671 Analyze (Prefix (R));
7672 Hi :=
7673 Convert_To (Implicit_Base,
7674 Make_Attribute_Reference (Loc,
7675 Attribute_Name => Name_Last,
7676 Prefix =>
7677 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7679 Lo :=
7680 Convert_To (Implicit_Base,
7681 Make_Attribute_Reference (Loc,
7682 Attribute_Name => Name_First,
7683 Prefix =>
7684 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7685 end if;
7686 end;
7688 else
7689 Hi :=
7690 Build_Scalar_Bound
7691 (Type_High_Bound (Parent_Type),
7692 Parent_Type, Implicit_Base);
7693 Lo :=
7694 Build_Scalar_Bound
7695 (Type_Low_Bound (Parent_Type),
7696 Parent_Type, Implicit_Base);
7697 end if;
7699 Rang_Expr :=
7700 Make_Range (Loc,
7701 Low_Bound => Lo,
7702 High_Bound => Hi);
7704 -- If we constructed a default range for the case where no range
7705 -- was given, then the expressions in the range must not freeze
7706 -- since they do not correspond to expressions in the source.
7707 -- However, if the type inherits predicates the expressions will
7708 -- be elaborated earlier and must freeze.
7710 if (Nkind (Indic) /= N_Subtype_Indication
7711 or else
7712 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7713 and then not Has_Predicates (Derived_Type)
7714 then
7715 Set_Must_Not_Freeze (Lo);
7716 Set_Must_Not_Freeze (Hi);
7717 Set_Must_Not_Freeze (Rang_Expr);
7718 end if;
7720 Rewrite (N,
7721 Make_Subtype_Declaration (Loc,
7722 Defining_Identifier => Derived_Type,
7723 Subtype_Indication =>
7724 Make_Subtype_Indication (Loc,
7725 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7726 Constraint =>
7727 Make_Range_Constraint (Loc,
7728 Range_Expression => Rang_Expr))));
7730 Analyze (N);
7732 -- Propagate the aspects from the original type declaration to the
7733 -- declaration of the implicit base.
7735 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7737 -- Apply a range check. Since this range expression doesn't have an
7738 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7739 -- this right???
7741 if Nkind (Indic) = N_Subtype_Indication then
7742 Apply_Range_Check
7743 (Range_Expression (Constraint (Indic)), Parent_Type,
7744 Source_Typ => Entity (Subtype_Mark (Indic)));
7745 end if;
7746 end if;
7747 end Build_Derived_Enumeration_Type;
7749 --------------------------------
7750 -- Build_Derived_Numeric_Type --
7751 --------------------------------
7753 procedure Build_Derived_Numeric_Type
7754 (N : Node_Id;
7755 Parent_Type : Entity_Id;
7756 Derived_Type : Entity_Id)
7758 Loc : constant Source_Ptr := Sloc (N);
7759 Tdef : constant Node_Id := Type_Definition (N);
7760 Indic : constant Node_Id := Subtype_Indication (Tdef);
7761 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7762 No_Constraint : constant Boolean := Nkind (Indic) /=
7763 N_Subtype_Indication;
7764 Implicit_Base : Entity_Id;
7766 Lo : Node_Id;
7767 Hi : Node_Id;
7769 begin
7770 -- Process the subtype indication including a validation check on
7771 -- the constraint if any.
7773 Discard_Node (Process_Subtype (Indic, N));
7775 -- Introduce an implicit base type for the derived type even if there
7776 -- is no constraint attached to it, since this seems closer to the Ada
7777 -- semantics.
7779 Implicit_Base :=
7780 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7782 Set_Etype (Implicit_Base, Parent_Base);
7783 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7784 Set_Size_Info (Implicit_Base, Parent_Base);
7785 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7786 Set_Parent (Implicit_Base, Parent (Derived_Type));
7787 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7788 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7790 -- Set RM Size for discrete type or decimal fixed-point type
7791 -- Ordinary fixed-point is excluded, why???
7793 if Is_Discrete_Type (Parent_Base)
7794 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7795 then
7796 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7797 end if;
7799 Set_Has_Delayed_Freeze (Implicit_Base);
7801 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7802 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7804 Set_Scalar_Range (Implicit_Base,
7805 Make_Range (Loc,
7806 Low_Bound => Lo,
7807 High_Bound => Hi));
7809 if Has_Infinities (Parent_Base) then
7810 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7811 end if;
7813 -- The Derived_Type, which is the entity of the declaration, is a
7814 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7815 -- absence of an explicit constraint.
7817 Set_Etype (Derived_Type, Implicit_Base);
7819 -- If we did not have a constraint, then the Ekind is set from the
7820 -- parent type (otherwise Process_Subtype has set the bounds)
7822 if No_Constraint then
7823 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7824 end if;
7826 -- If we did not have a range constraint, then set the range from the
7827 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7829 if No_Constraint or else not Has_Range_Constraint (Indic) then
7830 Set_Scalar_Range (Derived_Type,
7831 Make_Range (Loc,
7832 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7833 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7834 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7836 if Has_Infinities (Parent_Type) then
7837 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7838 end if;
7840 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7841 end if;
7843 Set_Is_Descendant_Of_Address (Derived_Type,
7844 Is_Descendant_Of_Address (Parent_Type));
7845 Set_Is_Descendant_Of_Address (Implicit_Base,
7846 Is_Descendant_Of_Address (Parent_Type));
7848 -- Set remaining type-specific fields, depending on numeric type
7850 if Is_Modular_Integer_Type (Parent_Type) then
7851 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7853 Set_Non_Binary_Modulus
7854 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7856 Set_Is_Known_Valid
7857 (Implicit_Base, Is_Known_Valid (Parent_Base));
7859 elsif Is_Floating_Point_Type (Parent_Type) then
7861 -- Digits of base type is always copied from the digits value of
7862 -- the parent base type, but the digits of the derived type will
7863 -- already have been set if there was a constraint present.
7865 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7866 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7868 if No_Constraint then
7869 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7870 end if;
7872 elsif Is_Fixed_Point_Type (Parent_Type) then
7874 -- Small of base type and derived type are always copied from the
7875 -- parent base type, since smalls never change. The delta of the
7876 -- base type is also copied from the parent base type. However the
7877 -- delta of the derived type will have been set already if a
7878 -- constraint was present.
7880 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7881 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7882 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7884 if No_Constraint then
7885 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7886 end if;
7888 -- The scale and machine radix in the decimal case are always
7889 -- copied from the parent base type.
7891 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7892 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7893 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7895 Set_Machine_Radix_10
7896 (Derived_Type, Machine_Radix_10 (Parent_Base));
7897 Set_Machine_Radix_10
7898 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7900 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7902 if No_Constraint then
7903 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7905 else
7906 -- the analysis of the subtype_indication sets the
7907 -- digits value of the derived type.
7909 null;
7910 end if;
7911 end if;
7912 end if;
7914 if Is_Integer_Type (Parent_Type) then
7915 Set_Has_Shift_Operator
7916 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7917 end if;
7919 -- The type of the bounds is that of the parent type, and they
7920 -- must be converted to the derived type.
7922 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7923 end Build_Derived_Numeric_Type;
7925 --------------------------------
7926 -- Build_Derived_Private_Type --
7927 --------------------------------
7929 procedure Build_Derived_Private_Type
7930 (N : Node_Id;
7931 Parent_Type : Entity_Id;
7932 Derived_Type : Entity_Id;
7933 Is_Completion : Boolean;
7934 Derive_Subps : Boolean := True)
7936 Loc : constant Source_Ptr := Sloc (N);
7937 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7938 Par_Scope : constant Entity_Id := Scope (Par_Base);
7939 Full_N : constant Node_Id := New_Copy_Tree (N);
7940 Full_Der : Entity_Id := New_Copy (Derived_Type);
7941 Full_P : Entity_Id;
7943 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7944 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7945 -- present (they cannot be both present for the same type), or Empty.
7947 procedure Build_Full_Derivation;
7948 -- Build full derivation, i.e. derive from the full view
7950 procedure Copy_And_Build;
7951 -- Copy derived type declaration, replace parent with its full view,
7952 -- and build derivation
7954 -------------------------
7955 -- Available_Full_View --
7956 -------------------------
7958 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7959 begin
7960 if Present (Full_View (Typ)) then
7961 return Full_View (Typ);
7963 elsif Present (Underlying_Full_View (Typ)) then
7965 -- We should be called on a type with an underlying full view
7966 -- only by means of the recursive call made in Copy_And_Build
7967 -- through the first call to Build_Derived_Type, or else if
7968 -- the parent scope is being analyzed because we are deriving
7969 -- a completion.
7971 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7973 return Underlying_Full_View (Typ);
7975 else
7976 return Empty;
7977 end if;
7978 end Available_Full_View;
7980 ---------------------------
7981 -- Build_Full_Derivation --
7982 ---------------------------
7984 procedure Build_Full_Derivation is
7985 begin
7986 -- If parent scope is not open, install the declarations
7988 if not In_Open_Scopes (Par_Scope) then
7989 Install_Private_Declarations (Par_Scope);
7990 Install_Visible_Declarations (Par_Scope);
7991 Copy_And_Build;
7992 Uninstall_Declarations (Par_Scope);
7994 -- If parent scope is open and in another unit, and parent has a
7995 -- completion, then the derivation is taking place in the visible
7996 -- part of a child unit. In that case retrieve the full view of
7997 -- the parent momentarily.
7999 elsif not In_Same_Source_Unit (N, Parent_Type)
8000 and then Present (Full_View (Parent_Type))
8001 then
8002 Full_P := Full_View (Parent_Type);
8003 Exchange_Declarations (Parent_Type);
8004 Copy_And_Build;
8005 Exchange_Declarations (Full_P);
8007 -- Otherwise it is a local derivation
8009 else
8010 Copy_And_Build;
8011 end if;
8012 end Build_Full_Derivation;
8014 --------------------
8015 -- Copy_And_Build --
8016 --------------------
8018 procedure Copy_And_Build is
8019 Full_Parent : Entity_Id := Parent_Type;
8021 begin
8022 -- If the parent is itself derived from another private type,
8023 -- installing the private declarations has not affected its
8024 -- privacy status, so use its own full view explicitly.
8026 if Is_Private_Type (Full_Parent)
8027 and then Present (Full_View (Full_Parent))
8028 then
8029 Full_Parent := Full_View (Full_Parent);
8030 end if;
8032 -- If the full view is itself derived from another private type
8033 -- and has got an underlying full view, and this is done for a
8034 -- completion, i.e. to build the underlying full view of the type,
8035 -- then use this underlying full view. We cannot do that if this
8036 -- is not a completion, i.e. to build the full view of the type,
8037 -- because this would break the privacy of the parent type, except
8038 -- if the parent scope is being analyzed because we are deriving a
8039 -- completion.
8041 if Is_Private_Type (Full_Parent)
8042 and then Present (Underlying_Full_View (Full_Parent))
8043 and then (Is_Completion or else In_Private_Part (Par_Scope))
8044 then
8045 Full_Parent := Underlying_Full_View (Full_Parent);
8046 end if;
8048 -- For private, record, concurrent, access and almost all enumeration
8049 -- types, the derivation from the full view requires a fully-fledged
8050 -- declaration. In the other cases, just use an itype.
8052 if Is_Private_Type (Full_Parent)
8053 or else Is_Record_Type (Full_Parent)
8054 or else Is_Concurrent_Type (Full_Parent)
8055 or else Is_Access_Type (Full_Parent)
8056 or else
8057 (Is_Enumeration_Type (Full_Parent)
8058 and then not Is_Standard_Character_Type (Full_Parent)
8059 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8060 then
8061 -- Copy and adjust declaration to provide a completion for what
8062 -- is originally a private declaration. Indicate that full view
8063 -- is internally generated.
8065 Set_Comes_From_Source (Full_N, False);
8066 Set_Comes_From_Source (Full_Der, False);
8067 Set_Parent (Full_Der, Full_N);
8068 Set_Defining_Identifier (Full_N, Full_Der);
8070 -- If there are no constraints, adjust the subtype mark
8072 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8073 N_Subtype_Indication
8074 then
8075 Set_Subtype_Indication
8076 (Type_Definition (Full_N),
8077 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8078 end if;
8080 Insert_After (N, Full_N);
8082 -- Build full view of derived type from full view of parent which
8083 -- is now installed. Subprograms have been derived on the partial
8084 -- view, the completion does not derive them anew.
8086 if Is_Record_Type (Full_Parent) then
8088 -- If parent type is tagged, the completion inherits the proper
8089 -- primitive operations.
8091 if Is_Tagged_Type (Parent_Type) then
8092 Build_Derived_Record_Type
8093 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8094 else
8095 Build_Derived_Record_Type
8096 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8097 end if;
8099 else
8100 -- If the parent type is private, this is not a completion and
8101 -- we build the full derivation recursively as a completion.
8103 Build_Derived_Type
8104 (Full_N, Full_Parent, Full_Der,
8105 Is_Completion => Is_Private_Type (Full_Parent),
8106 Derive_Subps => False);
8107 end if;
8109 -- The full declaration has been introduced into the tree and
8110 -- processed in the step above. It should not be analyzed again
8111 -- (when encountered later in the current list of declarations)
8112 -- to prevent spurious name conflicts. The full entity remains
8113 -- invisible.
8115 Set_Analyzed (Full_N);
8117 else
8118 Full_Der :=
8119 Make_Defining_Identifier (Sloc (Derived_Type),
8120 Chars => Chars (Derived_Type));
8121 Set_Is_Itype (Full_Der);
8122 Set_Associated_Node_For_Itype (Full_Der, N);
8123 Set_Parent (Full_Der, N);
8124 Build_Derived_Type
8125 (N, Full_Parent, Full_Der,
8126 Is_Completion => False, Derive_Subps => False);
8127 end if;
8129 Set_Has_Private_Declaration (Full_Der);
8130 Set_Has_Private_Declaration (Derived_Type);
8132 Set_Scope (Full_Der, Scope (Derived_Type));
8133 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8134 Set_Has_Size_Clause (Full_Der, False);
8135 Set_Has_Alignment_Clause (Full_Der, False);
8136 Set_Has_Delayed_Freeze (Full_Der);
8137 Set_Is_Frozen (Full_Der, False);
8138 Set_Freeze_Node (Full_Der, Empty);
8139 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8140 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8142 -- The convention on the base type may be set in the private part
8143 -- and not propagated to the subtype until later, so we obtain the
8144 -- convention from the base type of the parent.
8146 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8147 end Copy_And_Build;
8149 -- Start of processing for Build_Derived_Private_Type
8151 begin
8152 if Is_Tagged_Type (Parent_Type) then
8153 Full_P := Full_View (Parent_Type);
8155 -- A type extension of a type with unknown discriminants is an
8156 -- indefinite type that the back-end cannot handle directly.
8157 -- We treat it as a private type, and build a completion that is
8158 -- derived from the full view of the parent, and hopefully has
8159 -- known discriminants.
8161 -- If the full view of the parent type has an underlying record view,
8162 -- use it to generate the underlying record view of this derived type
8163 -- (required for chains of derivations with unknown discriminants).
8165 -- Minor optimization: we avoid the generation of useless underlying
8166 -- record view entities if the private type declaration has unknown
8167 -- discriminants but its corresponding full view has no
8168 -- discriminants.
8170 if Has_Unknown_Discriminants (Parent_Type)
8171 and then Present (Full_P)
8172 and then (Has_Discriminants (Full_P)
8173 or else Present (Underlying_Record_View (Full_P)))
8174 and then not In_Open_Scopes (Par_Scope)
8175 and then Expander_Active
8176 then
8177 declare
8178 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8179 New_Ext : constant Node_Id :=
8180 Copy_Separate_Tree
8181 (Record_Extension_Part (Type_Definition (N)));
8182 Decl : Node_Id;
8184 begin
8185 Build_Derived_Record_Type
8186 (N, Parent_Type, Derived_Type, Derive_Subps);
8188 -- Build anonymous completion, as a derivation from the full
8189 -- view of the parent. This is not a completion in the usual
8190 -- sense, because the current type is not private.
8192 Decl :=
8193 Make_Full_Type_Declaration (Loc,
8194 Defining_Identifier => Full_Der,
8195 Type_Definition =>
8196 Make_Derived_Type_Definition (Loc,
8197 Subtype_Indication =>
8198 New_Copy_Tree
8199 (Subtype_Indication (Type_Definition (N))),
8200 Record_Extension_Part => New_Ext));
8202 -- If the parent type has an underlying record view, use it
8203 -- here to build the new underlying record view.
8205 if Present (Underlying_Record_View (Full_P)) then
8206 pragma Assert
8207 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8208 = N_Identifier);
8209 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8210 Underlying_Record_View (Full_P));
8211 end if;
8213 Install_Private_Declarations (Par_Scope);
8214 Install_Visible_Declarations (Par_Scope);
8215 Insert_Before (N, Decl);
8217 -- Mark entity as an underlying record view before analysis,
8218 -- to avoid generating the list of its primitive operations
8219 -- (which is not really required for this entity) and thus
8220 -- prevent spurious errors associated with missing overriding
8221 -- of abstract primitives (overridden only for Derived_Type).
8223 Mutate_Ekind (Full_Der, E_Record_Type);
8224 Set_Is_Underlying_Record_View (Full_Der);
8225 Set_Default_SSO (Full_Der);
8226 Set_No_Reordering (Full_Der, No_Component_Reordering);
8228 Analyze (Decl);
8230 pragma Assert (Has_Discriminants (Full_Der)
8231 and then not Has_Unknown_Discriminants (Full_Der));
8233 Uninstall_Declarations (Par_Scope);
8235 -- Freeze the underlying record view, to prevent generation of
8236 -- useless dispatching information, which is simply shared with
8237 -- the real derived type.
8239 Set_Is_Frozen (Full_Der);
8241 -- If the derived type has access discriminants, create
8242 -- references to their anonymous types now, to prevent
8243 -- back-end problems when their first use is in generated
8244 -- bodies of primitives.
8246 declare
8247 E : Entity_Id;
8249 begin
8250 E := First_Entity (Full_Der);
8252 while Present (E) loop
8253 if Ekind (E) = E_Discriminant
8254 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8255 then
8256 Build_Itype_Reference (Etype (E), Decl);
8257 end if;
8259 Next_Entity (E);
8260 end loop;
8261 end;
8263 -- Set up links between real entity and underlying record view
8265 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8266 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8267 end;
8269 -- If discriminants are known, build derived record
8271 else
8272 Build_Derived_Record_Type
8273 (N, Parent_Type, Derived_Type, Derive_Subps);
8274 end if;
8276 return;
8278 elsif Has_Discriminants (Parent_Type) then
8280 -- Build partial view of derived type from partial view of parent.
8281 -- This must be done before building the full derivation because the
8282 -- second derivation will modify the discriminants of the first and
8283 -- the discriminants are chained with the rest of the components in
8284 -- the full derivation.
8286 Build_Derived_Record_Type
8287 (N, Parent_Type, Derived_Type, Derive_Subps);
8289 -- Build the full derivation if this is not the anonymous derived
8290 -- base type created by Build_Derived_Record_Type in the constrained
8291 -- case (see point 5. of its head comment) since we build it for the
8292 -- derived subtype.
8294 if Present (Available_Full_View (Parent_Type))
8295 and then not Is_Itype (Derived_Type)
8296 then
8297 declare
8298 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8299 Discr : Entity_Id;
8300 Last_Discr : Entity_Id;
8302 begin
8303 -- If this is not a completion, construct the implicit full
8304 -- view by deriving from the full view of the parent type.
8305 -- But if this is a completion, the derived private type
8306 -- being built is a full view and the full derivation can
8307 -- only be its underlying full view.
8309 Build_Full_Derivation;
8311 if not Is_Completion then
8312 Set_Full_View (Derived_Type, Full_Der);
8313 else
8314 Set_Underlying_Full_View (Derived_Type, Full_Der);
8315 Set_Is_Underlying_Full_View (Full_Der);
8316 end if;
8318 if not Is_Base_Type (Derived_Type) then
8319 Set_Full_View (Der_Base, Base_Type (Full_Der));
8320 end if;
8322 -- Copy the discriminant list from full view to the partial
8323 -- view (base type and its subtype). Gigi requires that the
8324 -- partial and full views have the same discriminants.
8326 -- Note that since the partial view points to discriminants
8327 -- in the full view, their scope will be that of the full
8328 -- view. This might cause some front end problems and need
8329 -- adjustment???
8331 Discr := First_Discriminant (Base_Type (Full_Der));
8332 Set_First_Entity (Der_Base, Discr);
8334 loop
8335 Last_Discr := Discr;
8336 Next_Discriminant (Discr);
8337 exit when No (Discr);
8338 end loop;
8340 Set_Last_Entity (Der_Base, Last_Discr);
8341 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8342 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8343 end;
8344 end if;
8346 elsif Present (Available_Full_View (Parent_Type))
8347 and then Has_Discriminants (Available_Full_View (Parent_Type))
8348 then
8349 if Has_Unknown_Discriminants (Parent_Type)
8350 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8351 N_Subtype_Indication
8352 then
8353 Error_Msg_N
8354 ("cannot constrain type with unknown discriminants",
8355 Subtype_Indication (Type_Definition (N)));
8356 return;
8357 end if;
8359 -- If this is not a completion, construct the implicit full view by
8360 -- deriving from the full view of the parent type. But if this is a
8361 -- completion, the derived private type being built is a full view
8362 -- and the full derivation can only be its underlying full view.
8364 Build_Full_Derivation;
8366 if not Is_Completion then
8367 Set_Full_View (Derived_Type, Full_Der);
8368 else
8369 Set_Underlying_Full_View (Derived_Type, Full_Der);
8370 Set_Is_Underlying_Full_View (Full_Der);
8371 end if;
8373 -- In any case, the primitive operations are inherited from the
8374 -- parent type, not from the internal full view.
8376 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8378 if Derive_Subps then
8379 -- Initialize the list of primitive operations to an empty list,
8380 -- to cover tagged types as well as untagged types. For untagged
8381 -- types this is used either to analyze the call as legal when
8382 -- Extensions_Allowed is True, or to issue a better error message
8383 -- otherwise.
8385 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8387 Derive_Subprograms (Parent_Type, Derived_Type);
8388 end if;
8390 Set_Stored_Constraint (Derived_Type, No_Elist);
8391 Set_Is_Constrained
8392 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8394 else
8395 -- Untagged type, No discriminants on either view
8397 if Nkind (Subtype_Indication (Type_Definition (N))) =
8398 N_Subtype_Indication
8399 then
8400 Error_Msg_N
8401 ("illegal constraint on type without discriminants", N);
8402 end if;
8404 if Present (Discriminant_Specifications (N))
8405 and then Present (Available_Full_View (Parent_Type))
8406 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8407 then
8408 Error_Msg_N ("cannot add discriminants to untagged type", N);
8409 end if;
8411 Set_Stored_Constraint (Derived_Type, No_Elist);
8412 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8414 Set_Is_Controlled_Active
8415 (Derived_Type, Is_Controlled_Active (Parent_Type));
8417 Set_Disable_Controlled
8418 (Derived_Type, Disable_Controlled (Parent_Type));
8420 Set_Has_Controlled_Component
8421 (Derived_Type, Has_Controlled_Component (Parent_Type));
8423 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8425 if not Is_Controlled (Parent_Type) then
8426 Set_Finalize_Storage_Only
8427 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8428 end if;
8430 -- If this is not a completion, construct the implicit full view by
8431 -- deriving from the full view of the parent type. But if this is a
8432 -- completion, the derived private type being built is a full view
8433 -- and the full derivation can only be its underlying full view.
8435 -- ??? If the parent type is untagged private and its completion is
8436 -- tagged, this mechanism will not work because we cannot derive from
8437 -- the tagged full view unless we have an extension.
8439 if Present (Available_Full_View (Parent_Type))
8440 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8441 and then not Error_Posted (N)
8442 then
8443 Build_Full_Derivation;
8445 if not Is_Completion then
8446 Set_Full_View (Derived_Type, Full_Der);
8447 else
8448 Set_Underlying_Full_View (Derived_Type, Full_Der);
8449 Set_Is_Underlying_Full_View (Full_Der);
8450 end if;
8451 end if;
8452 end if;
8454 Set_Has_Unknown_Discriminants (Derived_Type,
8455 Has_Unknown_Discriminants (Parent_Type));
8457 if Is_Private_Type (Derived_Type) then
8458 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8459 end if;
8461 -- If the parent base type is in scope, add the derived type to its
8462 -- list of private dependents, because its full view may become
8463 -- visible subsequently (in a nested private part, a body, or in a
8464 -- further child unit).
8466 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8467 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8469 -- Check for unusual case where a type completed by a private
8470 -- derivation occurs within a package nested in a child unit, and
8471 -- the parent is declared in an ancestor.
8473 if Is_Child_Unit (Scope (Current_Scope))
8474 and then Is_Completion
8475 and then In_Private_Part (Current_Scope)
8476 and then Scope (Parent_Type) /= Current_Scope
8478 -- Note that if the parent has a completion in the private part,
8479 -- (which is itself a derivation from some other private type)
8480 -- it is that completion that is visible, there is no full view
8481 -- available, and no special processing is needed.
8483 and then Present (Full_View (Parent_Type))
8484 then
8485 -- In this case, the full view of the parent type will become
8486 -- visible in the body of the enclosing child, and only then will
8487 -- the current type be possibly non-private. Build an underlying
8488 -- full view that will be installed when the enclosing child body
8489 -- is compiled.
8491 if Present (Underlying_Full_View (Derived_Type)) then
8492 Full_Der := Underlying_Full_View (Derived_Type);
8493 else
8494 Build_Full_Derivation;
8495 Set_Underlying_Full_View (Derived_Type, Full_Der);
8496 Set_Is_Underlying_Full_View (Full_Der);
8497 end if;
8499 -- The full view will be used to swap entities on entry/exit to
8500 -- the body, and must appear in the entity list for the package.
8502 Append_Entity (Full_Der, Scope (Derived_Type));
8503 end if;
8504 end if;
8505 end Build_Derived_Private_Type;
8507 -------------------------------
8508 -- Build_Derived_Record_Type --
8509 -------------------------------
8511 -- 1. INTRODUCTION
8513 -- Ideally we would like to use the same model of type derivation for
8514 -- tagged and untagged record types. Unfortunately this is not quite
8515 -- possible because the semantics of representation clauses is different
8516 -- for tagged and untagged records under inheritance. Consider the
8517 -- following:
8519 -- type R (...) is [tagged] record ... end record;
8520 -- type T (...) is new R (...) [with ...];
8522 -- The representation clauses for T can specify a completely different
8523 -- record layout from R's. Hence the same component can be placed in two
8524 -- very different positions in objects of type T and R. If R and T are
8525 -- tagged types, representation clauses for T can only specify the layout
8526 -- of non inherited components, thus components that are common in R and T
8527 -- have the same position in objects of type R and T.
8529 -- This has two implications. The first is that the entire tree for R's
8530 -- declaration needs to be copied for T in the untagged case, so that T
8531 -- can be viewed as a record type of its own with its own representation
8532 -- clauses. The second implication is the way we handle discriminants.
8533 -- Specifically, in the untagged case we need a way to communicate to Gigi
8534 -- what are the real discriminants in the record, while for the semantics
8535 -- we need to consider those introduced by the user to rename the
8536 -- discriminants in the parent type. This is handled by introducing the
8537 -- notion of stored discriminants. See below for more.
8539 -- Fortunately the way regular components are inherited can be handled in
8540 -- the same way in tagged and untagged types.
8542 -- To complicate things a bit more the private view of a private extension
8543 -- cannot be handled in the same way as the full view (for one thing the
8544 -- semantic rules are somewhat different). We will explain what differs
8545 -- below.
8547 -- 2. DISCRIMINANTS UNDER INHERITANCE
8549 -- The semantic rules governing the discriminants of derived types are
8550 -- quite subtle.
8552 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8553 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8555 -- If parent type has discriminants, then the discriminants that are
8556 -- declared in the derived type are [3.4 (11)]:
8558 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8559 -- there is one;
8561 -- o Otherwise, each discriminant of the parent type (implicitly declared
8562 -- in the same order with the same specifications). In this case, the
8563 -- discriminants are said to be "inherited", or if unknown in the parent
8564 -- are also unknown in the derived type.
8566 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8568 -- o The parent subtype must be constrained;
8570 -- o If the parent type is not a tagged type, then each discriminant of
8571 -- the derived type must be used in the constraint defining a parent
8572 -- subtype. [Implementation note: This ensures that the new discriminant
8573 -- can share storage with an existing discriminant.]
8575 -- For the derived type each discriminant of the parent type is either
8576 -- inherited, constrained to equal some new discriminant of the derived
8577 -- type, or constrained to the value of an expression.
8579 -- When inherited or constrained to equal some new discriminant, the
8580 -- parent discriminant and the discriminant of the derived type are said
8581 -- to "correspond".
8583 -- If a discriminant of the parent type is constrained to a specific value
8584 -- in the derived type definition, then the discriminant is said to be
8585 -- "specified" by that derived type definition.
8587 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8589 -- We have spoken about stored discriminants in point 1 (introduction)
8590 -- above. There are two sorts of stored discriminants: implicit and
8591 -- explicit. As long as the derived type inherits the same discriminants as
8592 -- the root record type, stored discriminants are the same as regular
8593 -- discriminants, and are said to be implicit. However, if any discriminant
8594 -- in the root type was renamed in the derived type, then the derived
8595 -- type will contain explicit stored discriminants. Explicit stored
8596 -- discriminants are discriminants in addition to the semantically visible
8597 -- discriminants defined for the derived type. Stored discriminants are
8598 -- used by Gigi to figure out what are the physical discriminants in
8599 -- objects of the derived type (see precise definition in einfo.ads).
8600 -- As an example, consider the following:
8602 -- type R (D1, D2, D3 : Int) is record ... end record;
8603 -- type T1 is new R;
8604 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8605 -- type T3 is new T2;
8606 -- type T4 (Y : Int) is new T3 (Y, 99);
8608 -- The following table summarizes the discriminants and stored
8609 -- discriminants in R and T1 through T4:
8611 -- Type Discrim Stored Discrim Comment
8612 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8613 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8614 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8615 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8616 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8618 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8619 -- find the corresponding discriminant in the parent type, while
8620 -- Original_Record_Component (abbreviated ORC below) the actual physical
8621 -- component that is renamed. Finally the field Is_Completely_Hidden
8622 -- (abbreviated ICH below) is set for all explicit stored discriminants
8623 -- (see einfo.ads for more info). For the above example this gives:
8625 -- Discrim CD ORC ICH
8626 -- ^^^^^^^ ^^ ^^^ ^^^
8627 -- D1 in R empty itself no
8628 -- D2 in R empty itself no
8629 -- D3 in R empty itself no
8631 -- D1 in T1 D1 in R itself no
8632 -- D2 in T1 D2 in R itself no
8633 -- D3 in T1 D3 in R itself no
8635 -- X1 in T2 D3 in T1 D3 in T2 no
8636 -- X2 in T2 D1 in T1 D1 in T2 no
8637 -- D1 in T2 empty itself yes
8638 -- D2 in T2 empty itself yes
8639 -- D3 in T2 empty itself yes
8641 -- X1 in T3 X1 in T2 D3 in T3 no
8642 -- X2 in T3 X2 in T2 D1 in T3 no
8643 -- D1 in T3 empty itself yes
8644 -- D2 in T3 empty itself yes
8645 -- D3 in T3 empty itself yes
8647 -- Y in T4 X1 in T3 D3 in T4 no
8648 -- D1 in T4 empty itself yes
8649 -- D2 in T4 empty itself yes
8650 -- D3 in T4 empty itself yes
8652 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8654 -- Type derivation for tagged types is fairly straightforward. If no
8655 -- discriminants are specified by the derived type, these are inherited
8656 -- from the parent. No explicit stored discriminants are ever necessary.
8657 -- The only manipulation that is done to the tree is that of adding a
8658 -- _parent field with parent type and constrained to the same constraint
8659 -- specified for the parent in the derived type definition. For instance:
8661 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8662 -- type T1 is new R with null record;
8663 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8665 -- are changed into:
8667 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8668 -- _parent : R (D1, D2, D3);
8669 -- end record;
8671 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8672 -- _parent : T1 (X2, 88, X1);
8673 -- end record;
8675 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8676 -- ORC and ICH fields are:
8678 -- Discrim CD ORC ICH
8679 -- ^^^^^^^ ^^ ^^^ ^^^
8680 -- D1 in R empty itself no
8681 -- D2 in R empty itself no
8682 -- D3 in R empty itself no
8684 -- D1 in T1 D1 in R D1 in R no
8685 -- D2 in T1 D2 in R D2 in R no
8686 -- D3 in T1 D3 in R D3 in R no
8688 -- X1 in T2 D3 in T1 D3 in R no
8689 -- X2 in T2 D1 in T1 D1 in R no
8691 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8693 -- Regardless of whether we dealing with a tagged or untagged type
8694 -- we will transform all derived type declarations of the form
8696 -- type T is new R (...) [with ...];
8697 -- or
8698 -- subtype S is R (...);
8699 -- type T is new S [with ...];
8700 -- into
8701 -- type BT is new R [with ...];
8702 -- subtype T is BT (...);
8704 -- That is, the base derived type is constrained only if it has no
8705 -- discriminants. The reason for doing this is that GNAT's semantic model
8706 -- assumes that a base type with discriminants is unconstrained.
8708 -- Note that, strictly speaking, the above transformation is not always
8709 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8711 -- procedure B34011A is
8712 -- type REC (D : integer := 0) is record
8713 -- I : Integer;
8714 -- end record;
8716 -- package P is
8717 -- type T6 is new Rec;
8718 -- function F return T6;
8719 -- end P;
8721 -- use P;
8722 -- package Q6 is
8723 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8724 -- end Q6;
8726 -- The definition of Q6.U is illegal. However transforming Q6.U into
8728 -- type BaseU is new T6;
8729 -- subtype U is BaseU (Q6.F.I)
8731 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8732 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8733 -- the transformation described above.
8735 -- There is another instance where the above transformation is incorrect.
8736 -- Consider:
8738 -- package Pack is
8739 -- type Base (D : Integer) is tagged null record;
8740 -- procedure P (X : Base);
8742 -- type Der is new Base (2) with null record;
8743 -- procedure P (X : Der);
8744 -- end Pack;
8746 -- Then the above transformation turns this into
8748 -- type Der_Base is new Base with null record;
8749 -- -- procedure P (X : Base) is implicitly inherited here
8750 -- -- as procedure P (X : Der_Base).
8752 -- subtype Der is Der_Base (2);
8753 -- procedure P (X : Der);
8754 -- -- The overriding of P (X : Der_Base) is illegal since we
8755 -- -- have a parameter conformance problem.
8757 -- To get around this problem, after having semantically processed Der_Base
8758 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8759 -- Discriminant_Constraint from Der so that when parameter conformance is
8760 -- checked when P is overridden, no semantic errors are flagged.
8762 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8764 -- Regardless of whether we are dealing with a tagged or untagged type
8765 -- we will transform all derived type declarations of the form
8767 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8768 -- type T is new R [with ...];
8769 -- into
8770 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8772 -- The reason for such transformation is that it allows us to implement a
8773 -- very clean form of component inheritance as explained below.
8775 -- Note that this transformation is not achieved by direct tree rewriting
8776 -- and manipulation, but rather by redoing the semantic actions that the
8777 -- above transformation will entail. This is done directly in routine
8778 -- Inherit_Components.
8780 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8782 -- In both tagged and untagged derived types, regular non discriminant
8783 -- components are inherited in the derived type from the parent type. In
8784 -- the absence of discriminants component, inheritance is straightforward
8785 -- as components can simply be copied from the parent.
8787 -- If the parent has discriminants, inheriting components constrained with
8788 -- these discriminants requires caution. Consider the following example:
8790 -- type R (D1, D2 : Positive) is [tagged] record
8791 -- S : String (D1 .. D2);
8792 -- end record;
8794 -- type T1 is new R [with null record];
8795 -- type T2 (X : positive) is new R (1, X) [with null record];
8797 -- As explained in 6. above, T1 is rewritten as
8798 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8799 -- which makes the treatment for T1 and T2 identical.
8801 -- What we want when inheriting S, is that references to D1 and D2 in R are
8802 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8803 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8804 -- with either discriminant references in the derived type or expressions.
8805 -- This replacement is achieved as follows: before inheriting R's
8806 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8807 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8808 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8809 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8810 -- by String (1 .. X).
8812 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8814 -- We explain here the rules governing private type extensions relevant to
8815 -- type derivation. These rules are explained on the following example:
8817 -- type D [(...)] is new A [(...)] with private; <-- partial view
8818 -- type D [(...)] is new P [(...)] with null record; <-- full view
8820 -- Type A is called the ancestor subtype of the private extension.
8821 -- Type P is the parent type of the full view of the private extension. It
8822 -- must be A or a type derived from A.
8824 -- The rules concerning the discriminants of private type extensions are
8825 -- [7.3(10-13)]:
8827 -- o If a private extension inherits known discriminants from the ancestor
8828 -- subtype, then the full view must also inherit its discriminants from
8829 -- the ancestor subtype and the parent subtype of the full view must be
8830 -- constrained if and only if the ancestor subtype is constrained.
8832 -- o If a partial view has unknown discriminants, then the full view may
8833 -- define a definite or an indefinite subtype, with or without
8834 -- discriminants.
8836 -- o If a partial view has neither known nor unknown discriminants, then
8837 -- the full view must define a definite subtype.
8839 -- o If the ancestor subtype of a private extension has constrained
8840 -- discriminants, then the parent subtype of the full view must impose a
8841 -- statically matching constraint on those discriminants.
8843 -- This means that only the following forms of private extensions are
8844 -- allowed:
8846 -- type D is new A with private; <-- partial view
8847 -- type D is new P with null record; <-- full view
8849 -- If A has no discriminants than P has no discriminants, otherwise P must
8850 -- inherit A's discriminants.
8852 -- type D is new A (...) with private; <-- partial view
8853 -- type D is new P (:::) with null record; <-- full view
8855 -- P must inherit A's discriminants and (...) and (:::) must statically
8856 -- match.
8858 -- subtype A is R (...);
8859 -- type D is new A with private; <-- partial view
8860 -- type D is new P with null record; <-- full view
8862 -- P must have inherited R's discriminants and must be derived from A or
8863 -- any of its subtypes.
8865 -- type D (..) is new A with private; <-- partial view
8866 -- type D (..) is new P [(:::)] with null record; <-- full view
8868 -- No specific constraints on P's discriminants or constraint (:::).
8869 -- Note that A can be unconstrained, but the parent subtype P must either
8870 -- be constrained or (:::) must be present.
8872 -- type D (..) is new A [(...)] with private; <-- partial view
8873 -- type D (..) is new P [(:::)] with null record; <-- full view
8875 -- P's constraints on A's discriminants must statically match those
8876 -- imposed by (...).
8878 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8880 -- The full view of a private extension is handled exactly as described
8881 -- above. The model chose for the private view of a private extension is
8882 -- the same for what concerns discriminants (i.e. they receive the same
8883 -- treatment as in the tagged case). However, the private view of the
8884 -- private extension always inherits the components of the parent base,
8885 -- without replacing any discriminant reference. Strictly speaking this is
8886 -- incorrect. However, Gigi never uses this view to generate code so this
8887 -- is a purely semantic issue. In theory, a set of transformations similar
8888 -- to those given in 5. and 6. above could be applied to private views of
8889 -- private extensions to have the same model of component inheritance as
8890 -- for non private extensions. However, this is not done because it would
8891 -- further complicate private type processing. Semantically speaking, this
8892 -- leaves us in an uncomfortable situation. As an example consider:
8894 -- package Pack is
8895 -- type R (D : integer) is tagged record
8896 -- S : String (1 .. D);
8897 -- end record;
8898 -- procedure P (X : R);
8899 -- type T is new R (1) with private;
8900 -- private
8901 -- type T is new R (1) with null record;
8902 -- end;
8904 -- This is transformed into:
8906 -- package Pack is
8907 -- type R (D : integer) is tagged record
8908 -- S : String (1 .. D);
8909 -- end record;
8910 -- procedure P (X : R);
8911 -- type T is new R (1) with private;
8912 -- private
8913 -- type BaseT is new R with null record;
8914 -- subtype T is BaseT (1);
8915 -- end;
8917 -- (strictly speaking the above is incorrect Ada)
8919 -- From the semantic standpoint the private view of private extension T
8920 -- should be flagged as constrained since one can clearly have
8922 -- Obj : T;
8924 -- in a unit withing Pack. However, when deriving subprograms for the
8925 -- private view of private extension T, T must be seen as unconstrained
8926 -- since T has discriminants (this is a constraint of the current
8927 -- subprogram derivation model). Thus, when processing the private view of
8928 -- a private extension such as T, we first mark T as unconstrained, we
8929 -- process it, we perform program derivation and just before returning from
8930 -- Build_Derived_Record_Type we mark T as constrained.
8932 -- ??? Are there are other uncomfortable cases that we will have to
8933 -- deal with.
8935 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8937 -- Types that are derived from a visible record type and have a private
8938 -- extension present other peculiarities. They behave mostly like private
8939 -- types, but if they have primitive operations defined, these will not
8940 -- have the proper signatures for further inheritance, because other
8941 -- primitive operations will use the implicit base that we define for
8942 -- private derivations below. This affect subprogram inheritance (see
8943 -- Derive_Subprograms for details). We also derive the implicit base from
8944 -- the base type of the full view, so that the implicit base is a record
8945 -- type and not another private type, This avoids infinite loops.
8947 procedure Build_Derived_Record_Type
8948 (N : Node_Id;
8949 Parent_Type : Entity_Id;
8950 Derived_Type : Entity_Id;
8951 Derive_Subps : Boolean := True)
8953 Discriminant_Specs : constant Boolean :=
8954 Present (Discriminant_Specifications (N));
8955 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8956 Loc : constant Source_Ptr := Sloc (N);
8957 Private_Extension : constant Boolean :=
8958 Nkind (N) = N_Private_Extension_Declaration;
8959 Assoc_List : Elist_Id;
8960 Constraint_Present : Boolean;
8961 Constrs : Elist_Id;
8962 Discrim : Entity_Id;
8963 Indic : Node_Id;
8964 Inherit_Discrims : Boolean := False;
8965 Last_Discrim : Entity_Id;
8966 New_Base : Entity_Id;
8967 New_Decl : Node_Id;
8968 New_Discrs : Elist_Id;
8969 New_Indic : Node_Id;
8970 Parent_Base : Entity_Id;
8971 Save_Etype : Entity_Id;
8972 Save_Discr_Constr : Elist_Id;
8973 Save_Next_Entity : Entity_Id;
8974 Type_Def : Node_Id;
8976 Discs : Elist_Id := New_Elmt_List;
8977 -- An empty Discs list means that there were no constraints in the
8978 -- subtype indication or that there was an error processing it.
8980 procedure Check_Generic_Ancestors;
8981 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8982 -- cannot be declared at a deeper level than its parent type is
8983 -- removed. The check on derivation within a generic body is also
8984 -- relaxed, but there's a restriction that a derived tagged type
8985 -- cannot be declared in a generic body if it's derived directly
8986 -- or indirectly from a formal type of that generic. This applies
8987 -- to progenitors as well.
8989 -----------------------------
8990 -- Check_Generic_Ancestors --
8991 -----------------------------
8993 procedure Check_Generic_Ancestors is
8994 Ancestor_Type : Entity_Id;
8995 Intf_List : List_Id;
8996 Intf_Name : Node_Id;
8998 procedure Check_Ancestor;
8999 -- For parent and progenitors.
9001 --------------------
9002 -- Check_Ancestor --
9003 --------------------
9005 procedure Check_Ancestor is
9006 begin
9007 -- If the derived type does have a formal type as an ancestor
9008 -- then it's an error if the derived type is declared within
9009 -- the body of the generic unit that declares the formal type
9010 -- in its generic formal part. It's sufficient to check whether
9011 -- the ancestor type is declared inside the same generic body
9012 -- as the derived type (such as within a nested generic spec),
9013 -- in which case the derivation is legal. If the formal type is
9014 -- declared outside of that generic body, then it's certain
9015 -- that the derived type is declared within the generic body
9016 -- of the generic unit declaring the formal type.
9018 if Is_Generic_Type (Ancestor_Type)
9019 and then Enclosing_Generic_Body (Ancestor_Type) /=
9020 Enclosing_Generic_Body (Derived_Type)
9021 then
9022 Error_Msg_NE
9023 ("ancestor type& is formal type of enclosing"
9024 & " generic unit (RM 3.9.1 (4/2))",
9025 Indic, Ancestor_Type);
9026 end if;
9027 end Check_Ancestor;
9029 begin
9030 if Nkind (N) = N_Private_Extension_Declaration then
9031 Intf_List := Interface_List (N);
9032 else
9033 Intf_List := Interface_List (Type_Definition (N));
9034 end if;
9036 if Present (Enclosing_Generic_Body (Derived_Type)) then
9037 Ancestor_Type := Parent_Type;
9039 while not Is_Generic_Type (Ancestor_Type)
9040 and then Etype (Ancestor_Type) /= Ancestor_Type
9041 loop
9042 Ancestor_Type := Etype (Ancestor_Type);
9043 end loop;
9045 Check_Ancestor;
9047 if Present (Intf_List) then
9048 Intf_Name := First (Intf_List);
9049 while Present (Intf_Name) loop
9050 Ancestor_Type := Entity (Intf_Name);
9051 Check_Ancestor;
9052 Next (Intf_Name);
9053 end loop;
9054 end if;
9055 end if;
9056 end Check_Generic_Ancestors;
9058 -- Start of processing for Build_Derived_Record_Type
9060 begin
9061 if Ekind (Parent_Type) = E_Record_Type_With_Private
9062 and then Present (Full_View (Parent_Type))
9063 and then Has_Discriminants (Parent_Type)
9064 then
9065 Parent_Base := Base_Type (Full_View (Parent_Type));
9066 else
9067 Parent_Base := Base_Type (Parent_Type);
9068 end if;
9070 -- If the parent type is declared as a subtype of another private
9071 -- type with inherited discriminants, its generated base type is
9072 -- itself a record subtype. To further inherit the constraint we
9073 -- need to use its own base to have an unconstrained type on which
9074 -- to apply the inherited constraint.
9076 if Ekind (Parent_Base) = E_Record_Subtype then
9077 Parent_Base := Base_Type (Parent_Base);
9078 end if;
9080 -- AI05-0115: if this is a derivation from a private type in some
9081 -- other scope that may lead to invisible components for the derived
9082 -- type, mark it accordingly.
9084 if Is_Private_Type (Parent_Type) then
9085 if Scope (Parent_Base) = Scope (Derived_Type) then
9086 null;
9088 elsif In_Open_Scopes (Scope (Parent_Base))
9089 and then In_Private_Part (Scope (Parent_Base))
9090 then
9091 null;
9093 else
9094 Set_Has_Private_Ancestor (Derived_Type);
9095 end if;
9097 else
9098 Set_Has_Private_Ancestor
9099 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9100 end if;
9102 -- Before we start the previously documented transformations, here is
9103 -- little fix for size and alignment of tagged types. Normally when we
9104 -- derive type D from type P, we copy the size and alignment of P as the
9105 -- default for D, and in the absence of explicit representation clauses
9106 -- for D, the size and alignment are indeed the same as the parent.
9108 -- But this is wrong for tagged types, since fields may be added, and
9109 -- the default size may need to be larger, and the default alignment may
9110 -- need to be larger.
9112 -- We therefore reset the size and alignment fields in the tagged case.
9113 -- Note that the size and alignment will in any case be at least as
9114 -- large as the parent type (since the derived type has a copy of the
9115 -- parent type in the _parent field)
9117 -- The type is also marked as being tagged here, which is needed when
9118 -- processing components with a self-referential anonymous access type
9119 -- in the call to Check_Anonymous_Access_Components below. Note that
9120 -- this flag is also set later on for completeness.
9122 if Is_Tagged then
9123 Set_Is_Tagged_Type (Derived_Type);
9124 Reinit_Size_Align (Derived_Type);
9125 end if;
9127 -- STEP 0a: figure out what kind of derived type declaration we have
9129 if Private_Extension then
9130 Type_Def := N;
9131 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9132 Set_Default_SSO (Derived_Type);
9133 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9135 else
9136 Type_Def := Type_Definition (N);
9138 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9139 -- Parent_Base can be a private type or private extension. However,
9140 -- for tagged types with an extension the newly added fields are
9141 -- visible and hence the Derived_Type is always an E_Record_Type.
9142 -- (except that the parent may have its own private fields).
9143 -- For untagged types we preserve the Ekind of the Parent_Base.
9145 if Present (Record_Extension_Part (Type_Def)) then
9146 Mutate_Ekind (Derived_Type, E_Record_Type);
9147 Set_Default_SSO (Derived_Type);
9148 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9150 -- Create internal access types for components with anonymous
9151 -- access types.
9153 if Ada_Version >= Ada_2005 then
9154 Check_Anonymous_Access_Components
9155 (N, Derived_Type, Derived_Type,
9156 Component_List (Record_Extension_Part (Type_Def)));
9157 end if;
9159 else
9160 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9161 end if;
9162 end if;
9164 -- Indic can either be an N_Identifier if the subtype indication
9165 -- contains no constraint or an N_Subtype_Indication if the subtype
9166 -- indication has a constraint. In either case it can include an
9167 -- interface list.
9169 Indic := Subtype_Indication (Type_Def);
9170 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9172 -- Check that the type has visible discriminants. The type may be
9173 -- a private type with unknown discriminants whose full view has
9174 -- discriminants which are invisible.
9176 if Constraint_Present then
9177 if not Has_Discriminants (Parent_Base)
9178 or else
9179 (Has_Unknown_Discriminants (Parent_Base)
9180 and then Is_Private_Type (Parent_Base))
9181 then
9182 Error_Msg_N
9183 ("invalid constraint: type has no discriminant",
9184 Constraint (Indic));
9186 Constraint_Present := False;
9187 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9189 elsif Is_Constrained (Parent_Type) then
9190 Error_Msg_N
9191 ("invalid constraint: parent type is already constrained",
9192 Constraint (Indic));
9194 Constraint_Present := False;
9195 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9196 end if;
9197 end if;
9199 -- STEP 0b: If needed, apply transformation given in point 5. above
9201 if not Private_Extension
9202 and then Has_Discriminants (Parent_Type)
9203 and then not Discriminant_Specs
9204 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9205 then
9206 -- First, we must analyze the constraint (see comment in point 5.)
9207 -- The constraint may come from the subtype indication of the full
9208 -- declaration.
9210 if Constraint_Present then
9211 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9213 -- If there is no explicit constraint, there might be one that is
9214 -- inherited from a constrained parent type. In that case verify that
9215 -- it conforms to the constraint in the partial view. In perverse
9216 -- cases the parent subtypes of the partial and full view can have
9217 -- different constraints.
9219 elsif Present (Stored_Constraint (Parent_Type)) then
9220 New_Discrs := Stored_Constraint (Parent_Type);
9222 else
9223 New_Discrs := No_Elist;
9224 end if;
9226 if Has_Discriminants (Derived_Type)
9227 and then Has_Private_Declaration (Derived_Type)
9228 and then Present (Discriminant_Constraint (Derived_Type))
9229 and then Present (New_Discrs)
9230 then
9231 -- Verify that constraints of the full view statically match
9232 -- those given in the partial view.
9234 declare
9235 C1, C2 : Elmt_Id;
9237 begin
9238 C1 := First_Elmt (New_Discrs);
9239 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9240 while Present (C1) and then Present (C2) loop
9241 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9242 or else
9243 (Is_OK_Static_Expression (Node (C1))
9244 and then Is_OK_Static_Expression (Node (C2))
9245 and then
9246 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9247 then
9248 null;
9250 else
9251 if Constraint_Present then
9252 Error_Msg_N
9253 ("constraint not conformant to previous declaration",
9254 Node (C1));
9255 else
9256 Error_Msg_N
9257 ("constraint of full view is incompatible "
9258 & "with partial view", N);
9259 end if;
9260 end if;
9262 Next_Elmt (C1);
9263 Next_Elmt (C2);
9264 end loop;
9265 end;
9266 end if;
9268 -- Insert and analyze the declaration for the unconstrained base type
9270 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9272 New_Decl :=
9273 Make_Full_Type_Declaration (Loc,
9274 Defining_Identifier => New_Base,
9275 Type_Definition =>
9276 Make_Derived_Type_Definition (Loc,
9277 Abstract_Present => Abstract_Present (Type_Def),
9278 Limited_Present => Limited_Present (Type_Def),
9279 Subtype_Indication =>
9280 New_Occurrence_Of (Parent_Base, Loc),
9281 Record_Extension_Part =>
9282 Relocate_Node (Record_Extension_Part (Type_Def)),
9283 Interface_List => Interface_List (Type_Def)));
9285 Set_Parent (New_Decl, Parent (N));
9286 Mark_Rewrite_Insertion (New_Decl);
9287 Insert_Before (N, New_Decl);
9289 -- In the extension case, make sure ancestor is frozen appropriately
9290 -- (see also non-discriminated case below).
9292 if Present (Record_Extension_Part (Type_Def))
9293 or else Is_Interface (Parent_Base)
9294 then
9295 Freeze_Before (New_Decl, Parent_Type);
9296 end if;
9298 -- Note that this call passes False for the Derive_Subps parameter
9299 -- because subprogram derivation is deferred until after creating
9300 -- the subtype (see below).
9302 Build_Derived_Type
9303 (New_Decl, Parent_Base, New_Base,
9304 Is_Completion => False, Derive_Subps => False);
9306 -- ??? This needs re-examination to determine whether the
9307 -- above call can simply be replaced by a call to Analyze.
9309 Set_Analyzed (New_Decl);
9311 -- Insert and analyze the declaration for the constrained subtype
9313 if Constraint_Present then
9314 New_Indic :=
9315 Make_Subtype_Indication (Loc,
9316 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9317 Constraint => Relocate_Node (Constraint (Indic)));
9319 else
9320 declare
9321 Constr_List : constant List_Id := New_List;
9322 C : Elmt_Id;
9323 Expr : Node_Id;
9325 begin
9326 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9327 while Present (C) loop
9328 Expr := Node (C);
9330 -- It is safe here to call New_Copy_Tree since we called
9331 -- Force_Evaluation on each constraint previously
9332 -- in Build_Discriminant_Constraints.
9334 Append (New_Copy_Tree (Expr), To => Constr_List);
9336 Next_Elmt (C);
9337 end loop;
9339 New_Indic :=
9340 Make_Subtype_Indication (Loc,
9341 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9342 Constraint =>
9343 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9344 end;
9345 end if;
9347 Rewrite (N,
9348 Make_Subtype_Declaration (Loc,
9349 Defining_Identifier => Derived_Type,
9350 Subtype_Indication => New_Indic));
9352 Analyze (N);
9354 -- Derivation of subprograms must be delayed until the full subtype
9355 -- has been established, to ensure proper overriding of subprograms
9356 -- inherited by full types. If the derivations occurred as part of
9357 -- the call to Build_Derived_Type above, then the check for type
9358 -- conformance would fail because earlier primitive subprograms
9359 -- could still refer to the full type prior the change to the new
9360 -- subtype and hence would not match the new base type created here.
9361 -- Subprograms are not derived, however, when Derive_Subps is False
9362 -- (since otherwise there could be redundant derivations).
9364 if Derive_Subps then
9365 Derive_Subprograms (Parent_Type, Derived_Type);
9366 end if;
9368 -- For tagged types the Discriminant_Constraint of the new base itype
9369 -- is inherited from the first subtype so that no subtype conformance
9370 -- problem arise when the first subtype overrides primitive
9371 -- operations inherited by the implicit base type.
9373 if Is_Tagged then
9374 Set_Discriminant_Constraint
9375 (New_Base, Discriminant_Constraint (Derived_Type));
9376 end if;
9378 return;
9379 end if;
9381 -- If we get here Derived_Type will have no discriminants or it will be
9382 -- a discriminated unconstrained base type.
9384 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9386 if Is_Tagged then
9388 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9389 -- The declaration of a specific descendant of an interface type
9390 -- freezes the interface type (RM 13.14).
9392 if not Private_Extension or else Is_Interface (Parent_Base) then
9393 Freeze_Before (N, Parent_Type);
9394 end if;
9396 if Ada_Version >= Ada_2005 then
9397 Check_Generic_Ancestors;
9399 elsif Type_Access_Level (Derived_Type) /=
9400 Type_Access_Level (Parent_Type)
9401 and then not Is_Generic_Type (Derived_Type)
9402 then
9403 if Is_Controlled (Parent_Type) then
9404 Error_Msg_N
9405 ("controlled type must be declared at the library level",
9406 Indic);
9407 else
9408 Error_Msg_N
9409 ("type extension at deeper accessibility level than parent",
9410 Indic);
9411 end if;
9413 else
9414 declare
9415 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9416 begin
9417 if Present (GB)
9418 and then GB /= Enclosing_Generic_Body (Parent_Base)
9419 then
9420 Error_Msg_NE
9421 ("parent type of& must not be outside generic body"
9422 & " (RM 3.9.1(4))",
9423 Indic, Derived_Type);
9424 end if;
9425 end;
9426 end if;
9427 end if;
9429 -- Ada 2005 (AI-251)
9431 if Ada_Version >= Ada_2005 and then Is_Tagged then
9433 -- "The declaration of a specific descendant of an interface type
9434 -- freezes the interface type" (RM 13.14).
9436 declare
9437 Iface : Node_Id;
9438 begin
9439 Iface := First (Interface_List (Type_Def));
9440 while Present (Iface) loop
9441 Freeze_Before (N, Etype (Iface));
9442 Next (Iface);
9443 end loop;
9444 end;
9445 end if;
9447 -- STEP 1b : preliminary cleanup of the full view of private types
9449 -- If the type is already marked as having discriminants, then it's the
9450 -- completion of a private type or private extension and we need to
9451 -- retain the discriminants from the partial view if the current
9452 -- declaration has Discriminant_Specifications so that we can verify
9453 -- conformance. However, we must remove any existing components that
9454 -- were inherited from the parent (and attached in Copy_And_Swap)
9455 -- because the full type inherits all appropriate components anyway, and
9456 -- we do not want the partial view's components interfering.
9458 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9459 Discrim := First_Discriminant (Derived_Type);
9460 loop
9461 Last_Discrim := Discrim;
9462 Next_Discriminant (Discrim);
9463 exit when No (Discrim);
9464 end loop;
9466 Set_Last_Entity (Derived_Type, Last_Discrim);
9468 -- In all other cases wipe out the list of inherited components (even
9469 -- inherited discriminants), it will be properly rebuilt here.
9471 else
9472 Set_First_Entity (Derived_Type, Empty);
9473 Set_Last_Entity (Derived_Type, Empty);
9474 end if;
9476 -- STEP 1c: Initialize some flags for the Derived_Type
9478 -- The following flags must be initialized here so that
9479 -- Process_Discriminants can check that discriminants of tagged types do
9480 -- not have a default initial value and that access discriminants are
9481 -- only specified for limited records. For completeness, these flags are
9482 -- also initialized along with all the other flags below.
9484 -- AI-419: Limitedness is not inherited from an interface parent, so to
9485 -- be limited in that case the type must be explicitly declared as
9486 -- limited. However, task and protected interfaces are always limited.
9488 if Limited_Present (Type_Def) then
9489 Set_Is_Limited_Record (Derived_Type);
9491 elsif Is_Limited_Record (Parent_Type)
9492 or else (Present (Full_View (Parent_Type))
9493 and then Is_Limited_Record (Full_View (Parent_Type)))
9494 then
9495 if not Is_Interface (Parent_Type)
9496 or else Is_Concurrent_Interface (Parent_Type)
9497 then
9498 Set_Is_Limited_Record (Derived_Type);
9499 end if;
9500 end if;
9502 -- STEP 2a: process discriminants of derived type if any
9504 Push_Scope (Derived_Type);
9506 if Discriminant_Specs then
9507 Set_Has_Unknown_Discriminants (Derived_Type, False);
9509 -- The following call initializes fields Has_Discriminants and
9510 -- Discriminant_Constraint, unless we are processing the completion
9511 -- of a private type declaration.
9513 Check_Or_Process_Discriminants (N, Derived_Type);
9515 -- For untagged types, the constraint on the Parent_Type must be
9516 -- present and is used to rename the discriminants.
9518 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9519 Error_Msg_N ("untagged parent must have discriminants", Indic);
9521 elsif not Is_Tagged and then not Constraint_Present then
9522 Error_Msg_N
9523 ("discriminant constraint needed for derived untagged records",
9524 Indic);
9526 -- Otherwise the parent subtype must be constrained unless we have a
9527 -- private extension.
9529 elsif not Constraint_Present
9530 and then not Private_Extension
9531 and then not Is_Constrained (Parent_Type)
9532 then
9533 Error_Msg_N
9534 ("unconstrained type not allowed in this context", Indic);
9536 elsif Constraint_Present then
9537 -- The following call sets the field Corresponding_Discriminant
9538 -- for the discriminants in the Derived_Type.
9540 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9542 -- For untagged types all new discriminants must rename
9543 -- discriminants in the parent. For private extensions new
9544 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9546 Discrim := First_Discriminant (Derived_Type);
9547 while Present (Discrim) loop
9548 if not Is_Tagged
9549 and then No (Corresponding_Discriminant (Discrim))
9550 then
9551 Error_Msg_N
9552 ("new discriminants must constrain old ones", Discrim);
9554 elsif Private_Extension
9555 and then Present (Corresponding_Discriminant (Discrim))
9556 then
9557 Error_Msg_N
9558 ("only static constraints allowed for parent"
9559 & " discriminants in the partial view", Indic);
9560 exit;
9561 end if;
9563 -- If a new discriminant is used in the constraint, then its
9564 -- subtype must be statically compatible with the subtype of
9565 -- the parent discriminant (RM 3.7(15)).
9567 if Present (Corresponding_Discriminant (Discrim)) then
9568 Check_Constraining_Discriminant
9569 (Discrim, Corresponding_Discriminant (Discrim));
9570 end if;
9572 Next_Discriminant (Discrim);
9573 end loop;
9575 -- Check whether the constraints of the full view statically
9576 -- match those imposed by the parent subtype [7.3(13)].
9578 if Present (Stored_Constraint (Derived_Type)) then
9579 declare
9580 C1, C2 : Elmt_Id;
9582 begin
9583 C1 := First_Elmt (Discs);
9584 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9585 while Present (C1) and then Present (C2) loop
9586 if not
9587 Fully_Conformant_Expressions (Node (C1), Node (C2))
9588 then
9589 Error_Msg_N
9590 ("not conformant with previous declaration",
9591 Node (C1));
9592 end if;
9594 Next_Elmt (C1);
9595 Next_Elmt (C2);
9596 end loop;
9597 end;
9598 end if;
9599 end if;
9601 -- STEP 2b: No new discriminants, inherit discriminants if any
9603 else
9604 if Private_Extension then
9605 Set_Has_Unknown_Discriminants
9606 (Derived_Type,
9607 Has_Unknown_Discriminants (Parent_Type)
9608 or else Unknown_Discriminants_Present (N));
9610 -- The partial view of the parent may have unknown discriminants,
9611 -- but if the full view has discriminants and the parent type is
9612 -- in scope they must be inherited.
9614 elsif Has_Unknown_Discriminants (Parent_Type)
9615 and then
9616 (not Has_Discriminants (Parent_Type)
9617 or else not In_Open_Scopes (Scope (Parent_Base)))
9618 then
9619 Set_Has_Unknown_Discriminants (Derived_Type);
9620 end if;
9622 if not Has_Unknown_Discriminants (Derived_Type)
9623 and then not Has_Unknown_Discriminants (Parent_Base)
9624 and then Has_Discriminants (Parent_Type)
9625 then
9626 Inherit_Discrims := True;
9627 Set_Has_Discriminants
9628 (Derived_Type, True);
9629 Set_Discriminant_Constraint
9630 (Derived_Type, Discriminant_Constraint (Parent_Base));
9631 end if;
9633 -- The following test is true for private types (remember
9634 -- transformation 5. is not applied to those) and in an error
9635 -- situation.
9637 if Constraint_Present then
9638 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9639 end if;
9641 -- For now mark a new derived type as constrained only if it has no
9642 -- discriminants. At the end of Build_Derived_Record_Type we properly
9643 -- set this flag in the case of private extensions. See comments in
9644 -- point 9. just before body of Build_Derived_Record_Type.
9646 Set_Is_Constrained
9647 (Derived_Type,
9648 not (Inherit_Discrims
9649 or else Has_Unknown_Discriminants (Derived_Type)));
9650 end if;
9652 -- STEP 3: initialize fields of derived type
9654 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9655 Set_Stored_Constraint (Derived_Type, No_Elist);
9657 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9658 -- but cannot be interfaces
9660 if not Private_Extension
9661 and then Ekind (Derived_Type) /= E_Private_Type
9662 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9663 then
9664 if Interface_Present (Type_Def) then
9665 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9666 end if;
9668 Set_Interfaces (Derived_Type, No_Elist);
9669 end if;
9671 -- Fields inherited from the Parent_Type
9673 Set_Has_Specified_Layout
9674 (Derived_Type, Has_Specified_Layout (Parent_Type));
9675 Set_Is_Limited_Composite
9676 (Derived_Type, Is_Limited_Composite (Parent_Type));
9677 Set_Is_Private_Composite
9678 (Derived_Type, Is_Private_Composite (Parent_Type));
9680 if Is_Tagged_Type (Parent_Type) then
9681 Set_No_Tagged_Streams_Pragma
9682 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9683 end if;
9685 -- Fields inherited from the Parent_Base
9687 Set_Has_Controlled_Component
9688 (Derived_Type, Has_Controlled_Component (Parent_Base));
9689 Set_Has_Non_Standard_Rep
9690 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9691 Set_Has_Primitive_Operations
9692 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9694 -- Set fields for private derived types
9696 if Is_Private_Type (Derived_Type) then
9697 Set_Depends_On_Private (Derived_Type, True);
9698 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9699 end if;
9701 -- Inherit fields for non-private types. If this is the completion of a
9702 -- derivation from a private type, the parent itself is private and the
9703 -- attributes come from its full view, which must be present.
9705 if Is_Record_Type (Derived_Type) then
9706 declare
9707 Parent_Full : Entity_Id;
9709 begin
9710 if Is_Private_Type (Parent_Base)
9711 and then not Is_Record_Type (Parent_Base)
9712 then
9713 Parent_Full := Full_View (Parent_Base);
9714 else
9715 Parent_Full := Parent_Base;
9716 end if;
9718 Set_Component_Alignment
9719 (Derived_Type, Component_Alignment (Parent_Full));
9720 Set_C_Pass_By_Copy
9721 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9722 Set_Has_Complex_Representation
9723 (Derived_Type, Has_Complex_Representation (Parent_Full));
9725 -- For untagged types, inherit the layout by default to avoid
9726 -- costly changes of representation for type conversions.
9728 if not Is_Tagged then
9729 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9730 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9731 end if;
9732 end;
9733 end if;
9735 -- Initialize the list of primitive operations to an empty list,
9736 -- to cover tagged types as well as untagged types. For untagged
9737 -- types this is used either to analyze the call as legal when
9738 -- Extensions_Allowed is True, or to issue a better error message
9739 -- otherwise.
9741 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9743 -- Set fields for tagged types
9745 if Is_Tagged then
9746 -- All tagged types defined in Ada.Finalization are controlled
9748 if Chars (Scope (Derived_Type)) = Name_Finalization
9749 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9750 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9751 then
9752 Set_Is_Controlled_Active (Derived_Type);
9753 else
9754 Set_Is_Controlled_Active
9755 (Derived_Type, Is_Controlled_Active (Parent_Base));
9756 end if;
9758 -- Minor optimization: there is no need to generate the class-wide
9759 -- entity associated with an underlying record view.
9761 if not Is_Underlying_Record_View (Derived_Type) then
9762 Make_Class_Wide_Type (Derived_Type);
9763 end if;
9765 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9767 if Has_Discriminants (Derived_Type)
9768 and then Constraint_Present
9769 then
9770 Set_Stored_Constraint
9771 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9772 end if;
9774 if Ada_Version >= Ada_2005 then
9775 declare
9776 Ifaces_List : Elist_Id;
9778 begin
9779 -- Checks rules 3.9.4 (13/2 and 14/2)
9781 if Comes_From_Source (Derived_Type)
9782 and then not Is_Private_Type (Derived_Type)
9783 and then Is_Interface (Parent_Type)
9784 and then not Is_Interface (Derived_Type)
9785 then
9786 if Is_Task_Interface (Parent_Type) then
9787 Error_Msg_N
9788 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9789 Derived_Type);
9791 elsif Is_Protected_Interface (Parent_Type) then
9792 Error_Msg_N
9793 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9794 Derived_Type);
9795 end if;
9796 end if;
9798 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9800 Check_Interfaces (N, Type_Def);
9802 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9803 -- not already in the parents.
9805 Collect_Interfaces
9806 (T => Derived_Type,
9807 Ifaces_List => Ifaces_List,
9808 Exclude_Parents => True);
9810 Set_Interfaces (Derived_Type, Ifaces_List);
9812 -- If the derived type is the anonymous type created for
9813 -- a declaration whose parent has a constraint, propagate
9814 -- the interface list to the source type. This must be done
9815 -- prior to the completion of the analysis of the source type
9816 -- because the components in the extension may contain current
9817 -- instances whose legality depends on some ancestor.
9819 if Is_Itype (Derived_Type) then
9820 declare
9821 Def : constant Node_Id :=
9822 Associated_Node_For_Itype (Derived_Type);
9823 begin
9824 if Present (Def)
9825 and then Nkind (Def) = N_Full_Type_Declaration
9826 then
9827 Set_Interfaces
9828 (Defining_Identifier (Def), Ifaces_List);
9829 end if;
9830 end;
9831 end if;
9833 -- A type extension is automatically Ghost when one of its
9834 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9835 -- also inherited when the parent type is Ghost, but this is
9836 -- done in Build_Derived_Type as the mechanism also handles
9837 -- untagged derivations.
9839 if Implements_Ghost_Interface (Derived_Type) then
9840 Set_Is_Ghost_Entity (Derived_Type);
9841 end if;
9842 end;
9843 end if;
9844 end if;
9846 -- STEP 4: Inherit components from the parent base and constrain them.
9847 -- Apply the second transformation described in point 6. above.
9849 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9850 or else not Has_Discriminants (Parent_Type)
9851 or else not Is_Constrained (Parent_Type)
9852 then
9853 Constrs := Discs;
9854 else
9855 Constrs := Discriminant_Constraint (Parent_Type);
9856 end if;
9858 Assoc_List :=
9859 Inherit_Components
9860 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9862 -- STEP 5a: Copy the parent record declaration for untagged types
9864 Set_Has_Implicit_Dereference
9865 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9867 if not Is_Tagged then
9869 -- Discriminant_Constraint (Derived_Type) has been properly
9870 -- constructed. Save it and temporarily set it to Empty because we
9871 -- do not want the call to New_Copy_Tree below to mess this list.
9873 if Has_Discriminants (Derived_Type) then
9874 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9875 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9876 else
9877 Save_Discr_Constr := No_Elist;
9878 end if;
9880 -- Save the Etype field of Derived_Type. It is correctly set now,
9881 -- but the call to New_Copy tree may remap it to point to itself,
9882 -- which is not what we want. Ditto for the Next_Entity field.
9884 Save_Etype := Etype (Derived_Type);
9885 Save_Next_Entity := Next_Entity (Derived_Type);
9887 -- Assoc_List maps all stored discriminants in the Parent_Base to
9888 -- stored discriminants in the Derived_Type. It is fundamental that
9889 -- no types or itypes with discriminants other than the stored
9890 -- discriminants appear in the entities declared inside
9891 -- Derived_Type, since the back end cannot deal with it.
9893 New_Decl :=
9894 New_Copy_Tree
9895 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9896 Copy_Dimensions_Of_Components (Derived_Type);
9898 -- Restore the fields saved prior to the New_Copy_Tree call
9899 -- and compute the stored constraint.
9901 Set_Etype (Derived_Type, Save_Etype);
9902 Link_Entities (Derived_Type, Save_Next_Entity);
9904 if Has_Discriminants (Derived_Type) then
9905 Set_Discriminant_Constraint
9906 (Derived_Type, Save_Discr_Constr);
9907 Set_Stored_Constraint
9908 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9910 Replace_Discriminants (Derived_Type, New_Decl);
9911 end if;
9913 -- Insert the new derived type declaration
9915 Rewrite (N, New_Decl);
9917 -- STEP 5b: Complete the processing for record extensions in generics
9919 -- There is no completion for record extensions declared in the
9920 -- parameter part of a generic, so we need to complete processing for
9921 -- these generic record extensions here. The Record_Type_Definition call
9922 -- will change the Ekind of the components from E_Void to E_Component.
9924 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9925 Record_Type_Definition (Empty, Derived_Type);
9927 -- STEP 5c: Process the record extension for non private tagged types
9929 elsif not Private_Extension then
9930 Expand_Record_Extension (Derived_Type, Type_Def);
9932 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9933 -- implemented interfaces if we are in expansion mode
9935 if Expander_Active
9936 and then Has_Interfaces (Derived_Type)
9937 then
9938 Add_Interface_Tag_Components (N, Derived_Type);
9939 end if;
9941 -- Analyze the record extension
9943 Record_Type_Definition
9944 (Record_Extension_Part (Type_Def), Derived_Type);
9945 end if;
9947 End_Scope;
9949 -- Nothing else to do if there is an error in the derivation.
9950 -- An unusual case: the full view may be derived from a type in an
9951 -- instance, when the partial view was used illegally as an actual
9952 -- in that instance, leading to a circular definition.
9954 if Etype (Derived_Type) = Any_Type
9955 or else Etype (Parent_Type) = Derived_Type
9956 then
9957 return;
9958 end if;
9960 -- Set delayed freeze and then derive subprograms, we need to do
9961 -- this in this order so that derived subprograms inherit the
9962 -- derived freeze if necessary.
9964 Set_Has_Delayed_Freeze (Derived_Type);
9966 if Derive_Subps then
9967 Derive_Subprograms (Parent_Type, Derived_Type);
9968 end if;
9970 -- If we have a private extension which defines a constrained derived
9971 -- type mark as constrained here after we have derived subprograms. See
9972 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9974 if Private_Extension and then Inherit_Discrims then
9975 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9976 Set_Is_Constrained (Derived_Type, True);
9977 Set_Discriminant_Constraint (Derived_Type, Discs);
9979 elsif Is_Constrained (Parent_Type) then
9980 Set_Is_Constrained
9981 (Derived_Type, True);
9982 Set_Discriminant_Constraint
9983 (Derived_Type, Discriminant_Constraint (Parent_Type));
9984 end if;
9985 end if;
9987 -- Update the class-wide type, which shares the now-completed entity
9988 -- list with its specific type. In case of underlying record views,
9989 -- we do not generate the corresponding class wide entity.
9991 if Is_Tagged
9992 and then not Is_Underlying_Record_View (Derived_Type)
9993 then
9994 Set_First_Entity
9995 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9996 Set_Last_Entity
9997 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9998 end if;
10000 Check_Function_Writable_Actuals (N);
10001 end Build_Derived_Record_Type;
10003 ------------------------
10004 -- Build_Derived_Type --
10005 ------------------------
10007 procedure Build_Derived_Type
10008 (N : Node_Id;
10009 Parent_Type : Entity_Id;
10010 Derived_Type : Entity_Id;
10011 Is_Completion : Boolean;
10012 Derive_Subps : Boolean := True)
10014 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10016 begin
10017 -- Set common attributes
10019 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10020 and then Ekind (Parent_Base) in Elementary_Kind
10021 then
10022 Reinit_Field_To_Zero (Derived_Type, F_Discriminant_Constraint);
10023 end if;
10025 Set_Scope (Derived_Type, Current_Scope);
10026 Set_Etype (Derived_Type, Parent_Base);
10027 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10028 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10030 Set_Size_Info (Derived_Type, Parent_Type);
10031 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10033 Set_Is_Controlled_Active
10034 (Derived_Type, Is_Controlled_Active (Parent_Type));
10036 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
10037 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10038 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10040 if Is_Tagged_Type (Derived_Type) then
10041 Set_No_Tagged_Streams_Pragma
10042 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10043 end if;
10045 -- If the parent has primitive routines and may have not-seen-yet aspect
10046 -- specifications (e.g., a Pack pragma), then set the derived type link
10047 -- in order to later diagnose "early derivation" issues. If in different
10048 -- compilation units, then "early derivation" cannot be an issue (and we
10049 -- don't like interunit references that go in the opposite direction of
10050 -- semantic dependencies).
10052 if Has_Primitive_Operations (Parent_Type)
10053 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10054 Enclosing_Comp_Unit_Node (Derived_Type)
10055 then
10056 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10057 end if;
10059 -- If the parent type is a private subtype, the convention on the base
10060 -- type may be set in the private part, and not propagated to the
10061 -- subtype until later, so we obtain the convention from the base type.
10063 Set_Convention (Derived_Type, Convention (Parent_Base));
10065 if Is_Tagged_Type (Derived_Type)
10066 and then Present (Class_Wide_Type (Derived_Type))
10067 then
10068 Set_Convention (Class_Wide_Type (Derived_Type),
10069 Convention (Class_Wide_Type (Parent_Base)));
10070 end if;
10072 -- Set SSO default for record or array type
10074 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10075 and then Is_Base_Type (Derived_Type)
10076 then
10077 Set_Default_SSO (Derived_Type);
10078 end if;
10080 -- A derived type inherits the Default_Initial_Condition pragma coming
10081 -- from any parent type within the derivation chain.
10083 if Has_DIC (Parent_Type) then
10084 Set_Has_Inherited_DIC (Derived_Type);
10085 end if;
10087 -- A derived type inherits any class-wide invariants coming from a
10088 -- parent type or an interface. Note that the invariant procedure of
10089 -- the parent type should not be inherited because the derived type may
10090 -- define invariants of its own.
10092 if not Is_Interface (Derived_Type) then
10093 if Has_Inherited_Invariants (Parent_Type)
10094 or else Has_Inheritable_Invariants (Parent_Type)
10095 then
10096 Set_Has_Inherited_Invariants (Derived_Type);
10098 elsif Is_Concurrent_Type (Derived_Type)
10099 or else Is_Tagged_Type (Derived_Type)
10100 then
10101 declare
10102 Iface : Entity_Id;
10103 Ifaces : Elist_Id;
10104 Iface_Elmt : Elmt_Id;
10106 begin
10107 Collect_Interfaces
10108 (T => Derived_Type,
10109 Ifaces_List => Ifaces,
10110 Exclude_Parents => True);
10112 if Present (Ifaces) then
10113 Iface_Elmt := First_Elmt (Ifaces);
10114 while Present (Iface_Elmt) loop
10115 Iface := Node (Iface_Elmt);
10117 if Has_Inheritable_Invariants (Iface) then
10118 Set_Has_Inherited_Invariants (Derived_Type);
10119 exit;
10120 end if;
10122 Next_Elmt (Iface_Elmt);
10123 end loop;
10124 end if;
10125 end;
10126 end if;
10127 end if;
10129 -- We similarly inherit predicates. Note that for scalar derived types
10130 -- the predicate is inherited from the first subtype, and not from its
10131 -- (anonymous) base type.
10133 if Has_Predicates (Parent_Type)
10134 or else Has_Predicates (First_Subtype (Parent_Type))
10135 then
10136 Set_Has_Predicates (Derived_Type);
10137 end if;
10139 -- The derived type inherits representation clauses from the parent
10140 -- type, and from any interfaces.
10142 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10144 declare
10145 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10146 begin
10147 while Present (Iface) loop
10148 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10149 Next (Iface);
10150 end loop;
10151 end;
10153 -- If the parent type has delayed rep aspects, then mark the derived
10154 -- type as possibly inheriting a delayed rep aspect.
10156 if Has_Delayed_Rep_Aspects (Parent_Type) then
10157 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10158 end if;
10160 -- A derived type becomes Ghost when its parent type is also Ghost
10161 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10162 -- directly inherited because the Ghost policy in effect may differ.
10164 if Is_Ghost_Entity (Parent_Type) then
10165 Set_Is_Ghost_Entity (Derived_Type);
10166 end if;
10168 -- Type dependent processing
10170 case Ekind (Parent_Type) is
10171 when Numeric_Kind =>
10172 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10174 when Array_Kind =>
10175 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10177 when Class_Wide_Kind
10178 | E_Record_Subtype
10179 | E_Record_Type
10181 Build_Derived_Record_Type
10182 (N, Parent_Type, Derived_Type, Derive_Subps);
10183 return;
10185 when Enumeration_Kind =>
10186 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10188 when Access_Kind =>
10189 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10191 when Incomplete_Or_Private_Kind =>
10192 Build_Derived_Private_Type
10193 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10195 -- For discriminated types, the derivation includes deriving
10196 -- primitive operations. For others it is done below.
10198 if Is_Tagged_Type (Parent_Type)
10199 or else Has_Discriminants (Parent_Type)
10200 or else (Present (Full_View (Parent_Type))
10201 and then Has_Discriminants (Full_View (Parent_Type)))
10202 then
10203 return;
10204 end if;
10206 when Concurrent_Kind =>
10207 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10209 when others =>
10210 raise Program_Error;
10211 end case;
10213 -- Nothing more to do if some error occurred
10215 if Etype (Derived_Type) = Any_Type then
10216 return;
10217 end if;
10219 -- If not already set, initialize the derived type's list of primitive
10220 -- operations to an empty element list.
10222 if not Present (Direct_Primitive_Operations (Derived_Type)) then
10223 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10225 -- If Etype of the derived type is the base type (as opposed to
10226 -- a parent type) and doesn't have an associated list of primitive
10227 -- operations, then set the base type's primitive list to the
10228 -- derived type's list. The lists need to be shared in common
10229 -- between the two.
10231 if Etype (Derived_Type) = Base_Type (Derived_Type)
10232 and then
10233 not Present (Direct_Primitive_Operations (Etype (Derived_Type)))
10234 then
10235 Set_Direct_Primitive_Operations
10236 (Etype (Derived_Type),
10237 Direct_Primitive_Operations (Derived_Type));
10238 end if;
10239 end if;
10241 -- Set delayed freeze and then derive subprograms, we need to do this
10242 -- in this order so that derived subprograms inherit the derived freeze
10243 -- if necessary.
10245 Set_Has_Delayed_Freeze (Derived_Type);
10247 if Derive_Subps then
10248 Derive_Subprograms (Parent_Type, Derived_Type);
10249 end if;
10251 Set_Has_Primitive_Operations
10252 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10253 end Build_Derived_Type;
10255 -----------------------
10256 -- Build_Discriminal --
10257 -----------------------
10259 procedure Build_Discriminal (Discrim : Entity_Id) is
10260 D_Minal : Entity_Id;
10261 CR_Disc : Entity_Id;
10263 begin
10264 -- A discriminal has the same name as the discriminant
10266 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10268 Mutate_Ekind (D_Minal, E_In_Parameter);
10269 Set_Mechanism (D_Minal, Default_Mechanism);
10270 Set_Etype (D_Minal, Etype (Discrim));
10271 Set_Scope (D_Minal, Current_Scope);
10272 Set_Parent (D_Minal, Parent (Discrim));
10274 Set_Discriminal (Discrim, D_Minal);
10275 Set_Discriminal_Link (D_Minal, Discrim);
10277 -- For task types, build at once the discriminants of the corresponding
10278 -- record, which are needed if discriminants are used in entry defaults
10279 -- and in family bounds.
10281 if Is_Concurrent_Type (Current_Scope)
10282 or else
10283 Is_Limited_Type (Current_Scope)
10284 then
10285 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10287 Mutate_Ekind (CR_Disc, E_In_Parameter);
10288 Set_Mechanism (CR_Disc, Default_Mechanism);
10289 Set_Etype (CR_Disc, Etype (Discrim));
10290 Set_Scope (CR_Disc, Current_Scope);
10291 Set_Discriminal_Link (CR_Disc, Discrim);
10292 Set_CR_Discriminant (Discrim, CR_Disc);
10293 end if;
10294 end Build_Discriminal;
10296 ------------------------------------
10297 -- Build_Discriminant_Constraints --
10298 ------------------------------------
10300 function Build_Discriminant_Constraints
10301 (T : Entity_Id;
10302 Def : Node_Id;
10303 Derived_Def : Boolean := False) return Elist_Id
10305 C : constant Node_Id := Constraint (Def);
10306 Nb_Discr : constant Nat := Number_Discriminants (T);
10308 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10309 -- Saves the expression corresponding to a given discriminant in T
10311 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10312 -- Return the Position number within array Discr_Expr of a discriminant
10313 -- D within the discriminant list of the discriminated type T.
10315 procedure Process_Discriminant_Expression
10316 (Expr : Node_Id;
10317 D : Entity_Id);
10318 -- If this is a discriminant constraint on a partial view, do not
10319 -- generate an overflow check on the discriminant expression. The check
10320 -- will be generated when constraining the full view. Otherwise the
10321 -- backend creates duplicate symbols for the temporaries corresponding
10322 -- to the expressions to be checked, causing spurious assembler errors.
10324 ------------------
10325 -- Pos_Of_Discr --
10326 ------------------
10328 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10329 Disc : Entity_Id;
10331 begin
10332 Disc := First_Discriminant (T);
10333 for J in Discr_Expr'Range loop
10334 if Disc = D then
10335 return J;
10336 end if;
10338 Next_Discriminant (Disc);
10339 end loop;
10341 -- Note: Since this function is called on discriminants that are
10342 -- known to belong to the discriminated type, falling through the
10343 -- loop with no match signals an internal compiler error.
10345 raise Program_Error;
10346 end Pos_Of_Discr;
10348 -------------------------------------
10349 -- Process_Discriminant_Expression --
10350 -------------------------------------
10352 procedure Process_Discriminant_Expression
10353 (Expr : Node_Id;
10354 D : Entity_Id)
10356 BDT : constant Entity_Id := Base_Type (Etype (D));
10358 begin
10359 -- If this is a discriminant constraint on a partial view, do
10360 -- not generate an overflow on the discriminant expression. The
10361 -- check will be generated when constraining the full view.
10363 if Is_Private_Type (T)
10364 and then Present (Full_View (T))
10365 then
10366 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10367 else
10368 Analyze_And_Resolve (Expr, BDT);
10369 end if;
10370 end Process_Discriminant_Expression;
10372 -- Declarations local to Build_Discriminant_Constraints
10374 Discr : Entity_Id;
10375 E : Entity_Id;
10376 Elist : constant Elist_Id := New_Elmt_List;
10378 Constr : Node_Id;
10379 Expr : Node_Id;
10380 Id : Node_Id;
10381 Position : Nat;
10382 Found : Boolean;
10384 Discrim_Present : Boolean := False;
10386 -- Start of processing for Build_Discriminant_Constraints
10388 begin
10389 -- The following loop will process positional associations only.
10390 -- For a positional association, the (single) discriminant is
10391 -- implicitly specified by position, in textual order (RM 3.7.2).
10393 Discr := First_Discriminant (T);
10394 Constr := First (Constraints (C));
10395 for D in Discr_Expr'Range loop
10396 exit when Nkind (Constr) = N_Discriminant_Association;
10398 if No (Constr) then
10399 Error_Msg_N ("too few discriminants given in constraint", C);
10400 return New_Elmt_List;
10402 elsif Nkind (Constr) = N_Range
10403 or else (Nkind (Constr) = N_Attribute_Reference
10404 and then Attribute_Name (Constr) = Name_Range)
10405 then
10406 Error_Msg_N
10407 ("a range is not a valid discriminant constraint", Constr);
10408 Discr_Expr (D) := Error;
10410 elsif Nkind (Constr) = N_Subtype_Indication then
10411 Error_Msg_N
10412 ("a subtype indication is not a valid discriminant constraint",
10413 Constr);
10414 Discr_Expr (D) := Error;
10416 else
10417 Process_Discriminant_Expression (Constr, Discr);
10418 Discr_Expr (D) := Constr;
10419 end if;
10421 Next_Discriminant (Discr);
10422 Next (Constr);
10423 end loop;
10425 if No (Discr) and then Present (Constr) then
10426 Error_Msg_N ("too many discriminants given in constraint", Constr);
10427 return New_Elmt_List;
10428 end if;
10430 -- Named associations can be given in any order, but if both positional
10431 -- and named associations are used in the same discriminant constraint,
10432 -- then positional associations must occur first, at their normal
10433 -- position. Hence once a named association is used, the rest of the
10434 -- discriminant constraint must use only named associations.
10436 while Present (Constr) loop
10438 -- Positional association forbidden after a named association
10440 if Nkind (Constr) /= N_Discriminant_Association then
10441 Error_Msg_N ("positional association follows named one", Constr);
10442 return New_Elmt_List;
10444 -- Otherwise it is a named association
10446 else
10447 -- E records the type of the discriminants in the named
10448 -- association. All the discriminants specified in the same name
10449 -- association must have the same type.
10451 E := Empty;
10453 -- Search the list of discriminants in T to see if the simple name
10454 -- given in the constraint matches any of them.
10456 Id := First (Selector_Names (Constr));
10457 while Present (Id) loop
10458 Found := False;
10460 -- If Original_Discriminant is present, we are processing a
10461 -- generic instantiation and this is an instance node. We need
10462 -- to find the name of the corresponding discriminant in the
10463 -- actual record type T and not the name of the discriminant in
10464 -- the generic formal. Example:
10466 -- generic
10467 -- type G (D : int) is private;
10468 -- package P is
10469 -- subtype W is G (D => 1);
10470 -- end package;
10471 -- type Rec (X : int) is record ... end record;
10472 -- package Q is new P (G => Rec);
10474 -- At the point of the instantiation, formal type G is Rec
10475 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10476 -- which really looks like "subtype W is Rec (D => 1);" at
10477 -- the point of instantiation, we want to find the discriminant
10478 -- that corresponds to D in Rec, i.e. X.
10480 if Present (Original_Discriminant (Id))
10481 and then In_Instance
10482 then
10483 Discr := Find_Corresponding_Discriminant (Id, T);
10484 Found := True;
10486 else
10487 Discr := First_Discriminant (T);
10488 while Present (Discr) loop
10489 if Chars (Discr) = Chars (Id) then
10490 Found := True;
10491 exit;
10492 end if;
10494 Next_Discriminant (Discr);
10495 end loop;
10497 if not Found then
10498 Error_Msg_N ("& does not match any discriminant", Id);
10499 return New_Elmt_List;
10501 -- If the parent type is a generic formal, preserve the
10502 -- name of the discriminant for subsequent instances.
10503 -- see comment at the beginning of this if statement.
10505 elsif Is_Generic_Type (Root_Type (T)) then
10506 Set_Original_Discriminant (Id, Discr);
10507 end if;
10508 end if;
10510 Position := Pos_Of_Discr (T, Discr);
10512 if Present (Discr_Expr (Position)) then
10513 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10515 else
10516 -- Each discriminant specified in the same named association
10517 -- must be associated with a separate copy of the
10518 -- corresponding expression.
10520 if Present (Next (Id)) then
10521 Expr := New_Copy_Tree (Expression (Constr));
10522 Set_Parent (Expr, Parent (Expression (Constr)));
10523 else
10524 Expr := Expression (Constr);
10525 end if;
10527 Discr_Expr (Position) := Expr;
10528 Process_Discriminant_Expression (Expr, Discr);
10529 end if;
10531 -- A discriminant association with more than one discriminant
10532 -- name is only allowed if the named discriminants are all of
10533 -- the same type (RM 3.7.1(8)).
10535 if E = Empty then
10536 E := Base_Type (Etype (Discr));
10538 elsif Base_Type (Etype (Discr)) /= E then
10539 Error_Msg_N
10540 ("all discriminants in an association " &
10541 "must have the same type", Id);
10542 end if;
10544 Next (Id);
10545 end loop;
10546 end if;
10548 Next (Constr);
10549 end loop;
10551 -- A discriminant constraint must provide exactly one value for each
10552 -- discriminant of the type (RM 3.7.1(8)).
10554 for J in Discr_Expr'Range loop
10555 if No (Discr_Expr (J)) then
10556 Error_Msg_N ("too few discriminants given in constraint", C);
10557 return New_Elmt_List;
10558 end if;
10559 end loop;
10561 -- Determine if there are discriminant expressions in the constraint
10563 for J in Discr_Expr'Range loop
10564 if Denotes_Discriminant
10565 (Discr_Expr (J), Check_Concurrent => True)
10566 then
10567 Discrim_Present := True;
10568 exit;
10569 end if;
10570 end loop;
10572 -- Build an element list consisting of the expressions given in the
10573 -- discriminant constraint and apply the appropriate checks. The list
10574 -- is constructed after resolving any named discriminant associations
10575 -- and therefore the expressions appear in the textual order of the
10576 -- discriminants.
10578 Discr := First_Discriminant (T);
10579 for J in Discr_Expr'Range loop
10580 if Discr_Expr (J) /= Error then
10581 Append_Elmt (Discr_Expr (J), Elist);
10583 -- If any of the discriminant constraints is given by a
10584 -- discriminant and we are in a derived type declaration we
10585 -- have a discriminant renaming. Establish link between new
10586 -- and old discriminant. The new discriminant has an implicit
10587 -- dereference if the old one does.
10589 if Denotes_Discriminant (Discr_Expr (J)) then
10590 if Derived_Def then
10591 declare
10592 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10594 begin
10595 Set_Corresponding_Discriminant (New_Discr, Discr);
10596 Set_Has_Implicit_Dereference (New_Discr,
10597 Has_Implicit_Dereference (Discr));
10598 end;
10599 end if;
10601 -- Force the evaluation of non-discriminant expressions.
10602 -- If we have found a discriminant in the constraint 3.4(26)
10603 -- and 3.8(18) demand that no range checks are performed are
10604 -- after evaluation. If the constraint is for a component
10605 -- definition that has a per-object constraint, expressions are
10606 -- evaluated but not checked either. In all other cases perform
10607 -- a range check.
10609 else
10610 if Discrim_Present then
10611 null;
10613 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10614 and then Has_Per_Object_Constraint
10615 (Defining_Identifier (Parent (Parent (Def))))
10616 then
10617 null;
10619 elsif Is_Access_Type (Etype (Discr)) then
10620 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10622 else
10623 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10624 end if;
10626 -- If the value of the discriminant may be visible in
10627 -- another unit or child unit, create an external name
10628 -- for it. We use the name of the object or component
10629 -- that carries the discriminated subtype. The code
10630 -- below may generate external symbols for the discriminant
10631 -- expression when not strictly needed, which is harmless.
10633 if Expander_Active
10634 and then Comes_From_Source (Def)
10635 and then not Is_Subprogram (Current_Scope)
10636 then
10637 declare
10638 Id : Entity_Id := Empty;
10639 begin
10640 if Nkind (Parent (Def)) = N_Object_Declaration then
10641 Id := Defining_Identifier (Parent (Def));
10643 elsif Nkind (Parent (Def)) = N_Component_Definition
10644 and then
10645 Nkind (Parent (Parent (Def)))
10646 = N_Component_Declaration
10647 then
10648 Id := Defining_Identifier (Parent (Parent (Def)));
10649 end if;
10651 if Present (Id) then
10652 Force_Evaluation (
10653 Discr_Expr (J),
10654 Related_Id => Id,
10655 Discr_Number => J);
10656 else
10657 Force_Evaluation (Discr_Expr (J));
10658 end if;
10659 end;
10660 else
10661 Force_Evaluation (Discr_Expr (J));
10662 end if;
10663 end if;
10665 -- Check that the designated type of an access discriminant's
10666 -- expression is not a class-wide type unless the discriminant's
10667 -- designated type is also class-wide.
10669 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10670 and then not Is_Class_Wide_Type
10671 (Designated_Type (Etype (Discr)))
10672 and then Etype (Discr_Expr (J)) /= Any_Type
10673 and then Is_Class_Wide_Type
10674 (Designated_Type (Etype (Discr_Expr (J))))
10675 then
10676 Wrong_Type (Discr_Expr (J), Etype (Discr));
10678 elsif Is_Access_Type (Etype (Discr))
10679 and then not Is_Access_Constant (Etype (Discr))
10680 and then Is_Access_Type (Etype (Discr_Expr (J)))
10681 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10682 then
10683 Error_Msg_NE
10684 ("constraint for discriminant& must be access to variable",
10685 Def, Discr);
10686 end if;
10687 end if;
10689 Next_Discriminant (Discr);
10690 end loop;
10692 return Elist;
10693 end Build_Discriminant_Constraints;
10695 ---------------------------------
10696 -- Build_Discriminated_Subtype --
10697 ---------------------------------
10699 procedure Build_Discriminated_Subtype
10700 (T : Entity_Id;
10701 Def_Id : Entity_Id;
10702 Elist : Elist_Id;
10703 Related_Nod : Node_Id;
10704 For_Access : Boolean := False)
10706 Has_Discrs : constant Boolean := Has_Discriminants (T);
10707 Constrained : constant Boolean :=
10708 (Has_Discrs
10709 and then not Is_Empty_Elmt_List (Elist)
10710 and then not Is_Class_Wide_Type (T))
10711 or else Is_Constrained (T);
10713 begin
10714 if Ekind (T) = E_Record_Type then
10715 Mutate_Ekind (Def_Id, E_Record_Subtype);
10717 -- Inherit preelaboration flag from base, for types for which it
10718 -- may have been set: records, private types, protected types.
10720 Set_Known_To_Have_Preelab_Init
10721 (Def_Id, Known_To_Have_Preelab_Init (T));
10723 elsif Ekind (T) = E_Task_Type then
10724 Mutate_Ekind (Def_Id, E_Task_Subtype);
10726 elsif Ekind (T) = E_Protected_Type then
10727 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10728 Set_Known_To_Have_Preelab_Init
10729 (Def_Id, Known_To_Have_Preelab_Init (T));
10731 elsif Is_Private_Type (T) then
10732 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10733 Set_Known_To_Have_Preelab_Init
10734 (Def_Id, Known_To_Have_Preelab_Init (T));
10736 -- Private subtypes may have private dependents
10738 Set_Private_Dependents (Def_Id, New_Elmt_List);
10740 elsif Is_Class_Wide_Type (T) then
10741 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10743 else
10744 -- Incomplete type. Attach subtype to list of dependents, to be
10745 -- completed with full view of parent type, unless is it the
10746 -- designated subtype of a record component within an init_proc.
10747 -- This last case arises for a component of an access type whose
10748 -- designated type is incomplete (e.g. a Taft Amendment type).
10749 -- The designated subtype is within an inner scope, and needs no
10750 -- elaboration, because only the access type is needed in the
10751 -- initialization procedure.
10753 if Ekind (T) = E_Incomplete_Type then
10754 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10755 else
10756 Mutate_Ekind (Def_Id, Ekind (T));
10757 end if;
10759 if For_Access and then Within_Init_Proc then
10760 null;
10761 else
10762 Append_Elmt (Def_Id, Private_Dependents (T));
10763 end if;
10764 end if;
10766 Set_Etype (Def_Id, T);
10767 Reinit_Size_Align (Def_Id);
10768 Set_Has_Discriminants (Def_Id, Has_Discrs);
10769 Set_Is_Constrained (Def_Id, Constrained);
10771 Set_First_Entity (Def_Id, First_Entity (T));
10772 Set_Last_Entity (Def_Id, Last_Entity (T));
10773 Set_Has_Implicit_Dereference
10774 (Def_Id, Has_Implicit_Dereference (T));
10775 Set_Has_Pragma_Unreferenced_Objects
10776 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10778 -- If the subtype is the completion of a private declaration, there may
10779 -- have been representation clauses for the partial view, and they must
10780 -- be preserved. Build_Derived_Type chains the inherited clauses with
10781 -- the ones appearing on the extension. If this comes from a subtype
10782 -- declaration, all clauses are inherited.
10784 if No (First_Rep_Item (Def_Id)) then
10785 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10786 end if;
10788 if Is_Tagged_Type (T) then
10789 Set_Is_Tagged_Type (Def_Id);
10790 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10791 Make_Class_Wide_Type (Def_Id);
10792 end if;
10794 Set_Stored_Constraint (Def_Id, No_Elist);
10796 if Has_Discrs then
10797 Set_Discriminant_Constraint (Def_Id, Elist);
10798 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10799 end if;
10801 if Is_Tagged_Type (T) then
10803 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10804 -- concurrent record type (which has the list of primitive
10805 -- operations).
10807 if Ada_Version >= Ada_2005
10808 and then Is_Concurrent_Type (T)
10809 then
10810 Set_Corresponding_Record_Type (Def_Id,
10811 Corresponding_Record_Type (T));
10812 else
10813 Set_Direct_Primitive_Operations (Def_Id,
10814 Direct_Primitive_Operations (T));
10815 end if;
10817 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10818 end if;
10820 -- Subtypes introduced by component declarations do not need to be
10821 -- marked as delayed, and do not get freeze nodes, because the semantics
10822 -- verifies that the parents of the subtypes are frozen before the
10823 -- enclosing record is frozen.
10825 if not Is_Type (Scope (Def_Id)) then
10826 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10828 if Is_Private_Type (T)
10829 and then Present (Full_View (T))
10830 then
10831 Conditional_Delay (Def_Id, Full_View (T));
10832 else
10833 Conditional_Delay (Def_Id, T);
10834 end if;
10835 end if;
10837 if Is_Record_Type (T) then
10838 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10840 if Has_Discrs
10841 and then not Is_Empty_Elmt_List (Elist)
10842 and then not For_Access
10843 then
10844 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10846 elsif not Is_Private_Type (T) then
10847 Set_Cloned_Subtype (Def_Id, T);
10848 end if;
10849 end if;
10850 end Build_Discriminated_Subtype;
10852 ---------------------------
10853 -- Build_Itype_Reference --
10854 ---------------------------
10856 procedure Build_Itype_Reference
10857 (Ityp : Entity_Id;
10858 Nod : Node_Id)
10860 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10861 begin
10863 -- Itype references are only created for use by the back-end
10865 if Inside_A_Generic then
10866 return;
10867 else
10868 Set_Itype (IR, Ityp);
10870 -- If Nod is a library unit entity, then Insert_After won't work,
10871 -- because Nod is not a member of any list. Therefore, we use
10872 -- Add_Global_Declaration in this case. This can happen if we have a
10873 -- build-in-place library function, child unit or not.
10875 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10876 or else (Nkind (Nod) in
10877 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
10878 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10879 then
10880 Add_Global_Declaration (IR);
10881 else
10882 Insert_After (Nod, IR);
10883 end if;
10884 end if;
10885 end Build_Itype_Reference;
10887 ------------------------
10888 -- Build_Scalar_Bound --
10889 ------------------------
10891 function Build_Scalar_Bound
10892 (Bound : Node_Id;
10893 Par_T : Entity_Id;
10894 Der_T : Entity_Id) return Node_Id
10896 New_Bound : Entity_Id;
10898 begin
10899 -- Note: not clear why this is needed, how can the original bound
10900 -- be unanalyzed at this point? and if it is, what business do we
10901 -- have messing around with it? and why is the base type of the
10902 -- parent type the right type for the resolution. It probably is
10903 -- not. It is OK for the new bound we are creating, but not for
10904 -- the old one??? Still if it never happens, no problem.
10906 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10908 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
10909 New_Bound := New_Copy (Bound);
10910 Set_Etype (New_Bound, Der_T);
10911 Set_Analyzed (New_Bound);
10913 elsif Is_Entity_Name (Bound) then
10914 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10916 -- The following is almost certainly wrong. What business do we have
10917 -- relocating a node (Bound) that is presumably still attached to
10918 -- the tree elsewhere???
10920 else
10921 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10922 end if;
10924 Set_Etype (New_Bound, Der_T);
10925 return New_Bound;
10926 end Build_Scalar_Bound;
10928 -------------------------------
10929 -- Check_Abstract_Overriding --
10930 -------------------------------
10932 procedure Check_Abstract_Overriding (T : Entity_Id) is
10933 Alias_Subp : Entity_Id;
10934 Elmt : Elmt_Id;
10935 Op_List : Elist_Id;
10936 Subp : Entity_Id;
10937 Type_Def : Node_Id;
10939 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10940 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10941 -- which has pragma Implemented already set. Check whether Subp's entity
10942 -- kind conforms to the implementation kind of the overridden routine.
10944 procedure Check_Pragma_Implemented
10945 (Subp : Entity_Id;
10946 Iface_Subp : Entity_Id);
10947 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10948 -- Iface_Subp and both entities have pragma Implemented already set on
10949 -- them. Check whether the two implementation kinds are conforming.
10951 procedure Inherit_Pragma_Implemented
10952 (Subp : Entity_Id;
10953 Iface_Subp : Entity_Id);
10954 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10955 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10956 -- Propagate the implementation kind of Iface_Subp to Subp.
10958 ------------------------------
10959 -- Check_Pragma_Implemented --
10960 ------------------------------
10962 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10963 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10964 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10965 Subp_Alias : constant Entity_Id := Alias (Subp);
10966 Contr_Typ : Entity_Id;
10967 Impl_Subp : Entity_Id;
10969 begin
10970 -- Subp must have an alias since it is a hidden entity used to link
10971 -- an interface subprogram to its overriding counterpart.
10973 pragma Assert (Present (Subp_Alias));
10975 -- Handle aliases to synchronized wrappers
10977 Impl_Subp := Subp_Alias;
10979 if Is_Primitive_Wrapper (Impl_Subp) then
10980 Impl_Subp := Wrapped_Entity (Impl_Subp);
10981 end if;
10983 -- Extract the type of the controlling formal
10985 Contr_Typ := Etype (First_Formal (Subp_Alias));
10987 if Is_Concurrent_Record_Type (Contr_Typ) then
10988 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10989 end if;
10991 -- An interface subprogram whose implementation kind is By_Entry must
10992 -- be implemented by an entry.
10994 if Impl_Kind = Name_By_Entry
10995 and then Ekind (Impl_Subp) /= E_Entry
10996 then
10997 Error_Msg_Node_2 := Iface_Alias;
10998 Error_Msg_NE
10999 ("type & must implement abstract subprogram & with an entry",
11000 Subp_Alias, Contr_Typ);
11002 elsif Impl_Kind = Name_By_Protected_Procedure then
11004 -- An interface subprogram whose implementation kind is By_
11005 -- Protected_Procedure cannot be implemented by a primitive
11006 -- procedure of a task type.
11008 if Ekind (Contr_Typ) /= E_Protected_Type then
11009 Error_Msg_Node_2 := Contr_Typ;
11010 Error_Msg_NE
11011 ("interface subprogram & cannot be implemented by a "
11012 & "primitive procedure of task type &",
11013 Subp_Alias, Iface_Alias);
11015 -- An interface subprogram whose implementation kind is By_
11016 -- Protected_Procedure must be implemented by a procedure.
11018 elsif Ekind (Impl_Subp) /= E_Procedure then
11019 Error_Msg_Node_2 := Iface_Alias;
11020 Error_Msg_NE
11021 ("type & must implement abstract subprogram & with a "
11022 & "procedure", Subp_Alias, Contr_Typ);
11024 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11025 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11026 then
11027 Error_Msg_Name_1 := Impl_Kind;
11028 Error_Msg_N
11029 ("overriding operation& must have synchronization%",
11030 Subp_Alias);
11031 end if;
11033 -- If primitive has Optional synchronization, overriding operation
11034 -- must match if it has an explicit synchronization.
11036 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11037 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11038 then
11039 Error_Msg_Name_1 := Impl_Kind;
11040 Error_Msg_N
11041 ("overriding operation& must have synchronization%", Subp_Alias);
11042 end if;
11043 end Check_Pragma_Implemented;
11045 ------------------------------
11046 -- Check_Pragma_Implemented --
11047 ------------------------------
11049 procedure Check_Pragma_Implemented
11050 (Subp : Entity_Id;
11051 Iface_Subp : Entity_Id)
11053 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11054 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11056 begin
11057 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11058 -- and overriding subprogram are different. In general this is an
11059 -- error except when the implementation kind of the overridden
11060 -- subprograms is By_Any or Optional.
11062 if Iface_Kind /= Subp_Kind
11063 and then Iface_Kind /= Name_By_Any
11064 and then Iface_Kind /= Name_Optional
11065 then
11066 if Iface_Kind = Name_By_Entry then
11067 Error_Msg_N
11068 ("incompatible implementation kind, overridden subprogram " &
11069 "is marked By_Entry", Subp);
11070 else
11071 Error_Msg_N
11072 ("incompatible implementation kind, overridden subprogram " &
11073 "is marked By_Protected_Procedure", Subp);
11074 end if;
11075 end if;
11076 end Check_Pragma_Implemented;
11078 --------------------------------
11079 -- Inherit_Pragma_Implemented --
11080 --------------------------------
11082 procedure Inherit_Pragma_Implemented
11083 (Subp : Entity_Id;
11084 Iface_Subp : Entity_Id)
11086 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11087 Loc : constant Source_Ptr := Sloc (Subp);
11088 Impl_Prag : Node_Id;
11090 begin
11091 -- Since the implementation kind is stored as a representation item
11092 -- rather than a flag, create a pragma node.
11094 Impl_Prag :=
11095 Make_Pragma (Loc,
11096 Chars => Name_Implemented,
11097 Pragma_Argument_Associations => New_List (
11098 Make_Pragma_Argument_Association (Loc,
11099 Expression => New_Occurrence_Of (Subp, Loc)),
11101 Make_Pragma_Argument_Association (Loc,
11102 Expression => Make_Identifier (Loc, Iface_Kind))));
11104 -- The pragma doesn't need to be analyzed because it is internally
11105 -- built. It is safe to directly register it as a rep item since we
11106 -- are only interested in the characters of the implementation kind.
11108 Record_Rep_Item (Subp, Impl_Prag);
11109 end Inherit_Pragma_Implemented;
11111 -- Start of processing for Check_Abstract_Overriding
11113 begin
11114 Op_List := Primitive_Operations (T);
11116 -- Loop to check primitive operations
11118 Elmt := First_Elmt (Op_List);
11119 while Present (Elmt) loop
11120 Subp := Node (Elmt);
11121 Alias_Subp := Alias (Subp);
11123 -- If the parent type is untagged, then no overriding error checks
11124 -- are needed (such as in the case of an implicit full type for
11125 -- a derived type whose parent is an untagged private type with
11126 -- a tagged full type).
11128 if not Is_Tagged_Type (Etype (T)) then
11129 null;
11131 -- Inherited subprograms are identified by the fact that they do not
11132 -- come from source, and the associated source location is the
11133 -- location of the first subtype of the derived type.
11135 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11136 -- subprograms that "require overriding".
11138 -- Special exception, do not complain about failure to override the
11139 -- stream routines _Input and _Output, as well as the primitive
11140 -- operations used in dispatching selects since we always provide
11141 -- automatic overridings for these subprograms.
11143 -- The partial view of T may have been a private extension, for
11144 -- which inherited functions dispatching on result are abstract.
11145 -- If the full view is a null extension, there is no need for
11146 -- overriding in Ada 2005, but wrappers need to be built for them
11147 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11149 elsif Is_Null_Extension (T)
11150 and then Has_Controlling_Result (Subp)
11151 and then Ada_Version >= Ada_2005
11152 and then Present (Alias_Subp)
11153 and then not Comes_From_Source (Subp)
11154 and then not Is_Abstract_Subprogram (Alias_Subp)
11155 and then not Is_Access_Type (Etype (Subp))
11156 then
11157 null;
11159 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11160 -- processing because this check is done with the aliased
11161 -- entity
11163 elsif Present (Interface_Alias (Subp)) then
11164 null;
11166 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11167 -- of a visible private primitive inherited from an ancestor with
11168 -- the aspect Type_Invariant'Class, unless the inherited primitive
11169 -- is abstract.
11171 elsif not Is_Abstract_Subprogram (Subp)
11172 and then not Comes_From_Source (Subp) -- An inherited subprogram
11173 and then Requires_Overriding (Subp)
11174 and then Present (Alias_Subp)
11175 and then Has_Invariants (Etype (T))
11176 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11177 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11178 and then Is_Private_Primitive (Alias_Subp)
11179 then
11180 Error_Msg_NE
11181 ("inherited private primitive & must be overridden", T, Subp);
11182 Error_Msg_N
11183 ("\because ancestor type has 'Type_'Invariant''Class " &
11184 "(RM 7.3.2(6.1))", T);
11186 elsif (Is_Abstract_Subprogram (Subp)
11187 or else Requires_Overriding (Subp)
11188 or else
11189 (Has_Controlling_Result (Subp)
11190 and then Present (Alias_Subp)
11191 and then not Comes_From_Source (Subp)
11192 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11193 and then not Is_TSS (Subp, TSS_Stream_Input)
11194 and then not Is_TSS (Subp, TSS_Stream_Output)
11195 and then not Is_Abstract_Type (T)
11196 and then not Is_Predefined_Interface_Primitive (Subp)
11198 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11199 -- with abstract interface types because the check will be done
11200 -- with the aliased entity (otherwise we generate a duplicated
11201 -- error message).
11203 and then No (Interface_Alias (Subp))
11204 then
11205 if Present (Alias_Subp) then
11207 -- Only perform the check for a derived subprogram when the
11208 -- type has an explicit record extension. This avoids incorrect
11209 -- flagging of abstract subprograms for the case of a type
11210 -- without an extension that is derived from a formal type
11211 -- with a tagged actual (can occur within a private part).
11213 -- Ada 2005 (AI-391): In the case of an inherited function with
11214 -- a controlling result of the type, the rule does not apply if
11215 -- the type is a null extension (unless the parent function
11216 -- itself is abstract, in which case the function must still be
11217 -- be overridden). The expander will generate an overriding
11218 -- wrapper function calling the parent subprogram (see
11219 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11221 Type_Def := Type_Definition (Parent (T));
11223 if Nkind (Type_Def) = N_Derived_Type_Definition
11224 and then Present (Record_Extension_Part (Type_Def))
11225 and then
11226 (Ada_Version < Ada_2005
11227 or else not Is_Null_Extension (T)
11228 or else Ekind (Subp) = E_Procedure
11229 or else not Has_Controlling_Result (Subp)
11230 or else Is_Abstract_Subprogram (Alias_Subp)
11231 or else Requires_Overriding (Subp)
11232 or else Is_Access_Type (Etype (Subp)))
11233 then
11234 -- Avoid reporting error in case of abstract predefined
11235 -- primitive inherited from interface type because the
11236 -- body of internally generated predefined primitives
11237 -- of tagged types are generated later by Freeze_Type
11239 if Is_Interface (Root_Type (T))
11240 and then Is_Abstract_Subprogram (Subp)
11241 and then Is_Predefined_Dispatching_Operation (Subp)
11242 and then not Comes_From_Source (Ultimate_Alias (Subp))
11243 then
11244 null;
11246 -- A null extension is not obliged to override an inherited
11247 -- procedure subject to pragma Extensions_Visible with value
11248 -- False and at least one controlling OUT parameter
11249 -- (SPARK RM 6.1.7(6)).
11251 elsif Is_Null_Extension (T)
11252 and then Is_EVF_Procedure (Subp)
11253 then
11254 null;
11256 -- Subprogram renamings cannot be overridden
11258 elsif Comes_From_Source (Subp)
11259 and then Present (Alias (Subp))
11260 then
11261 null;
11263 -- Skip reporting the error on Ada 2022 only subprograms
11264 -- that require overriding if we are not in Ada 2022 mode.
11266 elsif Ada_Version < Ada_2022
11267 and then Requires_Overriding (Subp)
11268 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11269 then
11270 null;
11272 else
11273 Error_Msg_NE
11274 ("type must be declared abstract or & overridden",
11275 T, Subp);
11277 -- Traverse the whole chain of aliased subprograms to
11278 -- complete the error notification. This is especially
11279 -- useful for traceability of the chain of entities when
11280 -- the subprogram corresponds with an interface
11281 -- subprogram (which may be defined in another package).
11283 if Present (Alias_Subp) then
11284 declare
11285 E : Entity_Id;
11287 begin
11288 E := Subp;
11289 while Present (Alias (E)) loop
11291 -- Avoid reporting redundant errors on entities
11292 -- inherited from interfaces
11294 if Sloc (E) /= Sloc (T) then
11295 Error_Msg_Sloc := Sloc (E);
11296 Error_Msg_NE
11297 ("\& has been inherited #", T, Subp);
11298 end if;
11300 E := Alias (E);
11301 end loop;
11303 Error_Msg_Sloc := Sloc (E);
11305 -- AI05-0068: report if there is an overriding
11306 -- non-abstract subprogram that is invisible.
11308 if Is_Hidden (E)
11309 and then not Is_Abstract_Subprogram (E)
11310 then
11311 Error_Msg_NE
11312 ("\& subprogram# is not visible",
11313 T, Subp);
11315 -- Clarify the case where a non-null extension must
11316 -- override inherited procedure subject to pragma
11317 -- Extensions_Visible with value False and at least
11318 -- one controlling OUT param.
11320 elsif Is_EVF_Procedure (E) then
11321 Error_Msg_NE
11322 ("\& # is subject to Extensions_Visible False",
11323 T, Subp);
11325 else
11326 Error_Msg_NE
11327 ("\& has been inherited from subprogram #",
11328 T, Subp);
11329 end if;
11330 end;
11331 end if;
11332 end if;
11334 -- Ada 2005 (AI-345): Protected or task type implementing
11335 -- abstract interfaces.
11337 elsif Is_Concurrent_Record_Type (T)
11338 and then Present (Interfaces (T))
11339 then
11340 -- There is no need to check here RM 9.4(11.9/3) since we
11341 -- are processing the corresponding record type and the
11342 -- mode of the overriding subprograms was verified by
11343 -- Check_Conformance when the corresponding concurrent
11344 -- type declaration was analyzed.
11346 Error_Msg_NE
11347 ("interface subprogram & must be overridden", T, Subp);
11349 -- Examine primitive operations of synchronized type to find
11350 -- homonyms that have the wrong profile.
11352 declare
11353 Prim : Entity_Id;
11355 begin
11356 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11357 while Present (Prim) loop
11358 if Chars (Prim) = Chars (Subp) then
11359 Error_Msg_NE
11360 ("profile is not type conformant with prefixed "
11361 & "view profile of inherited operation&",
11362 Prim, Subp);
11363 end if;
11365 Next_Entity (Prim);
11366 end loop;
11367 end;
11368 end if;
11370 else
11371 Error_Msg_Node_2 := T;
11372 Error_Msg_N
11373 ("abstract subprogram& not allowed for type&", Subp);
11375 -- Also post unconditional warning on the type (unconditional
11376 -- so that if there are more than one of these cases, we get
11377 -- them all, and not just the first one).
11379 Error_Msg_Node_2 := Subp;
11380 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11381 end if;
11383 -- A subprogram subject to pragma Extensions_Visible with value
11384 -- "True" cannot override a subprogram subject to the same pragma
11385 -- with value "False" (SPARK RM 6.1.7(5)).
11387 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11388 and then Present (Overridden_Operation (Subp))
11389 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11390 Extensions_Visible_False
11391 then
11392 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11393 Error_Msg_N
11394 ("subprogram & with Extensions_Visible True cannot override "
11395 & "subprogram # with Extensions_Visible False", Subp);
11396 end if;
11398 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11400 -- Subp is an expander-generated procedure which maps an interface
11401 -- alias to a protected wrapper. The interface alias is flagged by
11402 -- pragma Implemented. Ensure that Subp is a procedure when the
11403 -- implementation kind is By_Protected_Procedure or an entry when
11404 -- By_Entry.
11406 if Ada_Version >= Ada_2012
11407 and then Is_Hidden (Subp)
11408 and then Present (Interface_Alias (Subp))
11409 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11410 then
11411 Check_Pragma_Implemented (Subp);
11412 end if;
11414 -- Subp is an interface primitive which overrides another interface
11415 -- primitive marked with pragma Implemented.
11417 if Ada_Version >= Ada_2012
11418 and then Present (Overridden_Operation (Subp))
11419 and then Has_Rep_Pragma
11420 (Overridden_Operation (Subp), Name_Implemented)
11421 then
11422 -- If the overriding routine is also marked by Implemented, check
11423 -- that the two implementation kinds are conforming.
11425 if Has_Rep_Pragma (Subp, Name_Implemented) then
11426 Check_Pragma_Implemented
11427 (Subp => Subp,
11428 Iface_Subp => Overridden_Operation (Subp));
11430 -- Otherwise the overriding routine inherits the implementation
11431 -- kind from the overridden subprogram.
11433 else
11434 Inherit_Pragma_Implemented
11435 (Subp => Subp,
11436 Iface_Subp => Overridden_Operation (Subp));
11437 end if;
11438 end if;
11440 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11441 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11442 -- for procedures, since this is our pragma.
11444 if Present (Overridden_Operation (Subp))
11445 and then No_Return (Overridden_Operation (Subp))
11446 then
11448 -- If the subprogram is a renaming, check that the renamed
11449 -- subprogram is No_Return.
11451 if Present (Renamed_Or_Alias (Subp)) then
11452 if not No_Return (Renamed_Or_Alias (Subp)) then
11453 Error_Msg_NE ("subprogram & must be No_Return",
11454 Subp,
11455 Renamed_Or_Alias (Subp));
11456 Error_Msg_N ("\since renaming & overrides No_Return "
11457 & "subprogram (RM 6.5.1(6/2))",
11458 Subp);
11459 end if;
11461 -- Make sure that the subprogram itself is No_Return.
11463 elsif not No_Return (Subp) then
11464 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11465 Error_Msg_N
11466 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11467 Subp);
11468 end if;
11469 end if;
11471 -- If the operation is a wrapper for a synchronized primitive, it
11472 -- may be called indirectly through a dispatching select. We assume
11473 -- that it will be referenced elsewhere indirectly, and suppress
11474 -- warnings about an unused entity.
11476 if Is_Primitive_Wrapper (Subp)
11477 and then Present (Wrapped_Entity (Subp))
11478 then
11479 Set_Referenced (Wrapped_Entity (Subp));
11480 end if;
11482 Next_Elmt (Elmt);
11483 end loop;
11484 end Check_Abstract_Overriding;
11486 ------------------------------------------------
11487 -- Check_Access_Discriminant_Requires_Limited --
11488 ------------------------------------------------
11490 procedure Check_Access_Discriminant_Requires_Limited
11491 (D : Node_Id;
11492 Loc : Node_Id)
11494 begin
11495 -- A discriminant_specification for an access discriminant shall appear
11496 -- only in the declaration for a task or protected type, or for a type
11497 -- with the reserved word 'limited' in its definition or in one of its
11498 -- ancestors (RM 3.7(10)).
11500 -- AI-0063: The proper condition is that type must be immutably limited,
11501 -- or else be a partial view.
11503 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11504 if Is_Limited_View (Current_Scope)
11505 or else
11506 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11507 and then Limited_Present (Parent (Current_Scope)))
11508 then
11509 null;
11511 else
11512 Error_Msg_N
11513 ("access discriminants allowed only for limited types", Loc);
11514 end if;
11515 end if;
11516 end Check_Access_Discriminant_Requires_Limited;
11518 -----------------------------------
11519 -- Check_Aliased_Component_Types --
11520 -----------------------------------
11522 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11523 C : Entity_Id;
11525 begin
11526 -- ??? Also need to check components of record extensions, but not
11527 -- components of protected types (which are always limited).
11529 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11530 -- types to be unconstrained. This is safe because it is illegal to
11531 -- create access subtypes to such types with explicit discriminant
11532 -- constraints.
11534 if not Is_Limited_Type (T) then
11535 if Ekind (T) = E_Record_Type then
11536 C := First_Component (T);
11537 while Present (C) loop
11538 if Is_Aliased (C)
11539 and then Has_Discriminants (Etype (C))
11540 and then not Is_Constrained (Etype (C))
11541 and then not In_Instance_Body
11542 and then Ada_Version < Ada_2005
11543 then
11544 Error_Msg_N
11545 ("aliased component must be constrained (RM 3.6(11))",
11547 end if;
11549 Next_Component (C);
11550 end loop;
11552 elsif Ekind (T) = E_Array_Type then
11553 if Has_Aliased_Components (T)
11554 and then Has_Discriminants (Component_Type (T))
11555 and then not Is_Constrained (Component_Type (T))
11556 and then not In_Instance_Body
11557 and then Ada_Version < Ada_2005
11558 then
11559 Error_Msg_N
11560 ("aliased component type must be constrained (RM 3.6(11))",
11562 end if;
11563 end if;
11564 end if;
11565 end Check_Aliased_Component_Types;
11567 --------------------------------------
11568 -- Check_Anonymous_Access_Component --
11569 --------------------------------------
11571 procedure Check_Anonymous_Access_Component
11572 (Typ_Decl : Node_Id;
11573 Typ : Entity_Id;
11574 Prev : Entity_Id;
11575 Comp_Def : Node_Id;
11576 Access_Def : Node_Id)
11578 Loc : constant Source_Ptr := Sloc (Comp_Def);
11579 Anon_Access : Entity_Id;
11580 Acc_Def : Node_Id;
11581 Decl : Node_Id;
11582 Type_Def : Node_Id;
11584 procedure Build_Incomplete_Type_Declaration;
11585 -- If the record type contains components that include an access to the
11586 -- current record, then create an incomplete type declaration for the
11587 -- record, to be used as the designated type of the anonymous access.
11588 -- This is done only once, and only if there is no previous partial
11589 -- view of the type.
11591 function Designates_T (Subt : Node_Id) return Boolean;
11592 -- Check whether a node designates the enclosing record type, or 'Class
11593 -- of that type
11595 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11596 -- Check whether an access definition includes a reference to
11597 -- the enclosing record type. The reference can be a subtype mark
11598 -- in the access definition itself, a 'Class attribute reference, or
11599 -- recursively a reference appearing in a parameter specification
11600 -- or result definition of an access_to_subprogram definition.
11602 --------------------------------------
11603 -- Build_Incomplete_Type_Declaration --
11604 --------------------------------------
11606 procedure Build_Incomplete_Type_Declaration is
11607 Decl : Node_Id;
11608 Inc_T : Entity_Id;
11609 H : Entity_Id;
11611 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11612 -- it's "is new ... with record" or else "is tagged record ...".
11614 Typ_Def : constant Node_Id :=
11615 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11616 then Type_Definition (Typ_Decl) else Empty);
11617 Is_Tagged : constant Boolean :=
11618 Present (Typ_Def)
11619 and then
11620 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11621 and then
11622 Present (Record_Extension_Part (Typ_Def)))
11623 or else
11624 (Nkind (Typ_Def) = N_Record_Definition
11625 and then Tagged_Present (Typ_Def)));
11627 begin
11628 -- If there is a previous partial view, no need to create a new one
11629 -- If the partial view, given by Prev, is incomplete, If Prev is
11630 -- a private declaration, full declaration is flagged accordingly.
11632 if Prev /= Typ then
11633 if Is_Tagged then
11634 Make_Class_Wide_Type (Prev);
11635 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11636 Set_Etype (Class_Wide_Type (Typ), Typ);
11637 end if;
11639 return;
11641 elsif Has_Private_Declaration (Typ) then
11643 -- If we refer to T'Class inside T, and T is the completion of a
11644 -- private type, then make sure the class-wide type exists.
11646 if Is_Tagged then
11647 Make_Class_Wide_Type (Typ);
11648 end if;
11650 return;
11652 -- If there was a previous anonymous access type, the incomplete
11653 -- type declaration will have been created already.
11655 elsif Present (Current_Entity (Typ))
11656 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11657 and then Full_View (Current_Entity (Typ)) = Typ
11658 then
11659 if Is_Tagged
11660 and then Comes_From_Source (Current_Entity (Typ))
11661 and then not Is_Tagged_Type (Current_Entity (Typ))
11662 then
11663 Make_Class_Wide_Type (Typ);
11664 Error_Msg_N
11665 ("incomplete view of tagged type should be declared tagged??",
11666 Parent (Current_Entity (Typ)));
11667 end if;
11668 return;
11670 else
11671 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11672 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11674 -- Type has already been inserted into the current scope. Remove
11675 -- it, and add incomplete declaration for type, so that subsequent
11676 -- anonymous access types can use it. The entity is unchained from
11677 -- the homonym list and from immediate visibility. After analysis,
11678 -- the entity in the incomplete declaration becomes immediately
11679 -- visible in the record declaration that follows.
11681 H := Current_Entity (Typ);
11683 if H = Typ then
11684 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11686 else
11687 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11688 H := Homonym (Typ);
11689 end loop;
11691 Set_Homonym (H, Homonym (Typ));
11692 end if;
11694 Insert_Before (Typ_Decl, Decl);
11695 Analyze (Decl);
11696 Set_Full_View (Inc_T, Typ);
11697 Set_Incomplete_View (Typ_Decl, Inc_T);
11699 -- If the type is tagged, create a common class-wide type for
11700 -- both views, and set the Etype of the class-wide type to the
11701 -- full view.
11703 if Is_Tagged then
11704 Make_Class_Wide_Type (Inc_T);
11705 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11706 Set_Etype (Class_Wide_Type (Typ), Typ);
11707 end if;
11709 -- If the scope is a package with a limited view, create a shadow
11710 -- entity for the incomplete type like Build_Limited_Views, so as
11711 -- to make it possible for Remove_Limited_With_Unit to reinstall
11712 -- this incomplete type as the visible entity.
11714 if Ekind (Scope (Inc_T)) = E_Package
11715 and then Present (Limited_View (Scope (Inc_T)))
11716 then
11717 declare
11718 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11720 begin
11721 -- This is modeled on Build_Shadow_Entity
11723 Set_Chars (Shadow, Chars (Inc_T));
11724 Set_Parent (Shadow, Decl);
11725 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
11726 Set_Is_Internal (Shadow);
11727 Set_From_Limited_With (Shadow);
11728 Set_Non_Limited_View (Shadow, Inc_T);
11729 Set_Private_Dependents (Shadow, New_Elmt_List);
11731 if Is_Tagged then
11732 Set_Non_Limited_View
11733 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
11734 end if;
11736 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
11737 end;
11738 end if;
11739 end if;
11740 end Build_Incomplete_Type_Declaration;
11742 ------------------
11743 -- Designates_T --
11744 ------------------
11746 function Designates_T (Subt : Node_Id) return Boolean is
11747 Type_Id : constant Name_Id := Chars (Typ);
11749 function Names_T (Nam : Node_Id) return Boolean;
11750 -- The record type has not been introduced in the current scope
11751 -- yet, so we must examine the name of the type itself, either
11752 -- an identifier T, or an expanded name of the form P.T, where
11753 -- P denotes the current scope.
11755 -------------
11756 -- Names_T --
11757 -------------
11759 function Names_T (Nam : Node_Id) return Boolean is
11760 begin
11761 if Nkind (Nam) = N_Identifier then
11762 return Chars (Nam) = Type_Id;
11764 elsif Nkind (Nam) = N_Selected_Component then
11765 if Chars (Selector_Name (Nam)) = Type_Id then
11766 if Nkind (Prefix (Nam)) = N_Identifier then
11767 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11769 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11770 return Chars (Selector_Name (Prefix (Nam))) =
11771 Chars (Current_Scope);
11772 else
11773 return False;
11774 end if;
11776 else
11777 return False;
11778 end if;
11780 else
11781 return False;
11782 end if;
11783 end Names_T;
11785 -- Start of processing for Designates_T
11787 begin
11788 if Nkind (Subt) = N_Identifier then
11789 return Chars (Subt) = Type_Id;
11791 -- Reference can be through an expanded name which has not been
11792 -- analyzed yet, and which designates enclosing scopes.
11794 elsif Nkind (Subt) = N_Selected_Component then
11795 if Names_T (Subt) then
11796 return True;
11798 -- Otherwise it must denote an entity that is already visible.
11799 -- The access definition may name a subtype of the enclosing
11800 -- type, if there is a previous incomplete declaration for it.
11802 else
11803 Find_Selected_Component (Subt);
11804 return
11805 Is_Entity_Name (Subt)
11806 and then Scope (Entity (Subt)) = Current_Scope
11807 and then
11808 (Chars (Base_Type (Entity (Subt))) = Type_Id
11809 or else
11810 (Is_Class_Wide_Type (Entity (Subt))
11811 and then
11812 Chars (Etype (Base_Type (Entity (Subt)))) =
11813 Type_Id));
11814 end if;
11816 -- A reference to the current type may appear as the prefix of
11817 -- a 'Class attribute.
11819 elsif Nkind (Subt) = N_Attribute_Reference
11820 and then Attribute_Name (Subt) = Name_Class
11821 then
11822 return Names_T (Prefix (Subt));
11824 else
11825 return False;
11826 end if;
11827 end Designates_T;
11829 ----------------
11830 -- Mentions_T --
11831 ----------------
11833 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11834 Param_Spec : Node_Id;
11836 Acc_Subprg : constant Node_Id :=
11837 Access_To_Subprogram_Definition (Acc_Def);
11839 begin
11840 if No (Acc_Subprg) then
11841 return Designates_T (Subtype_Mark (Acc_Def));
11842 end if;
11844 -- Component is an access_to_subprogram: examine its formals,
11845 -- and result definition in the case of an access_to_function.
11847 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11848 while Present (Param_Spec) loop
11849 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11850 and then Mentions_T (Parameter_Type (Param_Spec))
11851 then
11852 return True;
11854 elsif Designates_T (Parameter_Type (Param_Spec)) then
11855 return True;
11856 end if;
11858 Next (Param_Spec);
11859 end loop;
11861 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11862 if Nkind (Result_Definition (Acc_Subprg)) =
11863 N_Access_Definition
11864 then
11865 return Mentions_T (Result_Definition (Acc_Subprg));
11866 else
11867 return Designates_T (Result_Definition (Acc_Subprg));
11868 end if;
11869 end if;
11871 return False;
11872 end Mentions_T;
11874 -- Start of processing for Check_Anonymous_Access_Component
11876 begin
11877 if Present (Access_Def) and then Mentions_T (Access_Def) then
11878 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
11880 Build_Incomplete_Type_Declaration;
11881 Anon_Access := Make_Temporary (Loc, 'S');
11883 -- Create a declaration for the anonymous access type: either
11884 -- an access_to_object or an access_to_subprogram.
11886 if Present (Acc_Def) then
11887 if Nkind (Acc_Def) = N_Access_Function_Definition then
11888 Type_Def :=
11889 Make_Access_Function_Definition (Loc,
11890 Parameter_Specifications =>
11891 Parameter_Specifications (Acc_Def),
11892 Result_Definition => Result_Definition (Acc_Def));
11893 else
11894 Type_Def :=
11895 Make_Access_Procedure_Definition (Loc,
11896 Parameter_Specifications =>
11897 Parameter_Specifications (Acc_Def));
11898 end if;
11900 else
11901 Type_Def :=
11902 Make_Access_To_Object_Definition (Loc,
11903 Subtype_Indication =>
11904 Relocate_Node (Subtype_Mark (Access_Def)));
11906 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
11907 Set_All_Present (Type_Def, All_Present (Access_Def));
11908 end if;
11910 Set_Null_Exclusion_Present
11911 (Type_Def, Null_Exclusion_Present (Access_Def));
11913 Decl :=
11914 Make_Full_Type_Declaration (Loc,
11915 Defining_Identifier => Anon_Access,
11916 Type_Definition => Type_Def);
11918 Insert_Before (Typ_Decl, Decl);
11919 Analyze (Decl);
11921 -- At first sight we could add here the extra formals of an access to
11922 -- subprogram; however, it must delayed till the freeze point so that
11923 -- we know the convention.
11925 if Nkind (Comp_Def) = N_Component_Definition then
11926 Rewrite (Comp_Def,
11927 Make_Component_Definition (Loc,
11928 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
11929 else
11930 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
11931 Rewrite (Comp_Def,
11932 Make_Discriminant_Specification (Loc,
11933 Defining_Identifier => Defining_Identifier (Comp_Def),
11934 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
11935 end if;
11937 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11938 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11939 else
11940 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
11941 end if;
11943 Set_Is_Local_Anonymous_Access (Anon_Access);
11944 end if;
11945 end Check_Anonymous_Access_Component;
11947 ---------------------------------------
11948 -- Check_Anonymous_Access_Components --
11949 ---------------------------------------
11951 procedure Check_Anonymous_Access_Components
11952 (Typ_Decl : Node_Id;
11953 Typ : Entity_Id;
11954 Prev : Entity_Id;
11955 Comp_List : Node_Id)
11957 Comp : Node_Id;
11958 begin
11959 if No (Comp_List) then
11960 return;
11961 end if;
11963 Comp := First (Component_Items (Comp_List));
11964 while Present (Comp) loop
11965 if Nkind (Comp) = N_Component_Declaration then
11966 Check_Anonymous_Access_Component
11967 (Typ_Decl, Typ, Prev,
11968 Component_Definition (Comp),
11969 Access_Definition (Component_Definition (Comp)));
11970 end if;
11972 Next (Comp);
11973 end loop;
11975 if Present (Variant_Part (Comp_List)) then
11976 declare
11977 V : Node_Id;
11978 begin
11979 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11980 while Present (V) loop
11981 Check_Anonymous_Access_Components
11982 (Typ_Decl, Typ, Prev, Component_List (V));
11983 Next_Non_Pragma (V);
11984 end loop;
11985 end;
11986 end if;
11987 end Check_Anonymous_Access_Components;
11989 ----------------------
11990 -- Check_Completion --
11991 ----------------------
11993 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11994 E : Entity_Id;
11996 procedure Post_Error;
11997 -- Post error message for lack of completion for entity E
11999 ----------------
12000 -- Post_Error --
12001 ----------------
12003 procedure Post_Error is
12004 procedure Missing_Body;
12005 -- Output missing body message
12007 ------------------
12008 -- Missing_Body --
12009 ------------------
12011 procedure Missing_Body is
12012 begin
12013 -- Spec is in same unit, so we can post on spec
12015 if In_Same_Source_Unit (Body_Id, E) then
12016 Error_Msg_N ("missing body for &", E);
12018 -- Spec is in a separate unit, so we have to post on the body
12020 else
12021 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12022 end if;
12023 end Missing_Body;
12025 -- Start of processing for Post_Error
12027 begin
12028 if not Comes_From_Source (E) then
12029 if Ekind (E) in E_Task_Type | E_Protected_Type then
12031 -- It may be an anonymous protected type created for a
12032 -- single variable. Post error on variable, if present.
12034 declare
12035 Var : Entity_Id;
12037 begin
12038 Var := First_Entity (Current_Scope);
12039 while Present (Var) loop
12040 exit when Etype (Var) = E
12041 and then Comes_From_Source (Var);
12043 Next_Entity (Var);
12044 end loop;
12046 if Present (Var) then
12047 E := Var;
12048 end if;
12049 end;
12050 end if;
12051 end if;
12053 -- If a generated entity has no completion, then either previous
12054 -- semantic errors have disabled the expansion phase, or else we had
12055 -- missing subunits, or else we are compiling without expansion,
12056 -- or else something is very wrong.
12058 if not Comes_From_Source (E) then
12059 pragma Assert
12060 (Serious_Errors_Detected > 0
12061 or else Configurable_Run_Time_Violations > 0
12062 or else Subunits_Missing
12063 or else not Expander_Active);
12064 return;
12066 -- Here for source entity
12068 else
12069 -- Here if no body to post the error message, so we post the error
12070 -- on the declaration that has no completion. This is not really
12071 -- the right place to post it, think about this later ???
12073 if No (Body_Id) then
12074 if Is_Type (E) then
12075 Error_Msg_NE
12076 ("missing full declaration for }", Parent (E), E);
12077 else
12078 Error_Msg_NE ("missing body for &", Parent (E), E);
12079 end if;
12081 -- Package body has no completion for a declaration that appears
12082 -- in the corresponding spec. Post error on the body, with a
12083 -- reference to the non-completed declaration.
12085 else
12086 Error_Msg_Sloc := Sloc (E);
12088 if Is_Type (E) then
12089 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12091 elsif Is_Overloadable (E)
12092 and then Current_Entity_In_Scope (E) /= E
12093 then
12094 -- It may be that the completion is mistyped and appears as
12095 -- a distinct overloading of the entity.
12097 declare
12098 Candidate : constant Entity_Id :=
12099 Current_Entity_In_Scope (E);
12100 Decl : constant Node_Id :=
12101 Unit_Declaration_Node (Candidate);
12103 begin
12104 if Is_Overloadable (Candidate)
12105 and then Ekind (Candidate) = Ekind (E)
12106 and then Nkind (Decl) = N_Subprogram_Body
12107 and then Acts_As_Spec (Decl)
12108 then
12109 Check_Type_Conformant (Candidate, E);
12111 else
12112 Missing_Body;
12113 end if;
12114 end;
12116 else
12117 Missing_Body;
12118 end if;
12119 end if;
12120 end if;
12121 end Post_Error;
12123 -- Local variables
12125 Pack_Id : constant Entity_Id := Current_Scope;
12127 -- Start of processing for Check_Completion
12129 begin
12130 E := First_Entity (Pack_Id);
12131 while Present (E) loop
12132 if Is_Intrinsic_Subprogram (E) then
12133 null;
12135 -- The following situation requires special handling: a child unit
12136 -- that appears in the context clause of the body of its parent:
12138 -- procedure Parent.Child (...);
12140 -- with Parent.Child;
12141 -- package body Parent is
12143 -- Here Parent.Child appears as a local entity, but should not be
12144 -- flagged as requiring completion, because it is a compilation
12145 -- unit.
12147 -- Ignore missing completion for a subprogram that does not come from
12148 -- source (including the _Call primitive operation of RAS types,
12149 -- which has to have the flag Comes_From_Source for other purposes):
12150 -- we assume that the expander will provide the missing completion.
12151 -- In case of previous errors, other expansion actions that provide
12152 -- bodies for null procedures with not be invoked, so inhibit message
12153 -- in those cases.
12155 -- Note that E_Operator is not in the list that follows, because
12156 -- this kind is reserved for predefined operators, that are
12157 -- intrinsic and do not need completion.
12159 elsif Ekind (E) in E_Function
12160 | E_Procedure
12161 | E_Generic_Function
12162 | E_Generic_Procedure
12163 then
12164 if Has_Completion (E) then
12165 null;
12167 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12168 null;
12170 elsif Is_Subprogram (E)
12171 and then (not Comes_From_Source (E)
12172 or else Chars (E) = Name_uCall)
12173 then
12174 null;
12176 elsif
12177 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12178 then
12179 null;
12181 elsif Nkind (Parent (E)) = N_Procedure_Specification
12182 and then Null_Present (Parent (E))
12183 and then Serious_Errors_Detected > 0
12184 then
12185 null;
12187 else
12188 Post_Error;
12189 end if;
12191 elsif Is_Entry (E) then
12192 if not Has_Completion (E)
12193 and then Ekind (Scope (E)) = E_Protected_Type
12194 then
12195 Post_Error;
12196 end if;
12198 elsif Is_Package_Or_Generic_Package (E) then
12199 if Unit_Requires_Body (E) then
12200 if not Has_Completion (E)
12201 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12202 N_Compilation_Unit
12203 then
12204 Post_Error;
12205 end if;
12207 elsif not Is_Child_Unit (E) then
12208 May_Need_Implicit_Body (E);
12209 end if;
12211 -- A formal incomplete type (Ada 2012) does not require a completion;
12212 -- other incomplete type declarations do.
12214 elsif Ekind (E) = E_Incomplete_Type then
12215 if No (Underlying_Type (E))
12216 and then not Is_Generic_Type (E)
12217 then
12218 Post_Error;
12219 end if;
12221 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12222 if not Has_Completion (E) then
12223 Post_Error;
12224 end if;
12226 -- A single task declared in the current scope is a constant, verify
12227 -- that the body of its anonymous type is in the same scope. If the
12228 -- task is defined elsewhere, this may be a renaming declaration for
12229 -- which no completion is needed.
12231 elsif Ekind (E) = E_Constant then
12232 if Ekind (Etype (E)) = E_Task_Type
12233 and then not Has_Completion (Etype (E))
12234 and then Scope (Etype (E)) = Current_Scope
12235 then
12236 Post_Error;
12237 end if;
12239 elsif Ekind (E) = E_Record_Type then
12240 if Is_Tagged_Type (E) then
12241 Check_Abstract_Overriding (E);
12242 Check_Conventions (E);
12243 end if;
12245 Check_Aliased_Component_Types (E);
12247 elsif Ekind (E) = E_Array_Type then
12248 Check_Aliased_Component_Types (E);
12250 end if;
12252 Next_Entity (E);
12253 end loop;
12254 end Check_Completion;
12256 -------------------------------------
12257 -- Check_Constraining_Discriminant --
12258 -------------------------------------
12260 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12262 New_Type : constant Entity_Id := Etype (New_Disc);
12263 Old_Type : Entity_Id;
12265 begin
12266 -- If the record type contains an array constrained by the discriminant
12267 -- but with some different bound, the compiler tries to create a smaller
12268 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12269 -- In this case, where the discriminant type is a scalar type, the check
12270 -- must use the original discriminant type in the parent declaration.
12272 if Is_Scalar_Type (New_Type) then
12273 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12274 else
12275 Old_Type := Etype (Old_Disc);
12276 end if;
12278 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12279 Error_Msg_N
12280 ("subtype must be statically compatible with parent discriminant",
12281 New_Disc);
12283 if not Predicates_Compatible (New_Type, Old_Type) then
12284 Error_Msg_N
12285 ("\subtype predicate is not compatible with parent discriminant",
12286 New_Disc);
12287 end if;
12288 end if;
12289 end Check_Constraining_Discriminant;
12291 ------------------------------------
12292 -- Check_CPP_Type_Has_No_Defaults --
12293 ------------------------------------
12295 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12296 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12297 Clist : Node_Id;
12298 Comp : Node_Id;
12300 begin
12301 -- Obtain the component list
12303 if Nkind (Tdef) = N_Record_Definition then
12304 Clist := Component_List (Tdef);
12305 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12306 Clist := Component_List (Record_Extension_Part (Tdef));
12307 end if;
12309 -- Check all components to ensure no default expressions
12311 if Present (Clist) then
12312 Comp := First (Component_Items (Clist));
12313 while Present (Comp) loop
12314 if Present (Expression (Comp)) then
12315 Error_Msg_N
12316 ("component of imported 'C'P'P type cannot have "
12317 & "default expression", Expression (Comp));
12318 end if;
12320 Next (Comp);
12321 end loop;
12322 end if;
12323 end Check_CPP_Type_Has_No_Defaults;
12325 ----------------------------
12326 -- Check_Delta_Expression --
12327 ----------------------------
12329 procedure Check_Delta_Expression (E : Node_Id) is
12330 begin
12331 if not (Is_Real_Type (Etype (E))) then
12332 Wrong_Type (E, Any_Real);
12334 elsif not Is_OK_Static_Expression (E) then
12335 Flag_Non_Static_Expr
12336 ("non-static expression used for delta value!", E);
12338 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12339 Error_Msg_N ("delta expression must be positive", E);
12341 else
12342 return;
12343 end if;
12345 -- If any of above errors occurred, then replace the incorrect
12346 -- expression by the real 0.1, which should prevent further errors.
12348 Rewrite (E,
12349 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12350 Analyze_And_Resolve (E, Standard_Float);
12351 end Check_Delta_Expression;
12353 -----------------------------
12354 -- Check_Digits_Expression --
12355 -----------------------------
12357 procedure Check_Digits_Expression (E : Node_Id) is
12358 begin
12359 if not (Is_Integer_Type (Etype (E))) then
12360 Wrong_Type (E, Any_Integer);
12362 elsif not Is_OK_Static_Expression (E) then
12363 Flag_Non_Static_Expr
12364 ("non-static expression used for digits value!", E);
12366 elsif Expr_Value (E) <= 0 then
12367 Error_Msg_N ("digits value must be greater than zero", E);
12369 else
12370 return;
12371 end if;
12373 -- If any of above errors occurred, then replace the incorrect
12374 -- expression by the integer 1, which should prevent further errors.
12376 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12377 Analyze_And_Resolve (E, Standard_Integer);
12379 end Check_Digits_Expression;
12381 --------------------------
12382 -- Check_Initialization --
12383 --------------------------
12385 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12386 begin
12387 -- Special processing for limited types
12389 if Is_Limited_Type (T)
12390 and then not In_Instance
12391 and then not In_Inlined_Body
12392 then
12393 if not OK_For_Limited_Init (T, Exp) then
12395 -- In GNAT mode, this is just a warning, to allow it to be evilly
12396 -- turned off. Otherwise it is a real error.
12398 if GNAT_Mode then
12399 Error_Msg_N
12400 ("??cannot initialize entities of limited type!", Exp);
12402 elsif Ada_Version < Ada_2005 then
12404 -- The side effect removal machinery may generate illegal Ada
12405 -- code to avoid the usage of access types and 'reference in
12406 -- SPARK mode. Since this is legal code with respect to theorem
12407 -- proving, do not emit the error.
12409 if GNATprove_Mode
12410 and then Nkind (Exp) = N_Function_Call
12411 and then Nkind (Parent (Exp)) = N_Object_Declaration
12412 and then not Comes_From_Source
12413 (Defining_Identifier (Parent (Exp)))
12414 then
12415 null;
12417 else
12418 Error_Msg_N
12419 ("cannot initialize entities of limited type", Exp);
12420 Explain_Limited_Type (T, Exp);
12421 end if;
12423 else
12424 -- Specialize error message according to kind of illegal
12425 -- initial expression. We check the Original_Node to cover
12426 -- cases where the initialization expression of an object
12427 -- declaration generated by the compiler has been rewritten
12428 -- (such as for dispatching calls).
12430 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12431 and then
12432 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12433 then
12434 -- No error for internally-generated object declarations,
12435 -- which can come from build-in-place assignment statements.
12437 if Nkind (Parent (Exp)) = N_Object_Declaration
12438 and then not Comes_From_Source
12439 (Defining_Identifier (Parent (Exp)))
12440 then
12441 null;
12443 else
12444 Error_Msg_N
12445 ("illegal context for call to function with limited "
12446 & "result", Exp);
12447 end if;
12449 else
12450 Error_Msg_N
12451 ("initialization of limited object requires aggregate or "
12452 & "function call", Exp);
12453 end if;
12454 end if;
12455 end if;
12456 end if;
12458 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12459 -- set unless we can be sure that no range check is required.
12461 if not Expander_Active
12462 and then Is_Scalar_Type (T)
12463 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12464 then
12465 Set_Do_Range_Check (Exp);
12466 end if;
12467 end Check_Initialization;
12469 ----------------------
12470 -- Check_Interfaces --
12471 ----------------------
12473 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12474 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12476 Iface : Node_Id;
12477 Iface_Def : Node_Id;
12478 Iface_Typ : Entity_Id;
12479 Parent_Node : Node_Id;
12481 Is_Task : Boolean := False;
12482 -- Set True if parent type or any progenitor is a task interface
12484 Is_Protected : Boolean := False;
12485 -- Set True if parent type or any progenitor is a protected interface
12487 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12488 -- Check that a progenitor is compatible with declaration. If an error
12489 -- message is output, it is posted on Error_Node.
12491 ------------------
12492 -- Check_Ifaces --
12493 ------------------
12495 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12496 Iface_Id : constant Entity_Id :=
12497 Defining_Identifier (Parent (Iface_Def));
12498 Type_Def : Node_Id;
12500 begin
12501 if Nkind (N) = N_Private_Extension_Declaration then
12502 Type_Def := N;
12503 else
12504 Type_Def := Type_Definition (N);
12505 end if;
12507 if Is_Task_Interface (Iface_Id) then
12508 Is_Task := True;
12510 elsif Is_Protected_Interface (Iface_Id) then
12511 Is_Protected := True;
12512 end if;
12514 if Is_Synchronized_Interface (Iface_Id) then
12516 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12517 -- extension derived from a synchronized interface must explicitly
12518 -- be declared synchronized, because the full view will be a
12519 -- synchronized type.
12521 if Nkind (N) = N_Private_Extension_Declaration then
12522 if not Synchronized_Present (N) then
12523 Error_Msg_NE
12524 ("private extension of& must be explicitly synchronized",
12525 N, Iface_Id);
12526 end if;
12528 -- However, by 3.9.4(16/2), a full type that is a record extension
12529 -- is never allowed to derive from a synchronized interface (note
12530 -- that interfaces must be excluded from this check, because those
12531 -- are represented by derived type definitions in some cases).
12533 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12534 and then not Interface_Present (Type_Definition (N))
12535 then
12536 Error_Msg_N ("record extension cannot derive from synchronized "
12537 & "interface", Error_Node);
12538 end if;
12539 end if;
12541 -- Check that the characteristics of the progenitor are compatible
12542 -- with the explicit qualifier in the declaration.
12543 -- The check only applies to qualifiers that come from source.
12544 -- Limited_Present also appears in the declaration of corresponding
12545 -- records, and the check does not apply to them.
12547 if Limited_Present (Type_Def)
12548 and then not
12549 Is_Concurrent_Record_Type (Defining_Identifier (N))
12550 then
12551 if Is_Limited_Interface (Parent_Type)
12552 and then not Is_Limited_Interface (Iface_Id)
12553 then
12554 Error_Msg_NE
12555 ("progenitor & must be limited interface",
12556 Error_Node, Iface_Id);
12558 elsif
12559 (Task_Present (Iface_Def)
12560 or else Protected_Present (Iface_Def)
12561 or else Synchronized_Present (Iface_Def))
12562 and then Nkind (N) /= N_Private_Extension_Declaration
12563 and then not Error_Posted (N)
12564 then
12565 Error_Msg_NE
12566 ("progenitor & must be limited interface",
12567 Error_Node, Iface_Id);
12568 end if;
12570 -- Protected interfaces can only inherit from limited, synchronized
12571 -- or protected interfaces.
12573 elsif Nkind (N) = N_Full_Type_Declaration
12574 and then Protected_Present (Type_Def)
12575 then
12576 if Limited_Present (Iface_Def)
12577 or else Synchronized_Present (Iface_Def)
12578 or else Protected_Present (Iface_Def)
12579 then
12580 null;
12582 elsif Task_Present (Iface_Def) then
12583 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12584 & "from task interface", Error_Node);
12586 else
12587 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12588 & "from non-limited interface", Error_Node);
12589 end if;
12591 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12592 -- limited and synchronized.
12594 elsif Synchronized_Present (Type_Def) then
12595 if Limited_Present (Iface_Def)
12596 or else Synchronized_Present (Iface_Def)
12597 then
12598 null;
12600 elsif Protected_Present (Iface_Def)
12601 and then Nkind (N) /= N_Private_Extension_Declaration
12602 then
12603 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12604 & "from protected interface", Error_Node);
12606 elsif Task_Present (Iface_Def)
12607 and then Nkind (N) /= N_Private_Extension_Declaration
12608 then
12609 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12610 & "from task interface", Error_Node);
12612 elsif not Is_Limited_Interface (Iface_Id) then
12613 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12614 & "from non-limited interface", Error_Node);
12615 end if;
12617 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12618 -- synchronized or task interfaces.
12620 elsif Nkind (N) = N_Full_Type_Declaration
12621 and then Task_Present (Type_Def)
12622 then
12623 if Limited_Present (Iface_Def)
12624 or else Synchronized_Present (Iface_Def)
12625 or else Task_Present (Iface_Def)
12626 then
12627 null;
12629 elsif Protected_Present (Iface_Def) then
12630 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12631 & "protected interface", Error_Node);
12633 else
12634 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12635 & "non-limited interface", Error_Node);
12636 end if;
12637 end if;
12638 end Check_Ifaces;
12640 -- Start of processing for Check_Interfaces
12642 begin
12643 if Is_Interface (Parent_Type) then
12644 if Is_Task_Interface (Parent_Type) then
12645 Is_Task := True;
12647 elsif Is_Protected_Interface (Parent_Type) then
12648 Is_Protected := True;
12649 end if;
12650 end if;
12652 if Nkind (N) = N_Private_Extension_Declaration then
12654 -- Check that progenitors are compatible with declaration
12656 Iface := First (Interface_List (Def));
12657 while Present (Iface) loop
12658 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12660 Parent_Node := Parent (Base_Type (Iface_Typ));
12661 Iface_Def := Type_Definition (Parent_Node);
12663 if not Is_Interface (Iface_Typ) then
12664 Diagnose_Interface (Iface, Iface_Typ);
12665 else
12666 Check_Ifaces (Iface_Def, Iface);
12667 end if;
12669 Next (Iface);
12670 end loop;
12672 if Is_Task and Is_Protected then
12673 Error_Msg_N
12674 ("type cannot derive from task and protected interface", N);
12675 end if;
12677 return;
12678 end if;
12680 -- Full type declaration of derived type.
12681 -- Check compatibility with parent if it is interface type
12683 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12684 and then Is_Interface (Parent_Type)
12685 then
12686 Parent_Node := Parent (Parent_Type);
12688 -- More detailed checks for interface varieties
12690 Check_Ifaces
12691 (Iface_Def => Type_Definition (Parent_Node),
12692 Error_Node => Subtype_Indication (Type_Definition (N)));
12693 end if;
12695 Iface := First (Interface_List (Def));
12696 while Present (Iface) loop
12697 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12699 Parent_Node := Parent (Base_Type (Iface_Typ));
12700 Iface_Def := Type_Definition (Parent_Node);
12702 if not Is_Interface (Iface_Typ) then
12703 Diagnose_Interface (Iface, Iface_Typ);
12705 else
12706 -- "The declaration of a specific descendant of an interface
12707 -- type freezes the interface type" RM 13.14
12709 Freeze_Before (N, Iface_Typ);
12710 Check_Ifaces (Iface_Def, Error_Node => Iface);
12711 end if;
12713 Next (Iface);
12714 end loop;
12716 if Is_Task and Is_Protected then
12717 Error_Msg_N
12718 ("type cannot derive from task and protected interface", N);
12719 end if;
12720 end Check_Interfaces;
12722 ------------------------------------
12723 -- Check_Or_Process_Discriminants --
12724 ------------------------------------
12726 -- If an incomplete or private type declaration was already given for the
12727 -- type, the discriminants may have already been processed if they were
12728 -- present on the incomplete declaration. In this case a full conformance
12729 -- check has been performed in Find_Type_Name, and we then recheck here
12730 -- some properties that can't be checked on the partial view alone.
12731 -- Otherwise we call Process_Discriminants.
12733 procedure Check_Or_Process_Discriminants
12734 (N : Node_Id;
12735 T : Entity_Id;
12736 Prev : Entity_Id := Empty)
12738 begin
12739 if Has_Discriminants (T) then
12741 -- Discriminants are already set on T if they were already present
12742 -- on the partial view. Make them visible to component declarations.
12744 declare
12745 D : Entity_Id;
12746 -- Discriminant on T (full view) referencing expr on partial view
12748 Prev_D : Entity_Id;
12749 -- Entity of corresponding discriminant on partial view
12751 New_D : Node_Id;
12752 -- Discriminant specification for full view, expression is
12753 -- the syntactic copy on full view (which has been checked for
12754 -- conformance with partial view), only used here to post error
12755 -- message.
12757 begin
12758 D := First_Discriminant (T);
12759 New_D := First (Discriminant_Specifications (N));
12760 while Present (D) loop
12761 Prev_D := Current_Entity (D);
12762 Set_Current_Entity (D);
12763 Set_Is_Immediately_Visible (D);
12764 Set_Homonym (D, Prev_D);
12766 -- Handle the case where there is an untagged partial view and
12767 -- the full view is tagged: must disallow discriminants with
12768 -- defaults, unless compiling for Ada 2012, which allows a
12769 -- limited tagged type to have defaulted discriminants (see
12770 -- AI05-0214). However, suppress error here if it was already
12771 -- reported on the default expression of the partial view.
12773 if Is_Tagged_Type (T)
12774 and then Present (Expression (Parent (D)))
12775 and then (not Is_Limited_Type (Current_Scope)
12776 or else Ada_Version < Ada_2012)
12777 and then not Error_Posted (Expression (Parent (D)))
12778 then
12779 if Ada_Version >= Ada_2012 then
12780 Error_Msg_N
12781 ("discriminants of nonlimited tagged type cannot have "
12782 & "defaults",
12783 Expression (New_D));
12784 else
12785 Error_Msg_N
12786 ("discriminants of tagged type cannot have defaults",
12787 Expression (New_D));
12788 end if;
12789 end if;
12791 -- Ada 2005 (AI-230): Access discriminant allowed in
12792 -- non-limited record types.
12794 if Ada_Version < Ada_2005 then
12796 -- This restriction gets applied to the full type here. It
12797 -- has already been applied earlier to the partial view.
12799 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12800 end if;
12802 Next_Discriminant (D);
12803 Next (New_D);
12804 end loop;
12805 end;
12807 elsif Present (Discriminant_Specifications (N)) then
12808 Process_Discriminants (N, Prev);
12809 end if;
12810 end Check_Or_Process_Discriminants;
12812 ----------------------
12813 -- Check_Real_Bound --
12814 ----------------------
12816 procedure Check_Real_Bound (Bound : Node_Id) is
12817 begin
12818 if not Is_Real_Type (Etype (Bound)) then
12819 Error_Msg_N
12820 ("bound in real type definition must be of real type", Bound);
12822 elsif not Is_OK_Static_Expression (Bound) then
12823 Flag_Non_Static_Expr
12824 ("non-static expression used for real type bound!", Bound);
12826 else
12827 return;
12828 end if;
12830 Rewrite
12831 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12832 Analyze (Bound);
12833 Resolve (Bound, Standard_Float);
12834 end Check_Real_Bound;
12836 ------------------------------
12837 -- Complete_Private_Subtype --
12838 ------------------------------
12840 procedure Complete_Private_Subtype
12841 (Priv : Entity_Id;
12842 Full : Entity_Id;
12843 Full_Base : Entity_Id;
12844 Related_Nod : Node_Id)
12846 Save_Next_Entity : Entity_Id;
12847 Save_Homonym : Entity_Id;
12849 begin
12850 -- Set semantic attributes for (implicit) private subtype completion.
12851 -- If the full type has no discriminants, then it is a copy of the
12852 -- full view of the base. Otherwise, it is a subtype of the base with
12853 -- a possible discriminant constraint. Save and restore the original
12854 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12855 -- not corrupt the entity chain.
12857 Save_Next_Entity := Next_Entity (Full);
12858 Save_Homonym := Homonym (Priv);
12860 if Is_Private_Type (Full_Base)
12861 or else Is_Record_Type (Full_Base)
12862 or else Is_Concurrent_Type (Full_Base)
12863 then
12864 Copy_Node (Priv, Full);
12866 -- Note that the Etype of the full view is the same as the Etype of
12867 -- the partial view. In this fashion, the subtype has access to the
12868 -- correct view of the parent.
12870 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12871 Set_Has_Unknown_Discriminants
12872 (Full, Has_Unknown_Discriminants (Full_Base));
12873 Set_First_Entity (Full, First_Entity (Full_Base));
12874 Set_Last_Entity (Full, Last_Entity (Full_Base));
12876 -- If the underlying base type is constrained, we know that the
12877 -- full view of the subtype is constrained as well (the converse
12878 -- is not necessarily true).
12880 if Is_Constrained (Full_Base) then
12881 Set_Is_Constrained (Full);
12882 end if;
12884 else
12885 Copy_Node (Full_Base, Full);
12887 -- The following subtlety with the Etype of the full view needs to be
12888 -- taken into account here. One could think that it must naturally be
12889 -- set to the base type of the full base:
12891 -- Set_Etype (Full, Base_Type (Full_Base));
12893 -- so that the full view becomes a subtype of the full base when the
12894 -- latter is a base type, which must for example happen when the full
12895 -- base is declared as derived type. That's also correct if the full
12896 -- base is declared as an array type, or a floating-point type, or a
12897 -- fixed-point type, or a signed integer type, as these declarations
12898 -- create an implicit base type and a first subtype so the Etype of
12899 -- the full views must be the implicit base type. But that's wrong
12900 -- if the full base is declared as an access type, or an enumeration
12901 -- type, or a modular integer type, as these declarations directly
12902 -- create a base type, i.e. with Etype pointing to itself. Moreover
12903 -- the full base being declared in the private part, i.e. when the
12904 -- views are swapped, the end result is that the Etype of the full
12905 -- base is set to its private view in this case and that we need to
12906 -- propagate this setting to the full view in order for the subtype
12907 -- to be compatible with the base type.
12909 if Is_Base_Type (Full_Base)
12910 and then (Is_Derived_Type (Full_Base)
12911 or else Ekind (Full_Base) in Array_Kind
12912 or else Ekind (Full_Base) in Fixed_Point_Kind
12913 or else Ekind (Full_Base) in Float_Kind
12914 or else Ekind (Full_Base) in Signed_Integer_Kind)
12915 then
12916 Set_Etype (Full, Full_Base);
12917 end if;
12919 Set_Chars (Full, Chars (Priv));
12920 Set_Sloc (Full, Sloc (Priv));
12921 Conditional_Delay (Full, Priv);
12922 end if;
12924 Link_Entities (Full, Save_Next_Entity);
12925 Set_Homonym (Full, Save_Homonym);
12926 Set_Associated_Node_For_Itype (Full, Related_Nod);
12928 if Ekind (Full) in Incomplete_Or_Private_Kind then
12929 Reinit_Field_To_Zero (Full, F_Private_Dependents);
12930 end if;
12932 -- Set common attributes for all subtypes: kind, convention, etc.
12934 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12935 Set_Convention (Full, Convention (Full_Base));
12936 Set_Is_First_Subtype (Full, False);
12937 Set_Scope (Full, Scope (Priv));
12938 Set_Size_Info (Full, Full_Base);
12939 Copy_RM_Size (To => Full, From => Full_Base);
12940 Set_Is_Itype (Full);
12942 -- A subtype of a private-type-without-discriminants, whose full-view
12943 -- has discriminants with default expressions, is not constrained.
12945 if not Has_Discriminants (Priv) then
12946 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12948 if Has_Discriminants (Full_Base) then
12949 Set_Discriminant_Constraint
12950 (Full, Discriminant_Constraint (Full_Base));
12952 -- The partial view may have been indefinite, the full view
12953 -- might not be.
12955 Set_Has_Unknown_Discriminants
12956 (Full, Has_Unknown_Discriminants (Full_Base));
12957 end if;
12958 end if;
12960 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12961 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12963 -- Freeze the private subtype entity if its parent is delayed, and not
12964 -- already frozen. We skip this processing if the type is an anonymous
12965 -- subtype of a record component, or is the corresponding record of a
12966 -- protected type, since these are processed when the enclosing type
12967 -- is frozen. If the parent type is declared in a nested package then
12968 -- the freezing of the private and full views also happens later.
12970 if not Is_Type (Scope (Full)) then
12971 if Is_Itype (Priv)
12972 and then In_Same_Source_Unit (Full, Full_Base)
12973 and then Scope (Full_Base) /= Scope (Full)
12974 then
12975 Set_Has_Delayed_Freeze (Full);
12976 Set_Has_Delayed_Freeze (Priv);
12978 else
12979 Set_Has_Delayed_Freeze (Full,
12980 Has_Delayed_Freeze (Full_Base)
12981 and then not Is_Frozen (Full_Base));
12982 end if;
12983 end if;
12985 Set_Freeze_Node (Full, Empty);
12986 Set_Is_Frozen (Full, False);
12988 if Has_Discriminants (Full) then
12989 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12990 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12992 if Has_Unknown_Discriminants (Full) then
12993 Set_Discriminant_Constraint (Full, No_Elist);
12994 end if;
12995 end if;
12997 if Ekind (Full_Base) = E_Record_Type
12998 and then Has_Discriminants (Full_Base)
12999 and then Has_Discriminants (Priv) -- might not, if errors
13000 and then not Has_Unknown_Discriminants (Priv)
13001 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13002 then
13003 Create_Constrained_Components
13004 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13006 -- If the full base is itself derived from private, build a congruent
13007 -- subtype of its underlying full view, for use by the back end.
13009 elsif Is_Private_Type (Full_Base)
13010 and then Present (Underlying_Full_View (Full_Base))
13011 then
13012 declare
13013 Underlying_Full_Base : constant Entity_Id
13014 := Underlying_Full_View (Full_Base);
13015 Underlying_Full : constant Entity_Id
13016 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13017 begin
13018 Set_Is_Itype (Underlying_Full);
13019 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13020 Complete_Private_Subtype
13021 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13022 Set_Underlying_Full_View (Full, Underlying_Full);
13023 Set_Is_Underlying_Full_View (Underlying_Full);
13024 end;
13026 elsif Is_Record_Type (Full_Base) then
13028 -- Show Full is simply a renaming of Full_Base
13030 Set_Cloned_Subtype (Full, Full_Base);
13031 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13033 -- Propagate predicates
13035 Propagate_Predicate_Attributes (Full, Full_Base);
13036 end if;
13038 -- It is unsafe to share the bounds of a scalar type, because the Itype
13039 -- is elaborated on demand, and if a bound is nonstatic, then different
13040 -- orders of elaboration in different units will lead to different
13041 -- external symbols.
13043 if Is_Scalar_Type (Full_Base) then
13044 Set_Scalar_Range (Full,
13045 Make_Range (Sloc (Related_Nod),
13046 Low_Bound =>
13047 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13048 High_Bound =>
13049 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13051 -- This completion inherits the bounds of the full parent, but if
13052 -- the parent is an unconstrained floating point type, so is the
13053 -- completion.
13055 if Is_Floating_Point_Type (Full_Base) then
13056 Set_Includes_Infinities
13057 (Scalar_Range (Full), Has_Infinities (Full_Base));
13058 end if;
13059 end if;
13061 -- ??? It seems that a lot of fields are missing that should be copied
13062 -- from Full_Base to Full. Here are some that are introduced in a
13063 -- non-disruptive way but a cleanup is necessary.
13065 if Is_Tagged_Type (Full_Base) then
13066 Set_Is_Tagged_Type (Full);
13067 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13069 Set_Direct_Primitive_Operations
13070 (Full, Direct_Primitive_Operations (Full_Base));
13071 Set_No_Tagged_Streams_Pragma
13072 (Full, No_Tagged_Streams_Pragma (Full_Base));
13074 if Is_Interface (Full_Base) then
13075 Set_Is_Interface (Full);
13076 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13077 end if;
13079 -- Inherit class_wide type of full_base in case the partial view was
13080 -- not tagged. Otherwise it has already been created when the private
13081 -- subtype was analyzed.
13083 if No (Class_Wide_Type (Full)) then
13084 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13085 end if;
13087 -- If this is a subtype of a protected or task type, constrain its
13088 -- corresponding record, unless this is a subtype without constraints,
13089 -- i.e. a simple renaming as with an actual subtype in an instance.
13091 elsif Is_Concurrent_Type (Full_Base) then
13092 if Has_Discriminants (Full)
13093 and then Present (Corresponding_Record_Type (Full_Base))
13094 and then
13095 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13096 then
13097 Set_Corresponding_Record_Type (Full,
13098 Constrain_Corresponding_Record
13099 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13101 else
13102 Set_Corresponding_Record_Type (Full,
13103 Corresponding_Record_Type (Full_Base));
13104 end if;
13105 end if;
13107 -- Link rep item chain, and also setting of Has_Predicates from private
13108 -- subtype to full subtype, since we will need these on the full subtype
13109 -- to create the predicate function. Note that the full subtype may
13110 -- already have rep items, inherited from the full view of the base
13111 -- type, so we must be sure not to overwrite these entries.
13113 declare
13114 Append : Boolean;
13115 Item : Node_Id;
13116 Next_Item : Node_Id;
13117 Priv_Item : Node_Id;
13119 begin
13120 Item := First_Rep_Item (Full);
13121 Priv_Item := First_Rep_Item (Priv);
13123 -- If no existing rep items on full type, we can just link directly
13124 -- to the list of items on the private type, if any exist.. Same if
13125 -- the rep items are only those inherited from the base
13127 if (No (Item)
13128 or else Nkind (Item) /= N_Aspect_Specification
13129 or else Entity (Item) = Full_Base)
13130 and then Present (First_Rep_Item (Priv))
13131 then
13132 Set_First_Rep_Item (Full, Priv_Item);
13134 -- Otherwise, search to the end of items currently linked to the full
13135 -- subtype and append the private items to the end. However, if Priv
13136 -- and Full already have the same list of rep items, then the append
13137 -- is not done, as that would create a circularity.
13139 -- The partial view may have a predicate and the rep item lists of
13140 -- both views agree when inherited from the same ancestor. In that
13141 -- case, simply propagate the list from one view to the other.
13142 -- A more complex analysis needed here ???
13144 elsif Present (Priv_Item)
13145 and then Item = Next_Rep_Item (Priv_Item)
13146 then
13147 Set_First_Rep_Item (Full, Priv_Item);
13149 elsif Item /= Priv_Item then
13150 Append := True;
13151 loop
13152 Next_Item := Next_Rep_Item (Item);
13153 exit when No (Next_Item);
13154 Item := Next_Item;
13156 -- If the private view has aspect specifications, the full view
13157 -- inherits them. Since these aspects may already have been
13158 -- attached to the full view during derivation, do not append
13159 -- them if already present.
13161 if Item = First_Rep_Item (Priv) then
13162 Append := False;
13163 exit;
13164 end if;
13165 end loop;
13167 -- And link the private type items at the end of the chain
13169 if Append then
13170 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13171 end if;
13172 end if;
13173 end;
13175 -- Make sure Has_Predicates is set on full type if it is set on the
13176 -- private type. Note that it may already be set on the full type and
13177 -- if so, we don't want to unset it. Similarly, propagate information
13178 -- about delayed aspects, because the corresponding pragmas must be
13179 -- analyzed when one of the views is frozen. This last step is needed
13180 -- in particular when the full type is a scalar type for which an
13181 -- anonymous base type is constructed.
13183 -- The predicate functions are generated either at the freeze point
13184 -- of the type or at the end of the visible part, and we must avoid
13185 -- generating them twice.
13187 Propagate_Predicate_Attributes (Full, Priv);
13189 if Has_Delayed_Aspects (Priv) then
13190 Set_Has_Delayed_Aspects (Full);
13191 end if;
13192 end Complete_Private_Subtype;
13194 ----------------------------
13195 -- Constant_Redeclaration --
13196 ----------------------------
13198 procedure Constant_Redeclaration
13199 (Id : Entity_Id;
13200 N : Node_Id;
13201 T : out Entity_Id)
13203 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13204 Obj_Def : constant Node_Id := Object_Definition (N);
13205 New_T : Entity_Id;
13207 procedure Check_Possible_Deferred_Completion
13208 (Prev_Id : Entity_Id;
13209 Curr_Obj_Def : Node_Id);
13210 -- Determine whether the two object definitions describe the partial
13211 -- and the full view of a constrained deferred constant. Generate
13212 -- a subtype for the full view and verify that it statically matches
13213 -- the subtype of the partial view.
13215 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13216 -- If deferred constant is an access type initialized with an allocator,
13217 -- check whether there is an illegal recursion in the definition,
13218 -- through a default value of some record subcomponent. This is normally
13219 -- detected when generating init procs, but requires this additional
13220 -- mechanism when expansion is disabled.
13222 ----------------------------------------
13223 -- Check_Possible_Deferred_Completion --
13224 ----------------------------------------
13226 procedure Check_Possible_Deferred_Completion
13227 (Prev_Id : Entity_Id;
13228 Curr_Obj_Def : Node_Id)
13230 Curr_Typ : Entity_Id;
13231 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13232 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13233 Mismatch : Boolean := False;
13234 begin
13235 if Anon_Acc then
13236 null;
13237 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13238 declare
13239 Loc : constant Source_Ptr := Sloc (N);
13240 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13241 Decl : constant Node_Id :=
13242 Make_Subtype_Declaration (Loc,
13243 Defining_Identifier => Def_Id,
13244 Subtype_Indication =>
13245 Relocate_Node (Curr_Obj_Def));
13247 begin
13248 Insert_Before_And_Analyze (N, Decl);
13249 Set_Etype (Id, Def_Id);
13250 Curr_Typ := Def_Id;
13251 end;
13252 else
13253 Curr_Typ := Etype (Curr_Obj_Def);
13254 end if;
13256 if Anon_Acc then
13257 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13258 Mismatch := True;
13259 elsif Has_Null_Exclusion (Prev_Typ)
13260 and then not Null_Exclusion_Present (Curr_Obj_Def)
13261 then
13262 Mismatch := True;
13263 end if;
13264 -- ??? Another check needed: mismatch if disagreement
13265 -- between designated types/profiles .
13266 else
13267 Mismatch :=
13268 Is_Constrained (Prev_Typ)
13269 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13270 end if;
13272 if Mismatch then
13273 Error_Msg_Sloc := Sloc (Prev_Id);
13274 Error_Msg_N ("subtype does not statically match deferred "
13275 & "declaration #", N);
13276 end if;
13277 end Check_Possible_Deferred_Completion;
13279 ---------------------------------
13280 -- Check_Recursive_Declaration --
13281 ---------------------------------
13283 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13284 Comp : Entity_Id;
13286 begin
13287 if Is_Record_Type (Typ) then
13288 Comp := First_Component (Typ);
13289 while Present (Comp) loop
13290 if Comes_From_Source (Comp) then
13291 if Present (Expression (Parent (Comp)))
13292 and then Is_Entity_Name (Expression (Parent (Comp)))
13293 and then Entity (Expression (Parent (Comp))) = Prev
13294 then
13295 Error_Msg_Sloc := Sloc (Parent (Comp));
13296 Error_Msg_NE
13297 ("illegal circularity with declaration for & #",
13298 N, Comp);
13299 return;
13301 elsif Is_Record_Type (Etype (Comp)) then
13302 Check_Recursive_Declaration (Etype (Comp));
13303 end if;
13304 end if;
13306 Next_Component (Comp);
13307 end loop;
13308 end if;
13309 end Check_Recursive_Declaration;
13311 -- Start of processing for Constant_Redeclaration
13313 begin
13314 if Nkind (Parent (Prev)) = N_Object_Declaration then
13315 if Nkind (Object_Definition
13316 (Parent (Prev))) = N_Subtype_Indication
13317 then
13318 -- Find type of new declaration. The constraints of the two
13319 -- views must match statically, but there is no point in
13320 -- creating an itype for the full view.
13322 if Nkind (Obj_Def) = N_Subtype_Indication then
13323 Find_Type (Subtype_Mark (Obj_Def));
13324 New_T := Entity (Subtype_Mark (Obj_Def));
13326 else
13327 Find_Type (Obj_Def);
13328 New_T := Entity (Obj_Def);
13329 end if;
13331 T := Etype (Prev);
13333 else
13334 -- The full view may impose a constraint, even if the partial
13335 -- view does not, so construct the subtype.
13337 New_T := Find_Type_Of_Object (Obj_Def, N);
13338 T := New_T;
13339 end if;
13341 else
13342 -- Current declaration is illegal, diagnosed below in Enter_Name
13344 T := Empty;
13345 New_T := Any_Type;
13346 end if;
13348 -- If previous full declaration or a renaming declaration exists, or if
13349 -- a homograph is present, let Enter_Name handle it, either with an
13350 -- error or with the removal of an overridden implicit subprogram.
13351 -- The previous one is a full declaration if it has an expression
13352 -- (which in the case of an aggregate is indicated by the Init flag).
13354 if Ekind (Prev) /= E_Constant
13355 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13356 or else Present (Expression (Parent (Prev)))
13357 or else Has_Init_Expression (Parent (Prev))
13358 or else Present (Full_View (Prev))
13359 then
13360 Enter_Name (Id);
13362 -- Verify that types of both declarations match, or else that both types
13363 -- are anonymous access types whose designated subtypes statically match
13364 -- (as allowed in Ada 2005 by AI-385).
13366 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13367 and then
13368 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13369 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13370 or else Is_Access_Constant (Etype (New_T)) /=
13371 Is_Access_Constant (Etype (Prev))
13372 or else Can_Never_Be_Null (Etype (New_T)) /=
13373 Can_Never_Be_Null (Etype (Prev))
13374 or else Null_Exclusion_Present (Parent (Prev)) /=
13375 Null_Exclusion_Present (Parent (Id))
13376 or else not Subtypes_Statically_Match
13377 (Designated_Type (Etype (Prev)),
13378 Designated_Type (Etype (New_T))))
13379 then
13380 Error_Msg_Sloc := Sloc (Prev);
13381 Error_Msg_N ("type does not match declaration#", N);
13382 Set_Full_View (Prev, Id);
13383 Set_Etype (Id, Any_Type);
13385 -- A deferred constant whose type is an anonymous array is always
13386 -- illegal (unless imported). A detailed error message might be
13387 -- helpful for Ada beginners.
13389 if Nkind (Object_Definition (Parent (Prev)))
13390 = N_Constrained_Array_Definition
13391 and then Nkind (Object_Definition (N))
13392 = N_Constrained_Array_Definition
13393 then
13394 Error_Msg_N ("\each anonymous array is a distinct type", N);
13395 Error_Msg_N ("a deferred constant must have a named type",
13396 Object_Definition (Parent (Prev)));
13397 end if;
13399 elsif
13400 Null_Exclusion_Present (Parent (Prev))
13401 and then not Null_Exclusion_Present (N)
13402 then
13403 Error_Msg_Sloc := Sloc (Prev);
13404 Error_Msg_N ("null-exclusion does not match declaration#", N);
13405 Set_Full_View (Prev, Id);
13406 Set_Etype (Id, Any_Type);
13408 -- If so, process the full constant declaration
13410 else
13411 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13412 -- the deferred declaration is constrained, then the subtype defined
13413 -- by the subtype_indication in the full declaration shall match it
13414 -- statically.
13416 Check_Possible_Deferred_Completion
13417 (Prev_Id => Prev,
13418 Curr_Obj_Def => Obj_Def);
13420 Set_Full_View (Prev, Id);
13421 Set_Is_Public (Id, Is_Public (Prev));
13422 Set_Is_Internal (Id);
13423 Append_Entity (Id, Current_Scope);
13425 -- Check ALIASED present if present before (RM 7.4(7))
13427 if Is_Aliased (Prev)
13428 and then not Aliased_Present (N)
13429 then
13430 Error_Msg_Sloc := Sloc (Prev);
13431 Error_Msg_N ("ALIASED required (see declaration #)", N);
13432 end if;
13434 -- Check that placement is in private part and that the incomplete
13435 -- declaration appeared in the visible part.
13437 if Ekind (Current_Scope) = E_Package
13438 and then not In_Private_Part (Current_Scope)
13439 then
13440 Error_Msg_Sloc := Sloc (Prev);
13441 Error_Msg_N
13442 ("full constant for declaration # must be in private part", N);
13444 elsif Ekind (Current_Scope) = E_Package
13445 and then
13446 List_Containing (Parent (Prev)) /=
13447 Visible_Declarations (Package_Specification (Current_Scope))
13448 then
13449 Error_Msg_N
13450 ("deferred constant must be declared in visible part",
13451 Parent (Prev));
13452 end if;
13454 if Is_Access_Type (T)
13455 and then Nkind (Expression (N)) = N_Allocator
13456 then
13457 Check_Recursive_Declaration (Designated_Type (T));
13458 end if;
13460 -- A deferred constant is a visible entity. If type has invariants,
13461 -- verify that the initial value satisfies them. This is not done in
13462 -- GNATprove mode, as GNATprove handles invariant checks itself.
13464 if Has_Invariants (T)
13465 and then Present (Invariant_Procedure (T))
13466 and then not GNATprove_Mode
13467 then
13468 Insert_After (N,
13469 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13470 end if;
13471 end if;
13472 end Constant_Redeclaration;
13474 ----------------------
13475 -- Constrain_Access --
13476 ----------------------
13478 procedure Constrain_Access
13479 (Def_Id : in out Entity_Id;
13480 S : Node_Id;
13481 Related_Nod : Node_Id)
13483 T : constant Entity_Id := Entity (Subtype_Mark (S));
13484 Desig_Type : constant Entity_Id := Designated_Type (T);
13485 Desig_Subtype : Entity_Id;
13486 Constraint_OK : Boolean := True;
13488 begin
13489 if Is_Array_Type (Desig_Type) then
13490 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13491 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13493 elsif (Is_Record_Type (Desig_Type)
13494 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13495 and then not Is_Constrained (Desig_Type)
13496 then
13497 -- If this is a constrained access definition for a record
13498 -- component, we leave the type as an unconstrained access,
13499 -- and mark the component so that its actual type is built
13500 -- at a point of use (e.g., an assignment statement). This
13501 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13503 if Desig_Type = Current_Scope
13504 and then No (Def_Id)
13505 then
13506 Desig_Subtype :=
13507 Create_Itype
13508 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13509 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13510 Def_Id := Entity (Subtype_Mark (S));
13512 -- We indicate that the component has a per-object constraint
13513 -- for treatment at a point of use, even though the constraint
13514 -- may be independent of discriminants of the enclosing type.
13516 if Nkind (Related_Nod) = N_Component_Declaration then
13517 Set_Has_Per_Object_Constraint
13518 (Defining_Identifier (Related_Nod));
13519 end if;
13521 -- This call added to ensure that the constraint is analyzed
13522 -- (needed for a B test). Note that we still return early from
13523 -- this procedure to avoid recursive processing.
13525 Constrain_Discriminated_Type
13526 (Desig_Subtype, S, Related_Nod, For_Access => True);
13527 return;
13528 end if;
13530 -- Enforce rule that the constraint is illegal if there is an
13531 -- unconstrained view of the designated type. This means that the
13532 -- partial view (either a private type declaration or a derivation
13533 -- from a private type) has no discriminants. (Defect Report
13534 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13536 -- Rule updated for Ada 2005: The private type is said to have
13537 -- a constrained partial view, given that objects of the type
13538 -- can be declared. Furthermore, the rule applies to all access
13539 -- types, unlike the rule concerning default discriminants (see
13540 -- RM 3.7.1(7/3))
13542 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13543 and then Has_Private_Declaration (Desig_Type)
13544 and then In_Open_Scopes (Scope (Desig_Type))
13545 and then Has_Discriminants (Desig_Type)
13546 then
13547 declare
13548 Pack : constant Node_Id :=
13549 Unit_Declaration_Node (Scope (Desig_Type));
13550 Decls : List_Id;
13551 Decl : Node_Id;
13553 begin
13554 if Nkind (Pack) = N_Package_Declaration then
13555 Decls := Visible_Declarations (Specification (Pack));
13556 Decl := First (Decls);
13557 while Present (Decl) loop
13558 if (Nkind (Decl) = N_Private_Type_Declaration
13559 and then Chars (Defining_Identifier (Decl)) =
13560 Chars (Desig_Type))
13562 or else
13563 (Nkind (Decl) = N_Full_Type_Declaration
13564 and then
13565 Chars (Defining_Identifier (Decl)) =
13566 Chars (Desig_Type)
13567 and then Is_Derived_Type (Desig_Type)
13568 and then
13569 Has_Private_Declaration (Etype (Desig_Type)))
13570 then
13571 if No (Discriminant_Specifications (Decl)) then
13572 Error_Msg_N
13573 ("cannot constrain access type if designated "
13574 & "type has constrained partial view", S);
13575 end if;
13577 exit;
13578 end if;
13580 Next (Decl);
13581 end loop;
13582 end if;
13583 end;
13584 end if;
13586 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13587 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13588 For_Access => True);
13590 elsif Is_Concurrent_Type (Desig_Type)
13591 and then not Is_Constrained (Desig_Type)
13592 then
13593 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13594 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13596 else
13597 Error_Msg_N ("invalid constraint on access type", S);
13599 -- We simply ignore an invalid constraint
13601 Desig_Subtype := Desig_Type;
13602 Constraint_OK := False;
13603 end if;
13605 if No (Def_Id) then
13606 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13607 else
13608 Mutate_Ekind (Def_Id, E_Access_Subtype);
13609 end if;
13611 if Constraint_OK then
13612 Set_Etype (Def_Id, Base_Type (T));
13614 if Is_Private_Type (Desig_Type) then
13615 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13616 end if;
13617 else
13618 Set_Etype (Def_Id, Any_Type);
13619 end if;
13621 Set_Size_Info (Def_Id, T);
13622 Set_Is_Constrained (Def_Id, Constraint_OK);
13623 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13624 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13625 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13626 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13628 Conditional_Delay (Def_Id, T);
13630 -- AI-363 : Subtypes of general access types whose designated types have
13631 -- default discriminants are disallowed. In instances, the rule has to
13632 -- be checked against the actual, of which T is the subtype. In a
13633 -- generic body, the rule is checked assuming that the actual type has
13634 -- defaulted discriminants.
13636 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13637 if Ekind (Base_Type (T)) = E_General_Access_Type
13638 and then Has_Defaulted_Discriminants (Desig_Type)
13639 then
13640 if Ada_Version < Ada_2005 then
13641 Error_Msg_N
13642 ("access subtype of general access type would not " &
13643 "be allowed in Ada 2005?y?", S);
13644 else
13645 Error_Msg_N
13646 ("access subtype of general access type not allowed", S);
13647 end if;
13649 Error_Msg_N ("\discriminants have defaults", S);
13651 elsif Is_Access_Type (T)
13652 and then Is_Generic_Type (Desig_Type)
13653 and then Has_Discriminants (Desig_Type)
13654 and then In_Package_Body (Current_Scope)
13655 then
13656 if Ada_Version < Ada_2005 then
13657 Error_Msg_N
13658 ("access subtype would not be allowed in generic body "
13659 & "in Ada 2005?y?", S);
13660 else
13661 Error_Msg_N
13662 ("access subtype not allowed in generic body", S);
13663 end if;
13665 Error_Msg_N
13666 ("\designated type is a discriminated formal", S);
13667 end if;
13668 end if;
13669 end Constrain_Access;
13671 ---------------------
13672 -- Constrain_Array --
13673 ---------------------
13675 procedure Constrain_Array
13676 (Def_Id : in out Entity_Id;
13677 SI : Node_Id;
13678 Related_Nod : Node_Id;
13679 Related_Id : Entity_Id;
13680 Suffix : Character)
13682 C : constant Node_Id := Constraint (SI);
13683 Number_Of_Constraints : Nat := 0;
13684 Index : Node_Id;
13685 S, T : Entity_Id;
13686 Constraint_OK : Boolean := True;
13687 Is_FLB_Array_Subtype : Boolean := False;
13689 begin
13690 T := Entity (Subtype_Mark (SI));
13692 if Is_Access_Type (T) then
13693 T := Designated_Type (T);
13694 end if;
13696 T := Underlying_Type (T);
13698 -- If an index constraint follows a subtype mark in a subtype indication
13699 -- then the type or subtype denoted by the subtype mark must not already
13700 -- impose an index constraint. The subtype mark must denote either an
13701 -- unconstrained array type or an access type whose designated type
13702 -- is such an array type... (RM 3.6.1)
13704 if Is_Constrained (T) then
13705 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13706 Constraint_OK := False;
13708 else
13709 S := First (Constraints (C));
13710 while Present (S) loop
13711 Number_Of_Constraints := Number_Of_Constraints + 1;
13712 Next (S);
13713 end loop;
13715 -- In either case, the index constraint must provide a discrete
13716 -- range for each index of the array type and the type of each
13717 -- discrete range must be the same as that of the corresponding
13718 -- index. (RM 3.6.1)
13720 if Number_Of_Constraints /= Number_Dimensions (T) then
13721 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13722 Constraint_OK := False;
13724 else
13725 S := First (Constraints (C));
13726 Index := First_Index (T);
13727 Analyze (Index);
13729 -- Apply constraints to each index type
13731 for J in 1 .. Number_Of_Constraints loop
13732 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13734 -- If the subtype of the index has been set to indicate that
13735 -- it has a fixed lower bound, then record that the subtype's
13736 -- entity will need to be marked as being a fixed-lower-bound
13737 -- array subtype.
13739 if S = First (Constraints (C)) then
13740 Is_FLB_Array_Subtype :=
13741 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13743 -- If the parent subtype (or should this be Etype of that?)
13744 -- is an FLB array subtype, we flag an error, because we
13745 -- don't currently allow subtypes of such subtypes to
13746 -- specify a fixed lower bound for any of their indexes,
13747 -- even if the index of the parent subtype is a "range <>"
13748 -- index.
13750 if Is_FLB_Array_Subtype
13751 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13752 then
13753 Error_Msg_NE
13754 ("index with fixed lower bound not allowed for subtype "
13755 & "of fixed-lower-bound }", S, T);
13757 Is_FLB_Array_Subtype := False;
13758 end if;
13760 elsif Is_FLB_Array_Subtype
13761 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13762 then
13763 Error_Msg_NE
13764 ("constrained index not allowed for fixed-lower-bound "
13765 & "subtype of}", S, T);
13767 elsif not Is_FLB_Array_Subtype
13768 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13769 then
13770 Error_Msg_NE
13771 ("index with fixed lower bound not allowed for "
13772 & "constrained subtype of}", S, T);
13773 end if;
13775 Next (Index);
13776 Next (S);
13777 end loop;
13779 end if;
13780 end if;
13782 if No (Def_Id) then
13783 Def_Id :=
13784 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13785 Set_Parent (Def_Id, Related_Nod);
13787 else
13788 Mutate_Ekind (Def_Id, E_Array_Subtype);
13789 end if;
13791 Set_Size_Info (Def_Id, (T));
13792 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13793 Set_Etype (Def_Id, Base_Type (T));
13795 if Constraint_OK then
13796 Set_First_Index (Def_Id, First (Constraints (C)));
13797 else
13798 Set_First_Index (Def_Id, First_Index (T));
13799 end if;
13801 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13802 Set_Is_Fixed_Lower_Bound_Array_Subtype
13803 (Def_Id, Is_FLB_Array_Subtype);
13804 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13805 Set_Is_Independent (Def_Id, Is_Independent (T));
13806 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13808 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13809 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13811 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13812 -- We need to initialize the attribute because if Def_Id is previously
13813 -- analyzed through a limited_with clause, it will have the attributes
13814 -- of an incomplete type, one of which is an Elist that overlaps the
13815 -- Packed_Array_Impl_Type field.
13817 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13819 -- Build a freeze node if parent still needs one. Also make sure that
13820 -- the Depends_On_Private status is set because the subtype will need
13821 -- reprocessing at the time the base type does, and also we must set a
13822 -- conditional delay.
13824 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13825 Conditional_Delay (Def_Id, T);
13826 end Constrain_Array;
13828 ------------------------------
13829 -- Constrain_Component_Type --
13830 ------------------------------
13832 function Constrain_Component_Type
13833 (Comp : Entity_Id;
13834 Constrained_Typ : Entity_Id;
13835 Related_Node : Node_Id;
13836 Typ : Entity_Id;
13837 Constraints : Elist_Id) return Entity_Id
13839 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13840 Compon_Type : constant Entity_Id := Etype (Comp);
13842 function Build_Constrained_Array_Type
13843 (Old_Type : Entity_Id) return Entity_Id;
13844 -- If Old_Type is an array type, one of whose indexes is constrained
13845 -- by a discriminant, build an Itype whose constraint replaces the
13846 -- discriminant with its value in the constraint.
13848 function Build_Constrained_Discriminated_Type
13849 (Old_Type : Entity_Id) return Entity_Id;
13850 -- Ditto for record components. Handle the case where the constraint
13851 -- is a conversion of the discriminant value, introduced during
13852 -- expansion.
13854 function Build_Constrained_Access_Type
13855 (Old_Type : Entity_Id) return Entity_Id;
13856 -- Ditto for access types. Makes use of previous two functions, to
13857 -- constrain designated type.
13859 function Is_Discriminant (Expr : Node_Id) return Boolean;
13860 -- Returns True if Expr is a discriminant
13862 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13863 -- Find the value of a discriminant named by Discr_Expr in Constraints
13865 -----------------------------------
13866 -- Build_Constrained_Access_Type --
13867 -----------------------------------
13869 function Build_Constrained_Access_Type
13870 (Old_Type : Entity_Id) return Entity_Id
13872 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13873 Itype : Entity_Id;
13874 Desig_Subtype : Entity_Id;
13875 Scop : Entity_Id;
13877 begin
13878 -- If the original access type was not embedded in the enclosing
13879 -- type definition, there is no need to produce a new access
13880 -- subtype. In fact every access type with an explicit constraint
13881 -- generates an itype whose scope is the enclosing record.
13883 if not Is_Type (Scope (Old_Type)) then
13884 return Old_Type;
13886 elsif Is_Array_Type (Desig_Type) then
13887 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13889 elsif Has_Discriminants (Desig_Type) then
13891 -- This may be an access type to an enclosing record type for
13892 -- which we are constructing the constrained components. Return
13893 -- the enclosing record subtype. This is not always correct,
13894 -- but avoids infinite recursion. ???
13896 Desig_Subtype := Any_Type;
13898 for J in reverse 0 .. Scope_Stack.Last loop
13899 Scop := Scope_Stack.Table (J).Entity;
13901 if Is_Type (Scop)
13902 and then Base_Type (Scop) = Base_Type (Desig_Type)
13903 then
13904 Desig_Subtype := Scop;
13905 end if;
13907 exit when not Is_Type (Scop);
13908 end loop;
13910 if Desig_Subtype = Any_Type then
13911 Desig_Subtype :=
13912 Build_Constrained_Discriminated_Type (Desig_Type);
13913 end if;
13915 else
13916 return Old_Type;
13917 end if;
13919 if Desig_Subtype /= Desig_Type then
13921 -- The Related_Node better be here or else we won't be able
13922 -- to attach new itypes to a node in the tree.
13924 pragma Assert (Present (Related_Node));
13926 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13928 Set_Etype (Itype, Base_Type (Old_Type));
13929 Set_Size_Info (Itype, (Old_Type));
13930 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13931 Set_Depends_On_Private (Itype, Has_Private_Component
13932 (Old_Type));
13933 Set_Is_Access_Constant (Itype, Is_Access_Constant
13934 (Old_Type));
13936 -- The new itype needs freezing when it depends on a not frozen
13937 -- type and the enclosing subtype needs freezing.
13939 if Has_Delayed_Freeze (Constrained_Typ)
13940 and then not Is_Frozen (Constrained_Typ)
13941 then
13942 Conditional_Delay (Itype, Base_Type (Old_Type));
13943 end if;
13945 return Itype;
13947 else
13948 return Old_Type;
13949 end if;
13950 end Build_Constrained_Access_Type;
13952 ----------------------------------
13953 -- Build_Constrained_Array_Type --
13954 ----------------------------------
13956 function Build_Constrained_Array_Type
13957 (Old_Type : Entity_Id) return Entity_Id
13959 Lo_Expr : Node_Id;
13960 Hi_Expr : Node_Id;
13961 Old_Index : Node_Id;
13962 Range_Node : Node_Id;
13963 Constr_List : List_Id;
13965 Need_To_Create_Itype : Boolean := False;
13967 begin
13968 Old_Index := First_Index (Old_Type);
13969 while Present (Old_Index) loop
13970 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13972 if Is_Discriminant (Lo_Expr)
13973 or else
13974 Is_Discriminant (Hi_Expr)
13975 then
13976 Need_To_Create_Itype := True;
13977 exit;
13978 end if;
13980 Next_Index (Old_Index);
13981 end loop;
13983 if Need_To_Create_Itype then
13984 Constr_List := New_List;
13986 Old_Index := First_Index (Old_Type);
13987 while Present (Old_Index) loop
13988 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13990 if Is_Discriminant (Lo_Expr) then
13991 Lo_Expr := Get_Discr_Value (Lo_Expr);
13992 end if;
13994 if Is_Discriminant (Hi_Expr) then
13995 Hi_Expr := Get_Discr_Value (Hi_Expr);
13996 end if;
13998 Range_Node :=
13999 Make_Range
14000 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14002 Append (Range_Node, To => Constr_List);
14004 Next_Index (Old_Index);
14005 end loop;
14007 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14009 else
14010 return Old_Type;
14011 end if;
14012 end Build_Constrained_Array_Type;
14014 ------------------------------------------
14015 -- Build_Constrained_Discriminated_Type --
14016 ------------------------------------------
14018 function Build_Constrained_Discriminated_Type
14019 (Old_Type : Entity_Id) return Entity_Id
14021 Expr : Node_Id;
14022 Constr_List : List_Id;
14023 Old_Constraint : Elmt_Id;
14025 Need_To_Create_Itype : Boolean := False;
14027 begin
14028 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14029 while Present (Old_Constraint) loop
14030 Expr := Node (Old_Constraint);
14032 if Is_Discriminant (Expr) then
14033 Need_To_Create_Itype := True;
14034 exit;
14036 -- After expansion of discriminated task types, the value
14037 -- of the discriminant may be converted to a run-time type
14038 -- for restricted run-times. Propagate the value of the
14039 -- discriminant as well, so that e.g. the secondary stack
14040 -- component has a static constraint. Necessary for LLVM.
14042 elsif Nkind (Expr) = N_Type_Conversion
14043 and then Is_Discriminant (Expression (Expr))
14044 then
14045 Need_To_Create_Itype := True;
14046 exit;
14047 end if;
14049 Next_Elmt (Old_Constraint);
14050 end loop;
14052 if Need_To_Create_Itype then
14053 Constr_List := New_List;
14055 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14056 while Present (Old_Constraint) loop
14057 Expr := Node (Old_Constraint);
14059 if Is_Discriminant (Expr) then
14060 Expr := Get_Discr_Value (Expr);
14062 elsif Nkind (Expr) = N_Type_Conversion
14063 and then Is_Discriminant (Expression (Expr))
14064 then
14065 Expr := New_Copy_Tree (Expr);
14066 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14067 end if;
14069 Append (New_Copy_Tree (Expr), To => Constr_List);
14071 Next_Elmt (Old_Constraint);
14072 end loop;
14074 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14076 else
14077 return Old_Type;
14078 end if;
14079 end Build_Constrained_Discriminated_Type;
14081 ---------------------
14082 -- Get_Discr_Value --
14083 ---------------------
14085 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14086 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14087 -- Entity of a discriminant that appear as a standalone expression in
14088 -- the constraint of a component.
14090 D : Entity_Id;
14091 E : Elmt_Id;
14093 begin
14094 -- The discriminant may be declared for the type, in which case we
14095 -- find it by iterating over the list of discriminants. If the
14096 -- discriminant is inherited from a parent type, it appears as the
14097 -- corresponding discriminant of the current type. This will be the
14098 -- case when constraining an inherited component whose constraint is
14099 -- given by a discriminant of the parent.
14101 D := First_Discriminant (Typ);
14102 E := First_Elmt (Constraints);
14104 while Present (D) loop
14105 if D = Discr_Id
14106 or else D = CR_Discriminant (Discr_Id)
14107 or else Corresponding_Discriminant (D) = Discr_Id
14108 then
14109 return New_Copy_Tree (Node (E));
14110 end if;
14112 Next_Discriminant (D);
14113 Next_Elmt (E);
14114 end loop;
14116 -- The Corresponding_Discriminant mechanism is incomplete, because
14117 -- the correspondence between new and old discriminants is not one
14118 -- to one: one new discriminant can constrain several old ones. In
14119 -- that case, scan sequentially the stored_constraint, the list of
14120 -- discriminants of the parents, and the constraints.
14122 -- Previous code checked for the present of the Stored_Constraint
14123 -- list for the derived type, but did not use it at all. Should it
14124 -- be present when the component is a discriminated task type?
14126 if Is_Derived_Type (Typ)
14127 and then Scope (Discr_Id) = Etype (Typ)
14128 then
14129 D := First_Discriminant (Etype (Typ));
14130 E := First_Elmt (Constraints);
14131 while Present (D) loop
14132 if D = Discr_Id then
14133 return New_Copy_Tree (Node (E));
14134 end if;
14136 Next_Discriminant (D);
14137 Next_Elmt (E);
14138 end loop;
14139 end if;
14141 -- Something is wrong if we did not find the value
14143 raise Program_Error;
14144 end Get_Discr_Value;
14146 ---------------------
14147 -- Is_Discriminant --
14148 ---------------------
14150 function Is_Discriminant (Expr : Node_Id) return Boolean is
14151 Discrim_Scope : Entity_Id;
14153 begin
14154 if Denotes_Discriminant (Expr) then
14155 Discrim_Scope := Scope (Entity (Expr));
14157 -- Either we have a reference to one of Typ's discriminants,
14159 pragma Assert (Discrim_Scope = Typ
14161 -- or to the discriminants of the parent type, in the case
14162 -- of a derivation of a tagged type with variants.
14164 or else Discrim_Scope = Etype (Typ)
14165 or else Full_View (Discrim_Scope) = Etype (Typ)
14167 -- or same as above for the case where the discriminants
14168 -- were declared in Typ's private view.
14170 or else (Is_Private_Type (Discrim_Scope)
14171 and then Chars (Discrim_Scope) = Chars (Typ))
14173 -- or else we are deriving from the full view and the
14174 -- discriminant is declared in the private entity.
14176 or else (Is_Private_Type (Typ)
14177 and then Chars (Discrim_Scope) = Chars (Typ))
14179 -- Or we are constrained the corresponding record of a
14180 -- synchronized type that completes a private declaration.
14182 or else (Is_Concurrent_Record_Type (Typ)
14183 and then
14184 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14186 -- or we have a class-wide type, in which case make sure the
14187 -- discriminant found belongs to the root type.
14189 or else (Is_Class_Wide_Type (Typ)
14190 and then Etype (Typ) = Discrim_Scope));
14192 return True;
14193 end if;
14195 -- In all other cases we have something wrong
14197 return False;
14198 end Is_Discriminant;
14200 -- Start of processing for Constrain_Component_Type
14202 begin
14203 if Nkind (Parent (Comp)) = N_Component_Declaration
14204 and then Comes_From_Source (Parent (Comp))
14205 and then Comes_From_Source
14206 (Subtype_Indication (Component_Definition (Parent (Comp))))
14207 and then
14208 Is_Entity_Name
14209 (Subtype_Indication (Component_Definition (Parent (Comp))))
14210 then
14211 return Compon_Type;
14213 elsif Is_Array_Type (Compon_Type) then
14214 return Build_Constrained_Array_Type (Compon_Type);
14216 elsif Has_Discriminants (Compon_Type) then
14217 return Build_Constrained_Discriminated_Type (Compon_Type);
14219 elsif Is_Access_Type (Compon_Type) then
14220 return Build_Constrained_Access_Type (Compon_Type);
14222 else
14223 return Compon_Type;
14224 end if;
14225 end Constrain_Component_Type;
14227 --------------------------
14228 -- Constrain_Concurrent --
14229 --------------------------
14231 -- For concurrent types, the associated record value type carries the same
14232 -- discriminants, so when we constrain a concurrent type, we must constrain
14233 -- the corresponding record type as well.
14235 procedure Constrain_Concurrent
14236 (Def_Id : in out Entity_Id;
14237 SI : Node_Id;
14238 Related_Nod : Node_Id;
14239 Related_Id : Entity_Id;
14240 Suffix : Character)
14242 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14243 -- case of a private subtype (needed when only doing semantic analysis).
14245 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14246 T_Val : Entity_Id;
14248 begin
14249 if Is_Access_Type (T_Ent) then
14250 T_Ent := Designated_Type (T_Ent);
14251 end if;
14253 T_Val := Corresponding_Record_Type (T_Ent);
14255 if Present (T_Val) then
14257 if No (Def_Id) then
14258 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14260 -- Elaborate itype now, as it may be used in a subsequent
14261 -- synchronized operation in another scope.
14263 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14264 Build_Itype_Reference (Def_Id, Related_Nod);
14265 end if;
14266 end if;
14268 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14269 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14271 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14272 Set_Corresponding_Record_Type (Def_Id,
14273 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14275 else
14276 -- If there is no associated record, expansion is disabled and this
14277 -- is a generic context. Create a subtype in any case, so that
14278 -- semantic analysis can proceed.
14280 if No (Def_Id) then
14281 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14282 end if;
14284 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14285 end if;
14286 end Constrain_Concurrent;
14288 ------------------------------------
14289 -- Constrain_Corresponding_Record --
14290 ------------------------------------
14292 function Constrain_Corresponding_Record
14293 (Prot_Subt : Entity_Id;
14294 Corr_Rec : Entity_Id;
14295 Related_Nod : Node_Id) return Entity_Id
14297 T_Sub : constant Entity_Id :=
14298 Create_Itype
14299 (Ekind => E_Record_Subtype,
14300 Related_Nod => Related_Nod,
14301 Related_Id => Corr_Rec,
14302 Suffix => 'C',
14303 Suffix_Index => -1);
14305 begin
14306 Set_Etype (T_Sub, Corr_Rec);
14307 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14308 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14309 Set_Is_Constrained (T_Sub, True);
14310 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14311 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14313 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14314 Set_Discriminant_Constraint
14315 (T_Sub, Discriminant_Constraint (Prot_Subt));
14316 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14317 Create_Constrained_Components
14318 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14319 end if;
14321 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14323 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14324 Conditional_Delay (T_Sub, Corr_Rec);
14326 else
14327 -- This is a component subtype: it will be frozen in the context of
14328 -- the enclosing record's init_proc, so that discriminant references
14329 -- are resolved to discriminals. (Note: we used to skip freezing
14330 -- altogether in that case, which caused errors downstream for
14331 -- components of a bit packed array type).
14333 Set_Has_Delayed_Freeze (T_Sub);
14334 end if;
14336 return T_Sub;
14337 end Constrain_Corresponding_Record;
14339 -----------------------
14340 -- Constrain_Decimal --
14341 -----------------------
14343 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14344 T : constant Entity_Id := Entity (Subtype_Mark (S));
14345 C : constant Node_Id := Constraint (S);
14346 Loc : constant Source_Ptr := Sloc (C);
14347 Range_Expr : Node_Id;
14348 Digits_Expr : Node_Id;
14349 Digits_Val : Uint;
14350 Bound_Val : Ureal;
14352 begin
14353 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14355 if Nkind (C) = N_Range_Constraint then
14356 Range_Expr := Range_Expression (C);
14357 Digits_Val := Digits_Value (T);
14359 else
14360 pragma Assert (Nkind (C) = N_Digits_Constraint);
14362 Digits_Expr := Digits_Expression (C);
14363 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14365 Check_Digits_Expression (Digits_Expr);
14366 Digits_Val := Expr_Value (Digits_Expr);
14368 if Digits_Val > Digits_Value (T) then
14369 Error_Msg_N
14370 ("digits expression is incompatible with subtype", C);
14371 Digits_Val := Digits_Value (T);
14372 end if;
14374 if Present (Range_Constraint (C)) then
14375 Range_Expr := Range_Expression (Range_Constraint (C));
14376 else
14377 Range_Expr := Empty;
14378 end if;
14379 end if;
14381 Set_Etype (Def_Id, Base_Type (T));
14382 Set_Size_Info (Def_Id, (T));
14383 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14384 Set_Delta_Value (Def_Id, Delta_Value (T));
14385 Set_Scale_Value (Def_Id, Scale_Value (T));
14386 Set_Small_Value (Def_Id, Small_Value (T));
14387 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14388 Set_Digits_Value (Def_Id, Digits_Val);
14390 -- Manufacture range from given digits value if no range present
14392 if No (Range_Expr) then
14393 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14394 Range_Expr :=
14395 Make_Range (Loc,
14396 Low_Bound =>
14397 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14398 High_Bound =>
14399 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14400 end if;
14402 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14403 Set_Discrete_RM_Size (Def_Id);
14405 -- Unconditionally delay the freeze, since we cannot set size
14406 -- information in all cases correctly until the freeze point.
14408 Set_Has_Delayed_Freeze (Def_Id);
14409 end Constrain_Decimal;
14411 ----------------------------------
14412 -- Constrain_Discriminated_Type --
14413 ----------------------------------
14415 procedure Constrain_Discriminated_Type
14416 (Def_Id : Entity_Id;
14417 S : Node_Id;
14418 Related_Nod : Node_Id;
14419 For_Access : Boolean := False)
14421 E : Entity_Id := Entity (Subtype_Mark (S));
14422 T : Entity_Id;
14424 procedure Fixup_Bad_Constraint;
14425 -- Called after finding a bad constraint, and after having posted an
14426 -- appropriate error message. The goal is to leave type Def_Id in as
14427 -- reasonable state as possible.
14429 --------------------------
14430 -- Fixup_Bad_Constraint --
14431 --------------------------
14433 procedure Fixup_Bad_Constraint is
14434 begin
14435 -- Set a reasonable Ekind for the entity, including incomplete types.
14437 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14439 -- Set Etype to the known type, to reduce chances of cascaded errors
14441 Set_Etype (Def_Id, E);
14442 Set_Error_Posted (Def_Id);
14443 end Fixup_Bad_Constraint;
14445 -- Local variables
14447 C : Node_Id;
14448 Constr : Elist_Id := New_Elmt_List;
14450 -- Start of processing for Constrain_Discriminated_Type
14452 begin
14453 C := Constraint (S);
14455 -- A discriminant constraint is only allowed in a subtype indication,
14456 -- after a subtype mark. This subtype mark must denote either a type
14457 -- with discriminants, or an access type whose designated type is a
14458 -- type with discriminants. A discriminant constraint specifies the
14459 -- values of these discriminants (RM 3.7.2(5)).
14461 T := Base_Type (Entity (Subtype_Mark (S)));
14463 if Is_Access_Type (T) then
14464 T := Designated_Type (T);
14465 end if;
14467 -- In an instance it may be necessary to retrieve the full view of a
14468 -- type with unknown discriminants, or a full view with defaulted
14469 -- discriminants. In other contexts the constraint is illegal.
14471 if In_Instance
14472 and then Is_Private_Type (T)
14473 and then Present (Full_View (T))
14474 and then
14475 (Has_Unknown_Discriminants (T)
14476 or else
14477 (not Has_Discriminants (T)
14478 and then Has_Defaulted_Discriminants (Full_View (T))))
14479 then
14480 T := Full_View (T);
14481 E := Full_View (E);
14482 end if;
14484 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14485 -- generating an error for access-to-incomplete subtypes.
14487 if Ada_Version >= Ada_2005
14488 and then Ekind (T) = E_Incomplete_Type
14489 and then Nkind (Parent (S)) = N_Subtype_Declaration
14490 and then not Is_Itype (Def_Id)
14491 then
14492 -- A little sanity check: emit an error message if the type has
14493 -- discriminants to begin with. Type T may be a regular incomplete
14494 -- type or imported via a limited with clause.
14496 if Has_Discriminants (T)
14497 or else (From_Limited_With (T)
14498 and then Present (Non_Limited_View (T))
14499 and then Nkind (Parent (Non_Limited_View (T))) =
14500 N_Full_Type_Declaration
14501 and then Present (Discriminant_Specifications
14502 (Parent (Non_Limited_View (T)))))
14503 then
14504 Error_Msg_N
14505 ("(Ada 2005) incomplete subtype may not be constrained", C);
14506 else
14507 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14508 end if;
14510 Fixup_Bad_Constraint;
14511 return;
14513 -- Check that the type has visible discriminants. The type may be
14514 -- a private type with unknown discriminants whose full view has
14515 -- discriminants which are invisible.
14517 elsif not Has_Discriminants (T)
14518 or else
14519 (Has_Unknown_Discriminants (T)
14520 and then Is_Private_Type (T))
14521 then
14522 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14523 Fixup_Bad_Constraint;
14524 return;
14526 elsif Is_Constrained (E)
14527 or else (Ekind (E) = E_Class_Wide_Subtype
14528 and then Present (Discriminant_Constraint (E)))
14529 then
14530 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14531 Fixup_Bad_Constraint;
14532 return;
14533 end if;
14535 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14536 -- applies to the base type.
14538 T := Base_Type (T);
14540 Constr := Build_Discriminant_Constraints (T, S);
14542 -- If the list returned was empty we had an error in building the
14543 -- discriminant constraint. We have also already signalled an error
14544 -- in the incomplete type case
14546 if Is_Empty_Elmt_List (Constr) then
14547 Fixup_Bad_Constraint;
14548 return;
14549 end if;
14551 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14552 end Constrain_Discriminated_Type;
14554 ---------------------------
14555 -- Constrain_Enumeration --
14556 ---------------------------
14558 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14559 T : constant Entity_Id := Entity (Subtype_Mark (S));
14560 C : constant Node_Id := Constraint (S);
14562 begin
14563 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14565 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14566 Set_Etype (Def_Id, Base_Type (T));
14567 Set_Size_Info (Def_Id, (T));
14568 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14569 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14571 -- Inherit the chain of representation items instead of replacing it
14572 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14573 -- the derived type as a subtype declaration and the former needs to
14574 -- preserve existing representation items (see Build_Derived_Type).
14576 Inherit_Rep_Item_Chain (Def_Id, T);
14578 Set_Discrete_RM_Size (Def_Id);
14579 end Constrain_Enumeration;
14581 ----------------------
14582 -- Constrain_Float --
14583 ----------------------
14585 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14586 T : constant Entity_Id := Entity (Subtype_Mark (S));
14587 C : Node_Id;
14588 D : Node_Id;
14589 Rais : Node_Id;
14591 begin
14592 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14594 Set_Etype (Def_Id, Base_Type (T));
14595 Set_Size_Info (Def_Id, (T));
14596 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14598 -- Process the constraint
14600 C := Constraint (S);
14602 -- Digits constraint present
14604 if Nkind (C) = N_Digits_Constraint then
14605 Check_Restriction (No_Obsolescent_Features, C);
14607 if Warn_On_Obsolescent_Feature then
14608 Error_Msg_N
14609 ("subtype digits constraint is an " &
14610 "obsolescent feature (RM J.3(8))?j?", C);
14611 end if;
14613 D := Digits_Expression (C);
14614 Analyze_And_Resolve (D, Any_Integer);
14615 Check_Digits_Expression (D);
14616 Set_Digits_Value (Def_Id, Expr_Value (D));
14618 -- Check that digits value is in range. Obviously we can do this
14619 -- at compile time, but it is strictly a runtime check, and of
14620 -- course there is an ACVC test that checks this.
14622 if Digits_Value (Def_Id) > Digits_Value (T) then
14623 Error_Msg_Uint_1 := Digits_Value (T);
14624 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14625 Rais :=
14626 Make_Raise_Constraint_Error (Sloc (D),
14627 Reason => CE_Range_Check_Failed);
14628 Insert_Action (Declaration_Node (Def_Id), Rais);
14629 end if;
14631 C := Range_Constraint (C);
14633 -- No digits constraint present
14635 else
14636 Set_Digits_Value (Def_Id, Digits_Value (T));
14637 end if;
14639 -- Range constraint present
14641 if Nkind (C) = N_Range_Constraint then
14642 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14644 -- No range constraint present
14646 else
14647 pragma Assert (No (C));
14648 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14649 end if;
14651 Set_Is_Constrained (Def_Id);
14652 end Constrain_Float;
14654 ---------------------
14655 -- Constrain_Index --
14656 ---------------------
14658 procedure Constrain_Index
14659 (Index : Node_Id;
14660 S : Node_Id;
14661 Related_Nod : Node_Id;
14662 Related_Id : Entity_Id;
14663 Suffix : Character;
14664 Suffix_Index : Pos)
14666 Def_Id : Entity_Id;
14667 R : Node_Id := Empty;
14668 T : constant Entity_Id := Etype (Index);
14669 Is_FLB_Index : Boolean := False;
14671 begin
14672 Def_Id :=
14673 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14674 Set_Etype (Def_Id, Base_Type (T));
14676 if Nkind (S) = N_Range
14677 or else
14678 (Nkind (S) = N_Attribute_Reference
14679 and then Attribute_Name (S) = Name_Range)
14680 then
14681 -- A Range attribute will be transformed into N_Range by Resolve
14683 -- If a range has an Empty upper bound, then remember that for later
14684 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14685 -- flag, and also set the upper bound of the range to the index
14686 -- subtype's upper bound rather than leaving it Empty. In truth,
14687 -- that upper bound corresponds to a box ("<>"), but it's convenient
14688 -- to set it to the upper bound to avoid needing to add special tests
14689 -- in various places for an Empty upper bound, and in any case it
14690 -- accurately characterizes the index's range of values.
14692 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14693 Is_FLB_Index := True;
14694 Set_High_Bound (S, Type_High_Bound (T));
14695 end if;
14697 R := S;
14699 Process_Range_Expr_In_Decl (R, T);
14701 if not Error_Posted (S)
14702 and then
14703 (Nkind (S) /= N_Range
14704 or else not Covers (T, (Etype (Low_Bound (S))))
14705 or else not Covers (T, (Etype (High_Bound (S)))))
14706 then
14707 if Base_Type (T) /= Any_Type
14708 and then Etype (Low_Bound (S)) /= Any_Type
14709 and then Etype (High_Bound (S)) /= Any_Type
14710 then
14711 Error_Msg_N ("range expected", S);
14712 end if;
14713 end if;
14715 elsif Nkind (S) = N_Subtype_Indication then
14717 -- The parser has verified that this is a discrete indication
14719 Resolve_Discrete_Subtype_Indication (S, T);
14720 Bad_Predicated_Subtype_Use
14721 ("subtype& has predicate, not allowed in index constraint",
14722 S, Entity (Subtype_Mark (S)));
14724 R := Range_Expression (Constraint (S));
14726 -- Capture values of bounds and generate temporaries for them if
14727 -- needed, since checks may cause duplication of the expressions
14728 -- which must not be reevaluated.
14730 -- The forced evaluation removes side effects from expressions, which
14731 -- should occur also in GNATprove mode. Otherwise, we end up with
14732 -- unexpected insertions of actions at places where this is not
14733 -- supposed to occur, e.g. on default parameters of a call.
14735 if Expander_Active or GNATprove_Mode then
14736 Force_Evaluation
14737 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14738 Force_Evaluation
14739 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14740 end if;
14742 elsif Nkind (S) = N_Discriminant_Association then
14744 -- Syntactically valid in subtype indication
14746 Error_Msg_N ("invalid index constraint", S);
14747 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14748 return;
14750 -- Subtype_Mark case, no anonymous subtypes to construct
14752 else
14753 Analyze (S);
14755 if Is_Entity_Name (S) then
14756 if not Is_Type (Entity (S)) then
14757 Error_Msg_N ("expect subtype mark for index constraint", S);
14759 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14760 Wrong_Type (S, Base_Type (T));
14762 -- Check error of subtype with predicate in index constraint
14764 else
14765 Bad_Predicated_Subtype_Use
14766 ("subtype& has predicate, not allowed in index constraint",
14767 S, Entity (S));
14768 end if;
14770 return;
14772 else
14773 Error_Msg_N ("invalid index constraint", S);
14774 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14775 return;
14776 end if;
14777 end if;
14779 -- Complete construction of the Itype
14781 if Is_Modular_Integer_Type (T) then
14782 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14784 elsif Is_Integer_Type (T) then
14785 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14787 else
14788 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14789 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14790 Set_First_Literal (Def_Id, First_Literal (T));
14791 end if;
14793 Set_Size_Info (Def_Id, (T));
14794 Copy_RM_Size (To => Def_Id, From => T);
14795 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14797 -- If this is a range for a fixed-lower-bound subtype, then set the
14798 -- index itype's low bound to the FLB and the index itype's upper bound
14799 -- to the high bound of the parent array type's index subtype. Also,
14800 -- mark the itype as an FLB index subtype.
14802 if Nkind (S) = N_Range and then Is_FLB_Index then
14803 Set_Scalar_Range
14804 (Def_Id,
14805 Make_Range (Sloc (S),
14806 Low_Bound => Low_Bound (S),
14807 High_Bound => Type_High_Bound (T)));
14808 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14810 else
14811 Set_Scalar_Range (Def_Id, R);
14812 end if;
14814 Set_Etype (S, Def_Id);
14815 Set_Discrete_RM_Size (Def_Id);
14816 end Constrain_Index;
14818 -----------------------
14819 -- Constrain_Integer --
14820 -----------------------
14822 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14823 T : constant Entity_Id := Entity (Subtype_Mark (S));
14824 C : constant Node_Id := Constraint (S);
14826 begin
14827 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14829 if Is_Modular_Integer_Type (T) then
14830 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14831 else
14832 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14833 end if;
14835 Set_Etype (Def_Id, Base_Type (T));
14836 Set_Size_Info (Def_Id, (T));
14837 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14838 Set_Discrete_RM_Size (Def_Id);
14839 end Constrain_Integer;
14841 ------------------------------
14842 -- Constrain_Ordinary_Fixed --
14843 ------------------------------
14845 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14846 T : constant Entity_Id := Entity (Subtype_Mark (S));
14847 C : Node_Id;
14848 D : Node_Id;
14849 Rais : Node_Id;
14851 begin
14852 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14853 Set_Etype (Def_Id, Base_Type (T));
14854 Set_Size_Info (Def_Id, (T));
14855 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14856 Set_Small_Value (Def_Id, Small_Value (T));
14858 -- Process the constraint
14860 C := Constraint (S);
14862 -- Delta constraint present
14864 if Nkind (C) = N_Delta_Constraint then
14865 Check_Restriction (No_Obsolescent_Features, C);
14867 if Warn_On_Obsolescent_Feature then
14868 Error_Msg_S
14869 ("subtype delta constraint is an " &
14870 "obsolescent feature (RM J.3(7))?j?");
14871 end if;
14873 D := Delta_Expression (C);
14874 Analyze_And_Resolve (D, Any_Real);
14875 Check_Delta_Expression (D);
14876 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14878 -- Check that delta value is in range. Obviously we can do this
14879 -- at compile time, but it is strictly a runtime check, and of
14880 -- course there is an ACVC test that checks this.
14882 if Delta_Value (Def_Id) < Delta_Value (T) then
14883 Error_Msg_N ("??delta value is too small", D);
14884 Rais :=
14885 Make_Raise_Constraint_Error (Sloc (D),
14886 Reason => CE_Range_Check_Failed);
14887 Insert_Action (Declaration_Node (Def_Id), Rais);
14888 end if;
14890 C := Range_Constraint (C);
14892 -- No delta constraint present
14894 else
14895 Set_Delta_Value (Def_Id, Delta_Value (T));
14896 end if;
14898 -- Range constraint present
14900 if Nkind (C) = N_Range_Constraint then
14901 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14903 -- No range constraint present
14905 else
14906 pragma Assert (No (C));
14907 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14908 end if;
14910 Set_Discrete_RM_Size (Def_Id);
14912 -- Unconditionally delay the freeze, since we cannot set size
14913 -- information in all cases correctly until the freeze point.
14915 Set_Has_Delayed_Freeze (Def_Id);
14916 end Constrain_Ordinary_Fixed;
14918 -----------------------
14919 -- Contain_Interface --
14920 -----------------------
14922 function Contain_Interface
14923 (Iface : Entity_Id;
14924 Ifaces : Elist_Id) return Boolean
14926 Iface_Elmt : Elmt_Id;
14928 begin
14929 if Present (Ifaces) then
14930 Iface_Elmt := First_Elmt (Ifaces);
14931 while Present (Iface_Elmt) loop
14932 if Node (Iface_Elmt) = Iface then
14933 return True;
14934 end if;
14936 Next_Elmt (Iface_Elmt);
14937 end loop;
14938 end if;
14940 return False;
14941 end Contain_Interface;
14943 ---------------------------
14944 -- Convert_Scalar_Bounds --
14945 ---------------------------
14947 procedure Convert_Scalar_Bounds
14948 (N : Node_Id;
14949 Parent_Type : Entity_Id;
14950 Derived_Type : Entity_Id;
14951 Loc : Source_Ptr)
14953 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14955 Lo : Node_Id;
14956 Hi : Node_Id;
14957 Rng : Node_Id;
14959 begin
14960 -- Defend against previous errors
14962 if No (Scalar_Range (Derived_Type)) then
14963 Check_Error_Detected;
14964 return;
14965 end if;
14967 Lo := Build_Scalar_Bound
14968 (Type_Low_Bound (Derived_Type),
14969 Parent_Type, Implicit_Base);
14971 Hi := Build_Scalar_Bound
14972 (Type_High_Bound (Derived_Type),
14973 Parent_Type, Implicit_Base);
14975 Rng :=
14976 Make_Range (Loc,
14977 Low_Bound => Lo,
14978 High_Bound => Hi);
14980 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14982 Set_Parent (Rng, N);
14983 Set_Scalar_Range (Derived_Type, Rng);
14985 -- Analyze the bounds
14987 Analyze_And_Resolve (Lo, Implicit_Base);
14988 Analyze_And_Resolve (Hi, Implicit_Base);
14990 -- Analyze the range itself, except that we do not analyze it if
14991 -- the bounds are real literals, and we have a fixed-point type.
14992 -- The reason for this is that we delay setting the bounds in this
14993 -- case till we know the final Small and Size values (see circuit
14994 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14996 if Is_Fixed_Point_Type (Parent_Type)
14997 and then Nkind (Lo) = N_Real_Literal
14998 and then Nkind (Hi) = N_Real_Literal
14999 then
15000 return;
15002 -- Here we do the analysis of the range
15004 -- Note: we do this manually, since if we do a normal Analyze and
15005 -- Resolve call, there are problems with the conversions used for
15006 -- the derived type range.
15008 else
15009 Set_Etype (Rng, Implicit_Base);
15010 Set_Analyzed (Rng, True);
15011 end if;
15012 end Convert_Scalar_Bounds;
15014 -------------------
15015 -- Copy_And_Swap --
15016 -------------------
15018 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15019 begin
15020 -- Initialize new full declaration entity by copying the pertinent
15021 -- fields of the corresponding private declaration entity.
15023 -- We temporarily set Ekind to a value appropriate for a type to
15024 -- avoid assert failures in Einfo from checking for setting type
15025 -- attributes on something that is not a type. Ekind (Priv) is an
15026 -- appropriate choice, since it allowed the attributes to be set
15027 -- in the first place. This Ekind value will be modified later.
15029 Mutate_Ekind (Full, Ekind (Priv));
15031 -- Also set Etype temporarily to Any_Type, again, in the absence
15032 -- of errors, it will be properly reset, and if there are errors,
15033 -- then we want a value of Any_Type to remain.
15035 Set_Etype (Full, Any_Type);
15037 -- Now start copying attributes
15039 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15041 if Has_Discriminants (Full) then
15042 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15043 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15044 end if;
15046 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15047 Set_Homonym (Full, Homonym (Priv));
15048 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15049 Set_Is_Public (Full, Is_Public (Priv));
15050 Set_Is_Pure (Full, Is_Pure (Priv));
15051 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15052 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15053 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15054 Set_Has_Pragma_Unreferenced_Objects
15055 (Full, Has_Pragma_Unreferenced_Objects
15056 (Priv));
15058 Conditional_Delay (Full, Priv);
15060 if Is_Tagged_Type (Full) then
15061 Set_Direct_Primitive_Operations
15062 (Full, Direct_Primitive_Operations (Priv));
15063 Set_No_Tagged_Streams_Pragma
15064 (Full, No_Tagged_Streams_Pragma (Priv));
15066 if Is_Base_Type (Priv) then
15067 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15068 end if;
15069 end if;
15071 Set_Is_Volatile (Full, Is_Volatile (Priv));
15072 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15073 Set_Scope (Full, Scope (Priv));
15074 Set_Prev_Entity (Full, Prev_Entity (Priv));
15075 Set_Next_Entity (Full, Next_Entity (Priv));
15076 Set_First_Entity (Full, First_Entity (Priv));
15077 Set_Last_Entity (Full, Last_Entity (Priv));
15079 -- If access types have been recorded for later handling, keep them in
15080 -- the full view so that they get handled when the full view freeze
15081 -- node is expanded.
15083 if Present (Freeze_Node (Priv))
15084 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15085 then
15086 Ensure_Freeze_Node (Full);
15087 Set_Access_Types_To_Process
15088 (Freeze_Node (Full),
15089 Access_Types_To_Process (Freeze_Node (Priv)));
15090 end if;
15092 -- Swap the two entities. Now Private is the full type entity and Full
15093 -- is the private one. They will be swapped back at the end of the
15094 -- private part. This swapping ensures that the entity that is visible
15095 -- in the private part is the full declaration.
15097 Exchange_Entities (Priv, Full);
15098 Append_Entity (Full, Scope (Full));
15099 end Copy_And_Swap;
15101 -------------------------------------
15102 -- Copy_Array_Base_Type_Attributes --
15103 -------------------------------------
15105 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15106 begin
15107 Set_Component_Alignment (T1, Component_Alignment (T2));
15108 Set_Component_Type (T1, Component_Type (T2));
15109 Set_Component_Size (T1, Component_Size (T2));
15110 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
15111 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15112 Propagate_Concurrent_Flags (T1, T2);
15113 Set_Is_Packed (T1, Is_Packed (T2));
15114 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15115 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15116 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15117 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15118 end Copy_Array_Base_Type_Attributes;
15120 -----------------------------------
15121 -- Copy_Array_Subtype_Attributes --
15122 -----------------------------------
15124 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15125 -- let it be recreated during freezing for the sake of better debug info.
15127 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15128 begin
15129 Set_Size_Info (T1, T2);
15131 Set_First_Index (T1, First_Index (T2));
15132 Set_Is_Aliased (T1, Is_Aliased (T2));
15133 Set_Is_Atomic (T1, Is_Atomic (T2));
15134 Set_Is_Independent (T1, Is_Independent (T2));
15135 Set_Is_Volatile (T1, Is_Volatile (T2));
15136 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15137 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15138 Set_Is_Constrained (T1, Is_Constrained (T2));
15139 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15140 Inherit_Rep_Item_Chain (T1, T2);
15141 Set_Convention (T1, Convention (T2));
15142 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15143 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15144 end Copy_Array_Subtype_Attributes;
15146 -----------------------------------
15147 -- Create_Constrained_Components --
15148 -----------------------------------
15150 procedure Create_Constrained_Components
15151 (Subt : Entity_Id;
15152 Decl_Node : Node_Id;
15153 Typ : Entity_Id;
15154 Constraints : Elist_Id)
15156 Loc : constant Source_Ptr := Sloc (Subt);
15157 Comp_List : constant Elist_Id := New_Elmt_List;
15158 Parent_Type : constant Entity_Id := Etype (Typ);
15159 Assoc_List : constant List_Id := New_List;
15161 Discr_Val : Elmt_Id;
15162 Errors : Boolean;
15163 New_C : Entity_Id;
15164 Old_C : Entity_Id;
15165 Is_Static : Boolean := True;
15166 Is_Compile_Time_Known : Boolean := True;
15168 procedure Collect_Fixed_Components (Typ : Entity_Id);
15169 -- Collect parent type components that do not appear in a variant part
15171 procedure Create_All_Components;
15172 -- Iterate over Comp_List to create the components of the subtype
15174 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15175 -- Creates a new component from Old_Compon, copying all the fields from
15176 -- it, including its Etype, inserts the new component in the Subt entity
15177 -- chain and returns the new component.
15179 function Is_Variant_Record (T : Entity_Id) return Boolean;
15180 -- If true, and discriminants are static, collect only components from
15181 -- variants selected by discriminant values.
15183 ------------------------------
15184 -- Collect_Fixed_Components --
15185 ------------------------------
15187 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15188 begin
15189 -- Build association list for discriminants, and find components of the
15190 -- variant part selected by the values of the discriminants.
15192 Old_C := First_Discriminant (Typ);
15193 Discr_Val := First_Elmt (Constraints);
15194 while Present (Old_C) loop
15195 Append_To (Assoc_List,
15196 Make_Component_Association (Loc,
15197 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15198 Expression => New_Copy (Node (Discr_Val))));
15200 Next_Elmt (Discr_Val);
15201 Next_Discriminant (Old_C);
15202 end loop;
15204 -- The tag and the possible parent component are unconditionally in
15205 -- the subtype.
15207 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15208 Old_C := First_Component (Typ);
15209 while Present (Old_C) loop
15210 if Chars (Old_C) in Name_uTag | Name_uParent then
15211 Append_Elmt (Old_C, Comp_List);
15212 end if;
15214 Next_Component (Old_C);
15215 end loop;
15216 end if;
15217 end Collect_Fixed_Components;
15219 ---------------------------
15220 -- Create_All_Components --
15221 ---------------------------
15223 procedure Create_All_Components is
15224 Comp : Elmt_Id;
15226 begin
15227 Comp := First_Elmt (Comp_List);
15228 while Present (Comp) loop
15229 Old_C := Node (Comp);
15230 New_C := Create_Component (Old_C);
15232 Set_Etype
15233 (New_C,
15234 Constrain_Component_Type
15235 (Old_C, Subt, Decl_Node, Typ, Constraints));
15236 Set_Is_Public (New_C, Is_Public (Subt));
15238 Next_Elmt (Comp);
15239 end loop;
15240 end Create_All_Components;
15242 ----------------------
15243 -- Create_Component --
15244 ----------------------
15246 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15247 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15249 begin
15250 if Ekind (Old_Compon) = E_Discriminant
15251 and then Is_Completely_Hidden (Old_Compon)
15252 then
15253 -- This is a shadow discriminant created for a discriminant of
15254 -- the parent type, which needs to be present in the subtype.
15255 -- Give the shadow discriminant an internal name that cannot
15256 -- conflict with that of visible components.
15258 Set_Chars (New_Compon, New_Internal_Name ('C'));
15259 end if;
15261 -- Set the parent so we have a proper link for freezing etc. This is
15262 -- not a real parent pointer, since of course our parent does not own
15263 -- up to us and reference us, we are an illegitimate child of the
15264 -- original parent.
15266 Set_Parent (New_Compon, Parent (Old_Compon));
15268 -- We do not want this node marked as Comes_From_Source, since
15269 -- otherwise it would get first class status and a separate cross-
15270 -- reference line would be generated. Illegitimate children do not
15271 -- rate such recognition.
15273 Set_Comes_From_Source (New_Compon, False);
15275 -- But it is a real entity, and a birth certificate must be properly
15276 -- registered by entering it into the entity list, and setting its
15277 -- scope to the given subtype. This turns out to be useful for the
15278 -- LLVM code generator, but that scope is not used otherwise.
15280 Enter_Name (New_Compon);
15281 Set_Scope (New_Compon, Subt);
15283 return New_Compon;
15284 end Create_Component;
15286 -----------------------
15287 -- Is_Variant_Record --
15288 -----------------------
15290 function Is_Variant_Record (T : Entity_Id) return Boolean is
15291 begin
15292 return Nkind (Parent (T)) = N_Full_Type_Declaration
15293 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
15294 and then Present (Component_List (Type_Definition (Parent (T))))
15295 and then
15296 Present
15297 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
15298 end Is_Variant_Record;
15300 -- Start of processing for Create_Constrained_Components
15302 begin
15303 pragma Assert (Subt /= Base_Type (Subt));
15304 pragma Assert (Typ = Base_Type (Typ));
15306 Set_First_Entity (Subt, Empty);
15307 Set_Last_Entity (Subt, Empty);
15309 -- Check whether constraint is fully static, in which case we can
15310 -- optimize the list of components.
15312 Discr_Val := First_Elmt (Constraints);
15313 while Present (Discr_Val) loop
15314 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15315 Is_Static := False;
15317 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15318 Is_Compile_Time_Known := False;
15319 exit;
15320 end if;
15321 end if;
15323 Next_Elmt (Discr_Val);
15324 end loop;
15326 Set_Has_Static_Discriminants (Subt, Is_Static);
15328 Push_Scope (Subt);
15330 -- Inherit the discriminants of the parent type
15332 Add_Discriminants : declare
15333 Num_Disc : Nat;
15334 Num_Stor : Nat;
15336 begin
15337 Num_Disc := 0;
15338 Old_C := First_Discriminant (Typ);
15340 while Present (Old_C) loop
15341 Num_Disc := Num_Disc + 1;
15342 New_C := Create_Component (Old_C);
15343 Set_Is_Public (New_C, Is_Public (Subt));
15344 Next_Discriminant (Old_C);
15345 end loop;
15347 -- For an untagged derived subtype, the number of discriminants may
15348 -- be smaller than the number of inherited discriminants, because
15349 -- several of them may be renamed by a single new discriminant or
15350 -- constrained. In this case, add the hidden discriminants back into
15351 -- the subtype, because they need to be present if the optimizer of
15352 -- the GCC 4.x back-end decides to break apart assignments between
15353 -- objects using the parent view into member-wise assignments.
15355 Num_Stor := 0;
15357 if Is_Derived_Type (Typ)
15358 and then not Is_Tagged_Type (Typ)
15359 then
15360 Old_C := First_Stored_Discriminant (Typ);
15362 while Present (Old_C) loop
15363 Num_Stor := Num_Stor + 1;
15364 Next_Stored_Discriminant (Old_C);
15365 end loop;
15366 end if;
15368 if Num_Stor > Num_Disc then
15370 -- Find out multiple uses of new discriminants, and add hidden
15371 -- components for the extra renamed discriminants. We recognize
15372 -- multiple uses through the Corresponding_Discriminant of a
15373 -- new discriminant: if it constrains several old discriminants,
15374 -- this field points to the last one in the parent type. The
15375 -- stored discriminants of the derived type have the same name
15376 -- as those of the parent.
15378 declare
15379 Constr : Elmt_Id;
15380 New_Discr : Entity_Id;
15381 Old_Discr : Entity_Id;
15383 begin
15384 Constr := First_Elmt (Stored_Constraint (Typ));
15385 Old_Discr := First_Stored_Discriminant (Typ);
15386 while Present (Constr) loop
15387 if Is_Entity_Name (Node (Constr))
15388 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15389 then
15390 New_Discr := Entity (Node (Constr));
15392 if Chars (Corresponding_Discriminant (New_Discr)) /=
15393 Chars (Old_Discr)
15394 then
15395 -- The new discriminant has been used to rename a
15396 -- subsequent old discriminant. Introduce a shadow
15397 -- component for the current old discriminant.
15399 New_C := Create_Component (Old_Discr);
15400 Set_Original_Record_Component (New_C, Old_Discr);
15401 end if;
15403 else
15404 -- The constraint has eliminated the old discriminant.
15405 -- Introduce a shadow component.
15407 New_C := Create_Component (Old_Discr);
15408 Set_Original_Record_Component (New_C, Old_Discr);
15409 end if;
15411 Next_Elmt (Constr);
15412 Next_Stored_Discriminant (Old_Discr);
15413 end loop;
15414 end;
15415 end if;
15416 end Add_Discriminants;
15418 if Is_Compile_Time_Known
15419 and then Is_Variant_Record (Typ)
15420 then
15421 Collect_Fixed_Components (Typ);
15422 Gather_Components
15423 (Typ,
15424 Component_List (Type_Definition (Parent (Typ))),
15425 Governed_By => Assoc_List,
15426 Into => Comp_List,
15427 Report_Errors => Errors,
15428 Allow_Compile_Time => True);
15429 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15431 Create_All_Components;
15433 -- If the subtype declaration is created for a tagged type derivation
15434 -- with constraints, we retrieve the record definition of the parent
15435 -- type to select the components of the proper variant.
15437 elsif Is_Compile_Time_Known
15438 and then Is_Tagged_Type (Typ)
15439 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15440 and then
15441 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15442 and then Is_Variant_Record (Parent_Type)
15443 then
15444 Collect_Fixed_Components (Typ);
15445 Gather_Components
15446 (Typ,
15447 Component_List (Type_Definition (Parent (Parent_Type))),
15448 Governed_By => Assoc_List,
15449 Into => Comp_List,
15450 Report_Errors => Errors,
15451 Allow_Compile_Time => True);
15453 -- Note: previously there was a check at this point that no errors
15454 -- were detected. As a consequence of AI05-220 there may be an error
15455 -- if an inherited discriminant that controls a variant has a non-
15456 -- static constraint.
15458 -- If the tagged derivation has a type extension, collect all the
15459 -- new relevant components therein via Gather_Components.
15461 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15462 then
15463 Gather_Components
15464 (Typ,
15465 Component_List
15466 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15467 Governed_By => Assoc_List,
15468 Into => Comp_List,
15469 Report_Errors => Errors,
15470 Allow_Compile_Time => True,
15471 Include_Interface_Tag => True);
15472 end if;
15474 Create_All_Components;
15476 else
15477 -- If discriminants are not static, or if this is a multi-level type
15478 -- extension, we have to include all components of the parent type.
15480 Old_C := First_Component (Typ);
15481 while Present (Old_C) loop
15482 New_C := Create_Component (Old_C);
15484 Set_Etype
15485 (New_C,
15486 Constrain_Component_Type
15487 (Old_C, Subt, Decl_Node, Typ, Constraints));
15488 Set_Is_Public (New_C, Is_Public (Subt));
15490 Next_Component (Old_C);
15491 end loop;
15492 end if;
15494 End_Scope;
15495 end Create_Constrained_Components;
15497 ------------------------------------------
15498 -- Decimal_Fixed_Point_Type_Declaration --
15499 ------------------------------------------
15501 procedure Decimal_Fixed_Point_Type_Declaration
15502 (T : Entity_Id;
15503 Def : Node_Id)
15505 Loc : constant Source_Ptr := Sloc (Def);
15506 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15507 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15508 Max_Digits : constant Nat :=
15509 (if System_Max_Integer_Size = 128 then 38 else 18);
15510 -- Maximum number of digits that can be represented in an integer
15512 Implicit_Base : Entity_Id;
15513 Digs_Val : Uint;
15514 Delta_Val : Ureal;
15515 Scale_Val : Uint;
15516 Bound_Val : Ureal;
15518 begin
15519 Check_Restriction (No_Fixed_Point, Def);
15521 -- Create implicit base type
15523 Implicit_Base :=
15524 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15525 Set_Etype (Implicit_Base, Implicit_Base);
15527 -- Analyze and process delta expression
15529 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15531 Check_Delta_Expression (Delta_Expr);
15532 Delta_Val := Expr_Value_R (Delta_Expr);
15534 -- Check delta is power of 10, and determine scale value from it
15536 declare
15537 Val : Ureal;
15539 begin
15540 Scale_Val := Uint_0;
15541 Val := Delta_Val;
15543 if Val < Ureal_1 then
15544 while Val < Ureal_1 loop
15545 Val := Val * Ureal_10;
15546 Scale_Val := Scale_Val + 1;
15547 end loop;
15549 if Scale_Val > Max_Digits then
15550 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15551 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15552 Scale_Val := UI_From_Int (Max_Digits);
15553 end if;
15555 else
15556 while Val > Ureal_1 loop
15557 Val := Val / Ureal_10;
15558 Scale_Val := Scale_Val - 1;
15559 end loop;
15561 if Scale_Val < -Max_Digits then
15562 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15563 Error_Msg_N ("scale is less than minimum value of ^", Def);
15564 Scale_Val := UI_From_Int (-Max_Digits);
15565 end if;
15566 end if;
15568 if Val /= Ureal_1 then
15569 Error_Msg_N ("delta expression must be a power of 10", Def);
15570 Delta_Val := Ureal_10 ** (-Scale_Val);
15571 end if;
15572 end;
15574 -- Set delta, scale and small (small = delta for decimal type)
15576 Set_Delta_Value (Implicit_Base, Delta_Val);
15577 Set_Scale_Value (Implicit_Base, Scale_Val);
15578 Set_Small_Value (Implicit_Base, Delta_Val);
15580 -- Analyze and process digits expression
15582 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15583 Check_Digits_Expression (Digs_Expr);
15584 Digs_Val := Expr_Value (Digs_Expr);
15586 if Digs_Val > Max_Digits then
15587 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15588 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15589 Digs_Val := UI_From_Int (Max_Digits);
15590 end if;
15592 Set_Digits_Value (Implicit_Base, Digs_Val);
15593 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15595 -- Set range of base type from digits value for now. This will be
15596 -- expanded to represent the true underlying base range by Freeze.
15598 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15600 -- Note: We leave Esize unset for now, size will be set at freeze
15601 -- time. We have to do this for ordinary fixed-point, because the size
15602 -- depends on the specified small, and we might as well do the same for
15603 -- decimal fixed-point.
15605 pragma Assert (not Known_Esize (Implicit_Base));
15607 -- If there are bounds given in the declaration use them as the
15608 -- bounds of the first named subtype.
15610 if Present (Real_Range_Specification (Def)) then
15611 declare
15612 RRS : constant Node_Id := Real_Range_Specification (Def);
15613 Low : constant Node_Id := Low_Bound (RRS);
15614 High : constant Node_Id := High_Bound (RRS);
15615 Low_Val : Ureal;
15616 High_Val : Ureal;
15618 begin
15619 Analyze_And_Resolve (Low, Any_Real);
15620 Analyze_And_Resolve (High, Any_Real);
15621 Check_Real_Bound (Low);
15622 Check_Real_Bound (High);
15623 Low_Val := Expr_Value_R (Low);
15624 High_Val := Expr_Value_R (High);
15626 if Low_Val < (-Bound_Val) then
15627 Error_Msg_N
15628 ("range low bound too small for digits value", Low);
15629 Low_Val := -Bound_Val;
15630 end if;
15632 if High_Val > Bound_Val then
15633 Error_Msg_N
15634 ("range high bound too large for digits value", High);
15635 High_Val := Bound_Val;
15636 end if;
15638 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15639 end;
15641 -- If no explicit range, use range that corresponds to given
15642 -- digits value. This will end up as the final range for the
15643 -- first subtype.
15645 else
15646 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15647 end if;
15649 -- Complete entity for first subtype. The inheritance of the rep item
15650 -- chain ensures that SPARK-related pragmas are not clobbered when the
15651 -- decimal fixed point type acts as a full view of a private type.
15653 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15654 Set_Etype (T, Implicit_Base);
15655 Set_Size_Info (T, Implicit_Base);
15656 Inherit_Rep_Item_Chain (T, Implicit_Base);
15657 Set_Digits_Value (T, Digs_Val);
15658 Set_Delta_Value (T, Delta_Val);
15659 Set_Small_Value (T, Delta_Val);
15660 Set_Scale_Value (T, Scale_Val);
15661 Set_Is_Constrained (T);
15662 end Decimal_Fixed_Point_Type_Declaration;
15664 -----------------------------------
15665 -- Derive_Progenitor_Subprograms --
15666 -----------------------------------
15668 procedure Derive_Progenitor_Subprograms
15669 (Parent_Type : Entity_Id;
15670 Tagged_Type : Entity_Id)
15672 E : Entity_Id;
15673 Elmt : Elmt_Id;
15674 Iface : Entity_Id;
15675 Iface_Alias : Entity_Id;
15676 Iface_Elmt : Elmt_Id;
15677 Iface_Subp : Entity_Id;
15678 New_Subp : Entity_Id := Empty;
15679 Prim_Elmt : Elmt_Id;
15680 Subp : Entity_Id;
15681 Typ : Entity_Id;
15683 begin
15684 pragma Assert (Ada_Version >= Ada_2005
15685 and then Is_Record_Type (Tagged_Type)
15686 and then Is_Tagged_Type (Tagged_Type)
15687 and then Has_Interfaces (Tagged_Type));
15689 -- Step 1: Transfer to the full-view primitives associated with the
15690 -- partial-view that cover interface primitives. Conceptually this
15691 -- work should be done later by Process_Full_View; done here to
15692 -- simplify its implementation at later stages. It can be safely
15693 -- done here because interfaces must be visible in the partial and
15694 -- private view (RM 7.3(7.3/2)).
15696 -- Small optimization: This work is only required if the parent may
15697 -- have entities whose Alias attribute reference an interface primitive.
15698 -- Such a situation may occur if the parent is an abstract type and the
15699 -- primitive has not been yet overridden or if the parent is a generic
15700 -- formal type covering interfaces.
15702 -- If the tagged type is not abstract, it cannot have abstract
15703 -- primitives (the only entities in the list of primitives of
15704 -- non-abstract tagged types that can reference abstract primitives
15705 -- through its Alias attribute are the internal entities that have
15706 -- attribute Interface_Alias, and these entities are generated later
15707 -- by Add_Internal_Interface_Entities).
15709 if In_Private_Part (Current_Scope)
15710 and then (Is_Abstract_Type (Parent_Type)
15711 or else
15712 Is_Generic_Type (Parent_Type))
15713 then
15714 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15715 while Present (Elmt) loop
15716 Subp := Node (Elmt);
15718 -- At this stage it is not possible to have entities in the list
15719 -- of primitives that have attribute Interface_Alias.
15721 pragma Assert (No (Interface_Alias (Subp)));
15723 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15725 if Is_Interface (Typ) then
15726 E := Find_Primitive_Covering_Interface
15727 (Tagged_Type => Tagged_Type,
15728 Iface_Prim => Subp);
15730 if Present (E)
15731 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15732 then
15733 Replace_Elmt (Elmt, E);
15734 Remove_Homonym (Subp);
15735 end if;
15736 end if;
15738 Next_Elmt (Elmt);
15739 end loop;
15740 end if;
15742 -- Step 2: Add primitives of progenitors that are not implemented by
15743 -- parents of Tagged_Type.
15745 if Present (Interfaces (Base_Type (Tagged_Type))) then
15746 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15747 while Present (Iface_Elmt) loop
15748 Iface := Node (Iface_Elmt);
15750 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15751 while Present (Prim_Elmt) loop
15752 Iface_Subp := Node (Prim_Elmt);
15753 Iface_Alias := Ultimate_Alias (Iface_Subp);
15755 -- Exclude derivation of predefined primitives except those
15756 -- that come from source, or are inherited from one that comes
15757 -- from source. Required to catch declarations of equality
15758 -- operators of interfaces. For example:
15760 -- type Iface is interface;
15761 -- function "=" (Left, Right : Iface) return Boolean;
15763 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15764 or else Comes_From_Source (Iface_Alias)
15765 then
15766 E :=
15767 Find_Primitive_Covering_Interface
15768 (Tagged_Type => Tagged_Type,
15769 Iface_Prim => Iface_Subp);
15771 -- If not found we derive a new primitive leaving its alias
15772 -- attribute referencing the interface primitive.
15774 if No (E) then
15775 Derive_Subprogram
15776 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15778 -- Ada 2012 (AI05-0197): If the covering primitive's name
15779 -- differs from the name of the interface primitive then it
15780 -- is a private primitive inherited from a parent type. In
15781 -- such case, given that Tagged_Type covers the interface,
15782 -- the inherited private primitive becomes visible. For such
15783 -- purpose we add a new entity that renames the inherited
15784 -- private primitive.
15786 elsif Chars (E) /= Chars (Iface_Subp) then
15787 pragma Assert (Has_Suffix (E, 'P'));
15788 Derive_Subprogram
15789 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15790 Set_Alias (New_Subp, E);
15791 Set_Is_Abstract_Subprogram (New_Subp,
15792 Is_Abstract_Subprogram (E));
15794 -- Propagate to the full view interface entities associated
15795 -- with the partial view.
15797 elsif In_Private_Part (Current_Scope)
15798 and then Present (Alias (E))
15799 and then Alias (E) = Iface_Subp
15800 and then
15801 List_Containing (Parent (E)) /=
15802 Private_Declarations
15803 (Specification
15804 (Unit_Declaration_Node (Current_Scope)))
15805 then
15806 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15807 end if;
15808 end if;
15810 Next_Elmt (Prim_Elmt);
15811 end loop;
15813 Next_Elmt (Iface_Elmt);
15814 end loop;
15815 end if;
15816 end Derive_Progenitor_Subprograms;
15818 -----------------------
15819 -- Derive_Subprogram --
15820 -----------------------
15822 procedure Derive_Subprogram
15823 (New_Subp : out Entity_Id;
15824 Parent_Subp : Entity_Id;
15825 Derived_Type : Entity_Id;
15826 Parent_Type : Entity_Id;
15827 Actual_Subp : Entity_Id := Empty)
15829 Formal : Entity_Id;
15830 -- Formal parameter of parent primitive operation
15832 Formal_Of_Actual : Entity_Id;
15833 -- Formal parameter of actual operation, when the derivation is to
15834 -- create a renaming for a primitive operation of an actual in an
15835 -- instantiation.
15837 New_Formal : Entity_Id;
15838 -- Formal of inherited operation
15840 Visible_Subp : Entity_Id := Parent_Subp;
15842 function Is_Private_Overriding return Boolean;
15843 -- If Subp is a private overriding of a visible operation, the inherited
15844 -- operation derives from the overridden op (even though its body is the
15845 -- overriding one) and the inherited operation is visible now. See
15846 -- sem_disp to see the full details of the handling of the overridden
15847 -- subprogram, which is removed from the list of primitive operations of
15848 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15849 -- and used to diagnose abstract operations that need overriding in the
15850 -- derived type.
15852 procedure Replace_Type (Id, New_Id : Entity_Id);
15853 -- When the type is an anonymous access type, create a new access type
15854 -- designating the derived type.
15856 procedure Set_Derived_Name;
15857 -- This procedure sets the appropriate Chars name for New_Subp. This
15858 -- is normally just a copy of the parent name. An exception arises for
15859 -- type support subprograms, where the name is changed to reflect the
15860 -- name of the derived type, e.g. if type foo is derived from type bar,
15861 -- then a procedure barDA is derived with a name fooDA.
15863 ---------------------------
15864 -- Is_Private_Overriding --
15865 ---------------------------
15867 function Is_Private_Overriding return Boolean is
15868 Prev : Entity_Id;
15870 begin
15871 -- If the parent is not a dispatching operation there is no
15872 -- need to investigate overridings
15874 if not Is_Dispatching_Operation (Parent_Subp) then
15875 return False;
15876 end if;
15878 -- The visible operation that is overridden is a homonym of the
15879 -- parent subprogram. We scan the homonym chain to find the one
15880 -- whose alias is the subprogram we are deriving.
15882 Prev := Current_Entity (Parent_Subp);
15883 while Present (Prev) loop
15884 if Ekind (Prev) = Ekind (Parent_Subp)
15885 and then Alias (Prev) = Parent_Subp
15886 and then Scope (Parent_Subp) = Scope (Prev)
15887 and then not Is_Hidden (Prev)
15888 then
15889 Visible_Subp := Prev;
15890 return True;
15891 end if;
15893 Prev := Homonym (Prev);
15894 end loop;
15896 return False;
15897 end Is_Private_Overriding;
15899 ------------------
15900 -- Replace_Type --
15901 ------------------
15903 procedure Replace_Type (Id, New_Id : Entity_Id) is
15904 Id_Type : constant Entity_Id := Etype (Id);
15905 Acc_Type : Entity_Id;
15906 Par : constant Node_Id := Parent (Derived_Type);
15908 begin
15909 -- When the type is an anonymous access type, create a new access
15910 -- type designating the derived type. This itype must be elaborated
15911 -- at the point of the derivation, not on subsequent calls that may
15912 -- be out of the proper scope for Gigi, so we insert a reference to
15913 -- it after the derivation.
15915 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15916 declare
15917 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15919 begin
15920 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15921 and then Present (Full_View (Desig_Typ))
15922 and then not Is_Private_Type (Parent_Type)
15923 then
15924 Desig_Typ := Full_View (Desig_Typ);
15925 end if;
15927 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15929 -- Ada 2005 (AI-251): Handle also derivations of abstract
15930 -- interface primitives.
15932 or else (Is_Interface (Desig_Typ)
15933 and then not Is_Class_Wide_Type (Desig_Typ))
15934 then
15935 Acc_Type := New_Copy (Id_Type);
15936 Set_Etype (Acc_Type, Acc_Type);
15937 Set_Scope (Acc_Type, New_Subp);
15939 -- Set size of anonymous access type. If we have an access
15940 -- to an unconstrained array, this is a fat pointer, so it
15941 -- is sizes at twice addtress size.
15943 if Is_Array_Type (Desig_Typ)
15944 and then not Is_Constrained (Desig_Typ)
15945 then
15946 Init_Size (Acc_Type, 2 * System_Address_Size);
15948 -- Other cases use a thin pointer
15950 else
15951 Init_Size (Acc_Type, System_Address_Size);
15952 end if;
15954 -- Set remaining characterstics of anonymous access type
15956 Reinit_Alignment (Acc_Type);
15957 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15959 Set_Etype (New_Id, Acc_Type);
15960 Set_Scope (New_Id, New_Subp);
15962 -- Create a reference to it
15964 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15966 else
15967 Set_Etype (New_Id, Id_Type);
15968 end if;
15969 end;
15971 -- In Ada2012, a formal may have an incomplete type but the type
15972 -- derivation that inherits the primitive follows the full view.
15974 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15975 or else
15976 (Ekind (Id_Type) = E_Record_Type_With_Private
15977 and then Present (Full_View (Id_Type))
15978 and then
15979 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15980 or else
15981 (Ada_Version >= Ada_2012
15982 and then Ekind (Id_Type) = E_Incomplete_Type
15983 and then Full_View (Id_Type) = Parent_Type)
15984 then
15985 -- Constraint checks on formals are generated during expansion,
15986 -- based on the signature of the original subprogram. The bounds
15987 -- of the derived type are not relevant, and thus we can use
15988 -- the base type for the formals. However, the return type may be
15989 -- used in a context that requires that the proper static bounds
15990 -- be used (a case statement, for example) and for those cases
15991 -- we must use the derived type (first subtype), not its base.
15993 -- If the derived_type_definition has no constraints, we know that
15994 -- the derived type has the same constraints as the first subtype
15995 -- of the parent, and we can also use it rather than its base,
15996 -- which can lead to more efficient code.
15998 if Etype (Id) = Parent_Type then
15999 if Is_Scalar_Type (Parent_Type)
16000 and then
16001 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16002 then
16003 Set_Etype (New_Id, Derived_Type);
16005 elsif Nkind (Par) = N_Full_Type_Declaration
16006 and then
16007 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16008 and then
16009 Is_Entity_Name
16010 (Subtype_Indication (Type_Definition (Par)))
16011 then
16012 Set_Etype (New_Id, Derived_Type);
16014 else
16015 Set_Etype (New_Id, Base_Type (Derived_Type));
16016 end if;
16018 else
16019 Set_Etype (New_Id, Base_Type (Derived_Type));
16020 end if;
16022 else
16023 Set_Etype (New_Id, Etype (Id));
16024 end if;
16025 end Replace_Type;
16027 ----------------------
16028 -- Set_Derived_Name --
16029 ----------------------
16031 procedure Set_Derived_Name is
16032 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16033 begin
16034 if Nm = TSS_Null then
16035 Set_Chars (New_Subp, Chars (Parent_Subp));
16036 else
16037 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16038 end if;
16039 end Set_Derived_Name;
16041 -- Start of processing for Derive_Subprogram
16043 begin
16044 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16045 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16047 -- Check whether the inherited subprogram is a private operation that
16048 -- should be inherited but not yet made visible. Such subprograms can
16049 -- become visible at a later point (e.g., the private part of a public
16050 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16051 -- following predicate is true, then this is not such a private
16052 -- operation and the subprogram simply inherits the name of the parent
16053 -- subprogram. Note the special check for the names of controlled
16054 -- operations, which are currently exempted from being inherited with
16055 -- a hidden name because they must be findable for generation of
16056 -- implicit run-time calls.
16058 if not Is_Hidden (Parent_Subp)
16059 or else Is_Internal (Parent_Subp)
16060 or else Is_Private_Overriding
16061 or else Is_Internal_Name (Chars (Parent_Subp))
16062 or else (Is_Controlled (Parent_Type)
16063 and then Chars (Parent_Subp) in Name_Adjust
16064 | Name_Finalize
16065 | Name_Initialize)
16066 then
16067 Set_Derived_Name;
16069 -- An inherited dispatching equality will be overridden by an internally
16070 -- generated one, or by an explicit one, so preserve its name and thus
16071 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16072 -- private operation it may become invisible if the full view has
16073 -- progenitors, and the dispatch table will be malformed.
16074 -- We check that the type is limited to handle the anomalous declaration
16075 -- of Limited_Controlled, which is derived from a non-limited type, and
16076 -- which is handled specially elsewhere as well.
16078 elsif Chars (Parent_Subp) = Name_Op_Eq
16079 and then Is_Dispatching_Operation (Parent_Subp)
16080 and then Etype (Parent_Subp) = Standard_Boolean
16081 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16082 and then
16083 Etype (First_Formal (Parent_Subp)) =
16084 Etype (Next_Formal (First_Formal (Parent_Subp)))
16085 then
16086 Set_Derived_Name;
16088 -- If parent is hidden, this can be a regular derivation if the
16089 -- parent is immediately visible in a non-instantiating context,
16090 -- or if we are in the private part of an instance. This test
16091 -- should still be refined ???
16093 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16094 -- operation as a non-visible operation in cases where the parent
16095 -- subprogram might not be visible now, but was visible within the
16096 -- original generic, so it would be wrong to make the inherited
16097 -- subprogram non-visible now. (Not clear if this test is fully
16098 -- correct; are there any cases where we should declare the inherited
16099 -- operation as not visible to avoid it being overridden, e.g., when
16100 -- the parent type is a generic actual with private primitives ???)
16102 -- (they should be treated the same as other private inherited
16103 -- subprograms, but it's not clear how to do this cleanly). ???
16105 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16106 and then Is_Immediately_Visible (Parent_Subp)
16107 and then not In_Instance)
16108 or else In_Instance_Not_Visible
16109 then
16110 Set_Derived_Name;
16112 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16113 -- overrides an interface primitive because interface primitives
16114 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16116 elsif Ada_Version >= Ada_2005
16117 and then Is_Dispatching_Operation (Parent_Subp)
16118 and then Present (Covered_Interface_Op (Parent_Subp))
16119 then
16120 Set_Derived_Name;
16122 -- Otherwise, the type is inheriting a private operation, so enter it
16123 -- with a special name so it can't be overridden. See also below, where
16124 -- we check for this case, and if so avoid setting Requires_Overriding.
16126 else
16127 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16128 end if;
16130 Set_Parent (New_Subp, Parent (Derived_Type));
16132 if Present (Actual_Subp) then
16133 Replace_Type (Actual_Subp, New_Subp);
16134 else
16135 Replace_Type (Parent_Subp, New_Subp);
16136 end if;
16138 Conditional_Delay (New_Subp, Parent_Subp);
16140 -- If we are creating a renaming for a primitive operation of an
16141 -- actual of a generic derived type, we must examine the signature
16142 -- of the actual primitive, not that of the generic formal, which for
16143 -- example may be an interface. However the name and initial value
16144 -- of the inherited operation are those of the formal primitive.
16146 Formal := First_Formal (Parent_Subp);
16148 if Present (Actual_Subp) then
16149 Formal_Of_Actual := First_Formal (Actual_Subp);
16150 else
16151 Formal_Of_Actual := Empty;
16152 end if;
16154 while Present (Formal) loop
16155 New_Formal := New_Copy (Formal);
16157 -- Extra formals are not inherited from a limited interface parent
16158 -- since limitedness is not inherited in such case (AI-419) and this
16159 -- affects the extra formals.
16161 if Is_Limited_Interface (Parent_Type) then
16162 Set_Extra_Formal (New_Formal, Empty);
16163 Set_Extra_Accessibility (New_Formal, Empty);
16164 end if;
16166 -- Normally we do not go copying parents, but in the case of
16167 -- formals, we need to link up to the declaration (which is the
16168 -- parameter specification), and it is fine to link up to the
16169 -- original formal's parameter specification in this case.
16171 Set_Parent (New_Formal, Parent (Formal));
16172 Append_Entity (New_Formal, New_Subp);
16174 if Present (Formal_Of_Actual) then
16175 Replace_Type (Formal_Of_Actual, New_Formal);
16176 Next_Formal (Formal_Of_Actual);
16177 else
16178 Replace_Type (Formal, New_Formal);
16179 end if;
16181 Next_Formal (Formal);
16182 end loop;
16184 -- Extra formals are shared between the parent subprogram and this
16185 -- internal entity built by Derive_Subprogram (implicit in the above
16186 -- copy of formals), unless the parent type is a limited interface type;
16187 -- hence we must inherit also the reference to the first extra formal.
16188 -- When the parent type is an interface, the extra formals will be added
16189 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16191 if not Is_Limited_Interface (Parent_Type) then
16192 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16194 if Ekind (New_Subp) = E_Function then
16195 Set_Extra_Accessibility_Of_Result (New_Subp,
16196 Extra_Accessibility_Of_Result (Parent_Subp));
16197 end if;
16198 end if;
16200 -- If this derivation corresponds to a tagged generic actual, then
16201 -- primitive operations rename those of the actual. Otherwise the
16202 -- primitive operations rename those of the parent type, If the parent
16203 -- renames an intrinsic operator, so does the new subprogram. We except
16204 -- concatenation, which is always properly typed, and does not get
16205 -- expanded as other intrinsic operations.
16207 if No (Actual_Subp) then
16208 if Is_Intrinsic_Subprogram (Parent_Subp) then
16209 Set_Convention (New_Subp, Convention_Intrinsic);
16210 Set_Is_Intrinsic_Subprogram (New_Subp);
16212 if Present (Alias (Parent_Subp))
16213 and then Chars (Parent_Subp) /= Name_Op_Concat
16214 then
16215 Set_Alias (New_Subp, Alias (Parent_Subp));
16216 else
16217 Set_Alias (New_Subp, Parent_Subp);
16218 end if;
16220 else
16221 Set_Alias (New_Subp, Parent_Subp);
16222 end if;
16224 else
16225 Set_Alias (New_Subp, Actual_Subp);
16226 end if;
16228 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16230 -- Derived subprograms of a tagged type must inherit the convention
16231 -- of the parent subprogram (a requirement of AI95-117). Derived
16232 -- subprograms of untagged types simply get convention Ada by default.
16234 -- If the derived type is a tagged generic formal type with unknown
16235 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16237 -- However, if the type is derived from a generic formal, the further
16238 -- inherited subprogram has the convention of the non-generic ancestor.
16239 -- Otherwise there would be no way to override the operation.
16240 -- (This is subject to forthcoming ARG discussions).
16242 if Is_Tagged_Type (Derived_Type) then
16243 if Is_Generic_Type (Derived_Type)
16244 and then Has_Unknown_Discriminants (Derived_Type)
16245 then
16246 Set_Convention (New_Subp, Convention_Intrinsic);
16248 else
16249 if Is_Generic_Type (Parent_Type)
16250 and then Has_Unknown_Discriminants (Parent_Type)
16251 then
16252 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16253 else
16254 Set_Convention (New_Subp, Convention (Parent_Subp));
16255 end if;
16256 end if;
16257 end if;
16259 -- Predefined controlled operations retain their name even if the parent
16260 -- is hidden (see above), but they are not primitive operations if the
16261 -- ancestor is not visible, for example if the parent is a private
16262 -- extension completed with a controlled extension. Note that a full
16263 -- type that is controlled can break privacy: the flag Is_Controlled is
16264 -- set on both views of the type.
16266 if Is_Controlled (Parent_Type)
16267 and then Chars (Parent_Subp) in Name_Initialize
16268 | Name_Adjust
16269 | Name_Finalize
16270 and then Is_Hidden (Parent_Subp)
16271 and then not Is_Visibly_Controlled (Parent_Type)
16272 then
16273 Set_Is_Hidden (New_Subp);
16274 end if;
16276 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16277 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16279 if Ekind (Parent_Subp) = E_Procedure then
16280 Set_Is_Valued_Procedure
16281 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16282 else
16283 Set_Has_Controlling_Result
16284 (New_Subp, Has_Controlling_Result (Parent_Subp));
16285 end if;
16287 -- No_Return must be inherited properly. If this is overridden in the
16288 -- case of a dispatching operation, then the check is made later in
16289 -- Check_Abstract_Overriding that the overriding operation is also
16290 -- No_Return (no such check is required for the nondispatching case).
16292 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16294 -- If the parent subprogram is marked as Ghost, then so is the derived
16295 -- subprogram. The ghost policy for the derived subprogram is set from
16296 -- the effective ghost policy at the point of derived type declaration.
16298 if Is_Ghost_Entity (Parent_Subp) then
16299 Set_Is_Ghost_Entity (New_Subp);
16300 end if;
16302 -- A derived function with a controlling result is abstract. If the
16303 -- Derived_Type is a nonabstract formal generic derived type, then
16304 -- inherited operations are not abstract: the required check is done at
16305 -- instantiation time. If the derivation is for a generic actual, the
16306 -- function is not abstract unless the actual is.
16308 if Is_Generic_Type (Derived_Type)
16309 and then not Is_Abstract_Type (Derived_Type)
16310 then
16311 null;
16313 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16314 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16315 -- that functions with controlling access results of record extensions
16316 -- with a null extension part require overriding (AI95-00391/06).
16318 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16319 -- implementing the rule of RM 7.3.2(6.1/4).
16321 -- A subprogram subject to pragma Extensions_Visible with value False
16322 -- requires overriding if the subprogram has at least one controlling
16323 -- OUT parameter (SPARK RM 6.1.7(6)).
16325 elsif Ada_Version >= Ada_2005
16326 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16327 or else (Is_Tagged_Type (Derived_Type)
16328 and then Etype (New_Subp) = Derived_Type
16329 and then not Is_Null_Extension (Derived_Type))
16330 or else (Is_Tagged_Type (Derived_Type)
16331 and then Ekind (Etype (New_Subp)) =
16332 E_Anonymous_Access_Type
16333 and then Designated_Type (Etype (New_Subp)) =
16334 Derived_Type)
16335 or else (Comes_From_Source (Alias (New_Subp))
16336 and then Is_EVF_Procedure (Alias (New_Subp)))
16338 -- AI12-0042: Set Requires_Overriding when a type extension
16339 -- inherits a private operation that is visible at the
16340 -- point of extension (Has_Private_Ancestor is False) from
16341 -- an ancestor that has Type_Invariant'Class, and when the
16342 -- type extension is in a visible part (the latter as
16343 -- clarified by AI12-0382).
16345 or else
16346 (not Has_Private_Ancestor (Derived_Type)
16347 and then Has_Invariants (Parent_Type)
16348 and then
16349 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16350 and then
16351 Class_Present
16352 (Get_Pragma (Parent_Type, Pragma_Invariant))
16353 and then Is_Private_Primitive (Parent_Subp)
16354 and then In_Visible_Part (Scope (Derived_Type))))
16356 and then No (Actual_Subp)
16357 then
16358 if not Is_Tagged_Type (Derived_Type)
16359 or else Is_Abstract_Type (Derived_Type)
16360 or else Is_Abstract_Subprogram (Alias (New_Subp))
16361 then
16362 Set_Is_Abstract_Subprogram (New_Subp);
16364 -- If the Chars of the new subprogram is different from that of the
16365 -- parent's one, it means that we entered it with a special name so
16366 -- it can't be overridden (see above). In that case we had better not
16367 -- *require* it to be overridden. This is the case where the parent
16368 -- type inherited the operation privately, so there's no danger of
16369 -- dangling dispatching.
16371 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16372 Set_Requires_Overriding (New_Subp);
16373 end if;
16375 elsif Ada_Version < Ada_2005
16376 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16377 or else (Is_Tagged_Type (Derived_Type)
16378 and then Etype (New_Subp) = Derived_Type
16379 and then No (Actual_Subp)))
16380 then
16381 Set_Is_Abstract_Subprogram (New_Subp);
16383 -- AI05-0097 : an inherited operation that dispatches on result is
16384 -- abstract if the derived type is abstract, even if the parent type
16385 -- is concrete and the derived type is a null extension.
16387 elsif Has_Controlling_Result (Alias (New_Subp))
16388 and then Is_Abstract_Type (Etype (New_Subp))
16389 then
16390 Set_Is_Abstract_Subprogram (New_Subp);
16392 -- Finally, if the parent type is abstract we must verify that all
16393 -- inherited operations are either non-abstract or overridden, or that
16394 -- the derived type itself is abstract (this check is performed at the
16395 -- end of a package declaration, in Check_Abstract_Overriding). A
16396 -- private overriding in the parent type will not be visible in the
16397 -- derivation if we are not in an inner package or in a child unit of
16398 -- the parent type, in which case the abstractness of the inherited
16399 -- operation is carried to the new subprogram.
16401 elsif Is_Abstract_Type (Parent_Type)
16402 and then not In_Open_Scopes (Scope (Parent_Type))
16403 and then Is_Private_Overriding
16404 and then Is_Abstract_Subprogram (Visible_Subp)
16405 then
16406 if No (Actual_Subp) then
16407 Set_Alias (New_Subp, Visible_Subp);
16408 Set_Is_Abstract_Subprogram (New_Subp, True);
16410 else
16411 -- If this is a derivation for an instance of a formal derived
16412 -- type, abstractness comes from the primitive operation of the
16413 -- actual, not from the operation inherited from the ancestor.
16415 Set_Is_Abstract_Subprogram
16416 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16417 end if;
16418 end if;
16420 New_Overloaded_Entity (New_Subp, Derived_Type);
16422 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16423 -- preconditions and the derived type is abstract, the derived operation
16424 -- is abstract as well if parent subprogram is not abstract or null.
16426 if Is_Abstract_Type (Derived_Type)
16427 and then Has_Non_Trivial_Precondition (Parent_Subp)
16428 and then Present (Interfaces (Derived_Type))
16429 then
16431 -- Add useful attributes of subprogram before the freeze point,
16432 -- in case freezing is delayed or there are previous errors.
16434 Set_Is_Dispatching_Operation (New_Subp);
16436 declare
16437 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16439 begin
16440 if Present (Iface_Prim)
16441 and then Has_Non_Trivial_Precondition (Iface_Prim)
16442 then
16443 Set_Is_Abstract_Subprogram (New_Subp);
16444 end if;
16445 end;
16446 end if;
16448 -- Check for case of a derived subprogram for the instantiation of a
16449 -- formal derived tagged type, if so mark the subprogram as dispatching
16450 -- and inherit the dispatching attributes of the actual subprogram. The
16451 -- derived subprogram is effectively renaming of the actual subprogram,
16452 -- so it needs to have the same attributes as the actual.
16454 if Present (Actual_Subp)
16455 and then Is_Dispatching_Operation (Actual_Subp)
16456 then
16457 Set_Is_Dispatching_Operation (New_Subp);
16459 if Present (DTC_Entity (Actual_Subp)) then
16460 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16461 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16462 end if;
16463 end if;
16465 -- Indicate that a derived subprogram does not require a body and that
16466 -- it does not require processing of default expressions.
16468 Set_Has_Completion (New_Subp);
16469 Set_Default_Expressions_Processed (New_Subp);
16471 if Ekind (New_Subp) = E_Function then
16472 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16473 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16474 end if;
16476 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16477 -- primitive subprogram S of a type T, then the aspect is inherited
16478 -- by the corresponding primitive subprogram of each descendant of T.
16480 if Is_Tagged_Type (Derived_Type)
16481 and then Is_Dispatching_Operation (New_Subp)
16482 and then Has_Yield_Aspect (Alias (New_Subp))
16483 then
16484 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16485 end if;
16487 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16488 end Derive_Subprogram;
16490 ------------------------
16491 -- Derive_Subprograms --
16492 ------------------------
16494 procedure Derive_Subprograms
16495 (Parent_Type : Entity_Id;
16496 Derived_Type : Entity_Id;
16497 Generic_Actual : Entity_Id := Empty)
16499 Op_List : constant Elist_Id :=
16500 Collect_Primitive_Operations (Parent_Type);
16502 function Check_Derived_Type return Boolean;
16503 -- Check that all the entities derived from Parent_Type are found in
16504 -- the list of primitives of Derived_Type exactly in the same order.
16506 procedure Derive_Interface_Subprogram
16507 (New_Subp : out Entity_Id;
16508 Subp : Entity_Id;
16509 Actual_Subp : Entity_Id);
16510 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16511 -- (which is an interface primitive). If Generic_Actual is present then
16512 -- Actual_Subp is the actual subprogram corresponding with the generic
16513 -- subprogram Subp.
16515 ------------------------
16516 -- Check_Derived_Type --
16517 ------------------------
16519 function Check_Derived_Type return Boolean is
16520 E : Entity_Id;
16521 Derived_Elmt : Elmt_Id;
16522 Derived_Op : Entity_Id;
16523 Derived_Ops : Elist_Id;
16524 Parent_Elmt : Elmt_Id;
16525 Parent_Op : Entity_Id;
16527 begin
16528 -- Traverse list of entities in the current scope searching for
16529 -- an incomplete type whose full-view is derived type.
16531 E := First_Entity (Scope (Derived_Type));
16532 while Present (E) and then E /= Derived_Type loop
16533 if Ekind (E) = E_Incomplete_Type
16534 and then Present (Full_View (E))
16535 and then Full_View (E) = Derived_Type
16536 then
16537 -- Disable this test if Derived_Type completes an incomplete
16538 -- type because in such case more primitives can be added
16539 -- later to the list of primitives of Derived_Type by routine
16540 -- Process_Incomplete_Dependents.
16542 return True;
16543 end if;
16545 Next_Entity (E);
16546 end loop;
16548 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16550 Derived_Elmt := First_Elmt (Derived_Ops);
16551 Parent_Elmt := First_Elmt (Op_List);
16552 while Present (Parent_Elmt) loop
16553 Parent_Op := Node (Parent_Elmt);
16554 Derived_Op := Node (Derived_Elmt);
16556 -- At this early stage Derived_Type has no entities with attribute
16557 -- Interface_Alias. In addition, such primitives are always
16558 -- located at the end of the list of primitives of Parent_Type.
16559 -- Therefore, if found we can safely stop processing pending
16560 -- entities.
16562 exit when Present (Interface_Alias (Parent_Op));
16564 -- Handle hidden entities
16566 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16567 and then Is_Hidden (Parent_Op)
16568 then
16569 if Present (Derived_Op)
16570 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16571 then
16572 Next_Elmt (Derived_Elmt);
16573 end if;
16575 else
16576 if No (Derived_Op)
16577 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16578 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16579 then
16580 return False;
16581 end if;
16583 Next_Elmt (Derived_Elmt);
16584 end if;
16586 Next_Elmt (Parent_Elmt);
16587 end loop;
16589 return True;
16590 end Check_Derived_Type;
16592 ---------------------------------
16593 -- Derive_Interface_Subprogram --
16594 ---------------------------------
16596 procedure Derive_Interface_Subprogram
16597 (New_Subp : out Entity_Id;
16598 Subp : Entity_Id;
16599 Actual_Subp : Entity_Id)
16601 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16602 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16604 begin
16605 pragma Assert (Is_Interface (Iface_Type));
16607 Derive_Subprogram
16608 (New_Subp => New_Subp,
16609 Parent_Subp => Iface_Subp,
16610 Derived_Type => Derived_Type,
16611 Parent_Type => Iface_Type,
16612 Actual_Subp => Actual_Subp);
16614 -- Given that this new interface entity corresponds with a primitive
16615 -- of the parent that was not overridden we must leave it associated
16616 -- with its parent primitive to ensure that it will share the same
16617 -- dispatch table slot when overridden. We must set the Alias to Subp
16618 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16619 -- (in case we inherited Subp from Iface_Type via a nonabstract
16620 -- generic formal type).
16622 if No (Actual_Subp) then
16623 Set_Alias (New_Subp, Subp);
16625 declare
16626 T : Entity_Id := Find_Dispatching_Type (Subp);
16627 begin
16628 while Etype (T) /= T loop
16629 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16630 Set_Is_Abstract_Subprogram (New_Subp, False);
16631 exit;
16632 end if;
16634 T := Etype (T);
16635 end loop;
16636 end;
16638 -- For instantiations this is not needed since the previous call to
16639 -- Derive_Subprogram leaves the entity well decorated.
16641 else
16642 pragma Assert (Alias (New_Subp) = Actual_Subp);
16643 null;
16644 end if;
16645 end Derive_Interface_Subprogram;
16647 -- Local variables
16649 Alias_Subp : Entity_Id;
16650 Act_List : Elist_Id;
16651 Act_Elmt : Elmt_Id;
16652 Act_Subp : Entity_Id := Empty;
16653 Elmt : Elmt_Id;
16654 Need_Search : Boolean := False;
16655 New_Subp : Entity_Id;
16656 Parent_Base : Entity_Id;
16657 Subp : Entity_Id;
16659 -- Start of processing for Derive_Subprograms
16661 begin
16662 if Ekind (Parent_Type) = E_Record_Type_With_Private
16663 and then Has_Discriminants (Parent_Type)
16664 and then Present (Full_View (Parent_Type))
16665 then
16666 Parent_Base := Full_View (Parent_Type);
16667 else
16668 Parent_Base := Parent_Type;
16669 end if;
16671 if Present (Generic_Actual) then
16672 Act_List := Collect_Primitive_Operations (Generic_Actual);
16673 Act_Elmt := First_Elmt (Act_List);
16674 else
16675 Act_List := No_Elist;
16676 Act_Elmt := No_Elmt;
16677 end if;
16679 -- Derive primitives inherited from the parent. Note that if the generic
16680 -- actual is present, this is not really a type derivation, it is a
16681 -- completion within an instance.
16683 -- Case 1: Derived_Type does not implement interfaces
16685 if not Is_Tagged_Type (Derived_Type)
16686 or else (not Has_Interfaces (Derived_Type)
16687 and then not (Present (Generic_Actual)
16688 and then Has_Interfaces (Generic_Actual)))
16689 then
16690 Elmt := First_Elmt (Op_List);
16691 while Present (Elmt) loop
16692 Subp := Node (Elmt);
16694 -- Literals are derived earlier in the process of building the
16695 -- derived type, and are skipped here.
16697 if Ekind (Subp) = E_Enumeration_Literal then
16698 null;
16700 -- The actual is a direct descendant and the common primitive
16701 -- operations appear in the same order.
16703 -- If the generic parent type is present, the derived type is an
16704 -- instance of a formal derived type, and within the instance its
16705 -- operations are those of the actual. We derive from the formal
16706 -- type but make the inherited operations aliases of the
16707 -- corresponding operations of the actual.
16709 else
16710 pragma Assert (No (Node (Act_Elmt))
16711 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16712 and then
16713 Type_Conformant
16714 (Subp, Node (Act_Elmt),
16715 Skip_Controlling_Formals => True)));
16717 Derive_Subprogram
16718 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16720 if Present (Act_Elmt) then
16721 Next_Elmt (Act_Elmt);
16722 end if;
16723 end if;
16725 Next_Elmt (Elmt);
16726 end loop;
16728 -- Case 2: Derived_Type implements interfaces
16730 else
16731 -- If the parent type has no predefined primitives we remove
16732 -- predefined primitives from the list of primitives of generic
16733 -- actual to simplify the complexity of this algorithm.
16735 if Present (Generic_Actual) then
16736 declare
16737 Has_Predefined_Primitives : Boolean := False;
16739 begin
16740 -- Check if the parent type has predefined primitives
16742 Elmt := First_Elmt (Op_List);
16743 while Present (Elmt) loop
16744 Subp := Node (Elmt);
16746 if Is_Predefined_Dispatching_Operation (Subp)
16747 and then not Comes_From_Source (Ultimate_Alias (Subp))
16748 then
16749 Has_Predefined_Primitives := True;
16750 exit;
16751 end if;
16753 Next_Elmt (Elmt);
16754 end loop;
16756 -- Remove predefined primitives of Generic_Actual. We must use
16757 -- an auxiliary list because in case of tagged types the value
16758 -- returned by Collect_Primitive_Operations is the value stored
16759 -- in its Primitive_Operations attribute (and we don't want to
16760 -- modify its current contents).
16762 if not Has_Predefined_Primitives then
16763 declare
16764 Aux_List : constant Elist_Id := New_Elmt_List;
16766 begin
16767 Elmt := First_Elmt (Act_List);
16768 while Present (Elmt) loop
16769 Subp := Node (Elmt);
16771 if not Is_Predefined_Dispatching_Operation (Subp)
16772 or else Comes_From_Source (Subp)
16773 then
16774 Append_Elmt (Subp, Aux_List);
16775 end if;
16777 Next_Elmt (Elmt);
16778 end loop;
16780 Act_List := Aux_List;
16781 end;
16782 end if;
16784 Act_Elmt := First_Elmt (Act_List);
16785 Act_Subp := Node (Act_Elmt);
16786 end;
16787 end if;
16789 -- Stage 1: If the generic actual is not present we derive the
16790 -- primitives inherited from the parent type. If the generic parent
16791 -- type is present, the derived type is an instance of a formal
16792 -- derived type, and within the instance its operations are those of
16793 -- the actual. We derive from the formal type but make the inherited
16794 -- operations aliases of the corresponding operations of the actual.
16796 Elmt := First_Elmt (Op_List);
16797 while Present (Elmt) loop
16798 Subp := Node (Elmt);
16799 Alias_Subp := Ultimate_Alias (Subp);
16801 -- Do not derive internal entities of the parent that link
16802 -- interface primitives with their covering primitive. These
16803 -- entities will be added to this type when frozen.
16805 if Present (Interface_Alias (Subp)) then
16806 goto Continue;
16807 end if;
16809 -- If the generic actual is present find the corresponding
16810 -- operation in the generic actual. If the parent type is a
16811 -- direct ancestor of the derived type then, even if it is an
16812 -- interface, the operations are inherited from the primary
16813 -- dispatch table and are in the proper order. If we detect here
16814 -- that primitives are not in the same order we traverse the list
16815 -- of primitive operations of the actual to find the one that
16816 -- implements the interface primitive.
16818 if Need_Search
16819 or else
16820 (Present (Generic_Actual)
16821 and then Present (Act_Subp)
16822 and then not
16823 (Primitive_Names_Match (Subp, Act_Subp)
16824 and then
16825 Type_Conformant (Subp, Act_Subp,
16826 Skip_Controlling_Formals => True)))
16827 then
16828 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16829 Use_Full_View => True));
16831 -- Remember that we need searching for all pending primitives
16833 Need_Search := True;
16835 -- Handle entities associated with interface primitives
16837 if Present (Alias_Subp)
16838 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16839 and then not Is_Predefined_Dispatching_Operation (Subp)
16840 then
16841 -- Search for the primitive in the homonym chain
16843 Act_Subp :=
16844 Find_Primitive_Covering_Interface
16845 (Tagged_Type => Generic_Actual,
16846 Iface_Prim => Alias_Subp);
16848 -- Previous search may not locate primitives covering
16849 -- interfaces defined in generics units or instantiations.
16850 -- (it fails if the covering primitive has formals whose
16851 -- type is also defined in generics or instantiations).
16852 -- In such case we search in the list of primitives of the
16853 -- generic actual for the internal entity that links the
16854 -- interface primitive and the covering primitive.
16856 if No (Act_Subp)
16857 and then Is_Generic_Type (Parent_Type)
16858 then
16859 -- This code has been designed to handle only generic
16860 -- formals that implement interfaces that are defined
16861 -- in a generic unit or instantiation. If this code is
16862 -- needed for other cases we must review it because
16863 -- (given that it relies on Original_Location to locate
16864 -- the primitive of Generic_Actual that covers the
16865 -- interface) it could leave linked through attribute
16866 -- Alias entities of unrelated instantiations).
16868 pragma Assert
16869 (Is_Generic_Unit
16870 (Scope (Find_Dispatching_Type (Alias_Subp)))
16871 or else
16872 Instantiation_Location
16873 (Sloc (Find_Dispatching_Type (Alias_Subp)))
16874 /= No_Location);
16875 declare
16876 Iface_Prim_Loc : constant Source_Ptr :=
16877 Original_Location (Sloc (Alias_Subp));
16879 Elmt : Elmt_Id;
16880 Prim : Entity_Id;
16882 begin
16883 Elmt :=
16884 First_Elmt (Primitive_Operations (Generic_Actual));
16886 Search : while Present (Elmt) loop
16887 Prim := Node (Elmt);
16889 if Present (Interface_Alias (Prim))
16890 and then Original_Location
16891 (Sloc (Interface_Alias (Prim))) =
16892 Iface_Prim_Loc
16893 then
16894 Act_Subp := Alias (Prim);
16895 exit Search;
16896 end if;
16898 Next_Elmt (Elmt);
16899 end loop Search;
16900 end;
16901 end if;
16903 pragma Assert (Present (Act_Subp)
16904 or else Is_Abstract_Type (Generic_Actual)
16905 or else Serious_Errors_Detected > 0);
16907 -- Handle predefined primitives plus the rest of user-defined
16908 -- primitives
16910 else
16911 Act_Elmt := First_Elmt (Act_List);
16912 while Present (Act_Elmt) loop
16913 Act_Subp := Node (Act_Elmt);
16915 exit when Primitive_Names_Match (Subp, Act_Subp)
16916 and then Type_Conformant
16917 (Subp, Act_Subp,
16918 Skip_Controlling_Formals => True)
16919 and then No (Interface_Alias (Act_Subp));
16921 Next_Elmt (Act_Elmt);
16922 end loop;
16924 if No (Act_Elmt) then
16925 Act_Subp := Empty;
16926 end if;
16927 end if;
16928 end if;
16930 -- Case 1: If the parent is a limited interface then it has the
16931 -- predefined primitives of synchronized interfaces. However, the
16932 -- actual type may be a non-limited type and hence it does not
16933 -- have such primitives.
16935 if Present (Generic_Actual)
16936 and then No (Act_Subp)
16937 and then Is_Limited_Interface (Parent_Base)
16938 and then Is_Predefined_Interface_Primitive (Subp)
16939 then
16940 null;
16942 -- Case 2: Inherit entities associated with interfaces that were
16943 -- not covered by the parent type. We exclude here null interface
16944 -- primitives because they do not need special management.
16946 -- We also exclude interface operations that are renamings. If the
16947 -- subprogram is an explicit renaming of an interface primitive,
16948 -- it is a regular primitive operation, and the presence of its
16949 -- alias is not relevant: it has to be derived like any other
16950 -- primitive.
16952 elsif Present (Alias (Subp))
16953 and then Nkind (Unit_Declaration_Node (Subp)) /=
16954 N_Subprogram_Renaming_Declaration
16955 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16956 and then not
16957 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16958 and then Null_Present (Parent (Alias_Subp)))
16959 then
16960 -- If this is an abstract private type then we transfer the
16961 -- derivation of the interface primitive from the partial view
16962 -- to the full view. This is safe because all the interfaces
16963 -- must be visible in the partial view. Done to avoid adding
16964 -- a new interface derivation to the private part of the
16965 -- enclosing package; otherwise this new derivation would be
16966 -- decorated as hidden when the analysis of the enclosing
16967 -- package completes.
16969 if Is_Abstract_Type (Derived_Type)
16970 and then In_Private_Part (Current_Scope)
16971 and then Has_Private_Declaration (Derived_Type)
16972 then
16973 declare
16974 Partial_View : Entity_Id;
16975 Elmt : Elmt_Id;
16976 Ent : Entity_Id;
16978 begin
16979 Partial_View := First_Entity (Current_Scope);
16980 loop
16981 exit when No (Partial_View)
16982 or else (Has_Private_Declaration (Partial_View)
16983 and then
16984 Full_View (Partial_View) = Derived_Type);
16986 Next_Entity (Partial_View);
16987 end loop;
16989 -- If the partial view was not found then the source code
16990 -- has errors and the derivation is not needed.
16992 if Present (Partial_View) then
16993 Elmt :=
16994 First_Elmt (Primitive_Operations (Partial_View));
16995 while Present (Elmt) loop
16996 Ent := Node (Elmt);
16998 if Present (Alias (Ent))
16999 and then Ultimate_Alias (Ent) = Alias (Subp)
17000 then
17001 Append_Elmt
17002 (Ent, Primitive_Operations (Derived_Type));
17003 exit;
17004 end if;
17006 Next_Elmt (Elmt);
17007 end loop;
17009 -- If the interface primitive was not found in the
17010 -- partial view then this interface primitive was
17011 -- overridden. We add a derivation to activate in
17012 -- Derive_Progenitor_Subprograms the machinery to
17013 -- search for it.
17015 if No (Elmt) then
17016 Derive_Interface_Subprogram
17017 (New_Subp => New_Subp,
17018 Subp => Subp,
17019 Actual_Subp => Act_Subp);
17020 end if;
17021 end if;
17022 end;
17023 else
17024 Derive_Interface_Subprogram
17025 (New_Subp => New_Subp,
17026 Subp => Subp,
17027 Actual_Subp => Act_Subp);
17028 end if;
17030 -- Case 3: Common derivation
17032 else
17033 Derive_Subprogram
17034 (New_Subp => New_Subp,
17035 Parent_Subp => Subp,
17036 Derived_Type => Derived_Type,
17037 Parent_Type => Parent_Base,
17038 Actual_Subp => Act_Subp);
17039 end if;
17041 -- No need to update Act_Elm if we must search for the
17042 -- corresponding operation in the generic actual
17044 if not Need_Search
17045 and then Present (Act_Elmt)
17046 then
17047 Next_Elmt (Act_Elmt);
17048 Act_Subp := Node (Act_Elmt);
17049 end if;
17051 <<Continue>>
17052 Next_Elmt (Elmt);
17053 end loop;
17055 -- Inherit additional operations from progenitors. If the derived
17056 -- type is a generic actual, there are not new primitive operations
17057 -- for the type because it has those of the actual, and therefore
17058 -- nothing needs to be done. The renamings generated above are not
17059 -- primitive operations, and their purpose is simply to make the
17060 -- proper operations visible within an instantiation.
17062 if No (Generic_Actual) then
17063 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17064 end if;
17065 end if;
17067 -- Final check: Direct descendants must have their primitives in the
17068 -- same order. We exclude from this test untagged types and instances
17069 -- of formal derived types. We skip this test if we have already
17070 -- reported serious errors in the sources.
17072 pragma Assert (not Is_Tagged_Type (Derived_Type)
17073 or else Present (Generic_Actual)
17074 or else Serious_Errors_Detected > 0
17075 or else Check_Derived_Type);
17076 end Derive_Subprograms;
17078 --------------------------------
17079 -- Derived_Standard_Character --
17080 --------------------------------
17082 procedure Derived_Standard_Character
17083 (N : Node_Id;
17084 Parent_Type : Entity_Id;
17085 Derived_Type : Entity_Id)
17087 Loc : constant Source_Ptr := Sloc (N);
17088 Def : constant Node_Id := Type_Definition (N);
17089 Indic : constant Node_Id := Subtype_Indication (Def);
17090 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17091 Implicit_Base : constant Entity_Id :=
17092 Create_Itype
17093 (E_Enumeration_Type, N, Derived_Type, 'B');
17095 Lo : Node_Id;
17096 Hi : Node_Id;
17098 begin
17099 Discard_Node (Process_Subtype (Indic, N));
17101 Set_Etype (Implicit_Base, Parent_Base);
17102 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17103 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17105 Set_Is_Character_Type (Implicit_Base, True);
17106 Set_Has_Delayed_Freeze (Implicit_Base);
17108 -- The bounds of the implicit base are the bounds of the parent base.
17109 -- Note that their type is the parent base.
17111 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17112 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17114 Set_Scalar_Range (Implicit_Base,
17115 Make_Range (Loc,
17116 Low_Bound => Lo,
17117 High_Bound => Hi));
17119 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17120 Set_Etype (Derived_Type, Implicit_Base);
17121 Set_Size_Info (Derived_Type, Parent_Type);
17123 if not Known_RM_Size (Derived_Type) then
17124 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17125 end if;
17127 Set_Is_Character_Type (Derived_Type, True);
17129 if Nkind (Indic) /= N_Subtype_Indication then
17131 -- If no explicit constraint, the bounds are those
17132 -- of the parent type.
17134 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17135 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17136 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17137 end if;
17139 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17140 end Derived_Standard_Character;
17142 ------------------------------
17143 -- Derived_Type_Declaration --
17144 ------------------------------
17146 procedure Derived_Type_Declaration
17147 (T : Entity_Id;
17148 N : Node_Id;
17149 Is_Completion : Boolean)
17151 Parent_Type : Entity_Id;
17153 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17154 -- Check whether the parent type is a generic formal, or derives
17155 -- directly or indirectly from one.
17157 ------------------------
17158 -- Comes_From_Generic --
17159 ------------------------
17161 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17162 begin
17163 if Is_Generic_Type (Typ) then
17164 return True;
17166 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17167 return True;
17169 elsif Is_Private_Type (Typ)
17170 and then Present (Full_View (Typ))
17171 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17172 then
17173 return True;
17175 elsif Is_Generic_Actual_Type (Typ) then
17176 return True;
17178 else
17179 return False;
17180 end if;
17181 end Comes_From_Generic;
17183 -- Local variables
17185 Def : constant Node_Id := Type_Definition (N);
17186 Iface_Def : Node_Id;
17187 Indic : constant Node_Id := Subtype_Indication (Def);
17188 Extension : constant Node_Id := Record_Extension_Part (Def);
17189 Parent_Node : Node_Id;
17190 Taggd : Boolean;
17192 -- Start of processing for Derived_Type_Declaration
17194 begin
17195 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17197 if SPARK_Mode = On
17198 and then Is_Tagged_Type (Parent_Type)
17199 then
17200 declare
17201 Partial_View : constant Entity_Id :=
17202 Incomplete_Or_Partial_View (Parent_Type);
17204 begin
17205 -- If the partial view was not found then the parent type is not
17206 -- a private type. Otherwise check if the partial view is a tagged
17207 -- private type.
17209 if Present (Partial_View)
17210 and then Is_Private_Type (Partial_View)
17211 and then not Is_Tagged_Type (Partial_View)
17212 then
17213 Error_Msg_NE
17214 ("cannot derive from & declared as untagged private "
17215 & "(SPARK RM 3.4(1))", N, Partial_View);
17216 end if;
17217 end;
17218 end if;
17220 -- Ada 2005 (AI-251): In case of interface derivation check that the
17221 -- parent is also an interface.
17223 if Interface_Present (Def) then
17224 if not Is_Interface (Parent_Type) then
17225 Diagnose_Interface (Indic, Parent_Type);
17227 else
17228 Parent_Node := Parent (Base_Type (Parent_Type));
17229 Iface_Def := Type_Definition (Parent_Node);
17231 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17232 -- other limited interfaces.
17234 if Limited_Present (Def) then
17235 if Limited_Present (Iface_Def) then
17236 null;
17238 elsif Protected_Present (Iface_Def) then
17239 Error_Msg_NE
17240 ("descendant of & must be declared as a protected "
17241 & "interface", N, Parent_Type);
17243 elsif Synchronized_Present (Iface_Def) then
17244 Error_Msg_NE
17245 ("descendant of & must be declared as a synchronized "
17246 & "interface", N, Parent_Type);
17248 elsif Task_Present (Iface_Def) then
17249 Error_Msg_NE
17250 ("descendant of & must be declared as a task interface",
17251 N, Parent_Type);
17253 else
17254 Error_Msg_N
17255 ("(Ada 2005) limited interface cannot inherit from "
17256 & "non-limited interface", Indic);
17257 end if;
17259 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17260 -- from non-limited or limited interfaces.
17262 elsif not Protected_Present (Def)
17263 and then not Synchronized_Present (Def)
17264 and then not Task_Present (Def)
17265 then
17266 if Limited_Present (Iface_Def) then
17267 null;
17269 elsif Protected_Present (Iface_Def) then
17270 Error_Msg_NE
17271 ("descendant of & must be declared as a protected "
17272 & "interface", N, Parent_Type);
17274 elsif Synchronized_Present (Iface_Def) then
17275 Error_Msg_NE
17276 ("descendant of & must be declared as a synchronized "
17277 & "interface", N, Parent_Type);
17279 elsif Task_Present (Iface_Def) then
17280 Error_Msg_NE
17281 ("descendant of & must be declared as a task interface",
17282 N, Parent_Type);
17283 else
17284 null;
17285 end if;
17286 end if;
17287 end if;
17288 end if;
17290 if Is_Tagged_Type (Parent_Type)
17291 and then Is_Concurrent_Type (Parent_Type)
17292 and then not Is_Interface (Parent_Type)
17293 then
17294 Error_Msg_N
17295 ("parent type of a record extension cannot be a synchronized "
17296 & "tagged type (RM 3.9.1 (3/1))", N);
17297 Set_Etype (T, Any_Type);
17298 return;
17299 end if;
17301 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17302 -- interfaces
17304 if Is_Tagged_Type (Parent_Type)
17305 and then Is_Non_Empty_List (Interface_List (Def))
17306 then
17307 declare
17308 Intf : Node_Id;
17309 T : Entity_Id;
17311 begin
17312 Intf := First (Interface_List (Def));
17313 while Present (Intf) loop
17314 T := Find_Type_Of_Subtype_Indic (Intf);
17316 if not Is_Interface (T) then
17317 Diagnose_Interface (Intf, T);
17319 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17320 -- a limited type from having a nonlimited progenitor.
17322 elsif (Limited_Present (Def)
17323 or else (not Is_Interface (Parent_Type)
17324 and then Is_Limited_Type (Parent_Type)))
17325 and then not Is_Limited_Interface (T)
17326 then
17327 Error_Msg_NE
17328 ("progenitor interface& of limited type must be limited",
17329 N, T);
17330 end if;
17332 Next (Intf);
17333 end loop;
17334 end;
17336 -- Check consistency of any nonoverridable aspects that are
17337 -- inherited from multiple sources.
17339 Check_Inherited_Nonoverridable_Aspects
17340 (Inheritor => T,
17341 Interface_List => Interface_List (Def),
17342 Parent_Type => Parent_Type);
17343 end if;
17345 if Parent_Type = Any_Type
17346 or else Etype (Parent_Type) = Any_Type
17347 or else (Is_Class_Wide_Type (Parent_Type)
17348 and then Etype (Parent_Type) = T)
17349 then
17350 -- If Parent_Type is undefined or illegal, make new type into a
17351 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17352 -- errors. If this is a self-definition, emit error now.
17354 if T = Parent_Type or else T = Etype (Parent_Type) then
17355 Error_Msg_N ("type cannot be used in its own definition", Indic);
17356 end if;
17358 Mutate_Ekind (T, Ekind (Parent_Type));
17359 Set_Etype (T, Any_Type);
17360 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17362 -- Initialize the list of primitive operations to an empty list,
17363 -- to cover tagged types as well as untagged types. For untagged
17364 -- types this is used either to analyze the call as legal when
17365 -- Extensions_Allowed is True, or to issue a better error message
17366 -- otherwise.
17368 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17370 return;
17371 end if;
17373 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17374 -- an interface is special because the list of interfaces in the full
17375 -- view can be given in any order. For example:
17377 -- type A is interface;
17378 -- type B is interface and A;
17379 -- type D is new B with private;
17380 -- private
17381 -- type D is new A and B with null record; -- 1 --
17383 -- In this case we perform the following transformation of -1-:
17385 -- type D is new B and A with null record;
17387 -- If the parent of the full-view covers the parent of the partial-view
17388 -- we have two possible cases:
17390 -- 1) They have the same parent
17391 -- 2) The parent of the full-view implements some further interfaces
17393 -- In both cases we do not need to perform the transformation. In the
17394 -- first case the source program is correct and the transformation is
17395 -- not needed; in the second case the source program does not fulfill
17396 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17397 -- later.
17399 -- This transformation not only simplifies the rest of the analysis of
17400 -- this type declaration but also simplifies the correct generation of
17401 -- the object layout to the expander.
17403 if In_Private_Part (Current_Scope)
17404 and then Is_Interface (Parent_Type)
17405 then
17406 declare
17407 Partial_View : Entity_Id;
17408 Partial_View_Parent : Entity_Id;
17410 function Reorder_Interfaces return Boolean;
17411 -- Look for an interface in the full view's interface list that
17412 -- matches the parent type of the partial view, and when found,
17413 -- rewrite the full view's parent with the partial view's parent,
17414 -- append the full view's original parent to the interface list,
17415 -- recursively call Derived_Type_Definition on the full type, and
17416 -- return True. If a match is not found, return False.
17418 ------------------------
17419 -- Reorder_Interfaces --
17420 ------------------------
17422 function Reorder_Interfaces return Boolean is
17423 Iface : Node_Id;
17424 New_Iface : Node_Id;
17426 begin
17427 Iface := First (Interface_List (Def));
17428 while Present (Iface) loop
17429 if Etype (Iface) = Etype (Partial_View) then
17430 Rewrite (Subtype_Indication (Def),
17431 New_Copy (Subtype_Indication (Parent (Partial_View))));
17433 New_Iface :=
17434 Make_Identifier (Sloc (N), Chars (Parent_Type));
17435 Rewrite (Iface, New_Iface);
17437 -- Analyze the transformed code
17439 Derived_Type_Declaration (T, N, Is_Completion);
17440 return True;
17441 end if;
17443 Next (Iface);
17444 end loop;
17445 return False;
17446 end Reorder_Interfaces;
17448 begin
17449 -- Look for the associated private type declaration
17451 Partial_View := Incomplete_Or_Partial_View (T);
17453 -- If the partial view was not found then the source code has
17454 -- errors and the transformation is not needed.
17456 if Present (Partial_View) then
17457 Partial_View_Parent := Etype (Partial_View);
17459 -- If the parent of the full-view covers the parent of the
17460 -- partial-view we have nothing else to do.
17462 if Interface_Present_In_Ancestor
17463 (Parent_Type, Partial_View_Parent)
17464 then
17465 null;
17467 -- Traverse the list of interfaces of the full view to look
17468 -- for the parent of the partial view and reorder the
17469 -- interfaces to match the order in the partial view,
17470 -- if needed.
17472 else
17474 if Reorder_Interfaces then
17475 -- Having the interfaces listed in any order is legal.
17476 -- However, the compiler does not properly handle
17477 -- different orders between partial and full views in
17478 -- generic units. We give a warning about the order
17479 -- mismatch, so the user can work around this problem.
17481 Error_Msg_N ("??full declaration does not respect " &
17482 "partial declaration order", T);
17483 Error_Msg_N ("\??consider reordering", T);
17485 return;
17486 end if;
17487 end if;
17488 end if;
17489 end;
17490 end if;
17492 -- Only composite types other than array types are allowed to have
17493 -- discriminants.
17495 if Present (Discriminant_Specifications (N)) then
17496 if (Is_Elementary_Type (Parent_Type)
17497 or else
17498 Is_Array_Type (Parent_Type))
17499 and then not Error_Posted (N)
17500 then
17501 Error_Msg_N
17502 ("elementary or array type cannot have discriminants",
17503 Defining_Identifier (First (Discriminant_Specifications (N))));
17505 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17506 -- only if we are not already processing a malformed syntax tree.
17508 if Is_Type (T) then
17509 Set_Has_Discriminants (T, False);
17510 end if;
17511 end if;
17512 end if;
17514 -- In Ada 83, a derived type defined in a package specification cannot
17515 -- be used for further derivation until the end of its visible part.
17516 -- Note that derivation in the private part of the package is allowed.
17518 if Ada_Version = Ada_83
17519 and then Is_Derived_Type (Parent_Type)
17520 and then In_Visible_Part (Scope (Parent_Type))
17521 then
17522 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17523 Error_Msg_N
17524 ("(Ada 83) premature use of type for derivation", Indic);
17525 end if;
17526 end if;
17528 -- Check for early use of incomplete or private type
17530 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17531 Error_Msg_N ("premature derivation of incomplete type", Indic);
17532 return;
17534 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17535 and then not Comes_From_Generic (Parent_Type))
17536 or else Has_Private_Component (Parent_Type)
17537 then
17538 -- The ancestor type of a formal type can be incomplete, in which
17539 -- case only the operations of the partial view are available in the
17540 -- generic. Subsequent checks may be required when the full view is
17541 -- analyzed to verify that a derivation from a tagged type has an
17542 -- extension.
17544 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17545 null;
17547 elsif No (Underlying_Type (Parent_Type))
17548 or else Has_Private_Component (Parent_Type)
17549 then
17550 Error_Msg_N
17551 ("premature derivation of derived or private type", Indic);
17553 -- Flag the type itself as being in error, this prevents some
17554 -- nasty problems with subsequent uses of the malformed type.
17556 Set_Error_Posted (T);
17558 -- Check that within the immediate scope of an untagged partial
17559 -- view it's illegal to derive from the partial view if the
17560 -- full view is tagged. (7.3(7))
17562 -- We verify that the Parent_Type is a partial view by checking
17563 -- that it is not a Full_Type_Declaration (i.e. a private type or
17564 -- private extension declaration), to distinguish a partial view
17565 -- from a derivation from a private type which also appears as
17566 -- E_Private_Type. If the parent base type is not declared in an
17567 -- enclosing scope there is no need to check.
17569 elsif Present (Full_View (Parent_Type))
17570 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17571 and then not Is_Tagged_Type (Parent_Type)
17572 and then Is_Tagged_Type (Full_View (Parent_Type))
17573 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17574 then
17575 Error_Msg_N
17576 ("premature derivation from type with tagged full view",
17577 Indic);
17578 end if;
17579 end if;
17581 -- Check that form of derivation is appropriate
17583 Taggd := Is_Tagged_Type (Parent_Type);
17585 -- Set the parent type to the class-wide type's specific type in this
17586 -- case to prevent cascading errors
17588 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17589 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17590 Set_Etype (T, Etype (Parent_Type));
17591 return;
17592 end if;
17594 if Present (Extension) and then not Taggd then
17595 Error_Msg_N
17596 ("type derived from untagged type cannot have extension", Indic);
17598 elsif No (Extension) and then Taggd then
17600 -- If this declaration is within a private part (or body) of a
17601 -- generic instantiation then the derivation is allowed (the parent
17602 -- type can only appear tagged in this case if it's a generic actual
17603 -- type, since it would otherwise have been rejected in the analysis
17604 -- of the generic template).
17606 if not Is_Generic_Actual_Type (Parent_Type)
17607 or else In_Visible_Part (Scope (Parent_Type))
17608 then
17609 if Is_Class_Wide_Type (Parent_Type) then
17610 Error_Msg_N
17611 ("parent type must not be a class-wide type", Indic);
17613 -- Use specific type to prevent cascaded errors.
17615 Parent_Type := Etype (Parent_Type);
17617 else
17618 Error_Msg_N
17619 ("type derived from tagged type must have extension", Indic);
17620 end if;
17621 end if;
17622 end if;
17624 -- AI-443: Synchronized formal derived types require a private
17625 -- extension. There is no point in checking the ancestor type or
17626 -- the progenitors since the construct is wrong to begin with.
17628 if Ada_Version >= Ada_2005
17629 and then Is_Generic_Type (T)
17630 and then Present (Original_Node (N))
17631 then
17632 declare
17633 Decl : constant Node_Id := Original_Node (N);
17635 begin
17636 if Nkind (Decl) = N_Formal_Type_Declaration
17637 and then Nkind (Formal_Type_Definition (Decl)) =
17638 N_Formal_Derived_Type_Definition
17639 and then Synchronized_Present (Formal_Type_Definition (Decl))
17640 and then No (Extension)
17642 -- Avoid emitting a duplicate error message
17644 and then not Error_Posted (Indic)
17645 then
17646 Error_Msg_N
17647 ("synchronized derived type must have extension", N);
17648 end if;
17649 end;
17650 end if;
17652 if Null_Exclusion_Present (Def)
17653 and then not Is_Access_Type (Parent_Type)
17654 then
17655 Error_Msg_N ("null exclusion can only apply to an access type", N);
17656 end if;
17658 Check_Wide_Character_Restriction (Parent_Type, Indic);
17660 -- Avoid deriving parent primitives of underlying record views
17662 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17663 Derive_Subps => not Is_Underlying_Record_View (T));
17665 -- AI-419: The parent type of an explicitly limited derived type must
17666 -- be a limited type or a limited interface.
17668 if Limited_Present (Def) then
17669 Set_Is_Limited_Record (T);
17671 if Is_Interface (T) then
17672 Set_Is_Limited_Interface (T);
17673 end if;
17675 if not Is_Limited_Type (Parent_Type)
17676 and then
17677 (not Is_Interface (Parent_Type)
17678 or else not Is_Limited_Interface (Parent_Type))
17679 then
17680 -- AI05-0096: a derivation in the private part of an instance is
17681 -- legal if the generic formal is untagged limited, and the actual
17682 -- is non-limited.
17684 if Is_Generic_Actual_Type (Parent_Type)
17685 and then In_Private_Part (Current_Scope)
17686 and then
17687 not Is_Tagged_Type
17688 (Generic_Parent_Type (Parent (Parent_Type)))
17689 then
17690 null;
17692 else
17693 Error_Msg_NE
17694 ("parent type& of limited type must be limited",
17695 N, Parent_Type);
17696 end if;
17697 end if;
17698 end if;
17699 end Derived_Type_Declaration;
17701 ------------------------
17702 -- Diagnose_Interface --
17703 ------------------------
17705 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17706 begin
17707 if not Is_Interface (E) and then E /= Any_Type then
17708 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17709 end if;
17710 end Diagnose_Interface;
17712 ----------------------------------
17713 -- Enumeration_Type_Declaration --
17714 ----------------------------------
17716 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17717 Ev : Uint;
17718 L : Node_Id;
17719 R_Node : Node_Id;
17720 B_Node : Node_Id;
17722 begin
17723 -- Create identifier node representing lower bound
17725 B_Node := New_Node (N_Identifier, Sloc (Def));
17726 L := First (Literals (Def));
17727 Set_Chars (B_Node, Chars (L));
17728 Set_Entity (B_Node, L);
17729 Set_Etype (B_Node, T);
17730 Set_Is_Static_Expression (B_Node, True);
17732 R_Node := New_Node (N_Range, Sloc (Def));
17733 Set_Low_Bound (R_Node, B_Node);
17735 Mutate_Ekind (T, E_Enumeration_Type);
17736 Set_First_Literal (T, L);
17737 Set_Etype (T, T);
17738 Set_Is_Constrained (T);
17740 Ev := Uint_0;
17742 -- Loop through literals of enumeration type setting pos and rep values
17743 -- except that if the Ekind is already set, then it means the literal
17744 -- was already constructed (case of a derived type declaration and we
17745 -- should not disturb the Pos and Rep values.
17747 while Present (L) loop
17748 if Ekind (L) /= E_Enumeration_Literal then
17749 Mutate_Ekind (L, E_Enumeration_Literal);
17750 Set_Enumeration_Pos (L, Ev);
17751 Set_Enumeration_Rep (L, Ev);
17752 Set_Is_Known_Valid (L, True);
17753 end if;
17755 Set_Etype (L, T);
17756 New_Overloaded_Entity (L);
17757 Generate_Definition (L);
17758 Set_Convention (L, Convention_Intrinsic);
17760 -- Case of character literal
17762 if Nkind (L) = N_Defining_Character_Literal then
17763 Set_Is_Character_Type (T, True);
17765 -- Check violation of No_Wide_Characters
17767 if Restriction_Check_Required (No_Wide_Characters) then
17768 Get_Name_String (Chars (L));
17770 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17771 Check_Restriction (No_Wide_Characters, L);
17772 end if;
17773 end if;
17774 end if;
17776 Ev := Ev + 1;
17777 Next (L);
17778 end loop;
17780 -- Now create a node representing upper bound
17782 B_Node := New_Node (N_Identifier, Sloc (Def));
17783 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17784 Set_Entity (B_Node, Last (Literals (Def)));
17785 Set_Etype (B_Node, T);
17786 Set_Is_Static_Expression (B_Node, True);
17788 Set_High_Bound (R_Node, B_Node);
17790 -- Initialize various fields of the type. Some of this information
17791 -- may be overwritten later through rep. clauses.
17793 Set_Scalar_Range (T, R_Node);
17794 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17795 Set_Enum_Esize (T);
17796 Set_Enum_Pos_To_Rep (T, Empty);
17798 -- Set Discard_Names if configuration pragma set, or if there is
17799 -- a parameterless pragma in the current declarative region
17801 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17802 Set_Discard_Names (T);
17803 end if;
17805 -- Process end label if there is one
17807 if Present (Def) then
17808 Process_End_Label (Def, 'e', T);
17809 end if;
17810 end Enumeration_Type_Declaration;
17812 ---------------------------------
17813 -- Expand_To_Stored_Constraint --
17814 ---------------------------------
17816 function Expand_To_Stored_Constraint
17817 (Typ : Entity_Id;
17818 Constraint : Elist_Id) return Elist_Id
17820 Explicitly_Discriminated_Type : Entity_Id;
17821 Expansion : Elist_Id;
17822 Discriminant : Entity_Id;
17824 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17825 -- Find the nearest type that actually specifies discriminants
17827 ---------------------------------
17828 -- Type_With_Explicit_Discrims --
17829 ---------------------------------
17831 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17832 Typ : constant E := Base_Type (Id);
17834 begin
17835 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17836 if Present (Full_View (Typ)) then
17837 return Type_With_Explicit_Discrims (Full_View (Typ));
17838 end if;
17840 else
17841 if Has_Discriminants (Typ) then
17842 return Typ;
17843 end if;
17844 end if;
17846 if Etype (Typ) = Typ then
17847 return Empty;
17848 elsif Has_Discriminants (Typ) then
17849 return Typ;
17850 else
17851 return Type_With_Explicit_Discrims (Etype (Typ));
17852 end if;
17854 end Type_With_Explicit_Discrims;
17856 -- Start of processing for Expand_To_Stored_Constraint
17858 begin
17859 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17860 return No_Elist;
17861 end if;
17863 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17865 if No (Explicitly_Discriminated_Type) then
17866 return No_Elist;
17867 end if;
17869 Expansion := New_Elmt_List;
17871 Discriminant :=
17872 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17873 while Present (Discriminant) loop
17874 Append_Elmt
17875 (Get_Discriminant_Value
17876 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17877 To => Expansion);
17878 Next_Stored_Discriminant (Discriminant);
17879 end loop;
17881 return Expansion;
17882 end Expand_To_Stored_Constraint;
17884 ---------------------------
17885 -- Find_Hidden_Interface --
17886 ---------------------------
17888 function Find_Hidden_Interface
17889 (Src : Elist_Id;
17890 Dest : Elist_Id) return Entity_Id
17892 Iface : Entity_Id;
17893 Iface_Elmt : Elmt_Id;
17895 begin
17896 if Present (Src) and then Present (Dest) then
17897 Iface_Elmt := First_Elmt (Src);
17898 while Present (Iface_Elmt) loop
17899 Iface := Node (Iface_Elmt);
17901 if Is_Interface (Iface)
17902 and then not Contain_Interface (Iface, Dest)
17903 then
17904 return Iface;
17905 end if;
17907 Next_Elmt (Iface_Elmt);
17908 end loop;
17909 end if;
17911 return Empty;
17912 end Find_Hidden_Interface;
17914 --------------------
17915 -- Find_Type_Name --
17916 --------------------
17918 function Find_Type_Name (N : Node_Id) return Entity_Id is
17919 Id : constant Entity_Id := Defining_Identifier (N);
17920 New_Id : Entity_Id;
17921 Prev : Entity_Id;
17922 Prev_Par : Node_Id;
17924 procedure Check_Duplicate_Aspects;
17925 -- Check that aspects specified in a completion have not been specified
17926 -- already in the partial view.
17928 procedure Tag_Mismatch;
17929 -- Diagnose a tagged partial view whose full view is untagged. We post
17930 -- the message on the full view, with a reference to the previous
17931 -- partial view. The partial view can be private or incomplete, and
17932 -- these are handled in a different manner, so we determine the position
17933 -- of the error message from the respective slocs of both.
17935 -----------------------------
17936 -- Check_Duplicate_Aspects --
17937 -----------------------------
17939 procedure Check_Duplicate_Aspects is
17940 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17941 -- Return the corresponding aspect of the partial view which matches
17942 -- the aspect id of Asp. Return Empty is no such aspect exists.
17944 -----------------------------
17945 -- Get_Partial_View_Aspect --
17946 -----------------------------
17948 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17949 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17950 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17951 Prev_Asp : Node_Id;
17953 begin
17954 if Present (Prev_Asps) then
17955 Prev_Asp := First (Prev_Asps);
17956 while Present (Prev_Asp) loop
17957 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17958 return Prev_Asp;
17959 end if;
17961 Next (Prev_Asp);
17962 end loop;
17963 end if;
17965 return Empty;
17966 end Get_Partial_View_Aspect;
17968 -- Local variables
17970 Full_Asps : constant List_Id := Aspect_Specifications (N);
17971 Full_Asp : Node_Id;
17972 Part_Asp : Node_Id;
17974 -- Start of processing for Check_Duplicate_Aspects
17976 begin
17977 if Present (Full_Asps) then
17978 Full_Asp := First (Full_Asps);
17979 while Present (Full_Asp) loop
17980 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17982 -- An aspect and its class-wide counterpart are two distinct
17983 -- aspects and may apply to both views of an entity.
17985 if Present (Part_Asp)
17986 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17987 then
17988 Error_Msg_N
17989 ("aspect already specified in private declaration",
17990 Full_Asp);
17992 Remove (Full_Asp);
17993 return;
17994 end if;
17996 if Has_Discriminants (Prev)
17997 and then not Has_Unknown_Discriminants (Prev)
17998 and then Get_Aspect_Id (Full_Asp) =
17999 Aspect_Implicit_Dereference
18000 then
18001 Error_Msg_N
18002 ("cannot specify aspect if partial view has known "
18003 & "discriminants", Full_Asp);
18004 end if;
18006 Next (Full_Asp);
18007 end loop;
18008 end if;
18009 end Check_Duplicate_Aspects;
18011 ------------------
18012 -- Tag_Mismatch --
18013 ------------------
18015 procedure Tag_Mismatch is
18016 begin
18017 if Sloc (Prev) < Sloc (Id) then
18018 if Ada_Version >= Ada_2012
18019 and then Nkind (N) = N_Private_Type_Declaration
18020 then
18021 Error_Msg_NE
18022 ("declaration of private } must be a tagged type", Id, Prev);
18023 else
18024 Error_Msg_NE
18025 ("full declaration of } must be a tagged type", Id, Prev);
18026 end if;
18028 else
18029 if Ada_Version >= Ada_2012
18030 and then Nkind (N) = N_Private_Type_Declaration
18031 then
18032 Error_Msg_NE
18033 ("declaration of private } must be a tagged type", Prev, Id);
18034 else
18035 Error_Msg_NE
18036 ("full declaration of } must be a tagged type", Prev, Id);
18037 end if;
18038 end if;
18039 end Tag_Mismatch;
18041 -- Start of processing for Find_Type_Name
18043 begin
18044 -- Find incomplete declaration, if one was given
18046 Prev := Current_Entity_In_Scope (Id);
18048 -- New type declaration
18050 if No (Prev) then
18051 Enter_Name (Id);
18052 return Id;
18054 -- Previous declaration exists
18056 else
18057 Prev_Par := Parent (Prev);
18059 -- Error if not incomplete/private case except if previous
18060 -- declaration is implicit, etc. Enter_Name will emit error if
18061 -- appropriate.
18063 if not Is_Incomplete_Or_Private_Type (Prev) then
18064 Enter_Name (Id);
18065 New_Id := Id;
18067 -- Check invalid completion of private or incomplete type
18069 elsif Nkind (N) not in N_Full_Type_Declaration
18070 | N_Task_Type_Declaration
18071 | N_Protected_Type_Declaration
18072 and then
18073 (Ada_Version < Ada_2012
18074 or else not Is_Incomplete_Type (Prev)
18075 or else Nkind (N) not in N_Private_Type_Declaration
18076 | N_Private_Extension_Declaration)
18077 then
18078 -- Completion must be a full type declarations (RM 7.3(4))
18080 Error_Msg_Sloc := Sloc (Prev);
18081 Error_Msg_NE ("invalid completion of }", Id, Prev);
18083 -- Set scope of Id to avoid cascaded errors. Entity is never
18084 -- examined again, except when saving globals in generics.
18086 Set_Scope (Id, Current_Scope);
18087 New_Id := Id;
18089 -- If this is a repeated incomplete declaration, no further
18090 -- checks are possible.
18092 if Nkind (N) = N_Incomplete_Type_Declaration then
18093 return Prev;
18094 end if;
18096 -- Case of full declaration of incomplete type
18098 elsif Ekind (Prev) = E_Incomplete_Type
18099 and then (Ada_Version < Ada_2012
18100 or else No (Full_View (Prev))
18101 or else not Is_Private_Type (Full_View (Prev)))
18102 then
18103 -- Indicate that the incomplete declaration has a matching full
18104 -- declaration. The defining occurrence of the incomplete
18105 -- declaration remains the visible one, and the procedure
18106 -- Get_Full_View dereferences it whenever the type is used.
18108 if Present (Full_View (Prev)) then
18109 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18110 end if;
18112 Set_Full_View (Prev, Id);
18113 Append_Entity (Id, Current_Scope);
18114 Set_Is_Public (Id, Is_Public (Prev));
18115 Set_Is_Internal (Id);
18116 New_Id := Prev;
18118 -- If the incomplete view is tagged, a class_wide type has been
18119 -- created already. Use it for the private type as well, in order
18120 -- to prevent multiple incompatible class-wide types that may be
18121 -- created for self-referential anonymous access components.
18123 if Is_Tagged_Type (Prev)
18124 and then Present (Class_Wide_Type (Prev))
18125 then
18126 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18127 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18129 -- Type of the class-wide type is the current Id. Previously
18130 -- this was not done for private declarations because of order-
18131 -- of-elaboration issues in the back end, but gigi now handles
18132 -- this properly.
18134 Set_Etype (Class_Wide_Type (Id), Id);
18135 end if;
18137 -- Case of full declaration of private type
18139 else
18140 -- If the private type was a completion of an incomplete type then
18141 -- update Prev to reference the private type
18143 if Ada_Version >= Ada_2012
18144 and then Ekind (Prev) = E_Incomplete_Type
18145 and then Present (Full_View (Prev))
18146 and then Is_Private_Type (Full_View (Prev))
18147 then
18148 Prev := Full_View (Prev);
18149 Prev_Par := Parent (Prev);
18150 end if;
18152 if Nkind (N) = N_Full_Type_Declaration
18153 and then Nkind (Type_Definition (N)) in
18154 N_Record_Definition | N_Derived_Type_Definition
18155 and then Interface_Present (Type_Definition (N))
18156 then
18157 Error_Msg_N
18158 ("completion of private type cannot be an interface", N);
18159 end if;
18161 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18162 if Etype (Prev) /= Prev then
18164 -- Prev is a private subtype or a derived type, and needs
18165 -- no completion.
18167 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18168 New_Id := Id;
18170 elsif Ekind (Prev) = E_Private_Type
18171 and then Nkind (N) in N_Task_Type_Declaration
18172 | N_Protected_Type_Declaration
18173 then
18174 Error_Msg_N
18175 ("completion of nonlimited type cannot be limited", N);
18177 elsif Ekind (Prev) = E_Record_Type_With_Private
18178 and then Nkind (N) in N_Task_Type_Declaration
18179 | N_Protected_Type_Declaration
18180 then
18181 if not Is_Limited_Record (Prev) then
18182 Error_Msg_N
18183 ("completion of nonlimited type cannot be limited", N);
18185 elsif No (Interface_List (N)) then
18186 Error_Msg_N
18187 ("completion of tagged private type must be tagged",
18189 end if;
18190 end if;
18192 -- Ada 2005 (AI-251): Private extension declaration of a task
18193 -- type or a protected type. This case arises when covering
18194 -- interface types.
18196 elsif Nkind (N) in N_Task_Type_Declaration
18197 | N_Protected_Type_Declaration
18198 then
18199 null;
18201 elsif Nkind (N) /= N_Full_Type_Declaration
18202 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18203 then
18204 Error_Msg_N
18205 ("full view of private extension must be an extension", N);
18207 elsif not (Abstract_Present (Parent (Prev)))
18208 and then Abstract_Present (Type_Definition (N))
18209 then
18210 Error_Msg_N
18211 ("full view of non-abstract extension cannot be abstract", N);
18212 end if;
18214 if not In_Private_Part (Current_Scope) then
18215 Error_Msg_N
18216 ("declaration of full view must appear in private part", N);
18217 end if;
18219 if Ada_Version >= Ada_2012 then
18220 Check_Duplicate_Aspects;
18221 end if;
18223 Copy_And_Swap (Prev, Id);
18224 Set_Has_Private_Declaration (Prev);
18225 Set_Has_Private_Declaration (Id);
18227 -- AI12-0133: Indicate whether we have a partial view with
18228 -- unknown discriminants, in which case initialization of objects
18229 -- of the type do not receive an invariant check.
18231 Set_Partial_View_Has_Unknown_Discr
18232 (Prev, Has_Unknown_Discriminants (Id));
18234 -- Preserve aspect and iterator flags that may have been set on
18235 -- the partial view.
18237 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18238 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18240 -- If no error, propagate freeze_node from private to full view.
18241 -- It may have been generated for an early operational item.
18243 if Present (Freeze_Node (Id))
18244 and then Serious_Errors_Detected = 0
18245 and then No (Full_View (Id))
18246 then
18247 Set_Freeze_Node (Prev, Freeze_Node (Id));
18248 Set_Freeze_Node (Id, Empty);
18249 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18250 end if;
18252 Set_Full_View (Id, Prev);
18253 New_Id := Prev;
18254 end if;
18256 -- Verify that full declaration conforms to partial one
18258 if Is_Incomplete_Or_Private_Type (Prev)
18259 and then Present (Discriminant_Specifications (Prev_Par))
18260 then
18261 if Present (Discriminant_Specifications (N)) then
18262 if Ekind (Prev) = E_Incomplete_Type then
18263 Check_Discriminant_Conformance (N, Prev, Prev);
18264 else
18265 Check_Discriminant_Conformance (N, Prev, Id);
18266 end if;
18268 else
18269 Error_Msg_N
18270 ("missing discriminants in full type declaration", N);
18272 -- To avoid cascaded errors on subsequent use, share the
18273 -- discriminants of the partial view.
18275 Set_Discriminant_Specifications (N,
18276 Discriminant_Specifications (Prev_Par));
18277 end if;
18278 end if;
18280 -- A prior untagged partial view can have an associated class-wide
18281 -- type due to use of the class attribute, and in this case the full
18282 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18283 -- of incomplete tagged declarations, but we check for it.
18285 if Is_Type (Prev)
18286 and then (Is_Tagged_Type (Prev)
18287 or else Present (Class_Wide_Type (Prev)))
18288 then
18289 -- Ada 2012 (AI05-0162): A private type may be the completion of
18290 -- an incomplete type.
18292 if Ada_Version >= Ada_2012
18293 and then Is_Incomplete_Type (Prev)
18294 and then Nkind (N) in N_Private_Type_Declaration
18295 | N_Private_Extension_Declaration
18296 then
18297 -- No need to check private extensions since they are tagged
18299 if Nkind (N) = N_Private_Type_Declaration
18300 and then not Tagged_Present (N)
18301 then
18302 Tag_Mismatch;
18303 end if;
18305 -- The full declaration is either a tagged type (including
18306 -- a synchronized type that implements interfaces) or a
18307 -- type extension, otherwise this is an error.
18309 elsif Nkind (N) in N_Task_Type_Declaration
18310 | N_Protected_Type_Declaration
18311 then
18312 if No (Interface_List (N)) and then not Error_Posted (N) then
18313 Tag_Mismatch;
18314 end if;
18316 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18318 -- Indicate that the previous declaration (tagged incomplete
18319 -- or private declaration) requires the same on the full one.
18321 if not Tagged_Present (Type_Definition (N)) then
18322 Tag_Mismatch;
18323 Set_Is_Tagged_Type (Id);
18324 end if;
18326 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18327 if No (Record_Extension_Part (Type_Definition (N))) then
18328 Error_Msg_NE
18329 ("full declaration of } must be a record extension",
18330 Prev, Id);
18332 -- Set some attributes to produce a usable full view
18334 Set_Is_Tagged_Type (Id);
18335 end if;
18337 else
18338 Tag_Mismatch;
18339 end if;
18340 end if;
18342 if Present (Prev)
18343 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18344 and then Present (Premature_Use (Parent (Prev)))
18345 then
18346 Error_Msg_Sloc := Sloc (N);
18347 Error_Msg_N
18348 ("\full declaration #", Premature_Use (Parent (Prev)));
18349 end if;
18351 return New_Id;
18352 end if;
18353 end Find_Type_Name;
18355 -------------------------
18356 -- Find_Type_Of_Object --
18357 -------------------------
18359 function Find_Type_Of_Object
18360 (Obj_Def : Node_Id;
18361 Related_Nod : Node_Id) return Entity_Id
18363 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18364 P : Node_Id := Parent (Obj_Def);
18365 T : Entity_Id;
18366 Nam : Name_Id;
18368 begin
18369 -- If the parent is a component_definition node we climb to the
18370 -- component_declaration node.
18372 if Nkind (P) = N_Component_Definition then
18373 P := Parent (P);
18374 end if;
18376 -- Case of an anonymous array subtype
18378 if Def_Kind in N_Array_Type_Definition then
18379 T := Empty;
18380 Array_Type_Declaration (T, Obj_Def);
18382 -- Create an explicit subtype whenever possible
18384 elsif Nkind (P) /= N_Component_Declaration
18385 and then Def_Kind = N_Subtype_Indication
18386 then
18387 -- Base name of subtype on object name, which will be unique in
18388 -- the current scope.
18390 -- If this is a duplicate declaration, return base type, to avoid
18391 -- generating duplicate anonymous types.
18393 if Error_Posted (P) then
18394 Analyze (Subtype_Mark (Obj_Def));
18395 return Entity (Subtype_Mark (Obj_Def));
18396 end if;
18398 Nam :=
18399 New_External_Name
18400 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18402 T := Make_Defining_Identifier (Sloc (P), Nam);
18404 -- If In_Spec_Expression, for example within a pre/postcondition,
18405 -- provide enough information for use of the subtype without
18406 -- depending on full analysis and freezing, which will happen when
18407 -- building the corresponding subprogram.
18409 if In_Spec_Expression then
18410 Analyze (Subtype_Mark (Obj_Def));
18412 declare
18413 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18414 Decl : constant Node_Id :=
18415 Make_Subtype_Declaration (Sloc (P),
18416 Defining_Identifier => T,
18417 Subtype_Indication => Relocate_Node (Obj_Def));
18418 begin
18419 Set_Etype (T, Base_T);
18420 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18421 Set_Parent (T, Obj_Def);
18422 Set_Scope (T, Current_Scope);
18424 if Ekind (T) = E_Array_Subtype then
18425 Constrain_Array (T, Obj_Def, Related_Nod, T, 'P');
18427 elsif Ekind (T) = E_Record_Subtype then
18428 Set_First_Entity (T, First_Entity (Base_T));
18429 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18430 Set_Is_Constrained (T);
18431 end if;
18433 Insert_Before (Related_Nod, Decl);
18434 end;
18436 return T;
18437 end if;
18439 -- When generating code, insert subtype declaration ahead of
18440 -- declaration that generated it.
18442 Insert_Action (Obj_Def,
18443 Make_Subtype_Declaration (Sloc (P),
18444 Defining_Identifier => T,
18445 Subtype_Indication => Relocate_Node (Obj_Def)));
18447 -- This subtype may need freezing, and this will not be done
18448 -- automatically if the object declaration is not in declarative
18449 -- part. Since this is an object declaration, the type cannot always
18450 -- be frozen here. Deferred constants do not freeze their type
18451 -- (which often enough will be private).
18453 if Nkind (P) = N_Object_Declaration
18454 and then Constant_Present (P)
18455 and then No (Expression (P))
18456 then
18457 null;
18459 -- Here we freeze the base type of object type to catch premature use
18460 -- of discriminated private type without a full view.
18462 else
18463 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18464 end if;
18466 -- Ada 2005 AI-406: the object definition in an object declaration
18467 -- can be an access definition.
18469 elsif Def_Kind = N_Access_Definition then
18470 T := Access_Definition (Related_Nod, Obj_Def);
18472 Set_Is_Local_Anonymous_Access
18473 (T, Ada_Version < Ada_2012
18474 or else Nkind (P) /= N_Object_Declaration
18475 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18477 -- Otherwise, the object definition is just a subtype_mark
18479 else
18480 T := Process_Subtype (Obj_Def, Related_Nod);
18481 end if;
18483 return T;
18484 end Find_Type_Of_Object;
18486 --------------------------------
18487 -- Find_Type_Of_Subtype_Indic --
18488 --------------------------------
18490 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18491 Typ : Entity_Id;
18493 begin
18494 -- Case of subtype mark with a constraint
18496 if Nkind (S) = N_Subtype_Indication then
18497 Find_Type (Subtype_Mark (S));
18498 Typ := Entity (Subtype_Mark (S));
18500 if not
18501 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18502 then
18503 Error_Msg_N
18504 ("incorrect constraint for this kind of type", Constraint (S));
18505 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18506 end if;
18508 -- Otherwise we have a subtype mark without a constraint
18510 elsif Error_Posted (S) then
18511 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18512 return Any_Type;
18514 else
18515 Find_Type (S);
18516 Typ := Entity (S);
18517 end if;
18519 return Typ;
18520 end Find_Type_Of_Subtype_Indic;
18522 -------------------------------------
18523 -- Floating_Point_Type_Declaration --
18524 -------------------------------------
18526 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18527 Digs : constant Node_Id := Digits_Expression (Def);
18528 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18529 Digs_Val : Uint;
18530 Base_Typ : Entity_Id;
18531 Implicit_Base : Entity_Id;
18533 function Can_Derive_From (E : Entity_Id) return Boolean;
18534 -- Find if given digits value, and possibly a specified range, allows
18535 -- derivation from specified type
18537 procedure Convert_Bound (B : Node_Id);
18538 -- If specified, the bounds must be static but may be of different
18539 -- types. They must be converted into machine numbers of the base type,
18540 -- in accordance with RM 4.9(38).
18542 function Find_Base_Type return Entity_Id;
18543 -- Find a predefined base type that Def can derive from, or generate
18544 -- an error and substitute Long_Long_Float if none exists.
18546 ---------------------
18547 -- Can_Derive_From --
18548 ---------------------
18550 function Can_Derive_From (E : Entity_Id) return Boolean is
18551 Spec : constant Entity_Id := Real_Range_Specification (Def);
18553 begin
18554 -- Check specified "digits" constraint
18556 if Digs_Val > Digits_Value (E) then
18557 return False;
18558 end if;
18560 -- Check for matching range, if specified
18562 if Present (Spec) then
18563 if Expr_Value_R (Type_Low_Bound (E)) >
18564 Expr_Value_R (Low_Bound (Spec))
18565 then
18566 return False;
18567 end if;
18569 if Expr_Value_R (Type_High_Bound (E)) <
18570 Expr_Value_R (High_Bound (Spec))
18571 then
18572 return False;
18573 end if;
18574 end if;
18576 return True;
18577 end Can_Derive_From;
18579 -------------------
18580 -- Convert_Bound --
18581 --------------------
18583 procedure Convert_Bound (B : Node_Id) is
18584 begin
18585 -- If the bound is not a literal it can only be static if it is
18586 -- a static constant, possibly of a specified type.
18588 if Is_Entity_Name (B)
18589 and then Ekind (Entity (B)) = E_Constant
18590 then
18591 Rewrite (B, Constant_Value (Entity (B)));
18592 end if;
18594 if Nkind (B) = N_Real_Literal then
18595 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18596 Set_Is_Machine_Number (B);
18597 Set_Etype (B, Base_Typ);
18598 end if;
18599 end Convert_Bound;
18601 --------------------
18602 -- Find_Base_Type --
18603 --------------------
18605 function Find_Base_Type return Entity_Id is
18606 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18608 begin
18609 -- Iterate over the predefined types in order, returning the first
18610 -- one that Def can derive from.
18612 while Present (Choice) loop
18613 if Can_Derive_From (Node (Choice)) then
18614 return Node (Choice);
18615 end if;
18617 Next_Elmt (Choice);
18618 end loop;
18620 -- If we can't derive from any existing type, use Long_Long_Float
18621 -- and give appropriate message explaining the problem.
18623 if Digs_Val > Max_Digs_Val then
18624 -- It might be the case that there is a type with the requested
18625 -- range, just not the combination of digits and range.
18627 Error_Msg_N
18628 ("no predefined type has requested range and precision",
18629 Real_Range_Specification (Def));
18631 else
18632 Error_Msg_N
18633 ("range too large for any predefined type",
18634 Real_Range_Specification (Def));
18635 end if;
18637 return Standard_Long_Long_Float;
18638 end Find_Base_Type;
18640 -- Start of processing for Floating_Point_Type_Declaration
18642 begin
18643 Check_Restriction (No_Floating_Point, Def);
18645 -- Create an implicit base type
18647 Implicit_Base :=
18648 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18650 -- Analyze and verify digits value
18652 Analyze_And_Resolve (Digs, Any_Integer);
18653 Check_Digits_Expression (Digs);
18654 Digs_Val := Expr_Value (Digs);
18656 -- Process possible range spec and find correct type to derive from
18658 Process_Real_Range_Specification (Def);
18660 -- Check that requested number of digits is not too high.
18662 if Digs_Val > Max_Digs_Val then
18664 -- The check for Max_Base_Digits may be somewhat expensive, as it
18665 -- requires reading System, so only do it when necessary.
18667 declare
18668 Max_Base_Digits : constant Uint :=
18669 Expr_Value
18670 (Expression
18671 (Parent (RTE (RE_Max_Base_Digits))));
18673 begin
18674 if Digs_Val > Max_Base_Digits then
18675 Error_Msg_Uint_1 := Max_Base_Digits;
18676 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18678 elsif No (Real_Range_Specification (Def)) then
18679 Error_Msg_Uint_1 := Max_Digs_Val;
18680 Error_Msg_N ("types with more than ^ digits need range spec "
18681 & "(RM 3.5.7(6))", Digs);
18682 end if;
18683 end;
18684 end if;
18686 -- Find a suitable type to derive from or complain and use a substitute
18688 Base_Typ := Find_Base_Type;
18690 -- If there are bounds given in the declaration use them as the bounds
18691 -- of the type, otherwise use the bounds of the predefined base type
18692 -- that was chosen based on the Digits value.
18694 if Present (Real_Range_Specification (Def)) then
18695 Set_Scalar_Range (T, Real_Range_Specification (Def));
18696 Set_Is_Constrained (T);
18698 Convert_Bound (Type_Low_Bound (T));
18699 Convert_Bound (Type_High_Bound (T));
18701 else
18702 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18703 end if;
18705 -- Complete definition of implicit base and declared first subtype. The
18706 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18707 -- are not clobbered when the floating point type acts as a full view of
18708 -- a private type.
18710 Set_Etype (Implicit_Base, Base_Typ);
18711 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18712 Set_Size_Info (Implicit_Base, Base_Typ);
18713 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18714 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18715 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18716 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18718 Mutate_Ekind (T, E_Floating_Point_Subtype);
18719 Set_Etype (T, Implicit_Base);
18720 Set_Size_Info (T, Implicit_Base);
18721 Set_RM_Size (T, RM_Size (Implicit_Base));
18722 Inherit_Rep_Item_Chain (T, Implicit_Base);
18724 if Digs_Val >= Uint_1 then
18725 Set_Digits_Value (T, Digs_Val);
18726 else
18727 pragma Assert (Serious_Errors_Detected > 0); null;
18728 end if;
18729 end Floating_Point_Type_Declaration;
18731 ----------------------------
18732 -- Get_Discriminant_Value --
18733 ----------------------------
18735 -- This is the situation:
18737 -- There is a non-derived type
18739 -- type T0 (Dx, Dy, Dz...)
18741 -- There are zero or more levels of derivation, with each derivation
18742 -- either purely inheriting the discriminants, or defining its own.
18744 -- type Ti is new Ti-1
18745 -- or
18746 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18747 -- or
18748 -- subtype Ti is ...
18750 -- The subtype issue is avoided by the use of Original_Record_Component,
18751 -- and the fact that derived subtypes also derive the constraints.
18753 -- This chain leads back from
18755 -- Typ_For_Constraint
18757 -- Typ_For_Constraint has discriminants, and the value for each
18758 -- discriminant is given by its corresponding Elmt of Constraints.
18760 -- Discriminant is some discriminant in this hierarchy
18762 -- We need to return its value
18764 -- We do this by recursively searching each level, and looking for
18765 -- Discriminant. Once we get to the bottom, we start backing up
18766 -- returning the value for it which may in turn be a discriminant
18767 -- further up, so on the backup we continue the substitution.
18769 function Get_Discriminant_Value
18770 (Discriminant : Entity_Id;
18771 Typ_For_Constraint : Entity_Id;
18772 Constraint : Elist_Id) return Node_Id
18774 function Root_Corresponding_Discriminant
18775 (Discr : Entity_Id) return Entity_Id;
18776 -- Given a discriminant, traverse the chain of inherited discriminants
18777 -- and return the topmost discriminant.
18779 function Search_Derivation_Levels
18780 (Ti : Entity_Id;
18781 Discrim_Values : Elist_Id;
18782 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18783 -- This is the routine that performs the recursive search of levels
18784 -- as described above.
18786 -------------------------------------
18787 -- Root_Corresponding_Discriminant --
18788 -------------------------------------
18790 function Root_Corresponding_Discriminant
18791 (Discr : Entity_Id) return Entity_Id
18793 D : Entity_Id;
18795 begin
18796 D := Discr;
18797 while Present (Corresponding_Discriminant (D)) loop
18798 D := Corresponding_Discriminant (D);
18799 end loop;
18801 return D;
18802 end Root_Corresponding_Discriminant;
18804 ------------------------------
18805 -- Search_Derivation_Levels --
18806 ------------------------------
18808 function Search_Derivation_Levels
18809 (Ti : Entity_Id;
18810 Discrim_Values : Elist_Id;
18811 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18813 Assoc : Elmt_Id;
18814 Disc : Entity_Id;
18815 Result : Node_Or_Entity_Id;
18816 Result_Entity : Node_Id;
18818 begin
18819 -- If inappropriate type, return Error, this happens only in
18820 -- cascaded error situations, and we want to avoid a blow up.
18822 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18823 return Error;
18824 end if;
18826 -- Look deeper if possible. Use Stored_Constraints only for
18827 -- untagged types. For tagged types use the given constraint.
18828 -- This asymmetry needs explanation???
18830 if not Stored_Discrim_Values
18831 and then Present (Stored_Constraint (Ti))
18832 and then not Is_Tagged_Type (Ti)
18833 then
18834 Result :=
18835 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18837 else
18838 declare
18839 Td : Entity_Id := Etype (Ti);
18841 begin
18842 -- If the parent type is private, the full view may include
18843 -- renamed discriminants, and it is those stored values that
18844 -- may be needed (the partial view never has more information
18845 -- than the full view).
18847 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18848 Td := Full_View (Td);
18849 end if;
18851 if Td = Ti then
18852 Result := Discriminant;
18854 else
18855 if Present (Stored_Constraint (Ti)) then
18856 Result :=
18857 Search_Derivation_Levels
18858 (Td, Stored_Constraint (Ti), True);
18859 else
18860 Result :=
18861 Search_Derivation_Levels
18862 (Td, Discrim_Values, Stored_Discrim_Values);
18863 end if;
18864 end if;
18865 end;
18866 end if;
18868 -- Extra underlying places to search, if not found above. For
18869 -- concurrent types, the relevant discriminant appears in the
18870 -- corresponding record. For a type derived from a private type
18871 -- without discriminant, the full view inherits the discriminants
18872 -- of the full view of the parent.
18874 if Result = Discriminant then
18875 if Is_Concurrent_Type (Ti)
18876 and then Present (Corresponding_Record_Type (Ti))
18877 then
18878 Result :=
18879 Search_Derivation_Levels (
18880 Corresponding_Record_Type (Ti),
18881 Discrim_Values,
18882 Stored_Discrim_Values);
18884 elsif Is_Private_Type (Ti)
18885 and then not Has_Discriminants (Ti)
18886 and then Present (Full_View (Ti))
18887 and then Etype (Full_View (Ti)) /= Ti
18888 then
18889 Result :=
18890 Search_Derivation_Levels (
18891 Full_View (Ti),
18892 Discrim_Values,
18893 Stored_Discrim_Values);
18894 end if;
18895 end if;
18897 -- If Result is not a (reference to a) discriminant, return it,
18898 -- otherwise set Result_Entity to the discriminant.
18900 if Nkind (Result) = N_Defining_Identifier then
18901 pragma Assert (Result = Discriminant);
18902 Result_Entity := Result;
18904 else
18905 if not Denotes_Discriminant (Result) then
18906 return Result;
18907 end if;
18909 Result_Entity := Entity (Result);
18910 end if;
18912 -- See if this level of derivation actually has discriminants because
18913 -- tagged derivations can add them, hence the lower levels need not
18914 -- have any.
18916 if not Has_Discriminants (Ti) then
18917 return Result;
18918 end if;
18920 -- Scan Ti's discriminants for Result_Entity, and return its
18921 -- corresponding value, if any.
18923 Result_Entity := Original_Record_Component (Result_Entity);
18925 Assoc := First_Elmt (Discrim_Values);
18927 if Stored_Discrim_Values then
18928 Disc := First_Stored_Discriminant (Ti);
18929 else
18930 Disc := First_Discriminant (Ti);
18931 end if;
18933 while Present (Disc) loop
18935 -- If no further associations return the discriminant, value will
18936 -- be found on the second pass.
18938 if No (Assoc) then
18939 return Result;
18940 end if;
18942 if Original_Record_Component (Disc) = Result_Entity then
18943 return Node (Assoc);
18944 end if;
18946 Next_Elmt (Assoc);
18948 if Stored_Discrim_Values then
18949 Next_Stored_Discriminant (Disc);
18950 else
18951 Next_Discriminant (Disc);
18952 end if;
18953 end loop;
18955 -- Could not find it
18957 return Result;
18958 end Search_Derivation_Levels;
18960 -- Local Variables
18962 Result : Node_Or_Entity_Id;
18964 -- Start of processing for Get_Discriminant_Value
18966 begin
18967 -- ??? This routine is a gigantic mess and will be deleted. For the
18968 -- time being just test for the trivial case before calling recurse.
18970 -- We are now celebrating the 20th anniversary of this comment!
18972 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18973 declare
18974 D : Entity_Id;
18975 E : Elmt_Id;
18977 begin
18978 D := First_Discriminant (Typ_For_Constraint);
18979 E := First_Elmt (Constraint);
18980 while Present (D) loop
18981 if Chars (D) = Chars (Discriminant) then
18982 return Node (E);
18983 end if;
18985 Next_Discriminant (D);
18986 Next_Elmt (E);
18987 end loop;
18988 end;
18989 end if;
18991 Result := Search_Derivation_Levels
18992 (Typ_For_Constraint, Constraint, False);
18994 -- ??? hack to disappear when this routine is gone
18996 if Nkind (Result) = N_Defining_Identifier then
18997 declare
18998 D : Entity_Id;
18999 E : Elmt_Id;
19001 begin
19002 D := First_Discriminant (Typ_For_Constraint);
19003 E := First_Elmt (Constraint);
19004 while Present (D) loop
19005 if Root_Corresponding_Discriminant (D) = Discriminant then
19006 return Node (E);
19007 end if;
19009 Next_Discriminant (D);
19010 Next_Elmt (E);
19011 end loop;
19012 end;
19013 end if;
19015 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19016 return Result;
19017 end Get_Discriminant_Value;
19019 --------------------------
19020 -- Has_Range_Constraint --
19021 --------------------------
19023 function Has_Range_Constraint (N : Node_Id) return Boolean is
19024 C : constant Node_Id := Constraint (N);
19026 begin
19027 if Nkind (C) = N_Range_Constraint then
19028 return True;
19030 elsif Nkind (C) = N_Digits_Constraint then
19031 return
19032 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19033 or else Present (Range_Constraint (C));
19035 elsif Nkind (C) = N_Delta_Constraint then
19036 return Present (Range_Constraint (C));
19038 else
19039 return False;
19040 end if;
19041 end Has_Range_Constraint;
19043 ------------------------
19044 -- Inherit_Components --
19045 ------------------------
19047 function Inherit_Components
19048 (N : Node_Id;
19049 Parent_Base : Entity_Id;
19050 Derived_Base : Entity_Id;
19051 Is_Tagged : Boolean;
19052 Inherit_Discr : Boolean;
19053 Discs : Elist_Id) return Elist_Id
19055 Assoc_List : constant Elist_Id := New_Elmt_List;
19057 procedure Inherit_Component
19058 (Old_C : Entity_Id;
19059 Plain_Discrim : Boolean := False;
19060 Stored_Discrim : Boolean := False);
19061 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19062 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19063 -- True, Old_C is a stored discriminant. If they are both false then
19064 -- Old_C is a regular component.
19066 -----------------------
19067 -- Inherit_Component --
19068 -----------------------
19070 procedure Inherit_Component
19071 (Old_C : Entity_Id;
19072 Plain_Discrim : Boolean := False;
19073 Stored_Discrim : Boolean := False)
19075 procedure Set_Anonymous_Type (Id : Entity_Id);
19076 -- Id denotes the entity of an access discriminant or anonymous
19077 -- access component. Set the type of Id to either the same type of
19078 -- Old_C or create a new one depending on whether the parent and
19079 -- the child types are in the same scope.
19081 ------------------------
19082 -- Set_Anonymous_Type --
19083 ------------------------
19085 procedure Set_Anonymous_Type (Id : Entity_Id) is
19086 Old_Typ : constant Entity_Id := Etype (Old_C);
19088 begin
19089 if Scope (Parent_Base) = Scope (Derived_Base) then
19090 Set_Etype (Id, Old_Typ);
19092 -- The parent and the derived type are in two different scopes.
19093 -- Reuse the type of the original discriminant / component by
19094 -- copying it in order to preserve all attributes.
19096 else
19097 declare
19098 Typ : constant Entity_Id := New_Copy (Old_Typ);
19100 begin
19101 Set_Etype (Id, Typ);
19103 -- Since we do not generate component declarations for
19104 -- inherited components, associate the itype with the
19105 -- derived type.
19107 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19108 Set_Scope (Typ, Derived_Base);
19109 end;
19110 end if;
19111 end Set_Anonymous_Type;
19113 -- Local variables and constants
19115 New_C : constant Entity_Id := New_Copy (Old_C);
19117 Corr_Discrim : Entity_Id;
19118 Discrim : Entity_Id;
19120 -- Start of processing for Inherit_Component
19122 begin
19123 pragma Assert (not Is_Tagged or not Stored_Discrim);
19125 Set_Parent (New_C, Parent (Old_C));
19127 -- Regular discriminants and components must be inserted in the scope
19128 -- of the Derived_Base. Do it here.
19130 if not Stored_Discrim then
19131 Enter_Name (New_C);
19132 end if;
19134 -- For tagged types the Original_Record_Component must point to
19135 -- whatever this field was pointing to in the parent type. This has
19136 -- already been achieved by the call to New_Copy above.
19138 if not Is_Tagged then
19139 Set_Original_Record_Component (New_C, New_C);
19140 Set_Corresponding_Record_Component (New_C, Old_C);
19141 end if;
19143 -- Set the proper type of an access discriminant
19145 if Ekind (New_C) = E_Discriminant
19146 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19147 then
19148 Set_Anonymous_Type (New_C);
19149 end if;
19151 -- If we have inherited a component then see if its Etype contains
19152 -- references to Parent_Base discriminants. In this case, replace
19153 -- these references with the constraints given in Discs. We do not
19154 -- do this for the partial view of private types because this is
19155 -- not needed (only the components of the full view will be used
19156 -- for code generation) and cause problem. We also avoid this
19157 -- transformation in some error situations.
19159 if Ekind (New_C) = E_Component then
19161 -- Set the proper type of an anonymous access component
19163 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19164 Set_Anonymous_Type (New_C);
19166 elsif (Is_Private_Type (Derived_Base)
19167 and then not Is_Generic_Type (Derived_Base))
19168 or else (Is_Empty_Elmt_List (Discs)
19169 and then not Expander_Active)
19170 then
19171 Set_Etype (New_C, Etype (Old_C));
19173 else
19174 -- The current component introduces a circularity of the
19175 -- following kind:
19177 -- limited with Pack_2;
19178 -- package Pack_1 is
19179 -- type T_1 is tagged record
19180 -- Comp : access Pack_2.T_2;
19181 -- ...
19182 -- end record;
19183 -- end Pack_1;
19185 -- with Pack_1;
19186 -- package Pack_2 is
19187 -- type T_2 is new Pack_1.T_1 with ...;
19188 -- end Pack_2;
19190 Set_Etype
19191 (New_C,
19192 Constrain_Component_Type
19193 (Old_C, Derived_Base, N, Parent_Base, Discs));
19194 end if;
19195 end if;
19197 -- In derived tagged types it is illegal to reference a non
19198 -- discriminant component in the parent type. To catch this, mark
19199 -- these components with an Ekind of E_Void. This will be reset in
19200 -- Record_Type_Definition after processing the record extension of
19201 -- the derived type.
19203 -- If the declaration is a private extension, there is no further
19204 -- record extension to process, and the components retain their
19205 -- current kind, because they are visible at this point.
19207 if Is_Tagged and then Ekind (New_C) = E_Component
19208 and then Nkind (N) /= N_Private_Extension_Declaration
19209 then
19210 Mutate_Ekind (New_C, E_Void);
19211 end if;
19213 if Plain_Discrim then
19214 Set_Corresponding_Discriminant (New_C, Old_C);
19215 Build_Discriminal (New_C);
19217 -- If we are explicitly inheriting a stored discriminant it will be
19218 -- completely hidden.
19220 elsif Stored_Discrim then
19221 Set_Corresponding_Discriminant (New_C, Empty);
19222 Set_Discriminal (New_C, Empty);
19223 Set_Is_Completely_Hidden (New_C);
19225 -- Set the Original_Record_Component of each discriminant in the
19226 -- derived base to point to the corresponding stored that we just
19227 -- created.
19229 Discrim := First_Discriminant (Derived_Base);
19230 while Present (Discrim) loop
19231 Corr_Discrim := Corresponding_Discriminant (Discrim);
19233 -- Corr_Discrim could be missing in an error situation
19235 if Present (Corr_Discrim)
19236 and then Original_Record_Component (Corr_Discrim) = Old_C
19237 then
19238 Set_Original_Record_Component (Discrim, New_C);
19239 Set_Corresponding_Record_Component (Discrim, Empty);
19240 end if;
19242 Next_Discriminant (Discrim);
19243 end loop;
19245 Append_Entity (New_C, Derived_Base);
19246 end if;
19248 if not Is_Tagged then
19249 Append_Elmt (Old_C, Assoc_List);
19250 Append_Elmt (New_C, Assoc_List);
19251 end if;
19252 end Inherit_Component;
19254 -- Variables local to Inherit_Component
19256 Loc : constant Source_Ptr := Sloc (N);
19258 Parent_Discrim : Entity_Id;
19259 Stored_Discrim : Entity_Id;
19260 D : Entity_Id;
19261 Component : Entity_Id;
19263 -- Start of processing for Inherit_Components
19265 begin
19266 if not Is_Tagged then
19267 Append_Elmt (Parent_Base, Assoc_List);
19268 Append_Elmt (Derived_Base, Assoc_List);
19269 end if;
19271 -- Inherit parent discriminants if needed
19273 if Inherit_Discr then
19274 Parent_Discrim := First_Discriminant (Parent_Base);
19275 while Present (Parent_Discrim) loop
19276 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19277 Next_Discriminant (Parent_Discrim);
19278 end loop;
19279 end if;
19281 -- Create explicit stored discrims for untagged types when necessary
19283 if not Has_Unknown_Discriminants (Derived_Base)
19284 and then Has_Discriminants (Parent_Base)
19285 and then not Is_Tagged
19286 and then
19287 (not Inherit_Discr
19288 or else First_Discriminant (Parent_Base) /=
19289 First_Stored_Discriminant (Parent_Base))
19290 then
19291 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19292 while Present (Stored_Discrim) loop
19293 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19294 Next_Stored_Discriminant (Stored_Discrim);
19295 end loop;
19296 end if;
19298 -- See if we can apply the second transformation for derived types, as
19299 -- explained in point 6. in the comments above Build_Derived_Record_Type
19300 -- This is achieved by appending Derived_Base discriminants into Discs,
19301 -- which has the side effect of returning a non empty Discs list to the
19302 -- caller of Inherit_Components, which is what we want. This must be
19303 -- done for private derived types if there are explicit stored
19304 -- discriminants, to ensure that we can retrieve the values of the
19305 -- constraints provided in the ancestors.
19307 if Inherit_Discr
19308 and then Is_Empty_Elmt_List (Discs)
19309 and then Present (First_Discriminant (Derived_Base))
19310 and then
19311 (not Is_Private_Type (Derived_Base)
19312 or else Is_Completely_Hidden
19313 (First_Stored_Discriminant (Derived_Base))
19314 or else Is_Generic_Type (Derived_Base))
19315 then
19316 D := First_Discriminant (Derived_Base);
19317 while Present (D) loop
19318 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19319 Next_Discriminant (D);
19320 end loop;
19321 end if;
19323 -- Finally, inherit non-discriminant components unless they are not
19324 -- visible because defined or inherited from the full view of the
19325 -- parent. Don't inherit the _parent field of the parent type.
19327 Component := First_Entity (Parent_Base);
19328 while Present (Component) loop
19330 -- Ada 2005 (AI-251): Do not inherit components associated with
19331 -- secondary tags of the parent.
19333 if Ekind (Component) = E_Component
19334 and then Present (Related_Type (Component))
19335 then
19336 null;
19338 elsif Ekind (Component) /= E_Component
19339 or else Chars (Component) = Name_uParent
19340 then
19341 null;
19343 -- If the derived type is within the parent type's declarative
19344 -- region, then the components can still be inherited even though
19345 -- they aren't visible at this point. This can occur for cases
19346 -- such as within public child units where the components must
19347 -- become visible upon entering the child unit's private part.
19349 elsif not Is_Visible_Component (Component)
19350 and then not In_Open_Scopes (Scope (Parent_Base))
19351 then
19352 null;
19354 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19355 then
19356 null;
19358 else
19359 Inherit_Component (Component);
19360 end if;
19362 Next_Entity (Component);
19363 end loop;
19365 -- For tagged derived types, inherited discriminants cannot be used in
19366 -- component declarations of the record extension part. To achieve this
19367 -- we mark the inherited discriminants as not visible.
19369 if Is_Tagged and then Inherit_Discr then
19370 D := First_Discriminant (Derived_Base);
19371 while Present (D) loop
19372 Set_Is_Immediately_Visible (D, False);
19373 Next_Discriminant (D);
19374 end loop;
19375 end if;
19377 return Assoc_List;
19378 end Inherit_Components;
19380 ----------------------
19381 -- Is_EVF_Procedure --
19382 ----------------------
19384 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19385 Formal : Entity_Id;
19387 begin
19388 -- Examine the formals of an Extensions_Visible False procedure looking
19389 -- for a controlling OUT parameter.
19391 if Ekind (Subp) = E_Procedure
19392 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19393 then
19394 Formal := First_Formal (Subp);
19395 while Present (Formal) loop
19396 if Ekind (Formal) = E_Out_Parameter
19397 and then Is_Controlling_Formal (Formal)
19398 then
19399 return True;
19400 end if;
19402 Next_Formal (Formal);
19403 end loop;
19404 end if;
19406 return False;
19407 end Is_EVF_Procedure;
19409 --------------------------
19410 -- Is_Private_Primitive --
19411 --------------------------
19413 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19414 Prim_Scope : constant Entity_Id := Scope (Prim);
19415 Priv_Entity : Entity_Id;
19416 begin
19417 if Is_Package_Or_Generic_Package (Prim_Scope) then
19418 Priv_Entity := First_Private_Entity (Prim_Scope);
19420 while Present (Priv_Entity) loop
19421 if Priv_Entity = Prim then
19422 return True;
19423 end if;
19425 Next_Entity (Priv_Entity);
19426 end loop;
19427 end if;
19429 return False;
19430 end Is_Private_Primitive;
19432 ------------------------------
19433 -- Is_Valid_Constraint_Kind --
19434 ------------------------------
19436 function Is_Valid_Constraint_Kind
19437 (T_Kind : Type_Kind;
19438 Constraint_Kind : Node_Kind) return Boolean
19440 begin
19441 case T_Kind is
19442 when Enumeration_Kind
19443 | Integer_Kind
19445 return Constraint_Kind = N_Range_Constraint;
19447 when Decimal_Fixed_Point_Kind =>
19448 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19450 when Ordinary_Fixed_Point_Kind =>
19451 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19453 when Float_Kind =>
19454 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19456 when Access_Kind
19457 | Array_Kind
19458 | Class_Wide_Kind
19459 | Concurrent_Kind
19460 | Private_Kind
19461 | E_Incomplete_Type
19462 | E_Record_Subtype
19463 | E_Record_Type
19465 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19467 when others =>
19468 return True; -- Error will be detected later
19469 end case;
19470 end Is_Valid_Constraint_Kind;
19472 --------------------------
19473 -- Is_Visible_Component --
19474 --------------------------
19476 function Is_Visible_Component
19477 (C : Entity_Id;
19478 N : Node_Id := Empty) return Boolean
19480 Original_Comp : Entity_Id := Empty;
19481 Original_Type : Entity_Id;
19482 Type_Scope : Entity_Id;
19484 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19485 -- Check whether parent type of inherited component is declared locally,
19486 -- possibly within a nested package or instance. The current scope is
19487 -- the derived record itself.
19489 -------------------
19490 -- Is_Local_Type --
19491 -------------------
19493 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19494 begin
19495 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19496 end Is_Local_Type;
19498 -- Start of processing for Is_Visible_Component
19500 begin
19501 if Ekind (C) in E_Component | E_Discriminant then
19502 Original_Comp := Original_Record_Component (C);
19503 end if;
19505 if No (Original_Comp) then
19507 -- Premature usage, or previous error
19509 return False;
19511 else
19512 Original_Type := Scope (Original_Comp);
19513 Type_Scope := Scope (Base_Type (Scope (C)));
19514 end if;
19516 -- This test only concerns tagged types
19518 if not Is_Tagged_Type (Original_Type) then
19520 -- Check if this is a renamed discriminant (hidden either by the
19521 -- derived type or by some ancestor), unless we are analyzing code
19522 -- generated by the expander since it may reference such components
19523 -- (for example see the expansion of Deep_Adjust).
19525 if Ekind (C) = E_Discriminant and then Present (N) then
19526 return
19527 not Comes_From_Source (N)
19528 or else not Is_Completely_Hidden (C);
19529 else
19530 return True;
19531 end if;
19533 -- If it is _Parent or _Tag, there is no visibility issue
19535 elsif not Comes_From_Source (Original_Comp) then
19536 return True;
19538 -- Discriminants are visible unless the (private) type has unknown
19539 -- discriminants. If the discriminant reference is inserted for a
19540 -- discriminant check on a full view it is also visible.
19542 elsif Ekind (Original_Comp) = E_Discriminant
19543 and then
19544 (not Has_Unknown_Discriminants (Original_Type)
19545 or else (Present (N)
19546 and then Nkind (N) = N_Selected_Component
19547 and then Nkind (Prefix (N)) = N_Type_Conversion
19548 and then not Comes_From_Source (Prefix (N))))
19549 then
19550 return True;
19552 -- If the component has been declared in an ancestor which is currently
19553 -- a private type, then it is not visible. The same applies if the
19554 -- component's containing type is not in an open scope and the original
19555 -- component's enclosing type is a visible full view of a private type
19556 -- (which can occur in cases where an attempt is being made to reference
19557 -- a component in a sibling package that is inherited from a visible
19558 -- component of a type in an ancestor package; the component in the
19559 -- sibling package should not be visible even though the component it
19560 -- inherited from is visible), but instance bodies are not subject to
19561 -- this second case since they have the Has_Private_View mechanism to
19562 -- ensure proper visibility. This does not apply however in the case
19563 -- where the scope of the type is a private child unit, or when the
19564 -- parent comes from a local package in which the ancestor is currently
19565 -- visible. The latter suppression of visibility is needed for cases
19566 -- that are tested in B730006.
19568 elsif Is_Private_Type (Original_Type)
19569 or else
19570 (not Is_Private_Descendant (Type_Scope)
19571 and then not In_Open_Scopes (Type_Scope)
19572 and then Has_Private_Declaration (Original_Type)
19573 and then not In_Instance_Body)
19574 then
19575 -- If the type derives from an entity in a formal package, there
19576 -- are no additional visible components.
19578 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19579 N_Formal_Package_Declaration
19580 then
19581 return False;
19583 -- if we are not in the private part of the current package, there
19584 -- are no additional visible components.
19586 elsif Ekind (Scope (Current_Scope)) = E_Package
19587 and then not In_Private_Part (Scope (Current_Scope))
19588 then
19589 return False;
19590 else
19591 return
19592 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19593 and then In_Open_Scopes (Scope (Original_Type))
19594 and then Is_Local_Type (Type_Scope);
19595 end if;
19597 -- There is another weird way in which a component may be invisible when
19598 -- the private and the full view are not derived from the same ancestor.
19599 -- Here is an example :
19601 -- type A1 is tagged record F1 : integer; end record;
19602 -- type A2 is new A1 with record F2 : integer; end record;
19603 -- type T is new A1 with private;
19604 -- private
19605 -- type T is new A2 with null record;
19607 -- In this case, the full view of T inherits F1 and F2 but the private
19608 -- view inherits only F1
19610 else
19611 declare
19612 Ancestor : Entity_Id := Scope (C);
19614 begin
19615 loop
19616 if Ancestor = Original_Type then
19617 return True;
19619 -- The ancestor may have a partial view of the original type,
19620 -- but if the full view is in scope, as in a child body, the
19621 -- component is visible.
19623 elsif In_Private_Part (Scope (Original_Type))
19624 and then Full_View (Ancestor) = Original_Type
19625 then
19626 return True;
19628 elsif Ancestor = Etype (Ancestor) then
19630 -- No further ancestors to examine
19632 return False;
19633 end if;
19635 Ancestor := Etype (Ancestor);
19636 end loop;
19637 end;
19638 end if;
19639 end Is_Visible_Component;
19641 --------------------------
19642 -- Make_Class_Wide_Type --
19643 --------------------------
19645 procedure Make_Class_Wide_Type (T : Entity_Id) is
19646 CW_Type : Entity_Id;
19647 CW_Name : Name_Id;
19648 Next_E : Entity_Id;
19649 Prev_E : Entity_Id;
19651 begin
19652 if Present (Class_Wide_Type (T)) then
19654 -- The class-wide type is a partially decorated entity created for a
19655 -- unanalyzed tagged type referenced through a limited with clause.
19656 -- When the tagged type is analyzed, its class-wide type needs to be
19657 -- redecorated. Note that we reuse the entity created by Decorate_
19658 -- Tagged_Type in order to preserve all links.
19660 if Materialize_Entity (Class_Wide_Type (T)) then
19661 CW_Type := Class_Wide_Type (T);
19662 Set_Materialize_Entity (CW_Type, False);
19664 -- The class wide type can have been defined by the partial view, in
19665 -- which case everything is already done.
19667 else
19668 return;
19669 end if;
19671 -- Default case, we need to create a new class-wide type
19673 else
19674 CW_Type :=
19675 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19676 end if;
19678 -- Inherit root type characteristics
19680 CW_Name := Chars (CW_Type);
19681 Next_E := Next_Entity (CW_Type);
19682 Prev_E := Prev_Entity (CW_Type);
19683 Copy_Node (T, CW_Type);
19684 Set_Comes_From_Source (CW_Type, False);
19685 Set_Chars (CW_Type, CW_Name);
19686 Set_Parent (CW_Type, Parent (T));
19687 Set_Prev_Entity (CW_Type, Prev_E);
19688 Set_Next_Entity (CW_Type, Next_E);
19690 -- Ensure we have a new freeze node for the class-wide type. The partial
19691 -- view may have freeze action of its own, requiring a proper freeze
19692 -- node, and the same freeze node cannot be shared between the two
19693 -- types.
19695 Set_Has_Delayed_Freeze (CW_Type);
19696 Set_Freeze_Node (CW_Type, Empty);
19698 -- Customize the class-wide type: It has no prim. op., it cannot be
19699 -- abstract, its Etype points back to the specific root type, and it
19700 -- cannot have any invariants.
19702 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19703 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19705 elsif Ekind (CW_Type) in Concurrent_Kind then
19706 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19707 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19709 if Ekind (CW_Type) in Task_Kind then
19710 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19711 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19712 end if;
19714 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19715 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19716 end if;
19718 elsif Ekind (CW_Type) = E_Record_Type then
19719 Reinit_Field_To_Zero (CW_Type, F_Corresponding_Concurrent_Type);
19720 end if;
19722 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19723 Set_Is_Tagged_Type (CW_Type, True);
19724 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19725 Set_Is_Abstract_Type (CW_Type, False);
19726 Set_Is_Constrained (CW_Type, False);
19727 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19728 Set_Default_SSO (CW_Type);
19729 Set_Has_Inheritable_Invariants (CW_Type, False);
19730 Set_Has_Inherited_Invariants (CW_Type, False);
19731 Set_Has_Own_Invariants (CW_Type, False);
19733 if Ekind (T) = E_Class_Wide_Subtype then
19734 Set_Etype (CW_Type, Etype (Base_Type (T)));
19735 else
19736 Set_Etype (CW_Type, T);
19737 end if;
19739 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19741 -- If this is the class_wide type of a constrained subtype, it does
19742 -- not have discriminants.
19744 Set_Has_Discriminants (CW_Type,
19745 Has_Discriminants (T) and then not Is_Constrained (T));
19747 Set_Has_Unknown_Discriminants (CW_Type, True);
19748 Set_Class_Wide_Type (T, CW_Type);
19749 Set_Equivalent_Type (CW_Type, Empty);
19751 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19753 Set_Class_Wide_Type (CW_Type, CW_Type);
19754 end Make_Class_Wide_Type;
19756 ----------------
19757 -- Make_Index --
19758 ----------------
19760 procedure Make_Index
19761 (N : Node_Id;
19762 Related_Nod : Node_Id;
19763 Related_Id : Entity_Id := Empty;
19764 Suffix_Index : Pos := 1)
19766 R : Node_Id;
19767 T : Entity_Id;
19768 Def_Id : Entity_Id := Empty;
19769 Found : Boolean := False;
19771 begin
19772 -- For a discrete range used in a constrained array definition and
19773 -- defined by a range, an implicit conversion to the predefined type
19774 -- INTEGER is assumed if each bound is either a numeric literal, a named
19775 -- number, or an attribute, and the type of both bounds (prior to the
19776 -- implicit conversion) is the type universal_integer. Otherwise, both
19777 -- bounds must be of the same discrete type, other than universal
19778 -- integer; this type must be determinable independently of the
19779 -- context, but using the fact that the type must be discrete and that
19780 -- both bounds must have the same type.
19782 -- Character literals also have a universal type in the absence of
19783 -- of additional context, and are resolved to Standard_Character.
19785 if Nkind (N) = N_Range then
19787 -- The index is given by a range constraint. The bounds are known
19788 -- to be of a consistent type.
19790 if not Is_Overloaded (N) then
19791 T := Etype (N);
19793 -- For universal bounds, choose the specific predefined type
19795 if T = Universal_Integer then
19796 T := Standard_Integer;
19798 elsif T = Any_Character then
19799 Ambiguous_Character (Low_Bound (N));
19801 T := Standard_Character;
19802 end if;
19804 -- The node may be overloaded because some user-defined operators
19805 -- are available, but if a universal interpretation exists it is
19806 -- also the selected one.
19808 elsif Universal_Interpretation (N) = Universal_Integer then
19809 T := Standard_Integer;
19811 else
19812 T := Any_Type;
19814 declare
19815 Ind : Interp_Index;
19816 It : Interp;
19818 begin
19819 Get_First_Interp (N, Ind, It);
19820 while Present (It.Typ) loop
19821 if Is_Discrete_Type (It.Typ) then
19823 if Found
19824 and then not Covers (It.Typ, T)
19825 and then not Covers (T, It.Typ)
19826 then
19827 Error_Msg_N ("ambiguous bounds in discrete range", N);
19828 exit;
19829 else
19830 T := It.Typ;
19831 Found := True;
19832 end if;
19833 end if;
19835 Get_Next_Interp (Ind, It);
19836 end loop;
19838 if T = Any_Type then
19839 Error_Msg_N ("discrete type required for range", N);
19840 Set_Etype (N, Any_Type);
19841 return;
19843 elsif T = Universal_Integer then
19844 T := Standard_Integer;
19845 end if;
19846 end;
19847 end if;
19849 if not Is_Discrete_Type (T) then
19850 Error_Msg_N ("discrete type required for range", N);
19851 Set_Etype (N, Any_Type);
19852 return;
19853 end if;
19855 -- If the range bounds are "T'First .. T'Last" where T is a name of a
19856 -- discrete type, then use T as the type of the index.
19858 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19859 and then Attribute_Name (Low_Bound (N)) = Name_First
19860 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19861 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19863 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19864 and then Attribute_Name (High_Bound (N)) = Name_Last
19865 and then Is_Entity_Name (Prefix (High_Bound (N)))
19866 and then Entity (Prefix (High_Bound (N))) = Def_Id
19867 then
19868 Def_Id := Entity (Prefix (Low_Bound (N)));
19869 end if;
19871 R := N;
19872 Process_Range_Expr_In_Decl (R, T);
19874 elsif Nkind (N) = N_Subtype_Indication then
19876 -- The index is given by a subtype with a range constraint
19878 T := Base_Type (Entity (Subtype_Mark (N)));
19880 if not Is_Discrete_Type (T) then
19881 Error_Msg_N ("discrete type required for range", N);
19882 Set_Etype (N, Any_Type);
19883 return;
19884 end if;
19886 R := Range_Expression (Constraint (N));
19888 Resolve (R, T);
19889 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19891 elsif Nkind (N) = N_Attribute_Reference then
19893 -- Catch beginner's error (use of attribute other than 'Range)
19895 if Attribute_Name (N) /= Name_Range then
19896 Error_Msg_N ("expect attribute ''Range", N);
19897 Set_Etype (N, Any_Type);
19898 return;
19899 end if;
19901 -- If the node denotes the range of a type mark, that is also the
19902 -- resulting type, and we do not need to create an Itype for it.
19904 if Is_Entity_Name (Prefix (N))
19905 and then Comes_From_Source (N)
19906 and then Is_Discrete_Type (Entity (Prefix (N)))
19907 then
19908 Def_Id := Entity (Prefix (N));
19909 end if;
19911 Analyze_And_Resolve (N);
19912 T := Etype (N);
19913 R := N;
19915 -- If none of the above, must be a subtype. We convert this to a
19916 -- range attribute reference because in the case of declared first
19917 -- named subtypes, the types in the range reference can be different
19918 -- from the type of the entity. A range attribute normalizes the
19919 -- reference and obtains the correct types for the bounds.
19921 -- This transformation is in the nature of an expansion, is only
19922 -- done if expansion is active. In particular, it is not done on
19923 -- formal generic types, because we need to retain the name of the
19924 -- original index for instantiation purposes.
19926 else
19927 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19928 Error_Msg_N ("invalid subtype mark in discrete range", N);
19929 Set_Etype (N, Any_Integer);
19930 return;
19932 else
19933 -- The type mark may be that of an incomplete type. It is only
19934 -- now that we can get the full view, previous analysis does
19935 -- not look specifically for a type mark.
19937 Set_Entity (N, Get_Full_View (Entity (N)));
19938 Set_Etype (N, Entity (N));
19939 Def_Id := Entity (N);
19941 if not Is_Discrete_Type (Def_Id) then
19942 Error_Msg_N ("discrete type required for index", N);
19943 Set_Etype (N, Any_Type);
19944 return;
19945 end if;
19946 end if;
19948 if Expander_Active then
19949 Rewrite (N,
19950 Make_Attribute_Reference (Sloc (N),
19951 Attribute_Name => Name_Range,
19952 Prefix => Relocate_Node (N)));
19954 -- The original was a subtype mark that does not freeze. This
19955 -- means that the rewritten version must not freeze either.
19957 Set_Must_Not_Freeze (N);
19958 Set_Must_Not_Freeze (Prefix (N));
19959 Analyze_And_Resolve (N);
19960 T := Etype (N);
19961 R := N;
19963 -- If expander is inactive, type is legal, nothing else to construct
19965 else
19966 return;
19967 end if;
19968 end if;
19970 if not Is_Discrete_Type (T) then
19971 Error_Msg_N ("discrete type required for range", N);
19972 Set_Etype (N, Any_Type);
19973 return;
19975 elsif T = Any_Type then
19976 Set_Etype (N, Any_Type);
19977 return;
19978 end if;
19980 -- We will now create the appropriate Itype to describe the range, but
19981 -- first a check. If we originally had a subtype, then we just label
19982 -- the range with this subtype. Not only is there no need to construct
19983 -- a new subtype, but it is wrong to do so for two reasons:
19985 -- 1. A legality concern, if we have a subtype, it must not freeze,
19986 -- and the Itype would cause freezing incorrectly
19988 -- 2. An efficiency concern, if we created an Itype, it would not be
19989 -- recognized as the same type for the purposes of eliminating
19990 -- checks in some circumstances.
19992 -- We signal this case by setting the subtype entity in Def_Id
19994 if No (Def_Id) then
19995 Def_Id :=
19996 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19997 Set_Etype (Def_Id, Base_Type (T));
19999 if Is_Signed_Integer_Type (T) then
20000 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20002 elsif Is_Modular_Integer_Type (T) then
20003 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20005 else
20006 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20007 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20008 Set_First_Literal (Def_Id, First_Literal (T));
20009 end if;
20011 Set_Size_Info (Def_Id, (T));
20012 Set_RM_Size (Def_Id, RM_Size (T));
20013 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20015 Set_Scalar_Range (Def_Id, R);
20016 Conditional_Delay (Def_Id, T);
20018 -- In the subtype indication case inherit properties of the parent
20020 if Nkind (N) = N_Subtype_Indication then
20022 -- It is enough to inherit predicate flags and not the predicate
20023 -- functions, because predicates on an index type are illegal
20024 -- anyway and the flags are enough to detect them.
20026 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20028 -- If the immediate parent of the new subtype is nonstatic, then
20029 -- the subtype we create is nonstatic as well, even if its bounds
20030 -- are static.
20032 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20033 Set_Is_Non_Static_Subtype (Def_Id);
20034 end if;
20035 end if;
20037 Set_Parent (Def_Id, N);
20038 end if;
20040 -- Final step is to label the index with this constructed type
20042 Set_Etype (N, Def_Id);
20043 end Make_Index;
20045 ------------------------------
20046 -- Modular_Type_Declaration --
20047 ------------------------------
20049 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20050 Mod_Expr : constant Node_Id := Expression (Def);
20051 M_Val : Uint;
20053 procedure Set_Modular_Size (Bits : Int);
20054 -- Sets RM_Size to Bits, and Esize to normal word size above this
20056 ----------------------
20057 -- Set_Modular_Size --
20058 ----------------------
20060 procedure Set_Modular_Size (Bits : Int) is
20061 Siz : Int;
20063 begin
20064 Set_RM_Size (T, UI_From_Int (Bits));
20066 if Bits < System_Max_Binary_Modulus_Power then
20067 Siz := 8;
20069 while Siz < 128 loop
20070 exit when Bits <= Siz;
20071 Siz := Siz * 2;
20072 end loop;
20074 Set_Esize (T, UI_From_Int (Siz));
20076 else
20077 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20078 end if;
20080 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20081 Set_Is_Known_Valid (T);
20082 end if;
20083 end Set_Modular_Size;
20085 -- Start of processing for Modular_Type_Declaration
20087 begin
20088 -- If the mod expression is (exactly) 2 * literal, where literal is
20089 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20091 if Warn_On_Suspicious_Modulus_Value
20092 and then Nkind (Mod_Expr) = N_Op_Multiply
20093 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20094 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20095 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20096 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20097 then
20098 Error_Msg_N
20099 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20100 end if;
20102 -- Proceed with analysis of mod expression
20104 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20106 Set_Etype (T, T);
20107 Mutate_Ekind (T, E_Modular_Integer_Type);
20108 Reinit_Alignment (T);
20109 Set_Is_Constrained (T);
20111 if not Is_OK_Static_Expression (Mod_Expr) then
20112 Flag_Non_Static_Expr
20113 ("non-static expression used for modular type bound!", Mod_Expr);
20114 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20115 else
20116 M_Val := Expr_Value (Mod_Expr);
20117 end if;
20119 if M_Val < 1 then
20120 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20121 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20122 end if;
20124 if M_Val > 2 ** Standard_Long_Integer_Size then
20125 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20126 end if;
20128 Set_Modulus (T, M_Val);
20130 -- Create bounds for the modular type based on the modulus given in
20131 -- the type declaration and then analyze and resolve those bounds.
20133 Set_Scalar_Range (T,
20134 Make_Range (Sloc (Mod_Expr),
20135 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20136 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20138 -- Properly analyze the literals for the range. We do this manually
20139 -- because we can't go calling Resolve, since we are resolving these
20140 -- bounds with the type, and this type is certainly not complete yet.
20142 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20143 Set_Etype (High_Bound (Scalar_Range (T)), T);
20144 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20145 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20147 -- Loop through powers of two to find number of bits required
20149 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20151 -- Binary case
20153 if M_Val = 2 ** Bits then
20154 Set_Modular_Size (Bits);
20155 return;
20157 -- Nonbinary case
20159 elsif M_Val < 2 ** Bits then
20160 Set_Non_Binary_Modulus (T);
20162 if Bits > System_Max_Nonbinary_Modulus_Power then
20163 Error_Msg_Uint_1 :=
20164 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20165 Error_Msg_F
20166 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20167 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20168 return;
20170 else
20171 -- In the nonbinary case, set size as per RM 13.3(55)
20173 Set_Modular_Size (Bits);
20174 return;
20175 end if;
20176 end if;
20178 end loop;
20180 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20181 -- so we just signal an error and set the maximum size.
20183 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20184 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20186 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20187 Reinit_Alignment (T);
20189 end Modular_Type_Declaration;
20191 --------------------------
20192 -- New_Concatenation_Op --
20193 --------------------------
20195 procedure New_Concatenation_Op (Typ : Entity_Id) is
20196 Loc : constant Source_Ptr := Sloc (Typ);
20197 Op : Entity_Id;
20199 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20200 -- Create abbreviated declaration for the formal of a predefined
20201 -- Operator 'Op' of type 'Typ'
20203 --------------------
20204 -- Make_Op_Formal --
20205 --------------------
20207 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20208 Formal : Entity_Id;
20209 begin
20210 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20211 Set_Etype (Formal, Typ);
20212 Set_Mechanism (Formal, Default_Mechanism);
20213 return Formal;
20214 end Make_Op_Formal;
20216 -- Start of processing for New_Concatenation_Op
20218 begin
20219 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20221 Mutate_Ekind (Op, E_Operator);
20222 Set_Scope (Op, Current_Scope);
20223 Set_Etype (Op, Typ);
20224 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20225 Set_Is_Immediately_Visible (Op);
20226 Set_Is_Intrinsic_Subprogram (Op);
20227 Set_Has_Completion (Op);
20228 Append_Entity (Op, Current_Scope);
20230 Set_Name_Entity_Id (Name_Op_Concat, Op);
20232 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20233 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20234 end New_Concatenation_Op;
20236 -------------------------
20237 -- OK_For_Limited_Init --
20238 -------------------------
20240 -- ???Check all calls of this, and compare the conditions under which it's
20241 -- called.
20243 function OK_For_Limited_Init
20244 (Typ : Entity_Id;
20245 Exp : Node_Id) return Boolean
20247 begin
20248 return Is_CPP_Constructor_Call (Exp)
20249 or else (Ada_Version >= Ada_2005
20250 and then not Debug_Flag_Dot_L
20251 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20252 end OK_For_Limited_Init;
20254 -------------------------------
20255 -- OK_For_Limited_Init_In_05 --
20256 -------------------------------
20258 function OK_For_Limited_Init_In_05
20259 (Typ : Entity_Id;
20260 Exp : Node_Id) return Boolean
20262 begin
20263 -- An object of a limited interface type can be initialized with any
20264 -- expression of a nonlimited descendant type. However this does not
20265 -- apply if this is a view conversion of some other expression. This
20266 -- is checked below.
20268 if Is_Class_Wide_Type (Typ)
20269 and then Is_Limited_Interface (Typ)
20270 and then not Is_Limited_Type (Etype (Exp))
20271 and then Nkind (Exp) /= N_Type_Conversion
20272 then
20273 return True;
20274 end if;
20276 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20277 -- case of limited aggregates (including extension aggregates), and
20278 -- function calls. The function call may have been given in prefixed
20279 -- notation, in which case the original node is an indexed component.
20280 -- If the function is parameterless, the original node was an explicit
20281 -- dereference. The function may also be parameterless, in which case
20282 -- the source node is just an identifier.
20284 -- A branch of a conditional expression may have been removed if the
20285 -- condition is statically known. This happens during expansion, and
20286 -- thus will not happen if previous errors were encountered. The check
20287 -- will have been performed on the chosen branch, which replaces the
20288 -- original conditional expression.
20290 if No (Exp) then
20291 return True;
20292 end if;
20294 case Nkind (Original_Node (Exp)) is
20295 when N_Aggregate
20296 | N_Delta_Aggregate
20297 | N_Extension_Aggregate
20298 | N_Function_Call
20299 | N_Op
20301 return True;
20303 when N_Identifier =>
20304 return Present (Entity (Original_Node (Exp)))
20305 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20307 when N_Qualified_Expression =>
20308 return
20309 OK_For_Limited_Init_In_05
20310 (Typ, Expression (Original_Node (Exp)));
20312 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20313 -- with a function call, the expander has rewritten the call into an
20314 -- N_Type_Conversion node to force displacement of the pointer to
20315 -- reference the component containing the secondary dispatch table.
20316 -- Otherwise a type conversion is not a legal context.
20317 -- A return statement for a build-in-place function returning a
20318 -- synchronized type also introduces an unchecked conversion.
20320 when N_Type_Conversion
20321 | N_Unchecked_Type_Conversion
20323 return not Comes_From_Source (Exp)
20324 and then
20325 -- If the conversion has been rewritten, check Original_Node;
20326 -- otherwise, check the expression of the compiler-generated
20327 -- conversion (which is a conversion that we want to ignore
20328 -- for purposes of the limited-initialization restrictions).
20330 (if Is_Rewrite_Substitution (Exp)
20331 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20332 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20334 when N_Explicit_Dereference
20335 | N_Indexed_Component
20336 | N_Selected_Component
20338 return Nkind (Exp) = N_Function_Call;
20340 -- A use of 'Input is a function call, hence allowed. Normally the
20341 -- attribute will be changed to a call, but the attribute by itself
20342 -- can occur with -gnatc.
20344 when N_Attribute_Reference =>
20345 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20347 -- "return raise ..." is OK
20349 when N_Raise_Expression =>
20350 return True;
20352 -- For a case expression, all dependent expressions must be legal
20354 when N_Case_Expression =>
20355 declare
20356 Alt : Node_Id;
20358 begin
20359 Alt := First (Alternatives (Original_Node (Exp)));
20360 while Present (Alt) loop
20361 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20362 return False;
20363 end if;
20365 Next (Alt);
20366 end loop;
20368 return True;
20369 end;
20371 -- For an if expression, all dependent expressions must be legal
20373 when N_If_Expression =>
20374 declare
20375 Then_Expr : constant Node_Id :=
20376 Next (First (Expressions (Original_Node (Exp))));
20377 Else_Expr : constant Node_Id := Next (Then_Expr);
20378 begin
20379 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20380 and then
20381 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20382 end;
20384 when others =>
20385 return False;
20386 end case;
20387 end OK_For_Limited_Init_In_05;
20389 -------------------------------------------
20390 -- Ordinary_Fixed_Point_Type_Declaration --
20391 -------------------------------------------
20393 procedure Ordinary_Fixed_Point_Type_Declaration
20394 (T : Entity_Id;
20395 Def : Node_Id)
20397 Loc : constant Source_Ptr := Sloc (Def);
20398 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20399 RRS : constant Node_Id := Real_Range_Specification (Def);
20400 Implicit_Base : Entity_Id;
20401 Delta_Val : Ureal;
20402 Small_Val : Ureal;
20403 Low_Val : Ureal;
20404 High_Val : Ureal;
20406 begin
20407 Check_Restriction (No_Fixed_Point, Def);
20409 -- Create implicit base type
20411 Implicit_Base :=
20412 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20413 Set_Etype (Implicit_Base, Implicit_Base);
20415 -- Analyze and process delta expression
20417 Analyze_And_Resolve (Delta_Expr, Any_Real);
20419 Check_Delta_Expression (Delta_Expr);
20420 Delta_Val := Expr_Value_R (Delta_Expr);
20422 Set_Delta_Value (Implicit_Base, Delta_Val);
20424 -- Compute default small from given delta, which is the largest power
20425 -- of two that does not exceed the given delta value.
20427 declare
20428 Tmp : Ureal;
20429 Scale : Int;
20431 begin
20432 Tmp := Ureal_1;
20433 Scale := 0;
20435 if Delta_Val < Ureal_1 then
20436 while Delta_Val < Tmp loop
20437 Tmp := Tmp / Ureal_2;
20438 Scale := Scale + 1;
20439 end loop;
20441 else
20442 loop
20443 Tmp := Tmp * Ureal_2;
20444 exit when Tmp > Delta_Val;
20445 Scale := Scale - 1;
20446 end loop;
20447 end if;
20449 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20450 end;
20452 Set_Small_Value (Implicit_Base, Small_Val);
20454 -- If no range was given, set a dummy range
20456 if RRS <= Empty_Or_Error then
20457 Low_Val := -Small_Val;
20458 High_Val := Small_Val;
20460 -- Otherwise analyze and process given range
20462 else
20463 declare
20464 Low : constant Node_Id := Low_Bound (RRS);
20465 High : constant Node_Id := High_Bound (RRS);
20467 begin
20468 Analyze_And_Resolve (Low, Any_Real);
20469 Analyze_And_Resolve (High, Any_Real);
20470 Check_Real_Bound (Low);
20471 Check_Real_Bound (High);
20473 -- Obtain and set the range
20475 Low_Val := Expr_Value_R (Low);
20476 High_Val := Expr_Value_R (High);
20478 if Low_Val > High_Val then
20479 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20480 end if;
20481 end;
20482 end if;
20484 -- The range for both the implicit base and the declared first subtype
20485 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20486 -- set a temporary range in place. Note that the bounds of the base
20487 -- type will be widened to be symmetrical and to fill the available
20488 -- bits when the type is frozen.
20490 -- We could do this with all discrete types, and probably should, but
20491 -- we absolutely have to do it for fixed-point, since the end-points
20492 -- of the range and the size are determined by the small value, which
20493 -- could be reset before the freeze point.
20495 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20496 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20498 -- Complete definition of first subtype. The inheritance of the rep item
20499 -- chain ensures that SPARK-related pragmas are not clobbered when the
20500 -- ordinary fixed point type acts as a full view of a private type.
20502 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20503 Set_Etype (T, Implicit_Base);
20504 Reinit_Size_Align (T);
20505 Inherit_Rep_Item_Chain (T, Implicit_Base);
20506 Set_Small_Value (T, Small_Val);
20507 Set_Delta_Value (T, Delta_Val);
20508 Set_Is_Constrained (T);
20509 end Ordinary_Fixed_Point_Type_Declaration;
20511 ----------------------------------
20512 -- Preanalyze_Assert_Expression --
20513 ----------------------------------
20515 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20516 begin
20517 In_Assertion_Expr := In_Assertion_Expr + 1;
20518 Preanalyze_Spec_Expression (N, T);
20519 In_Assertion_Expr := In_Assertion_Expr - 1;
20520 end Preanalyze_Assert_Expression;
20522 -- ??? The variant below explicitly saves and restores all the flags,
20523 -- because it is impossible to compose the existing variety of
20524 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20525 -- to achieve the desired semantics.
20527 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20528 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20529 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
20530 Save_Full_Analysis : constant Boolean := Full_Analysis;
20532 begin
20533 In_Assertion_Expr := In_Assertion_Expr + 1;
20534 In_Spec_Expression := True;
20535 Set_Must_Not_Freeze (N);
20536 Inside_Preanalysis_Without_Freezing :=
20537 Inside_Preanalysis_Without_Freezing + 1;
20538 Full_Analysis := False;
20539 Expander_Mode_Save_And_Set (False);
20541 if GNATprove_Mode then
20542 Analyze_And_Resolve (N);
20543 else
20544 Analyze_And_Resolve (N, Suppress => All_Checks);
20545 end if;
20547 Expander_Mode_Restore;
20548 Full_Analysis := Save_Full_Analysis;
20549 Inside_Preanalysis_Without_Freezing :=
20550 Inside_Preanalysis_Without_Freezing - 1;
20551 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
20552 In_Spec_Expression := Save_In_Spec_Expression;
20553 In_Assertion_Expr := In_Assertion_Expr - 1;
20554 end Preanalyze_Assert_Expression;
20556 -----------------------------------
20557 -- Preanalyze_Default_Expression --
20558 -----------------------------------
20560 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20561 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20562 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20564 begin
20565 In_Default_Expr := True;
20566 In_Spec_Expression := True;
20568 Preanalyze_With_Freezing_And_Resolve (N, T);
20570 In_Default_Expr := Save_In_Default_Expr;
20571 In_Spec_Expression := Save_In_Spec_Expression;
20572 end Preanalyze_Default_Expression;
20574 --------------------------------
20575 -- Preanalyze_Spec_Expression --
20576 --------------------------------
20578 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20579 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20580 begin
20581 In_Spec_Expression := True;
20582 Preanalyze_And_Resolve (N, T);
20583 In_Spec_Expression := Save_In_Spec_Expression;
20584 end Preanalyze_Spec_Expression;
20586 ----------------------------------------
20587 -- Prepare_Private_Subtype_Completion --
20588 ----------------------------------------
20590 procedure Prepare_Private_Subtype_Completion
20591 (Id : Entity_Id;
20592 Related_Nod : Node_Id)
20594 Id_B : constant Entity_Id := Base_Type (Id);
20595 Full_B : constant Entity_Id := Full_View (Id_B);
20596 Full : Entity_Id;
20598 begin
20599 if Present (Full_B) then
20601 -- The Base_Type is already completed, we can complete the subtype
20602 -- now. We have to create a new entity with the same name, Thus we
20603 -- can't use Create_Itype.
20605 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20606 Set_Is_Itype (Full);
20607 Set_Associated_Node_For_Itype (Full, Related_Nod);
20608 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20609 Set_Full_View (Id, Full);
20610 end if;
20612 -- The parent subtype may be private, but the base might not, in some
20613 -- nested instances. In that case, the subtype does not need to be
20614 -- exchanged. It would still be nice to make private subtypes and their
20615 -- bases consistent at all times ???
20617 if Is_Private_Type (Id_B) then
20618 Append_Elmt (Id, Private_Dependents (Id_B));
20619 end if;
20620 end Prepare_Private_Subtype_Completion;
20622 ---------------------------
20623 -- Process_Discriminants --
20624 ---------------------------
20626 procedure Process_Discriminants
20627 (N : Node_Id;
20628 Prev : Entity_Id := Empty)
20630 Elist : constant Elist_Id := New_Elmt_List;
20631 Id : Node_Id;
20632 Discr : Node_Id;
20633 Discr_Number : Uint;
20634 Discr_Type : Entity_Id;
20635 Default_Present : Boolean := False;
20636 Default_Not_Present : Boolean := False;
20638 begin
20639 -- A composite type other than an array type can have discriminants.
20640 -- On entry, the current scope is the composite type.
20642 -- The discriminants are initially entered into the scope of the type
20643 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20644 -- use, as explained at the end of this procedure.
20646 Discr := First (Discriminant_Specifications (N));
20647 while Present (Discr) loop
20648 Enter_Name (Defining_Identifier (Discr));
20650 -- For navigation purposes we add a reference to the discriminant
20651 -- in the entity for the type. If the current declaration is a
20652 -- completion, place references on the partial view. Otherwise the
20653 -- type is the current scope.
20655 if Present (Prev) then
20657 -- The references go on the partial view, if present. If the
20658 -- partial view has discriminants, the references have been
20659 -- generated already.
20661 if not Has_Discriminants (Prev) then
20662 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20663 end if;
20664 else
20665 Generate_Reference
20666 (Current_Scope, Defining_Identifier (Discr), 'd');
20667 end if;
20669 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20670 Check_Anonymous_Access_Component
20671 (Typ_Decl => N,
20672 Typ => Defining_Identifier (N),
20673 Prev => Prev,
20674 Comp_Def => Discr,
20675 Access_Def => Discriminant_Type (Discr));
20677 -- if Check_Anonymous_Access_Component replaced Discr then
20678 -- its Original_Node points to the old Discr and the access type
20679 -- for Discr_Type has already been created.
20681 if Is_Rewrite_Substitution (Discr) then
20682 Discr_Type := Etype (Discriminant_Type (Discr));
20683 else
20684 Discr_Type :=
20685 Access_Definition (Discr, Discriminant_Type (Discr));
20687 -- Ada 2005 (AI-254)
20689 if Present (Access_To_Subprogram_Definition
20690 (Discriminant_Type (Discr)))
20691 and then Protected_Present (Access_To_Subprogram_Definition
20692 (Discriminant_Type (Discr)))
20693 then
20694 Discr_Type :=
20695 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20696 end if;
20697 end if;
20698 else
20699 Find_Type (Discriminant_Type (Discr));
20700 Discr_Type := Etype (Discriminant_Type (Discr));
20702 if Error_Posted (Discriminant_Type (Discr)) then
20703 Discr_Type := Any_Type;
20704 end if;
20705 end if;
20707 -- Handling of discriminants that are access types
20709 if Is_Access_Type (Discr_Type) then
20711 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20712 -- limited record types
20714 if Ada_Version < Ada_2005 then
20715 Check_Access_Discriminant_Requires_Limited
20716 (Discr, Discriminant_Type (Discr));
20717 end if;
20719 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20720 Error_Msg_N
20721 ("(Ada 83) access discriminant not allowed", Discr);
20722 end if;
20724 -- If not access type, must be a discrete type
20726 elsif not Is_Discrete_Type (Discr_Type) then
20727 Error_Msg_N
20728 ("discriminants must have a discrete or access type",
20729 Discriminant_Type (Discr));
20730 end if;
20732 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20734 -- If a discriminant specification includes the assignment compound
20735 -- delimiter followed by an expression, the expression is the default
20736 -- expression of the discriminant; the default expression must be of
20737 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20738 -- a default expression, we do the special preanalysis, since this
20739 -- expression does not freeze (see section "Handling of Default and
20740 -- Per-Object Expressions" in spec of package Sem).
20742 if Present (Expression (Discr)) then
20743 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20745 -- Legaity checks
20747 if Nkind (N) = N_Formal_Type_Declaration then
20748 Error_Msg_N
20749 ("discriminant defaults not allowed for formal type",
20750 Expression (Discr));
20752 -- Flag an error for a tagged type with defaulted discriminants,
20753 -- excluding limited tagged types when compiling for Ada 2012
20754 -- (see AI05-0214).
20756 elsif Is_Tagged_Type (Current_Scope)
20757 and then (not Is_Limited_Type (Current_Scope)
20758 or else Ada_Version < Ada_2012)
20759 and then Comes_From_Source (N)
20760 then
20761 -- Note: see similar test in Check_Or_Process_Discriminants, to
20762 -- handle the (illegal) case of the completion of an untagged
20763 -- view with discriminants with defaults by a tagged full view.
20764 -- We skip the check if Discr does not come from source, to
20765 -- account for the case of an untagged derived type providing
20766 -- defaults for a renamed discriminant from a private untagged
20767 -- ancestor with a tagged full view (ACATS B460006).
20769 if Ada_Version >= Ada_2012 then
20770 Error_Msg_N
20771 ("discriminants of nonlimited tagged type cannot have"
20772 & " defaults",
20773 Expression (Discr));
20774 else
20775 Error_Msg_N
20776 ("discriminants of tagged type cannot have defaults",
20777 Expression (Discr));
20778 end if;
20780 else
20781 Default_Present := True;
20782 Append_Elmt (Expression (Discr), Elist);
20784 -- Tag the defining identifiers for the discriminants with
20785 -- their corresponding default expressions from the tree.
20787 Set_Discriminant_Default_Value
20788 (Defining_Identifier (Discr), Expression (Discr));
20789 end if;
20791 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20792 -- gets set unless we can be sure that no range check is required.
20794 if not Expander_Active
20795 and then not
20796 Is_In_Range
20797 (Expression (Discr), Discr_Type, Assume_Valid => True)
20798 then
20799 Set_Do_Range_Check (Expression (Discr));
20800 end if;
20802 -- No default discriminant value given
20804 else
20805 Default_Not_Present := True;
20806 end if;
20808 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20809 -- Discr_Type but with the null-exclusion attribute
20811 if Ada_Version >= Ada_2005 then
20813 -- Ada 2005 (AI-231): Static checks
20815 if Can_Never_Be_Null (Discr_Type) then
20816 Null_Exclusion_Static_Checks (Discr);
20818 elsif Is_Access_Type (Discr_Type)
20819 and then Null_Exclusion_Present (Discr)
20821 -- No need to check itypes because in their case this check
20822 -- was done at their point of creation
20824 and then not Is_Itype (Discr_Type)
20825 then
20826 if Can_Never_Be_Null (Discr_Type) then
20827 Error_Msg_NE
20828 ("`NOT NULL` not allowed (& already excludes null)",
20829 Discr,
20830 Discr_Type);
20831 end if;
20833 Set_Etype (Defining_Identifier (Discr),
20834 Create_Null_Excluding_Itype
20835 (T => Discr_Type,
20836 Related_Nod => Discr));
20838 -- Check for improper null exclusion if the type is otherwise
20839 -- legal for a discriminant.
20841 elsif Null_Exclusion_Present (Discr)
20842 and then Is_Discrete_Type (Discr_Type)
20843 then
20844 Error_Msg_N
20845 ("null exclusion can only apply to an access type", Discr);
20846 end if;
20848 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20849 -- can't have defaults. Synchronized types, or types that are
20850 -- explicitly limited are fine, but special tests apply to derived
20851 -- types in generics: in a generic body we have to assume the
20852 -- worst, and therefore defaults are not allowed if the parent is
20853 -- a generic formal private type (see ACATS B370001).
20855 if Is_Access_Type (Discr_Type) and then Default_Present then
20856 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20857 or else Is_Limited_Record (Current_Scope)
20858 or else Is_Concurrent_Type (Current_Scope)
20859 or else Is_Concurrent_Record_Type (Current_Scope)
20860 or else Ekind (Current_Scope) = E_Limited_Private_Type
20861 then
20862 if not Is_Derived_Type (Current_Scope)
20863 or else not Is_Generic_Type (Etype (Current_Scope))
20864 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20865 or else Limited_Present
20866 (Type_Definition (Parent (Current_Scope)))
20867 then
20868 null;
20870 else
20871 Error_Msg_N
20872 ("access discriminants of nonlimited types cannot "
20873 & "have defaults", Expression (Discr));
20874 end if;
20876 elsif Present (Expression (Discr)) then
20877 Error_Msg_N
20878 ("(Ada 2005) access discriminants of nonlimited types "
20879 & "cannot have defaults", Expression (Discr));
20880 end if;
20881 end if;
20882 end if;
20884 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20885 -- This check is relevant only when SPARK_Mode is on as it is not a
20886 -- standard Ada legality rule. The only way for a discriminant to be
20887 -- effectively volatile is to have an effectively volatile type, so
20888 -- we check this directly, because the Ekind of Discr might not be
20889 -- set yet (to help preventing cascaded errors on derived types).
20891 if SPARK_Mode = On
20892 and then Is_Effectively_Volatile (Discr_Type)
20893 then
20894 Error_Msg_N ("discriminant cannot be volatile", Discr);
20895 end if;
20897 Next (Discr);
20898 end loop;
20900 -- An element list consisting of the default expressions of the
20901 -- discriminants is constructed in the above loop and used to set
20902 -- the Discriminant_Constraint attribute for the type. If an object
20903 -- is declared of this (record or task) type without any explicit
20904 -- discriminant constraint given, this element list will form the
20905 -- actual parameters for the corresponding initialization procedure
20906 -- for the type.
20908 Set_Discriminant_Constraint (Current_Scope, Elist);
20909 Set_Stored_Constraint (Current_Scope, No_Elist);
20911 -- Default expressions must be provided either for all or for none
20912 -- of the discriminants of a discriminant part. (RM 3.7.1)
20914 if Default_Present and then Default_Not_Present then
20915 Error_Msg_N
20916 ("incomplete specification of defaults for discriminants", N);
20917 end if;
20919 -- The use of the name of a discriminant is not allowed in default
20920 -- expressions of a discriminant part if the specification of the
20921 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20923 -- To detect this, the discriminant names are entered initially with an
20924 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20925 -- attempt to use a void entity (for example in an expression that is
20926 -- type-checked) produces the error message: premature usage. Now after
20927 -- completing the semantic analysis of the discriminant part, we can set
20928 -- the Ekind of all the discriminants appropriately.
20930 Discr := First (Discriminant_Specifications (N));
20931 Discr_Number := Uint_1;
20932 while Present (Discr) loop
20933 Id := Defining_Identifier (Discr);
20935 if Ekind (Id) = E_In_Parameter then
20936 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
20937 end if;
20939 Mutate_Ekind (Id, E_Discriminant);
20940 Reinit_Component_Location (Id);
20941 Reinit_Esize (Id);
20942 Set_Discriminant_Number (Id, Discr_Number);
20944 -- Make sure this is always set, even in illegal programs
20946 Set_Corresponding_Discriminant (Id, Empty);
20948 -- Initialize the Original_Record_Component to the entity itself.
20949 -- Inherit_Components will propagate the right value to
20950 -- discriminants in derived record types.
20952 Set_Original_Record_Component (Id, Id);
20954 -- Create the discriminal for the discriminant
20956 Build_Discriminal (Id);
20958 Next (Discr);
20959 Discr_Number := Discr_Number + 1;
20960 end loop;
20962 Set_Has_Discriminants (Current_Scope);
20963 end Process_Discriminants;
20965 -----------------------
20966 -- Process_Full_View --
20967 -----------------------
20969 -- WARNING: This routine manages Ghost regions. Return statements must be
20970 -- replaced by gotos which jump to the end of the routine and restore the
20971 -- Ghost mode.
20973 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20974 procedure Collect_Implemented_Interfaces
20975 (Typ : Entity_Id;
20976 Ifaces : Elist_Id);
20977 -- Ada 2005: Gather all the interfaces that Typ directly or
20978 -- inherently implements. Duplicate entries are not added to
20979 -- the list Ifaces.
20981 ------------------------------------
20982 -- Collect_Implemented_Interfaces --
20983 ------------------------------------
20985 procedure Collect_Implemented_Interfaces
20986 (Typ : Entity_Id;
20987 Ifaces : Elist_Id)
20989 Iface : Entity_Id;
20990 Iface_Elmt : Elmt_Id;
20992 begin
20993 -- Abstract interfaces are only associated with tagged record types
20995 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20996 return;
20997 end if;
20999 -- Recursively climb to the ancestors
21001 if Etype (Typ) /= Typ
21003 -- Protect the frontend against wrong cyclic declarations like:
21005 -- type B is new A with private;
21006 -- type C is new A with private;
21007 -- private
21008 -- type B is new C with null record;
21009 -- type C is new B with null record;
21011 and then Etype (Typ) /= Priv_T
21012 and then Etype (Typ) /= Full_T
21013 then
21014 -- Keep separate the management of private type declarations
21016 if Ekind (Typ) = E_Record_Type_With_Private then
21018 -- Handle the following illegal usage:
21019 -- type Private_Type is tagged private;
21020 -- private
21021 -- type Private_Type is new Type_Implementing_Iface;
21023 if Present (Full_View (Typ))
21024 and then Etype (Typ) /= Full_View (Typ)
21025 then
21026 if Is_Interface (Etype (Typ)) then
21027 Append_Unique_Elmt (Etype (Typ), Ifaces);
21028 end if;
21030 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21031 end if;
21033 -- Non-private types
21035 else
21036 if Is_Interface (Etype (Typ)) then
21037 Append_Unique_Elmt (Etype (Typ), Ifaces);
21038 end if;
21040 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21041 end if;
21042 end if;
21044 -- Handle entities in the list of abstract interfaces
21046 if Present (Interfaces (Typ)) then
21047 Iface_Elmt := First_Elmt (Interfaces (Typ));
21048 while Present (Iface_Elmt) loop
21049 Iface := Node (Iface_Elmt);
21051 pragma Assert (Is_Interface (Iface));
21053 if not Contain_Interface (Iface, Ifaces) then
21054 Append_Elmt (Iface, Ifaces);
21055 Collect_Implemented_Interfaces (Iface, Ifaces);
21056 end if;
21058 Next_Elmt (Iface_Elmt);
21059 end loop;
21060 end if;
21061 end Collect_Implemented_Interfaces;
21063 -- Local variables
21065 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21066 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21067 -- Save the Ghost-related attributes to restore on exit
21069 Full_Indic : Node_Id;
21070 Full_Parent : Entity_Id;
21071 Priv_Parent : Entity_Id;
21073 -- Start of processing for Process_Full_View
21075 begin
21076 Mark_And_Set_Ghost_Completion (N, Priv_T);
21078 -- First some sanity checks that must be done after semantic
21079 -- decoration of the full view and thus cannot be placed with other
21080 -- similar checks in Find_Type_Name
21082 if not Is_Limited_Type (Priv_T)
21083 and then (Is_Limited_Type (Full_T)
21084 or else Is_Limited_Composite (Full_T))
21085 then
21086 if In_Instance then
21087 null;
21088 else
21089 Error_Msg_N
21090 ("completion of nonlimited type cannot be limited", Full_T);
21091 Explain_Limited_Type (Full_T, Full_T);
21092 end if;
21094 elsif Is_Abstract_Type (Full_T)
21095 and then not Is_Abstract_Type (Priv_T)
21096 then
21097 Error_Msg_N
21098 ("completion of nonabstract type cannot be abstract", Full_T);
21100 elsif Is_Tagged_Type (Priv_T)
21101 and then Is_Limited_Type (Priv_T)
21102 and then not Is_Limited_Type (Full_T)
21103 then
21104 -- If pragma CPP_Class was applied to the private declaration
21105 -- propagate the limitedness to the full-view
21107 if Is_CPP_Class (Priv_T) then
21108 Set_Is_Limited_Record (Full_T);
21110 -- GNAT allow its own definition of Limited_Controlled to disobey
21111 -- this rule in order in ease the implementation. This test is safe
21112 -- because Root_Controlled is defined in a child of System that
21113 -- normal programs are not supposed to use.
21115 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21116 Set_Is_Limited_Composite (Full_T);
21117 else
21118 Error_Msg_N
21119 ("completion of limited tagged type must be limited", Full_T);
21120 end if;
21122 elsif Is_Generic_Type (Priv_T) then
21123 Error_Msg_N ("generic type cannot have a completion", Full_T);
21124 end if;
21126 -- Check that ancestor interfaces of private and full views are
21127 -- consistent. We omit this check for synchronized types because
21128 -- they are performed on the corresponding record type when frozen.
21130 if Ada_Version >= Ada_2005
21131 and then Is_Tagged_Type (Priv_T)
21132 and then Is_Tagged_Type (Full_T)
21133 and then not Is_Concurrent_Type (Full_T)
21134 then
21135 declare
21136 Iface : Entity_Id;
21137 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21138 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21140 begin
21141 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21142 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21144 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21145 -- an interface type if and only if the full type is descendant
21146 -- of the interface type (AARM 7.3 (7.3/2)).
21148 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21150 if Present (Iface) then
21151 Error_Msg_NE
21152 ("interface in partial view& not implemented by full type "
21153 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21154 end if;
21156 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21158 if Present (Iface) then
21159 Error_Msg_NE
21160 ("interface & not implemented by partial view "
21161 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21162 end if;
21163 end;
21164 end if;
21166 if Is_Tagged_Type (Priv_T)
21167 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21168 and then Is_Derived_Type (Full_T)
21169 then
21170 Priv_Parent := Etype (Priv_T);
21172 -- The full view of a private extension may have been transformed
21173 -- into an unconstrained derived type declaration and a subtype
21174 -- declaration (see build_derived_record_type for details).
21176 if Nkind (N) = N_Subtype_Declaration then
21177 Full_Indic := Subtype_Indication (N);
21178 Full_Parent := Etype (Base_Type (Full_T));
21179 else
21180 Full_Indic := Subtype_Indication (Type_Definition (N));
21181 Full_Parent := Etype (Full_T);
21182 end if;
21184 -- Check that the parent type of the full type is a descendant of
21185 -- the ancestor subtype given in the private extension. If either
21186 -- entity has an Etype equal to Any_Type then we had some previous
21187 -- error situation [7.3(8)].
21189 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21190 goto Leave;
21192 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21193 -- any order. Therefore we don't have to check that its parent must
21194 -- be a descendant of the parent of the private type declaration.
21196 elsif Is_Interface (Priv_Parent)
21197 and then Is_Interface (Full_Parent)
21198 then
21199 null;
21201 -- Ada 2005 (AI-251): If the parent of the private type declaration
21202 -- is an interface there is no need to check that it is an ancestor
21203 -- of the associated full type declaration. The required tests for
21204 -- this case are performed by Build_Derived_Record_Type.
21206 elsif not Is_Interface (Base_Type (Priv_Parent))
21207 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21208 then
21209 Error_Msg_N
21210 ("parent of full type must descend from parent of private "
21211 & "extension", Full_Indic);
21213 -- First check a formal restriction, and then proceed with checking
21214 -- Ada rules. Since the formal restriction is not a serious error, we
21215 -- don't prevent further error detection for this check, hence the
21216 -- ELSE.
21218 else
21219 -- Check the rules of 7.3(10): if the private extension inherits
21220 -- known discriminants, then the full type must also inherit those
21221 -- discriminants from the same (ancestor) type, and the parent
21222 -- subtype of the full type must be constrained if and only if
21223 -- the ancestor subtype of the private extension is constrained.
21225 if No (Discriminant_Specifications (Parent (Priv_T)))
21226 and then not Has_Unknown_Discriminants (Priv_T)
21227 and then Has_Discriminants (Base_Type (Priv_Parent))
21228 then
21229 declare
21230 Priv_Indic : constant Node_Id :=
21231 Subtype_Indication (Parent (Priv_T));
21233 Priv_Constr : constant Boolean :=
21234 Is_Constrained (Priv_Parent)
21235 or else
21236 Nkind (Priv_Indic) = N_Subtype_Indication
21237 or else
21238 Is_Constrained (Entity (Priv_Indic));
21240 Full_Constr : constant Boolean :=
21241 Is_Constrained (Full_Parent)
21242 or else
21243 Nkind (Full_Indic) = N_Subtype_Indication
21244 or else
21245 Is_Constrained (Entity (Full_Indic));
21247 Priv_Discr : Entity_Id;
21248 Full_Discr : Entity_Id;
21250 begin
21251 Priv_Discr := First_Discriminant (Priv_Parent);
21252 Full_Discr := First_Discriminant (Full_Parent);
21253 while Present (Priv_Discr) and then Present (Full_Discr) loop
21254 if Original_Record_Component (Priv_Discr) =
21255 Original_Record_Component (Full_Discr)
21256 or else
21257 Corresponding_Discriminant (Priv_Discr) =
21258 Corresponding_Discriminant (Full_Discr)
21259 then
21260 null;
21261 else
21262 exit;
21263 end if;
21265 Next_Discriminant (Priv_Discr);
21266 Next_Discriminant (Full_Discr);
21267 end loop;
21269 if Present (Priv_Discr) or else Present (Full_Discr) then
21270 Error_Msg_N
21271 ("full view must inherit discriminants of the parent "
21272 & "type used in the private extension", Full_Indic);
21274 elsif Priv_Constr and then not Full_Constr then
21275 Error_Msg_N
21276 ("parent subtype of full type must be constrained",
21277 Full_Indic);
21279 elsif Full_Constr and then not Priv_Constr then
21280 Error_Msg_N
21281 ("parent subtype of full type must be unconstrained",
21282 Full_Indic);
21283 end if;
21284 end;
21286 -- Check the rules of 7.3(12): if a partial view has neither
21287 -- known or unknown discriminants, then the full type
21288 -- declaration shall define a definite subtype.
21290 elsif not Has_Unknown_Discriminants (Priv_T)
21291 and then not Has_Discriminants (Priv_T)
21292 and then not Is_Constrained (Full_T)
21293 then
21294 Error_Msg_N
21295 ("full view must define a constrained type if partial view "
21296 & "has no discriminants", Full_T);
21297 end if;
21299 -- Do we implement the following properly???
21300 -- If the ancestor subtype of a private extension has constrained
21301 -- discriminants, then the parent subtype of the full view shall
21302 -- impose a statically matching constraint on those discriminants
21303 -- [7.3(13)].
21304 end if;
21306 else
21307 -- For untagged types, verify that a type without discriminants is
21308 -- not completed with an unconstrained type. A separate error message
21309 -- is produced if the full type has defaulted discriminants.
21311 if Is_Definite_Subtype (Priv_T)
21312 and then not Is_Definite_Subtype (Full_T)
21313 then
21314 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21315 Error_Msg_NE
21316 ("full view of& not compatible with declaration#",
21317 Full_T, Priv_T);
21319 if not Is_Tagged_Type (Full_T) then
21320 Error_Msg_N
21321 ("\one is constrained, the other unconstrained", Full_T);
21322 end if;
21323 end if;
21324 end if;
21326 -- AI-419: verify that the use of "limited" is consistent
21328 declare
21329 Orig_Decl : constant Node_Id := Original_Node (N);
21331 begin
21332 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21333 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21334 and then Nkind
21335 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21336 then
21337 if not Limited_Present (Parent (Priv_T))
21338 and then not Synchronized_Present (Parent (Priv_T))
21339 and then Limited_Present (Type_Definition (Orig_Decl))
21340 then
21341 Error_Msg_N
21342 ("full view of non-limited extension cannot be limited", N);
21344 -- Conversely, if the partial view carries the limited keyword,
21345 -- the full view must as well, even if it may be redundant.
21347 elsif Limited_Present (Parent (Priv_T))
21348 and then not Limited_Present (Type_Definition (Orig_Decl))
21349 then
21350 Error_Msg_N
21351 ("full view of limited extension must be explicitly limited",
21353 end if;
21354 end if;
21355 end;
21357 -- Ada 2005 (AI-443): A synchronized private extension must be
21358 -- completed by a task or protected type.
21360 if Ada_Version >= Ada_2005
21361 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21362 and then Synchronized_Present (Parent (Priv_T))
21363 and then not Is_Concurrent_Type (Full_T)
21364 then
21365 Error_Msg_N ("full view of synchronized extension must " &
21366 "be synchronized type", N);
21367 end if;
21369 -- Ada 2005 AI-363: if the full view has discriminants with
21370 -- defaults, it is illegal to declare constrained access subtypes
21371 -- whose designated type is the current type. This allows objects
21372 -- of the type that are declared in the heap to be unconstrained.
21374 if not Has_Unknown_Discriminants (Priv_T)
21375 and then not Has_Discriminants (Priv_T)
21376 and then Has_Defaulted_Discriminants (Full_T)
21377 then
21378 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21379 Set_Has_Constrained_Partial_View (Priv_T);
21380 end if;
21382 -- Create a full declaration for all its subtypes recorded in
21383 -- Private_Dependents and swap them similarly to the base type. These
21384 -- are subtypes that have been define before the full declaration of
21385 -- the private type. We also swap the entry in Private_Dependents list
21386 -- so we can properly restore the private view on exit from the scope.
21388 declare
21389 Priv_Elmt : Elmt_Id;
21390 Priv_Scop : Entity_Id;
21391 Priv : Entity_Id;
21392 Full : Entity_Id;
21394 begin
21395 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21396 while Present (Priv_Elmt) loop
21397 Priv := Node (Priv_Elmt);
21398 Priv_Scop := Scope (Priv);
21400 if Ekind (Priv) in E_Private_Subtype
21401 | E_Limited_Private_Subtype
21402 | E_Record_Subtype_With_Private
21403 then
21404 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21405 Set_Is_Itype (Full);
21406 Set_Parent (Full, Parent (Priv));
21407 Set_Associated_Node_For_Itype (Full, N);
21409 -- Now we need to complete the private subtype, but since the
21410 -- base type has already been swapped, we must also swap the
21411 -- subtypes (and thus, reverse the arguments in the call to
21412 -- Complete_Private_Subtype). Also note that we may need to
21413 -- re-establish the scope of the private subtype.
21415 Copy_And_Swap (Priv, Full);
21417 if not In_Open_Scopes (Priv_Scop) then
21418 Push_Scope (Priv_Scop);
21420 else
21421 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21423 Priv_Scop := Empty;
21424 end if;
21426 Complete_Private_Subtype (Full, Priv, Full_T, N);
21427 Set_Full_View (Full, Priv);
21429 if Present (Priv_Scop) then
21430 Pop_Scope;
21431 end if;
21433 Replace_Elmt (Priv_Elmt, Full);
21434 end if;
21436 Next_Elmt (Priv_Elmt);
21437 end loop;
21438 end;
21440 declare
21441 Disp_Typ : Entity_Id;
21442 Full_List : Elist_Id;
21443 Prim : Entity_Id;
21444 Prim_Elmt : Elmt_Id;
21445 Priv_List : Elist_Id;
21447 function Contains
21448 (E : Entity_Id;
21449 L : Elist_Id) return Boolean;
21450 -- Determine whether list L contains element E
21452 --------------
21453 -- Contains --
21454 --------------
21456 function Contains
21457 (E : Entity_Id;
21458 L : Elist_Id) return Boolean
21460 List_Elmt : Elmt_Id;
21462 begin
21463 List_Elmt := First_Elmt (L);
21464 while Present (List_Elmt) loop
21465 if Node (List_Elmt) = E then
21466 return True;
21467 end if;
21469 Next_Elmt (List_Elmt);
21470 end loop;
21472 return False;
21473 end Contains;
21475 -- Start of processing
21477 begin
21478 -- If the private view was tagged, copy the new primitive operations
21479 -- from the private view to the full view.
21481 if Is_Tagged_Type (Full_T) then
21482 if Is_Tagged_Type (Priv_T) then
21483 Priv_List := Primitive_Operations (Priv_T);
21484 Prim_Elmt := First_Elmt (Priv_List);
21486 -- In the case of a concurrent type completing a private tagged
21487 -- type, primitives may have been declared in between the two
21488 -- views. These subprograms need to be wrapped the same way
21489 -- entries and protected procedures are handled because they
21490 -- cannot be directly shared by the two views.
21492 if Is_Concurrent_Type (Full_T) then
21493 declare
21494 Conc_Typ : constant Entity_Id :=
21495 Corresponding_Record_Type (Full_T);
21496 Curr_Nod : Node_Id := Parent (Conc_Typ);
21497 Wrap_Spec : Node_Id;
21499 begin
21500 while Present (Prim_Elmt) loop
21501 Prim := Node (Prim_Elmt);
21503 if Comes_From_Source (Prim)
21504 and then not Is_Abstract_Subprogram (Prim)
21505 then
21506 Wrap_Spec :=
21507 Make_Subprogram_Declaration (Sloc (Prim),
21508 Specification =>
21509 Build_Wrapper_Spec
21510 (Subp_Id => Prim,
21511 Obj_Typ => Conc_Typ,
21512 Formals =>
21513 Parameter_Specifications
21514 (Parent (Prim))));
21516 Insert_After (Curr_Nod, Wrap_Spec);
21517 Curr_Nod := Wrap_Spec;
21519 Analyze (Wrap_Spec);
21521 -- Remove the wrapper from visibility to avoid
21522 -- spurious conflict with the wrapped entity.
21524 Set_Is_Immediately_Visible
21525 (Defining_Entity (Specification (Wrap_Spec)),
21526 False);
21527 end if;
21529 Next_Elmt (Prim_Elmt);
21530 end loop;
21532 goto Leave;
21533 end;
21535 -- For nonconcurrent types, transfer explicit primitives, but
21536 -- omit those inherited from the parent of the private view
21537 -- since they will be re-inherited later on.
21539 else
21540 Full_List := Primitive_Operations (Full_T);
21541 while Present (Prim_Elmt) loop
21542 Prim := Node (Prim_Elmt);
21544 if Comes_From_Source (Prim)
21545 and then not Contains (Prim, Full_List)
21546 then
21547 Append_Elmt (Prim, Full_List);
21548 end if;
21550 Next_Elmt (Prim_Elmt);
21551 end loop;
21552 end if;
21554 -- Untagged private view
21556 else
21557 Full_List := Primitive_Operations (Full_T);
21559 -- In this case the partial view is untagged, so here we locate
21560 -- all of the earlier primitives that need to be treated as
21561 -- dispatching (those that appear between the two views). Note
21562 -- that these additional operations must all be new operations
21563 -- (any earlier operations that override inherited operations
21564 -- of the full view will already have been inserted in the
21565 -- primitives list, marked by Check_Operation_From_Private_View
21566 -- as dispatching. Note that implicit "/=" operators are
21567 -- excluded from being added to the primitives list since they
21568 -- shouldn't be treated as dispatching (tagged "/=" is handled
21569 -- specially).
21571 Prim := Next_Entity (Full_T);
21572 while Present (Prim) and then Prim /= Priv_T loop
21573 if Ekind (Prim) in E_Procedure | E_Function then
21574 Disp_Typ := Find_Dispatching_Type (Prim);
21576 if Disp_Typ = Full_T
21577 and then (Chars (Prim) /= Name_Op_Ne
21578 or else Comes_From_Source (Prim))
21579 then
21580 Check_Controlling_Formals (Full_T, Prim);
21582 if Is_Suitable_Primitive (Prim)
21583 and then not Is_Dispatching_Operation (Prim)
21584 then
21585 Append_Elmt (Prim, Full_List);
21586 Set_Is_Dispatching_Operation (Prim);
21587 Set_DT_Position_Value (Prim, No_Uint);
21588 end if;
21590 elsif Is_Dispatching_Operation (Prim)
21591 and then Disp_Typ /= Full_T
21592 then
21593 -- Verify that it is not otherwise controlled by a
21594 -- formal or a return value of type T.
21596 Check_Controlling_Formals (Disp_Typ, Prim);
21597 end if;
21598 end if;
21600 Next_Entity (Prim);
21601 end loop;
21602 end if;
21604 -- For the tagged case, the two views can share the same primitive
21605 -- operations list and the same class-wide type. Update attributes
21606 -- of the class-wide type which depend on the full declaration.
21608 if Is_Tagged_Type (Priv_T) then
21609 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21610 Set_Class_Wide_Type
21611 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21613 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21614 end if;
21616 -- For untagged types, copy the primitives across from the private
21617 -- view to the full view, for support of prefixed calls when
21618 -- extensions are enabled, and better error messages otherwise.
21620 else
21621 Priv_List := Primitive_Operations (Priv_T);
21622 Prim_Elmt := First_Elmt (Priv_List);
21624 Full_List := Primitive_Operations (Full_T);
21625 while Present (Prim_Elmt) loop
21626 Prim := Node (Prim_Elmt);
21627 Append_Elmt (Prim, Full_List);
21628 Next_Elmt (Prim_Elmt);
21629 end loop;
21630 end if;
21631 end;
21633 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21635 if Known_To_Have_Preelab_Init (Priv_T) then
21637 -- Case where there is a pragma Preelaborable_Initialization. We
21638 -- always allow this in predefined units, which is cheating a bit,
21639 -- but it means we don't have to struggle to meet the requirements in
21640 -- the RM for having Preelaborable Initialization. Otherwise we
21641 -- require that the type meets the RM rules. But we can't check that
21642 -- yet, because of the rule about overriding Initialize, so we simply
21643 -- set a flag that will be checked at freeze time.
21645 if not In_Predefined_Unit (Full_T) then
21646 Set_Must_Have_Preelab_Init (Full_T);
21647 end if;
21648 end if;
21650 -- If pragma CPP_Class was applied to the private type declaration,
21651 -- propagate it now to the full type declaration.
21653 if Is_CPP_Class (Priv_T) then
21654 Set_Is_CPP_Class (Full_T);
21655 Set_Convention (Full_T, Convention_CPP);
21657 -- Check that components of imported CPP types do not have default
21658 -- expressions.
21660 Check_CPP_Type_Has_No_Defaults (Full_T);
21661 end if;
21663 -- If the private view has user specified stream attributes, then so has
21664 -- the full view.
21666 -- Why the test, how could these flags be already set in Full_T ???
21668 if Has_Specified_Stream_Read (Priv_T) then
21669 Set_Has_Specified_Stream_Read (Full_T);
21670 end if;
21672 if Has_Specified_Stream_Write (Priv_T) then
21673 Set_Has_Specified_Stream_Write (Full_T);
21674 end if;
21676 if Has_Specified_Stream_Input (Priv_T) then
21677 Set_Has_Specified_Stream_Input (Full_T);
21678 end if;
21680 if Has_Specified_Stream_Output (Priv_T) then
21681 Set_Has_Specified_Stream_Output (Full_T);
21682 end if;
21684 -- Propagate Default_Initial_Condition-related attributes from the
21685 -- partial view to the full view.
21687 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21689 -- And to the underlying full view, if any
21691 if Is_Private_Type (Full_T)
21692 and then Present (Underlying_Full_View (Full_T))
21693 then
21694 Propagate_DIC_Attributes
21695 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21696 end if;
21698 -- Propagate invariant-related attributes from the partial view to the
21699 -- full view.
21701 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21703 -- And to the underlying full view, if any
21705 if Is_Private_Type (Full_T)
21706 and then Present (Underlying_Full_View (Full_T))
21707 then
21708 Propagate_Invariant_Attributes
21709 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21710 end if;
21712 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21713 -- in the full view without advertising the inheritance in the partial
21714 -- view. This can only occur when the partial view has no parent type
21715 -- and the full view has an interface as a parent. Any other scenarios
21716 -- are illegal because implemented interfaces must match between the
21717 -- two views.
21719 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21720 declare
21721 Full_Par : constant Entity_Id := Etype (Full_T);
21722 Priv_Par : constant Entity_Id := Etype (Priv_T);
21724 begin
21725 if not Is_Interface (Priv_Par)
21726 and then Is_Interface (Full_Par)
21727 and then Has_Inheritable_Invariants (Full_Par)
21728 then
21729 Error_Msg_N
21730 ("hidden inheritance of class-wide type invariants not "
21731 & "allowed", N);
21732 end if;
21733 end;
21734 end if;
21736 -- Propagate predicates to full type, and predicate function if already
21737 -- defined. It is not clear that this can actually happen? the partial
21738 -- view cannot be frozen yet, and the predicate function has not been
21739 -- built. Still it is a cheap check and seems safer to make it.
21741 Propagate_Predicate_Attributes (Full_T, Priv_T);
21743 if Is_Private_Type (Full_T)
21744 and then Present (Underlying_Full_View (Full_T))
21745 then
21746 Propagate_Predicate_Attributes
21747 (Underlying_Full_View (Full_T), Priv_T);
21748 end if;
21750 <<Leave>>
21751 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21752 end Process_Full_View;
21754 -----------------------------------
21755 -- Process_Incomplete_Dependents --
21756 -----------------------------------
21758 procedure Process_Incomplete_Dependents
21759 (N : Node_Id;
21760 Full_T : Entity_Id;
21761 Inc_T : Entity_Id)
21763 Inc_Elmt : Elmt_Id;
21764 Priv_Dep : Entity_Id;
21765 New_Subt : Entity_Id;
21767 Disc_Constraint : Elist_Id;
21769 begin
21770 if No (Private_Dependents (Inc_T)) then
21771 return;
21772 end if;
21774 -- Itypes that may be generated by the completion of an incomplete
21775 -- subtype are not used by the back-end and not attached to the tree.
21776 -- They are created only for constraint-checking purposes.
21778 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21779 while Present (Inc_Elmt) loop
21780 Priv_Dep := Node (Inc_Elmt);
21782 if Ekind (Priv_Dep) = E_Subprogram_Type then
21784 -- An Access_To_Subprogram type may have a return type or a
21785 -- parameter type that is incomplete. Replace with the full view.
21787 if Etype (Priv_Dep) = Inc_T then
21788 Set_Etype (Priv_Dep, Full_T);
21789 end if;
21791 declare
21792 Formal : Entity_Id;
21794 begin
21795 Formal := First_Formal (Priv_Dep);
21796 while Present (Formal) loop
21797 if Etype (Formal) = Inc_T then
21798 Set_Etype (Formal, Full_T);
21799 end if;
21801 Next_Formal (Formal);
21802 end loop;
21803 end;
21805 elsif Is_Overloadable (Priv_Dep) then
21807 -- If a subprogram in the incomplete dependents list is primitive
21808 -- for a tagged full type then mark it as a dispatching operation,
21809 -- check whether it overrides an inherited subprogram, and check
21810 -- restrictions on its controlling formals. Note that a protected
21811 -- operation is never dispatching: only its wrapper operation
21812 -- (which has convention Ada) is.
21814 if Is_Tagged_Type (Full_T)
21815 and then Is_Primitive (Priv_Dep)
21816 and then Convention (Priv_Dep) /= Convention_Protected
21817 then
21818 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21819 Set_Is_Dispatching_Operation (Priv_Dep);
21820 Check_Controlling_Formals (Full_T, Priv_Dep);
21821 end if;
21823 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21825 -- Can happen during processing of a body before the completion
21826 -- of a TA type. Ignore, because spec is also on dependent list.
21828 return;
21830 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21831 -- corresponding subtype of the full view.
21833 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21834 and then Comes_From_Source (Priv_Dep)
21835 then
21836 Set_Subtype_Indication
21837 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21838 Reinit_Field_To_Zero
21839 (Priv_Dep, F_Private_Dependents,
21840 Old_Ekind => E_Incomplete_Subtype);
21841 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21842 Set_Etype (Priv_Dep, Full_T);
21843 Set_Analyzed (Parent (Priv_Dep), False);
21845 -- Reanalyze the declaration, suppressing the call to Enter_Name
21846 -- to avoid duplicate names.
21848 Analyze_Subtype_Declaration
21849 (N => Parent (Priv_Dep),
21850 Skip => True);
21852 -- Dependent is a subtype
21854 else
21855 -- We build a new subtype indication using the full view of the
21856 -- incomplete parent. The discriminant constraints have been
21857 -- elaborated already at the point of the subtype declaration.
21859 New_Subt := Create_Itype (E_Void, N);
21861 if Has_Discriminants (Full_T) then
21862 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21863 else
21864 Disc_Constraint := No_Elist;
21865 end if;
21867 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21868 Set_Full_View (Priv_Dep, New_Subt);
21869 end if;
21871 Next_Elmt (Inc_Elmt);
21872 end loop;
21873 end Process_Incomplete_Dependents;
21875 --------------------------------
21876 -- Process_Range_Expr_In_Decl --
21877 --------------------------------
21879 procedure Process_Range_Expr_In_Decl
21880 (R : Node_Id;
21881 T : Entity_Id;
21882 Subtyp : Entity_Id := Empty;
21883 Check_List : List_Id := No_List)
21885 Lo, Hi : Node_Id;
21886 R_Checks : Check_Result;
21887 Insert_Node : Node_Id;
21888 Def_Id : Entity_Id;
21890 begin
21891 Analyze_And_Resolve (R, Base_Type (T));
21893 if Nkind (R) = N_Range then
21894 Lo := Low_Bound (R);
21895 Hi := High_Bound (R);
21897 -- Validity checks on the range of a quantified expression are
21898 -- delayed until the construct is transformed into a loop.
21900 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21901 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21902 then
21903 null;
21905 -- We need to ensure validity of the bounds here, because if we
21906 -- go ahead and do the expansion, then the expanded code will get
21907 -- analyzed with range checks suppressed and we miss the check.
21909 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21910 -- the temporaries generated by routine Remove_Side_Effects by means
21911 -- of validity checks must use the same names. When a range appears
21912 -- in the parent of a generic, the range is processed with checks
21913 -- disabled as part of the generic context and with checks enabled
21914 -- for code generation purposes. This leads to link issues as the
21915 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21916 -- template sees the temporaries generated by Remove_Side_Effects.
21918 else
21919 Validity_Check_Range (R, Subtyp);
21920 end if;
21922 -- If there were errors in the declaration, try and patch up some
21923 -- common mistakes in the bounds. The cases handled are literals
21924 -- which are Integer where the expected type is Real and vice versa.
21925 -- These corrections allow the compilation process to proceed further
21926 -- along since some basic assumptions of the format of the bounds
21927 -- are guaranteed.
21929 if Etype (R) = Any_Type then
21930 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21931 Rewrite (Lo,
21932 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21934 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21935 Rewrite (Hi,
21936 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21938 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21939 Rewrite (Lo,
21940 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21942 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21943 Rewrite (Hi,
21944 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21945 end if;
21947 Set_Etype (Lo, T);
21948 Set_Etype (Hi, T);
21949 end if;
21951 -- If the bounds of the range have been mistakenly given as string
21952 -- literals (perhaps in place of character literals), then an error
21953 -- has already been reported, but we rewrite the string literal as a
21954 -- bound of the range's type to avoid blowups in later processing
21955 -- that looks at static values.
21957 if Nkind (Lo) = N_String_Literal then
21958 Rewrite (Lo,
21959 Make_Attribute_Reference (Sloc (Lo),
21960 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21961 Attribute_Name => Name_First));
21962 Analyze_And_Resolve (Lo);
21963 end if;
21965 if Nkind (Hi) = N_String_Literal then
21966 Rewrite (Hi,
21967 Make_Attribute_Reference (Sloc (Hi),
21968 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21969 Attribute_Name => Name_First));
21970 Analyze_And_Resolve (Hi);
21971 end if;
21973 -- If bounds aren't scalar at this point then exit, avoiding
21974 -- problems with further processing of the range in this procedure.
21976 if not Is_Scalar_Type (Etype (Lo)) then
21977 return;
21978 end if;
21980 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21981 -- then range of the base type. Here we check whether the bounds
21982 -- are in the range of the subtype itself. Note that if the bounds
21983 -- represent the null range the Constraint_Error exception should
21984 -- not be raised.
21986 -- Capture values of bounds and generate temporaries for them
21987 -- if needed, before applying checks, since checks may cause
21988 -- duplication of the expression without forcing evaluation.
21990 -- The forced evaluation removes side effects from expressions,
21991 -- which should occur also in GNATprove mode. Otherwise, we end up
21992 -- with unexpected insertions of actions at places where this is
21993 -- not supposed to occur, e.g. on default parameters of a call.
21995 if Expander_Active or GNATprove_Mode then
21997 -- Call Force_Evaluation to create declarations as needed
21998 -- to deal with side effects, and also create typ_FIRST/LAST
21999 -- entities for bounds if we have a subtype name.
22001 -- Note: we do this transformation even if expansion is not
22002 -- active if we are in GNATprove_Mode since the transformation
22003 -- is in general required to ensure that the resulting tree has
22004 -- proper Ada semantics.
22006 Force_Evaluation
22007 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22008 Force_Evaluation
22009 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22010 end if;
22012 -- We use a flag here instead of suppressing checks on the type
22013 -- because the type we check against isn't necessarily the place
22014 -- where we put the check.
22016 R_Checks := Get_Range_Checks (R, T);
22018 -- Look up tree to find an appropriate insertion point. We can't
22019 -- just use insert_actions because later processing depends on
22020 -- the insertion node. Prior to Ada 2012 the insertion point could
22021 -- only be a declaration or a loop, but quantified expressions can
22022 -- appear within any context in an expression, and the insertion
22023 -- point can be any statement, pragma, or declaration.
22025 Insert_Node := Parent (R);
22026 while Present (Insert_Node) loop
22027 exit when
22028 Nkind (Insert_Node) in N_Declaration
22029 and then
22030 Nkind (Insert_Node) not in N_Component_Declaration
22031 | N_Loop_Parameter_Specification
22032 | N_Function_Specification
22033 | N_Procedure_Specification;
22035 exit when Nkind (Insert_Node) in
22036 N_Later_Decl_Item |
22037 N_Statement_Other_Than_Procedure_Call |
22038 N_Procedure_Call_Statement |
22039 N_Pragma;
22041 Insert_Node := Parent (Insert_Node);
22042 end loop;
22044 if Present (Insert_Node) then
22046 -- Case of loop statement. Verify that the range is part of the
22047 -- subtype indication of the iteration scheme.
22049 if Nkind (Insert_Node) = N_Loop_Statement then
22050 declare
22051 Indic : Node_Id;
22053 begin
22054 Indic := Parent (R);
22055 while Present (Indic)
22056 and then Nkind (Indic) /= N_Subtype_Indication
22057 loop
22058 Indic := Parent (Indic);
22059 end loop;
22061 if Present (Indic) then
22062 Def_Id := Etype (Subtype_Mark (Indic));
22064 Insert_Range_Checks
22065 (R_Checks,
22066 Insert_Node,
22067 Def_Id,
22068 Sloc (Insert_Node),
22069 Do_Before => True);
22070 end if;
22071 end;
22073 -- Case of declarations. If the declaration is for a type and
22074 -- involves discriminants, the checks are premature at the
22075 -- declaration point and need to wait for the expansion of the
22076 -- initialization procedure, which will pass in the list to put
22077 -- them on; otherwise, the checks are done at the declaration
22078 -- point and there is no need to do them again in the
22079 -- initialization procedure.
22081 elsif Nkind (Insert_Node) in N_Declaration then
22082 Def_Id := Defining_Identifier (Insert_Node);
22084 if (Ekind (Def_Id) = E_Record_Type
22085 and then Depends_On_Discriminant (R))
22086 or else
22087 (Ekind (Def_Id) = E_Protected_Type
22088 and then Has_Discriminants (Def_Id))
22089 then
22090 if Present (Check_List) then
22091 Append_Range_Checks
22092 (R_Checks,
22093 Check_List, Def_Id, Sloc (Insert_Node));
22094 end if;
22096 else
22097 if No (Check_List) then
22098 Insert_Range_Checks
22099 (R_Checks,
22100 Insert_Node, Def_Id, Sloc (Insert_Node));
22101 end if;
22102 end if;
22104 -- Case of statements. Drop the checks, as the range appears in
22105 -- the context of a quantified expression. Insertion will take
22106 -- place when expression is expanded.
22108 else
22109 null;
22110 end if;
22111 end if;
22113 -- Case of other than an explicit N_Range node
22115 -- The forced evaluation removes side effects from expressions, which
22116 -- should occur also in GNATprove mode. Otherwise, we end up with
22117 -- unexpected insertions of actions at places where this is not
22118 -- supposed to occur, e.g. on default parameters of a call.
22120 elsif Expander_Active or GNATprove_Mode then
22121 Get_Index_Bounds (R, Lo, Hi);
22122 Force_Evaluation (Lo);
22123 Force_Evaluation (Hi);
22124 end if;
22125 end Process_Range_Expr_In_Decl;
22127 --------------------------------------
22128 -- Process_Real_Range_Specification --
22129 --------------------------------------
22131 procedure Process_Real_Range_Specification (Def : Node_Id) is
22132 Spec : constant Node_Id := Real_Range_Specification (Def);
22133 Lo : Node_Id;
22134 Hi : Node_Id;
22135 Err : Boolean := False;
22137 procedure Analyze_Bound (N : Node_Id);
22138 -- Analyze and check one bound
22140 -------------------
22141 -- Analyze_Bound --
22142 -------------------
22144 procedure Analyze_Bound (N : Node_Id) is
22145 begin
22146 Analyze_And_Resolve (N, Any_Real);
22148 if not Is_OK_Static_Expression (N) then
22149 Flag_Non_Static_Expr
22150 ("bound in real type definition is not static!", N);
22151 Err := True;
22152 end if;
22153 end Analyze_Bound;
22155 -- Start of processing for Process_Real_Range_Specification
22157 begin
22158 if Present (Spec) then
22159 Lo := Low_Bound (Spec);
22160 Hi := High_Bound (Spec);
22161 Analyze_Bound (Lo);
22162 Analyze_Bound (Hi);
22164 -- If error, clear away junk range specification
22166 if Err then
22167 Set_Real_Range_Specification (Def, Empty);
22168 end if;
22169 end if;
22170 end Process_Real_Range_Specification;
22172 ---------------------
22173 -- Process_Subtype --
22174 ---------------------
22176 function Process_Subtype
22177 (S : Node_Id;
22178 Related_Nod : Node_Id;
22179 Related_Id : Entity_Id := Empty;
22180 Suffix : Character := ' ') return Entity_Id
22182 procedure Check_Incomplete (T : Node_Id);
22183 -- Called to verify that an incomplete type is not used prematurely
22185 ----------------------
22186 -- Check_Incomplete --
22187 ----------------------
22189 procedure Check_Incomplete (T : Node_Id) is
22190 begin
22191 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22193 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22194 and then
22195 not (Ada_Version >= Ada_2005
22196 and then
22197 (Nkind (Parent (T)) = N_Subtype_Declaration
22198 or else (Nkind (Parent (T)) = N_Subtype_Indication
22199 and then Nkind (Parent (Parent (T))) =
22200 N_Subtype_Declaration)))
22201 then
22202 Error_Msg_N ("invalid use of type before its full declaration", T);
22203 end if;
22204 end Check_Incomplete;
22206 -- Local variables
22208 P : Node_Id;
22209 Def_Id : Entity_Id;
22210 Error_Node : Node_Id;
22211 Full_View_Id : Entity_Id;
22212 Subtype_Mark_Id : Entity_Id;
22214 May_Have_Null_Exclusion : Boolean;
22216 -- Start of processing for Process_Subtype
22218 begin
22219 -- Case of no constraints present
22221 if Nkind (S) /= N_Subtype_Indication then
22222 Find_Type (S);
22224 -- No way to proceed if the subtype indication is malformed. This
22225 -- will happen for example when the subtype indication in an object
22226 -- declaration is missing altogether and the expression is analyzed
22227 -- as if it were that indication.
22229 if not Is_Entity_Name (S) then
22230 return Any_Type;
22231 end if;
22233 Check_Incomplete (S);
22234 P := Parent (S);
22236 -- The following mirroring of assertion in Null_Exclusion_Present is
22237 -- ugly, can't we have a range, a static predicate or even a flag???
22239 May_Have_Null_Exclusion :=
22240 Present (P)
22241 and then
22242 Nkind (P) in N_Access_Definition
22243 | N_Access_Function_Definition
22244 | N_Access_Procedure_Definition
22245 | N_Access_To_Object_Definition
22246 | N_Allocator
22247 | N_Component_Definition
22248 | N_Derived_Type_Definition
22249 | N_Discriminant_Specification
22250 | N_Formal_Object_Declaration
22251 | N_Function_Specification
22252 | N_Object_Declaration
22253 | N_Object_Renaming_Declaration
22254 | N_Parameter_Specification
22255 | N_Subtype_Declaration;
22257 -- Ada 2005 (AI-231): Static check
22259 if Ada_Version >= Ada_2005
22260 and then May_Have_Null_Exclusion
22261 and then Null_Exclusion_Present (P)
22262 and then Nkind (P) /= N_Access_To_Object_Definition
22263 and then not Is_Access_Type (Entity (S))
22264 then
22265 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22266 end if;
22268 -- Create an Itype that is a duplicate of Entity (S) but with the
22269 -- null-exclusion attribute.
22271 if May_Have_Null_Exclusion
22272 and then Is_Access_Type (Entity (S))
22273 and then Null_Exclusion_Present (P)
22275 -- No need to check the case of an access to object definition.
22276 -- It is correct to define double not-null pointers.
22278 -- Example:
22279 -- type Not_Null_Int_Ptr is not null access Integer;
22280 -- type Acc is not null access Not_Null_Int_Ptr;
22282 and then Nkind (P) /= N_Access_To_Object_Definition
22283 then
22284 if Can_Never_Be_Null (Entity (S)) then
22285 case Nkind (Related_Nod) is
22286 when N_Full_Type_Declaration =>
22287 if Nkind (Type_Definition (Related_Nod))
22288 in N_Array_Type_Definition
22289 then
22290 Error_Node :=
22291 Subtype_Indication
22292 (Component_Definition
22293 (Type_Definition (Related_Nod)));
22294 else
22295 Error_Node :=
22296 Subtype_Indication (Type_Definition (Related_Nod));
22297 end if;
22299 when N_Subtype_Declaration =>
22300 Error_Node := Subtype_Indication (Related_Nod);
22302 when N_Object_Declaration =>
22303 Error_Node := Object_Definition (Related_Nod);
22305 when N_Component_Declaration =>
22306 Error_Node :=
22307 Subtype_Indication (Component_Definition (Related_Nod));
22309 when N_Allocator =>
22310 Error_Node := Expression (Related_Nod);
22312 when others =>
22313 pragma Assert (False);
22314 Error_Node := Related_Nod;
22315 end case;
22317 Error_Msg_NE
22318 ("`NOT NULL` not allowed (& already excludes null)",
22319 Error_Node,
22320 Entity (S));
22321 end if;
22323 Set_Etype (S,
22324 Create_Null_Excluding_Itype
22325 (T => Entity (S),
22326 Related_Nod => P));
22327 Set_Entity (S, Etype (S));
22328 end if;
22330 return Entity (S);
22332 -- Case of constraint present, so that we have an N_Subtype_Indication
22333 -- node (this node is created only if constraints are present).
22335 else
22336 Find_Type (Subtype_Mark (S));
22338 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22339 and then not
22340 (Nkind (Parent (S)) = N_Subtype_Declaration
22341 and then Is_Itype (Defining_Identifier (Parent (S))))
22342 then
22343 Check_Incomplete (Subtype_Mark (S));
22344 end if;
22346 P := Parent (S);
22347 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22349 -- Explicit subtype declaration case
22351 if Nkind (P) = N_Subtype_Declaration then
22352 Def_Id := Defining_Identifier (P);
22354 -- Explicit derived type definition case
22356 elsif Nkind (P) = N_Derived_Type_Definition then
22357 Def_Id := Defining_Identifier (Parent (P));
22359 -- Implicit case, the Def_Id must be created as an implicit type.
22360 -- The one exception arises in the case of concurrent types, array
22361 -- and access types, where other subsidiary implicit types may be
22362 -- created and must appear before the main implicit type. In these
22363 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22364 -- has not yet been called to create Def_Id.
22366 else
22367 if Is_Array_Type (Subtype_Mark_Id)
22368 or else Is_Concurrent_Type (Subtype_Mark_Id)
22369 or else Is_Access_Type (Subtype_Mark_Id)
22370 then
22371 Def_Id := Empty;
22373 -- For the other cases, we create a new unattached Itype,
22374 -- and set the indication to ensure it gets attached later.
22376 else
22377 Def_Id :=
22378 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22379 end if;
22380 end if;
22382 -- If the kind of constraint is invalid for this kind of type,
22383 -- then give an error, and then pretend no constraint was given.
22385 if not Is_Valid_Constraint_Kind
22386 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22387 then
22388 Error_Msg_N
22389 ("incorrect constraint for this kind of type", Constraint (S));
22391 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22393 -- Set Ekind of orphan itype, to prevent cascaded errors
22395 if Present (Def_Id) then
22396 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22397 end if;
22399 -- Make recursive call, having got rid of the bogus constraint
22401 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22402 end if;
22404 -- Remaining processing depends on type. Select on Base_Type kind to
22405 -- ensure getting to the concrete type kind in the case of a private
22406 -- subtype (needed when only doing semantic analysis).
22408 case Ekind (Base_Type (Subtype_Mark_Id)) is
22409 when Access_Kind =>
22411 -- If this is a constraint on a class-wide type, discard it.
22412 -- There is currently no way to express a partial discriminant
22413 -- constraint on a type with unknown discriminants. This is
22414 -- a pathology that the ACATS wisely decides not to test.
22416 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22417 if Comes_From_Source (S) then
22418 Error_Msg_N
22419 ("constraint on class-wide type ignored??",
22420 Constraint (S));
22421 end if;
22423 if Nkind (P) = N_Subtype_Declaration then
22424 Set_Subtype_Indication (P,
22425 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22426 end if;
22428 return Subtype_Mark_Id;
22429 end if;
22431 Constrain_Access (Def_Id, S, Related_Nod);
22433 if Expander_Active
22434 and then Is_Itype (Designated_Type (Def_Id))
22435 and then Nkind (Related_Nod) = N_Subtype_Declaration
22436 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22437 then
22438 Build_Itype_Reference
22439 (Designated_Type (Def_Id), Related_Nod);
22440 end if;
22442 when Array_Kind =>
22443 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22445 when Decimal_Fixed_Point_Kind =>
22446 Constrain_Decimal (Def_Id, S);
22448 when Enumeration_Kind =>
22449 Constrain_Enumeration (Def_Id, S);
22451 when Ordinary_Fixed_Point_Kind =>
22452 Constrain_Ordinary_Fixed (Def_Id, S);
22454 when Float_Kind =>
22455 Constrain_Float (Def_Id, S);
22457 when Integer_Kind =>
22458 Constrain_Integer (Def_Id, S);
22460 when Class_Wide_Kind
22461 | E_Incomplete_Type
22462 | E_Record_Subtype
22463 | E_Record_Type
22465 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22467 if Ekind (Def_Id) = E_Incomplete_Type then
22468 Set_Private_Dependents (Def_Id, New_Elmt_List);
22469 end if;
22471 when Private_Kind =>
22473 -- A private type with unknown discriminants may be completed
22474 -- by an unconstrained array type.
22476 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22477 and then Present (Full_View (Subtype_Mark_Id))
22478 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22479 then
22480 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22482 -- ... but more commonly is completed by a discriminated record
22483 -- type.
22485 else
22486 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22487 end if;
22489 -- The base type may be private but Def_Id may be a full view
22490 -- in an instance.
22492 if Is_Private_Type (Def_Id) then
22493 Set_Private_Dependents (Def_Id, New_Elmt_List);
22494 end if;
22496 -- In case of an invalid constraint prevent further processing
22497 -- since the type constructed is missing expected fields.
22499 if Etype (Def_Id) = Any_Type then
22500 return Def_Id;
22501 end if;
22503 -- If the full view is that of a task with discriminants,
22504 -- we must constrain both the concurrent type and its
22505 -- corresponding record type. Otherwise we will just propagate
22506 -- the constraint to the full view, if available.
22508 if Present (Full_View (Subtype_Mark_Id))
22509 and then Has_Discriminants (Subtype_Mark_Id)
22510 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22511 then
22512 Full_View_Id :=
22513 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22515 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22516 Constrain_Concurrent (Full_View_Id, S,
22517 Related_Nod, Related_Id, Suffix);
22518 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22519 Set_Full_View (Def_Id, Full_View_Id);
22521 -- Introduce an explicit reference to the private subtype,
22522 -- to prevent scope anomalies in gigi if first use appears
22523 -- in a nested context, e.g. a later function body.
22524 -- Should this be generated in other contexts than a full
22525 -- type declaration?
22527 if Is_Itype (Def_Id)
22528 and then
22529 Nkind (Parent (P)) = N_Full_Type_Declaration
22530 then
22531 Build_Itype_Reference (Def_Id, Parent (P));
22532 end if;
22534 else
22535 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22536 end if;
22538 when Concurrent_Kind =>
22539 Constrain_Concurrent (Def_Id, S,
22540 Related_Nod, Related_Id, Suffix);
22542 when others =>
22543 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22544 end case;
22546 -- Size, Alignment, Representation aspects and Convention are always
22547 -- inherited from the base type.
22549 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22550 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22551 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22553 -- The anonymous subtype created for the subtype indication
22554 -- inherits the predicates of the parent.
22556 if Has_Predicates (Subtype_Mark_Id) then
22557 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22559 -- Indicate where the predicate function may be found
22561 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22562 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22563 end if;
22564 end if;
22566 return Def_Id;
22567 end if;
22568 end Process_Subtype;
22570 -----------------------------
22571 -- Record_Type_Declaration --
22572 -----------------------------
22574 procedure Record_Type_Declaration
22575 (T : Entity_Id;
22576 N : Node_Id;
22577 Prev : Entity_Id)
22579 Def : constant Node_Id := Type_Definition (N);
22580 Is_Tagged : Boolean;
22581 Tag_Comp : Entity_Id;
22583 begin
22584 -- These flags must be initialized before calling Process_Discriminants
22585 -- because this routine makes use of them.
22587 Mutate_Ekind (T, E_Record_Type);
22588 Set_Etype (T, T);
22589 Reinit_Size_Align (T);
22590 Set_Interfaces (T, No_Elist);
22591 Set_Stored_Constraint (T, No_Elist);
22592 Set_Default_SSO (T);
22593 Set_No_Reordering (T, No_Component_Reordering);
22595 -- Normal case
22597 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22598 -- The flag Is_Tagged_Type might have already been set by
22599 -- Find_Type_Name if it detected an error for declaration T. This
22600 -- arises in the case of private tagged types where the full view
22601 -- omits the word tagged.
22603 Is_Tagged :=
22604 Tagged_Present (Def)
22605 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22607 Set_Is_Limited_Record (T, Limited_Present (Def));
22609 if Is_Tagged then
22610 Set_Is_Tagged_Type (T, True);
22611 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22612 end if;
22614 -- Type is abstract if full declaration carries keyword, or if
22615 -- previous partial view did.
22617 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22618 or else Abstract_Present (Def));
22620 else
22621 Is_Tagged := True;
22622 Analyze_Interface_Declaration (T, Def);
22624 if Present (Discriminant_Specifications (N)) then
22625 Error_Msg_N
22626 ("interface types cannot have discriminants",
22627 Defining_Identifier
22628 (First (Discriminant_Specifications (N))));
22629 end if;
22630 end if;
22632 -- First pass: if there are self-referential access components,
22633 -- create the required anonymous access type declarations, and if
22634 -- need be an incomplete type declaration for T itself.
22636 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22638 if Ada_Version >= Ada_2005
22639 and then Present (Interface_List (Def))
22640 then
22641 Check_Interfaces (N, Def);
22643 declare
22644 Ifaces_List : Elist_Id;
22646 begin
22647 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22648 -- already in the parents.
22650 Collect_Interfaces
22651 (T => T,
22652 Ifaces_List => Ifaces_List,
22653 Exclude_Parents => True);
22655 Set_Interfaces (T, Ifaces_List);
22656 end;
22657 end if;
22659 -- Records constitute a scope for the component declarations within.
22660 -- The scope is created prior to the processing of these declarations.
22661 -- Discriminants are processed first, so that they are visible when
22662 -- processing the other components. The Ekind of the record type itself
22663 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22665 -- Enter record scope
22667 Push_Scope (T);
22669 -- If an incomplete or private type declaration was already given for
22670 -- the type, then this scope already exists, and the discriminants have
22671 -- been declared within. We must verify that the full declaration
22672 -- matches the incomplete one.
22674 Check_Or_Process_Discriminants (N, T, Prev);
22676 Set_Is_Constrained (T, not Has_Discriminants (T));
22677 Set_Has_Delayed_Freeze (T, True);
22679 -- For tagged types add a manually analyzed component corresponding
22680 -- to the component _tag, the corresponding piece of tree will be
22681 -- expanded as part of the freezing actions if it is not a CPP_Class.
22683 if Is_Tagged then
22685 -- Do not add the tag unless we are in expansion mode
22687 if Expander_Active then
22688 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22689 Enter_Name (Tag_Comp);
22691 Mutate_Ekind (Tag_Comp, E_Component);
22692 Set_Is_Tag (Tag_Comp);
22693 Set_Is_Aliased (Tag_Comp);
22694 Set_Is_Independent (Tag_Comp);
22695 Set_Etype (Tag_Comp, RTE (RE_Tag));
22696 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22697 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22698 Reinit_Component_Location (Tag_Comp);
22700 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22701 -- implemented interfaces.
22703 if Has_Interfaces (T) then
22704 Add_Interface_Tag_Components (N, T);
22705 end if;
22706 end if;
22708 Make_Class_Wide_Type (T);
22709 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22710 end if;
22712 -- We must suppress range checks when processing record components in
22713 -- the presence of discriminants, since we don't want spurious checks to
22714 -- be generated during their analysis, but Suppress_Range_Checks flags
22715 -- must be reset the after processing the record definition.
22717 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22718 -- couldn't we just use the normal range check suppression method here.
22719 -- That would seem cleaner ???
22721 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22722 Set_Kill_Range_Checks (T, True);
22723 Record_Type_Definition (Def, Prev);
22724 Set_Kill_Range_Checks (T, False);
22725 else
22726 Record_Type_Definition (Def, Prev);
22727 end if;
22729 -- Exit from record scope
22731 End_Scope;
22733 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22734 -- the implemented interfaces and associate them an aliased entity.
22736 if Is_Tagged
22737 and then not Is_Empty_List (Interface_List (Def))
22738 then
22739 Derive_Progenitor_Subprograms (T, T);
22740 end if;
22742 Check_Function_Writable_Actuals (N);
22743 end Record_Type_Declaration;
22745 ----------------------------
22746 -- Record_Type_Definition --
22747 ----------------------------
22749 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22750 Component : Entity_Id;
22751 Ctrl_Components : Boolean := False;
22752 Final_Storage_Only : Boolean;
22753 T : Entity_Id;
22755 begin
22756 if Ekind (Prev_T) = E_Incomplete_Type then
22757 T := Full_View (Prev_T);
22758 else
22759 T := Prev_T;
22760 end if;
22762 Final_Storage_Only := not Is_Controlled (T);
22764 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22765 -- type declaration.
22767 if Parent_Kind (Def) = N_Derived_Type_Definition
22768 and then Limited_Present (Parent (Def))
22769 then
22770 Set_Is_Limited_Record (T);
22771 end if;
22773 -- If the component list of a record type is defined by the reserved
22774 -- word null and there is no discriminant part, then the record type has
22775 -- no components and all records of the type are null records (RM 3.7)
22776 -- This procedure is also called to process the extension part of a
22777 -- record extension, in which case the current scope may have inherited
22778 -- components.
22780 if Present (Def)
22781 and then Present (Component_List (Def))
22782 and then not Null_Present (Component_List (Def))
22783 then
22784 Analyze_Declarations (Component_Items (Component_List (Def)));
22786 if Present (Variant_Part (Component_List (Def))) then
22787 Analyze (Variant_Part (Component_List (Def)));
22788 end if;
22789 end if;
22791 -- After completing the semantic analysis of the record definition,
22792 -- record components, both new and inherited, are accessible. Set their
22793 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22794 -- whose Ekind may be void.
22796 Component := First_Entity (Current_Scope);
22797 while Present (Component) loop
22798 if Ekind (Component) = E_Void
22799 and then not Is_Itype (Component)
22800 then
22801 Mutate_Ekind (Component, E_Component);
22802 Reinit_Component_Location (Component);
22803 end if;
22805 Propagate_Concurrent_Flags (T, Etype (Component));
22807 if Ekind (Component) /= E_Component then
22808 null;
22810 -- Do not set Has_Controlled_Component on a class-wide equivalent
22811 -- type. See Make_CW_Equivalent_Type.
22813 elsif not Is_Class_Wide_Equivalent_Type (T)
22814 and then (Has_Controlled_Component (Etype (Component))
22815 or else (Chars (Component) /= Name_uParent
22816 and then Is_Controlled (Etype (Component))))
22817 then
22818 Set_Has_Controlled_Component (T, True);
22819 Final_Storage_Only :=
22820 Final_Storage_Only
22821 and then Finalize_Storage_Only (Etype (Component));
22822 Ctrl_Components := True;
22823 end if;
22825 Next_Entity (Component);
22826 end loop;
22828 -- A Type is Finalize_Storage_Only only if all its controlled components
22829 -- are also.
22831 if Ctrl_Components then
22832 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22833 end if;
22835 -- Place reference to end record on the proper entity, which may
22836 -- be a partial view.
22838 if Present (Def) then
22839 Process_End_Label (Def, 'e', Prev_T);
22840 end if;
22841 end Record_Type_Definition;
22843 ---------------------------
22844 -- Replace_Discriminants --
22845 ---------------------------
22847 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22848 function Process (N : Node_Id) return Traverse_Result;
22850 -------------
22851 -- Process --
22852 -------------
22854 function Process (N : Node_Id) return Traverse_Result is
22855 Comp : Entity_Id;
22857 begin
22858 if Nkind (N) = N_Discriminant_Specification then
22859 Comp := First_Discriminant (Typ);
22860 while Present (Comp) loop
22861 if Original_Record_Component (Comp) = Defining_Identifier (N)
22862 or else Chars (Comp) = Chars (Defining_Identifier (N))
22863 then
22864 Set_Defining_Identifier (N, Comp);
22865 exit;
22866 end if;
22868 Next_Discriminant (Comp);
22869 end loop;
22871 elsif Nkind (N) = N_Variant_Part then
22872 Comp := First_Discriminant (Typ);
22873 while Present (Comp) loop
22874 if Original_Record_Component (Comp) = Entity (Name (N))
22875 or else Chars (Comp) = Chars (Name (N))
22876 then
22877 -- Make sure to preserve the type coming from the parent on
22878 -- the Name, even if the subtype of the discriminant can be
22879 -- constrained, so that discrete choices inherited from the
22880 -- parent in the variant part are not flagged as violating
22881 -- the constraints of the subtype.
22883 declare
22884 Typ : constant Entity_Id := Etype (Name (N));
22885 begin
22886 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22887 Set_Etype (Name (N), Typ);
22888 end;
22889 exit;
22890 end if;
22892 Next_Discriminant (Comp);
22893 end loop;
22894 end if;
22896 return OK;
22897 end Process;
22899 procedure Replace is new Traverse_Proc (Process);
22901 -- Start of processing for Replace_Discriminants
22903 begin
22904 Replace (Decl);
22905 end Replace_Discriminants;
22907 -------------------------------
22908 -- Set_Completion_Referenced --
22909 -------------------------------
22911 procedure Set_Completion_Referenced (E : Entity_Id) is
22912 begin
22913 -- If in main unit, mark entity that is a completion as referenced,
22914 -- warnings go on the partial view when needed.
22916 if In_Extended_Main_Source_Unit (E) then
22917 Set_Referenced (E);
22918 end if;
22919 end Set_Completion_Referenced;
22921 ---------------------
22922 -- Set_Default_SSO --
22923 ---------------------
22925 procedure Set_Default_SSO (T : Entity_Id) is
22926 begin
22927 case Opt.Default_SSO is
22928 when ' ' =>
22929 null;
22930 when 'L' =>
22931 Set_SSO_Set_Low_By_Default (T, True);
22932 when 'H' =>
22933 Set_SSO_Set_High_By_Default (T, True);
22934 when others =>
22935 raise Program_Error;
22936 end case;
22937 end Set_Default_SSO;
22939 ---------------------
22940 -- Set_Fixed_Range --
22941 ---------------------
22943 -- The range for fixed-point types is complicated by the fact that we
22944 -- do not know the exact end points at the time of the declaration. This
22945 -- is true for three reasons:
22947 -- A size clause may affect the fudging of the end-points.
22948 -- A small clause may affect the values of the end-points.
22949 -- We try to include the end-points if it does not affect the size.
22951 -- This means that the actual end-points must be established at the
22952 -- point when the type is frozen. Meanwhile, we first narrow the range
22953 -- as permitted (so that it will fit if necessary in a small specified
22954 -- size), and then build a range subtree with these narrowed bounds.
22955 -- Set_Fixed_Range constructs the range from real literal values, and
22956 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22958 -- The parent of this range is set to point to the entity so that it is
22959 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22960 -- other scalar types, which are just pointers to the range in the
22961 -- original tree, this would otherwise be an orphan).
22963 -- The tree is left unanalyzed. When the type is frozen, the processing
22964 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22965 -- analyzed, and uses this as an indication that it should complete
22966 -- work on the range (it will know the final small and size values).
22968 procedure Set_Fixed_Range
22969 (E : Entity_Id;
22970 Loc : Source_Ptr;
22971 Lo : Ureal;
22972 Hi : Ureal)
22974 S : constant Node_Id :=
22975 Make_Range (Loc,
22976 Low_Bound => Make_Real_Literal (Loc, Lo),
22977 High_Bound => Make_Real_Literal (Loc, Hi));
22978 begin
22979 Set_Scalar_Range (E, S);
22980 Set_Parent (S, E);
22982 -- Before the freeze point, the bounds of a fixed point are universal
22983 -- and carry the corresponding type.
22985 Set_Etype (Low_Bound (S), Universal_Real);
22986 Set_Etype (High_Bound (S), Universal_Real);
22987 end Set_Fixed_Range;
22989 ----------------------------------
22990 -- Set_Scalar_Range_For_Subtype --
22991 ----------------------------------
22993 procedure Set_Scalar_Range_For_Subtype
22994 (Def_Id : Entity_Id;
22995 R : Node_Id;
22996 Subt : Entity_Id)
22998 Kind : constant Entity_Kind := Ekind (Def_Id);
23000 begin
23001 -- Defend against previous error
23003 if Nkind (R) = N_Error then
23004 return;
23005 end if;
23007 Set_Scalar_Range (Def_Id, R);
23009 -- We need to link the range into the tree before resolving it so
23010 -- that types that are referenced, including importantly the subtype
23011 -- itself, are properly frozen (Freeze_Expression requires that the
23012 -- expression be properly linked into the tree). Of course if it is
23013 -- already linked in, then we do not disturb the current link.
23015 if No (Parent (R)) then
23016 Set_Parent (R, Def_Id);
23017 end if;
23019 -- Reset the kind of the subtype during analysis of the range, to
23020 -- catch possible premature use in the bounds themselves.
23022 Mutate_Ekind (Def_Id, E_Void);
23023 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23024 Mutate_Ekind (Def_Id, Kind);
23025 end Set_Scalar_Range_For_Subtype;
23027 --------------------------------------------------------
23028 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23029 --------------------------------------------------------
23031 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23032 (E : Entity_Id)
23034 begin
23035 -- Make sure set if encountered during Expand_To_Stored_Constraint
23037 Set_Stored_Constraint (E, No_Elist);
23039 -- Give it the right value
23041 if Is_Constrained (E) and then Has_Discriminants (E) then
23042 Set_Stored_Constraint (E,
23043 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23044 end if;
23045 end Set_Stored_Constraint_From_Discriminant_Constraint;
23047 -------------------------------------
23048 -- Signed_Integer_Type_Declaration --
23049 -------------------------------------
23051 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23052 Implicit_Base : Entity_Id;
23053 Base_Typ : Entity_Id;
23054 Lo_Val : Uint;
23055 Hi_Val : Uint;
23056 Errs : Boolean := False;
23057 Lo : Node_Id;
23058 Hi : Node_Id;
23060 function Can_Derive_From (E : Entity_Id) return Boolean;
23061 -- Determine whether given bounds allow derivation from specified type
23063 procedure Check_Bound (Expr : Node_Id);
23064 -- Check bound to make sure it is integral and static. If not, post
23065 -- appropriate error message and set Errs flag
23067 ---------------------
23068 -- Can_Derive_From --
23069 ---------------------
23071 -- Note we check both bounds against both end values, to deal with
23072 -- strange types like ones with a range of 0 .. -12341234.
23074 function Can_Derive_From (E : Entity_Id) return Boolean is
23075 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23076 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23077 begin
23078 return Lo <= Lo_Val and then Lo_Val <= Hi
23079 and then
23080 Lo <= Hi_Val and then Hi_Val <= Hi;
23081 end Can_Derive_From;
23083 -----------------
23084 -- Check_Bound --
23085 -----------------
23087 procedure Check_Bound (Expr : Node_Id) is
23088 begin
23089 -- If a range constraint is used as an integer type definition, each
23090 -- bound of the range must be defined by a static expression of some
23091 -- integer type, but the two bounds need not have the same integer
23092 -- type (Negative bounds are allowed.) (RM 3.5.4)
23094 if not Is_Integer_Type (Etype (Expr)) then
23095 Error_Msg_N
23096 ("integer type definition bounds must be of integer type", Expr);
23097 Errs := True;
23099 elsif not Is_OK_Static_Expression (Expr) then
23100 Flag_Non_Static_Expr
23101 ("non-static expression used for integer type bound!", Expr);
23102 Errs := True;
23104 -- Otherwise the bounds are folded into literals
23106 elsif Is_Entity_Name (Expr) then
23107 Fold_Uint (Expr, Expr_Value (Expr), True);
23108 end if;
23109 end Check_Bound;
23111 -- Start of processing for Signed_Integer_Type_Declaration
23113 begin
23114 -- Create an anonymous base type
23116 Implicit_Base :=
23117 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23119 -- Analyze and check the bounds, they can be of any integer type
23121 Lo := Low_Bound (Def);
23122 Hi := High_Bound (Def);
23124 -- Arbitrarily use Integer as the type if either bound had an error
23126 if Hi = Error or else Lo = Error then
23127 Base_Typ := Any_Integer;
23128 Set_Error_Posted (T, True);
23129 Errs := True;
23131 -- Here both bounds are OK expressions
23133 else
23134 Analyze_And_Resolve (Lo, Any_Integer);
23135 Analyze_And_Resolve (Hi, Any_Integer);
23137 Check_Bound (Lo);
23138 Check_Bound (Hi);
23140 if Errs then
23141 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23142 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23143 end if;
23145 -- Find type to derive from
23147 Lo_Val := Expr_Value (Lo);
23148 Hi_Val := Expr_Value (Hi);
23150 if Can_Derive_From (Standard_Short_Short_Integer) then
23151 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23153 elsif Can_Derive_From (Standard_Short_Integer) then
23154 Base_Typ := Base_Type (Standard_Short_Integer);
23156 elsif Can_Derive_From (Standard_Integer) then
23157 Base_Typ := Base_Type (Standard_Integer);
23159 elsif Can_Derive_From (Standard_Long_Integer) then
23160 Base_Typ := Base_Type (Standard_Long_Integer);
23162 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23163 Check_Restriction (No_Long_Long_Integers, Def);
23164 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23166 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23167 Check_Restriction (No_Long_Long_Integers, Def);
23168 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23170 else
23171 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23172 Error_Msg_N ("integer type definition bounds out of range", Def);
23173 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23174 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23175 end if;
23176 end if;
23178 -- Set the type of the bounds to the implicit base: we cannot set it to
23179 -- the new type, because this would be a forward reference for the code
23180 -- generator and, if the original type is user-defined, this could even
23181 -- lead to spurious semantic errors. Furthermore we do not set it to be
23182 -- universal, because this could make it much larger than needed here.
23184 if not Errs then
23185 Set_Etype (Lo, Implicit_Base);
23186 Set_Etype (Hi, Implicit_Base);
23187 end if;
23189 -- Complete both implicit base and declared first subtype entities. The
23190 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23191 -- are not clobbered when the signed integer type acts as a full view of
23192 -- a private type.
23194 Set_Etype (Implicit_Base, Base_Typ);
23195 Set_Size_Info (Implicit_Base, Base_Typ);
23196 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23197 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23198 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23200 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23201 Set_Etype (T, Implicit_Base);
23202 Set_Size_Info (T, Implicit_Base);
23203 Inherit_Rep_Item_Chain (T, Implicit_Base);
23204 Set_Scalar_Range (T, Def);
23205 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23206 Set_Is_Constrained (T);
23207 end Signed_Integer_Type_Declaration;
23209 end Sem_Ch3;