MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobc79d323395f3303eb1dd3319e740146ac80d1901
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
1692 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean;
1693 -- Determine if an error has been posted in some formal of Subp.
1695 -----------------------------
1696 -- Error_Posted_In_Formals --
1697 -----------------------------
1699 function Error_Posted_In_Formals (Subp : Entity_Id) return Boolean is
1700 Formal : Entity_Id := First_Formal (Subp);
1702 begin
1703 while Present (Formal) loop
1704 if Error_Posted (Formal) then
1705 return True;
1706 end if;
1708 Next_Formal (Formal);
1709 end loop;
1711 return False;
1712 end Error_Posted_In_Formals;
1714 -- Local variables
1716 Elmt : Elmt_Id;
1717 Iface : Entity_Id;
1718 Iface_Elmt : Elmt_Id;
1719 Iface_Prim : Entity_Id;
1720 Ifaces_List : Elist_Id;
1721 New_Subp : Entity_Id := Empty;
1722 Prim : Entity_Id;
1723 Restore_Scope : Boolean := False;
1725 begin
1726 pragma Assert (Ada_Version >= Ada_2005
1727 and then Is_Record_Type (Tagged_Type)
1728 and then Is_Tagged_Type (Tagged_Type)
1729 and then Has_Interfaces (Tagged_Type)
1730 and then not Is_Interface (Tagged_Type));
1732 -- Ensure that the internal entities are added to the scope of the type
1734 if Scope (Tagged_Type) /= Current_Scope then
1735 Push_Scope (Scope (Tagged_Type));
1736 Restore_Scope := True;
1737 end if;
1739 Collect_Interfaces (Tagged_Type, Ifaces_List);
1741 Iface_Elmt := First_Elmt (Ifaces_List);
1742 while Present (Iface_Elmt) loop
1743 Iface := Node (Iface_Elmt);
1745 -- Originally we excluded here from this processing interfaces that
1746 -- are parents of Tagged_Type because their primitives are located
1747 -- in the primary dispatch table (and hence no auxiliary internal
1748 -- entities are required to handle secondary dispatch tables in such
1749 -- case). However, these auxiliary entities are also required to
1750 -- handle derivations of interfaces in formals of generics (see
1751 -- Derive_Subprograms).
1753 Elmt := First_Elmt (Primitive_Operations (Iface));
1754 while Present (Elmt) loop
1755 Iface_Prim := Node (Elmt);
1757 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1758 Prim :=
1759 Find_Primitive_Covering_Interface
1760 (Tagged_Type => Tagged_Type,
1761 Iface_Prim => Iface_Prim);
1763 if No (Prim) and then Serious_Errors_Detected > 0 then
1764 goto Continue;
1765 end if;
1767 pragma Assert (Present (Prim));
1769 -- Check subtype conformance; we skip this check if errors have
1770 -- been reported in the primitive (or in the formals of the
1771 -- primitive) because Find_Primitive_Covering_Interface relies
1772 -- on the subprogram Type_Conformant to locate the primitive,
1773 -- and reports errors if the formals don't match.
1775 if not Error_Posted (Prim)
1776 and then not Error_Posted_In_Formals (Prim)
1777 then
1778 declare
1779 Alias_Prim : Entity_Id;
1780 Alias_Typ : Entity_Id;
1781 Err_Loc : Node_Id := Empty;
1782 Ret_Type : Entity_Id;
1784 begin
1785 -- For inherited primitives, in case of reporting an
1786 -- error, the error must be reported on this primitive
1787 -- (i.e. in the name of its type declaration); otherwise
1788 -- the error would be reported in the formal of the
1789 -- alias primitive defined on its parent type.
1791 if Nkind (Parent (Prim)) = N_Full_Type_Declaration then
1792 Err_Loc := Prim;
1793 end if;
1795 -- Check subtype conformance of procedures, functions
1796 -- with matching return type, or functions not returning
1797 -- interface types.
1799 if Ekind (Prim) = E_Procedure
1800 or else Etype (Iface_Prim) = Etype (Prim)
1801 or else not Is_Interface (Etype (Iface_Prim))
1802 then
1803 Check_Subtype_Conformant
1804 (New_Id => Prim,
1805 Old_Id => Iface_Prim,
1806 Err_Loc => Err_Loc,
1807 Skip_Controlling_Formals => True);
1809 -- Check subtype conformance of functions returning an
1810 -- interface type; temporarily force both entities to
1811 -- return the same type. Required because subprogram
1812 -- Subtype_Conformant does not handle this case.
1814 else
1815 Ret_Type := Etype (Iface_Prim);
1816 Set_Etype (Iface_Prim, Etype (Prim));
1818 Check_Subtype_Conformant
1819 (New_Id => Prim,
1820 Old_Id => Iface_Prim,
1821 Err_Loc => Err_Loc,
1822 Skip_Controlling_Formals => True);
1824 Set_Etype (Iface_Prim, Ret_Type);
1825 end if;
1827 -- Complete the error when reported on inherited
1828 -- primitives.
1830 if Nkind (Parent (Prim)) = N_Full_Type_Declaration
1831 and then (Error_Posted (Prim)
1832 or else Error_Posted_In_Formals (Prim))
1833 and then Present (Alias (Prim))
1834 then
1835 Alias_Prim := Ultimate_Alias (Prim);
1836 Alias_Typ := Find_Dispatching_Type (Alias_Prim);
1838 if Alias_Typ /= Tagged_Type
1839 and then Is_Ancestor (Alias_Typ, Tagged_Type)
1840 then
1841 Error_Msg_Sloc := Sloc (Alias_Prim);
1842 Error_Msg_N
1843 ("in primitive inherited from #!", Prim);
1844 end if;
1845 end if;
1846 end;
1847 end if;
1849 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1850 -- differs from the name of the interface primitive then it is
1851 -- a private primitive inherited from a parent type. In such
1852 -- case, given that Tagged_Type covers the interface, the
1853 -- inherited private primitive becomes visible. For such
1854 -- purpose we add a new entity that renames the inherited
1855 -- private primitive.
1857 if Chars (Prim) /= Chars (Iface_Prim) then
1858 pragma Assert (Has_Suffix (Prim, 'P'));
1859 Derive_Subprogram
1860 (New_Subp => New_Subp,
1861 Parent_Subp => Iface_Prim,
1862 Derived_Type => Tagged_Type,
1863 Parent_Type => Iface);
1864 Set_Alias (New_Subp, Prim);
1865 Set_Is_Abstract_Subprogram
1866 (New_Subp, Is_Abstract_Subprogram (Prim));
1867 end if;
1869 Derive_Subprogram
1870 (New_Subp => New_Subp,
1871 Parent_Subp => Iface_Prim,
1872 Derived_Type => Tagged_Type,
1873 Parent_Type => Iface);
1875 declare
1876 Anc : Entity_Id;
1877 begin
1878 if Is_Inherited_Operation (Prim)
1879 and then Present (Alias (Prim))
1880 then
1881 Anc := Alias (Prim);
1882 else
1883 Anc := Overridden_Operation (Prim);
1884 end if;
1886 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1887 -- nonconforming preconditions in both an ancestor and
1888 -- a progenitor operation.
1890 -- If the operation is a primitive wrapper it is an explicit
1891 -- (overriding) operqtion and all is fine.
1893 if Present (Anc)
1894 and then Has_Non_Trivial_Precondition (Anc)
1895 and then Has_Non_Trivial_Precondition (Iface_Prim)
1896 then
1897 if Is_Abstract_Subprogram (Prim)
1898 or else
1899 (Ekind (Prim) = E_Procedure
1900 and then Nkind (Parent (Prim)) =
1901 N_Procedure_Specification
1902 and then Null_Present (Parent (Prim)))
1903 or else Is_Primitive_Wrapper (Prim)
1904 then
1905 null;
1907 -- The operation is inherited and must be overridden
1909 elsif not Comes_From_Source (Prim) then
1910 Error_Msg_NE
1911 ("&inherits non-conforming preconditions and must "
1912 & "be overridden (RM 6.1.1 (10-16))",
1913 Parent (Tagged_Type), Prim);
1914 end if;
1915 end if;
1916 end;
1918 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1919 -- associated with interface types. These entities are
1920 -- only registered in the list of primitives of its
1921 -- corresponding tagged type because they are only used
1922 -- to fill the contents of the secondary dispatch tables.
1923 -- Therefore they are removed from the homonym chains.
1925 Set_Is_Hidden (New_Subp);
1926 Set_Is_Internal (New_Subp);
1927 Set_Alias (New_Subp, Prim);
1928 Set_Is_Abstract_Subprogram
1929 (New_Subp, Is_Abstract_Subprogram (Prim));
1930 Set_Interface_Alias (New_Subp, Iface_Prim);
1932 -- If the returned type is an interface then propagate it to
1933 -- the returned type. Needed by the thunk to generate the code
1934 -- which displaces "this" to reference the corresponding
1935 -- secondary dispatch table in the returned object.
1937 if Is_Interface (Etype (Iface_Prim)) then
1938 Set_Etype (New_Subp, Etype (Iface_Prim));
1939 end if;
1941 -- Internal entities associated with interface types are only
1942 -- registered in the list of primitives of the tagged type.
1943 -- They are only used to fill the contents of the secondary
1944 -- dispatch tables. Therefore they are not needed in the
1945 -- homonym chains.
1947 Remove_Homonym (New_Subp);
1949 -- Hidden entities associated with interfaces must have set
1950 -- the Has_Delay_Freeze attribute to ensure that, in case
1951 -- of locally defined tagged types (or compiling with static
1952 -- dispatch tables generation disabled) the corresponding
1953 -- entry of the secondary dispatch table is filled when such
1954 -- an entity is frozen.
1956 Set_Has_Delayed_Freeze (New_Subp);
1957 end if;
1959 <<Continue>>
1960 Next_Elmt (Elmt);
1961 end loop;
1963 Next_Elmt (Iface_Elmt);
1964 end loop;
1966 if Restore_Scope then
1967 Pop_Scope;
1968 end if;
1969 end Add_Internal_Interface_Entities;
1971 -----------------------------------
1972 -- Analyze_Component_Declaration --
1973 -----------------------------------
1975 procedure Analyze_Component_Declaration (N : Node_Id) is
1976 Id : constant Entity_Id := Defining_Identifier (N);
1977 E : constant Node_Id := Expression (N);
1978 Typ : constant Node_Id :=
1979 Subtype_Indication (Component_Definition (N));
1980 T : Entity_Id;
1981 P : Entity_Id;
1983 function Contains_POC (Constr : Node_Id) return Boolean;
1984 -- Determines whether a constraint uses the discriminant of a record
1985 -- type thus becoming a per-object constraint (POC).
1987 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1988 -- Typ is the type of the current component, check whether this type is
1989 -- a limited type. Used to validate declaration against that of
1990 -- enclosing record.
1992 ------------------
1993 -- Contains_POC --
1994 ------------------
1996 function Contains_POC (Constr : Node_Id) return Boolean is
1997 begin
1998 -- Prevent cascaded errors
2000 if Error_Posted (Constr) then
2001 return False;
2002 end if;
2004 case Nkind (Constr) is
2005 when N_Attribute_Reference =>
2006 return Attribute_Name (Constr) = Name_Access
2007 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
2009 when N_Discriminant_Association =>
2010 return Denotes_Discriminant (Expression (Constr));
2012 when N_Identifier =>
2013 return Denotes_Discriminant (Constr);
2015 when N_Index_Or_Discriminant_Constraint =>
2016 declare
2017 IDC : Node_Id;
2019 begin
2020 IDC := First (Constraints (Constr));
2021 while Present (IDC) loop
2023 -- One per-object constraint is sufficient
2025 if Contains_POC (IDC) then
2026 return True;
2027 end if;
2029 Next (IDC);
2030 end loop;
2032 return False;
2033 end;
2035 when N_Range =>
2036 return Denotes_Discriminant (Low_Bound (Constr))
2037 or else
2038 Denotes_Discriminant (High_Bound (Constr));
2040 when N_Range_Constraint =>
2041 return Denotes_Discriminant (Range_Expression (Constr));
2043 when others =>
2044 return False;
2045 end case;
2046 end Contains_POC;
2048 ----------------------
2049 -- Is_Known_Limited --
2050 ----------------------
2052 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
2053 P : constant Entity_Id := Etype (Typ);
2054 R : constant Entity_Id := Root_Type (Typ);
2056 begin
2057 if Is_Limited_Record (Typ) then
2058 return True;
2060 -- If the root type is limited (and not a limited interface) so is
2061 -- the current type.
2063 elsif Is_Limited_Record (R)
2064 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
2065 then
2066 return True;
2068 -- Else the type may have a limited interface progenitor, but a
2069 -- limited record parent that is not an interface.
2071 elsif R /= P
2072 and then Is_Limited_Record (P)
2073 and then not Is_Interface (P)
2074 then
2075 return True;
2077 else
2078 return False;
2079 end if;
2080 end Is_Known_Limited;
2082 -- Start of processing for Analyze_Component_Declaration
2084 begin
2085 Generate_Definition (Id);
2086 Enter_Name (Id);
2088 if Present (Typ) then
2089 T := Find_Type_Of_Object
2090 (Subtype_Indication (Component_Definition (N)), N);
2092 -- Ada 2005 (AI-230): Access Definition case
2094 else
2095 pragma Assert (Present
2096 (Access_Definition (Component_Definition (N))));
2098 T := Access_Definition
2099 (Related_Nod => N,
2100 N => Access_Definition (Component_Definition (N)));
2101 Set_Is_Local_Anonymous_Access (T);
2103 -- Ada 2005 (AI-254)
2105 if Present (Access_To_Subprogram_Definition
2106 (Access_Definition (Component_Definition (N))))
2107 and then Protected_Present (Access_To_Subprogram_Definition
2108 (Access_Definition
2109 (Component_Definition (N))))
2110 then
2111 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2112 end if;
2113 end if;
2115 -- If the subtype is a constrained subtype of the enclosing record,
2116 -- (which must have a partial view) the back-end does not properly
2117 -- handle the recursion. Rewrite the component declaration with an
2118 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2119 -- the tree directly because side effects have already been removed from
2120 -- discriminant constraints.
2122 if Ekind (T) = E_Access_Subtype
2123 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2124 and then Comes_From_Source (T)
2125 and then Nkind (Parent (T)) = N_Subtype_Declaration
2126 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2127 then
2128 Rewrite
2129 (Subtype_Indication (Component_Definition (N)),
2130 New_Copy_Tree (Subtype_Indication (Parent (T))));
2131 T := Find_Type_Of_Object
2132 (Subtype_Indication (Component_Definition (N)), N);
2133 end if;
2135 -- If the component declaration includes a default expression, then we
2136 -- check that the component is not of a limited type (RM 3.7(5)),
2137 -- and do the special preanalysis of the expression (see section on
2138 -- "Handling of Default and Per-Object Expressions" in the spec of
2139 -- package Sem).
2141 if Present (E) then
2142 Preanalyze_Default_Expression (E, T);
2143 Check_Initialization (T, E);
2145 if Ada_Version >= Ada_2005
2146 and then Ekind (T) = E_Anonymous_Access_Type
2147 and then Etype (E) /= Any_Type
2148 then
2149 -- Check RM 3.9.2(9): "if the expected type for an expression is
2150 -- an anonymous access-to-specific tagged type, then the object
2151 -- designated by the expression shall not be dynamically tagged
2152 -- unless it is a controlling operand in a call on a dispatching
2153 -- operation"
2155 if Is_Tagged_Type (Directly_Designated_Type (T))
2156 and then
2157 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2158 and then
2159 Ekind (Directly_Designated_Type (Etype (E))) =
2160 E_Class_Wide_Type
2161 then
2162 Error_Msg_N
2163 ("access to specific tagged type required (RM 3.9.2(9))", E);
2164 end if;
2166 -- (Ada 2005: AI-230): Accessibility check for anonymous
2167 -- components
2169 if Type_Access_Level (Etype (E)) >
2170 Deepest_Type_Access_Level (T)
2171 then
2172 Error_Msg_N
2173 ("expression has deeper access level than component " &
2174 "(RM 3.10.2 (12.2))", E);
2175 end if;
2177 -- The initialization expression is a reference to an access
2178 -- discriminant. The type of the discriminant is always deeper
2179 -- than any access type.
2181 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2182 and then Is_Entity_Name (E)
2183 and then Ekind (Entity (E)) = E_In_Parameter
2184 and then Present (Discriminal_Link (Entity (E)))
2185 then
2186 Error_Msg_N
2187 ("discriminant has deeper accessibility level than target",
2189 end if;
2190 end if;
2191 end if;
2193 -- The parent type may be a private view with unknown discriminants,
2194 -- and thus unconstrained. Regular components must be constrained.
2196 if not Is_Definite_Subtype (T)
2197 and then Chars (Id) /= Name_uParent
2198 then
2199 if Is_Class_Wide_Type (T) then
2200 Error_Msg_N
2201 ("class-wide subtype with unknown discriminants" &
2202 " in component declaration",
2203 Subtype_Indication (Component_Definition (N)));
2204 else
2205 Error_Msg_N
2206 ("unconstrained subtype in component declaration",
2207 Subtype_Indication (Component_Definition (N)));
2208 end if;
2210 -- Components cannot be abstract, except for the special case of
2211 -- the _Parent field (case of extending an abstract tagged type)
2213 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2214 Error_Msg_N ("type of a component cannot be abstract", N);
2215 end if;
2217 Set_Etype (Id, T);
2219 if Aliased_Present (Component_Definition (N)) then
2220 Set_Is_Aliased (Id);
2222 -- AI12-001: All aliased objects are considered to be specified as
2223 -- independently addressable (RM C.6(8.1/4)).
2225 Set_Is_Independent (Id);
2226 end if;
2228 -- The component declaration may have a per-object constraint, set
2229 -- the appropriate flag in the defining identifier of the subtype.
2231 if Present (Subtype_Indication (Component_Definition (N))) then
2232 declare
2233 Sindic : constant Node_Id :=
2234 Subtype_Indication (Component_Definition (N));
2235 begin
2236 if Nkind (Sindic) = N_Subtype_Indication
2237 and then Present (Constraint (Sindic))
2238 and then Contains_POC (Constraint (Sindic))
2239 then
2240 Set_Has_Per_Object_Constraint (Id);
2241 end if;
2242 end;
2243 end if;
2245 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2246 -- out some static checks.
2248 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2249 Null_Exclusion_Static_Checks (N);
2250 end if;
2252 -- If this component is private (or depends on a private type), flag the
2253 -- record type to indicate that some operations are not available.
2255 P := Private_Component (T);
2257 if Present (P) then
2259 -- Check for circular definitions
2261 if P = Any_Type then
2262 Set_Etype (Id, Any_Type);
2264 -- There is a gap in the visibility of operations only if the
2265 -- component type is not defined in the scope of the record type.
2267 elsif Scope (P) = Scope (Current_Scope) then
2268 null;
2270 elsif Is_Limited_Type (P) then
2271 Set_Is_Limited_Composite (Current_Scope);
2273 else
2274 Set_Is_Private_Composite (Current_Scope);
2275 end if;
2276 end if;
2278 if P /= Any_Type
2279 and then Is_Limited_Type (T)
2280 and then Chars (Id) /= Name_uParent
2281 and then Is_Tagged_Type (Current_Scope)
2282 then
2283 if Is_Derived_Type (Current_Scope)
2284 and then not Is_Known_Limited (Current_Scope)
2285 then
2286 Error_Msg_N
2287 ("extension of nonlimited type cannot have limited components",
2290 if Is_Interface (Root_Type (Current_Scope)) then
2291 Error_Msg_N
2292 ("\limitedness is not inherited from limited interface", N);
2293 Error_Msg_N ("\add LIMITED to type indication", N);
2294 end if;
2296 Explain_Limited_Type (T, N);
2297 Set_Etype (Id, Any_Type);
2298 Set_Is_Limited_Composite (Current_Scope, False);
2300 elsif not Is_Derived_Type (Current_Scope)
2301 and then not Is_Limited_Record (Current_Scope)
2302 and then not Is_Concurrent_Type (Current_Scope)
2303 then
2304 Error_Msg_N
2305 ("nonlimited tagged type cannot have limited components", N);
2306 Explain_Limited_Type (T, N);
2307 Set_Etype (Id, Any_Type);
2308 Set_Is_Limited_Composite (Current_Scope, False);
2309 end if;
2310 end if;
2312 Set_Original_Record_Component (Id, Id);
2314 if Has_Aspects (N) then
2315 Analyze_Aspect_Specifications (N, Id);
2316 end if;
2318 Analyze_Dimension (N);
2319 end Analyze_Component_Declaration;
2321 --------------------------
2322 -- Analyze_Declarations --
2323 --------------------------
2325 procedure Analyze_Declarations (L : List_Id) is
2326 Decl : Node_Id;
2328 procedure Adjust_Decl;
2329 -- Adjust Decl not to include implicit label declarations, since these
2330 -- have strange Sloc values that result in elaboration check problems.
2331 -- (They have the sloc of the label as found in the source, and that
2332 -- is ahead of the current declarative part).
2334 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2335 -- Create the subprogram bodies which verify the run-time semantics of
2336 -- the pragmas listed below for each elibigle type found in declarative
2337 -- list Decls. The pragmas are:
2339 -- Default_Initial_Condition
2340 -- Invariant
2341 -- Type_Invariant
2343 -- Context denotes the owner of the declarative list.
2345 procedure Check_Entry_Contracts;
2346 -- Perform a preanalysis of the pre- and postconditions of an entry
2347 -- declaration. This must be done before full resolution and creation
2348 -- of the parameter block, etc. to catch illegal uses within the
2349 -- contract expression. Full analysis of the expression is done when
2350 -- the contract is processed.
2352 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2353 -- Check if a nested package has entities within it that rely on library
2354 -- level private types where the full view has not been completed for
2355 -- the purposes of checking if it is acceptable to freeze an expression
2356 -- function at the point of declaration.
2358 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2359 -- Determine whether Body_Decl denotes the body of a late controlled
2360 -- primitive (either Initialize, Adjust or Finalize). If this is the
2361 -- case, add a proper spec if the body lacks one. The spec is inserted
2362 -- before Body_Decl and immediately analyzed.
2364 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2365 -- Spec_Id is the entity of a package that may define abstract states,
2366 -- and in the case of a child unit, whose ancestors may define abstract
2367 -- states. If the states have partial visible refinement, remove the
2368 -- partial visibility of each constituent at the end of the package
2369 -- spec and body declarations.
2371 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2372 -- Spec_Id is the entity of a package that may define abstract states.
2373 -- If the states have visible refinement, remove the visibility of each
2374 -- constituent at the end of the package body declaration.
2376 procedure Resolve_Aspects;
2377 -- Utility to resolve the expressions of aspects at the end of a list of
2378 -- declarations, or before a declaration that freezes previous entities,
2379 -- such as in a subprogram body.
2381 -----------------
2382 -- Adjust_Decl --
2383 -----------------
2385 procedure Adjust_Decl is
2386 begin
2387 while Present (Prev (Decl))
2388 and then Nkind (Decl) = N_Implicit_Label_Declaration
2389 loop
2390 Prev (Decl);
2391 end loop;
2392 end Adjust_Decl;
2394 ----------------------------
2395 -- Build_Assertion_Bodies --
2396 ----------------------------
2398 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2399 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2400 -- Create the subprogram bodies which verify the run-time semantics
2401 -- of the pragmas listed below for type Typ. The pragmas are:
2403 -- Default_Initial_Condition
2404 -- Invariant
2405 -- Type_Invariant
2407 -------------------------------------
2408 -- Build_Assertion_Bodies_For_Type --
2409 -------------------------------------
2411 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2412 begin
2413 if Nkind (Context) = N_Package_Specification then
2415 -- Preanalyze and resolve the class-wide invariants of an
2416 -- interface at the end of whichever declarative part has the
2417 -- interface type. Note that an interface may be declared in
2418 -- any non-package declarative part, but reaching the end of
2419 -- such a declarative part will always freeze the type and
2420 -- generate the invariant procedure (see Freeze_Type).
2422 if Is_Interface (Typ) then
2424 -- Interfaces are treated as the partial view of a private
2425 -- type, in order to achieve uniformity with the general
2426 -- case. As a result, an interface receives only a "partial"
2427 -- invariant procedure, which is never called.
2429 if Has_Own_Invariants (Typ) then
2430 Build_Invariant_Procedure_Body
2431 (Typ => Typ,
2432 Partial_Invariant => True);
2433 end if;
2435 elsif Decls = Visible_Declarations (Context) then
2436 -- Preanalyze and resolve the invariants of a private type
2437 -- at the end of the visible declarations to catch potential
2438 -- errors. Inherited class-wide invariants are not included
2439 -- because they have already been resolved.
2441 if Ekind (Typ) in E_Limited_Private_Type
2442 | E_Private_Type
2443 | E_Record_Type_With_Private
2444 and then Has_Own_Invariants (Typ)
2445 then
2446 Build_Invariant_Procedure_Body
2447 (Typ => Typ,
2448 Partial_Invariant => True);
2449 end if;
2451 -- Preanalyze and resolve the Default_Initial_Condition
2452 -- assertion expression at the end of the declarations to
2453 -- catch any errors.
2455 if Ekind (Typ) in E_Limited_Private_Type
2456 | E_Private_Type
2457 | E_Record_Type_With_Private
2458 and then Has_Own_DIC (Typ)
2459 then
2460 Build_DIC_Procedure_Body
2461 (Typ => Typ,
2462 Partial_DIC => True);
2463 end if;
2465 elsif Decls = Private_Declarations (Context) then
2467 -- Preanalyze and resolve the invariants of a private type's
2468 -- full view at the end of the private declarations to catch
2469 -- potential errors.
2471 if (not Is_Private_Type (Typ)
2472 or else Present (Underlying_Full_View (Typ)))
2473 and then Has_Private_Declaration (Typ)
2474 and then Has_Invariants (Typ)
2475 then
2476 Build_Invariant_Procedure_Body (Typ);
2477 end if;
2479 if (not Is_Private_Type (Typ)
2480 or else Present (Underlying_Full_View (Typ)))
2481 and then Has_Private_Declaration (Typ)
2482 and then Has_DIC (Typ)
2483 then
2484 Build_DIC_Procedure_Body (Typ);
2485 end if;
2486 end if;
2487 end if;
2488 end Build_Assertion_Bodies_For_Type;
2490 -- Local variables
2492 Decl : Node_Id;
2493 Decl_Id : Entity_Id;
2495 -- Start of processing for Build_Assertion_Bodies
2497 begin
2498 Decl := First (Decls);
2499 while Present (Decl) loop
2500 if Is_Declaration (Decl) then
2501 Decl_Id := Defining_Entity (Decl);
2503 if Is_Type (Decl_Id) then
2504 Build_Assertion_Bodies_For_Type (Decl_Id);
2505 end if;
2506 end if;
2508 Next (Decl);
2509 end loop;
2510 end Build_Assertion_Bodies;
2512 ---------------------------
2513 -- Check_Entry_Contracts --
2514 ---------------------------
2516 procedure Check_Entry_Contracts is
2517 ASN : Node_Id;
2518 Ent : Entity_Id;
2519 Exp : Node_Id;
2521 begin
2522 Ent := First_Entity (Current_Scope);
2523 while Present (Ent) loop
2525 -- This only concerns entries with pre/postconditions
2527 if Ekind (Ent) = E_Entry
2528 and then Present (Contract (Ent))
2529 and then Present (Pre_Post_Conditions (Contract (Ent)))
2530 then
2531 ASN := Pre_Post_Conditions (Contract (Ent));
2532 Push_Scope (Ent);
2533 Install_Formals (Ent);
2535 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2536 -- is performed on a copy of the pragma expression, to prevent
2537 -- modifying the original expression.
2539 while Present (ASN) loop
2540 if Nkind (ASN) = N_Pragma then
2541 Exp :=
2542 New_Copy_Tree
2543 (Expression
2544 (First (Pragma_Argument_Associations (ASN))));
2545 Set_Parent (Exp, ASN);
2547 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2548 end if;
2550 ASN := Next_Pragma (ASN);
2551 end loop;
2553 End_Scope;
2554 end if;
2556 Next_Entity (Ent);
2557 end loop;
2558 end Check_Entry_Contracts;
2560 ----------------------------------
2561 -- Contains_Lib_Incomplete_Type --
2562 ----------------------------------
2564 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2565 Curr : Entity_Id;
2567 begin
2568 -- Avoid looking through scopes that do not meet the precondition of
2569 -- Pkg not being within a library unit spec.
2571 if not Is_Compilation_Unit (Pkg)
2572 and then not Is_Generic_Instance (Pkg)
2573 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2574 then
2575 -- Loop through all entities in the current scope to identify
2576 -- an entity that depends on a private type.
2578 Curr := First_Entity (Pkg);
2579 loop
2580 if Nkind (Curr) in N_Entity
2581 and then Depends_On_Private (Curr)
2582 then
2583 return True;
2584 end if;
2586 exit when Last_Entity (Current_Scope) = Curr;
2587 Next_Entity (Curr);
2588 end loop;
2589 end if;
2591 return False;
2592 end Contains_Lib_Incomplete_Type;
2594 --------------------------------------
2595 -- Handle_Late_Controlled_Primitive --
2596 --------------------------------------
2598 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2599 Body_Spec : constant Node_Id := Specification (Body_Decl);
2600 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2601 Loc : constant Source_Ptr := Sloc (Body_Id);
2602 Params : constant List_Id :=
2603 Parameter_Specifications (Body_Spec);
2604 Spec : Node_Id;
2605 Spec_Id : Entity_Id;
2606 Typ : Node_Id;
2608 begin
2609 -- Consider only procedure bodies whose name matches one of the three
2610 -- controlled primitives.
2612 if Nkind (Body_Spec) /= N_Procedure_Specification
2613 or else Chars (Body_Id) not in Name_Adjust
2614 | Name_Finalize
2615 | Name_Initialize
2616 then
2617 return;
2619 -- A controlled primitive must have exactly one formal which is not
2620 -- an anonymous access type.
2622 elsif List_Length (Params) /= 1 then
2623 return;
2624 end if;
2626 Typ := Parameter_Type (First (Params));
2628 if Nkind (Typ) = N_Access_Definition then
2629 return;
2630 end if;
2632 Find_Type (Typ);
2634 -- The type of the formal must be derived from [Limited_]Controlled
2636 if not Is_Controlled (Entity (Typ)) then
2637 return;
2638 end if;
2640 -- Check whether a specification exists for this body. We do not
2641 -- analyze the spec of the body in full, because it will be analyzed
2642 -- again when the body is properly analyzed, and we cannot create
2643 -- duplicate entries in the formals chain. We look for an explicit
2644 -- specification because the body may be an overriding operation and
2645 -- an inherited spec may be present.
2647 Spec_Id := Current_Entity (Body_Id);
2649 while Present (Spec_Id) loop
2650 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2651 and then Scope (Spec_Id) = Current_Scope
2652 and then Present (First_Formal (Spec_Id))
2653 and then No (Next_Formal (First_Formal (Spec_Id)))
2654 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2655 and then Comes_From_Source (Spec_Id)
2656 then
2657 return;
2658 end if;
2660 Spec_Id := Homonym (Spec_Id);
2661 end loop;
2663 -- At this point the body is known to be a late controlled primitive.
2664 -- Generate a matching spec and insert it before the body. Note the
2665 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2666 -- tree in this case.
2668 Spec := Copy_Separate_Tree (Body_Spec);
2670 -- Ensure that the subprogram declaration does not inherit the null
2671 -- indicator from the body as we now have a proper spec/body pair.
2673 Set_Null_Present (Spec, False);
2675 -- Ensure that the freeze node is inserted after the declaration of
2676 -- the primitive since its expansion will freeze the primitive.
2678 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2680 Insert_Before_And_Analyze (Body_Decl, Decl);
2681 end Handle_Late_Controlled_Primitive;
2683 ----------------------------------------
2684 -- Remove_Partial_Visible_Refinements --
2685 ----------------------------------------
2687 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2688 State_Elmt : Elmt_Id;
2689 begin
2690 if Present (Abstract_States (Spec_Id)) then
2691 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2692 while Present (State_Elmt) loop
2693 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2694 Next_Elmt (State_Elmt);
2695 end loop;
2696 end if;
2698 -- For a child unit, also hide the partial state refinement from
2699 -- ancestor packages.
2701 if Is_Child_Unit (Spec_Id) then
2702 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2703 end if;
2704 end Remove_Partial_Visible_Refinements;
2706 --------------------------------
2707 -- Remove_Visible_Refinements --
2708 --------------------------------
2710 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2711 State_Elmt : Elmt_Id;
2712 begin
2713 if Present (Abstract_States (Spec_Id)) then
2714 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2715 while Present (State_Elmt) loop
2716 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2717 Next_Elmt (State_Elmt);
2718 end loop;
2719 end if;
2720 end Remove_Visible_Refinements;
2722 ---------------------
2723 -- Resolve_Aspects --
2724 ---------------------
2726 procedure Resolve_Aspects is
2727 E : Entity_Id;
2729 begin
2730 E := First_Entity (Current_Scope);
2731 while Present (E) loop
2732 Resolve_Aspect_Expressions (E);
2734 -- Now that the aspect expressions have been resolved, if this is
2735 -- at the end of the visible declarations, we can set the flag
2736 -- Known_To_Have_Preelab_Init properly on types declared in the
2737 -- visible part, which is needed for checking whether full types
2738 -- in the private part satisfy the Preelaborable_Initialization
2739 -- aspect of the partial view. We can't wait for the creation of
2740 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2741 -- freeze point may occur after the end of the package declaration
2742 -- (in the case of nested packages).
2744 if Is_Type (E)
2745 and then L = Visible_Declarations (Parent (L))
2746 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2747 then
2748 declare
2749 ASN : constant Node_Id :=
2750 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2751 Expr : constant Node_Id := Expression (ASN);
2752 begin
2753 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2754 -- expression, or if the expression is True (or was folded
2755 -- to True), or if the expression is a conjunction of one or
2756 -- more Preelaborable_Initialization attributes applied to
2757 -- formal types and wasn't folded to False. (Note that
2758 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2759 -- Original_Node if needed, hence test for Standard_False.)
2761 if No (Expr)
2762 or else (Is_Entity_Name (Expr)
2763 and then Entity (Expr) = Standard_True)
2764 or else
2765 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2766 and then
2767 not (Is_Entity_Name (Expr)
2768 and then Entity (Expr) = Standard_False))
2769 then
2770 Set_Known_To_Have_Preelab_Init (E);
2771 end if;
2772 end;
2773 end if;
2775 Next_Entity (E);
2776 end loop;
2777 end Resolve_Aspects;
2779 -- Local variables
2781 Context : Node_Id := Empty;
2782 Ctrl_Typ : Entity_Id := Empty;
2783 Freeze_From : Entity_Id := Empty;
2784 Next_Decl : Node_Id;
2786 -- Start of processing for Analyze_Declarations
2788 begin
2789 Decl := First (L);
2790 while Present (Decl) loop
2792 -- Complete analysis of declaration
2794 Analyze (Decl);
2795 Next_Decl := Next (Decl);
2797 if No (Freeze_From) then
2798 Freeze_From := First_Entity (Current_Scope);
2799 end if;
2801 -- Remember if the declaration we just processed is the full type
2802 -- declaration of a controlled type (to handle late overriding of
2803 -- initialize, adjust or finalize).
2805 if Nkind (Decl) = N_Full_Type_Declaration
2806 and then Is_Controlled (Defining_Identifier (Decl))
2807 then
2808 Ctrl_Typ := Defining_Identifier (Decl);
2809 end if;
2811 -- At the end of a declarative part, freeze remaining entities
2812 -- declared in it. The end of the visible declarations of package
2813 -- specification is not the end of a declarative part if private
2814 -- declarations are present. The end of a package declaration is a
2815 -- freezing point only if it a library package. A task definition or
2816 -- protected type definition is not a freeze point either. Finally,
2817 -- we do not freeze entities in generic scopes, because there is no
2818 -- code generated for them and freeze nodes will be generated for
2819 -- the instance.
2821 -- The end of a package instantiation is not a freeze point, but
2822 -- for now we make it one, because the generic body is inserted
2823 -- (currently) immediately after. Generic instantiations will not
2824 -- be a freeze point once delayed freezing of bodies is implemented.
2825 -- (This is needed in any case for early instantiations ???).
2827 if No (Next_Decl) then
2828 if Nkind (Parent (L)) = N_Component_List then
2829 null;
2831 elsif Nkind (Parent (L)) in
2832 N_Protected_Definition | N_Task_Definition
2833 then
2834 Check_Entry_Contracts;
2836 elsif Nkind (Parent (L)) /= N_Package_Specification then
2837 if Nkind (Parent (L)) = N_Package_Body then
2838 Freeze_From := First_Entity (Current_Scope);
2839 end if;
2841 -- There may have been several freezing points previously,
2842 -- for example object declarations or subprogram bodies, but
2843 -- at the end of a declarative part we check freezing from
2844 -- the beginning, even though entities may already be frozen,
2845 -- in order to perform visibility checks on delayed aspects.
2847 Adjust_Decl;
2849 -- If the current scope is a generic subprogram body. Skip the
2850 -- generic formal parameters that are not frozen here.
2852 if Is_Subprogram (Current_Scope)
2853 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2854 N_Generic_Subprogram_Declaration
2855 and then Present (First_Entity (Current_Scope))
2856 then
2857 while Is_Generic_Formal (Freeze_From) loop
2858 Next_Entity (Freeze_From);
2859 end loop;
2861 Freeze_All (Freeze_From, Decl);
2862 Freeze_From := Last_Entity (Current_Scope);
2864 else
2865 -- For declarations in a subprogram body there is no issue
2866 -- with name resolution in aspect specifications.
2868 Freeze_All (First_Entity (Current_Scope), Decl);
2869 Freeze_From := Last_Entity (Current_Scope);
2870 end if;
2872 -- Current scope is a package specification
2874 elsif Scope (Current_Scope) /= Standard_Standard
2875 and then not Is_Child_Unit (Current_Scope)
2876 and then No (Generic_Parent (Parent (L)))
2877 then
2878 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2879 -- resolved at the end of the immediately enclosing declaration
2880 -- list (AI05-0183-1).
2882 Resolve_Aspects;
2884 elsif L /= Visible_Declarations (Parent (L))
2885 or else Is_Empty_List (Private_Declarations (Parent (L)))
2886 then
2887 Adjust_Decl;
2889 -- End of a package declaration
2891 -- This is a freeze point because it is the end of a
2892 -- compilation unit.
2894 Freeze_All (First_Entity (Current_Scope), Decl);
2895 Freeze_From := Last_Entity (Current_Scope);
2897 -- At the end of the visible declarations the expressions in
2898 -- aspects of all entities declared so far must be resolved.
2899 -- The entities themselves might be frozen later, and the
2900 -- generated pragmas and attribute definition clauses analyzed
2901 -- in full at that point, but name resolution must take place
2902 -- now.
2903 -- In addition to being the proper semantics, this is mandatory
2904 -- within generic units, because global name capture requires
2905 -- those expressions to be analyzed, given that the generated
2906 -- pragmas do not appear in the original generic tree.
2908 elsif Serious_Errors_Detected = 0 then
2909 Resolve_Aspects;
2910 end if;
2912 -- If next node is a body then freeze all types before the body.
2913 -- An exception occurs for some expander-generated bodies. If these
2914 -- are generated at places where in general language rules would not
2915 -- allow a freeze point, then we assume that the expander has
2916 -- explicitly checked that all required types are properly frozen,
2917 -- and we do not cause general freezing here. This special circuit
2918 -- is used when the encountered body is marked as having already
2919 -- been analyzed.
2921 -- In all other cases (bodies that come from source, and expander
2922 -- generated bodies that have not been analyzed yet), freeze all
2923 -- types now. Note that in the latter case, the expander must take
2924 -- care to attach the bodies at a proper place in the tree so as to
2925 -- not cause unwanted freezing at that point.
2927 -- It is also necessary to check for a case where both an expression
2928 -- function is used and the current scope depends on an incomplete
2929 -- private type from a library unit, otherwise premature freezing of
2930 -- the private type will occur.
2932 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2933 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2934 or else not Was_Expression_Function (Next_Decl))
2935 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2936 and then not Contains_Lib_Incomplete_Type
2937 (Current_Scope)))
2938 then
2939 -- When a controlled type is frozen, the expander generates stream
2940 -- and controlled-type support routines. If the freeze is caused
2941 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2942 -- expander will end up using the wrong version of these routines,
2943 -- as the body has not been processed yet. To remedy this, detect
2944 -- a late controlled primitive and create a proper spec for it.
2945 -- This ensures that the primitive will override its inherited
2946 -- counterpart before the freeze takes place.
2948 -- If the declaration we just processed is a body, do not attempt
2949 -- to examine Next_Decl as the late primitive idiom can only apply
2950 -- to the first encountered body.
2952 -- ??? A cleaner approach may be possible and/or this solution
2953 -- could be extended to general-purpose late primitives.
2955 if Present (Ctrl_Typ) then
2957 -- No need to continue searching for late body overriding if
2958 -- the controlled type is already frozen.
2960 if Is_Frozen (Ctrl_Typ) then
2961 Ctrl_Typ := Empty;
2963 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2964 Handle_Late_Controlled_Primitive (Next_Decl);
2965 end if;
2966 end if;
2968 Adjust_Decl;
2970 -- The generated body of an expression function does not freeze,
2971 -- unless it is a completion, in which case only the expression
2972 -- itself freezes. This is handled when the body itself is
2973 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2975 Freeze_All (Freeze_From, Decl);
2976 Freeze_From := Last_Entity (Current_Scope);
2977 end if;
2979 Decl := Next_Decl;
2980 end loop;
2982 -- Post-freezing actions
2984 if Present (L) then
2985 Context := Parent (L);
2987 -- Certain contract annotations have forward visibility semantics and
2988 -- must be analyzed after all declarative items have been processed.
2989 -- This timing ensures that entities referenced by such contracts are
2990 -- visible.
2992 -- Analyze the contract of an immediately enclosing package spec or
2993 -- body first because other contracts may depend on its information.
2995 if Nkind (Context) = N_Package_Body then
2996 Analyze_Package_Body_Contract (Defining_Entity (Context));
2998 elsif Nkind (Context) = N_Package_Specification then
2999 Analyze_Package_Contract (Defining_Entity (Context));
3000 end if;
3002 -- Analyze the contracts of various constructs in the declarative
3003 -- list.
3005 Analyze_Contracts (L);
3007 if Nkind (Context) = N_Package_Body then
3009 -- Ensure that all abstract states and objects declared in the
3010 -- state space of a package body are utilized as constituents.
3012 Check_Unused_Body_States (Defining_Entity (Context));
3014 -- State refinements are visible up to the end of the package body
3015 -- declarations. Hide the state refinements from visibility to
3016 -- restore the original state conditions.
3018 Remove_Visible_Refinements (Corresponding_Spec (Context));
3019 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
3021 elsif Nkind (Context) = N_Package_Specification then
3023 -- Partial state refinements are visible up to the end of the
3024 -- package spec declarations. Hide the partial state refinements
3025 -- from visibility to restore the original state conditions.
3027 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
3028 end if;
3030 -- Verify that all abstract states found in any package declared in
3031 -- the input declarative list have proper refinements. The check is
3032 -- performed only when the context denotes a block, entry, package,
3033 -- protected, subprogram, or task body (SPARK RM 7.1.4(4) and SPARK
3034 -- RM 7.2.2(3)).
3036 Check_State_Refinements (Context);
3038 -- Create the subprogram bodies which verify the run-time semantics
3039 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
3040 -- types within the current declarative list. This ensures that all
3041 -- assertion expressions are preanalyzed and resolved at the end of
3042 -- the declarative part. Note that the resolution happens even when
3043 -- freezing does not take place.
3045 Build_Assertion_Bodies (L, Context);
3046 end if;
3047 end Analyze_Declarations;
3049 -----------------------------------
3050 -- Analyze_Full_Type_Declaration --
3051 -----------------------------------
3053 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
3054 Def : constant Node_Id := Type_Definition (N);
3055 Def_Id : constant Entity_Id := Defining_Identifier (N);
3056 T : Entity_Id;
3057 Prev : Entity_Id;
3059 Is_Remote : constant Boolean :=
3060 (Is_Remote_Types (Current_Scope)
3061 or else Is_Remote_Call_Interface (Current_Scope))
3062 and then not (In_Private_Part (Current_Scope)
3063 or else In_Package_Body (Current_Scope));
3065 procedure Check_Nonoverridable_Aspects;
3066 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
3067 -- be overridden, and can only be confirmed on derivation.
3069 procedure Check_Ops_From_Incomplete_Type;
3070 -- If there is a tagged incomplete partial view of the type, traverse
3071 -- the primitives of the incomplete view and change the type of any
3072 -- controlling formals and result to indicate the full view. The
3073 -- primitives will be added to the full type's primitive operations
3074 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
3075 -- is called from Process_Incomplete_Dependents).
3077 ----------------------------------
3078 -- Check_Nonoverridable_Aspects --
3079 ----------------------------------
3081 procedure Check_Nonoverridable_Aspects is
3082 function Get_Aspect_Spec
3083 (Specs : List_Id;
3084 Aspect_Name : Name_Id) return Node_Id;
3085 -- Check whether a list of aspect specifications includes an entry
3086 -- for a specific aspect. The list is either that of a partial or
3087 -- a full view.
3089 ---------------------
3090 -- Get_Aspect_Spec --
3091 ---------------------
3093 function Get_Aspect_Spec
3094 (Specs : List_Id;
3095 Aspect_Name : Name_Id) return Node_Id
3097 Spec : Node_Id;
3099 begin
3100 Spec := First (Specs);
3101 while Present (Spec) loop
3102 if Chars (Identifier (Spec)) = Aspect_Name then
3103 return Spec;
3104 end if;
3105 Next (Spec);
3106 end loop;
3108 return Empty;
3109 end Get_Aspect_Spec;
3111 -- Local variables
3113 Prev_Aspects : constant List_Id :=
3114 Aspect_Specifications (Parent (Def_Id));
3115 Par_Type : Entity_Id;
3116 Prev_Aspect : Node_Id;
3118 -- Start of processing for Check_Nonoverridable_Aspects
3120 begin
3121 -- Get parent type of derived type. Note that Prev is the entity in
3122 -- the partial declaration, but its contents are now those of full
3123 -- view, while Def_Id reflects the partial view.
3125 if Is_Private_Type (Def_Id) then
3126 Par_Type := Etype (Full_View (Def_Id));
3127 else
3128 Par_Type := Etype (Def_Id);
3129 end if;
3131 -- If there is an inherited Implicit_Dereference, verify that it is
3132 -- made explicit in the partial view.
3134 if Has_Discriminants (Base_Type (Par_Type))
3135 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3136 and then Present (Discriminant_Specifications (Parent (Prev)))
3137 and then Present (Get_Reference_Discriminant (Par_Type))
3138 then
3139 Prev_Aspect :=
3140 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3142 if No (Prev_Aspect)
3143 and then Present
3144 (Discriminant_Specifications
3145 (Original_Node (Parent (Prev))))
3146 then
3147 Error_Msg_N
3148 ("type does not inherit implicit dereference", Prev);
3150 else
3151 -- If one of the views has the aspect specified, verify that it
3152 -- is consistent with that of the parent.
3154 declare
3155 Cur_Discr : constant Entity_Id :=
3156 Get_Reference_Discriminant (Prev);
3157 Par_Discr : constant Entity_Id :=
3158 Get_Reference_Discriminant (Par_Type);
3160 begin
3161 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3162 Error_Msg_N
3163 ("aspect inconsistent with that of parent", N);
3164 end if;
3166 -- Check that specification in partial view matches the
3167 -- inherited aspect. Compare names directly because aspect
3168 -- expression may not be analyzed.
3170 if Present (Prev_Aspect)
3171 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3172 and then Chars (Expression (Prev_Aspect)) /=
3173 Chars (Cur_Discr)
3174 then
3175 Error_Msg_N
3176 ("aspect inconsistent with that of parent", N);
3177 end if;
3178 end;
3179 end if;
3180 end if;
3182 -- What about other nonoverridable aspects???
3183 end Check_Nonoverridable_Aspects;
3185 ------------------------------------
3186 -- Check_Ops_From_Incomplete_Type --
3187 ------------------------------------
3189 procedure Check_Ops_From_Incomplete_Type is
3190 Elmt : Elmt_Id;
3191 Formal : Entity_Id;
3192 Op : Entity_Id;
3194 begin
3195 if Prev /= T
3196 and then Ekind (Prev) = E_Incomplete_Type
3197 and then Is_Tagged_Type (Prev)
3198 and then Is_Tagged_Type (T)
3199 and then Present (Primitive_Operations (Prev))
3200 then
3201 Elmt := First_Elmt (Primitive_Operations (Prev));
3202 while Present (Elmt) loop
3203 Op := Node (Elmt);
3205 Formal := First_Formal (Op);
3206 while Present (Formal) loop
3207 if Etype (Formal) = Prev then
3208 Set_Etype (Formal, T);
3209 end if;
3211 Next_Formal (Formal);
3212 end loop;
3214 if Etype (Op) = Prev then
3215 Set_Etype (Op, T);
3216 end if;
3218 Next_Elmt (Elmt);
3219 end loop;
3220 end if;
3221 end Check_Ops_From_Incomplete_Type;
3223 -- Start of processing for Analyze_Full_Type_Declaration
3225 begin
3226 Prev := Find_Type_Name (N);
3228 -- The full view, if present, now points to the current type. If there
3229 -- is an incomplete partial view, set a link to it, to simplify the
3230 -- retrieval of primitive operations of the type.
3232 -- Ada 2005 (AI-50217): If the type was previously decorated when
3233 -- imported through a LIMITED WITH clause, it appears as incomplete
3234 -- but has no full view.
3236 if Ekind (Prev) = E_Incomplete_Type
3237 and then Present (Full_View (Prev))
3238 then
3239 T := Full_View (Prev);
3240 Set_Incomplete_View (N, Prev);
3241 else
3242 T := Prev;
3243 end if;
3245 Set_Is_Pure (T, Is_Pure (Current_Scope));
3247 -- We set the flag Is_First_Subtype here. It is needed to set the
3248 -- corresponding flag for the Implicit class-wide-type created
3249 -- during tagged types processing.
3251 Set_Is_First_Subtype (T, True);
3253 -- Only composite types other than array types are allowed to have
3254 -- discriminants.
3256 case Nkind (Def) is
3258 -- For derived types, the rule will be checked once we've figured
3259 -- out the parent type.
3261 when N_Derived_Type_Definition =>
3262 null;
3264 -- For record types, discriminants are allowed.
3266 when N_Record_Definition =>
3267 null;
3269 when others =>
3270 if Present (Discriminant_Specifications (N)) then
3271 Error_Msg_N
3272 ("elementary or array type cannot have discriminants",
3273 Defining_Identifier
3274 (First (Discriminant_Specifications (N))));
3275 end if;
3276 end case;
3278 -- Elaborate the type definition according to kind, and generate
3279 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3280 -- already done (this happens during the reanalysis that follows a call
3281 -- to the high level optimizer).
3283 if not Analyzed (T) then
3284 Set_Analyzed (T);
3286 -- Set the SPARK mode from the current context
3288 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3289 Set_SPARK_Pragma_Inherited (T);
3291 case Nkind (Def) is
3292 when N_Access_To_Subprogram_Definition =>
3293 Access_Subprogram_Declaration (T, Def);
3295 -- If this is a remote access to subprogram, we must create the
3296 -- equivalent fat pointer type, and related subprograms.
3298 if Is_Remote then
3299 Process_Remote_AST_Declaration (N);
3300 end if;
3302 -- Validate categorization rule against access type declaration
3303 -- usually a violation in Pure unit, Shared_Passive unit.
3305 Validate_Access_Type_Declaration (T, N);
3307 -- If the type has contracts, we create the corresponding
3308 -- wrapper at once, before analyzing the aspect specifications,
3309 -- so that pre/postconditions can be handled directly on the
3310 -- generated wrapper.
3312 if Ada_Version >= Ada_2022
3313 and then Present (Aspect_Specifications (N))
3314 and then Expander_Active
3315 then
3316 Build_Access_Subprogram_Wrapper (N);
3317 end if;
3319 when N_Access_To_Object_Definition =>
3320 Access_Type_Declaration (T, Def);
3322 -- Validate categorization rule against access type declaration
3323 -- usually a violation in Pure unit, Shared_Passive unit.
3325 Validate_Access_Type_Declaration (T, N);
3327 -- If we are in a Remote_Call_Interface package and define a
3328 -- RACW, then calling stubs and specific stream attributes
3329 -- must be added.
3331 if Is_Remote
3332 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3333 then
3334 Add_RACW_Features (Def_Id);
3335 end if;
3337 when N_Array_Type_Definition =>
3338 Array_Type_Declaration (T, Def);
3340 when N_Derived_Type_Definition =>
3341 Derived_Type_Declaration (T, N, T /= Def_Id);
3343 -- Save the scenario for examination by the ABE Processing
3344 -- phase.
3346 Record_Elaboration_Scenario (N);
3348 when N_Enumeration_Type_Definition =>
3349 Enumeration_Type_Declaration (T, Def);
3351 when N_Floating_Point_Definition =>
3352 Floating_Point_Type_Declaration (T, Def);
3354 when N_Decimal_Fixed_Point_Definition =>
3355 Decimal_Fixed_Point_Type_Declaration (T, Def);
3357 when N_Ordinary_Fixed_Point_Definition =>
3358 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3360 when N_Signed_Integer_Type_Definition =>
3361 Signed_Integer_Type_Declaration (T, Def);
3363 when N_Modular_Type_Definition =>
3364 Modular_Type_Declaration (T, Def);
3366 when N_Record_Definition =>
3367 Record_Type_Declaration (T, N, Prev);
3369 -- If declaration has a parse error, nothing to elaborate.
3371 when N_Error =>
3372 null;
3374 when others =>
3375 raise Program_Error;
3376 end case;
3377 end if;
3379 if Etype (T) = Any_Type then
3380 return;
3381 end if;
3383 -- Set the primitives list of the full type and its base type when
3384 -- needed. T may be E_Void in cases of earlier errors, and in that
3385 -- case we bypass this.
3387 if Ekind (T) /= E_Void then
3388 if not Present (Direct_Primitive_Operations (T)) then
3389 if Etype (T) = T then
3390 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3392 -- If Etype of T is the base type (as opposed to a parent type)
3393 -- and already has an associated list of primitive operations,
3394 -- then set T's primitive list to the base type's list. Otherwise,
3395 -- create a new empty primitives list and share the list between
3396 -- T and its base type. The lists need to be shared in common.
3398 elsif Etype (T) = Base_Type (T) then
3400 if not Present (Direct_Primitive_Operations (Base_Type (T)))
3401 then
3402 Set_Direct_Primitive_Operations
3403 (Base_Type (T), New_Elmt_List);
3404 end if;
3406 Set_Direct_Primitive_Operations
3407 (T, Direct_Primitive_Operations (Base_Type (T)));
3409 -- Case where the Etype is a parent type, so we need a new
3410 -- primitives list for T.
3412 else
3413 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3414 end if;
3416 -- If T already has a Direct_Primitive_Operations list but its
3417 -- base type doesn't then set the base type's list to T's list.
3419 elsif not Present (Direct_Primitive_Operations (Base_Type (T))) then
3420 Set_Direct_Primitive_Operations
3421 (Base_Type (T), Direct_Primitive_Operations (T));
3422 end if;
3423 end if;
3425 -- Some common processing for all types
3427 Set_Depends_On_Private (T, Has_Private_Component (T));
3428 Check_Ops_From_Incomplete_Type;
3430 -- Both the declared entity, and its anonymous base type if one was
3431 -- created, need freeze nodes allocated.
3433 declare
3434 B : constant Entity_Id := Base_Type (T);
3436 begin
3437 -- In the case where the base type differs from the first subtype, we
3438 -- pre-allocate a freeze node, and set the proper link to the first
3439 -- subtype. Freeze_Entity will use this preallocated freeze node when
3440 -- it freezes the entity.
3442 -- This does not apply if the base type is a generic type, whose
3443 -- declaration is independent of the current derived definition.
3445 if B /= T and then not Is_Generic_Type (B) then
3446 Ensure_Freeze_Node (B);
3447 Set_First_Subtype_Link (Freeze_Node (B), T);
3448 end if;
3450 -- A type that is imported through a limited_with clause cannot
3451 -- generate any code, and thus need not be frozen. However, an access
3452 -- type with an imported designated type needs a finalization list,
3453 -- which may be referenced in some other package that has non-limited
3454 -- visibility on the designated type. Thus we must create the
3455 -- finalization list at the point the access type is frozen, to
3456 -- prevent unsatisfied references at link time.
3458 if not From_Limited_With (T) or else Is_Access_Type (T) then
3459 Set_Has_Delayed_Freeze (T);
3460 end if;
3461 end;
3463 -- Case where T is the full declaration of some private type which has
3464 -- been swapped in Defining_Identifier (N).
3466 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3467 Process_Full_View (N, T, Def_Id);
3469 -- Record the reference. The form of this is a little strange, since
3470 -- the full declaration has been swapped in. So the first parameter
3471 -- here represents the entity to which a reference is made which is
3472 -- the "real" entity, i.e. the one swapped in, and the second
3473 -- parameter provides the reference location.
3475 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3476 -- since we don't want a complaint about the full type being an
3477 -- unwanted reference to the private type
3479 declare
3480 B : constant Boolean := Has_Pragma_Unreferenced (T);
3481 begin
3482 Set_Has_Pragma_Unreferenced (T, False);
3483 Generate_Reference (T, T, 'c');
3484 Set_Has_Pragma_Unreferenced (T, B);
3485 end;
3487 Set_Completion_Referenced (Def_Id);
3489 -- For completion of incomplete type, process incomplete dependents
3490 -- and always mark the full type as referenced (it is the incomplete
3491 -- type that we get for any real reference).
3493 elsif Ekind (Prev) = E_Incomplete_Type then
3494 Process_Incomplete_Dependents (N, T, Prev);
3495 Generate_Reference (Prev, Def_Id, 'c');
3496 Set_Completion_Referenced (Def_Id);
3498 -- If not private type or incomplete type completion, this is a real
3499 -- definition of a new entity, so record it.
3501 else
3502 Generate_Definition (Def_Id);
3503 end if;
3505 -- Propagate any pending access types whose finalization masters need to
3506 -- be fully initialized from the partial to the full view. Guard against
3507 -- an illegal full view that remains unanalyzed.
3509 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3510 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3511 end if;
3513 if Chars (Scope (Def_Id)) = Name_System
3514 and then Chars (Def_Id) = Name_Address
3515 and then In_Predefined_Unit (N)
3516 then
3517 Set_Is_Descendant_Of_Address (Def_Id);
3518 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3519 Set_Is_Descendant_Of_Address (Prev);
3520 end if;
3522 Set_Optimize_Alignment_Flags (Def_Id);
3523 Check_Eliminated (Def_Id);
3525 -- If the declaration is a completion and aspects are present, apply
3526 -- them to the entity for the type which is currently the partial
3527 -- view, but which is the one that will be frozen.
3529 if Has_Aspects (N) then
3531 -- In most cases the partial view is a private type, and both views
3532 -- appear in different declarative parts. In the unusual case where
3533 -- the partial view is incomplete, perform the analysis on the
3534 -- full view, to prevent freezing anomalies with the corresponding
3535 -- class-wide type, which otherwise might be frozen before the
3536 -- dispatch table is built.
3538 if Prev /= Def_Id
3539 and then Ekind (Prev) /= E_Incomplete_Type
3540 then
3541 Analyze_Aspect_Specifications (N, Prev);
3543 -- Normal case
3545 else
3546 Analyze_Aspect_Specifications (N, Def_Id);
3547 end if;
3548 end if;
3550 if Is_Derived_Type (Prev)
3551 and then Def_Id /= Prev
3552 then
3553 Check_Nonoverridable_Aspects;
3554 end if;
3556 -- Check for tagged type declaration at library level
3558 if Is_Tagged_Type (T)
3559 and then not Is_Library_Level_Entity (T)
3560 then
3561 Check_Restriction (No_Local_Tagged_Types, T);
3562 end if;
3563 end Analyze_Full_Type_Declaration;
3565 ----------------------------------
3566 -- Analyze_Incomplete_Type_Decl --
3567 ----------------------------------
3569 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3570 F : constant Boolean := Is_Pure (Current_Scope);
3571 T : Entity_Id;
3573 begin
3574 Generate_Definition (Defining_Identifier (N));
3576 -- Process an incomplete declaration. The identifier must not have been
3577 -- declared already in the scope. However, an incomplete declaration may
3578 -- appear in the private part of a package, for a private type that has
3579 -- already been declared.
3581 -- In this case, the discriminants (if any) must match
3583 T := Find_Type_Name (N);
3585 Mutate_Ekind (T, E_Incomplete_Type);
3586 Set_Etype (T, T);
3587 Set_Is_First_Subtype (T);
3588 Reinit_Size_Align (T);
3590 -- Set the SPARK mode from the current context
3592 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3593 Set_SPARK_Pragma_Inherited (T);
3595 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3596 -- incomplete types.
3598 if Tagged_Present (N) then
3599 Set_Is_Tagged_Type (T, True);
3600 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3601 Make_Class_Wide_Type (T);
3602 end if;
3604 -- Initialize the list of primitive operations to an empty list,
3605 -- to cover tagged types as well as untagged types. For untagged
3606 -- types this is used either to analyze the call as legal when
3607 -- Core_Extensions_Allowed is True, or to issue a better error message
3608 -- otherwise.
3610 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3612 Set_Stored_Constraint (T, No_Elist);
3614 if Present (Discriminant_Specifications (N)) then
3615 Push_Scope (T);
3616 Process_Discriminants (N);
3617 End_Scope;
3618 end if;
3620 -- If the type has discriminants, nontrivial subtypes may be declared
3621 -- before the full view of the type. The full views of those subtypes
3622 -- will be built after the full view of the type.
3624 Set_Private_Dependents (T, New_Elmt_List);
3625 Set_Is_Pure (T, F);
3626 end Analyze_Incomplete_Type_Decl;
3628 -----------------------------------
3629 -- Analyze_Interface_Declaration --
3630 -----------------------------------
3632 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3633 CW : constant Entity_Id := Class_Wide_Type (T);
3635 begin
3636 Set_Is_Tagged_Type (T);
3637 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3639 Set_Is_Limited_Record (T, Limited_Present (Def)
3640 or else Task_Present (Def)
3641 or else Protected_Present (Def)
3642 or else Synchronized_Present (Def));
3644 -- Type is abstract if full declaration carries keyword, or if previous
3645 -- partial view did.
3647 Set_Is_Abstract_Type (T);
3648 Set_Is_Interface (T);
3650 -- Type is a limited interface if it includes the keyword limited, task,
3651 -- protected, or synchronized.
3653 Set_Is_Limited_Interface
3654 (T, Limited_Present (Def)
3655 or else Protected_Present (Def)
3656 or else Synchronized_Present (Def)
3657 or else Task_Present (Def));
3659 Set_Interfaces (T, New_Elmt_List);
3660 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3662 -- Complete the decoration of the class-wide entity if it was already
3663 -- built (i.e. during the creation of the limited view)
3665 if Present (CW) then
3666 Set_Is_Interface (CW);
3667 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3668 end if;
3670 -- Check runtime support for synchronized interfaces
3672 if Is_Concurrent_Interface (T)
3673 and then not RTE_Available (RE_Select_Specific_Data)
3674 then
3675 Error_Msg_CRT ("synchronized interfaces", T);
3676 end if;
3677 end Analyze_Interface_Declaration;
3679 -----------------------------
3680 -- Analyze_Itype_Reference --
3681 -----------------------------
3683 -- Nothing to do. This node is placed in the tree only for the benefit of
3684 -- back end processing, and has no effect on the semantic processing.
3686 procedure Analyze_Itype_Reference (N : Node_Id) is
3687 begin
3688 pragma Assert (Is_Itype (Itype (N)));
3689 null;
3690 end Analyze_Itype_Reference;
3692 --------------------------------
3693 -- Analyze_Number_Declaration --
3694 --------------------------------
3696 procedure Analyze_Number_Declaration (N : Node_Id) is
3697 E : constant Node_Id := Expression (N);
3698 Id : constant Entity_Id := Defining_Identifier (N);
3699 Index : Interp_Index;
3700 It : Interp;
3701 T : Entity_Id;
3703 begin
3704 Generate_Definition (Id);
3705 Enter_Name (Id);
3707 -- This is an optimization of a common case of an integer literal
3709 if Nkind (E) = N_Integer_Literal then
3710 Set_Is_Static_Expression (E, True);
3711 Set_Etype (E, Universal_Integer);
3713 Set_Etype (Id, Universal_Integer);
3714 Mutate_Ekind (Id, E_Named_Integer);
3715 Set_Is_Frozen (Id, True);
3717 Set_Debug_Info_Needed (Id);
3718 return;
3719 end if;
3721 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3723 -- Process expression, replacing error by integer zero, to avoid
3724 -- cascaded errors or aborts further along in the processing
3726 -- Replace Error by integer zero, which seems least likely to cause
3727 -- cascaded errors.
3729 if E = Error then
3730 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3731 Set_Error_Posted (E);
3732 end if;
3734 Analyze (E);
3736 -- Verify that the expression is static and numeric. If
3737 -- the expression is overloaded, we apply the preference
3738 -- rule that favors root numeric types.
3740 if not Is_Overloaded (E) then
3741 T := Etype (E);
3742 if Has_Dynamic_Predicate_Aspect (T)
3743 or else Has_Ghost_Predicate_Aspect (T)
3744 then
3745 Error_Msg_N
3746 ("subtype has non-static predicate, "
3747 & "not allowed in number declaration", N);
3748 end if;
3750 else
3751 T := Any_Type;
3753 Get_First_Interp (E, Index, It);
3754 while Present (It.Typ) loop
3755 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3756 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3757 then
3758 if T = Any_Type then
3759 T := It.Typ;
3761 elsif Is_Universal_Numeric_Type (It.Typ) then
3762 -- Choose universal interpretation over any other
3764 T := It.Typ;
3765 exit;
3766 end if;
3767 end if;
3769 Get_Next_Interp (Index, It);
3770 end loop;
3771 end if;
3773 if Is_Integer_Type (T) then
3774 Resolve (E, T);
3775 Set_Etype (Id, Universal_Integer);
3776 Mutate_Ekind (Id, E_Named_Integer);
3778 elsif Is_Real_Type (T) then
3780 -- Because the real value is converted to universal_real, this is a
3781 -- legal context for a universal fixed expression.
3783 if T = Universal_Fixed then
3784 declare
3785 Loc : constant Source_Ptr := Sloc (N);
3786 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3787 Subtype_Mark =>
3788 New_Occurrence_Of (Universal_Real, Loc),
3789 Expression => Relocate_Node (E));
3791 begin
3792 Rewrite (E, Conv);
3793 Analyze (E);
3794 end;
3796 elsif T = Any_Fixed then
3797 Error_Msg_N ("illegal context for mixed mode operation", E);
3799 -- Expression is of the form : universal_fixed * integer. Try to
3800 -- resolve as universal_real.
3802 T := Universal_Real;
3803 Set_Etype (E, T);
3804 end if;
3806 Resolve (E, T);
3807 Set_Etype (Id, Universal_Real);
3808 Mutate_Ekind (Id, E_Named_Real);
3810 else
3811 Wrong_Type (E, Any_Numeric);
3812 Resolve (E, T);
3814 Set_Etype (Id, T);
3815 Mutate_Ekind (Id, E_Constant);
3816 Set_Never_Set_In_Source (Id, True);
3817 Set_Is_True_Constant (Id, True);
3818 return;
3819 end if;
3821 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3822 Set_Etype (E, Etype (Id));
3823 end if;
3825 if not Is_OK_Static_Expression (E) then
3826 Flag_Non_Static_Expr
3827 ("non-static expression used in number declaration!", E);
3828 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3829 Set_Etype (E, Any_Type);
3830 end if;
3832 Analyze_Dimension (N);
3833 end Analyze_Number_Declaration;
3835 --------------------------------
3836 -- Analyze_Object_Declaration --
3837 --------------------------------
3839 -- WARNING: This routine manages Ghost regions. Return statements must be
3840 -- replaced by gotos which jump to the end of the routine and restore the
3841 -- Ghost mode.
3843 procedure Analyze_Object_Declaration (N : Node_Id) is
3844 Loc : constant Source_Ptr := Sloc (N);
3845 Id : constant Entity_Id := Defining_Identifier (N);
3846 Next_Decl : constant Node_Id := Next (N);
3848 Act_T : Entity_Id;
3849 T : Entity_Id;
3851 E : Node_Id := Expression (N);
3852 -- E is set to Expression (N) throughout this routine. When Expression
3853 -- (N) is modified, E is changed accordingly.
3855 procedure Check_Dynamic_Object (Typ : Entity_Id);
3856 -- A library-level object with nonstatic discriminant constraints may
3857 -- require dynamic allocation. The declaration is illegal if the
3858 -- profile includes the restriction No_Implicit_Heap_Allocations.
3860 procedure Check_For_Null_Excluding_Components
3861 (Obj_Typ : Entity_Id;
3862 Obj_Decl : Node_Id);
3863 -- Verify that each null-excluding component of object declaration
3864 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3865 -- a compile-time warning if this is not the case.
3867 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
3868 -- Check that the return subtype indication properly matches the result
3869 -- subtype of the function in an extended return object declaration, as
3870 -- required by RM 6.5(5.1/2-5.3/2).
3872 function Count_Tasks (T : Entity_Id) return Uint;
3873 -- This function is called when a non-generic library level object of a
3874 -- task type is declared. Its function is to count the static number of
3875 -- tasks declared within the type (it is only called if Has_Task is set
3876 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3877 -- or a variant record type is encountered, Check_Restriction is called
3878 -- indicating the count is unknown.
3880 function Delayed_Aspect_Present return Boolean;
3881 -- If the declaration has an expression that is an aggregate, and it
3882 -- has aspects that require delayed analysis, the resolution of the
3883 -- aggregate must be deferred to the freeze point of the object. This
3884 -- special processing was created for address clauses, but it must
3885 -- also apply to address aspects. This must be done before the aspect
3886 -- specifications are analyzed because we must handle the aggregate
3887 -- before the analysis of the object declaration is complete.
3889 -- Any other relevant delayed aspects on object declarations ???
3891 --------------------------
3892 -- Check_Dynamic_Object --
3893 --------------------------
3895 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3896 Comp : Entity_Id;
3897 Obj_Type : Entity_Id;
3899 begin
3900 Obj_Type := Typ;
3902 if Is_Private_Type (Obj_Type)
3903 and then Present (Full_View (Obj_Type))
3904 then
3905 Obj_Type := Full_View (Obj_Type);
3906 end if;
3908 if Known_Static_Esize (Obj_Type) then
3909 return;
3910 end if;
3912 if Restriction_Active (No_Implicit_Heap_Allocations)
3913 and then Expander_Active
3914 and then Has_Discriminants (Obj_Type)
3915 then
3916 Comp := First_Component (Obj_Type);
3917 while Present (Comp) loop
3918 if Known_Static_Esize (Etype (Comp))
3919 or else Size_Known_At_Compile_Time (Etype (Comp))
3920 then
3921 null;
3923 elsif Is_Record_Type (Etype (Comp)) then
3924 Check_Dynamic_Object (Etype (Comp));
3926 elsif not Discriminated_Size (Comp)
3927 and then Comes_From_Source (Comp)
3928 then
3929 Error_Msg_NE
3930 ("component& of non-static size will violate restriction "
3931 & "No_Implicit_Heap_Allocation?", N, Comp);
3933 end if;
3935 Next_Component (Comp);
3936 end loop;
3937 end if;
3938 end Check_Dynamic_Object;
3940 -----------------------------------------
3941 -- Check_For_Null_Excluding_Components --
3942 -----------------------------------------
3944 procedure Check_For_Null_Excluding_Components
3945 (Obj_Typ : Entity_Id;
3946 Obj_Decl : Node_Id)
3948 procedure Check_Component
3949 (Comp_Typ : Entity_Id;
3950 Comp_Decl : Node_Id := Empty;
3951 Array_Comp : Boolean := False);
3952 -- Apply a compile-time null-exclusion check on a component denoted
3953 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3954 -- subcomponents (if any).
3956 ---------------------
3957 -- Check_Component --
3958 ---------------------
3960 procedure Check_Component
3961 (Comp_Typ : Entity_Id;
3962 Comp_Decl : Node_Id := Empty;
3963 Array_Comp : Boolean := False)
3965 Comp : Entity_Id;
3966 T : Entity_Id;
3968 begin
3969 -- Do not consider internally-generated components or those that
3970 -- are already initialized.
3972 if Present (Comp_Decl)
3973 and then (not Comes_From_Source (Comp_Decl)
3974 or else Present (Expression (Comp_Decl)))
3975 then
3976 return;
3977 end if;
3979 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3980 and then Present (Full_View (Comp_Typ))
3981 then
3982 T := Full_View (Comp_Typ);
3983 else
3984 T := Comp_Typ;
3985 end if;
3987 -- Verify a component of a null-excluding access type
3989 if Is_Access_Type (T)
3990 and then Can_Never_Be_Null (T)
3991 then
3992 if Comp_Decl = Obj_Decl then
3993 Null_Exclusion_Static_Checks
3994 (N => Obj_Decl,
3995 Comp => Empty,
3996 Array_Comp => Array_Comp);
3998 else
3999 Null_Exclusion_Static_Checks
4000 (N => Obj_Decl,
4001 Comp => Comp_Decl,
4002 Array_Comp => Array_Comp);
4003 end if;
4005 -- Check array components
4007 elsif Is_Array_Type (T) then
4009 -- There is no suitable component when the object is of an
4010 -- array type. However, a namable component may appear at some
4011 -- point during the recursive inspection, but not at the top
4012 -- level. At the top level just indicate array component case.
4014 if Comp_Decl = Obj_Decl then
4015 Check_Component (Component_Type (T), Array_Comp => True);
4016 else
4017 Check_Component (Component_Type (T), Comp_Decl);
4018 end if;
4020 -- Verify all components of type T
4022 -- Note: No checks are performed on types with discriminants due
4023 -- to complexities involving variants. ???
4025 elsif (Is_Concurrent_Type (T)
4026 or else Is_Incomplete_Or_Private_Type (T)
4027 or else Is_Record_Type (T))
4028 and then not Has_Discriminants (T)
4029 then
4030 Comp := First_Component (T);
4031 while Present (Comp) loop
4032 Check_Component (Etype (Comp), Parent (Comp));
4034 Next_Component (Comp);
4035 end loop;
4036 end if;
4037 end Check_Component;
4039 -- Start processing for Check_For_Null_Excluding_Components
4041 begin
4042 Check_Component (Obj_Typ, Obj_Decl);
4043 end Check_For_Null_Excluding_Components;
4045 -------------------------------------
4046 -- Check_Return_Subtype_Indication --
4047 -------------------------------------
4049 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
4050 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
4051 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4052 Func_Id : constant Entity_Id := Return_Applies_To (Scope (Obj_Id));
4053 R_Typ : constant Entity_Id := Etype (Func_Id);
4054 Indic : constant Node_Id :=
4055 Object_Definition (Original_Node (Obj_Decl));
4057 procedure Error_No_Match (N : Node_Id);
4058 -- Output error messages for case where types do not statically
4059 -- match. N is the location for the messages.
4061 --------------------
4062 -- Error_No_Match --
4063 --------------------
4065 procedure Error_No_Match (N : Node_Id) is
4066 begin
4067 Error_Msg_N
4068 ("subtype must statically match function result subtype", N);
4070 if not Predicates_Match (Obj_Typ, R_Typ) then
4071 Error_Msg_Node_2 := R_Typ;
4072 Error_Msg_NE
4073 ("\predicate of& does not match predicate of&",
4074 N, Obj_Typ);
4075 end if;
4076 end Error_No_Match;
4078 -- Start of processing for Check_Return_Subtype_Indication
4080 begin
4081 -- First, avoid cascaded errors
4083 if Error_Posted (Obj_Decl) or else Error_Posted (Indic) then
4084 return;
4085 end if;
4087 -- "return access T" case; check that the return statement also has
4088 -- "access T", and that the subtypes statically match:
4089 -- if this is an access to subprogram the signatures must match.
4091 if Is_Anonymous_Access_Type (R_Typ) then
4092 if Is_Anonymous_Access_Type (Obj_Typ) then
4093 if Ekind (Designated_Type (Obj_Typ)) /= E_Subprogram_Type
4094 then
4095 if Base_Type (Designated_Type (Obj_Typ)) /=
4096 Base_Type (Designated_Type (R_Typ))
4097 or else not Subtypes_Statically_Match (Obj_Typ, R_Typ)
4098 then
4099 Error_No_Match (Subtype_Mark (Indic));
4100 end if;
4102 else
4103 -- For two anonymous access to subprogram types, the types
4104 -- themselves must be type conformant.
4106 if not Conforming_Types
4107 (Obj_Typ, R_Typ, Fully_Conformant)
4108 then
4109 Error_No_Match (Indic);
4110 end if;
4111 end if;
4113 else
4114 Error_Msg_N ("must use anonymous access type", Indic);
4115 end if;
4117 -- If the return object is of an anonymous access type, then report
4118 -- an error if the function's result type is not also anonymous.
4120 elsif Is_Anonymous_Access_Type (Obj_Typ) then
4121 pragma Assert (not Is_Anonymous_Access_Type (R_Typ));
4122 Error_Msg_N
4123 ("anonymous access not allowed for function with named access "
4124 & "result", Indic);
4126 -- Subtype indication case: check that the return object's type is
4127 -- covered by the result type, and that the subtypes statically match
4128 -- when the result subtype is constrained. Also handle record types
4129 -- with unknown discriminants for which we have built the underlying
4130 -- record view. Coverage is needed to allow specific-type return
4131 -- objects when the result type is class-wide (see AI05-32).
4133 elsif Covers (Base_Type (R_Typ), Base_Type (Obj_Typ))
4134 or else (Is_Underlying_Record_View (Base_Type (Obj_Typ))
4135 and then
4136 Covers
4137 (Base_Type (R_Typ),
4138 Underlying_Record_View (Base_Type (Obj_Typ))))
4139 then
4140 -- A null exclusion may be present on the return type, on the
4141 -- function specification, on the object declaration or on the
4142 -- subtype itself.
4144 if Is_Access_Type (R_Typ)
4145 and then
4146 (Can_Never_Be_Null (R_Typ)
4147 or else Null_Exclusion_Present (Parent (Func_Id))) /=
4148 Can_Never_Be_Null (Obj_Typ)
4149 then
4150 Error_No_Match (Indic);
4151 end if;
4153 -- AI05-103: for elementary types, subtypes must statically match
4155 if Is_Constrained (R_Typ) or else Is_Access_Type (R_Typ) then
4156 if not Subtypes_Statically_Match (Obj_Typ, R_Typ) then
4157 Error_No_Match (Indic);
4158 end if;
4159 end if;
4161 -- All remaining cases are illegal
4163 -- Note: previous versions of this subprogram allowed the return
4164 -- value to be the ancestor of the return type if the return type
4165 -- was a null extension. This was plainly incorrect.
4167 else
4168 Error_Msg_N
4169 ("wrong type for return_subtype_indication", Indic);
4170 end if;
4171 end Check_Return_Subtype_Indication;
4173 -----------------
4174 -- Count_Tasks --
4175 -----------------
4177 function Count_Tasks (T : Entity_Id) return Uint is
4178 C : Entity_Id;
4179 X : Node_Id;
4180 V : Uint;
4182 begin
4183 if Is_Task_Type (T) then
4184 return Uint_1;
4186 elsif Is_Record_Type (T) then
4187 if Has_Discriminants (T) then
4188 Check_Restriction (Max_Tasks, N);
4189 return Uint_0;
4191 else
4192 V := Uint_0;
4193 C := First_Component (T);
4194 while Present (C) loop
4195 V := V + Count_Tasks (Etype (C));
4196 Next_Component (C);
4197 end loop;
4199 return V;
4200 end if;
4202 elsif Is_Array_Type (T) then
4203 X := First_Index (T);
4204 V := Count_Tasks (Component_Type (T));
4205 while Present (X) loop
4206 C := Etype (X);
4208 if not Is_OK_Static_Subtype (C) then
4209 Check_Restriction (Max_Tasks, N);
4210 return Uint_0;
4211 else
4212 V := V * (UI_Max (Uint_0,
4213 Expr_Value (Type_High_Bound (C)) -
4214 Expr_Value (Type_Low_Bound (C)) + Uint_1));
4215 end if;
4217 Next_Index (X);
4218 end loop;
4220 return V;
4222 else
4223 return Uint_0;
4224 end if;
4225 end Count_Tasks;
4227 ----------------------------
4228 -- Delayed_Aspect_Present --
4229 ----------------------------
4231 function Delayed_Aspect_Present return Boolean is
4232 A : Node_Id;
4233 A_Id : Aspect_Id;
4235 begin
4236 if Present (Aspect_Specifications (N)) then
4237 A := First (Aspect_Specifications (N));
4239 while Present (A) loop
4240 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4242 if A_Id = Aspect_Address then
4244 -- Set flag on object entity, for later processing at
4245 -- the freeze point.
4247 Set_Has_Delayed_Aspects (Id);
4248 return True;
4249 end if;
4251 Next (A);
4252 end loop;
4253 end if;
4255 return False;
4256 end Delayed_Aspect_Present;
4258 -- Local variables
4260 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4261 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4262 -- Save the Ghost-related attributes to restore on exit
4264 Prev_Entity : Entity_Id := Empty;
4265 Related_Id : Entity_Id;
4267 -- Start of processing for Analyze_Object_Declaration
4269 begin
4270 -- There are three kinds of implicit types generated by an
4271 -- object declaration:
4273 -- 1. Those generated by the original Object Definition
4275 -- 2. Those generated by the Expression
4277 -- 3. Those used to constrain the Object Definition with the
4278 -- expression constraints when the definition is unconstrained.
4280 -- They must be generated in this order to avoid order of elaboration
4281 -- issues. Thus the first step (after entering the name) is to analyze
4282 -- the object definition.
4284 if Constant_Present (N) then
4285 Prev_Entity := Current_Entity_In_Scope (Id);
4287 if Present (Prev_Entity)
4288 and then
4289 -- If the homograph is an implicit subprogram, it is overridden
4290 -- by the current declaration.
4292 ((Is_Overloadable (Prev_Entity)
4293 and then Is_Inherited_Operation (Prev_Entity))
4295 -- The current object is a discriminal generated for an entry
4296 -- family index. Even though the index is a constant, in this
4297 -- particular context there is no true constant redeclaration.
4298 -- Enter_Name will handle the visibility.
4300 or else
4301 (Is_Discriminal (Id)
4302 and then Ekind (Discriminal_Link (Id)) =
4303 E_Entry_Index_Parameter)
4305 -- The current object is the renaming for a generic declared
4306 -- within the instance.
4308 or else
4309 (Ekind (Prev_Entity) = E_Package
4310 and then Nkind (Parent (Prev_Entity)) =
4311 N_Package_Renaming_Declaration
4312 and then not Comes_From_Source (Prev_Entity)
4313 and then
4314 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4316 -- The entity may be a homonym of a private component of the
4317 -- enclosing protected object, for which we create a local
4318 -- renaming declaration. The declaration is legal, even if
4319 -- useless when it just captures that component.
4321 or else
4322 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4323 and then Nkind (Parent (Prev_Entity)) =
4324 N_Object_Renaming_Declaration))
4325 then
4326 Prev_Entity := Empty;
4327 end if;
4328 end if;
4330 if Present (Prev_Entity) then
4332 -- The object declaration is Ghost when it completes a deferred Ghost
4333 -- constant.
4335 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4337 Constant_Redeclaration (Id, N, T);
4339 Generate_Reference (Prev_Entity, Id, 'c');
4340 Set_Completion_Referenced (Id);
4342 if Error_Posted (N) then
4344 -- Type mismatch or illegal redeclaration; do not analyze
4345 -- expression to avoid cascaded errors.
4347 T := Find_Type_Of_Object (Object_Definition (N), N);
4348 Set_Etype (Id, T);
4349 Mutate_Ekind (Id, E_Variable);
4350 goto Leave;
4351 end if;
4353 -- In the normal case, enter identifier at the start to catch premature
4354 -- usage in the initialization expression.
4356 else
4357 Generate_Definition (Id);
4358 Enter_Name (Id);
4360 Mark_Coextensions (N, Object_Definition (N));
4362 T := Find_Type_Of_Object (Object_Definition (N), N);
4364 if Nkind (Object_Definition (N)) = N_Access_Definition
4365 and then Present
4366 (Access_To_Subprogram_Definition (Object_Definition (N)))
4367 and then Protected_Present
4368 (Access_To_Subprogram_Definition (Object_Definition (N)))
4369 then
4370 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4371 end if;
4373 if Error_Posted (Id) then
4374 Set_Etype (Id, T);
4375 Mutate_Ekind (Id, E_Variable);
4376 goto Leave;
4377 end if;
4378 end if;
4380 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4381 -- out some static checks.
4383 if Ada_Version >= Ada_2005 then
4385 -- In case of aggregates we must also take care of the correct
4386 -- initialization of nested aggregates bug this is done at the
4387 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4389 if Can_Never_Be_Null (T) then
4390 if Present (Expression (N))
4391 and then Nkind (Expression (N)) = N_Aggregate
4392 then
4393 null;
4395 elsif Comes_From_Source (Id) then
4396 declare
4397 Save_Typ : constant Entity_Id := Etype (Id);
4398 begin
4399 Set_Etype (Id, T); -- Temp. decoration for static checks
4400 Null_Exclusion_Static_Checks (N);
4401 Set_Etype (Id, Save_Typ);
4402 end;
4403 end if;
4405 -- We might be dealing with an object of a composite type containing
4406 -- null-excluding components without an aggregate, so we must verify
4407 -- that such components have default initialization.
4409 else
4410 Check_For_Null_Excluding_Components (T, N);
4411 end if;
4412 end if;
4414 -- Object is marked pure if it is in a pure scope
4416 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4418 -- If deferred constant, make sure context is appropriate. We detect
4419 -- a deferred constant as a constant declaration with no expression.
4420 -- A deferred constant can appear in a package body if its completion
4421 -- is by means of an interface pragma.
4423 if Constant_Present (N) and then No (E) then
4425 -- A deferred constant may appear in the declarative part of the
4426 -- following constructs:
4428 -- blocks
4429 -- entry bodies
4430 -- extended return statements
4431 -- package specs
4432 -- package bodies
4433 -- subprogram bodies
4434 -- task bodies
4436 -- When declared inside a package spec, a deferred constant must be
4437 -- completed by a full constant declaration or pragma Import. In all
4438 -- other cases, the only proper completion is pragma Import. Extended
4439 -- return statements are flagged as invalid contexts because they do
4440 -- not have a declarative part and so cannot accommodate the pragma.
4442 if Ekind (Current_Scope) = E_Return_Statement then
4443 Error_Msg_N
4444 ("invalid context for deferred constant declaration (RM 7.4)",
4446 Error_Msg_N
4447 ("\declaration requires an initialization expression",
4449 Set_Constant_Present (N, False);
4451 -- In Ada 83, deferred constant must be of private type
4453 elsif not Is_Private_Type (T) then
4454 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4455 Error_Msg_N
4456 ("(Ada 83) deferred constant must be private type", N);
4457 end if;
4458 end if;
4460 -- If not a deferred constant, then the object declaration freezes
4461 -- its type, unless the object is of an anonymous type and has delayed
4462 -- aspects. In that case the type is frozen when the object itself is.
4464 else
4465 Check_Fully_Declared (T, N);
4467 if Has_Delayed_Aspects (Id)
4468 and then Is_Array_Type (T)
4469 and then Is_Itype (T)
4470 then
4471 Set_Has_Delayed_Freeze (T);
4472 else
4473 Freeze_Before (N, T);
4474 end if;
4475 end if;
4477 -- If the object was created by a constrained array definition, then
4478 -- set the link in both the anonymous base type and anonymous subtype
4479 -- that are built to represent the array type to point to the object.
4481 if Nkind (Object_Definition (Declaration_Node (Id))) =
4482 N_Constrained_Array_Definition
4483 then
4484 Set_Related_Array_Object (T, Id);
4485 Set_Related_Array_Object (Base_Type (T), Id);
4486 end if;
4488 -- Check for protected objects not at library level
4490 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4491 Check_Restriction (No_Local_Protected_Objects, Id);
4492 end if;
4494 -- Check for violation of No_Local_Timing_Events
4496 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4497 Check_Restriction (No_Local_Timing_Events, Id);
4498 end if;
4500 -- The actual subtype of the object is the nominal subtype, unless
4501 -- the nominal one is unconstrained and obtained from the expression.
4503 Act_T := T;
4505 if Is_Library_Level_Entity (Id) then
4506 Check_Dynamic_Object (T);
4507 end if;
4509 -- Process initialization expression if present and not in error
4511 if Present (E) and then E /= Error then
4513 -- Generate an error in case of CPP class-wide object initialization.
4514 -- Required because otherwise the expansion of the class-wide
4515 -- assignment would try to use 'size to initialize the object
4516 -- (primitive that is not available in CPP tagged types).
4518 if Is_Class_Wide_Type (Act_T)
4519 and then
4520 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4521 or else
4522 (Present (Full_View (Root_Type (Etype (Act_T))))
4523 and then
4524 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4525 then
4526 Error_Msg_N
4527 ("predefined assignment not available for 'C'P'P tagged types",
4529 end if;
4531 Mark_Coextensions (N, E);
4532 Analyze (E);
4534 -- In case of errors detected in the analysis of the expression,
4535 -- decorate it with the expected type to avoid cascaded errors.
4537 if No (Etype (E)) then
4538 Set_Etype (E, T);
4539 end if;
4541 -- If an initialization expression is present, then we set the
4542 -- Is_True_Constant flag. It will be reset if this is a variable
4543 -- and it is indeed modified.
4545 Set_Is_True_Constant (Id, True);
4547 -- If we are analyzing a constant declaration, set its completion
4548 -- flag after analyzing and resolving the expression.
4550 if Constant_Present (N) then
4551 Set_Has_Completion (Id);
4552 end if;
4554 -- Set type and resolve (type may be overridden later on). Note:
4555 -- Ekind (Id) must still be E_Void at this point so that incorrect
4556 -- early usage within E is properly diagnosed.
4558 Set_Etype (Id, T);
4560 -- If the expression is an aggregate we must look ahead to detect
4561 -- the possible presence of an address clause, and defer resolution
4562 -- and expansion of the aggregate to the freeze point of the entity.
4564 -- This is not always legal because the aggregate may contain other
4565 -- references that need freezing, e.g. references to other entities
4566 -- with address clauses. In any case, when compiling with -gnatI the
4567 -- presence of the address clause must be ignored.
4569 if Comes_From_Source (N)
4570 and then Expander_Active
4571 and then Nkind (E) = N_Aggregate
4572 and then
4573 ((Present (Following_Address_Clause (N))
4574 and then not Ignore_Rep_Clauses)
4575 or else Delayed_Aspect_Present)
4576 then
4577 Set_Etype (E, T);
4579 -- If the aggregate is limited it will be built in place, and its
4580 -- expansion is deferred until the object declaration is expanded.
4582 -- This is also required when generating C code to ensure that an
4583 -- object with an alignment or address clause can be initialized
4584 -- by means of component by component assignments.
4586 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4587 Set_Expansion_Delayed (E);
4588 end if;
4590 else
4591 -- If the expression is a formal that is a "subprogram pointer"
4592 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4593 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4594 -- the corresponding check, as is done for assignments.
4596 if Is_Entity_Name (E)
4597 and then Present (Entity (E))
4598 and then Is_Formal (Entity (E))
4599 and then
4600 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4601 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4602 then
4603 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4604 end if;
4606 Resolve (E, T);
4607 end if;
4609 -- No further action needed if E is a call to an inlined function
4610 -- which returns an unconstrained type and it has been expanded into
4611 -- a procedure call. In that case N has been replaced by an object
4612 -- declaration without initializing expression and it has been
4613 -- analyzed (see Expand_Inlined_Call).
4615 if Back_End_Inlining
4616 and then Expander_Active
4617 and then Nkind (E) = N_Function_Call
4618 and then Nkind (Name (E)) in N_Has_Entity
4619 and then Is_Inlined (Entity (Name (E)))
4620 and then not Is_Constrained (Etype (E))
4621 and then Analyzed (N)
4622 and then No (Expression (N))
4623 then
4624 goto Leave;
4625 end if;
4627 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4628 -- node (which was marked already-analyzed), we need to set the type
4629 -- to something else than Universal_Access to keep gigi happy.
4631 if Etype (E) = Universal_Access then
4632 Set_Etype (E, T);
4633 end if;
4635 -- If the object is an access to variable, the initialization
4636 -- expression cannot be an access to constant.
4638 if Is_Access_Type (T)
4639 and then not Is_Access_Constant (T)
4640 and then Is_Access_Type (Etype (E))
4641 and then Is_Access_Constant (Etype (E))
4642 then
4643 Error_Msg_N
4644 ("access to variable cannot be initialized with an "
4645 & "access-to-constant expression", E);
4646 end if;
4648 if not Assignment_OK (N) then
4649 Check_Initialization (T, E);
4650 end if;
4652 Check_Unset_Reference (E);
4654 -- If this is a variable, then set current value. If this is a
4655 -- declared constant of a scalar type with a static expression,
4656 -- indicate that it is always valid.
4658 if not Constant_Present (N) then
4659 if Compile_Time_Known_Value (E) then
4660 Set_Current_Value (Id, E);
4661 end if;
4663 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4664 Set_Is_Known_Valid (Id);
4666 -- If it is a constant initialized with a valid nonstatic entity,
4667 -- the constant is known valid as well, and can inherit the subtype
4668 -- of the entity if it is a subtype of the given type. This info
4669 -- is preserved on the actual subtype of the constant.
4671 elsif Is_Scalar_Type (T)
4672 and then Is_Entity_Name (E)
4673 and then Is_Known_Valid (Entity (E))
4674 and then In_Subrange_Of (Etype (Entity (E)), T)
4675 then
4676 Set_Is_Known_Valid (Id);
4677 Mutate_Ekind (Id, E_Constant);
4678 Set_Actual_Subtype (Id, Etype (Entity (E)));
4679 end if;
4681 -- Deal with setting of null flags
4683 if Is_Access_Type (T) then
4684 if Known_Non_Null (E) then
4685 Set_Is_Known_Non_Null (Id, True);
4686 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4687 Set_Is_Known_Null (Id, True);
4688 end if;
4689 end if;
4691 -- Check incorrect use of dynamically tagged expressions
4693 if Is_Tagged_Type (T) then
4694 Check_Dynamically_Tagged_Expression
4695 (Expr => E,
4696 Typ => T,
4697 Related_Nod => N);
4698 end if;
4700 Apply_Scalar_Range_Check (E, T);
4701 Apply_Static_Length_Check (E, T);
4703 -- A formal parameter of a specific tagged type whose related
4704 -- subprogram is subject to pragma Extensions_Visible with value
4705 -- "False" cannot be implicitly converted to a class-wide type by
4706 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4707 -- not consider internally generated expressions.
4709 if Is_Class_Wide_Type (T)
4710 and then Comes_From_Source (E)
4711 and then Is_EVF_Expression (E)
4712 then
4713 Error_Msg_N
4714 ("formal parameter cannot be implicitly converted to "
4715 & "class-wide type when Extensions_Visible is False", E);
4716 end if;
4717 end if;
4719 -- If the No_Streams restriction is set, check that the type of the
4720 -- object is not, and does not contain, any subtype derived from
4721 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4722 -- Has_Stream just for efficiency reasons. There is no point in
4723 -- spending time on a Has_Stream check if the restriction is not set.
4725 if Restriction_Check_Required (No_Streams) then
4726 if Has_Stream (T) then
4727 Check_Restriction (No_Streams, N);
4728 end if;
4729 end if;
4731 -- Deal with predicate check before we start to do major rewriting. It
4732 -- is OK to initialize and then check the initialized value, since the
4733 -- object goes out of scope if we get a predicate failure. Note that we
4734 -- do this in the analyzer and not the expander because the analyzer
4735 -- does some substantial rewriting in some cases.
4737 -- We need a predicate check if the type has predicates that are not
4738 -- ignored, and if either there is an initializing expression, or for
4739 -- default initialization when we have at least one case of an explicit
4740 -- default initial value (including via a Default_Value or
4741 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4742 -- an internal declaration whose initialization comes later (as for an
4743 -- aggregate expansion) or a deferred constant.
4744 -- If expression is an aggregate it may be expanded into assignments
4745 -- and the declaration itself is marked with No_Initialization, but
4746 -- the predicate still applies.
4748 if not Suppress_Assignment_Checks (N)
4749 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4750 and then
4751 (not No_Initialization (N)
4752 or else (Present (E) and then Nkind (E) = N_Aggregate))
4753 and then
4754 (Present (E)
4755 or else
4756 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4757 and then not (Constant_Present (N) and then No (E))
4758 then
4759 -- If the type has a static predicate and the expression is known at
4760 -- compile time, see if the expression satisfies the predicate.
4761 -- In the case of a static expression, this must be done even if
4762 -- the predicate is not enabled (as per static expression rules).
4764 if Present (E) then
4765 Check_Expression_Against_Static_Predicate (E, T);
4766 end if;
4768 -- Do not perform further predicate-related checks unless
4769 -- predicates are enabled for the subtype.
4771 if not Predicate_Enabled (T) then
4772 null;
4774 -- If the type is a null record and there is no explicit initial
4775 -- expression, no predicate check applies.
4777 elsif No (E) and then Is_Null_Record_Type (T) then
4778 null;
4780 -- If there is an address clause for this object, do not generate a
4781 -- predicate check here. It will be generated later, at the freezng
4782 -- point. It would be wrong to generate references to the object
4783 -- here, before the address has been determined.
4785 elsif Has_Aspect (Id, Aspect_Address)
4786 or else Present (Following_Address_Clause (N))
4787 then
4788 null;
4790 -- Do not generate a predicate check if the initialization expression
4791 -- is a type conversion whose target subtype statically matches the
4792 -- object's subtype because the conversion has been subjected to the
4793 -- same check. This is a small optimization which avoids redundant
4794 -- checks.
4796 elsif Present (E)
4797 and then Nkind (E) in N_Type_Conversion
4798 and then Subtypes_Statically_Match (Etype (Subtype_Mark (E)), T)
4799 then
4800 null;
4802 else
4803 -- The check must be inserted after the expanded aggregate
4804 -- expansion code, if any.
4806 declare
4807 Check : constant Node_Id :=
4808 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4809 begin
4810 if No (Next_Decl) then
4811 Append_To (List_Containing (N), Check);
4812 else
4813 Insert_Before (Next_Decl, Check);
4814 end if;
4815 end;
4816 end if;
4817 end if;
4819 -- Case of unconstrained type
4821 if not Is_Definite_Subtype (T) then
4823 -- Nothing to do in deferred constant case
4825 if Constant_Present (N) and then No (E) then
4826 null;
4828 -- Case of no initialization present
4830 elsif No (E) then
4831 if No_Initialization (N) then
4832 null;
4834 elsif Is_Class_Wide_Type (T) then
4835 Error_Msg_N
4836 ("initialization required in class-wide declaration", N);
4838 else
4839 Error_Msg_N
4840 ("unconstrained subtype not allowed (need initialization)",
4841 Object_Definition (N));
4843 if Is_Record_Type (T) and then Has_Discriminants (T) then
4844 Error_Msg_N
4845 ("\provide initial value or explicit discriminant values",
4846 Object_Definition (N));
4848 Error_Msg_NE
4849 ("\or give default discriminant values for type&",
4850 Object_Definition (N), T);
4852 elsif Is_Array_Type (T) then
4853 Error_Msg_N
4854 ("\provide initial value or explicit array bounds",
4855 Object_Definition (N));
4856 end if;
4857 end if;
4859 -- Case of initialization present but in error. Set initial
4860 -- expression as absent (but do not make above complaints).
4862 elsif E = Error then
4863 Set_Expression (N, Empty);
4864 E := Empty;
4866 -- Case of initialization present
4868 else
4869 -- Unconstrained variables not allowed in Ada 83
4871 if Ada_Version = Ada_83
4872 and then not Constant_Present (N)
4873 and then Comes_From_Source (Object_Definition (N))
4874 then
4875 Error_Msg_N
4876 ("(Ada 83) unconstrained variable not allowed",
4877 Object_Definition (N));
4878 end if;
4880 -- Now we constrain the variable from the initializing expression
4882 -- If the expression is an aggregate, it has been expanded into
4883 -- individual assignments. Retrieve the actual type from the
4884 -- expanded construct.
4886 if Is_Array_Type (T)
4887 and then No_Initialization (N)
4888 and then Nkind (Original_Node (E)) = N_Aggregate
4889 then
4890 Act_T := Etype (E);
4892 -- In case of class-wide interface object declarations we delay
4893 -- the generation of the equivalent record type declarations until
4894 -- its expansion because there are cases in they are not required.
4896 elsif Is_Interface (T) then
4897 null;
4899 -- If the type is an unchecked union, no subtype can be built from
4900 -- the expression. Rewrite declaration as a renaming, which the
4901 -- back-end can handle properly. This is a rather unusual case,
4902 -- because most unchecked_union declarations have default values
4903 -- for discriminants and are thus not indefinite.
4905 elsif Is_Unchecked_Union (T) then
4906 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4907 Mutate_Ekind (Id, E_Constant);
4908 else
4909 Mutate_Ekind (Id, E_Variable);
4910 end if;
4912 -- If the expression is an aggregate it contains the required
4913 -- discriminant values but it has not been resolved yet, so do
4914 -- it now, and treat it as the initial expression of an object
4915 -- declaration, rather than a renaming.
4917 if Nkind (E) = N_Aggregate then
4918 Analyze_And_Resolve (E, T);
4920 else
4921 Rewrite (N,
4922 Make_Object_Renaming_Declaration (Loc,
4923 Defining_Identifier => Id,
4924 Subtype_Mark => New_Occurrence_Of (T, Loc),
4925 Name => E));
4927 Set_Renamed_Object (Id, E);
4928 Freeze_Before (N, T);
4929 Set_Is_Frozen (Id);
4930 goto Leave;
4931 end if;
4933 else
4934 -- Ensure that the generated subtype has a unique external name
4935 -- when the related object is public. This guarantees that the
4936 -- subtype and its bounds will not be affected by switches or
4937 -- pragmas that may offset the internal counter due to extra
4938 -- generated code.
4940 if Is_Public (Id) then
4941 Related_Id := Id;
4942 else
4943 Related_Id := Empty;
4944 end if;
4946 -- If the object has an unconstrained array subtype with fixed
4947 -- lower bound, then sliding to that bound may be needed.
4949 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4950 Expand_Sliding_Conversion (E, T);
4951 end if;
4953 if In_Spec_Expression and then In_Declare_Expr > 0 then
4954 -- It is too early to be doing expansion-ish things,
4955 -- so exit early. But we have to set Ekind (Id) now so
4956 -- that subsequent uses of this entity are not rejected
4957 -- via the same mechanism that (correctly) rejects
4958 -- "X : Integer := X;".
4960 if Constant_Present (N) then
4961 Mutate_Ekind (Id, E_Constant);
4962 Set_Is_True_Constant (Id);
4963 else
4964 Mutate_Ekind (Id, E_Variable);
4965 if Present (E) then
4966 Set_Has_Initial_Value (Id);
4967 end if;
4968 end if;
4970 goto Leave;
4971 end if;
4973 Expand_Subtype_From_Expr
4974 (N => N,
4975 Unc_Type => T,
4976 Subtype_Indic => Object_Definition (N),
4977 Exp => E,
4978 Related_Id => Related_Id);
4980 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4981 end if;
4983 if Act_T /= T then
4984 declare
4985 Full_View_Present : constant Boolean :=
4986 Is_Private_Type (Act_T)
4987 and then Present (Full_View (Act_T));
4988 -- Propagate attributes to full view when needed
4990 begin
4991 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4993 if Full_View_Present then
4994 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4995 end if;
4997 if Aliased_Present (N) then
4998 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
5000 if Full_View_Present then
5001 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
5002 end if;
5003 end if;
5005 Freeze_Before (N, Act_T);
5006 end;
5007 end if;
5009 Freeze_Before (N, T);
5010 end if;
5012 elsif Is_Array_Type (T)
5013 and then No_Initialization (N)
5014 and then (Nkind (Original_Node (E)) = N_Aggregate
5015 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
5016 and then Nkind (Original_Node (Expression
5017 (Original_Node (E)))) = N_Aggregate))
5018 then
5019 if not Is_Entity_Name (Object_Definition (N)) then
5020 Act_T := Etype (E);
5021 Check_Compile_Time_Size (Act_T);
5022 end if;
5024 -- When the given object definition and the aggregate are specified
5025 -- independently, and their lengths might differ do a length check.
5026 -- This cannot happen if the aggregate is of the form (others =>...)
5028 if Nkind (E) = N_Raise_Constraint_Error then
5030 -- Aggregate is statically illegal. Place back in declaration
5032 Set_Expression (N, E);
5033 Set_No_Initialization (N, False);
5035 elsif T = Etype (E) then
5036 null;
5038 elsif Nkind (E) = N_Aggregate
5039 and then Present (Component_Associations (E))
5040 and then Present (Choice_List (First (Component_Associations (E))))
5041 and then
5042 Nkind (First (Choice_List (First (Component_Associations (E))))) =
5043 N_Others_Choice
5044 then
5045 null;
5047 else
5048 Apply_Length_Check (E, T);
5049 end if;
5051 -- When possible, and not a deferred constant, build the default subtype
5053 elsif Build_Default_Subtype_OK (T)
5054 and then (not Constant_Present (N) or else Present (E))
5055 then
5056 if No (E) then
5057 Act_T := Build_Default_Subtype (T, N);
5058 else
5059 -- Ada 2005: A limited object may be initialized by means of an
5060 -- aggregate. If the type has default discriminants it has an
5061 -- unconstrained nominal type, Its actual subtype will be obtained
5062 -- from the aggregate, and not from the default discriminants.
5064 Act_T := Etype (E);
5065 end if;
5067 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
5068 Freeze_Before (N, Act_T);
5070 elsif Nkind (E) = N_Function_Call
5071 and then Constant_Present (N)
5072 and then Has_Unconstrained_Elements (Etype (E))
5073 then
5074 -- The back-end has problems with constants of a discriminated type
5075 -- with defaults, if the initial value is a function call. We
5076 -- generate an intermediate temporary that will receive a reference
5077 -- to the result of the call. The initialization expression then
5078 -- becomes a dereference of that temporary.
5080 Remove_Side_Effects (E);
5082 -- If this is a constant declaration of an unconstrained type and
5083 -- the initialization is an aggregate, we can use the subtype of the
5084 -- aggregate for the declared entity because it is immutable.
5086 elsif not Is_Constrained (T)
5087 and then Has_Discriminants (T)
5088 and then Constant_Present (N)
5089 and then not Has_Unchecked_Union (T)
5090 and then Nkind (E) = N_Aggregate
5091 then
5092 Act_T := Etype (E);
5093 end if;
5095 -- Check No_Wide_Characters restriction
5097 Check_Wide_Character_Restriction (T, Object_Definition (N));
5099 -- Indicate this is not set in source. Certainly true for constants, and
5100 -- true for variables so far (will be reset for a variable if and when
5101 -- we encounter a modification in the source).
5103 Set_Never_Set_In_Source (Id);
5105 -- Now establish the proper kind and type of the object
5107 if Ekind (Id) = E_Void then
5108 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
5109 end if;
5111 if Constant_Present (N) then
5112 Mutate_Ekind (Id, E_Constant);
5113 Set_Is_True_Constant (Id);
5115 else
5116 Mutate_Ekind (Id, E_Variable);
5118 -- A variable is set as shared passive if it appears in a shared
5119 -- passive package, and is at the outer level. This is not done for
5120 -- entities generated during expansion, because those are always
5121 -- manipulated locally.
5123 if Is_Shared_Passive (Current_Scope)
5124 and then Is_Library_Level_Entity (Id)
5125 and then Comes_From_Source (Id)
5126 then
5127 Set_Is_Shared_Passive (Id);
5128 Check_Shared_Var (Id, T, N);
5129 end if;
5131 -- Set Has_Initial_Value if initializing expression present. Note
5132 -- that if there is no initializing expression, we leave the state
5133 -- of this flag unchanged (usually it will be False, but notably in
5134 -- the case of exception choice variables, it will already be true).
5136 if Present (E) then
5137 Set_Has_Initial_Value (Id);
5138 end if;
5139 end if;
5141 -- Set the SPARK mode from the current context (may be overwritten later
5142 -- with explicit pragma).
5144 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
5145 Set_SPARK_Pragma_Inherited (Id);
5147 -- Preserve relevant elaboration-related attributes of the context which
5148 -- are no longer available or very expensive to recompute once analysis,
5149 -- resolution, and expansion are over.
5151 Mark_Elaboration_Attributes
5152 (N_Id => Id,
5153 Checks => True,
5154 Warnings => True);
5156 -- Initialize alignment and size and capture alignment setting
5158 Reinit_Alignment (Id);
5159 Reinit_Esize (Id);
5160 Set_Optimize_Alignment_Flags (Id);
5162 -- Deal with aliased case
5164 if Aliased_Present (N) then
5165 Set_Is_Aliased (Id);
5167 -- AI12-001: All aliased objects are considered to be specified as
5168 -- independently addressable (RM C.6(8.1/4)).
5170 Set_Is_Independent (Id);
5172 -- If the object is aliased and the type is unconstrained with
5173 -- defaulted discriminants and there is no expression, then the
5174 -- object is constrained by the defaults, so it is worthwhile
5175 -- building the corresponding subtype.
5177 -- Ada 2005 (AI-363): If the aliased object is discriminated and
5178 -- unconstrained, then only establish an actual subtype if the
5179 -- nominal subtype is indefinite. In definite cases the object is
5180 -- unconstrained in Ada 2005.
5182 if No (E)
5183 and then Is_Record_Type (T)
5184 and then not Is_Constrained (T)
5185 and then Has_Discriminants (T)
5186 and then (Ada_Version < Ada_2005
5187 or else not Is_Definite_Subtype (T))
5188 then
5189 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
5190 end if;
5191 end if;
5193 -- Now we can set the type of the object
5195 Set_Etype (Id, Act_T);
5197 -- Non-constant object is marked to be treated as volatile if type is
5198 -- volatile and we clear the Current_Value setting that may have been
5199 -- set above. Doing so for constants isn't required and might interfere
5200 -- with possible uses of the object as a static expression in contexts
5201 -- incompatible with volatility (e.g. as a case-statement alternative).
5203 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
5204 Set_Treat_As_Volatile (Id);
5205 Set_Current_Value (Id, Empty);
5206 end if;
5208 -- Deal with controlled types
5210 if Has_Controlled_Component (Etype (Id))
5211 or else Is_Controlled (Etype (Id))
5212 then
5213 if not Is_Library_Level_Entity (Id) then
5214 Check_Restriction (No_Nested_Finalization, N);
5215 else
5216 Validate_Controlled_Object (Id);
5217 end if;
5218 end if;
5220 if Has_Task (Etype (Id)) then
5221 Check_Restriction (No_Tasking, N);
5223 -- Deal with counting max tasks
5225 -- Nothing to do if inside a generic
5227 if Inside_A_Generic then
5228 null;
5230 -- If library level entity, then count tasks
5232 elsif Is_Library_Level_Entity (Id) then
5233 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
5235 -- If not library level entity, then indicate we don't know max
5236 -- tasks and also check task hierarchy restriction and blocking
5237 -- operation (since starting a task is definitely blocking).
5239 else
5240 Check_Restriction (Max_Tasks, N);
5241 Check_Restriction (No_Task_Hierarchy, N);
5242 Check_Potentially_Blocking_Operation (N);
5243 end if;
5245 -- A rather specialized test. If we see two tasks being declared
5246 -- of the same type in the same object declaration, and the task
5247 -- has an entry with an address clause, we know that program error
5248 -- will be raised at run time since we can't have two tasks with
5249 -- entries at the same address.
5251 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
5252 declare
5253 E : Entity_Id;
5255 begin
5256 E := First_Entity (Etype (Id));
5257 while Present (E) loop
5258 if Ekind (E) = E_Entry
5259 and then Present (Get_Attribute_Definition_Clause
5260 (E, Attribute_Address))
5261 then
5262 Error_Msg_Warn := SPARK_Mode /= On;
5263 Error_Msg_N
5264 ("more than one task with same entry address<<", N);
5265 Error_Msg_N ("\Program_Error [<<", N);
5266 Insert_Action (N,
5267 Make_Raise_Program_Error (Loc,
5268 Reason => PE_Duplicated_Entry_Address));
5269 exit;
5270 end if;
5272 Next_Entity (E);
5273 end loop;
5274 end;
5275 end if;
5276 end if;
5278 -- Check specific legality rules for a return object
5280 if Is_Return_Object (Id) then
5281 Check_Return_Subtype_Indication (N);
5282 end if;
5284 -- Some simple constant-propagation: if the expression is a constant
5285 -- string initialized with a literal, share the literal. This avoids
5286 -- a run-time copy.
5288 if Present (E)
5289 and then Is_Entity_Name (E)
5290 and then Ekind (Entity (E)) = E_Constant
5291 and then Base_Type (Etype (E)) = Standard_String
5292 then
5293 declare
5294 Val : constant Node_Id := Constant_Value (Entity (E));
5295 begin
5296 if Present (Val) and then Nkind (Val) = N_String_Literal then
5297 Rewrite (E, New_Copy (Val));
5298 end if;
5299 end;
5300 end if;
5302 if Present (Prev_Entity)
5303 and then Is_Frozen (Prev_Entity)
5304 and then not Error_Posted (Id)
5305 then
5306 Error_Msg_N ("full constant declaration appears too late", N);
5307 end if;
5309 Check_Eliminated (Id);
5311 -- Deal with setting In_Private_Part flag if in private part
5313 if Ekind (Scope (Id)) = E_Package
5314 and then In_Private_Part (Scope (Id))
5315 then
5316 Set_In_Private_Part (Id);
5317 end if;
5319 <<Leave>>
5320 -- Initialize the refined state of a variable here because this is a
5321 -- common destination for legal and illegal object declarations.
5323 if Ekind (Id) = E_Variable then
5324 Set_Encapsulating_State (Id, Empty);
5325 end if;
5327 if Has_Aspects (N) then
5328 Analyze_Aspect_Specifications (N, Id);
5329 end if;
5331 Analyze_Dimension (N);
5333 -- Verify whether the object declaration introduces an illegal hidden
5334 -- state within a package subject to a null abstract state.
5336 if Ekind (Id) = E_Variable then
5337 Check_No_Hidden_State (Id);
5338 end if;
5340 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5341 end Analyze_Object_Declaration;
5343 ---------------------------
5344 -- Analyze_Others_Choice --
5345 ---------------------------
5347 -- Nothing to do for the others choice node itself, the semantic analysis
5348 -- of the others choice will occur as part of the processing of the parent
5350 procedure Analyze_Others_Choice (N : Node_Id) is
5351 pragma Warnings (Off, N);
5352 begin
5353 null;
5354 end Analyze_Others_Choice;
5356 -------------------------------------------
5357 -- Analyze_Private_Extension_Declaration --
5358 -------------------------------------------
5360 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5361 Indic : constant Node_Id := Subtype_Indication (N);
5362 T : constant Entity_Id := Defining_Identifier (N);
5363 Iface : Entity_Id;
5364 Iface_Elmt : Elmt_Id;
5365 Parent_Base : Entity_Id;
5366 Parent_Type : Entity_Id;
5368 begin
5369 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5371 if Is_Non_Empty_List (Interface_List (N)) then
5372 declare
5373 Intf : Node_Id;
5374 T : Entity_Id;
5376 begin
5377 Intf := First (Interface_List (N));
5378 while Present (Intf) loop
5379 T := Find_Type_Of_Subtype_Indic (Intf);
5381 Diagnose_Interface (Intf, T);
5382 Next (Intf);
5383 end loop;
5384 end;
5385 end if;
5387 Generate_Definition (T);
5389 -- For other than Ada 2012, just enter the name in the current scope
5391 if Ada_Version < Ada_2012 then
5392 Enter_Name (T);
5394 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5395 -- case of private type that completes an incomplete type.
5397 else
5398 declare
5399 Prev : Entity_Id;
5401 begin
5402 Prev := Find_Type_Name (N);
5404 pragma Assert (Prev = T
5405 or else (Ekind (Prev) = E_Incomplete_Type
5406 and then Present (Full_View (Prev))
5407 and then Full_View (Prev) = T));
5408 end;
5409 end if;
5411 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5412 Parent_Base := Base_Type (Parent_Type);
5414 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5415 Mutate_Ekind (T, Ekind (Parent_Type));
5416 Set_Etype (T, Any_Type);
5417 goto Leave;
5419 elsif not Is_Tagged_Type (Parent_Type) then
5420 Error_Msg_N
5421 ("parent of type extension must be a tagged type", Indic);
5422 goto Leave;
5424 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5425 Error_Msg_N ("premature derivation of incomplete type", Indic);
5426 goto Leave;
5428 elsif Is_Concurrent_Type (Parent_Type) then
5429 Error_Msg_N
5430 ("parent type of a private extension cannot be a synchronized "
5431 & "tagged type (RM 3.9.1 (3/1))", N);
5433 Set_Etype (T, Any_Type);
5434 Mutate_Ekind (T, E_Limited_Private_Type);
5435 Set_Private_Dependents (T, New_Elmt_List);
5436 Set_Error_Posted (T);
5437 goto Leave;
5438 end if;
5440 Check_Wide_Character_Restriction (Parent_Type, Indic);
5442 -- Perhaps the parent type should be changed to the class-wide type's
5443 -- specific type in this case to prevent cascading errors ???
5445 if Is_Class_Wide_Type (Parent_Type) then
5446 Error_Msg_N
5447 ("parent of type extension must not be a class-wide type", Indic);
5448 goto Leave;
5449 end if;
5451 if (not Is_Package_Or_Generic_Package (Current_Scope)
5452 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5453 or else In_Private_Part (Current_Scope)
5454 then
5455 Error_Msg_N ("invalid context for private extension", N);
5456 end if;
5458 -- Set common attributes
5460 Set_Is_Pure (T, Is_Pure (Current_Scope));
5461 Set_Scope (T, Current_Scope);
5462 Mutate_Ekind (T, E_Record_Type_With_Private);
5463 Reinit_Size_Align (T);
5464 Set_Default_SSO (T);
5465 Set_No_Reordering (T, No_Component_Reordering);
5467 Set_Etype (T, Parent_Base);
5468 Propagate_Concurrent_Flags (T, Parent_Base);
5470 Set_Convention (T, Convention (Parent_Type));
5471 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5472 Set_Is_First_Subtype (T);
5474 -- Set the SPARK mode from the current context
5476 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5477 Set_SPARK_Pragma_Inherited (T);
5479 if Unknown_Discriminants_Present (N) then
5480 Set_Discriminant_Constraint (T, No_Elist);
5481 end if;
5483 Build_Derived_Record_Type (N, Parent_Type, T);
5485 -- A private extension inherits the Default_Initial_Condition pragma
5486 -- coming from any parent type within the derivation chain.
5488 if Has_DIC (Parent_Type) then
5489 Set_Has_Inherited_DIC (T);
5490 end if;
5492 -- A private extension inherits any class-wide invariants coming from a
5493 -- parent type or an interface. Note that the invariant procedure of the
5494 -- parent type should not be inherited because the private extension may
5495 -- define invariants of its own.
5497 if Has_Inherited_Invariants (Parent_Type)
5498 or else Has_Inheritable_Invariants (Parent_Type)
5499 then
5500 Set_Has_Inherited_Invariants (T);
5502 elsif Present (Interfaces (T)) then
5503 Iface_Elmt := First_Elmt (Interfaces (T));
5504 while Present (Iface_Elmt) loop
5505 Iface := Node (Iface_Elmt);
5507 if Has_Inheritable_Invariants (Iface) then
5508 Set_Has_Inherited_Invariants (T);
5509 exit;
5510 end if;
5512 Next_Elmt (Iface_Elmt);
5513 end loop;
5514 end if;
5516 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5517 -- synchronized formal derived type.
5519 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5520 Set_Is_Limited_Record (T);
5522 -- Formal derived type case
5524 if Is_Generic_Type (T) then
5526 -- The parent must be a tagged limited type or a synchronized
5527 -- interface.
5529 if (not Is_Tagged_Type (Parent_Type)
5530 or else not Is_Limited_Type (Parent_Type))
5531 and then
5532 (not Is_Interface (Parent_Type)
5533 or else not Is_Synchronized_Interface (Parent_Type))
5534 then
5535 Error_Msg_NE
5536 ("parent type of & must be tagged limited or synchronized",
5537 N, T);
5538 end if;
5540 -- The progenitors (if any) must be limited or synchronized
5541 -- interfaces.
5543 if Present (Interfaces (T)) then
5544 Iface_Elmt := First_Elmt (Interfaces (T));
5545 while Present (Iface_Elmt) loop
5546 Iface := Node (Iface_Elmt);
5548 if not Is_Limited_Interface (Iface)
5549 and then not Is_Synchronized_Interface (Iface)
5550 then
5551 Error_Msg_NE
5552 ("progenitor & must be limited or synchronized",
5553 N, Iface);
5554 end if;
5556 Next_Elmt (Iface_Elmt);
5557 end loop;
5558 end if;
5560 -- Regular derived extension, the parent must be a limited or
5561 -- synchronized interface.
5563 else
5564 if not Is_Interface (Parent_Type)
5565 or else (not Is_Limited_Interface (Parent_Type)
5566 and then not Is_Synchronized_Interface (Parent_Type))
5567 then
5568 Error_Msg_NE
5569 ("parent type of & must be limited interface", N, T);
5570 end if;
5571 end if;
5573 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5574 -- extension with a synchronized parent must be explicitly declared
5575 -- synchronized, because the full view will be a synchronized type.
5576 -- This must be checked before the check for limited types below,
5577 -- to ensure that types declared limited are not allowed to extend
5578 -- synchronized interfaces.
5580 elsif Is_Interface (Parent_Type)
5581 and then Is_Synchronized_Interface (Parent_Type)
5582 and then not Synchronized_Present (N)
5583 then
5584 Error_Msg_NE
5585 ("private extension of& must be explicitly synchronized",
5586 N, Parent_Type);
5588 elsif Limited_Present (N) then
5589 Set_Is_Limited_Record (T);
5591 if not Is_Limited_Type (Parent_Type)
5592 and then
5593 (not Is_Interface (Parent_Type)
5594 or else not Is_Limited_Interface (Parent_Type))
5595 then
5596 Error_Msg_NE ("parent type& of limited extension must be limited",
5597 N, Parent_Type);
5598 end if;
5599 end if;
5601 -- Remember that its parent type has a private extension. Used to warn
5602 -- on public primitives of the parent type defined after its private
5603 -- extensions (see Check_Dispatching_Operation).
5605 Set_Has_Private_Extension (Parent_Type);
5607 <<Leave>>
5608 if Has_Aspects (N) then
5609 Analyze_Aspect_Specifications (N, T);
5610 end if;
5611 end Analyze_Private_Extension_Declaration;
5613 ---------------------------------
5614 -- Analyze_Subtype_Declaration --
5615 ---------------------------------
5617 procedure Analyze_Subtype_Declaration
5618 (N : Node_Id;
5619 Skip : Boolean := False)
5621 Id : constant Entity_Id := Defining_Identifier (N);
5622 T : Entity_Id;
5624 begin
5625 Generate_Definition (Id);
5626 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5627 Reinit_Size_Align (Id);
5629 -- The following guard condition on Enter_Name is to handle cases where
5630 -- the defining identifier has already been entered into the scope but
5631 -- the declaration as a whole needs to be analyzed.
5633 -- This case in particular happens for derived enumeration types. The
5634 -- derived enumeration type is processed as an inserted enumeration type
5635 -- declaration followed by a rewritten subtype declaration. The defining
5636 -- identifier, however, is entered into the name scope very early in the
5637 -- processing of the original type declaration and therefore needs to be
5638 -- avoided here, when the created subtype declaration is analyzed. (See
5639 -- Build_Derived_Types)
5641 -- This also happens when the full view of a private type is a derived
5642 -- type with constraints. In this case the entity has been introduced
5643 -- in the private declaration.
5645 -- Finally this happens in some complex cases when validity checks are
5646 -- enabled, where the same subtype declaration may be analyzed twice.
5647 -- This can happen if the subtype is created by the preanalysis of
5648 -- an attribute that gives the range of a loop statement, and the loop
5649 -- itself appears within an if_statement that will be rewritten during
5650 -- expansion.
5652 if Skip
5653 or else (Present (Etype (Id))
5654 and then (Is_Private_Type (Etype (Id))
5655 or else Is_Task_Type (Etype (Id))
5656 or else Is_Rewrite_Substitution (N)))
5657 then
5658 null;
5660 elsif Current_Entity (Id) = Id then
5661 null;
5663 else
5664 Enter_Name (Id);
5665 end if;
5667 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5669 -- Class-wide equivalent types of records with unknown discriminants
5670 -- involve the generation of an itype which serves as the private view
5671 -- of a constrained record subtype. In such cases the base type of the
5672 -- current subtype we are processing is the private itype. Use the full
5673 -- of the private itype when decorating various attributes.
5675 if Is_Itype (T)
5676 and then Is_Private_Type (T)
5677 and then Present (Full_View (T))
5678 then
5679 T := Full_View (T);
5680 end if;
5682 -- Inherit common attributes
5684 Set_Is_Volatile (Id, Is_Volatile (T));
5685 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5686 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5687 Set_Convention (Id, Convention (T));
5689 -- If ancestor has predicates then so does the subtype, and in addition
5690 -- we must delay the freeze to properly arrange predicate inheritance.
5692 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5693 -- in which T = ID, so the above tests and assignments do nothing???
5695 if Has_Predicates (T)
5696 or else (Present (Ancestor_Subtype (T))
5697 and then Has_Predicates (Ancestor_Subtype (T)))
5698 then
5699 Set_Has_Predicates (Id);
5700 Set_Has_Delayed_Freeze (Id);
5702 -- Generated subtypes inherit the predicate function from the parent
5703 -- (no aspects to examine on the generated declaration).
5705 if not Comes_From_Source (N) then
5706 Mutate_Ekind (Id, Ekind (T));
5708 if Present (Predicate_Function (Id)) then
5709 null;
5711 elsif Present (Predicate_Function (T)) then
5712 Set_Predicate_Function (Id, Predicate_Function (T));
5714 elsif Present (Ancestor_Subtype (T))
5715 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5716 then
5717 Set_Predicate_Function (Id,
5718 Predicate_Function (Ancestor_Subtype (T)));
5719 end if;
5720 end if;
5721 end if;
5723 -- In the case where there is no constraint given in the subtype
5724 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5725 -- semantic attributes must be established here.
5727 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5728 Set_Etype (Id, Base_Type (T));
5730 case Ekind (T) is
5731 when Array_Kind =>
5732 Mutate_Ekind (Id, E_Array_Subtype);
5733 Copy_Array_Subtype_Attributes (Id, T);
5734 Set_Packed_Array_Impl_Type (Id, Packed_Array_Impl_Type (T));
5736 when Decimal_Fixed_Point_Kind =>
5737 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5738 Set_Digits_Value (Id, Digits_Value (T));
5739 Set_Delta_Value (Id, Delta_Value (T));
5740 Set_Scale_Value (Id, Scale_Value (T));
5741 Set_Small_Value (Id, Small_Value (T));
5742 Set_Scalar_Range (Id, Scalar_Range (T));
5743 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5744 Set_Is_Constrained (Id, Is_Constrained (T));
5745 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5746 Copy_RM_Size (To => Id, From => T);
5748 when Enumeration_Kind =>
5749 Mutate_Ekind (Id, E_Enumeration_Subtype);
5750 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5751 Set_Scalar_Range (Id, Scalar_Range (T));
5752 Set_Is_Character_Type (Id, Is_Character_Type (T));
5753 Set_Is_Constrained (Id, Is_Constrained (T));
5754 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5755 Copy_RM_Size (To => Id, From => T);
5757 when Ordinary_Fixed_Point_Kind =>
5758 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5759 Set_Scalar_Range (Id, Scalar_Range (T));
5760 Set_Small_Value (Id, Small_Value (T));
5761 Set_Delta_Value (Id, Delta_Value (T));
5762 Set_Is_Constrained (Id, Is_Constrained (T));
5763 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5764 Copy_RM_Size (To => Id, From => T);
5766 when Float_Kind =>
5767 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5768 Set_Scalar_Range (Id, Scalar_Range (T));
5769 Set_Digits_Value (Id, Digits_Value (T));
5770 Set_Is_Constrained (Id, Is_Constrained (T));
5772 -- If the floating point type has dimensions, these will be
5773 -- inherited subsequently when Analyze_Dimensions is called.
5775 when Signed_Integer_Kind =>
5776 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5777 Set_Scalar_Range (Id, Scalar_Range (T));
5778 Set_Is_Constrained (Id, Is_Constrained (T));
5779 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5780 Copy_RM_Size (To => Id, From => T);
5782 when Modular_Integer_Kind =>
5783 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5784 Set_Scalar_Range (Id, Scalar_Range (T));
5785 Set_Is_Constrained (Id, Is_Constrained (T));
5786 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5787 Copy_RM_Size (To => Id, From => T);
5789 when Class_Wide_Kind =>
5790 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5791 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5792 Set_Cloned_Subtype (Id, T);
5793 Set_Is_Tagged_Type (Id, True);
5794 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5795 Set_Has_Unknown_Discriminants
5796 (Id, True);
5797 Set_No_Tagged_Streams_Pragma
5798 (Id, No_Tagged_Streams_Pragma (T));
5800 if Ekind (T) = E_Class_Wide_Subtype then
5801 Set_Equivalent_Type (Id, Equivalent_Type (T));
5802 end if;
5804 when E_Record_Subtype
5805 | E_Record_Type
5807 Mutate_Ekind (Id, E_Record_Subtype);
5809 -- Subtype declarations introduced for formal type parameters
5810 -- in generic instantiations should inherit the Size value of
5811 -- the type they rename.
5813 if Present (Generic_Parent_Type (N)) then
5814 Copy_RM_Size (To => Id, From => T);
5815 end if;
5817 if Ekind (T) = E_Record_Subtype
5818 and then Present (Cloned_Subtype (T))
5819 then
5820 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5821 else
5822 Set_Cloned_Subtype (Id, T);
5823 end if;
5825 Set_First_Entity (Id, First_Entity (T));
5826 Set_Last_Entity (Id, Last_Entity (T));
5827 Set_Has_Discriminants (Id, Has_Discriminants (T));
5828 Set_Is_Constrained (Id, Is_Constrained (T));
5829 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5830 Set_Has_Implicit_Dereference
5831 (Id, Has_Implicit_Dereference (T));
5832 Set_Has_Unknown_Discriminants
5833 (Id, Has_Unknown_Discriminants (T));
5835 if Has_Discriminants (T) then
5836 Set_Discriminant_Constraint
5837 (Id, Discriminant_Constraint (T));
5838 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5840 elsif Has_Unknown_Discriminants (Id) then
5841 Set_Discriminant_Constraint (Id, No_Elist);
5842 end if;
5844 if Is_Tagged_Type (T) then
5845 Set_Is_Tagged_Type (Id, True);
5846 Set_No_Tagged_Streams_Pragma
5847 (Id, No_Tagged_Streams_Pragma (T));
5848 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5849 Set_Direct_Primitive_Operations
5850 (Id, Direct_Primitive_Operations (T));
5851 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5853 if Is_Interface (T) then
5854 Set_Is_Interface (Id);
5855 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5856 end if;
5857 end if;
5859 when Private_Kind =>
5860 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5861 Set_Has_Discriminants (Id, Has_Discriminants (T));
5862 Set_Is_Constrained (Id, Is_Constrained (T));
5863 Set_First_Entity (Id, First_Entity (T));
5864 Set_Last_Entity (Id, Last_Entity (T));
5865 Set_Private_Dependents (Id, New_Elmt_List);
5866 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5867 Set_Has_Implicit_Dereference
5868 (Id, Has_Implicit_Dereference (T));
5869 Set_Has_Unknown_Discriminants
5870 (Id, Has_Unknown_Discriminants (T));
5871 Set_Known_To_Have_Preelab_Init
5872 (Id, Known_To_Have_Preelab_Init (T));
5874 if Is_Tagged_Type (T) then
5875 Set_Is_Tagged_Type (Id);
5876 Set_No_Tagged_Streams_Pragma (Id,
5877 No_Tagged_Streams_Pragma (T));
5878 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5879 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5880 Set_Direct_Primitive_Operations (Id,
5881 Direct_Primitive_Operations (T));
5882 end if;
5884 -- In general the attributes of the subtype of a private type
5885 -- are the attributes of the partial view of parent. However,
5886 -- the full view may be a discriminated type, and the subtype
5887 -- must share the discriminant constraint to generate correct
5888 -- calls to initialization procedures.
5890 if Has_Discriminants (T) then
5891 Set_Discriminant_Constraint
5892 (Id, Discriminant_Constraint (T));
5893 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5895 elsif Present (Full_View (T))
5896 and then Has_Discriminants (Full_View (T))
5897 then
5898 Set_Discriminant_Constraint
5899 (Id, Discriminant_Constraint (Full_View (T)));
5900 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5902 -- This would seem semantically correct, but apparently
5903 -- generates spurious errors about missing components ???
5905 -- Set_Has_Discriminants (Id);
5906 end if;
5908 Prepare_Private_Subtype_Completion (Id, N);
5910 -- If this is the subtype of a constrained private type with
5911 -- discriminants that has got a full view and we also have
5912 -- built a completion just above, show that the completion
5913 -- is a clone of the full view to the back-end.
5915 if Has_Discriminants (T)
5916 and then not Has_Unknown_Discriminants (T)
5917 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5918 and then Present (Full_View (T))
5919 and then Present (Full_View (Id))
5920 then
5921 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5922 end if;
5924 when Access_Kind =>
5925 Mutate_Ekind (Id, E_Access_Subtype);
5926 Set_Is_Constrained (Id, Is_Constrained (T));
5927 Set_Is_Access_Constant
5928 (Id, Is_Access_Constant (T));
5929 Set_Directly_Designated_Type
5930 (Id, Designated_Type (T));
5931 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5933 -- A Pure library_item must not contain the declaration of a
5934 -- named access type, except within a subprogram, generic
5935 -- subprogram, task unit, or protected unit, or if it has
5936 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5938 if Comes_From_Source (Id)
5939 and then In_Pure_Unit
5940 and then not In_Subprogram_Task_Protected_Unit
5941 and then not No_Pool_Assigned (Id)
5942 then
5943 Error_Msg_N
5944 ("named access types not allowed in pure unit", N);
5945 end if;
5947 when Concurrent_Kind =>
5948 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5949 Set_Corresponding_Record_Type (Id,
5950 Corresponding_Record_Type (T));
5951 Set_First_Entity (Id, First_Entity (T));
5952 Set_First_Private_Entity (Id, First_Private_Entity (T));
5953 Set_Has_Discriminants (Id, Has_Discriminants (T));
5954 Set_Is_Constrained (Id, Is_Constrained (T));
5955 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5956 Set_Last_Entity (Id, Last_Entity (T));
5958 if Is_Tagged_Type (T) then
5959 Set_No_Tagged_Streams_Pragma
5960 (Id, No_Tagged_Streams_Pragma (T));
5961 end if;
5963 if Has_Discriminants (T) then
5964 Set_Discriminant_Constraint
5965 (Id, Discriminant_Constraint (T));
5966 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5967 end if;
5969 when Incomplete_Kind =>
5970 if Ada_Version >= Ada_2005 then
5972 -- In Ada 2005 an incomplete type can be explicitly tagged:
5973 -- propagate indication. Note that we also have to include
5974 -- subtypes for Ada 2012 extended use of incomplete types.
5976 Mutate_Ekind (Id, E_Incomplete_Subtype);
5977 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5978 Set_Private_Dependents (Id, New_Elmt_List);
5980 if Is_Tagged_Type (Id) then
5981 Set_No_Tagged_Streams_Pragma
5982 (Id, No_Tagged_Streams_Pragma (T));
5983 end if;
5985 -- For tagged types, or when prefixed-call syntax is allowed
5986 -- for untagged types, initialize the list of primitive
5987 -- operations to an empty list.
5989 if Is_Tagged_Type (Id)
5990 or else Core_Extensions_Allowed
5991 then
5992 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5993 end if;
5995 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5996 -- incomplete type visible through a limited with clause.
5998 if From_Limited_With (T)
5999 and then Present (Non_Limited_View (T))
6000 then
6001 Set_From_Limited_With (Id);
6002 Set_Non_Limited_View (Id, Non_Limited_View (T));
6004 -- Ada 2005 (AI-412): Add the regular incomplete subtype
6005 -- to the private dependents of the original incomplete
6006 -- type for future transformation.
6008 else
6009 Append_Elmt (Id, Private_Dependents (T));
6010 end if;
6012 -- If the subtype name denotes an incomplete type an error
6013 -- was already reported by Process_Subtype.
6015 else
6016 Set_Etype (Id, Any_Type);
6017 end if;
6019 when others =>
6020 raise Program_Error;
6021 end case;
6023 -- If there is no constraint in the subtype indication, the
6024 -- declared entity inherits predicates from the parent.
6026 Inherit_Predicate_Flags (Id, T);
6027 end if;
6029 if Etype (Id) = Any_Type then
6030 goto Leave;
6031 end if;
6033 -- When prefixed calls are enabled for untagged types, the subtype
6034 -- shares the primitive operations of its base type. Do this even
6035 -- when Extensions_Allowed is False to issue better error messages.
6037 Set_Direct_Primitive_Operations
6038 (Id, Direct_Primitive_Operations (Base_Type (T)));
6040 -- Some common processing on all types
6042 Set_Size_Info (Id, T);
6043 Set_First_Rep_Item (Id, First_Rep_Item (T));
6045 -- If the parent type is a generic actual, so is the subtype. This may
6046 -- happen in a nested instance. Why Comes_From_Source test???
6048 if not Comes_From_Source (N) then
6049 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
6050 end if;
6052 -- If this is a subtype declaration for an actual in an instance,
6053 -- inherit static and dynamic predicates if any.
6055 -- If declaration has no aspect specifications, inherit predicate
6056 -- info as well. Unclear how to handle the case of both specified
6057 -- and inherited predicates ??? Other inherited aspects, such as
6058 -- invariants, should be OK, but the combination with later pragmas
6059 -- may also require special merging.
6061 if Has_Predicates (T)
6062 and then Present (Predicate_Function (T))
6063 and then
6064 ((In_Instance and then not Comes_From_Source (N))
6065 or else No (Aspect_Specifications (N)))
6066 then
6067 -- Inherit Subprograms_For_Type from the full view, if present
6069 if Present (Full_View (T))
6070 and then Present (Subprograms_For_Type (Full_View (T)))
6071 then
6072 Set_Subprograms_For_Type
6073 (Id, Subprograms_For_Type (Full_View (T)));
6074 else
6075 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
6076 end if;
6078 -- If the current declaration created both a private and a full view,
6079 -- then propagate Predicate_Function to the latter as well.
6081 if Present (Full_View (Id))
6082 and then No (Predicate_Function (Full_View (Id)))
6083 then
6084 Set_Subprograms_For_Type
6085 (Full_View (Id), Subprograms_For_Type (Id));
6086 end if;
6088 if Has_Static_Predicate (T) then
6089 Set_Has_Static_Predicate (Id);
6090 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
6091 end if;
6092 end if;
6094 -- If the base type is a scalar type, or else if there is no
6095 -- constraint, the atomic flag is inherited by the subtype.
6096 -- Ditto for the Independent aspect.
6098 if Is_Scalar_Type (Id)
6099 or else Is_Entity_Name (Subtype_Indication (N))
6100 then
6101 Set_Is_Atomic (Id, Is_Atomic (T));
6102 Set_Is_Independent (Id, Is_Independent (T));
6103 end if;
6105 -- Remaining processing depends on characteristics of base type
6107 T := Etype (Id);
6109 Set_Is_Immediately_Visible (Id, True);
6110 Set_Depends_On_Private (Id, Has_Private_Component (T));
6111 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
6113 if Is_Interface (T) then
6114 Set_Is_Interface (Id);
6115 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
6116 end if;
6118 if Present (Generic_Parent_Type (N))
6119 and then
6120 (Nkind (Parent (Generic_Parent_Type (N))) /=
6121 N_Formal_Type_Declaration
6122 or else Nkind (Formal_Type_Definition
6123 (Parent (Generic_Parent_Type (N)))) /=
6124 N_Formal_Private_Type_Definition)
6125 then
6126 if Is_Tagged_Type (Id) then
6128 -- If this is a generic actual subtype for a synchronized type,
6129 -- the primitive operations are those of the corresponding record
6130 -- for which there is a separate subtype declaration.
6132 if Is_Concurrent_Type (Id) then
6133 null;
6134 elsif Is_Class_Wide_Type (Id) then
6135 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
6136 else
6137 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
6138 end if;
6140 elsif Scope (Etype (Id)) /= Standard_Standard then
6141 Derive_Subprograms (Generic_Parent_Type (N), Id);
6142 end if;
6143 end if;
6145 if Is_Private_Type (T) and then Present (Full_View (T)) then
6146 Conditional_Delay (Id, Full_View (T));
6148 -- The subtypes of components or subcomponents of protected types
6149 -- do not need freeze nodes, which would otherwise appear in the
6150 -- wrong scope (before the freeze node for the protected type). The
6151 -- proper subtypes are those of the subcomponents of the corresponding
6152 -- record.
6154 elsif Ekind (Scope (Id)) /= E_Protected_Type
6155 and then Present (Scope (Scope (Id))) -- error defense
6156 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
6157 then
6158 Conditional_Delay (Id, T);
6159 end if;
6161 -- If we have a subtype of an incomplete type whose full type is a
6162 -- derived numeric type, we need to have a freeze node for the subtype.
6163 -- Otherwise gigi will complain while computing the (static) bounds of
6164 -- the subtype.
6166 if Is_Itype (T)
6167 and then Is_Elementary_Type (Id)
6168 and then Etype (Id) /= Id
6169 then
6170 declare
6171 Partial : constant Entity_Id :=
6172 Incomplete_Or_Partial_View (First_Subtype (Id));
6173 begin
6174 if Present (Partial)
6175 and then Ekind (Partial) = E_Incomplete_Type
6176 then
6177 Set_Has_Delayed_Freeze (Id);
6178 end if;
6179 end;
6180 end if;
6182 -- Check that Constraint_Error is raised for a scalar subtype indication
6183 -- when the lower or upper bound of a non-null range lies outside the
6184 -- range of the type mark. Likewise for an array subtype, but check the
6185 -- compatibility for each index.
6187 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6188 declare
6189 Indic_Typ : constant Entity_Id :=
6190 Underlying_Type (Etype (Subtype_Mark (Subtype_Indication (N))));
6191 Subt_Index : Node_Id;
6192 Target_Index : Node_Id;
6194 begin
6195 if Is_Scalar_Type (Etype (Id))
6196 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
6197 then
6198 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
6200 elsif Is_Array_Type (Etype (Id))
6201 and then Present (First_Index (Id))
6202 then
6203 Subt_Index := First_Index (Id);
6204 Target_Index := First_Index (Indic_Typ);
6206 while Present (Subt_Index) loop
6207 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
6208 and then Is_Scalar_Type (Entity (Subt_Index)))
6209 or else Nkind (Subt_Index) = N_Subtype_Indication)
6210 and then
6211 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
6212 then
6213 Apply_Range_Check
6214 (Scalar_Range (Etype (Subt_Index)),
6215 Etype (Target_Index),
6216 Insert_Node => N);
6217 end if;
6219 Next_Index (Subt_Index);
6220 Next_Index (Target_Index);
6221 end loop;
6222 end if;
6223 end;
6224 end if;
6226 Set_Optimize_Alignment_Flags (Id);
6227 Check_Eliminated (Id);
6229 <<Leave>>
6230 if Has_Aspects (N) then
6231 Analyze_Aspect_Specifications (N, Id);
6232 end if;
6234 Analyze_Dimension (N);
6236 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
6237 -- indications on composite types where the constraints are dynamic.
6238 -- Note that object declarations and aggregates generate implicit
6239 -- subtype declarations, which this covers. One special case is that the
6240 -- implicitly generated "=" for discriminated types includes an
6241 -- offending subtype declaration, which is harmless, so we ignore it
6242 -- here.
6244 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6245 declare
6246 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6247 begin
6248 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6249 and then not (Is_Internal (Id)
6250 and then Is_TSS (Scope (Id),
6251 TSS_Composite_Equality))
6252 and then not Within_Init_Proc
6253 and then not All_Composite_Constraints_Static (Cstr)
6254 then
6255 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6256 end if;
6257 end;
6258 end if;
6259 end Analyze_Subtype_Declaration;
6261 --------------------------------
6262 -- Analyze_Subtype_Indication --
6263 --------------------------------
6265 procedure Analyze_Subtype_Indication (N : Node_Id) is
6266 T : constant Entity_Id := Subtype_Mark (N);
6267 R : constant Node_Id := Range_Expression (Constraint (N));
6269 begin
6270 Analyze (T);
6272 if R = Error then
6273 Set_Error_Posted (R);
6274 Set_Error_Posted (T);
6275 else
6276 Analyze (R);
6277 Set_Etype (N, Etype (R));
6278 Resolve (R, Entity (T));
6279 end if;
6280 end Analyze_Subtype_Indication;
6282 --------------------------
6283 -- Analyze_Variant_Part --
6284 --------------------------
6286 procedure Analyze_Variant_Part (N : Node_Id) is
6287 Discr_Name : Node_Id;
6288 Discr_Type : Entity_Id;
6290 procedure Process_Variant (A : Node_Id);
6291 -- Analyze declarations for a single variant
6293 package Analyze_Variant_Choices is
6294 new Generic_Analyze_Choices (Process_Variant);
6295 use Analyze_Variant_Choices;
6297 ---------------------
6298 -- Process_Variant --
6299 ---------------------
6301 procedure Process_Variant (A : Node_Id) is
6302 CL : constant Node_Id := Component_List (A);
6303 begin
6304 if not Null_Present (CL) then
6305 Analyze_Declarations (Component_Items (CL));
6307 if Present (Variant_Part (CL)) then
6308 Analyze (Variant_Part (CL));
6309 end if;
6310 end if;
6311 end Process_Variant;
6313 -- Start of processing for Analyze_Variant_Part
6315 begin
6316 Discr_Name := Name (N);
6317 Analyze (Discr_Name);
6319 -- If Discr_Name bad, get out (prevent cascaded errors)
6321 if Etype (Discr_Name) = Any_Type then
6322 return;
6323 end if;
6325 -- Check invalid discriminant in variant part
6327 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6328 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6329 end if;
6331 Discr_Type := Etype (Entity (Discr_Name));
6333 if not Is_Discrete_Type (Discr_Type) then
6334 Error_Msg_N
6335 ("discriminant in a variant part must be of a discrete type",
6336 Name (N));
6337 return;
6338 end if;
6340 -- Now analyze the choices, which also analyzes the declarations that
6341 -- are associated with each choice.
6343 Analyze_Choices (Variants (N), Discr_Type);
6345 -- Note: we used to instantiate and call Check_Choices here to check
6346 -- that the choices covered the discriminant, but it's too early to do
6347 -- that because of statically predicated subtypes, whose analysis may
6348 -- be deferred to their freeze point which may be as late as the freeze
6349 -- point of the containing record. So this call is now to be found in
6350 -- Freeze_Record_Declaration.
6352 end Analyze_Variant_Part;
6354 ----------------------------
6355 -- Array_Type_Declaration --
6356 ----------------------------
6358 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6359 Component_Def : constant Node_Id := Component_Definition (Def);
6360 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6361 P : constant Node_Id := Parent (Def);
6362 Element_Type : Entity_Id;
6363 Implicit_Base : Entity_Id;
6364 Index : Node_Id;
6365 Nb_Index : Pos;
6366 Priv : Entity_Id;
6367 Related_Id : Entity_Id;
6368 Has_FLB_Index : Boolean := False;
6370 begin
6371 if Nkind (Def) = N_Constrained_Array_Definition then
6372 Index := First (Discrete_Subtype_Definitions (Def));
6373 else
6374 Index := First (Subtype_Marks (Def));
6375 end if;
6377 -- Find proper names for the implicit types which may be public. In case
6378 -- of anonymous arrays we use the name of the first object of that type
6379 -- as prefix.
6381 if No (T) then
6382 Related_Id := Defining_Identifier (P);
6383 else
6384 Related_Id := T;
6385 end if;
6387 Nb_Index := 1;
6388 while Present (Index) loop
6389 Analyze (Index);
6391 -- Test for odd case of trying to index a type by the type itself
6393 if Is_Entity_Name (Index) and then Entity (Index) = T then
6394 Error_Msg_N ("type& cannot be indexed by itself", Index);
6395 Set_Entity (Index, Standard_Boolean);
6396 Set_Etype (Index, Standard_Boolean);
6397 end if;
6399 -- Add a subtype declaration for each index of private array type
6400 -- declaration whose type is also private. For example:
6402 -- package Pkg is
6403 -- type Index is private;
6404 -- private
6405 -- type Table is array (Index) of ...
6406 -- end;
6408 -- This is currently required by the expander for the internally
6409 -- generated equality subprogram of records with variant parts in
6410 -- which the type of some component is such a private type. And it
6411 -- also helps semantic analysis in peculiar cases where the array
6412 -- type is referenced from an instance but not the index directly.
6414 if Is_Package_Or_Generic_Package (Current_Scope)
6415 and then In_Private_Part (Current_Scope)
6416 and then Has_Private_Declaration (Etype (Index))
6417 and then Scope (Etype (Index)) = Current_Scope
6418 then
6419 declare
6420 Loc : constant Source_Ptr := Sloc (Def);
6421 Decl : Node_Id;
6422 New_E : Entity_Id;
6424 begin
6425 New_E := Make_Temporary (Loc, 'T');
6426 Set_Is_Internal (New_E);
6428 Decl :=
6429 Make_Subtype_Declaration (Loc,
6430 Defining_Identifier => New_E,
6431 Subtype_Indication =>
6432 New_Occurrence_Of (Etype (Index), Loc));
6434 Insert_Before (Parent (Def), Decl);
6435 Analyze (Decl);
6436 Set_Etype (Index, New_E);
6438 -- If the index is a range or a subtype indication it carries
6439 -- no entity. Example:
6441 -- package Pkg is
6442 -- type T is private;
6443 -- private
6444 -- type T is new Natural;
6445 -- Table : array (T(1) .. T(10)) of Boolean;
6446 -- end Pkg;
6448 -- Otherwise the type of the reference is its entity.
6450 if Is_Entity_Name (Index) then
6451 Set_Entity (Index, New_E);
6452 end if;
6453 end;
6454 end if;
6456 Make_Index (Index, P, Related_Id, Nb_Index);
6458 -- In the case where we have an unconstrained array with an index
6459 -- given by a subtype_indication, this is necessarily a "fixed lower
6460 -- bound" index. We change the upper bound of that index to the upper
6461 -- bound of the index's subtype (denoted by the subtype_mark), since
6462 -- that upper bound was originally set by the parser to be the same
6463 -- as the lower bound. In truth, that upper bound corresponds to
6464 -- a box ("<>"), and could be set to Empty, but it's convenient to
6465 -- set it to the upper bound to avoid needing to add special tests
6466 -- in various places for an Empty upper bound, and in any case that
6467 -- accurately characterizes the index's range of values.
6469 if Nkind (Def) = N_Unconstrained_Array_Definition
6470 and then Nkind (Index) = N_Subtype_Indication
6471 then
6472 declare
6473 Index_Subtype_High_Bound : constant Entity_Id :=
6474 Type_High_Bound (Entity (Subtype_Mark (Index)));
6475 begin
6476 Set_High_Bound (Range_Expression (Constraint (Index)),
6477 Index_Subtype_High_Bound);
6479 -- Record that the array type has one or more indexes with
6480 -- a fixed lower bound.
6482 Has_FLB_Index := True;
6484 -- Mark the index as belonging to an array type with a fixed
6485 -- lower bound.
6487 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6488 end;
6489 end if;
6491 -- Check error of subtype with predicate for index type
6493 Bad_Predicated_Subtype_Use
6494 ("subtype& has predicate, not allowed as index subtype",
6495 Index, Etype (Index));
6497 -- Move to next index
6499 Next (Index);
6500 Nb_Index := Nb_Index + 1;
6501 end loop;
6503 -- Process subtype indication if one is present
6505 if Present (Component_Typ) then
6506 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6507 Set_Etype (Component_Typ, Element_Type);
6509 -- Ada 2005 (AI-230): Access Definition case
6511 else pragma Assert (Present (Access_Definition (Component_Def)));
6513 -- Indicate that the anonymous access type is created by the
6514 -- array type declaration.
6516 Element_Type := Access_Definition
6517 (Related_Nod => P,
6518 N => Access_Definition (Component_Def));
6519 Set_Is_Local_Anonymous_Access (Element_Type);
6521 -- Propagate the parent. This field is needed if we have to generate
6522 -- the master_id associated with an anonymous access to task type
6523 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6525 Copy_Parent (To => Element_Type, From => T);
6527 -- Ada 2005 (AI-230): In case of components that are anonymous access
6528 -- types the level of accessibility depends on the enclosing type
6529 -- declaration
6531 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6533 -- Ada 2005 (AI-254)
6535 declare
6536 CD : constant Node_Id :=
6537 Access_To_Subprogram_Definition
6538 (Access_Definition (Component_Def));
6539 begin
6540 if Present (CD) and then Protected_Present (CD) then
6541 Element_Type :=
6542 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6543 end if;
6544 end;
6545 end if;
6547 -- Constrained array case
6549 if No (T) then
6550 -- We might be creating more than one itype with the same Related_Id,
6551 -- e.g. for an array object definition and its initial value. Give
6552 -- them unique suffixes, because GNATprove require distinct types to
6553 -- have different names.
6555 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6556 end if;
6558 if Nkind (Def) = N_Constrained_Array_Definition then
6559 -- Establish Implicit_Base as unconstrained base type
6561 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6563 Set_Etype (Implicit_Base, Implicit_Base);
6564 Set_Scope (Implicit_Base, Current_Scope);
6565 Set_Has_Delayed_Freeze (Implicit_Base);
6566 Set_Default_SSO (Implicit_Base);
6568 -- The constrained array type is a subtype of the unconstrained one
6570 Mutate_Ekind (T, E_Array_Subtype);
6571 Reinit_Size_Align (T);
6572 Set_Etype (T, Implicit_Base);
6573 Set_Scope (T, Current_Scope);
6574 Set_Is_Constrained (T);
6575 Set_First_Index (T,
6576 First (Discrete_Subtype_Definitions (Def)));
6577 Set_Has_Delayed_Freeze (T);
6579 -- Complete setup of implicit base type
6581 pragma Assert (not Known_Component_Size (Implicit_Base));
6582 Set_Component_Type (Implicit_Base, Element_Type);
6583 Set_Finalize_Storage_Only
6584 (Implicit_Base,
6585 Finalize_Storage_Only (Element_Type));
6586 Set_First_Index (Implicit_Base, First_Index (T));
6587 Set_Has_Controlled_Component
6588 (Implicit_Base,
6589 Has_Controlled_Component (Element_Type)
6590 or else Is_Controlled (Element_Type));
6591 Set_Packed_Array_Impl_Type
6592 (Implicit_Base, Empty);
6594 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6596 -- Unconstrained array case
6598 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6599 Mutate_Ekind (T, E_Array_Type);
6600 Reinit_Size_Align (T);
6601 Set_Etype (T, T);
6602 Set_Scope (T, Current_Scope);
6603 pragma Assert (not Known_Component_Size (T));
6604 Set_Is_Constrained (T, False);
6605 Set_Is_Fixed_Lower_Bound_Array_Subtype
6606 (T, Has_FLB_Index);
6607 Set_First_Index (T, First (Subtype_Marks (Def)));
6608 Set_Has_Delayed_Freeze (T, True);
6609 Propagate_Concurrent_Flags (T, Element_Type);
6610 Set_Has_Controlled_Component (T, Has_Controlled_Component
6611 (Element_Type)
6612 or else
6613 Is_Controlled (Element_Type));
6614 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6615 (Element_Type));
6616 Set_Default_SSO (T);
6617 end if;
6619 -- Common attributes for both cases
6621 Set_Component_Type (Base_Type (T), Element_Type);
6622 Set_Packed_Array_Impl_Type (T, Empty);
6624 if Aliased_Present (Component_Definition (Def)) then
6625 Set_Has_Aliased_Components (Etype (T));
6627 -- AI12-001: All aliased objects are considered to be specified as
6628 -- independently addressable (RM C.6(8.1/4)).
6630 Set_Has_Independent_Components (Etype (T));
6631 end if;
6633 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6634 -- array type to ensure that objects of this type are initialized.
6636 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6637 Set_Can_Never_Be_Null (T);
6639 if Null_Exclusion_Present (Component_Definition (Def))
6641 -- No need to check itypes because in their case this check was
6642 -- done at their point of creation
6644 and then not Is_Itype (Element_Type)
6645 then
6646 Error_Msg_N
6647 ("`NOT NULL` not allowed (null already excluded)",
6648 Subtype_Indication (Component_Definition (Def)));
6649 end if;
6650 end if;
6652 Priv := Private_Component (Element_Type);
6654 if Present (Priv) then
6656 -- Check for circular definitions
6658 if Priv = Any_Type then
6659 Set_Component_Type (Etype (T), Any_Type);
6661 -- There is a gap in the visibility of operations on the composite
6662 -- type only if the component type is defined in a different scope.
6664 elsif Scope (Priv) = Current_Scope then
6665 null;
6667 elsif Is_Limited_Type (Priv) then
6668 Set_Is_Limited_Composite (Etype (T));
6669 Set_Is_Limited_Composite (T);
6670 else
6671 Set_Is_Private_Composite (Etype (T));
6672 Set_Is_Private_Composite (T);
6673 end if;
6674 end if;
6676 -- A syntax error in the declaration itself may lead to an empty index
6677 -- list, in which case do a minimal patch.
6679 if No (First_Index (T)) then
6680 Error_Msg_N ("missing index definition in array type declaration", T);
6682 declare
6683 Indexes : constant List_Id :=
6684 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6685 begin
6686 Set_Discrete_Subtype_Definitions (Def, Indexes);
6687 Set_First_Index (T, First (Indexes));
6688 return;
6689 end;
6690 end if;
6692 -- Create a concatenation operator for the new type. Internal array
6693 -- types created for packed entities do not need such, they are
6694 -- compatible with the user-defined type.
6696 if Number_Dimensions (T) = 1
6697 and then not Is_Packed_Array_Impl_Type (T)
6698 then
6699 New_Concatenation_Op (T);
6700 end if;
6702 -- In the case of an unconstrained array the parser has already verified
6703 -- that all the indexes are unconstrained but we still need to make sure
6704 -- that the element type is constrained.
6706 if not Is_Definite_Subtype (Element_Type) then
6707 Error_Msg_N
6708 ("unconstrained element type in array declaration",
6709 Subtype_Indication (Component_Def));
6711 elsif Is_Abstract_Type (Element_Type) then
6712 Error_Msg_N
6713 ("the type of a component cannot be abstract",
6714 Subtype_Indication (Component_Def));
6715 end if;
6717 -- There may be an invariant declared for the component type, but
6718 -- the construction of the component invariant checking procedure
6719 -- takes place during expansion.
6720 end Array_Type_Declaration;
6722 ------------------------------------------------------
6723 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6724 ------------------------------------------------------
6726 function Replace_Anonymous_Access_To_Protected_Subprogram
6727 (N : Node_Id) return Entity_Id
6729 Loc : constant Source_Ptr := Sloc (N);
6731 Curr_Scope : constant Scope_Stack_Entry :=
6732 Scope_Stack.Table (Scope_Stack.Last);
6734 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6736 Acc : Node_Id;
6737 -- Access definition in declaration
6739 Comp : Node_Id;
6740 -- Object definition or formal definition with an access definition
6742 Decl : Node_Id;
6743 -- Declaration of anonymous access to subprogram type
6745 Spec : Node_Id;
6746 -- Original specification in access to subprogram
6748 P : Node_Id;
6750 begin
6751 Set_Is_Internal (Anon);
6753 case Nkind (N) is
6754 when N_Constrained_Array_Definition
6755 | N_Component_Declaration
6756 | N_Unconstrained_Array_Definition
6758 Comp := Component_Definition (N);
6759 Acc := Access_Definition (Comp);
6761 when N_Discriminant_Specification =>
6762 Comp := Discriminant_Type (N);
6763 Acc := Comp;
6765 when N_Parameter_Specification =>
6766 Comp := Parameter_Type (N);
6767 Acc := Comp;
6769 when N_Access_Function_Definition =>
6770 Comp := Result_Definition (N);
6771 Acc := Comp;
6773 when N_Object_Declaration =>
6774 Comp := Object_Definition (N);
6775 Acc := Comp;
6777 when N_Function_Specification =>
6778 Comp := Result_Definition (N);
6779 Acc := Comp;
6781 when others =>
6782 raise Program_Error;
6783 end case;
6785 Spec := Access_To_Subprogram_Definition (Acc);
6787 Decl :=
6788 Make_Full_Type_Declaration (Loc,
6789 Defining_Identifier => Anon,
6790 Type_Definition => Copy_Separate_Tree (Spec));
6792 Mark_Rewrite_Insertion (Decl);
6794 -- Insert the new declaration in the nearest enclosing scope. If the
6795 -- parent is a body and N is its return type, the declaration belongs
6796 -- in the enclosing scope. Likewise if N is the type of a parameter.
6798 P := Parent (N);
6800 if Nkind (N) = N_Function_Specification
6801 and then Nkind (P) = N_Subprogram_Body
6802 then
6803 P := Parent (P);
6804 elsif Nkind (N) = N_Parameter_Specification
6805 and then Nkind (P) in N_Subprogram_Specification
6806 and then Nkind (Parent (P)) = N_Subprogram_Body
6807 then
6808 P := Parent (Parent (P));
6809 end if;
6811 while Present (P) and then not Has_Declarations (P) loop
6812 P := Parent (P);
6813 end loop;
6815 pragma Assert (Present (P));
6817 if Nkind (P) = N_Package_Specification then
6818 Prepend (Decl, Visible_Declarations (P));
6819 else
6820 Prepend (Decl, Declarations (P));
6821 end if;
6823 -- Replace the anonymous type with an occurrence of the new declaration.
6824 -- In all cases the rewritten node does not have the null-exclusion
6825 -- attribute because (if present) it was already inherited by the
6826 -- anonymous entity (Anon). Thus, in case of components we do not
6827 -- inherit this attribute.
6829 if Nkind (N) = N_Parameter_Specification then
6830 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6831 Set_Etype (Defining_Identifier (N), Anon);
6832 Set_Null_Exclusion_Present (N, False);
6834 elsif Nkind (N) = N_Object_Declaration then
6835 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6836 Set_Etype (Defining_Identifier (N), Anon);
6838 elsif Nkind (N) = N_Access_Function_Definition then
6839 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6841 elsif Nkind (N) = N_Function_Specification then
6842 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6843 Set_Etype (Defining_Unit_Name (N), Anon);
6845 else
6846 Rewrite (Comp,
6847 Make_Component_Definition (Loc,
6848 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6849 end if;
6851 Mark_Rewrite_Insertion (Comp);
6853 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6854 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6855 and then not Is_Type (Current_Scope))
6856 then
6858 -- Declaration can be analyzed in the current scope.
6860 Analyze (Decl);
6862 else
6863 -- Temporarily remove the current scope (record or subprogram) from
6864 -- the stack to add the new declarations to the enclosing scope.
6865 -- The anonymous entity is an Itype with the proper attributes.
6867 Scope_Stack.Decrement_Last;
6868 Analyze (Decl);
6869 Set_Is_Itype (Anon);
6870 Set_Associated_Node_For_Itype (Anon, N);
6871 Scope_Stack.Append (Curr_Scope);
6872 end if;
6874 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6875 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6876 return Anon;
6877 end Replace_Anonymous_Access_To_Protected_Subprogram;
6879 -------------------------------------
6880 -- Build_Access_Subprogram_Wrapper --
6881 -------------------------------------
6883 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6884 Loc : constant Source_Ptr := Sloc (Decl);
6885 Id : constant Entity_Id := Defining_Identifier (Decl);
6886 Type_Def : constant Node_Id := Type_Definition (Decl);
6887 Specs : constant List_Id :=
6888 Parameter_Specifications (Type_Def);
6889 Profile : constant List_Id := New_List;
6890 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6892 Contracts : constant List_Id := New_List;
6893 Form_P : Node_Id;
6894 New_P : Node_Id;
6895 New_Decl : Node_Id;
6896 Spec : Node_Id;
6898 procedure Replace_Type_Name (Expr : Node_Id);
6899 -- In the expressions for contract aspects, replace occurrences of the
6900 -- access type with the name of the subprogram entity, as needed, e.g.
6901 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6902 -- remain on the original access type declaration. What about expanded
6903 -- names denoting formals, whose prefix in source is the type name ???
6905 -----------------------
6906 -- Replace_Type_Name --
6907 -----------------------
6909 procedure Replace_Type_Name (Expr : Node_Id) is
6910 function Process (N : Node_Id) return Traverse_Result;
6911 function Process (N : Node_Id) return Traverse_Result is
6912 begin
6913 if Nkind (N) = N_Attribute_Reference
6914 and then Is_Entity_Name (Prefix (N))
6915 and then Chars (Prefix (N)) = Chars (Id)
6916 then
6917 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6918 end if;
6920 return OK;
6921 end Process;
6923 procedure Traverse is new Traverse_Proc (Process);
6924 begin
6925 Traverse (Expr);
6926 end Replace_Type_Name;
6928 begin
6929 if Ekind (Id) in E_Access_Subprogram_Type
6930 | E_Access_Protected_Subprogram_Type
6931 | E_Anonymous_Access_Protected_Subprogram_Type
6932 | E_Anonymous_Access_Subprogram_Type
6933 then
6934 null;
6936 else
6937 Error_Msg_N
6938 ("illegal pre/postcondition on access type", Decl);
6939 return;
6940 end if;
6942 declare
6943 Asp : Node_Id;
6944 A_Id : Aspect_Id;
6946 begin
6947 Asp := First (Aspect_Specifications (Decl));
6948 while Present (Asp) loop
6949 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6950 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6951 Append (New_Copy_Tree (Asp), Contracts);
6952 Replace_Type_Name (Expression (Last (Contracts)));
6953 end if;
6954 Next (Asp);
6955 end loop;
6956 end;
6958 -- If there are no contract aspects, no need for a wrapper.
6960 if Is_Empty_List (Contracts) then
6961 return;
6962 end if;
6964 Form_P := First (Specs);
6966 while Present (Form_P) loop
6967 New_P := New_Copy_Tree (Form_P);
6968 Set_Defining_Identifier (New_P,
6969 Make_Defining_Identifier
6970 (Loc, Chars (Defining_Identifier (Form_P))));
6971 Append (New_P, Profile);
6972 Next (Form_P);
6973 end loop;
6975 -- Add to parameter specifications the access parameter that is passed
6976 -- in from an indirect call.
6978 Append (
6979 Make_Parameter_Specification (Loc,
6980 Defining_Identifier => Make_Temporary (Loc, 'P'),
6981 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6982 Profile);
6984 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6985 Spec :=
6986 Make_Procedure_Specification (Loc,
6987 Defining_Unit_Name => Subp,
6988 Parameter_Specifications => Profile);
6989 Mutate_Ekind (Subp, E_Procedure);
6990 else
6991 Spec :=
6992 Make_Function_Specification (Loc,
6993 Defining_Unit_Name => Subp,
6994 Parameter_Specifications => Profile,
6995 Result_Definition =>
6996 New_Copy_Tree
6997 (Result_Definition (Type_Definition (Decl))));
6998 Mutate_Ekind (Subp, E_Function);
6999 end if;
7001 New_Decl :=
7002 Make_Subprogram_Declaration (Loc, Specification => Spec);
7003 Set_Aspect_Specifications (New_Decl, Contracts);
7004 Set_Is_Wrapper (Subp);
7006 -- The wrapper is declared in the freezing actions to facilitate its
7007 -- identification and thus avoid handling it as a primitive operation
7008 -- of a tagged type (see Is_Access_To_Subprogram_Wrapper); otherwise it
7009 -- may be handled as a dispatching operation and erroneously registered
7010 -- in a dispatch table.
7012 Append_Freeze_Action (Id, New_Decl);
7014 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
7015 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
7016 end Build_Access_Subprogram_Wrapper;
7018 -------------------------------
7019 -- Build_Derived_Access_Type --
7020 -------------------------------
7022 procedure Build_Derived_Access_Type
7023 (N : Node_Id;
7024 Parent_Type : Entity_Id;
7025 Derived_Type : Entity_Id)
7027 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
7029 Desig_Type : Entity_Id;
7030 Discr : Entity_Id;
7031 Discr_Con_Elist : Elist_Id;
7032 Discr_Con_El : Elmt_Id;
7033 Subt : Entity_Id;
7035 begin
7036 -- Set the designated type so it is available in case this is an access
7037 -- to a self-referential type, e.g. a standard list type with a next
7038 -- pointer. Will be reset after subtype is built.
7040 Set_Directly_Designated_Type
7041 (Derived_Type, Designated_Type (Parent_Type));
7043 Subt := Process_Subtype (S, N);
7045 if Nkind (S) /= N_Subtype_Indication
7046 and then Subt /= Base_Type (Subt)
7047 then
7048 Mutate_Ekind (Derived_Type, E_Access_Subtype);
7049 end if;
7051 if Ekind (Derived_Type) = E_Access_Subtype then
7052 declare
7053 Pbase : constant Entity_Id := Base_Type (Parent_Type);
7054 Ibase : constant Entity_Id :=
7055 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
7056 Svg_Chars : constant Name_Id := Chars (Ibase);
7057 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
7058 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
7060 begin
7061 Copy_Node (Pbase, Ibase);
7063 -- Restore Itype status after Copy_Node
7065 Set_Is_Itype (Ibase);
7066 Set_Associated_Node_For_Itype (Ibase, N);
7068 Set_Chars (Ibase, Svg_Chars);
7069 Set_Prev_Entity (Ibase, Svg_Prev_E);
7070 Set_Next_Entity (Ibase, Svg_Next_E);
7071 Set_Sloc (Ibase, Sloc (Derived_Type));
7072 Set_Scope (Ibase, Scope (Derived_Type));
7073 Set_Freeze_Node (Ibase, Empty);
7074 Set_Is_Frozen (Ibase, False);
7075 Set_Comes_From_Source (Ibase, False);
7076 Set_Is_First_Subtype (Ibase, False);
7078 Set_Etype (Ibase, Pbase);
7079 Set_Etype (Derived_Type, Ibase);
7080 end;
7081 end if;
7083 Set_Directly_Designated_Type
7084 (Derived_Type, Designated_Type (Subt));
7086 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
7087 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
7088 Set_Size_Info (Derived_Type, Parent_Type);
7089 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
7090 Set_Depends_On_Private (Derived_Type,
7091 Has_Private_Component (Derived_Type));
7092 Conditional_Delay (Derived_Type, Subt);
7094 if Is_Access_Subprogram_Type (Derived_Type)
7095 and then Is_Base_Type (Derived_Type)
7096 then
7097 Set_Can_Use_Internal_Rep
7098 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
7099 end if;
7101 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
7102 -- that it is not redundant.
7104 if Null_Exclusion_Present (Type_Definition (N)) then
7105 Set_Can_Never_Be_Null (Derived_Type);
7107 elsif Can_Never_Be_Null (Parent_Type) then
7108 Set_Can_Never_Be_Null (Derived_Type);
7109 end if;
7111 -- Note: we do not copy the Storage_Size_Variable, since we always go to
7112 -- the root type for this information.
7114 -- Apply range checks to discriminants for derived record case
7115 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
7117 Desig_Type := Designated_Type (Derived_Type);
7119 if Is_Composite_Type (Desig_Type)
7120 and then not Is_Array_Type (Desig_Type)
7121 and then Has_Discriminants (Desig_Type)
7122 and then Base_Type (Desig_Type) /= Desig_Type
7123 then
7124 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
7125 Discr_Con_El := First_Elmt (Discr_Con_Elist);
7127 Discr := First_Discriminant (Base_Type (Desig_Type));
7128 while Present (Discr_Con_El) loop
7129 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
7130 Next_Elmt (Discr_Con_El);
7131 Next_Discriminant (Discr);
7132 end loop;
7133 end if;
7134 end Build_Derived_Access_Type;
7136 ------------------------------
7137 -- Build_Derived_Array_Type --
7138 ------------------------------
7140 procedure Build_Derived_Array_Type
7141 (N : Node_Id;
7142 Parent_Type : Entity_Id;
7143 Derived_Type : Entity_Id)
7145 Loc : constant Source_Ptr := Sloc (N);
7146 Tdef : constant Node_Id := Type_Definition (N);
7147 Indic : constant Node_Id := Subtype_Indication (Tdef);
7148 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7149 Implicit_Base : Entity_Id := Empty;
7150 New_Indic : Node_Id;
7152 procedure Make_Implicit_Base;
7153 -- If the parent subtype is constrained, the derived type is a subtype
7154 -- of an implicit base type derived from the parent base.
7156 ------------------------
7157 -- Make_Implicit_Base --
7158 ------------------------
7160 procedure Make_Implicit_Base is
7161 begin
7162 Implicit_Base :=
7163 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7165 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7166 Set_Etype (Implicit_Base, Parent_Base);
7168 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
7169 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
7171 Set_Has_Delayed_Freeze (Implicit_Base, True);
7172 end Make_Implicit_Base;
7174 -- Start of processing for Build_Derived_Array_Type
7176 begin
7177 if not Is_Constrained (Parent_Type) then
7178 if Nkind (Indic) /= N_Subtype_Indication then
7179 Mutate_Ekind (Derived_Type, E_Array_Type);
7181 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7182 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
7184 Set_Has_Delayed_Freeze (Derived_Type, True);
7186 else
7187 Make_Implicit_Base;
7188 Set_Etype (Derived_Type, Implicit_Base);
7190 New_Indic :=
7191 Make_Subtype_Declaration (Loc,
7192 Defining_Identifier => Derived_Type,
7193 Subtype_Indication =>
7194 Make_Subtype_Indication (Loc,
7195 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7196 Constraint => Constraint (Indic)));
7198 Rewrite (N, New_Indic);
7199 Analyze (N);
7200 end if;
7202 else
7203 if Nkind (Indic) /= N_Subtype_Indication then
7204 Make_Implicit_Base;
7206 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
7207 Set_Etype (Derived_Type, Implicit_Base);
7208 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
7210 else
7211 Error_Msg_N ("illegal constraint on constrained type", Indic);
7212 end if;
7213 end if;
7215 -- If parent type is not a derived type itself, and is declared in
7216 -- closed scope (e.g. a subprogram), then we must explicitly introduce
7217 -- the new type's concatenation operator since Derive_Subprograms
7218 -- will not inherit the parent's operator. If the parent type is
7219 -- unconstrained, the operator is of the unconstrained base type.
7221 if Number_Dimensions (Parent_Type) = 1
7222 and then not Is_Limited_Type (Parent_Type)
7223 and then not Is_Derived_Type (Parent_Type)
7224 and then not Is_Package_Or_Generic_Package
7225 (Scope (Base_Type (Parent_Type)))
7226 then
7227 if not Is_Constrained (Parent_Type)
7228 and then Is_Constrained (Derived_Type)
7229 then
7230 New_Concatenation_Op (Implicit_Base);
7231 else
7232 New_Concatenation_Op (Derived_Type);
7233 end if;
7234 end if;
7235 end Build_Derived_Array_Type;
7237 -----------------------------------
7238 -- Build_Derived_Concurrent_Type --
7239 -----------------------------------
7241 procedure Build_Derived_Concurrent_Type
7242 (N : Node_Id;
7243 Parent_Type : Entity_Id;
7244 Derived_Type : Entity_Id)
7246 Loc : constant Source_Ptr := Sloc (N);
7247 Def : constant Node_Id := Type_Definition (N);
7248 Indic : constant Node_Id := Subtype_Indication (Def);
7250 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7251 Corr_Decl : Node_Id := Empty;
7252 Corr_Decl_Needed : Boolean;
7253 -- If the derived type has fewer discriminants than its parent, the
7254 -- corresponding record is also a derived type, in order to account for
7255 -- the bound discriminants. We create a full type declaration for it in
7256 -- this case.
7258 Constraint_Present : constant Boolean :=
7259 Nkind (Indic) = N_Subtype_Indication;
7261 D_Constraint : Node_Id;
7262 New_Constraint : Elist_Id := No_Elist;
7263 Old_Disc : Entity_Id;
7264 New_Disc : Entity_Id;
7265 New_N : Node_Id;
7267 begin
7268 Set_Stored_Constraint (Derived_Type, No_Elist);
7269 Corr_Decl_Needed := False;
7270 Old_Disc := Empty;
7272 if Present (Discriminant_Specifications (N))
7273 and then Constraint_Present
7274 then
7275 Old_Disc := First_Discriminant (Parent_Type);
7276 New_Disc := First (Discriminant_Specifications (N));
7277 while Present (New_Disc) and then Present (Old_Disc) loop
7278 Next_Discriminant (Old_Disc);
7279 Next (New_Disc);
7280 end loop;
7281 end if;
7283 if Present (Old_Disc) and then Expander_Active then
7285 -- The new type has fewer discriminants, so we need to create a new
7286 -- corresponding record, which is derived from the corresponding
7287 -- record of the parent, and has a stored constraint that captures
7288 -- the values of the discriminant constraints. The corresponding
7289 -- record is needed only if expander is active and code generation is
7290 -- enabled.
7292 -- The type declaration for the derived corresponding record has the
7293 -- same discriminant part and constraints as the current declaration.
7294 -- Copy the unanalyzed tree to build declaration.
7296 Corr_Decl_Needed := True;
7297 New_N := Copy_Separate_Tree (N);
7299 Corr_Decl :=
7300 Make_Full_Type_Declaration (Loc,
7301 Defining_Identifier => Corr_Record,
7302 Discriminant_Specifications =>
7303 Discriminant_Specifications (New_N),
7304 Type_Definition =>
7305 Make_Derived_Type_Definition (Loc,
7306 Subtype_Indication =>
7307 Make_Subtype_Indication (Loc,
7308 Subtype_Mark =>
7309 New_Occurrence_Of
7310 (Corresponding_Record_Type (Parent_Type), Loc),
7311 Constraint =>
7312 Constraint
7313 (Subtype_Indication (Type_Definition (New_N))))));
7314 end if;
7316 -- Copy Storage_Size and Relative_Deadline variables if task case
7318 if Is_Task_Type (Parent_Type) then
7319 Set_Storage_Size_Variable (Derived_Type,
7320 Storage_Size_Variable (Parent_Type));
7321 Set_Relative_Deadline_Variable (Derived_Type,
7322 Relative_Deadline_Variable (Parent_Type));
7323 end if;
7325 if Present (Discriminant_Specifications (N)) then
7326 Push_Scope (Derived_Type);
7327 Check_Or_Process_Discriminants (N, Derived_Type);
7329 if Constraint_Present then
7330 New_Constraint :=
7331 Expand_To_Stored_Constraint
7332 (Parent_Type,
7333 Build_Discriminant_Constraints
7334 (Parent_Type, Indic, True));
7335 end if;
7337 End_Scope;
7339 elsif Constraint_Present then
7341 -- Build an unconstrained derived type and rewrite the derived type
7342 -- as a subtype of this new base type.
7344 declare
7345 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7346 New_Base : Entity_Id;
7347 New_Decl : Node_Id;
7348 New_Indic : Node_Id;
7350 begin
7351 New_Base :=
7352 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7354 New_Decl :=
7355 Make_Full_Type_Declaration (Loc,
7356 Defining_Identifier => New_Base,
7357 Type_Definition =>
7358 Make_Derived_Type_Definition (Loc,
7359 Abstract_Present => Abstract_Present (Def),
7360 Limited_Present => Limited_Present (Def),
7361 Subtype_Indication =>
7362 New_Occurrence_Of (Parent_Base, Loc)));
7364 Mark_Rewrite_Insertion (New_Decl);
7365 Insert_Before (N, New_Decl);
7366 Analyze (New_Decl);
7368 New_Indic :=
7369 Make_Subtype_Indication (Loc,
7370 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7371 Constraint => Relocate_Node (Constraint (Indic)));
7373 Rewrite (N,
7374 Make_Subtype_Declaration (Loc,
7375 Defining_Identifier => Derived_Type,
7376 Subtype_Indication => New_Indic));
7378 Analyze (N);
7379 return;
7380 end;
7381 end if;
7383 -- By default, operations and private data are inherited from parent.
7384 -- However, in the presence of bound discriminants, a new corresponding
7385 -- record will be created, see below.
7387 Set_Has_Discriminants
7388 (Derived_Type, Has_Discriminants (Parent_Type));
7389 Set_Corresponding_Record_Type
7390 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7392 -- Is_Constrained is set according the parent subtype, but is set to
7393 -- False if the derived type is declared with new discriminants.
7395 Set_Is_Constrained
7396 (Derived_Type,
7397 (Is_Constrained (Parent_Type) or else Constraint_Present)
7398 and then not Present (Discriminant_Specifications (N)));
7400 if Constraint_Present then
7401 if not Has_Discriminants (Parent_Type) then
7402 Error_Msg_N ("untagged parent must have discriminants", N);
7404 elsif Present (Discriminant_Specifications (N)) then
7406 -- Verify that new discriminants are used to constrain old ones
7408 D_Constraint := First (Constraints (Constraint (Indic)));
7410 Old_Disc := First_Discriminant (Parent_Type);
7412 while Present (D_Constraint) loop
7413 if Nkind (D_Constraint) /= N_Discriminant_Association then
7415 -- Positional constraint. If it is a reference to a new
7416 -- discriminant, it constrains the corresponding old one.
7418 if Nkind (D_Constraint) = N_Identifier then
7419 New_Disc := First_Discriminant (Derived_Type);
7420 while Present (New_Disc) loop
7421 exit when Chars (New_Disc) = Chars (D_Constraint);
7422 Next_Discriminant (New_Disc);
7423 end loop;
7425 if Present (New_Disc) then
7426 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7427 end if;
7428 end if;
7430 Next_Discriminant (Old_Disc);
7432 -- if this is a named constraint, search by name for the old
7433 -- discriminants constrained by the new one.
7435 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7437 -- Find new discriminant with that name
7439 New_Disc := First_Discriminant (Derived_Type);
7440 while Present (New_Disc) loop
7441 exit when
7442 Chars (New_Disc) = Chars (Expression (D_Constraint));
7443 Next_Discriminant (New_Disc);
7444 end loop;
7446 if Present (New_Disc) then
7448 -- Verify that new discriminant renames some discriminant
7449 -- of the parent type, and associate the new discriminant
7450 -- with one or more old ones that it renames.
7452 declare
7453 Selector : Node_Id;
7455 begin
7456 Selector := First (Selector_Names (D_Constraint));
7457 while Present (Selector) loop
7458 Old_Disc := First_Discriminant (Parent_Type);
7459 while Present (Old_Disc) loop
7460 exit when Chars (Old_Disc) = Chars (Selector);
7461 Next_Discriminant (Old_Disc);
7462 end loop;
7464 if Present (Old_Disc) then
7465 Set_Corresponding_Discriminant
7466 (New_Disc, Old_Disc);
7467 end if;
7469 Next (Selector);
7470 end loop;
7471 end;
7472 end if;
7473 end if;
7475 Next (D_Constraint);
7476 end loop;
7478 New_Disc := First_Discriminant (Derived_Type);
7479 while Present (New_Disc) loop
7480 if No (Corresponding_Discriminant (New_Disc)) then
7481 Error_Msg_NE
7482 ("new discriminant& must constrain old one", N, New_Disc);
7484 -- If a new discriminant is used in the constraint, then its
7485 -- subtype must be statically compatible with the subtype of
7486 -- the parent discriminant (RM 3.7(15)).
7488 else
7489 Check_Constraining_Discriminant
7490 (New_Disc, Corresponding_Discriminant (New_Disc));
7491 end if;
7493 Next_Discriminant (New_Disc);
7494 end loop;
7495 end if;
7497 elsif Present (Discriminant_Specifications (N)) then
7498 Error_Msg_N
7499 ("missing discriminant constraint in untagged derivation", N);
7500 end if;
7502 -- The entity chain of the derived type includes the new discriminants
7503 -- but shares operations with the parent.
7505 if Present (Discriminant_Specifications (N)) then
7506 Old_Disc := First_Discriminant (Parent_Type);
7507 while Present (Old_Disc) loop
7508 if No (Next_Entity (Old_Disc))
7509 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7510 then
7511 Link_Entities
7512 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7513 exit;
7514 end if;
7516 Next_Discriminant (Old_Disc);
7517 end loop;
7519 else
7520 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7521 if Has_Discriminants (Parent_Type) then
7522 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7523 Set_Discriminant_Constraint (
7524 Derived_Type, Discriminant_Constraint (Parent_Type));
7525 end if;
7526 end if;
7528 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7530 Set_Has_Completion (Derived_Type);
7532 if Corr_Decl_Needed then
7533 Set_Stored_Constraint (Derived_Type, New_Constraint);
7534 Insert_After (N, Corr_Decl);
7535 Analyze (Corr_Decl);
7536 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7537 end if;
7538 end Build_Derived_Concurrent_Type;
7540 ------------------------------------
7541 -- Build_Derived_Enumeration_Type --
7542 ------------------------------------
7544 procedure Build_Derived_Enumeration_Type
7545 (N : Node_Id;
7546 Parent_Type : Entity_Id;
7547 Derived_Type : Entity_Id)
7549 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7550 -- When the type declaration includes a constraint, we generate
7551 -- a subtype declaration of an anonymous base type, with the constraint
7552 -- given in the original type declaration. Conceptually, the bounds
7553 -- are converted to the new base type, and this conversion freezes
7554 -- (prematurely) that base type, when the bounds are simply literals.
7555 -- As a result, a representation clause for the derived type is then
7556 -- rejected or ignored. This procedure recognizes the simple case of
7557 -- literal bounds, which allows us to indicate that the conversions
7558 -- are not freeze points, and the subsequent representation clause
7559 -- can be accepted.
7560 -- A similar approach might be used to resolve the long-standing
7561 -- problem of premature freezing of derived numeric types ???
7563 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7564 begin
7565 return Nkind (B) = N_Type_Conversion
7566 and then Is_Entity_Name (Expression (B))
7567 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7568 end Bound_Belongs_To_Type;
7570 Loc : constant Source_Ptr := Sloc (N);
7571 Def : constant Node_Id := Type_Definition (N);
7572 Indic : constant Node_Id := Subtype_Indication (Def);
7573 Implicit_Base : Entity_Id;
7574 Literal : Entity_Id;
7575 New_Lit : Entity_Id;
7576 Literals_List : List_Id;
7577 Type_Decl : Node_Id;
7578 Hi, Lo : Node_Id;
7579 Rang_Expr : Node_Id;
7581 begin
7582 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7583 -- not have explicit literals lists we need to process types derived
7584 -- from them specially. This is handled by Derived_Standard_Character.
7585 -- If the parent type is a generic type, there are no literals either,
7586 -- and we construct the same skeletal representation as for the generic
7587 -- parent type.
7589 if Is_Standard_Character_Type (Parent_Type) then
7590 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7592 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7593 declare
7594 Lo : Node_Id;
7595 Hi : Node_Id;
7597 begin
7598 if Nkind (Indic) /= N_Subtype_Indication then
7599 Lo :=
7600 Make_Attribute_Reference (Loc,
7601 Attribute_Name => Name_First,
7602 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7603 Set_Etype (Lo, Derived_Type);
7605 Hi :=
7606 Make_Attribute_Reference (Loc,
7607 Attribute_Name => Name_Last,
7608 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7609 Set_Etype (Hi, Derived_Type);
7611 Set_Scalar_Range (Derived_Type,
7612 Make_Range (Loc,
7613 Low_Bound => Lo,
7614 High_Bound => Hi));
7615 else
7617 -- Analyze subtype indication and verify compatibility
7618 -- with parent type.
7620 if Base_Type (Process_Subtype (Indic, N)) /=
7621 Base_Type (Parent_Type)
7622 then
7623 Error_Msg_N
7624 ("illegal constraint for formal discrete type", N);
7625 end if;
7626 end if;
7627 end;
7629 else
7630 -- If a constraint is present, analyze the bounds to catch
7631 -- premature usage of the derived literals.
7633 if Nkind (Indic) = N_Subtype_Indication
7634 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7635 then
7636 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7637 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7638 end if;
7640 -- Create an implicit base type for the derived type even if there
7641 -- is no constraint attached to it, since this seems closer to the
7642 -- Ada semantics. Use an Itype like for the implicit base type of
7643 -- other kinds of derived type, but build a full type declaration
7644 -- for it so as to analyze the new literals properly. Then build a
7645 -- subtype declaration tree which applies the constraint (if any)
7646 -- and have it replace the derived type declaration.
7648 Literal := First_Literal (Parent_Type);
7649 Literals_List := New_List;
7650 while Present (Literal)
7651 and then Ekind (Literal) = E_Enumeration_Literal
7652 loop
7653 -- Literals of the derived type have the same representation as
7654 -- those of the parent type, but this representation can be
7655 -- overridden by an explicit representation clause. Indicate
7656 -- that there is no explicit representation given yet. These
7657 -- derived literals are implicit operations of the new type,
7658 -- and can be overridden by explicit ones.
7660 if Nkind (Literal) = N_Defining_Character_Literal then
7661 New_Lit :=
7662 Make_Defining_Character_Literal (Loc, Chars (Literal));
7663 else
7664 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7665 end if;
7667 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7668 Set_Is_Not_Self_Hidden (New_Lit);
7669 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7670 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7671 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7672 Set_Alias (New_Lit, Literal);
7673 Set_Is_Known_Valid (New_Lit, True);
7675 Append (New_Lit, Literals_List);
7676 Next_Literal (Literal);
7677 end loop;
7679 Implicit_Base :=
7680 Create_Itype (E_Enumeration_Type, N, Derived_Type, 'B');
7682 -- Indicate the proper nature of the derived type. This must be done
7683 -- before analysis of the literals, to recognize cases when a literal
7684 -- may be hidden by a previous explicit function definition (cf.
7685 -- c83031a).
7687 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7688 Set_Etype (Derived_Type, Implicit_Base);
7690 Type_Decl :=
7691 Make_Full_Type_Declaration (Loc,
7692 Defining_Identifier => Implicit_Base,
7693 Type_Definition =>
7694 Make_Enumeration_Type_Definition (Loc, Literals_List));
7696 -- Do not insert the declarationn, just analyze it in the context
7698 Set_Parent (Type_Decl, Parent (N));
7699 Analyze (Type_Decl);
7701 -- The anonymous base now has a full declaration, but this base
7702 -- is not a first subtype.
7704 Set_Is_First_Subtype (Implicit_Base, False);
7706 -- After the implicit base is analyzed its Etype needs to be changed
7707 -- to reflect the fact that it is derived from the parent type which
7708 -- was ignored during analysis. We also set the size at this point.
7710 Set_Etype (Implicit_Base, Parent_Type);
7712 Set_Size_Info (Implicit_Base, Parent_Type);
7713 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7714 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7716 -- Copy other flags from parent type
7718 Set_Has_Non_Standard_Rep
7719 (Implicit_Base, Has_Non_Standard_Rep
7720 (Parent_Type));
7721 Set_Has_Pragma_Ordered
7722 (Implicit_Base, Has_Pragma_Ordered
7723 (Parent_Type));
7724 Set_Has_Delayed_Freeze (Implicit_Base);
7726 -- Process the subtype indication including a validation check on the
7727 -- constraint, if any. If a constraint is given, its bounds must be
7728 -- implicitly converted to the new type.
7730 if Nkind (Indic) = N_Subtype_Indication then
7731 declare
7732 R : constant Node_Id :=
7733 Range_Expression (Constraint (Indic));
7735 begin
7736 if Nkind (R) = N_Range then
7737 Hi := Build_Scalar_Bound
7738 (High_Bound (R), Parent_Type, Implicit_Base);
7739 Lo := Build_Scalar_Bound
7740 (Low_Bound (R), Parent_Type, Implicit_Base);
7742 else
7743 -- Constraint is a Range attribute. Replace with explicit
7744 -- mention of the bounds of the prefix, which must be a
7745 -- subtype.
7747 Analyze (Prefix (R));
7748 Hi :=
7749 Convert_To (Implicit_Base,
7750 Make_Attribute_Reference (Loc,
7751 Attribute_Name => Name_Last,
7752 Prefix =>
7753 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7755 Lo :=
7756 Convert_To (Implicit_Base,
7757 Make_Attribute_Reference (Loc,
7758 Attribute_Name => Name_First,
7759 Prefix =>
7760 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7761 end if;
7762 end;
7764 else
7765 Hi :=
7766 Build_Scalar_Bound
7767 (Type_High_Bound (Parent_Type),
7768 Parent_Type, Implicit_Base);
7769 Lo :=
7770 Build_Scalar_Bound
7771 (Type_Low_Bound (Parent_Type),
7772 Parent_Type, Implicit_Base);
7773 end if;
7775 Rang_Expr :=
7776 Make_Range (Loc,
7777 Low_Bound => Lo,
7778 High_Bound => Hi);
7780 -- If we constructed a default range for the case where no range
7781 -- was given, then the expressions in the range must not freeze
7782 -- since they do not correspond to expressions in the source.
7783 -- However, if the type inherits predicates the expressions will
7784 -- be elaborated earlier and must freeze.
7786 if (Nkind (Indic) /= N_Subtype_Indication
7787 or else
7788 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7789 and then not Has_Predicates (Derived_Type)
7790 then
7791 Set_Must_Not_Freeze (Lo);
7792 Set_Must_Not_Freeze (Hi);
7793 Set_Must_Not_Freeze (Rang_Expr);
7794 end if;
7796 Rewrite (N,
7797 Make_Subtype_Declaration (Loc,
7798 Defining_Identifier => Derived_Type,
7799 Subtype_Indication =>
7800 Make_Subtype_Indication (Loc,
7801 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7802 Constraint =>
7803 Make_Range_Constraint (Loc,
7804 Range_Expression => Rang_Expr))));
7806 Analyze (N);
7808 -- Propagate the aspects from the original type declaration to the
7809 -- declaration of the implicit base.
7811 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7813 -- Apply a range check. Since this range expression doesn't have an
7814 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7815 -- this right???
7817 if Nkind (Indic) = N_Subtype_Indication then
7818 Apply_Range_Check
7819 (Range_Expression (Constraint (Indic)), Parent_Type,
7820 Source_Typ => Entity (Subtype_Mark (Indic)));
7821 end if;
7822 end if;
7823 end Build_Derived_Enumeration_Type;
7825 --------------------------------
7826 -- Build_Derived_Numeric_Type --
7827 --------------------------------
7829 procedure Build_Derived_Numeric_Type
7830 (N : Node_Id;
7831 Parent_Type : Entity_Id;
7832 Derived_Type : Entity_Id)
7834 Loc : constant Source_Ptr := Sloc (N);
7835 Tdef : constant Node_Id := Type_Definition (N);
7836 Indic : constant Node_Id := Subtype_Indication (Tdef);
7837 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7838 No_Constraint : constant Boolean := Nkind (Indic) /=
7839 N_Subtype_Indication;
7840 Implicit_Base : Entity_Id;
7842 Lo : Node_Id;
7843 Hi : Node_Id;
7845 begin
7846 -- Process the subtype indication including a validation check on
7847 -- the constraint if any.
7849 Discard_Node (Process_Subtype (Indic, N));
7851 -- Introduce an implicit base type for the derived type even if there
7852 -- is no constraint attached to it, since this seems closer to the Ada
7853 -- semantics.
7855 Implicit_Base :=
7856 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7858 Set_Etype (Implicit_Base, Parent_Base);
7859 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7860 Set_Size_Info (Implicit_Base, Parent_Base);
7861 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7862 Set_Parent (Implicit_Base, Parent (Derived_Type));
7863 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7864 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7866 -- Set RM Size for discrete type or decimal fixed-point type
7867 -- Ordinary fixed-point is excluded, why???
7869 if Is_Discrete_Type (Parent_Base)
7870 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7871 then
7872 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7873 end if;
7875 Set_Has_Delayed_Freeze (Implicit_Base);
7877 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7878 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7880 Set_Scalar_Range (Implicit_Base,
7881 Make_Range (Loc,
7882 Low_Bound => Lo,
7883 High_Bound => Hi));
7885 if Has_Infinities (Parent_Base) then
7886 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7887 end if;
7889 -- The Derived_Type, which is the entity of the declaration, is a
7890 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7891 -- absence of an explicit constraint.
7893 Set_Etype (Derived_Type, Implicit_Base);
7895 -- If we did not have a constraint, then the Ekind is set from the
7896 -- parent type (otherwise Process_Subtype has set the bounds)
7898 if No_Constraint then
7899 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7900 end if;
7902 -- If we did not have a range constraint, then set the range from the
7903 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7905 if No_Constraint or else not Has_Range_Constraint (Indic) then
7906 Set_Scalar_Range (Derived_Type,
7907 Make_Range (Loc,
7908 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7909 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7910 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7912 if Has_Infinities (Parent_Type) then
7913 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7914 end if;
7916 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7917 end if;
7919 Set_Is_Descendant_Of_Address (Derived_Type,
7920 Is_Descendant_Of_Address (Parent_Type));
7921 Set_Is_Descendant_Of_Address (Implicit_Base,
7922 Is_Descendant_Of_Address (Parent_Type));
7924 -- Set remaining type-specific fields, depending on numeric type
7926 if Is_Modular_Integer_Type (Parent_Type) then
7927 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7929 Set_Non_Binary_Modulus
7930 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7932 Set_Is_Known_Valid
7933 (Implicit_Base, Is_Known_Valid (Parent_Base));
7935 elsif Is_Floating_Point_Type (Parent_Type) then
7937 -- Digits of base type is always copied from the digits value of
7938 -- the parent base type, but the digits of the derived type will
7939 -- already have been set if there was a constraint present.
7941 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7942 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7944 if No_Constraint then
7945 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7946 end if;
7948 elsif Is_Fixed_Point_Type (Parent_Type) then
7950 -- Small of base type and derived type are always copied from the
7951 -- parent base type, since smalls never change. The delta of the
7952 -- base type is also copied from the parent base type. However the
7953 -- delta of the derived type will have been set already if a
7954 -- constraint was present.
7956 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7957 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7958 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7960 if No_Constraint then
7961 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7962 end if;
7964 -- The scale and machine radix in the decimal case are always
7965 -- copied from the parent base type.
7967 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7968 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7969 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7971 Set_Machine_Radix_10
7972 (Derived_Type, Machine_Radix_10 (Parent_Base));
7973 Set_Machine_Radix_10
7974 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7976 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7978 if No_Constraint then
7979 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7981 else
7982 -- the analysis of the subtype_indication sets the
7983 -- digits value of the derived type.
7985 null;
7986 end if;
7987 end if;
7988 end if;
7990 if Is_Integer_Type (Parent_Type) then
7991 Set_Has_Shift_Operator
7992 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7993 end if;
7995 -- The type of the bounds is that of the parent type, and they
7996 -- must be converted to the derived type.
7998 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7999 end Build_Derived_Numeric_Type;
8001 --------------------------------
8002 -- Build_Derived_Private_Type --
8003 --------------------------------
8005 procedure Build_Derived_Private_Type
8006 (N : Node_Id;
8007 Parent_Type : Entity_Id;
8008 Derived_Type : Entity_Id;
8009 Is_Completion : Boolean;
8010 Derive_Subps : Boolean := True)
8012 Loc : constant Source_Ptr := Sloc (N);
8013 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
8014 Par_Scope : constant Entity_Id := Scope (Par_Base);
8015 Full_N : constant Node_Id := New_Copy_Tree (N);
8016 Full_Der : Entity_Id := New_Copy (Derived_Type);
8017 Full_P : Entity_Id;
8019 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
8020 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
8021 -- present (they cannot be both present for the same type), or Empty.
8023 procedure Build_Full_Derivation;
8024 -- Build full derivation, i.e. derive from the full view
8026 procedure Copy_And_Build;
8027 -- Copy derived type declaration, replace parent with its full view,
8028 -- and build derivation
8030 -------------------------
8031 -- Available_Full_View --
8032 -------------------------
8034 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
8035 begin
8036 if Present (Full_View (Typ)) then
8037 return Full_View (Typ);
8039 elsif Present (Underlying_Full_View (Typ)) then
8041 -- We should be called on a type with an underlying full view
8042 -- only by means of the recursive call made in Copy_And_Build
8043 -- through the first call to Build_Derived_Type, or else if
8044 -- the parent scope is being analyzed because we are deriving
8045 -- a completion.
8047 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
8049 return Underlying_Full_View (Typ);
8051 else
8052 return Empty;
8053 end if;
8054 end Available_Full_View;
8056 ---------------------------
8057 -- Build_Full_Derivation --
8058 ---------------------------
8060 procedure Build_Full_Derivation is
8061 begin
8062 -- If parent scope is not open, install the declarations
8064 if not In_Open_Scopes (Par_Scope) then
8065 Install_Private_Declarations (Par_Scope);
8066 Install_Visible_Declarations (Par_Scope);
8067 Copy_And_Build;
8068 Uninstall_Declarations (Par_Scope);
8070 -- If parent scope is open and in another unit, and parent has a
8071 -- completion, then the derivation is taking place in the visible
8072 -- part of a child unit. In that case retrieve the full view of
8073 -- the parent momentarily.
8075 elsif not In_Same_Source_Unit (N, Parent_Type)
8076 and then Present (Full_View (Parent_Type))
8077 then
8078 Full_P := Full_View (Parent_Type);
8079 Exchange_Declarations (Parent_Type);
8080 Copy_And_Build;
8081 Exchange_Declarations (Full_P);
8083 -- Otherwise it is a local derivation
8085 else
8086 Copy_And_Build;
8087 end if;
8088 end Build_Full_Derivation;
8090 --------------------
8091 -- Copy_And_Build --
8092 --------------------
8094 procedure Copy_And_Build is
8095 Full_Parent : Entity_Id := Parent_Type;
8097 begin
8098 -- If the parent is itself derived from another private type,
8099 -- installing the private declarations has not affected its
8100 -- privacy status, so use its own full view explicitly.
8102 if Is_Private_Type (Full_Parent)
8103 and then Present (Full_View (Full_Parent))
8104 then
8105 Full_Parent := Full_View (Full_Parent);
8106 end if;
8108 -- If the full view is itself derived from another private type
8109 -- and has got an underlying full view, and this is done for a
8110 -- completion, i.e. to build the underlying full view of the type,
8111 -- then use this underlying full view. We cannot do that if this
8112 -- is not a completion, i.e. to build the full view of the type,
8113 -- because this would break the privacy of the parent type, except
8114 -- if the parent scope is being analyzed because we are deriving a
8115 -- completion.
8117 if Is_Private_Type (Full_Parent)
8118 and then Present (Underlying_Full_View (Full_Parent))
8119 and then (Is_Completion or else In_Private_Part (Par_Scope))
8120 then
8121 Full_Parent := Underlying_Full_View (Full_Parent);
8122 end if;
8124 -- For private, record, concurrent, access and almost all enumeration
8125 -- types, the derivation from the full view requires a fully-fledged
8126 -- declaration. In the other cases, just use an itype.
8128 if Is_Private_Type (Full_Parent)
8129 or else Is_Record_Type (Full_Parent)
8130 or else Is_Concurrent_Type (Full_Parent)
8131 or else Is_Access_Type (Full_Parent)
8132 or else
8133 (Is_Enumeration_Type (Full_Parent)
8134 and then not Is_Standard_Character_Type (Full_Parent)
8135 and then not Is_Generic_Type (Root_Type (Full_Parent)))
8136 then
8137 -- Copy and adjust declaration to provide a completion for what
8138 -- is originally a private declaration. Indicate that full view
8139 -- is internally generated.
8141 Set_Comes_From_Source (Full_N, False);
8142 Set_Comes_From_Source (Full_Der, False);
8143 Set_Parent (Full_Der, Full_N);
8144 Set_Defining_Identifier (Full_N, Full_Der);
8146 -- If there are no constraints, adjust the subtype mark
8148 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
8149 N_Subtype_Indication
8150 then
8151 Set_Subtype_Indication
8152 (Type_Definition (Full_N),
8153 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
8154 end if;
8156 Insert_After (N, Full_N);
8158 -- Build full view of derived type from full view of parent which
8159 -- is now installed. Subprograms have been derived on the partial
8160 -- view, the completion does not derive them anew.
8162 if Is_Record_Type (Full_Parent) then
8164 -- If parent type is tagged, the completion inherits the proper
8165 -- primitive operations.
8167 if Is_Tagged_Type (Parent_Type) then
8168 Build_Derived_Record_Type
8169 (Full_N, Full_Parent, Full_Der, Derive_Subps);
8170 else
8171 Build_Derived_Record_Type
8172 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
8173 end if;
8175 else
8176 -- If the parent type is private, this is not a completion and
8177 -- we build the full derivation recursively as a completion.
8179 Build_Derived_Type
8180 (Full_N, Full_Parent, Full_Der,
8181 Is_Completion => Is_Private_Type (Full_Parent),
8182 Derive_Subps => False);
8183 end if;
8185 -- The full declaration has been introduced into the tree and
8186 -- processed in the step above. It should not be analyzed again
8187 -- (when encountered later in the current list of declarations)
8188 -- to prevent spurious name conflicts. The full entity remains
8189 -- invisible.
8191 Set_Analyzed (Full_N);
8193 else
8194 Full_Der :=
8195 Make_Defining_Identifier (Sloc (Derived_Type),
8196 Chars => Chars (Derived_Type));
8197 Set_Is_Itype (Full_Der);
8198 Set_Associated_Node_For_Itype (Full_Der, N);
8199 Set_Parent (Full_Der, N);
8200 Build_Derived_Type
8201 (N, Full_Parent, Full_Der,
8202 Is_Completion => False, Derive_Subps => False);
8203 Set_Is_Not_Self_Hidden (Full_Der);
8204 end if;
8206 Set_Has_Private_Declaration (Full_Der);
8207 Set_Has_Private_Declaration (Derived_Type);
8209 Set_Scope (Full_Der, Scope (Derived_Type));
8210 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8211 Set_Has_Size_Clause (Full_Der, False);
8212 Set_Has_Alignment_Clause (Full_Der, False);
8213 Set_Has_Delayed_Freeze (Full_Der);
8214 Set_Is_Frozen (Full_Der, False);
8215 Set_Freeze_Node (Full_Der, Empty);
8216 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8217 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8219 -- The convention on the base type may be set in the private part
8220 -- and not propagated to the subtype until later, so we obtain the
8221 -- convention from the base type of the parent.
8223 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8224 end Copy_And_Build;
8226 -- Start of processing for Build_Derived_Private_Type
8228 begin
8229 if Is_Tagged_Type (Parent_Type) then
8230 Full_P := Full_View (Parent_Type);
8232 -- A type extension of a type with unknown discriminants is an
8233 -- indefinite type that the back-end cannot handle directly.
8234 -- We treat it as a private type, and build a completion that is
8235 -- derived from the full view of the parent, and hopefully has
8236 -- known discriminants.
8238 -- If the full view of the parent type has an underlying record view,
8239 -- use it to generate the underlying record view of this derived type
8240 -- (required for chains of derivations with unknown discriminants).
8242 -- Minor optimization: we avoid the generation of useless underlying
8243 -- record view entities if the private type declaration has unknown
8244 -- discriminants but its corresponding full view has no
8245 -- discriminants.
8247 if Has_Unknown_Discriminants (Parent_Type)
8248 and then Present (Full_P)
8249 and then (Has_Discriminants (Full_P)
8250 or else Present (Underlying_Record_View (Full_P)))
8251 and then not In_Open_Scopes (Par_Scope)
8252 and then Expander_Active
8253 then
8254 declare
8255 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8256 New_Ext : constant Node_Id :=
8257 Copy_Separate_Tree
8258 (Record_Extension_Part (Type_Definition (N)));
8259 Decl : Node_Id;
8261 begin
8262 Build_Derived_Record_Type
8263 (N, Parent_Type, Derived_Type, Derive_Subps);
8265 -- Build anonymous completion, as a derivation from the full
8266 -- view of the parent. This is not a completion in the usual
8267 -- sense, because the current type is not private.
8269 Decl :=
8270 Make_Full_Type_Declaration (Loc,
8271 Defining_Identifier => Full_Der,
8272 Type_Definition =>
8273 Make_Derived_Type_Definition (Loc,
8274 Subtype_Indication =>
8275 New_Copy_Tree
8276 (Subtype_Indication (Type_Definition (N))),
8277 Record_Extension_Part => New_Ext));
8279 -- If the parent type has an underlying record view, use it
8280 -- here to build the new underlying record view.
8282 if Present (Underlying_Record_View (Full_P)) then
8283 pragma Assert
8284 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8285 = N_Identifier);
8286 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8287 Underlying_Record_View (Full_P));
8288 end if;
8290 Install_Private_Declarations (Par_Scope);
8291 Install_Visible_Declarations (Par_Scope);
8292 Insert_Before (N, Decl);
8294 -- Mark entity as an underlying record view before analysis,
8295 -- to avoid generating the list of its primitive operations
8296 -- (which is not really required for this entity) and thus
8297 -- prevent spurious errors associated with missing overriding
8298 -- of abstract primitives (overridden only for Derived_Type).
8300 Mutate_Ekind (Full_Der, E_Record_Type);
8301 Set_Is_Underlying_Record_View (Full_Der);
8302 Set_Default_SSO (Full_Der);
8303 Set_No_Reordering (Full_Der, No_Component_Reordering);
8305 Analyze (Decl);
8307 pragma Assert (Has_Discriminants (Full_Der)
8308 and then not Has_Unknown_Discriminants (Full_Der));
8310 Uninstall_Declarations (Par_Scope);
8312 -- Freeze the underlying record view, to prevent generation of
8313 -- useless dispatching information, which is simply shared with
8314 -- the real derived type.
8316 Set_Is_Frozen (Full_Der);
8318 -- If the derived type has access discriminants, create
8319 -- references to their anonymous types now, to prevent
8320 -- back-end problems when their first use is in generated
8321 -- bodies of primitives.
8323 declare
8324 E : Entity_Id;
8326 begin
8327 E := First_Entity (Full_Der);
8329 while Present (E) loop
8330 if Ekind (E) = E_Discriminant
8331 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8332 then
8333 Build_Itype_Reference (Etype (E), Decl);
8334 end if;
8336 Next_Entity (E);
8337 end loop;
8338 end;
8340 -- Set up links between real entity and underlying record view
8342 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8343 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8344 end;
8346 -- If discriminants are known, build derived record
8348 else
8349 Build_Derived_Record_Type
8350 (N, Parent_Type, Derived_Type, Derive_Subps);
8351 end if;
8353 return;
8355 elsif Has_Discriminants (Parent_Type) then
8357 -- Build partial view of derived type from partial view of parent.
8358 -- This must be done before building the full derivation because the
8359 -- second derivation will modify the discriminants of the first and
8360 -- the discriminants are chained with the rest of the components in
8361 -- the full derivation.
8363 Build_Derived_Record_Type
8364 (N, Parent_Type, Derived_Type, Derive_Subps);
8366 -- Build the full derivation if this is not the anonymous derived
8367 -- base type created by Build_Derived_Record_Type in the constrained
8368 -- case (see point 5. of its head comment) since we build it for the
8369 -- derived subtype.
8371 if Present (Available_Full_View (Parent_Type))
8372 and then not Is_Itype (Derived_Type)
8373 then
8374 declare
8375 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8376 Discr : Entity_Id;
8377 Last_Discr : Entity_Id;
8379 begin
8380 -- If this is not a completion, construct the implicit full
8381 -- view by deriving from the full view of the parent type.
8382 -- But if this is a completion, the derived private type
8383 -- being built is a full view and the full derivation can
8384 -- only be its underlying full view.
8386 Build_Full_Derivation;
8388 if not Is_Completion then
8389 Set_Full_View (Derived_Type, Full_Der);
8390 else
8391 Set_Underlying_Full_View (Derived_Type, Full_Der);
8392 Set_Is_Underlying_Full_View (Full_Der);
8393 end if;
8395 if not Is_Base_Type (Derived_Type) then
8396 Set_Full_View (Der_Base, Base_Type (Full_Der));
8397 end if;
8399 -- Copy the discriminant list from full view to the partial
8400 -- view (base type and its subtype). Gigi requires that the
8401 -- partial and full views have the same discriminants.
8403 -- Note that since the partial view points to discriminants
8404 -- in the full view, their scope will be that of the full
8405 -- view. This might cause some front end problems and need
8406 -- adjustment???
8408 Discr := First_Discriminant (Base_Type (Full_Der));
8409 Set_First_Entity (Der_Base, Discr);
8411 loop
8412 Last_Discr := Discr;
8413 Next_Discriminant (Discr);
8414 exit when No (Discr);
8415 end loop;
8417 Set_Last_Entity (Der_Base, Last_Discr);
8418 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8419 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8420 end;
8421 end if;
8423 elsif Present (Available_Full_View (Parent_Type))
8424 and then Has_Discriminants (Available_Full_View (Parent_Type))
8425 then
8426 if Has_Unknown_Discriminants (Parent_Type)
8427 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8428 N_Subtype_Indication
8429 then
8430 Error_Msg_N
8431 ("cannot constrain type with unknown discriminants",
8432 Subtype_Indication (Type_Definition (N)));
8433 return;
8434 end if;
8436 -- If this is not a completion, construct the implicit full view by
8437 -- deriving from the full view of the parent type. But if this is a
8438 -- completion, the derived private type being built is a full view
8439 -- and the full derivation can only be its underlying full view.
8441 Build_Full_Derivation;
8443 if not Is_Completion then
8444 Set_Full_View (Derived_Type, Full_Der);
8445 else
8446 Set_Underlying_Full_View (Derived_Type, Full_Der);
8447 Set_Is_Underlying_Full_View (Full_Der);
8448 end if;
8450 -- In any case, the primitive operations are inherited from the
8451 -- parent type, not from the internal full view.
8453 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8455 if Derive_Subps then
8456 -- Initialize the list of primitive operations to an empty list,
8457 -- to cover tagged types as well as untagged types. For untagged
8458 -- types this is used either to analyze the call as legal when
8459 -- Extensions_Allowed is True, or to issue a better error message
8460 -- otherwise.
8462 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8464 Derive_Subprograms (Parent_Type, Derived_Type);
8465 end if;
8467 Set_Stored_Constraint (Derived_Type, No_Elist);
8468 Set_Is_Constrained
8469 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8471 else
8472 -- Untagged type, No discriminants on either view
8474 if Nkind (Subtype_Indication (Type_Definition (N))) =
8475 N_Subtype_Indication
8476 then
8477 Error_Msg_N
8478 ("illegal constraint on type without discriminants", N);
8479 end if;
8481 if Present (Discriminant_Specifications (N))
8482 and then Present (Available_Full_View (Parent_Type))
8483 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8484 then
8485 Error_Msg_N ("cannot add discriminants to untagged type", N);
8486 end if;
8488 Set_Stored_Constraint (Derived_Type, No_Elist);
8489 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8491 Set_Is_Controlled_Active
8492 (Derived_Type, Is_Controlled_Active (Parent_Type));
8494 Set_Disable_Controlled
8495 (Derived_Type, Disable_Controlled (Parent_Type));
8497 Set_Has_Controlled_Component
8498 (Derived_Type, Has_Controlled_Component (Parent_Type));
8500 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8502 if not Is_Controlled (Parent_Type) then
8503 Set_Finalize_Storage_Only
8504 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8505 end if;
8507 -- If this is not a completion, construct the implicit full view by
8508 -- deriving from the full view of the parent type. But if this is a
8509 -- completion, the derived private type being built is a full view
8510 -- and the full derivation can only be its underlying full view.
8512 -- ??? If the parent type is untagged private and its completion is
8513 -- tagged, this mechanism will not work because we cannot derive from
8514 -- the tagged full view unless we have an extension.
8516 if Present (Available_Full_View (Parent_Type))
8517 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8518 and then not Error_Posted (N)
8519 then
8520 Build_Full_Derivation;
8522 if not Is_Completion then
8523 Set_Full_View (Derived_Type, Full_Der);
8524 else
8525 Set_Underlying_Full_View (Derived_Type, Full_Der);
8526 Set_Is_Underlying_Full_View (Full_Der);
8527 end if;
8528 end if;
8529 end if;
8531 Set_Has_Unknown_Discriminants (Derived_Type,
8532 Has_Unknown_Discriminants (Parent_Type));
8534 if Is_Private_Type (Derived_Type) then
8535 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8536 end if;
8538 -- If the parent base type is in scope, add the derived type to its
8539 -- list of private dependents, because its full view may become
8540 -- visible subsequently (in a nested private part, a body, or in a
8541 -- further child unit).
8543 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8544 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8546 -- Check for unusual case where a type completed by a private
8547 -- derivation occurs within a package nested in a child unit, and
8548 -- the parent is declared in an ancestor.
8550 if Is_Child_Unit (Scope (Current_Scope))
8551 and then Is_Completion
8552 and then In_Private_Part (Current_Scope)
8553 and then Scope (Parent_Type) /= Current_Scope
8555 -- Note that if the parent has a completion in the private part,
8556 -- (which is itself a derivation from some other private type)
8557 -- it is that completion that is visible, there is no full view
8558 -- available, and no special processing is needed.
8560 and then Present (Full_View (Parent_Type))
8561 then
8562 -- In this case, the full view of the parent type will become
8563 -- visible in the body of the enclosing child, and only then will
8564 -- the current type be possibly non-private. Build an underlying
8565 -- full view that will be installed when the enclosing child body
8566 -- is compiled.
8568 if Present (Underlying_Full_View (Derived_Type)) then
8569 Full_Der := Underlying_Full_View (Derived_Type);
8570 else
8571 Build_Full_Derivation;
8572 Set_Underlying_Full_View (Derived_Type, Full_Der);
8573 Set_Is_Underlying_Full_View (Full_Der);
8574 end if;
8576 -- The full view will be used to swap entities on entry/exit to
8577 -- the body, and must appear in the entity list for the package.
8579 Append_Entity (Full_Der, Scope (Derived_Type));
8580 end if;
8581 end if;
8582 end Build_Derived_Private_Type;
8584 -------------------------------
8585 -- Build_Derived_Record_Type --
8586 -------------------------------
8588 -- 1. INTRODUCTION
8590 -- Ideally we would like to use the same model of type derivation for
8591 -- tagged and untagged record types. Unfortunately this is not quite
8592 -- possible because the semantics of representation clauses is different
8593 -- for tagged and untagged records under inheritance. Consider the
8594 -- following:
8596 -- type R (...) is [tagged] record ... end record;
8597 -- type T (...) is new R (...) [with ...];
8599 -- The representation clauses for T can specify a completely different
8600 -- record layout from R's. Hence the same component can be placed in two
8601 -- very different positions in objects of type T and R. If R and T are
8602 -- tagged types, representation clauses for T can only specify the layout
8603 -- of non inherited components, thus components that are common in R and T
8604 -- have the same position in objects of type R and T.
8606 -- This has two implications. The first is that the entire tree for R's
8607 -- declaration needs to be copied for T in the untagged case, so that T
8608 -- can be viewed as a record type of its own with its own representation
8609 -- clauses. The second implication is the way we handle discriminants.
8610 -- Specifically, in the untagged case we need a way to communicate to Gigi
8611 -- what are the real discriminants in the record, while for the semantics
8612 -- we need to consider those introduced by the user to rename the
8613 -- discriminants in the parent type. This is handled by introducing the
8614 -- notion of stored discriminants. See below for more.
8616 -- Fortunately the way regular components are inherited can be handled in
8617 -- the same way in tagged and untagged types.
8619 -- To complicate things a bit more the private view of a private extension
8620 -- cannot be handled in the same way as the full view (for one thing the
8621 -- semantic rules are somewhat different). We will explain what differs
8622 -- below.
8624 -- 2. DISCRIMINANTS UNDER INHERITANCE
8626 -- The semantic rules governing the discriminants of derived types are
8627 -- quite subtle.
8629 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8630 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8632 -- If parent type has discriminants, then the discriminants that are
8633 -- declared in the derived type are [3.4 (11)]:
8635 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8636 -- there is one;
8638 -- o Otherwise, each discriminant of the parent type (implicitly declared
8639 -- in the same order with the same specifications). In this case, the
8640 -- discriminants are said to be "inherited", or if unknown in the parent
8641 -- are also unknown in the derived type.
8643 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8645 -- o The parent subtype must be constrained;
8647 -- o If the parent type is not a tagged type, then each discriminant of
8648 -- the derived type must be used in the constraint defining a parent
8649 -- subtype. [Implementation note: This ensures that the new discriminant
8650 -- can share storage with an existing discriminant.]
8652 -- For the derived type each discriminant of the parent type is either
8653 -- inherited, constrained to equal some new discriminant of the derived
8654 -- type, or constrained to the value of an expression.
8656 -- When inherited or constrained to equal some new discriminant, the
8657 -- parent discriminant and the discriminant of the derived type are said
8658 -- to "correspond".
8660 -- If a discriminant of the parent type is constrained to a specific value
8661 -- in the derived type definition, then the discriminant is said to be
8662 -- "specified" by that derived type definition.
8664 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8666 -- We have spoken about stored discriminants in point 1 (introduction)
8667 -- above. There are two sorts of stored discriminants: implicit and
8668 -- explicit. As long as the derived type inherits the same discriminants as
8669 -- the root record type, stored discriminants are the same as regular
8670 -- discriminants, and are said to be implicit. However, if any discriminant
8671 -- in the root type was renamed in the derived type, then the derived
8672 -- type will contain explicit stored discriminants. Explicit stored
8673 -- discriminants are discriminants in addition to the semantically visible
8674 -- discriminants defined for the derived type. Stored discriminants are
8675 -- used by Gigi to figure out what are the physical discriminants in
8676 -- objects of the derived type (see precise definition in einfo.ads).
8677 -- As an example, consider the following:
8679 -- type R (D1, D2, D3 : Int) is record ... end record;
8680 -- type T1 is new R;
8681 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8682 -- type T3 is new T2;
8683 -- type T4 (Y : Int) is new T3 (Y, 99);
8685 -- The following table summarizes the discriminants and stored
8686 -- discriminants in R and T1 through T4:
8688 -- Type Discrim Stored Discrim Comment
8689 -- R (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in R
8690 -- T1 (D1, D2, D3) (D1, D2, D3) Stored discrims implicit in T1
8691 -- T2 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T2
8692 -- T3 (X1, X2) (D1, D2, D3) Stored discrims EXPLICIT in T3
8693 -- T4 (Y) (D1, D2, D3) Stored discrims EXPLICIT in T4
8695 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8696 -- find the corresponding discriminant in the parent type, while
8697 -- Original_Record_Component (abbreviated ORC below) the actual physical
8698 -- component that is renamed. Finally the field Is_Completely_Hidden
8699 -- (abbreviated ICH below) is set for all explicit stored discriminants
8700 -- (see einfo.ads for more info). For the above example this gives:
8702 -- Discrim CD ORC ICH
8703 -- ^^^^^^^ ^^ ^^^ ^^^
8704 -- D1 in R empty itself no
8705 -- D2 in R empty itself no
8706 -- D3 in R empty itself no
8708 -- D1 in T1 D1 in R itself no
8709 -- D2 in T1 D2 in R itself no
8710 -- D3 in T1 D3 in R itself no
8712 -- X1 in T2 D3 in T1 D3 in T2 no
8713 -- X2 in T2 D1 in T1 D1 in T2 no
8714 -- D1 in T2 empty itself yes
8715 -- D2 in T2 empty itself yes
8716 -- D3 in T2 empty itself yes
8718 -- X1 in T3 X1 in T2 D3 in T3 no
8719 -- X2 in T3 X2 in T2 D1 in T3 no
8720 -- D1 in T3 empty itself yes
8721 -- D2 in T3 empty itself yes
8722 -- D3 in T3 empty itself yes
8724 -- Y in T4 X1 in T3 D3 in T4 no
8725 -- D1 in T4 empty itself yes
8726 -- D2 in T4 empty itself yes
8727 -- D3 in T4 empty itself yes
8729 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8731 -- Type derivation for tagged types is fairly straightforward. If no
8732 -- discriminants are specified by the derived type, these are inherited
8733 -- from the parent. No explicit stored discriminants are ever necessary.
8734 -- The only manipulation that is done to the tree is that of adding a
8735 -- _parent field with parent type and constrained to the same constraint
8736 -- specified for the parent in the derived type definition. For instance:
8738 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8739 -- type T1 is new R with null record;
8740 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8742 -- are changed into:
8744 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8745 -- _parent : R (D1, D2, D3);
8746 -- end record;
8748 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8749 -- _parent : T1 (X2, 88, X1);
8750 -- end record;
8752 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8753 -- ORC and ICH fields are:
8755 -- Discrim CD ORC ICH
8756 -- ^^^^^^^ ^^ ^^^ ^^^
8757 -- D1 in R empty itself no
8758 -- D2 in R empty itself no
8759 -- D3 in R empty itself no
8761 -- D1 in T1 D1 in R D1 in R no
8762 -- D2 in T1 D2 in R D2 in R no
8763 -- D3 in T1 D3 in R D3 in R no
8765 -- X1 in T2 D3 in T1 D3 in R no
8766 -- X2 in T2 D1 in T1 D1 in R no
8768 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8770 -- Regardless of whether we are dealing with a tagged or untagged type
8771 -- we will transform all derived type declarations of the form
8773 -- type T is new R (...) [with ...];
8774 -- or
8775 -- subtype S is R (...);
8776 -- type T is new S [with ...];
8777 -- into
8778 -- type BT is new R [with ...];
8779 -- subtype T is BT (...);
8781 -- That is, the base derived type is constrained only if it has no
8782 -- discriminants. The reason for doing this is that GNAT's semantic model
8783 -- assumes that a base type with discriminants is unconstrained.
8785 -- Note that, strictly speaking, the above transformation is not always
8786 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8788 -- procedure B34011A is
8789 -- type REC (D : integer := 0) is record
8790 -- I : Integer;
8791 -- end record;
8793 -- package P is
8794 -- type T6 is new Rec;
8795 -- function F return T6;
8796 -- end P;
8798 -- use P;
8799 -- package Q6 is
8800 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8801 -- end Q6;
8803 -- The definition of Q6.U is illegal. However transforming Q6.U into
8805 -- type BaseU is new T6;
8806 -- subtype U is BaseU (Q6.F.I)
8808 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8809 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8810 -- the transformation described above.
8812 -- There is another instance where the above transformation is incorrect.
8813 -- Consider:
8815 -- package Pack is
8816 -- type Base (D : Integer) is tagged null record;
8817 -- procedure P (X : Base);
8819 -- type Der is new Base (2) with null record;
8820 -- procedure P (X : Der);
8821 -- end Pack;
8823 -- Then the above transformation turns this into
8825 -- type Der_Base is new Base with null record;
8826 -- -- procedure P (X : Base) is implicitly inherited here
8827 -- -- as procedure P (X : Der_Base).
8829 -- subtype Der is Der_Base (2);
8830 -- procedure P (X : Der);
8831 -- -- The overriding of P (X : Der_Base) is illegal since we
8832 -- -- have a parameter conformance problem.
8834 -- To get around this problem, after having semantically processed Der_Base
8835 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8836 -- Discriminant_Constraint from Der so that when parameter conformance is
8837 -- checked when P is overridden, no semantic errors are flagged.
8839 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8841 -- Regardless of whether we are dealing with a tagged or untagged type
8842 -- we will transform all derived type declarations of the form
8844 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8845 -- type T is new R [with ...];
8846 -- into
8847 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8849 -- The reason for such transformation is that it allows us to implement a
8850 -- very clean form of component inheritance as explained below.
8852 -- Note that this transformation is not achieved by direct tree rewriting
8853 -- and manipulation, but rather by redoing the semantic actions that the
8854 -- above transformation will entail. This is done directly in routine
8855 -- Inherit_Components.
8857 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8859 -- In both tagged and untagged derived types, regular non discriminant
8860 -- components are inherited in the derived type from the parent type. In
8861 -- the absence of discriminants component, inheritance is straightforward
8862 -- as components can simply be copied from the parent.
8864 -- If the parent has discriminants, inheriting components constrained with
8865 -- these discriminants requires caution. Consider the following example:
8867 -- type R (D1, D2 : Positive) is [tagged] record
8868 -- S : String (D1 .. D2);
8869 -- end record;
8871 -- type T1 is new R [with null record];
8872 -- type T2 (X : positive) is new R (1, X) [with null record];
8874 -- As explained in 6. above, T1 is rewritten as
8875 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8876 -- which makes the treatment for T1 and T2 identical.
8878 -- What we want when inheriting S, is that references to D1 and D2 in R are
8879 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8880 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8881 -- with either discriminant references in the derived type or expressions.
8882 -- This replacement is achieved as follows: before inheriting R's
8883 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8884 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8885 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8886 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8887 -- by String (1 .. X).
8889 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8891 -- We explain here the rules governing private type extensions relevant to
8892 -- type derivation. These rules are explained on the following example:
8894 -- type D [(...)] is new A [(...)] with private; <-- partial view
8895 -- type D [(...)] is new P [(...)] with null record; <-- full view
8897 -- Type A is called the ancestor subtype of the private extension.
8898 -- Type P is the parent type of the full view of the private extension. It
8899 -- must be A or a type derived from A.
8901 -- The rules concerning the discriminants of private type extensions are
8902 -- [7.3(10-13)]:
8904 -- o If a private extension inherits known discriminants from the ancestor
8905 -- subtype, then the full view must also inherit its discriminants from
8906 -- the ancestor subtype and the parent subtype of the full view must be
8907 -- constrained if and only if the ancestor subtype is constrained.
8909 -- o If a partial view has unknown discriminants, then the full view may
8910 -- define a definite or an indefinite subtype, with or without
8911 -- discriminants.
8913 -- o If a partial view has neither known nor unknown discriminants, then
8914 -- the full view must define a definite subtype.
8916 -- o If the ancestor subtype of a private extension has constrained
8917 -- discriminants, then the parent subtype of the full view must impose a
8918 -- statically matching constraint on those discriminants.
8920 -- This means that only the following forms of private extensions are
8921 -- allowed:
8923 -- type D is new A with private; <-- partial view
8924 -- type D is new P with null record; <-- full view
8926 -- If A has no discriminants than P has no discriminants, otherwise P must
8927 -- inherit A's discriminants.
8929 -- type D is new A (...) with private; <-- partial view
8930 -- type D is new P (:::) with null record; <-- full view
8932 -- P must inherit A's discriminants and (...) and (:::) must statically
8933 -- match.
8935 -- subtype A is R (...);
8936 -- type D is new A with private; <-- partial view
8937 -- type D is new P with null record; <-- full view
8939 -- P must have inherited R's discriminants and must be derived from A or
8940 -- any of its subtypes.
8942 -- type D (..) is new A with private; <-- partial view
8943 -- type D (..) is new P [(:::)] with null record; <-- full view
8945 -- No specific constraints on P's discriminants or constraint (:::).
8946 -- Note that A can be unconstrained, but the parent subtype P must either
8947 -- be constrained or (:::) must be present.
8949 -- type D (..) is new A [(...)] with private; <-- partial view
8950 -- type D (..) is new P [(:::)] with null record; <-- full view
8952 -- P's constraints on A's discriminants must statically match those
8953 -- imposed by (...).
8955 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8957 -- The full view of a private extension is handled exactly as described
8958 -- above. The model chose for the private view of a private extension is
8959 -- the same for what concerns discriminants (i.e. they receive the same
8960 -- treatment as in the tagged case). However, the private view of the
8961 -- private extension always inherits the components of the parent base,
8962 -- without replacing any discriminant reference. Strictly speaking this is
8963 -- incorrect. However, Gigi never uses this view to generate code so this
8964 -- is a purely semantic issue. In theory, a set of transformations similar
8965 -- to those given in 5. and 6. above could be applied to private views of
8966 -- private extensions to have the same model of component inheritance as
8967 -- for non private extensions. However, this is not done because it would
8968 -- further complicate private type processing. Semantically speaking, this
8969 -- leaves us in an uncomfortable situation. As an example consider:
8971 -- package Pack is
8972 -- type R (D : integer) is tagged record
8973 -- S : String (1 .. D);
8974 -- end record;
8975 -- procedure P (X : R);
8976 -- type T is new R (1) with private;
8977 -- private
8978 -- type T is new R (1) with null record;
8979 -- end;
8981 -- This is transformed into:
8983 -- package Pack is
8984 -- type R (D : integer) is tagged record
8985 -- S : String (1 .. D);
8986 -- end record;
8987 -- procedure P (X : R);
8988 -- type T is new R (1) with private;
8989 -- private
8990 -- type BaseT is new R with null record;
8991 -- subtype T is BaseT (1);
8992 -- end;
8994 -- (strictly speaking the above is incorrect Ada)
8996 -- From the semantic standpoint the private view of private extension T
8997 -- should be flagged as constrained since one can clearly have
8999 -- Obj : T;
9001 -- in a unit withing Pack. However, when deriving subprograms for the
9002 -- private view of private extension T, T must be seen as unconstrained
9003 -- since T has discriminants (this is a constraint of the current
9004 -- subprogram derivation model). Thus, when processing the private view of
9005 -- a private extension such as T, we first mark T as unconstrained, we
9006 -- process it, we perform program derivation and just before returning from
9007 -- Build_Derived_Record_Type we mark T as constrained.
9009 -- ??? Are there are other uncomfortable cases that we will have to
9010 -- deal with.
9012 -- 10. RECORD_TYPE_WITH_PRIVATE complications
9014 -- Types that are derived from a visible record type and have a private
9015 -- extension present other peculiarities. They behave mostly like private
9016 -- types, but if they have primitive operations defined, these will not
9017 -- have the proper signatures for further inheritance, because other
9018 -- primitive operations will use the implicit base that we define for
9019 -- private derivations below. This affect subprogram inheritance (see
9020 -- Derive_Subprograms for details). We also derive the implicit base from
9021 -- the base type of the full view, so that the implicit base is a record
9022 -- type and not another private type, This avoids infinite loops.
9024 procedure Build_Derived_Record_Type
9025 (N : Node_Id;
9026 Parent_Type : Entity_Id;
9027 Derived_Type : Entity_Id;
9028 Derive_Subps : Boolean := True)
9030 Discriminant_Specs : constant Boolean :=
9031 Present (Discriminant_Specifications (N));
9032 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
9033 Loc : constant Source_Ptr := Sloc (N);
9034 Private_Extension : constant Boolean :=
9035 Nkind (N) = N_Private_Extension_Declaration;
9036 Assoc_List : Elist_Id;
9037 Constraint_Present : Boolean;
9038 Constrs : Elist_Id;
9039 Discrim : Entity_Id;
9040 Indic : Node_Id;
9041 Inherit_Discrims : Boolean := False;
9042 Last_Discrim : Entity_Id;
9043 New_Base : Entity_Id;
9044 New_Decl : Node_Id;
9045 New_Discrs : Elist_Id;
9046 New_Indic : Node_Id;
9047 Parent_Base : Entity_Id;
9048 Save_Etype : Entity_Id;
9049 Save_Discr_Constr : Elist_Id;
9050 Save_Next_Entity : Entity_Id;
9051 Type_Def : Node_Id;
9053 Discs : Elist_Id := New_Elmt_List;
9054 -- An empty Discs list means that there were no constraints in the
9055 -- subtype indication or that there was an error processing it.
9057 procedure Check_Generic_Ancestors;
9058 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
9059 -- cannot be declared at a deeper level than its parent type is
9060 -- removed. The check on derivation within a generic body is also
9061 -- relaxed, but there's a restriction that a derived tagged type
9062 -- cannot be declared in a generic body if it's derived directly
9063 -- or indirectly from a formal type of that generic. This applies
9064 -- to progenitors as well.
9066 -----------------------------
9067 -- Check_Generic_Ancestors --
9068 -----------------------------
9070 procedure Check_Generic_Ancestors is
9071 Ancestor_Type : Entity_Id;
9072 Intf_List : List_Id;
9073 Intf_Name : Node_Id;
9075 procedure Check_Ancestor;
9076 -- For parent and progenitors.
9078 --------------------
9079 -- Check_Ancestor --
9080 --------------------
9082 procedure Check_Ancestor is
9083 begin
9084 -- If the derived type does have a formal type as an ancestor
9085 -- then it's an error if the derived type is declared within
9086 -- the body of the generic unit that declares the formal type
9087 -- in its generic formal part. It's sufficient to check whether
9088 -- the ancestor type is declared inside the same generic body
9089 -- as the derived type (such as within a nested generic spec),
9090 -- in which case the derivation is legal. If the formal type is
9091 -- declared outside of that generic body, then it's certain
9092 -- that the derived type is declared within the generic body
9093 -- of the generic unit declaring the formal type.
9095 if Is_Generic_Type (Ancestor_Type)
9096 and then Enclosing_Generic_Body (Ancestor_Type) /=
9097 Enclosing_Generic_Body (Derived_Type)
9098 then
9099 Error_Msg_NE
9100 ("ancestor type& is formal type of enclosing"
9101 & " generic unit (RM 3.9.1 (4/2))",
9102 Indic, Ancestor_Type);
9103 end if;
9104 end Check_Ancestor;
9106 begin
9107 if Nkind (N) = N_Private_Extension_Declaration then
9108 Intf_List := Interface_List (N);
9109 else
9110 Intf_List := Interface_List (Type_Definition (N));
9111 end if;
9113 if Present (Enclosing_Generic_Body (Derived_Type)) then
9114 Ancestor_Type := Parent_Type;
9116 while not Is_Generic_Type (Ancestor_Type)
9117 and then Etype (Ancestor_Type) /= Ancestor_Type
9118 loop
9119 Ancestor_Type := Etype (Ancestor_Type);
9120 end loop;
9122 Check_Ancestor;
9124 if Present (Intf_List) then
9125 Intf_Name := First (Intf_List);
9126 while Present (Intf_Name) loop
9127 Ancestor_Type := Entity (Intf_Name);
9128 Check_Ancestor;
9129 Next (Intf_Name);
9130 end loop;
9131 end if;
9132 end if;
9133 end Check_Generic_Ancestors;
9135 -- Start of processing for Build_Derived_Record_Type
9137 begin
9138 -- If the parent type is a private extension with discriminants, we
9139 -- need to have an unconstrained type on which to apply the inherited
9140 -- constraint, so we get to the full view. However, this means that the
9141 -- derived type and its implicit base type created below will not point
9142 -- to the same view of their respective parent type and, thus, special
9143 -- glue code like Exp_Ch7.Convert_View is needed to bridge this gap.
9145 if Ekind (Parent_Type) = E_Record_Type_With_Private
9146 and then Has_Discriminants (Parent_Type)
9147 and then Present (Full_View (Parent_Type))
9148 then
9149 Parent_Base := Base_Type (Full_View (Parent_Type));
9150 else
9151 Parent_Base := Base_Type (Parent_Type);
9152 end if;
9154 -- If the parent type is declared as a subtype of another private
9155 -- type with inherited discriminants, its generated base type is
9156 -- itself a record subtype. To further inherit the constraint we
9157 -- need to use its own base to have an unconstrained type on which
9158 -- to apply the inherited constraint.
9160 if Ekind (Parent_Base) = E_Record_Subtype then
9161 Parent_Base := Base_Type (Parent_Base);
9162 end if;
9164 -- If the parent base is a private type and only its full view has
9165 -- discriminants, use the full view's base type.
9167 -- This can happen when we are deriving from a subtype of a derived type
9168 -- of a private type derived from a discriminated type with known
9169 -- discriminant:
9171 -- package Pkg;
9172 -- type Root_Type(I: Positive) is record
9173 -- ...
9174 -- end record;
9175 -- type Bounded_Root_Type is private;
9176 -- private
9177 -- type Bounded_Root_Type is new Root_Type(10);
9178 -- end Pkg;
9180 -- package Pkg2 is
9181 -- type Constrained_Root_Type is new Pkg.Bounded_Root_Type;
9182 -- end Pkg2;
9183 -- subtype Sub_Base is Pkg2.Constrained_Root_Type;
9184 -- type New_Der_Type is new Sub_Base;
9186 if Is_Private_Type (Parent_Base)
9187 and then Present (Full_View (Parent_Base))
9188 and then not Has_Discriminants (Parent_Base)
9189 and then Has_Discriminants (Full_View (Parent_Base))
9190 then
9191 Parent_Base := Base_Type (Full_View (Parent_Base));
9192 end if;
9194 -- AI05-0115: if this is a derivation from a private type in some
9195 -- other scope that may lead to invisible components for the derived
9196 -- type, mark it accordingly.
9198 if Is_Private_Type (Parent_Type) then
9199 if Scope (Parent_Base) = Scope (Derived_Type) then
9200 null;
9202 elsif In_Open_Scopes (Scope (Parent_Base))
9203 and then In_Private_Part (Scope (Parent_Base))
9204 then
9205 null;
9207 else
9208 Set_Has_Private_Ancestor (Derived_Type);
9209 end if;
9211 else
9212 Set_Has_Private_Ancestor
9213 (Derived_Type, Has_Private_Ancestor (Parent_Type));
9214 end if;
9216 -- Before we start the previously documented transformations, here is
9217 -- little fix for size and alignment of tagged types. Normally when we
9218 -- derive type D from type P, we copy the size and alignment of P as the
9219 -- default for D, and in the absence of explicit representation clauses
9220 -- for D, the size and alignment are indeed the same as the parent.
9222 -- But this is wrong for tagged types, since fields may be added, and
9223 -- the default size may need to be larger, and the default alignment may
9224 -- need to be larger.
9226 -- We therefore reset the size and alignment fields in the tagged case.
9227 -- Note that the size and alignment will in any case be at least as
9228 -- large as the parent type (since the derived type has a copy of the
9229 -- parent type in the _parent field)
9231 -- The type is also marked as being tagged here, which is needed when
9232 -- processing components with a self-referential anonymous access type
9233 -- in the call to Check_Anonymous_Access_Components below. Note that
9234 -- this flag is also set later on for completeness.
9236 if Is_Tagged then
9237 Set_Is_Tagged_Type (Derived_Type);
9238 Reinit_Size_Align (Derived_Type);
9239 end if;
9241 -- STEP 0a: figure out what kind of derived type declaration we have
9243 if Private_Extension then
9244 Type_Def := N;
9245 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
9246 Set_Default_SSO (Derived_Type);
9247 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9249 else
9250 Type_Def := Type_Definition (N);
9252 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9253 -- Parent_Base can be a private type or private extension. However,
9254 -- for tagged types with an extension the newly added fields are
9255 -- visible and hence the Derived_Type is always an E_Record_Type.
9256 -- (except that the parent may have its own private fields).
9257 -- For untagged types we preserve the Ekind of the Parent_Base.
9259 if Present (Record_Extension_Part (Type_Def)) then
9260 Mutate_Ekind (Derived_Type, E_Record_Type);
9261 Set_Default_SSO (Derived_Type);
9262 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9264 -- Create internal access types for components with anonymous
9265 -- access types.
9267 if Ada_Version >= Ada_2005 then
9268 Check_Anonymous_Access_Components
9269 (N, Derived_Type, Derived_Type,
9270 Component_List (Record_Extension_Part (Type_Def)));
9271 end if;
9273 else
9274 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9275 end if;
9276 end if;
9278 -- Indic can either be an N_Identifier if the subtype indication
9279 -- contains no constraint or an N_Subtype_Indication if the subtype
9280 -- indication has a constraint. In either case it can include an
9281 -- interface list.
9283 Indic := Subtype_Indication (Type_Def);
9284 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9286 -- Check that the type has visible discriminants. The type may be
9287 -- a private type with unknown discriminants whose full view has
9288 -- discriminants which are invisible.
9290 if Constraint_Present then
9291 if not Has_Discriminants (Parent_Base)
9292 or else
9293 (Has_Unknown_Discriminants (Parent_Base)
9294 and then Is_Private_Type (Parent_Base))
9295 then
9296 Error_Msg_N
9297 ("invalid constraint: type has no discriminant",
9298 Constraint (Indic));
9300 Constraint_Present := False;
9301 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9303 elsif Is_Constrained (Parent_Type) then
9304 Error_Msg_N
9305 ("invalid constraint: parent type is already constrained",
9306 Constraint (Indic));
9308 Constraint_Present := False;
9309 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9310 end if;
9311 end if;
9313 -- STEP 0b: If needed, apply transformation given in point 5. above
9315 if not Private_Extension
9316 and then Has_Discriminants (Parent_Type)
9317 and then not Discriminant_Specs
9318 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9319 then
9320 -- First, we must analyze the constraint (see comment in point 5.)
9321 -- The constraint may come from the subtype indication of the full
9322 -- declaration. Temporarily set the state of the Derived_Type to
9323 -- "self-hidden" (see RM-8.3(17)).
9325 if Constraint_Present then
9326 pragma Assert (Is_Not_Self_Hidden (Derived_Type));
9327 Set_Is_Not_Self_Hidden (Derived_Type, False);
9328 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9329 Set_Is_Not_Self_Hidden (Derived_Type);
9331 -- If there is no explicit constraint, there might be one that is
9332 -- inherited from a constrained parent type. In that case verify that
9333 -- it conforms to the constraint in the partial view. In perverse
9334 -- cases the parent subtypes of the partial and full view can have
9335 -- different constraints.
9337 elsif Present (Stored_Constraint (Parent_Type)) then
9338 New_Discrs := Stored_Constraint (Parent_Type);
9340 else
9341 New_Discrs := No_Elist;
9342 end if;
9344 if Has_Discriminants (Derived_Type)
9345 and then Has_Private_Declaration (Derived_Type)
9346 and then Present (Discriminant_Constraint (Derived_Type))
9347 and then Present (New_Discrs)
9348 then
9349 -- Verify that constraints of the full view statically match
9350 -- those given in the partial view.
9352 declare
9353 C1, C2 : Elmt_Id;
9355 begin
9356 C1 := First_Elmt (New_Discrs);
9357 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9358 while Present (C1) and then Present (C2) loop
9359 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9360 or else
9361 (Is_OK_Static_Expression (Node (C1))
9362 and then Is_OK_Static_Expression (Node (C2))
9363 and then
9364 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9365 then
9366 null;
9368 else
9369 if Constraint_Present then
9370 Error_Msg_N
9371 ("constraint not conformant to previous declaration",
9372 Node (C1));
9373 else
9374 Error_Msg_N
9375 ("constraint of full view is incompatible "
9376 & "with partial view", N);
9377 end if;
9378 end if;
9380 Next_Elmt (C1);
9381 Next_Elmt (C2);
9382 end loop;
9383 end;
9384 end if;
9386 -- Insert and analyze the declaration for the unconstrained base type
9388 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9390 New_Decl :=
9391 Make_Full_Type_Declaration (Loc,
9392 Defining_Identifier => New_Base,
9393 Type_Definition =>
9394 Make_Derived_Type_Definition (Loc,
9395 Abstract_Present => Abstract_Present (Type_Def),
9396 Limited_Present => Limited_Present (Type_Def),
9397 Subtype_Indication =>
9398 New_Occurrence_Of (Parent_Base, Loc),
9399 Record_Extension_Part =>
9400 Relocate_Node (Record_Extension_Part (Type_Def)),
9401 Interface_List => Interface_List (Type_Def)));
9403 Set_Parent (New_Decl, Parent (N));
9404 Mark_Rewrite_Insertion (New_Decl);
9405 Insert_Before (N, New_Decl);
9407 -- In the extension case, make sure ancestor is frozen appropriately
9408 -- (see also non-discriminated case below).
9410 if Present (Record_Extension_Part (Type_Def))
9411 or else Is_Interface (Parent_Base)
9412 then
9413 Freeze_Before (New_Decl, Parent_Type);
9414 end if;
9416 -- Note that this call passes False for the Derive_Subps parameter
9417 -- because subprogram derivation is deferred until after creating
9418 -- the subtype (see below).
9420 Build_Derived_Type
9421 (New_Decl, Parent_Base, New_Base,
9422 Is_Completion => False, Derive_Subps => False);
9424 -- ??? This needs re-examination to determine whether the
9425 -- following call can simply be replaced by a call to Analyze.
9427 Set_Analyzed (New_Decl);
9429 -- Insert and analyze the declaration for the constrained subtype
9431 if Constraint_Present then
9432 New_Indic :=
9433 Make_Subtype_Indication (Loc,
9434 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9435 Constraint => Relocate_Node (Constraint (Indic)));
9437 else
9438 declare
9439 Constr_List : constant List_Id := New_List;
9440 C : Elmt_Id;
9441 Expr : Node_Id;
9443 begin
9444 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9445 while Present (C) loop
9446 Expr := Node (C);
9448 -- It is safe here to call New_Copy_Tree since we called
9449 -- Force_Evaluation on each constraint previously
9450 -- in Build_Discriminant_Constraints.
9452 Append (New_Copy_Tree (Expr), To => Constr_List);
9454 Next_Elmt (C);
9455 end loop;
9457 New_Indic :=
9458 Make_Subtype_Indication (Loc,
9459 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9460 Constraint =>
9461 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9462 end;
9463 end if;
9465 Rewrite (N,
9466 Make_Subtype_Declaration (Loc,
9467 Defining_Identifier => Derived_Type,
9468 Subtype_Indication => New_Indic));
9470 Analyze (N);
9472 -- Derivation of subprograms must be delayed until the full subtype
9473 -- has been established, to ensure proper overriding of subprograms
9474 -- inherited by full types. If the derivations occurred as part of
9475 -- the call to Build_Derived_Type above, then the check for type
9476 -- conformance would fail because earlier primitive subprograms
9477 -- could still refer to the full type prior the change to the new
9478 -- subtype and hence would not match the new base type created here.
9479 -- Subprograms are not derived, however, when Derive_Subps is False
9480 -- (since otherwise there could be redundant derivations).
9482 if Derive_Subps then
9483 Derive_Subprograms (Parent_Type, Derived_Type);
9484 end if;
9486 -- For tagged types the Discriminant_Constraint of the new base itype
9487 -- is inherited from the first subtype so that no subtype conformance
9488 -- problem arise when the first subtype overrides primitive
9489 -- operations inherited by the implicit base type.
9491 if Is_Tagged then
9492 Set_Discriminant_Constraint
9493 (New_Base, Discriminant_Constraint (Derived_Type));
9494 end if;
9496 return;
9497 end if;
9499 -- If we get here Derived_Type will have no discriminants or it will be
9500 -- a discriminated unconstrained base type.
9502 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9504 if Is_Tagged then
9506 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9507 -- The declaration of a specific descendant of an interface type
9508 -- freezes the interface type (RM 13.14).
9510 if not Private_Extension or else Is_Interface (Parent_Base) then
9511 Freeze_Before (N, Parent_Type);
9512 end if;
9514 if Ada_Version >= Ada_2005 then
9515 Check_Generic_Ancestors;
9517 elsif Type_Access_Level (Derived_Type) /=
9518 Type_Access_Level (Parent_Type)
9519 and then not Is_Generic_Type (Derived_Type)
9520 then
9521 if Is_Controlled (Parent_Type) then
9522 Error_Msg_N
9523 ("controlled type must be declared at the library level",
9524 Indic);
9525 else
9526 Error_Msg_N
9527 ("type extension at deeper accessibility level than parent",
9528 Indic);
9529 end if;
9531 else
9532 declare
9533 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9534 begin
9535 if Present (GB)
9536 and then GB /= Enclosing_Generic_Body (Parent_Base)
9537 then
9538 Error_Msg_NE
9539 ("parent type of& must not be outside generic body"
9540 & " (RM 3.9.1(4))",
9541 Indic, Derived_Type);
9542 end if;
9543 end;
9544 end if;
9545 end if;
9547 -- Ada 2005 (AI-251)
9549 if Ada_Version >= Ada_2005 and then Is_Tagged then
9551 -- "The declaration of a specific descendant of an interface type
9552 -- freezes the interface type" (RM 13.14).
9554 declare
9555 Iface : Node_Id;
9556 begin
9557 Iface := First (Interface_List (Type_Def));
9558 while Present (Iface) loop
9559 Freeze_Before (N, Etype (Iface));
9560 Next (Iface);
9561 end loop;
9562 end;
9563 end if;
9565 -- STEP 1b : preliminary cleanup of the full view of private types
9567 -- If the type is already marked as having discriminants, then it's the
9568 -- completion of a private type or private extension and we need to
9569 -- retain the discriminants from the partial view if the current
9570 -- declaration has Discriminant_Specifications so that we can verify
9571 -- conformance. However, we must remove any existing components that
9572 -- were inherited from the parent (and attached in Copy_And_Swap)
9573 -- because the full type inherits all appropriate components anyway, and
9574 -- we do not want the partial view's components interfering.
9576 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9577 Discrim := First_Discriminant (Derived_Type);
9578 loop
9579 Last_Discrim := Discrim;
9580 Next_Discriminant (Discrim);
9581 exit when No (Discrim);
9582 end loop;
9584 Set_Last_Entity (Derived_Type, Last_Discrim);
9586 -- In all other cases wipe out the list of inherited components (even
9587 -- inherited discriminants), it will be properly rebuilt here.
9589 else
9590 Set_First_Entity (Derived_Type, Empty);
9591 Set_Last_Entity (Derived_Type, Empty);
9592 end if;
9594 -- STEP 1c: Initialize some flags for the Derived_Type
9596 -- The following flags must be initialized here so that
9597 -- Process_Discriminants can check that discriminants of tagged types do
9598 -- not have a default initial value and that access discriminants are
9599 -- only specified for limited records. For completeness, these flags are
9600 -- also initialized along with all the other flags below.
9602 -- AI-419: Limitedness is not inherited from an interface parent, so to
9603 -- be limited in that case the type must be explicitly declared as
9604 -- limited, or synchronized. While task and protected interfaces are
9605 -- always limited, a synchronized private extension might not inherit
9606 -- from such interfaces, and so we also need to recognize the
9607 -- explicit limitedness implied by a synchronized private extension
9608 -- that does not derive from a synchronized interface (see RM-7.3(6/2)).
9610 if Limited_Present (Type_Def)
9611 or else Synchronized_Present (Type_Def)
9612 then
9613 Set_Is_Limited_Record (Derived_Type);
9615 elsif Is_Limited_Record (Parent_Type)
9616 or else (Present (Full_View (Parent_Type))
9617 and then Is_Limited_Record (Full_View (Parent_Type)))
9618 then
9619 if not Is_Interface (Parent_Type)
9620 or else Is_Concurrent_Interface (Parent_Type)
9621 then
9622 Set_Is_Limited_Record (Derived_Type);
9623 end if;
9624 end if;
9626 -- STEP 2a: process discriminants of derived type if any
9628 Push_Scope (Derived_Type);
9630 if Discriminant_Specs then
9631 Set_Has_Unknown_Discriminants (Derived_Type, False);
9633 -- The following call to Check_Or_Process_Discriminants initializes
9634 -- fields Has_Discriminants and Discriminant_Constraint, unless we
9635 -- are processing the completion of a private type declaration.
9636 -- Temporarily set the state of the Derived_Type to "self-hidden"
9637 -- (see RM-8.3(17)), unless it is already the case.
9639 if Is_Not_Self_Hidden (Derived_Type) then
9640 Set_Is_Not_Self_Hidden (Derived_Type, False);
9641 Check_Or_Process_Discriminants (N, Derived_Type);
9642 Set_Is_Not_Self_Hidden (Derived_Type);
9643 else
9644 Check_Or_Process_Discriminants (N, Derived_Type);
9645 end if;
9647 -- For untagged types, the constraint on the Parent_Type must be
9648 -- present and is used to rename the discriminants.
9650 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9651 Error_Msg_N ("untagged parent must have discriminants", Indic);
9653 elsif not Is_Tagged and then not Constraint_Present then
9654 Error_Msg_N
9655 ("discriminant constraint needed for derived untagged records",
9656 Indic);
9658 -- Otherwise the parent subtype must be constrained unless we have a
9659 -- private extension.
9661 elsif not Constraint_Present
9662 and then not Private_Extension
9663 and then not Is_Constrained (Parent_Type)
9664 then
9665 Error_Msg_N
9666 ("unconstrained type not allowed in this context", Indic);
9668 elsif Constraint_Present then
9669 -- The following call sets the field Corresponding_Discriminant
9670 -- for the discriminants in the Derived_Type.
9672 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9674 -- For untagged types all new discriminants must rename
9675 -- discriminants in the parent. For private extensions new
9676 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9678 Discrim := First_Discriminant (Derived_Type);
9679 while Present (Discrim) loop
9680 if not Is_Tagged
9681 and then No (Corresponding_Discriminant (Discrim))
9682 then
9683 Error_Msg_N
9684 ("new discriminants must constrain old ones", Discrim);
9686 elsif Private_Extension
9687 and then Present (Corresponding_Discriminant (Discrim))
9688 then
9689 Error_Msg_N
9690 ("only static constraints allowed for parent"
9691 & " discriminants in the partial view", Indic);
9692 exit;
9693 end if;
9695 -- If a new discriminant is used in the constraint, then its
9696 -- subtype must be statically compatible with the subtype of
9697 -- the parent discriminant (RM 3.7(15)).
9699 if Present (Corresponding_Discriminant (Discrim)) then
9700 Check_Constraining_Discriminant
9701 (Discrim, Corresponding_Discriminant (Discrim));
9702 end if;
9704 Next_Discriminant (Discrim);
9705 end loop;
9707 -- Check whether the constraints of the full view statically
9708 -- match those imposed by the parent subtype [7.3(13)].
9710 if Present (Stored_Constraint (Derived_Type)) then
9711 declare
9712 C1, C2 : Elmt_Id;
9714 begin
9715 C1 := First_Elmt (Discs);
9716 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9717 while Present (C1) and then Present (C2) loop
9718 if not
9719 Fully_Conformant_Expressions (Node (C1), Node (C2))
9720 then
9721 Error_Msg_N
9722 ("not conformant with previous declaration",
9723 Node (C1));
9724 end if;
9726 Next_Elmt (C1);
9727 Next_Elmt (C2);
9728 end loop;
9729 end;
9730 end if;
9731 end if;
9733 -- STEP 2b: No new discriminants, inherit discriminants if any
9735 else
9736 if Private_Extension then
9737 Set_Has_Unknown_Discriminants
9738 (Derived_Type,
9739 Has_Unknown_Discriminants (Parent_Type)
9740 or else Unknown_Discriminants_Present (N));
9742 -- The partial view of the parent may have unknown discriminants,
9743 -- but if the full view has discriminants and the parent type is
9744 -- in scope they must be inherited.
9746 elsif Has_Unknown_Discriminants (Parent_Type)
9747 and then
9748 (not Has_Discriminants (Parent_Type)
9749 or else not In_Open_Scopes (Scope (Parent_Base)))
9750 then
9751 Set_Has_Unknown_Discriminants (Derived_Type);
9752 end if;
9754 if not Has_Unknown_Discriminants (Derived_Type)
9755 and then not Has_Unknown_Discriminants (Parent_Base)
9756 and then Has_Discriminants (Parent_Type)
9757 then
9758 Inherit_Discrims := True;
9759 Set_Has_Discriminants
9760 (Derived_Type, True);
9761 Set_Discriminant_Constraint
9762 (Derived_Type, Discriminant_Constraint (Parent_Base));
9763 end if;
9765 -- The following test is true for private types (remember
9766 -- transformation 5. is not applied to those) and in an error
9767 -- situation.
9769 if Constraint_Present then
9770 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9771 end if;
9773 -- For now mark a new derived type as constrained only if it has no
9774 -- discriminants. At the end of Build_Derived_Record_Type we properly
9775 -- set this flag in the case of private extensions. See comments in
9776 -- point 9. just before body of Build_Derived_Record_Type.
9778 Set_Is_Constrained
9779 (Derived_Type,
9780 not (Inherit_Discrims
9781 or else Has_Unknown_Discriminants (Derived_Type)));
9782 end if;
9784 -- STEP 3: initialize fields of derived type
9786 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9787 Set_Stored_Constraint (Derived_Type, No_Elist);
9789 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9790 -- but cannot be interfaces
9792 if not Private_Extension
9793 and then Ekind (Derived_Type) /= E_Private_Type
9794 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9795 then
9796 if Interface_Present (Type_Def) then
9797 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9798 end if;
9800 Set_Interfaces (Derived_Type, No_Elist);
9801 end if;
9803 -- Fields inherited from the Parent_Type
9805 Set_Has_Specified_Layout
9806 (Derived_Type, Has_Specified_Layout (Parent_Type));
9807 Set_Is_Limited_Composite
9808 (Derived_Type, Is_Limited_Composite (Parent_Type));
9809 Set_Is_Private_Composite
9810 (Derived_Type, Is_Private_Composite (Parent_Type));
9812 if Is_Tagged_Type (Parent_Type) then
9813 Set_No_Tagged_Streams_Pragma
9814 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9815 end if;
9817 -- Fields inherited from the Parent_Base
9819 Set_Has_Controlled_Component
9820 (Derived_Type, Has_Controlled_Component (Parent_Base));
9821 Set_Has_Non_Standard_Rep
9822 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9823 Set_Has_Primitive_Operations
9824 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9826 -- Set fields for private derived types
9828 if Is_Private_Type (Derived_Type) then
9829 Set_Depends_On_Private (Derived_Type, True);
9830 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9831 end if;
9833 -- Inherit fields for non-private types. If this is the completion of a
9834 -- derivation from a private type, the parent itself is private and the
9835 -- attributes come from its full view, which must be present.
9837 if Is_Record_Type (Derived_Type) then
9838 declare
9839 Parent_Full : Entity_Id;
9841 begin
9842 if Is_Private_Type (Parent_Base)
9843 and then not Is_Record_Type (Parent_Base)
9844 then
9845 Parent_Full := Full_View (Parent_Base);
9846 else
9847 Parent_Full := Parent_Base;
9848 end if;
9850 Set_Component_Alignment
9851 (Derived_Type, Component_Alignment (Parent_Full));
9852 Set_C_Pass_By_Copy
9853 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9854 Set_Has_Complex_Representation
9855 (Derived_Type, Has_Complex_Representation (Parent_Full));
9857 -- For untagged types, inherit the layout by default to avoid
9858 -- costly changes of representation for type conversions.
9860 if not Is_Tagged then
9861 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9862 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9863 end if;
9864 end;
9865 end if;
9867 -- Initialize the list of primitive operations to an empty list,
9868 -- to cover tagged types as well as untagged types. For untagged
9869 -- types this is used either to analyze the call as legal when
9870 -- Extensions_Allowed is True, or to issue a better error message
9871 -- otherwise.
9873 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9875 -- Set fields for tagged types
9877 if Is_Tagged then
9878 -- All tagged types defined in Ada.Finalization are controlled
9880 if Chars (Scope (Derived_Type)) = Name_Finalization
9881 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9882 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9883 then
9884 Set_Is_Controlled_Active (Derived_Type);
9885 else
9886 Set_Is_Controlled_Active
9887 (Derived_Type, Is_Controlled_Active (Parent_Base));
9888 end if;
9890 -- Minor optimization: there is no need to generate the class-wide
9891 -- entity associated with an underlying record view.
9893 if not Is_Underlying_Record_View (Derived_Type) then
9894 Make_Class_Wide_Type (Derived_Type);
9895 end if;
9897 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9899 if Has_Discriminants (Derived_Type)
9900 and then Constraint_Present
9901 then
9902 Set_Stored_Constraint
9903 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9904 end if;
9906 if Ada_Version >= Ada_2005 then
9907 declare
9908 Ifaces_List : Elist_Id;
9910 begin
9911 -- Checks rules 3.9.4 (13/2 and 14/2)
9913 if Comes_From_Source (Derived_Type)
9914 and then not Is_Private_Type (Derived_Type)
9915 and then Is_Interface (Parent_Type)
9916 and then not Is_Interface (Derived_Type)
9917 then
9918 if Is_Task_Interface (Parent_Type) then
9919 Error_Msg_N
9920 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9921 Derived_Type);
9923 elsif Is_Protected_Interface (Parent_Type) then
9924 Error_Msg_N
9925 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9926 Derived_Type);
9927 end if;
9928 end if;
9930 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9932 Check_Interfaces (N, Type_Def);
9934 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9935 -- not already in the parents.
9937 Collect_Interfaces
9938 (T => Derived_Type,
9939 Ifaces_List => Ifaces_List,
9940 Exclude_Parents => True);
9942 Set_Interfaces (Derived_Type, Ifaces_List);
9944 -- If the derived type is the anonymous type created for
9945 -- a declaration whose parent has a constraint, propagate
9946 -- the interface list to the source type. This must be done
9947 -- prior to the completion of the analysis of the source type
9948 -- because the components in the extension may contain current
9949 -- instances whose legality depends on some ancestor.
9951 if Is_Itype (Derived_Type) then
9952 declare
9953 Def : constant Node_Id :=
9954 Associated_Node_For_Itype (Derived_Type);
9955 begin
9956 if Present (Def)
9957 and then Nkind (Def) = N_Full_Type_Declaration
9958 then
9959 Set_Interfaces
9960 (Defining_Identifier (Def), Ifaces_List);
9961 end if;
9962 end;
9963 end if;
9965 -- A type extension is automatically Ghost when one of its
9966 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9967 -- also inherited when the parent type is Ghost, but this is
9968 -- done in Build_Derived_Type as the mechanism also handles
9969 -- untagged derivations.
9971 if Implements_Ghost_Interface (Derived_Type) then
9972 Set_Is_Ghost_Entity (Derived_Type);
9973 end if;
9974 end;
9975 end if;
9976 end if;
9978 -- STEP 4: Inherit components from the parent base and constrain them.
9979 -- Apply the second transformation described in point 6. above.
9981 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9982 or else not Has_Discriminants (Parent_Type)
9983 or else not Is_Constrained (Parent_Type)
9984 then
9985 Constrs := Discs;
9986 else
9987 Constrs := Discriminant_Constraint (Parent_Type);
9988 end if;
9990 Assoc_List :=
9991 Inherit_Components
9992 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9994 -- STEP 5a: Copy the parent record declaration for untagged types
9996 Set_Has_Implicit_Dereference
9997 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9999 if not Is_Tagged then
10001 -- Discriminant_Constraint (Derived_Type) has been properly
10002 -- constructed. Save it and temporarily set it to Empty because we
10003 -- do not want the call to New_Copy_Tree below to mess this list.
10005 if Has_Discriminants (Derived_Type) then
10006 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
10007 Set_Discriminant_Constraint (Derived_Type, No_Elist);
10008 else
10009 Save_Discr_Constr := No_Elist;
10010 end if;
10012 -- Save the Etype field of Derived_Type. It is correctly set now,
10013 -- but the call to New_Copy tree may remap it to point to itself,
10014 -- which is not what we want. Ditto for the Next_Entity field.
10016 Save_Etype := Etype (Derived_Type);
10017 Save_Next_Entity := Next_Entity (Derived_Type);
10019 -- Assoc_List maps all stored discriminants in the Parent_Base to
10020 -- stored discriminants in the Derived_Type. It is fundamental that
10021 -- no types or itypes with discriminants other than the stored
10022 -- discriminants appear in the entities declared inside
10023 -- Derived_Type, since the back end cannot deal with it.
10025 New_Decl :=
10026 New_Copy_Tree
10027 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
10028 Copy_Dimensions_Of_Components (Derived_Type);
10030 -- Restore the fields saved prior to the New_Copy_Tree call
10031 -- and compute the stored constraint.
10033 Set_Etype (Derived_Type, Save_Etype);
10034 Link_Entities (Derived_Type, Save_Next_Entity);
10036 if Has_Discriminants (Derived_Type) then
10037 Set_Discriminant_Constraint
10038 (Derived_Type, Save_Discr_Constr);
10039 Set_Stored_Constraint
10040 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
10042 Replace_Discriminants (Derived_Type, New_Decl);
10043 end if;
10045 -- Insert the new derived type declaration
10047 Rewrite (N, New_Decl);
10049 -- STEP 5b: Complete the processing for record extensions in generics
10051 -- There is no completion for record extensions declared in the
10052 -- parameter part of a generic, so we need to complete processing for
10053 -- these generic record extensions here. Record_Type_Definition will
10054 -- set the Is_Not_Self_Hidden flag.
10056 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
10057 Record_Type_Definition (Empty, Derived_Type);
10059 -- STEP 5c: Process the record extension for non private tagged types
10061 elsif not Private_Extension then
10062 Expand_Record_Extension (Derived_Type, Type_Def);
10064 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
10065 -- implemented interfaces if we are in expansion mode
10067 if Expander_Active
10068 and then Has_Interfaces (Derived_Type)
10069 then
10070 Add_Interface_Tag_Components (N, Derived_Type);
10071 end if;
10073 -- Analyze the record extension
10075 Record_Type_Definition
10076 (Record_Extension_Part (Type_Def), Derived_Type);
10077 end if;
10079 End_Scope;
10081 -- Nothing else to do if there is an error in the derivation.
10082 -- An unusual case: the full view may be derived from a type in an
10083 -- instance, when the partial view was used illegally as an actual
10084 -- in that instance, leading to a circular definition.
10086 if Etype (Derived_Type) = Any_Type
10087 or else Etype (Parent_Type) = Derived_Type
10088 then
10089 return;
10090 end if;
10092 -- Set delayed freeze and then derive subprograms, we need to do
10093 -- this in this order so that derived subprograms inherit the
10094 -- derived freeze if necessary.
10096 Set_Has_Delayed_Freeze (Derived_Type);
10098 if Derive_Subps then
10099 Derive_Subprograms (Parent_Type, Derived_Type);
10100 end if;
10102 -- If we have a private extension which defines a constrained derived
10103 -- type mark as constrained here after we have derived subprograms. See
10104 -- comment on point 9. just above the body of Build_Derived_Record_Type.
10106 if Private_Extension and then Inherit_Discrims then
10107 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
10108 Set_Is_Constrained (Derived_Type, True);
10109 Set_Discriminant_Constraint (Derived_Type, Discs);
10111 elsif Is_Constrained (Parent_Type) then
10112 Set_Is_Constrained
10113 (Derived_Type, True);
10114 Set_Discriminant_Constraint
10115 (Derived_Type, Discriminant_Constraint (Parent_Type));
10116 end if;
10117 end if;
10119 -- Update the class-wide type, which shares the now-completed entity
10120 -- list with its specific type. In case of underlying record views,
10121 -- we do not generate the corresponding class wide entity.
10123 if Is_Tagged
10124 and then not Is_Underlying_Record_View (Derived_Type)
10125 then
10126 Set_First_Entity
10127 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
10128 Set_Last_Entity
10129 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
10130 end if;
10132 Check_Function_Writable_Actuals (N);
10133 end Build_Derived_Record_Type;
10135 ------------------------
10136 -- Build_Derived_Type --
10137 ------------------------
10139 procedure Build_Derived_Type
10140 (N : Node_Id;
10141 Parent_Type : Entity_Id;
10142 Derived_Type : Entity_Id;
10143 Is_Completion : Boolean;
10144 Derive_Subps : Boolean := True)
10146 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
10148 begin
10149 -- Set common attributes
10151 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
10152 and then Ekind (Parent_Base) in Elementary_Kind
10153 then
10154 Reinit_Field_To_Zero (Derived_Type, F_Discriminant_Constraint);
10155 end if;
10157 Set_Scope (Derived_Type, Current_Scope);
10158 Set_Etype (Derived_Type, Parent_Base);
10159 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
10160 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
10162 Set_Size_Info (Derived_Type, Parent_Type);
10163 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
10165 Set_Is_Controlled_Active
10166 (Derived_Type, Is_Controlled_Active (Parent_Type));
10168 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
10169 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
10170 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
10172 if Is_Tagged_Type (Derived_Type) then
10173 Set_No_Tagged_Streams_Pragma
10174 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
10175 end if;
10177 -- If the parent has primitive routines and may have not-seen-yet aspect
10178 -- specifications (e.g., a Pack pragma), then set the derived type link
10179 -- in order to later diagnose "early derivation" issues. If in different
10180 -- compilation units, then "early derivation" cannot be an issue (and we
10181 -- don't like interunit references that go in the opposite direction of
10182 -- semantic dependencies).
10184 if Has_Primitive_Operations (Parent_Type)
10185 and then Enclosing_Comp_Unit_Node (Parent_Type) =
10186 Enclosing_Comp_Unit_Node (Derived_Type)
10187 then
10188 Set_Derived_Type_Link (Parent_Base, Derived_Type);
10189 end if;
10191 -- If the parent type is a private subtype, the convention on the base
10192 -- type may be set in the private part, and not propagated to the
10193 -- subtype until later, so we obtain the convention from the base type.
10195 Set_Convention (Derived_Type, Convention (Parent_Base));
10197 if Is_Tagged_Type (Derived_Type)
10198 and then Present (Class_Wide_Type (Derived_Type))
10199 then
10200 Set_Convention (Class_Wide_Type (Derived_Type),
10201 Convention (Class_Wide_Type (Parent_Base)));
10202 end if;
10204 -- Set SSO default for record or array type
10206 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
10207 and then Is_Base_Type (Derived_Type)
10208 then
10209 Set_Default_SSO (Derived_Type);
10210 end if;
10212 -- A derived type inherits the Default_Initial_Condition pragma coming
10213 -- from any parent type within the derivation chain.
10215 if Has_DIC (Parent_Type) then
10216 Set_Has_Inherited_DIC (Derived_Type);
10217 end if;
10219 -- A derived type inherits any class-wide invariants coming from a
10220 -- parent type or an interface. Note that the invariant procedure of
10221 -- the parent type should not be inherited because the derived type may
10222 -- define invariants of its own.
10224 if not Is_Interface (Derived_Type) then
10225 if Has_Inherited_Invariants (Parent_Type)
10226 or else Has_Inheritable_Invariants (Parent_Type)
10227 then
10228 Set_Has_Inherited_Invariants (Derived_Type);
10230 elsif Is_Concurrent_Type (Derived_Type)
10231 or else Is_Tagged_Type (Derived_Type)
10232 then
10233 declare
10234 Iface : Entity_Id;
10235 Ifaces : Elist_Id;
10236 Iface_Elmt : Elmt_Id;
10238 begin
10239 Collect_Interfaces
10240 (T => Derived_Type,
10241 Ifaces_List => Ifaces,
10242 Exclude_Parents => True);
10244 if Present (Ifaces) then
10245 Iface_Elmt := First_Elmt (Ifaces);
10246 while Present (Iface_Elmt) loop
10247 Iface := Node (Iface_Elmt);
10249 if Has_Inheritable_Invariants (Iface) then
10250 Set_Has_Inherited_Invariants (Derived_Type);
10251 exit;
10252 end if;
10254 Next_Elmt (Iface_Elmt);
10255 end loop;
10256 end if;
10257 end;
10258 end if;
10259 end if;
10261 -- We similarly inherit predicates
10263 Inherit_Predicate_Flags (Derived_Type, Parent_Type, Only_Flags => True);
10265 -- The derived type inherits representation clauses from the parent
10266 -- type, and from any interfaces.
10268 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10270 declare
10271 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10272 begin
10273 while Present (Iface) loop
10274 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10275 Next (Iface);
10276 end loop;
10277 end;
10279 -- If the parent type has delayed rep aspects, then mark the derived
10280 -- type as possibly inheriting a delayed rep aspect.
10282 if Has_Delayed_Rep_Aspects (Parent_Type) then
10283 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10284 end if;
10286 -- A derived type becomes Ghost when its parent type is also Ghost
10287 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10288 -- directly inherited because the Ghost policy in effect may differ.
10290 if Is_Ghost_Entity (Parent_Type) then
10291 Set_Is_Ghost_Entity (Derived_Type);
10292 end if;
10294 -- Type dependent processing
10296 case Ekind (Parent_Type) is
10297 when Numeric_Kind =>
10298 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10300 when Array_Kind =>
10301 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10303 when Class_Wide_Kind
10304 | E_Record_Subtype
10305 | E_Record_Type
10307 Build_Derived_Record_Type
10308 (N, Parent_Type, Derived_Type, Derive_Subps);
10309 return;
10311 when Enumeration_Kind =>
10312 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10314 when Access_Kind =>
10315 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10317 when Incomplete_Or_Private_Kind =>
10318 Build_Derived_Private_Type
10319 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10321 -- For discriminated types, the derivation includes deriving
10322 -- primitive operations. For others it is done below.
10324 if Is_Tagged_Type (Parent_Type)
10325 or else Has_Discriminants (Parent_Type)
10326 or else (Present (Full_View (Parent_Type))
10327 and then Has_Discriminants (Full_View (Parent_Type)))
10328 then
10329 return;
10330 end if;
10332 when Concurrent_Kind =>
10333 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10335 when others =>
10336 raise Program_Error;
10337 end case;
10339 -- Nothing more to do if some error occurred
10341 if Etype (Derived_Type) = Any_Type then
10342 return;
10343 end if;
10345 -- If not already set, initialize the derived type's list of primitive
10346 -- operations to an empty element list.
10348 if not Present (Direct_Primitive_Operations (Derived_Type)) then
10349 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10351 -- If Etype of the derived type is the base type (as opposed to
10352 -- a parent type) and doesn't have an associated list of primitive
10353 -- operations, then set the base type's primitive list to the
10354 -- derived type's list. The lists need to be shared in common
10355 -- between the two.
10357 if Etype (Derived_Type) = Base_Type (Derived_Type)
10358 and then
10359 not Present (Direct_Primitive_Operations (Etype (Derived_Type)))
10360 then
10361 Set_Direct_Primitive_Operations
10362 (Etype (Derived_Type),
10363 Direct_Primitive_Operations (Derived_Type));
10364 end if;
10365 end if;
10367 -- Set delayed freeze and then derive subprograms, we need to do this
10368 -- in this order so that derived subprograms inherit the derived freeze
10369 -- if necessary.
10371 Set_Has_Delayed_Freeze (Derived_Type);
10373 if Derive_Subps then
10374 Derive_Subprograms (Parent_Type, Derived_Type);
10375 end if;
10377 Set_Has_Primitive_Operations
10378 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10379 end Build_Derived_Type;
10381 -----------------------
10382 -- Build_Discriminal --
10383 -----------------------
10385 procedure Build_Discriminal (Discrim : Entity_Id) is
10386 D_Minal : Entity_Id;
10387 CR_Disc : Entity_Id;
10389 begin
10390 -- A discriminal has the same name as the discriminant
10392 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10394 Mutate_Ekind (D_Minal, E_In_Parameter);
10395 Set_Mechanism (D_Minal, Default_Mechanism);
10396 Set_Etype (D_Minal, Etype (Discrim));
10397 Set_Scope (D_Minal, Current_Scope);
10398 Set_Parent (D_Minal, Parent (Discrim));
10400 Set_Discriminal (Discrim, D_Minal);
10401 Set_Discriminal_Link (D_Minal, Discrim);
10403 -- For task types, build at once the discriminants of the corresponding
10404 -- record, which are needed if discriminants are used in entry defaults
10405 -- and in family bounds.
10407 if Is_Concurrent_Type (Current_Scope)
10408 or else
10409 Is_Limited_Type (Current_Scope)
10410 then
10411 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10413 Mutate_Ekind (CR_Disc, E_In_Parameter);
10414 Set_Mechanism (CR_Disc, Default_Mechanism);
10415 Set_Etype (CR_Disc, Etype (Discrim));
10416 Set_Scope (CR_Disc, Current_Scope);
10417 Set_Discriminal_Link (CR_Disc, Discrim);
10418 Set_CR_Discriminant (Discrim, CR_Disc);
10419 end if;
10420 end Build_Discriminal;
10422 ------------------------------------
10423 -- Build_Discriminant_Constraints --
10424 ------------------------------------
10426 function Build_Discriminant_Constraints
10427 (T : Entity_Id;
10428 Def : Node_Id;
10429 Derived_Def : Boolean := False) return Elist_Id
10431 C : constant Node_Id := Constraint (Def);
10432 Nb_Discr : constant Nat := Number_Discriminants (T);
10434 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10435 -- Saves the expression corresponding to a given discriminant in T
10437 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10438 -- Return the Position number within array Discr_Expr of a discriminant
10439 -- D within the discriminant list of the discriminated type T.
10441 procedure Process_Discriminant_Expression
10442 (Expr : Node_Id;
10443 D : Entity_Id);
10444 -- If this is a discriminant constraint on a partial view, do not
10445 -- generate an overflow check on the discriminant expression. The check
10446 -- will be generated when constraining the full view. Otherwise the
10447 -- backend creates duplicate symbols for the temporaries corresponding
10448 -- to the expressions to be checked, causing spurious assembler errors.
10450 ------------------
10451 -- Pos_Of_Discr --
10452 ------------------
10454 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10455 Disc : Entity_Id;
10457 begin
10458 Disc := First_Discriminant (T);
10459 for J in Discr_Expr'Range loop
10460 if Disc = D then
10461 return J;
10462 end if;
10464 Next_Discriminant (Disc);
10465 end loop;
10467 -- Note: Since this function is called on discriminants that are
10468 -- known to belong to the discriminated type, falling through the
10469 -- loop with no match signals an internal compiler error.
10471 raise Program_Error;
10472 end Pos_Of_Discr;
10474 -------------------------------------
10475 -- Process_Discriminant_Expression --
10476 -------------------------------------
10478 procedure Process_Discriminant_Expression
10479 (Expr : Node_Id;
10480 D : Entity_Id)
10482 BDT : constant Entity_Id := Base_Type (Etype (D));
10484 begin
10485 -- If this is a discriminant constraint on a partial view, do
10486 -- not generate an overflow on the discriminant expression. The
10487 -- check will be generated when constraining the full view.
10489 if Is_Private_Type (T)
10490 and then Present (Full_View (T))
10491 then
10492 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10493 else
10494 Analyze_And_Resolve (Expr, BDT);
10495 end if;
10496 end Process_Discriminant_Expression;
10498 -- Declarations local to Build_Discriminant_Constraints
10500 Discr : Entity_Id;
10501 E : Entity_Id;
10502 Elist : constant Elist_Id := New_Elmt_List;
10504 Constr : Node_Id;
10505 Expr : Node_Id;
10506 Id : Node_Id;
10507 Position : Nat;
10508 Found : Boolean;
10510 Discrim_Present : Boolean := False;
10512 -- Start of processing for Build_Discriminant_Constraints
10514 begin
10515 -- The following loop will process positional associations only.
10516 -- For a positional association, the (single) discriminant is
10517 -- implicitly specified by position, in textual order (RM 3.7.2).
10519 Discr := First_Discriminant (T);
10520 Constr := First (Constraints (C));
10521 for D in Discr_Expr'Range loop
10522 exit when Nkind (Constr) = N_Discriminant_Association;
10524 if No (Constr) then
10525 Error_Msg_N ("too few discriminants given in constraint", C);
10526 return New_Elmt_List;
10528 elsif Nkind (Constr) = N_Range
10529 or else (Nkind (Constr) = N_Attribute_Reference
10530 and then Attribute_Name (Constr) = Name_Range)
10531 then
10532 Error_Msg_N
10533 ("a range is not a valid discriminant constraint", Constr);
10534 Discr_Expr (D) := Error;
10536 elsif Nkind (Constr) = N_Subtype_Indication then
10537 Error_Msg_N
10538 ("a subtype indication is not a valid discriminant constraint",
10539 Constr);
10540 Discr_Expr (D) := Error;
10542 else
10543 Process_Discriminant_Expression (Constr, Discr);
10544 Discr_Expr (D) := Constr;
10545 end if;
10547 Next_Discriminant (Discr);
10548 Next (Constr);
10549 end loop;
10551 if No (Discr) and then Present (Constr) then
10552 Error_Msg_N ("too many discriminants given in constraint", Constr);
10553 return New_Elmt_List;
10554 end if;
10556 -- Named associations can be given in any order, but if both positional
10557 -- and named associations are used in the same discriminant constraint,
10558 -- then positional associations must occur first, at their normal
10559 -- position. Hence once a named association is used, the rest of the
10560 -- discriminant constraint must use only named associations.
10562 while Present (Constr) loop
10564 -- Positional association forbidden after a named association
10566 if Nkind (Constr) /= N_Discriminant_Association then
10567 Error_Msg_N ("positional association follows named one", Constr);
10568 return New_Elmt_List;
10570 -- Otherwise it is a named association
10572 else
10573 -- E records the type of the discriminants in the named
10574 -- association. All the discriminants specified in the same name
10575 -- association must have the same type.
10577 E := Empty;
10579 -- Search the list of discriminants in T to see if the simple name
10580 -- given in the constraint matches any of them.
10582 Id := First (Selector_Names (Constr));
10583 while Present (Id) loop
10584 Found := False;
10586 -- If Original_Discriminant is present, we are processing a
10587 -- generic instantiation and this is an instance node. We need
10588 -- to find the name of the corresponding discriminant in the
10589 -- actual record type T and not the name of the discriminant in
10590 -- the generic formal. Example:
10592 -- generic
10593 -- type G (D : int) is private;
10594 -- package P is
10595 -- subtype W is G (D => 1);
10596 -- end package;
10597 -- type Rec (X : int) is record ... end record;
10598 -- package Q is new P (G => Rec);
10600 -- At the point of the instantiation, formal type G is Rec
10601 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10602 -- which really looks like "subtype W is Rec (D => 1);" at
10603 -- the point of instantiation, we want to find the discriminant
10604 -- that corresponds to D in Rec, i.e. X.
10606 if Present (Original_Discriminant (Id))
10607 and then In_Instance
10608 then
10609 Discr := Find_Corresponding_Discriminant (Id, T);
10610 Found := True;
10612 else
10613 Discr := First_Discriminant (T);
10614 while Present (Discr) loop
10615 if Chars (Discr) = Chars (Id) then
10616 Found := True;
10617 exit;
10618 end if;
10620 Next_Discriminant (Discr);
10621 end loop;
10623 if not Found then
10624 Error_Msg_N ("& does not match any discriminant", Id);
10625 return New_Elmt_List;
10627 -- If the parent type is a generic formal, preserve the
10628 -- name of the discriminant for subsequent instances.
10629 -- see comment at the beginning of this if statement.
10631 elsif Is_Generic_Type (Root_Type (T)) then
10632 Set_Original_Discriminant (Id, Discr);
10633 end if;
10634 end if;
10636 Position := Pos_Of_Discr (T, Discr);
10638 if Present (Discr_Expr (Position)) then
10639 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10641 else
10642 -- Each discriminant specified in the same named association
10643 -- must be associated with a separate copy of the
10644 -- corresponding expression.
10646 if Present (Next (Id)) then
10647 Expr := New_Copy_Tree (Expression (Constr));
10648 Set_Parent (Expr, Parent (Expression (Constr)));
10649 else
10650 Expr := Expression (Constr);
10651 end if;
10653 Discr_Expr (Position) := Expr;
10654 Process_Discriminant_Expression (Expr, Discr);
10655 end if;
10657 -- A discriminant association with more than one discriminant
10658 -- name is only allowed if the named discriminants are all of
10659 -- the same type (RM 3.7.1(8)).
10661 if E = Empty then
10662 E := Base_Type (Etype (Discr));
10664 elsif Base_Type (Etype (Discr)) /= E then
10665 Error_Msg_N
10666 ("all discriminants in an association " &
10667 "must have the same type", Id);
10668 end if;
10670 Next (Id);
10671 end loop;
10672 end if;
10674 Next (Constr);
10675 end loop;
10677 -- A discriminant constraint must provide exactly one value for each
10678 -- discriminant of the type (RM 3.7.1(8)).
10680 for J in Discr_Expr'Range loop
10681 if No (Discr_Expr (J)) then
10682 Error_Msg_N ("too few discriminants given in constraint", C);
10683 return New_Elmt_List;
10684 end if;
10685 end loop;
10687 -- Determine if there are discriminant expressions in the constraint
10689 for J in Discr_Expr'Range loop
10690 if Denotes_Discriminant
10691 (Discr_Expr (J), Check_Concurrent => True)
10692 then
10693 Discrim_Present := True;
10694 exit;
10695 end if;
10696 end loop;
10698 -- Build an element list consisting of the expressions given in the
10699 -- discriminant constraint and apply the appropriate checks. The list
10700 -- is constructed after resolving any named discriminant associations
10701 -- and therefore the expressions appear in the textual order of the
10702 -- discriminants.
10704 Discr := First_Discriminant (T);
10705 for J in Discr_Expr'Range loop
10706 if Discr_Expr (J) /= Error then
10707 Append_Elmt (Discr_Expr (J), Elist);
10709 -- If any of the discriminant constraints is given by a
10710 -- discriminant and we are in a derived type declaration we
10711 -- have a discriminant renaming. Establish link between new
10712 -- and old discriminant. The new discriminant has an implicit
10713 -- dereference if the old one does.
10715 if Denotes_Discriminant (Discr_Expr (J)) then
10716 if Derived_Def then
10717 declare
10718 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10720 begin
10721 Set_Corresponding_Discriminant (New_Discr, Discr);
10722 Set_Has_Implicit_Dereference (New_Discr,
10723 Has_Implicit_Dereference (Discr));
10724 end;
10725 end if;
10727 -- Force the evaluation of non-discriminant expressions.
10728 -- If we have found a discriminant in the constraint 3.4(26)
10729 -- and 3.8(18) demand that no range checks are performed are
10730 -- after evaluation. If the constraint is for a component
10731 -- definition that has a per-object constraint, expressions are
10732 -- evaluated but not checked either. In all other cases perform
10733 -- a range check.
10735 else
10736 if Discrim_Present then
10737 null;
10739 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10740 and then Has_Per_Object_Constraint
10741 (Defining_Identifier (Parent (Parent (Def))))
10742 then
10743 null;
10745 elsif Is_Access_Type (Etype (Discr)) then
10746 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10748 else
10749 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10750 end if;
10752 -- If the value of the discriminant may be visible in
10753 -- another unit or child unit, create an external name
10754 -- for it. We use the name of the object or component
10755 -- that carries the discriminated subtype. The code
10756 -- below may generate external symbols for the discriminant
10757 -- expression when not strictly needed, which is harmless.
10759 if Expander_Active
10760 and then Comes_From_Source (Def)
10761 and then not Is_Subprogram (Current_Scope)
10762 then
10763 declare
10764 Id : Entity_Id := Empty;
10765 begin
10766 if Nkind (Parent (Def)) = N_Object_Declaration then
10767 Id := Defining_Identifier (Parent (Def));
10769 elsif Nkind (Parent (Def)) = N_Component_Definition
10770 and then
10771 Nkind (Parent (Parent (Def)))
10772 = N_Component_Declaration
10773 then
10774 Id := Defining_Identifier (Parent (Parent (Def)));
10775 end if;
10777 if Present (Id) then
10778 Force_Evaluation (
10779 Discr_Expr (J),
10780 Related_Id => Id,
10781 Discr_Number => J);
10782 else
10783 Force_Evaluation (Discr_Expr (J));
10784 end if;
10785 end;
10786 else
10787 Force_Evaluation (Discr_Expr (J));
10788 end if;
10789 end if;
10791 -- Check that the designated type of an access discriminant's
10792 -- expression is not a class-wide type unless the discriminant's
10793 -- designated type is also class-wide.
10795 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10796 and then not Is_Class_Wide_Type
10797 (Designated_Type (Etype (Discr)))
10798 and then Etype (Discr_Expr (J)) /= Any_Type
10799 and then Is_Class_Wide_Type
10800 (Designated_Type (Etype (Discr_Expr (J))))
10801 then
10802 Wrong_Type (Discr_Expr (J), Etype (Discr));
10804 elsif Is_Access_Type (Etype (Discr))
10805 and then not Is_Access_Constant (Etype (Discr))
10806 and then Is_Access_Type (Etype (Discr_Expr (J)))
10807 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10808 then
10809 Error_Msg_NE
10810 ("constraint for discriminant& must be access to variable",
10811 Def, Discr);
10812 end if;
10813 end if;
10815 Next_Discriminant (Discr);
10816 end loop;
10818 return Elist;
10819 end Build_Discriminant_Constraints;
10821 ---------------------------------
10822 -- Build_Discriminated_Subtype --
10823 ---------------------------------
10825 procedure Build_Discriminated_Subtype
10826 (T : Entity_Id;
10827 Def_Id : Entity_Id;
10828 Elist : Elist_Id;
10829 Related_Nod : Node_Id;
10830 For_Access : Boolean := False)
10832 Has_Discrs : constant Boolean := Has_Discriminants (T);
10833 Constrained : constant Boolean :=
10834 (Has_Discrs
10835 and then not Is_Empty_Elmt_List (Elist)
10836 and then not Is_Class_Wide_Type (T))
10837 or else Is_Constrained (T);
10839 begin
10840 if Ekind (T) = E_Record_Type then
10841 Mutate_Ekind (Def_Id, E_Record_Subtype);
10843 -- Inherit preelaboration flag from base, for types for which it
10844 -- may have been set: records, private types, protected types.
10846 Set_Known_To_Have_Preelab_Init
10847 (Def_Id, Known_To_Have_Preelab_Init (T));
10849 elsif Ekind (T) = E_Task_Type then
10850 Mutate_Ekind (Def_Id, E_Task_Subtype);
10852 elsif Ekind (T) = E_Protected_Type then
10853 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10854 Set_Known_To_Have_Preelab_Init
10855 (Def_Id, Known_To_Have_Preelab_Init (T));
10857 elsif Is_Private_Type (T) then
10858 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10859 Set_Known_To_Have_Preelab_Init
10860 (Def_Id, Known_To_Have_Preelab_Init (T));
10862 -- Private subtypes may have private dependents
10864 Set_Private_Dependents (Def_Id, New_Elmt_List);
10866 elsif Is_Class_Wide_Type (T) then
10867 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10869 else
10870 -- Incomplete type. Attach subtype to list of dependents, to be
10871 -- completed with full view of parent type, unless is it the
10872 -- designated subtype of a record component within an init_proc.
10873 -- This last case arises for a component of an access type whose
10874 -- designated type is incomplete (e.g. a Taft Amendment type).
10875 -- The designated subtype is within an inner scope, and needs no
10876 -- elaboration, because only the access type is needed in the
10877 -- initialization procedure.
10879 if Ekind (T) = E_Incomplete_Type then
10880 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10881 else
10882 Mutate_Ekind (Def_Id, Ekind (T));
10883 end if;
10885 if For_Access and then Within_Init_Proc then
10886 null;
10887 else
10888 Append_Elmt (Def_Id, Private_Dependents (T));
10889 end if;
10890 end if;
10892 Set_Etype (Def_Id, T);
10893 Reinit_Size_Align (Def_Id);
10894 Set_Has_Discriminants (Def_Id, Has_Discrs);
10895 Set_Is_Constrained (Def_Id, Constrained);
10897 Set_First_Entity (Def_Id, First_Entity (T));
10898 Set_Last_Entity (Def_Id, Last_Entity (T));
10899 Set_Has_Implicit_Dereference
10900 (Def_Id, Has_Implicit_Dereference (T));
10901 Set_Has_Pragma_Unreferenced_Objects
10902 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10904 -- If the subtype is the completion of a private declaration, there may
10905 -- have been representation clauses for the partial view, and they must
10906 -- be preserved. Build_Derived_Type chains the inherited clauses with
10907 -- the ones appearing on the extension. If this comes from a subtype
10908 -- declaration, all clauses are inherited.
10910 if No (First_Rep_Item (Def_Id)) then
10911 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10912 end if;
10914 if Is_Tagged_Type (T) then
10915 Set_Is_Tagged_Type (Def_Id);
10916 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10917 Make_Class_Wide_Type (Def_Id);
10918 end if;
10920 Set_Stored_Constraint (Def_Id, No_Elist);
10922 if Has_Discrs then
10923 Set_Discriminant_Constraint (Def_Id, Elist);
10924 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10925 end if;
10927 if Is_Tagged_Type (T) then
10929 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10930 -- concurrent record type (which has the list of primitive
10931 -- operations).
10933 if Ada_Version >= Ada_2005
10934 and then Is_Concurrent_Type (T)
10935 then
10936 Set_Corresponding_Record_Type (Def_Id,
10937 Corresponding_Record_Type (T));
10938 else
10939 Set_Direct_Primitive_Operations (Def_Id,
10940 Direct_Primitive_Operations (T));
10941 end if;
10943 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10944 end if;
10946 -- Subtypes introduced by component declarations do not need to be
10947 -- marked as delayed, and do not get freeze nodes, because the semantics
10948 -- verifies that the parents of the subtypes are frozen before the
10949 -- enclosing record is frozen.
10951 if not Is_Type (Scope (Def_Id)) then
10952 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10954 if Is_Private_Type (T)
10955 and then Present (Full_View (T))
10956 then
10957 Conditional_Delay (Def_Id, Full_View (T));
10958 else
10959 Conditional_Delay (Def_Id, T);
10960 end if;
10961 end if;
10963 if Is_Record_Type (T) then
10964 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10966 if Has_Discrs
10967 and then not Is_Empty_Elmt_List (Elist)
10968 and then not For_Access
10969 then
10970 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10972 elsif not Is_Private_Type (T) then
10973 Set_Cloned_Subtype (Def_Id, T);
10974 end if;
10975 end if;
10976 end Build_Discriminated_Subtype;
10978 ---------------------------
10979 -- Build_Itype_Reference --
10980 ---------------------------
10982 procedure Build_Itype_Reference
10983 (Ityp : Entity_Id;
10984 Nod : Node_Id)
10986 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10987 begin
10989 -- Itype references are only created for use by the back-end
10991 if Inside_A_Generic then
10992 return;
10993 else
10994 Set_Itype (IR, Ityp);
10996 -- If Nod is a library unit entity, then Insert_After won't work,
10997 -- because Nod is not a member of any list. Therefore, we use
10998 -- Add_Global_Declaration in this case. This can happen if we have a
10999 -- build-in-place library function, child unit or not.
11001 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
11002 or else (Nkind (Nod) in
11003 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
11004 and then Is_Compilation_Unit (Defining_Entity (Nod)))
11005 then
11006 Add_Global_Declaration (IR);
11007 else
11008 Insert_After (Nod, IR);
11009 end if;
11010 end if;
11011 end Build_Itype_Reference;
11013 ------------------------
11014 -- Build_Scalar_Bound --
11015 ------------------------
11017 function Build_Scalar_Bound
11018 (Bound : Node_Id;
11019 Par_T : Entity_Id;
11020 Der_T : Entity_Id) return Node_Id
11022 New_Bound : Entity_Id;
11024 begin
11025 -- Note: not clear why this is needed, how can the original bound
11026 -- be unanalyzed at this point? and if it is, what business do we
11027 -- have messing around with it? and why is the base type of the
11028 -- parent type the right type for the resolution. It probably is
11029 -- not. It is OK for the new bound we are creating, but not for
11030 -- the old one??? Still if it never happens, no problem.
11032 Analyze_And_Resolve (Bound, Base_Type (Par_T));
11034 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
11035 New_Bound := New_Copy (Bound);
11036 Set_Etype (New_Bound, Der_T);
11037 Set_Analyzed (New_Bound);
11039 elsif Is_Entity_Name (Bound) then
11040 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
11042 -- The following is almost certainly wrong. What business do we have
11043 -- relocating a node (Bound) that is presumably still attached to
11044 -- the tree elsewhere???
11046 else
11047 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
11048 end if;
11050 Set_Etype (New_Bound, Der_T);
11051 return New_Bound;
11052 end Build_Scalar_Bound;
11054 -------------------------------
11055 -- Check_Abstract_Overriding --
11056 -------------------------------
11058 procedure Check_Abstract_Overriding (T : Entity_Id) is
11059 Alias_Subp : Entity_Id;
11060 Elmt : Elmt_Id;
11061 Op_List : Elist_Id;
11062 Subp : Entity_Id;
11063 Type_Def : Node_Id;
11065 procedure Check_Pragma_Implemented (Subp : Entity_Id);
11066 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
11067 -- which has pragma Implemented already set. Check whether Subp's entity
11068 -- kind conforms to the implementation kind of the overridden routine.
11070 procedure Check_Pragma_Implemented
11071 (Subp : Entity_Id;
11072 Iface_Subp : Entity_Id);
11073 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
11074 -- Iface_Subp and both entities have pragma Implemented already set on
11075 -- them. Check whether the two implementation kinds are conforming.
11077 procedure Inherit_Pragma_Implemented
11078 (Subp : Entity_Id;
11079 Iface_Subp : Entity_Id);
11080 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
11081 -- subprogram Iface_Subp which has been marked by pragma Implemented.
11082 -- Propagate the implementation kind of Iface_Subp to Subp.
11084 ------------------------------
11085 -- Check_Pragma_Implemented --
11086 ------------------------------
11088 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
11089 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
11090 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
11091 Subp_Alias : constant Entity_Id := Alias (Subp);
11092 Contr_Typ : Entity_Id;
11093 Impl_Subp : Entity_Id;
11095 begin
11096 -- Subp must have an alias since it is a hidden entity used to link
11097 -- an interface subprogram to its overriding counterpart.
11099 pragma Assert (Present (Subp_Alias));
11101 -- Handle aliases to synchronized wrappers
11103 Impl_Subp := Subp_Alias;
11105 if Is_Primitive_Wrapper (Impl_Subp) then
11106 Impl_Subp := Wrapped_Entity (Impl_Subp);
11107 end if;
11109 -- Extract the type of the controlling formal
11111 Contr_Typ := Etype (First_Formal (Subp_Alias));
11113 if Is_Concurrent_Record_Type (Contr_Typ) then
11114 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
11115 end if;
11117 -- An interface subprogram whose implementation kind is By_Entry must
11118 -- be implemented by an entry.
11120 if Impl_Kind = Name_By_Entry
11121 and then Ekind (Impl_Subp) /= E_Entry
11122 then
11123 Error_Msg_Node_2 := Iface_Alias;
11124 Error_Msg_NE
11125 ("type & must implement abstract subprogram & with an entry",
11126 Subp_Alias, Contr_Typ);
11128 elsif Impl_Kind = Name_By_Protected_Procedure then
11130 -- An interface subprogram whose implementation kind is By_
11131 -- Protected_Procedure cannot be implemented by a primitive
11132 -- procedure of a task type.
11134 if Ekind (Contr_Typ) /= E_Protected_Type then
11135 Error_Msg_Node_2 := Contr_Typ;
11136 Error_Msg_NE
11137 ("interface subprogram & cannot be implemented by a "
11138 & "primitive procedure of task type &",
11139 Subp_Alias, Iface_Alias);
11141 -- An interface subprogram whose implementation kind is By_
11142 -- Protected_Procedure must be implemented by a procedure.
11144 elsif Ekind (Impl_Subp) /= E_Procedure then
11145 Error_Msg_Node_2 := Iface_Alias;
11146 Error_Msg_NE
11147 ("type & must implement abstract subprogram & with a "
11148 & "procedure", Subp_Alias, Contr_Typ);
11150 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11151 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11152 then
11153 Error_Msg_Name_1 := Impl_Kind;
11154 Error_Msg_N
11155 ("overriding operation& must have synchronization%",
11156 Subp_Alias);
11157 end if;
11159 -- If primitive has Optional synchronization, overriding operation
11160 -- must match if it has an explicit synchronization.
11162 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
11163 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
11164 then
11165 Error_Msg_Name_1 := Impl_Kind;
11166 Error_Msg_N
11167 ("overriding operation& must have synchronization%", Subp_Alias);
11168 end if;
11169 end Check_Pragma_Implemented;
11171 ------------------------------
11172 -- Check_Pragma_Implemented --
11173 ------------------------------
11175 procedure Check_Pragma_Implemented
11176 (Subp : Entity_Id;
11177 Iface_Subp : Entity_Id)
11179 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11180 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
11182 begin
11183 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
11184 -- and overriding subprogram are different. In general this is an
11185 -- error except when the implementation kind of the overridden
11186 -- subprograms is By_Any or Optional.
11188 if Iface_Kind /= Subp_Kind
11189 and then Iface_Kind /= Name_By_Any
11190 and then Iface_Kind /= Name_Optional
11191 then
11192 if Iface_Kind = Name_By_Entry then
11193 Error_Msg_N
11194 ("incompatible implementation kind, overridden subprogram " &
11195 "is marked By_Entry", Subp);
11196 else
11197 Error_Msg_N
11198 ("incompatible implementation kind, overridden subprogram " &
11199 "is marked By_Protected_Procedure", Subp);
11200 end if;
11201 end if;
11202 end Check_Pragma_Implemented;
11204 --------------------------------
11205 -- Inherit_Pragma_Implemented --
11206 --------------------------------
11208 procedure Inherit_Pragma_Implemented
11209 (Subp : Entity_Id;
11210 Iface_Subp : Entity_Id)
11212 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
11213 Loc : constant Source_Ptr := Sloc (Subp);
11214 Impl_Prag : Node_Id;
11216 begin
11217 -- Since the implementation kind is stored as a representation item
11218 -- rather than a flag, create a pragma node.
11220 Impl_Prag :=
11221 Make_Pragma (Loc,
11222 Chars => Name_Implemented,
11223 Pragma_Argument_Associations => New_List (
11224 Make_Pragma_Argument_Association (Loc,
11225 Expression => New_Occurrence_Of (Subp, Loc)),
11227 Make_Pragma_Argument_Association (Loc,
11228 Expression => Make_Identifier (Loc, Iface_Kind))));
11230 -- The pragma doesn't need to be analyzed because it is internally
11231 -- built. It is safe to directly register it as a rep item since we
11232 -- are only interested in the characters of the implementation kind.
11234 Record_Rep_Item (Subp, Impl_Prag);
11235 end Inherit_Pragma_Implemented;
11237 -- Start of processing for Check_Abstract_Overriding
11239 begin
11240 Op_List := Primitive_Operations (T);
11242 -- Loop to check primitive operations
11244 Elmt := First_Elmt (Op_List);
11245 while Present (Elmt) loop
11246 Subp := Node (Elmt);
11247 Alias_Subp := Alias (Subp);
11249 -- If the parent type is untagged, then no overriding error checks
11250 -- are needed (such as in the case of an implicit full type for
11251 -- a derived type whose parent is an untagged private type with
11252 -- a tagged full type).
11254 if not Is_Tagged_Type (Etype (T)) then
11255 null;
11257 -- Inherited subprograms are identified by the fact that they do not
11258 -- come from source, and the associated source location is the
11259 -- location of the first subtype of the derived type.
11261 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
11262 -- subprograms that "require overriding".
11264 -- Special exception, do not complain about failure to override the
11265 -- stream routines _Input and _Output, as well as the primitive
11266 -- operations used in dispatching selects since we always provide
11267 -- automatic overridings for these subprograms.
11269 -- The partial view of T may have been a private extension, for
11270 -- which inherited functions dispatching on result are abstract.
11271 -- If the full view is a null extension, there is no need for
11272 -- overriding in Ada 2005, but wrappers need to be built for them
11273 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11275 elsif Is_Null_Extension (T)
11276 and then Has_Controlling_Result (Subp)
11277 and then Ada_Version >= Ada_2005
11278 and then Present (Alias_Subp)
11279 and then not Comes_From_Source (Subp)
11280 and then not Is_Abstract_Subprogram (Alias_Subp)
11281 and then not Is_Access_Type (Etype (Subp))
11282 then
11283 null;
11285 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11286 -- processing because this check is done with the aliased
11287 -- entity
11289 elsif Present (Interface_Alias (Subp)) then
11290 null;
11292 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11293 -- of a visible private primitive inherited from an ancestor with
11294 -- the aspect Type_Invariant'Class, unless the inherited primitive
11295 -- is abstract.
11297 elsif not Is_Abstract_Subprogram (Subp)
11298 and then not Comes_From_Source (Subp) -- An inherited subprogram
11299 and then Requires_Overriding (Subp)
11300 and then Present (Alias_Subp)
11301 and then Has_Invariants (Etype (T))
11302 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11303 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11304 and then Is_Private_Primitive (Alias_Subp)
11305 then
11306 Error_Msg_NE
11307 ("inherited private primitive & must be overridden", T, Subp);
11308 Error_Msg_N
11309 ("\because ancestor type has 'Type_'Invariant''Class " &
11310 "(RM 7.3.2(6.1))", T);
11312 elsif (Is_Abstract_Subprogram (Subp)
11313 or else Requires_Overriding (Subp)
11314 or else
11315 (Has_Controlling_Result (Subp)
11316 and then Present (Alias_Subp)
11317 and then not Comes_From_Source (Subp)
11318 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11319 and then not Is_TSS (Subp, TSS_Stream_Input)
11320 and then not Is_TSS (Subp, TSS_Stream_Output)
11321 and then not Is_Abstract_Type (T)
11322 and then not Is_Predefined_Interface_Primitive (Subp)
11324 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11325 -- with abstract interface types because the check will be done
11326 -- with the aliased entity (otherwise we generate a duplicated
11327 -- error message).
11329 and then No (Interface_Alias (Subp))
11330 then
11331 if Present (Alias_Subp) then
11333 -- Only perform the check for a derived subprogram when the
11334 -- type has an explicit record extension. This avoids incorrect
11335 -- flagging of abstract subprograms for the case of a type
11336 -- without an extension that is derived from a formal type
11337 -- with a tagged actual (can occur within a private part).
11339 -- Ada 2005 (AI-391): In the case of an inherited function with
11340 -- a controlling result of the type, the rule does not apply if
11341 -- the type is a null extension (unless the parent function
11342 -- itself is abstract, in which case the function must still be
11343 -- be overridden). The expander will generate an overriding
11344 -- wrapper function calling the parent subprogram (see
11345 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11347 Type_Def := Type_Definition (Parent (T));
11349 if Nkind (Type_Def) = N_Derived_Type_Definition
11350 and then Present (Record_Extension_Part (Type_Def))
11351 and then
11352 (Ada_Version < Ada_2005
11353 or else not Is_Null_Extension (T)
11354 or else Ekind (Subp) = E_Procedure
11355 or else not Has_Controlling_Result (Subp)
11356 or else Is_Abstract_Subprogram (Alias_Subp)
11357 or else Requires_Overriding (Subp)
11358 or else Is_Access_Type (Etype (Subp)))
11359 then
11360 -- Avoid reporting error in case of abstract predefined
11361 -- primitive inherited from interface type because the
11362 -- body of internally generated predefined primitives
11363 -- of tagged types are generated later by Freeze_Type
11365 if Is_Interface (Root_Type (T))
11366 and then Is_Abstract_Subprogram (Subp)
11367 and then Is_Predefined_Dispatching_Operation (Subp)
11368 and then not Comes_From_Source (Ultimate_Alias (Subp))
11369 then
11370 null;
11372 -- A null extension is not obliged to override an inherited
11373 -- procedure subject to pragma Extensions_Visible with value
11374 -- False and at least one controlling OUT parameter
11375 -- (SPARK RM 6.1.7(6)).
11377 elsif Is_Null_Extension (T)
11378 and then Is_EVF_Procedure (Subp)
11379 then
11380 null;
11382 -- Subprogram renamings cannot be overridden
11384 elsif Comes_From_Source (Subp)
11385 and then Present (Alias (Subp))
11386 then
11387 null;
11389 -- Skip reporting the error on Ada 2022 only subprograms
11390 -- that require overriding if we are not in Ada 2022 mode.
11392 elsif Ada_Version < Ada_2022
11393 and then Requires_Overriding (Subp)
11394 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11395 then
11396 null;
11398 else
11399 Error_Msg_NE
11400 ("type must be declared abstract or & overridden",
11401 T, Subp);
11403 -- Traverse the whole chain of aliased subprograms to
11404 -- complete the error notification. This is especially
11405 -- useful for traceability of the chain of entities when
11406 -- the subprogram corresponds with an interface
11407 -- subprogram (which may be defined in another package).
11409 if Present (Alias_Subp) then
11410 declare
11411 E : Entity_Id;
11413 begin
11414 E := Subp;
11415 while Present (Alias (E)) loop
11417 -- Avoid reporting redundant errors on entities
11418 -- inherited from interfaces
11420 if Sloc (E) /= Sloc (T) then
11421 Error_Msg_Sloc := Sloc (E);
11422 Error_Msg_NE
11423 ("\& has been inherited #", T, Subp);
11424 end if;
11426 E := Alias (E);
11427 end loop;
11429 Error_Msg_Sloc := Sloc (E);
11431 -- AI05-0068: report if there is an overriding
11432 -- non-abstract subprogram that is invisible.
11434 if Is_Hidden (E)
11435 and then not Is_Abstract_Subprogram (E)
11436 then
11437 Error_Msg_NE
11438 ("\& subprogram# is not visible",
11439 T, Subp);
11441 -- Clarify the case where a non-null extension must
11442 -- override inherited procedure subject to pragma
11443 -- Extensions_Visible with value False and at least
11444 -- one controlling OUT param.
11446 elsif Is_EVF_Procedure (E) then
11447 Error_Msg_NE
11448 ("\& # is subject to Extensions_Visible False",
11449 T, Subp);
11451 else
11452 Error_Msg_NE
11453 ("\& has been inherited from subprogram #",
11454 T, Subp);
11455 end if;
11456 end;
11457 end if;
11458 end if;
11460 -- Ada 2005 (AI-345): Protected or task type implementing
11461 -- abstract interfaces.
11463 elsif Is_Concurrent_Record_Type (T)
11464 and then Present (Interfaces (T))
11465 then
11466 -- There is no need to check here RM 9.4(11.9/3) since we
11467 -- are processing the corresponding record type and the
11468 -- mode of the overriding subprograms was verified by
11469 -- Check_Conformance when the corresponding concurrent
11470 -- type declaration was analyzed.
11472 Error_Msg_NE
11473 ("interface subprogram & must be overridden", T, Subp);
11475 -- Examine primitive operations of synchronized type to find
11476 -- homonyms that have the wrong profile.
11478 declare
11479 Prim : Entity_Id;
11481 begin
11482 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11483 while Present (Prim) loop
11484 if Chars (Prim) = Chars (Subp) then
11485 Error_Msg_NE
11486 ("profile is not type conformant with prefixed "
11487 & "view profile of inherited operation&",
11488 Prim, Subp);
11489 end if;
11491 Next_Entity (Prim);
11492 end loop;
11493 end;
11494 end if;
11496 else
11497 Error_Msg_Node_2 := T;
11498 Error_Msg_N
11499 ("abstract subprogram& not allowed for type&", Subp);
11501 -- Also post unconditional warning on the type (unconditional
11502 -- so that if there are more than one of these cases, we get
11503 -- them all, and not just the first one).
11505 Error_Msg_Node_2 := Subp;
11506 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11507 end if;
11509 -- A subprogram subject to pragma Extensions_Visible with value
11510 -- "True" cannot override a subprogram subject to the same pragma
11511 -- with value "False" (SPARK RM 6.1.7(5)).
11513 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11514 and then Present (Overridden_Operation (Subp))
11515 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11516 Extensions_Visible_False
11517 then
11518 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11519 Error_Msg_N
11520 ("subprogram & with Extensions_Visible True cannot override "
11521 & "subprogram # with Extensions_Visible False", Subp);
11522 end if;
11524 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11526 -- Subp is an expander-generated procedure which maps an interface
11527 -- alias to a protected wrapper. The interface alias is flagged by
11528 -- pragma Implemented. Ensure that Subp is a procedure when the
11529 -- implementation kind is By_Protected_Procedure or an entry when
11530 -- By_Entry.
11532 if Ada_Version >= Ada_2012
11533 and then Is_Hidden (Subp)
11534 and then Present (Interface_Alias (Subp))
11535 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11536 then
11537 Check_Pragma_Implemented (Subp);
11538 end if;
11540 -- Subp is an interface primitive which overrides another interface
11541 -- primitive marked with pragma Implemented.
11543 if Ada_Version >= Ada_2012
11544 and then Present (Overridden_Operation (Subp))
11545 and then Has_Rep_Pragma
11546 (Overridden_Operation (Subp), Name_Implemented)
11547 then
11548 -- If the overriding routine is also marked by Implemented, check
11549 -- that the two implementation kinds are conforming.
11551 if Has_Rep_Pragma (Subp, Name_Implemented) then
11552 Check_Pragma_Implemented
11553 (Subp => Subp,
11554 Iface_Subp => Overridden_Operation (Subp));
11556 -- Otherwise the overriding routine inherits the implementation
11557 -- kind from the overridden subprogram.
11559 else
11560 Inherit_Pragma_Implemented
11561 (Subp => Subp,
11562 Iface_Subp => Overridden_Operation (Subp));
11563 end if;
11564 end if;
11566 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11567 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11568 -- for procedures, since this is our pragma.
11570 if Present (Overridden_Operation (Subp))
11571 and then No_Return (Overridden_Operation (Subp))
11572 then
11574 -- If the subprogram is a renaming, check that the renamed
11575 -- subprogram is No_Return.
11577 if Present (Renamed_Or_Alias (Subp)) then
11578 if not No_Return (Renamed_Or_Alias (Subp)) then
11579 Error_Msg_NE ("subprogram & must be No_Return",
11580 Subp,
11581 Renamed_Or_Alias (Subp));
11582 Error_Msg_N ("\since renaming & overrides No_Return "
11583 & "subprogram (RM 6.5.1(6/2))",
11584 Subp);
11585 end if;
11587 -- Make sure that the subprogram itself is No_Return.
11589 elsif not No_Return (Subp) then
11590 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11591 Error_Msg_N
11592 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11593 Subp);
11594 end if;
11595 end if;
11597 -- If the operation is a wrapper for a synchronized primitive, it
11598 -- may be called indirectly through a dispatching select. We assume
11599 -- that it will be referenced elsewhere indirectly, and suppress
11600 -- warnings about an unused entity.
11602 if Is_Primitive_Wrapper (Subp)
11603 and then Present (Wrapped_Entity (Subp))
11604 then
11605 Set_Referenced (Wrapped_Entity (Subp));
11606 end if;
11608 Next_Elmt (Elmt);
11609 end loop;
11610 end Check_Abstract_Overriding;
11612 ------------------------------------------------
11613 -- Check_Access_Discriminant_Requires_Limited --
11614 ------------------------------------------------
11616 procedure Check_Access_Discriminant_Requires_Limited
11617 (D : Node_Id;
11618 Loc : Node_Id)
11620 begin
11621 -- A discriminant_specification for an access discriminant shall appear
11622 -- only in the declaration for a task or protected type, or for a type
11623 -- with the reserved word 'limited' in its definition or in one of its
11624 -- ancestors (RM 3.7(10)).
11626 -- AI-0063: The proper condition is that type must be immutably limited,
11627 -- or else be a partial view.
11629 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11630 if Is_Limited_View (Current_Scope)
11631 or else
11632 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11633 and then Limited_Present (Parent (Current_Scope)))
11634 then
11635 null;
11637 else
11638 Error_Msg_N
11639 ("access discriminants allowed only for limited types", Loc);
11640 end if;
11641 end if;
11642 end Check_Access_Discriminant_Requires_Limited;
11644 -----------------------------------
11645 -- Check_Aliased_Component_Types --
11646 -----------------------------------
11648 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11649 C : Entity_Id;
11651 begin
11652 -- ??? Also need to check components of record extensions, but not
11653 -- components of protected types (which are always limited).
11655 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11656 -- types to be unconstrained. This is safe because it is illegal to
11657 -- create access subtypes to such types with explicit discriminant
11658 -- constraints.
11660 if not Is_Limited_Type (T) then
11661 if Ekind (T) = E_Record_Type then
11662 C := First_Component (T);
11663 while Present (C) loop
11664 if Is_Aliased (C)
11665 and then Has_Discriminants (Etype (C))
11666 and then not Is_Constrained (Etype (C))
11667 and then not In_Instance_Body
11668 and then Ada_Version < Ada_2005
11669 then
11670 Error_Msg_N
11671 ("aliased component must be constrained (RM 3.6(11))",
11673 end if;
11675 Next_Component (C);
11676 end loop;
11678 elsif Ekind (T) = E_Array_Type then
11679 if Has_Aliased_Components (T)
11680 and then Has_Discriminants (Component_Type (T))
11681 and then not Is_Constrained (Component_Type (T))
11682 and then not In_Instance_Body
11683 and then Ada_Version < Ada_2005
11684 then
11685 Error_Msg_N
11686 ("aliased component type must be constrained (RM 3.6(11))",
11688 end if;
11689 end if;
11690 end if;
11691 end Check_Aliased_Component_Types;
11693 --------------------------------------
11694 -- Check_Anonymous_Access_Component --
11695 --------------------------------------
11697 procedure Check_Anonymous_Access_Component
11698 (Typ_Decl : Node_Id;
11699 Typ : Entity_Id;
11700 Prev : Entity_Id;
11701 Comp_Def : Node_Id;
11702 Access_Def : Node_Id)
11704 Loc : constant Source_Ptr := Sloc (Comp_Def);
11705 Anon_Access : Entity_Id;
11706 Acc_Def : Node_Id;
11707 Decl : Node_Id;
11708 Type_Def : Node_Id;
11710 procedure Build_Incomplete_Type_Declaration;
11711 -- If the record type contains components that include an access to the
11712 -- current record, then create an incomplete type declaration for the
11713 -- record, to be used as the designated type of the anonymous access.
11714 -- This is done only once, and only if there is no previous partial
11715 -- view of the type.
11717 function Designates_T (Subt : Node_Id) return Boolean;
11718 -- Check whether a node designates the enclosing record type, or 'Class
11719 -- of that type
11721 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11722 -- Check whether an access definition includes a reference to
11723 -- the enclosing record type. The reference can be a subtype mark
11724 -- in the access definition itself, a 'Class attribute reference, or
11725 -- recursively a reference appearing in a parameter specification
11726 -- or result definition of an access_to_subprogram definition.
11728 --------------------------------------
11729 -- Build_Incomplete_Type_Declaration --
11730 --------------------------------------
11732 procedure Build_Incomplete_Type_Declaration is
11733 Decl : Node_Id;
11734 Inc_T : Entity_Id;
11735 H : Entity_Id;
11737 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11738 -- it's "is new ... with record" or else "is tagged record ...".
11740 Typ_Def : constant Node_Id :=
11741 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11742 then Type_Definition (Typ_Decl) else Empty);
11743 Is_Tagged : constant Boolean :=
11744 Present (Typ_Def)
11745 and then
11746 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11747 and then
11748 Present (Record_Extension_Part (Typ_Def)))
11749 or else
11750 (Nkind (Typ_Def) = N_Record_Definition
11751 and then Tagged_Present (Typ_Def)));
11753 begin
11754 -- If there is a previous partial view, no need to create a new one
11755 -- If the partial view, given by Prev, is incomplete, If Prev is
11756 -- a private declaration, full declaration is flagged accordingly.
11758 if Prev /= Typ then
11759 if Is_Tagged then
11760 Make_Class_Wide_Type (Prev);
11761 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11762 Set_Etype (Class_Wide_Type (Typ), Typ);
11763 end if;
11765 return;
11767 elsif Has_Private_Declaration (Typ) then
11769 -- If we refer to T'Class inside T, and T is the completion of a
11770 -- private type, then make sure the class-wide type exists.
11772 if Is_Tagged then
11773 Make_Class_Wide_Type (Typ);
11774 end if;
11776 return;
11778 -- If there was a previous anonymous access type, the incomplete
11779 -- type declaration will have been created already.
11781 elsif Present (Current_Entity (Typ))
11782 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11783 and then Full_View (Current_Entity (Typ)) = Typ
11784 then
11785 if Is_Tagged
11786 and then Comes_From_Source (Current_Entity (Typ))
11787 and then not Is_Tagged_Type (Current_Entity (Typ))
11788 then
11789 Make_Class_Wide_Type (Typ);
11790 Error_Msg_N
11791 ("incomplete view of tagged type should be declared tagged??",
11792 Parent (Current_Entity (Typ)));
11793 end if;
11794 return;
11796 else
11797 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11798 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11800 -- Type has already been inserted into the current scope. Remove
11801 -- it, and add incomplete declaration for type, so that subsequent
11802 -- anonymous access types can use it. The entity is unchained from
11803 -- the homonym list and from immediate visibility. After analysis,
11804 -- the entity in the incomplete declaration becomes immediately
11805 -- visible in the record declaration that follows.
11807 H := Current_Entity (Typ);
11809 if H = Typ then
11810 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11812 else
11813 while Present (Homonym (H)) and then Homonym (H) /= Typ loop
11814 H := Homonym (Typ);
11815 end loop;
11817 Set_Homonym (H, Homonym (Typ));
11818 end if;
11820 Insert_Before (Typ_Decl, Decl);
11821 Analyze (Decl);
11822 Set_Full_View (Inc_T, Typ);
11823 Set_Incomplete_View (Typ_Decl, Inc_T);
11825 -- If the type is tagged, create a common class-wide type for
11826 -- both views, and set the Etype of the class-wide type to the
11827 -- full view.
11829 if Is_Tagged then
11830 Make_Class_Wide_Type (Inc_T);
11831 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11832 Set_Etype (Class_Wide_Type (Typ), Typ);
11833 end if;
11835 -- If the scope is a package with a limited view, create a shadow
11836 -- entity for the incomplete type like Build_Limited_Views, so as
11837 -- to make it possible for Remove_Limited_With_Unit to reinstall
11838 -- this incomplete type as the visible entity.
11840 if Ekind (Scope (Inc_T)) = E_Package
11841 and then Present (Limited_View (Scope (Inc_T)))
11842 then
11843 declare
11844 Shadow : constant Entity_Id := Make_Temporary (Loc, 'Z');
11846 begin
11847 -- This is modeled on Build_Shadow_Entity
11849 Set_Chars (Shadow, Chars (Inc_T));
11850 Set_Parent (Shadow, Decl);
11851 Decorate_Type (Shadow, Scope (Inc_T), Is_Tagged);
11852 Set_Is_Internal (Shadow);
11853 Set_From_Limited_With (Shadow);
11854 Set_Non_Limited_View (Shadow, Inc_T);
11855 Set_Private_Dependents (Shadow, New_Elmt_List);
11857 if Is_Tagged then
11858 Set_Non_Limited_View
11859 (Class_Wide_Type (Shadow), Class_Wide_Type (Inc_T));
11860 end if;
11862 Append_Entity (Shadow, Limited_View (Scope (Inc_T)));
11863 end;
11864 end if;
11865 end if;
11866 end Build_Incomplete_Type_Declaration;
11868 ------------------
11869 -- Designates_T --
11870 ------------------
11872 function Designates_T (Subt : Node_Id) return Boolean is
11873 Type_Id : constant Name_Id := Chars (Typ);
11875 function Names_T (Nam : Node_Id) return Boolean;
11876 -- The record type has not been introduced in the current scope
11877 -- yet, so we must examine the name of the type itself, either
11878 -- an identifier T, or an expanded name of the form P.T, where
11879 -- P denotes the current scope.
11881 -------------
11882 -- Names_T --
11883 -------------
11885 function Names_T (Nam : Node_Id) return Boolean is
11886 begin
11887 if Nkind (Nam) = N_Identifier then
11888 return Chars (Nam) = Type_Id;
11890 elsif Nkind (Nam) = N_Selected_Component then
11891 if Chars (Selector_Name (Nam)) = Type_Id then
11892 if Nkind (Prefix (Nam)) = N_Identifier then
11893 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11895 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11896 return Chars (Selector_Name (Prefix (Nam))) =
11897 Chars (Current_Scope);
11898 else
11899 return False;
11900 end if;
11902 else
11903 return False;
11904 end if;
11906 else
11907 return False;
11908 end if;
11909 end Names_T;
11911 -- Start of processing for Designates_T
11913 begin
11914 if Nkind (Subt) = N_Identifier then
11915 return Chars (Subt) = Type_Id;
11917 -- Reference can be through an expanded name which has not been
11918 -- analyzed yet, and which designates enclosing scopes.
11920 elsif Nkind (Subt) = N_Selected_Component then
11921 if Names_T (Subt) then
11922 return True;
11924 -- Otherwise it must denote an entity that is already visible.
11925 -- The access definition may name a subtype of the enclosing
11926 -- type, if there is a previous incomplete declaration for it.
11928 else
11929 Find_Selected_Component (Subt);
11930 return
11931 Is_Entity_Name (Subt)
11932 and then Scope (Entity (Subt)) = Current_Scope
11933 and then
11934 (Chars (Base_Type (Entity (Subt))) = Type_Id
11935 or else
11936 (Is_Class_Wide_Type (Entity (Subt))
11937 and then
11938 Chars (Etype (Base_Type (Entity (Subt)))) =
11939 Type_Id));
11940 end if;
11942 -- A reference to the current type may appear as the prefix of
11943 -- a 'Class attribute.
11945 elsif Nkind (Subt) = N_Attribute_Reference
11946 and then Attribute_Name (Subt) = Name_Class
11947 then
11948 return Names_T (Prefix (Subt));
11950 else
11951 return False;
11952 end if;
11953 end Designates_T;
11955 ----------------
11956 -- Mentions_T --
11957 ----------------
11959 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11960 Param_Spec : Node_Id;
11962 Acc_Subprg : constant Node_Id :=
11963 Access_To_Subprogram_Definition (Acc_Def);
11965 begin
11966 if No (Acc_Subprg) then
11967 return Designates_T (Subtype_Mark (Acc_Def));
11968 end if;
11970 -- Component is an access_to_subprogram: examine its formals,
11971 -- and result definition in the case of an access_to_function.
11973 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11974 while Present (Param_Spec) loop
11975 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11976 and then Mentions_T (Parameter_Type (Param_Spec))
11977 then
11978 return True;
11980 elsif Designates_T (Parameter_Type (Param_Spec)) then
11981 return True;
11982 end if;
11984 Next (Param_Spec);
11985 end loop;
11987 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11988 if Nkind (Result_Definition (Acc_Subprg)) =
11989 N_Access_Definition
11990 then
11991 return Mentions_T (Result_Definition (Acc_Subprg));
11992 else
11993 return Designates_T (Result_Definition (Acc_Subprg));
11994 end if;
11995 end if;
11997 return False;
11998 end Mentions_T;
12000 -- Start of processing for Check_Anonymous_Access_Component
12002 begin
12003 if Present (Access_Def) and then Mentions_T (Access_Def) then
12004 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
12006 Build_Incomplete_Type_Declaration;
12007 Anon_Access := Make_Temporary (Loc, 'S');
12009 -- Create a declaration for the anonymous access type: either
12010 -- an access_to_object or an access_to_subprogram.
12012 if Present (Acc_Def) then
12013 if Nkind (Acc_Def) = N_Access_Function_Definition then
12014 Type_Def :=
12015 Make_Access_Function_Definition (Loc,
12016 Parameter_Specifications =>
12017 Parameter_Specifications (Acc_Def),
12018 Result_Definition => Result_Definition (Acc_Def));
12019 else
12020 Type_Def :=
12021 Make_Access_Procedure_Definition (Loc,
12022 Parameter_Specifications =>
12023 Parameter_Specifications (Acc_Def));
12024 end if;
12026 else
12027 Type_Def :=
12028 Make_Access_To_Object_Definition (Loc,
12029 Subtype_Indication =>
12030 Relocate_Node (Subtype_Mark (Access_Def)));
12032 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
12033 Set_All_Present (Type_Def, All_Present (Access_Def));
12034 end if;
12036 Set_Null_Exclusion_Present
12037 (Type_Def, Null_Exclusion_Present (Access_Def));
12039 Decl :=
12040 Make_Full_Type_Declaration (Loc,
12041 Defining_Identifier => Anon_Access,
12042 Type_Definition => Type_Def);
12044 Insert_Before (Typ_Decl, Decl);
12045 Analyze (Decl);
12047 -- At first sight we could add here the extra formals of an access to
12048 -- subprogram; however, it must delayed till the freeze point so that
12049 -- we know the convention.
12051 if Nkind (Comp_Def) = N_Component_Definition then
12052 Rewrite (Comp_Def,
12053 Make_Component_Definition (Loc,
12054 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
12055 else
12056 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
12057 Rewrite (Comp_Def,
12058 Make_Discriminant_Specification (Loc,
12059 Defining_Identifier => Defining_Identifier (Comp_Def),
12060 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
12061 end if;
12063 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
12064 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
12065 else
12066 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
12067 end if;
12069 Set_Is_Local_Anonymous_Access (Anon_Access);
12070 end if;
12071 end Check_Anonymous_Access_Component;
12073 ---------------------------------------
12074 -- Check_Anonymous_Access_Components --
12075 ---------------------------------------
12077 procedure Check_Anonymous_Access_Components
12078 (Typ_Decl : Node_Id;
12079 Typ : Entity_Id;
12080 Prev : Entity_Id;
12081 Comp_List : Node_Id)
12083 Comp : Node_Id;
12084 begin
12085 if No (Comp_List) then
12086 return;
12087 end if;
12089 Set_Is_Not_Self_Hidden (Typ);
12091 Comp := First (Component_Items (Comp_List));
12092 while Present (Comp) loop
12093 if Nkind (Comp) = N_Component_Declaration then
12094 Check_Anonymous_Access_Component
12095 (Typ_Decl, Typ, Prev,
12096 Component_Definition (Comp),
12097 Access_Definition (Component_Definition (Comp)));
12098 end if;
12100 Next (Comp);
12101 end loop;
12103 if Present (Variant_Part (Comp_List)) then
12104 declare
12105 V : Node_Id;
12106 begin
12107 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
12108 while Present (V) loop
12109 Check_Anonymous_Access_Components
12110 (Typ_Decl, Typ, Prev, Component_List (V));
12111 Next_Non_Pragma (V);
12112 end loop;
12113 end;
12114 end if;
12115 end Check_Anonymous_Access_Components;
12117 ----------------------
12118 -- Check_Completion --
12119 ----------------------
12121 procedure Check_Completion (Body_Id : Node_Id := Empty) is
12122 E : Entity_Id;
12124 procedure Post_Error;
12125 -- Post error message for lack of completion for entity E
12127 ----------------
12128 -- Post_Error --
12129 ----------------
12131 procedure Post_Error is
12132 procedure Missing_Body;
12133 -- Output missing body message
12135 ------------------
12136 -- Missing_Body --
12137 ------------------
12139 procedure Missing_Body is
12140 begin
12141 -- Spec is in same unit, so we can post on spec
12143 if In_Same_Source_Unit (Body_Id, E) then
12144 Error_Msg_N ("missing body for &", E);
12146 -- Spec is in a separate unit, so we have to post on the body
12148 else
12149 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
12150 end if;
12151 end Missing_Body;
12153 -- Start of processing for Post_Error
12155 begin
12156 if not Comes_From_Source (E) then
12157 if Ekind (E) in E_Task_Type | E_Protected_Type then
12159 -- It may be an anonymous protected type created for a
12160 -- single variable. Post error on variable, if present.
12162 declare
12163 Var : Entity_Id;
12165 begin
12166 Var := First_Entity (Current_Scope);
12167 while Present (Var) loop
12168 exit when Etype (Var) = E
12169 and then Comes_From_Source (Var);
12171 Next_Entity (Var);
12172 end loop;
12174 if Present (Var) then
12175 E := Var;
12176 end if;
12177 end;
12178 end if;
12179 end if;
12181 -- If a generated entity has no completion, then either previous
12182 -- semantic errors have disabled the expansion phase, or else we had
12183 -- missing subunits, or else we are compiling without expansion,
12184 -- or else something is very wrong.
12186 if not Comes_From_Source (E) then
12187 pragma Assert
12188 (Serious_Errors_Detected > 0
12189 or else Configurable_Run_Time_Violations > 0
12190 or else Subunits_Missing
12191 or else not Expander_Active);
12192 return;
12194 -- Here for source entity
12196 else
12197 -- Here if no body to post the error message, so we post the error
12198 -- on the declaration that has no completion. This is not really
12199 -- the right place to post it, think about this later ???
12201 if No (Body_Id) then
12202 if Is_Type (E) then
12203 Error_Msg_NE
12204 ("missing full declaration for }", Parent (E), E);
12205 else
12206 Error_Msg_NE ("missing body for &", Parent (E), E);
12207 end if;
12209 -- Package body has no completion for a declaration that appears
12210 -- in the corresponding spec. Post error on the body, with a
12211 -- reference to the non-completed declaration.
12213 else
12214 Error_Msg_Sloc := Sloc (E);
12216 if Is_Type (E) then
12217 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
12219 elsif Is_Overloadable (E)
12220 and then Current_Entity_In_Scope (E) /= E
12221 then
12222 -- It may be that the completion is mistyped and appears as
12223 -- a distinct overloading of the entity.
12225 declare
12226 Candidate : constant Entity_Id :=
12227 Current_Entity_In_Scope (E);
12228 Decl : constant Node_Id :=
12229 Unit_Declaration_Node (Candidate);
12231 begin
12232 if Is_Overloadable (Candidate)
12233 and then Ekind (Candidate) = Ekind (E)
12234 and then Nkind (Decl) = N_Subprogram_Body
12235 and then Acts_As_Spec (Decl)
12236 then
12237 Check_Type_Conformant (Candidate, E);
12239 else
12240 Missing_Body;
12241 end if;
12242 end;
12244 else
12245 Missing_Body;
12246 end if;
12247 end if;
12248 end if;
12249 end Post_Error;
12251 -- Local variables
12253 Pack_Id : constant Entity_Id := Current_Scope;
12255 -- Start of processing for Check_Completion
12257 begin
12258 E := First_Entity (Pack_Id);
12259 while Present (E) loop
12260 if Is_Intrinsic_Subprogram (E) then
12261 null;
12263 -- The following situation requires special handling: a child unit
12264 -- that appears in the context clause of the body of its parent:
12266 -- procedure Parent.Child (...);
12268 -- with Parent.Child;
12269 -- package body Parent is
12271 -- Here Parent.Child appears as a local entity, but should not be
12272 -- flagged as requiring completion, because it is a compilation
12273 -- unit.
12275 -- Ignore missing completion for a subprogram that does not come from
12276 -- source (including the _Call primitive operation of RAS types,
12277 -- which has to have the flag Comes_From_Source for other purposes):
12278 -- we assume that the expander will provide the missing completion.
12279 -- In case of previous errors, other expansion actions that provide
12280 -- bodies for null procedures with not be invoked, so inhibit message
12281 -- in those cases.
12283 -- Note that E_Operator is not in the list that follows, because
12284 -- this kind is reserved for predefined operators, that are
12285 -- intrinsic and do not need completion.
12287 elsif Ekind (E) in E_Function
12288 | E_Procedure
12289 | E_Generic_Function
12290 | E_Generic_Procedure
12291 then
12292 if Has_Completion (E) then
12293 null;
12295 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12296 null;
12298 elsif Is_Subprogram (E)
12299 and then (not Comes_From_Source (E)
12300 or else Chars (E) = Name_uCall)
12301 then
12302 null;
12304 elsif
12305 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12306 then
12307 null;
12309 elsif Nkind (Parent (E)) = N_Procedure_Specification
12310 and then Null_Present (Parent (E))
12311 and then Serious_Errors_Detected > 0
12312 then
12313 null;
12315 else
12316 Post_Error;
12317 end if;
12319 elsif Is_Entry (E) then
12320 if not Has_Completion (E)
12321 and then Ekind (Scope (E)) = E_Protected_Type
12322 then
12323 Post_Error;
12324 end if;
12326 elsif Is_Package_Or_Generic_Package (E) then
12327 if Unit_Requires_Body (E) then
12328 if not Has_Completion (E)
12329 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12330 N_Compilation_Unit
12331 then
12332 Post_Error;
12333 end if;
12335 elsif not Is_Child_Unit (E) then
12336 May_Need_Implicit_Body (E);
12337 end if;
12339 -- A formal incomplete type (Ada 2012) does not require a completion;
12340 -- other incomplete type declarations do.
12342 elsif Ekind (E) = E_Incomplete_Type then
12343 if No (Underlying_Type (E))
12344 and then not Is_Generic_Type (E)
12345 then
12346 Post_Error;
12347 end if;
12349 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12350 if not Has_Completion (E) then
12351 Post_Error;
12352 end if;
12354 -- A single task declared in the current scope is a constant, verify
12355 -- that the body of its anonymous type is in the same scope. If the
12356 -- task is defined elsewhere, this may be a renaming declaration for
12357 -- which no completion is needed.
12359 elsif Ekind (E) = E_Constant then
12360 if Ekind (Etype (E)) = E_Task_Type
12361 and then not Has_Completion (Etype (E))
12362 and then Scope (Etype (E)) = Current_Scope
12363 then
12364 Post_Error;
12365 end if;
12367 elsif Ekind (E) = E_Record_Type then
12368 if Is_Tagged_Type (E) then
12369 Check_Abstract_Overriding (E);
12370 Check_Conventions (E);
12371 end if;
12373 Check_Aliased_Component_Types (E);
12375 elsif Ekind (E) = E_Array_Type then
12376 Check_Aliased_Component_Types (E);
12378 end if;
12380 Next_Entity (E);
12381 end loop;
12382 end Check_Completion;
12384 -------------------------------------
12385 -- Check_Constraining_Discriminant --
12386 -------------------------------------
12388 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12390 New_Type : constant Entity_Id := Etype (New_Disc);
12391 Old_Type : Entity_Id;
12393 begin
12394 -- If the record type contains an array constrained by the discriminant
12395 -- but with some different bound, the compiler tries to create a smaller
12396 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12397 -- In this case, where the discriminant type is a scalar type, the check
12398 -- must use the original discriminant type in the parent declaration.
12400 if Is_Scalar_Type (New_Type) then
12401 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12402 else
12403 Old_Type := Etype (Old_Disc);
12404 end if;
12406 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12407 Error_Msg_N
12408 ("subtype must be statically compatible with parent discriminant",
12409 New_Disc);
12411 if not Predicates_Compatible (New_Type, Old_Type) then
12412 Error_Msg_N
12413 ("\subtype predicate is not compatible with parent discriminant",
12414 New_Disc);
12415 end if;
12416 end if;
12417 end Check_Constraining_Discriminant;
12419 ------------------------------------
12420 -- Check_CPP_Type_Has_No_Defaults --
12421 ------------------------------------
12423 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12424 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12425 Clist : Node_Id;
12426 Comp : Node_Id;
12428 begin
12429 -- Obtain the component list
12431 if Nkind (Tdef) = N_Record_Definition then
12432 Clist := Component_List (Tdef);
12433 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12434 Clist := Component_List (Record_Extension_Part (Tdef));
12435 end if;
12437 -- Check all components to ensure no default expressions
12439 if Present (Clist) then
12440 Comp := First_Non_Pragma (Component_Items (Clist));
12441 while Present (Comp) loop
12442 if Present (Expression (Comp)) then
12443 Error_Msg_N
12444 ("component of imported 'C'P'P type cannot have "
12445 & "default expression", Expression (Comp));
12446 end if;
12448 Next_Non_Pragma (Comp);
12449 end loop;
12450 end if;
12451 end Check_CPP_Type_Has_No_Defaults;
12453 ----------------------------
12454 -- Check_Delta_Expression --
12455 ----------------------------
12457 procedure Check_Delta_Expression (E : Node_Id) is
12458 begin
12459 if not (Is_Real_Type (Etype (E))) then
12460 Wrong_Type (E, Any_Real);
12462 elsif not Is_OK_Static_Expression (E) then
12463 Flag_Non_Static_Expr
12464 ("non-static expression used for delta value!", E);
12466 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12467 Error_Msg_N ("delta expression must be positive", E);
12469 else
12470 return;
12471 end if;
12473 -- If any of above errors occurred, then replace the incorrect
12474 -- expression by the real 0.1, which should prevent further errors.
12476 Rewrite (E,
12477 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12478 Analyze_And_Resolve (E, Standard_Float);
12479 end Check_Delta_Expression;
12481 -----------------------------
12482 -- Check_Digits_Expression --
12483 -----------------------------
12485 procedure Check_Digits_Expression (E : Node_Id) is
12486 begin
12487 if not (Is_Integer_Type (Etype (E))) then
12488 Wrong_Type (E, Any_Integer);
12490 elsif not Is_OK_Static_Expression (E) then
12491 Flag_Non_Static_Expr
12492 ("non-static expression used for digits value!", E);
12494 elsif Expr_Value (E) <= 0 then
12495 Error_Msg_N ("digits value must be greater than zero", E);
12497 else
12498 return;
12499 end if;
12501 -- If any of above errors occurred, then replace the incorrect
12502 -- expression by the integer 1, which should prevent further errors.
12504 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12505 Analyze_And_Resolve (E, Standard_Integer);
12507 end Check_Digits_Expression;
12509 --------------------------
12510 -- Check_Initialization --
12511 --------------------------
12513 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12514 begin
12515 -- Special processing for limited types
12517 if Is_Limited_Type (T)
12518 and then not In_Instance
12519 and then not In_Inlined_Body
12520 then
12521 if not OK_For_Limited_Init (T, Exp) then
12523 -- In GNAT mode, this is just a warning, to allow it to be evilly
12524 -- turned off. Otherwise it is a real error.
12526 if GNAT_Mode then
12527 Error_Msg_N
12528 ("??cannot initialize entities of limited type!", Exp);
12530 elsif Ada_Version < Ada_2005 then
12532 -- The side effect removal machinery may generate illegal Ada
12533 -- code to avoid the usage of access types and 'reference in
12534 -- SPARK mode. Since this is legal code with respect to theorem
12535 -- proving, do not emit the error.
12537 if GNATprove_Mode
12538 and then Nkind (Exp) = N_Function_Call
12539 and then Nkind (Parent (Exp)) = N_Object_Declaration
12540 and then not Comes_From_Source
12541 (Defining_Identifier (Parent (Exp)))
12542 then
12543 null;
12545 else
12546 Error_Msg_N
12547 ("cannot initialize entities of limited type", Exp);
12548 Explain_Limited_Type (T, Exp);
12549 end if;
12551 else
12552 -- Specialize error message according to kind of illegal
12553 -- initial expression. We check the Original_Node to cover
12554 -- cases where the initialization expression of an object
12555 -- declaration generated by the compiler has been rewritten
12556 -- (such as for dispatching calls).
12558 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12559 and then
12560 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12561 then
12562 -- No error for internally-generated object declarations,
12563 -- which can come from build-in-place assignment statements.
12565 if Nkind (Parent (Exp)) = N_Object_Declaration
12566 and then not Comes_From_Source
12567 (Defining_Identifier (Parent (Exp)))
12568 then
12569 null;
12571 else
12572 Error_Msg_N
12573 ("illegal context for call to function with limited "
12574 & "result", Exp);
12575 end if;
12577 else
12578 Error_Msg_N
12579 ("initialization of limited object requires aggregate or "
12580 & "function call", Exp);
12581 end if;
12582 end if;
12583 end if;
12584 end if;
12586 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12587 -- set unless we can be sure that no range check is required.
12589 if not Expander_Active
12590 and then Is_Scalar_Type (T)
12591 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12592 then
12593 Set_Do_Range_Check (Exp);
12594 end if;
12595 end Check_Initialization;
12597 ----------------------
12598 -- Check_Interfaces --
12599 ----------------------
12601 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12602 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12604 Iface : Node_Id;
12605 Iface_Def : Node_Id;
12606 Iface_Typ : Entity_Id;
12607 Parent_Node : Node_Id;
12609 Is_Task : Boolean := False;
12610 -- Set True if parent type or any progenitor is a task interface
12612 Is_Protected : Boolean := False;
12613 -- Set True if parent type or any progenitor is a protected interface
12615 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12616 -- Check that a progenitor is compatible with declaration. If an error
12617 -- message is output, it is posted on Error_Node.
12619 ------------------
12620 -- Check_Ifaces --
12621 ------------------
12623 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12624 Iface_Id : constant Entity_Id :=
12625 Defining_Identifier (Parent (Iface_Def));
12626 Type_Def : Node_Id;
12628 begin
12629 if Nkind (N) = N_Private_Extension_Declaration then
12630 Type_Def := N;
12631 else
12632 Type_Def := Type_Definition (N);
12633 end if;
12635 if Is_Task_Interface (Iface_Id) then
12636 Is_Task := True;
12638 elsif Is_Protected_Interface (Iface_Id) then
12639 Is_Protected := True;
12640 end if;
12642 if Is_Synchronized_Interface (Iface_Id) then
12644 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12645 -- extension derived from a synchronized interface must explicitly
12646 -- be declared synchronized, because the full view will be a
12647 -- synchronized type.
12649 if Nkind (N) = N_Private_Extension_Declaration then
12650 if not Synchronized_Present (N) then
12651 Error_Msg_NE
12652 ("private extension of& must be explicitly synchronized",
12653 N, Iface_Id);
12654 end if;
12656 -- However, by 3.9.4(16/2), a full type that is a record extension
12657 -- is never allowed to derive from a synchronized interface (note
12658 -- that interfaces must be excluded from this check, because those
12659 -- are represented by derived type definitions in some cases).
12661 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12662 and then not Interface_Present (Type_Definition (N))
12663 then
12664 Error_Msg_N ("record extension cannot derive from synchronized "
12665 & "interface", Error_Node);
12666 end if;
12667 end if;
12669 -- Check that the characteristics of the progenitor are compatible
12670 -- with the explicit qualifier in the declaration.
12671 -- The check only applies to qualifiers that come from source.
12672 -- Limited_Present also appears in the declaration of corresponding
12673 -- records, and the check does not apply to them.
12675 if Limited_Present (Type_Def)
12676 and then not
12677 Is_Concurrent_Record_Type (Defining_Identifier (N))
12678 then
12679 if Is_Limited_Interface (Parent_Type)
12680 and then not Is_Limited_Interface (Iface_Id)
12681 then
12682 Error_Msg_NE
12683 ("progenitor & must be limited interface",
12684 Error_Node, Iface_Id);
12686 elsif
12687 (Task_Present (Iface_Def)
12688 or else Protected_Present (Iface_Def)
12689 or else Synchronized_Present (Iface_Def))
12690 and then Nkind (N) /= N_Private_Extension_Declaration
12691 and then not Error_Posted (N)
12692 then
12693 Error_Msg_NE
12694 ("progenitor & must be limited interface",
12695 Error_Node, Iface_Id);
12696 end if;
12698 -- Protected interfaces can only inherit from limited, synchronized
12699 -- or protected interfaces.
12701 elsif Nkind (N) = N_Full_Type_Declaration
12702 and then Protected_Present (Type_Def)
12703 then
12704 if Limited_Present (Iface_Def)
12705 or else Synchronized_Present (Iface_Def)
12706 or else Protected_Present (Iface_Def)
12707 then
12708 null;
12710 elsif Task_Present (Iface_Def) then
12711 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12712 & "from task interface", Error_Node);
12714 else
12715 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12716 & "from non-limited interface", Error_Node);
12717 end if;
12719 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12720 -- limited and synchronized.
12722 elsif Synchronized_Present (Type_Def) then
12723 if Limited_Present (Iface_Def)
12724 or else Synchronized_Present (Iface_Def)
12725 then
12726 null;
12728 elsif Protected_Present (Iface_Def)
12729 and then Nkind (N) /= N_Private_Extension_Declaration
12730 then
12731 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12732 & "from protected interface", Error_Node);
12734 elsif Task_Present (Iface_Def)
12735 and then Nkind (N) /= N_Private_Extension_Declaration
12736 then
12737 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12738 & "from task interface", Error_Node);
12740 elsif not Is_Limited_Interface (Iface_Id) then
12741 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12742 & "from non-limited interface", Error_Node);
12743 end if;
12745 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12746 -- synchronized or task interfaces.
12748 elsif Nkind (N) = N_Full_Type_Declaration
12749 and then Task_Present (Type_Def)
12750 then
12751 if Limited_Present (Iface_Def)
12752 or else Synchronized_Present (Iface_Def)
12753 or else Task_Present (Iface_Def)
12754 then
12755 null;
12757 elsif Protected_Present (Iface_Def) then
12758 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12759 & "protected interface", Error_Node);
12761 else
12762 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12763 & "non-limited interface", Error_Node);
12764 end if;
12765 end if;
12766 end Check_Ifaces;
12768 -- Start of processing for Check_Interfaces
12770 begin
12771 if Is_Interface (Parent_Type) then
12772 if Is_Task_Interface (Parent_Type) then
12773 Is_Task := True;
12775 elsif Is_Protected_Interface (Parent_Type) then
12776 Is_Protected := True;
12777 end if;
12778 end if;
12780 if Nkind (N) = N_Private_Extension_Declaration then
12782 -- Check that progenitors are compatible with declaration
12784 Iface := First (Interface_List (Def));
12785 while Present (Iface) loop
12786 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12788 Parent_Node := Parent (Base_Type (Iface_Typ));
12789 Iface_Def := Type_Definition (Parent_Node);
12791 if not Is_Interface (Iface_Typ) then
12792 Diagnose_Interface (Iface, Iface_Typ);
12793 else
12794 Check_Ifaces (Iface_Def, Iface);
12795 end if;
12797 Next (Iface);
12798 end loop;
12800 if Is_Task and Is_Protected then
12801 Error_Msg_N
12802 ("type cannot derive from task and protected interface", N);
12803 end if;
12805 return;
12806 end if;
12808 -- Full type declaration of derived type.
12809 -- Check compatibility with parent if it is interface type
12811 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12812 and then Is_Interface (Parent_Type)
12813 then
12814 Parent_Node := Parent (Parent_Type);
12816 -- More detailed checks for interface varieties
12818 Check_Ifaces
12819 (Iface_Def => Type_Definition (Parent_Node),
12820 Error_Node => Subtype_Indication (Type_Definition (N)));
12821 end if;
12823 Iface := First (Interface_List (Def));
12824 while Present (Iface) loop
12825 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12827 Parent_Node := Parent (Base_Type (Iface_Typ));
12828 Iface_Def := Type_Definition (Parent_Node);
12830 if not Is_Interface (Iface_Typ) then
12831 Diagnose_Interface (Iface, Iface_Typ);
12833 else
12834 -- "The declaration of a specific descendant of an interface
12835 -- type freezes the interface type" RM 13.14
12837 Freeze_Before (N, Iface_Typ);
12838 Check_Ifaces (Iface_Def, Error_Node => Iface);
12839 end if;
12841 Next (Iface);
12842 end loop;
12844 if Is_Task and Is_Protected then
12845 Error_Msg_N
12846 ("type cannot derive from task and protected interface", N);
12847 end if;
12848 end Check_Interfaces;
12850 ------------------------------------
12851 -- Check_Or_Process_Discriminants --
12852 ------------------------------------
12854 -- If an incomplete or private type declaration was already given for the
12855 -- type, the discriminants may have already been processed if they were
12856 -- present on the incomplete declaration. In this case a full conformance
12857 -- check has been performed in Find_Type_Name, and we then recheck here
12858 -- some properties that can't be checked on the partial view alone.
12859 -- Otherwise we call Process_Discriminants.
12861 procedure Check_Or_Process_Discriminants
12862 (N : Node_Id;
12863 T : Entity_Id;
12864 Prev : Entity_Id := Empty)
12866 begin
12867 if Has_Discriminants (T) then
12869 -- Discriminants are already set on T if they were already present
12870 -- on the partial view. Make them visible to component declarations.
12872 declare
12873 D : Entity_Id;
12874 -- Discriminant on T (full view) referencing expr on partial view
12876 Prev_D : Entity_Id;
12877 -- Entity of corresponding discriminant on partial view
12879 New_D : Node_Id;
12880 -- Discriminant specification for full view, expression is
12881 -- the syntactic copy on full view (which has been checked for
12882 -- conformance with partial view), only used here to post error
12883 -- message.
12885 begin
12886 D := First_Discriminant (T);
12887 New_D := First (Discriminant_Specifications (N));
12888 while Present (D) loop
12889 Prev_D := Current_Entity (D);
12890 Set_Current_Entity (D);
12891 Set_Is_Immediately_Visible (D);
12892 Set_Homonym (D, Prev_D);
12894 -- Handle the case where there is an untagged partial view and
12895 -- the full view is tagged: must disallow discriminants with
12896 -- defaults, unless compiling for Ada 2012, which allows a
12897 -- limited tagged type to have defaulted discriminants (see
12898 -- AI05-0214). However, suppress error here if it was already
12899 -- reported on the default expression of the partial view.
12901 if Is_Tagged_Type (T)
12902 and then Present (Expression (Parent (D)))
12903 and then (not Is_Limited_Type (Current_Scope)
12904 or else Ada_Version < Ada_2012)
12905 and then not Error_Posted (Expression (Parent (D)))
12906 then
12907 if Ada_Version >= Ada_2012 then
12908 Error_Msg_N
12909 ("discriminants of nonlimited tagged type cannot have "
12910 & "defaults",
12911 Expression (New_D));
12912 else
12913 Error_Msg_N
12914 ("discriminants of tagged type cannot have defaults",
12915 Expression (New_D));
12916 end if;
12917 end if;
12919 -- Ada 2005 (AI-230): Access discriminant allowed in
12920 -- non-limited record types.
12922 if Ada_Version < Ada_2005 then
12924 -- This restriction gets applied to the full type here. It
12925 -- has already been applied earlier to the partial view.
12927 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12928 end if;
12930 Next_Discriminant (D);
12931 Next (New_D);
12932 end loop;
12933 end;
12935 elsif Present (Discriminant_Specifications (N)) then
12936 Process_Discriminants (N, Prev);
12937 end if;
12938 end Check_Or_Process_Discriminants;
12940 ----------------------
12941 -- Check_Real_Bound --
12942 ----------------------
12944 procedure Check_Real_Bound (Bound : Node_Id) is
12945 begin
12946 if not Is_Real_Type (Etype (Bound)) then
12947 Error_Msg_N
12948 ("bound in real type definition must be of real type", Bound);
12950 elsif not Is_OK_Static_Expression (Bound) then
12951 Flag_Non_Static_Expr
12952 ("non-static expression used for real type bound!", Bound);
12954 else
12955 return;
12956 end if;
12958 Rewrite
12959 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12960 Analyze (Bound);
12961 Resolve (Bound, Standard_Float);
12962 end Check_Real_Bound;
12964 ------------------------------
12965 -- Complete_Private_Subtype --
12966 ------------------------------
12968 procedure Complete_Private_Subtype
12969 (Priv : Entity_Id;
12970 Full : Entity_Id;
12971 Full_Base : Entity_Id;
12972 Related_Nod : Node_Id)
12974 Save_Next_Entity : Entity_Id;
12975 Save_Homonym : Entity_Id;
12977 begin
12978 -- Set semantic attributes for (implicit) private subtype completion.
12979 -- If the full type has no discriminants, then it is a copy of the
12980 -- full view of the base. Otherwise, it is a subtype of the base with
12981 -- a possible discriminant constraint. Save and restore the original
12982 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12983 -- not corrupt the entity chain.
12985 Save_Next_Entity := Next_Entity (Full);
12986 Save_Homonym := Homonym (Priv);
12988 if Is_Private_Type (Full_Base)
12989 or else Is_Record_Type (Full_Base)
12990 or else Is_Concurrent_Type (Full_Base)
12991 then
12992 Copy_Node (Priv, Full);
12994 -- Note that the Etype of the full view is the same as the Etype of
12995 -- the partial view. In this fashion, the subtype has access to the
12996 -- correct view of the parent.
12998 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12999 Set_Has_Unknown_Discriminants
13000 (Full, Has_Unknown_Discriminants (Full_Base));
13001 Set_First_Entity (Full, First_Entity (Full_Base));
13002 Set_Last_Entity (Full, Last_Entity (Full_Base));
13004 -- If the underlying base type is constrained, we know that the
13005 -- full view of the subtype is constrained as well (the converse
13006 -- is not necessarily true).
13008 if Is_Constrained (Full_Base) then
13009 Set_Is_Constrained (Full);
13010 end if;
13012 else
13013 Copy_Node (Full_Base, Full);
13015 -- The following subtlety with the Etype of the full view needs to be
13016 -- taken into account here. One could think that it must naturally be
13017 -- set to the base type of the full base:
13019 -- Set_Etype (Full, Base_Type (Full_Base));
13021 -- so that the full view becomes a subtype of the full base when the
13022 -- latter is a base type, which must for example happen when the full
13023 -- base is declared as derived type. That's also correct if the full
13024 -- base is declared as an array type, or a floating-point type, or a
13025 -- fixed-point type, or a signed integer type, as these declarations
13026 -- create an implicit base type and a first subtype so the Etype of
13027 -- the full views must be the implicit base type. But that's wrong
13028 -- if the full base is declared as an access type, or an enumeration
13029 -- type, or a modular integer type, as these declarations directly
13030 -- create a base type, i.e. with Etype pointing to itself. Moreover
13031 -- the full base being declared in the private part, i.e. when the
13032 -- views are swapped, the end result is that the Etype of the full
13033 -- base is set to its private view in this case and that we need to
13034 -- propagate this setting to the full view in order for the subtype
13035 -- to be compatible with the base type.
13037 if Is_Base_Type (Full_Base)
13038 and then (Is_Derived_Type (Full_Base)
13039 or else Ekind (Full_Base) in Array_Kind
13040 or else Ekind (Full_Base) in Fixed_Point_Kind
13041 or else Ekind (Full_Base) in Float_Kind
13042 or else Ekind (Full_Base) in Signed_Integer_Kind)
13043 then
13044 Set_Etype (Full, Full_Base);
13045 end if;
13047 Set_Chars (Full, Chars (Priv));
13048 Set_Sloc (Full, Sloc (Priv));
13049 Conditional_Delay (Full, Priv);
13050 end if;
13052 Link_Entities (Full, Save_Next_Entity);
13053 Set_Homonym (Full, Save_Homonym);
13054 Set_Associated_Node_For_Itype (Full, Related_Nod);
13056 if Ekind (Full) in Incomplete_Or_Private_Kind then
13057 Reinit_Field_To_Zero (Full, F_Private_Dependents);
13058 end if;
13060 -- Set common attributes for all subtypes: kind, convention, etc.
13062 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
13063 Set_Is_Not_Self_Hidden (Full);
13064 Set_Convention (Full, Convention (Full_Base));
13065 Set_Is_First_Subtype (Full, False);
13066 Set_Scope (Full, Scope (Priv));
13067 Set_Size_Info (Full, Full_Base);
13068 Copy_RM_Size (To => Full, From => Full_Base);
13069 Set_Is_Itype (Full);
13071 -- A subtype of a private-type-without-discriminants, whose full-view
13072 -- has discriminants with default expressions, is not constrained.
13074 if not Has_Discriminants (Priv) then
13075 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
13077 if Has_Discriminants (Full_Base) then
13078 Set_Discriminant_Constraint
13079 (Full, Discriminant_Constraint (Full_Base));
13081 -- The partial view may have been indefinite, the full view
13082 -- might not be.
13084 Set_Has_Unknown_Discriminants
13085 (Full, Has_Unknown_Discriminants (Full_Base));
13086 end if;
13087 end if;
13089 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
13090 Set_Depends_On_Private (Full, Has_Private_Component (Full));
13092 -- Freeze the private subtype entity if its parent is delayed, and not
13093 -- already frozen. We skip this processing if the type is an anonymous
13094 -- subtype of a record component, or is the corresponding record of a
13095 -- protected type, since these are processed when the enclosing type
13096 -- is frozen. If the parent type is declared in a nested package then
13097 -- the freezing of the private and full views also happens later.
13099 if not Is_Type (Scope (Full)) then
13100 if Is_Itype (Priv)
13101 and then In_Same_Source_Unit (Full, Full_Base)
13102 and then Scope (Full_Base) /= Scope (Full)
13103 then
13104 Set_Has_Delayed_Freeze (Full);
13105 Set_Has_Delayed_Freeze (Priv);
13107 else
13108 Set_Has_Delayed_Freeze (Full,
13109 Has_Delayed_Freeze (Full_Base)
13110 and then not Is_Frozen (Full_Base));
13111 end if;
13112 end if;
13114 Set_Freeze_Node (Full, Empty);
13115 Set_Is_Frozen (Full, False);
13117 if Has_Discriminants (Full) then
13118 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
13119 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
13121 if Has_Unknown_Discriminants (Full) then
13122 Set_Discriminant_Constraint (Full, No_Elist);
13123 end if;
13124 end if;
13126 if Ekind (Full_Base) = E_Record_Type
13127 and then Has_Discriminants (Full_Base)
13128 and then Has_Discriminants (Priv) -- might not, if errors
13129 and then not Has_Unknown_Discriminants (Priv)
13130 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
13131 then
13132 Create_Constrained_Components
13133 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
13135 -- If the full base is itself derived from private, build a congruent
13136 -- subtype of its underlying full view, for use by the back end.
13138 elsif Is_Private_Type (Full_Base)
13139 and then Present (Underlying_Full_View (Full_Base))
13140 then
13141 declare
13142 Underlying_Full_Base : constant Entity_Id
13143 := Underlying_Full_View (Full_Base);
13144 Underlying_Full : constant Entity_Id
13145 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13146 begin
13147 Set_Is_Itype (Underlying_Full);
13148 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
13149 Complete_Private_Subtype
13150 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
13151 Set_Underlying_Full_View (Full, Underlying_Full);
13152 Set_Is_Underlying_Full_View (Underlying_Full);
13153 end;
13155 elsif Is_Record_Type (Full_Base) then
13157 -- Show Full is simply a renaming of Full_Base
13159 Set_Cloned_Subtype (Full, Full_Base);
13160 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13162 -- Propagate predicates
13164 Propagate_Predicate_Attributes (Full, Full_Base);
13165 end if;
13167 -- It is unsafe to share the bounds of a scalar type, because the Itype
13168 -- is elaborated on demand, and if a bound is nonstatic, then different
13169 -- orders of elaboration in different units will lead to different
13170 -- external symbols.
13172 if Is_Scalar_Type (Full_Base) then
13173 Set_Scalar_Range (Full,
13174 Make_Range (Sloc (Related_Nod),
13175 Low_Bound =>
13176 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
13177 High_Bound =>
13178 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
13180 -- This completion inherits the bounds of the full parent, but if
13181 -- the parent is an unconstrained floating point type, so is the
13182 -- completion.
13184 if Is_Floating_Point_Type (Full_Base) then
13185 Set_Includes_Infinities
13186 (Scalar_Range (Full), Has_Infinities (Full_Base));
13187 end if;
13188 end if;
13190 -- ??? It seems that a lot of fields are missing that should be copied
13191 -- from Full_Base to Full. Here are some that are introduced in a
13192 -- non-disruptive way but a cleanup is necessary.
13194 if Is_Tagged_Type (Full_Base) then
13195 Set_Is_Tagged_Type (Full);
13196 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
13198 Set_Direct_Primitive_Operations
13199 (Full, Direct_Primitive_Operations (Full_Base));
13200 Set_No_Tagged_Streams_Pragma
13201 (Full, No_Tagged_Streams_Pragma (Full_Base));
13203 if Is_Interface (Full_Base) then
13204 Set_Is_Interface (Full);
13205 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
13206 end if;
13208 -- Inherit class_wide type of full_base in case the partial view was
13209 -- not tagged. Otherwise it has already been created when the private
13210 -- subtype was analyzed.
13212 if No (Class_Wide_Type (Full)) then
13213 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
13214 end if;
13216 -- If this is a subtype of a protected or task type, constrain its
13217 -- corresponding record, unless this is a subtype without constraints,
13218 -- i.e. a simple renaming as with an actual subtype in an instance.
13220 elsif Is_Concurrent_Type (Full_Base) then
13221 if Has_Discriminants (Full)
13222 and then Present (Corresponding_Record_Type (Full_Base))
13223 and then
13224 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
13225 then
13226 Set_Corresponding_Record_Type (Full,
13227 Constrain_Corresponding_Record
13228 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
13230 else
13231 Set_Corresponding_Record_Type (Full,
13232 Corresponding_Record_Type (Full_Base));
13233 end if;
13234 end if;
13236 -- Link rep item chain, and also setting of Has_Predicates from private
13237 -- subtype to full subtype, since we will need these on the full subtype
13238 -- to create the predicate function. Note that the full subtype may
13239 -- already have rep items, inherited from the full view of the base
13240 -- type, so we must be sure not to overwrite these entries.
13242 declare
13243 Append : Boolean;
13244 Item : Node_Id;
13245 Next_Item : Node_Id;
13246 Priv_Item : Node_Id;
13248 begin
13249 Item := First_Rep_Item (Full);
13250 Priv_Item := First_Rep_Item (Priv);
13252 -- If no existing rep items on full type, we can just link directly
13253 -- to the list of items on the private type, if any exist.. Same if
13254 -- the rep items are only those inherited from the base
13256 if (No (Item)
13257 or else Nkind (Item) /= N_Aspect_Specification
13258 or else Entity (Item) = Full_Base)
13259 and then Present (First_Rep_Item (Priv))
13260 then
13261 Set_First_Rep_Item (Full, Priv_Item);
13263 -- Otherwise, search to the end of items currently linked to the full
13264 -- subtype and append the private items to the end. However, if Priv
13265 -- and Full already have the same list of rep items, then the append
13266 -- is not done, as that would create a circularity.
13268 -- The partial view may have a predicate and the rep item lists of
13269 -- both views agree when inherited from the same ancestor. In that
13270 -- case, simply propagate the list from one view to the other.
13271 -- A more complex analysis needed here ???
13273 elsif Present (Priv_Item)
13274 and then Item = Next_Rep_Item (Priv_Item)
13275 then
13276 Set_First_Rep_Item (Full, Priv_Item);
13278 elsif Item /= Priv_Item then
13279 Append := True;
13280 loop
13281 Next_Item := Next_Rep_Item (Item);
13282 exit when No (Next_Item);
13283 Item := Next_Item;
13285 -- If the private view has aspect specifications, the full view
13286 -- inherits them. Since these aspects may already have been
13287 -- attached to the full view during derivation, do not append
13288 -- them if already present.
13290 if Item = First_Rep_Item (Priv) then
13291 Append := False;
13292 exit;
13293 end if;
13294 end loop;
13296 -- And link the private type items at the end of the chain
13298 if Append then
13299 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13300 end if;
13301 end if;
13302 end;
13304 -- Make sure Has_Predicates is set on full type if it is set on the
13305 -- private type. Note that it may already be set on the full type and
13306 -- if so, we don't want to unset it. Similarly, propagate information
13307 -- about delayed aspects, because the corresponding pragmas must be
13308 -- analyzed when one of the views is frozen. This last step is needed
13309 -- in particular when the full type is a scalar type for which an
13310 -- anonymous base type is constructed.
13312 -- The predicate functions are generated either at the freeze point
13313 -- of the type or at the end of the visible part, and we must avoid
13314 -- generating them twice.
13316 Propagate_Predicate_Attributes (Full, Priv);
13318 if Has_Delayed_Aspects (Priv) then
13319 Set_Has_Delayed_Aspects (Full);
13320 end if;
13321 end Complete_Private_Subtype;
13323 ----------------------------
13324 -- Constant_Redeclaration --
13325 ----------------------------
13327 procedure Constant_Redeclaration
13328 (Id : Entity_Id;
13329 N : Node_Id;
13330 T : out Entity_Id)
13332 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13333 Obj_Def : constant Node_Id := Object_Definition (N);
13334 New_T : Entity_Id;
13336 procedure Check_Possible_Deferred_Completion
13337 (Prev_Id : Entity_Id;
13338 Curr_Obj_Def : Node_Id);
13339 -- Determine whether the two object definitions describe the partial
13340 -- and the full view of a constrained deferred constant. Generate
13341 -- a subtype for the full view and verify that it statically matches
13342 -- the subtype of the partial view.
13344 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13345 -- If deferred constant is an access type initialized with an allocator,
13346 -- check whether there is an illegal recursion in the definition,
13347 -- through a default value of some record subcomponent. This is normally
13348 -- detected when generating init procs, but requires this additional
13349 -- mechanism when expansion is disabled.
13351 ----------------------------------------
13352 -- Check_Possible_Deferred_Completion --
13353 ----------------------------------------
13355 procedure Check_Possible_Deferred_Completion
13356 (Prev_Id : Entity_Id;
13357 Curr_Obj_Def : Node_Id)
13359 Curr_Typ : Entity_Id;
13360 Prev_Typ : constant Entity_Id := Etype (Prev_Id);
13361 Anon_Acc : constant Boolean := Is_Anonymous_Access_Type (Prev_Typ);
13362 Mismatch : Boolean := False;
13363 begin
13364 if Anon_Acc then
13365 null;
13366 elsif Nkind (Curr_Obj_Def) = N_Subtype_Indication then
13367 declare
13368 Loc : constant Source_Ptr := Sloc (N);
13369 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13370 Decl : constant Node_Id :=
13371 Make_Subtype_Declaration (Loc,
13372 Defining_Identifier => Def_Id,
13373 Subtype_Indication =>
13374 Relocate_Node (Curr_Obj_Def));
13376 begin
13377 Insert_Before_And_Analyze (N, Decl);
13378 Set_Etype (Id, Def_Id);
13379 Curr_Typ := Def_Id;
13380 end;
13381 else
13382 Curr_Typ := Etype (Curr_Obj_Def);
13383 end if;
13385 if Anon_Acc then
13386 if Nkind (Curr_Obj_Def) /= N_Access_Definition then
13387 Mismatch := True;
13388 elsif Has_Null_Exclusion (Prev_Typ)
13389 and then not Null_Exclusion_Present (Curr_Obj_Def)
13390 then
13391 Mismatch := True;
13392 end if;
13393 -- ??? Another check needed: mismatch if disagreement
13394 -- between designated types/profiles .
13395 else
13396 Mismatch :=
13397 Is_Constrained (Prev_Typ)
13398 and then not Subtypes_Statically_Match (Prev_Typ, Curr_Typ);
13399 end if;
13401 if Mismatch then
13402 Error_Msg_Sloc := Sloc (Prev_Id);
13403 Error_Msg_N ("subtype does not statically match deferred "
13404 & "declaration #", N);
13405 end if;
13406 end Check_Possible_Deferred_Completion;
13408 ---------------------------------
13409 -- Check_Recursive_Declaration --
13410 ---------------------------------
13412 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13413 Comp : Entity_Id;
13415 begin
13416 if Is_Record_Type (Typ) then
13417 Comp := First_Component (Typ);
13418 while Present (Comp) loop
13419 if Comes_From_Source (Comp) then
13420 if Present (Expression (Parent (Comp)))
13421 and then Is_Entity_Name (Expression (Parent (Comp)))
13422 and then Entity (Expression (Parent (Comp))) = Prev
13423 then
13424 Error_Msg_Sloc := Sloc (Parent (Comp));
13425 Error_Msg_NE
13426 ("illegal circularity with declaration for & #",
13427 N, Comp);
13428 return;
13430 elsif Is_Record_Type (Etype (Comp)) then
13431 Check_Recursive_Declaration (Etype (Comp));
13432 end if;
13433 end if;
13435 Next_Component (Comp);
13436 end loop;
13437 end if;
13438 end Check_Recursive_Declaration;
13440 -- Start of processing for Constant_Redeclaration
13442 begin
13443 if Nkind (Parent (Prev)) = N_Object_Declaration then
13444 if Nkind (Object_Definition
13445 (Parent (Prev))) = N_Subtype_Indication
13446 then
13447 -- Find type of new declaration. The constraints of the two
13448 -- views must match statically, but there is no point in
13449 -- creating an itype for the full view.
13451 if Nkind (Obj_Def) = N_Subtype_Indication then
13452 Find_Type (Subtype_Mark (Obj_Def));
13453 New_T := Entity (Subtype_Mark (Obj_Def));
13455 else
13456 Find_Type (Obj_Def);
13457 New_T := Entity (Obj_Def);
13458 end if;
13460 T := Etype (Prev);
13462 else
13463 -- The full view may impose a constraint, even if the partial
13464 -- view does not, so construct the subtype.
13466 New_T := Find_Type_Of_Object (Obj_Def, N);
13467 T := New_T;
13468 end if;
13470 else
13471 -- Current declaration is illegal, diagnosed below in Enter_Name
13473 T := Empty;
13474 New_T := Any_Type;
13475 end if;
13477 -- If previous full declaration or a renaming declaration exists, or if
13478 -- a homograph is present, let Enter_Name handle it, either with an
13479 -- error or with the removal of an overridden implicit subprogram.
13480 -- The previous one is a full declaration if it has an expression
13481 -- (which in the case of an aggregate is indicated by the Init flag).
13483 if Ekind (Prev) /= E_Constant
13484 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13485 or else Present (Expression (Parent (Prev)))
13486 or else Has_Init_Expression (Parent (Prev))
13487 or else Present (Full_View (Prev))
13488 then
13489 Enter_Name (Id);
13491 -- Verify that types of both declarations match, or else that both types
13492 -- are anonymous access types whose designated subtypes statically match
13493 -- (as allowed in Ada 2005 by AI-385).
13495 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13496 and then
13497 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13498 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13499 or else Is_Access_Constant (Etype (New_T)) /=
13500 Is_Access_Constant (Etype (Prev))
13501 or else Can_Never_Be_Null (Etype (New_T)) /=
13502 Can_Never_Be_Null (Etype (Prev))
13503 or else Null_Exclusion_Present (Parent (Prev)) /=
13504 Null_Exclusion_Present (Parent (Id))
13505 or else not Subtypes_Statically_Match
13506 (Designated_Type (Etype (Prev)),
13507 Designated_Type (Etype (New_T))))
13508 then
13509 Error_Msg_Sloc := Sloc (Prev);
13510 Error_Msg_N ("type does not match declaration#", N);
13511 Set_Full_View (Prev, Id);
13512 Set_Etype (Id, Any_Type);
13514 -- A deferred constant whose type is an anonymous array is always
13515 -- illegal (unless imported). A detailed error message might be
13516 -- helpful for Ada beginners.
13518 if Nkind (Object_Definition (Parent (Prev)))
13519 = N_Constrained_Array_Definition
13520 and then Nkind (Object_Definition (N))
13521 = N_Constrained_Array_Definition
13522 then
13523 Error_Msg_N ("\each anonymous array is a distinct type", N);
13524 Error_Msg_N ("a deferred constant must have a named type",
13525 Object_Definition (Parent (Prev)));
13526 end if;
13528 elsif
13529 Null_Exclusion_Present (Parent (Prev))
13530 and then not Null_Exclusion_Present (N)
13531 then
13532 Error_Msg_Sloc := Sloc (Prev);
13533 Error_Msg_N ("null-exclusion does not match declaration#", N);
13534 Set_Full_View (Prev, Id);
13535 Set_Etype (Id, Any_Type);
13537 -- If so, process the full constant declaration
13539 else
13540 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13541 -- the deferred declaration is constrained, then the subtype defined
13542 -- by the subtype_indication in the full declaration shall match it
13543 -- statically.
13545 Check_Possible_Deferred_Completion
13546 (Prev_Id => Prev,
13547 Curr_Obj_Def => Obj_Def);
13549 Set_Full_View (Prev, Id);
13550 Set_Is_Public (Id, Is_Public (Prev));
13551 Set_Is_Internal (Id);
13552 Append_Entity (Id, Current_Scope);
13554 -- Check ALIASED present if present before (RM 7.4(7))
13556 if Is_Aliased (Prev)
13557 and then not Aliased_Present (N)
13558 then
13559 Error_Msg_Sloc := Sloc (Prev);
13560 Error_Msg_N ("ALIASED required (see declaration #)", N);
13561 end if;
13563 -- Check that placement is in private part and that the incomplete
13564 -- declaration appeared in the visible part.
13566 if Ekind (Current_Scope) = E_Package
13567 and then not In_Private_Part (Current_Scope)
13568 then
13569 Error_Msg_Sloc := Sloc (Prev);
13570 Error_Msg_N
13571 ("full constant for declaration # must be in private part", N);
13573 elsif Ekind (Current_Scope) = E_Package
13574 and then
13575 List_Containing (Parent (Prev)) /=
13576 Visible_Declarations (Package_Specification (Current_Scope))
13577 then
13578 Error_Msg_N
13579 ("deferred constant must be declared in visible part",
13580 Parent (Prev));
13581 end if;
13583 if Is_Access_Type (T)
13584 and then Nkind (Expression (N)) = N_Allocator
13585 then
13586 Check_Recursive_Declaration (Designated_Type (T));
13587 end if;
13589 -- A deferred constant is a visible entity. If type has invariants,
13590 -- verify that the initial value satisfies them. This is not done in
13591 -- GNATprove mode, as GNATprove handles invariant checks itself.
13593 if Has_Invariants (T)
13594 and then Present (Invariant_Procedure (T))
13595 and then not GNATprove_Mode
13596 then
13597 Insert_After (N,
13598 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13599 end if;
13600 end if;
13601 end Constant_Redeclaration;
13603 ----------------------
13604 -- Constrain_Access --
13605 ----------------------
13607 procedure Constrain_Access
13608 (Def_Id : in out Entity_Id;
13609 S : Node_Id;
13610 Related_Nod : Node_Id)
13612 T : constant Entity_Id := Entity (Subtype_Mark (S));
13613 Desig_Type : constant Entity_Id := Designated_Type (T);
13614 Desig_Subtype : Entity_Id;
13615 Constraint_OK : Boolean := True;
13617 begin
13618 if Is_Array_Type (Desig_Type) then
13619 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13620 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13622 elsif (Is_Record_Type (Desig_Type)
13623 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13624 and then not Is_Constrained (Desig_Type)
13625 then
13626 -- If this is a constrained access definition for a record
13627 -- component, we leave the type as an unconstrained access,
13628 -- and mark the component so that its actual type is built
13629 -- at a point of use (e.g., an assignment statement). This
13630 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13632 if Desig_Type = Current_Scope
13633 and then No (Def_Id)
13634 then
13635 Desig_Subtype :=
13636 Create_Itype
13637 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13638 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13639 Def_Id := Entity (Subtype_Mark (S));
13641 -- We indicate that the component has a per-object constraint
13642 -- for treatment at a point of use, even though the constraint
13643 -- may be independent of discriminants of the enclosing type.
13645 if Nkind (Related_Nod) = N_Component_Declaration then
13646 Set_Has_Per_Object_Constraint
13647 (Defining_Identifier (Related_Nod));
13648 end if;
13650 -- This call added to ensure that the constraint is analyzed
13651 -- (needed for a B test). Note that we still return early from
13652 -- this procedure to avoid recursive processing.
13654 Constrain_Discriminated_Type
13655 (Desig_Subtype, S, Related_Nod, For_Access => True);
13656 return;
13657 end if;
13659 -- Enforce rule that the constraint is illegal if there is an
13660 -- unconstrained view of the designated type. This means that the
13661 -- partial view (either a private type declaration or a derivation
13662 -- from a private type) has no discriminants. (Defect Report
13663 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13665 -- Rule updated for Ada 2005: The private type is said to have
13666 -- a constrained partial view, given that objects of the type
13667 -- can be declared. Furthermore, the rule applies to all access
13668 -- types, unlike the rule concerning default discriminants (see
13669 -- RM 3.7.1(7/3))
13671 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13672 and then Has_Private_Declaration (Desig_Type)
13673 and then In_Open_Scopes (Scope (Desig_Type))
13674 and then Has_Discriminants (Desig_Type)
13675 then
13676 declare
13677 Pack : constant Node_Id :=
13678 Unit_Declaration_Node (Scope (Desig_Type));
13679 Decls : List_Id;
13680 Decl : Node_Id;
13682 begin
13683 if Nkind (Pack) = N_Package_Declaration then
13684 Decls := Visible_Declarations (Specification (Pack));
13685 Decl := First (Decls);
13686 while Present (Decl) loop
13687 if (Nkind (Decl) = N_Private_Type_Declaration
13688 and then Chars (Defining_Identifier (Decl)) =
13689 Chars (Desig_Type))
13691 or else
13692 (Nkind (Decl) = N_Full_Type_Declaration
13693 and then
13694 Chars (Defining_Identifier (Decl)) =
13695 Chars (Desig_Type)
13696 and then Is_Derived_Type (Desig_Type)
13697 and then
13698 Has_Private_Declaration (Etype (Desig_Type)))
13699 then
13700 if No (Discriminant_Specifications (Decl)) then
13701 Error_Msg_N
13702 ("cannot constrain access type if designated "
13703 & "type has constrained partial view", S);
13704 end if;
13706 exit;
13707 end if;
13709 Next (Decl);
13710 end loop;
13711 end if;
13712 end;
13713 end if;
13715 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13716 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13717 For_Access => True);
13719 elsif Is_Concurrent_Type (Desig_Type)
13720 and then not Is_Constrained (Desig_Type)
13721 then
13722 Desig_Subtype := Create_Itype (E_Void, Related_Nod);
13723 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13725 else
13726 Error_Msg_N ("invalid constraint on access type", S);
13728 -- We simply ignore an invalid constraint
13730 Desig_Subtype := Desig_Type;
13731 Constraint_OK := False;
13732 end if;
13734 if No (Def_Id) then
13735 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13736 else
13737 Mutate_Ekind (Def_Id, E_Access_Subtype);
13738 end if;
13740 if Constraint_OK then
13741 Set_Etype (Def_Id, Base_Type (T));
13743 if Is_Private_Type (Desig_Type) then
13744 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13745 end if;
13746 else
13747 Set_Etype (Def_Id, Any_Type);
13748 end if;
13750 Set_Size_Info (Def_Id, T);
13751 Set_Is_Constrained (Def_Id, Constraint_OK);
13752 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13753 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13754 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13755 Set_Can_Never_Be_Null (Def_Id, Can_Never_Be_Null (T));
13757 Conditional_Delay (Def_Id, T);
13759 -- AI-363 : Subtypes of general access types whose designated types have
13760 -- default discriminants are disallowed. In instances, the rule has to
13761 -- be checked against the actual, of which T is the subtype. In a
13762 -- generic body, the rule is checked assuming that the actual type has
13763 -- defaulted discriminants.
13765 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13766 if Ekind (Base_Type (T)) = E_General_Access_Type
13767 and then Has_Defaulted_Discriminants (Desig_Type)
13768 then
13769 if Ada_Version < Ada_2005 then
13770 Error_Msg_N
13771 ("access subtype of general access type would not " &
13772 "be allowed in Ada 2005?y?", S);
13773 else
13774 Error_Msg_N
13775 ("access subtype of general access type not allowed", S);
13776 end if;
13778 Error_Msg_N ("\discriminants have defaults", S);
13780 elsif Is_Access_Type (T)
13781 and then Is_Generic_Type (Desig_Type)
13782 and then Has_Discriminants (Desig_Type)
13783 and then In_Package_Body (Current_Scope)
13784 then
13785 if Ada_Version < Ada_2005 then
13786 Error_Msg_N
13787 ("access subtype would not be allowed in generic body "
13788 & "in Ada 2005?y?", S);
13789 else
13790 Error_Msg_N
13791 ("access subtype not allowed in generic body", S);
13792 end if;
13794 Error_Msg_N
13795 ("\designated type is a discriminated formal", S);
13796 end if;
13797 end if;
13798 end Constrain_Access;
13800 ---------------------
13801 -- Constrain_Array --
13802 ---------------------
13804 procedure Constrain_Array
13805 (Def_Id : in out Entity_Id;
13806 SI : Node_Id;
13807 Related_Nod : Node_Id;
13808 Related_Id : Entity_Id;
13809 Suffix : Character)
13811 C : constant Node_Id := Constraint (SI);
13812 Number_Of_Constraints : Nat := 0;
13813 Index : Node_Id;
13814 S, T : Entity_Id;
13815 Constraint_OK : Boolean := True;
13816 Is_FLB_Array_Subtype : Boolean := False;
13818 begin
13819 T := Entity (Subtype_Mark (SI));
13821 if Is_Access_Type (T) then
13822 T := Designated_Type (T);
13823 end if;
13825 T := Underlying_Type (T);
13827 -- If an index constraint follows a subtype mark in a subtype indication
13828 -- then the type or subtype denoted by the subtype mark must not already
13829 -- impose an index constraint. The subtype mark must denote either an
13830 -- unconstrained array type or an access type whose designated type
13831 -- is such an array type... (RM 3.6.1)
13833 if Is_Constrained (T) then
13834 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13835 Constraint_OK := False;
13837 else
13838 S := First (Constraints (C));
13839 while Present (S) loop
13840 Number_Of_Constraints := Number_Of_Constraints + 1;
13841 Next (S);
13842 end loop;
13844 -- In either case, the index constraint must provide a discrete
13845 -- range for each index of the array type and the type of each
13846 -- discrete range must be the same as that of the corresponding
13847 -- index. (RM 3.6.1)
13849 if Number_Of_Constraints /= Number_Dimensions (T) then
13850 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13851 Constraint_OK := False;
13853 else
13854 S := First (Constraints (C));
13855 Index := First_Index (T);
13856 Analyze (Index);
13858 -- Apply constraints to each index type
13860 for J in 1 .. Number_Of_Constraints loop
13861 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13863 -- If the subtype of the index has been set to indicate that
13864 -- it has a fixed lower bound, then record that the subtype's
13865 -- entity will need to be marked as being a fixed-lower-bound
13866 -- array subtype.
13868 if S = First (Constraints (C)) then
13869 Is_FLB_Array_Subtype :=
13870 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13872 -- If the parent subtype (or should this be Etype of that?)
13873 -- is an FLB array subtype, we flag an error, because we
13874 -- don't currently allow subtypes of such subtypes to
13875 -- specify a fixed lower bound for any of their indexes,
13876 -- even if the index of the parent subtype is a "range <>"
13877 -- index.
13879 if Is_FLB_Array_Subtype
13880 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13881 then
13882 Error_Msg_NE
13883 ("index with fixed lower bound not allowed for subtype "
13884 & "of fixed-lower-bound }", S, T);
13886 Is_FLB_Array_Subtype := False;
13887 end if;
13889 elsif Is_FLB_Array_Subtype
13890 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13891 then
13892 Error_Msg_NE
13893 ("constrained index not allowed for fixed-lower-bound "
13894 & "subtype of}", S, T);
13896 elsif not Is_FLB_Array_Subtype
13897 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13898 then
13899 Error_Msg_NE
13900 ("index with fixed lower bound not allowed for "
13901 & "constrained subtype of}", S, T);
13902 end if;
13904 Next (Index);
13905 Next (S);
13906 end loop;
13908 end if;
13909 end if;
13911 if No (Def_Id) then
13912 Def_Id :=
13913 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13914 Set_Parent (Def_Id, Related_Nod);
13916 else
13917 Mutate_Ekind (Def_Id, E_Array_Subtype);
13918 end if;
13920 Set_Size_Info (Def_Id, (T));
13921 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13922 Set_Etype (Def_Id, Base_Type (T));
13924 if Constraint_OK then
13925 Set_First_Index (Def_Id, First (Constraints (C)));
13926 else
13927 Set_First_Index (Def_Id, First_Index (T));
13928 end if;
13930 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13931 Set_Is_Fixed_Lower_Bound_Array_Subtype
13932 (Def_Id, Is_FLB_Array_Subtype);
13933 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13934 Set_Is_Independent (Def_Id, Is_Independent (T));
13935 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13937 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13938 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13940 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13941 -- We need to initialize the attribute because if Def_Id is previously
13942 -- analyzed through a limited_with clause, it will have the attributes
13943 -- of an incomplete type, one of which is an Elist that overlaps the
13944 -- Packed_Array_Impl_Type field.
13946 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13948 -- Build a freeze node if parent still needs one. Also make sure that
13949 -- the Depends_On_Private status is set because the subtype will need
13950 -- reprocessing at the time the base type does, and also we must set a
13951 -- conditional delay.
13953 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13954 Conditional_Delay (Def_Id, T);
13955 end Constrain_Array;
13957 ------------------------------
13958 -- Constrain_Component_Type --
13959 ------------------------------
13961 function Constrain_Component_Type
13962 (Comp : Entity_Id;
13963 Constrained_Typ : Entity_Id;
13964 Related_Node : Node_Id;
13965 Typ : Entity_Id;
13966 Constraints : Elist_Id) return Entity_Id
13968 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13969 Compon_Type : constant Entity_Id := Etype (Comp);
13971 function Build_Constrained_Array_Type
13972 (Old_Type : Entity_Id) return Entity_Id;
13973 -- If Old_Type is an array type, one of whose indexes is constrained
13974 -- by a discriminant, build an Itype whose constraint replaces the
13975 -- discriminant with its value in the constraint.
13977 function Build_Constrained_Discriminated_Type
13978 (Old_Type : Entity_Id) return Entity_Id;
13979 -- Ditto for record components. Handle the case where the constraint
13980 -- is a conversion of the discriminant value, introduced during
13981 -- expansion.
13983 function Build_Constrained_Access_Type
13984 (Old_Type : Entity_Id) return Entity_Id;
13985 -- Ditto for access types. Makes use of previous two functions, to
13986 -- constrain designated type.
13988 function Is_Discriminant (Expr : Node_Id) return Boolean;
13989 -- Returns True if Expr is a discriminant
13991 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13992 -- Find the value of a discriminant named by Discr_Expr in Constraints
13994 -----------------------------------
13995 -- Build_Constrained_Access_Type --
13996 -----------------------------------
13998 function Build_Constrained_Access_Type
13999 (Old_Type : Entity_Id) return Entity_Id
14001 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
14002 Itype : Entity_Id;
14003 Desig_Subtype : Entity_Id;
14004 Scop : Entity_Id;
14006 begin
14007 -- If the original access type was not embedded in the enclosing
14008 -- type definition, there is no need to produce a new access
14009 -- subtype. In fact every access type with an explicit constraint
14010 -- generates an itype whose scope is the enclosing record.
14012 if not Is_Type (Scope (Old_Type)) then
14013 return Old_Type;
14015 elsif Is_Array_Type (Desig_Type) then
14016 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
14018 elsif Has_Discriminants (Desig_Type) then
14020 -- This may be an access type to an enclosing record type for
14021 -- which we are constructing the constrained components. Return
14022 -- the enclosing record subtype. This is not always correct,
14023 -- but avoids infinite recursion. ???
14025 Desig_Subtype := Any_Type;
14027 for J in reverse 0 .. Scope_Stack.Last loop
14028 Scop := Scope_Stack.Table (J).Entity;
14030 if Is_Type (Scop)
14031 and then Base_Type (Scop) = Base_Type (Desig_Type)
14032 then
14033 Desig_Subtype := Scop;
14034 end if;
14036 exit when not Is_Type (Scop);
14037 end loop;
14039 if Desig_Subtype = Any_Type then
14040 Desig_Subtype :=
14041 Build_Constrained_Discriminated_Type (Desig_Type);
14042 end if;
14044 else
14045 return Old_Type;
14046 end if;
14048 if Desig_Subtype /= Desig_Type then
14050 -- The Related_Node better be here or else we won't be able
14051 -- to attach new itypes to a node in the tree.
14053 pragma Assert (Present (Related_Node));
14055 Itype := Create_Itype (E_Access_Subtype, Related_Node);
14057 Set_Etype (Itype, Base_Type (Old_Type));
14058 Set_Size_Info (Itype, (Old_Type));
14059 Set_Directly_Designated_Type (Itype, Desig_Subtype);
14060 Set_Depends_On_Private (Itype, Has_Private_Component
14061 (Old_Type));
14062 Set_Is_Access_Constant (Itype, Is_Access_Constant
14063 (Old_Type));
14065 -- The new itype needs freezing when it depends on a not frozen
14066 -- type and the enclosing subtype needs freezing.
14068 if Has_Delayed_Freeze (Constrained_Typ)
14069 and then not Is_Frozen (Constrained_Typ)
14070 then
14071 Conditional_Delay (Itype, Base_Type (Old_Type));
14072 end if;
14074 return Itype;
14076 else
14077 return Old_Type;
14078 end if;
14079 end Build_Constrained_Access_Type;
14081 ----------------------------------
14082 -- Build_Constrained_Array_Type --
14083 ----------------------------------
14085 function Build_Constrained_Array_Type
14086 (Old_Type : Entity_Id) return Entity_Id
14088 Lo_Expr : Node_Id;
14089 Hi_Expr : Node_Id;
14090 Old_Index : Node_Id;
14091 Range_Node : Node_Id;
14092 Constr_List : List_Id;
14094 Need_To_Create_Itype : Boolean := False;
14096 begin
14097 Old_Index := First_Index (Old_Type);
14098 while Present (Old_Index) loop
14099 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14101 if Is_Discriminant (Lo_Expr)
14102 or else
14103 Is_Discriminant (Hi_Expr)
14104 then
14105 Need_To_Create_Itype := True;
14106 exit;
14107 end if;
14109 Next_Index (Old_Index);
14110 end loop;
14112 if Need_To_Create_Itype then
14113 Constr_List := New_List;
14115 Old_Index := First_Index (Old_Type);
14116 while Present (Old_Index) loop
14117 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
14119 if Is_Discriminant (Lo_Expr) then
14120 Lo_Expr := Get_Discr_Value (Lo_Expr);
14121 end if;
14123 if Is_Discriminant (Hi_Expr) then
14124 Hi_Expr := Get_Discr_Value (Hi_Expr);
14125 end if;
14127 Range_Node :=
14128 Make_Range
14129 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
14131 Append (Range_Node, To => Constr_List);
14133 Next_Index (Old_Index);
14134 end loop;
14136 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14138 else
14139 return Old_Type;
14140 end if;
14141 end Build_Constrained_Array_Type;
14143 ------------------------------------------
14144 -- Build_Constrained_Discriminated_Type --
14145 ------------------------------------------
14147 function Build_Constrained_Discriminated_Type
14148 (Old_Type : Entity_Id) return Entity_Id
14150 Expr : Node_Id;
14151 Constr_List : List_Id;
14152 Old_Constraint : Elmt_Id;
14154 Need_To_Create_Itype : Boolean := False;
14156 begin
14157 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14158 while Present (Old_Constraint) loop
14159 Expr := Node (Old_Constraint);
14161 if Is_Discriminant (Expr) then
14162 Need_To_Create_Itype := True;
14163 exit;
14165 -- After expansion of discriminated task types, the value
14166 -- of the discriminant may be converted to a run-time type
14167 -- for restricted run-times. Propagate the value of the
14168 -- discriminant as well, so that e.g. the secondary stack
14169 -- component has a static constraint. Necessary for LLVM.
14171 elsif Nkind (Expr) = N_Type_Conversion
14172 and then Is_Discriminant (Expression (Expr))
14173 then
14174 Need_To_Create_Itype := True;
14175 exit;
14176 end if;
14178 Next_Elmt (Old_Constraint);
14179 end loop;
14181 if Need_To_Create_Itype then
14182 Constr_List := New_List;
14184 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
14185 while Present (Old_Constraint) loop
14186 Expr := Node (Old_Constraint);
14188 if Is_Discriminant (Expr) then
14189 Expr := Get_Discr_Value (Expr);
14191 elsif Nkind (Expr) = N_Type_Conversion
14192 and then Is_Discriminant (Expression (Expr))
14193 then
14194 Expr := New_Copy_Tree (Expr);
14195 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
14196 end if;
14198 Append (New_Copy_Tree (Expr), To => Constr_List);
14200 Next_Elmt (Old_Constraint);
14201 end loop;
14203 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
14205 else
14206 return Old_Type;
14207 end if;
14208 end Build_Constrained_Discriminated_Type;
14210 ---------------------
14211 -- Get_Discr_Value --
14212 ---------------------
14214 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
14215 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
14216 -- Entity of a discriminant that appear as a standalone expression in
14217 -- the constraint of a component.
14219 D : Entity_Id;
14220 E : Elmt_Id;
14222 begin
14223 -- The discriminant may be declared for the type, in which case we
14224 -- find it by iterating over the list of discriminants. If the
14225 -- discriminant is inherited from a parent type, it appears as the
14226 -- corresponding discriminant of the current type. This will be the
14227 -- case when constraining an inherited component whose constraint is
14228 -- given by a discriminant of the parent.
14230 D := First_Discriminant (Typ);
14231 E := First_Elmt (Constraints);
14233 while Present (D) loop
14234 if D = Discr_Id
14235 or else D = CR_Discriminant (Discr_Id)
14236 or else Corresponding_Discriminant (D) = Discr_Id
14237 then
14238 return New_Copy_Tree (Node (E));
14239 end if;
14241 Next_Discriminant (D);
14242 Next_Elmt (E);
14243 end loop;
14245 -- The Corresponding_Discriminant mechanism is incomplete, because
14246 -- the correspondence between new and old discriminants is not one
14247 -- to one: one new discriminant can constrain several old ones. In
14248 -- that case, scan sequentially the stored_constraint, the list of
14249 -- discriminants of the parents, and the constraints.
14251 -- Previous code checked for the present of the Stored_Constraint
14252 -- list for the derived type, but did not use it at all. Should it
14253 -- be present when the component is a discriminated task type?
14255 if Is_Derived_Type (Typ)
14256 and then Scope (Discr_Id) = Etype (Typ)
14257 then
14258 D := First_Discriminant (Etype (Typ));
14259 E := First_Elmt (Constraints);
14260 while Present (D) loop
14261 if D = Discr_Id then
14262 return New_Copy_Tree (Node (E));
14263 end if;
14265 Next_Discriminant (D);
14266 Next_Elmt (E);
14267 end loop;
14268 end if;
14270 -- Something is wrong if we did not find the value
14272 raise Program_Error;
14273 end Get_Discr_Value;
14275 ---------------------
14276 -- Is_Discriminant --
14277 ---------------------
14279 function Is_Discriminant (Expr : Node_Id) return Boolean is
14280 Discrim_Scope : Entity_Id;
14282 begin
14283 if Denotes_Discriminant (Expr) then
14284 Discrim_Scope := Scope (Entity (Expr));
14286 -- Either we have a reference to one of Typ's discriminants,
14288 pragma Assert (Discrim_Scope = Typ
14290 -- or to the discriminants of the parent type, in the case
14291 -- of a derivation of a tagged type with variants.
14293 or else Discrim_Scope = Etype (Typ)
14294 or else Full_View (Discrim_Scope) = Etype (Typ)
14296 -- or same as above for the case where the discriminants
14297 -- were declared in Typ's private view.
14299 or else (Is_Private_Type (Discrim_Scope)
14300 and then Chars (Discrim_Scope) = Chars (Typ))
14302 -- or else we are deriving from the full view and the
14303 -- discriminant is declared in the private entity.
14305 or else (Is_Private_Type (Typ)
14306 and then Chars (Discrim_Scope) = Chars (Typ))
14308 -- Or we are constrained the corresponding record of a
14309 -- synchronized type that completes a private declaration.
14311 or else (Is_Concurrent_Record_Type (Typ)
14312 and then
14313 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
14315 -- or we have a class-wide type, in which case make sure the
14316 -- discriminant found belongs to the root type.
14318 or else (Is_Class_Wide_Type (Typ)
14319 and then Etype (Typ) = Discrim_Scope));
14321 return True;
14322 end if;
14324 -- In all other cases we have something wrong
14326 return False;
14327 end Is_Discriminant;
14329 -- Start of processing for Constrain_Component_Type
14331 begin
14332 if Nkind (Parent (Comp)) = N_Component_Declaration
14333 and then Comes_From_Source (Parent (Comp))
14334 and then Comes_From_Source
14335 (Subtype_Indication (Component_Definition (Parent (Comp))))
14336 and then
14337 Is_Entity_Name
14338 (Subtype_Indication (Component_Definition (Parent (Comp))))
14339 then
14340 return Compon_Type;
14342 elsif Is_Array_Type (Compon_Type) then
14343 return Build_Constrained_Array_Type (Compon_Type);
14345 elsif Has_Discriminants (Compon_Type) then
14346 return Build_Constrained_Discriminated_Type (Compon_Type);
14348 elsif Is_Access_Type (Compon_Type) then
14349 return Build_Constrained_Access_Type (Compon_Type);
14351 else
14352 return Compon_Type;
14353 end if;
14354 end Constrain_Component_Type;
14356 --------------------------
14357 -- Constrain_Concurrent --
14358 --------------------------
14360 -- For concurrent types, the associated record value type carries the same
14361 -- discriminants, so when we constrain a concurrent type, we must constrain
14362 -- the corresponding record type as well.
14364 procedure Constrain_Concurrent
14365 (Def_Id : in out Entity_Id;
14366 SI : Node_Id;
14367 Related_Nod : Node_Id;
14368 Related_Id : Entity_Id;
14369 Suffix : Character)
14371 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14372 -- case of a private subtype (needed when only doing semantic analysis).
14374 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14375 T_Val : Entity_Id;
14377 begin
14378 if Is_Access_Type (T_Ent) then
14379 T_Ent := Designated_Type (T_Ent);
14380 end if;
14382 T_Val := Corresponding_Record_Type (T_Ent);
14384 if Present (T_Val) then
14386 if No (Def_Id) then
14387 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14389 -- Elaborate itype now, as it may be used in a subsequent
14390 -- synchronized operation in another scope.
14392 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14393 Build_Itype_Reference (Def_Id, Related_Nod);
14394 end if;
14395 end if;
14397 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14398 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14400 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14401 Set_Corresponding_Record_Type (Def_Id,
14402 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14404 else
14405 -- If there is no associated record, expansion is disabled and this
14406 -- is a generic context. Create a subtype in any case, so that
14407 -- semantic analysis can proceed.
14409 if No (Def_Id) then
14410 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14411 end if;
14413 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14414 end if;
14415 end Constrain_Concurrent;
14417 ------------------------------------
14418 -- Constrain_Corresponding_Record --
14419 ------------------------------------
14421 function Constrain_Corresponding_Record
14422 (Prot_Subt : Entity_Id;
14423 Corr_Rec : Entity_Id;
14424 Related_Nod : Node_Id) return Entity_Id
14426 T_Sub : constant Entity_Id :=
14427 Create_Itype
14428 (Ekind => E_Record_Subtype,
14429 Related_Nod => Related_Nod,
14430 Related_Id => Corr_Rec,
14431 Suffix => 'C',
14432 Suffix_Index => -1);
14434 begin
14435 Set_Etype (T_Sub, Corr_Rec);
14436 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14437 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14438 Set_Is_Constrained (T_Sub, True);
14439 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14440 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14441 Set_Direct_Primitive_Operations
14442 (T_Sub, Direct_Primitive_Operations (Corr_Rec));
14444 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14445 Set_Discriminant_Constraint
14446 (T_Sub, Discriminant_Constraint (Prot_Subt));
14447 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14448 Create_Constrained_Components
14449 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14450 end if;
14452 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14454 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14455 Conditional_Delay (T_Sub, Corr_Rec);
14457 else
14458 -- This is a component subtype: it will be frozen in the context of
14459 -- the enclosing record's init_proc, so that discriminant references
14460 -- are resolved to discriminals. (Note: we used to skip freezing
14461 -- altogether in that case, which caused errors downstream for
14462 -- components of a bit packed array type).
14464 Set_Has_Delayed_Freeze (T_Sub);
14465 end if;
14467 return T_Sub;
14468 end Constrain_Corresponding_Record;
14470 -----------------------
14471 -- Constrain_Decimal --
14472 -----------------------
14474 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14475 T : constant Entity_Id := Entity (Subtype_Mark (S));
14476 C : constant Node_Id := Constraint (S);
14477 Loc : constant Source_Ptr := Sloc (C);
14478 Range_Expr : Node_Id;
14479 Digits_Expr : Node_Id;
14480 Digits_Val : Uint;
14481 Bound_Val : Ureal;
14483 begin
14484 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14486 if Nkind (C) = N_Range_Constraint then
14487 Range_Expr := Range_Expression (C);
14488 Digits_Val := Digits_Value (T);
14490 else
14491 pragma Assert (Nkind (C) = N_Digits_Constraint);
14493 Digits_Expr := Digits_Expression (C);
14494 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14496 Check_Digits_Expression (Digits_Expr);
14497 Digits_Val := Expr_Value (Digits_Expr);
14499 if Digits_Val > Digits_Value (T) then
14500 Error_Msg_N
14501 ("digits expression is incompatible with subtype", C);
14502 Digits_Val := Digits_Value (T);
14503 end if;
14505 if Present (Range_Constraint (C)) then
14506 Range_Expr := Range_Expression (Range_Constraint (C));
14507 else
14508 Range_Expr := Empty;
14509 end if;
14510 end if;
14512 Set_Etype (Def_Id, Base_Type (T));
14513 Set_Size_Info (Def_Id, (T));
14514 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14515 Set_Delta_Value (Def_Id, Delta_Value (T));
14516 Set_Scale_Value (Def_Id, Scale_Value (T));
14517 Set_Small_Value (Def_Id, Small_Value (T));
14518 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14519 Set_Digits_Value (Def_Id, Digits_Val);
14521 -- Manufacture range from given digits value if no range present
14523 if No (Range_Expr) then
14524 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14525 Range_Expr :=
14526 Make_Range (Loc,
14527 Low_Bound =>
14528 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14529 High_Bound =>
14530 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14531 end if;
14533 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14534 Set_Discrete_RM_Size (Def_Id);
14536 -- Unconditionally delay the freeze, since we cannot set size
14537 -- information in all cases correctly until the freeze point.
14539 Set_Has_Delayed_Freeze (Def_Id);
14540 end Constrain_Decimal;
14542 ----------------------------------
14543 -- Constrain_Discriminated_Type --
14544 ----------------------------------
14546 procedure Constrain_Discriminated_Type
14547 (Def_Id : Entity_Id;
14548 S : Node_Id;
14549 Related_Nod : Node_Id;
14550 For_Access : Boolean := False)
14552 E : Entity_Id := Entity (Subtype_Mark (S));
14553 T : Entity_Id;
14555 procedure Fixup_Bad_Constraint;
14556 -- Called after finding a bad constraint, and after having posted an
14557 -- appropriate error message. The goal is to leave type Def_Id in as
14558 -- reasonable state as possible.
14560 --------------------------
14561 -- Fixup_Bad_Constraint --
14562 --------------------------
14564 procedure Fixup_Bad_Constraint is
14565 begin
14566 -- Set a reasonable Ekind for the entity, including incomplete types.
14568 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14570 -- Set Etype to the known type, to reduce chances of cascaded errors
14572 Set_Etype (Def_Id, E);
14573 Set_Error_Posted (Def_Id);
14574 end Fixup_Bad_Constraint;
14576 -- Local variables
14578 C : Node_Id;
14579 Constr : Elist_Id := New_Elmt_List;
14581 -- Start of processing for Constrain_Discriminated_Type
14583 begin
14584 C := Constraint (S);
14586 -- A discriminant constraint is only allowed in a subtype indication,
14587 -- after a subtype mark. This subtype mark must denote either a type
14588 -- with discriminants, or an access type whose designated type is a
14589 -- type with discriminants. A discriminant constraint specifies the
14590 -- values of these discriminants (RM 3.7.2(5)).
14592 T := Base_Type (Entity (Subtype_Mark (S)));
14594 if Is_Access_Type (T) then
14595 T := Designated_Type (T);
14596 end if;
14598 -- In an instance it may be necessary to retrieve the full view of a
14599 -- type with unknown discriminants, or a full view with defaulted
14600 -- discriminants. In other contexts the constraint is illegal.
14602 if In_Instance
14603 and then Is_Private_Type (T)
14604 and then Present (Full_View (T))
14605 and then
14606 (Has_Unknown_Discriminants (T)
14607 or else
14608 (not Has_Discriminants (T)
14609 and then Has_Defaulted_Discriminants (Full_View (T))))
14610 then
14611 T := Full_View (T);
14612 E := Full_View (E);
14613 end if;
14615 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14616 -- generating an error for access-to-incomplete subtypes.
14618 if Ada_Version >= Ada_2005
14619 and then Ekind (T) = E_Incomplete_Type
14620 and then Nkind (Parent (S)) = N_Subtype_Declaration
14621 and then not Is_Itype (Def_Id)
14622 then
14623 -- A little sanity check: emit an error message if the type has
14624 -- discriminants to begin with. Type T may be a regular incomplete
14625 -- type or imported via a limited with clause.
14627 if Has_Discriminants (T)
14628 or else (From_Limited_With (T)
14629 and then Present (Non_Limited_View (T))
14630 and then Nkind (Parent (Non_Limited_View (T))) =
14631 N_Full_Type_Declaration
14632 and then Present (Discriminant_Specifications
14633 (Parent (Non_Limited_View (T)))))
14634 then
14635 Error_Msg_N
14636 ("(Ada 2005) incomplete subtype may not be constrained", C);
14637 else
14638 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14639 end if;
14641 Fixup_Bad_Constraint;
14642 return;
14644 -- Check that the type has visible discriminants. The type may be
14645 -- a private type with unknown discriminants whose full view has
14646 -- discriminants which are invisible.
14648 elsif not Has_Discriminants (T)
14649 or else
14650 (Has_Unknown_Discriminants (T)
14651 and then Is_Private_Type (T))
14652 then
14653 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14654 Fixup_Bad_Constraint;
14655 return;
14657 elsif Is_Constrained (E)
14658 or else (Ekind (E) = E_Class_Wide_Subtype
14659 and then Present (Discriminant_Constraint (E)))
14660 then
14661 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14662 Fixup_Bad_Constraint;
14663 return;
14664 end if;
14666 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14667 -- applies to the base type.
14669 T := Base_Type (T);
14671 Constr := Build_Discriminant_Constraints (T, S);
14673 -- If the list returned was empty we had an error in building the
14674 -- discriminant constraint. We have also already signalled an error
14675 -- in the incomplete type case
14677 if Is_Empty_Elmt_List (Constr) then
14678 Fixup_Bad_Constraint;
14679 return;
14680 end if;
14682 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14683 end Constrain_Discriminated_Type;
14685 ---------------------------
14686 -- Constrain_Enumeration --
14687 ---------------------------
14689 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14690 T : constant Entity_Id := Entity (Subtype_Mark (S));
14691 C : constant Node_Id := Constraint (S);
14693 begin
14694 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14696 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14697 Set_Etype (Def_Id, Base_Type (T));
14698 Set_Size_Info (Def_Id, (T));
14699 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14700 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14702 -- Inherit the chain of representation items instead of replacing it
14703 -- because Build_Derived_Enumeration_Type rewrites the declaration of
14704 -- the derived type as a subtype declaration and the former needs to
14705 -- preserve existing representation items (see Build_Derived_Type).
14707 Inherit_Rep_Item_Chain (Def_Id, T);
14709 Set_Discrete_RM_Size (Def_Id);
14710 end Constrain_Enumeration;
14712 ----------------------
14713 -- Constrain_Float --
14714 ----------------------
14716 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14717 T : constant Entity_Id := Entity (Subtype_Mark (S));
14718 C : Node_Id;
14719 D : Node_Id;
14720 Rais : Node_Id;
14722 begin
14723 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14725 Set_Etype (Def_Id, Base_Type (T));
14726 Set_Size_Info (Def_Id, (T));
14727 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14729 -- Process the constraint
14731 C := Constraint (S);
14733 -- Digits constraint present
14735 if Nkind (C) = N_Digits_Constraint then
14736 Check_Restriction (No_Obsolescent_Features, C);
14738 if Warn_On_Obsolescent_Feature then
14739 Error_Msg_N
14740 ("subtype digits constraint is an " &
14741 "obsolescent feature (RM J.3(8))?j?", C);
14742 end if;
14744 D := Digits_Expression (C);
14745 Analyze_And_Resolve (D, Any_Integer);
14746 Check_Digits_Expression (D);
14747 Set_Digits_Value (Def_Id, Expr_Value (D));
14749 -- Check that digits value is in range. Obviously we can do this
14750 -- at compile time, but it is strictly a runtime check, and of
14751 -- course there is an ACVC test that checks this.
14753 if Digits_Value (Def_Id) > Digits_Value (T) then
14754 Error_Msg_Uint_1 := Digits_Value (T);
14755 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14756 Rais :=
14757 Make_Raise_Constraint_Error (Sloc (D),
14758 Reason => CE_Range_Check_Failed);
14759 Insert_Action (Declaration_Node (Def_Id), Rais);
14760 end if;
14762 C := Range_Constraint (C);
14764 -- No digits constraint present
14766 else
14767 Set_Digits_Value (Def_Id, Digits_Value (T));
14768 end if;
14770 -- Range constraint present
14772 if Nkind (C) = N_Range_Constraint then
14773 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14775 -- No range constraint present
14777 else
14778 pragma Assert (No (C));
14779 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14780 end if;
14782 Set_Is_Constrained (Def_Id);
14783 end Constrain_Float;
14785 ---------------------
14786 -- Constrain_Index --
14787 ---------------------
14789 procedure Constrain_Index
14790 (Index : Node_Id;
14791 S : Node_Id;
14792 Related_Nod : Node_Id;
14793 Related_Id : Entity_Id;
14794 Suffix : Character;
14795 Suffix_Index : Pos)
14797 Def_Id : Entity_Id;
14798 R : Node_Id := Empty;
14799 T : constant Entity_Id := Etype (Index);
14800 Is_FLB_Index : Boolean := False;
14802 begin
14803 Def_Id :=
14804 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14805 Set_Etype (Def_Id, Base_Type (T));
14807 if Nkind (S) = N_Range
14808 or else
14809 (Nkind (S) = N_Attribute_Reference
14810 and then Attribute_Name (S) = Name_Range)
14811 then
14812 -- A Range attribute will be transformed into N_Range by Resolve
14814 -- If a range has an Empty upper bound, then remember that for later
14815 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14816 -- flag, and also set the upper bound of the range to the index
14817 -- subtype's upper bound rather than leaving it Empty. In truth,
14818 -- that upper bound corresponds to a box ("<>"), but it's convenient
14819 -- to set it to the upper bound to avoid needing to add special tests
14820 -- in various places for an Empty upper bound, and in any case it
14821 -- accurately characterizes the index's range of values.
14823 if Nkind (S) = N_Range and then No (High_Bound (S)) then
14824 Is_FLB_Index := True;
14825 Set_High_Bound (S, Type_High_Bound (T));
14826 end if;
14828 R := S;
14830 Process_Range_Expr_In_Decl (R, T);
14832 if not Error_Posted (S)
14833 and then
14834 (Nkind (S) /= N_Range
14835 or else not Covers (T, (Etype (Low_Bound (S))))
14836 or else not Covers (T, (Etype (High_Bound (S)))))
14837 then
14838 if Base_Type (T) /= Any_Type
14839 and then Etype (Low_Bound (S)) /= Any_Type
14840 and then Etype (High_Bound (S)) /= Any_Type
14841 then
14842 Error_Msg_N ("range expected", S);
14843 end if;
14844 end if;
14846 elsif Nkind (S) = N_Subtype_Indication then
14848 -- The parser has verified that this is a discrete indication
14850 Resolve_Discrete_Subtype_Indication (S, T);
14851 Bad_Predicated_Subtype_Use
14852 ("subtype& has predicate, not allowed in index constraint",
14853 S, Entity (Subtype_Mark (S)));
14855 R := Range_Expression (Constraint (S));
14857 -- Capture values of bounds and generate temporaries for them if
14858 -- needed, since checks may cause duplication of the expressions
14859 -- which must not be reevaluated.
14861 -- The forced evaluation removes side effects from expressions, which
14862 -- should occur also in GNATprove mode. Otherwise, we end up with
14863 -- unexpected insertions of actions at places where this is not
14864 -- supposed to occur, e.g. on default parameters of a call.
14866 if Expander_Active or GNATprove_Mode then
14867 Force_Evaluation
14868 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14869 Force_Evaluation
14870 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14871 end if;
14873 elsif Nkind (S) = N_Discriminant_Association then
14875 -- Syntactically valid in subtype indication
14877 Error_Msg_N ("invalid index constraint", S);
14878 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14879 return;
14881 -- Subtype_Mark case, no anonymous subtypes to construct
14883 else
14884 Analyze (S);
14886 if Is_Entity_Name (S) then
14887 if not Is_Type (Entity (S)) then
14888 Error_Msg_N ("expect subtype mark for index constraint", S);
14890 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14891 Wrong_Type (S, Base_Type (T));
14893 -- Check error of subtype with predicate in index constraint
14895 else
14896 Bad_Predicated_Subtype_Use
14897 ("subtype& has predicate, not allowed in index constraint",
14898 S, Entity (S));
14899 end if;
14901 return;
14903 else
14904 Error_Msg_N ("invalid index constraint", S);
14905 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14906 return;
14907 end if;
14908 end if;
14910 -- Complete construction of the Itype
14912 if Is_Modular_Integer_Type (T) then
14913 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14915 elsif Is_Integer_Type (T) then
14916 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14918 else
14919 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14920 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14921 Set_First_Literal (Def_Id, First_Literal (T));
14922 end if;
14924 Set_Size_Info (Def_Id, (T));
14925 Copy_RM_Size (To => Def_Id, From => T);
14926 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14928 -- If this is a range for a fixed-lower-bound subtype, then set the
14929 -- index itype's low bound to the FLB and the index itype's upper bound
14930 -- to the high bound of the parent array type's index subtype. Also,
14931 -- mark the itype as an FLB index subtype.
14933 if Nkind (S) = N_Range and then Is_FLB_Index then
14934 Set_Scalar_Range
14935 (Def_Id,
14936 Make_Range (Sloc (S),
14937 Low_Bound => Low_Bound (S),
14938 High_Bound => Type_High_Bound (T)));
14939 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14941 else
14942 Set_Scalar_Range (Def_Id, R);
14943 end if;
14945 Set_Etype (S, Def_Id);
14946 Set_Discrete_RM_Size (Def_Id);
14947 end Constrain_Index;
14949 -----------------------
14950 -- Constrain_Integer --
14951 -----------------------
14953 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14954 T : constant Entity_Id := Entity (Subtype_Mark (S));
14955 C : constant Node_Id := Constraint (S);
14957 begin
14958 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14960 if Is_Modular_Integer_Type (T) then
14961 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14962 else
14963 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14964 end if;
14966 Set_Etype (Def_Id, Base_Type (T));
14967 Set_Size_Info (Def_Id, (T));
14968 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14969 Set_Discrete_RM_Size (Def_Id);
14970 end Constrain_Integer;
14972 ------------------------------
14973 -- Constrain_Ordinary_Fixed --
14974 ------------------------------
14976 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14977 T : constant Entity_Id := Entity (Subtype_Mark (S));
14978 C : Node_Id;
14979 D : Node_Id;
14980 Rais : Node_Id;
14982 begin
14983 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14984 Set_Etype (Def_Id, Base_Type (T));
14985 Set_Size_Info (Def_Id, (T));
14986 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14987 Set_Small_Value (Def_Id, Small_Value (T));
14989 -- Process the constraint
14991 C := Constraint (S);
14993 -- Delta constraint present
14995 if Nkind (C) = N_Delta_Constraint then
14996 Check_Restriction (No_Obsolescent_Features, C);
14998 if Warn_On_Obsolescent_Feature then
14999 Error_Msg_S
15000 ("subtype delta constraint is an " &
15001 "obsolescent feature (RM J.3(7))?j?");
15002 end if;
15004 D := Delta_Expression (C);
15005 Analyze_And_Resolve (D, Any_Real);
15006 Check_Delta_Expression (D);
15007 Set_Delta_Value (Def_Id, Expr_Value_R (D));
15009 -- Check that delta value is in range. Obviously we can do this
15010 -- at compile time, but it is strictly a runtime check, and of
15011 -- course there is an ACVC test that checks this.
15013 if Delta_Value (Def_Id) < Delta_Value (T) then
15014 Error_Msg_N ("??delta value is too small", D);
15015 Rais :=
15016 Make_Raise_Constraint_Error (Sloc (D),
15017 Reason => CE_Range_Check_Failed);
15018 Insert_Action (Declaration_Node (Def_Id), Rais);
15019 end if;
15021 C := Range_Constraint (C);
15023 -- No delta constraint present
15025 else
15026 Set_Delta_Value (Def_Id, Delta_Value (T));
15027 end if;
15029 -- Range constraint present
15031 if Nkind (C) = N_Range_Constraint then
15032 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
15034 -- No range constraint present
15036 else
15037 pragma Assert (No (C));
15038 Set_Scalar_Range (Def_Id, Scalar_Range (T));
15039 end if;
15041 Set_Discrete_RM_Size (Def_Id);
15043 -- Unconditionally delay the freeze, since we cannot set size
15044 -- information in all cases correctly until the freeze point.
15046 Set_Has_Delayed_Freeze (Def_Id);
15047 end Constrain_Ordinary_Fixed;
15049 -----------------------
15050 -- Contain_Interface --
15051 -----------------------
15053 function Contain_Interface
15054 (Iface : Entity_Id;
15055 Ifaces : Elist_Id) return Boolean
15057 Iface_Elmt : Elmt_Id;
15059 begin
15060 if Present (Ifaces) then
15061 Iface_Elmt := First_Elmt (Ifaces);
15062 while Present (Iface_Elmt) loop
15063 if Node (Iface_Elmt) = Iface then
15064 return True;
15065 end if;
15067 Next_Elmt (Iface_Elmt);
15068 end loop;
15069 end if;
15071 return False;
15072 end Contain_Interface;
15074 ---------------------------
15075 -- Convert_Scalar_Bounds --
15076 ---------------------------
15078 procedure Convert_Scalar_Bounds
15079 (N : Node_Id;
15080 Parent_Type : Entity_Id;
15081 Derived_Type : Entity_Id;
15082 Loc : Source_Ptr)
15084 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
15086 Lo : Node_Id;
15087 Hi : Node_Id;
15088 Rng : Node_Id;
15090 begin
15091 -- Defend against previous errors
15093 if No (Scalar_Range (Derived_Type)) then
15094 Check_Error_Detected;
15095 return;
15096 end if;
15098 Lo := Build_Scalar_Bound
15099 (Type_Low_Bound (Derived_Type),
15100 Parent_Type, Implicit_Base);
15102 Hi := Build_Scalar_Bound
15103 (Type_High_Bound (Derived_Type),
15104 Parent_Type, Implicit_Base);
15106 Rng :=
15107 Make_Range (Loc,
15108 Low_Bound => Lo,
15109 High_Bound => Hi);
15111 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
15113 Set_Parent (Rng, N);
15114 Set_Scalar_Range (Derived_Type, Rng);
15116 -- Analyze the bounds
15118 Analyze_And_Resolve (Lo, Implicit_Base);
15119 Analyze_And_Resolve (Hi, Implicit_Base);
15121 -- Analyze the range itself, except that we do not analyze it if
15122 -- the bounds are real literals, and we have a fixed-point type.
15123 -- The reason for this is that we delay setting the bounds in this
15124 -- case till we know the final Small and Size values (see circuit
15125 -- in Freeze.Freeze_Fixed_Point_Type for further details).
15127 if Is_Fixed_Point_Type (Parent_Type)
15128 and then Nkind (Lo) = N_Real_Literal
15129 and then Nkind (Hi) = N_Real_Literal
15130 then
15131 return;
15133 -- Here we do the analysis of the range
15135 -- Note: we do this manually, since if we do a normal Analyze and
15136 -- Resolve call, there are problems with the conversions used for
15137 -- the derived type range.
15139 else
15140 Set_Etype (Rng, Implicit_Base);
15141 Set_Analyzed (Rng, True);
15142 end if;
15143 end Convert_Scalar_Bounds;
15145 -------------------
15146 -- Copy_And_Swap --
15147 -------------------
15149 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
15150 begin
15151 -- Initialize new full declaration entity by copying the pertinent
15152 -- fields of the corresponding private declaration entity.
15154 -- We temporarily set Ekind to a value appropriate for a type to
15155 -- avoid assert failures in Einfo from checking for setting type
15156 -- attributes on something that is not a type. Ekind (Priv) is an
15157 -- appropriate choice, since it allowed the attributes to be set
15158 -- in the first place. This Ekind value will be modified later.
15160 Mutate_Ekind (Full, Ekind (Priv));
15162 -- Also set Etype temporarily to Any_Type, again, in the absence
15163 -- of errors, it will be properly reset, and if there are errors,
15164 -- then we want a value of Any_Type to remain.
15166 Set_Etype (Full, Any_Type);
15168 -- Now start copying attributes
15170 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
15172 if Has_Discriminants (Full) then
15173 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
15174 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
15175 end if;
15177 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
15178 Set_Homonym (Full, Homonym (Priv));
15179 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
15180 Set_Is_Public (Full, Is_Public (Priv));
15181 Set_Is_Pure (Full, Is_Pure (Priv));
15182 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
15183 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
15184 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
15185 Set_Has_Pragma_Unreferenced_Objects
15186 (Full, Has_Pragma_Unreferenced_Objects
15187 (Priv));
15189 Conditional_Delay (Full, Priv);
15191 if Is_Tagged_Type (Full) then
15192 Set_Direct_Primitive_Operations
15193 (Full, Direct_Primitive_Operations (Priv));
15194 Set_No_Tagged_Streams_Pragma
15195 (Full, No_Tagged_Streams_Pragma (Priv));
15197 if Is_Base_Type (Priv) then
15198 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
15199 end if;
15200 end if;
15202 Set_Is_Volatile (Full, Is_Volatile (Priv));
15203 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
15204 Set_Scope (Full, Scope (Priv));
15205 Set_Prev_Entity (Full, Prev_Entity (Priv));
15206 Set_Next_Entity (Full, Next_Entity (Priv));
15207 Set_First_Entity (Full, First_Entity (Priv));
15208 Set_Last_Entity (Full, Last_Entity (Priv));
15210 -- If access types have been recorded for later handling, keep them in
15211 -- the full view so that they get handled when the full view freeze
15212 -- node is expanded.
15214 if Present (Freeze_Node (Priv))
15215 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
15216 then
15217 Ensure_Freeze_Node (Full);
15218 Set_Access_Types_To_Process
15219 (Freeze_Node (Full),
15220 Access_Types_To_Process (Freeze_Node (Priv)));
15221 end if;
15223 -- Swap the two entities. Now Private is the full type entity and Full
15224 -- is the private one. They will be swapped back at the end of the
15225 -- private part. This swapping ensures that the entity that is visible
15226 -- in the private part is the full declaration.
15228 Exchange_Entities (Priv, Full);
15229 Set_Is_Not_Self_Hidden (Priv);
15230 Append_Entity (Full, Scope (Full));
15231 end Copy_And_Swap;
15233 -------------------------------------
15234 -- Copy_Array_Base_Type_Attributes --
15235 -------------------------------------
15237 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
15238 begin
15239 Set_Component_Alignment (T1, Component_Alignment (T2));
15240 Set_Component_Type (T1, Component_Type (T2));
15241 Set_Component_Size (T1, Component_Size (T2));
15242 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
15243 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
15244 Propagate_Concurrent_Flags (T1, T2);
15245 Set_Is_Packed (T1, Is_Packed (T2));
15246 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
15247 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
15248 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
15249 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
15250 end Copy_Array_Base_Type_Attributes;
15252 -----------------------------------
15253 -- Copy_Array_Subtype_Attributes --
15254 -----------------------------------
15256 -- Note that we used to copy Packed_Array_Impl_Type too here, but we now
15257 -- let it be recreated during freezing for the sake of better debug info.
15259 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
15260 begin
15261 Set_Size_Info (T1, T2);
15263 Set_First_Index (T1, First_Index (T2));
15264 Set_Is_Aliased (T1, Is_Aliased (T2));
15265 Set_Is_Atomic (T1, Is_Atomic (T2));
15266 Set_Is_Independent (T1, Is_Independent (T2));
15267 Set_Is_Volatile (T1, Is_Volatile (T2));
15268 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
15269 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
15270 Set_Is_Constrained (T1, Is_Constrained (T2));
15271 Set_Depends_On_Private (T1, Has_Private_Component (T2));
15272 Inherit_Rep_Item_Chain (T1, T2);
15273 Set_Convention (T1, Convention (T2));
15274 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
15275 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
15276 end Copy_Array_Subtype_Attributes;
15278 -----------------------------------
15279 -- Create_Constrained_Components --
15280 -----------------------------------
15282 procedure Create_Constrained_Components
15283 (Subt : Entity_Id;
15284 Decl_Node : Node_Id;
15285 Typ : Entity_Id;
15286 Constraints : Elist_Id)
15288 Loc : constant Source_Ptr := Sloc (Subt);
15289 Comp_List : constant Elist_Id := New_Elmt_List;
15290 Parent_Type : constant Entity_Id := Etype (Typ);
15292 Assoc_List : List_Id;
15293 Discr_Val : Elmt_Id;
15294 Errors : Boolean;
15295 New_C : Entity_Id;
15296 Old_C : Entity_Id;
15297 Is_Static : Boolean := True;
15298 Is_Compile_Time_Known : Boolean := True;
15300 procedure Collect_Fixed_Components (Typ : Entity_Id);
15301 -- Collect parent type components that do not appear in a variant part
15303 procedure Create_All_Components;
15304 -- Iterate over Comp_List to create the components of the subtype
15306 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
15307 -- Creates a new component from Old_Compon, copying all the fields from
15308 -- it, including its Etype, inserts the new component in the Subt entity
15309 -- chain and returns the new component.
15311 function Is_Variant_Record (T : Entity_Id) return Boolean;
15312 -- If true, and discriminants are static, collect only components from
15313 -- variants selected by discriminant values.
15315 ------------------------------
15316 -- Collect_Fixed_Components --
15317 ------------------------------
15319 procedure Collect_Fixed_Components (Typ : Entity_Id) is
15320 begin
15321 -- Build association list for discriminants, and find components of
15322 -- the variant part selected by the values of the discriminants.
15324 Assoc_List := New_List;
15326 Old_C := First_Discriminant (Typ);
15327 Discr_Val := First_Elmt (Constraints);
15328 while Present (Old_C) loop
15329 Append_To (Assoc_List,
15330 Make_Component_Association (Loc,
15331 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15332 Expression => New_Copy (Node (Discr_Val))));
15334 Next_Elmt (Discr_Val);
15335 Next_Discriminant (Old_C);
15336 end loop;
15338 -- The tag and the possible parent component are unconditionally in
15339 -- the subtype.
15341 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15342 Old_C := First_Component (Typ);
15343 while Present (Old_C) loop
15344 if Chars (Old_C) in Name_uTag | Name_uParent then
15345 Append_Elmt (Old_C, Comp_List);
15346 end if;
15348 Next_Component (Old_C);
15349 end loop;
15350 end if;
15351 end Collect_Fixed_Components;
15353 ---------------------------
15354 -- Create_All_Components --
15355 ---------------------------
15357 procedure Create_All_Components is
15358 Comp : Elmt_Id;
15360 begin
15361 Comp := First_Elmt (Comp_List);
15362 while Present (Comp) loop
15363 Old_C := Node (Comp);
15364 New_C := Create_Component (Old_C);
15366 Set_Etype
15367 (New_C,
15368 Constrain_Component_Type
15369 (Old_C, Subt, Decl_Node, Typ, Constraints));
15370 Set_Is_Public (New_C, Is_Public (Subt));
15372 Next_Elmt (Comp);
15373 end loop;
15374 end Create_All_Components;
15376 ----------------------
15377 -- Create_Component --
15378 ----------------------
15380 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15381 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15383 begin
15384 if Ekind (Old_Compon) = E_Discriminant
15385 and then Is_Completely_Hidden (Old_Compon)
15386 then
15387 -- This is a shadow discriminant created for a discriminant of
15388 -- the parent type, which needs to be present in the subtype.
15389 -- Give the shadow discriminant an internal name that cannot
15390 -- conflict with that of visible components.
15392 Set_Chars (New_Compon, New_Internal_Name ('C'));
15393 end if;
15395 -- Set the parent so we have a proper link for freezing etc. This is
15396 -- not a real parent pointer, since of course our parent does not own
15397 -- up to us and reference us, we are an illegitimate child of the
15398 -- original parent.
15400 Set_Parent (New_Compon, Parent (Old_Compon));
15402 -- We do not want this node marked as Comes_From_Source, since
15403 -- otherwise it would get first class status and a separate cross-
15404 -- reference line would be generated. Illegitimate children do not
15405 -- rate such recognition.
15407 Set_Comes_From_Source (New_Compon, False);
15409 -- But it is a real entity, and a birth certificate must be properly
15410 -- registered by entering it into the entity list, and setting its
15411 -- scope to the given subtype. This turns out to be useful for the
15412 -- LLVM code generator, but that scope is not used otherwise.
15414 Enter_Name (New_Compon);
15415 Set_Scope (New_Compon, Subt);
15417 return New_Compon;
15418 end Create_Component;
15420 -----------------------
15421 -- Is_Variant_Record --
15422 -----------------------
15424 function Is_Variant_Record (T : Entity_Id) return Boolean is
15425 Decl : constant Node_Id := Parent (T);
15426 begin
15427 return Nkind (Decl) = N_Full_Type_Declaration
15428 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
15429 and then Present (Component_List (Type_Definition (Decl)))
15430 and then
15431 Present (Variant_Part (Component_List (Type_Definition (Decl))));
15432 end Is_Variant_Record;
15434 -- Start of processing for Create_Constrained_Components
15436 begin
15437 pragma Assert (Subt /= Base_Type (Subt));
15438 pragma Assert (Typ = Base_Type (Typ));
15440 Set_First_Entity (Subt, Empty);
15441 Set_Last_Entity (Subt, Empty);
15443 -- Check whether constraint is fully static, in which case we can
15444 -- optimize the list of components.
15446 Discr_Val := First_Elmt (Constraints);
15447 while Present (Discr_Val) loop
15448 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15449 Is_Static := False;
15451 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15452 Is_Compile_Time_Known := False;
15453 exit;
15454 end if;
15455 end if;
15457 Next_Elmt (Discr_Val);
15458 end loop;
15460 Set_Has_Static_Discriminants (Subt, Is_Static);
15462 Push_Scope (Subt);
15464 -- Inherit the discriminants of the parent type
15466 Add_Discriminants : declare
15467 Num_Disc : Nat;
15468 Num_Stor : Nat;
15470 begin
15471 Num_Disc := 0;
15472 Old_C := First_Discriminant (Typ);
15474 while Present (Old_C) loop
15475 Num_Disc := Num_Disc + 1;
15476 New_C := Create_Component (Old_C);
15477 Set_Is_Public (New_C, Is_Public (Subt));
15478 Next_Discriminant (Old_C);
15479 end loop;
15481 -- For an untagged derived subtype, the number of discriminants may
15482 -- be smaller than the number of inherited discriminants, because
15483 -- several of them may be renamed by a single new discriminant or
15484 -- constrained. In this case, add the hidden discriminants back into
15485 -- the subtype, because they need to be present if the optimizer of
15486 -- the GCC 4.x back-end decides to break apart assignments between
15487 -- objects using the parent view into member-wise assignments.
15489 Num_Stor := 0;
15491 if Is_Derived_Type (Typ)
15492 and then not Is_Tagged_Type (Typ)
15493 then
15494 Old_C := First_Stored_Discriminant (Typ);
15496 while Present (Old_C) loop
15497 Num_Stor := Num_Stor + 1;
15498 Next_Stored_Discriminant (Old_C);
15499 end loop;
15500 end if;
15502 if Num_Stor > Num_Disc then
15504 -- Find out multiple uses of new discriminants, and add hidden
15505 -- components for the extra renamed discriminants. We recognize
15506 -- multiple uses through the Corresponding_Discriminant of a
15507 -- new discriminant: if it constrains several old discriminants,
15508 -- this field points to the last one in the parent type. The
15509 -- stored discriminants of the derived type have the same name
15510 -- as those of the parent.
15512 declare
15513 Constr : Elmt_Id;
15514 New_Discr : Entity_Id;
15515 Old_Discr : Entity_Id;
15517 begin
15518 Constr := First_Elmt (Stored_Constraint (Typ));
15519 Old_Discr := First_Stored_Discriminant (Typ);
15520 while Present (Constr) loop
15521 if Is_Entity_Name (Node (Constr))
15522 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15523 then
15524 New_Discr := Entity (Node (Constr));
15526 if Chars (Corresponding_Discriminant (New_Discr)) /=
15527 Chars (Old_Discr)
15528 then
15529 -- The new discriminant has been used to rename a
15530 -- subsequent old discriminant. Introduce a shadow
15531 -- component for the current old discriminant.
15533 New_C := Create_Component (Old_Discr);
15534 Set_Original_Record_Component (New_C, Old_Discr);
15535 end if;
15537 else
15538 -- The constraint has eliminated the old discriminant.
15539 -- Introduce a shadow component.
15541 New_C := Create_Component (Old_Discr);
15542 Set_Original_Record_Component (New_C, Old_Discr);
15543 end if;
15545 Next_Elmt (Constr);
15546 Next_Stored_Discriminant (Old_Discr);
15547 end loop;
15548 end;
15549 end if;
15550 end Add_Discriminants;
15552 if Is_Compile_Time_Known
15553 and then Is_Variant_Record (Typ)
15554 then
15555 Collect_Fixed_Components (Typ);
15556 Gather_Components
15557 (Typ,
15558 Component_List (Type_Definition (Parent (Typ))),
15559 Governed_By => Assoc_List,
15560 Into => Comp_List,
15561 Report_Errors => Errors,
15562 Allow_Compile_Time => True);
15563 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15565 Create_All_Components;
15567 -- If the subtype declaration is created for a tagged type derivation
15568 -- with constraints, we retrieve the record definition of the parent
15569 -- type to select the components of the proper variant.
15571 elsif Is_Compile_Time_Known
15572 and then Is_Tagged_Type (Typ)
15573 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15574 and then
15575 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15576 and then Is_Variant_Record (Parent_Type)
15577 then
15578 Collect_Fixed_Components (Typ);
15579 Gather_Components
15580 (Typ,
15581 Component_List (Type_Definition (Parent (Parent_Type))),
15582 Governed_By => Assoc_List,
15583 Into => Comp_List,
15584 Report_Errors => Errors,
15585 Allow_Compile_Time => True);
15587 -- Note: previously there was a check at this point that no errors
15588 -- were detected. As a consequence of AI05-220 there may be an error
15589 -- if an inherited discriminant that controls a variant has a non-
15590 -- static constraint.
15592 -- If the tagged derivation has a type extension, collect all the
15593 -- new relevant components therein via Gather_Components.
15595 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15596 then
15597 Gather_Components
15598 (Typ,
15599 Component_List
15600 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15601 Governed_By => Assoc_List,
15602 Into => Comp_List,
15603 Report_Errors => Errors,
15604 Allow_Compile_Time => True,
15605 Include_Interface_Tag => True);
15606 end if;
15608 Create_All_Components;
15610 else
15611 -- If discriminants are not static, or if this is a multi-level type
15612 -- extension, we have to include all components of the parent type.
15614 Old_C := First_Component (Typ);
15615 while Present (Old_C) loop
15616 New_C := Create_Component (Old_C);
15618 Set_Etype
15619 (New_C,
15620 Constrain_Component_Type
15621 (Old_C, Subt, Decl_Node, Typ, Constraints));
15622 Set_Is_Public (New_C, Is_Public (Subt));
15624 Next_Component (Old_C);
15625 end loop;
15626 end if;
15628 End_Scope;
15629 end Create_Constrained_Components;
15631 ------------------------------------------
15632 -- Decimal_Fixed_Point_Type_Declaration --
15633 ------------------------------------------
15635 procedure Decimal_Fixed_Point_Type_Declaration
15636 (T : Entity_Id;
15637 Def : Node_Id)
15639 Loc : constant Source_Ptr := Sloc (Def);
15640 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15641 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15642 Max_Digits : constant Nat :=
15643 (if System_Max_Integer_Size = 128 then 38 else 18);
15644 -- Maximum number of digits that can be represented in an integer
15646 Implicit_Base : Entity_Id;
15647 Digs_Val : Uint;
15648 Delta_Val : Ureal;
15649 Scale_Val : Uint;
15650 Bound_Val : Ureal;
15652 begin
15653 Check_Restriction (No_Fixed_Point, Def);
15655 -- Create implicit base type
15657 Implicit_Base :=
15658 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15659 Set_Etype (Implicit_Base, Implicit_Base);
15661 -- Analyze and process delta expression
15663 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15665 Check_Delta_Expression (Delta_Expr);
15666 Delta_Val := Expr_Value_R (Delta_Expr);
15668 -- Check delta is power of 10, and determine scale value from it
15670 declare
15671 Val : Ureal;
15673 begin
15674 Scale_Val := Uint_0;
15675 Val := Delta_Val;
15677 if Val < Ureal_1 then
15678 while Val < Ureal_1 loop
15679 Val := Val * Ureal_10;
15680 Scale_Val := Scale_Val + 1;
15681 end loop;
15683 if Scale_Val > Max_Digits then
15684 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15685 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15686 Scale_Val := UI_From_Int (Max_Digits);
15687 end if;
15689 else
15690 while Val > Ureal_1 loop
15691 Val := Val / Ureal_10;
15692 Scale_Val := Scale_Val - 1;
15693 end loop;
15695 if Scale_Val < -Max_Digits then
15696 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15697 Error_Msg_N ("scale is less than minimum value of ^", Def);
15698 Scale_Val := UI_From_Int (-Max_Digits);
15699 end if;
15700 end if;
15702 if Val /= Ureal_1 then
15703 Error_Msg_N ("delta expression must be a power of 10", Def);
15704 Delta_Val := Ureal_10 ** (-Scale_Val);
15705 end if;
15706 end;
15708 -- Set delta, scale and small (small = delta for decimal type)
15710 Set_Delta_Value (Implicit_Base, Delta_Val);
15711 Set_Scale_Value (Implicit_Base, Scale_Val);
15712 Set_Small_Value (Implicit_Base, Delta_Val);
15714 -- Analyze and process digits expression
15716 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15717 Check_Digits_Expression (Digs_Expr);
15718 Digs_Val := Expr_Value (Digs_Expr);
15720 if Digs_Val > Max_Digits then
15721 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15722 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15723 Digs_Val := UI_From_Int (Max_Digits);
15724 end if;
15726 Set_Digits_Value (Implicit_Base, Digs_Val);
15727 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15729 -- Set range of base type from digits value for now. This will be
15730 -- expanded to represent the true underlying base range by Freeze.
15732 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15734 -- Note: We leave Esize unset for now, size will be set at freeze
15735 -- time. We have to do this for ordinary fixed-point, because the size
15736 -- depends on the specified small, and we might as well do the same for
15737 -- decimal fixed-point.
15739 pragma Assert (not Known_Esize (Implicit_Base));
15741 -- If there are bounds given in the declaration use them as the
15742 -- bounds of the first named subtype.
15744 if Present (Real_Range_Specification (Def)) then
15745 declare
15746 RRS : constant Node_Id := Real_Range_Specification (Def);
15747 Low : constant Node_Id := Low_Bound (RRS);
15748 High : constant Node_Id := High_Bound (RRS);
15749 Low_Val : Ureal;
15750 High_Val : Ureal;
15752 begin
15753 Analyze_And_Resolve (Low, Any_Real);
15754 Analyze_And_Resolve (High, Any_Real);
15755 Check_Real_Bound (Low);
15756 Check_Real_Bound (High);
15757 Low_Val := Expr_Value_R (Low);
15758 High_Val := Expr_Value_R (High);
15760 if Low_Val < (-Bound_Val) then
15761 Error_Msg_N
15762 ("range low bound too small for digits value", Low);
15763 Low_Val := -Bound_Val;
15764 end if;
15766 if High_Val > Bound_Val then
15767 Error_Msg_N
15768 ("range high bound too large for digits value", High);
15769 High_Val := Bound_Val;
15770 end if;
15772 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15773 end;
15775 -- If no explicit range, use range that corresponds to given
15776 -- digits value. This will end up as the final range for the
15777 -- first subtype.
15779 else
15780 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15781 end if;
15783 -- Complete entity for first subtype. The inheritance of the rep item
15784 -- chain ensures that SPARK-related pragmas are not clobbered when the
15785 -- decimal fixed point type acts as a full view of a private type.
15787 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15788 Set_Etype (T, Implicit_Base);
15789 Set_Size_Info (T, Implicit_Base);
15790 Inherit_Rep_Item_Chain (T, Implicit_Base);
15791 Set_Digits_Value (T, Digs_Val);
15792 Set_Delta_Value (T, Delta_Val);
15793 Set_Small_Value (T, Delta_Val);
15794 Set_Scale_Value (T, Scale_Val);
15795 Set_Is_Constrained (T);
15796 end Decimal_Fixed_Point_Type_Declaration;
15798 -----------------------------------
15799 -- Derive_Progenitor_Subprograms --
15800 -----------------------------------
15802 procedure Derive_Progenitor_Subprograms
15803 (Parent_Type : Entity_Id;
15804 Tagged_Type : Entity_Id)
15806 E : Entity_Id;
15807 Elmt : Elmt_Id;
15808 Iface : Entity_Id;
15809 Iface_Alias : Entity_Id;
15810 Iface_Elmt : Elmt_Id;
15811 Iface_Subp : Entity_Id;
15812 New_Subp : Entity_Id := Empty;
15813 Prim_Elmt : Elmt_Id;
15814 Subp : Entity_Id;
15815 Typ : Entity_Id;
15817 begin
15818 pragma Assert (Ada_Version >= Ada_2005
15819 and then Is_Record_Type (Tagged_Type)
15820 and then Is_Tagged_Type (Tagged_Type)
15821 and then Has_Interfaces (Tagged_Type));
15823 -- Step 1: Transfer to the full-view primitives associated with the
15824 -- partial-view that cover interface primitives. Conceptually this
15825 -- work should be done later by Process_Full_View; done here to
15826 -- simplify its implementation at later stages. It can be safely
15827 -- done here because interfaces must be visible in the partial and
15828 -- private view (RM 7.3(7.3/2)).
15830 -- Small optimization: This work is only required if the parent may
15831 -- have entities whose Alias attribute reference an interface primitive.
15832 -- Such a situation may occur if the parent is an abstract type and the
15833 -- primitive has not been yet overridden or if the parent is a generic
15834 -- formal type covering interfaces.
15836 -- If the tagged type is not abstract, it cannot have abstract
15837 -- primitives (the only entities in the list of primitives of
15838 -- non-abstract tagged types that can reference abstract primitives
15839 -- through its Alias attribute are the internal entities that have
15840 -- attribute Interface_Alias, and these entities are generated later
15841 -- by Add_Internal_Interface_Entities).
15843 if In_Private_Part (Current_Scope)
15844 and then (Is_Abstract_Type (Parent_Type)
15845 or else
15846 Is_Generic_Type (Parent_Type))
15847 then
15848 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15849 while Present (Elmt) loop
15850 Subp := Node (Elmt);
15852 -- At this stage it is not possible to have entities in the list
15853 -- of primitives that have attribute Interface_Alias.
15855 pragma Assert (No (Interface_Alias (Subp)));
15857 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15859 if Is_Interface (Typ) then
15860 E := Find_Primitive_Covering_Interface
15861 (Tagged_Type => Tagged_Type,
15862 Iface_Prim => Subp);
15864 if Present (E)
15865 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15866 then
15867 Replace_Elmt (Elmt, E);
15868 Remove_Homonym (Subp);
15869 end if;
15870 end if;
15872 Next_Elmt (Elmt);
15873 end loop;
15874 end if;
15876 -- Step 2: Add primitives of progenitors that are not implemented by
15877 -- parents of Tagged_Type.
15879 if Present (Interfaces (Base_Type (Tagged_Type))) then
15880 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15881 while Present (Iface_Elmt) loop
15882 Iface := Node (Iface_Elmt);
15884 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15885 while Present (Prim_Elmt) loop
15886 Iface_Subp := Node (Prim_Elmt);
15887 Iface_Alias := Ultimate_Alias (Iface_Subp);
15889 -- Exclude derivation of predefined primitives except those
15890 -- that come from source, or are inherited from one that comes
15891 -- from source. Required to catch declarations of equality
15892 -- operators of interfaces. For example:
15894 -- type Iface is interface;
15895 -- function "=" (Left, Right : Iface) return Boolean;
15897 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15898 or else Comes_From_Source (Iface_Alias)
15899 then
15900 E :=
15901 Find_Primitive_Covering_Interface
15902 (Tagged_Type => Tagged_Type,
15903 Iface_Prim => Iface_Subp);
15905 -- If not found we derive a new primitive leaving its alias
15906 -- attribute referencing the interface primitive.
15908 if No (E) then
15909 Derive_Subprogram
15910 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15912 -- Ada 2012 (AI05-0197): If the covering primitive's name
15913 -- differs from the name of the interface primitive then it
15914 -- is a private primitive inherited from a parent type. In
15915 -- such case, given that Tagged_Type covers the interface,
15916 -- the inherited private primitive becomes visible. For such
15917 -- purpose we add a new entity that renames the inherited
15918 -- private primitive.
15920 elsif Chars (E) /= Chars (Iface_Subp) then
15921 pragma Assert (Has_Suffix (E, 'P'));
15922 Derive_Subprogram
15923 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15924 Set_Alias (New_Subp, E);
15925 Set_Is_Abstract_Subprogram (New_Subp,
15926 Is_Abstract_Subprogram (E));
15928 -- Propagate to the full view interface entities associated
15929 -- with the partial view.
15931 elsif In_Private_Part (Current_Scope)
15932 and then Present (Alias (E))
15933 and then Alias (E) = Iface_Subp
15934 and then
15935 List_Containing (Parent (E)) /=
15936 Private_Declarations
15937 (Specification
15938 (Unit_Declaration_Node (Current_Scope)))
15939 then
15940 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15941 end if;
15942 end if;
15944 Next_Elmt (Prim_Elmt);
15945 end loop;
15947 Next_Elmt (Iface_Elmt);
15948 end loop;
15949 end if;
15950 end Derive_Progenitor_Subprograms;
15952 -----------------------
15953 -- Derive_Subprogram --
15954 -----------------------
15956 procedure Derive_Subprogram
15957 (New_Subp : out Entity_Id;
15958 Parent_Subp : Entity_Id;
15959 Derived_Type : Entity_Id;
15960 Parent_Type : Entity_Id;
15961 Actual_Subp : Entity_Id := Empty)
15963 Formal : Entity_Id;
15964 -- Formal parameter of parent primitive operation
15966 Formal_Of_Actual : Entity_Id;
15967 -- Formal parameter of actual operation, when the derivation is to
15968 -- create a renaming for a primitive operation of an actual in an
15969 -- instantiation.
15971 New_Formal : Entity_Id;
15972 -- Formal of inherited operation
15974 Visible_Subp : Entity_Id := Parent_Subp;
15976 function Is_Private_Overriding return Boolean;
15977 -- If Subp is a private overriding of a visible operation, the inherited
15978 -- operation derives from the overridden op (even though its body is the
15979 -- overriding one) and the inherited operation is visible now. See
15980 -- sem_disp to see the full details of the handling of the overridden
15981 -- subprogram, which is removed from the list of primitive operations of
15982 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15983 -- and used to diagnose abstract operations that need overriding in the
15984 -- derived type.
15986 procedure Replace_Type (Id, New_Id : Entity_Id);
15987 -- Set the Etype of New_Id to the appropriate subtype determined from
15988 -- the Etype of Id, following (RM 3.4 (18, 19, 20, 21)). Id is either
15989 -- the parent type's primitive subprogram or one of its formals, and
15990 -- New_Id is the corresponding entity for the derived type. When the
15991 -- Etype of Id is an anonymous access type, create a new access type
15992 -- designating the derived type.
15994 procedure Set_Derived_Name;
15995 -- This procedure sets the appropriate Chars name for New_Subp. This
15996 -- is normally just a copy of the parent name. An exception arises for
15997 -- type support subprograms, where the name is changed to reflect the
15998 -- name of the derived type, e.g. if type foo is derived from type bar,
15999 -- then a procedure barDA is derived with a name fooDA.
16001 ---------------------------
16002 -- Is_Private_Overriding --
16003 ---------------------------
16005 function Is_Private_Overriding return Boolean is
16006 Prev : Entity_Id;
16008 begin
16009 -- If the parent is not a dispatching operation there is no
16010 -- need to investigate overridings
16012 if not Is_Dispatching_Operation (Parent_Subp) then
16013 return False;
16014 end if;
16016 -- The visible operation that is overridden is a homonym of the
16017 -- parent subprogram. We scan the homonym chain to find the one
16018 -- whose alias is the subprogram we are deriving.
16020 Prev := Current_Entity (Parent_Subp);
16021 while Present (Prev) loop
16022 if Ekind (Prev) = Ekind (Parent_Subp)
16023 and then Alias (Prev) = Parent_Subp
16024 and then Scope (Parent_Subp) = Scope (Prev)
16025 and then not Is_Hidden (Prev)
16026 then
16027 Visible_Subp := Prev;
16028 return True;
16029 end if;
16031 Prev := Homonym (Prev);
16032 end loop;
16034 return False;
16035 end Is_Private_Overriding;
16037 ------------------
16038 -- Replace_Type --
16039 ------------------
16041 procedure Replace_Type (Id, New_Id : Entity_Id) is
16042 Id_Type : constant Entity_Id := Etype (Id);
16043 Par : constant Node_Id := Parent (Derived_Type);
16045 begin
16046 -- When the type is an anonymous access type, create a new access
16047 -- type designating the derived type. This itype must be elaborated
16048 -- at the point of the derivation, not on subsequent calls that may
16049 -- be out of the proper scope for Gigi, so we insert a reference to
16050 -- it after the derivation.
16052 if Ekind (Id_Type) = E_Anonymous_Access_Type then
16053 declare
16054 Acc_Type : Entity_Id;
16055 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
16057 begin
16058 if Ekind (Desig_Typ) = E_Record_Type_With_Private
16059 and then Present (Full_View (Desig_Typ))
16060 and then not Is_Private_Type (Parent_Type)
16061 then
16062 Desig_Typ := Full_View (Desig_Typ);
16063 end if;
16065 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
16067 -- Ada 2005 (AI-251): Handle also derivations of abstract
16068 -- interface primitives.
16070 or else (Is_Interface (Desig_Typ)
16071 and then not Is_Class_Wide_Type (Desig_Typ))
16072 then
16073 Acc_Type := New_Copy (Id_Type);
16074 Set_Etype (Acc_Type, Acc_Type);
16075 Set_Scope (Acc_Type, New_Subp);
16077 -- Set size of anonymous access type. If we have an access
16078 -- to an unconstrained array, this is a fat pointer, so it
16079 -- is sizes at twice addtress size.
16081 if Is_Array_Type (Desig_Typ)
16082 and then not Is_Constrained (Desig_Typ)
16083 then
16084 Init_Size (Acc_Type, 2 * System_Address_Size);
16086 -- Other cases use a thin pointer
16088 else
16089 Init_Size (Acc_Type, System_Address_Size);
16090 end if;
16092 -- Set remaining characterstics of anonymous access type
16094 Reinit_Alignment (Acc_Type);
16095 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
16097 Set_Etype (New_Id, Acc_Type);
16098 Set_Scope (New_Id, New_Subp);
16100 -- Create a reference to it
16102 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
16104 else
16105 Set_Etype (New_Id, Id_Type);
16106 end if;
16107 end;
16109 -- In Ada2012, a formal may have an incomplete type but the type
16110 -- derivation that inherits the primitive follows the full view.
16112 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
16113 or else
16114 (Ekind (Id_Type) = E_Record_Type_With_Private
16115 and then Present (Full_View (Id_Type))
16116 and then
16117 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
16118 or else
16119 (Ada_Version >= Ada_2012
16120 and then Ekind (Id_Type) = E_Incomplete_Type
16121 and then Full_View (Id_Type) = Parent_Type)
16122 then
16123 -- Constraint checks on formals are generated during expansion,
16124 -- based on the signature of the original subprogram. The bounds
16125 -- of the derived type are not relevant, and thus we can use
16126 -- the base type for the formals. However, the return type may be
16127 -- used in a context that requires that the proper static bounds
16128 -- be used (a case statement, for example) and for those cases
16129 -- we must use the derived type (first subtype), not its base.
16131 -- If the derived_type_definition has no constraints, we know that
16132 -- the derived type has the same constraints as the first subtype
16133 -- of the parent, and we can also use it rather than its base,
16134 -- which can lead to more efficient code.
16136 if Id_Type = Parent_Type then
16137 if Is_Scalar_Type (Parent_Type)
16138 and then
16139 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
16140 then
16141 Set_Etype (New_Id, Derived_Type);
16143 elsif Nkind (Par) = N_Full_Type_Declaration
16144 and then
16145 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
16146 and then
16147 Is_Entity_Name
16148 (Subtype_Indication (Type_Definition (Par)))
16149 then
16150 Set_Etype (New_Id, Derived_Type);
16152 else
16153 Set_Etype (New_Id, Base_Type (Derived_Type));
16154 end if;
16156 else
16157 Set_Etype (New_Id, Base_Type (Derived_Type));
16158 end if;
16160 else
16161 Set_Etype (New_Id, Id_Type);
16162 end if;
16163 end Replace_Type;
16165 ----------------------
16166 -- Set_Derived_Name --
16167 ----------------------
16169 procedure Set_Derived_Name is
16170 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
16171 begin
16172 if Nm = TSS_Null then
16173 Set_Chars (New_Subp, Chars (Parent_Subp));
16174 else
16175 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
16176 end if;
16177 end Set_Derived_Name;
16179 -- Start of processing for Derive_Subprogram
16181 begin
16182 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
16183 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
16184 Set_Is_Not_Self_Hidden (New_Subp);
16186 -- Check whether the inherited subprogram is a private operation that
16187 -- should be inherited but not yet made visible. Such subprograms can
16188 -- become visible at a later point (e.g., the private part of a public
16189 -- child unit) via Declare_Inherited_Private_Subprograms. If the
16190 -- following predicate is true, then this is not such a private
16191 -- operation and the subprogram simply inherits the name of the parent
16192 -- subprogram. Note the special check for the names of controlled
16193 -- operations, which are currently exempted from being inherited with
16194 -- a hidden name because they must be findable for generation of
16195 -- implicit run-time calls.
16197 if not Is_Hidden (Parent_Subp)
16198 or else Is_Internal (Parent_Subp)
16199 or else Is_Private_Overriding
16200 or else Is_Internal_Name (Chars (Parent_Subp))
16201 or else (Is_Controlled (Parent_Type)
16202 and then Chars (Parent_Subp) in Name_Adjust
16203 | Name_Finalize
16204 | Name_Initialize)
16205 then
16206 Set_Derived_Name;
16208 -- An inherited dispatching equality will be overridden by an internally
16209 -- generated one, or by an explicit one, so preserve its name and thus
16210 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
16211 -- private operation it may become invisible if the full view has
16212 -- progenitors, and the dispatch table will be malformed.
16213 -- We check that the type is limited to handle the anomalous declaration
16214 -- of Limited_Controlled, which is derived from a non-limited type, and
16215 -- which is handled specially elsewhere as well.
16217 elsif Chars (Parent_Subp) = Name_Op_Eq
16218 and then Is_Dispatching_Operation (Parent_Subp)
16219 and then Etype (Parent_Subp) = Standard_Boolean
16220 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
16221 and then
16222 Etype (First_Formal (Parent_Subp)) =
16223 Etype (Next_Formal (First_Formal (Parent_Subp)))
16224 then
16225 Set_Derived_Name;
16227 -- If parent is hidden, this can be a regular derivation if the
16228 -- parent is immediately visible in a non-instantiating context,
16229 -- or if we are in the private part of an instance. This test
16230 -- should still be refined ???
16232 -- The test for In_Instance_Not_Visible avoids inheriting the derived
16233 -- operation as a non-visible operation in cases where the parent
16234 -- subprogram might not be visible now, but was visible within the
16235 -- original generic, so it would be wrong to make the inherited
16236 -- subprogram non-visible now. (Not clear if this test is fully
16237 -- correct; are there any cases where we should declare the inherited
16238 -- operation as not visible to avoid it being overridden, e.g., when
16239 -- the parent type is a generic actual with private primitives ???)
16241 -- (they should be treated the same as other private inherited
16242 -- subprograms, but it's not clear how to do this cleanly). ???
16244 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16245 and then Is_Immediately_Visible (Parent_Subp)
16246 and then not In_Instance)
16247 or else In_Instance_Not_Visible
16248 then
16249 Set_Derived_Name;
16251 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
16252 -- overrides an interface primitive because interface primitives
16253 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
16255 elsif Ada_Version >= Ada_2005
16256 and then Is_Dispatching_Operation (Parent_Subp)
16257 and then Present (Covered_Interface_Op (Parent_Subp))
16258 then
16259 Set_Derived_Name;
16261 -- Otherwise, the type is inheriting a private operation, so enter it
16262 -- with a special name so it can't be overridden. See also below, where
16263 -- we check for this case, and if so avoid setting Requires_Overriding.
16265 else
16266 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
16267 end if;
16269 Set_Parent (New_Subp, Parent (Derived_Type));
16271 if Present (Actual_Subp) then
16272 Replace_Type (Actual_Subp, New_Subp);
16273 else
16274 Replace_Type (Parent_Subp, New_Subp);
16275 end if;
16277 Conditional_Delay (New_Subp, Parent_Subp);
16279 -- If we are creating a renaming for a primitive operation of an
16280 -- actual of a generic derived type, we must examine the signature
16281 -- of the actual primitive, not that of the generic formal, which for
16282 -- example may be an interface. However the name and initial value
16283 -- of the inherited operation are those of the formal primitive.
16285 Formal := First_Formal (Parent_Subp);
16287 if Present (Actual_Subp) then
16288 Formal_Of_Actual := First_Formal (Actual_Subp);
16289 else
16290 Formal_Of_Actual := Empty;
16291 end if;
16293 while Present (Formal) loop
16294 New_Formal := New_Copy (Formal);
16296 -- Extra formals are not inherited from a limited interface parent
16297 -- since limitedness is not inherited in such case (AI-419) and this
16298 -- affects the extra formals.
16300 if Is_Limited_Interface (Parent_Type) then
16301 Set_Extra_Formal (New_Formal, Empty);
16302 Set_Extra_Accessibility (New_Formal, Empty);
16303 end if;
16305 -- Normally we do not go copying parents, but in the case of
16306 -- formals, we need to link up to the declaration (which is the
16307 -- parameter specification), and it is fine to link up to the
16308 -- original formal's parameter specification in this case.
16310 Set_Parent (New_Formal, Parent (Formal));
16311 Append_Entity (New_Formal, New_Subp);
16313 if Present (Formal_Of_Actual) then
16314 Replace_Type (Formal_Of_Actual, New_Formal);
16315 Next_Formal (Formal_Of_Actual);
16316 else
16317 Replace_Type (Formal, New_Formal);
16318 end if;
16320 Next_Formal (Formal);
16321 end loop;
16323 -- Extra formals are shared between the parent subprogram and this
16324 -- internal entity built by Derive_Subprogram (implicit in the above
16325 -- copy of formals), unless the parent type is a limited interface type;
16326 -- hence we must inherit also the reference to the first extra formal.
16327 -- When the parent type is an interface, the extra formals will be added
16328 -- when the tagged type is frozen (see Expand_Freeze_Record_Type).
16330 if not Is_Limited_Interface (Parent_Type) then
16331 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
16333 if Ekind (New_Subp) = E_Function then
16334 Set_Extra_Accessibility_Of_Result (New_Subp,
16335 Extra_Accessibility_Of_Result (Parent_Subp));
16336 end if;
16337 end if;
16339 -- If this derivation corresponds to a tagged generic actual, then
16340 -- primitive operations rename those of the actual. Otherwise the
16341 -- primitive operations rename those of the parent type, If the parent
16342 -- renames an intrinsic operator, so does the new subprogram. We except
16343 -- concatenation, which is always properly typed, and does not get
16344 -- expanded as other intrinsic operations.
16346 if No (Actual_Subp) then
16347 if Is_Intrinsic_Subprogram (Parent_Subp) then
16348 Set_Convention (New_Subp, Convention_Intrinsic);
16349 Set_Is_Intrinsic_Subprogram (New_Subp);
16351 if Present (Alias (Parent_Subp))
16352 and then Chars (Parent_Subp) /= Name_Op_Concat
16353 then
16354 Set_Alias (New_Subp, Alias (Parent_Subp));
16355 else
16356 Set_Alias (New_Subp, Parent_Subp);
16357 end if;
16359 else
16360 Set_Alias (New_Subp, Parent_Subp);
16361 end if;
16363 else
16364 Set_Alias (New_Subp, Actual_Subp);
16365 end if;
16367 Copy_Strub_Mode (New_Subp, Alias (New_Subp));
16369 -- Derived subprograms of a tagged type must inherit the convention
16370 -- of the parent subprogram (a requirement of AI95-117). Derived
16371 -- subprograms of untagged types simply get convention Ada by default.
16373 -- If the derived type is a tagged generic formal type with unknown
16374 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16376 -- However, if the type is derived from a generic formal, the further
16377 -- inherited subprogram has the convention of the non-generic ancestor.
16378 -- Otherwise there would be no way to override the operation.
16379 -- (This is subject to forthcoming ARG discussions).
16381 if Is_Tagged_Type (Derived_Type) then
16382 if Is_Generic_Type (Derived_Type)
16383 and then Has_Unknown_Discriminants (Derived_Type)
16384 then
16385 Set_Convention (New_Subp, Convention_Intrinsic);
16387 else
16388 if Is_Generic_Type (Parent_Type)
16389 and then Has_Unknown_Discriminants (Parent_Type)
16390 then
16391 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16392 else
16393 Set_Convention (New_Subp, Convention (Parent_Subp));
16394 end if;
16395 end if;
16396 end if;
16398 -- Predefined controlled operations retain their name even if the parent
16399 -- is hidden (see above), but they are not primitive operations if the
16400 -- ancestor is not visible, for example if the parent is a private
16401 -- extension completed with a controlled extension. Note that a full
16402 -- type that is controlled can break privacy: the flag Is_Controlled is
16403 -- set on both views of the type.
16405 if Is_Controlled (Parent_Type)
16406 and then Chars (Parent_Subp) in Name_Initialize
16407 | Name_Adjust
16408 | Name_Finalize
16409 and then Is_Hidden (Parent_Subp)
16410 and then not Is_Visibly_Controlled (Parent_Type)
16411 then
16412 Set_Is_Hidden (New_Subp);
16413 end if;
16415 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16416 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16418 if Ekind (Parent_Subp) = E_Procedure then
16419 Set_Is_Valued_Procedure
16420 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16421 else
16422 Set_Has_Controlling_Result
16423 (New_Subp, Has_Controlling_Result (Parent_Subp));
16424 end if;
16426 -- No_Return must be inherited properly. If this is overridden in the
16427 -- case of a dispatching operation, then the check is made later in
16428 -- Check_Abstract_Overriding that the overriding operation is also
16429 -- No_Return (no such check is required for the nondispatching case).
16431 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16433 -- If the parent subprogram is marked as Ghost, then so is the derived
16434 -- subprogram. The ghost policy for the derived subprogram is set from
16435 -- the effective ghost policy at the point of derived type declaration.
16437 if Is_Ghost_Entity (Parent_Subp) then
16438 Set_Is_Ghost_Entity (New_Subp);
16439 end if;
16441 -- A derived function with a controlling result is abstract. If the
16442 -- Derived_Type is a nonabstract formal generic derived type, then
16443 -- inherited operations are not abstract: the required check is done at
16444 -- instantiation time. If the derivation is for a generic actual, the
16445 -- function is not abstract unless the actual is.
16447 if Is_Generic_Type (Derived_Type)
16448 and then not Is_Abstract_Type (Derived_Type)
16449 then
16450 null;
16452 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16453 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16454 -- that functions with controlling access results of record extensions
16455 -- with a null extension part require overriding (AI95-00391/06).
16457 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16458 -- implementing the rule of RM 7.3.2(6.1/4).
16460 -- A subprogram subject to pragma Extensions_Visible with value False
16461 -- requires overriding if the subprogram has at least one controlling
16462 -- OUT parameter (SPARK RM 6.1.7(6)).
16464 elsif Ada_Version >= Ada_2005
16465 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16466 or else (Is_Tagged_Type (Derived_Type)
16467 and then Etype (New_Subp) = Derived_Type
16468 and then not Is_Null_Extension (Derived_Type))
16469 or else (Is_Tagged_Type (Derived_Type)
16470 and then Ekind (Etype (New_Subp)) =
16471 E_Anonymous_Access_Type
16472 and then Designated_Type (Etype (New_Subp)) =
16473 Derived_Type)
16474 or else (Comes_From_Source (Alias (New_Subp))
16475 and then Is_EVF_Procedure (Alias (New_Subp)))
16477 -- AI12-0042: Set Requires_Overriding when a type extension
16478 -- inherits a private operation that is visible at the
16479 -- point of extension (Has_Private_Ancestor is False) from
16480 -- an ancestor that has Type_Invariant'Class, and when the
16481 -- type extension is in a visible part (the latter as
16482 -- clarified by AI12-0382).
16484 or else
16485 (not Has_Private_Ancestor (Derived_Type)
16486 and then Has_Invariants (Parent_Type)
16487 and then
16488 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16489 and then
16490 Class_Present
16491 (Get_Pragma (Parent_Type, Pragma_Invariant))
16492 and then Is_Private_Primitive (Parent_Subp)
16493 and then In_Visible_Part (Scope (Derived_Type))))
16495 and then No (Actual_Subp)
16496 then
16497 if not Is_Tagged_Type (Derived_Type)
16498 or else Is_Abstract_Type (Derived_Type)
16499 or else Is_Abstract_Subprogram (Alias (New_Subp))
16500 then
16501 Set_Is_Abstract_Subprogram (New_Subp);
16503 -- If the Chars of the new subprogram is different from that of the
16504 -- parent's one, it means that we entered it with a special name so
16505 -- it can't be overridden (see above). In that case we had better not
16506 -- *require* it to be overridden. This is the case where the parent
16507 -- type inherited the operation privately, so there's no danger of
16508 -- dangling dispatching.
16510 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16511 Set_Requires_Overriding (New_Subp);
16512 end if;
16514 elsif Ada_Version < Ada_2005
16515 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16516 or else (Is_Tagged_Type (Derived_Type)
16517 and then Etype (New_Subp) = Derived_Type
16518 and then No (Actual_Subp)))
16519 then
16520 Set_Is_Abstract_Subprogram (New_Subp);
16522 -- AI05-0097 : an inherited operation that dispatches on result is
16523 -- abstract if the derived type is abstract, even if the parent type
16524 -- is concrete and the derived type is a null extension.
16526 elsif Has_Controlling_Result (Alias (New_Subp))
16527 and then Is_Abstract_Type (Etype (New_Subp))
16528 then
16529 Set_Is_Abstract_Subprogram (New_Subp);
16531 -- Finally, if the parent type is abstract we must verify that all
16532 -- inherited operations are either non-abstract or overridden, or that
16533 -- the derived type itself is abstract (this check is performed at the
16534 -- end of a package declaration, in Check_Abstract_Overriding). A
16535 -- private overriding in the parent type will not be visible in the
16536 -- derivation if we are not in an inner package or in a child unit of
16537 -- the parent type, in which case the abstractness of the inherited
16538 -- operation is carried to the new subprogram.
16540 elsif Is_Abstract_Type (Parent_Type)
16541 and then not In_Open_Scopes (Scope (Parent_Type))
16542 and then Is_Private_Overriding
16543 and then Is_Abstract_Subprogram (Visible_Subp)
16544 then
16545 if No (Actual_Subp) then
16546 Set_Alias (New_Subp, Visible_Subp);
16547 Set_Is_Abstract_Subprogram (New_Subp, True);
16549 else
16550 -- If this is a derivation for an instance of a formal derived
16551 -- type, abstractness comes from the primitive operation of the
16552 -- actual, not from the operation inherited from the ancestor.
16554 Set_Is_Abstract_Subprogram
16555 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16556 end if;
16557 end if;
16559 New_Overloaded_Entity (New_Subp, Derived_Type);
16561 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16562 -- preconditions and the derived type is abstract, the derived operation
16563 -- is abstract as well if parent subprogram is not abstract or null.
16565 if Is_Abstract_Type (Derived_Type)
16566 and then Has_Non_Trivial_Precondition (Parent_Subp)
16567 and then Present (Interfaces (Derived_Type))
16568 then
16570 -- Add useful attributes of subprogram before the freeze point,
16571 -- in case freezing is delayed or there are previous errors.
16573 Set_Is_Dispatching_Operation (New_Subp);
16575 declare
16576 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16578 begin
16579 if Present (Iface_Prim)
16580 and then Has_Non_Trivial_Precondition (Iface_Prim)
16581 then
16582 Set_Is_Abstract_Subprogram (New_Subp);
16583 end if;
16584 end;
16585 end if;
16587 -- Check for case of a derived subprogram for the instantiation of a
16588 -- formal derived tagged type, if so mark the subprogram as dispatching
16589 -- and inherit the dispatching attributes of the actual subprogram. The
16590 -- derived subprogram is effectively renaming of the actual subprogram,
16591 -- so it needs to have the same attributes as the actual.
16593 if Present (Actual_Subp)
16594 and then Is_Dispatching_Operation (Actual_Subp)
16595 then
16596 Set_Is_Dispatching_Operation (New_Subp);
16598 if Present (DTC_Entity (Actual_Subp)) then
16599 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16600 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16601 end if;
16602 end if;
16604 -- Indicate that a derived subprogram does not require a body and that
16605 -- it does not require processing of default expressions.
16607 Set_Has_Completion (New_Subp);
16608 Set_Default_Expressions_Processed (New_Subp);
16610 if Ekind (New_Subp) = E_Function then
16611 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16612 Set_Returns_By_Ref (New_Subp, Returns_By_Ref (Parent_Subp));
16613 end if;
16615 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16616 -- primitive subprogram S of a type T, then the aspect is inherited
16617 -- by the corresponding primitive subprogram of each descendant of T.
16619 if Is_Tagged_Type (Derived_Type)
16620 and then Is_Dispatching_Operation (New_Subp)
16621 and then Has_Yield_Aspect (Alias (New_Subp))
16622 then
16623 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16624 end if;
16626 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16627 end Derive_Subprogram;
16629 ------------------------
16630 -- Derive_Subprograms --
16631 ------------------------
16633 procedure Derive_Subprograms
16634 (Parent_Type : Entity_Id;
16635 Derived_Type : Entity_Id;
16636 Generic_Actual : Entity_Id := Empty)
16638 Op_List : constant Elist_Id :=
16639 Collect_Primitive_Operations (Parent_Type);
16641 function Check_Derived_Type return Boolean;
16642 -- Check that all the entities derived from Parent_Type are found in
16643 -- the list of primitives of Derived_Type exactly in the same order.
16645 procedure Derive_Interface_Subprogram
16646 (New_Subp : out Entity_Id;
16647 Subp : Entity_Id;
16648 Actual_Subp : Entity_Id);
16649 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16650 -- (which is an interface primitive). If Generic_Actual is present then
16651 -- Actual_Subp is the actual subprogram corresponding with the generic
16652 -- subprogram Subp.
16654 ------------------------
16655 -- Check_Derived_Type --
16656 ------------------------
16658 function Check_Derived_Type return Boolean is
16659 E : Entity_Id;
16660 Derived_Elmt : Elmt_Id;
16661 Derived_Op : Entity_Id;
16662 Derived_Ops : Elist_Id;
16663 Parent_Elmt : Elmt_Id;
16664 Parent_Op : Entity_Id;
16666 begin
16667 -- Traverse list of entities in the current scope searching for
16668 -- an incomplete type whose full-view is derived type.
16670 E := First_Entity (Scope (Derived_Type));
16671 while Present (E) and then E /= Derived_Type loop
16672 if Ekind (E) = E_Incomplete_Type
16673 and then Present (Full_View (E))
16674 and then Full_View (E) = Derived_Type
16675 then
16676 -- Disable this test if Derived_Type completes an incomplete
16677 -- type because in such case more primitives can be added
16678 -- later to the list of primitives of Derived_Type by routine
16679 -- Process_Incomplete_Dependents.
16681 return True;
16682 end if;
16684 Next_Entity (E);
16685 end loop;
16687 Derived_Ops := Collect_Primitive_Operations (Derived_Type);
16689 Derived_Elmt := First_Elmt (Derived_Ops);
16690 Parent_Elmt := First_Elmt (Op_List);
16691 while Present (Parent_Elmt) loop
16692 Parent_Op := Node (Parent_Elmt);
16693 Derived_Op := Node (Derived_Elmt);
16695 -- At this early stage Derived_Type has no entities with attribute
16696 -- Interface_Alias. In addition, such primitives are always
16697 -- located at the end of the list of primitives of Parent_Type.
16698 -- Therefore, if found we can safely stop processing pending
16699 -- entities.
16701 exit when Present (Interface_Alias (Parent_Op));
16703 -- Handle hidden entities
16705 if not Is_Predefined_Dispatching_Operation (Parent_Op)
16706 and then Is_Hidden (Parent_Op)
16707 then
16708 if Present (Derived_Op)
16709 and then Primitive_Names_Match (Parent_Op, Derived_Op)
16710 then
16711 Next_Elmt (Derived_Elmt);
16712 end if;
16714 else
16715 if No (Derived_Op)
16716 or else Ekind (Parent_Op) /= Ekind (Derived_Op)
16717 or else not Primitive_Names_Match (Parent_Op, Derived_Op)
16718 then
16719 return False;
16720 end if;
16722 Next_Elmt (Derived_Elmt);
16723 end if;
16725 Next_Elmt (Parent_Elmt);
16726 end loop;
16728 return True;
16729 end Check_Derived_Type;
16731 ---------------------------------
16732 -- Derive_Interface_Subprogram --
16733 ---------------------------------
16735 procedure Derive_Interface_Subprogram
16736 (New_Subp : out Entity_Id;
16737 Subp : Entity_Id;
16738 Actual_Subp : Entity_Id)
16740 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16741 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16743 begin
16744 pragma Assert (Is_Interface (Iface_Type));
16746 Derive_Subprogram
16747 (New_Subp => New_Subp,
16748 Parent_Subp => Iface_Subp,
16749 Derived_Type => Derived_Type,
16750 Parent_Type => Iface_Type,
16751 Actual_Subp => Actual_Subp);
16753 -- Given that this new interface entity corresponds with a primitive
16754 -- of the parent that was not overridden we must leave it associated
16755 -- with its parent primitive to ensure that it will share the same
16756 -- dispatch table slot when overridden. We must set the Alias to Subp
16757 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16758 -- (in case we inherited Subp from Iface_Type via a nonabstract
16759 -- generic formal type).
16761 if No (Actual_Subp) then
16762 Set_Alias (New_Subp, Subp);
16764 declare
16765 T : Entity_Id := Find_Dispatching_Type (Subp);
16766 begin
16767 while Etype (T) /= T loop
16768 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16769 Set_Is_Abstract_Subprogram (New_Subp, False);
16770 exit;
16771 end if;
16773 T := Etype (T);
16774 end loop;
16775 end;
16777 -- For instantiations this is not needed since the previous call to
16778 -- Derive_Subprogram leaves the entity well decorated.
16780 else
16781 pragma Assert (Alias (New_Subp) = Actual_Subp);
16782 null;
16783 end if;
16784 end Derive_Interface_Subprogram;
16786 -- Local variables
16788 Alias_Subp : Entity_Id;
16789 Act_List : Elist_Id;
16790 Act_Elmt : Elmt_Id;
16791 Act_Subp : Entity_Id := Empty;
16792 Elmt : Elmt_Id;
16793 Need_Search : Boolean := False;
16794 New_Subp : Entity_Id;
16795 Parent_Base : Entity_Id;
16796 Subp : Entity_Id;
16798 -- Start of processing for Derive_Subprograms
16800 begin
16801 if Ekind (Parent_Type) = E_Record_Type_With_Private
16802 and then Has_Discriminants (Parent_Type)
16803 and then Present (Full_View (Parent_Type))
16804 then
16805 Parent_Base := Full_View (Parent_Type);
16806 else
16807 Parent_Base := Parent_Type;
16808 end if;
16810 if Present (Generic_Actual) then
16811 Act_List := Collect_Primitive_Operations (Generic_Actual);
16812 Act_Elmt := First_Elmt (Act_List);
16813 else
16814 Act_List := No_Elist;
16815 Act_Elmt := No_Elmt;
16816 end if;
16818 -- Derive primitives inherited from the parent. Note that if the generic
16819 -- actual is present, this is not really a type derivation, it is a
16820 -- completion within an instance.
16822 -- Case 1: Derived_Type does not implement interfaces
16824 if not Is_Tagged_Type (Derived_Type)
16825 or else (not Has_Interfaces (Derived_Type)
16826 and then not (Present (Generic_Actual)
16827 and then Has_Interfaces (Generic_Actual)))
16828 then
16829 Elmt := First_Elmt (Op_List);
16830 while Present (Elmt) loop
16831 Subp := Node (Elmt);
16833 -- Literals are derived earlier in the process of building the
16834 -- derived type, and are skipped here.
16836 if Ekind (Subp) = E_Enumeration_Literal then
16837 null;
16839 -- The actual is a direct descendant and the common primitive
16840 -- operations appear in the same order.
16842 -- If the generic parent type is present, the derived type is an
16843 -- instance of a formal derived type, and within the instance its
16844 -- operations are those of the actual. We derive from the formal
16845 -- type but make the inherited operations aliases of the
16846 -- corresponding operations of the actual.
16848 else
16849 pragma Assert (No (Node (Act_Elmt))
16850 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16851 and then
16852 Type_Conformant
16853 (Subp, Node (Act_Elmt),
16854 Skip_Controlling_Formals => True)));
16856 Derive_Subprogram
16857 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16859 if Present (Act_Elmt) then
16860 Next_Elmt (Act_Elmt);
16861 end if;
16862 end if;
16864 Next_Elmt (Elmt);
16865 end loop;
16867 -- Case 2: Derived_Type implements interfaces
16869 else
16870 -- If the parent type has no predefined primitives we remove
16871 -- predefined primitives from the list of primitives of generic
16872 -- actual to simplify the complexity of this algorithm.
16874 if Present (Generic_Actual) then
16875 declare
16876 Has_Predefined_Primitives : Boolean := False;
16878 begin
16879 -- Check if the parent type has predefined primitives
16881 Elmt := First_Elmt (Op_List);
16882 while Present (Elmt) loop
16883 Subp := Node (Elmt);
16885 if Is_Predefined_Dispatching_Operation (Subp)
16886 and then not Comes_From_Source (Ultimate_Alias (Subp))
16887 then
16888 Has_Predefined_Primitives := True;
16889 exit;
16890 end if;
16892 Next_Elmt (Elmt);
16893 end loop;
16895 -- Remove predefined primitives of Generic_Actual. We must use
16896 -- an auxiliary list because in case of tagged types the value
16897 -- returned by Collect_Primitive_Operations is the value stored
16898 -- in its Primitive_Operations attribute (and we don't want to
16899 -- modify its current contents).
16901 if not Has_Predefined_Primitives then
16902 declare
16903 Aux_List : constant Elist_Id := New_Elmt_List;
16905 begin
16906 Elmt := First_Elmt (Act_List);
16907 while Present (Elmt) loop
16908 Subp := Node (Elmt);
16910 if not Is_Predefined_Dispatching_Operation (Subp)
16911 or else Comes_From_Source (Subp)
16912 then
16913 Append_Elmt (Subp, Aux_List);
16914 end if;
16916 Next_Elmt (Elmt);
16917 end loop;
16919 Act_List := Aux_List;
16920 end;
16921 end if;
16923 Act_Elmt := First_Elmt (Act_List);
16924 Act_Subp := Node (Act_Elmt);
16925 end;
16926 end if;
16928 -- Stage 1: If the generic actual is not present we derive the
16929 -- primitives inherited from the parent type. If the generic parent
16930 -- type is present, the derived type is an instance of a formal
16931 -- derived type, and within the instance its operations are those of
16932 -- the actual. We derive from the formal type but make the inherited
16933 -- operations aliases of the corresponding operations of the actual.
16935 Elmt := First_Elmt (Op_List);
16936 while Present (Elmt) loop
16937 Subp := Node (Elmt);
16938 Alias_Subp := Ultimate_Alias (Subp);
16940 -- Do not derive internal entities of the parent that link
16941 -- interface primitives with their covering primitive. These
16942 -- entities will be added to this type when frozen.
16944 if Present (Interface_Alias (Subp)) then
16945 goto Continue;
16946 end if;
16948 -- If the generic actual is present find the corresponding
16949 -- operation in the generic actual. If the parent type is a
16950 -- direct ancestor of the derived type then, even if it is an
16951 -- interface, the operations are inherited from the primary
16952 -- dispatch table and are in the proper order. If we detect here
16953 -- that primitives are not in the same order we traverse the list
16954 -- of primitive operations of the actual to find the one that
16955 -- implements the interface primitive.
16957 if Need_Search
16958 or else
16959 (Present (Generic_Actual)
16960 and then Present (Act_Subp)
16961 and then not
16962 (Primitive_Names_Match (Subp, Act_Subp)
16963 and then
16964 Type_Conformant (Subp, Act_Subp,
16965 Skip_Controlling_Formals => True)))
16966 then
16967 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16968 Use_Full_View => True));
16970 -- Remember that we need searching for all pending primitives
16972 Need_Search := True;
16974 -- Handle entities associated with interface primitives
16976 if Present (Alias_Subp)
16977 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16978 and then not Is_Predefined_Dispatching_Operation (Subp)
16979 then
16980 -- Search for the primitive in the homonym chain
16982 Act_Subp :=
16983 Find_Primitive_Covering_Interface
16984 (Tagged_Type => Generic_Actual,
16985 Iface_Prim => Alias_Subp);
16987 -- Previous search may not locate primitives covering
16988 -- interfaces defined in generics units or instantiations.
16989 -- (it fails if the covering primitive has formals whose
16990 -- type is also defined in generics or instantiations).
16991 -- In such case we search in the list of primitives of the
16992 -- generic actual for the internal entity that links the
16993 -- interface primitive and the covering primitive.
16995 if No (Act_Subp)
16996 and then Is_Generic_Type (Parent_Type)
16997 then
16998 -- This code has been designed to handle only generic
16999 -- formals that implement interfaces that are defined
17000 -- in a generic unit or instantiation. If this code is
17001 -- needed for other cases we must review it because
17002 -- (given that it relies on Original_Location to locate
17003 -- the primitive of Generic_Actual that covers the
17004 -- interface) it could leave linked through attribute
17005 -- Alias entities of unrelated instantiations).
17007 pragma Assert
17008 (Is_Generic_Unit
17009 (Scope (Find_Dispatching_Type (Alias_Subp)))
17010 or else
17011 Instantiation_Location
17012 (Sloc (Find_Dispatching_Type (Alias_Subp)))
17013 /= No_Location);
17014 declare
17015 Iface_Prim_Loc : constant Source_Ptr :=
17016 Original_Location (Sloc (Alias_Subp));
17018 Elmt : Elmt_Id;
17019 Prim : Entity_Id;
17021 begin
17022 Elmt :=
17023 First_Elmt (Primitive_Operations (Generic_Actual));
17025 Search : while Present (Elmt) loop
17026 Prim := Node (Elmt);
17028 if Present (Interface_Alias (Prim))
17029 and then Original_Location
17030 (Sloc (Interface_Alias (Prim))) =
17031 Iface_Prim_Loc
17032 then
17033 Act_Subp := Alias (Prim);
17034 exit Search;
17035 end if;
17037 Next_Elmt (Elmt);
17038 end loop Search;
17039 end;
17040 end if;
17042 pragma Assert (Present (Act_Subp)
17043 or else Is_Abstract_Type (Generic_Actual)
17044 or else Serious_Errors_Detected > 0);
17046 -- Handle predefined primitives plus the rest of user-defined
17047 -- primitives
17049 else
17050 Act_Elmt := First_Elmt (Act_List);
17051 while Present (Act_Elmt) loop
17052 Act_Subp := Node (Act_Elmt);
17054 exit when Primitive_Names_Match (Subp, Act_Subp)
17055 and then Type_Conformant
17056 (Subp, Act_Subp,
17057 Skip_Controlling_Formals => True)
17058 and then No (Interface_Alias (Act_Subp));
17060 Next_Elmt (Act_Elmt);
17061 end loop;
17063 if No (Act_Elmt) then
17064 Act_Subp := Empty;
17065 end if;
17066 end if;
17067 end if;
17069 -- Case 1: If the parent is a limited interface then it has the
17070 -- predefined primitives of synchronized interfaces. However, the
17071 -- actual type may be a non-limited type and hence it does not
17072 -- have such primitives.
17074 if Present (Generic_Actual)
17075 and then No (Act_Subp)
17076 and then Is_Limited_Interface (Parent_Base)
17077 and then Is_Predefined_Interface_Primitive (Subp)
17078 then
17079 null;
17081 -- Case 2: Inherit entities associated with interfaces that were
17082 -- not covered by the parent type. We exclude here null interface
17083 -- primitives because they do not need special management.
17085 -- We also exclude interface operations that are renamings. If the
17086 -- subprogram is an explicit renaming of an interface primitive,
17087 -- it is a regular primitive operation, and the presence of its
17088 -- alias is not relevant: it has to be derived like any other
17089 -- primitive.
17091 elsif Present (Alias (Subp))
17092 and then Nkind (Unit_Declaration_Node (Subp)) /=
17093 N_Subprogram_Renaming_Declaration
17094 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
17095 and then not
17096 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
17097 and then Null_Present (Parent (Alias_Subp)))
17098 then
17099 -- If this is an abstract private type then we transfer the
17100 -- derivation of the interface primitive from the partial view
17101 -- to the full view. This is safe because all the interfaces
17102 -- must be visible in the partial view. Done to avoid adding
17103 -- a new interface derivation to the private part of the
17104 -- enclosing package; otherwise this new derivation would be
17105 -- decorated as hidden when the analysis of the enclosing
17106 -- package completes.
17108 if Is_Abstract_Type (Derived_Type)
17109 and then In_Private_Part (Current_Scope)
17110 and then Has_Private_Declaration (Derived_Type)
17111 then
17112 declare
17113 Partial_View : Entity_Id;
17114 Elmt : Elmt_Id;
17115 Ent : Entity_Id;
17117 begin
17118 Partial_View := First_Entity (Current_Scope);
17119 loop
17120 exit when No (Partial_View)
17121 or else (Has_Private_Declaration (Partial_View)
17122 and then
17123 Full_View (Partial_View) = Derived_Type);
17125 Next_Entity (Partial_View);
17126 end loop;
17128 -- If the partial view was not found then the source code
17129 -- has errors and the derivation is not needed.
17131 if Present (Partial_View) then
17132 Elmt :=
17133 First_Elmt (Primitive_Operations (Partial_View));
17134 while Present (Elmt) loop
17135 Ent := Node (Elmt);
17137 if Present (Alias (Ent))
17138 and then Ultimate_Alias (Ent) = Alias (Subp)
17139 then
17140 Append_Elmt
17141 (Ent, Primitive_Operations (Derived_Type));
17142 exit;
17143 end if;
17145 Next_Elmt (Elmt);
17146 end loop;
17148 -- If the interface primitive was not found in the
17149 -- partial view then this interface primitive was
17150 -- overridden. We add a derivation to activate in
17151 -- Derive_Progenitor_Subprograms the machinery to
17152 -- search for it.
17154 if No (Elmt) then
17155 Derive_Interface_Subprogram
17156 (New_Subp => New_Subp,
17157 Subp => Subp,
17158 Actual_Subp => Act_Subp);
17159 end if;
17160 end if;
17161 end;
17162 else
17163 Derive_Interface_Subprogram
17164 (New_Subp => New_Subp,
17165 Subp => Subp,
17166 Actual_Subp => Act_Subp);
17167 end if;
17169 -- Case 3: Common derivation
17171 else
17172 Derive_Subprogram
17173 (New_Subp => New_Subp,
17174 Parent_Subp => Subp,
17175 Derived_Type => Derived_Type,
17176 Parent_Type => Parent_Base,
17177 Actual_Subp => Act_Subp);
17178 end if;
17180 -- No need to update Act_Elm if we must search for the
17181 -- corresponding operation in the generic actual
17183 if not Need_Search
17184 and then Present (Act_Elmt)
17185 then
17186 Next_Elmt (Act_Elmt);
17187 Act_Subp := Node (Act_Elmt);
17188 end if;
17190 <<Continue>>
17191 Next_Elmt (Elmt);
17192 end loop;
17194 -- Inherit additional operations from progenitors. If the derived
17195 -- type is a generic actual, there are not new primitive operations
17196 -- for the type because it has those of the actual, and therefore
17197 -- nothing needs to be done. The renamings generated above are not
17198 -- primitive operations, and their purpose is simply to make the
17199 -- proper operations visible within an instantiation.
17201 if No (Generic_Actual) then
17202 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
17203 end if;
17204 end if;
17206 -- Final check: Direct descendants must have their primitives in the
17207 -- same order. We exclude from this test untagged types and instances
17208 -- of formal derived types. We skip this test if we have already
17209 -- reported serious errors in the sources.
17211 pragma Assert (not Is_Tagged_Type (Derived_Type)
17212 or else Present (Generic_Actual)
17213 or else Serious_Errors_Detected > 0
17214 or else Check_Derived_Type);
17215 end Derive_Subprograms;
17217 --------------------------------
17218 -- Derived_Standard_Character --
17219 --------------------------------
17221 procedure Derived_Standard_Character
17222 (N : Node_Id;
17223 Parent_Type : Entity_Id;
17224 Derived_Type : Entity_Id)
17226 Loc : constant Source_Ptr := Sloc (N);
17227 Def : constant Node_Id := Type_Definition (N);
17228 Indic : constant Node_Id := Subtype_Indication (Def);
17229 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
17230 Implicit_Base : constant Entity_Id :=
17231 Create_Itype
17232 (E_Enumeration_Type, N, Derived_Type, 'B');
17234 Lo : Node_Id;
17235 Hi : Node_Id;
17237 begin
17238 Discard_Node (Process_Subtype (Indic, N));
17240 Set_Etype (Implicit_Base, Parent_Base);
17241 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
17242 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
17244 Set_Is_Character_Type (Implicit_Base, True);
17245 Set_Has_Delayed_Freeze (Implicit_Base);
17247 -- The bounds of the implicit base are the bounds of the parent base.
17248 -- Note that their type is the parent base.
17250 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
17251 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
17253 Set_Scalar_Range (Implicit_Base,
17254 Make_Range (Loc,
17255 Low_Bound => Lo,
17256 High_Bound => Hi));
17258 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
17259 Set_Etype (Derived_Type, Implicit_Base);
17260 Set_Size_Info (Derived_Type, Parent_Type);
17262 if not Known_RM_Size (Derived_Type) then
17263 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
17264 end if;
17266 Set_Is_Character_Type (Derived_Type, True);
17268 if Nkind (Indic) /= N_Subtype_Indication then
17270 -- If no explicit constraint, the bounds are those
17271 -- of the parent type.
17273 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
17274 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
17275 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
17276 end if;
17278 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
17279 end Derived_Standard_Character;
17281 ------------------------------
17282 -- Derived_Type_Declaration --
17283 ------------------------------
17285 procedure Derived_Type_Declaration
17286 (T : Entity_Id;
17287 N : Node_Id;
17288 Is_Completion : Boolean)
17290 Parent_Type : Entity_Id;
17292 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
17293 -- Check whether the parent type is a generic formal, or derives
17294 -- directly or indirectly from one.
17296 ------------------------
17297 -- Comes_From_Generic --
17298 ------------------------
17300 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
17301 begin
17302 if Is_Generic_Type (Typ) then
17303 return True;
17305 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
17306 return True;
17308 elsif Is_Private_Type (Typ)
17309 and then Present (Full_View (Typ))
17310 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
17311 then
17312 return True;
17314 elsif Is_Generic_Actual_Type (Typ) then
17315 return True;
17317 else
17318 return False;
17319 end if;
17320 end Comes_From_Generic;
17322 -- Local variables
17324 Def : constant Node_Id := Type_Definition (N);
17325 Iface_Def : Node_Id;
17326 Indic : constant Node_Id := Subtype_Indication (Def);
17327 Extension : constant Node_Id := Record_Extension_Part (Def);
17328 Parent_Node : Node_Id;
17329 Taggd : Boolean;
17331 -- Start of processing for Derived_Type_Declaration
17333 begin
17334 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17336 if SPARK_Mode = On
17337 and then Is_Tagged_Type (Parent_Type)
17338 then
17339 declare
17340 Partial_View : constant Entity_Id :=
17341 Incomplete_Or_Partial_View (Parent_Type);
17343 begin
17344 -- If the partial view was not found then the parent type is not
17345 -- a private type. Otherwise check if the partial view is a tagged
17346 -- private type.
17348 if Present (Partial_View)
17349 and then Is_Private_Type (Partial_View)
17350 and then not Is_Tagged_Type (Partial_View)
17351 then
17352 Error_Msg_NE
17353 ("cannot derive from & declared as untagged private "
17354 & "(SPARK RM 3.4(1))", N, Partial_View);
17355 end if;
17356 end;
17357 end if;
17359 -- Ada 2005 (AI-251): In case of interface derivation check that the
17360 -- parent is also an interface.
17362 if Interface_Present (Def) then
17363 if not Is_Interface (Parent_Type) then
17364 Diagnose_Interface (Indic, Parent_Type);
17366 else
17367 Parent_Node := Parent (Base_Type (Parent_Type));
17368 Iface_Def := Type_Definition (Parent_Node);
17370 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17371 -- other limited interfaces.
17373 if Limited_Present (Def) then
17374 if Limited_Present (Iface_Def) then
17375 null;
17377 elsif Protected_Present (Iface_Def) then
17378 Error_Msg_NE
17379 ("descendant of & must be declared as a protected "
17380 & "interface", N, Parent_Type);
17382 elsif Synchronized_Present (Iface_Def) then
17383 Error_Msg_NE
17384 ("descendant of & must be declared as a synchronized "
17385 & "interface", N, Parent_Type);
17387 elsif Task_Present (Iface_Def) then
17388 Error_Msg_NE
17389 ("descendant of & must be declared as a task interface",
17390 N, Parent_Type);
17392 else
17393 Error_Msg_N
17394 ("(Ada 2005) limited interface cannot inherit from "
17395 & "non-limited interface", Indic);
17396 end if;
17398 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17399 -- from non-limited or limited interfaces.
17401 elsif not Protected_Present (Def)
17402 and then not Synchronized_Present (Def)
17403 and then not Task_Present (Def)
17404 then
17405 if Limited_Present (Iface_Def) then
17406 null;
17408 elsif Protected_Present (Iface_Def) then
17409 Error_Msg_NE
17410 ("descendant of & must be declared as a protected "
17411 & "interface", N, Parent_Type);
17413 elsif Synchronized_Present (Iface_Def) then
17414 Error_Msg_NE
17415 ("descendant of & must be declared as a synchronized "
17416 & "interface", N, Parent_Type);
17418 elsif Task_Present (Iface_Def) then
17419 Error_Msg_NE
17420 ("descendant of & must be declared as a task interface",
17421 N, Parent_Type);
17422 else
17423 null;
17424 end if;
17425 end if;
17426 end if;
17427 end if;
17429 if Is_Tagged_Type (Parent_Type)
17430 and then Is_Concurrent_Type (Parent_Type)
17431 and then not Is_Interface (Parent_Type)
17432 then
17433 Error_Msg_N
17434 ("parent type of a record extension cannot be a synchronized "
17435 & "tagged type (RM 3.9.1 (3/1))", N);
17436 Set_Etype (T, Any_Type);
17437 return;
17438 end if;
17440 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17441 -- interfaces
17443 if Is_Tagged_Type (Parent_Type)
17444 and then Is_Non_Empty_List (Interface_List (Def))
17445 then
17446 declare
17447 Intf : Node_Id;
17448 T : Entity_Id;
17450 begin
17451 Intf := First (Interface_List (Def));
17452 while Present (Intf) loop
17453 T := Find_Type_Of_Subtype_Indic (Intf);
17455 if not Is_Interface (T) then
17456 Diagnose_Interface (Intf, T);
17458 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17459 -- a limited type from having a nonlimited progenitor.
17461 elsif (Limited_Present (Def)
17462 or else (not Is_Interface (Parent_Type)
17463 and then Is_Limited_Type (Parent_Type)))
17464 and then not Is_Limited_Interface (T)
17465 then
17466 Error_Msg_NE
17467 ("progenitor interface& of limited type must be limited",
17468 N, T);
17469 end if;
17471 Next (Intf);
17472 end loop;
17473 end;
17475 -- Check consistency of any nonoverridable aspects that are
17476 -- inherited from multiple sources.
17478 Check_Inherited_Nonoverridable_Aspects
17479 (Inheritor => T,
17480 Interface_List => Interface_List (Def),
17481 Parent_Type => Parent_Type);
17482 end if;
17484 if Parent_Type = Any_Type
17485 or else Etype (Parent_Type) = Any_Type
17486 or else (Is_Class_Wide_Type (Parent_Type)
17487 and then Etype (Parent_Type) = T)
17488 then
17489 -- If Parent_Type is undefined or illegal, make new type into a
17490 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17491 -- errors. If this is a self-definition, emit error now.
17493 if T = Parent_Type or else T = Etype (Parent_Type) then
17494 Error_Msg_N ("type cannot be used in its own definition", Indic);
17495 end if;
17497 Mutate_Ekind (T, Ekind (Parent_Type));
17498 Set_Etype (T, Any_Type);
17499 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17501 -- Initialize the list of primitive operations to an empty list,
17502 -- to cover tagged types as well as untagged types. For untagged
17503 -- types this is used either to analyze the call as legal when
17504 -- Extensions_Allowed is True, or to issue a better error message
17505 -- otherwise.
17507 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17509 return;
17510 end if;
17512 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17513 -- an interface is special because the list of interfaces in the full
17514 -- view can be given in any order. For example:
17516 -- type A is interface;
17517 -- type B is interface and A;
17518 -- type D is new B with private;
17519 -- private
17520 -- type D is new A and B with null record; -- 1 --
17522 -- In this case we perform the following transformation of -1-:
17524 -- type D is new B and A with null record;
17526 -- If the parent of the full-view covers the parent of the partial-view
17527 -- we have two possible cases:
17529 -- 1) They have the same parent
17530 -- 2) The parent of the full-view implements some further interfaces
17532 -- In both cases we do not need to perform the transformation. In the
17533 -- first case the source program is correct and the transformation is
17534 -- not needed; in the second case the source program does not fulfill
17535 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17536 -- later.
17538 -- This transformation not only simplifies the rest of the analysis of
17539 -- this type declaration but also simplifies the correct generation of
17540 -- the object layout to the expander.
17542 if In_Private_Part (Current_Scope)
17543 and then Is_Interface (Parent_Type)
17544 then
17545 declare
17546 Partial_View : Entity_Id;
17547 Partial_View_Parent : Entity_Id;
17549 function Reorder_Interfaces return Boolean;
17550 -- Look for an interface in the full view's interface list that
17551 -- matches the parent type of the partial view, and when found,
17552 -- rewrite the full view's parent with the partial view's parent,
17553 -- append the full view's original parent to the interface list,
17554 -- recursively call Derived_Type_Definition on the full type, and
17555 -- return True. If a match is not found, return False.
17557 ------------------------
17558 -- Reorder_Interfaces --
17559 ------------------------
17561 function Reorder_Interfaces return Boolean is
17562 Iface : Node_Id;
17563 New_Iface : Node_Id;
17565 begin
17566 Iface := First (Interface_List (Def));
17567 while Present (Iface) loop
17568 if Etype (Iface) = Etype (Partial_View) then
17569 Rewrite (Subtype_Indication (Def),
17570 New_Copy (Subtype_Indication (Parent (Partial_View))));
17572 New_Iface :=
17573 Make_Identifier (Sloc (N), Chars (Parent_Type));
17574 Rewrite (Iface, New_Iface);
17576 -- Analyze the transformed code
17578 Derived_Type_Declaration (T, N, Is_Completion);
17579 return True;
17580 end if;
17582 Next (Iface);
17583 end loop;
17584 return False;
17585 end Reorder_Interfaces;
17587 begin
17588 -- Look for the associated private type declaration
17590 Partial_View := Incomplete_Or_Partial_View (T);
17592 -- If the partial view was not found then the source code has
17593 -- errors and the transformation is not needed.
17595 if Present (Partial_View) then
17596 Partial_View_Parent := Etype (Partial_View);
17598 -- If the parent of the full-view covers the parent of the
17599 -- partial-view we have nothing else to do.
17601 if Interface_Present_In_Ancestor
17602 (Parent_Type, Partial_View_Parent)
17603 then
17604 null;
17606 -- Traverse the list of interfaces of the full view to look
17607 -- for the parent of the partial view and reorder the
17608 -- interfaces to match the order in the partial view,
17609 -- if needed.
17611 else
17613 if Reorder_Interfaces then
17614 -- Having the interfaces listed in any order is legal.
17615 -- However, the compiler does not properly handle
17616 -- different orders between partial and full views in
17617 -- generic units. We give a warning about the order
17618 -- mismatch, so the user can work around this problem.
17620 Error_Msg_N ("??full declaration does not respect " &
17621 "partial declaration order", T);
17622 Error_Msg_N ("\??consider reordering", T);
17624 return;
17625 end if;
17626 end if;
17627 end if;
17628 end;
17629 end if;
17631 -- Only composite types other than array types are allowed to have
17632 -- discriminants.
17634 if Present (Discriminant_Specifications (N)) then
17635 if (Is_Elementary_Type (Parent_Type)
17636 or else
17637 Is_Array_Type (Parent_Type))
17638 and then not Error_Posted (N)
17639 then
17640 Error_Msg_N
17641 ("elementary or array type cannot have discriminants",
17642 Defining_Identifier (First (Discriminant_Specifications (N))));
17644 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17645 -- only if we are not already processing a malformed syntax tree.
17647 if Is_Type (T) then
17648 Set_Has_Discriminants (T, False);
17649 end if;
17650 end if;
17651 end if;
17653 -- In Ada 83, a derived type defined in a package specification cannot
17654 -- be used for further derivation until the end of its visible part.
17655 -- Note that derivation in the private part of the package is allowed.
17657 if Ada_Version = Ada_83
17658 and then Is_Derived_Type (Parent_Type)
17659 and then In_Visible_Part (Scope (Parent_Type))
17660 then
17661 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17662 Error_Msg_N
17663 ("(Ada 83) premature use of type for derivation", Indic);
17664 end if;
17665 end if;
17667 -- Check for early use of incomplete or private type
17669 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17670 Error_Msg_N ("premature derivation of incomplete type", Indic);
17671 return;
17673 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17674 and then not Comes_From_Generic (Parent_Type))
17675 or else Has_Private_Component (Parent_Type)
17676 then
17677 -- The ancestor type of a formal type can be incomplete, in which
17678 -- case only the operations of the partial view are available in the
17679 -- generic. Subsequent checks may be required when the full view is
17680 -- analyzed to verify that a derivation from a tagged type has an
17681 -- extension.
17683 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17684 null;
17686 elsif No (Underlying_Type (Parent_Type))
17687 or else Has_Private_Component (Parent_Type)
17688 then
17689 Error_Msg_N
17690 ("premature derivation of derived or private type", Indic);
17692 -- Flag the type itself as being in error, this prevents some
17693 -- nasty problems with subsequent uses of the malformed type.
17695 Set_Error_Posted (T);
17697 -- Check that within the immediate scope of an untagged partial
17698 -- view it's illegal to derive from the partial view if the
17699 -- full view is tagged. (7.3(7))
17701 -- We verify that the Parent_Type is a partial view by checking
17702 -- that it is not a Full_Type_Declaration (i.e. a private type or
17703 -- private extension declaration), to distinguish a partial view
17704 -- from a derivation from a private type which also appears as
17705 -- E_Private_Type. If the parent base type is not declared in an
17706 -- enclosing scope there is no need to check.
17708 elsif Present (Full_View (Parent_Type))
17709 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17710 and then not Is_Tagged_Type (Parent_Type)
17711 and then Is_Tagged_Type (Full_View (Parent_Type))
17712 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17713 then
17714 Error_Msg_N
17715 ("premature derivation from type with tagged full view",
17716 Indic);
17717 end if;
17718 end if;
17720 -- Check that form of derivation is appropriate
17722 Taggd := Is_Tagged_Type (Parent_Type);
17724 -- Set the parent type to the class-wide type's specific type in this
17725 -- case to prevent cascading errors
17727 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17728 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17729 Set_Etype (T, Etype (Parent_Type));
17730 return;
17731 end if;
17733 if Present (Extension) and then not Taggd then
17734 Error_Msg_N
17735 ("type derived from untagged type cannot have extension", Indic);
17737 elsif No (Extension) and then Taggd then
17739 -- If this declaration is within a private part (or body) of a
17740 -- generic instantiation then the derivation is allowed (the parent
17741 -- type can only appear tagged in this case if it's a generic actual
17742 -- type, since it would otherwise have been rejected in the analysis
17743 -- of the generic template).
17745 if not Is_Generic_Actual_Type (Parent_Type)
17746 or else In_Visible_Part (Scope (Parent_Type))
17747 then
17748 if Is_Class_Wide_Type (Parent_Type) then
17749 Error_Msg_N
17750 ("parent type must not be a class-wide type", Indic);
17752 -- Use specific type to prevent cascaded errors.
17754 Parent_Type := Etype (Parent_Type);
17756 else
17757 Error_Msg_N
17758 ("type derived from tagged type must have extension", Indic);
17759 end if;
17760 end if;
17761 end if;
17763 -- AI-443: Synchronized formal derived types require a private
17764 -- extension. There is no point in checking the ancestor type or
17765 -- the progenitors since the construct is wrong to begin with.
17767 if Ada_Version >= Ada_2005
17768 and then Is_Generic_Type (T)
17769 and then Present (Original_Node (N))
17770 then
17771 declare
17772 Decl : constant Node_Id := Original_Node (N);
17774 begin
17775 if Nkind (Decl) = N_Formal_Type_Declaration
17776 and then Nkind (Formal_Type_Definition (Decl)) =
17777 N_Formal_Derived_Type_Definition
17778 and then Synchronized_Present (Formal_Type_Definition (Decl))
17779 and then No (Extension)
17781 -- Avoid emitting a duplicate error message
17783 and then not Error_Posted (Indic)
17784 then
17785 Error_Msg_N
17786 ("synchronized derived type must have extension", N);
17787 end if;
17788 end;
17789 end if;
17791 if Null_Exclusion_Present (Def)
17792 and then not Is_Access_Type (Parent_Type)
17793 then
17794 Error_Msg_N ("null exclusion can only apply to an access type", N);
17795 end if;
17797 Check_Wide_Character_Restriction (Parent_Type, Indic);
17799 -- Avoid deriving parent primitives of underlying record views
17801 Set_Is_Not_Self_Hidden (T);
17803 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17804 Derive_Subps => not Is_Underlying_Record_View (T));
17806 -- AI-419: The parent type of an explicitly limited derived type must
17807 -- be a limited type or a limited interface.
17809 if Limited_Present (Def) then
17810 Set_Is_Limited_Record (T);
17812 if Is_Interface (T) then
17813 Set_Is_Limited_Interface (T);
17814 end if;
17816 if not Is_Limited_Type (Parent_Type)
17817 and then
17818 (not Is_Interface (Parent_Type)
17819 or else not Is_Limited_Interface (Parent_Type))
17820 then
17821 -- AI05-0096: a derivation in the private part of an instance is
17822 -- legal if the generic formal is untagged limited, and the actual
17823 -- is non-limited.
17825 if Is_Generic_Actual_Type (Parent_Type)
17826 and then In_Private_Part (Current_Scope)
17827 and then
17828 not Is_Tagged_Type
17829 (Generic_Parent_Type (Parent (Parent_Type)))
17830 then
17831 null;
17833 else
17834 Error_Msg_NE
17835 ("parent type& of limited type must be limited",
17836 N, Parent_Type);
17837 end if;
17838 end if;
17839 end if;
17840 end Derived_Type_Declaration;
17842 ------------------------
17843 -- Diagnose_Interface --
17844 ------------------------
17846 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17847 begin
17848 if not Is_Interface (E) and then E /= Any_Type then
17849 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17850 end if;
17851 end Diagnose_Interface;
17853 ----------------------------------
17854 -- Enumeration_Type_Declaration --
17855 ----------------------------------
17857 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17858 Ev : Uint;
17859 L : Node_Id;
17860 R_Node : Node_Id;
17861 B_Node : Node_Id;
17863 begin
17864 -- Create identifier node representing lower bound
17866 B_Node := New_Node (N_Identifier, Sloc (Def));
17867 L := First (Literals (Def));
17868 Set_Chars (B_Node, Chars (L));
17869 Set_Entity (B_Node, L);
17870 Set_Etype (B_Node, T);
17871 Set_Is_Static_Expression (B_Node, True);
17873 R_Node := New_Node (N_Range, Sloc (Def));
17874 Set_Low_Bound (R_Node, B_Node);
17876 Mutate_Ekind (T, E_Enumeration_Type);
17877 Set_First_Literal (T, L);
17878 Set_Etype (T, T);
17879 Set_Is_Constrained (T);
17881 Ev := Uint_0;
17883 -- Loop through literals of enumeration type setting pos and rep values
17884 -- except that if the Ekind is already set, then it means the literal
17885 -- was already constructed (case of a derived type declaration and we
17886 -- should not disturb the Pos and Rep values.
17888 while Present (L) loop
17889 if Ekind (L) /= E_Enumeration_Literal then
17890 Mutate_Ekind (L, E_Enumeration_Literal);
17891 Set_Is_Not_Self_Hidden (L);
17892 Set_Enumeration_Pos (L, Ev);
17893 Set_Enumeration_Rep (L, Ev);
17894 Set_Is_Known_Valid (L, True);
17895 end if;
17897 Set_Etype (L, T);
17898 New_Overloaded_Entity (L);
17899 Generate_Definition (L);
17900 Set_Convention (L, Convention_Intrinsic);
17902 -- Case of character literal
17904 if Nkind (L) = N_Defining_Character_Literal then
17905 Set_Is_Character_Type (T, True);
17907 -- Check violation of No_Wide_Characters
17909 if Restriction_Check_Required (No_Wide_Characters) then
17910 Get_Name_String (Chars (L));
17912 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17913 Check_Restriction (No_Wide_Characters, L);
17914 end if;
17915 end if;
17916 end if;
17918 Ev := Ev + 1;
17919 Next (L);
17920 end loop;
17922 -- Now create a node representing upper bound
17924 B_Node := New_Node (N_Identifier, Sloc (Def));
17925 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17926 Set_Entity (B_Node, Last (Literals (Def)));
17927 Set_Etype (B_Node, T);
17928 Set_Is_Static_Expression (B_Node, True);
17930 Set_High_Bound (R_Node, B_Node);
17932 -- Initialize various fields of the type. Some of this information
17933 -- may be overwritten later through rep. clauses.
17935 Set_Scalar_Range (T, R_Node);
17936 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17937 Set_Enum_Esize (T);
17938 Set_Enum_Pos_To_Rep (T, Empty);
17940 -- Set Discard_Names if configuration pragma set, or if there is
17941 -- a parameterless pragma in the current declarative region
17943 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17944 Set_Discard_Names (T);
17945 end if;
17947 -- Process end label if there is one
17949 if Present (Def) then
17950 Process_End_Label (Def, 'e', T);
17951 end if;
17952 end Enumeration_Type_Declaration;
17954 ---------------------------------
17955 -- Expand_To_Stored_Constraint --
17956 ---------------------------------
17958 function Expand_To_Stored_Constraint
17959 (Typ : Entity_Id;
17960 Constraint : Elist_Id) return Elist_Id
17962 Explicitly_Discriminated_Type : Entity_Id;
17963 Expansion : Elist_Id;
17964 Discriminant : Entity_Id;
17966 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17967 -- Find the nearest type that actually specifies discriminants
17969 ---------------------------------
17970 -- Type_With_Explicit_Discrims --
17971 ---------------------------------
17973 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17974 Typ : constant E := Base_Type (Id);
17976 begin
17977 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17978 if Present (Full_View (Typ)) then
17979 return Type_With_Explicit_Discrims (Full_View (Typ));
17980 end if;
17982 else
17983 if Has_Discriminants (Typ) then
17984 return Typ;
17985 end if;
17986 end if;
17988 if Etype (Typ) = Typ then
17989 return Empty;
17990 elsif Has_Discriminants (Typ) then
17991 return Typ;
17992 else
17993 return Type_With_Explicit_Discrims (Etype (Typ));
17994 end if;
17996 end Type_With_Explicit_Discrims;
17998 -- Start of processing for Expand_To_Stored_Constraint
18000 begin
18001 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
18002 return No_Elist;
18003 end if;
18005 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
18007 if No (Explicitly_Discriminated_Type) then
18008 return No_Elist;
18009 end if;
18011 Expansion := New_Elmt_List;
18013 Discriminant :=
18014 First_Stored_Discriminant (Explicitly_Discriminated_Type);
18015 while Present (Discriminant) loop
18016 Append_Elmt
18017 (Get_Discriminant_Value
18018 (Discriminant, Explicitly_Discriminated_Type, Constraint),
18019 To => Expansion);
18020 Next_Stored_Discriminant (Discriminant);
18021 end loop;
18023 return Expansion;
18024 end Expand_To_Stored_Constraint;
18026 ---------------------------
18027 -- Find_Hidden_Interface --
18028 ---------------------------
18030 function Find_Hidden_Interface
18031 (Src : Elist_Id;
18032 Dest : Elist_Id) return Entity_Id
18034 Iface : Entity_Id;
18035 Iface_Elmt : Elmt_Id;
18037 begin
18038 if Present (Src) and then Present (Dest) then
18039 Iface_Elmt := First_Elmt (Src);
18040 while Present (Iface_Elmt) loop
18041 Iface := Node (Iface_Elmt);
18043 if Is_Interface (Iface)
18044 and then not Contain_Interface (Iface, Dest)
18045 then
18046 return Iface;
18047 end if;
18049 Next_Elmt (Iface_Elmt);
18050 end loop;
18051 end if;
18053 return Empty;
18054 end Find_Hidden_Interface;
18056 --------------------
18057 -- Find_Type_Name --
18058 --------------------
18060 function Find_Type_Name (N : Node_Id) return Entity_Id is
18061 Id : constant Entity_Id := Defining_Identifier (N);
18062 New_Id : Entity_Id;
18063 Prev : Entity_Id;
18064 Prev_Par : Node_Id;
18066 procedure Check_Duplicate_Aspects;
18067 -- Check that aspects specified in a completion have not been specified
18068 -- already in the partial view.
18070 procedure Tag_Mismatch;
18071 -- Diagnose a tagged partial view whose full view is untagged. We post
18072 -- the message on the full view, with a reference to the previous
18073 -- partial view. The partial view can be private or incomplete, and
18074 -- these are handled in a different manner, so we determine the position
18075 -- of the error message from the respective slocs of both.
18077 -----------------------------
18078 -- Check_Duplicate_Aspects --
18079 -----------------------------
18081 procedure Check_Duplicate_Aspects is
18082 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
18083 -- Return the corresponding aspect of the partial view which matches
18084 -- the aspect id of Asp. Return Empty is no such aspect exists.
18086 -----------------------------
18087 -- Get_Partial_View_Aspect --
18088 -----------------------------
18090 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
18091 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
18092 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
18093 Prev_Asp : Node_Id;
18095 begin
18096 if Present (Prev_Asps) then
18097 Prev_Asp := First (Prev_Asps);
18098 while Present (Prev_Asp) loop
18099 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
18100 return Prev_Asp;
18101 end if;
18103 Next (Prev_Asp);
18104 end loop;
18105 end if;
18107 return Empty;
18108 end Get_Partial_View_Aspect;
18110 -- Local variables
18112 Full_Asps : constant List_Id := Aspect_Specifications (N);
18113 Full_Asp : Node_Id;
18114 Part_Asp : Node_Id;
18116 -- Start of processing for Check_Duplicate_Aspects
18118 begin
18119 if Present (Full_Asps) then
18120 Full_Asp := First (Full_Asps);
18121 while Present (Full_Asp) loop
18122 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
18124 -- An aspect and its class-wide counterpart are two distinct
18125 -- aspects and may apply to both views of an entity.
18127 if Present (Part_Asp)
18128 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
18129 then
18130 Error_Msg_N
18131 ("aspect already specified in private declaration",
18132 Full_Asp);
18134 Remove (Full_Asp);
18135 return;
18136 end if;
18138 if Has_Discriminants (Prev)
18139 and then not Has_Unknown_Discriminants (Prev)
18140 and then Get_Aspect_Id (Full_Asp) =
18141 Aspect_Implicit_Dereference
18142 then
18143 Error_Msg_N
18144 ("cannot specify aspect if partial view has known "
18145 & "discriminants", Full_Asp);
18146 end if;
18148 Next (Full_Asp);
18149 end loop;
18150 end if;
18151 end Check_Duplicate_Aspects;
18153 ------------------
18154 -- Tag_Mismatch --
18155 ------------------
18157 procedure Tag_Mismatch is
18158 begin
18159 if Sloc (Prev) < Sloc (Id) then
18160 if Ada_Version >= Ada_2012
18161 and then Nkind (N) = N_Private_Type_Declaration
18162 then
18163 Error_Msg_NE
18164 ("declaration of private } must be a tagged type", Id, Prev);
18165 else
18166 Error_Msg_NE
18167 ("full declaration of } must be a tagged type", Id, Prev);
18168 end if;
18170 else
18171 if Ada_Version >= Ada_2012
18172 and then Nkind (N) = N_Private_Type_Declaration
18173 then
18174 Error_Msg_NE
18175 ("declaration of private } must be a tagged type", Prev, Id);
18176 else
18177 Error_Msg_NE
18178 ("full declaration of } must be a tagged type", Prev, Id);
18179 end if;
18180 end if;
18181 end Tag_Mismatch;
18183 -- Start of processing for Find_Type_Name
18185 begin
18186 -- Find incomplete declaration, if one was given
18188 Prev := Current_Entity_In_Scope (Id);
18190 -- New type declaration
18192 if No (Prev) then
18193 Enter_Name (Id);
18194 return Id;
18196 -- Previous declaration exists
18198 else
18199 Prev_Par := Parent (Prev);
18201 -- Error if not incomplete/private case except if previous
18202 -- declaration is implicit, etc. Enter_Name will emit error if
18203 -- appropriate.
18205 if not Is_Incomplete_Or_Private_Type (Prev) then
18206 Enter_Name (Id);
18207 New_Id := Id;
18209 -- Check invalid completion of private or incomplete type
18211 elsif Nkind (N) not in N_Full_Type_Declaration
18212 | N_Task_Type_Declaration
18213 | N_Protected_Type_Declaration
18214 and then
18215 (Ada_Version < Ada_2012
18216 or else not Is_Incomplete_Type (Prev)
18217 or else Nkind (N) not in N_Private_Type_Declaration
18218 | N_Private_Extension_Declaration)
18219 then
18220 -- Completion must be a full type declarations (RM 7.3(4))
18222 Error_Msg_Sloc := Sloc (Prev);
18223 Error_Msg_NE ("invalid completion of }", Id, Prev);
18225 -- Set scope of Id to avoid cascaded errors. Entity is never
18226 -- examined again, except when saving globals in generics.
18228 Set_Scope (Id, Current_Scope);
18229 New_Id := Id;
18231 -- If this is a repeated incomplete declaration, no further
18232 -- checks are possible.
18234 if Nkind (N) = N_Incomplete_Type_Declaration then
18235 return Prev;
18236 end if;
18238 -- Case of full declaration of incomplete type
18240 elsif Ekind (Prev) = E_Incomplete_Type
18241 and then (Ada_Version < Ada_2012
18242 or else No (Full_View (Prev))
18243 or else not Is_Private_Type (Full_View (Prev)))
18244 then
18245 -- Indicate that the incomplete declaration has a matching full
18246 -- declaration. The defining occurrence of the incomplete
18247 -- declaration remains the visible one, and the procedure
18248 -- Get_Full_View dereferences it whenever the type is used.
18250 if Present (Full_View (Prev)) then
18251 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18252 end if;
18254 Set_Full_View (Prev, Id);
18255 Append_Entity (Id, Current_Scope);
18256 Set_Is_Public (Id, Is_Public (Prev));
18257 Set_Is_Internal (Id);
18258 New_Id := Prev;
18260 -- If the incomplete view is tagged, a class_wide type has been
18261 -- created already. Use it for the private type as well, in order
18262 -- to prevent multiple incompatible class-wide types that may be
18263 -- created for self-referential anonymous access components.
18265 if Is_Tagged_Type (Prev)
18266 and then Present (Class_Wide_Type (Prev))
18267 then
18268 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
18269 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
18271 -- Type of the class-wide type is the current Id. Previously
18272 -- this was not done for private declarations because of order-
18273 -- of-elaboration issues in the back end, but gigi now handles
18274 -- this properly.
18276 Set_Etype (Class_Wide_Type (Id), Id);
18277 end if;
18279 -- Case of full declaration of private type
18281 else
18282 -- If the private type was a completion of an incomplete type then
18283 -- update Prev to reference the private type
18285 if Ada_Version >= Ada_2012
18286 and then Ekind (Prev) = E_Incomplete_Type
18287 and then Present (Full_View (Prev))
18288 and then Is_Private_Type (Full_View (Prev))
18289 then
18290 Prev := Full_View (Prev);
18291 Prev_Par := Parent (Prev);
18292 end if;
18294 if Nkind (N) = N_Full_Type_Declaration
18295 and then Nkind (Type_Definition (N)) in
18296 N_Record_Definition | N_Derived_Type_Definition
18297 and then Interface_Present (Type_Definition (N))
18298 then
18299 Error_Msg_N
18300 ("completion of private type cannot be an interface", N);
18301 end if;
18303 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
18304 if Etype (Prev) /= Prev then
18306 -- Prev is a private subtype or a derived type, and needs
18307 -- no completion.
18309 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
18310 New_Id := Id;
18312 elsif Ekind (Prev) = E_Private_Type
18313 and then Nkind (N) in N_Task_Type_Declaration
18314 | N_Protected_Type_Declaration
18315 then
18316 Error_Msg_N
18317 ("completion of nonlimited type cannot be limited", N);
18319 elsif Ekind (Prev) = E_Record_Type_With_Private
18320 and then Nkind (N) in N_Task_Type_Declaration
18321 | N_Protected_Type_Declaration
18322 then
18323 if not Is_Limited_Record (Prev) then
18324 Error_Msg_N
18325 ("completion of nonlimited type cannot be limited", N);
18327 elsif No (Interface_List (N)) then
18328 Error_Msg_N
18329 ("completion of tagged private type must be tagged",
18331 end if;
18332 end if;
18334 -- Ada 2005 (AI-251): Private extension declaration of a task
18335 -- type or a protected type. This case arises when covering
18336 -- interface types.
18338 elsif Nkind (N) in N_Task_Type_Declaration
18339 | N_Protected_Type_Declaration
18340 then
18341 null;
18343 elsif Nkind (N) /= N_Full_Type_Declaration
18344 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
18345 then
18346 Error_Msg_N
18347 ("full view of private extension must be an extension", N);
18349 elsif not (Abstract_Present (Parent (Prev)))
18350 and then Abstract_Present (Type_Definition (N))
18351 then
18352 Error_Msg_N
18353 ("full view of non-abstract extension cannot be abstract", N);
18354 end if;
18356 if not In_Private_Part (Current_Scope) then
18357 Error_Msg_N
18358 ("declaration of full view must appear in private part", N);
18359 end if;
18361 if Ada_Version >= Ada_2012 then
18362 Check_Duplicate_Aspects;
18363 end if;
18365 Copy_And_Swap (Prev, Id);
18366 Set_Has_Private_Declaration (Prev);
18367 Set_Has_Private_Declaration (Id);
18369 -- AI12-0133: Indicate whether we have a partial view with
18370 -- unknown discriminants, in which case initialization of objects
18371 -- of the type do not receive an invariant check.
18373 Set_Partial_View_Has_Unknown_Discr
18374 (Prev, Has_Unknown_Discriminants (Id));
18376 -- Preserve aspect and iterator flags that may have been set on
18377 -- the partial view.
18379 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18380 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18382 -- If no error, propagate freeze_node from private to full view.
18383 -- It may have been generated for an early operational item.
18385 if Present (Freeze_Node (Id))
18386 and then Serious_Errors_Detected = 0
18387 and then No (Full_View (Id))
18388 then
18389 Set_Freeze_Node (Prev, Freeze_Node (Id));
18390 Set_Freeze_Node (Id, Empty);
18391 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18392 end if;
18394 Set_Full_View (Id, Prev);
18395 New_Id := Prev;
18396 end if;
18398 -- Verify that full declaration conforms to partial one
18400 if Is_Incomplete_Or_Private_Type (Prev)
18401 and then Present (Discriminant_Specifications (Prev_Par))
18402 then
18403 if Present (Discriminant_Specifications (N)) then
18404 if Ekind (Prev) = E_Incomplete_Type then
18405 Check_Discriminant_Conformance (N, Prev, Prev);
18406 else
18407 Check_Discriminant_Conformance (N, Prev, Id);
18408 end if;
18410 else
18411 Error_Msg_N
18412 ("missing discriminants in full type declaration", N);
18414 -- To avoid cascaded errors on subsequent use, share the
18415 -- discriminants of the partial view.
18417 Set_Discriminant_Specifications (N,
18418 Discriminant_Specifications (Prev_Par));
18419 end if;
18420 end if;
18422 -- A prior untagged partial view can have an associated class-wide
18423 -- type due to use of the class attribute, and in this case the full
18424 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18425 -- of incomplete tagged declarations, but we check for it.
18427 if Is_Type (Prev)
18428 and then (Is_Tagged_Type (Prev)
18429 or else Present (Class_Wide_Type (Prev)))
18430 then
18431 -- Ada 2012 (AI05-0162): A private type may be the completion of
18432 -- an incomplete type.
18434 if Ada_Version >= Ada_2012
18435 and then Is_Incomplete_Type (Prev)
18436 and then Nkind (N) in N_Private_Type_Declaration
18437 | N_Private_Extension_Declaration
18438 then
18439 -- No need to check private extensions since they are tagged
18441 if Nkind (N) = N_Private_Type_Declaration
18442 and then not Tagged_Present (N)
18443 then
18444 Tag_Mismatch;
18445 end if;
18447 -- The full declaration is either a tagged type (including
18448 -- a synchronized type that implements interfaces) or a
18449 -- type extension, otherwise this is an error.
18451 elsif Nkind (N) in N_Task_Type_Declaration
18452 | N_Protected_Type_Declaration
18453 then
18454 if No (Interface_List (N)) and then not Error_Posted (N) then
18455 Tag_Mismatch;
18456 end if;
18458 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18460 -- Indicate that the previous declaration (tagged incomplete
18461 -- or private declaration) requires the same on the full one.
18463 if not Tagged_Present (Type_Definition (N)) then
18464 Tag_Mismatch;
18465 Set_Is_Tagged_Type (Id);
18466 end if;
18468 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18469 if No (Record_Extension_Part (Type_Definition (N))) then
18470 Error_Msg_NE
18471 ("full declaration of } must be a record extension",
18472 Prev, Id);
18474 -- Set some attributes to produce a usable full view
18476 Set_Is_Tagged_Type (Id);
18477 end if;
18479 else
18480 Tag_Mismatch;
18481 end if;
18482 end if;
18484 if Present (Prev)
18485 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18486 and then Present (Premature_Use (Parent (Prev)))
18487 then
18488 Error_Msg_Sloc := Sloc (N);
18489 Error_Msg_N
18490 ("\full declaration #", Premature_Use (Parent (Prev)));
18491 end if;
18493 return New_Id;
18494 end if;
18495 end Find_Type_Name;
18497 -------------------------
18498 -- Find_Type_Of_Object --
18499 -------------------------
18501 function Find_Type_Of_Object
18502 (Obj_Def : Node_Id;
18503 Related_Nod : Node_Id) return Entity_Id
18505 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18506 P : Node_Id := Parent (Obj_Def);
18507 T : Entity_Id;
18508 Nam : Name_Id;
18510 begin
18511 -- If the parent is a component_definition node we climb to the
18512 -- component_declaration node.
18514 if Nkind (P) = N_Component_Definition then
18515 P := Parent (P);
18516 end if;
18518 -- Case of an anonymous array subtype
18520 if Def_Kind in N_Array_Type_Definition then
18521 T := Empty;
18522 Array_Type_Declaration (T, Obj_Def);
18524 -- Create an explicit subtype whenever possible
18526 elsif Nkind (P) /= N_Component_Declaration
18527 and then Def_Kind = N_Subtype_Indication
18528 then
18529 -- Base name of subtype on object name, which will be unique in
18530 -- the current scope.
18532 -- If this is a duplicate declaration, return base type, to avoid
18533 -- generating duplicate anonymous types.
18535 if Error_Posted (P) then
18536 Analyze (Subtype_Mark (Obj_Def));
18537 return Entity (Subtype_Mark (Obj_Def));
18538 end if;
18540 Nam :=
18541 New_External_Name
18542 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18544 T := Make_Defining_Identifier (Sloc (P), Nam);
18546 -- If In_Spec_Expression, for example within a pre/postcondition,
18547 -- provide enough information for use of the subtype without
18548 -- depending on full analysis and freezing, which will happen when
18549 -- building the corresponding subprogram.
18551 if In_Spec_Expression then
18552 Analyze (Subtype_Mark (Obj_Def));
18554 declare
18555 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18556 New_Def : constant Node_Id := New_Copy_Tree (Obj_Def);
18557 Decl : constant Node_Id :=
18558 Make_Subtype_Declaration (Sloc (P),
18559 Defining_Identifier => T,
18560 Subtype_Indication => New_Def);
18562 begin
18563 Set_Etype (T, Base_T);
18564 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18565 Set_Parent (T, Decl);
18566 Set_Scope (T, Current_Scope);
18568 if Ekind (T) = E_Array_Subtype then
18569 Constrain_Array (T, New_Def, Related_Nod, T, 'P');
18571 elsif Ekind (T) = E_Record_Subtype then
18572 Set_First_Entity (T, First_Entity (Base_T));
18573 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18574 Set_Is_Constrained (T);
18575 end if;
18577 Insert_Before (Related_Nod, Decl);
18578 end;
18580 return T;
18581 end if;
18583 -- When generating code, insert subtype declaration ahead of
18584 -- declaration that generated it.
18586 Insert_Action (Obj_Def,
18587 Make_Subtype_Declaration (Sloc (P),
18588 Defining_Identifier => T,
18589 Subtype_Indication => Relocate_Node (Obj_Def)));
18591 -- This subtype may need freezing, and this will not be done
18592 -- automatically if the object declaration is not in declarative
18593 -- part. Since this is an object declaration, the type cannot always
18594 -- be frozen here. Deferred constants do not freeze their type
18595 -- (which often enough will be private).
18597 if Nkind (P) = N_Object_Declaration
18598 and then Constant_Present (P)
18599 and then No (Expression (P))
18600 then
18601 null;
18603 -- Here we freeze the base type of object type to catch premature use
18604 -- of discriminated private type without a full view.
18606 else
18607 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18608 end if;
18610 -- Ada 2005 AI-406: the object definition in an object declaration
18611 -- can be an access definition.
18613 elsif Def_Kind = N_Access_Definition then
18614 T := Access_Definition (Related_Nod, Obj_Def);
18616 Set_Is_Local_Anonymous_Access
18617 (T, Ada_Version < Ada_2012
18618 or else Nkind (P) /= N_Object_Declaration
18619 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18621 -- Otherwise, the object definition is just a subtype_mark
18623 else
18624 T := Process_Subtype (Obj_Def, Related_Nod);
18625 end if;
18627 return T;
18628 end Find_Type_Of_Object;
18630 --------------------------------
18631 -- Find_Type_Of_Subtype_Indic --
18632 --------------------------------
18634 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18635 Typ : Entity_Id;
18637 begin
18638 -- Case of subtype mark with a constraint
18640 if Nkind (S) = N_Subtype_Indication then
18641 Find_Type (Subtype_Mark (S));
18642 Typ := Entity (Subtype_Mark (S));
18644 if not
18645 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18646 then
18647 Error_Msg_N
18648 ("incorrect constraint for this kind of type", Constraint (S));
18649 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18650 end if;
18652 -- Otherwise we have a subtype mark without a constraint
18654 elsif Error_Posted (S) then
18655 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18656 return Any_Type;
18658 else
18659 Find_Type (S);
18660 Typ := Entity (S);
18661 end if;
18663 return Typ;
18664 end Find_Type_Of_Subtype_Indic;
18666 -------------------------------------
18667 -- Floating_Point_Type_Declaration --
18668 -------------------------------------
18670 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18671 Digs : constant Node_Id := Digits_Expression (Def);
18672 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18673 Digs_Val : Uint;
18674 Base_Typ : Entity_Id;
18675 Implicit_Base : Entity_Id;
18677 function Can_Derive_From (E : Entity_Id) return Boolean;
18678 -- Find if given digits value, and possibly a specified range, allows
18679 -- derivation from specified type
18681 procedure Convert_Bound (B : Node_Id);
18682 -- If specified, the bounds must be static but may be of different
18683 -- types. They must be converted into machine numbers of the base type,
18684 -- in accordance with RM 4.9(38).
18686 function Find_Base_Type return Entity_Id;
18687 -- Find a predefined base type that Def can derive from, or generate
18688 -- an error and substitute Long_Long_Float if none exists.
18690 ---------------------
18691 -- Can_Derive_From --
18692 ---------------------
18694 function Can_Derive_From (E : Entity_Id) return Boolean is
18695 Spec : constant Entity_Id := Real_Range_Specification (Def);
18697 begin
18698 -- Check specified "digits" constraint
18700 if Digs_Val > Digits_Value (E) then
18701 return False;
18702 end if;
18704 -- Check for matching range, if specified
18706 if Present (Spec) then
18707 if Expr_Value_R (Type_Low_Bound (E)) >
18708 Expr_Value_R (Low_Bound (Spec))
18709 then
18710 return False;
18711 end if;
18713 if Expr_Value_R (Type_High_Bound (E)) <
18714 Expr_Value_R (High_Bound (Spec))
18715 then
18716 return False;
18717 end if;
18718 end if;
18720 return True;
18721 end Can_Derive_From;
18723 -------------------
18724 -- Convert_Bound --
18725 --------------------
18727 procedure Convert_Bound (B : Node_Id) is
18728 begin
18729 -- If the bound is not a literal it can only be static if it is
18730 -- a static constant, possibly of a specified type.
18732 if Is_Entity_Name (B)
18733 and then Ekind (Entity (B)) = E_Constant
18734 then
18735 Rewrite (B, Constant_Value (Entity (B)));
18736 end if;
18738 if Nkind (B) = N_Real_Literal then
18739 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18740 Set_Is_Machine_Number (B);
18741 Set_Etype (B, Base_Typ);
18742 end if;
18743 end Convert_Bound;
18745 --------------------
18746 -- Find_Base_Type --
18747 --------------------
18749 function Find_Base_Type return Entity_Id is
18750 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18752 begin
18753 -- Iterate over the predefined types in order, returning the first
18754 -- one that Def can derive from.
18756 while Present (Choice) loop
18757 if Can_Derive_From (Node (Choice)) then
18758 return Node (Choice);
18759 end if;
18761 Next_Elmt (Choice);
18762 end loop;
18764 -- If we can't derive from any existing type, use Long_Long_Float
18765 -- and give appropriate message explaining the problem.
18767 if Digs_Val > Max_Digs_Val then
18768 -- It might be the case that there is a type with the requested
18769 -- range, just not the combination of digits and range.
18771 Error_Msg_N
18772 ("no predefined type has requested range and precision",
18773 Real_Range_Specification (Def));
18775 else
18776 Error_Msg_N
18777 ("range too large for any predefined type",
18778 Real_Range_Specification (Def));
18779 end if;
18781 return Standard_Long_Long_Float;
18782 end Find_Base_Type;
18784 -- Start of processing for Floating_Point_Type_Declaration
18786 begin
18787 Check_Restriction (No_Floating_Point, Def);
18789 -- Create an implicit base type
18791 Implicit_Base :=
18792 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18794 -- Analyze and verify digits value
18796 Analyze_And_Resolve (Digs, Any_Integer);
18797 Check_Digits_Expression (Digs);
18798 Digs_Val := Expr_Value (Digs);
18800 -- Process possible range spec and find correct type to derive from
18802 Process_Real_Range_Specification (Def);
18804 -- Check that requested number of digits is not too high.
18806 if Digs_Val > Max_Digs_Val then
18808 -- The check for Max_Base_Digits may be somewhat expensive, as it
18809 -- requires reading System, so only do it when necessary.
18811 declare
18812 Max_Base_Digits : constant Uint :=
18813 Expr_Value
18814 (Expression
18815 (Parent (RTE (RE_Max_Base_Digits))));
18817 begin
18818 if Digs_Val > Max_Base_Digits then
18819 Error_Msg_Uint_1 := Max_Base_Digits;
18820 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18822 elsif No (Real_Range_Specification (Def)) then
18823 Error_Msg_Uint_1 := Max_Digs_Val;
18824 Error_Msg_N ("types with more than ^ digits need range spec "
18825 & "(RM 3.5.7(6))", Digs);
18826 end if;
18827 end;
18828 end if;
18830 -- Find a suitable type to derive from or complain and use a substitute
18832 Base_Typ := Find_Base_Type;
18834 -- If there are bounds given in the declaration use them as the bounds
18835 -- of the type, otherwise use the bounds of the predefined base type
18836 -- that was chosen based on the Digits value.
18838 if Present (Real_Range_Specification (Def)) then
18839 Set_Scalar_Range (T, Real_Range_Specification (Def));
18840 Set_Is_Constrained (T);
18842 Convert_Bound (Type_Low_Bound (T));
18843 Convert_Bound (Type_High_Bound (T));
18845 else
18846 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18847 end if;
18849 -- Complete definition of implicit base and declared first subtype. The
18850 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18851 -- are not clobbered when the floating point type acts as a full view of
18852 -- a private type.
18854 Set_Etype (Implicit_Base, Base_Typ);
18855 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18856 Set_Size_Info (Implicit_Base, Base_Typ);
18857 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18858 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18859 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18860 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18862 Mutate_Ekind (T, E_Floating_Point_Subtype);
18863 Set_Etype (T, Implicit_Base);
18864 Set_Size_Info (T, Implicit_Base);
18865 Set_RM_Size (T, RM_Size (Implicit_Base));
18866 Inherit_Rep_Item_Chain (T, Implicit_Base);
18868 if Digs_Val >= Uint_1 then
18869 Set_Digits_Value (T, Digs_Val);
18870 else
18871 pragma Assert (Serious_Errors_Detected > 0); null;
18872 end if;
18873 end Floating_Point_Type_Declaration;
18875 ----------------------------
18876 -- Get_Discriminant_Value --
18877 ----------------------------
18879 -- This is the situation:
18881 -- There is a non-derived type
18883 -- type T0 (Dx, Dy, Dz...)
18885 -- There are zero or more levels of derivation, with each derivation
18886 -- either purely inheriting the discriminants, or defining its own.
18888 -- type Ti is new Ti-1
18889 -- or
18890 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18891 -- or
18892 -- subtype Ti is ...
18894 -- The subtype issue is avoided by the use of Original_Record_Component,
18895 -- and the fact that derived subtypes also derive the constraints.
18897 -- This chain leads back from
18899 -- Typ_For_Constraint
18901 -- Typ_For_Constraint has discriminants, and the value for each
18902 -- discriminant is given by its corresponding Elmt of Constraints.
18904 -- Discriminant is some discriminant in this hierarchy
18906 -- We need to return its value
18908 -- We do this by recursively searching each level, and looking for
18909 -- Discriminant. Once we get to the bottom, we start backing up
18910 -- returning the value for it which may in turn be a discriminant
18911 -- further up, so on the backup we continue the substitution.
18913 function Get_Discriminant_Value
18914 (Discriminant : Entity_Id;
18915 Typ_For_Constraint : Entity_Id;
18916 Constraint : Elist_Id) return Node_Id
18918 function Root_Corresponding_Discriminant
18919 (Discr : Entity_Id) return Entity_Id;
18920 -- Given a discriminant, traverse the chain of inherited discriminants
18921 -- and return the topmost discriminant.
18923 function Search_Derivation_Levels
18924 (Ti : Entity_Id;
18925 Discrim_Values : Elist_Id;
18926 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18927 -- This is the routine that performs the recursive search of levels
18928 -- as described above.
18930 -------------------------------------
18931 -- Root_Corresponding_Discriminant --
18932 -------------------------------------
18934 function Root_Corresponding_Discriminant
18935 (Discr : Entity_Id) return Entity_Id
18937 D : Entity_Id;
18939 begin
18940 D := Discr;
18941 while Present (Corresponding_Discriminant (D)) loop
18942 D := Corresponding_Discriminant (D);
18943 end loop;
18945 return D;
18946 end Root_Corresponding_Discriminant;
18948 ------------------------------
18949 -- Search_Derivation_Levels --
18950 ------------------------------
18952 function Search_Derivation_Levels
18953 (Ti : Entity_Id;
18954 Discrim_Values : Elist_Id;
18955 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18957 Assoc : Elmt_Id;
18958 Disc : Entity_Id;
18959 Result : Node_Or_Entity_Id;
18960 Result_Entity : Node_Id;
18962 begin
18963 -- If inappropriate type, return Error, this happens only in
18964 -- cascaded error situations, and we want to avoid a blow up.
18966 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18967 return Error;
18968 end if;
18970 -- Look deeper if possible. Use Stored_Constraints only for
18971 -- untagged types. For tagged types use the given constraint.
18972 -- This asymmetry needs explanation???
18974 if not Stored_Discrim_Values
18975 and then Present (Stored_Constraint (Ti))
18976 and then not Is_Tagged_Type (Ti)
18977 then
18978 Result :=
18979 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18981 else
18982 declare
18983 Td : Entity_Id := Etype (Ti);
18985 begin
18986 -- If the parent type is private, the full view may include
18987 -- renamed discriminants, and it is those stored values that
18988 -- may be needed (the partial view never has more information
18989 -- than the full view).
18991 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18992 Td := Full_View (Td);
18993 end if;
18995 if Td = Ti then
18996 Result := Discriminant;
18998 else
18999 if Present (Stored_Constraint (Ti)) then
19000 Result :=
19001 Search_Derivation_Levels
19002 (Td, Stored_Constraint (Ti), True);
19003 else
19004 Result :=
19005 Search_Derivation_Levels
19006 (Td, Discrim_Values, Stored_Discrim_Values);
19007 end if;
19008 end if;
19009 end;
19010 end if;
19012 -- Extra underlying places to search, if not found above. For
19013 -- concurrent types, the relevant discriminant appears in the
19014 -- corresponding record. For a type derived from a private type
19015 -- without discriminant, the full view inherits the discriminants
19016 -- of the full view of the parent.
19018 if Result = Discriminant then
19019 if Is_Concurrent_Type (Ti)
19020 and then Present (Corresponding_Record_Type (Ti))
19021 then
19022 Result :=
19023 Search_Derivation_Levels (
19024 Corresponding_Record_Type (Ti),
19025 Discrim_Values,
19026 Stored_Discrim_Values);
19028 elsif Is_Private_Type (Ti)
19029 and then not Has_Discriminants (Ti)
19030 and then Present (Full_View (Ti))
19031 and then Etype (Full_View (Ti)) /= Ti
19032 then
19033 Result :=
19034 Search_Derivation_Levels (
19035 Full_View (Ti),
19036 Discrim_Values,
19037 Stored_Discrim_Values);
19038 end if;
19039 end if;
19041 -- If Result is not a (reference to a) discriminant, return it,
19042 -- otherwise set Result_Entity to the discriminant.
19044 if Nkind (Result) = N_Defining_Identifier then
19045 pragma Assert (Result = Discriminant);
19046 Result_Entity := Result;
19048 else
19049 if not Denotes_Discriminant (Result) then
19050 return Result;
19051 end if;
19053 Result_Entity := Entity (Result);
19054 end if;
19056 -- See if this level of derivation actually has discriminants because
19057 -- tagged derivations can add them, hence the lower levels need not
19058 -- have any.
19060 if not Has_Discriminants (Ti) then
19061 return Result;
19062 end if;
19064 -- Scan Ti's discriminants for Result_Entity, and return its
19065 -- corresponding value, if any.
19067 Result_Entity := Original_Record_Component (Result_Entity);
19069 Assoc := First_Elmt (Discrim_Values);
19071 if Stored_Discrim_Values then
19072 Disc := First_Stored_Discriminant (Ti);
19073 else
19074 Disc := First_Discriminant (Ti);
19075 end if;
19077 while Present (Disc) loop
19079 -- If no further associations return the discriminant, value will
19080 -- be found on the second pass.
19082 if No (Assoc) then
19083 return Result;
19084 end if;
19086 if Original_Record_Component (Disc) = Result_Entity then
19087 return Node (Assoc);
19088 end if;
19090 Next_Elmt (Assoc);
19092 if Stored_Discrim_Values then
19093 Next_Stored_Discriminant (Disc);
19094 else
19095 Next_Discriminant (Disc);
19096 end if;
19097 end loop;
19099 -- Could not find it
19101 return Result;
19102 end Search_Derivation_Levels;
19104 -- Local Variables
19106 Result : Node_Or_Entity_Id;
19108 -- Start of processing for Get_Discriminant_Value
19110 begin
19111 -- ??? This routine is a gigantic mess and will be deleted. For the
19112 -- time being just test for the trivial case before calling recurse.
19114 -- We are now celebrating the 20th anniversary of this comment!
19116 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
19117 declare
19118 D : Entity_Id;
19119 E : Elmt_Id;
19121 begin
19122 D := First_Discriminant (Typ_For_Constraint);
19123 E := First_Elmt (Constraint);
19124 while Present (D) loop
19125 if Chars (D) = Chars (Discriminant) then
19126 return Node (E);
19127 end if;
19129 Next_Discriminant (D);
19130 Next_Elmt (E);
19131 end loop;
19132 end;
19133 end if;
19135 Result := Search_Derivation_Levels
19136 (Typ_For_Constraint, Constraint, False);
19138 -- ??? hack to disappear when this routine is gone
19140 if Nkind (Result) = N_Defining_Identifier then
19141 declare
19142 D : Entity_Id;
19143 E : Elmt_Id;
19145 begin
19146 D := First_Discriminant (Typ_For_Constraint);
19147 E := First_Elmt (Constraint);
19148 while Present (D) loop
19149 if Root_Corresponding_Discriminant (D) = Discriminant then
19150 return Node (E);
19151 end if;
19153 Next_Discriminant (D);
19154 Next_Elmt (E);
19155 end loop;
19156 end;
19157 end if;
19159 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
19160 return Result;
19161 end Get_Discriminant_Value;
19163 --------------------------
19164 -- Has_Range_Constraint --
19165 --------------------------
19167 function Has_Range_Constraint (N : Node_Id) return Boolean is
19168 C : constant Node_Id := Constraint (N);
19170 begin
19171 if Nkind (C) = N_Range_Constraint then
19172 return True;
19174 elsif Nkind (C) = N_Digits_Constraint then
19175 return
19176 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
19177 or else Present (Range_Constraint (C));
19179 elsif Nkind (C) = N_Delta_Constraint then
19180 return Present (Range_Constraint (C));
19182 else
19183 return False;
19184 end if;
19185 end Has_Range_Constraint;
19187 ------------------------
19188 -- Inherit_Components --
19189 ------------------------
19191 function Inherit_Components
19192 (N : Node_Id;
19193 Parent_Base : Entity_Id;
19194 Derived_Base : Entity_Id;
19195 Is_Tagged : Boolean;
19196 Inherit_Discr : Boolean;
19197 Discs : Elist_Id) return Elist_Id
19199 Assoc_List : constant Elist_Id := New_Elmt_List;
19201 procedure Inherit_Component
19202 (Old_C : Entity_Id;
19203 Plain_Discrim : Boolean := False;
19204 Stored_Discrim : Boolean := False);
19205 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
19206 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
19207 -- True, Old_C is a stored discriminant. If they are both false then
19208 -- Old_C is a regular component.
19210 -----------------------
19211 -- Inherit_Component --
19212 -----------------------
19214 procedure Inherit_Component
19215 (Old_C : Entity_Id;
19216 Plain_Discrim : Boolean := False;
19217 Stored_Discrim : Boolean := False)
19219 procedure Set_Anonymous_Type (Id : Entity_Id);
19220 -- Id denotes the entity of an access discriminant or anonymous
19221 -- access component. Set the type of Id to either the same type of
19222 -- Old_C or create a new one depending on whether the parent and
19223 -- the child types are in the same scope.
19225 ------------------------
19226 -- Set_Anonymous_Type --
19227 ------------------------
19229 procedure Set_Anonymous_Type (Id : Entity_Id) is
19230 Old_Typ : constant Entity_Id := Etype (Old_C);
19232 begin
19233 if Scope (Parent_Base) = Scope (Derived_Base) then
19234 Set_Etype (Id, Old_Typ);
19236 -- The parent and the derived type are in two different scopes.
19237 -- Reuse the type of the original discriminant / component by
19238 -- copying it in order to preserve all attributes.
19240 else
19241 declare
19242 Typ : constant Entity_Id := New_Copy (Old_Typ);
19244 begin
19245 Set_Etype (Id, Typ);
19247 -- Since we do not generate component declarations for
19248 -- inherited components, associate the itype with the
19249 -- derived type.
19251 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
19252 Set_Scope (Typ, Derived_Base);
19253 end;
19254 end if;
19255 end Set_Anonymous_Type;
19257 -- Local variables and constants
19259 New_C : constant Entity_Id := New_Copy (Old_C);
19261 Corr_Discrim : Entity_Id;
19262 Discrim : Entity_Id;
19264 -- Start of processing for Inherit_Component
19266 begin
19267 pragma Assert (not Is_Tagged or not Stored_Discrim);
19269 Set_Parent (New_C, Parent (Old_C));
19271 -- Regular discriminants and components must be inserted in the scope
19272 -- of the Derived_Base. Do it here.
19274 if not Stored_Discrim then
19275 Enter_Name (New_C);
19276 end if;
19278 -- For tagged types the Original_Record_Component must point to
19279 -- whatever this field was pointing to in the parent type. This has
19280 -- already been achieved by the call to New_Copy above.
19282 if not Is_Tagged then
19283 Set_Original_Record_Component (New_C, New_C);
19284 Set_Corresponding_Record_Component (New_C, Old_C);
19285 end if;
19287 -- Set the proper type of an access discriminant
19289 if Ekind (New_C) = E_Discriminant
19290 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
19291 then
19292 Set_Anonymous_Type (New_C);
19293 end if;
19295 -- If we have inherited a component then see if its Etype contains
19296 -- references to Parent_Base discriminants. In this case, replace
19297 -- these references with the constraints given in Discs. We do not
19298 -- do this for the partial view of private types because this is
19299 -- not needed (only the components of the full view will be used
19300 -- for code generation) and cause problem. We also avoid this
19301 -- transformation in some error situations.
19303 if Ekind (New_C) = E_Component then
19305 -- Set the proper type of an anonymous access component
19307 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
19308 Set_Anonymous_Type (New_C);
19310 elsif (Is_Private_Type (Derived_Base)
19311 and then not Is_Generic_Type (Derived_Base))
19312 or else (Is_Empty_Elmt_List (Discs)
19313 and then not Expander_Active)
19314 then
19315 Set_Etype (New_C, Etype (Old_C));
19317 else
19318 -- The current component introduces a circularity of the
19319 -- following kind:
19321 -- limited with Pack_2;
19322 -- package Pack_1 is
19323 -- type T_1 is tagged record
19324 -- Comp : access Pack_2.T_2;
19325 -- ...
19326 -- end record;
19327 -- end Pack_1;
19329 -- with Pack_1;
19330 -- package Pack_2 is
19331 -- type T_2 is new Pack_1.T_1 with ...;
19332 -- end Pack_2;
19334 Set_Etype
19335 (New_C,
19336 Constrain_Component_Type
19337 (Old_C, Derived_Base, N, Parent_Base, Discs));
19338 end if;
19339 end if;
19341 if Plain_Discrim then
19342 Set_Corresponding_Discriminant (New_C, Old_C);
19343 Build_Discriminal (New_C);
19345 -- If we are explicitly inheriting a stored discriminant it will be
19346 -- completely hidden.
19348 elsif Stored_Discrim then
19349 Set_Corresponding_Discriminant (New_C, Empty);
19350 Set_Discriminal (New_C, Empty);
19351 Set_Is_Completely_Hidden (New_C);
19353 -- Set the Original_Record_Component of each discriminant in the
19354 -- derived base to point to the corresponding stored that we just
19355 -- created.
19357 Discrim := First_Discriminant (Derived_Base);
19358 while Present (Discrim) loop
19359 Corr_Discrim := Corresponding_Discriminant (Discrim);
19361 -- Corr_Discrim could be missing in an error situation
19363 if Present (Corr_Discrim)
19364 and then Original_Record_Component (Corr_Discrim) = Old_C
19365 then
19366 Set_Original_Record_Component (Discrim, New_C);
19367 Set_Corresponding_Record_Component (Discrim, Empty);
19368 end if;
19370 Next_Discriminant (Discrim);
19371 end loop;
19373 Append_Entity (New_C, Derived_Base);
19374 end if;
19376 if not Is_Tagged then
19377 Append_Elmt (Old_C, Assoc_List);
19378 Append_Elmt (New_C, Assoc_List);
19379 end if;
19380 end Inherit_Component;
19382 -- Variables local to Inherit_Component
19384 Loc : constant Source_Ptr := Sloc (N);
19386 Parent_Discrim : Entity_Id;
19387 Stored_Discrim : Entity_Id;
19388 D : Entity_Id;
19389 Component : Entity_Id;
19391 -- Start of processing for Inherit_Components
19393 begin
19394 if not Is_Tagged then
19395 Append_Elmt (Parent_Base, Assoc_List);
19396 Append_Elmt (Derived_Base, Assoc_List);
19397 end if;
19399 -- Inherit parent discriminants if needed
19401 if Inherit_Discr then
19402 Parent_Discrim := First_Discriminant (Parent_Base);
19403 while Present (Parent_Discrim) loop
19404 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19405 Next_Discriminant (Parent_Discrim);
19406 end loop;
19407 end if;
19409 -- Create explicit stored discrims for untagged types when necessary
19411 if not Has_Unknown_Discriminants (Derived_Base)
19412 and then Has_Discriminants (Parent_Base)
19413 and then not Is_Tagged
19414 and then
19415 (not Inherit_Discr
19416 or else First_Discriminant (Parent_Base) /=
19417 First_Stored_Discriminant (Parent_Base))
19418 then
19419 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19420 while Present (Stored_Discrim) loop
19421 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19422 Next_Stored_Discriminant (Stored_Discrim);
19423 end loop;
19424 end if;
19426 -- See if we can apply the second transformation for derived types, as
19427 -- explained in point 6. in the comments above Build_Derived_Record_Type
19428 -- This is achieved by appending Derived_Base discriminants into Discs,
19429 -- which has the side effect of returning a non empty Discs list to the
19430 -- caller of Inherit_Components, which is what we want. This must be
19431 -- done for private derived types if there are explicit stored
19432 -- discriminants, to ensure that we can retrieve the values of the
19433 -- constraints provided in the ancestors.
19435 if Inherit_Discr
19436 and then Is_Empty_Elmt_List (Discs)
19437 and then Present (First_Discriminant (Derived_Base))
19438 and then
19439 (not Is_Private_Type (Derived_Base)
19440 or else Is_Completely_Hidden
19441 (First_Stored_Discriminant (Derived_Base))
19442 or else Is_Generic_Type (Derived_Base))
19443 then
19444 D := First_Discriminant (Derived_Base);
19445 while Present (D) loop
19446 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19447 Next_Discriminant (D);
19448 end loop;
19449 end if;
19451 -- Finally, inherit non-discriminant components unless they are not
19452 -- visible because defined or inherited from the full view of the
19453 -- parent. Don't inherit the _parent field of the parent type.
19455 Component := First_Entity (Parent_Base);
19456 while Present (Component) loop
19458 -- Ada 2005 (AI-251): Do not inherit components associated with
19459 -- secondary tags of the parent.
19461 if Ekind (Component) = E_Component
19462 and then Present (Related_Type (Component))
19463 then
19464 null;
19466 elsif Ekind (Component) /= E_Component
19467 or else Chars (Component) = Name_uParent
19468 then
19469 null;
19471 -- If the derived type is within the parent type's declarative
19472 -- region, then the components can still be inherited even though
19473 -- they aren't visible at this point. This can occur for cases
19474 -- such as within public child units where the components must
19475 -- become visible upon entering the child unit's private part.
19477 elsif not Is_Visible_Component (Component)
19478 and then not In_Open_Scopes (Scope (Parent_Base))
19479 then
19480 null;
19482 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19483 then
19484 null;
19486 else
19487 Inherit_Component (Component);
19488 end if;
19490 Next_Entity (Component);
19491 end loop;
19493 -- For tagged derived types, inherited discriminants cannot be used in
19494 -- component declarations of the record extension part. To achieve this
19495 -- we mark the inherited discriminants as not visible.
19497 if Is_Tagged and then Inherit_Discr then
19498 D := First_Discriminant (Derived_Base);
19499 while Present (D) loop
19500 Set_Is_Immediately_Visible (D, False);
19501 Next_Discriminant (D);
19502 end loop;
19503 end if;
19505 return Assoc_List;
19506 end Inherit_Components;
19508 ----------------------
19509 -- Is_EVF_Procedure --
19510 ----------------------
19512 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19513 Formal : Entity_Id;
19515 begin
19516 -- Examine the formals of an Extensions_Visible False procedure looking
19517 -- for a controlling OUT parameter.
19519 if Ekind (Subp) = E_Procedure
19520 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19521 then
19522 Formal := First_Formal (Subp);
19523 while Present (Formal) loop
19524 if Ekind (Formal) = E_Out_Parameter
19525 and then Is_Controlling_Formal (Formal)
19526 then
19527 return True;
19528 end if;
19530 Next_Formal (Formal);
19531 end loop;
19532 end if;
19534 return False;
19535 end Is_EVF_Procedure;
19537 --------------------------
19538 -- Is_Private_Primitive --
19539 --------------------------
19541 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19542 Prim_Scope : constant Entity_Id := Scope (Prim);
19543 Priv_Entity : Entity_Id;
19544 begin
19545 if Is_Package_Or_Generic_Package (Prim_Scope) then
19546 Priv_Entity := First_Private_Entity (Prim_Scope);
19548 while Present (Priv_Entity) loop
19549 if Priv_Entity = Prim then
19550 return True;
19551 end if;
19553 Next_Entity (Priv_Entity);
19554 end loop;
19555 end if;
19557 return False;
19558 end Is_Private_Primitive;
19560 ------------------------------
19561 -- Is_Valid_Constraint_Kind --
19562 ------------------------------
19564 function Is_Valid_Constraint_Kind
19565 (T_Kind : Type_Kind;
19566 Constraint_Kind : Node_Kind) return Boolean
19568 begin
19569 case T_Kind is
19570 when Enumeration_Kind
19571 | Integer_Kind
19573 return Constraint_Kind = N_Range_Constraint;
19575 when Decimal_Fixed_Point_Kind =>
19576 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19578 when Ordinary_Fixed_Point_Kind =>
19579 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19581 when Float_Kind =>
19582 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19584 when Access_Kind
19585 | Array_Kind
19586 | Class_Wide_Kind
19587 | Concurrent_Kind
19588 | Private_Kind
19589 | E_Incomplete_Type
19590 | E_Record_Subtype
19591 | E_Record_Type
19593 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19595 when others =>
19596 return True; -- Error will be detected later
19597 end case;
19598 end Is_Valid_Constraint_Kind;
19600 --------------------------
19601 -- Is_Visible_Component --
19602 --------------------------
19604 function Is_Visible_Component
19605 (C : Entity_Id;
19606 N : Node_Id := Empty) return Boolean
19608 Original_Comp : Entity_Id := Empty;
19609 Original_Type : Entity_Id;
19610 Type_Scope : Entity_Id;
19612 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19613 -- Check whether parent type of inherited component is declared locally,
19614 -- possibly within a nested package or instance. The current scope is
19615 -- the derived record itself.
19617 -------------------
19618 -- Is_Local_Type --
19619 -------------------
19621 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19622 begin
19623 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19624 end Is_Local_Type;
19626 -- Start of processing for Is_Visible_Component
19628 begin
19629 if Ekind (C) in E_Component | E_Discriminant then
19630 Original_Comp := Original_Record_Component (C);
19631 end if;
19633 if No (Original_Comp) then
19635 -- Premature usage, or previous error
19637 return False;
19639 else
19640 Original_Type := Scope (Original_Comp);
19641 Type_Scope := Scope (Base_Type (Scope (C)));
19642 end if;
19644 -- This test only concerns tagged types
19646 if not Is_Tagged_Type (Original_Type) then
19648 -- Check if this is a renamed discriminant (hidden either by the
19649 -- derived type or by some ancestor), unless we are analyzing code
19650 -- generated by the expander since it may reference such components
19651 -- (for example see the expansion of Deep_Adjust).
19653 if Ekind (C) = E_Discriminant and then Present (N) then
19654 return
19655 not Comes_From_Source (N)
19656 or else not Is_Completely_Hidden (C);
19657 else
19658 return True;
19659 end if;
19661 -- If it is _Parent or _Tag, there is no visibility issue
19663 elsif not Comes_From_Source (Original_Comp) then
19664 return True;
19666 -- Discriminants are visible unless the (private) type has unknown
19667 -- discriminants. If the discriminant reference is inserted for a
19668 -- discriminant check on a full view it is also visible.
19670 elsif Ekind (Original_Comp) = E_Discriminant
19671 and then
19672 (not Has_Unknown_Discriminants (Original_Type)
19673 or else (Present (N)
19674 and then Nkind (N) = N_Selected_Component
19675 and then Nkind (Prefix (N)) = N_Type_Conversion
19676 and then not Comes_From_Source (Prefix (N))))
19677 then
19678 return True;
19680 -- If the component has been declared in an ancestor which is currently
19681 -- a private type, then it is not visible. The same applies if the
19682 -- component's containing type is not in an open scope and the original
19683 -- component's enclosing type is a visible full view of a private type
19684 -- (which can occur in cases where an attempt is being made to reference
19685 -- a component in a sibling package that is inherited from a visible
19686 -- component of a type in an ancestor package; the component in the
19687 -- sibling package should not be visible even though the component it
19688 -- inherited from is visible), but instance bodies are not subject to
19689 -- this second case since they have the Has_Private_View mechanism to
19690 -- ensure proper visibility. This does not apply however in the case
19691 -- where the scope of the type is a private child unit, or when the
19692 -- parent comes from a local package in which the ancestor is currently
19693 -- visible. The latter suppression of visibility is needed for cases
19694 -- that are tested in B730006.
19696 elsif Is_Private_Type (Original_Type)
19697 or else
19698 (not Is_Private_Descendant (Type_Scope)
19699 and then not In_Open_Scopes (Type_Scope)
19700 and then Has_Private_Declaration (Original_Type)
19701 and then not In_Instance_Body)
19702 then
19703 -- If the type derives from an entity in a formal package, there
19704 -- are no additional visible components.
19706 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19707 N_Formal_Package_Declaration
19708 then
19709 return False;
19711 -- if we are not in the private part of the current package, there
19712 -- are no additional visible components.
19714 elsif Ekind (Scope (Current_Scope)) = E_Package
19715 and then not In_Private_Part (Scope (Current_Scope))
19716 then
19717 return False;
19718 else
19719 return
19720 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19721 and then In_Open_Scopes (Scope (Original_Type))
19722 and then Is_Local_Type (Type_Scope);
19723 end if;
19725 -- There is another weird way in which a component may be invisible when
19726 -- the private and the full view are not derived from the same ancestor.
19727 -- Here is an example :
19729 -- type A1 is tagged record F1 : integer; end record;
19730 -- type A2 is new A1 with record F2 : integer; end record;
19731 -- type T is new A1 with private;
19732 -- private
19733 -- type T is new A2 with null record;
19735 -- In this case, the full view of T inherits F1 and F2 but the private
19736 -- view inherits only F1
19738 else
19739 declare
19740 Ancestor : Entity_Id := Scope (C);
19742 begin
19743 loop
19744 if Ancestor = Original_Type then
19745 return True;
19747 -- The ancestor may have a partial view of the original type,
19748 -- but if the full view is in scope, as in a child body, the
19749 -- component is visible.
19751 elsif In_Private_Part (Scope (Original_Type))
19752 and then Full_View (Ancestor) = Original_Type
19753 then
19754 return True;
19756 elsif Ancestor = Etype (Ancestor) then
19758 -- No further ancestors to examine
19760 return False;
19761 end if;
19763 Ancestor := Etype (Ancestor);
19764 end loop;
19765 end;
19766 end if;
19767 end Is_Visible_Component;
19769 --------------------------
19770 -- Make_Class_Wide_Type --
19771 --------------------------
19773 procedure Make_Class_Wide_Type (T : Entity_Id) is
19774 CW_Type : Entity_Id;
19775 CW_Name : Name_Id;
19776 Next_E : Entity_Id;
19777 Prev_E : Entity_Id;
19779 begin
19780 if Present (Class_Wide_Type (T)) then
19782 -- The class-wide type is a partially decorated entity created for a
19783 -- unanalyzed tagged type referenced through a limited with clause.
19784 -- When the tagged type is analyzed, its class-wide type needs to be
19785 -- redecorated. Note that we reuse the entity created by Decorate_
19786 -- Tagged_Type in order to preserve all links.
19788 if Materialize_Entity (Class_Wide_Type (T)) then
19789 CW_Type := Class_Wide_Type (T);
19790 Set_Materialize_Entity (CW_Type, False);
19792 -- The class wide type can have been defined by the partial view, in
19793 -- which case everything is already done.
19795 else
19796 return;
19797 end if;
19799 -- Default case, we need to create a new class-wide type
19801 else
19802 CW_Type :=
19803 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19804 end if;
19806 -- Inherit root type characteristics
19808 CW_Name := Chars (CW_Type);
19809 Next_E := Next_Entity (CW_Type);
19810 Prev_E := Prev_Entity (CW_Type);
19811 Copy_Node (T, CW_Type);
19812 Set_Comes_From_Source (CW_Type, False);
19813 Set_Chars (CW_Type, CW_Name);
19814 Set_Parent (CW_Type, Parent (T));
19815 Set_Prev_Entity (CW_Type, Prev_E);
19816 Set_Next_Entity (CW_Type, Next_E);
19818 -- Ensure we have a new freeze node for the class-wide type. The partial
19819 -- view may have freeze action of its own, requiring a proper freeze
19820 -- node, and the same freeze node cannot be shared between the two
19821 -- types.
19823 Set_Has_Delayed_Freeze (CW_Type);
19824 Set_Freeze_Node (CW_Type, Empty);
19826 -- Customize the class-wide type: It has no prim. op., it cannot be
19827 -- abstract, its Etype points back to the specific root type, and it
19828 -- cannot have any invariants.
19830 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19831 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19833 elsif Ekind (CW_Type) in Concurrent_Kind then
19834 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19835 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19837 if Ekind (CW_Type) in Task_Kind then
19838 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19839 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19840 end if;
19842 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19843 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19844 end if;
19846 elsif Ekind (CW_Type) = E_Record_Type then
19847 Reinit_Field_To_Zero (CW_Type, F_Corresponding_Concurrent_Type);
19848 end if;
19850 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19851 Set_Is_Tagged_Type (CW_Type, True);
19852 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19853 Set_Is_Abstract_Type (CW_Type, False);
19854 Set_Is_Constrained (CW_Type, False);
19855 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19856 Set_Default_SSO (CW_Type);
19857 Set_Has_Inheritable_Invariants (CW_Type, False);
19858 Set_Has_Inherited_Invariants (CW_Type, False);
19859 Set_Has_Own_Invariants (CW_Type, False);
19861 if Ekind (T) = E_Class_Wide_Subtype then
19862 Set_Etype (CW_Type, Etype (Base_Type (T)));
19863 else
19864 Set_Etype (CW_Type, T);
19865 end if;
19867 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19869 -- If this is the class_wide type of a constrained subtype, it does
19870 -- not have discriminants.
19872 Set_Has_Discriminants (CW_Type,
19873 Has_Discriminants (T) and then not Is_Constrained (T));
19875 Set_Has_Unknown_Discriminants (CW_Type, True);
19876 Set_Class_Wide_Type (T, CW_Type);
19877 Set_Equivalent_Type (CW_Type, Empty);
19879 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19881 Set_Class_Wide_Type (CW_Type, CW_Type);
19882 end Make_Class_Wide_Type;
19884 ----------------
19885 -- Make_Index --
19886 ----------------
19888 procedure Make_Index
19889 (N : Node_Id;
19890 Related_Nod : Node_Id;
19891 Related_Id : Entity_Id := Empty;
19892 Suffix_Index : Pos := 1)
19894 R : Node_Id;
19895 T : Entity_Id;
19896 Def_Id : Entity_Id := Empty;
19897 Found : Boolean := False;
19899 begin
19900 -- For a discrete range used in a constrained array definition and
19901 -- defined by a range, an implicit conversion to the predefined type
19902 -- INTEGER is assumed if each bound is either a numeric literal, a named
19903 -- number, or an attribute, and the type of both bounds (prior to the
19904 -- implicit conversion) is the type universal_integer. Otherwise, both
19905 -- bounds must be of the same discrete type, other than universal
19906 -- integer; this type must be determinable independently of the
19907 -- context, but using the fact that the type must be discrete and that
19908 -- both bounds must have the same type.
19910 -- Character literals also have a universal type in the absence of
19911 -- of additional context, and are resolved to Standard_Character.
19913 if Nkind (N) = N_Range then
19915 -- The index is given by a range constraint. The bounds are known
19916 -- to be of a consistent type.
19918 if not Is_Overloaded (N) then
19919 T := Etype (N);
19921 -- For universal bounds, choose the specific predefined type
19923 if T = Universal_Integer then
19924 T := Standard_Integer;
19926 elsif T = Any_Character then
19927 Ambiguous_Character (Low_Bound (N));
19929 T := Standard_Character;
19930 end if;
19932 -- The node may be overloaded because some user-defined operators
19933 -- are available, but if a universal interpretation exists it is
19934 -- also the selected one.
19936 elsif Universal_Interpretation (N) = Universal_Integer then
19937 T := Standard_Integer;
19939 else
19940 T := Any_Type;
19942 declare
19943 Ind : Interp_Index;
19944 It : Interp;
19946 begin
19947 Get_First_Interp (N, Ind, It);
19948 while Present (It.Typ) loop
19949 if Is_Discrete_Type (It.Typ) then
19951 if Found
19952 and then not Covers (It.Typ, T)
19953 and then not Covers (T, It.Typ)
19954 then
19955 Error_Msg_N ("ambiguous bounds in discrete range", N);
19956 exit;
19957 else
19958 T := It.Typ;
19959 Found := True;
19960 end if;
19961 end if;
19963 Get_Next_Interp (Ind, It);
19964 end loop;
19966 if T = Any_Type then
19967 Error_Msg_N ("discrete type required for range", N);
19968 Set_Etype (N, Any_Type);
19969 return;
19971 elsif T = Universal_Integer then
19972 T := Standard_Integer;
19973 end if;
19974 end;
19975 end if;
19977 if not Is_Discrete_Type (T) then
19978 Error_Msg_N ("discrete type required for range", N);
19979 Set_Etype (N, Any_Type);
19980 return;
19981 end if;
19983 -- If the range bounds are "T'First .. T'Last" where T is a name of a
19984 -- discrete type, then use T as the type of the index.
19986 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19987 and then Attribute_Name (Low_Bound (N)) = Name_First
19988 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19989 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19991 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19992 and then Attribute_Name (High_Bound (N)) = Name_Last
19993 and then Is_Entity_Name (Prefix (High_Bound (N)))
19994 and then Entity (Prefix (High_Bound (N))) = Def_Id
19995 then
19996 Def_Id := Entity (Prefix (Low_Bound (N)));
19997 end if;
19999 R := N;
20000 Process_Range_Expr_In_Decl (R, T);
20002 elsif Nkind (N) = N_Subtype_Indication then
20004 -- The index is given by a subtype with a range constraint
20006 T := Base_Type (Entity (Subtype_Mark (N)));
20008 if not Is_Discrete_Type (T) then
20009 Error_Msg_N ("discrete type required for range", N);
20010 Set_Etype (N, Any_Type);
20011 return;
20012 end if;
20014 R := Range_Expression (Constraint (N));
20016 Resolve (R, T);
20017 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
20019 elsif Nkind (N) = N_Attribute_Reference then
20021 -- Catch beginner's error (use of attribute other than 'Range)
20023 if Attribute_Name (N) /= Name_Range then
20024 Error_Msg_N ("expect attribute ''Range", N);
20025 Set_Etype (N, Any_Type);
20026 return;
20027 end if;
20029 -- If the node denotes the range of a type mark, that is also the
20030 -- resulting type, and we do not need to create an Itype for it.
20032 if Is_Entity_Name (Prefix (N))
20033 and then Comes_From_Source (N)
20034 and then Is_Discrete_Type (Entity (Prefix (N)))
20035 then
20036 Def_Id := Entity (Prefix (N));
20037 end if;
20039 Analyze_And_Resolve (N);
20040 T := Etype (N);
20041 R := N;
20043 -- If none of the above, must be a subtype. We convert this to a
20044 -- range attribute reference because in the case of declared first
20045 -- named subtypes, the types in the range reference can be different
20046 -- from the type of the entity. A range attribute normalizes the
20047 -- reference and obtains the correct types for the bounds.
20049 -- This transformation is in the nature of an expansion, is only
20050 -- done if expansion is active. In particular, it is not done on
20051 -- formal generic types, because we need to retain the name of the
20052 -- original index for instantiation purposes.
20054 else
20055 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
20056 Error_Msg_N ("invalid subtype mark in discrete range", N);
20057 Set_Etype (N, Any_Integer);
20058 return;
20060 else
20061 -- The type mark may be that of an incomplete type. It is only
20062 -- now that we can get the full view, previous analysis does
20063 -- not look specifically for a type mark.
20065 Set_Entity (N, Get_Full_View (Entity (N)));
20066 Set_Etype (N, Entity (N));
20067 Def_Id := Entity (N);
20069 if not Is_Discrete_Type (Def_Id) then
20070 Error_Msg_N ("discrete type required for index", N);
20071 Set_Etype (N, Any_Type);
20072 return;
20073 end if;
20074 end if;
20076 if Expander_Active then
20077 Rewrite (N,
20078 Make_Attribute_Reference (Sloc (N),
20079 Attribute_Name => Name_Range,
20080 Prefix => Relocate_Node (N)));
20082 -- The original was a subtype mark that does not freeze. This
20083 -- means that the rewritten version must not freeze either.
20085 Set_Must_Not_Freeze (N);
20086 Set_Must_Not_Freeze (Prefix (N));
20087 Analyze_And_Resolve (N);
20088 T := Etype (N);
20089 R := N;
20091 -- If expander is inactive, type is legal, nothing else to construct
20093 else
20094 return;
20095 end if;
20096 end if;
20098 if not Is_Discrete_Type (T) then
20099 Error_Msg_N ("discrete type required for range", N);
20100 Set_Etype (N, Any_Type);
20101 return;
20103 elsif T = Any_Type then
20104 Set_Etype (N, Any_Type);
20105 return;
20106 end if;
20108 -- We will now create the appropriate Itype to describe the range, but
20109 -- first a check. If we originally had a subtype, then we just label
20110 -- the range with this subtype. Not only is there no need to construct
20111 -- a new subtype, but it is wrong to do so for two reasons:
20113 -- 1. A legality concern, if we have a subtype, it must not freeze,
20114 -- and the Itype would cause freezing incorrectly
20116 -- 2. An efficiency concern, if we created an Itype, it would not be
20117 -- recognized as the same type for the purposes of eliminating
20118 -- checks in some circumstances.
20120 -- We signal this case by setting the subtype entity in Def_Id
20122 if No (Def_Id) then
20123 Def_Id :=
20124 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
20125 Set_Etype (Def_Id, Base_Type (T));
20127 if Is_Signed_Integer_Type (T) then
20128 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
20130 elsif Is_Modular_Integer_Type (T) then
20131 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
20133 else
20134 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
20135 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
20136 Set_First_Literal (Def_Id, First_Literal (T));
20137 end if;
20139 Set_Size_Info (Def_Id, (T));
20140 Set_RM_Size (Def_Id, RM_Size (T));
20141 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
20143 Set_Scalar_Range (Def_Id, R);
20144 Conditional_Delay (Def_Id, T);
20146 -- In the subtype indication case inherit properties of the parent
20148 if Nkind (N) = N_Subtype_Indication then
20150 -- It is enough to inherit predicate flags and not the predicate
20151 -- functions, because predicates on an index type are illegal
20152 -- anyway and the flags are enough to detect them.
20154 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
20156 -- If the immediate parent of the new subtype is nonstatic, then
20157 -- the subtype we create is nonstatic as well, even if its bounds
20158 -- are static.
20160 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
20161 Set_Is_Non_Static_Subtype (Def_Id);
20162 end if;
20163 end if;
20165 Set_Parent (Def_Id, N);
20166 end if;
20168 -- Final step is to label the index with this constructed type
20170 Set_Etype (N, Def_Id);
20171 end Make_Index;
20173 ------------------------------
20174 -- Modular_Type_Declaration --
20175 ------------------------------
20177 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20178 Mod_Expr : constant Node_Id := Expression (Def);
20179 M_Val : Uint;
20181 procedure Set_Modular_Size (Bits : Int);
20182 -- Sets RM_Size to Bits, and Esize to normal word size above this
20184 ----------------------
20185 -- Set_Modular_Size --
20186 ----------------------
20188 procedure Set_Modular_Size (Bits : Int) is
20189 Siz : Int;
20191 begin
20192 Set_RM_Size (T, UI_From_Int (Bits));
20194 if Bits < System_Max_Binary_Modulus_Power then
20195 Siz := 8;
20197 while Siz < 128 loop
20198 exit when Bits <= Siz;
20199 Siz := Siz * 2;
20200 end loop;
20202 Set_Esize (T, UI_From_Int (Siz));
20204 else
20205 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
20206 end if;
20208 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
20209 Set_Is_Known_Valid (T);
20210 end if;
20211 end Set_Modular_Size;
20213 -- Start of processing for Modular_Type_Declaration
20215 begin
20216 -- If the mod expression is (exactly) 2 * literal, where literal is
20217 -- 128 or less, then almost certainly the * was meant to be **. Warn.
20219 if Warn_On_Suspicious_Modulus_Value
20220 and then Nkind (Mod_Expr) = N_Op_Multiply
20221 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
20222 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
20223 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
20224 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
20225 then
20226 Error_Msg_N
20227 ("suspicious MOD value, was '*'* intended'??.m?", Mod_Expr);
20228 end if;
20230 -- Proceed with analysis of mod expression
20232 Analyze_And_Resolve (Mod_Expr, Any_Integer);
20234 Set_Etype (T, T);
20235 Mutate_Ekind (T, E_Modular_Integer_Type);
20236 Reinit_Alignment (T);
20237 Set_Is_Constrained (T);
20239 if not Is_OK_Static_Expression (Mod_Expr) then
20240 Flag_Non_Static_Expr
20241 ("non-static expression used for modular type bound!", Mod_Expr);
20242 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20243 else
20244 M_Val := Expr_Value (Mod_Expr);
20245 end if;
20247 if M_Val < 1 then
20248 Error_Msg_N ("modulus value must be positive", Mod_Expr);
20249 M_Val := 2 ** System_Max_Binary_Modulus_Power;
20250 end if;
20252 if M_Val > 2 ** Standard_Long_Integer_Size then
20253 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
20254 end if;
20256 Set_Modulus (T, M_Val);
20258 -- Create bounds for the modular type based on the modulus given in
20259 -- the type declaration and then analyze and resolve those bounds.
20261 Set_Scalar_Range (T,
20262 Make_Range (Sloc (Mod_Expr),
20263 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
20264 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
20266 -- Properly analyze the literals for the range. We do this manually
20267 -- because we can't go calling Resolve, since we are resolving these
20268 -- bounds with the type, and this type is certainly not complete yet.
20270 Set_Etype (Low_Bound (Scalar_Range (T)), T);
20271 Set_Etype (High_Bound (Scalar_Range (T)), T);
20272 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
20273 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
20275 -- Loop through powers of two to find number of bits required
20277 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
20279 -- Binary case
20281 if M_Val = 2 ** Bits then
20282 Set_Modular_Size (Bits);
20283 return;
20285 -- Nonbinary case
20287 elsif M_Val < 2 ** Bits then
20288 Set_Non_Binary_Modulus (T);
20290 if Bits > System_Max_Nonbinary_Modulus_Power then
20291 Error_Msg_Uint_1 :=
20292 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
20293 Error_Msg_F
20294 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
20295 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20296 return;
20298 else
20299 -- In the nonbinary case, set size as per RM 13.3(55)
20301 Set_Modular_Size (Bits);
20302 return;
20303 end if;
20304 end if;
20306 end loop;
20308 -- If we fall through, then the size exceed System.Max_Binary_Modulus
20309 -- so we just signal an error and set the maximum size.
20311 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
20312 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
20314 Set_Modular_Size (System_Max_Binary_Modulus_Power);
20315 Reinit_Alignment (T);
20317 end Modular_Type_Declaration;
20319 --------------------------
20320 -- New_Concatenation_Op --
20321 --------------------------
20323 procedure New_Concatenation_Op (Typ : Entity_Id) is
20324 Loc : constant Source_Ptr := Sloc (Typ);
20325 Op : Entity_Id;
20327 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
20328 -- Create abbreviated declaration for the formal of a predefined
20329 -- Operator 'Op' of type 'Typ'
20331 --------------------
20332 -- Make_Op_Formal --
20333 --------------------
20335 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
20336 Formal : Entity_Id;
20337 begin
20338 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
20339 Set_Etype (Formal, Typ);
20340 Set_Mechanism (Formal, Default_Mechanism);
20341 return Formal;
20342 end Make_Op_Formal;
20344 -- Start of processing for New_Concatenation_Op
20346 begin
20347 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
20349 Mutate_Ekind (Op, E_Operator);
20350 Set_Is_Not_Self_Hidden (Op);
20351 Set_Scope (Op, Current_Scope);
20352 Set_Etype (Op, Typ);
20353 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
20354 Set_Is_Immediately_Visible (Op);
20355 Set_Is_Intrinsic_Subprogram (Op);
20356 Set_Has_Completion (Op);
20357 Append_Entity (Op, Current_Scope);
20359 Set_Name_Entity_Id (Name_Op_Concat, Op);
20361 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20362 Append_Entity (Make_Op_Formal (Typ, Op), Op);
20363 end New_Concatenation_Op;
20365 -------------------------
20366 -- OK_For_Limited_Init --
20367 -------------------------
20369 -- ???Check all calls of this, and compare the conditions under which it's
20370 -- called.
20372 function OK_For_Limited_Init
20373 (Typ : Entity_Id;
20374 Exp : Node_Id) return Boolean
20376 begin
20377 return Is_CPP_Constructor_Call (Exp)
20378 or else (Ada_Version >= Ada_2005
20379 and then not Debug_Flag_Dot_L
20380 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20381 end OK_For_Limited_Init;
20383 -------------------------------
20384 -- OK_For_Limited_Init_In_05 --
20385 -------------------------------
20387 function OK_For_Limited_Init_In_05
20388 (Typ : Entity_Id;
20389 Exp : Node_Id) return Boolean
20391 begin
20392 -- An object of a limited interface type can be initialized with any
20393 -- expression of a nonlimited descendant type. However this does not
20394 -- apply if this is a view conversion of some other expression. This
20395 -- is checked below.
20397 if Is_Class_Wide_Type (Typ)
20398 and then Is_Limited_Interface (Typ)
20399 and then not Is_Limited_Type (Etype (Exp))
20400 and then Nkind (Exp) /= N_Type_Conversion
20401 then
20402 return True;
20403 end if;
20405 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20406 -- case of limited aggregates (including extension aggregates), and
20407 -- function calls. The function call may have been given in prefixed
20408 -- notation, in which case the original node is an indexed component.
20409 -- If the function is parameterless, the original node was an explicit
20410 -- dereference. The function may also be parameterless, in which case
20411 -- the source node is just an identifier.
20413 -- A branch of a conditional expression may have been removed if the
20414 -- condition is statically known. This happens during expansion, and
20415 -- thus will not happen if previous errors were encountered. The check
20416 -- will have been performed on the chosen branch, which replaces the
20417 -- original conditional expression.
20419 if No (Exp) then
20420 return True;
20421 end if;
20423 case Nkind (Original_Node (Exp)) is
20424 when N_Aggregate
20425 | N_Delta_Aggregate
20426 | N_Extension_Aggregate
20427 | N_Function_Call
20428 | N_Op
20430 return True;
20432 when N_Identifier =>
20433 return Present (Entity (Original_Node (Exp)))
20434 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20436 when N_Qualified_Expression =>
20437 return
20438 OK_For_Limited_Init_In_05
20439 (Typ, Expression (Original_Node (Exp)));
20441 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20442 -- with a function call, the expander has rewritten the call into an
20443 -- N_Type_Conversion node to force displacement of the pointer to
20444 -- reference the component containing the secondary dispatch table.
20445 -- Otherwise a type conversion is not a legal context.
20446 -- A return statement for a build-in-place function returning a
20447 -- synchronized type also introduces an unchecked conversion.
20449 when N_Type_Conversion
20450 | N_Unchecked_Type_Conversion
20452 return not Comes_From_Source (Exp)
20453 and then
20454 -- If the conversion has been rewritten, check Original_Node;
20455 -- otherwise, check the expression of the compiler-generated
20456 -- conversion (which is a conversion that we want to ignore
20457 -- for purposes of the limited-initialization restrictions).
20459 (if Is_Rewrite_Substitution (Exp)
20460 then OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp))
20461 else OK_For_Limited_Init_In_05 (Typ, Expression (Exp)));
20463 when N_Explicit_Dereference
20464 | N_Indexed_Component
20465 | N_Selected_Component
20467 return Nkind (Exp) = N_Function_Call;
20469 -- A use of 'Input is a function call, hence allowed. Normally the
20470 -- attribute will be changed to a call, but the attribute by itself
20471 -- can occur with -gnatc.
20473 when N_Attribute_Reference =>
20474 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20476 -- "return raise ..." is OK
20478 when N_Raise_Expression =>
20479 return True;
20481 -- For a case expression, all dependent expressions must be legal
20483 when N_Case_Expression =>
20484 declare
20485 Alt : Node_Id;
20487 begin
20488 Alt := First (Alternatives (Original_Node (Exp)));
20489 while Present (Alt) loop
20490 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20491 return False;
20492 end if;
20494 Next (Alt);
20495 end loop;
20497 return True;
20498 end;
20500 -- For an if expression, all dependent expressions must be legal
20502 when N_If_Expression =>
20503 declare
20504 Then_Expr : constant Node_Id :=
20505 Next (First (Expressions (Original_Node (Exp))));
20506 Else_Expr : constant Node_Id := Next (Then_Expr);
20507 begin
20508 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20509 and then
20510 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20511 end;
20513 when others =>
20514 return False;
20515 end case;
20516 end OK_For_Limited_Init_In_05;
20518 -------------------------------------------
20519 -- Ordinary_Fixed_Point_Type_Declaration --
20520 -------------------------------------------
20522 procedure Ordinary_Fixed_Point_Type_Declaration
20523 (T : Entity_Id;
20524 Def : Node_Id)
20526 Loc : constant Source_Ptr := Sloc (Def);
20527 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20528 RRS : constant Node_Id := Real_Range_Specification (Def);
20529 Implicit_Base : Entity_Id;
20530 Delta_Val : Ureal;
20531 Small_Val : Ureal;
20532 Low_Val : Ureal;
20533 High_Val : Ureal;
20535 begin
20536 Check_Restriction (No_Fixed_Point, Def);
20538 -- Create implicit base type
20540 Implicit_Base :=
20541 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20542 Set_Etype (Implicit_Base, Implicit_Base);
20544 -- Analyze and process delta expression
20546 Analyze_And_Resolve (Delta_Expr, Any_Real);
20548 Check_Delta_Expression (Delta_Expr);
20549 Delta_Val := Expr_Value_R (Delta_Expr);
20551 Set_Delta_Value (Implicit_Base, Delta_Val);
20553 -- Compute default small from given delta, which is the largest power
20554 -- of two that does not exceed the given delta value.
20556 declare
20557 Tmp : Ureal;
20558 Scale : Int;
20560 begin
20561 Tmp := Ureal_1;
20562 Scale := 0;
20564 if Delta_Val < Ureal_1 then
20565 while Delta_Val < Tmp loop
20566 Tmp := Tmp / Ureal_2;
20567 Scale := Scale + 1;
20568 end loop;
20570 else
20571 loop
20572 Tmp := Tmp * Ureal_2;
20573 exit when Tmp > Delta_Val;
20574 Scale := Scale - 1;
20575 end loop;
20576 end if;
20578 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20579 end;
20581 Set_Small_Value (Implicit_Base, Small_Val);
20583 -- If no range was given, set a dummy range
20585 if RRS <= Empty_Or_Error then
20586 Low_Val := -Small_Val;
20587 High_Val := Small_Val;
20589 -- Otherwise analyze and process given range
20591 else
20592 declare
20593 Low : constant Node_Id := Low_Bound (RRS);
20594 High : constant Node_Id := High_Bound (RRS);
20596 begin
20597 Analyze_And_Resolve (Low, Any_Real);
20598 Analyze_And_Resolve (High, Any_Real);
20599 Check_Real_Bound (Low);
20600 Check_Real_Bound (High);
20602 -- Obtain and set the range
20604 Low_Val := Expr_Value_R (Low);
20605 High_Val := Expr_Value_R (High);
20607 if Low_Val > High_Val then
20608 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20609 end if;
20610 end;
20611 end if;
20613 -- The range for both the implicit base and the declared first subtype
20614 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20615 -- set a temporary range in place. Note that the bounds of the base
20616 -- type will be widened to be symmetrical and to fill the available
20617 -- bits when the type is frozen.
20619 -- We could do this with all discrete types, and probably should, but
20620 -- we absolutely have to do it for fixed-point, since the end-points
20621 -- of the range and the size are determined by the small value, which
20622 -- could be reset before the freeze point.
20624 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20625 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20627 -- Complete definition of first subtype. The inheritance of the rep item
20628 -- chain ensures that SPARK-related pragmas are not clobbered when the
20629 -- ordinary fixed point type acts as a full view of a private type.
20631 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20632 Set_Etype (T, Implicit_Base);
20633 Reinit_Size_Align (T);
20634 Inherit_Rep_Item_Chain (T, Implicit_Base);
20635 Set_Small_Value (T, Small_Val);
20636 Set_Delta_Value (T, Delta_Val);
20637 Set_Is_Constrained (T);
20638 end Ordinary_Fixed_Point_Type_Declaration;
20640 ----------------------------------
20641 -- Preanalyze_Assert_Expression --
20642 ----------------------------------
20644 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20645 begin
20646 In_Assertion_Expr := In_Assertion_Expr + 1;
20647 Preanalyze_Spec_Expression (N, T);
20648 In_Assertion_Expr := In_Assertion_Expr - 1;
20649 end Preanalyze_Assert_Expression;
20651 -- ??? The variant below explicitly saves and restores all the flags,
20652 -- because it is impossible to compose the existing variety of
20653 -- Analyze/Resolve (and their wrappers, e.g. Preanalyze_Spec_Expression)
20654 -- to achieve the desired semantics.
20656 procedure Preanalyze_Assert_Expression (N : Node_Id) is
20657 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20658 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
20659 Save_Full_Analysis : constant Boolean := Full_Analysis;
20661 begin
20662 In_Assertion_Expr := In_Assertion_Expr + 1;
20663 In_Spec_Expression := True;
20664 Set_Must_Not_Freeze (N);
20665 Inside_Preanalysis_Without_Freezing :=
20666 Inside_Preanalysis_Without_Freezing + 1;
20667 Full_Analysis := False;
20668 Expander_Mode_Save_And_Set (False);
20670 if GNATprove_Mode then
20671 Analyze_And_Resolve (N);
20672 else
20673 Analyze_And_Resolve (N, Suppress => All_Checks);
20674 end if;
20676 Expander_Mode_Restore;
20677 Full_Analysis := Save_Full_Analysis;
20678 Inside_Preanalysis_Without_Freezing :=
20679 Inside_Preanalysis_Without_Freezing - 1;
20680 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
20681 In_Spec_Expression := Save_In_Spec_Expression;
20682 In_Assertion_Expr := In_Assertion_Expr - 1;
20683 end Preanalyze_Assert_Expression;
20685 -----------------------------------
20686 -- Preanalyze_Default_Expression --
20687 -----------------------------------
20689 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20690 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20691 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20693 begin
20694 In_Default_Expr := True;
20695 In_Spec_Expression := True;
20697 Preanalyze_With_Freezing_And_Resolve (N, T);
20699 In_Default_Expr := Save_In_Default_Expr;
20700 In_Spec_Expression := Save_In_Spec_Expression;
20701 end Preanalyze_Default_Expression;
20703 --------------------------------
20704 -- Preanalyze_Spec_Expression --
20705 --------------------------------
20707 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20708 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20709 begin
20710 In_Spec_Expression := True;
20711 Preanalyze_And_Resolve (N, T);
20712 In_Spec_Expression := Save_In_Spec_Expression;
20713 end Preanalyze_Spec_Expression;
20715 ----------------------------------------
20716 -- Prepare_Private_Subtype_Completion --
20717 ----------------------------------------
20719 procedure Prepare_Private_Subtype_Completion
20720 (Id : Entity_Id;
20721 Related_Nod : Node_Id)
20723 Id_B : constant Entity_Id := Base_Type (Id);
20724 Full_B : constant Entity_Id := Full_View (Id_B);
20725 Full : Entity_Id;
20727 begin
20728 if Present (Full_B) then
20730 -- The Base_Type is already completed, we can complete the subtype
20731 -- now. We have to create a new entity with the same name, Thus we
20732 -- can't use Create_Itype.
20734 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20735 Set_Is_Itype (Full);
20736 Set_Associated_Node_For_Itype (Full, Related_Nod);
20737 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20738 Set_Full_View (Id, Full);
20739 end if;
20741 -- The parent subtype may be private, but the base might not, in some
20742 -- nested instances. In that case, the subtype does not need to be
20743 -- exchanged. It would still be nice to make private subtypes and their
20744 -- bases consistent at all times ???
20746 if Is_Private_Type (Id_B) then
20747 Append_Elmt (Id, Private_Dependents (Id_B));
20748 end if;
20749 end Prepare_Private_Subtype_Completion;
20751 ---------------------------
20752 -- Process_Discriminants --
20753 ---------------------------
20755 procedure Process_Discriminants
20756 (N : Node_Id;
20757 Prev : Entity_Id := Empty)
20759 Elist : constant Elist_Id := New_Elmt_List;
20760 Id : Node_Id;
20761 Discr : Node_Id;
20762 Discr_Number : Uint;
20763 Discr_Type : Entity_Id;
20764 Default_Present : Boolean := False;
20765 Default_Not_Present : Boolean := False;
20767 begin
20768 -- A composite type other than an array type can have discriminants.
20769 -- On entry, the current scope is the composite type.
20771 -- The discriminants are initially entered into the scope of the type
20772 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20773 -- use, as explained at the end of this procedure.
20775 Discr := First (Discriminant_Specifications (N));
20776 while Present (Discr) loop
20777 Enter_Name (Defining_Identifier (Discr));
20779 -- For navigation purposes we add a reference to the discriminant
20780 -- in the entity for the type. If the current declaration is a
20781 -- completion, place references on the partial view. Otherwise the
20782 -- type is the current scope.
20784 if Present (Prev) then
20786 -- The references go on the partial view, if present. If the
20787 -- partial view has discriminants, the references have been
20788 -- generated already.
20790 if not Has_Discriminants (Prev) then
20791 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20792 end if;
20793 else
20794 Generate_Reference
20795 (Current_Scope, Defining_Identifier (Discr), 'd');
20796 end if;
20798 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20799 Check_Anonymous_Access_Component
20800 (Typ_Decl => N,
20801 Typ => Defining_Identifier (N),
20802 Prev => Prev,
20803 Comp_Def => Discr,
20804 Access_Def => Discriminant_Type (Discr));
20806 -- if Check_Anonymous_Access_Component replaced Discr then
20807 -- its Original_Node points to the old Discr and the access type
20808 -- for Discr_Type has already been created.
20810 if Is_Rewrite_Substitution (Discr) then
20811 Discr_Type := Etype (Discriminant_Type (Discr));
20812 else
20813 Discr_Type :=
20814 Access_Definition (Discr, Discriminant_Type (Discr));
20816 -- Ada 2005 (AI-254)
20818 if Present (Access_To_Subprogram_Definition
20819 (Discriminant_Type (Discr)))
20820 and then Protected_Present (Access_To_Subprogram_Definition
20821 (Discriminant_Type (Discr)))
20822 then
20823 Discr_Type :=
20824 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20825 end if;
20826 end if;
20827 else
20828 Find_Type (Discriminant_Type (Discr));
20829 Discr_Type := Etype (Discriminant_Type (Discr));
20831 if Error_Posted (Discriminant_Type (Discr)) then
20832 Discr_Type := Any_Type;
20833 end if;
20834 end if;
20836 -- Handling of discriminants that are access types
20838 if Is_Access_Type (Discr_Type) then
20840 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20841 -- limited record types
20843 if Ada_Version < Ada_2005 then
20844 Check_Access_Discriminant_Requires_Limited
20845 (Discr, Discriminant_Type (Discr));
20846 end if;
20848 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20849 Error_Msg_N
20850 ("(Ada 83) access discriminant not allowed", Discr);
20851 end if;
20853 -- If not access type, must be a discrete type
20855 elsif not Is_Discrete_Type (Discr_Type) then
20856 Error_Msg_N
20857 ("discriminants must have a discrete or access type",
20858 Discriminant_Type (Discr));
20859 end if;
20861 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20863 -- If a discriminant specification includes the assignment compound
20864 -- delimiter followed by an expression, the expression is the default
20865 -- expression of the discriminant; the default expression must be of
20866 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20867 -- a default expression, we do the special preanalysis, since this
20868 -- expression does not freeze (see section "Handling of Default and
20869 -- Per-Object Expressions" in spec of package Sem).
20871 if Present (Expression (Discr)) then
20872 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20874 -- Legaity checks
20876 if Nkind (N) = N_Formal_Type_Declaration then
20877 Error_Msg_N
20878 ("discriminant defaults not allowed for formal type",
20879 Expression (Discr));
20881 -- Flag an error for a tagged type with defaulted discriminants,
20882 -- excluding limited tagged types when compiling for Ada 2012
20883 -- (see AI05-0214).
20885 elsif Is_Tagged_Type (Current_Scope)
20886 and then (not Is_Limited_Type (Current_Scope)
20887 or else Ada_Version < Ada_2012)
20888 and then Comes_From_Source (N)
20889 then
20890 -- Note: see similar test in Check_Or_Process_Discriminants, to
20891 -- handle the (illegal) case of the completion of an untagged
20892 -- view with discriminants with defaults by a tagged full view.
20893 -- We skip the check if Discr does not come from source, to
20894 -- account for the case of an untagged derived type providing
20895 -- defaults for a renamed discriminant from a private untagged
20896 -- ancestor with a tagged full view (ACATS B460006).
20898 if Ada_Version >= Ada_2012 then
20899 Error_Msg_N
20900 ("discriminants of nonlimited tagged type cannot have"
20901 & " defaults",
20902 Expression (Discr));
20903 else
20904 Error_Msg_N
20905 ("discriminants of tagged type cannot have defaults",
20906 Expression (Discr));
20907 end if;
20909 else
20910 Default_Present := True;
20911 Append_Elmt (Expression (Discr), Elist);
20913 -- Tag the defining identifiers for the discriminants with
20914 -- their corresponding default expressions from the tree.
20916 Set_Discriminant_Default_Value
20917 (Defining_Identifier (Discr), Expression (Discr));
20918 end if;
20920 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20921 -- gets set unless we can be sure that no range check is required.
20923 if not Expander_Active
20924 and then not
20925 Is_In_Range
20926 (Expression (Discr), Discr_Type, Assume_Valid => True)
20927 then
20928 Set_Do_Range_Check (Expression (Discr));
20929 end if;
20931 -- No default discriminant value given
20933 else
20934 Default_Not_Present := True;
20935 end if;
20937 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20938 -- Discr_Type but with the null-exclusion attribute
20940 if Ada_Version >= Ada_2005 then
20942 -- Ada 2005 (AI-231): Static checks
20944 if Can_Never_Be_Null (Discr_Type) then
20945 Null_Exclusion_Static_Checks (Discr);
20947 elsif Is_Access_Type (Discr_Type)
20948 and then Null_Exclusion_Present (Discr)
20950 -- No need to check itypes because in their case this check
20951 -- was done at their point of creation
20953 and then not Is_Itype (Discr_Type)
20954 then
20955 if Can_Never_Be_Null (Discr_Type) then
20956 Error_Msg_NE
20957 ("`NOT NULL` not allowed (& already excludes null)",
20958 Discr,
20959 Discr_Type);
20960 end if;
20962 Set_Etype (Defining_Identifier (Discr),
20963 Create_Null_Excluding_Itype
20964 (T => Discr_Type,
20965 Related_Nod => Discr));
20967 -- Check for improper null exclusion if the type is otherwise
20968 -- legal for a discriminant.
20970 elsif Null_Exclusion_Present (Discr)
20971 and then Is_Discrete_Type (Discr_Type)
20972 then
20973 Error_Msg_N
20974 ("null exclusion can only apply to an access type", Discr);
20975 end if;
20977 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20978 -- can't have defaults. Synchronized types, or types that are
20979 -- explicitly limited are fine, but special tests apply to derived
20980 -- types in generics: in a generic body we have to assume the
20981 -- worst, and therefore defaults are not allowed if the parent is
20982 -- a generic formal private type (see ACATS B370001).
20984 if Is_Access_Type (Discr_Type) and then Default_Present then
20985 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20986 or else Is_Limited_Record (Current_Scope)
20987 or else Is_Concurrent_Type (Current_Scope)
20988 or else Is_Concurrent_Record_Type (Current_Scope)
20989 or else Ekind (Current_Scope) = E_Limited_Private_Type
20990 then
20991 if not Is_Derived_Type (Current_Scope)
20992 or else not Is_Generic_Type (Etype (Current_Scope))
20993 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20994 or else Limited_Present
20995 (Type_Definition (Parent (Current_Scope)))
20996 then
20997 null;
20999 else
21000 Error_Msg_N
21001 ("access discriminants of nonlimited types cannot "
21002 & "have defaults", Expression (Discr));
21003 end if;
21005 elsif Present (Expression (Discr)) then
21006 Error_Msg_N
21007 ("(Ada 2005) access discriminants of nonlimited types "
21008 & "cannot have defaults", Expression (Discr));
21009 end if;
21010 end if;
21011 end if;
21013 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
21014 -- This check is relevant only when SPARK_Mode is on as it is not a
21015 -- standard Ada legality rule. The only way for a discriminant to be
21016 -- effectively volatile is to have an effectively volatile type, so
21017 -- we check this directly, because the Ekind of Discr might not be
21018 -- set yet (to help preventing cascaded errors on derived types).
21020 if SPARK_Mode = On
21021 and then Is_Effectively_Volatile (Discr_Type)
21022 then
21023 Error_Msg_N ("discriminant cannot be volatile", Discr);
21024 end if;
21026 Next (Discr);
21027 end loop;
21029 -- An element list consisting of the default expressions of the
21030 -- discriminants is constructed in the above loop and used to set
21031 -- the Discriminant_Constraint attribute for the type. If an object
21032 -- is declared of this (record or task) type without any explicit
21033 -- discriminant constraint given, this element list will form the
21034 -- actual parameters for the corresponding initialization procedure
21035 -- for the type.
21037 Set_Discriminant_Constraint (Current_Scope, Elist);
21038 Set_Stored_Constraint (Current_Scope, No_Elist);
21040 -- Default expressions must be provided either for all or for none
21041 -- of the discriminants of a discriminant part. (RM 3.7.1)
21043 if Default_Present and then Default_Not_Present then
21044 Error_Msg_N
21045 ("incomplete specification of defaults for discriminants", N);
21046 end if;
21048 -- The use of the name of a discriminant is not allowed in default
21049 -- expressions of a discriminant part if the specification of the
21050 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
21052 -- To detect this, the discriminant names are entered initially with an
21053 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
21054 -- attempt to use a void entity (for example in an expression that is
21055 -- type-checked) produces the error message: premature usage. Now after
21056 -- completing the semantic analysis of the discriminant part, we can set
21057 -- the Ekind of all the discriminants appropriately.
21059 Discr := First (Discriminant_Specifications (N));
21060 Discr_Number := Uint_1;
21061 while Present (Discr) loop
21062 Id := Defining_Identifier (Discr);
21064 if Ekind (Id) = E_In_Parameter then
21065 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
21066 end if;
21068 Mutate_Ekind (Id, E_Discriminant);
21069 Set_Is_Not_Self_Hidden (Id);
21070 Reinit_Component_Location (Id);
21071 Reinit_Esize (Id);
21072 Set_Discriminant_Number (Id, Discr_Number);
21074 -- Make sure this is always set, even in illegal programs
21076 Set_Corresponding_Discriminant (Id, Empty);
21078 -- Initialize the Original_Record_Component to the entity itself.
21079 -- Inherit_Components will propagate the right value to
21080 -- discriminants in derived record types.
21082 Set_Original_Record_Component (Id, Id);
21084 -- Create the discriminal for the discriminant
21086 Build_Discriminal (Id);
21088 Next (Discr);
21089 Discr_Number := Discr_Number + 1;
21090 end loop;
21092 Set_Has_Discriminants (Current_Scope);
21093 end Process_Discriminants;
21095 -----------------------
21096 -- Process_Full_View --
21097 -----------------------
21099 -- WARNING: This routine manages Ghost regions. Return statements must be
21100 -- replaced by gotos which jump to the end of the routine and restore the
21101 -- Ghost mode.
21103 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
21104 procedure Collect_Implemented_Interfaces
21105 (Typ : Entity_Id;
21106 Ifaces : Elist_Id);
21107 -- Ada 2005: Gather all the interfaces that Typ directly or
21108 -- inherently implements. Duplicate entries are not added to
21109 -- the list Ifaces.
21111 ------------------------------------
21112 -- Collect_Implemented_Interfaces --
21113 ------------------------------------
21115 procedure Collect_Implemented_Interfaces
21116 (Typ : Entity_Id;
21117 Ifaces : Elist_Id)
21119 Iface : Entity_Id;
21120 Iface_Elmt : Elmt_Id;
21122 begin
21123 -- Abstract interfaces are only associated with tagged record types
21125 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
21126 return;
21127 end if;
21129 -- Recursively climb to the ancestors
21131 if Etype (Typ) /= Typ
21133 -- Protect the frontend against wrong cyclic declarations like:
21135 -- type B is new A with private;
21136 -- type C is new A with private;
21137 -- private
21138 -- type B is new C with null record;
21139 -- type C is new B with null record;
21141 and then Etype (Typ) /= Priv_T
21142 and then Etype (Typ) /= Full_T
21143 then
21144 -- Keep separate the management of private type declarations
21146 if Ekind (Typ) = E_Record_Type_With_Private then
21148 -- Handle the following illegal usage:
21149 -- type Private_Type is tagged private;
21150 -- private
21151 -- type Private_Type is new Type_Implementing_Iface;
21153 if Present (Full_View (Typ))
21154 and then Etype (Typ) /= Full_View (Typ)
21155 then
21156 if Is_Interface (Etype (Typ)) then
21157 Append_Unique_Elmt (Etype (Typ), Ifaces);
21158 end if;
21160 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21161 end if;
21163 -- Non-private types
21165 else
21166 if Is_Interface (Etype (Typ)) then
21167 Append_Unique_Elmt (Etype (Typ), Ifaces);
21168 end if;
21170 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
21171 end if;
21172 end if;
21174 -- Handle entities in the list of abstract interfaces
21176 if Present (Interfaces (Typ)) then
21177 Iface_Elmt := First_Elmt (Interfaces (Typ));
21178 while Present (Iface_Elmt) loop
21179 Iface := Node (Iface_Elmt);
21181 pragma Assert (Is_Interface (Iface));
21183 if not Contain_Interface (Iface, Ifaces) then
21184 Append_Elmt (Iface, Ifaces);
21185 Collect_Implemented_Interfaces (Iface, Ifaces);
21186 end if;
21188 Next_Elmt (Iface_Elmt);
21189 end loop;
21190 end if;
21191 end Collect_Implemented_Interfaces;
21193 -- Local variables
21195 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
21196 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
21197 -- Save the Ghost-related attributes to restore on exit
21199 Full_Indic : Node_Id;
21200 Full_Parent : Entity_Id;
21201 Priv_Parent : Entity_Id;
21203 -- Start of processing for Process_Full_View
21205 begin
21206 Mark_And_Set_Ghost_Completion (N, Priv_T);
21208 -- First some sanity checks that must be done after semantic
21209 -- decoration of the full view and thus cannot be placed with other
21210 -- similar checks in Find_Type_Name
21212 if not Is_Limited_Type (Priv_T)
21213 and then (Is_Limited_Type (Full_T)
21214 or else Is_Limited_Composite (Full_T))
21215 then
21216 if In_Instance then
21217 null;
21218 else
21219 Error_Msg_N
21220 ("completion of nonlimited type cannot be limited", Full_T);
21221 Explain_Limited_Type (Full_T, Full_T);
21222 end if;
21224 elsif Is_Abstract_Type (Full_T)
21225 and then not Is_Abstract_Type (Priv_T)
21226 then
21227 Error_Msg_N
21228 ("completion of nonabstract type cannot be abstract", Full_T);
21230 elsif Is_Tagged_Type (Priv_T)
21231 and then Is_Limited_Type (Priv_T)
21232 and then not Is_Limited_Type (Full_T)
21233 then
21234 -- If pragma CPP_Class was applied to the private declaration
21235 -- propagate the limitedness to the full-view
21237 if Is_CPP_Class (Priv_T) then
21238 Set_Is_Limited_Record (Full_T);
21240 -- GNAT allow its own definition of Limited_Controlled to disobey
21241 -- this rule in order in ease the implementation. This test is safe
21242 -- because Root_Controlled is defined in a child of System that
21243 -- normal programs are not supposed to use.
21245 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
21246 Set_Is_Limited_Composite (Full_T);
21247 else
21248 Error_Msg_N
21249 ("completion of limited tagged type must be limited", Full_T);
21250 end if;
21252 elsif Is_Generic_Type (Priv_T) then
21253 Error_Msg_N ("generic type cannot have a completion", Full_T);
21254 end if;
21256 -- Check that ancestor interfaces of private and full views are
21257 -- consistent. We omit this check for synchronized types because
21258 -- they are performed on the corresponding record type when frozen.
21260 if Ada_Version >= Ada_2005
21261 and then Is_Tagged_Type (Priv_T)
21262 and then Is_Tagged_Type (Full_T)
21263 and then not Is_Concurrent_Type (Full_T)
21264 then
21265 declare
21266 Iface : Entity_Id;
21267 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
21268 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
21270 begin
21271 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
21272 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
21274 -- Ada 2005 (AI-251): The partial view shall be a descendant of
21275 -- an interface type if and only if the full type is descendant
21276 -- of the interface type (AARM 7.3 (7.3/2)).
21278 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
21280 if Present (Iface) then
21281 Error_Msg_NE
21282 ("interface in partial view& not implemented by full type "
21283 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21284 end if;
21286 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
21288 if Present (Iface) then
21289 Error_Msg_NE
21290 ("interface & not implemented by partial view "
21291 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
21292 end if;
21293 end;
21294 end if;
21296 if Is_Tagged_Type (Priv_T)
21297 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21298 and then Is_Derived_Type (Full_T)
21299 then
21300 Priv_Parent := Etype (Priv_T);
21302 -- The full view of a private extension may have been transformed
21303 -- into an unconstrained derived type declaration and a subtype
21304 -- declaration (see build_derived_record_type for details).
21306 if Nkind (N) = N_Subtype_Declaration then
21307 Full_Indic := Subtype_Indication (N);
21308 Full_Parent := Etype (Base_Type (Full_T));
21309 else
21310 Full_Indic := Subtype_Indication (Type_Definition (N));
21311 Full_Parent := Etype (Full_T);
21312 end if;
21314 -- Check that the parent type of the full type is a descendant of
21315 -- the ancestor subtype given in the private extension. If either
21316 -- entity has an Etype equal to Any_Type then we had some previous
21317 -- error situation [7.3(8)].
21319 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
21320 goto Leave;
21322 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
21323 -- any order. Therefore we don't have to check that its parent must
21324 -- be a descendant of the parent of the private type declaration.
21326 elsif Is_Interface (Priv_Parent)
21327 and then Is_Interface (Full_Parent)
21328 then
21329 null;
21331 -- Ada 2005 (AI-251): If the parent of the private type declaration
21332 -- is an interface there is no need to check that it is an ancestor
21333 -- of the associated full type declaration. The required tests for
21334 -- this case are performed by Build_Derived_Record_Type.
21336 elsif not Is_Interface (Base_Type (Priv_Parent))
21337 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
21338 then
21339 Error_Msg_N
21340 ("parent of full type must descend from parent of private "
21341 & "extension", Full_Indic);
21343 -- First check a formal restriction, and then proceed with checking
21344 -- Ada rules. Since the formal restriction is not a serious error, we
21345 -- don't prevent further error detection for this check, hence the
21346 -- ELSE.
21348 else
21349 -- Check the rules of 7.3(10): if the private extension inherits
21350 -- known discriminants, then the full type must also inherit those
21351 -- discriminants from the same (ancestor) type, and the parent
21352 -- subtype of the full type must be constrained if and only if
21353 -- the ancestor subtype of the private extension is constrained.
21355 if No (Discriminant_Specifications (Parent (Priv_T)))
21356 and then not Has_Unknown_Discriminants (Priv_T)
21357 and then Has_Discriminants (Base_Type (Priv_Parent))
21358 then
21359 declare
21360 Priv_Indic : constant Node_Id :=
21361 Subtype_Indication (Parent (Priv_T));
21363 Priv_Constr : constant Boolean :=
21364 Is_Constrained (Priv_Parent)
21365 or else
21366 Nkind (Priv_Indic) = N_Subtype_Indication
21367 or else
21368 Is_Constrained (Entity (Priv_Indic));
21370 Full_Constr : constant Boolean :=
21371 Is_Constrained (Full_Parent)
21372 or else
21373 Nkind (Full_Indic) = N_Subtype_Indication
21374 or else
21375 Is_Constrained (Entity (Full_Indic));
21377 Priv_Discr : Entity_Id;
21378 Full_Discr : Entity_Id;
21380 begin
21381 Priv_Discr := First_Discriminant (Priv_Parent);
21382 Full_Discr := First_Discriminant (Full_Parent);
21383 while Present (Priv_Discr) and then Present (Full_Discr) loop
21384 if Original_Record_Component (Priv_Discr) =
21385 Original_Record_Component (Full_Discr)
21386 or else
21387 Corresponding_Discriminant (Priv_Discr) =
21388 Corresponding_Discriminant (Full_Discr)
21389 then
21390 null;
21391 else
21392 exit;
21393 end if;
21395 Next_Discriminant (Priv_Discr);
21396 Next_Discriminant (Full_Discr);
21397 end loop;
21399 if Present (Priv_Discr) or else Present (Full_Discr) then
21400 Error_Msg_N
21401 ("full view must inherit discriminants of the parent "
21402 & "type used in the private extension", Full_Indic);
21404 elsif Priv_Constr and then not Full_Constr then
21405 Error_Msg_N
21406 ("parent subtype of full type must be constrained",
21407 Full_Indic);
21409 elsif Full_Constr and then not Priv_Constr then
21410 Error_Msg_N
21411 ("parent subtype of full type must be unconstrained",
21412 Full_Indic);
21413 end if;
21414 end;
21416 -- Check the rules of 7.3(12): if a partial view has neither
21417 -- known or unknown discriminants, then the full type
21418 -- declaration shall define a definite subtype.
21420 elsif not Has_Unknown_Discriminants (Priv_T)
21421 and then not Has_Discriminants (Priv_T)
21422 and then not Is_Constrained (Full_T)
21423 then
21424 Error_Msg_N
21425 ("full view must define a constrained type if partial view "
21426 & "has no discriminants", Full_T);
21427 end if;
21429 -- Do we implement the following properly???
21430 -- If the ancestor subtype of a private extension has constrained
21431 -- discriminants, then the parent subtype of the full view shall
21432 -- impose a statically matching constraint on those discriminants
21433 -- [7.3(13)].
21434 end if;
21436 else
21437 -- For untagged types, verify that a type without discriminants is
21438 -- not completed with an unconstrained type. A separate error message
21439 -- is produced if the full type has defaulted discriminants.
21441 if Is_Definite_Subtype (Priv_T)
21442 and then not Is_Definite_Subtype (Full_T)
21443 then
21444 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21445 Error_Msg_NE
21446 ("full view of& not compatible with declaration#",
21447 Full_T, Priv_T);
21449 if not Is_Tagged_Type (Full_T) then
21450 Error_Msg_N
21451 ("\one is constrained, the other unconstrained", Full_T);
21452 end if;
21453 end if;
21454 end if;
21456 -- AI-419: verify that the use of "limited" is consistent
21458 declare
21459 Orig_Decl : constant Node_Id := Original_Node (N);
21461 begin
21462 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21463 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21464 and then Nkind
21465 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21466 then
21467 if not Limited_Present (Parent (Priv_T))
21468 and then not Synchronized_Present (Parent (Priv_T))
21469 and then Limited_Present (Type_Definition (Orig_Decl))
21470 then
21471 Error_Msg_N
21472 ("full view of non-limited extension cannot be limited", N);
21474 -- Conversely, if the partial view carries the limited keyword,
21475 -- the full view must as well, even if it may be redundant.
21477 elsif Limited_Present (Parent (Priv_T))
21478 and then not Limited_Present (Type_Definition (Orig_Decl))
21479 then
21480 Error_Msg_N
21481 ("full view of limited extension must be explicitly limited",
21483 end if;
21484 end if;
21485 end;
21487 -- Ada 2005 (AI-443): A synchronized private extension must be
21488 -- completed by a task or protected type.
21490 if Ada_Version >= Ada_2005
21491 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21492 and then Synchronized_Present (Parent (Priv_T))
21493 and then not Is_Concurrent_Type (Full_T)
21494 then
21495 Error_Msg_N ("full view of synchronized extension must " &
21496 "be synchronized type", N);
21497 end if;
21499 -- Ada 2005 AI-363: if the full view has discriminants with
21500 -- defaults, it is illegal to declare constrained access subtypes
21501 -- whose designated type is the current type. This allows objects
21502 -- of the type that are declared in the heap to be unconstrained.
21504 if not Has_Unknown_Discriminants (Priv_T)
21505 and then not Has_Discriminants (Priv_T)
21506 and then Has_Defaulted_Discriminants (Full_T)
21507 then
21508 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21509 Set_Has_Constrained_Partial_View (Priv_T);
21510 end if;
21512 -- Create a full declaration for all its subtypes recorded in
21513 -- Private_Dependents and swap them similarly to the base type. These
21514 -- are subtypes that have been define before the full declaration of
21515 -- the private type. We also swap the entry in Private_Dependents list
21516 -- so we can properly restore the private view on exit from the scope.
21518 declare
21519 Priv_Elmt : Elmt_Id;
21520 Priv_Scop : Entity_Id;
21521 Priv : Entity_Id;
21522 Full : Entity_Id;
21524 begin
21525 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21526 while Present (Priv_Elmt) loop
21527 Priv := Node (Priv_Elmt);
21528 Priv_Scop := Scope (Priv);
21530 if Ekind (Priv) in E_Private_Subtype
21531 | E_Limited_Private_Subtype
21532 | E_Record_Subtype_With_Private
21533 then
21534 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21535 Set_Is_Itype (Full);
21536 Set_Parent (Full, Parent (Priv));
21537 Set_Associated_Node_For_Itype (Full, N);
21539 -- Now we need to complete the private subtype, but since the
21540 -- base type has already been swapped, we must also swap the
21541 -- subtypes (and thus, reverse the arguments in the call to
21542 -- Complete_Private_Subtype). Also note that we may need to
21543 -- re-establish the scope of the private subtype.
21545 Copy_And_Swap (Priv, Full);
21547 if not In_Open_Scopes (Priv_Scop) then
21548 Push_Scope (Priv_Scop);
21550 else
21551 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21553 Priv_Scop := Empty;
21554 end if;
21556 Complete_Private_Subtype (Full, Priv, Full_T, N);
21557 Set_Full_View (Full, Priv);
21559 if Present (Priv_Scop) then
21560 Pop_Scope;
21561 end if;
21563 Replace_Elmt (Priv_Elmt, Full);
21564 end if;
21566 Next_Elmt (Priv_Elmt);
21567 end loop;
21568 end;
21570 declare
21571 Disp_Typ : Entity_Id;
21572 Full_List : Elist_Id;
21573 Prim : Entity_Id;
21574 Prim_Elmt : Elmt_Id;
21575 Priv_List : Elist_Id;
21577 function Contains
21578 (E : Entity_Id;
21579 L : Elist_Id) return Boolean;
21580 -- Determine whether list L contains element E
21582 --------------
21583 -- Contains --
21584 --------------
21586 function Contains
21587 (E : Entity_Id;
21588 L : Elist_Id) return Boolean
21590 List_Elmt : Elmt_Id;
21592 begin
21593 List_Elmt := First_Elmt (L);
21594 while Present (List_Elmt) loop
21595 if Node (List_Elmt) = E then
21596 return True;
21597 end if;
21599 Next_Elmt (List_Elmt);
21600 end loop;
21602 return False;
21603 end Contains;
21605 -- Start of processing
21607 begin
21608 -- If the private view was tagged, copy the new primitive operations
21609 -- from the private view to the full view.
21611 if Is_Tagged_Type (Full_T) then
21612 if Is_Tagged_Type (Priv_T) then
21613 Priv_List := Primitive_Operations (Priv_T);
21614 Prim_Elmt := First_Elmt (Priv_List);
21616 -- In the case of a concurrent type completing a private tagged
21617 -- type, primitives may have been declared in between the two
21618 -- views. These subprograms need to be wrapped the same way
21619 -- entries and protected procedures are handled because they
21620 -- cannot be directly shared by the two views.
21622 if Is_Concurrent_Type (Full_T) then
21623 declare
21624 Conc_Typ : constant Entity_Id :=
21625 Corresponding_Record_Type (Full_T);
21626 Curr_Nod : Node_Id := Parent (Conc_Typ);
21627 Wrap_Spec : Node_Id;
21629 begin
21630 while Present (Prim_Elmt) loop
21631 Prim := Node (Prim_Elmt);
21633 if Comes_From_Source (Prim)
21634 and then not Is_Abstract_Subprogram (Prim)
21635 then
21636 Wrap_Spec :=
21637 Make_Subprogram_Declaration (Sloc (Prim),
21638 Specification =>
21639 Build_Wrapper_Spec
21640 (Subp_Id => Prim,
21641 Obj_Typ => Conc_Typ,
21642 Formals =>
21643 Parameter_Specifications
21644 (Parent (Prim))));
21646 Insert_After (Curr_Nod, Wrap_Spec);
21647 Curr_Nod := Wrap_Spec;
21649 Analyze (Wrap_Spec);
21651 -- Remove the wrapper from visibility to avoid
21652 -- spurious conflict with the wrapped entity.
21654 Set_Is_Immediately_Visible
21655 (Defining_Entity (Specification (Wrap_Spec)),
21656 False);
21657 end if;
21659 Next_Elmt (Prim_Elmt);
21660 end loop;
21662 goto Leave;
21663 end;
21665 -- For nonconcurrent types, transfer explicit primitives, but
21666 -- omit those inherited from the parent of the private view
21667 -- since they will be re-inherited later on.
21669 else
21670 Full_List := Primitive_Operations (Full_T);
21671 while Present (Prim_Elmt) loop
21672 Prim := Node (Prim_Elmt);
21674 if Comes_From_Source (Prim)
21675 and then not Contains (Prim, Full_List)
21676 then
21677 Append_Elmt (Prim, Full_List);
21678 end if;
21680 Next_Elmt (Prim_Elmt);
21681 end loop;
21682 end if;
21684 -- Untagged private view
21686 else
21687 Full_List := Primitive_Operations (Full_T);
21689 -- In this case the partial view is untagged, so here we locate
21690 -- all of the earlier primitives that need to be treated as
21691 -- dispatching (those that appear between the two views). Note
21692 -- that these additional operations must all be new operations
21693 -- (any earlier operations that override inherited operations
21694 -- of the full view will already have been inserted in the
21695 -- primitives list, marked by Check_Operation_From_Private_View
21696 -- as dispatching. Note that implicit "/=" operators are
21697 -- excluded from being added to the primitives list since they
21698 -- shouldn't be treated as dispatching (tagged "/=" is handled
21699 -- specially).
21701 Prim := Next_Entity (Full_T);
21702 while Present (Prim) and then Prim /= Priv_T loop
21703 if Ekind (Prim) in E_Procedure | E_Function then
21704 Disp_Typ := Find_Dispatching_Type (Prim);
21706 if Disp_Typ = Full_T
21707 and then (Chars (Prim) /= Name_Op_Ne
21708 or else Comes_From_Source (Prim))
21709 then
21710 Check_Controlling_Formals (Full_T, Prim);
21712 if Is_Suitable_Primitive (Prim)
21713 and then not Is_Dispatching_Operation (Prim)
21714 then
21715 Append_Elmt (Prim, Full_List);
21716 Set_Is_Dispatching_Operation (Prim);
21717 Set_DT_Position_Value (Prim, No_Uint);
21718 end if;
21720 elsif Is_Dispatching_Operation (Prim)
21721 and then Disp_Typ /= Full_T
21722 then
21723 -- Verify that it is not otherwise controlled by a
21724 -- formal or a return value of type T.
21726 Check_Controlling_Formals (Disp_Typ, Prim);
21727 end if;
21728 end if;
21730 Next_Entity (Prim);
21731 end loop;
21732 end if;
21734 -- For the tagged case, the two views can share the same primitive
21735 -- operations list and the same class-wide type. Update attributes
21736 -- of the class-wide type which depend on the full declaration.
21738 if Is_Tagged_Type (Priv_T) then
21739 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21740 Set_Class_Wide_Type
21741 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21743 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21744 end if;
21746 -- For untagged types, copy the primitives across from the private
21747 -- view to the full view, for support of prefixed calls when
21748 -- extensions are enabled, and better error messages otherwise.
21750 else
21751 Priv_List := Primitive_Operations (Priv_T);
21752 Prim_Elmt := First_Elmt (Priv_List);
21754 Full_List := Primitive_Operations (Full_T);
21755 while Present (Prim_Elmt) loop
21756 Prim := Node (Prim_Elmt);
21757 Append_Elmt (Prim, Full_List);
21758 Next_Elmt (Prim_Elmt);
21759 end loop;
21760 end if;
21761 end;
21763 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21765 if Known_To_Have_Preelab_Init (Priv_T) then
21767 -- Case where there is a pragma Preelaborable_Initialization. We
21768 -- always allow this in predefined units, which is cheating a bit,
21769 -- but it means we don't have to struggle to meet the requirements in
21770 -- the RM for having Preelaborable Initialization. Otherwise we
21771 -- require that the type meets the RM rules. But we can't check that
21772 -- yet, because of the rule about overriding Initialize, so we simply
21773 -- set a flag that will be checked at freeze time.
21775 if not In_Predefined_Unit (Full_T) then
21776 Set_Must_Have_Preelab_Init (Full_T);
21777 end if;
21778 end if;
21780 -- If pragma CPP_Class was applied to the private type declaration,
21781 -- propagate it now to the full type declaration.
21783 if Is_CPP_Class (Priv_T) then
21784 Set_Is_CPP_Class (Full_T);
21785 Set_Convention (Full_T, Convention_CPP);
21787 -- Check that components of imported CPP types do not have default
21788 -- expressions.
21790 Check_CPP_Type_Has_No_Defaults (Full_T);
21791 end if;
21793 -- If the private view has user specified stream attributes, then so has
21794 -- the full view.
21796 -- Why the test, how could these flags be already set in Full_T ???
21798 if Has_Specified_Stream_Read (Priv_T) then
21799 Set_Has_Specified_Stream_Read (Full_T);
21800 end if;
21802 if Has_Specified_Stream_Write (Priv_T) then
21803 Set_Has_Specified_Stream_Write (Full_T);
21804 end if;
21806 if Has_Specified_Stream_Input (Priv_T) then
21807 Set_Has_Specified_Stream_Input (Full_T);
21808 end if;
21810 if Has_Specified_Stream_Output (Priv_T) then
21811 Set_Has_Specified_Stream_Output (Full_T);
21812 end if;
21814 -- Propagate Default_Initial_Condition-related attributes from the
21815 -- partial view to the full view.
21817 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21819 -- And to the underlying full view, if any
21821 if Is_Private_Type (Full_T)
21822 and then Present (Underlying_Full_View (Full_T))
21823 then
21824 Propagate_DIC_Attributes
21825 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21826 end if;
21828 -- Propagate invariant-related attributes from the partial view to the
21829 -- full view.
21831 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21833 -- And to the underlying full view, if any
21835 if Is_Private_Type (Full_T)
21836 and then Present (Underlying_Full_View (Full_T))
21837 then
21838 Propagate_Invariant_Attributes
21839 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21840 end if;
21842 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21843 -- in the full view without advertising the inheritance in the partial
21844 -- view. This can only occur when the partial view has no parent type
21845 -- and the full view has an interface as a parent. Any other scenarios
21846 -- are illegal because implemented interfaces must match between the
21847 -- two views.
21849 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21850 declare
21851 Full_Par : constant Entity_Id := Etype (Full_T);
21852 Priv_Par : constant Entity_Id := Etype (Priv_T);
21854 begin
21855 if not Is_Interface (Priv_Par)
21856 and then Is_Interface (Full_Par)
21857 and then Has_Inheritable_Invariants (Full_Par)
21858 then
21859 Error_Msg_N
21860 ("hidden inheritance of class-wide type invariants not "
21861 & "allowed", N);
21862 end if;
21863 end;
21864 end if;
21866 -- Propagate predicates to full type, and predicate function if already
21867 -- defined. It is not clear that this can actually happen? the partial
21868 -- view cannot be frozen yet, and the predicate function has not been
21869 -- built. Still it is a cheap check and seems safer to make it.
21871 Propagate_Predicate_Attributes (Full_T, Priv_T);
21873 if Is_Private_Type (Full_T)
21874 and then Present (Underlying_Full_View (Full_T))
21875 then
21876 Propagate_Predicate_Attributes
21877 (Underlying_Full_View (Full_T), Priv_T);
21878 end if;
21880 <<Leave>>
21881 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21882 end Process_Full_View;
21884 -----------------------------------
21885 -- Process_Incomplete_Dependents --
21886 -----------------------------------
21888 procedure Process_Incomplete_Dependents
21889 (N : Node_Id;
21890 Full_T : Entity_Id;
21891 Inc_T : Entity_Id)
21893 Inc_Elmt : Elmt_Id;
21894 Priv_Dep : Entity_Id;
21895 New_Subt : Entity_Id;
21897 Disc_Constraint : Elist_Id;
21899 begin
21900 if No (Private_Dependents (Inc_T)) then
21901 return;
21902 end if;
21904 -- Itypes that may be generated by the completion of an incomplete
21905 -- subtype are not used by the back-end and not attached to the tree.
21906 -- They are created only for constraint-checking purposes.
21908 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21909 while Present (Inc_Elmt) loop
21910 Priv_Dep := Node (Inc_Elmt);
21912 if Ekind (Priv_Dep) = E_Subprogram_Type then
21914 -- An Access_To_Subprogram type may have a return type or a
21915 -- parameter type that is incomplete. Replace with the full view.
21917 if Etype (Priv_Dep) = Inc_T then
21918 Set_Etype (Priv_Dep, Full_T);
21919 end if;
21921 declare
21922 Formal : Entity_Id;
21924 begin
21925 Formal := First_Formal (Priv_Dep);
21926 while Present (Formal) loop
21927 if Etype (Formal) = Inc_T then
21928 Set_Etype (Formal, Full_T);
21929 end if;
21931 Next_Formal (Formal);
21932 end loop;
21933 end;
21935 elsif Is_Overloadable (Priv_Dep) then
21937 -- If a subprogram in the incomplete dependents list is primitive
21938 -- for a tagged full type then mark it as a dispatching operation,
21939 -- check whether it overrides an inherited subprogram, and check
21940 -- restrictions on its controlling formals. Note that a protected
21941 -- operation is never dispatching: only its wrapper operation
21942 -- (which has convention Ada) is.
21944 if Is_Tagged_Type (Full_T)
21945 and then Is_Primitive (Priv_Dep)
21946 and then Convention (Priv_Dep) /= Convention_Protected
21947 then
21948 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21949 Set_Is_Dispatching_Operation (Priv_Dep);
21950 Check_Controlling_Formals (Full_T, Priv_Dep);
21951 end if;
21953 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21955 -- Can happen during processing of a body before the completion
21956 -- of a TA type. Ignore, because spec is also on dependent list.
21958 return;
21960 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21961 -- corresponding subtype of the full view.
21963 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21964 and then Comes_From_Source (Priv_Dep)
21965 then
21966 Set_Subtype_Indication
21967 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21968 Reinit_Field_To_Zero
21969 (Priv_Dep, F_Private_Dependents,
21970 Old_Ekind => E_Incomplete_Subtype);
21971 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21972 Set_Etype (Priv_Dep, Full_T);
21973 Set_Analyzed (Parent (Priv_Dep), False);
21975 -- Reanalyze the declaration, suppressing the call to Enter_Name
21976 -- to avoid duplicate names.
21978 Analyze_Subtype_Declaration
21979 (N => Parent (Priv_Dep),
21980 Skip => True);
21982 -- Dependent is a subtype
21984 else
21985 -- We build a new subtype indication using the full view of the
21986 -- incomplete parent. The discriminant constraints have been
21987 -- elaborated already at the point of the subtype declaration.
21989 New_Subt := Create_Itype (E_Void, N);
21991 if Has_Discriminants (Full_T) then
21992 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21993 else
21994 Disc_Constraint := No_Elist;
21995 end if;
21997 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21998 Set_Full_View (Priv_Dep, New_Subt);
21999 end if;
22001 Next_Elmt (Inc_Elmt);
22002 end loop;
22003 end Process_Incomplete_Dependents;
22005 --------------------------------
22006 -- Process_Range_Expr_In_Decl --
22007 --------------------------------
22009 procedure Process_Range_Expr_In_Decl
22010 (R : Node_Id;
22011 T : Entity_Id;
22012 Subtyp : Entity_Id := Empty;
22013 Check_List : List_Id := No_List)
22015 Lo, Hi : Node_Id;
22016 R_Checks : Check_Result;
22017 Insert_Node : Node_Id;
22018 Def_Id : Entity_Id;
22020 begin
22021 Analyze_And_Resolve (R, Base_Type (T));
22023 if Nkind (R) = N_Range then
22024 Lo := Low_Bound (R);
22025 Hi := High_Bound (R);
22027 -- Validity checks on the range of a quantified expression are
22028 -- delayed until the construct is transformed into a loop.
22030 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
22031 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
22032 then
22033 null;
22035 -- We need to ensure validity of the bounds here, because if we
22036 -- go ahead and do the expansion, then the expanded code will get
22037 -- analyzed with range checks suppressed and we miss the check.
22039 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
22040 -- the temporaries generated by routine Remove_Side_Effects by means
22041 -- of validity checks must use the same names. When a range appears
22042 -- in the parent of a generic, the range is processed with checks
22043 -- disabled as part of the generic context and with checks enabled
22044 -- for code generation purposes. This leads to link issues as the
22045 -- generic contains references to xxx_FIRST/_LAST, but the inlined
22046 -- template sees the temporaries generated by Remove_Side_Effects.
22048 else
22049 Validity_Check_Range (R, Subtyp);
22050 end if;
22052 -- If there were errors in the declaration, try and patch up some
22053 -- common mistakes in the bounds. The cases handled are literals
22054 -- which are Integer where the expected type is Real and vice versa.
22055 -- These corrections allow the compilation process to proceed further
22056 -- along since some basic assumptions of the format of the bounds
22057 -- are guaranteed.
22059 if Etype (R) = Any_Type then
22060 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
22061 Rewrite (Lo,
22062 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
22064 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
22065 Rewrite (Hi,
22066 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
22068 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
22069 Rewrite (Lo,
22070 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
22072 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
22073 Rewrite (Hi,
22074 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
22075 end if;
22077 Set_Etype (Lo, T);
22078 Set_Etype (Hi, T);
22079 end if;
22081 -- If the bounds of the range have been mistakenly given as string
22082 -- literals (perhaps in place of character literals), then an error
22083 -- has already been reported, but we rewrite the string literal as a
22084 -- bound of the range's type to avoid blowups in later processing
22085 -- that looks at static values.
22087 if Nkind (Lo) = N_String_Literal then
22088 Rewrite (Lo,
22089 Make_Attribute_Reference (Sloc (Lo),
22090 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
22091 Attribute_Name => Name_First));
22092 Analyze_And_Resolve (Lo);
22093 end if;
22095 if Nkind (Hi) = N_String_Literal then
22096 Rewrite (Hi,
22097 Make_Attribute_Reference (Sloc (Hi),
22098 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
22099 Attribute_Name => Name_First));
22100 Analyze_And_Resolve (Hi);
22101 end if;
22103 -- If bounds aren't scalar at this point then exit, avoiding
22104 -- problems with further processing of the range in this procedure.
22106 if not Is_Scalar_Type (Etype (Lo)) then
22107 return;
22108 end if;
22110 -- Resolve (actually Sem_Eval) has checked that the bounds are in
22111 -- then range of the base type. Here we check whether the bounds
22112 -- are in the range of the subtype itself. Note that if the bounds
22113 -- represent the null range the Constraint_Error exception should
22114 -- not be raised.
22116 -- Capture values of bounds and generate temporaries for them
22117 -- if needed, before applying checks, since checks may cause
22118 -- duplication of the expression without forcing evaluation.
22120 -- The forced evaluation removes side effects from expressions,
22121 -- which should occur also in GNATprove mode. Otherwise, we end up
22122 -- with unexpected insertions of actions at places where this is
22123 -- not supposed to occur, e.g. on default parameters of a call.
22125 if Expander_Active or GNATprove_Mode then
22127 -- Call Force_Evaluation to create declarations as needed
22128 -- to deal with side effects, and also create typ_FIRST/LAST
22129 -- entities for bounds if we have a subtype name.
22131 -- Note: we do this transformation even if expansion is not
22132 -- active if we are in GNATprove_Mode since the transformation
22133 -- is in general required to ensure that the resulting tree has
22134 -- proper Ada semantics.
22136 Force_Evaluation
22137 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
22138 Force_Evaluation
22139 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
22140 end if;
22142 -- We use a flag here instead of suppressing checks on the type
22143 -- because the type we check against isn't necessarily the place
22144 -- where we put the check.
22146 R_Checks := Get_Range_Checks (R, T);
22148 -- Look up tree to find an appropriate insertion point. We can't
22149 -- just use insert_actions because later processing depends on
22150 -- the insertion node. Prior to Ada 2012 the insertion point could
22151 -- only be a declaration or a loop, but quantified expressions can
22152 -- appear within any context in an expression, and the insertion
22153 -- point can be any statement, pragma, or declaration.
22155 Insert_Node := Parent (R);
22156 while Present (Insert_Node) loop
22157 exit when
22158 Nkind (Insert_Node) in N_Declaration
22159 and then
22160 Nkind (Insert_Node) not in N_Component_Declaration
22161 | N_Loop_Parameter_Specification
22162 | N_Function_Specification
22163 | N_Procedure_Specification;
22165 exit when Nkind (Insert_Node) in
22166 N_Later_Decl_Item |
22167 N_Statement_Other_Than_Procedure_Call |
22168 N_Procedure_Call_Statement |
22169 N_Pragma;
22171 Insert_Node := Parent (Insert_Node);
22172 end loop;
22174 if Present (Insert_Node) then
22176 -- Case of loop statement. Verify that the range is part of the
22177 -- subtype indication of the iteration scheme.
22179 if Nkind (Insert_Node) = N_Loop_Statement then
22180 declare
22181 Indic : Node_Id;
22183 begin
22184 Indic := Parent (R);
22185 while Present (Indic)
22186 and then Nkind (Indic) /= N_Subtype_Indication
22187 loop
22188 Indic := Parent (Indic);
22189 end loop;
22191 if Present (Indic) then
22192 Def_Id := Etype (Subtype_Mark (Indic));
22194 Insert_Range_Checks
22195 (R_Checks,
22196 Insert_Node,
22197 Def_Id,
22198 Sloc (Insert_Node),
22199 Do_Before => True);
22200 end if;
22201 end;
22203 -- Case of declarations. If the declaration is for a type and
22204 -- involves discriminants, the checks are premature at the
22205 -- declaration point and need to wait for the expansion of the
22206 -- initialization procedure, which will pass in the list to put
22207 -- them on; otherwise, the checks are done at the declaration
22208 -- point and there is no need to do them again in the
22209 -- initialization procedure.
22211 elsif Nkind (Insert_Node) in N_Declaration then
22212 Def_Id := Defining_Identifier (Insert_Node);
22214 if (Ekind (Def_Id) = E_Record_Type
22215 and then Depends_On_Discriminant (R))
22216 or else
22217 (Ekind (Def_Id) = E_Protected_Type
22218 and then Has_Discriminants (Def_Id))
22219 then
22220 if Present (Check_List) then
22221 Append_Range_Checks
22222 (R_Checks,
22223 Check_List, Def_Id, Sloc (Insert_Node));
22224 end if;
22226 else
22227 if No (Check_List) then
22228 Insert_Range_Checks
22229 (R_Checks,
22230 Insert_Node, Def_Id, Sloc (Insert_Node));
22231 end if;
22232 end if;
22234 -- Case of statements. Drop the checks, as the range appears in
22235 -- the context of a quantified expression. Insertion will take
22236 -- place when expression is expanded.
22238 else
22239 null;
22240 end if;
22241 end if;
22243 -- Case of other than an explicit N_Range node
22245 -- The forced evaluation removes side effects from expressions, which
22246 -- should occur also in GNATprove mode. Otherwise, we end up with
22247 -- unexpected insertions of actions at places where this is not
22248 -- supposed to occur, e.g. on default parameters of a call.
22250 elsif Expander_Active or GNATprove_Mode then
22251 Get_Index_Bounds (R, Lo, Hi);
22252 Force_Evaluation (Lo);
22253 Force_Evaluation (Hi);
22254 end if;
22255 end Process_Range_Expr_In_Decl;
22257 --------------------------------------
22258 -- Process_Real_Range_Specification --
22259 --------------------------------------
22261 procedure Process_Real_Range_Specification (Def : Node_Id) is
22262 Spec : constant Node_Id := Real_Range_Specification (Def);
22263 Lo : Node_Id;
22264 Hi : Node_Id;
22265 Err : Boolean := False;
22267 procedure Analyze_Bound (N : Node_Id);
22268 -- Analyze and check one bound
22270 -------------------
22271 -- Analyze_Bound --
22272 -------------------
22274 procedure Analyze_Bound (N : Node_Id) is
22275 begin
22276 Analyze_And_Resolve (N, Any_Real);
22278 if not Is_OK_Static_Expression (N) then
22279 Flag_Non_Static_Expr
22280 ("bound in real type definition is not static!", N);
22281 Err := True;
22282 end if;
22283 end Analyze_Bound;
22285 -- Start of processing for Process_Real_Range_Specification
22287 begin
22288 if Present (Spec) then
22289 Lo := Low_Bound (Spec);
22290 Hi := High_Bound (Spec);
22291 Analyze_Bound (Lo);
22292 Analyze_Bound (Hi);
22294 -- If error, clear away junk range specification
22296 if Err then
22297 Set_Real_Range_Specification (Def, Empty);
22298 end if;
22299 end if;
22300 end Process_Real_Range_Specification;
22302 ---------------------
22303 -- Process_Subtype --
22304 ---------------------
22306 function Process_Subtype
22307 (S : Node_Id;
22308 Related_Nod : Node_Id;
22309 Related_Id : Entity_Id := Empty;
22310 Suffix : Character := ' ') return Entity_Id
22312 procedure Check_Incomplete (T : Node_Id);
22313 -- Called to verify that an incomplete type is not used prematurely
22315 ----------------------
22316 -- Check_Incomplete --
22317 ----------------------
22319 procedure Check_Incomplete (T : Node_Id) is
22320 begin
22321 -- Ada 2005 (AI-412): Incomplete subtypes are legal
22323 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
22324 and then
22325 not (Ada_Version >= Ada_2005
22326 and then
22327 (Nkind (Parent (T)) = N_Subtype_Declaration
22328 or else (Nkind (Parent (T)) = N_Subtype_Indication
22329 and then Nkind (Parent (Parent (T))) =
22330 N_Subtype_Declaration)))
22331 then
22332 Error_Msg_N ("invalid use of type before its full declaration", T);
22333 end if;
22334 end Check_Incomplete;
22336 -- Local variables
22338 P : Node_Id;
22339 Def_Id : Entity_Id;
22340 Error_Node : Node_Id;
22341 Full_View_Id : Entity_Id;
22342 Subtype_Mark_Id : Entity_Id;
22344 May_Have_Null_Exclusion : Boolean;
22346 -- Start of processing for Process_Subtype
22348 begin
22349 -- Case of no constraints present
22351 if Nkind (S) /= N_Subtype_Indication then
22352 Find_Type (S);
22354 -- No way to proceed if the subtype indication is malformed. This
22355 -- will happen for example when the subtype indication in an object
22356 -- declaration is missing altogether and the expression is analyzed
22357 -- as if it were that indication.
22359 if not Is_Entity_Name (S) then
22360 return Any_Type;
22361 end if;
22363 Check_Incomplete (S);
22364 P := Parent (S);
22366 -- The following mirroring of assertion in Null_Exclusion_Present is
22367 -- ugly, can't we have a range, a static predicate or even a flag???
22369 May_Have_Null_Exclusion :=
22370 Present (P)
22371 and then
22372 Nkind (P) in N_Access_Definition
22373 | N_Access_Function_Definition
22374 | N_Access_Procedure_Definition
22375 | N_Access_To_Object_Definition
22376 | N_Allocator
22377 | N_Component_Definition
22378 | N_Derived_Type_Definition
22379 | N_Discriminant_Specification
22380 | N_Formal_Object_Declaration
22381 | N_Function_Specification
22382 | N_Object_Declaration
22383 | N_Object_Renaming_Declaration
22384 | N_Parameter_Specification
22385 | N_Subtype_Declaration;
22387 -- Ada 2005 (AI-231): Static check
22389 if Ada_Version >= Ada_2005
22390 and then May_Have_Null_Exclusion
22391 and then Null_Exclusion_Present (P)
22392 and then Nkind (P) /= N_Access_To_Object_Definition
22393 and then not Is_Access_Type (Entity (S))
22394 then
22395 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22396 end if;
22398 -- Create an Itype that is a duplicate of Entity (S) but with the
22399 -- null-exclusion attribute.
22401 if May_Have_Null_Exclusion
22402 and then Is_Access_Type (Entity (S))
22403 and then Null_Exclusion_Present (P)
22405 -- No need to check the case of an access to object definition.
22406 -- It is correct to define double not-null pointers.
22408 -- Example:
22409 -- type Not_Null_Int_Ptr is not null access Integer;
22410 -- type Acc is not null access Not_Null_Int_Ptr;
22412 and then Nkind (P) /= N_Access_To_Object_Definition
22413 then
22414 if Can_Never_Be_Null (Entity (S)) then
22415 case Nkind (Related_Nod) is
22416 when N_Full_Type_Declaration =>
22417 if Nkind (Type_Definition (Related_Nod))
22418 in N_Array_Type_Definition
22419 then
22420 Error_Node :=
22421 Subtype_Indication
22422 (Component_Definition
22423 (Type_Definition (Related_Nod)));
22424 else
22425 Error_Node :=
22426 Subtype_Indication (Type_Definition (Related_Nod));
22427 end if;
22429 when N_Subtype_Declaration =>
22430 Error_Node := Subtype_Indication (Related_Nod);
22432 when N_Object_Declaration =>
22433 Error_Node := Object_Definition (Related_Nod);
22435 when N_Component_Declaration =>
22436 Error_Node :=
22437 Subtype_Indication (Component_Definition (Related_Nod));
22439 when N_Allocator =>
22440 Error_Node := Expression (Related_Nod);
22442 when others =>
22443 pragma Assert (False);
22444 Error_Node := Related_Nod;
22445 end case;
22447 Error_Msg_NE
22448 ("`NOT NULL` not allowed (& already excludes null)",
22449 Error_Node,
22450 Entity (S));
22451 end if;
22453 Set_Etype (S,
22454 Create_Null_Excluding_Itype
22455 (T => Entity (S),
22456 Related_Nod => P));
22457 Set_Entity (S, Etype (S));
22458 end if;
22460 return Entity (S);
22462 -- Case of constraint present, so that we have an N_Subtype_Indication
22463 -- node (this node is created only if constraints are present).
22465 else
22466 Find_Type (Subtype_Mark (S));
22468 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22469 and then not
22470 (Nkind (Parent (S)) = N_Subtype_Declaration
22471 and then Is_Itype (Defining_Identifier (Parent (S))))
22472 then
22473 Check_Incomplete (Subtype_Mark (S));
22474 end if;
22476 P := Parent (S);
22477 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22479 -- Explicit subtype declaration case
22481 if Nkind (P) = N_Subtype_Declaration then
22482 Def_Id := Defining_Identifier (P);
22484 -- Explicit derived type definition case
22486 elsif Nkind (P) = N_Derived_Type_Definition then
22487 Def_Id := Defining_Identifier (Parent (P));
22489 -- Implicit case, the Def_Id must be created as an implicit type.
22490 -- The one exception arises in the case of concurrent types, array
22491 -- and access types, where other subsidiary implicit types may be
22492 -- created and must appear before the main implicit type. In these
22493 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22494 -- has not yet been called to create Def_Id.
22496 else
22497 if Is_Array_Type (Subtype_Mark_Id)
22498 or else Is_Concurrent_Type (Subtype_Mark_Id)
22499 or else Is_Access_Type (Subtype_Mark_Id)
22500 then
22501 Def_Id := Empty;
22503 -- For the other cases, we create a new unattached Itype,
22504 -- and set the indication to ensure it gets attached later.
22506 else
22507 Def_Id :=
22508 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22509 end if;
22510 end if;
22512 -- If the kind of constraint is invalid for this kind of type,
22513 -- then give an error, and then pretend no constraint was given.
22515 if not Is_Valid_Constraint_Kind
22516 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22517 then
22518 Error_Msg_N
22519 ("incorrect constraint for this kind of type", Constraint (S));
22521 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22523 -- Set Ekind of orphan itype, to prevent cascaded errors
22525 if Present (Def_Id) then
22526 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22527 end if;
22529 -- Make recursive call, having got rid of the bogus constraint
22531 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22532 end if;
22534 -- Remaining processing depends on type. Select on Base_Type kind to
22535 -- ensure getting to the concrete type kind in the case of a private
22536 -- subtype (needed when only doing semantic analysis).
22538 case Ekind (Base_Type (Subtype_Mark_Id)) is
22539 when Access_Kind =>
22541 -- If this is a constraint on a class-wide type, discard it.
22542 -- There is currently no way to express a partial discriminant
22543 -- constraint on a type with unknown discriminants. This is
22544 -- a pathology that the ACATS wisely decides not to test.
22546 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22547 if Comes_From_Source (S) then
22548 Error_Msg_N
22549 ("constraint on class-wide type ignored??",
22550 Constraint (S));
22551 end if;
22553 if Nkind (P) = N_Subtype_Declaration then
22554 Set_Subtype_Indication (P,
22555 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22556 end if;
22558 return Subtype_Mark_Id;
22559 end if;
22561 Constrain_Access (Def_Id, S, Related_Nod);
22563 if Expander_Active
22564 and then Is_Itype (Designated_Type (Def_Id))
22565 and then Nkind (Related_Nod) = N_Subtype_Declaration
22566 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22567 then
22568 Build_Itype_Reference
22569 (Designated_Type (Def_Id), Related_Nod);
22570 end if;
22572 when Array_Kind =>
22573 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22575 when Decimal_Fixed_Point_Kind =>
22576 Constrain_Decimal (Def_Id, S);
22578 when Enumeration_Kind =>
22579 Constrain_Enumeration (Def_Id, S);
22581 when Ordinary_Fixed_Point_Kind =>
22582 Constrain_Ordinary_Fixed (Def_Id, S);
22584 when Float_Kind =>
22585 Constrain_Float (Def_Id, S);
22587 when Integer_Kind =>
22588 Constrain_Integer (Def_Id, S);
22590 when Class_Wide_Kind
22591 | E_Incomplete_Type
22592 | E_Record_Subtype
22593 | E_Record_Type
22595 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22597 if Ekind (Def_Id) = E_Incomplete_Type then
22598 Set_Private_Dependents (Def_Id, New_Elmt_List);
22599 end if;
22601 when Private_Kind =>
22603 -- A private type with unknown discriminants may be completed
22604 -- by an unconstrained array type.
22606 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22607 and then Present (Full_View (Subtype_Mark_Id))
22608 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22609 then
22610 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22612 -- ... but more commonly is completed by a discriminated record
22613 -- type.
22615 else
22616 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22617 end if;
22619 -- The base type may be private but Def_Id may be a full view
22620 -- in an instance.
22622 if Is_Private_Type (Def_Id) then
22623 Set_Private_Dependents (Def_Id, New_Elmt_List);
22624 end if;
22626 -- In case of an invalid constraint prevent further processing
22627 -- since the type constructed is missing expected fields.
22629 if Etype (Def_Id) = Any_Type then
22630 return Def_Id;
22631 end if;
22633 -- If the full view is that of a task with discriminants,
22634 -- we must constrain both the concurrent type and its
22635 -- corresponding record type. Otherwise we will just propagate
22636 -- the constraint to the full view, if available.
22638 if Present (Full_View (Subtype_Mark_Id))
22639 and then Has_Discriminants (Subtype_Mark_Id)
22640 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22641 then
22642 Full_View_Id :=
22643 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22645 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22646 Constrain_Concurrent (Full_View_Id, S,
22647 Related_Nod, Related_Id, Suffix);
22648 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22649 Set_Full_View (Def_Id, Full_View_Id);
22651 -- Introduce an explicit reference to the private subtype,
22652 -- to prevent scope anomalies in gigi if first use appears
22653 -- in a nested context, e.g. a later function body.
22654 -- Should this be generated in other contexts than a full
22655 -- type declaration?
22657 if Is_Itype (Def_Id)
22658 and then
22659 Nkind (Parent (P)) = N_Full_Type_Declaration
22660 then
22661 Build_Itype_Reference (Def_Id, Parent (P));
22662 end if;
22664 else
22665 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22666 end if;
22668 when Concurrent_Kind =>
22669 Constrain_Concurrent (Def_Id, S,
22670 Related_Nod, Related_Id, Suffix);
22672 when others =>
22673 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22674 end case;
22676 -- Size, Alignment, Representation aspects and Convention are always
22677 -- inherited from the base type.
22679 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22680 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22681 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22683 -- The anonymous subtype created for the subtype indication
22684 -- inherits the predicates of the parent.
22686 if Has_Predicates (Subtype_Mark_Id) then
22687 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22689 -- Indicate where the predicate function may be found
22691 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22692 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22693 end if;
22694 end if;
22696 return Def_Id;
22697 end if;
22698 end Process_Subtype;
22700 -----------------------------
22701 -- Record_Type_Declaration --
22702 -----------------------------
22704 procedure Record_Type_Declaration
22705 (T : Entity_Id;
22706 N : Node_Id;
22707 Prev : Entity_Id)
22709 Def : constant Node_Id := Type_Definition (N);
22710 Is_Tagged : Boolean;
22711 Tag_Comp : Entity_Id;
22713 begin
22714 -- These flags must be initialized before calling Process_Discriminants
22715 -- because this routine makes use of them.
22717 Mutate_Ekind (T, E_Record_Type);
22718 Set_Etype (T, T);
22719 Reinit_Size_Align (T);
22720 Set_Interfaces (T, No_Elist);
22721 Set_Stored_Constraint (T, No_Elist);
22722 Set_Default_SSO (T);
22723 Set_No_Reordering (T, No_Component_Reordering);
22725 -- Normal case
22727 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22728 -- The flag Is_Tagged_Type might have already been set by
22729 -- Find_Type_Name if it detected an error for declaration T. This
22730 -- arises in the case of private tagged types where the full view
22731 -- omits the word tagged.
22733 Is_Tagged :=
22734 Tagged_Present (Def)
22735 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22737 Set_Is_Limited_Record (T, Limited_Present (Def));
22739 if Is_Tagged then
22740 Set_Is_Tagged_Type (T, True);
22741 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22742 end if;
22744 -- Type is abstract if full declaration carries keyword, or if
22745 -- previous partial view did.
22747 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22748 or else Abstract_Present (Def));
22750 else
22751 Is_Tagged := True;
22752 Analyze_Interface_Declaration (T, Def);
22754 if Present (Discriminant_Specifications (N)) then
22755 Error_Msg_N
22756 ("interface types cannot have discriminants",
22757 Defining_Identifier
22758 (First (Discriminant_Specifications (N))));
22759 end if;
22760 end if;
22762 -- First pass: if there are self-referential access components,
22763 -- create the required anonymous access type declarations, and if
22764 -- need be an incomplete type declaration for T itself.
22766 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22768 if Ada_Version >= Ada_2005
22769 and then Present (Interface_List (Def))
22770 then
22771 Check_Interfaces (N, Def);
22773 declare
22774 Ifaces_List : Elist_Id;
22776 begin
22777 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22778 -- already in the parents.
22780 Collect_Interfaces
22781 (T => T,
22782 Ifaces_List => Ifaces_List,
22783 Exclude_Parents => True);
22785 Set_Interfaces (T, Ifaces_List);
22786 end;
22787 end if;
22789 -- Records constitute a scope for the component declarations within.
22790 -- The scope is created prior to the processing of these declarations.
22791 -- Discriminants are processed first, so that they are visible when
22792 -- processing the other components. The Ekind of the record type itself
22793 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22795 -- Enter record scope
22797 Push_Scope (T);
22799 -- If an incomplete or private type declaration was already given for
22800 -- the type, then this scope already exists, and the discriminants have
22801 -- been declared within. We must verify that the full declaration
22802 -- matches the incomplete one.
22804 Check_Or_Process_Discriminants (N, T, Prev);
22806 Set_Is_Constrained (T, not Has_Discriminants (T));
22807 Set_Has_Delayed_Freeze (T, True);
22809 -- For tagged types add a manually analyzed component corresponding
22810 -- to the component _tag, the corresponding piece of tree will be
22811 -- expanded as part of the freezing actions if it is not a CPP_Class.
22813 if Is_Tagged then
22815 -- Do not add the tag unless we are in expansion mode
22817 if Expander_Active then
22818 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22819 Enter_Name (Tag_Comp);
22821 Mutate_Ekind (Tag_Comp, E_Component);
22822 Set_Is_Tag (Tag_Comp);
22823 Set_Is_Aliased (Tag_Comp);
22824 Set_Is_Independent (Tag_Comp);
22825 Set_Etype (Tag_Comp, RTE (RE_Tag));
22826 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22827 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22828 Reinit_Component_Location (Tag_Comp);
22830 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22831 -- implemented interfaces.
22833 if Has_Interfaces (T) then
22834 Add_Interface_Tag_Components (N, T);
22835 end if;
22836 end if;
22838 Make_Class_Wide_Type (T);
22839 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22840 end if;
22842 -- We must suppress range checks when processing record components in
22843 -- the presence of discriminants, since we don't want spurious checks to
22844 -- be generated during their analysis, but Suppress_Range_Checks flags
22845 -- must be reset the after processing the record definition.
22847 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22848 -- couldn't we just use the normal range check suppression method here.
22849 -- That would seem cleaner ???
22851 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22852 Set_Kill_Range_Checks (T, True);
22853 Record_Type_Definition (Def, Prev);
22854 Set_Kill_Range_Checks (T, False);
22855 else
22856 Record_Type_Definition (Def, Prev);
22857 end if;
22859 -- Exit from record scope
22861 End_Scope;
22863 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22864 -- the implemented interfaces and associate them an aliased entity.
22866 if Is_Tagged
22867 and then not Is_Empty_List (Interface_List (Def))
22868 then
22869 Derive_Progenitor_Subprograms (T, T);
22870 end if;
22872 Check_Function_Writable_Actuals (N);
22873 end Record_Type_Declaration;
22875 ----------------------------
22876 -- Record_Type_Definition --
22877 ----------------------------
22879 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22880 Component : Entity_Id;
22881 Ctrl_Components : Boolean := False;
22882 Final_Storage_Only : Boolean;
22883 T : Entity_Id;
22885 begin
22886 if Ekind (Prev_T) = E_Incomplete_Type then
22887 T := Full_View (Prev_T);
22888 else
22889 T := Prev_T;
22890 end if;
22892 Set_Is_Not_Self_Hidden (T);
22894 Final_Storage_Only := not Is_Controlled (T);
22896 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22897 -- type declaration.
22899 if Parent_Kind (Def) = N_Derived_Type_Definition
22900 and then Limited_Present (Parent (Def))
22901 then
22902 Set_Is_Limited_Record (T);
22903 end if;
22905 -- If the component list of a record type is defined by the reserved
22906 -- word null and there is no discriminant part, then the record type has
22907 -- no components and all records of the type are null records (RM 3.7)
22908 -- This procedure is also called to process the extension part of a
22909 -- record extension, in which case the current scope may have inherited
22910 -- components.
22912 if Present (Def)
22913 and then Present (Component_List (Def))
22914 and then not Null_Present (Component_List (Def))
22915 then
22916 Analyze_Declarations (Component_Items (Component_List (Def)));
22918 if Present (Variant_Part (Component_List (Def))) then
22919 Analyze (Variant_Part (Component_List (Def)));
22920 end if;
22921 end if;
22923 -- After completing the semantic analysis of the record definition,
22924 -- record components, both new and inherited, are accessible. Set their
22925 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22926 -- whose Ekind may be void.
22928 Component := First_Entity (Current_Scope);
22929 while Present (Component) loop
22930 if Ekind (Component) = E_Void
22931 and then not Is_Itype (Component)
22932 then
22933 Mutate_Ekind (Component, E_Component);
22934 Reinit_Component_Location (Component);
22935 Set_Is_Not_Self_Hidden (Component);
22936 end if;
22938 Propagate_Concurrent_Flags (T, Etype (Component));
22940 if Ekind (Component) /= E_Component then
22941 null;
22943 -- Do not set Has_Controlled_Component on a class-wide equivalent
22944 -- type. See Make_CW_Equivalent_Type.
22946 elsif not Is_Class_Wide_Equivalent_Type (T)
22947 and then (Has_Controlled_Component (Etype (Component))
22948 or else (Chars (Component) /= Name_uParent
22949 and then Is_Controlled (Etype (Component))))
22950 then
22951 Set_Has_Controlled_Component (T, True);
22952 Final_Storage_Only :=
22953 Final_Storage_Only
22954 and then Finalize_Storage_Only (Etype (Component));
22955 Ctrl_Components := True;
22956 end if;
22958 Next_Entity (Component);
22959 end loop;
22961 -- A Type is Finalize_Storage_Only only if all its controlled components
22962 -- are also.
22964 if Ctrl_Components then
22965 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22966 end if;
22968 -- Place reference to end record on the proper entity, which may
22969 -- be a partial view.
22971 if Present (Def) then
22972 Process_End_Label (Def, 'e', Prev_T);
22973 end if;
22974 end Record_Type_Definition;
22976 ---------------------------
22977 -- Replace_Discriminants --
22978 ---------------------------
22980 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22981 function Process (N : Node_Id) return Traverse_Result;
22983 -------------
22984 -- Process --
22985 -------------
22987 function Process (N : Node_Id) return Traverse_Result is
22988 Comp : Entity_Id;
22990 begin
22991 if Nkind (N) = N_Discriminant_Specification then
22992 Comp := First_Discriminant (Typ);
22993 while Present (Comp) loop
22994 if Original_Record_Component (Comp) = Defining_Identifier (N)
22995 or else Chars (Comp) = Chars (Defining_Identifier (N))
22996 then
22997 Set_Defining_Identifier (N, Comp);
22998 exit;
22999 end if;
23001 Next_Discriminant (Comp);
23002 end loop;
23004 elsif Nkind (N) = N_Variant_Part then
23005 Comp := First_Discriminant (Typ);
23006 while Present (Comp) loop
23007 if Original_Record_Component (Comp) = Entity (Name (N))
23008 or else Chars (Comp) = Chars (Name (N))
23009 then
23010 -- Make sure to preserve the type coming from the parent on
23011 -- the Name, even if the subtype of the discriminant can be
23012 -- constrained, so that discrete choices inherited from the
23013 -- parent in the variant part are not flagged as violating
23014 -- the constraints of the subtype.
23016 declare
23017 Typ : constant Entity_Id := Etype (Name (N));
23018 begin
23019 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
23020 Set_Etype (Name (N), Typ);
23021 end;
23022 exit;
23023 end if;
23025 Next_Discriminant (Comp);
23026 end loop;
23027 end if;
23029 return OK;
23030 end Process;
23032 procedure Replace is new Traverse_Proc (Process);
23034 -- Start of processing for Replace_Discriminants
23036 begin
23037 Replace (Decl);
23038 end Replace_Discriminants;
23040 -------------------------------
23041 -- Set_Completion_Referenced --
23042 -------------------------------
23044 procedure Set_Completion_Referenced (E : Entity_Id) is
23045 begin
23046 -- If in main unit, mark entity that is a completion as referenced,
23047 -- warnings go on the partial view when needed.
23049 if In_Extended_Main_Source_Unit (E) then
23050 Set_Referenced (E);
23051 end if;
23052 end Set_Completion_Referenced;
23054 ---------------------
23055 -- Set_Default_SSO --
23056 ---------------------
23058 procedure Set_Default_SSO (T : Entity_Id) is
23059 begin
23060 case Opt.Default_SSO is
23061 when ' ' =>
23062 null;
23063 when 'L' =>
23064 Set_SSO_Set_Low_By_Default (T, True);
23065 when 'H' =>
23066 Set_SSO_Set_High_By_Default (T, True);
23067 when others =>
23068 raise Program_Error;
23069 end case;
23070 end Set_Default_SSO;
23072 ---------------------
23073 -- Set_Fixed_Range --
23074 ---------------------
23076 -- The range for fixed-point types is complicated by the fact that we
23077 -- do not know the exact end points at the time of the declaration. This
23078 -- is true for three reasons:
23080 -- A size clause may affect the fudging of the end-points.
23081 -- A small clause may affect the values of the end-points.
23082 -- We try to include the end-points if it does not affect the size.
23084 -- This means that the actual end-points must be established at the
23085 -- point when the type is frozen. Meanwhile, we first narrow the range
23086 -- as permitted (so that it will fit if necessary in a small specified
23087 -- size), and then build a range subtree with these narrowed bounds.
23088 -- Set_Fixed_Range constructs the range from real literal values, and
23089 -- sets the range as the Scalar_Range of the given fixed-point type entity.
23091 -- The parent of this range is set to point to the entity so that it is
23092 -- properly hooked into the tree (unlike normal Scalar_Range entries for
23093 -- other scalar types, which are just pointers to the range in the
23094 -- original tree, this would otherwise be an orphan).
23096 -- The tree is left unanalyzed. When the type is frozen, the processing
23097 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
23098 -- analyzed, and uses this as an indication that it should complete
23099 -- work on the range (it will know the final small and size values).
23101 procedure Set_Fixed_Range
23102 (E : Entity_Id;
23103 Loc : Source_Ptr;
23104 Lo : Ureal;
23105 Hi : Ureal)
23107 S : constant Node_Id :=
23108 Make_Range (Loc,
23109 Low_Bound => Make_Real_Literal (Loc, Lo),
23110 High_Bound => Make_Real_Literal (Loc, Hi));
23111 begin
23112 Set_Scalar_Range (E, S);
23113 Set_Parent (S, E);
23115 -- Before the freeze point, the bounds of a fixed point are universal
23116 -- and carry the corresponding type.
23118 Set_Etype (Low_Bound (S), Universal_Real);
23119 Set_Etype (High_Bound (S), Universal_Real);
23120 end Set_Fixed_Range;
23122 ----------------------------------
23123 -- Set_Scalar_Range_For_Subtype --
23124 ----------------------------------
23126 procedure Set_Scalar_Range_For_Subtype
23127 (Def_Id : Entity_Id;
23128 R : Node_Id;
23129 Subt : Entity_Id)
23131 Kind : constant Entity_Kind := Ekind (Def_Id);
23133 begin
23134 -- Defend against previous error
23136 if Nkind (R) = N_Error then
23137 return;
23138 end if;
23140 Set_Scalar_Range (Def_Id, R);
23142 -- We need to link the range into the tree before resolving it so
23143 -- that types that are referenced, including importantly the subtype
23144 -- itself, are properly frozen (Freeze_Expression requires that the
23145 -- expression be properly linked into the tree). Of course if it is
23146 -- already linked in, then we do not disturb the current link.
23148 if No (Parent (R)) then
23149 Set_Parent (R, Def_Id);
23150 end if;
23152 -- Reset the kind of the subtype during analysis of the range, to
23153 -- catch possible premature use in the bounds themselves.
23155 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
23156 pragma Assert (Ekind (Def_Id) = Kind);
23157 end Set_Scalar_Range_For_Subtype;
23159 --------------------------------------------------------
23160 -- Set_Stored_Constraint_From_Discriminant_Constraint --
23161 --------------------------------------------------------
23163 procedure Set_Stored_Constraint_From_Discriminant_Constraint
23164 (E : Entity_Id)
23166 begin
23167 -- Make sure set if encountered during Expand_To_Stored_Constraint
23169 Set_Stored_Constraint (E, No_Elist);
23171 -- Give it the right value
23173 if Is_Constrained (E) and then Has_Discriminants (E) then
23174 Set_Stored_Constraint (E,
23175 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
23176 end if;
23177 end Set_Stored_Constraint_From_Discriminant_Constraint;
23179 -------------------------------------
23180 -- Signed_Integer_Type_Declaration --
23181 -------------------------------------
23183 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
23184 Implicit_Base : Entity_Id;
23185 Base_Typ : Entity_Id;
23186 Lo_Val : Uint;
23187 Hi_Val : Uint;
23188 Errs : Boolean := False;
23189 Lo : Node_Id;
23190 Hi : Node_Id;
23192 function Can_Derive_From (E : Entity_Id) return Boolean;
23193 -- Determine whether given bounds allow derivation from specified type
23195 procedure Check_Bound (Expr : Node_Id);
23196 -- Check bound to make sure it is integral and static. If not, post
23197 -- appropriate error message and set Errs flag
23199 ---------------------
23200 -- Can_Derive_From --
23201 ---------------------
23203 -- Note we check both bounds against both end values, to deal with
23204 -- strange types like ones with a range of 0 .. -12341234.
23206 function Can_Derive_From (E : Entity_Id) return Boolean is
23207 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
23208 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
23209 begin
23210 return Lo <= Lo_Val and then Lo_Val <= Hi
23211 and then
23212 Lo <= Hi_Val and then Hi_Val <= Hi;
23213 end Can_Derive_From;
23215 -----------------
23216 -- Check_Bound --
23217 -----------------
23219 procedure Check_Bound (Expr : Node_Id) is
23220 begin
23221 -- If a range constraint is used as an integer type definition, each
23222 -- bound of the range must be defined by a static expression of some
23223 -- integer type, but the two bounds need not have the same integer
23224 -- type (Negative bounds are allowed.) (RM 3.5.4)
23226 if not Is_Integer_Type (Etype (Expr)) then
23227 Error_Msg_N
23228 ("integer type definition bounds must be of integer type", Expr);
23229 Errs := True;
23231 elsif not Is_OK_Static_Expression (Expr) then
23232 Flag_Non_Static_Expr
23233 ("non-static expression used for integer type bound!", Expr);
23234 Errs := True;
23236 -- Otherwise the bounds are folded into literals
23238 elsif Is_Entity_Name (Expr) then
23239 Fold_Uint (Expr, Expr_Value (Expr), True);
23240 end if;
23241 end Check_Bound;
23243 -- Start of processing for Signed_Integer_Type_Declaration
23245 begin
23246 -- Create an anonymous base type
23248 Implicit_Base :=
23249 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
23251 -- Analyze and check the bounds, they can be of any integer type
23253 Lo := Low_Bound (Def);
23254 Hi := High_Bound (Def);
23256 -- Arbitrarily use Integer as the type if either bound had an error
23258 if Hi = Error or else Lo = Error then
23259 Base_Typ := Any_Integer;
23260 Set_Error_Posted (T, True);
23261 Errs := True;
23263 -- Here both bounds are OK expressions
23265 else
23266 Analyze_And_Resolve (Lo, Any_Integer);
23267 Analyze_And_Resolve (Hi, Any_Integer);
23269 Check_Bound (Lo);
23270 Check_Bound (Hi);
23272 if Errs then
23273 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23274 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23275 end if;
23277 -- Find type to derive from
23279 Lo_Val := Expr_Value (Lo);
23280 Hi_Val := Expr_Value (Hi);
23282 if Can_Derive_From (Standard_Short_Short_Integer) then
23283 Base_Typ := Base_Type (Standard_Short_Short_Integer);
23285 elsif Can_Derive_From (Standard_Short_Integer) then
23286 Base_Typ := Base_Type (Standard_Short_Integer);
23288 elsif Can_Derive_From (Standard_Integer) then
23289 Base_Typ := Base_Type (Standard_Integer);
23291 elsif Can_Derive_From (Standard_Long_Integer) then
23292 Base_Typ := Base_Type (Standard_Long_Integer);
23294 elsif Can_Derive_From (Standard_Long_Long_Integer) then
23295 Check_Restriction (No_Long_Long_Integers, Def);
23296 Base_Typ := Base_Type (Standard_Long_Long_Integer);
23298 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
23299 Check_Restriction (No_Long_Long_Integers, Def);
23300 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23302 else
23303 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
23304 Error_Msg_N ("integer type definition bounds out of range", Def);
23305 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
23306 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
23307 end if;
23308 end if;
23310 -- Set the type of the bounds to the implicit base: we cannot set it to
23311 -- the new type, because this would be a forward reference for the code
23312 -- generator and, if the original type is user-defined, this could even
23313 -- lead to spurious semantic errors. Furthermore we do not set it to be
23314 -- universal, because this could make it much larger than needed here.
23316 if not Errs then
23317 Set_Etype (Lo, Implicit_Base);
23318 Set_Etype (Hi, Implicit_Base);
23319 end if;
23321 -- Complete both implicit base and declared first subtype entities. The
23322 -- inheritance of the rep item chain ensures that SPARK-related pragmas
23323 -- are not clobbered when the signed integer type acts as a full view of
23324 -- a private type.
23326 Set_Etype (Implicit_Base, Base_Typ);
23327 Set_Size_Info (Implicit_Base, Base_Typ);
23328 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
23329 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
23330 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
23332 Mutate_Ekind (T, E_Signed_Integer_Subtype);
23333 Set_Etype (T, Implicit_Base);
23334 Set_Size_Info (T, Implicit_Base);
23335 Inherit_Rep_Item_Chain (T, Implicit_Base);
23336 Set_Scalar_Range (T, Def);
23337 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
23338 Set_Is_Constrained (T);
23339 end Signed_Integer_Type_Declaration;
23341 end Sem_Ch3;